f
Dependencies: mbed 4DGL-uLCD-SE MMA8452
wave_player/wave_player.h@0:8e3b9bb1084a, 2022-03-10 (annotated)
- Committer:
- lballard9
- Date:
- Thu Mar 10 05:18:13 2022 +0000
- Revision:
- 0:8e3b9bb1084a
shell rev b;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lballard9 | 0:8e3b9bb1084a | 1 | #include <mbed.h> |
lballard9 | 0:8e3b9bb1084a | 2 | |
lballard9 | 0:8e3b9bb1084a | 3 | typedef struct uFMT_STRUCT { |
lballard9 | 0:8e3b9bb1084a | 4 | short comp_code; |
lballard9 | 0:8e3b9bb1084a | 5 | short num_channels; |
lballard9 | 0:8e3b9bb1084a | 6 | unsigned sample_rate; |
lballard9 | 0:8e3b9bb1084a | 7 | unsigned avg_Bps; |
lballard9 | 0:8e3b9bb1084a | 8 | short block_align; |
lballard9 | 0:8e3b9bb1084a | 9 | short sig_bps; |
lballard9 | 0:8e3b9bb1084a | 10 | } FMT_STRUCT; |
lballard9 | 0:8e3b9bb1084a | 11 | |
lballard9 | 0:8e3b9bb1084a | 12 | |
lballard9 | 0:8e3b9bb1084a | 13 | /** wave file player class. |
lballard9 | 0:8e3b9bb1084a | 14 | * |
lballard9 | 0:8e3b9bb1084a | 15 | * Example: |
lballard9 | 0:8e3b9bb1084a | 16 | * @code |
lballard9 | 0:8e3b9bb1084a | 17 | * #include <mbed.h> |
lballard9 | 0:8e3b9bb1084a | 18 | * #include <wave_player.h> |
lballard9 | 0:8e3b9bb1084a | 19 | * |
lballard9 | 0:8e3b9bb1084a | 20 | * AnalogOut DACout(p18); |
lballard9 | 0:8e3b9bb1084a | 21 | * wave_player waver(&DACout); |
lballard9 | 0:8e3b9bb1084a | 22 | * |
lballard9 | 0:8e3b9bb1084a | 23 | * int main() { |
lballard9 | 0:8e3b9bb1084a | 24 | * FILE *wave_file; |
lballard9 | 0:8e3b9bb1084a | 25 | * |
lballard9 | 0:8e3b9bb1084a | 26 | * printf("\n\n\nHello, wave world!\n"); |
lballard9 | 0:8e3b9bb1084a | 27 | * wave_file=fopen("/sd/44_8_st.wav","r"); |
lballard9 | 0:8e3b9bb1084a | 28 | * waver.play(wave_file); |
lballard9 | 0:8e3b9bb1084a | 29 | * fclose(wave_file); |
lballard9 | 0:8e3b9bb1084a | 30 | * } |
lballard9 | 0:8e3b9bb1084a | 31 | * @endcode |
lballard9 | 0:8e3b9bb1084a | 32 | */ |
lballard9 | 0:8e3b9bb1084a | 33 | class wave_player { |
lballard9 | 0:8e3b9bb1084a | 34 | |
lballard9 | 0:8e3b9bb1084a | 35 | public: |
lballard9 | 0:8e3b9bb1084a | 36 | /** Create a wave player using a pointer to the given AnalogOut object. |
lballard9 | 0:8e3b9bb1084a | 37 | * |
lballard9 | 0:8e3b9bb1084a | 38 | * @param _dac pointer to an AnalogOut object to which the samples are sent. |
lballard9 | 0:8e3b9bb1084a | 39 | */ |
lballard9 | 0:8e3b9bb1084a | 40 | wave_player(AnalogOut *_dac); |
lballard9 | 0:8e3b9bb1084a | 41 | |
lballard9 | 0:8e3b9bb1084a | 42 | /** the player function. |
lballard9 | 0:8e3b9bb1084a | 43 | * |
lballard9 | 0:8e3b9bb1084a | 44 | * @param wavefile A pointer to an opened wave file |
lballard9 | 0:8e3b9bb1084a | 45 | */ |
lballard9 | 0:8e3b9bb1084a | 46 | void play(FILE *wavefile); |
lballard9 | 0:8e3b9bb1084a | 47 | |
lballard9 | 0:8e3b9bb1084a | 48 | /** Set the printf verbosity of the wave player. A nonzero verbosity level |
lballard9 | 0:8e3b9bb1084a | 49 | * will put wave_player in a mode where the complete contents of the wave |
lballard9 | 0:8e3b9bb1084a | 50 | * file are echoed to the screen, including header values, and including |
lballard9 | 0:8e3b9bb1084a | 51 | * all of the sample values placed into the DAC FIFO, and the sample values |
lballard9 | 0:8e3b9bb1084a | 52 | * removed from the DAC FIFO by the ISR. The sample output frequency is |
lballard9 | 0:8e3b9bb1084a | 53 | * fixed at 2 Hz in this mode, so it's all very slow and the DAC output isn't |
lballard9 | 0:8e3b9bb1084a | 54 | * very useful, but it lets you see what's going on and may help for debugging |
lballard9 | 0:8e3b9bb1084a | 55 | * wave files that don't play correctly. |
lballard9 | 0:8e3b9bb1084a | 56 | * |
lballard9 | 0:8e3b9bb1084a | 57 | * @param v the verbosity level |
lballard9 | 0:8e3b9bb1084a | 58 | */ |
lballard9 | 0:8e3b9bb1084a | 59 | void set_verbosity(int v); |
lballard9 | 0:8e3b9bb1084a | 60 | |
lballard9 | 0:8e3b9bb1084a | 61 | private: |
lballard9 | 0:8e3b9bb1084a | 62 | void dac_out(void); |
lballard9 | 0:8e3b9bb1084a | 63 | int verbosity; |
lballard9 | 0:8e3b9bb1084a | 64 | AnalogOut *wave_DAC; |
lballard9 | 0:8e3b9bb1084a | 65 | Ticker tick; |
lballard9 | 0:8e3b9bb1084a | 66 | unsigned short DAC_fifo[256]; |
lballard9 | 0:8e3b9bb1084a | 67 | short DAC_wptr; |
lballard9 | 0:8e3b9bb1084a | 68 | volatile short DAC_rptr; |
lballard9 | 0:8e3b9bb1084a | 69 | short DAC_on; |
lballard9 | 0:8e3b9bb1084a | 70 | }; |
lballard9 | 0:8e3b9bb1084a | 71 | |
lballard9 | 0:8e3b9bb1084a | 72 |