Hardware Reference
In-Depth Information
To play back Audio i les from the SD card, you will also require the SD and
SPI libraries; import
SD.h
and
SPI.h
.
#include <SD.h>
#include <SPI.h>
NOTE
The Arduino Due is supported only in the versions of the Arduino IDE.
Version 1.0 does not support the Due, and you cannot import the Audio library from
the menu. Version 1.5 and above support both the Arduino Due and the Audio library.
To initiate the Audio library, you run
begin()
.
Audio.begin(rate, size);
This function takes two arguments: the
rate
and a
size
. The audio rate is the
number of samples per second; for example, 22050 or 44100 are typical values.
For stereo audio i les, you must double the audio rate (44100 for 22.05 kHz and
88200 for 44.1 kHz). The
size
parameter indicates the size of an audio buffer
that will be created by this function, in milliseconds. For example, to prepare
the Arduino Due to play a 44.1-kHz stereo i le with a 100-millisecond buffer,
use the following:
// 44100Khz stereo => 88200 sample rate
// 100 mSec of prebuffering.
Audio.begin(88200, 100);
When the Audio library is ready, you must prepare your samples to be played.
This is done with the
prepare()
function:
Audio.prepare(buffer, samples, volume);
The
buffer
parameter is the name of a buffer created by your sketch; it is
not the audio buffer created by the
begin()
function. The
samples
parameter
is the number of samples to write, and the
volume
parameter is the volume of
the audio output, expressed as a 10-bit number; 0 is a silent output, and 1023
is the maximum volume possible.
The i nal step is to write the data into the audio buffer using the
write()
function.
Audio.write(buffer, length);
The
buffer
parameter and the length parameter are identical to the
parameters used in the
prepare()
function. This function writes the samples
to the internal audio buffer. If the audio file is not played, playback
commences. If the i le is currently played, this adds the samples to the end of
the internal buffer.
Search WWH ::
Custom Search