Sound update
Dependencies: 4DGL-uLCD-SE Physac-MBED PinDetect SDFileSystem mbed-rtos mbed
Revision 21:df9e8f45e14c, committed 19 months ago
- Comitter:
- jstephens78
- Date:
- Tue Dec 06 22:09:51 2022 +0000
- Parent:
- 20:73e4f12277bd
- Child:
- 22:601e6f9077e4
- Commit message:
- Uncomment audio thread
Changed in this revision
--- a/globals.h Mon Dec 05 20:51:36 2022 +0000 +++ b/globals.h Tue Dec 06 22:09:51 2022 +0000 @@ -33,7 +33,6 @@ extern PwmOut Rgb; extern PwmOut rGb; extern PwmOut rgB; -extern PwmOut PWMout; //extern AnalogOut DACout; //extern wave_player waver;
--- a/main.cpp Mon Dec 05 20:51:36 2022 +0000
+++ b/main.cpp Tue Dec 06 22:09:51 2022 +0000
@@ -5,7 +5,7 @@
#include "hockey.h"
#include "tetris.h"
-//#include "Speaker.h"
+#include "Speaker.h"
#include "SDFileSystem.h"
#include "uLCD_4DGL.h"
@@ -27,15 +27,14 @@
Mutex uLCD_mutex;
BluefruitController blue(p13,p14);
-BusIn navSwitch(p15, p16, p17, p18, p19);
+BusIn navSwitch(p15, p16, p17, p19, p20);
SDFileSystem sd(p5, p6, p7, p8, "sd");
PwmOut Rgb(p23);
PwmOut rGb(p24);
PwmOut rgB(p25);
-PwmOut PWMout(p21);
-//AnalogOut DACout(p20);
-//wave_player waver(&DACout);
+AnalogOut DACout(p18);
+wave_player waver(&DACout);
@@ -44,7 +43,10 @@
////////////////////////////////
// The following variables are *not* from "globals.h" and are not meant for
// reference outside of main.cpp
-DigitalOut myled(LED1);
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
@@ -53,26 +55,30 @@
//////////////////////
void audioThread()
{
- while(true) Thread::wait(1000);
- /*
- FILE *wave_file;
- PWMout.period(1.0/400000.0);
- while (game1 == false && game2 == false) {
- wave_file=fopen("/sd/MiiMenu.wav","r");
- waver.play(wave_file);
- fclose(wave_file);
+ while (true) {
+ FILE *wave_file;
+ while (game1 == false && game2 == false) {
+ led2 = true;
+ wave_file=fopen("/sd/MiiMenu.wav","r");
+ waver.play(wave_file);
+ fclose(wave_file);
+ led2 = false;
+ }
+ while (game1 == true && game2 == false) {
+ led2 = true;
+ wave_file=fopen("/sd/tetris.wav","r");
+ waver.play(wave_file);
+ fclose(wave_file);
+ led2 = false;
+ }
+ while (game2 == true && game1 == false) {
+ led2 = true;
+ wave_file=fopen("/sd/WiiPlayAirHockey.wav","r");
+ waver.play(wave_file);
+ fclose(wave_file);
+ led2 = false;
+ }
}
- while (game1 == true && game2 == false) {
- wave_file=fopen("/sd/tetris.wav","r");
- waver.play(wave_file);
- fclose(wave_file);
- }
- while (game2 == true && game1 == false) {
- wave_file=fopen("/sd/WiiPlayAirHockey.wav","r");
- waver.play(wave_file);
- fclose(wave_file);
- }
- */
}
void ledThread()
@@ -109,9 +115,9 @@
// The main thread goes on to blink LEDs
while (true) {
- myled = 1;
+ led1 = 1;
Thread::wait(500);
- myled = 0;
+ led1 = 0;
Thread::wait(500);
PRINTF("[MAIN] Thread stacks: %lu %lu %lu %lu\r\n",
--- a/wave_player_appbd/wave_player.cpp Mon Dec 05 20:51:36 2022 +0000
+++ b/wave_player_appbd/wave_player.cpp Tue Dec 06 22:09:51 2022 +0000
@@ -20,9 +20,9 @@
// constructor -- accepts an mbed pin to use for AnalogOut. Only p18 will work
wave_player::wave_player(AnalogOut *_dac)
{
- wave_DAC=_dac;
- wave_DAC->write_u16(32768); //DAC is 0-3.3V, so idles at ~1.6V
- verbosity=0;
+ wave_DAC=_dac;
+ wave_DAC->write_u16(32768); //DAC is 0-3.3V, so idles at ~1.6V
+ verbosity=0;
}
//-----------------------------------------------------------------------------
@@ -34,7 +34,7 @@
//-----------------------------------------------------------------------------
void wave_player::set_verbosity(int v)
{
- verbosity=v;
+ verbosity=v;
}
//-----------------------------------------------------------------------------
@@ -46,77 +46,77 @@
//-----------------------------------------------------------------------------
void wave_player::play(FILE *wavefile)
{
- unsigned chunk_id,chunk_size,channel;
- unsigned data,samp_int,i;
- short unsigned dac_data;
- long long slice_value;
- char *slice_buf;
- short *data_sptr;
- unsigned char *data_bptr;
- int *data_wptr;
- FMT_STRUCT wav_format;
- long slice,num_slices;
- DAC_wptr=0;
- DAC_rptr=0;
- for (i=0;i<256;i+=2) {
- DAC_fifo[i]=0;
- DAC_fifo[i+1]=3000;
- }
- DAC_wptr=4;
- DAC_on=0;
+ unsigned chunk_id,chunk_size,channel;
+ unsigned data,samp_int,i;
+ short unsigned dac_data;
+ long long slice_value;
+ char *slice_buf;
+ short *data_sptr;
+ unsigned char *data_bptr;
+ int *data_wptr;
+ FMT_STRUCT wav_format;
+ long slice,num_slices;
+ DAC_wptr=0;
+ DAC_rptr=0;
+ for (i=0; i<256; i+=2) {
+ DAC_fifo[i]=0;
+ DAC_fifo[i+1]=3000;
+ }
+ DAC_wptr=4;
+ DAC_on=0;
- fread(&chunk_id,4,1,wavefile);
- fread(&chunk_size,4,1,wavefile);
- while (!feof(wavefile)) {
- if (verbosity)
- printf("Read chunk ID 0x%x, size 0x%x\n",chunk_id,chunk_size);
- switch (chunk_id) {
- case 0x46464952:
- fread(&data,4,1,wavefile);
- if (verbosity) {
- printf("RIFF chunk\n");
- printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size);
- printf(" RIFF type 0x%x\n",data);
- }
- break;
- case 0x20746d66:
- fread(&wav_format,sizeof(wav_format),1,wavefile);
- if (verbosity) {
- printf("FORMAT chunk\n");
- printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size);
- printf(" compression code %d\n",wav_format.comp_code);
- printf(" %d channels\n",wav_format.num_channels);
- printf(" %d samples/sec\n",wav_format.sample_rate);
- printf(" %d bytes/sec\n",wav_format.avg_Bps);
- printf(" block align %d\n",wav_format.block_align);
- printf(" %d bits per sample\n",wav_format.sig_bps);
- }
- if (chunk_size > sizeof(wav_format))
- fseek(wavefile,chunk_size-sizeof(wav_format),SEEK_CUR);
- break;
- case 0x61746164:
+ fread(&chunk_id,4,1,wavefile);
+ fread(&chunk_size,4,1,wavefile);
+ while (!feof(wavefile)) {
+ if (verbosity)
+ printf("Read chunk ID 0x%x, size 0x%x\n",chunk_id,chunk_size);
+ switch (chunk_id) {
+ case 0x46464952:
+ fread(&data,4,1,wavefile);
+ if (verbosity) {
+ printf("RIFF chunk\n");
+ printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size);
+ printf(" RIFF type 0x%x\n",data);
+ }
+ break;
+ case 0x20746d66:
+ fread(&wav_format,sizeof(wav_format),1,wavefile);
+ if (verbosity) {
+ printf("FORMAT chunk\n");
+ printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size);
+ printf(" compression code %d\n",wav_format.comp_code);
+ printf(" %d channels\n",wav_format.num_channels);
+ printf(" %d samples/sec\n",wav_format.sample_rate);
+ printf(" %d bytes/sec\n",wav_format.avg_Bps);
+ printf(" block align %d\n",wav_format.block_align);
+ printf(" %d bits per sample\n",wav_format.sig_bps);
+ }
+ if (chunk_size > sizeof(wav_format))
+ fseek(wavefile,chunk_size-sizeof(wav_format),SEEK_CUR);
+ break;
+ case 0x61746164:
// allocate a buffer big enough to hold a slice
- slice_buf=(char *)malloc(wav_format.block_align);
- if (!slice_buf) {
- printf("Unable to malloc slice buffer");
- exit(1);
- }
- num_slices=chunk_size/wav_format.block_align;
- samp_int=1000000/(wav_format.sample_rate);
- if (verbosity) {
- printf("DATA chunk\n");
- printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size);
- printf(" %d slices\n",num_slices);
- printf(" Ideal sample interval=%d\n",(unsigned)(1000000.0/wav_format.sample_rate));
- printf(" programmed interrupt tick interval=%d\n",samp_int);
- }
+ slice_buf=(char *)malloc(wav_format.block_align);
+ if (!slice_buf) {
+ printf("Unable to malloc slice buffer");
+ exit(1);
+ }
+ num_slices=chunk_size/wav_format.block_align;
+ samp_int=1000000/(wav_format.sample_rate);
+ if (verbosity) {
+ printf("DATA chunk\n");
+ printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size);
+ printf(" %d slices\n",num_slices);
+ printf(" Ideal sample interval=%d\n",(unsigned)(1000000.0/wav_format.sample_rate));
+ printf(" programmed interrupt tick interval=%d\n",samp_int);
+ }
// starting up ticker to write samples out -- no printfs until tick.detach is called
- if (verbosity)
- tick.attach_us(this,&wave_player::dac_out, 500000);
- else
- tick.attach_us(this,&wave_player::dac_out, samp_int);
- DAC_on=1;
+ if (verbosity)
+ tick.attach_us(this,&wave_player::dac_out, 500000);
+ else
+ tick.attach_us(this,&wave_player::dac_out, samp_int);
+ DAC_on=1;
// start reading slices, which contain one sample each for however many channels
// are in the wave file. one channel=mono, two channels=stereo, etc. Since
@@ -128,84 +128,87 @@
// note that from what I can find that 8 bit wave files use unsigned data,
// while 16 and 32 bit wave files use signed data
//
- for (slice=0;slice<num_slices;slice+=1) {
- fread(slice_buf,wav_format.block_align,1,wavefile);
- if (feof(wavefile)) {
- printf("Oops -- not enough slices in the wave file\n");
- exit(1);
- }
- data_sptr=(short *)slice_buf; // 16 bit samples
- data_bptr=(unsigned char *)slice_buf; // 8 bit samples
- data_wptr=(int *)slice_buf; // 32 bit samples
- slice_value=0;
- for (channel=0;channel<wav_format.num_channels;channel++) {
- switch (wav_format.sig_bps) {
- case 16:
- if (verbosity)
- printf("16 bit channel %d data=%d ",channel,data_sptr[channel]);
- slice_value+=data_sptr[channel];
- break;
- case 32:
- if (verbosity)
- printf("32 bit channel %d data=%d ",channel,data_wptr[channel]);
- slice_value+=data_wptr[channel];
- break;
- case 8:
- if (verbosity)
- printf("8 bit channel %d data=%d ",channel,(int)data_bptr[channel]);
- slice_value+=data_bptr[channel];
- break;
- }
- }
- slice_value/=wav_format.num_channels;
-
+ for (slice=0; slice<num_slices; slice+=1) {
+ fread(slice_buf,wav_format.block_align,1,wavefile);
+ if (feof(wavefile)) {
+ printf("Oops -- not enough slices in the wave file\n");
+ exit(1);
+ }
+ data_sptr=(short *)slice_buf; // 16 bit samples
+ data_bptr=(unsigned char *)slice_buf; // 8 bit samples
+ data_wptr=(int *)slice_buf; // 32 bit samples
+ slice_value=0;
+ for (channel=0; channel<wav_format.num_channels; channel++) {
+ switch (wav_format.sig_bps) {
+ case 16:
+ if (verbosity)
+ printf("16 bit channel %d data=%d ",channel,data_sptr[channel]);
+ slice_value+=data_sptr[channel];
+ break;
+ case 32:
+ if (verbosity)
+ printf("32 bit channel %d data=%d ",channel,data_wptr[channel]);
+ slice_value+=data_wptr[channel];
+ break;
+ case 8:
+ if (verbosity)
+ printf("8 bit channel %d data=%d ",channel,(int)data_bptr[channel]);
+ slice_value+=data_bptr[channel];
+ break;
+ }
+ }
+ slice_value/=wav_format.num_channels;
+
// slice_value is now averaged. Next it needs to be scaled to an unsigned 16 bit value
// with DC offset so it can be written to the DAC.
- switch (wav_format.sig_bps) {
- case 8: slice_value<<=8;
- break;
- case 16: slice_value+=32768;
- break;
- case 32: slice_value>>=16;
- slice_value+=32768;
- break;
- }
- dac_data=(short unsigned)slice_value;
- if (verbosity)
- printf("sample %d wptr %d slice_value %d dac_data %u\n",slice,DAC_wptr,(int)slice_value,dac_data);
- DAC_fifo[DAC_wptr]=dac_data;
- DAC_wptr=(DAC_wptr+1) & 0xff;
- while (DAC_wptr==DAC_rptr) {
- }
+ switch (wav_format.sig_bps) {
+ case 8:
+ slice_value<<=8;
+ break;
+ case 16:
+ slice_value+=32768;
+ break;
+ case 32:
+ slice_value>>=16;
+ slice_value+=32768;
+ break;
+ }
+ dac_data=(short unsigned)slice_value;
+ if (verbosity)
+ printf("sample %d wptr %d slice_value %d dac_data %u\n",slice,DAC_wptr,(int)slice_value,dac_data);
+ DAC_fifo[DAC_wptr]=dac_data;
+ DAC_wptr=(DAC_wptr+1) & 0xff;
+ while (DAC_wptr==DAC_rptr) {
+ }
+ }
+ DAC_on=0;
+ tick.detach();
+ free(slice_buf);
+ break;
+ case 0x5453494c:
+ if (verbosity)
+ printf("INFO chunk, size %d\n",chunk_size);
+ fseek(wavefile,chunk_size,SEEK_CUR);
+ break;
+ default:
+ printf("unknown chunk type 0x%x, size %d\n",chunk_id,chunk_size);
+ data=fseek(wavefile,chunk_size,SEEK_CUR);
+ break;
}
- DAC_on=0;
- tick.detach();
- free(slice_buf);
- break;
- case 0x5453494c:
- if (verbosity)
- printf("INFO chunk, size %d\n",chunk_size);
- fseek(wavefile,chunk_size,SEEK_CUR);
- break;
- default:
- printf("unknown chunk type 0x%x, size %d\n",chunk_id,chunk_size);
- data=fseek(wavefile,chunk_size,SEEK_CUR);
- break;
+ fread(&chunk_id,4,1,wavefile);
+ fread(&chunk_size,4,1,wavefile);
}
- fread(&chunk_id,4,1,wavefile);
- fread(&chunk_size,4,1,wavefile);
- }
}
void wave_player::dac_out()
{
- if (DAC_on) {
+ if (DAC_on) {
#ifdef VERBOSE
- printf("ISR rdptr %d got %u\n",DAC_rptr,DAC_fifo[DAC_rptr]);
+ printf("ISR rdptr %d got %u\n",DAC_rptr,DAC_fifo[DAC_rptr]);
#endif
- wave_DAC->write_u16(DAC_fifo[DAC_rptr]);
- DAC_rptr=(DAC_rptr+1) & 0xff;
- }
+ wave_DAC->write_u16(DAC_fifo[DAC_rptr]);
+ DAC_rptr=(DAC_rptr+1) & 0xff;
+ }
}