Trait pxl::Synthesizer[][src]

pub trait Synthesizer: Send + 'static {
    fn synthesize(
        &mut self,
        _samples_played: u64,
        _output_buffer: &mut [Sample]
    ) { ... } }

Trait for things that can generate sound

When a computer program plays audio, it typically generates audio samples that are fed to a DAC, a digital-to-analog converter. This is highly latency-sensitive: audible pops and crackles may be heard if the program does not generate samples quickly enough.

A pxl::Program may not be available to generate samples at all times, for example if it is busy rendering. To make sure that samples for the underlying audio hardware can be generated at all times, even during rendering, audio-sample synthesis is delegated to a dedicated Synthesizer object.

A pxl::Program that wishes to play audio should return an Arc<Mutex<Synthesizer>> from Program::synthesizer. Synthesizer::synthesize will be called as needed on the returned Sythesizer object to generate samples to feed the underlying audio hardware.

To communicate with and update the Synthesizer, the pxl::Program implementation should keep it's own Arc<Mutex<Synthesizer>> with a reference to the synthesizer object, locking it and updating it as needed.

In order to avoid starving the audio hardware of samples, the lock holding the synthesizer should only be kept locked briefly.

Provided Methods

Synthesize audio

Called by the runtime as needed to fill the outgoing audio buffer

  • samples_played — number of samples written by previous calls to synthesize
  • output_buffer — the audio samples that synthesize should write

Implementors