mX mbed BaseBoard audio

Dependencies:   mbed

Committer:
ashwin_athani
Date:
Wed Dec 08 06:21:06 2010 +0000
Revision:
0:6c621d41bf07

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ashwin_athani 0:6c621d41bf07 1 #include "mbed.h"
ashwin_athani 0:6c621d41bf07 2 #include "SDFileSystem.h"
ashwin_athani 0:6c621d41bf07 3
ashwin_athani 0:6c621d41bf07 4 AnalogOut DACout(p18);
ashwin_athani 0:6c621d41bf07 5 DigitalOut led1(LED1);
ashwin_athani 0:6c621d41bf07 6 DigitalOut led2(LED2);
ashwin_athani 0:6c621d41bf07 7 DigitalOut led3(LED3);
ashwin_athani 0:6c621d41bf07 8 DigitalOut led4(p8);
ashwin_athani 0:6c621d41bf07 9 DigitalOut digout(LED4);
ashwin_athani 0:6c621d41bf07 10 Ticker tick;
ashwin_athani 0:6c621d41bf07 11 //SDFileSystem sd(p11, p12, p13, p14, "sd");
ashwin_athani 0:6c621d41bf07 12 SDFileSystem sd(p5, p6, p7, p8, "sd"); // NGX mbed base board
ashwin_athani 0:6c621d41bf07 13
ashwin_athani 0:6c621d41bf07 14 #define SAMPLE_FREQ 40000
ashwin_athani 0:6c621d41bf07 15 #define BUF_SIZE (SAMPLE_FREQ/10)
ashwin_athani 0:6c621d41bf07 16 #define SLICE_BUF_SIZE 1
ashwin_athani 0:6c621d41bf07 17
ashwin_athani 0:6c621d41bf07 18 void dac_out(void);
ashwin_athani 0:6c621d41bf07 19 void play_wave(char *);
ashwin_athani 0:6c621d41bf07 20 void cleanup(char *);
ashwin_athani 0:6c621d41bf07 21 void fill_adc_buf(short *, unsigned);
ashwin_athani 0:6c621d41bf07 22 void swapword(unsigned *);
ashwin_athani 0:6c621d41bf07 23
ashwin_athani 0:6c621d41bf07 24 // a FIFO for the DAC
ashwin_athani 0:6c621d41bf07 25 short DAC_fifo[256];
ashwin_athani 0:6c621d41bf07 26 short DAC_wptr;
ashwin_athani 0:6c621d41bf07 27 short DAC_rptr;
ashwin_athani 0:6c621d41bf07 28 short DAC_on;
ashwin_athani 0:6c621d41bf07 29
ashwin_athani 0:6c621d41bf07 30 typedef struct uFMT_STRUCT {
ashwin_athani 0:6c621d41bf07 31 short comp_code;
ashwin_athani 0:6c621d41bf07 32 short num_channels;
ashwin_athani 0:6c621d41bf07 33 unsigned sample_rate;
ashwin_athani 0:6c621d41bf07 34 unsigned avg_Bps;
ashwin_athani 0:6c621d41bf07 35 short block_align;
ashwin_athani 0:6c621d41bf07 36 short sig_bps;
ashwin_athani 0:6c621d41bf07 37 } FMT_STRUCT;
ashwin_athani 0:6c621d41bf07 38
ashwin_athani 0:6c621d41bf07 39
ashwin_athani 0:6c621d41bf07 40 int main() {
ashwin_athani 0:6c621d41bf07 41 led1=0; wait(.5); led1=1; wait(.5); led1=0;
ashwin_athani 0:6c621d41bf07 42 printf("Hello, world!\n");
ashwin_athani 0:6c621d41bf07 43 play_wave("/sd/forest.wav");
ashwin_athani 0:6c621d41bf07 44 printf("Back from forest\n");
ashwin_athani 0:6c621d41bf07 45 printf("Goodbye, world!\n");
ashwin_athani 0:6c621d41bf07 46 led1=1; wait(.5); led1=0; wait(.5); led1=1;
ashwin_athani 0:6c621d41bf07 47 }
ashwin_athani 0:6c621d41bf07 48
ashwin_athani 0:6c621d41bf07 49 void play_wave(char *wavname)
ashwin_athani 0:6c621d41bf07 50 {
ashwin_athani 0:6c621d41bf07 51 unsigned chunk_id,chunk_size,channel;
ashwin_athani 0:6c621d41bf07 52 // unsigned *data_wptr,data,samp_int,i;
ashwin_athani 0:6c621d41bf07 53 unsigned data,samp_int,i;
ashwin_athani 0:6c621d41bf07 54 short dac_data;
ashwin_athani 0:6c621d41bf07 55 char *slice_buf;
ashwin_athani 0:6c621d41bf07 56 short *data_sptr;
ashwin_athani 0:6c621d41bf07 57 // char *data_bptr;
ashwin_athani 0:6c621d41bf07 58 FMT_STRUCT wav_format;
ashwin_athani 0:6c621d41bf07 59 FILE *wavfile;
ashwin_athani 0:6c621d41bf07 60 long slice,num_slices;
ashwin_athani 0:6c621d41bf07 61 DAC_wptr=0;
ashwin_athani 0:6c621d41bf07 62 DAC_rptr=0;
ashwin_athani 0:6c621d41bf07 63 for (i=0;i<256;i+=2) {
ashwin_athani 0:6c621d41bf07 64 DAC_fifo[i]=0;
ashwin_athani 0:6c621d41bf07 65 DAC_fifo[i+1]=3000;
ashwin_athani 0:6c621d41bf07 66 }
ashwin_athani 0:6c621d41bf07 67 DAC_wptr=4;
ashwin_athani 0:6c621d41bf07 68 DAC_on=0;
ashwin_athani 0:6c621d41bf07 69
ashwin_athani 0:6c621d41bf07 70 led1=led2=led3=led4=0;
ashwin_athani 0:6c621d41bf07 71
ashwin_athani 0:6c621d41bf07 72 printf("Playing wave file '%s'\n",wavname);
ashwin_athani 0:6c621d41bf07 73
ashwin_athani 0:6c621d41bf07 74 wavfile=fopen(wavname,"rb");
ashwin_athani 0:6c621d41bf07 75 if (!wavfile) {
ashwin_athani 0:6c621d41bf07 76 printf("Unable to open wav file '%s'\n",wavname);
ashwin_athani 0:6c621d41bf07 77 exit(1);
ashwin_athani 0:6c621d41bf07 78 }
ashwin_athani 0:6c621d41bf07 79
ashwin_athani 0:6c621d41bf07 80 fread(&chunk_id,4,1,wavfile);
ashwin_athani 0:6c621d41bf07 81 fread(&chunk_size,4,1,wavfile);
ashwin_athani 0:6c621d41bf07 82 while (!feof(wavfile)) {
ashwin_athani 0:6c621d41bf07 83 printf("Read chunk ID 0x%x, size 0x%x\n",chunk_id,chunk_size);
ashwin_athani 0:6c621d41bf07 84 switch (chunk_id) {
ashwin_athani 0:6c621d41bf07 85 case 0x46464952:
ashwin_athani 0:6c621d41bf07 86 fread(&data,4,1,wavfile);
ashwin_athani 0:6c621d41bf07 87 printf("RIFF chunk\n");
ashwin_athani 0:6c621d41bf07 88 printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size);
ashwin_athani 0:6c621d41bf07 89 printf(" RIFF type 0x%x\n",data);
ashwin_athani 0:6c621d41bf07 90 break;
ashwin_athani 0:6c621d41bf07 91 case 0x20746d66:
ashwin_athani 0:6c621d41bf07 92 fread(&wav_format,sizeof(wav_format),1,wavfile);
ashwin_athani 0:6c621d41bf07 93 printf("FORMAT chunk\n");
ashwin_athani 0:6c621d41bf07 94 printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size);
ashwin_athani 0:6c621d41bf07 95 printf(" compression code %d\n",wav_format.comp_code);
ashwin_athani 0:6c621d41bf07 96 printf(" %d channels\n",wav_format.num_channels);
ashwin_athani 0:6c621d41bf07 97 printf(" %d samples/sec\n",wav_format.sample_rate);
ashwin_athani 0:6c621d41bf07 98 printf(" %d bytes/sec\n",wav_format.avg_Bps);
ashwin_athani 0:6c621d41bf07 99 printf(" block align %d\n",wav_format.block_align);
ashwin_athani 0:6c621d41bf07 100 printf(" %d bits per sample\n",wav_format.sig_bps);
ashwin_athani 0:6c621d41bf07 101 if (chunk_size > sizeof(wav_format))
ashwin_athani 0:6c621d41bf07 102 fseek(wavfile,chunk_size-sizeof(wav_format),SEEK_CUR);
ashwin_athani 0:6c621d41bf07 103 // create a slice buffer large enough to hold multiple slices
ashwin_athani 0:6c621d41bf07 104 slice_buf=(char *)malloc(wav_format.block_align*SLICE_BUF_SIZE);
ashwin_athani 0:6c621d41bf07 105 if (!slice_buf) {
ashwin_athani 0:6c621d41bf07 106 printf("Unable to malloc slice buffer");
ashwin_athani 0:6c621d41bf07 107 exit(1);
ashwin_athani 0:6c621d41bf07 108 }
ashwin_athani 0:6c621d41bf07 109 break;
ashwin_athani 0:6c621d41bf07 110 case 0x61746164:
ashwin_athani 0:6c621d41bf07 111 slice_buf=(char *)malloc(wav_format.block_align*SLICE_BUF_SIZE);
ashwin_athani 0:6c621d41bf07 112 if (!slice_buf) {
ashwin_athani 0:6c621d41bf07 113 printf("Unable to malloc slice buffer");
ashwin_athani 0:6c621d41bf07 114 exit(1);
ashwin_athani 0:6c621d41bf07 115 } num_slices=chunk_size/wav_format.block_align;
ashwin_athani 0:6c621d41bf07 116 printf("DATA chunk\n");
ashwin_athani 0:6c621d41bf07 117 printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size);
ashwin_athani 0:6c621d41bf07 118 printf(" %d slices\n",num_slices);
ashwin_athani 0:6c621d41bf07 119 printf(" Ideal sample interval=%d\n",(unsigned)(1000000.0/wav_format.sample_rate));
ashwin_athani 0:6c621d41bf07 120 samp_int=1000000/(wav_format.sample_rate);
ashwin_athani 0:6c621d41bf07 121 printf(" programmed interrupt tick interval=%d\n",samp_int);
ashwin_athani 0:6c621d41bf07 122
ashwin_athani 0:6c621d41bf07 123 // starting up ticker to write samples out -- no printfs until tick.detach is called
ashwin_athani 0:6c621d41bf07 124 tick.attach_us(&dac_out, samp_int);
ashwin_athani 0:6c621d41bf07 125 DAC_on=1;
ashwin_athani 0:6c621d41bf07 126 led2=1;
ashwin_athani 0:6c621d41bf07 127 for (slice=0;slice<num_slices;slice+=SLICE_BUF_SIZE) {
ashwin_athani 0:6c621d41bf07 128 fread(slice_buf,wav_format.block_align*SLICE_BUF_SIZE,1,wavfile);
ashwin_athani 0:6c621d41bf07 129 if (feof(wavfile)) {
ashwin_athani 0:6c621d41bf07 130 printf("Oops -- not enough slices in the wave file\n");
ashwin_athani 0:6c621d41bf07 131 exit(1);
ashwin_athani 0:6c621d41bf07 132 }
ashwin_athani 0:6c621d41bf07 133 data_sptr=(short *)slice_buf;
ashwin_athani 0:6c621d41bf07 134 for (i=0;i<SLICE_BUF_SIZE;i++) {
ashwin_athani 0:6c621d41bf07 135 dac_data=0;
ashwin_athani 0:6c621d41bf07 136
ashwin_athani 0:6c621d41bf07 137 // for a stereo wave file average the two channels.
ashwin_athani 0:6c621d41bf07 138 for (channel=0;channel<wav_format.num_channels;channel++) {
ashwin_athani 0:6c621d41bf07 139 switch (wav_format.sig_bps) {
ashwin_athani 0:6c621d41bf07 140 case 16:
ashwin_athani 0:6c621d41bf07 141 dac_data+=( ((int)(*data_sptr++)) +32768)>>5;
ashwin_athani 0:6c621d41bf07 142 break;
ashwin_athani 0:6c621d41bf07 143 }
ashwin_athani 0:6c621d41bf07 144 }
ashwin_athani 0:6c621d41bf07 145 dac_data>>=1;
ashwin_athani 0:6c621d41bf07 146 DAC_fifo[DAC_wptr]=dac_data;
ashwin_athani 0:6c621d41bf07 147 DAC_wptr=(DAC_wptr+1) & 0xff;
ashwin_athani 0:6c621d41bf07 148 while (DAC_wptr==DAC_rptr) {
ashwin_athani 0:6c621d41bf07 149 led1=1;
ashwin_athani 0:6c621d41bf07 150 }
ashwin_athani 0:6c621d41bf07 151 led1=0;
ashwin_athani 0:6c621d41bf07 152 }
ashwin_athani 0:6c621d41bf07 153 }
ashwin_athani 0:6c621d41bf07 154 DAC_on=0;
ashwin_athani 0:6c621d41bf07 155 led2=0;
ashwin_athani 0:6c621d41bf07 156 tick.detach();
ashwin_athani 0:6c621d41bf07 157 printf("Ticker detached\n");
ashwin_athani 0:6c621d41bf07 158 led3=1;
ashwin_athani 0:6c621d41bf07 159 free(slice_buf);
ashwin_athani 0:6c621d41bf07 160 break;
ashwin_athani 0:6c621d41bf07 161 case 0x5453494c:
ashwin_athani 0:6c621d41bf07 162 printf("INFO chunk, size %d\n",chunk_size);
ashwin_athani 0:6c621d41bf07 163 fseek(wavfile,chunk_size,SEEK_CUR);
ashwin_athani 0:6c621d41bf07 164 break;
ashwin_athani 0:6c621d41bf07 165 default:
ashwin_athani 0:6c621d41bf07 166 printf("unknown chunk type 0x%x, size %d\n",chunk_id,chunk_size);
ashwin_athani 0:6c621d41bf07 167 data=fseek(wavfile,chunk_size,SEEK_CUR);
ashwin_athani 0:6c621d41bf07 168 break;
ashwin_athani 0:6c621d41bf07 169 }
ashwin_athani 0:6c621d41bf07 170 fread(&chunk_id,4,1,wavfile);
ashwin_athani 0:6c621d41bf07 171 fread(&chunk_size,4,1,wavfile);
ashwin_athani 0:6c621d41bf07 172 }
ashwin_athani 0:6c621d41bf07 173 printf("Done with wave file\n");
ashwin_athani 0:6c621d41bf07 174 fclose(wavfile);
ashwin_athani 0:6c621d41bf07 175 led1=0;
ashwin_athani 0:6c621d41bf07 176 }
ashwin_athani 0:6c621d41bf07 177
ashwin_athani 0:6c621d41bf07 178
ashwin_athani 0:6c621d41bf07 179 void dac_out()
ashwin_athani 0:6c621d41bf07 180 {
ashwin_athani 0:6c621d41bf07 181 if (DAC_on) {
ashwin_athani 0:6c621d41bf07 182 digout=1;
ashwin_athani 0:6c621d41bf07 183 DACout.write_u16(DAC_fifo[DAC_rptr]);
ashwin_athani 0:6c621d41bf07 184 DAC_rptr=(DAC_rptr+1) & 0xff;
ashwin_athani 0:6c621d41bf07 185 digout=0;
ashwin_athani 0:6c621d41bf07 186 }
ashwin_athani 0:6c621d41bf07 187 }
ashwin_athani 0:6c621d41bf07 188
ashwin_athani 0:6c621d41bf07 189