@@ -24,6 +24,9 @@ pub struct State {
2424 pub timer : f32 ,
2525 pub show_configuration_window : bool ,
2626 pub cycles_per_frame : u32 ,
27+ beep_audio_source : AudioSource ,
28+ sound : Option < Sound > ,
29+ is_beeping : bool ,
2730}
2831
2932#[ derive( Parser ) ]
@@ -32,7 +35,7 @@ struct Args {
3235 file : OsString ,
3336}
3437
35- pub fn setup ( gfx : & mut Graphics ) -> State {
38+ pub fn setup ( app : & mut App , gfx : & mut Graphics ) -> State {
3639 let args = Args :: parse ( ) ;
3740
3841 let file_path = if args. file . is_empty ( ) {
@@ -78,6 +81,11 @@ pub fn setup(gfx: &mut Graphics) -> State {
7881
7982 let config = Configuration :: load ( ) . unwrap ( ) ;
8083
84+ let audio_source = app
85+ . audio
86+ . create_source ( include_bytes ! ( "../../assets/beep.ogg" ) )
87+ . expect ( "Failed to create audio source" ) ;
88+
8189 State {
8290 last_dir,
8391 configuration : config. clone ( ) ,
@@ -92,6 +100,9 @@ pub fn setup(gfx: &mut Graphics) -> State {
92100 timer : 0.0 ,
93101 show_configuration_window : false ,
94102 cycles_per_frame : config. vm . cycles_per_frame ,
103+ beep_audio_source : audio_source,
104+ sound : None ,
105+ is_beeping : false ,
95106 }
96107}
97108
@@ -123,9 +134,21 @@ pub fn update(app: &mut App, state: &mut State) {
123134 if state. vm . delay_timer != 0 {
124135 state. vm . delay_timer -= 1 ;
125136 }
126- if state. vm . sound_timer != 0 {
127- // TODO: play sound here
137+
138+ if state. vm . sound_timer > 0 {
139+ if !state. is_beeping {
140+ state. sound = Option :: from ( app. audio . play_sound (
141+ & state. beep_audio_source ,
142+ state. configuration . sound . volume ,
143+ true ,
144+ ) ) ;
145+ state. is_beeping = true ;
146+ }
147+
128148 state. vm . sound_timer -= 1 ;
149+ } else if state. is_beeping {
150+ app. audio . stop ( & state. sound . clone ( ) . unwrap ( ) ) ;
151+ state. is_beeping = false ;
129152 }
130153 }
131154
0 commit comments