Add a CallbackInfo argument to the stream data callback

I began on an implementation of the timestamp API described in #363 but
quickly realised that it might be best to land the API for providing
extra information to the user's callback first.

This PR adds two new types: `InputCallbackInfo` and `OutputCallbackInfo`.
These types are delivered to the user's data callback as a new, second
argument.

While these types are currently empty, the intention is for these types
to provide information relevant to the current request for or delivery
of data. This includes:

- Timestamp information #363.
- Flags related to the state of the stream (e.g buffer
  underflow/overflow).

In order to maintain flexibility to avoid breaking things, I figure we
can keep the fields of these types private and provide methods for
retrieving this info.

@Ralith, @ishitatsuyuki does this seem OK to you?
This commit is contained in:
mitchmindtree 2020-04-16 14:50:36 +02:00
parent 608d23adc6
commit bcf962c447
14 changed files with 104 additions and 83 deletions

View file

@ -37,7 +37,9 @@ where
let stream = device.build_output_stream(
config,
move |data: &mut [T]| write_data(data, channels, &mut next_value),
move |data: &mut [T], _: &cpal::OutputCallbackInfo| {
write_data(data, channels, &mut next_value)
},
err_fn,
)?;
stream.play()?;

View file

@ -46,7 +46,7 @@ fn main() -> Result<(), anyhow::Error> {
producer.push(0.0).unwrap();
}
let input_data_fn = move |data: &[f32]| {
let input_data_fn = move |data: &[f32], _: &cpal::InputCallbackInfo| {
let mut output_fell_behind = false;
for &sample in data {
if producer.push(sample).is_err() {
@ -58,7 +58,7 @@ fn main() -> Result<(), anyhow::Error> {
}
};
let output_data_fn = move |data: &mut [f32]| {
let output_data_fn = move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
let mut input_fell_behind = None;
for sample in data {
*sample = match consumer.pop() {

View file

@ -43,17 +43,17 @@ fn main() -> Result<(), anyhow::Error> {
let stream = match config.sample_format() {
cpal::SampleFormat::F32 => device.build_input_stream(
&config.into(),
move |data| write_input_data::<f32, f32>(data, &writer_2),
move |data, _: &_| write_input_data::<f32, f32>(data, &writer_2),
err_fn,
)?,
cpal::SampleFormat::I16 => device.build_input_stream(
&config.into(),
move |data| write_input_data::<i16, i16>(data, &writer_2),
move |data, _: &_| write_input_data::<i16, i16>(data, &writer_2),
err_fn,
)?,
cpal::SampleFormat::U16 => device.build_input_stream(
&config.into(),
move |data| write_input_data::<u16, i16>(data, &writer_2),
move |data, _: &_| write_input_data::<u16, i16>(data, &writer_2),
err_fn,
)?,
};