Kojo
/
RTOS-VS1053b-mp3_v01
My first attempt of Thread based mp3 player with mbed RTOS *player thread *volume control thread
Thread_mp3/main_mp3.cpp@1:4d5a54104bbb, 2012-03-19 (annotated)
- Committer:
- takashikojo
- Date:
- Mon Mar 19 12:00:14 2012 +0000
- Revision:
- 1:4d5a54104bbb
- Parent:
- 0:82078eeba8ba
Thread based mp3 player with mbed RTOS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takashikojo | 0:82078eeba8ba | 1 | /* mbed MP3 Shield Player - Testapplication for VLSI VS1053b Lib |
takashikojo | 0:82078eeba8ba | 2 | * Copyright (c) 2010 Christian Schmiljun |
takashikojo | 0:82078eeba8ba | 3 | * |
takashikojo | 0:82078eeba8ba | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
takashikojo | 0:82078eeba8ba | 5 | * of this software and associated documentation files (the "Software"), to deal |
takashikojo | 0:82078eeba8ba | 6 | * in the Software without restriction, including without limitation the rights |
takashikojo | 0:82078eeba8ba | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
takashikojo | 0:82078eeba8ba | 8 | * copies of the Software, and to permit persons to whom the Software is |
takashikojo | 0:82078eeba8ba | 9 | * furnished to do so, subject to the following conditions: |
takashikojo | 0:82078eeba8ba | 10 | * |
takashikojo | 0:82078eeba8ba | 11 | * The above copyright notice and this permission notice shall be included in |
takashikojo | 0:82078eeba8ba | 12 | * all copies or substantial portions of the Software. |
takashikojo | 0:82078eeba8ba | 13 | * |
takashikojo | 0:82078eeba8ba | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
takashikojo | 0:82078eeba8ba | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
takashikojo | 0:82078eeba8ba | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
takashikojo | 0:82078eeba8ba | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
takashikojo | 0:82078eeba8ba | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
takashikojo | 0:82078eeba8ba | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
takashikojo | 0:82078eeba8ba | 20 | * THE SOFTWARE. |
takashikojo | 0:82078eeba8ba | 21 | */ |
takashikojo | 0:82078eeba8ba | 22 | |
takashikojo | 0:82078eeba8ba | 23 | |
takashikojo | 0:82078eeba8ba | 24 | #include "mbed.h" |
takashikojo | 0:82078eeba8ba | 25 | #include "rtos.h" |
takashikojo | 0:82078eeba8ba | 26 | |
takashikojo | 0:82078eeba8ba | 27 | #include "defines.h" |
takashikojo | 0:82078eeba8ba | 28 | #include "VS1053.h" |
takashikojo | 0:82078eeba8ba | 29 | |
takashikojo | 0:82078eeba8ba | 30 | |
takashikojo | 0:82078eeba8ba | 31 | // Volume [ 0x0000 (loud) .. 0xFEFE (quiet) ] |
takashikojo | 0:82078eeba8ba | 32 | #define VOLUME -22.0f |
takashikojo | 0:82078eeba8ba | 33 | // Switch for local filesystem or SDCard |
takashikojo | 0:82078eeba8ba | 34 | // #define USE_SDCARD |
takashikojo | 0:82078eeba8ba | 35 | |
takashikojo | 0:82078eeba8ba | 36 | // ---------------------------------------------------------------------------- |
takashikojo | 0:82078eeba8ba | 37 | // |
takashikojo | 0:82078eeba8ba | 38 | // Pin Assigenment for SDCard on Cool Components Workshop Board |
takashikojo | 0:82078eeba8ba | 39 | // |
takashikojo | 0:82078eeba8ba | 40 | // SDCard | mbed Side |
takashikojo | 0:82078eeba8ba | 41 | // --------------------------------------- |
takashikojo | 0:82078eeba8ba | 42 | // mosi-----------------5 |
takashikojo | 0:82078eeba8ba | 43 | // miso-----------------6 |
takashikojo | 0:82078eeba8ba | 44 | // sclk-----------------7 |
takashikojo | 0:82078eeba8ba | 45 | // cs-------------------8 |
takashikojo | 0:82078eeba8ba | 46 | // |
takashikojo | 0:82078eeba8ba | 47 | // ---------------------------------------------------------------------------- |
takashikojo | 0:82078eeba8ba | 48 | #ifdef USE_SDCARD |
takashikojo | 0:82078eeba8ba | 49 | #include "SDFileSystem.h" |
takashikojo | 0:82078eeba8ba | 50 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
takashikojo | 0:82078eeba8ba | 51 | #define DIR_NAME "/sd/Musik" |
takashikojo | 0:82078eeba8ba | 52 | #endif |
takashikojo | 0:82078eeba8ba | 53 | |
takashikojo | 0:82078eeba8ba | 54 | #ifdef LOCAL_FILE |
takashikojo | 0:82078eeba8ba | 55 | LocalFileSystem sd("local"); |
takashikojo | 0:82078eeba8ba | 56 | #define DIR_NAME "/local" |
takashikojo | 0:82078eeba8ba | 57 | #endif |
takashikojo | 0:82078eeba8ba | 58 | |
takashikojo | 0:82078eeba8ba | 59 | #define MSC_FILE |
takashikojo | 0:82078eeba8ba | 60 | #ifdef MSC_FILE |
takashikojo | 0:82078eeba8ba | 61 | #include "MSCFileSystem.h" |
takashikojo | 0:82078eeba8ba | 62 | MSCFileSystem sd("msc"); |
takashikojo | 0:82078eeba8ba | 63 | #define DIR_NAME "/msc" |
takashikojo | 0:82078eeba8ba | 64 | #endif |
takashikojo | 0:82078eeba8ba | 65 | |
takashikojo | 0:82078eeba8ba | 66 | |
takashikojo | 0:82078eeba8ba | 67 | |
takashikojo | 0:82078eeba8ba | 68 | // ---------------------------------------------------------------------------- |
takashikojo | 0:82078eeba8ba | 69 | // |
takashikojo | 0:82078eeba8ba | 70 | // Pin Assigenment for Arduino MP3 Shield |
takashikojo | 0:82078eeba8ba | 71 | // (VS1053 employed, issued by sparkfun.com) |
takashikojo | 0:82078eeba8ba | 72 | // |
takashikojo | 0:82078eeba8ba | 73 | // MP3 Sheild Side | mbed Side |
takashikojo | 0:82078eeba8ba | 74 | // --------------------------------------- |
takashikojo | 0:82078eeba8ba | 75 | // RX-------------------10 (optional) |
takashikojo | 0:82078eeba8ba | 76 | // TX-------------------09 (optional) |
takashikojo | 0:82078eeba8ba | 77 | // D2(BSYNC)------------11 |
takashikojo | 0:82078eeba8ba | 78 | // D3(DREQ)-------------10 |
takashikojo | 0:82078eeba8ba | 79 | // |
takashikojo | 0:82078eeba8ba | 80 | // D9(CS)---------------8 |
takashikojo | 0:82078eeba8ba | 81 | // |
takashikojo | 0:82078eeba8ba | 82 | // D11(MOSI)------------5 |
takashikojo | 0:82078eeba8ba | 83 | // D12(MISO)------------6 |
takashikojo | 0:82078eeba8ba | 84 | // D13(SCK)-------------7 |
takashikojo | 0:82078eeba8ba | 85 | // |
takashikojo | 0:82078eeba8ba | 86 | // GND------------------GND(1) |
takashikojo | 0:82078eeba8ba | 87 | // 5V-------------------VU(39) |
takashikojo | 0:82078eeba8ba | 88 | // RESET----------------9 |
takashikojo | 0:82078eeba8ba | 89 | // |
takashikojo | 0:82078eeba8ba | 90 | // ---------------------------------------------------------------------------- |
takashikojo | 0:82078eeba8ba | 91 | const int VS1053B_BUFFER_SIZE = (8 * 1024 + 1); |
takashikojo | 0:82078eeba8ba | 92 | char VS1053_BUFFER[VS1053B_BUFFER_SIZE]; |
takashikojo | 0:82078eeba8ba | 93 | char* VS1053B_BUFFER_POINTER = VS1053_BUFFER; |
takashikojo | 0:82078eeba8ba | 94 | VS1053 mp3( p5, p6, p7, p8, p9, p10, p11, VS1053_BUFFER, VS1053B_BUFFER_SIZE); |
takashikojo | 0:82078eeba8ba | 95 | |
takashikojo | 0:82078eeba8ba | 96 | // Serial for Debug |
takashikojo | 0:82078eeba8ba | 97 | Serial pc(USBTX, USBRX); |
takashikojo | 0:82078eeba8ba | 98 | |
takashikojo | 0:82078eeba8ba | 99 | Ticker timer; |
takashikojo | 0:82078eeba8ba | 100 | Ticker timer2; |
takashikojo | 0:82078eeba8ba | 101 | |
takashikojo | 0:82078eeba8ba | 102 | DigitalIn _DREQ(p11); |
takashikojo | 0:82078eeba8ba | 103 | bool next = false; |
takashikojo | 0:82078eeba8ba | 104 | |
takashikojo | 0:82078eeba8ba | 105 | void statisticsOutput(); |
takashikojo | 0:82078eeba8ba | 106 | |
takashikojo | 1:4d5a54104bbb | 107 | void SetVolume(void const *) { |
takashikojo | 0:82078eeba8ba | 108 | while (1) { |
takashikojo | 0:82078eeba8ba | 109 | if (pc.readable()) { |
takashikojo | 0:82078eeba8ba | 110 | unsigned char c = pc.getc(); |
takashikojo | 0:82078eeba8ba | 111 | // scanf ("%x",&i); |
takashikojo | 0:82078eeba8ba | 112 | switch (c) { |
takashikojo | 0:82078eeba8ba | 113 | case '1': |
takashikojo | 0:82078eeba8ba | 114 | case '2': |
takashikojo | 0:82078eeba8ba | 115 | case '3': |
takashikojo | 0:82078eeba8ba | 116 | case '4': |
takashikojo | 0:82078eeba8ba | 117 | mp3.setPlaySpeed(c - 48); |
takashikojo | 0:82078eeba8ba | 118 | break; |
takashikojo | 0:82078eeba8ba | 119 | case '+': |
takashikojo | 0:82078eeba8ba | 120 | mp3.setVolume(mp3.getVolume() + 0.5); |
takashikojo | 0:82078eeba8ba | 121 | break; |
takashikojo | 0:82078eeba8ba | 122 | case '-': |
takashikojo | 0:82078eeba8ba | 123 | mp3.setVolume(mp3.getVolume() - 0.5); |
takashikojo | 0:82078eeba8ba | 124 | break; |
takashikojo | 0:82078eeba8ba | 125 | case 'k': |
takashikojo | 0:82078eeba8ba | 126 | mp3.setVolume(mp3.getBalance() + 0.5); |
takashikojo | 0:82078eeba8ba | 127 | break; |
takashikojo | 0:82078eeba8ba | 128 | case 'l': |
takashikojo | 0:82078eeba8ba | 129 | mp3.setVolume(mp3.getBalance() - 0.5); |
takashikojo | 0:82078eeba8ba | 130 | break; |
takashikojo | 0:82078eeba8ba | 131 | case 'a': |
takashikojo | 0:82078eeba8ba | 132 | mp3.setTrebleFrequency(mp3.getTrebleFrequency() + 1000); |
takashikojo | 0:82078eeba8ba | 133 | break; |
takashikojo | 0:82078eeba8ba | 134 | case 'y': |
takashikojo | 0:82078eeba8ba | 135 | mp3.setTrebleFrequency(mp3.getTrebleFrequency() - 1000); |
takashikojo | 0:82078eeba8ba | 136 | break; |
takashikojo | 0:82078eeba8ba | 137 | case 's': |
takashikojo | 0:82078eeba8ba | 138 | mp3.setTrebleAmplitude(mp3.getTrebleAmplitude() - 1); |
takashikojo | 0:82078eeba8ba | 139 | break; |
takashikojo | 0:82078eeba8ba | 140 | case 'x': |
takashikojo | 0:82078eeba8ba | 141 | mp3.setTrebleAmplitude(mp3.getTrebleAmplitude() + 1); |
takashikojo | 0:82078eeba8ba | 142 | break; |
takashikojo | 0:82078eeba8ba | 143 | case 'd': |
takashikojo | 0:82078eeba8ba | 144 | mp3.setBassFrequency(mp3.getBassFrequency() + 10); |
takashikojo | 0:82078eeba8ba | 145 | break; |
takashikojo | 0:82078eeba8ba | 146 | case 'c': |
takashikojo | 0:82078eeba8ba | 147 | mp3.setBassFrequency(mp3.getBassFrequency() - 10); |
takashikojo | 0:82078eeba8ba | 148 | break; |
takashikojo | 0:82078eeba8ba | 149 | case 'f': |
takashikojo | 0:82078eeba8ba | 150 | mp3.setBassAmplitude(mp3.getBassAmplitude() - 1); |
takashikojo | 0:82078eeba8ba | 151 | break; |
takashikojo | 0:82078eeba8ba | 152 | case 'v': |
takashikojo | 0:82078eeba8ba | 153 | mp3.setBassAmplitude(mp3.getBassAmplitude() + 1); |
takashikojo | 0:82078eeba8ba | 154 | break; |
takashikojo | 0:82078eeba8ba | 155 | case 'z': |
takashikojo | 0:82078eeba8ba | 156 | timer.attach(&statisticsOutput, 1); |
takashikojo | 0:82078eeba8ba | 157 | break; |
takashikojo | 0:82078eeba8ba | 158 | case 'u': |
takashikojo | 0:82078eeba8ba | 159 | timer.detach(); |
takashikojo | 0:82078eeba8ba | 160 | break; |
takashikojo | 0:82078eeba8ba | 161 | case 'i': |
takashikojo | 0:82078eeba8ba | 162 | statisticsOutput(); |
takashikojo | 0:82078eeba8ba | 163 | break; |
takashikojo | 0:82078eeba8ba | 164 | case 'h': |
takashikojo | 0:82078eeba8ba | 165 | mp3.play(); |
takashikojo | 0:82078eeba8ba | 166 | break; |
takashikojo | 0:82078eeba8ba | 167 | case 'j': |
takashikojo | 0:82078eeba8ba | 168 | mp3.pause(); |
takashikojo | 0:82078eeba8ba | 169 | break; |
takashikojo | 0:82078eeba8ba | 170 | case 'n': |
takashikojo | 0:82078eeba8ba | 171 | next = true; |
takashikojo | 0:82078eeba8ba | 172 | break; |
takashikojo | 0:82078eeba8ba | 173 | default: |
takashikojo | 0:82078eeba8ba | 174 | break; |
takashikojo | 0:82078eeba8ba | 175 | } |
takashikojo | 0:82078eeba8ba | 176 | } |
takashikojo | 0:82078eeba8ba | 177 | Thread::wait(100) ; |
takashikojo | 0:82078eeba8ba | 178 | } |
takashikojo | 0:82078eeba8ba | 179 | } |
takashikojo | 0:82078eeba8ba | 180 | |
takashikojo | 0:82078eeba8ba | 181 | void statisticsOutput() { |
takashikojo | 0:82078eeba8ba | 182 | printf("Statistics\r\n"); |
takashikojo | 0:82078eeba8ba | 183 | printf("Buffer - Size: %i, Free: %i, Loaded: %i\r\n", mp3.bufferLength(), mp3.bufferFree(), mp3.bufferCount()); |
takashikojo | 0:82078eeba8ba | 184 | printf("DREQ: %#x\r\n", _DREQ.read()); |
takashikojo | 0:82078eeba8ba | 185 | } |
takashikojo | 0:82078eeba8ba | 186 | |
takashikojo | 0:82078eeba8ba | 187 | DigitalOut led2(LED2); // Ticker for mp3 |
takashikojo | 0:82078eeba8ba | 188 | |
takashikojo | 1:4d5a54104bbb | 189 | void playFile(char *fname) { |
takashikojo | 1:4d5a54104bbb | 190 | FILE *song; |
takashikojo | 1:4d5a54104bbb | 191 | static char array[2048]; |
takashikojo | 1:4d5a54104bbb | 192 | |
takashikojo | 1:4d5a54104bbb | 193 | song = fopen(fname, "rb"); |
takashikojo | 1:4d5a54104bbb | 194 | if (!song) { |
takashikojo | 1:4d5a54104bbb | 195 | printf("Couldn't open %s\r\n", fname); |
takashikojo | 1:4d5a54104bbb | 196 | return ; |
takashikojo | 1:4d5a54104bbb | 197 | } |
takashikojo | 1:4d5a54104bbb | 198 | |
takashikojo | 1:4d5a54104bbb | 199 | int count = 0; |
takashikojo | 1:4d5a54104bbb | 200 | bool test = true; |
takashikojo | 1:4d5a54104bbb | 201 | |
takashikojo | 1:4d5a54104bbb | 202 | while (!feof(song) && !next) { |
takashikojo | 1:4d5a54104bbb | 203 | int n=fread(&array, 1, sizeof(array), song); |
takashikojo | 1:4d5a54104bbb | 204 | while (mp3.bufferFree() < n) { |
takashikojo | 1:4d5a54104bbb | 205 | led2 = 1 ; |
takashikojo | 1:4d5a54104bbb | 206 | Thread::wait(100) ; |
takashikojo | 1:4d5a54104bbb | 207 | led2 = 0 ; |
takashikojo | 1:4d5a54104bbb | 208 | } |
takashikojo | 1:4d5a54104bbb | 209 | mp3.bufferPutStream(array,n); |
takashikojo | 1:4d5a54104bbb | 210 | if (count > 2 && test) { |
takashikojo | 1:4d5a54104bbb | 211 | test = false; |
takashikojo | 1:4d5a54104bbb | 212 | mp3.play(); |
takashikojo | 1:4d5a54104bbb | 213 | } |
takashikojo | 1:4d5a54104bbb | 214 | count++; |
takashikojo | 1:4d5a54104bbb | 215 | } |
takashikojo | 1:4d5a54104bbb | 216 | if (next) { |
takashikojo | 1:4d5a54104bbb | 217 | mp3.stop(); |
takashikojo | 1:4d5a54104bbb | 218 | next = false; |
takashikojo | 1:4d5a54104bbb | 219 | } else { |
takashikojo | 1:4d5a54104bbb | 220 | mp3.terminateStream(); |
takashikojo | 1:4d5a54104bbb | 221 | } |
takashikojo | 1:4d5a54104bbb | 222 | fclose(song); //close the file |
takashikojo | 1:4d5a54104bbb | 223 | } |
takashikojo | 1:4d5a54104bbb | 224 | |
takashikojo | 1:4d5a54104bbb | 225 | |
takashikojo | 0:82078eeba8ba | 226 | void main_mp3 (void const *argument) { |
takashikojo | 0:82078eeba8ba | 227 | |
takashikojo | 0:82078eeba8ba | 228 | // ------------------------------------------------------------------------ |
takashikojo | 0:82078eeba8ba | 229 | // MP3 Initialising |
takashikojo | 0:82078eeba8ba | 230 | // ------------------------------------------------------------------------ |
takashikojo | 0:82078eeba8ba | 231 | printf("Initialize mp3 Codec...\r\n"); |
takashikojo | 0:82078eeba8ba | 232 | mp3.initialize(); |
takashikojo | 0:82078eeba8ba | 233 | printf("mp3 Codec is initialized\r\n"); |
takashikojo | 1:4d5a54104bbb | 234 | |
takashikojo | 0:82078eeba8ba | 235 | mp3.setVolume(VOLUME); |
takashikojo | 1:4d5a54104bbb | 236 | Thread t(SetVolume , NULL, osPriorityNormal, (DEFAULT_STACK_SIZE)); |
takashikojo | 0:82078eeba8ba | 237 | |
takashikojo | 0:82078eeba8ba | 238 | // ------------------------------------------------------------------------ |
takashikojo | 0:82078eeba8ba | 239 | // Play mp3 file |
takashikojo | 0:82078eeba8ba | 240 | // ------------------------------------------------------------------------ |
takashikojo | 0:82078eeba8ba | 241 | |
takashikojo | 0:82078eeba8ba | 242 | //while (1) |
takashikojo | 0:82078eeba8ba | 243 | { |
takashikojo | 0:82078eeba8ba | 244 | DIR *d; |
takashikojo | 0:82078eeba8ba | 245 | struct dirent *p; |
takashikojo | 0:82078eeba8ba | 246 | d = opendir(DIR_NAME); |
takashikojo | 0:82078eeba8ba | 247 | if (d != NULL) { |
takashikojo | 0:82078eeba8ba | 248 | while ((p = readdir(d)) != NULL) { |
takashikojo | 0:82078eeba8ba | 249 | static char str[160] = DIR_NAME; |
takashikojo | 0:82078eeba8ba | 250 | |
takashikojo | 0:82078eeba8ba | 251 | //check extension |
takashikojo | 0:82078eeba8ba | 252 | char * extension =strrchr( p->d_name,'.') + 1; |
takashikojo | 0:82078eeba8ba | 253 | bool isAudioFile = false; |
takashikojo | 0:82078eeba8ba | 254 | if (strcmp(extension, "mp3") == 0) |
takashikojo | 0:82078eeba8ba | 255 | isAudioFile = true; |
takashikojo | 0:82078eeba8ba | 256 | else if (strcmp(extension, "MP3") == 0) |
takashikojo | 0:82078eeba8ba | 257 | isAudioFile = true; |
takashikojo | 0:82078eeba8ba | 258 | else if (strcmp(extension, "mp4") == 0) |
takashikojo | 0:82078eeba8ba | 259 | isAudioFile = true; |
takashikojo | 0:82078eeba8ba | 260 | else if (strcmp(extension, "MP4") == 0) |
takashikojo | 0:82078eeba8ba | 261 | isAudioFile = true; |
takashikojo | 0:82078eeba8ba | 262 | if (isAudioFile ) { |
takashikojo | 0:82078eeba8ba | 263 | printf("Now Playing: %s\r\n", p->d_name); |
takashikojo | 0:82078eeba8ba | 264 | |
takashikojo | 0:82078eeba8ba | 265 | sprintf(str, "%s/%s", DIR_NAME, p->d_name); |
takashikojo | 0:82078eeba8ba | 266 | printf("Path %s\r\n", str); |
takashikojo | 0:82078eeba8ba | 267 | |
takashikojo | 1:4d5a54104bbb | 268 | playFile(str) ; /*** Play the mp3 file ***/ |
takashikojo | 0:82078eeba8ba | 269 | |
takashikojo | 0:82078eeba8ba | 270 | printf("End of song.\r\n"); |
takashikojo | 0:82078eeba8ba | 271 | wait(1.0); |
takashikojo | 0:82078eeba8ba | 272 | } else { |
takashikojo | 0:82078eeba8ba | 273 | printf("File ignored. Extension: %s\r\n", extension); |
takashikojo | 0:82078eeba8ba | 274 | } |
takashikojo | 0:82078eeba8ba | 275 | |
takashikojo | 0:82078eeba8ba | 276 | } |
takashikojo | 0:82078eeba8ba | 277 | closedir(d); |
takashikojo | 0:82078eeba8ba | 278 | } else { |
takashikojo | 0:82078eeba8ba | 279 | error("Could not open directory!"); |
takashikojo | 0:82078eeba8ba | 280 | // break; |
takashikojo | 0:82078eeba8ba | 281 | } |
takashikojo | 0:82078eeba8ba | 282 | |
takashikojo | 0:82078eeba8ba | 283 | } |
takashikojo | 0:82078eeba8ba | 284 | printf("Done.\r\n"); |
takashikojo | 0:82078eeba8ba | 285 | |
takashikojo | 0:82078eeba8ba | 286 | } |