Crate audio_streams

source ·
Expand description

Provides an interface for playing and recording audio.

When implementing an audio playback system, the StreamSource trait is implemented. Implementors of this trait allow creation of PlaybackBufferStream objects. The PlaybackBufferStream provides the actual audio buffers to be filled with audio samples. These buffers can be filled with write_playback_buffer.

Users playing audio fill the provided buffers with audio. When a PlaybackBuffer is dropped, the samples written to it are committed to the PlaybackBufferStream it came from.

use audio_streams::{BoxError, PlaybackBuffer, SampleFormat, StreamSource, NoopStreamSource};
use std::io::Write;

const buffer_size: usize = 120;
const num_channels: usize = 2;

let mut stream_source = NoopStreamSource::new();
let sample_format = SampleFormat::S16LE;
let frame_size = num_channels * sample_format.sample_bytes();

let (_, mut stream) = stream_source
    .new_playback_stream(num_channels, sample_format, 48000, buffer_size)?;
// Play 10 buffers of DC.
let mut buf = Vec::new();
buf.resize(buffer_size * frame_size, 0xa5u8);
for _ in 0..10 {
    let mut copy_cb = |stream_buffer: &mut PlaybackBuffer| {
        assert_eq!(stream_buffer.write(&buf)?, buffer_size * frame_size);
        Ok(())
    };
    stream.write_playback_buffer(&mut copy_cb)?;
}

Re-exports

Modules

Structs

Enums

Traits

  • AsyncBufferCommit is a cleanup funcion that must be called before dropping the buffer, allowing arbitrary code to be run after the buffer is filled or read by the user.
  • PlaybackBufferStream provides PlaybackBuffers asynchronously to fill with audio samples for playback.
  • BufferCommit is a cleanup funcion that must be called before dropping the buffer, allowing arbitrary code to be run after the buffer is filled or read by the user.
  • PlaybackBufferStream provides PlaybackBuffers to fill with audio samples for playback.
  • StreamControl provides a way to set the volume and mute states of a stream. StreamControl is separate from the stream so it can be owned by a different thread if needed.
  • StreamSource creates streams for playback or capture of audio.
  • StreamSourceGenerator is a trait used to abstract types that create StreamSource. It can be used when multiple types of StreamSource are needed.

Functions

  • Call f with a AsyncPlaybackBuffer, and trigger the buffer done call back after. f should write playback data to the given AsyncPlaybackBuffer.

Type Definitions

  • Errors that can pass across threads.