Skip to content

Commit ee25f78

Browse files
committed
more cleanup
1 parent 037d134 commit ee25f78

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ rust-version = "1.68.2"
1010
libc = "*"
1111
sdl2 = "0.35"
1212
projectm-rs = "1.0.5"
13+
# projectm-rs = { path = "../projectm-rs" }
1314
#projectm-rs = { git = "https://github.com/projectM-visualizer/projectm-rs" }
1415

1516
# gl = "0.14.0"

src/app/audio.rs

+26-28
Original file line numberDiff line numberDiff line change
@@ -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

165171
struct 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
}
176176
unsafe impl Send for AudioCaptureCallback {}
177177
unsafe 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

Comments
 (0)