pub struct AudioHandle<T: Sample + AudioSample> {
    pub sample_rate: u32,
    pub channels: u8,
    pub producer: AudioFrameProducer<T>,
    /* private fields */
}
Expand description

The struct for producing and controlling the audio playback.

It embeds the interconnected pair of carousel’s AudioFrameProducer with the AudioFrameConsumer directly exposing the producer to the user. The consumer lives in the cpal audio thread and is responsible for filling up the audio output buffer with sample data sent from the producer.

The T parameter should be one of the sample primitives.

Fields§

§sample_rate: u32

The audio sample frequency of the output stream.

§channels: u8

The number of audio channels in the output stream.

§producer: AudioFrameProducer<T>

The audio sample producer, interconnected with an audio consumer living in the audio thread.

Implementations§

Starts playback of the audio device.

Examples found in repository?
src/host/cpal.rs (line 88)
85
86
87
88
89
90
91
92
    pub fn play(&self) -> Result<(), AudioHandleError> {
        use AudioHandleAnyFormat::*;
        match self {
            I16(audio) => audio.play(),
            U16(audio) => audio.play(),
            F32(audio) => audio.play(),
        }
    }

Pauses playback of the audio device.

Examples found in repository?
src/host/cpal.rs (line 97)
94
95
96
97
98
99
100
101
    pub fn pause(&self) -> Result<(), AudioHandleError> {
        use AudioHandleAnyFormat::*;
        match self {
            I16(audio) => audio.pause(),
            U16(audio) => audio.pause(),
            F32(audio) => audio.pause(),
        }
    }

Closes audio playback and frees underlying resources.

Creates an instance of the AudioHandle from the provided cpal device with the desired audio parameters.

  • config specifies the desired audio parameters.
  • frame_duration_nanos is the duration in nanoseconds of the standard emulation frame.
  • latency is the audio latency passed to the create_carousel.
Examples found in repository?
src/host/cpal.rs (line 163)
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
    pub fn create_with_device_and_config(
            device: &cpal::Device,
            config: &cpal::StreamConfig,
            frame_duration_nanos: u32,
            latency: usize,
        ) -> Result<Self, AudioHandleError>
    {
        Ok(match device.default_output_config()?.sample_format() {
            SampleFormat::I16 => AudioHandleAnyFormat::I16(
                AudioHandle::<i16>::create_with_device_and_config(device, config, frame_duration_nanos, latency)?
            ),
            SampleFormat::U16 => AudioHandleAnyFormat::U16(
                AudioHandle::<u16>::create_with_device_and_config(device, config, frame_duration_nanos, latency)?
            ),
            SampleFormat::F32 => AudioHandleAnyFormat::F32(
                AudioHandle::<f32>::create_with_device_and_config(device, config, frame_duration_nanos, latency)?
            )
        })
    }

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Convert to S a sample type from self.
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.