Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
VS1053.h
00001 /* mbed VLSI VS1053b library 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 /* This code based on: 00024 * mbeduino_MP3_Shield_MP3Player 00025 * http://mbed.org/users/xshige/programs/mbeduino_MP3_Shield_MP3Player/lgcx63 00026 * 2010-10-16 00027 */ 00028 00029 #ifndef _VS1053_H 00030 #define _VS1053_H 00031 00032 // ---------------------------------------------------------------------------- 00033 // Extended settings 00034 // ---------------------------------------------------------------------------- 00035 // Enable debug output (Output -> printf ...) 00036 // -------------------------------------------------------------------------- 00037 // #define DEBUG 00038 // #define DEBUGOUT (x,y...) printf(x, ##y); 00039 // Patches, Addons 00040 // -------------------------------------------------------------------------- 00041 // #define VS1053_PATCH_1_4_FLAC 00042 // #define VS1053_PATCH_1_5 00043 // #define VS1053_PATCH_1_5_FLAC 00044 // #define VS1053_SPECANA 00045 // #define VS1053B_PCM_RECORDER_0_9 00046 // ---------------------------------------------------------------------------- 00047 00048 #include "defines.h" 00049 #include "mbed.h" 00050 00051 #if defined(VS1053_PATCH_1_4_FLAC) && defined(VS1053_PATCH_1_5) && defined(VS1053_PATCH_1_5_FLAC) && defined(VS1053_SPECANA) && defined(VS1053B_PCM_RECORDER_0_9) 00052 #error "VS1053: Exclusive use of patch and app versions." 00053 #endif 00054 #if defined(VS1053_PATCH_1_4_FLAC) || defined(VS1053_PATCH_1_5) || defined(VS1053_PATCH_1_5_FLAC) || defined(VS1053_SPECANA) || defined(VS1053B_PCM_RECORDER_0_9) 00055 #define VS_PATCH 00056 #endif 00057 00058 #ifndef DEBUGOUT 00059 #define DEBUGOUT(x,y...) 00060 #endif 00061 00062 00063 #define DEFAULT_BALANCE_DIFERENCE_LEFT_RIGHT 0.0f 00064 #define DEFAULT_VOLUME -40.0f 00065 #define DEFAULT_BASS_AMPLITUDE 5 // 0 - 15 dB 00066 #define DEFAULT_BASS_FREQUENCY 100 // 20 - 150 Hz 00067 #define DEFAULT_TREBLE_AMPLITUDE 0 // -8 - 7 dB 00068 #define DEFAULT_TREBLE_FREQUENCY 15000 //1000 - 15000 Hz 00069 00070 00071 00072 // SCI register address assignment 00073 #define SCI_MODE 0x00 00074 #define SCI_STATUS 0x01 00075 #define SCI_BASS 0x02 00076 #define SCI_CLOCKF 0x03 00077 #define SCI_DECODE_TIME 0x04 00078 #define SCI_AUDATA 0x05 00079 #define SCI_WRAM 0x06 00080 #define SCI_WRAMADDR 0x07 00081 #define SCI_HDAT0 0x08 00082 #define SCI_HDAT1 0x09 00083 #define SCI_AIADDR 0x0A 00084 #define SCI_VOL 0x0B 00085 #define SCI_AICTRL0 0x0C 00086 #define SCI_AICTRL1 0x0D 00087 #define SCI_AICTRL2 0x0E 00088 #define SCI_AICTRL3 0x0F 00089 00090 00091 //SCI_MODE register bits as of p.38 of the datasheet 00092 #define SM_DIFF 0x0001 00093 #define SM_LAYER12 0x0002 00094 #define SM_RESET 0x0004 00095 #define SM_CANCEL 0x0008 00096 #define SM_EARSPEAKER_LO 0x0010 00097 #define SM_TESTS 0x0020 00098 #define SM_STREAM 0x0040 00099 #define SM_EARSPEAKER_HI 0x0080 00100 #define SM_DACT 0x0100 00101 #define SM_SDIORD 0x0200 00102 #define SM_SDISHARE 0x0400 00103 #define SM_SDINEW 0x0800 00104 #define SM_ADPCM 0x1000 00105 #define SM_B13 0x2000 00106 #define SM_LINE1 0x4000 00107 #define SM_CLK_RANGE 0x8000 00108 00109 //SCI_CLOCKF register bits as of p.42 of the datasheet 00110 #define SC_ADD_NOMOD 0x0000 00111 #define SC_ADD_10x 0x0800 00112 #define SC_ADD_15x 0x1000 00113 #define SC_ADD_20x 0x1800 00114 #define SC_MULT_XTALI 0x0000 00115 #define SC_MULT_XTALIx20 0x2000 00116 #define SC_MULT_XTALIx25 0x4000 00117 #define SC_MULT_XTALIx30 0x6000 00118 #define SC_MULT_XTALIx35 0x8000 00119 #define SC_MULT_XTALIx40 0xA000 00120 #define SC_MULT_XTALIx45 0xC000 00121 #define SC_MULT_XTALIx50 0xE000 00122 00123 // Extra Parameter in X memory (refer to p.58 of the datasheet) 00124 #define para_chipID_0 0x1E00 00125 #define para_chipID_1 0x1E01 00126 #define para_version 0x1E02 00127 #define para_config1 0x1E03 00128 #define para_playSpeed 0x1E04 00129 #define para_byteRate 0x1E05 00130 #define para_endFillByte 0x1E06 00131 // 00132 #define para_positionMsec_0 0x1E27 00133 #define para_positionMsec_1 0x1E28 00134 #define para_resync 0x1E29 00135 00136 #define INTERRUPT_HANDLER_ENABLE _DREQ_INTERUPT_IN.rise(this, &VS1053::dataRequestHandler); timer.attach_us(this, &VS1053::dataRequestHandler, 1000) 00137 #define INTERRUPT_HANDLER_DISABLE _DREQ_INTERUPT_IN.rise(NULL); timer.detach() 00138 00139 00140 /** Types of audio streams 00141 * 00142 */ 00143 enum AudioType 00144 { 00145 WAV, /*!< WAVE audio stream */ 00146 AAC, /*!< AAC audio stream (ADTS (.aac), MPEG2 ADIF (.aac) and MPEG4 AUDIO (.mp4 / .m4a / .3gp / .3g2)) */ 00147 WMA, /*!< Windows Media Audio (WMA) stream */ 00148 MIDI, /*!< Midi audio stream */ 00149 OGG_VORBIS, /*!< Ogg Vorbis audio stream */ 00150 MP3, /*!< MPEG Audio Layer */ 00151 UNKNOWN /*!< Unknown */ 00152 }; 00153 00154 typedef enum AudioType AudioType; 00155 00156 /** Types of MPEG Audio Layer stream IDs. 00157 * 00158 */ 00159 enum MP3_ID 00160 { 00161 MPG2_5a = 0, /*!< MPG 2.5, nonstandard, proprietary */ 00162 MPG2_5b = 1, /*!< MPG 2.5, nonstandard, proprietary */ 00163 MPG2_0 = 2, /*!< ISO 13818-3 MPG 2.0 */ 00164 MPG1_0 = 3 /*!< ISO 11172-3 MPG 1.0 */ 00165 }; 00166 00167 typedef enum MP3_ID MP3_ID; 00168 00169 /** Types of MPEG Audio Layer channel modes. 00170 * 00171 */ 00172 enum MP3_MODE 00173 { 00174 STEREO = 0, /*!< Stereo */ 00175 JOINT_STEREO = 1, /*!< Joint Stereo */ 00176 DUAL_CHANNEL = 2, /*!< Dual Channel */ 00177 MONO = 3 /*!< Mono */ 00178 }; 00179 00180 typedef enum MP3_MODE MP3_MODE; 00181 /** Struct for informations about audio streams. 00182 * 00183 */ 00184 typedef struct AudioInfo 00185 { 00186 AudioType type : 4; /*!< Type of the audio stream - important for the interpretation of the lower union */ 00187 unsigned short kBitRate ; /*!< Average bitrate of the audio stream - in kBit/s */ 00188 unsigned short decodeTime ; /*!< Decode time */ 00189 union { 00190 struct { 00191 MP3_ID id : 2; /*!< ID */ 00192 char layer : 2; /*!< Layer */ 00193 char protrectBit : 1; /*!< Protect bit, see p.44 of the datasheet */ 00194 char padBit : 1; /*!< Pad bit, see p.44 of the datasheet */ 00195 MP3_MODE mode : 2; /*!< Channel mode */ 00196 char extension : 2; /*!< Extension, see p.44 of the datasheet */ 00197 char copyright : 1; /*!< Copyright, see p.44 of the datasheet */ 00198 char original : 1; /*!< Original, see p.44 of the datasheet */ 00199 char emphasis : 2; /*!< Emphasis, see p.44 of the datasheet */ 00200 char kSampleRate : 6; /*!< Samplerate - in kHz (rounded) */ 00201 } mp3 ; /*!< MPEG Audio Layer */ 00202 struct { 00203 00204 } wma ; /*!< Windows Media Audio (WMA) stream */ 00205 struct { 00206 00207 } aac ; /*!< AAC audio stream */ 00208 struct { 00209 00210 } other ; /*!< Other */ 00211 } ext; 00212 00213 } AudioInfo; 00214 00215 /** Class for VS1053 - Ogg Vorbis / MP3 / AAC / WMA / FLAC / MIDI Audio Codec Chip. 00216 * Datasheet, see http://www.vlsi.fi/fileadmin/datasheets/vlsi/vs1053.pdf 00217 * 00218 * This code based on: 00219 * mbeduino_MP3_Shield_MP3Player 00220 * http://mbed.org/users/xshige/programs/mbeduino_MP3_Shield_MP3Player/lgcx63 00221 * 2010-10-16 00222 * 00223 * For the use of this class, a file "defines.h" must be created. 00224 * It controls debug output and vs1053 patches. 00225 * 00226 * defines.h: 00227 *@code 00228 * #ifndef _DEFINES_H 00229 * #define _DEFINES_H 00230 * // ---------------------------------------------------------------------------- 00231 * // debug output 00232 * // ---------------------------------------------------------------------------- 00233 * // optional 00234 * #define DEBUG 00235 * #ifdef DEBUG 00236 * # define DEBUGOUT(x,y...) printf(x, ##y); 00237 * #else 00238 * # define DEBUGOUT(x,y...) 00239 * #endif 00240 * // ---------------------------------------------------------------------------- 00241 * // VLSI VS1053b library, patch, apps 00242 * // ---------------------------------------------------------------------------- 00243 * // optional, ONLY ONE 00244 * //#define VS1053_PATCH_1_4_FLAC 00245 * //#define VS1053_PATCH_1_5 00246 * #define VS1053_PATCH_1_5_FLAC 00247 * //#define VS1053_SPECANA 00248 * //#define VS1053B_PCM_RECORDER_0_9 00249 * 00250 * #endif //_DEFINES_H 00251 *@endcode 00252 * 00253 * For a complete sample, see http://mbed.org/users/christi_s/programs/Lib_VS1053b 00254 * 00255 */ 00256 class VS1053 { 00257 00258 public: 00259 /** Create a vs1053b object. 00260 * 00261 * @param mosi 00262 * SPI Master Out, Slave In pin to vs1053b. 00263 * @param miso 00264 * SPI Master In, Slave Out pin to vs1053b. 00265 * @param sck 00266 * SPI Clock pin to vs1053b. 00267 * @param cs 00268 * Pin to vs1053b control chip select. 00269 * @param rst 00270 * Pin to vs1053b reset. 00271 * @param dreq 00272 * Pin to vs1053b data request. 00273 * @param dcs 00274 * Pin to vs1053b data chip select. 00275 * @param buffer 00276 * Array to cache audio data. 00277 * @param buffer_size 00278 * Length of the array. 00279 */ 00280 VS1053( 00281 PinName mosi, 00282 PinName miso, 00283 PinName sck, 00284 PinName cs, 00285 PinName rst, 00286 PinName dreq, 00287 PinName dcs, 00288 char* buffer, 00289 int buffer_size 00290 ); 00291 00292 /** Reset the vs1053b. (hardware reset) 00293 * 00294 */ 00295 void reset(void); 00296 00297 /** Stop the playback if the song is completed. 00298 * You must call this function for default playback. 00299 * 00300 */ 00301 void terminateStream(void); 00302 00303 /** Initialize the vs1053b device. 00304 * 00305 * @return 00306 * TRUE on success, FALSE on failure. 00307 */ 00308 bool initialize(void); 00309 00310 /** Set the volume. 00311 * 00312 * @param volume 00313 * Volume -0.5dB, -1.0dB, .. -64.0dB. 00314 */ 00315 void setVolume(float volume = DEFAULT_VOLUME); 00316 00317 /** Get the volume. 00318 * 00319 * @return 00320 * Return the volume in dB. 00321 */ 00322 float getVolume(); 00323 00324 /** Set the balance - volume difference between left-right. 00325 * 00326 * @param balance 00327 * Difference in dB. 00328 */ 00329 void setBalance(float balance = DEFAULT_BALANCE_DIFERENCE_LEFT_RIGHT); 00330 00331 /** Get the balance - volume difference between left-right. 00332 * 00333 * @return 00334 * Difference in dB. 00335 */ 00336 float getBalance(); 00337 00338 /** Get the treble frequency limit. 00339 * 00340 * @return 00341 * Frequenzy 1000, 2000 .. 15000Hz. 00342 */ 00343 int getTrebleFrequency(void); 00344 /** Set the treble frequency limit. 00345 * 00346 * @param frequency 00347 * Frequenzy 1000, 2000, .. 15000Hz. 00348 */ 00349 void setTrebleFrequency(int frequency = DEFAULT_TREBLE_FREQUENCY); 00350 00351 /** Get the treble amplitude. 00352 * 00353 * @return 00354 * Amplitude -8 .. 7dB (in 1.5dB steps); 0 = off. 00355 */ 00356 int getTrebleAmplitude(void); 00357 /** Set the treble amplitude. 00358 * 00359 * @param amplitude 00360 * Amplitude -8 .. 7dB (in 1.5dB steps); 0 = off. 00361 */ 00362 void setTrebleAmplitude(int amplitude = DEFAULT_TREBLE_AMPLITUDE); 00363 00364 /** Get the bass frequency limit. 00365 * 00366 * @return 00367 * Frequenzy 20, 30, .. 150Hz. 00368 */ 00369 int getBassFrequency(void); 00370 /** Set the bass frequency limit. 00371 * 00372 * @param frequency 00373 * Frequenzy 20, 30, .. 150Hz. 00374 */ 00375 void setBassFrequency(int frequency= DEFAULT_BASS_FREQUENCY); 00376 00377 /** Get the bass amplitude. 00378 * 00379 * @return 00380 * Amplitude 0 .. 15dB (in 1dB steps); 0 = off. 00381 */ 00382 int getBassAmplitude(void); 00383 /** Set the bass amplitude. 00384 * 00385 * @param amplitude 00386 * Amplitude 0 .. 15dB (in 1dB steps); 0 = off. 00387 */ 00388 void setBassAmplitude(int amplitude = DEFAULT_BASS_AMPLITUDE); 00389 00390 /** Set the speed of a playback. 00391 * 00392 * @param speed 00393 * Speed 0, 1, .. (0, 1 normal speed). 00394 * Speeds greater 2 are not recommended, buffer must be filled quickly enough. 00395 */ 00396 void setPlaySpeed(unsigned short speed); 00397 00398 /** Copy a byte into the audio data buffer. 00399 * 00400 * @param c 00401 * Data for the buffer. 00402 * 00403 * @return 00404 * TRUE on success; FALSE on failure, c isn't copied in the buffer. 00405 */ 00406 bool bufferSetByte(char c); 00407 00408 /** Copy a array of bytes into the audio data buffer. 00409 * 00410 * @param s 00411 * Data for the buffer. 00412 * 00413 * @param length 00414 * Size of data array. 00415 * 00416 * @return 00417 * TRUE on success; FALSE on failure, s isn't copied in the buffer. 00418 */ 00419 bool bufferPutStream(const char *s, unsigned int length); 00420 00421 /** Get the free space of the audio data buffer. 00422 * 00423 * @return 00424 * Space 0 .. BUFFER_SIZE - 1. 00425 */ 00426 unsigned int bufferFree(void); 00427 00428 /** Get the busy space of the audio data buffer. 00429 * 00430 * @return 00431 * Space 0 .. BUFFER_SIZE - 1. 00432 */ 00433 unsigned int bufferCount(void); 00434 00435 /** Complete length of the audio buffer. 00436 * 00437 * @return 00438 * Buffer length. 00439 */ 00440 unsigned int bufferLength(void); 00441 00442 /** Start playing audio from buffer. 00443 * 00444 */ 00445 void play(void); 00446 00447 /** Interrupt the playback. 00448 * 00449 */ 00450 void pause(void); 00451 00452 /** Stop the playback in the middle of a song. 00453 * After this call, you can now send the next audio file to buffer. 00454 * 00455 */ 00456 void stop(void); 00457 00458 /** Get information about played audio stream. 00459 * 00460 * @param aInfo 00461 * Return value for the informations. 00462 * 00463 */ 00464 void getAudioInfo(AudioInfo* aInfo); 00465 00466 protected: 00467 unsigned short int wram_read(unsigned short int); 00468 void wram_write(unsigned short int, unsigned short int); 00469 void write_plugin(const unsigned short* , unsigned int); 00470 void cs_low(void); 00471 void cs_high(void); 00472 void dcs_low(void); 00473 void dcs_high(void); 00474 void sci_en(void); 00475 void sci_dis(void); 00476 void sdi_en(void); 00477 void sdi_dis(void); 00478 00479 void spi_initialise(void); 00480 00481 void sdi_initialise(void); 00482 00483 void sci_write(unsigned char, unsigned short int); 00484 void sdi_write(unsigned char); 00485 unsigned short int sci_read(unsigned short int); 00486 void sine_test_activate(unsigned char); 00487 00488 void sine_test_deactivate(void); 00489 00490 void changeVolume(void); 00491 00492 00493 // TODO 00494 void power_down(void); 00495 00496 void changeBass(void); 00497 00498 unsigned char bufferGetByte(void); 00499 void bufferReset(void); 00500 void dataRequestHandler(void); 00501 00502 SPI _spi; 00503 DigitalOut _CS; 00504 DigitalOut _RST; 00505 DigitalOut _DCS; 00506 DigitalIn _DREQ; 00507 InterruptIn _DREQ_INTERUPT_IN; 00508 00509 char* _buffer; 00510 char* _bufferReadPointer; 00511 char* _bufferWritePointer; 00512 int BUFFER_SIZE; 00513 00514 bool _isIdle; 00515 00516 // variables to save 00517 // volume, values in db 00518 float _balance; 00519 float _volume; 00520 // bass enhancer settings 00521 int _sb_amplitude; 00522 int _sb_freqlimit; 00523 int _st_amplitude; 00524 int _st_freqlimit; 00525 00526 00527 Ticker timer; 00528 00529 static const char _sampleRateTable[4][4]; // _sampleRateTable[id][srate] 00530 00531 }; 00532 00533 #endif
Generated on Sun Jul 17 2022 15:50:53 by
1.7.2