test1

Fork of VS1053 by Chawisorn samrit

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers player.cpp Source File

player.cpp

00001 #include "player.h"
00002 #include"SDFileSystem.h"
00003 
00004 SDFileSystem sd(D11, D12, D13, D9, "sd"); // the pinout on the mbed Cool
00005 vs10xx vs1053(D11, D12, D13, D6, D7, D2, D8);//mosi,miso,sclk,xcs,xdcs,dreq,xreset
00006 Serial pc(USBTX, USBRX);
00007 playerStatetype  playerState;
00008 ctrlStatetype ctrlState;
00009 static unsigned char fileBuf[48000];
00010 unsigned char *bufptr;
00011 char karn;
00012 char list[20][50];            //song list
00013 
00014 char data ;
00015 void Player::begin(void)
00016 {
00017     
00018     DirHandle *dir;
00019     struct dirent *ptr;
00020         FileHandle *fp;
00021    
00022     vs1053.reset();
00023     dir = opendir("/sd");
00024     printf("\r\n**********playing list**********\r\n");
00025     unsigned char i = 0,j=0;
00026     while(((ptr = dir->readdir()) != NULL)&&(i <20))
00027     {
00028         if(strstr(ptr->d_name,".mp3")||strstr(ptr->d_name,".MP3"))
00029         {
00030                             fp =sd.open(ptr->d_name, O_RDONLY);
00031                             if(fp != NULL) 
00032                             {
00033                                 char *byte = ptr->d_name;
00034                                 j=0;
00035                                 while(*byte){
00036                                     list[i][j++]  = *byte++;
00037                                     karn=i;
00038                 }
00039                 pc.printf("%2d . %s\r\n", i,list[i++]);
00040                 //fp->close();
00041                             }
00042         }
00043     }
00044         dir->closedir();
00045 }
00046 
00047 void Player::stop(void){
00048     playerState =PS_STOP;
00049     }
00050  
00051   
00052 /*  This function plays back an audio file.  */
00053 void Player::playFile(char *file) {
00054     int bytes;        // How many bytes in buffer left
00055     char n;
00056     
00057     playerState = PS_PLAY;
00058     vs1053.setFreq(24000000);     //hight speed
00059     
00060     FileHandle *fp =sd.open(file, O_RDONLY);
00061     
00062     if(fp == NULL) {
00063         printf("Could not open %s\r\n",file);
00064 
00065     }
00066     else
00067     {
00068         printf("Playing %s ...\r\n",file);
00069         
00070         /* Main playback loop */
00071         while((bytes = fp->read(fileBuf,48000)) > 0) {
00072         
00073         {
00074             bufptr = fileBuf;
00075             // actual audio data gets sent to VS10xx.
00076             while(bytes > 0)
00077             {
00078                 n = (bytes < 32)?bytes:32; //การสื่อสารระหว่าง sd card มั้ง ถ้าตัดเล่นไม่ได้
00079                 vs1053.writeData(bufptr,n);
00080                 bytes -= n;
00081                 bufptr += n;
00082                 
00083             }
00084             
00085             
00086            /* if(pc.readable()) {
00087                 data = pc.getc();
00088                 uint8_t vol = 0xff;
00089                 vs1053.setVolume(vol);
00090                 }*/
00091                 
00092             uint8_t vol = 0x20;//set vlume
00093             vs1053.setVolume(vol);  //set vlume
00094             
00095             
00096             if(playerState != PS_PLAY)         //stop
00097             {
00098                 fp->close();
00099                 vs1053.softReset();
00100             }
00101                 
00102             }
00103         }
00104     }
00105 }
00106 
00107 
00108