1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//! Produces audio in a 3D space.

mod distances;
mod handle;
mod settings;

pub use distances::*;
pub use handle::*;
pub use settings::*;

use std::sync::{
	atomic::{AtomicBool, Ordering},
	Arc,
};

use atomic_arena::Key;
use glam::Vec3;

use crate::{
	clock::clock_info::ClockInfoProvider,
	dsp::Frame,
	modulator::value_provider::ModulatorValueProvider,
	tween::{Easing, Parameter, Tween, Value},
};

use super::scene::SpatialSceneId;

pub(crate) struct Emitter {
	shared: Arc<EmitterShared>,
	position: Parameter<Vec3>,
	distances: EmitterDistances,
	attenuation_function: Option<Easing>,
	enable_spatialization: bool,
	persist_until_sounds_finish: bool,
	input: Frame,
	used_this_frame: bool,
	finished: bool,
}

impl Emitter {
	pub fn new(position: Value<Vec3>, settings: EmitterSettings) -> Self {
		Self {
			shared: Arc::new(EmitterShared::new()),
			position: Parameter::new(position, Vec3::ZERO),
			distances: settings.distances,
			attenuation_function: settings.attenuation_function,
			enable_spatialization: settings.enable_spatialization,
			persist_until_sounds_finish: settings.persist_until_sounds_finish,
			input: Frame::ZERO,
			used_this_frame: false,
			finished: false,
		}
	}

	pub fn output(&self) -> Frame {
		self.input
	}

	pub fn shared(&self) -> Arc<EmitterShared> {
		self.shared.clone()
	}

	pub fn position(&self) -> Vec3 {
		self.position.value()
	}

	pub fn distances(&self) -> EmitterDistances {
		self.distances
	}

	pub fn attenuation_function(&self) -> Option<Easing> {
		self.attenuation_function
	}

	pub fn enable_spatialization(&self) -> bool {
		self.enable_spatialization
	}

	pub fn finished(&self) -> bool {
		self.finished
	}

	pub fn set_position(&mut self, position: Value<Vec3>, tween: Tween) {
		self.position.set(position, tween);
	}

	pub fn add_input(&mut self, input: Frame) {
		self.input += input;
		self.used_this_frame = true;
	}

	pub fn after_process(&mut self) {
		if self.should_be_finished() {
			self.finished = true;
		}
		self.input = Frame::ZERO;
		self.used_this_frame = false;
	}

	pub fn update(
		&mut self,
		dt: f64,
		clock_info_provider: &ClockInfoProvider,
		modulator_value_provider: &ModulatorValueProvider,
	) {
		self.position
			.update(dt, clock_info_provider, modulator_value_provider);
	}

	fn should_be_finished(&self) -> bool {
		if !self.shared.is_marked_for_removal() {
			return false;
		}
		if self.persist_until_sounds_finish && self.used_this_frame {
			return false;
		}
		true
	}
}

/// A unique identifier for an emitter.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct EmitterId {
	pub(crate) key: Key,
	pub(crate) scene_id: SpatialSceneId,
}

impl EmitterId {
	/// Returns the ID of the spatial scene this emitter belongs to.
	pub fn scene(&self) -> SpatialSceneId {
		self.scene_id
	}
}

pub(crate) struct EmitterShared {
	removed: AtomicBool,
}

impl EmitterShared {
	pub fn new() -> Self {
		Self {
			removed: AtomicBool::new(false),
		}
	}

	pub fn is_marked_for_removal(&self) -> bool {
		self.removed.load(Ordering::SeqCst)
	}

	pub fn mark_for_removal(&self) {
		self.removed.store(true, Ordering::SeqCst);
	}
}