1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-04 15:24:12 +01:00
Arduino/libraries/Audio/examples/SimpleAudioPlayer/SimpleAudioPlayer.ino

85 lines
1.8 KiB
Arduino
Raw Normal View History

/*
Simple Audio Player
Demonstrates the use of the Audio library for the Arduino Due
Hardware required :
* Arduino shield with a SD card on CS4
* A sound file named "test.wav" in the root directory of the SD card
* An audio amplifier to connect to the DAC0 and ground
* A speaker to connect to the audio amplifier
Original by Massimo Banzi September 20, 2012
Modified by Scott Fitzgerald October 19, 2012
This example code is in the public domain
http://www.arduino.cc/en/Tutorial/SimpleAudioPlayer
*/
2012-09-29 04:49:15 +02:00
#include <SD.h>
#include <SPI.h>
#include <Audio.h>
void setup()
{
// debug output at 9600 baud
Serial.begin(9600);
2012-09-29 04:49:15 +02:00
// setup SD-card
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println(" failed!");
return;
}
Serial.println(" done.");
// hi-speed SPI transfers
SPI.setClockDivider(4);
// 44100Khz stereo => 88200 sample rate
// 100 mSec of prebuffering.
Audio.begin(88200, 100);
}
void loop()
{
int count = 0;
2012-09-29 04:49:15 +02:00
// open wave file from sdcard
File myFile = SD.open("test.wav");
2012-09-29 04:49:15 +02:00
if (!myFile) {
// if the file didn't open, print an error and stop
Serial.println("error opening test.wav");
2012-09-29 04:49:15 +02:00
while (true);
}
const int S = 1024; // Number of samples to read in block
short buffer[S];
2012-09-29 04:49:15 +02:00
Serial.print("Playing");
// until the file is not finished
while (myFile.available()) {
// read from the file into buffer
myFile.read(buffer, sizeof(buffer));
// Prepare samples
int volume = 1024;
Audio.prepare(buffer, S, volume);
2012-09-29 04:49:15 +02:00
// Feed samples to audio
Audio.write(buffer, S);
// Every 100 block print a '.'
count++;
if (count == 100) {
Serial.print(".");
count = 0;
}
}
myFile.close();
Serial.println("End of file. Thank you for listening!");
while (true) ;
}