Skip to content

Instantly share code, notes, and snippets.

@andysheen
Last active January 6, 2020 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andysheen/d9001c40b254185c81d014e40ef99fdb to your computer and use it in GitHub Desktop.
Save andysheen/d9001c40b254185c81d014e40ef99fdb to your computer and use it in GitHub Desktop.
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#define BUTTON 1
AudioMixer4 mixer1; // mixers to combine wav file and audio shield inputs
AudioMixer4 mixer2;
AudioPlaySdWav playWav1;
AudioOutputI2S audioOutput;
// wire up the interfaces between audio components with patch cords
// mixer inputs
AudioConnection patchCord1(playWav1, 0, mixer1, 0);
AudioConnection patchCord3(playWav1, 1, mixer2, 0);
// mixer outputs
AudioConnection patchCord5(mixer1, 0, audioOutput, 0);
AudioConnection patchCord6(mixer2, 0, audioOutput, 1);
AudioControlSGTL5000 sgtl5000_1;
// Use these with the Teensy Audio Shield
#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 7
#define SDCARD_SCK_PIN 14
byte prevSong = 15;
int songCount = 0;
void setup() {
pinMode(BUTTON, INPUT_PULLUP);
analogReference(INTERNAL);
analogReadResolution(12);
randomSeed(analogRead(17)*analogRead(16));
Serial.begin(9600);
// 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
sgtl5000_1.enable();
sgtl5000_1.volume(1);
mixer1.gain(0, 1);
mixer2.gain(0, 1);
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
// stop here, but print a message repetitively
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
}
void playFile(const char *filename)
{
Serial.print("Playing file: ");
Serial.println(filename);
// Start playing the file. This sketch continues to
// run while the file plays.
playWav1.play(filename);
// A brief delay for the library read WAV info
delay(5);
// Simply wait for the file to finish playing.
while (playWav1.isPlaying()) {
}
}
void loop() {
if (digitalRead(BUTTON) == LOW) {
//Put a function in here to play if the button has been pressed
playFile("SOUND1.WAV");
//Removing the comment '//' lines below will add the functions to the program
//playRandomFile();
//playFileSequentially();
}
}
/*
/ ----------------------------OTHER FUNCTIONS----------------------------------------- /
*/
void playRandomFile() {
int songSelection = random(5);
Serial.println(songSelection);
while (songSelection == prevSong) {
songSelection = random(14);
}
switch (songSelection) {
case 0:
playFile("SOUND1.WAV"); // filenames are always uppercase 8.3 format
break;
case 1:
playFile("SOUND2.WAV");
break;
case 2:
playFile("SOUND3.WAV");
break;
case 3:
playFile("SOUND4.WAV");
break;
case 4:
playFile("SOUND5.WAV");
break;
}
delay(1000);
prevSong = songSelection;
}
void playFileSequentially() {
Serial.println(songCount);
if (songCount > 4) {
songCount = 0;
}
switch (songCount) {
case 0:
playFile("SOUND1.WAV"); // filenames are always uppercase 8.3 format
break;
case 1:
playFile("SOUND2.WAV");
break;
case 2:
playFile("SOUND3.WAV");
break;
case 3:
playFile("SOUND4.WAV");
break;
case 4:
playFile("SOUND5.WAV");
break;
}
delay(1000);
soungCount++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment