4 ms·
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 mult
by riskable 6y ago
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.
- virgil_disgr4ce 6y agoI 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.
- chabes 6y agoSome MCUs have multiple cores
- makapuf 6y agoBut those dual core are generally way more powerful that a cortex m4 at 100mhz with a dac, where pwm gen is not necessary.
- GeorgeTirebiter 6y agoCounter/Timer peripherals on even low-end chips can help here.
- mycroftcomptro 6y agoIt'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.
- Nihilartikel 6y agoIt'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 https://github.com/blakelivingston/DuinoTune
- aappleby 6y agoYou 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.