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_streamsto perform async operations.
Structs§
AsyncPlaybackBufferis the async version ofPlaybackBuffer.AudioBufferis one buffer that holds buffer_size audio frames. It is the inner data ofPlaybackBufferandCaptureBuffer.- NoopStream data that is needed from the buffer complete callback.
- Stream that accepts playback samples but drops them.
- No-op control for
NoopStreams. - Source of
NoopStreamandNoopStreamControlobjects. NoopStreamSourceGeneratoris a struct that implementsStreamSourceGeneratorto generateNoopStreamSource.PlaybackBufferis 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§
AsyncBufferCommitis 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.PlaybackBufferStreamprovidesPlaybackBuffers asynchronously to fill with audio samples for playback.BufferCommitis 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.PlaybackBufferStreamprovidesPlaybackBuffers to fill with audio samples for playback.StreamControlprovides a way to set the volume and mute states of a stream.StreamControlis separate from the stream so it can be owned by a different thread if needed.StreamSourcecreates streams for playback or capture of audio.StreamSourceGeneratoris a trait used to abstract types that createStreamSource. It can be used when multiple types ofStreamSourceare needed.
Functions§
- Call
fwith aAsyncPlaybackBuffer, and trigger the buffer done call back after.fshould write playback data to the givenAsyncPlaybackBuffer.
Type Aliases§
- Errors that can pass across threads.