Flash-memory usage for PCM

A project log for Storing and playing back lofi audio on an MCU

Software and hardware for storing 8-kHz, 8-bit (or less) audio on an AVR MCU, and playing it back

Johan CarlssonJohan Carlsson 05/17/2022 at 23:370 Comments

Out-of-order log here. I thought I had posted this a while ago, but I only made a plot. Anyway, the results found inspired me to try to do PCM on an ATtiny85.

The topic if this log is simply "how many seconds of 8-bit audio can you fit into a given amount of flash memory". To answer that, below is a plot of the output from avr-size (the dec column). The input to avr-size is the executable built from a code snippet that includes a raudio header file and plays it back using PCM. Code built with avr-gcc v. 11.2.1 using options "-Os -mmcu=atmega328p".

So for an ATmega328 (with 32 kB of flash) you can play 3.89 s of 8-bit audio and for an ATtiny85 (with 8 kB of flash) 0.89 s. The exact formula is flash usage in bytes m = 859 + 8,000 x s, where s is the length of the 8-bit audio sample in seconds. Of the 859 bytes 5 are raudio meta data (bit depth and sample length) and 854 are the compiled code that does PCM.

For bit-crushed raudio you can obviously fit longer samples into flash, 2x more for 4-bit, 4x more for 2-bit and 8x more for 1-bit. So for 1-bit raudio you could fit a 31.1 s sample into the flash of an ATmega328 and 7.1 s on an ATtiny85. Generalized formula: m = 859 + 1,000 x d x s, where d is the bit depth.

So you won't be putting your music collection in flash, but there should be enough space to add an aural modality to your AVR MCU project.