1
- use std:: sync:: RwLock ;
2
-
3
1
use crate :: analysis:: {
4
2
Analyser , AnalyserRingBuffer , DEFAULT_FFT_SIZE , DEFAULT_MAX_DECIBELS , DEFAULT_MIN_DECIBELS ,
5
3
DEFAULT_SMOOTHING_TIME_CONSTANT ,
@@ -56,7 +54,7 @@ impl Default for AnalyserOptions {
56
54
///
57
55
/// let context = AudioContext::default();
58
56
///
59
- /// let analyser = context.create_analyser();
57
+ /// let mut analyser = context.create_analyser();
60
58
/// analyser.connect(&context.destination());
61
59
///
62
60
/// let osc = context.create_oscillator();
@@ -82,8 +80,7 @@ impl Default for AnalyserOptions {
82
80
pub struct AnalyserNode {
83
81
registration : AudioContextRegistration ,
84
82
channel_config : ChannelConfig ,
85
- // RwLock is needed to make the AnalyserNode API immutable
86
- analyser : RwLock < Analyser > ,
83
+ analyser : Analyser ,
87
84
}
88
85
89
86
impl AudioNode for AnalyserNode {
@@ -125,7 +122,7 @@ impl AnalyserNode {
125
122
let node = AnalyserNode {
126
123
registration,
127
124
channel_config : options. channel_config . into ( ) ,
128
- analyser : RwLock :: new ( analyser ) ,
125
+ analyser,
129
126
} ;
130
127
131
128
( node, Box :: new ( render) )
@@ -138,16 +135,16 @@ impl AnalyserNode {
138
135
///
139
136
/// This method may panic if the lock to the inner analyser is poisoned
140
137
pub fn fft_size ( & self ) -> usize {
141
- self . analyser . read ( ) . unwrap ( ) . fft_size ( )
138
+ self . analyser . fft_size ( )
142
139
}
143
140
144
141
/// Set FFT size
145
142
///
146
143
/// # Panics
147
144
///
148
145
/// 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) ;
151
148
}
152
149
153
150
/// Time averaging parameter with the last analysis frame.
@@ -158,19 +155,16 @@ impl AnalyserNode {
158
155
///
159
156
/// This method may panic if the lock to the inner analyser is poisoned
160
157
pub fn smoothing_time_constant ( & self ) -> f64 {
161
- self . analyser . read ( ) . unwrap ( ) . smoothing_time_constant ( )
158
+ self . analyser . smoothing_time_constant ( )
162
159
}
163
160
164
161
/// Set smoothing time constant
165
162
///
166
163
/// # Panics
167
164
///
168
165
/// 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) ;
174
168
}
175
169
176
170
/// Minimum power value in the scaling range for the FFT analysis data for
@@ -180,7 +174,7 @@ impl AnalyserNode {
180
174
///
181
175
/// This method may panic if the lock to the inner analyser is poisoned
182
176
pub fn min_decibels ( & self ) -> f64 {
183
- self . analyser . read ( ) . unwrap ( ) . min_decibels ( )
177
+ self . analyser . min_decibels ( )
184
178
}
185
179
186
180
/// Set min decibels
@@ -189,8 +183,8 @@ impl AnalyserNode {
189
183
///
190
184
/// This function panics if the value is set to a value more than or equal
191
185
/// 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) ;
194
188
}
195
189
196
190
/// Maximum power value in the scaling range for the FFT analysis data for
@@ -200,7 +194,7 @@ impl AnalyserNode {
200
194
///
201
195
/// This method may panic if the lock to the inner analyser is poisoned
202
196
pub fn max_decibels ( & self ) -> f64 {
203
- self . analyser . read ( ) . unwrap ( ) . max_decibels ( )
197
+ self . analyser . max_decibels ( )
204
198
}
205
199
206
200
/// Set max decibels
@@ -209,8 +203,8 @@ impl AnalyserNode {
209
203
///
210
204
/// This function panics if the value is set to a value less than or equal
211
205
/// 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) ;
214
208
}
215
209
216
210
/// Number of bins in the FFT results, is half the FFT size
@@ -219,44 +213,35 @@ impl AnalyserNode {
219
213
///
220
214
/// This method may panic if the lock to the inner analyser is poisoned
221
215
pub fn frequency_bin_count ( & self ) -> usize {
222
- self . analyser . read ( ) . unwrap ( ) . frequency_bin_count ( )
216
+ self . analyser . frequency_bin_count ( )
223
217
}
224
218
225
219
/// Copy the current time domain data as f32 values into the provided buffer
226
220
///
227
221
/// # Panics
228
222
///
229
223
/// 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) ;
235
226
}
236
227
237
228
/// Copy the current time domain data as u8 values into the provided buffer
238
229
///
239
230
/// # Panics
240
231
///
241
232
/// 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) ;
247
235
}
248
236
249
237
/// Copy the current frequency data into the provided buffer
250
238
///
251
239
/// # Panics
252
240
///
253
241
/// 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 ] ) {
255
243
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) ;
260
245
}
261
246
262
247
/// Copy the current frequency data scaled between min_decibels and
@@ -265,12 +250,9 @@ impl AnalyserNode {
265
250
/// # Panics
266
251
///
267
252
/// 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 ] ) {
269
254
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) ;
274
256
}
275
257
}
276
258
0 commit comments