Wave playing code based of sravet/wave_player, but with mbed-os v5 support and additional PWM output

Dependents:   waveplayer_mbedOS_v5_helloworld

Committer:
HighTide
Date:
Tue Oct 01 00:41:24 2019 +0000
Revision:
2:106018dd7db9
Parent:
1:acc3e18e77ad
Added PWM output and mbed-os v5 support

Who changed what in which revision?

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