For MAX323630FTHR: Plays a WAV file in the SD card. Interfaced through serial port using puTTY or powershell.
Dependencies: USBMSD_BD SDFileSystem max32630fthr USBDevice
main.cpp
- Committer:
- Lugs
- Date:
- 2019-07-16
- Revision:
- 0:12c56a256ee1
- Child:
- 1:7884bc0fb012
File content as of revision 0:12c56a256ee1:
#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) #define BUFFER_SIZE 1024 #define HALF_BUFFER 512 DigitalOut rLED(LED1); DigitalOut gLED(LED2); DigitalOut bLED(LED3); DigitalIn Button(P2_3); MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); PwmOut PWM(P5_6); AnalogIn POT(AIN_0); 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 { long int size; int channels; int sampleRate; int bitsPerSample; }; WavFile Track; 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; } 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"); rLED = LED_ON; wait_ms(500); rLED = LED_OFF; gLED = LED_ON; 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; } printf("File 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 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 printf("Sample Rate: %i\r\n",Track.sampleRate); printf("%i channels \r\n",Track.channels); //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); 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); int g=0; while(ftell(audio) != Track.size) { printf("Loop %i\f",g++); if(bufferPOS < HALF_BUFFER && flag == 0) { 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; } } };