pub trait AudioInputCallback {
    type FrameType: IsFrameType;

    // Required method
    fn on_audio_ready(
        &mut self,
        audio_stream: &mut dyn AudioInputStreamSafe,
        audio_data: &[<Self::FrameType as IsFrameType>::Type]
    ) -> DataCallbackResult;

    // Provided methods
    fn on_error_before_close(
        &mut self,
        _audio_stream: &mut dyn AudioInputStreamSafe,
        _error: Error
    ) { ... }
    fn on_error_after_close(
        &mut self,
        _audio_stream: &mut dyn AudioInputStreamSafe,
        _error: Error
    ) { ... }
}
Expand description

This trait defines a callback interface for:

  1. moving data to/from an audio stream using on_audio_ready
  2. being alerted when a stream has an error using on_error_* methods

Required Associated Types§

source

type FrameType: IsFrameType

The sample type and number of channels for processing.

Oboe supports only two sample types:

  • i16 - signed 16-bit integer samples
  • f32 - 32-bit floating point samples

Oboe supports only mono and stereo channel configurations.

Required Methods§

source

fn on_audio_ready( &mut self, audio_stream: &mut dyn AudioInputStreamSafe, audio_data: &[<Self::FrameType as IsFrameType>::Type] ) -> DataCallbackResult

A buffer is ready for processing.

For an output stream, this function should render and write num_frames of data in the stream’s current data format to the audioData buffer.

For an input stream, this function should read and process num_frames of data from the audioData buffer.

The audio data is passed through the buffer. So do NOT call read() or write() on the stream that is making the callback.

Note that numFrames can vary unless AudioStreamBuilder::setFramesPerCallback() is called.

Also note that this callback function should be considered a “real-time” function. It must not do anything that could cause an unbounded delay because that can cause the audio to glitch or pop.

These are things the function should NOT do:

  • allocate memory
  • any file operations such as opening, closing, reading or writing
  • any network operations such as streaming
  • use any mutexes or other blocking synchronization primitives
  • sleep
  • stop or close stream
  • read or write on stream which invoked it

The following are OK to call from the data callback:

  • stream.get_*()

If you need to move data, eg. MIDI commands, in or out of the callback function then we recommend the use of non-blocking techniques such as an atomic FIFO.

Provided Methods§

source

fn on_error_before_close( &mut self, _audio_stream: &mut dyn AudioInputStreamSafe, _error: Error )

This will be called when an error occurs on a stream or when the stream is disconnected.

Note that this will be called on a different thread than the onAudioReady() thread. This thread will be created by Oboe.

The underlying stream will already be stopped by Oboe but not yet closed. So the stream can be queried.

Do not close or delete the stream in this method because it will be closed after this method returns.

source

fn on_error_after_close( &mut self, _audio_stream: &mut dyn AudioInputStreamSafe, _error: Error )

This will be called when an error occurs on a stream or when the stream is disconnected. The underlying AAudio or OpenSL ES stream will already be stopped AND closed by Oboe. So the underlying stream cannot be referenced. But you can still query most parameters.

This callback could be used to reopen a new stream on another device. You can safely delete the old AudioStream in this method.

Implementors§