Go Down

Topic: Due Audio Library: use of sample rate counter-intuitive (Read 986 times) previous topic - next topic

Nantonos

Oct 22, 2012, 08:32 pm Last Edit: Oct 22, 2012, 08:36 pm by Nantonos Reason: 1
I came across this in the Due 1.5 Audio > Examples

Code: [Select]
 // 44100Khz stereo => 88200 sample rate
 // 100 mSec of prebuffering.
 Audio.begin(88200, 100);


That is confusing - the sample rate for 44.1k digital audio is, well, 44.1k. Then I noticed the 'stereo' and, following up to
hardware/arduino/sam/libraries/Audio I see in Audio.cpp

Code: [Select]
void AudioClass::begin(uint32_t sampleRate, uint32_t msPreBuffer) {
// Allocate a buffer to keep msPreBuffer milliseconds of audio
bufferSize = msPreBuffer * sampleRate / 1000;


Okay, so stereo needs twice the buffer size of mono. Fair enough. But - is the sample rate actually twice? Is a single DAC being used to produce both left and right channels? Due has two DACs and no obvious way to de-interleave audio from a single pin.

Sometimes, code may need to know the actual sample rate in use. For example, different filtering may be used. Or, the sample rate may be communicated to other devices.

Comon sample rates for audio are:

  • 44.1 kHz (CD red book audio)

  • 48 kHz (DAT audio)

  • 88.2 kHz (pro audio, uncommon)

  • 96 kHz (pro audio, widepread)

  • 192 kHz (pro audio, uncommon)



Notice that many of these values are twice one of the other values. So it is not enough to look at a sample rate and decide if that must be stereo or mono based on the value.

It seems to me that a better way to define Audio.begin would be with three parameters - sample rate, ms of buffer, channels (2 for stereo, 1 for mono. Also allows upward compatibility for 6 (5.1 audio) and 8 (7.1 audio) in the future).

So that would be

Code: [Select]
#define MONO 1
#define STEREO 2

 // 44100Khz stereo
 // 100 mSec of prebuffering.
 Audio.begin(44100, 100, STEREO);


and

Code: [Select]
[code]void AudioClass::begin(uint32_t sampleRate, uint32_t msPreBuffer, uint32_t  channels) {
// Allocate a buffer to keep msPreBuffer milliseconds of audio
bufferSize = msPreBuffer * sampleRate * channels / 1000;

[/code]

Simpler and easier and more extensible.

Go Up