Speakertest

Dependencies:   SDFileSystem mbed wave_player

Committer:
Nikolas
Date:
Mon Jan 25 13:14:30 2016 +0000
Revision:
0:7e6d1d7b2ae0
Speakertest program;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nikolas 0:7e6d1d7b2ae0 1 // Soundboard that plays 1 of 4 .wav files stored on the SD card based on 1 of
Nikolas 0:7e6d1d7b2ae0 2 // 4 buttons pressed
Nikolas 0:7e6d1d7b2ae0 3
Nikolas 0:7e6d1d7b2ae0 4 #include "mbed.h"
Nikolas 0:7e6d1d7b2ae0 5 #include "wave_player.h"
Nikolas 0:7e6d1d7b2ae0 6 #include "SDFileSystem.h"
Nikolas 0:7e6d1d7b2ae0 7
Nikolas 0:7e6d1d7b2ae0 8 // .wav files to play
Nikolas 0:7e6d1d7b2ae0 9 const char *filenames[1] = { "/sd/Sandstorm.wav"
Nikolas 0:7e6d1d7b2ae0 10 };
Nikolas 0:7e6d1d7b2ae0 11
Nikolas 0:7e6d1d7b2ae0 12 // Define button
Nikolas 0:7e6d1d7b2ae0 13 DigitalIn button_1(p27);
Nikolas 0:7e6d1d7b2ae0 14
Nikolas 0:7e6d1d7b2ae0 15 // USB serial (tx, rx)
Nikolas 0:7e6d1d7b2ae0 16 Serial pc(USBTX, USBRX);
Nikolas 0:7e6d1d7b2ae0 17
Nikolas 0:7e6d1d7b2ae0 18 // SD card
Nikolas 0:7e6d1d7b2ae0 19 SDFileSystem sd(p11, p12, p13, p24, "sd");
Nikolas 0:7e6d1d7b2ae0 20
Nikolas 0:7e6d1d7b2ae0 21 // Audio out (DAC)
Nikolas 0:7e6d1d7b2ae0 22 AnalogOut DACout(p18);
Nikolas 0:7e6d1d7b2ae0 23 wave_player waver(&DACout);
Nikolas 0:7e6d1d7b2ae0 24
Nikolas 0:7e6d1d7b2ae0 25 // Play a .wav file
Nikolas 0:7e6d1d7b2ae0 26 int playSound(int file_num) {
Nikolas 0:7e6d1d7b2ae0 27
Nikolas 0:7e6d1d7b2ae0 28 FILE *file;
Nikolas 0:7e6d1d7b2ae0 29
Nikolas 0:7e6d1d7b2ae0 30 // Open sound file for reading
Nikolas 0:7e6d1d7b2ae0 31 file = fopen(filenames[file_num], "r");
Nikolas 0:7e6d1d7b2ae0 32 if ( file == NULL ) {
Nikolas 0:7e6d1d7b2ae0 33 error("ERROR: Could not open file for reading!\n");
Nikolas 0:7e6d1d7b2ae0 34 return -1;
Nikolas 0:7e6d1d7b2ae0 35 }
Nikolas 0:7e6d1d7b2ae0 36
Nikolas 0:7e6d1d7b2ae0 37 // Play the sound file
Nikolas 0:7e6d1d7b2ae0 38 pc.printf("Playing sound clip %i\r\n", (file_num + 1));
Nikolas 0:7e6d1d7b2ae0 39 waver.play(file);
Nikolas 0:7e6d1d7b2ae0 40
Nikolas 0:7e6d1d7b2ae0 41 // Reset to beginning of file and close it
Nikolas 0:7e6d1d7b2ae0 42 fseek(file, 0, SEEK_SET);
Nikolas 0:7e6d1d7b2ae0 43 fclose(file);
Nikolas 0:7e6d1d7b2ae0 44
Nikolas 0:7e6d1d7b2ae0 45 return 0;
Nikolas 0:7e6d1d7b2ae0 46 }
Nikolas 0:7e6d1d7b2ae0 47
Nikolas 0:7e6d1d7b2ae0 48 int main() {
Nikolas 0:7e6d1d7b2ae0 49
Nikolas 0:7e6d1d7b2ae0 50 // Use internal pull-up resistors
Nikolas 0:7e6d1d7b2ae0 51 button_1.mode(PullUp);
Nikolas 0:7e6d1d7b2ae0 52
Nikolas 0:7e6d1d7b2ae0 53 pc.printf("\r\nHardware Soundboard\r\n");
Nikolas 0:7e6d1d7b2ae0 54
Nikolas 0:7e6d1d7b2ae0 55 while(1) {
Nikolas 0:7e6d1d7b2ae0 56
Nikolas 0:7e6d1d7b2ae0 57 // Figure out which button was pressed and play that file
Nikolas 0:7e6d1d7b2ae0 58 if ( button_1 == 0 ) {
Nikolas 0:7e6d1d7b2ae0 59 playSound(0);
Nikolas 0:7e6d1d7b2ae0 60 }
Nikolas 0:7e6d1d7b2ae0 61
Nikolas 0:7e6d1d7b2ae0 62 // Wait 10ms before sampling the buttons again
Nikolas 0:7e6d1d7b2ae0 63 wait(0.01);
Nikolas 0:7e6d1d7b2ae0 64 }
Nikolas 0:7e6d1d7b2ae0 65 }