nivedita velagaleti
/
lab3_musicplayer
music main
Fork of AppBoard_Waveplayer by
main.cpp@11:e48cb1e38995, 2015-04-14 (annotated)
- Committer:
- niv17
- Date:
- Tue Apr 14 03:46:33 2015 +0000
- Revision:
- 11:e48cb1e38995
- Parent:
- 10:6eefc37bfa23
Version that needs to be fixed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
niv17 | 11:e48cb1e38995 | 1 | #include "mbed.h" |
niv17 | 11:e48cb1e38995 | 2 | #include "WaveFileReader.h" |
niv17 | 11:e48cb1e38995 | 3 | #include "USBHostMSD.h" |
niv17 | 11:e48cb1e38995 | 4 | #include "CircularBuffer.h" |
niv17 | 11:e48cb1e38995 | 5 | |
niv17 | 11:e48cb1e38995 | 6 | #define MAXSIZE 4096 |
niv17 | 11:e48cb1e38995 | 7 | #define EXTRASIZE 0 |
niv17 | 11:e48cb1e38995 | 8 | |
niv17 | 11:e48cb1e38995 | 9 | Serial pc(USBTX,USBRX); |
niv17 | 11:e48cb1e38995 | 10 | |
niv17 | 11:e48cb1e38995 | 11 | unsigned short buffer[MAXSIZE]; |
niv17 | 11:e48cb1e38995 | 12 | AnalogOut DACout(p18); |
niv17 | 11:e48cb1e38995 | 13 | Ticker ticker; |
niv17 | 11:e48cb1e38995 | 14 | //CircularBuffer<unsigned short> circularBuffer(MAXSIZE+EXTRASIZE); |
niv17 | 11:e48cb1e38995 | 15 | |
niv17 | 11:e48cb1e38995 | 16 | void writeAnalog(){ |
niv17 | 11:e48cb1e38995 | 17 | static int i=0; |
niv17 | 11:e48cb1e38995 | 18 | if(i>=1024)i = 0; |
niv17 | 11:e48cb1e38995 | 19 | //DACout.write_u16(circularBuffer.readOneSample()); |
niv17 | 11:e48cb1e38995 | 20 | DACout.write_u16(buffer[i]); |
niv17 | 11:e48cb1e38995 | 21 | i++; |
niv17 | 11:e48cb1e38995 | 22 | |
niv17 | 11:e48cb1e38995 | 23 | } |
niv17 | 11:e48cb1e38995 | 24 | |
niv17 | 11:e48cb1e38995 | 25 | int main() { |
niv17 | 11:e48cb1e38995 | 26 | |
niv17 | 11:e48cb1e38995 | 27 | pc.printf("\r\n Detecting USB..."); |
niv17 | 11:e48cb1e38995 | 28 | fflush(stdout); |
niv17 | 11:e48cb1e38995 | 29 | |
niv17 | 11:e48cb1e38995 | 30 | USBHostMSD msd("usb"); |
niv17 | 11:e48cb1e38995 | 31 | while(!msd.connect()) { |
niv17 | 11:e48cb1e38995 | 32 | Thread::wait(500); |
niv17 | 11:e48cb1e38995 | 33 | pc.printf("\r\nwaiting.."); |
niv17 | 11:e48cb1e38995 | 34 | } |
niv17 | 11:e48cb1e38995 | 35 | pc.printf("\r\n USD Detected"); |
niv17 | 11:e48cb1e38995 | 36 | fflush(stdout); |
niv17 | 11:e48cb1e38995 | 37 | |
niv17 | 11:e48cb1e38995 | 38 | FILE *file; |
niv17 | 11:e48cb1e38995 | 39 | file = fopen("/usb/sample.wav","r"); |
niv17 | 11:e48cb1e38995 | 40 | WaveFileReader waveReader(file,false); |
niv17 | 11:e48cb1e38995 | 41 | pc.printf("\r\n File opened"); |
niv17 | 11:e48cb1e38995 | 42 | fflush(stdout); |
niv17 | 11:e48cb1e38995 | 43 | |
niv17 | 11:e48cb1e38995 | 44 | float samplePeriod = waveReader.readSamplePeriod(); |
niv17 | 11:e48cb1e38995 | 45 | waveReader.read(buffer,MAXSIZE); |
niv17 | 11:e48cb1e38995 | 46 | ticker.attach_us(&writeAnalog,samplePeriod); |
niv17 | 11:e48cb1e38995 | 47 | //pc.printf("\r\n Ticker attached"); |
niv17 | 11:e48cb1e38995 | 48 | |
niv17 | 11:e48cb1e38995 | 49 | // while(1){ |
niv17 | 11:e48cb1e38995 | 50 | // while(circularBuffer.writeSizeRemaining() < MAXSIZE){} |
niv17 | 11:e48cb1e38995 | 51 | // //pc.printf("\r\n Reading next MAXSIZE"); |
niv17 | 11:e48cb1e38995 | 52 | // //fflush(stdout); |
niv17 | 11:e48cb1e38995 | 53 | // int length = waveReader.read(buffer,MAXSIZE); |
niv17 | 11:e48cb1e38995 | 54 | // if(length<MAXSIZE){ |
niv17 | 11:e48cb1e38995 | 55 | // circularBuffer.write(buffer,MAXSIZE); |
niv17 | 11:e48cb1e38995 | 56 | // break; |
niv17 | 11:e48cb1e38995 | 57 | // } |
niv17 | 11:e48cb1e38995 | 58 | // //Mixer.mix(buf); |
niv17 | 11:e48cb1e38995 | 59 | // circularBuffer.write(buffer,MAXSIZE); |
niv17 | 11:e48cb1e38995 | 60 | // } |
niv17 | 11:e48cb1e38995 | 61 | fclose(file); |
niv17 | 11:e48cb1e38995 | 62 | while(1) { |
niv17 | 11:e48cb1e38995 | 63 | |
niv17 | 11:e48cb1e38995 | 64 | } |
niv17 | 11:e48cb1e38995 | 65 | } |