Skip to content

Commit 6b53f9b

Browse files
committed
AnalyserNode: don't use RwLock for interior mutability
1 parent 45b7afc commit 6b53f9b

File tree

3 files changed

+36
-54
lines changed

3 files changed

+36
-54
lines changed

examples/analyser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn main() {
2525
..AudioContextOptions::default()
2626
});
2727

28-
let analyser = context.create_analyser();
28+
let mut analyser = context.create_analyser();
2929
analyser.connect(&context.destination());
3030

3131
let osc = context.create_oscillator();

showcase/mic_playback/src/main.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// `cargo run --release --example mic_playback`
33

44
use web_audio_api::context::{AudioContext, AudioContextRegistration, BaseAudioContext};
5-
use web_audio_api::media_devices;
5+
use web_audio_api::media_devices::{self, MediaStreamConstraints};
66
use web_audio_api::node::BiquadFilterType;
77
use web_audio_api::node::{
88
AnalyserNode, AudioBufferSourceNode, AudioNode, AudioScheduledSourceNode, BiquadFilterNode,
@@ -12,7 +12,7 @@ use web_audio_api::render::{AudioParamValues, AudioProcessor, AudioRenderQuantum
1212
use web_audio_api::AudioBuffer;
1313

1414
use std::io::{stdin, stdout, Write};
15-
use std::sync::Arc;
15+
use std::sync::{Arc, Mutex};
1616

1717
use crossbeam_channel::{self, Receiver, Sender};
1818

@@ -223,12 +223,12 @@ fn draw_plot(stdout: &mut termion::raw::RawTerminal<std::io::Stdout>, plot: Stri
223223
}
224224

225225
fn poll_frequency_graph(
226-
analyser: Arc<AnalyserNode>,
226+
analyser: Arc<Mutex<AnalyserNode>>,
227227
plot_send: Sender<UiEvent>,
228228
width: u16,
229229
height: u16,
230230
) -> ! {
231-
let bin_count = analyser.frequency_bin_count();
231+
let bin_count = analyser.lock().unwrap().frequency_bin_count();
232232
let mut freq_buffer = vec![0.; bin_count];
233233

234234
loop {
@@ -237,7 +237,7 @@ fn poll_frequency_graph(
237237

238238
// todo, check BaseAudioContext.state if it is still running
239239

240-
analyser.get_float_frequency_data(&mut freq_buffer);
240+
analyser.lock().unwrap().get_float_frequency_data(&mut freq_buffer);
241241

242242
let points: Vec<_> = freq_buffer
243243
.iter()
@@ -264,7 +264,7 @@ fn poll_frequency_graph(
264264
struct AudioThread {
265265
context: AudioContext,
266266
mic_in: MediaStreamAudioSourceNode,
267-
analyser: Arc<AnalyserNode>,
267+
analyser: Arc<Mutex<AnalyserNode>>,
268268
recorder: Option<MediaRecorder>,
269269
buffer_source: AudioBufferSourceNode,
270270
gain_node: Option<GainNode>,
@@ -287,10 +287,10 @@ impl AudioThread {
287287
fn new() -> Self {
288288
let context = AudioContext::default();
289289

290-
let mic = media_devices::get_user_media();
290+
let mic = media_devices::get_user_media_sync(MediaStreamConstraints::Audio);
291291
let mic_in = context.create_media_stream_source(&mic);
292292

293-
let analyser = Arc::new(context.create_analyser());
293+
let analyser = Arc::new(Mutex::new(context.create_analyser()));
294294
let buffer_source = context.create_buffer_source();
295295

296296
let mut instance = Self {
@@ -311,7 +311,7 @@ impl AudioThread {
311311
instance
312312
}
313313

314-
fn analyser(&self) -> Arc<AnalyserNode> {
314+
fn analyser(&self) -> Arc<Mutex<AnalyserNode>> {
315315
self.analyser.clone()
316316
}
317317

@@ -380,7 +380,7 @@ impl AudioThread {
380380
buffer_source.connect(&biquad);
381381
biquad.connect(&gain);
382382
gain.connect(&self.context.destination());
383-
gain.connect(&*self.analyser);
383+
gain.connect(&*self.analyser.lock().unwrap());
384384

385385
buffer_source.start();
386386

@@ -400,7 +400,7 @@ impl AudioThread {
400400

401401
let recorder = MediaRecorder::new(&self.context);
402402
self.mic_in.connect(&recorder);
403-
self.mic_in.connect(&*self.analyser);
403+
self.mic_in.connect(&*self.analyser.lock().unwrap());
404404

405405
self.recorder = Some(recorder);
406406
}

src/node/analyser.rs

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::sync::RwLock;
2-
31
use crate::analysis::{
42
Analyser, AnalyserRingBuffer, DEFAULT_FFT_SIZE, DEFAULT_MAX_DECIBELS, DEFAULT_MIN_DECIBELS,
53
DEFAULT_SMOOTHING_TIME_CONSTANT,
@@ -56,7 +54,7 @@ impl Default for AnalyserOptions {
5654
///
5755
/// let context = AudioContext::default();
5856
///
59-
/// let analyser = context.create_analyser();
57+
/// let mut analyser = context.create_analyser();
6058
/// analyser.connect(&context.destination());
6159
///
6260
/// let osc = context.create_oscillator();
@@ -82,8 +80,7 @@ impl Default for AnalyserOptions {
8280
pub struct AnalyserNode {
8381
registration: AudioContextRegistration,
8482
channel_config: ChannelConfig,
85-
// RwLock is needed to make the AnalyserNode API immutable
86-
analyser: RwLock<Analyser>,
83+
analyser: Analyser,
8784
}
8885

8986
impl AudioNode for AnalyserNode {
@@ -125,7 +122,7 @@ impl AnalyserNode {
125122
let node = AnalyserNode {
126123
registration,
127124
channel_config: options.channel_config.into(),
128-
analyser: RwLock::new(analyser),
125+
analyser,
129126
};
130127

131128
(node, Box::new(render))
@@ -138,16 +135,16 @@ impl AnalyserNode {
138135
///
139136
/// This method may panic if the lock to the inner analyser is poisoned
140137
pub fn fft_size(&self) -> usize {
141-
self.analyser.read().unwrap().fft_size()
138+
self.analyser.fft_size()
142139
}
143140

144141
/// Set FFT size
145142
///
146143
/// # Panics
147144
///
148145
/// This function panics if fft_size is not a power of two or not in the range [32, 32768]
149-
pub fn set_fft_size(&self, fft_size: usize) {
150-
self.analyser.write().unwrap().set_fft_size(fft_size);
146+
pub fn set_fft_size(&mut self, fft_size: usize) {
147+
self.analyser.set_fft_size(fft_size);
151148
}
152149

153150
/// Time averaging parameter with the last analysis frame.
@@ -158,19 +155,16 @@ impl AnalyserNode {
158155
///
159156
/// This method may panic if the lock to the inner analyser is poisoned
160157
pub fn smoothing_time_constant(&self) -> f64 {
161-
self.analyser.read().unwrap().smoothing_time_constant()
158+
self.analyser.smoothing_time_constant()
162159
}
163160

164161
/// Set smoothing time constant
165162
///
166163
/// # Panics
167164
///
168165
/// This function panics if the value is set to a value less than 0 or more than 1.
169-
pub fn set_smoothing_time_constant(&self, value: f64) {
170-
self.analyser
171-
.write()
172-
.unwrap()
173-
.set_smoothing_time_constant(value);
166+
pub fn set_smoothing_time_constant(&mut self, value: f64) {
167+
self.analyser.set_smoothing_time_constant(value);
174168
}
175169

176170
/// Minimum power value in the scaling range for the FFT analysis data for
@@ -180,7 +174,7 @@ impl AnalyserNode {
180174
///
181175
/// This method may panic if the lock to the inner analyser is poisoned
182176
pub fn min_decibels(&self) -> f64 {
183-
self.analyser.read().unwrap().min_decibels()
177+
self.analyser.min_decibels()
184178
}
185179

186180
/// Set min decibels
@@ -189,8 +183,8 @@ impl AnalyserNode {
189183
///
190184
/// This function panics if the value is set to a value more than or equal
191185
/// to max decibels.
192-
pub fn set_min_decibels(&self, value: f64) {
193-
self.analyser.write().unwrap().set_min_decibels(value);
186+
pub fn set_min_decibels(&mut self, value: f64) {
187+
self.analyser.set_min_decibels(value);
194188
}
195189

196190
/// Maximum power value in the scaling range for the FFT analysis data for
@@ -200,7 +194,7 @@ impl AnalyserNode {
200194
///
201195
/// This method may panic if the lock to the inner analyser is poisoned
202196
pub fn max_decibels(&self) -> f64 {
203-
self.analyser.read().unwrap().max_decibels()
197+
self.analyser.max_decibels()
204198
}
205199

206200
/// Set max decibels
@@ -209,8 +203,8 @@ impl AnalyserNode {
209203
///
210204
/// This function panics if the value is set to a value less than or equal
211205
/// to min decibels.
212-
pub fn set_max_decibels(&self, value: f64) {
213-
self.analyser.write().unwrap().set_max_decibels(value);
206+
pub fn set_max_decibels(&mut self, value: f64) {
207+
self.analyser.set_max_decibels(value);
214208
}
215209

216210
/// Number of bins in the FFT results, is half the FFT size
@@ -219,44 +213,35 @@ impl AnalyserNode {
219213
///
220214
/// This method may panic if the lock to the inner analyser is poisoned
221215
pub fn frequency_bin_count(&self) -> usize {
222-
self.analyser.read().unwrap().frequency_bin_count()
216+
self.analyser.frequency_bin_count()
223217
}
224218

225219
/// Copy the current time domain data as f32 values into the provided buffer
226220
///
227221
/// # Panics
228222
///
229223
/// This method may panic if the lock to the inner analyser is poisoned
230-
pub fn get_float_time_domain_data(&self, buffer: &mut [f32]) {
231-
self.analyser
232-
.write()
233-
.unwrap()
234-
.get_float_time_domain_data(buffer);
224+
pub fn get_float_time_domain_data(&mut self, buffer: &mut [f32]) {
225+
self.analyser.get_float_time_domain_data(buffer);
235226
}
236227

237228
/// Copy the current time domain data as u8 values into the provided buffer
238229
///
239230
/// # Panics
240231
///
241232
/// This method may panic if the lock to the inner analyser is poisoned
242-
pub fn get_byte_time_domain_data(&self, buffer: &mut [u8]) {
243-
self.analyser
244-
.write()
245-
.unwrap()
246-
.get_byte_time_domain_data(buffer);
233+
pub fn get_byte_time_domain_data(&mut self, buffer: &mut [u8]) {
234+
self.analyser.get_byte_time_domain_data(buffer);
247235
}
248236

249237
/// Copy the current frequency data into the provided buffer
250238
///
251239
/// # Panics
252240
///
253241
/// This method may panic if the lock to the inner analyser is poisoned
254-
pub fn get_float_frequency_data(&self, buffer: &mut [f32]) {
242+
pub fn get_float_frequency_data(&mut self, buffer: &mut [f32]) {
255243
let current_time = self.registration.context().current_time();
256-
self.analyser
257-
.write()
258-
.unwrap()
259-
.get_float_frequency_data(buffer, current_time);
244+
self.analyser.get_float_frequency_data(buffer, current_time);
260245
}
261246

262247
/// Copy the current frequency data scaled between min_decibels and
@@ -265,12 +250,9 @@ impl AnalyserNode {
265250
/// # Panics
266251
///
267252
/// This method may panic if the lock to the inner analyser is poisoned
268-
pub fn get_byte_frequency_data(&self, buffer: &mut [u8]) {
253+
pub fn get_byte_frequency_data(&mut self, buffer: &mut [u8]) {
269254
let current_time = self.registration.context().current_time();
270-
self.analyser
271-
.write()
272-
.unwrap()
273-
.get_byte_frequency_data(buffer, current_time);
255+
self.analyser.get_byte_frequency_data(buffer, current_time);
274256
}
275257
}
276258

0 commit comments

Comments
 (0)