Multichannel SD Audio Mixing
Making a MOD player without much RAM.

TRAXMOD
Main Page
Support Forum

By Edward Schlunder / K9spud LLC
April 1st, 2008

Introduction

Previously, in our SD Caching Methodology paper, we described a system of caching 64 byte blocks of SD card data in RAM in order to achieve playback of multichannel digital audio from an SD Card. While that method does work, it hits some major scaling limitations when trying to play multichannel audio beyond 4 or 8 channels. Because each channel requires two 64 byte RAM buffers, RAM becomes prohibitively tight:
ChannelsRAM Required
4512
81024
121536
162048
202560
243072
283584
324096
At 32 channels, half of the LPC2103's 8KB of RAM is gone before we even consider space for the output audio buffer, music pattern data, sample metadata, instrument metadata, channel metadata, FAT fragmentation dereference table, stack space, and other program variables used for running the whole show. Playing 32 channel IT files with lots of New Note Actions will never be possible using this algorithm and a cheap little microcontroller.

MMC/SD Card Theoretical Limits

Let's take a step back and look at the theoretical limits of the SD Card. If audio was perfectly interleved and contiguously placed on the SD Card, how many channels of audio could we possibly stream off of the SD Card? SD Cards accept a maximum clock rate of 25MHz, and using SPI, it takes a minimum of 16 clocks to retrieve each 16-bit sample.

An SD Card could, in theory, play up to 35 channels of audio sampled at 44.1KHz with 16 bits of resolution, under ideal conditions (contiguous, interleaved audio data).

While MOD files do not have perfectly interleaved audio data, they do have vastly less demanding samples to mix. A typical MOD file is 4 channels of audio, sampled at around 8KHz, with only 8 bits of resolution. Going by these numbers, it seems reasonable to assume that an SD Card should easily play a MOD file with audio data being streamed off of the SD Card.

Mixing Algorithm

Instead of keeping two 64 byte RAM buffers for each audio channel, let's use only two 512 byte RAM buffers total, no matter how many audio channels we have. 512 bytes is chosen because that is the default block size used for SD Cards, and it will be a required block size for future SDHC Cards that need to address more than 4GB of space.

Basically, we want the SD Card to be loading sample data for a channel into one of the two 512 byte RAM buffers, while the other RAM buffer is being mixed into the output audio. When the SD Card finishes reading, the SD Card will read another channel's sample data into the other 512 byte RAM buffer and the recently loaded RAM buffer will be mixed into the output audio.

It gets complicated because each channel will mix a different length of data into the output audio depending on the frequency of the note that the channel is playing.


Copyright © 2008, K9spud LLC.