Skip to content

Commit 51d3486

Browse files
committed
sndio: address feedback from code review
* Add SampleRateMap with SampleRate keys rather than raw HashMap with u32 keys. * Use FrameCount type alias rather than usize for buffer size. * More robust error handling. * Document InnerState state transitions.
1 parent 47877cf commit 51d3486

File tree

3 files changed

+133
-65
lines changed

3 files changed

+133
-65
lines changed

src/host/sndio/adapters.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use crate::{Data, InputCallbackInfo, OutputCallbackInfo, Sample, SampleFormat};
1+
use crate::{Data, FrameCount, InputCallbackInfo, OutputCallbackInfo, Sample, SampleFormat};
22

33
/// When given an input data callback that expects samples in the specified sample format, return
44
/// an input data callback that expects samples in the I16 sample format. The `buffer_size` is in
55
/// samples.
66
pub(super) fn input_adapter_callback<D>(
77
mut original_data_callback: D,
8-
buffer_size: usize,
8+
buffer_size: FrameCount,
99
sample_format: SampleFormat,
1010
) -> Box<dyn FnMut(&Data, &InputCallbackInfo) + Send + 'static>
1111
where
@@ -18,7 +18,7 @@ where
1818
}
1919
SampleFormat::F32 => {
2020
// Make the backing buffer for the Data used in the closure.
21-
let mut adapted_buf = vec![0f32].repeat(buffer_size);
21+
let mut adapted_buf = vec![0f32].repeat(buffer_size as usize);
2222
Box::new(move |data: &Data, info: &InputCallbackInfo| {
2323
let data_slice: &[i16] = data.as_slice().unwrap(); // unwrap OK because data is always i16
2424
let adapted_slice = &mut adapted_buf;
@@ -32,15 +32,15 @@ where
3232
let adapted_data = unsafe {
3333
Data::from_parts(
3434
adapted_buf.as_mut_ptr() as *mut _,
35-
buffer_size,
35+
buffer_size as usize, // TODO: this is converting a FrameCount to a number of samples; invalid for stereo!
3636
sample_format,
3737
)
3838
};
3939
original_data_callback(&adapted_data, info);
4040
})
4141
}
4242
SampleFormat::U16 => {
43-
let mut adapted_buf = vec![0u16].repeat(buffer_size);
43+
let mut adapted_buf = vec![0u16].repeat(buffer_size as usize);
4444
Box::new(move |data: &Data, info: &InputCallbackInfo| {
4545
let data_slice: &[i16] = data.as_slice().unwrap(); // unwrap OK because data is always i16
4646
let adapted_slice = &mut adapted_buf;
@@ -54,7 +54,7 @@ where
5454
let adapted_data = unsafe {
5555
Data::from_parts(
5656
adapted_buf.as_mut_ptr() as *mut _,
57-
buffer_size,
57+
buffer_size as usize, // TODO: this is converting a FrameCount to a number of samples; invalid for stereo!
5858
sample_format,
5959
)
6060
};
@@ -69,7 +69,7 @@ where
6969
/// sample format. The `buffer_size` is in samples.
7070
pub(super) fn output_adapter_callback<D>(
7171
mut original_data_callback: D,
72-
buffer_size: usize,
72+
buffer_size: FrameCount,
7373
sample_format: SampleFormat,
7474
) -> Box<dyn FnMut(&mut Data, &OutputCallbackInfo) + Send + 'static>
7575
where
@@ -82,15 +82,15 @@ where
8282
}
8383
SampleFormat::F32 => {
8484
// Make the backing buffer for the Data used in the closure.
85-
let mut adapted_buf = vec![0f32].repeat(buffer_size);
85+
let mut adapted_buf = vec![0f32].repeat(buffer_size as usize);
8686

8787
Box::new(move |data: &mut Data, info: &OutputCallbackInfo| {
8888
// Note: we construct adapted_data here instead of in the parent function because
8989
// adapted_buf needs to be owned by the closure.
9090
let mut adapted_data = unsafe {
9191
Data::from_parts(
9292
adapted_buf.as_mut_ptr() as *mut _,
93-
buffer_size,
93+
buffer_size as usize, // TODO: this is converting a FrameCount to a number of samples; invalid for stereo!
9494
sample_format,
9595
)
9696
};
@@ -108,15 +108,15 @@ where
108108
}
109109
SampleFormat::U16 => {
110110
// Make the backing buffer for the Data used in the closure.
111-
let mut adapted_buf = vec![0u16].repeat(buffer_size);
111+
let mut adapted_buf = vec![0u16].repeat(buffer_size as usize);
112112

113113
Box::new(move |data: &mut Data, info: &OutputCallbackInfo| {
114114
// Note: we construct adapted_data here instead of in the parent function because
115115
// adapted_buf needs to be owned by the closure.
116116
let mut adapted_data = unsafe {
117117
Data::from_parts(
118118
adapted_buf.as_mut_ptr() as *mut _,
119-
buffer_size,
119+
buffer_size as usize, // TODO: this is converting a FrameCount to a number of samples; invalid for stereo!
120120
sample_format,
121121
)
122122
};

0 commit comments

Comments
 (0)