Skip to main content

AudioEngine

Struct AudioEngine 

Source
pub struct AudioEngine<'a, const N: usize = 2> { /* private fields */ }
Expand description

N-voice mixer with bank playback, dynamic voice allocation, and duty-modulated PWM output.

Implementations§

Source§

impl<'a> AudioEngine<'a, 2>

Source

pub fn new(config: AudioConfig) -> Self

Create a standard 2-voice audio engine.

Source

pub fn from_sample_rate( sample_rate_hz: u32, pwm_period: u16, duty_mode: DutyMode, ) -> Self

Source§

impl<'a, const N: usize> AudioEngine<'a, N>

Source

pub fn with_voice_count(config: AudioConfig) -> Self

Create an audio engine with generic voice count N.

Source

pub fn config(&self) -> AudioConfig

Source

pub fn set_stealing_policy(&mut self, policy: VoiceStealingPolicy)

Source

pub fn stealing_policy(&self) -> VoiceStealingPolicy

Source

pub fn voice_count(&self) -> usize

Source

pub fn active_voice_count(&self) -> usize

Source

pub fn voice(&self, idx: usize) -> Option<&Voice<'a>>

Source

pub fn voice_mut(&mut self, idx: usize) -> Option<&mut Voice<'a>>

Source

pub fn set_bank(&mut self, bank: SoundBank<'a>)

Source

pub fn set_master_gain_q8(&mut self, gain: u8)

Source

pub fn stop_all(&mut self)

Source

pub fn is_playing(&self) -> bool

Source

pub fn allocate_voice(&mut self, priority: u8) -> Option<usize>

Dynamically allocate a voice channel based on priority and stealing policy.

Source

pub fn play( &mut self, effect_id: u16, adsr: AdsrSpec, ) -> Result<usize, AudioError>

Play effect on an automatically allocated voice channel.

Source

pub fn play_with_priority( &mut self, effect_id: u16, adsr: AdsrSpec, priority: u8, ) -> Result<usize, AudioError>

Play with custom priority on an allocated voice channel.

Source

pub fn play_wavetable( &mut self, table: &'a [u8], freq_hz: u32, adsr: AdsrSpec, ) -> Result<usize, AudioError>

Play custom wavetable on an automatically allocated voice channel.

Source

pub fn play_wavetable_with_priority( &mut self, table: &'a [u8], freq_hz: u32, adsr: AdsrSpec, priority: u8, ) -> Result<usize, AudioError>

Play custom wavetable with custom priority on an allocated voice channel.

Source

pub fn crossfade_to( &mut self, effect_id: u16, duration_ms: u16, adsr: AdsrSpec, ) -> Result<(), AudioError>

Crossfade voice 0 → effect_id on voice 1 over duration_ms.

Source

pub fn start_on_voice( &mut self, idx: usize, bank: &SoundBank<'a>, entry: EffectEntry, adsr: AdsrSpec, priority: u8, ) -> Result<(), AudioError>

Source

pub fn tick_pcm(&mut self) -> i8

Mixed PCM sample after envelopes (before PWM mapping). Useful for WAV preview.

Source

pub fn tick(&mut self) -> u16

One audio sample tick → PWM duty compare value.

Source

pub fn fill_duty_buffer(&mut self, out: &mut [u16]) -> usize

Fill a DMA buffer with consecutive duty values (one per sample tick).

Returns how many slots were written (out.len()).

Source

pub fn fill_pcm_i8_buffer(&mut self, out: &mut [i8]) -> usize

Fill a DMA buffer with consecutive signed 8-bit PCM samples (-128..=127).

Source

pub fn fill_pcm_i16_buffer(&mut self, out: &mut [i16]) -> usize

Fill a DMA buffer with consecutive signed 16-bit PCM samples (-32768..=32767).

Source

pub fn fill_pcm_i32_buffer(&mut self, out: &mut [i32]) -> usize

Fill a DMA buffer with consecutive signed 32-bit PCM samples (24-bit aligned).

Source

pub fn fill_dac_u8_buffer(&mut self, out: &mut [u8]) -> usize

Fill a DMA buffer with consecutive unsigned 8-bit DAC values (0..=255).

Source

pub fn fill_dac_u12_buffer(&mut self, out: &mut [u16]) -> usize

Fill a DMA buffer with consecutive unsigned 12-bit DAC values (0..=4095, e.g. STM32 DAC1).

Source

pub fn fill_dac_u16_buffer(&mut self, out: &mut [u16]) -> usize

Fill a DMA buffer with consecutive unsigned 16-bit DAC values (0..=65535).

Source

pub fn fill_stereo_i16_buffer(&mut self, out: &mut [i16]) -> usize

Fill an interleaved stereo 16-bit PCM buffer (duplicating mono mixed sample to L and R channels).

Source

pub fn fill_stereo_i32_buffer(&mut self, out: &mut [i32]) -> usize

Fill an interleaved stereo 32-bit PCM buffer (for 24-bit / 32-bit SAI / I2S DMA peripherals).

Source

pub fn fill_custom_buffer<T>( &mut self, out: &mut [T], map_fn: impl FnMut(i8) -> T, ) -> usize

Generic buffer filling using a custom sample mapping closure.

Source

pub fn tick_pwm(&mut self) -> u16

Alias for Self::tick.

Source

pub fn play_tone(&mut self, freq_hz: u32, duration_ms: u16, waveform: Waveform)

Tier A tone without a bank.

Trait Implementations§

Source§

impl Default for AudioEngine<'static, 2>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a, const N: usize> Freeze for AudioEngine<'a, N>
where [Voice<'a>; N]: Freeze,

§

impl<'a, const N: usize> RefUnwindSafe for AudioEngine<'a, N>
where [Voice<'a>; N]: RefUnwindSafe,

§

impl<'a, const N: usize> Send for AudioEngine<'a, N>
where [Voice<'a>; N]: Send,

§

impl<'a, const N: usize> Sync for AudioEngine<'a, N>
where [Voice<'a>; N]: Sync,

§

impl<'a, const N: usize> Unpin for AudioEngine<'a, N>
where [Voice<'a>; N]: Unpin,

§

impl<'a, const N: usize> UnsafeUnpin for AudioEngine<'a, N>
where [Voice<'a>; N]: UnsafeUnpin,

§

impl<'a, const N: usize> UnwindSafe for AudioEngine<'a, N>
where [Voice<'a>; N]: UnwindSafe,

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.

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.

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.