Sample application for vs1053b library (rev. lkw4hi)

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed MP3 Shield Player - Testapplication for VLSI VS1053b Lib
00002  * Copyright (c) 2010 Christian Schmiljun
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022 
00023 
00024 #include "mbed.h"
00025 #include "defines.h"
00026 #include "VS1053.h"
00027 
00028 // Volume [ 0x0000 (loud) .. 0xFEFE (quiet) ]
00029 #define VOLUME -42.0f
00030 // Switch for local filesystem or SDCard
00031 #define USE_SDCARD
00032 
00033 // ----------------------------------------------------------------------------
00034 //
00035 // Pin Assigenment for SDCard on Cool Components Workshop Board
00036 //
00037 // SDCard             |  mbed Side
00038 // ---------------------------------------
00039 //  mosi-----------------5
00040 //  miso-----------------6
00041 //  sclk-----------------7
00042 //  cs-------------------8
00043 //
00044 // ----------------------------------------------------------------------------
00045 #ifdef USE_SDCARD
00046 #include "SDFileSystem.h"
00047 
00048 SDFileSystem sd(p5, p6, p7, p8, "sd");
00049 #define DIR_NAME "/sd/Musik"
00050 #else
00051 LocalFileSystem sd("local");
00052 #define DIR_NAME "/local"
00053 #endif
00054 
00055 
00056 
00057 
00058 // ----------------------------------------------------------------------------
00059 //
00060 // Pin Assigenment for Arduino MP3 Shield
00061 // (VS1053 employed, issued by sparkfun.com)
00062 //
00063 // MP3 Sheild Side   |  mbed Side
00064 // ---------------------------------------
00065 //  RX-------------------10 (optional)
00066 //  TX-------------------09 (optional)
00067 //  D2(BSYNC)------------17
00068 //  D3(DREQ)-------------16
00069 //
00070 //  D9(CS)---------------14
00071 //
00072 //  D11(MOSI)------------11
00073 //  D12(MISO)------------12
00074 //  D13(SCK)-------------13
00075 //
00076 //  GND------------------GND(1)
00077 //  5V-------------------VU(39)
00078 //  RESET----------------15
00079 //
00080 // ----------------------------------------------------------------------------
00081 const int VS1053B_BUFFER_SIZE = (16 * 1024 + 1);
00082 char VS1053_BUFFER[VS1053B_BUFFER_SIZE];
00083 char* VS1053B_BUFFER_POINTER  = VS1053_BUFFER;
00084 VS1053 mp3( p11, p12, p13, p14, p15, p16, p17, VS1053_BUFFER, VS1053B_BUFFER_SIZE);
00085 
00086 // Serial for Debug
00087 Serial pc(USBTX, USBRX);
00088 
00089 Ticker timer;
00090 Ticker timer2;
00091 
00092 DigitalIn _DREQ(p16);
00093 bool next = false;
00094 
00095 void statisticsOutput();
00096 
00097 void setVolume(void) {
00098     if (pc.readable())
00099     {
00100         unsigned char c = pc.getc();
00101 // scanf ("%x",&i);                                 
00102         switch (c)
00103         {
00104         case '1':
00105         case '2':
00106         case '3':
00107         case '4':
00108             mp3.setPlaySpeed(c - 48);
00109             break;
00110         case '+':    
00111             mp3.setVolume(mp3.getVolume() + 0.5);
00112             break;
00113         case '-':    
00114             mp3.setVolume(mp3.getVolume() - 0.5);
00115             break;
00116         case 'k':    
00117             mp3.setVolume(mp3.getBalance() + 0.5);
00118             break;
00119         case 'l':    
00120             mp3.setVolume(mp3.getBalance() - 0.5);
00121             break;
00122         case 'a':    
00123             mp3.setTrebleFrequency(mp3.getTrebleFrequency() + 1000);
00124             break;            
00125         case 'y':    
00126             mp3.setTrebleFrequency(mp3.getTrebleFrequency() - 1000);
00127             break;            
00128         case 's':    
00129             mp3.setTrebleAmplitude(mp3.getTrebleAmplitude() - 1);
00130             break;            
00131         case 'x':    
00132             mp3.setTrebleAmplitude(mp3.getTrebleAmplitude() + 1);
00133             break;            
00134         case 'd':    
00135             mp3.setBassFrequency(mp3.getBassFrequency() + 10);
00136             break;            
00137         case 'c':    
00138             mp3.setBassFrequency(mp3.getBassFrequency() - 10);
00139             break;            
00140         case 'f':    
00141             mp3.setBassAmplitude(mp3.getBassAmplitude() - 1);
00142             break;            
00143         case 'v':    
00144             mp3.setBassAmplitude(mp3.getBassAmplitude() + 1);
00145             break;  
00146         case 'z':                
00147             timer.attach(&statisticsOutput, 1);
00148             break;       
00149         case 'u':                
00150             timer.detach();
00151             break;       
00152         case 'i':                
00153             statisticsOutput();
00154             break;
00155         case 'h':    
00156             mp3.play();
00157             break;  
00158         case 'j':    
00159             mp3.pause();
00160             break;  
00161         case 'n':    
00162             next = true;
00163             break;  
00164         default:
00165             break;
00166         }    
00167     }
00168 }
00169 
00170 
00171 
00172 void statisticsOutput()
00173 {    
00174     printf("Statistics\r\n");
00175     printf("Buffer - Size: %i, Free: %i, Loaded: %i\r\n", mp3.bufferLength(), mp3.bufferFree(), mp3.bufferCount());
00176     printf("DREQ: %#x\r\n", _DREQ.read()); 
00177 }
00178 
00179 int main () {        
00180     
00181     // ------------------------------------------------------------------------
00182     //  MP3 Initialising
00183     // ------------------------------------------------------------------------
00184     printf("Initialize mp3 Codec...\r\n");
00185     mp3.initialize();
00186     printf("mp3 Codec is initialized\r\n");
00187 
00188 
00189     mp3.setVolume(VOLUME);
00190     pc.attach(&setVolume);                        
00191 
00192     // ------------------------------------------------------------------------
00193     //  Play mp3 file
00194     // ------------------------------------------------------------------------    
00195 
00196     //while (1)
00197     {
00198         DIR *d;
00199         struct dirent *p;
00200         d = opendir(DIR_NAME);
00201         if(d != NULL) 
00202         {
00203             while ((p = readdir(d)) != NULL) {                          
00204                 char str[160] = DIR_NAME;
00205                 
00206                 //check extension
00207                 char * extension =strrchr( p->d_name,'.') + 1;
00208                 bool isAudioFile = false; 
00209                 if (strcmp(extension, "mp3") == 0)
00210                     isAudioFile = true;
00211                 else if (strcmp(extension, "MP3") == 0)
00212                     isAudioFile = true;
00213                 else if (strcmp(extension, "mp4") == 0)
00214                     isAudioFile = true;
00215                 else if (strcmp(extension, "MP4") == 0)
00216                     isAudioFile = true;
00217                 if (isAudioFile ) 
00218                 {                                                                                         
00219                     printf("Now Playing: %s\r\n", p->d_name);
00220                     
00221                     sprintf(str, "%s/%s", DIR_NAME, p->d_name);
00222                     printf("Path %s\r\n", str);
00223                     
00224                     FILE *song;
00225             
00226                     char array[2048];
00227                                    
00228                     song = fopen(str, "rb");
00229                     //song = fopen("/sd/Musik/01.mp3", "rb");
00230                 
00231                     if (!song) {
00232                         printf("Couldn't open %s\r\n", str);
00233                         continue;
00234                         //exit(1);
00235                     }                                        
00236                     
00237                     int count = 0;
00238                     bool test = true;
00239                     
00240                     while (!feof(song) && !next) {
00241                         int n=fread(&array, 1, sizeof(array), song);
00242                         while (mp3.bufferFree() < n) 
00243                             ;                        
00244                         mp3.bufferPutStream(array,n);                        
00245                         if (count > 2 && test)
00246                         {
00247                             test = false;
00248                             mp3.play();
00249                         }
00250                         count++;
00251                     }                            
00252                     if (next)
00253                     {
00254                         mp3.stop();
00255                         next = false;
00256                     }
00257                     else
00258                     {            
00259                         mp3.terminateStream();
00260                     }
00261                     fclose(song);  //close the file
00262                     
00263                     printf("End of song.\r\n");
00264                     wait(1.0);
00265                 }
00266                 else
00267                 {
00268                      printf("File ignored. Extension: %s\r\n", extension);                
00269                 }
00270 
00271             }
00272             closedir(d);
00273         } 
00274         else 
00275         {
00276             error("Could not open directory!");
00277     //        break;
00278         }
00279                 
00280      }
00281     printf("Done.\r\n");
00282 
00283 }