Close

Audio processing

A project log for Piano signal processor

Add metronome, reverb, compression, recording in an outboard box.

lion-mclionheadlion mclionhead 03/18/2021 at 06:590 Comments

There was another experiment to pass analog audio from line input to phones output like a normal musician would do.  The minimum buffer size for ALSA was 64 samples & the latency was negligible.  There's little point to it, since the digital mode ended up working & it gives an exact preview of what's being recorded.



The I2S to USB converter had an obscure parameter for adjusting the packet size.  This could go all the way down to 64 samples while ALSA bottomed out at 64 samples.  This latency was acceptable.   There's still a slight softness in the keys from latency, but it's the best lions can afford for what it is.   

The I2S clock is slightly faster than the sound card clock, so interpolation & extra buffering was necessary.  The clock drift requires a buffer that occasionally reads 1 more sample than it writes.  The mane limitation on buffer size is ALSA reading 64 at a time & I2S writing 64 at a time.  You need 2 64 sample buffers to be sure ALSA always has data to read.  The extra input sample puts it at 129, but audio comes in blocks of 64, so 192 samples is the smallest lag before the interpolation becomes possible.

To make recording work, the file had to be opened O_DIRECT & written with page aligned buffers.  Raw I/O has come a long way since lions last used it, 20 years ago.  There wasn't originally a way to prevent filesystem caching without using a raw partition.  SD cards go a lot faster with O_DIRECT.  After 10 years of not knowing if recording was working, there was finally a live preview of exactly what's being recorded.

Reverb required a real short FFT & floating point.  FFT rather than FIR has proven the only way to cut off the ringing.  With recording, reverb, & metronome, the PI with ondemand frequency scaling managed to keep up with 31% of CPU usage.  The PI automatically goes to 800Mhz.  If the clockspeed is locked to 1.5Ghz, it drops to 18%.  Using doubles instead of floats kicks it up to 20% & doesn't affect the sound.  There's still an annoying artifact in the reverb which peaks at middle A.

An ESP32 which converted directly from I2S to headphones would take several weeks to order but be the most realtime.  It would not be fast enough to do a comparable reverb.


Another strategy was to reprogram the I2S to USB converter to also add reverb, metronome, & a DAC inside the case.  It would output audio directly to the phones while continuing to send data to USB for recording.  It might need a UART interface to control the reverb & metronome.  This too wouldn't have enough horsepower for much of a reverb.

The worst case was to abandon audio processing, use the ALSA mixer to monitor the line input & only use software for playing metronome sounds.

Discussions