#include <Arduino.h>
#include <Audio.h>
AudioPreload preload;
AudioPlayWAVstereo player;
AudioOutputI2S i2s_out;
AudioControlSGTL5000 sgtl5000;
AudioConnection connections[]{{player, 0, i2s_out, 0}, {player, 1, i2s_out, 1}};
void setup() {
Serial.begin(115200);
// Initialize SD reader
SPI.setMOSI(7);
SPI.setSCK(14);
while (!SD.begin(10)) {
Serial.println("Unable to access the SD card! Retrying...");
delay(1000);
}
// Initialize Audio stuff
AudioMemory(16);
sgtl5000.enable();
sgtl5000.volume(0.5f);
/**
* Allocate buffers for the AudioPreload and the AudioPlayWAVstereo
*
* I'm noticing some weirdness with different buffer sizes that cause a
* combination of 3 different effects:
* 1) Audio output "buzzes", probably due to constantly replaying the file
* 2) wav file play gets truncated, and the file replays intermittently after
* less than a second
* 3) Teensy crashes and reboots after a while, usually only when one of the
* first two issues is encountered
*
* Note: The buffer sizes here were manually adjusted until things worked (or
* didnt) I originally tested with an SD card with 52 files on it. Replacing
* that SD card with one that has a single wav file on it greatly reduced
* these buffer size threshold values. To me that indicates that the issue is
* somehow linked with the time it takes the SD reader to find and open the
* file.
*
* AudioPreload min buffer size was originally found to be 1329 bytes
* AudioPlayWAVstereo min buffer size was originally found to be 1248 bytes
*
* Further testing shows that values above this minimum threshold is somewhat
* inconsistent, and changes with whether or not we're using an
* AudioPreloader. When we're using minimally sized buffers, it seems like
* odd-sized buffers have more problems than even-sized ones, although
* sometimes it seems that even sufficiently large odd-sized buffers are
* problematic as well.
*
* Also it seems like these same effects can be repeated with larger buffer
* sizes too (see line 108).
*/
/**
* Preload our file
*/
// Works fine but seems to crash sometimes, intermittently
// preload.preLoad("chime.wav", MemBuffer::inHeap, 810);
// Seems to work fine
preload.preLoad("chime.wav", MemBuffer::inHeap, 1024);
// preload.preLoad("chime.wav", MemBuffer::inHeap, 1023);
// preload.preLoad("chime.wav", MemBuffer::inHeap, 900);
// These cause the audio output to sort of "buzz" (eventually crash sometimes)
// preload.preLoad("chime.wav", MemBuffer::inHeap, 809);
// preload.preLoad("chime.wav", MemBuffer::inHeap, 808);
/**
* Create wav player buffer
* WITHOUT PRELOADER (playing from filename only)
*/
// Just right (but may ocassionally crash)
// player.createBuffer(1248, MemBuffer::inHeap);
// player.createBuffer(1250, MemBuffer::inHeap);
// player.createBuffer(1252, MemBuffer::inHeap);
// These cause the audio output to sort of "buzz" (eventually crash sometimes)
// player.createBuffer(1246, MemBuffer::inHeap);
// player.createBuffer(1247, MemBuffer::inHeap);
// player.createBuffer(1249, MemBuffer::inHeap);
// player.createBuffer(1251, MemBuffer::inHeap);
/**
* Create wav player buffer
* WITH PRELOADER
*/
// Just right
// player.createBuffer(1024, MemBuffer::inHeap);
// player.createBuffer(2024, MemBuffer::inHeap);
// player.createBuffer(2026, MemBuffer::inHeap);
// These cause the audio output to sort of "buzz" (eventually crash sometimes)
// player.createBuffer(1023, MemBuffer::inHeap);
// player.createBuffer(1025, MemBuffer::inHeap);
// player.createBuffer(1225, MemBuffer::inHeap);
// These cause the file to replay ~ once/sec (eventually crash sometimes)
player.createBuffer(1022, MemBuffer::inHeap);
// player.createBuffer(2025, MemBuffer::inHeap);
}
void loop() {
// Repeat the file when it's done playing
// NOTE: with small enough buffers, this causes the file to constantly replay
// from the beginning. I can't say if this is EVERY time through loop, but
// it's enough to produce a steady buzz instead of playing the whole file.
// Other times we just get the first chunk of the file repeating once or twice
// every second.
if (!player.isPlaying()) {
// This can be reproduced with either preload or directly passing filename
player.play(preload);
// player.play("chime.wav");
}
}