Skip to content

Commit 07ceda7

Browse files
committed
sndio: refactor adapters
1 parent 37600dd commit 07ceda7

File tree

1 file changed

+88
-52
lines changed

1 file changed

+88
-52
lines changed

src/host/sndio/adapters.rs

Lines changed: 88 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,57 @@ pub(super) fn input_adapter_callback<D>(
1111
where
1212
D: FnMut(&Data, &InputCallbackInfo) + Send + 'static,
1313
{
14-
if sample_format == SampleFormat::I16 {
15-
// no-op
16-
return Box::new(original_data_callback);
17-
}
18-
19-
// Make the backing buffer for the Data used in the closure.
20-
let mut buf: Vec<u8> = vec![0].repeat(buffer_size * sample_format.sample_size());
21-
22-
Box::new(move |data: &Data, info: &InputCallbackInfo| {
23-
// Note: we construct adapted_data here instead of in the parent function because buf needs
24-
// to be owned by the closure.
25-
let mut adapted_data =
26-
unsafe { Data::from_parts(buf.as_mut_ptr() as *mut _, buffer_size, sample_format) };
27-
let data_slice: &[i16] = data.as_slice().unwrap(); // unwrap OK because data is always i16
28-
match sample_format {
29-
SampleFormat::F32 => {
30-
let adapted_slice: &mut [f32] = adapted_data.as_slice_mut().unwrap(); // unwrap OK because of the match
14+
match sample_format {
15+
SampleFormat::I16 => {
16+
// no-op
17+
return Box::new(original_data_callback);
18+
}
19+
SampleFormat::F32 => {
20+
// Make the backing buffer for the Data used in the closure.
21+
let mut adapted_buf = vec![0f32].repeat(buffer_size);
22+
Box::new(move |data: &Data, info: &InputCallbackInfo| {
23+
let data_slice: &[i16] = data.as_slice().unwrap(); // unwrap OK because data is always i16
24+
let adapted_slice = &mut adapted_buf;
3125
assert_eq!(data_slice.len(), adapted_slice.len());
3226
for (i, adapted_ref) in adapted_slice.iter_mut().enumerate() {
3327
*adapted_ref = data_slice[i].to_f32();
3428
}
35-
}
36-
SampleFormat::U16 => {
37-
let adapted_slice: &mut [u16] = adapted_data.as_slice_mut().unwrap(); // unwrap OK because of the match
29+
30+
// Note: we construct adapted_data here instead of in the parent function because adapted_buf needs
31+
// to be owned by the closure.
32+
let adapted_data = unsafe {
33+
Data::from_parts(
34+
adapted_buf.as_mut_ptr() as *mut _,
35+
buffer_size,
36+
sample_format,
37+
)
38+
};
39+
original_data_callback(&adapted_data, info);
40+
})
41+
}
42+
SampleFormat::U16 => {
43+
let mut adapted_buf = vec![0u16].repeat(buffer_size);
44+
Box::new(move |data: &Data, info: &InputCallbackInfo| {
45+
let data_slice: &[i16] = data.as_slice().unwrap(); // unwrap OK because data is always i16
46+
let adapted_slice = &mut adapted_buf;
3847
assert_eq!(data_slice.len(), adapted_slice.len());
3948
for (i, adapted_ref) in adapted_slice.iter_mut().enumerate() {
4049
*adapted_ref = data_slice[i].to_u16();
4150
}
42-
}
43-
SampleFormat::I16 => {
44-
unreachable!("i16 should've already been handled above");
45-
}
51+
52+
// Note: we construct adapted_data here instead of in the parent function because adapted_buf needs
53+
// to be owned by the closure.
54+
let adapted_data = unsafe {
55+
Data::from_parts(
56+
adapted_buf.as_mut_ptr() as *mut _,
57+
buffer_size,
58+
sample_format,
59+
)
60+
};
61+
original_data_callback(&adapted_data, info);
62+
})
4663
}
47-
original_data_callback(&adapted_data, info);
48-
})
64+
}
4965
}
5066

5167
/// When given an output data callback that expects a place to write samples in the specified
@@ -59,42 +75,62 @@ pub(super) fn output_adapter_callback<D>(
5975
where
6076
D: FnMut(&mut Data, &OutputCallbackInfo) + Send + 'static,
6177
{
62-
if sample_format == SampleFormat::I16 {
63-
// no-op
64-
return Box::new(original_data_callback);
65-
}
66-
67-
// Make the backing buffer for the Data used in the closure.
68-
let mut buf: Vec<u8> = vec![0].repeat(buffer_size * sample_format.sample_size());
78+
match sample_format {
79+
SampleFormat::I16 => {
80+
// no-op
81+
return Box::new(original_data_callback);
82+
}
83+
SampleFormat::F32 => {
84+
// Make the backing buffer for the Data used in the closure.
85+
let mut adapted_buf = vec![0f32].repeat(buffer_size);
6986

70-
Box::new(move |data: &mut Data, info: &OutputCallbackInfo| {
71-
// Note: we construct adapted_data here instead of in the parent function because buf needs
72-
// to be owned by the closure.
73-
let mut adapted_data =
74-
unsafe { Data::from_parts(buf.as_mut_ptr() as *mut _, buffer_size, sample_format) };
87+
Box::new(move |data: &mut Data, info: &OutputCallbackInfo| {
88+
// Note: we construct adapted_data here instead of in the parent function because
89+
// adapted_buf needs to be owned by the closure.
90+
let mut adapted_data = unsafe {
91+
Data::from_parts(
92+
adapted_buf.as_mut_ptr() as *mut _,
93+
buffer_size,
94+
sample_format,
95+
)
96+
};
7597

76-
// Populate buf / adapted_data.
77-
original_data_callback(&mut adapted_data, info);
98+
// Populate adapted_buf / adapted_data.
99+
original_data_callback(&mut adapted_data, info);
78100

79-
let data_slice: &mut [i16] = data.as_slice_mut().unwrap(); // unwrap OK because data is always i16
80-
match sample_format {
81-
SampleFormat::F32 => {
82-
let adapted_slice: &[f32] = adapted_data.as_slice().unwrap(); // unwrap OK because of the match
101+
let data_slice: &mut [i16] = data.as_slice_mut().unwrap(); // unwrap OK because data is always i16
102+
let adapted_slice = &adapted_buf;
83103
assert_eq!(data_slice.len(), adapted_slice.len());
84104
for (i, data_ref) in data_slice.iter_mut().enumerate() {
85105
*data_ref = adapted_slice[i].to_i16();
86106
}
87-
}
88-
SampleFormat::U16 => {
89-
let adapted_slice: &[u16] = adapted_data.as_slice().unwrap(); // unwrap OK because of the match
107+
})
108+
}
109+
SampleFormat::U16 => {
110+
// Make the backing buffer for the Data used in the closure.
111+
let mut adapted_buf = vec![0u16].repeat(buffer_size);
112+
113+
Box::new(move |data: &mut Data, info: &OutputCallbackInfo| {
114+
// Note: we construct adapted_data here instead of in the parent function because
115+
// adapted_buf needs to be owned by the closure.
116+
let mut adapted_data = unsafe {
117+
Data::from_parts(
118+
adapted_buf.as_mut_ptr() as *mut _,
119+
buffer_size,
120+
sample_format,
121+
)
122+
};
123+
124+
// Populate adapted_buf / adapted_data.
125+
original_data_callback(&mut adapted_data, info);
126+
127+
let data_slice: &mut [i16] = data.as_slice_mut().unwrap(); // unwrap OK because data is always i16
128+
let adapted_slice = &adapted_buf;
90129
assert_eq!(data_slice.len(), adapted_slice.len());
91130
for (i, data_ref) in data_slice.iter_mut().enumerate() {
92131
*data_ref = adapted_slice[i].to_i16();
93132
}
94-
}
95-
SampleFormat::I16 => {
96-
unreachable!("i16 should've already been handled above");
97-
}
133+
})
98134
}
99-
})
135+
}
100136
}

0 commit comments

Comments
 (0)