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_streamsto perform async operations. - capture
- shm_
streams
Structs§
- Async
Playback Buffer AsyncPlaybackBufferis the async version ofPlaybackBuffer.- Audio
Buffer 🔒 AudioBufferis one buffer that holds buffer_size audio frames. It is the inner data ofPlaybackBufferandCaptureBuffer.- Noop
Buffer 🔒Commit - NoopStream data that is needed from the buffer complete callback.
- Noop
Stream - Stream that accepts playback samples but drops them.
- Noop
Stream Control - No-op control for
NoopStreams. - Noop
Stream Source - Source of
NoopStreamandNoopStreamControlobjects. - Noop
Stream Source Generator NoopStreamSourceGeneratoris a struct that implementsStreamSourceGeneratorto generateNoopStreamSource.- Playback
Buffer 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§
- Error
- Playback
Buffer Error - Errors that are possible from a
PlaybackBuffer. - Sample
Format - Sample
Format Error - Errors that are possible from a
SampleFormat. - Stream
Direction - Valid directions of an audio stream.
- Stream
Effect - Valid effects for an audio stream.
- Stream
Effect Error - Errors that are possible from a
StreamEffect.
Traits§
- Async
Buffer Commit 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.- Async
Playback Buffer Stream PlaybackBufferStreamprovidesPlaybackBuffers asynchronously to fill with audio samples for playback.- Buffer
Commit 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.- Playback
Buffer Stream PlaybackBufferStreamprovidesPlaybackBuffers to fill with audio samples for playback.- Stream
Control 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.- Stream
Source StreamSourcecreates streams for playback or capture of audio.- Stream
Source Generator StreamSourceGeneratoris a trait used to abstract types that createStreamSource. It can be used when multiple types ofStreamSourceare needed.
Functions§
- async_
write_ playback_ buffer - Call
fwith aAsyncPlaybackBuffer, and trigger the buffer done call back after.fshould write playback data to the givenAsyncPlaybackBuffer.
Type Aliases§
- BoxError
- Errors that can pass across threads.