Wacky Knobs

← Lab / Understanding audio DSP code, sorry it’s only math :)

· 9 min read

How it looks on real hardware: a delay on an STM32

← Delay in code

In the last article the delay was a loop: “for each input sample x”. On a real chip samples do not arrive in your function one by one: they come in blocks, the DMA brings them, and the CPU has a strict deadline to process each block. The math does not change by a single line. What changes is who brings the samples, and when.

The path of sound through a digital pedal

  1. Analog input. A buffer and a filter get the guitar signal ready for the chip.
  2. Codec (ADC). Turns voltage into numbers 48,000 times a second, usually as 16- or 24-bit integers.
  3. I2S / SAI. The serial link the codec uses to send samples to the microcontroller. On an STM32 that is the SAI peripheral (or SPI in I2S mode).
  4. DMA. A courier that copies samples from the SAI peripheral into RAM without the CPU.
  5. Your code. The CPU takes a block from RAM, runs it through the delay and writes the result into another buffer.
  6. Back out. The DMA carries the result to the codec (DAC), which turns it back into voltage, and the sound goes to the amp.

Why blocks, and why DMA

At 48 kHz a new sample arrives every 20.8 microseconds. If the CPU got an interrupt for every sample, it would drop everything 48,000 times a second, save its state, process one number and return. Entering and leaving an interrupt has a cost, so a large part of the time would go on paperwork. So the DMA collects samples into a buffer and the CPU is only called when a whole block is ready, say 64 samples. The loop from the last article then simply runs 64 times in a row.

The double buffer (ping-pong)

One problem is left: while the CPU processes a block, the DMA must not write over it. The answer is a buffer of double length split into two halves. The DMA fills half A. When A is full, a half complete interrupt fires and the DMA moves on to half B, while the CPU processes A. When B is full, the complete interrupt fires, the DMA wraps back to A and the CPU processes B. Round and round, just like the ring buffer from last time, only with two big cells.

That gives the one rule of embedded audio: the CPU has to finish a block before the DMA comes to collect it. The deadline is exactly one half, BLOCK / 48000 seconds. If it is late, the DMA waits for nobody. It sends whatever is in the buffer, which is an old block, and you hear crackling or buzzing.

What to try in the simulator

  • An overloaded CPU. Push “effect work” over 100 %. The CPU no longer makes a single block, and the DMA sends nothing but stale data. It buzzes at 48000 / (2 × BLOCK), because the same old buffer repeats.
  • A stall on a small block. Turn on the 3 ms stall with block 64. The deadline is only 1.33 ms, so every stall breaks one or two blocks and you hear a click.
  • The same stall on a big block. Switch to 256. The deadline is now 5.33 ms and the stall fits in it. The sound is clean, but the latency is 10.7 ms.
  • Latency by ear. Block 1024 and the direct signal. The same sound arrives twice, 42.7 ms apart, so you hear a short echo without any delay effect at all.

The code, line by line

Above is real C for an STM32 with the HAL library, cut down to what matters. Setting up pins, clocks and the codec itself (clicked together in CubeMX) is left out.

  1. BLOCK is the size of one half. The buffer has two, so it is 2 * BLOCK long.
  2. HAL_SAI_Receive_DMA / Transmit_DMA start the DMA once, at the beginning. The channel is set to circular, so after the end of the buffer it starts over by itself. The CPU never touches it again.
  3. The HalfCplt and Cplt callbacks are the two interrupts. The first says “half A is ready”, the second “half B is ready”. Both call the same function, just with the other half.
  4. process is the loop from the last article. The only new part is the conversion: the codec sends integers (−32768 … 32767) and the effect works with numbers from −1 to 1. On the way out the result is clipped and turned back into an integer, because a number above 32767 would wrap around to the negative side and crack.

A real project has a couple more details. The codec sends stereo, so the samples in the buffer go left, right, left, right. And input and output usually share one clock, so the rx and tx DMA run in step, just like in the simulator.

Latency: how late the clean sound is

A sample first waits for its half to fill up (BLOCK samples), then waits for the DMA to reach the processed block (another BLOCK). So the latency is 2 × BLOCK samples, plus about 1 ms for the filters inside the codec. Sound travels about 34 cm per millisecond, so 3 ms is like standing a meter from your amp. Nobody notices that. Above 10 ms most guitarists start to feel their tone “running away”.

BLOCKCallback deadlineLatency (without codec)Interrupts per second
160.33 ms0.67 ms3,000
320.67 ms1.33 ms1,500
641.33 ms2.67 ms750
1282.67 ms5.33 ms375
2565.33 ms10.7 ms188
102421.3 ms42.7 ms47

A big block survives stalls and spends less on interrupts, but it is late. A small block is barely late, but the CPU always has to be ready. Pedals usually pick 32 to 64 samples per half.

Memory in real life

This is where hardware differs most from a browser. A 2-second delay in float numbers needs 384 KB, and many microcontrollers do not have that much RAM in total. The table shows the theoretical maximum for a mono delay at 48 kHz, if all of the RAM went to the buffer:

ChipRAMfloat (4 B)int16 (2 B)
STM32F411128 KBabout 0.7 sabout 1.4 s
STM32F407192 KBabout 1 sabout 2 s
STM32H7501 MBabout 5 sabout 10 s
Daisy Seed (H750 + 64 MB SDRAM)64 MBabout 5.8 minabout 11 min

That is why the delay buffer is often stored as int16, half the memory, while the math runs in float. Or external SDRAM is added, as on the Daisy Seed board.

Three traps everyone falls into once

  • The cache on the STM32H7. The H7 has a data cache, and the DMA writes straight to RAM. The CPU can then read a stale copy from the cache. Before reading the rx buffer call SCB_InvalidateDCache_by_Addr, and after writing the tx buffer SCB_CleanDCache_by_Addr. The other option is to put the buffers in a part of RAM that is not cached.
  • Nothing slow in the callback. printf, waiting for a button, drawing on a display or writing to an SD card have no place in an interrupt. Those are exactly the stalls from the simulator. The callback only computes sound, and everything else goes in the main loop.
  • Clip before converting. If a y above 1 is turned straight into an int16_t, the number wraps to the opposite side and you hear a crack, not gentle distortion.

What next

This frame (DMA, a double buffer and a callback that calls process) stays the same for every effect in this series. Chorus, tremolo, overdrive or reverb only change what happens inside the loop. Once the straight path through the chip works without crackles, everything else is the math from the other articles.

More in Understanding audio DSP code, sorry it’s only math :)

← Lab