For MAX323630FTHR: Plays a WAV file in the SD card. Interfaced through serial port using puTTY or powershell.
Dependencies: USBMSD_BD SDFileSystem max32630fthr USBDevice
Diff: main.cpp
- Revision:
- 1:7884bc0fb012
- Parent:
- 0:12c56a256ee1
- Child:
- 2:d4f9c8c25fa6
--- a/main.cpp Tue Jul 16 08:11:48 2019 +0000 +++ b/main.cpp Fri Jul 26 01:02:51 2019 +0000 @@ -1,15 +1,13 @@ #include "mbed.h" #include "max32630fthr.h" -#include "USBSerial.h" #include "stdio.h" -#include "SDFileSystem.h" - -//still needs BITS PER SAMPLE PARSING. -//do this like so: PWM.write(WavValue/2^BPS) - +#include "ctype.h" +#include "generalinterface.h" #define BUFFER_SIZE 1024 #define HALF_BUFFER 512 +bool debugState = 1; + DigitalOut rLED(LED1); DigitalOut gLED(LED2); DigitalOut bLED(LED3); @@ -17,39 +15,37 @@ DigitalIn Button(P2_3); MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); -PwmOut PWM(P5_6); +PwmOut PWM(P4_0); AnalogIn POT(AIN_0); +int i; volatile int bufferPOS = 0; - -Serial daplink(P2_1,P2_0); -USBSerial microUSB; -SDFileSystem sd(P0_5, P0_6, P0_4, P0_7, "sd"); - -int audioDataBuffer[BUFFER_SIZE]; - -struct WavFile -{ +short audioDataBuffer[BUFFER_SIZE]; +struct WavFile { long int size; int channels; int sampleRate; int bitsPerSample; }; +float maxSampleVal=1; WavFile Track; +Ticker SampleTime; + +DigitalIn db1(P3_3); void placeNewSample(void) { - //POT.read(); //COMMENTED OUT FOR TESTING - PWM = (float) audioDataBuffer[bufferPOS]/Track.bitsPerSample; //multiply by POT value for volume. - bufferPOS = (bufferPOS+1) & 0x07F; + PWM.write((((float)audioDataBuffer[bufferPOS])/maxSampleVal)+1); //multiply by POT value for volume. + bufferPOS = (bufferPOS+2)%BUFFER_SIZE; + //if(!Button) + //{ + // SampleTime.detach(); + //} + //printf("%i/%f = %f\r\n",audioDataBuffer[bufferPOS],(maxSampleVal),((float)audioDataBuffer[bufferPOS])/maxSampleVal)+1; } - -int main() -{ - - daplink.printf("\f---DAPLINK SERIAL PORT---\r\n\r\nWAV PLAYER VER 2\r\n\r\n"); - microUSB.printf("micro USB serial port\r\n"); +void presence() +{ rLED = LED_ON; wait_ms(500); rLED = LED_OFF; @@ -57,78 +53,102 @@ wait_ms(500); bLED = LED_ON; gLED = LED_OFF; - - Ticker SampleTime; - - FILE *audio = fopen("/sd/1.wav","r"); - - if(!audio) - { - printf("Failed to open file. [Exit]\r\n"); - return 0; +} + +int main() +{ + startFileSystem(); + + daplink.printf("\f---DAPLINK SERIAL PORT---\r\n\r\nWAV PLAYER VER 2\r\n\r\n"); + microUSB.printf("micro USB serial port\r\n"); + + presence(); + +restart: + + char *title = new char[24]; + title[0] = '/'; + title[1] = 'f'; + title[2] = 's'; + title[3] = '/'; + FILE *audio; + while(1) { + printf("Please input filename: "); + char *inputptr = title+4; + if(!getInput(24,inputptr)) { + printf("Filenames cannot be more than 20 characters.\033[A\r\n"); + } + audio = fopen(title,"r"); + if(audio == NULL) { + printf("File not found. Please append filetype at the end of filename.\033[A\r\n"); + continue; + } + break; } - - printf("File opened.\r\n\r\n"); - + printf("\r\nFile opened.\r\n\r\n"); + //find file size fseek(audio,0L,SEEK_END); Track.size = ftell(audio); - + printf("File Size:%li \r\n",Track.size); - - //read sample rate of wav file + + //read sample rate and channels of wav file fseek(audio,22,SEEK_SET); - fread(audioDataBuffer, 4, 1,audio); - - printf("Raw Sample Rate Data: %i\r\n",audioDataBuffer[4]); - - Track.channels = audioDataBuffer[0]; - Track.sampleRate =(audioDataBuffer[3] << 8) + audioDataBuffer[2]; //needs to be done due to little-endian encoding of wave file headers - + fread(&Track.channels, 2, 1,audio); + fread(&Track.sampleRate, 2, 1,audio); printf("Sample Rate: %i\r\n",Track.sampleRate); printf("%i channels \r\n",Track.channels); - + if(Track.sampleRate >= 20000) { + printf("WARNING: Microcontroller may not be able to play sample rates higher than 20kHz properly.\r\n"); + } + //read bits per sample fseek(audio,34,SEEK_SET); fread(audioDataBuffer, 2, 1,audio); Track.bitsPerSample = (audioDataBuffer[1] << 8) + audioDataBuffer[0]; - printf("Bits Per Sample: %i\r\n",Track.bitsPerSample); - + for(int i=0;i<Track.bitsPerSample;i++) + { + maxSampleVal *= 2; + } + maxSampleVal/=2; + + int flag; //find start of actual audio fseek(audio,44,SEEK_SET); - int bytecount; - bytecount = Track.size; - - printf("Size: %i bytes\r\nPlay time: %f minutes",Track.size,(float)((Track.size-44)*8)/Track.bitsPerSample); - - //initialize PWM to 1us period [1MHz] - PWM.period_us(1); - SampleTime.attach(&placeNewSample,1/(Track.sampleRate)); - //microcontroller may not be strong enough to call fread after attaching a 44.1kHz wav file. - - printf("\r\nTicker attached.\r\nRunning Loop...\r\n\r\n"); - - //take the first block of audio data into the buffer (buffer size is two blocks) - fread(audioDataBuffer,HALF_BUFFER,1,audio); + + printf("Size: %i bytes\r\n",Track.size); + + PWM.period_us(10); + short size = sizeof(short); - int g=0; + fread((void *)audioDataBuffer,size,HALF_BUFFER,audio); + printf("Sample time will be attached with period %f.",1.0/(double)(Track.sampleRate)); + SampleTime.attach(&placeNewSample,1.0/(double)(Track.sampleRate)); + //microcontroller may not be strong enough to call fread after attaching a 44.1kHz wav file. + printf("\r\nTicker attached.\r\nRunning buffer loop...\r\n\r\n"); - while(ftell(audio) != Track.size) + //take the first block of audio data into the buffer (buffer size is two blocks) + flag = 0; + while(ftell(audio) < Track.size) { + if((bufferPOS < HALF_BUFFER) && flag == 0) { + fread((void *)(audioDataBuffer + HALF_BUFFER),size,HALF_BUFFER,audio); + flag = !flag; + } else if((bufferPOS >= HALF_BUFFER) && flag == 1) { + fread((void *)audioDataBuffer,size,HALF_BUFFER,audio); + flag = !flag; + } + } + SampleTime.detach(); + bLED=LED_OFF; + rLED=LED_ON; + while(1) { - printf("Loop %i\f",g++); - if(bufferPOS < HALF_BUFFER && flag == 0) + if(!db1) { - fread(audioDataBuffer + HALF_BUFFER,HALF_BUFFER,1,audio); - bytecount -= HALF_BUFFER; - flag = 1; - } - else if(bufferPOS >= HALF_BUFFER && flag == 1) - { - fread(audioDataBuffer,HALF_BUFFER,1,audio); - bytecount -= HALF_BUFFER; - flag = 0; + goto restart; } } };