f303k8 wav player

Dependencies:   SDFileSystem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers wavPlayer.cpp Source File

wavPlayer.cpp

00001 #include "mbed.h"
00002 #include "wavChunks.h"
00003 #include "wavPlayer.h"
00004 #include <stdio.h>
00005 #include "SDFileSystem.h"
00006 #include "TIM6Driver.h"
00007 
00008 wavPlayerOnDAC::wavPlayerOnDAC(PinName pinDAC) : 
00009     DACout(pinDAC) 
00010 {
00011 }
00012 
00013 void wavPlayerOnDAC::setFile(FIL* tgtFile)
00014 {
00015     uint16_t i;
00016     uint32_t    wavReadByte;
00017     uint32_t        ReadID;
00018     wavfil = *tgtFile;
00019 
00020     f_read(&wavfil, &wavRIFFHedder, RIFFHedderSize, &wavReadByte);
00021     printf("hedder:%x\r\n", wavRIFFHedder.riff);
00022     printf("size:%x\r\n", wavRIFFHedder.size);
00023     printf("type:%x\r\n", wavRIFFHedder.type);
00024     
00025     printf("readID\r\n");
00026     f_read(&wavfil, &ReadID, sizeof(ReadID), &wavReadByte);
00027     printf("ID:%x\r\n",ReadID);
00028 
00029     if (ReadID == 0x74786562)   //bext
00030     {
00031         printf("\r\n*bextChunk\r\n");
00032         f_read(&wavfil, &wavBextChunk, BextChunkSize, &wavReadByte);
00033         printf("size:%x\r\n", wavBextChunk.ckSize);
00034         f_lseek(&wavfil, f_tell(&wavfil) + wavBextChunk.ckSize);
00035         f_read(&wavfil, &ReadID, sizeof(ReadID), &wavReadByte);
00036     }
00037     if (ReadID == 0x20746D66)
00038     {
00039         f_read(&wavfil, &wavFormatChunk, FormatChunkSize, &wavReadByte);
00040         printf("\r\n*fmtChunk\r\n");
00041         printf("size:%d\r\n", wavFormatChunk.size);
00042         printf("format:%d\r\n", wavFormatChunk.format);
00043         printf("channels:%d\r\n", wavFormatChunk.channels);
00044         printf("samplerate:%d\r\n", wavFormatChunk.samplerate);
00045         TIM6ClockSet(TIM6CLK/wavFormatChunk.samplerate);
00046         printf("byteParSec:%d\r\n", wavFormatChunk.bytepersec);
00047         printf("blockalign:%d\r\n", wavFormatChunk.blockalign);
00048         printf("bitswidth:%d\r\n", wavFormatChunk.bitswidth);
00049         setParameters(wavFormatChunk.bitswidth,wavFormatChunk.channels);
00050     }
00051 
00052     seaechChunk(&wavfil, "data");
00053     f_read(&wavfil, &wavDataChunk, DataChunkSize, &wavReadByte);
00054     printf("\r\n*dataChunk\r\n");
00055     printf("id:%x\r\n", wavDataChunk.id);
00056     printf("size:%x\r\n", wavDataChunk.size);
00057     
00058     f_read(&wavfil,&wavReadData.raw,WAV_READ_SIZE,&wavReadByte);
00059     storeWavDatatoBuff(0);
00060     
00061     f_read(&wavfil,&wavReadData.raw,WAV_READ_SIZE,&wavReadByte);
00062     storeWavDatatoBuff(1);
00063     fWavPlaying = 1;
00064     dac_c = 0;
00065     dac_rp=0;
00066 }
00067 
00068 //return 0:playing 1:stop
00069 uint8_t wavPlayerOnDAC::readProc(void)
00070 {
00071     uint16_t i,j;
00072     uint32_t    wavReadByte;
00073 
00074     if(f_eof(&wavfil) != 0)
00075     {
00076         fWavPlaying = 0;
00077     }
00078 
00079     if(fWavPlaying == 0)
00080     {
00081         dac_on = 0;
00082         return 1;
00083     }
00084     dac_on = 1;
00085     for(j=0;j<2;j++)
00086     {
00087         if(dac_flag[j] == 0)
00088         {
00089             f_read(&wavfil,&wavReadData.raw,WAV_READ_SIZE,&wavReadByte);
00090             storeWavDatatoBuff(j);
00091         }
00092     }
00093     return 0;
00094 }
00095 
00096 void wavPlayerOnDAC::rewind(void)
00097 {
00098     f_lseek(&wavfil,0);
00099     fWavPlaying = 1;
00100 }
00101 void wavPlayerOnDAC::stop(void)
00102 {
00103     fWavPlaying = 0;
00104 }
00105 
00106 void wavPlayerOnDAC::DACOutProc(void)
00107 {
00108     if(dac_on == 1) {
00109         if(dac_flag[dac_c] == 1)
00110         {
00111             DACout.write_u16(DACData[dac_c][dac_rp]);
00112             if( dac_rp == dacBuffIndex_n-1)
00113             {
00114                 dac_flag[dac_c] = 0;
00115                 dac_rp = 0;
00116                 dac_c = (dac_c+1)%2;
00117             }
00118             else
00119             {
00120                 dac_rp++;
00121             }
00122         }
00123     }
00124 }
00125 
00126 void wavPlayerOnDAC::rampUp(void)
00127 {
00128     uint16_t i;
00129     for(i=0;i<32768;i+=64)
00130     {
00131         DACout.write_u16(i);
00132         wait_us(1);
00133     }
00134 }
00135 void wavPlayerOnDAC::rampDown(void)
00136 {
00137     uint16_t i;
00138     for(i=0;i<32768;i+=64)
00139     {
00140         DACout.write_u16(32768-i);
00141         wait_us(1);
00142     }
00143 }
00144 
00145 uint8_t wavPlayerOnDAC::seaechChunk(FIL *fp,char chunk[5])
00146 {
00147     uint8_t readByteData;
00148     uint8_t data[4];
00149     uint8_t ret=0;
00150     uint32_t    wavReadByte;
00151 
00152 
00153     while ( (f_error(fp) == 0) && (f_eof(fp) == 0) )
00154     {
00155         f_read(fp,&readByteData,sizeof(readByteData),&wavReadByte);
00156         data[0] = readByteData;
00157         if (data[3] == chunk[0] && data[2] == chunk[1] && data[1] == chunk[2] && data[0] == chunk[3])
00158         {
00159             printf("found Chunk\r\n");
00160             ret = 1;
00161             break;
00162         }
00163         else
00164         {
00165             data[3] = data[2];
00166             data[2] = data[1];
00167             data[1] = data[0];
00168         }
00169     }
00170     return ret;
00171 }
00172 
00173 void wavPlayerOnDAC::setParameters(uint16_t bitsWidth,uint8_t ch)
00174 {
00175     wavBitsWidth = bitsWidth;
00176     wavChannels = ch;
00177     if(wavBitsWidth == 8)
00178     {
00179         if(wavChannels == 1)
00180         {
00181             dacBuffIndex_n = WAV_READ_SIZE;
00182         }
00183         else
00184         {
00185             dacBuffIndex_n = WAV_READ_SIZE/2;
00186         }
00187     }
00188     else if(wavBitsWidth == 16)
00189     {
00190         if(wavChannels == 1)
00191         {
00192             dacBuffIndex_n = WAV_READ_SIZE/2;
00193         }
00194         else
00195         {
00196             dacBuffIndex_n = WAV_READ_SIZE/4;
00197         }
00198     }
00199 }
00200 
00201 void wavPlayerOnDAC::storeWavDatatoBuff(uint8_t selectBufNo)
00202 {
00203     uint16_t i;
00204     for(i=0;i<dacBuffIndex_n;i++)
00205     {
00206         if(wavBitsWidth == 8)
00207         {
00208             if(wavChannels == 1)
00209             {
00210                 DACData[selectBufNo][i] = wavReadData.mono8bit[i]<<8;
00211             }
00212             else
00213             {
00214                 DACData[selectBufNo][i] = wavReadData.stereo8bit[i].L<<8;
00215             }
00216         }
00217         else
00218         {
00219             if(wavChannels == 1)
00220             {
00221                 DACData[selectBufNo][i] = (wavReadData.mono16bit[i])+32768;
00222             }
00223             else
00224             {
00225                 DACData[selectBufNo][i] = (wavReadData.stereo16bit[i].L)+32768;
00226             }
00227         }
00228     }
00229     dac_flag[selectBufNo] = 1;
00230 }