11use projectm:: core:: ProjectM ;
2- use sdl3:: audio:: { AudioDevice , AudioDeviceID , AudioSpec , AudioStream } ;
2+ use sdl3:: audio:: { AudioDevice , AudioDeviceID , AudioSpec , AudioStreamOwner } ;
33
44use super :: config:: FrameRate ;
55use super :: ProjectMWrapped ;
@@ -9,7 +9,7 @@ const CHANNELS: u32 = 2; // Number of audio channels
99
1010pub struct Audio {
1111 audio_subsystem : sdl3:: AudioSubsystem ,
12- recording_stream : Option < Box < AudioStream > > ,
12+ recording_stream : Option < Box < AudioStreamOwner > > ,
1313 is_capturing : bool ,
1414 frame_rate : Option < FrameRate > ,
1515 projectm : ProjectMWrapped ,
@@ -50,7 +50,11 @@ impl Audio {
5050
5151 println ! ( "Audio Devices:" ) ;
5252 for device in devices {
53- println ! ( " - {} [{}]" , device. name( ) , device. id( ) ) ;
53+ println ! (
54+ " - {} [{}]" ,
55+ device. name( ) . unwrap_or_else( |_| "unknown" . to_string( ) ) ,
56+ device. id( )
57+ ) ;
5458 }
5559 }
5660
@@ -67,26 +71,30 @@ impl Audio {
6771 format : Some ( sdl3:: audio:: AudioFormat :: f32_sys ( ) ) , // Assuming F32SYS is the correct format
6872 } ;
6973
70- // Open audio device for recording (use default device if none specified)
71- let device_id = device_id
72- . or_else ( || Some ( self . get_default_recording_device ( ) . id ( ) ) )
73- . unwrap ( ) ; // Ensure device_id is Some
74+ let device = match device_id {
75+ Some ( id ) => AudioDevice :: new ( id , self . audio_subsystem . clone ( ) ) ,
76+ None => self . get_default_recording_device ( ) ,
77+ } ;
7478
75- let audio_stream = match AudioStream :: open_device_stream ( device_id , Some ( & desired_spec) ) {
79+ let audio_stream = match device . open_device_stream ( Some ( & desired_spec) ) {
7680 Ok ( stream) => stream,
7781 Err ( e) => {
7882 println ! ( "Failed to open audio stream: {}" , e) ;
7983 return ;
8084 }
8185 } ;
82- println ! ( "Capturing audio from device {:?}" , audio_stream) ;
86+
87+ println ! ( "Capturing audio from device {:?}" , audio_stream. device_id( ) ) ;
8388
8489 // Get the actual device ID and name from the stream
8590 let actual_device_id = audio_stream. device_id ( ) ;
8691 let actual_device_name = audio_stream. device_name ( ) ;
8792
8893 if actual_device_id. is_none ( ) {
89- println ! ( "Failed to get device ID from audio stream: {:?}" , audio_stream) ;
94+ println ! (
95+ "Failed to get device ID from audio stream: {:?}" ,
96+ audio_stream. device_name( )
97+ ) ;
9098 return ;
9199 }
92100
@@ -123,7 +131,7 @@ impl Audio {
123131 let current_device_index = current_device_name. as_ref ( ) . and_then ( |name| {
124132 device_list
125133 . iter ( )
126- . position ( |d| d. name ( ) == * name)
134+ . position ( |d| d. name ( ) == Ok ( name. to_string ( ) ) )
127135 } ) ;
128136
129137 let current_device_index = current_device_index. unwrap_or_else ( || {
@@ -138,23 +146,21 @@ impl Audio {
138146 println ! (
139147 "Switching from device '{}' to '{}'" ,
140148 current_device_name. unwrap_or_else( || "unknown" . to_string( ) ) ,
141- next_device_id. name( )
149+ next_device_id. name( ) . unwrap_or_else ( |_| "unknown" . to_string ( ) )
142150 ) ;
143151
144152 // Start capturing from next device
145153 self . begin_audio_recording ( Some ( next_device_id) ) ;
146154 }
147155
148-
149156 pub fn stop_audio_recording ( & mut self ) {
150157 if let Some ( stream) = self . recording_stream . take ( ) {
151158 // Retrieve the device name before dropping the stream
152- let current_device_name = stream. device_name ( ) . unwrap_or_else ( || "unknown" . to_string ( ) ) ;
159+ let current_device_name = stream
160+ . device_name ( )
161+ . unwrap_or_else ( || "unknown" . to_string ( ) ) ;
153162
154- println ! (
155- "Stopping audio capture for device {}" ,
156- current_device_name
157- ) ;
163+ println ! ( "Stopping audio capture for device {}" , current_device_name) ;
158164
159165 // The recording device will be closed when the stream is dropped
160166 self . is_capturing = false ;
@@ -205,17 +211,18 @@ impl Audio {
205211 }
206212 }
207213 }
208-
209214 }
210215
211216 fn get_device_list ( & self ) -> Vec < AudioDeviceID > {
212- self . audio_subsystem . audio_recording_device_ids ( ) . unwrap_or_else ( |e| {
213- println ! ( "Failed to get audio device list: {}" , e) ;
214- Vec :: new ( )
215- } )
217+ self . audio_subsystem
218+ . audio_recording_device_ids ( )
219+ . unwrap_or_else ( |e| {
220+ println ! ( "Failed to get audio device list: {}" , e) ;
221+ Vec :: new ( )
222+ } )
216223 }
217224
218225 pub fn recording_device_name ( & self ) -> Option < String > {
219226 self . current_device_name . clone ( )
220227 }
221- }
228+ }
0 commit comments