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>

source

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 are 44100 or 48000.
  • buffer_size: The size of the buffer that will store the audio samples. See BufferSize 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.

source

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.

source

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.

source

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.

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for AudioPlayer<T>

§

impl<T> !Send for AudioPlayer<T>

§

impl<T> !Sync for AudioPlayer<T>

§

impl<T> Unpin for AudioPlayer<T>
where T: Unpin,

§

impl<T> !UnwindSafe for AudioPlayer<T>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<S> FromSample<S> for S

§

fn from_sample_(s: S) -> S

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

§

fn to_sample_(self) -> U

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,