News for hackers. Stuff that matters.
Re:Digital Audio 101: Playing Audio from a Microcontroller by riskable on Thursday April 15, 2021 @12:13PM (#26822353)
I'd be more interested to see methods of playing notes like this asynchronously; without having it block the main loop while the notes play.

If you've got multiple cores it's no problem: Just run the note-playing code on a different core. However, if you've only got one core you'll need some method to play those notes without disrupting the main loop.

Re:Digital Audio 101: Playing Audio from a Microcontroller by mycroftcomptro on Thursday April 15, 2021 @12:26PM (#26822567)
It's pretty easy to do with one core, too - I would guess that the author didn't use interrupts or peripherals because they were writing about MCUs in general rather than a specific chipset. The K210 they used as an example is actually targeted towards ML applications.

Most chips will have some sort of PWM or advanced timer peripheral that can generate waveforms on a pin without direct CPU intervention. You write to registers to change the frequency, duty cycle, etc.

Many chips also include asynchronous DACs, but they're usually too low-resolution for high-fidelity audio.

Most microcontrollers also include some form of DMA, which can shuttle data between peripherals and memory without CPU intervention. You can also user timer peripherals to trigger DMA transfers, which lets you send buffered data to a DAC on a KHz schedule while the CPU does other things.

And of course, you can usually use timer peripherals to periodically interrupt the main thread when you don't want to wait in a busy-loop.

Re:Digital Audio 101: Playing Audio from a Microcontroller by Nihilartikel on Thursday April 15, 2021 @12:30PM (#26822642)
It's a little bit more complexity, but cheesy 'multitasking' can be achieved with interrupts on HW timers. The main loop sets the synth 'state' and the interrupts act on it.

In DuinoTune [1] the song playback had a per song 'tick' timer interrupt at 12 per beat which handles note on/off, envelope, pitch/audio glides. And a per-sample timer interrupt which does a mixdown of the voices and updates the hardware PWM duty-cycle to generate the waveform.

[1] https://github.com/blakelivingston/DuinoTune

Re:Digital Audio 101: Playing Audio from a Microcontroller by aappleby on Thursday April 15, 2021 @01:48PM (#26823814)
You set up a timer to fire a "calculate next sample" interrupt at ~4khz, then your main thread just walks through your MIDI file or whatever and tweaks the synthesizer settings.
Re:Digital Audio 101: Playing Audio from a Microcontroller by virgil_disgr4ce on Thursday April 15, 2021 @12:22PM (#26822502)
I mean... a microcontroller has only one 'loop'. If you don't want to 'block' it, you either 1) use external devices, i.e. a synth chip or 2) compose the multiple notes you want into one signal. If you're doing PWM only then option 2 will rapidly start to present interesting challenges.
Re:Digital Audio 101: Playing Audio from a Microcontroller by GeorgeTirebiter on Thursday April 15, 2021 @08:19PM (#26827824)
Counter/Timer peripherals on even low-end chips can help here.
Re:Digital Audio 101: Playing Audio from a Microcontroller by chabes on Thursday April 15, 2021 @12:25PM (#26822550)
Some MCUs have multiple cores
Re:Digital Audio 101: Playing Audio from a Microcontroller by makapuf on Thursday April 15, 2021 @01:59PM (#26823986)
But those dual core are generally way more powerful that a cortex m4 at 100mhz with a dac, where pwm gen is not necessary.