@@ -181,18 +181,18 @@ impl OscillatorNode {
181
181
/// * `context` - The `AudioContext`
182
182
/// * `options` - The OscillatorOptions
183
183
pub fn new < C : BaseAudioContext > ( context : & C , options : OscillatorOptions ) -> Self {
184
- context. register ( move |registration| {
184
+ let OscillatorOptions {
185
+ type_,
186
+ frequency,
187
+ detune,
188
+ channel_config,
189
+ periodic_wave,
190
+ } = options;
191
+
192
+ let mut node = context. register ( move |registration| {
185
193
let sample_rate = context. sample_rate ( ) ;
186
194
let nyquist = sample_rate / 2. ;
187
195
188
- let OscillatorOptions {
189
- type_,
190
- frequency,
191
- detune,
192
- channel_config,
193
- periodic_wave,
194
- } = options;
195
-
196
196
// frequency audio parameter
197
197
let freq_param_options = AudioParamDescriptor {
198
198
name : String :: new ( ) ,
@@ -229,21 +229,23 @@ impl OscillatorNode {
229
229
sine_table : precomputed_sine_table ( ) ,
230
230
} ;
231
231
232
- let mut node = Self {
232
+ let node = Self {
233
233
registration,
234
234
channel_config : channel_config. into ( ) ,
235
235
frequency : f_param,
236
236
detune : det_param,
237
237
type_,
238
238
} ;
239
239
240
- // if periodic wave has been given, init it
241
- if let Some ( p_wave) = periodic_wave {
242
- node. set_periodic_wave ( p_wave) ;
243
- }
244
-
245
240
( node, Box :: new ( renderer) )
246
- } )
241
+ } ) ;
242
+
243
+ // renderer has been sent to render thread, we can send it messages
244
+ if let Some ( p_wave) = periodic_wave {
245
+ node. set_periodic_wave ( p_wave) ;
246
+ }
247
+
248
+ node
247
249
}
248
250
249
251
/// A-rate [`AudioParam`] that defines the fundamental frequency of the
@@ -579,7 +581,7 @@ mod tests {
579
581
580
582
let context = OfflineAudioContext :: new ( 2 , 1 , 44_100. ) ;
581
583
582
- let osc = context. create_oscillator ( ) ;
584
+ let mut osc = context. create_oscillator ( ) ;
583
585
584
586
let freq = osc. frequency . value ( ) ;
585
587
assert_float_eq ! ( freq, default_freq, abs_all <= 0. ) ;
@@ -588,6 +590,11 @@ mod tests {
588
590
assert_float_eq ! ( det, default_det, abs_all <= 0. ) ;
589
591
590
592
assert_eq ! ( osc. type_( ) , default_type) ;
593
+
594
+ // should not panic when run
595
+ osc. start ( ) ;
596
+ osc. connect ( & context. destination ( ) ) ;
597
+ let _ = context. start_rendering_sync ( ) ;
591
598
}
592
599
593
600
#[ test]
@@ -598,7 +605,7 @@ mod tests {
598
605
599
606
let context = OfflineAudioContext :: new ( 2 , 1 , 44_100. ) ;
600
607
601
- let osc = OscillatorNode :: new ( & context, OscillatorOptions :: default ( ) ) ;
608
+ let mut osc = OscillatorNode :: new ( & context, OscillatorOptions :: default ( ) ) ;
602
609
603
610
let freq = osc. frequency . value ( ) ;
604
611
assert_float_eq ! ( freq, default_freq, abs_all <= 0. ) ;
@@ -607,6 +614,11 @@ mod tests {
607
614
assert_float_eq ! ( det, default_det, abs_all <= 0. ) ;
608
615
609
616
assert_eq ! ( osc. type_( ) , default_type) ;
617
+
618
+ // should not panic when run
619
+ osc. start ( ) ;
620
+ osc. connect ( & context. destination ( ) ) ;
621
+ let _ = context. start_rendering_sync ( ) ;
610
622
}
611
623
612
624
#[ test]
@@ -630,9 +642,14 @@ mod tests {
630
642
..OscillatorOptions :: default ( )
631
643
} ;
632
644
633
- let osc = OscillatorNode :: new ( & context, options) ;
645
+ let mut osc = OscillatorNode :: new ( & context, options) ;
634
646
635
647
assert_eq ! ( osc. type_( ) , expected_type) ;
648
+
649
+ // should not panic when run
650
+ osc. start ( ) ;
651
+ osc. connect ( & context. destination ( ) ) ;
652
+ let _ = context. start_rendering_sync ( ) ;
636
653
}
637
654
638
655
#[ test]
@@ -652,6 +669,11 @@ mod tests {
652
669
653
670
osc. set_type ( OscillatorType :: Sine ) ;
654
671
assert_eq ! ( osc. type_( ) , expected_type) ;
672
+
673
+ // should not panic when run
674
+ osc. start ( ) ;
675
+ osc. connect ( & context. destination ( ) ) ;
676
+ let _ = context. start_rendering_sync ( ) ;
655
677
}
656
678
657
679
// # Test waveforms
0 commit comments