audio_master/audio_master/
stream.rs

1use num_traits::Float;
2
3use crate::{
4    audio_buffer::{AudioBufferImpl, AudioBufferInterleavedImpl, AudioChannelLayout},
5    audio_math::AudioFilterImpl,
6    consts::{MAX_RESAMPLE_SPEED, MAX_SAMPLE_RATE, MAX_VOLUME, MIN_RESAMPLE_SPEED, MIN_SAMPLE_RATE, MIN_VOLUME},
7    float_type::FloatType,
8    resampler::{ResamplerImpl, ResamplerQuality},
9};
10
11use super::audio_master::{AudioProcessor, AudioStream, AudioStreamImpl, AudioStreamPrivateImpl};
12
13impl<T: FloatType + Float + 'static> AudioStreamImpl<T> for AudioStream<T> {
14    fn use_normalization(&mut self, norm: bool) {
15        unsafe {
16            let mut inner = self.inner.write().unwrap_unchecked();
17            inner.use_normalization = norm;
18        }
19    }
20    fn is_using_normalization(&self) -> bool {
21        unsafe {
22            let inner = self.inner.read().unwrap_unchecked();
23            return inner.use_normalization;
24        }
25    }
26
27    fn get_timestamp(&self) -> f64 {
28        unsafe {
29            let inner = self.inner.read().unwrap_unchecked();
30            return inner.timestamp;
31        }
32    }
33
34    fn get_resample_quality(&self) -> ResamplerQuality {
35        unsafe {
36            let inner = self.inner.read().unwrap_unchecked();
37            return inner.resampler.get_quality();
38        }
39    }
40
41    fn set_resample_quality(&mut self, quality: ResamplerQuality) {
42        unsafe {
43            let mut inner = self.inner.write().unwrap_unchecked();
44        }
45    }
46
47    fn get_speed(&self) -> f32 {
48        unsafe {
49            let inner = self.inner.read().unwrap_unchecked();
50            return inner.speed;
51        }
52    }
53
54    fn set_speed(&mut self, speed: f32) {
55        unsafe {
56            let mut inner = self.inner.write().unwrap_unchecked();
57            inner.speed = speed.clamp(MIN_RESAMPLE_SPEED, MAX_RESAMPLE_SPEED)
58        }
59    }
60
61    fn get_volume(&self) -> f32 {
62        unsafe {
63            let inner = self.inner.read().unwrap_unchecked();
64            return inner.volume;
65        }
66    }
67
68    fn set_volume(&self, volume: f32) {
69        unsafe {
70            let mut inner = self.inner.write().unwrap_unchecked();
71            inner.volume = volume.clamp(MIN_VOLUME, MAX_VOLUME);
72        }
73    }
74
75    fn use_filter(&mut self, filter: bool) {
76        unsafe {
77            let mut inner = self.inner.write().unwrap_unchecked();
78            inner.use_filter = filter;
79        }
80    }
81
82    fn is_using_filter(&self) -> bool {
83        unsafe {
84            let inner = self.inner.read().unwrap_unchecked();
85            return inner.use_filter;
86        }
87    }
88
89    fn get_low_cut(&self) -> f32 {
90        unsafe {
91            let inner = self.inner.read().unwrap_unchecked();
92            return inner.filter.get_low_cut();
93        }
94    }
95
96    fn set_low_cut(&mut self, low_cut: f32) {
97        unsafe {
98            let mut inner = self.inner.write().unwrap_unchecked();
99            inner.filter.set_low_cut(low_cut);
100        }
101    }
102
103    fn get_high_cut(&self) -> f32 {
104        unsafe {
105            let inner = self.inner.read().unwrap_unchecked();
106            return inner.filter.get_high_cut();
107        }
108    }
109
110    fn set_high_cut(&mut self, high_cut: f32) {
111        unsafe {
112            let mut inner = self.inner.write().unwrap_unchecked();
113            inner.filter.set_high_cut(high_cut);
114        }
115    }
116
117    fn get_sample_rate(&self) -> f32 {
118        unsafe {
119            let inner = self.inner.read().unwrap_unchecked();
120            return inner.sample_rate;
121        }
122    }
123
124    fn set_sample_rate(&mut self, sample_rate: f32) {
125        unsafe {
126            let mut inner = self.inner.write().unwrap_unchecked();
127            inner.sample_rate = sample_rate.clamp(MIN_SAMPLE_RATE, MAX_SAMPLE_RATE);
128        }
129    }
130
131    fn get_processor(&self) -> AudioProcessor<T> {
132        unsafe {
133            let inner = self.inner.read().unwrap_unchecked();
134            return inner.processor.clone();
135        }
136    }
137
138    fn resume(&mut self) {
139        unsafe {
140            let mut inner = self.inner.write().unwrap_unchecked();
141            inner.is_paused = false;
142        }
143    }
144
145    fn pause(&mut self) {
146        unsafe {
147            let mut inner = self.inner.write().unwrap_unchecked();
148            inner.is_paused = true;
149        }
150    }
151
152    fn get_channel_layout(&self) -> AudioChannelLayout {
153        unsafe {
154            let inner = self.inner.read().unwrap_unchecked();
155            return inner.channel_layout;
156        }
157    }
158
159    fn set_channel_layout(&mut self, layout: AudioChannelLayout) {
160        unsafe {
161            let mut inner = self.inner.write().unwrap_unchecked();
162
163            if inner.channel_layout == layout {
164                return;
165            }
166            inner.channel_layout = layout;
167
168            inner.buffer.set_channel_layout(layout);
169            inner.cb_buffer.set_channel_layout(layout);
170            inner.buffer_resampled.set_channel_layout(layout);
171            inner.filter.set_channel_layout(layout);
172            inner.resampler.set_channel_layout(layout);
173
174            inner.clear_buffers();
175        }
176    }
177}