Struct dynwave::AudioPlayer
source · pub struct AudioPlayer<T: Sample> { /* private fields */ }
Expand description
The AudioPlayer
struct represents an audio player that can play audio samples stream
coming from an external generating source, such as an emulator.
The AudioPlayer
may resample the audio if the generated sample rate is not supported by the audio device,
which may cause a slight performance hit due to the resampling process. If the machine supports the input sample rate,
no resampling will be done, and the audio samples will be used as is.
§Example
Here’s an example of how to use the AudioPlayer
:
// create a buffer, that can hold 1 second worth of samples
// (base it depend on how fast you generate samples, less buffer is better for latency)
let mut player = AudioPlayer::<f32>::new(44100, BufferSize::OneSecond).unwrap();
// Start playing the audio
player.play().unwrap();
// generate audio samples (can be done in a emulation loop for example)
let samples = generate_samples();
player.queue(&samples);
// pause the audio
player.pause().unwrap();
Implementations§
source§impl<T: Sample + SizedSample> AudioPlayer<T>
impl<T: Sample + SizedSample> AudioPlayer<T>
sourcepub fn new(
sample_rate: u32,
buffer_size: BufferSize
) -> Result<Self, AudioPlayerError>
pub fn new( sample_rate: u32, buffer_size: BufferSize ) -> Result<Self, AudioPlayerError>
Creates a new instance of AudioPlayer
.
§Parameters
sample_rate
: The sample rate of the audio player in Hz. Common values are44100
or48000
.buffer_size
: The size of the buffer that will store the audio samples. SeeBufferSize
for options.
§Returns
Might return an Error
if:
- No output device is found
- The output device does not support dual channel
- Some error happened with the device backend
- Could not create the audio stream
Check AudioPlayerError
for more information about the possible errors.
§Example
let sample_rate = 44100;
let buffer_size = BufferSize::HalfSecond;
let player = AudioPlayer::<f32>::new(sample_rate, buffer_size).unwrap();
This example creates a new AudioPlayer
with a sample rate of 44100 Hz and a buffer size of half a second.
sourcepub fn play(&self) -> Result<(), PlayError>
pub fn play(&self) -> Result<(), PlayError>
Start the player
If the player is playing and if the buffer is emptied (played until finished without adding more data), popping sound might be heard.
Might return an Error
if:
- The device associated with the stream is no longer available
- Some error happened with the device backend
Check PlayError
for more information about the possible errors.
sourcepub fn pause(&self) -> Result<(), PlayError>
pub fn pause(&self) -> Result<(), PlayError>
Pause the player
Might return an Error
if:
- The device associated with the stream is no longer available
- Some error happened with the device backend
Check PlayError
for more information about the possible errors.
sourcepub fn queue(&mut self, data: &[T])
pub fn queue(&mut self, data: &[T])
Queues audio samples to be played.
The queue
function takes a slice of audio samples and adds them to the buffer. If a resampler
is present,
it resamples the audio data before adding it to the buffer.
If the buffer is full, the function will drop the audio samples that don’t fit in the buffer and won’t block.
If the player is playing, the audio samples will be played immediately, and if the buffer is emptied, popping sound might be heard.
§Parameters
data
: A slice of audio samples to be played.
§Example
let sample_rate = 44100;
let buffer_size = BufferSize::HalfSecond;
let mut player = AudioPlayer::new(sample_rate, buffer_size).unwrap();
let samples = vec![0.5, 0.7, 0.9, 1.0, 0.9, 0.7, 0.5, 0.3, 0.1];
player.queue(&samples);
This example creates a new AudioPlayer
with a sample rate of 44100 Hz and a buffer size of half a second, queues some audio samples, and then starts playing the audio.