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