File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1
1
use rand:: seq:: SliceRandom ;
2
2
use regex:: Regex ;
3
+ use rodio:: Source ;
4
+ use rodio:: { OutputStream , Sink } ;
3
5
use std:: collections:: HashMap ;
4
6
use std:: env;
5
7
use std:: fs:: { self , File } ;
@@ -579,6 +581,39 @@ impl Executor {
579
581
. collect :: < Vec < Type > > ( ) ,
580
582
) ) ,
581
583
584
+ // Play sound from frequency
585
+ "play-sound" => {
586
+ fn play_sine_wave ( frequency : f64 , duration_secs : u64 ) {
587
+ let sample_rate = 44100 ;
588
+
589
+ let num_samples = ( duration_secs * sample_rate) as usize ;
590
+ let samples: Vec < f32 > = ( 0 ..num_samples)
591
+ . map ( |t| {
592
+ let t = t as f64 / sample_rate as f64 ;
593
+ ( t * frequency * 2.0 * std:: f64:: consts:: PI ) . sin ( ) as f32
594
+ } )
595
+ . collect ( ) ;
596
+
597
+ let ( _stream, stream_handle) = OutputStream :: try_default ( ) . unwrap ( ) ;
598
+ let sink = Sink :: try_new ( & stream_handle) . unwrap ( ) ;
599
+
600
+ for _ in samples {
601
+ sink. append (
602
+ rodio:: source:: SineWave :: new ( frequency as f32 )
603
+ . take_duration ( Duration :: from_secs ( duration_secs) ) ,
604
+ ) ;
605
+ }
606
+
607
+ sink. play ( ) ;
608
+ std:: thread:: sleep ( Duration :: from_secs ( duration_secs) ) ;
609
+ }
610
+
611
+ let duration_secs = self . pop_stack ( ) . get_number ( ) as u64 ;
612
+ let frequency = self . pop_stack ( ) . get_number ( ) ;
613
+
614
+ play_sine_wave ( frequency, duration_secs) ;
615
+ }
616
+
582
617
// Commands of control
583
618
584
619
// evaluate string as program
You can’t perform that action at this time.
0 commit comments