embedded-audio-core 0.2.1

no_std duty-modulated PWM audio: effect banks, tiered DSP, and mixing for Cortex-M
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
use crate::bank::{EffectEntry, SoundBank};
use crate::config::{AudioConfig, crossfade_step_q8};
use crate::envelope::AdsrSpec;
use crate::error::AudioError;
use crate::fixed::{apply_gain_q8, mix_crossfade};
use crate::output::{
    DutyMode, PwmMapper, limit_bus, pcm_to_dac_u8, pcm_to_dac_u12, pcm_to_dac_u16, pcm_to_i16,
    pcm_to_i32,
};

use crate::voice::Voice;

#[cfg(feature = "fm")]
use crate::output::{FmMapper, FmTick};

/// Policy for dynamic voice allocation when triggering new effects.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum VoiceStealingPolicy {
    /// Steal the lowest-priority voice if all voices are active and new priority is higher.
    #[default]
    LowestPriorityOldest,
    /// Only allocate if a voice channel is completely free.
    FreeChannelOnly,
}

/// N-voice mixer with bank playback, dynamic voice allocation, and duty-modulated PWM output.
pub struct AudioEngine<'a, const N: usize = 2> {
    bank: Option<SoundBank<'a>>,
    config: AudioConfig,
    voices: [Voice<'a>; N],
    mapper: PwmMapper,
    crossfade_t_q8: u8,
    crossfade_step_q8: u8,
    crossfade_active: bool,
    stealing_policy: VoiceStealingPolicy,
    #[cfg(feature = "fm")]
    fm_mapper: FmMapper,
}

impl<'a> AudioEngine<'a, 2> {
    /// Create a standard 2-voice audio engine.
    pub fn new(config: AudioConfig) -> Self {
        Self::with_voice_count(config)
    }

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

    #[cfg(feature = "fm")]
    pub fn new_markham() -> Self {
        use crate::profile::markham;
        Self::new(AudioConfig::new(
            markham::CONTROL_TICK_HZ,
            0,
            DutyMode::Linear,
        ))
    }
}

impl<'a, const N: usize> AudioEngine<'a, N> {
    /// Create an audio engine with generic voice count N.
    pub fn with_voice_count(config: AudioConfig) -> Self {
        let rate = config.sample_rate_hz;
        Self {
            bank: None,
            config,
            voices: [Voice::silent(rate); N],
            mapper: PwmMapper::new(config.duty_mode),
            crossfade_t_q8: 0,
            crossfade_step_q8: 0,
            crossfade_active: false,
            stealing_policy: VoiceStealingPolicy::default(),
            #[cfg(feature = "fm")]
            fm_mapper: FmMapper::markham(),
        }
    }

    pub fn config(&self) -> AudioConfig {
        self.config
    }

    pub fn set_stealing_policy(&mut self, policy: VoiceStealingPolicy) {
        self.stealing_policy = policy;
    }

    pub fn stealing_policy(&self) -> VoiceStealingPolicy {
        self.stealing_policy
    }

    pub fn voice_count(&self) -> usize {
        N
    }

    pub fn active_voice_count(&self) -> usize {
        self.voices.iter().filter(|v| v.is_audible()).count()
    }

