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§
- Traits required by
audio_streams
to perform async operations.
Structs§
AsyncPlaybackBuffer
is the async version ofPlaybackBuffer
.AudioBuffer
is one buffer that holds buffer_size audio frames. It is the inner data ofPlaybackBuffer
andCaptureBuffer
.- NoopStream data that is needed from the buffer complete callback.
- Stream that accepts playback samples but drops them.
- No-op control for
NoopStream
s. - Source of
NoopStream
andNoopStreamControl
objects. NoopStreamSourceGenerator
is a struct that implementsStreamSourceGenerator
to generateNoopStreamSource
.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§
- Errors that are possible from a
PlaybackBuffer
. - Errors that are possible from a
SampleFormat
. - Valid directions of an audio stream.
- Valid effects for an audio stream.
- Errors that are possible from a
StreamEffect
.
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
providesPlaybackBuffer
s 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
providesPlaybackBuffer
s 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 createStreamSource
. It can be used when multiple types ofStreamSource
are needed.
Functions§
- Call
f
with aAsyncPlaybackBuffer
, and trigger the buffer done call back after.f
should write playback data to the givenAsyncPlaybackBuffer
.
Type Aliases§
- Errors that can pass across threads.