// Simple WAV file player example
//
// Three types of output may be used, by configuring the code below.
//
// 1: Digital I2S - Normally used with the audio shield:
// http://www.pjrc.com/store/teensy3_audio.html
//
// 2: Digital S/PDIF - Connect pin 22 to a S/PDIF transmitter
// https://www.oshpark.com/shared_projects/KcDBKHta
//
// 3: Analog DAC - Connect the DAC pin to an amplified speaker
// http://www.pjrc.com/teensy/gui/?info=AudioOutputAnalog
//
// To configure the output type, first uncomment one of the three
// output objects. If not using the audio shield, comment out
// the sgtl5000_1 lines in setup(), so it does not wait forever
// trying to configure the SGTL5000 codec chip.
//
// The SD card may connect to different pins, depending on the
// hardware you are using. Uncomment or configure the SD card
// pins to match your hardware.
//
// Data files to put on your SD card can be downloaded here:
// http://www.pjrc.com/teensy/td_libs_AudioDataFiles.html
//
// This example code is in the public domain.
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
AudioPlaySerialflashRaw playRaw;
#define USE_PROP_SHIELD 0
#define USE_ANALOG_DAC 0
#define USE_ANALOG_DACS 0
#define USE_SPDIF 0
#define USE_AUDIO_SHIELD 1
#define USE_I2S 1
#define USE_AUDIO_SPI 0
// Output devices
#if USE_I2S || USE_AUDIO_SHIELD
AudioOutputI2S audioOutput;
#if USE_AUDIO_SHIELD
AudioControlSGTL5000 sgtl5000;
#endif
#endif
#if USE_SPDIF
AudioOutputSPDIF audioOutput;
#endif
#if USE_ANALOG_DAC || USE_PROP_SHIELD
AudioOutputAnalog audioOutput;
#endif
#if USE_ANALOG_DACS
AudioOutputAnalogStereo audioOutput;
#endif
AudioConnection patchCord1 (playRaw, 0, audioOutput, 0);
// Use these with the Teensy Audio Shield
#if (USE_AUDIO_SHIELD || USE_AUDIO_SPI) && !defined(__IMXRT1062__)
#define MOSI_PIN 7
#define SCK_PIN 14
#else
#define MOSI_PIN 11
#define SCK_PIN 13
#endif
//#define FLASH_CHIP_SELECT 4
#define FLASH_CHIP_SELECT 6
#define PROP_SHIELD_AUDIO 5
void setup ()
{
while (!Serial && millis () < 3000UL)
;
Serial.begin (9600);
//uncomment these if you have other SPI chips connected
//to keep them disabled while using only SerialFlash
pinMode (10, INPUT_PULLUP);
if (MOSI_PIN != 7)
SPI.setMOSI (MOSI_PIN);
if (SCK_PIN != 13)
SPI.setSCK (SCK_PIN);
SPI.begin ();
eeprom_initialize ();
// Audio connections require memory to work. For more
// detailed information, see the MemoryAndCpuUsage example
AudioMemory (8);
// Comment these out if not using the audio adaptor board.
// This may wait forever if the SDA & SCL pins lack
// pullup resistors
#if USE_AUDIO_SHIELD
sgtl5000.enable ();
sgtl5000.volume (0.5f);
#endif
// Enable the amplifier on the prop shield
#if USE_PROP_SHIELD
pinMode (PROP_SHIELD_AUDIO, OUTPUT);
digitalWrite (PROP_SHIELD_AUDIO, HIGH);
#endif
Serial.printf ("MOSI = %d, SCK = %d, CS = %d%s%s%s%s%s%s%s\n",
MOSI_PIN,
SCK_PIN,
FLASH_CHIP_SELECT,
(USE_I2S || USE_AUDIO_SHIELD) ? ", i2s" : "",
(USE_ANALOG_DAC || USE_PROP_SHIELD) ? ", dac" : "",
USE_ANALOG_DACS ? ", dacs" : "",
USE_SPDIF ? ", S/PDIF" : "",
USE_AUDIO_SHIELD ? ", audio shield" : "",
USE_PROP_SHIELD ? ", prop shield" : "",
USE_AUDIO_SPI ? ", audio spi" : "");
// Start SerialFlash
if (!SerialFlash.begin (FLASH_CHIP_SELECT)) {
Serial.println ("Cannot access SPI Flash chip");
while (1)
{
delay (1000);
}
}
}
void playFile (const char *filename)
{
Serial.printf ("Playing file: %s\n", filename);
// Start playing the file. This sketch continues to
// run while the file plays.
playRaw.play (filename);
// A brief delay for the library read WAV info
delay (5);
// Simply wait for the file to finish playing.
while (playRaw.isPlaying ()) {
// uncomment these lines if you audio shield
// has the optional volume pot soldered
//float vol = analogRead(15);
//vol = vol / 1024;
// sgtl5000_1.volume(vol);
}
}
void loop() {
playFile ("SDTEST1.RAW"); // filenames are always uppercase 8.3 format
delay (500);
playFile ("SDTEST2.RAW");
delay (500);
playFile ("SDTEST3.RAW");
delay (500);
playFile ("SDTEST4.RAW");
delay (1500);
}