    pub fn voice(&self, idx: usize) -> Option<&Voice<'a>> {
        self.voices.get(idx)
    }

    pub fn voice_mut(&mut self, idx: usize) -> Option<&mut Voice<'a>> {
        self.voices.get_mut(idx)
    }

    pub fn set_bank(&mut self, bank: SoundBank<'a>) {
        let rate = bank.sample_rate_hz;
        self.config.sample_rate_hz = rate;
        self.voices = [Voice::silent(rate); N];
        self.bank = Some(bank);
    }

    pub fn set_master_gain_q8(&mut self, gain: u8) {
        self.config.master_gain_q8 = gain;
    }

    pub fn stop_all(&mut self) {
        for v in &mut self.voices {
            v.stop_immediate();
        }
        self.crossfade_active = false;
        self.crossfade_step_q8 = 0;
        self.mapper.reset();
    }

    pub fn is_playing(&self) -> bool {
        self.voices.iter().any(|v| v.is_audible())
    }

    /// Dynamically allocate a voice channel based on priority and stealing policy.
    pub fn allocate_voice(&mut self, priority: u8) -> Option<usize> {
        if N == 0 {
            return None;
        }
        // 1. Look for an inaudible / idle voice
        for (i, v) in self.voices.iter().enumerate() {
            if !v.is_audible() {
                return Some(i);
            }
        }
        // 2. Check stealing policy
        if self.stealing_policy == VoiceStealingPolicy::LowestPriorityOldest {
            let mut lowest_idx = None;
            let mut lowest_prio = priority;
            for (i, v) in self.voices.iter().enumerate() {
                if v.priority < lowest_prio {
                    lowest_prio = v.priority;
                    lowest_idx = Some(i);
                }
            }
            return lowest_idx;
        }
        None
    }

    /// Play effect on an automatically allocated voice channel.
    pub fn play(&mut self, effect_id: u16, adsr: AdsrSpec) -> Result<usize, AudioError> {
        self.play_with_priority(effect_id, adsr, 128)
    }

    /// Play with custom priority on an allocated voice channel.
    pub fn play_with_priority(
        &mut self,
        effect_id: u16,
        adsr: AdsrSpec,
        priority: u8,
    ) -> Result<usize, AudioError> {
        let idx = self.allocate_voice(priority).ok_or(AudioError::VoiceBusy)?;
        let bank = self.bank.ok_or(AudioError::NoBank)?;
        let entry = bank.find_by_id(effect_id)?;
        self.start_on_voice(idx, &bank, entry, adsr, priority)?;
        Ok(idx)
    }

    /// Play custom wavetable on an automatically allocated voice channel.
    pub fn play_wavetable(
        &mut self,
        table: &'a [u8],
        freq_hz: u32,
        adsr: AdsrSpec,
    ) -> Result<usize, AudioError> {
        self.play_wavetable_with_priority(table, freq_hz, adsr, 128)
    }

    /// Play custom wavetable with custom priority on an allocated voice channel.
    pub fn play_wavetable_with_priority(
        &mut self,
        table: &'a [u8],
        freq_hz: u32,
        adsr: AdsrSpec,
        priority: u8,
    ) -> Result<usize, AudioError> {
        if table.len() < 256 {
            return Err(AudioError::InvalidPayload);
        }
        let idx = self.allocate_voice(priority).ok_or(AudioError::VoiceBusy)?;
        let rate = self.config.sample_rate_hz;
        let mut voice = Voice::silent(rate);
        voice.set_gain_q8(255);
        voice.priority = priority;
        voice.source.start_wavetable(table, freq_hz, rate);
        voice.trigger_adsr(adsr);
        self.voices[idx] = voice;
        Ok(idx)
    }

    /// Crossfade voice 0 → `effect_id` on voice 1 over `duration_ms`.
    pub fn crossfade_to(
        &mut self,
        effect_id: u16,
        duration_ms: u16,
        adsr: AdsrSpec,
    ) -> Result<(), AudioError> {
        if N < 2 {
            return Err(AudioError::VoiceBusy);
        }
        let bank = self.bank.ok_or(AudioError::NoBank)?;
        let entry = bank.find_by_id(effect_id)?;
        self.voices[0].release();
        self.start_on_voice(1, &bank, entry, adsr, 128)?;
        self.crossfade_active = true;
        self.crossfade_t_q8 = 0;
        self.crossfade_step_q8 = crossfade_step_q8(duration_ms, self.config.sample_rate_hz);
        Ok(())
    }

    fn advance_crossfade(&mut self) {
        if !self.crossfade_active || N < 2 {
            return;
        }
        let t = self.crossfade_t_q8.saturating_add(self.crossfade_step_q8);
        self.crossfade_t_q8 = t;
        if t == 255 {
            self.voices[0].stop_immediate();
            self.voices[0].source = self.voices[1].source;
            self.voices[0].set_gain_q8(self.voices[1].gain_q8());
            self.voices[0].priority = self.voices[1].priority;
            self.voices[1].stop_immediate();
            self.crossfade_active = false;
            self.crossfade_t_q8 = 0;
        }
    }

    pub fn start_on_voice(
        &mut self,
        idx: usize,
        bank: &SoundBank<'a>,
        entry: EffectEntry,
        adsr: AdsrSpec,
        priority: u8,
    ) -> Result<(), AudioError> {
        if idx >= N {
            return Err(AudioError::VoiceBusy);
        }
        let payload = bank.payload(&entry)?;
        let rate = bank.sample_rate_hz;
        let mut voice = Voice::silent(rate);
        voice.set_gain_q8(entry.default_gain_q8);
        voice.priority = priority;
        if !voice.source.start_from_entry(
            entry.kind,
            entry.flags,
            entry.param0,
            entry.param1,
            payload,
            rate,
        ) {
            return Err(AudioError::InvalidEffectKind);
        }
        voice.trigger_adsr(adsr);
        self.voices[idx] = voice;
        Ok(())
    }

    fn tick_mixed_pcm(&mut self) -> i8 {
        self.advance_crossfade();
        for v in &mut self.voices {
            v.tick_envelope();
        }

        if N == 0 {
            return 0;
        }

        if self.crossfade_active && N >= 2 {
            let sa = self.voices[0].next_sample();
            let sb = self.voices[1].next_sample();
            let mixed = match (sa, sb) {
                (None, None) => 0,
                (Some(s), None) => s as i32,
                (None, Some(s)) => s as i32,
                (Some(sa), Some(sb)) => mix_crossfade(sa, sb, self.crossfade_t_q8) as i32,
            };
            return apply_gain_q8(limit_bus(mixed), self.config.master_gain_q8);
        }

        let mut sum: i32 = 0;
        let mut active_count: i32 = 0;
        for v in &mut self.voices {
            if let Some(s) = v.next_sample() {
                sum += s as i32;
                active_count += 1;
            }
        }

        let mixed = if active_count > 1 {
            sum / active_count
        } else {
            sum
        };

        apply_gain_q8(limit_bus(mixed), self.config.master_gain_q8)
    }

    /// Mixed PCM sample after envelopes (before PWM mapping). Useful for WAV preview.
    pub fn tick_pcm(&mut self) -> i8 {
        self.tick_mixed_pcm()
    }

    #[cfg(feature = "dsp")]
    /// Mixed PCM sample tick normalized to floating-point range `[-1.0, 1.0]`.
    pub fn tick_pcm_f32(&mut self) -> f32 {
        self.tick_mixed_pcm() as f32 / 128.0
    }

    #[cfg(feature = "dsp")]
    /// Fill a buffer with consecutive normalized floating-point PCM samples.
    pub fn fill_pcm_f32_buffer(&mut self, out: &mut [f32]) -> usize {
        for sample in out.iter_mut() {
            *sample = self.tick_pcm_f32();
        }
        out.len()
    }

    /// One audio sample tick → PWM duty compare value.
    pub fn tick(&mut self) -> u16 {
        let pcm = self.tick_mixed_pcm();
        self.mapper.map(pcm, self.config.pwm_period)
    }

    /// Fill a DMA buffer with consecutive duty values (one per sample tick).
    ///
    /// Returns how many slots were written (`out.len()`).
    pub fn fill_duty_buffer(&mut self, out: &mut [u16]) -> usize {
        let period = self.config.pwm_period;
        for duty in out.iter_mut() {
            let pcm = self.tick_mixed_pcm();
            *duty = self.mapper.map(pcm, period);
        }
        out.len()
    }

    /// Fill a DMA buffer with consecutive signed 8-bit PCM samples (-128..=127).
    pub fn fill_pcm_i8_buffer(&mut self, out: &mut [i8]) -> usize {
        for slot in out.iter_mut() {
            *slot = self.tick_mixed_pcm();
        }
        out.len()
    }

    /// Fill a DMA buffer with consecutive signed 16-bit PCM samples (-32768..=32767).
    pub fn fill_pcm_i16_buffer(&mut self, out: &mut [i16]) -> usize {
        for slot in out.iter_mut() {
            let pcm = self.tick_mixed_pcm();
            *slot = pcm_to_i16(pcm);
        }
        out.len()
    }

    /// Fill a DMA buffer with consecutive signed 32-bit PCM samples (24-bit aligned).
    pub fn fill_pcm_i32_buffer(&mut self, out: &mut [i32]) -> usize {
        for slot in out.iter_mut() {
            let pcm = self.tick_mixed_pcm();
            *slot = pcm_to_i32(pcm);
        }
        out.len()
    }

    /// Fill a DMA buffer with consecutive unsigned 8-bit DAC values (0..=255).
    pub fn fill_dac_u8_buffer(&mut self, out: &mut [u8]) -> usize {
        for slot in out.iter_mut() {
            let pcm = self.tick_mixed_pcm();
            *slot = pcm_to_dac_u8(pcm);
        }
        out.len()
    }

    /// Fill a DMA buffer with consecutive unsigned 12-bit DAC values (0..=4095, e.g. STM32 DAC1).
    pub fn fill_dac_u12_buffer(&mut self, out: &mut [u16]) -> usize {
        for slot in out.iter_mut() {
            let pcm = self.tick_mixed_pcm();
            *slot = pcm_to_dac_u12(pcm);
        }
        out.len()
    }

    /// Fill a DMA buffer with consecutive unsigned 16-bit DAC values (0..=65535).
    pub fn fill_dac_u16_buffer(&mut self, out: &mut [u16]) -> usize {
        for slot in out.iter_mut() {
            let pcm = self.tick_mixed_pcm();
            *slot = pcm_to_dac_u16(pcm);
        }
        out.len()
    }

    /// Fill an interleaved stereo 16-bit PCM buffer (duplicating mono mixed sample to L and R channels).
    pub fn fill_stereo_i16_buffer(&mut self, out: &mut [i16]) -> usize {
        let mono_count = out.len() / 2;
        for i in 0..mono_count {
            let pcm = self.tick_mixed_pcm();
            let sample = pcm_to_i16(pcm);
            out[i * 2] = sample;
            out[i * 2 + 1] = sample;
        }
        mono_count * 2
    }

    /// Fill an interleaved stereo 32-bit PCM buffer (for 24-bit / 32-bit SAI / I2S DMA peripherals).
    pub fn fill_stereo_i32_buffer(&mut self, out: &mut [i32]) -> usize {
        let mono_count = out.len() / 2;
        for i in 0..mono_count {
            let pcm = self.tick_mixed_pcm();
            let sample = pcm_to_i32(pcm);
            out[i * 2] = sample;
            out[i * 2 + 1] = sample;
        }
        mono_count * 2
    }

    /// Generic buffer filling using a custom sample mapping closure.
    pub fn fill_custom_buffer<T>(
        &mut self,
        out: &mut [T],
        mut map_fn: impl FnMut(i8) -> T,
    ) -> usize {
        for slot in out.iter_mut() {
            let pcm = self.tick_mixed_pcm();
            *slot = map_fn(pcm);
        }
        out.len()
    }

    /// Alias for [`Self::tick`].
    #[inline]
    pub fn tick_pwm(&mut self) -> u16 {
        self.tick()
    }

    /// Tier A tone without a bank.
    pub fn play_tone(&mut self, freq_hz: u32, duration_ms: u16, waveform: crate::synth::Waveform) {
        let rate = self.config.sample_rate_hz;
        if N > 0 {
            self.voices[0] = Voice::silent(rate);
            self.voices[0]
                .source
                .start_tone(freq_hz, duration_ms, waveform, rate);
            self.voices[0].trigger_adsr(AdsrSpec::click());
        }
    }

    #[cfg(feature = "fm")]
    pub fn set_fm_mapper(&mut self, mapper: FmMapper) {
        self.fm_mapper = mapper;
    }

    #[cfg(feature = "fm")]
    #[allow(clippy::collapsible_if)]
    /// FM buzzer backend (optional; not used for duty-modulation products).
    pub fn tick_fm(&mut self) -> FmTick {
        let pcm = self.tick_mixed_pcm();
        let rate = self.config.sample_rate_hz;

        if !self.crossfade_active && N > 0 {
            if let Some(hz) = self.voices[0].source.carrier_hz(rate) {
                return FmMapper::from_carrier(hz, self.voices[0].source.is_active());
            }
        }

        self.fm_mapper.map_pcm(pcm)
    }
}

impl Default for AudioEngine<'static, 2> {
    fn default() -> Self {
        Self::new(AudioConfig::default())
    }
}