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§

pub use async_api::AsyncStream;
pub use async_api::AudioStreamsExecutor;

Modules§

async_api
Traits required by audio_streams to perform async operations.
capture
shm_streams

Structs§

AsyncPlaybackBuffer
AsyncPlaybackBuffer is the async version of PlaybackBuffer.
AudioBuffer 🔒
AudioBuffer is one buffer that holds buffer_size audio frames. It is the inner data of PlaybackBuffer and CaptureBuffer.
NoopBufferCommit 🔒
NoopStream data that is needed from the buffer complete callback.
NoopStream
Stream that accepts playback samples but drops them.
NoopStreamControl
No-op control for NoopStreams.
NoopStreamSource
Source of NoopStream and NoopStreamControl objects.
NoopStreamSourceGenerator
NoopStreamSourceGenerator is a struct that implements StreamSourceGenerator to generate NoopStreamSource.
PlaybackBuffer
PlaybackBuffer is one buffer that holds buffer_size audio frames. It is used to temporarily allow access to an audio buffer and notifes the owning stream of write completion when dropped.

Enums§

Error
PlaybackBufferError
Errors that are possible from a PlaybackBuffer.
SampleFormat
SampleFormatError
Errors that are possible from a SampleFormat.
StreamDirection
Valid directions of an audio stream.
StreamEffect
Valid effects for an audio stream.
StreamEffectError
Errors that are possible from a StreamEffect.

Traits§

AsyncBufferCommit
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.
AsyncPlaybackBufferStream
PlaybackBufferStream provides PlaybackBuffers asynchronously to fill with audio samples for playback.
BufferCommit
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
PlaybackBufferStream provides PlaybackBuffers to fill with audio samples for playback.
StreamControl
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
StreamSource creates streams for playback or capture of audio.
StreamSourceGenerator
StreamSourceGenerator is a trait used to abstract types that create StreamSource. It can be used when multiple types of StreamSource are needed.

Functions§

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

Type Aliases§

BoxError
Errors that can pass across threads.