Pages: [1]   Go Down
Author Topic: Due Audio Library: use of sample rate counter-intuitive  (Read 967 times)
0 Members and 1 Guest are viewing this topic.
Nice, France
Offline Offline
Full Member
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I came across this in the Due 1.5 Audio > Examples

 // 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

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

#define MONO 1
#define STEREO 2

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


[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;

Simpler and easier and more extensible.
« Last Edit: October 22, 2012, 01:36:20 pm by Nantonos » Logged

Pages: [1]   Go Up
Jump to: