Lawrence Quizon / Mbed OS play_wav

Dependencies:   USBMSD_BD SDFileSystem max32630fthr USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "max32630fthr.h"
00003 #include "stdio.h"
00004 #include "ctype.h"
00005 #include "generalinterface.h"
00006 #define BUFFER_SIZE 1024
00007 #define HALF_BUFFER 512
00008 
00009 bool debugState = 1;
00010 
00011 DigitalOut rLED(LED1);
00012 DigitalOut gLED(LED2);
00013 DigitalOut bLED(LED3);
00014 
00015 DigitalIn Button(P2_3);
00016 
00017 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
00018 PwmOut PWM(P4_0);
00019 AnalogIn POT(AIN_0);
00020 int i;
00021 volatile int bufferPOS = 0;
00022 short audioDataBuffer[BUFFER_SIZE];
00023 struct WavFile {
00024     long int size;
00025     int channels;
00026     int sampleRate;
00027     int bitsPerSample;
00028 };
00029 
00030 float maxSampleVal=1;
00031 WavFile Track;
00032 Ticker SampleTime;
00033 
00034 DigitalIn db1(P3_3);
00035 float potval;
00036 
00037 void placeNewSample(void)
00038 {
00039     PWM.write((((float)audioDataBuffer[bufferPOS]*potval)/maxSampleVal)+0.5); //multiply by POT value for volume.
00040     bufferPOS = (bufferPOS+2)%BUFFER_SIZE;
00041     //if(!Button)
00042     //{
00043     //    SampleTime.detach();
00044     //}
00045     //printf("%i/%f = %f\r\n",audioDataBuffer[bufferPOS],(maxSampleVal),((float)audioDataBuffer[bufferPOS])/maxSampleVal)+1;
00046 }
00047 
00048 void presence()
00049 {
00050     rLED = LED_ON;
00051     wait_ms(500);
00052     rLED = LED_OFF;
00053     gLED = LED_ON;
00054     wait_ms(500);
00055     bLED = LED_ON;
00056     gLED = LED_OFF;
00057 }
00058 
00059 int main()
00060 {
00061     startFileSystem();
00062 
00063     daplink.printf("\f---DAPLINK SERIAL PORT---\r\n\r\nWAV PLAYER VER 2\r\n\r\n");
00064     microUSB.printf("micro USB serial port\r\n");
00065 
00066     presence();
00067 
00068 restart:
00069 
00070     char *title = new char[24];
00071     title[0] = '/';
00072     title[1] = 'f';
00073     title[2] = 's';
00074     title[3] = '/';
00075     FILE *audio;
00076     while(1) {
00077         printf("Please input filename: ");
00078         char *inputptr = title+4;
00079         if(!getInput(24,inputptr)) {
00080             printf("Filenames cannot be more than 20 characters.\r\n");
00081         }
00082         audio = fopen(title,"r");
00083         if(audio == NULL) {
00084             printf("File not found. Please append filetype at the end of filename.\r\n");
00085             continue;
00086         }
00087         break;
00088     }
00089     printf("\r\nFile opened.\r\n\r\n");
00090 
00091     //find file size
00092     fseek(audio,0L,SEEK_END);
00093     Track.size = ftell(audio);
00094 
00095     printf("File Size:%li \r\n",Track.size);
00096 
00097     //read sample rate and channels of wav file
00098     fseek(audio,22,SEEK_SET);
00099     fread(&Track.channels, 2, 1,audio);
00100     fread(&Track.sampleRate, 2, 1,audio);
00101     printf("Sample Rate: %i\r\n",Track.sampleRate);
00102     printf("%i channels \r\n",Track.channels);
00103     if(Track.sampleRate >= 20000) {
00104         printf("WARNING: Microcontroller may not be able to play sample rates higher than 20kHz properly.\r\n");
00105     }
00106 
00107     //read bits per sample
00108     fseek(audio,34,SEEK_SET);
00109     fread(audioDataBuffer, 2, 1,audio);
00110     Track.bitsPerSample = (audioDataBuffer[1] << 8) + audioDataBuffer[0];
00111     printf("Bits Per Sample: %i\r\n",Track.bitsPerSample);
00112     for(int i=0;i<Track.bitsPerSample;i++)
00113     {
00114         maxSampleVal *= 2;
00115     }
00116     maxSampleVal/=2;
00117 
00118     int flag;
00119     //find start of actual audio
00120     fseek(audio,44,SEEK_SET);
00121 
00122     printf("Size: %i bytes\r\n",Track.size);
00123 
00124     PWM.period_us(20);
00125     short size = sizeof(short);
00126     
00127     fread((void *)audioDataBuffer,size,HALF_BUFFER,audio);
00128     printf("Sample time will be attached with period %f.",1.0/(double)(Track.sampleRate));
00129     SampleTime.attach(&placeNewSample,1.0/(double)(Track.sampleRate));
00130     //microcontroller may not be strong enough to call fread after attaching a 44.1kHz wav file.
00131     printf("\r\nTicker attached.\r\nRunning buffer loop...\r\n\r\n");
00132 
00133     //take the first block of audio data into the buffer (buffer size is two blocks)
00134     flag = 0;
00135     while(ftell(audio) < Track.size) {
00136         potval = POT.read()*2;
00137         if((bufferPOS < HALF_BUFFER) && flag == 0) {
00138             fread((void *)(audioDataBuffer + HALF_BUFFER),size,HALF_BUFFER,audio);
00139             flag = !flag;
00140         } else if((bufferPOS >= HALF_BUFFER) && flag == 1) {
00141             fread((void *)audioDataBuffer,size,HALF_BUFFER,audio);
00142             flag = !flag;
00143         }
00144     }
00145     SampleTime.detach();
00146     bLED=LED_OFF;
00147     rLED=LED_ON;
00148     while(1)
00149     {
00150         if(!db1)
00151         {
00152             goto restart;
00153         }
00154     }
00155 };
00156