@@ -19,7 +19,7 @@ pub struct Audio {
1919 device_index : AudioDeviceIndex ,
2020 is_capturing : bool ,
2121 frame_rate : Option < FrameRate > ,
22- capturing_device : Option < AudioDevice < AudioCaptureCallback > > ,
22+ capturing_device : Option < Box < AudioDevice < AudioCaptureCallback > > > ,
2323 projectm : ProjectMWrapped ,
2424}
2525
@@ -103,34 +103,35 @@ impl Audio {
103103 let sample_rate: u32 = 44100 ;
104104 let frame_rate = self . frame_rate . unwrap ( ) ;
105105
106- // should be enough for 1 frame
107- let buffer_size = ( sample_rate / frame_rate) as u16 ;
106+ // how many samples to capture at a time
107+ // should be enough for 1 frame or less
108+ // should not be larger than max_samples / channels
109+ let max_samples: usize = projectm_rs:: core:: Projectm :: pcm_get_max_samples ( )
110+ . try_into ( )
111+ . unwrap ( ) ;
112+ let samples_per_frame = ( sample_rate / frame_rate) as usize ;
113+ let buffer_size = std:: cmp:: min ( max_samples / 2 , samples_per_frame) ;
114+ println ! ( "Buffer size: {}" , buffer_size) ;
108115
109116 let desired_spec = AudioSpecDesired {
110117 freq : Some ( sample_rate. try_into ( ) . unwrap ( ) ) ,
111118 channels : Some ( 2 ) ,
112- samples : Some ( buffer_size) ,
119+ samples : Some ( buffer_size. try_into ( ) . unwrap ( ) ) ,
113120 } ;
114121
115122 // open audio device for capture
123+ let device_name = self . get_current_device_name ( ) ;
116124 let audio_device = match self
117125 . audio_subsystem // sdl
118- . open_capture ( None , & desired_spec, |_spec| {
119- println ! (
120- "Beginning audio capture for device {}" ,
121- self . get_current_device_name( )
122- ) ;
126+ . open_capture ( device_name. as_str ( ) , & desired_spec, |_spec| {
127+ println ! ( "Beginning audio capture for device {}" , device_name) ;
123128
124129 // print spec
125130 println ! ( "Audio Spec: {:?}" , _spec) ;
126131
127132 // return callback fn
128133 AudioCaptureCallback {
129134 pm : self . projectm . clone ( ) ,
130- // spec,
131- // buffer_size,
132- // buffer: vec![0; buffer_size as usize],
133- // position: 0,
134135 }
135136 } ) {
136137 Ok ( device) => device,
@@ -140,12 +141,12 @@ impl Audio {
140141 }
141142 } ;
142143
144+ // start capturing
145+ audio_device. resume ( ) ;
146+
143147 // take ownership of device
144- self . capturing_device = Some ( audio_device) ;
148+ self . capturing_device = Some ( Box :: new ( audio_device) ) ;
145149 self . is_capturing = true ;
146-
147- // play device
148- self . capturing_device . as_mut ( ) . unwrap ( ) . resume ( ) ;
149150 }
150151
151152 pub fn stop_audio_capture ( & mut self ) {
@@ -157,21 +158,20 @@ impl Audio {
157158 self . capturing_device. as_ref( ) . unwrap( ) . status( )
158159 ) ;
159160
161+ // take ownership of device
162+ // capture device will be dropped when this function returns
163+ // and the audio callback will stop being called
164+ let device = self . capturing_device . take ( ) . unwrap ( ) ;
165+ device. pause ( ) ;
166+
160167 self . is_capturing = false ;
161- self . capturing_device = None ;
162168 }
163169}
164170
165171struct AudioCaptureCallback {
166- // audio_tx: mpsc::Sender<Vec<SampleFormat>>,
167-
168172 // we need to keep a reference to the projectm instance to
169173 // add the audio data to it
170174 pm : Arc < Mutex < ProjectMHandle > > ,
171- // spec: sdl2::audio::AudioSpec,
172- // buffer_size: SampleFormat,
173- // buffer: Vec<u8>,
174- // position: usize,
175175}
176176unsafe impl Send for AudioCaptureCallback { }
177177unsafe impl Sync for AudioCaptureCallback { }
@@ -182,9 +182,7 @@ impl AudioCallback for AudioCaptureCallback {
182182 // we are receiving some chunk of audio data
183183 // we need to pass it to projectm
184184 fn callback ( & mut self , out : & mut [ SampleFormat ] ) {
185- {
186- let pm = * self . pm . lock ( ) . unwrap ( ) ;
187- projectm_rs:: core:: Projectm :: pcm_add_float ( pm, out. to_vec ( ) , 2 ) ;
188- }
185+ let pm = * self . pm . lock ( ) . unwrap ( ) ;
186+ projectm_rs:: core:: Projectm :: pcm_add_float ( pm, out. to_vec ( ) , 2 ) ;
189187 }
190188}
0 commit comments