@@ -19,7 +19,7 @@ pub struct Audio {
19
19
device_index : AudioDeviceIndex ,
20
20
is_capturing : bool ,
21
21
frame_rate : Option < FrameRate > ,
22
- capturing_device : Option < AudioDevice < AudioCaptureCallback > > ,
22
+ capturing_device : Option < Box < AudioDevice < AudioCaptureCallback > > > ,
23
23
projectm : ProjectMWrapped ,
24
24
}
25
25
@@ -103,34 +103,35 @@ impl Audio {
103
103
let sample_rate: u32 = 44100 ;
104
104
let frame_rate = self . frame_rate . unwrap ( ) ;
105
105
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) ;
108
115
109
116
let desired_spec = AudioSpecDesired {
110
117
freq : Some ( sample_rate. try_into ( ) . unwrap ( ) ) ,
111
118
channels : Some ( 2 ) ,
112
- samples : Some ( buffer_size) ,
119
+ samples : Some ( buffer_size. try_into ( ) . unwrap ( ) ) ,
113
120
} ;
114
121
115
122
// open audio device for capture
123
+ let device_name = self . get_current_device_name ( ) ;
116
124
let audio_device = match self
117
125
. 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) ;
123
128
124
129
// print spec
125
130
println ! ( "Audio Spec: {:?}" , _spec) ;
126
131
127
132
// return callback fn
128
133
AudioCaptureCallback {
129
134
pm : self . projectm . clone ( ) ,
130
- // spec,
131
- // buffer_size,
132
- // buffer: vec![0; buffer_size as usize],
133
- // position: 0,
134
135
}
135
136
} ) {
136
137
Ok ( device) => device,
@@ -140,12 +141,12 @@ impl Audio {
140
141
}
141
142
} ;
142
143
144
+ // start capturing
145
+ audio_device. resume ( ) ;
146
+
143
147
// take ownership of device
144
- self . capturing_device = Some ( audio_device) ;
148
+ self . capturing_device = Some ( Box :: new ( audio_device) ) ;
145
149
self . is_capturing = true ;
146
-
147
- // play device
148
- self . capturing_device . as_mut ( ) . unwrap ( ) . resume ( ) ;
149
150
}
150
151
151
152
pub fn stop_audio_capture ( & mut self ) {
@@ -157,21 +158,20 @@ impl Audio {
157
158
self . capturing_device. as_ref( ) . unwrap( ) . status( )
158
159
) ;
159
160
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
+
160
167
self . is_capturing = false ;
161
- self . capturing_device = None ;
162
168
}
163
169
}
164
170
165
171
struct AudioCaptureCallback {
166
- // audio_tx: mpsc::Sender<Vec<SampleFormat>>,
167
-
168
172
// we need to keep a reference to the projectm instance to
169
173
// add the audio data to it
170
174
pm : Arc < Mutex < ProjectMHandle > > ,
171
- // spec: sdl2::audio::AudioSpec,
172
- // buffer_size: SampleFormat,
173
- // buffer: Vec<u8>,
174
- // position: usize,
175
175
}
176
176
unsafe impl Send for AudioCaptureCallback { }
177
177
unsafe impl Sync for AudioCaptureCallback { }
@@ -182,9 +182,7 @@ impl AudioCallback for AudioCaptureCallback {
182
182
// we are receiving some chunk of audio data
183
183
// we need to pass it to projectm
184
184
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 ) ;
189
187
}
190
188
}
0 commit comments