A library which allows the playing of Wav files using the TLV320
Dependents: RSALB_hbridge_helloworld RSALB_lobster WavPlayer_test AudioCODEC_HelloWorld
WavPlayer.h@3:a7380cfc1987, 2012-09-21 (annotated)
- Committer:
- p07gbar
- Date:
- Fri Sep 21 14:24:00 2012 +0000
- Revision:
- 3:a7380cfc1987
- Parent:
- 0:3695886f3495
Minor fix
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
p07gbar | 0:3695886f3495 | 1 | /** |
p07gbar | 0:3695886f3495 | 2 | * @author Giles Barton-Owen |
p07gbar | 0:3695886f3495 | 3 | * |
p07gbar | 0:3695886f3495 | 4 | * @section LICENSE |
p07gbar | 0:3695886f3495 | 5 | * |
p07gbar | 0:3695886f3495 | 6 | * Copyright (c) 2012 mbed |
p07gbar | 0:3695886f3495 | 7 | * |
p07gbar | 0:3695886f3495 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
p07gbar | 0:3695886f3495 | 9 | * of this software and associated documentation files (the "Software"), to deal |
p07gbar | 0:3695886f3495 | 10 | * in the Software without restriction, including without limitation the rights |
p07gbar | 0:3695886f3495 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
p07gbar | 0:3695886f3495 | 12 | * copies of the Software, and to permit persons to whom the Software is |
p07gbar | 0:3695886f3495 | 13 | * furnished to do so, subject to the following conditions: |
p07gbar | 0:3695886f3495 | 14 | * |
p07gbar | 0:3695886f3495 | 15 | * The above copyright notice and this permission notice shall be included in |
p07gbar | 0:3695886f3495 | 16 | * all copies or substantial portions of the Software. |
p07gbar | 0:3695886f3495 | 17 | * |
p07gbar | 0:3695886f3495 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
p07gbar | 0:3695886f3495 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
p07gbar | 0:3695886f3495 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
p07gbar | 0:3695886f3495 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
p07gbar | 0:3695886f3495 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
p07gbar | 0:3695886f3495 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
p07gbar | 0:3695886f3495 | 24 | * THE SOFTWARE. |
p07gbar | 0:3695886f3495 | 25 | * |
p07gbar | 0:3695886f3495 | 26 | * @section DESCRIPTION |
p07gbar | 0:3695886f3495 | 27 | * A WAV player library for the TLV320 and the LPC1768's built in I2S peripheral |
p07gbar | 0:3695886f3495 | 28 | * |
p07gbar | 0:3695886f3495 | 29 | */ |
p07gbar | 0:3695886f3495 | 30 | |
p07gbar | 0:3695886f3495 | 31 | #ifndef WAVPLAYER_H |
p07gbar | 0:3695886f3495 | 32 | #define WAVPLAYER_H |
p07gbar | 0:3695886f3495 | 33 | |
p07gbar | 0:3695886f3495 | 34 | #include "mbed.h" |
p07gbar | 0:3695886f3495 | 35 | #include "WavPlayerConfig.h" |
p07gbar | 0:3695886f3495 | 36 | #include "RingBuffer.h" |
p07gbar | 0:3695886f3495 | 37 | #include "TLV320.h" |
p07gbar | 0:3695886f3495 | 38 | #include "I2S.h" |
p07gbar | 0:3695886f3495 | 39 | |
p07gbar | 0:3695886f3495 | 40 | /** A class to play WAV files from a file system, tested with USB |
p07gbar | 0:3695886f3495 | 41 | * |
p07gbar | 0:3695886f3495 | 42 | * Example (note, this has requires the USB MSC library to be imported): |
p07gbar | 0:3695886f3495 | 43 | * @code |
p07gbar | 0:3695886f3495 | 44 | * |
p07gbar | 0:3695886f3495 | 45 | * #include "mbed.h" |
p07gbar | 0:3695886f3495 | 46 | * #include "WavPlayer.h" |
p07gbar | 0:3695886f3495 | 47 | * #include "MSCFileSystem.h" |
p07gbar | 0:3695886f3495 | 48 | * |
p07gbar | 0:3695886f3495 | 49 | * MSCFileSystem msc("msc"); // Mount flash drive under the name "msc" |
p07gbar | 0:3695886f3495 | 50 | * WavPlayer player; |
p07gbar | 0:3695886f3495 | 51 | * |
p07gbar | 0:3695886f3495 | 52 | * int main() { |
p07gbar | 0:3695886f3495 | 53 | * FILE *fp = fopen("/msc/test.wav", "r"); // Open "out.txt" on the local file system for writing |
p07gbar | 0:3695886f3495 | 54 | * player.open(&fp); |
p07gbar | 0:3695886f3495 | 55 | * player.play(); |
p07gbar | 0:3695886f3495 | 56 | * fclose(fp); |
p07gbar | 0:3695886f3495 | 57 | * } |
p07gbar | 0:3695886f3495 | 58 | * @endcode |
p07gbar | 0:3695886f3495 | 59 | */ |
p07gbar | 0:3695886f3495 | 60 | |
p07gbar | 0:3695886f3495 | 61 | |
p07gbar | 0:3695886f3495 | 62 | class WavPlayer |
p07gbar | 0:3695886f3495 | 63 | { |
p07gbar | 0:3695886f3495 | 64 | public: |
p07gbar | 0:3695886f3495 | 65 | /** Create a WavPlayer instance |
p07gbar | 0:3695886f3495 | 66 | * |
p07gbar | 0:3695886f3495 | 67 | */ |
p07gbar | 0:3695886f3495 | 68 | WavPlayer(); |
p07gbar | 0:3695886f3495 | 69 | |
p07gbar | 0:3695886f3495 | 70 | /** Create a WavPlayer instance |
p07gbar | 0:3695886f3495 | 71 | * |
p07gbar | 0:3695886f3495 | 72 | * @param fpp A pointer to a file pointer to read out of |
p07gbar | 0:3695886f3495 | 73 | */ |
p07gbar | 0:3695886f3495 | 74 | WavPlayer(FILE **fpp); |
p07gbar | 0:3695886f3495 | 75 | |
p07gbar | 0:3695886f3495 | 76 | /** Set the file to read out of |
p07gbar | 0:3695886f3495 | 77 | * |
p07gbar | 0:3695886f3495 | 78 | * @param fpp A pointer to a file pointer to read out of |
p07gbar | 0:3695886f3495 | 79 | */ |
p07gbar | 0:3695886f3495 | 80 | void open(FILE **fpp); |
p07gbar | 0:3695886f3495 | 81 | |
p07gbar | 0:3695886f3495 | 82 | /** Extract the header infomation, automatically called by open |
p07gbar | 0:3695886f3495 | 83 | */ |
p07gbar | 0:3695886f3495 | 84 | int getConfig(); |
p07gbar | 0:3695886f3495 | 85 | |
p07gbar | 0:3695886f3495 | 86 | /** Play the entire file. Blocking |
p07gbar | 0:3695886f3495 | 87 | */ |
p07gbar | 0:3695886f3495 | 88 | float play(); |
p07gbar | 0:3695886f3495 | 89 | |
p07gbar | 0:3695886f3495 | 90 | /** Play the file for a certain number of seconds. Blocking |
p07gbar | 0:3695886f3495 | 91 | * |
p07gbar | 0:3695886f3495 | 92 | * @param time The number of seconds to play the file for. |
p07gbar | 0:3695886f3495 | 93 | */ |
p07gbar | 0:3695886f3495 | 94 | float play(float time); |
p07gbar | 0:3695886f3495 | 95 | |
p07gbar | 0:3695886f3495 | 96 | /** Play the file for a certain number of seconds, from a certain start point. Blocking |
p07gbar | 0:3695886f3495 | 97 | * |
p07gbar | 0:3695886f3495 | 98 | * @param start The start time |
p07gbar | 0:3695886f3495 | 99 | * @param timefor The number of seconds to play the file for. |
p07gbar | 0:3695886f3495 | 100 | */ |
p07gbar | 0:3695886f3495 | 101 | float play(float start, float timefor); |
p07gbar | 0:3695886f3495 | 102 | |
p07gbar | 0:3695886f3495 | 103 | |
p07gbar | 0:3695886f3495 | 104 | |
p07gbar | 0:3695886f3495 | 105 | private: |
p07gbar | 0:3695886f3495 | 106 | WavPlayerConfig config; |
p07gbar | 0:3695886f3495 | 107 | FILE ** filepp; |
p07gbar | 0:3695886f3495 | 108 | RingBuffer rbuf; |
p07gbar | 0:3695886f3495 | 109 | |
p07gbar | 0:3695886f3495 | 110 | I2S i2s; |
p07gbar | 0:3695886f3495 | 111 | TLV320 codec; |
p07gbar | 0:3695886f3495 | 112 | static void i2sisr(); |
p07gbar | 0:3695886f3495 | 113 | void i2sisr_(); |
p07gbar | 0:3695886f3495 | 114 | static WavPlayer* instance; |
p07gbar | 0:3695886f3495 | 115 | |
p07gbar | 0:3695886f3495 | 116 | int fseekread(FILE *fp, char* str, int offset, int len); |
p07gbar | 0:3695886f3495 | 117 | uint32_t getu32(char str[], int len); |
p07gbar | 0:3695886f3495 | 118 | uint16_t getu16(char str[], int len); |
p07gbar | 0:3695886f3495 | 119 | int32_t get32(char str[], int len); |
p07gbar | 0:3695886f3495 | 120 | int16_t get16(char str[], int len); |
p07gbar | 0:3695886f3495 | 121 | uint32_t fsru32(FILE *fp, int offset, int len = 4); |
p07gbar | 0:3695886f3495 | 122 | uint16_t fsru16(FILE *fp, int offset, int len = 2); |
p07gbar | 0:3695886f3495 | 123 | void clear(char* str, int len); |
p07gbar | 0:3695886f3495 | 124 | |
p07gbar | 0:3695886f3495 | 125 | int findChunk(FILE *fp, char* match, int len, int fileSize, int startOffset = 12); |
p07gbar | 0:3695886f3495 | 126 | int findChunk(FILE *fp, char* match, int len, int startOffset = 12); |
p07gbar | 0:3695886f3495 | 127 | void fgets_m(char* str, int num, FILE* fp); |
p07gbar | 0:3695886f3495 | 128 | |
p07gbar | 0:3695886f3495 | 129 | float sine_gen(int16_t* buf, int len, float div, float phase); |
p07gbar | 0:3695886f3495 | 130 | |
p07gbar | 0:3695886f3495 | 131 | bool flag_vol; |
p07gbar | 0:3695886f3495 | 132 | double volume; |
p07gbar | 0:3695886f3495 | 133 | |
p07gbar | 0:3695886f3495 | 134 | bool stereo; |
p07gbar | 0:3695886f3495 | 135 | |
p07gbar | 0:3695886f3495 | 136 | bool flag_play; |
p07gbar | 0:3695886f3495 | 137 | |
p07gbar | 0:3695886f3495 | 138 | Timer timer; |
p07gbar | 0:3695886f3495 | 139 | |
p07gbar | 0:3695886f3495 | 140 | int isr_underrun; |
p07gbar | 0:3695886f3495 | 141 | |
p07gbar | 0:3695886f3495 | 142 | float read_time; |
p07gbar | 0:3695886f3495 | 143 | int read_time_div; |
p07gbar | 0:3695886f3495 | 144 | |
p07gbar | 0:3695886f3495 | 145 | float isr_time; |
p07gbar | 0:3695886f3495 | 146 | int isr_time_div; |
p07gbar | 0:3695886f3495 | 147 | |
p07gbar | 0:3695886f3495 | 148 | float current_time; |
p07gbar | 0:3695886f3495 | 149 | |
p07gbar | 0:3695886f3495 | 150 | int slow_count; |
p07gbar | 0:3695886f3495 | 151 | |
p07gbar | 0:3695886f3495 | 152 | //Timer isrtimer; |
p07gbar | 0:3695886f3495 | 153 | |
p07gbar | 0:3695886f3495 | 154 | //DigitalOut ext_flag; |
p07gbar | 0:3695886f3495 | 155 | //DigitalOut run_flag; |
p07gbar | 0:3695886f3495 | 156 | |
p07gbar | 0:3695886f3495 | 157 | }; |
p07gbar | 0:3695886f3495 | 158 | |
p07gbar | 0:3695886f3495 | 159 | #endif |