Test of Embedded Artists LPCXpresso baseboard ethernet, SD card, audio and OLED display facilities. The program displays the day, date and time on the baseboard OLED and sounds the Big Ben chimes on the hour and quarter hour. On initial startup the program checks that the mbed clock is set and that the chime wav files can be accessed on the SD card. If not it asks to be connected to the internet to obtain the current time and to download the wav files to the SD card.

Dependencies:   EthernetNetIf NTPClient_NetServices mbed EAOLED

Committer:
tom_coxon
Date:
Sat Aug 14 10:33:13 2010 +0000
Revision:
0:f61e8db0570d

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tom_coxon 0:f61e8db0570d 1 /*
tom_coxon 0:f61e8db0570d 2 Library wave file player by Tom Coxon
tom_coxon 0:f61e8db0570d 3
tom_coxon 0:f61e8db0570d 4 Based on WAVEplayer by Vlad Cazan/Stephan Rochon modified by Tom Coxon to:
tom_coxon 0:f61e8db0570d 5
tom_coxon 0:f61e8db0570d 6 1. Run correctly on the Embedded Artists LPCXpresso baseboard.
tom_coxon 0:f61e8db0570d 7 2. To play 8 bit sample size in addition to original 16 bit
tom_coxon 0:f61e8db0570d 8 3. To be more fault tolerant when playing wav files.
tom_coxon 0:f61e8db0570d 9 */
tom_coxon 0:f61e8db0570d 10
tom_coxon 0:f61e8db0570d 11 #include "wavplayer.h"
tom_coxon 0:f61e8db0570d 12
tom_coxon 0:f61e8db0570d 13 Ticker tick;
tom_coxon 0:f61e8db0570d 14
tom_coxon 0:f61e8db0570d 15 AnalogOut DACout(p18);
tom_coxon 0:f61e8db0570d 16
tom_coxon 0:f61e8db0570d 17 void WavPlayer::dac_out() {
tom_coxon 0:f61e8db0570d 18 if (DAC_on) {
tom_coxon 0:f61e8db0570d 19 DACout.write_u16(DAC_fifo[DAC_rptr]);
tom_coxon 0:f61e8db0570d 20 DAC_rptr=(DAC_rptr+1) & 0xff;
tom_coxon 0:f61e8db0570d 21 }
tom_coxon 0:f61e8db0570d 22 }
tom_coxon 0:f61e8db0570d 23
tom_coxon 0:f61e8db0570d 24 //void play_wave(char *wavname) {
tom_coxon 0:f61e8db0570d 25 void WavPlayer::play_wave(char *wavname) {
tom_coxon 0:f61e8db0570d 26 unsigned chunk_id,chunk_size,channel;
tom_coxon 0:f61e8db0570d 27 unsigned data,samp_int,i;
tom_coxon 0:f61e8db0570d 28 short dac_data;
tom_coxon 0:f61e8db0570d 29 char *slice_buf;
tom_coxon 0:f61e8db0570d 30 short *data_sptr;
tom_coxon 0:f61e8db0570d 31 FMT_STRUCT wav_format;
tom_coxon 0:f61e8db0570d 32 FILE *wavfile;
tom_coxon 0:f61e8db0570d 33 long slice,num_slices;
tom_coxon 0:f61e8db0570d 34 DAC_wptr=0;
tom_coxon 0:f61e8db0570d 35 DAC_rptr=0;
tom_coxon 0:f61e8db0570d 36
tom_coxon 0:f61e8db0570d 37 size_t result;
tom_coxon 0:f61e8db0570d 38
tom_coxon 0:f61e8db0570d 39 for (i=0;i<256;i+=2) {
tom_coxon 0:f61e8db0570d 40 DAC_fifo[i]=0;
tom_coxon 0:f61e8db0570d 41 DAC_fifo[i+1]=3000;
tom_coxon 0:f61e8db0570d 42 }
tom_coxon 0:f61e8db0570d 43 DAC_wptr=4;
tom_coxon 0:f61e8db0570d 44 DAC_on=0;
tom_coxon 0:f61e8db0570d 45
tom_coxon 0:f61e8db0570d 46 printf("Playing wave file '%s'\r\n",wavname);
tom_coxon 0:f61e8db0570d 47
tom_coxon 0:f61e8db0570d 48 wavfile=fopen(wavname,"rb");
tom_coxon 0:f61e8db0570d 49 if (!wavfile) {
tom_coxon 0:f61e8db0570d 50 printf("Unable to open wav file '%s'\r\n",wavname);
tom_coxon 0:f61e8db0570d 51 exit(1);
tom_coxon 0:f61e8db0570d 52 }
tom_coxon 0:f61e8db0570d 53
tom_coxon 0:f61e8db0570d 54 fread(&chunk_id,4,1,wavfile);
tom_coxon 0:f61e8db0570d 55 fread(&chunk_size,4,1,wavfile);
tom_coxon 0:f61e8db0570d 56 while (!feof(wavfile)) {
tom_coxon 0:f61e8db0570d 57 printf("Read chunk ID 0x%x, size 0x%x\r\n",chunk_id,chunk_size);
tom_coxon 0:f61e8db0570d 58 switch (chunk_id) {
tom_coxon 0:f61e8db0570d 59 case 0x46464952:
tom_coxon 0:f61e8db0570d 60 fread(&data,4,1,wavfile);
tom_coxon 0:f61e8db0570d 61 printf("RIFF chunk\r\n");
tom_coxon 0:f61e8db0570d 62 printf(" chunk size %d (0x%x)\r\n",chunk_size,chunk_size);
tom_coxon 0:f61e8db0570d 63 printf(" RIFF type 0x%x\r\n",data);
tom_coxon 0:f61e8db0570d 64 break;
tom_coxon 0:f61e8db0570d 65 case 0x20746d66:
tom_coxon 0:f61e8db0570d 66 fread(&wav_format,sizeof(wav_format),1,wavfile);
tom_coxon 0:f61e8db0570d 67 printf("FORMAT chunk\r\n");
tom_coxon 0:f61e8db0570d 68 printf(" chunk size %d (0x%x)\r\n",chunk_size,chunk_size);
tom_coxon 0:f61e8db0570d 69 printf(" compression code %d\r\n",wav_format.comp_code);
tom_coxon 0:f61e8db0570d 70 printf(" %d channels\r\n",wav_format.num_channels);
tom_coxon 0:f61e8db0570d 71 printf(" %d samples/sec\r\n",wav_format.sample_rate);
tom_coxon 0:f61e8db0570d 72 printf(" %d bytes/sec\r\n",wav_format.avg_Bps);
tom_coxon 0:f61e8db0570d 73 printf(" block align %d\r\n",wav_format.block_align);
tom_coxon 0:f61e8db0570d 74 printf(" %d bits per sample\r\n",wav_format.sig_bps);
tom_coxon 0:f61e8db0570d 75 if (chunk_size > sizeof(wav_format))
tom_coxon 0:f61e8db0570d 76 fseek(wavfile,chunk_size-sizeof(wav_format),SEEK_CUR);
tom_coxon 0:f61e8db0570d 77 // create a slice buffer large enough to hold multiple slices
tom_coxon 0:f61e8db0570d 78 slice_buf=(char *)malloc(wav_format.block_align*SLICE_BUF_SIZE);
tom_coxon 0:f61e8db0570d 79 if (!slice_buf) {
tom_coxon 0:f61e8db0570d 80 printf("Unable to malloc slice buffer");
tom_coxon 0:f61e8db0570d 81 exit(1);
tom_coxon 0:f61e8db0570d 82 }
tom_coxon 0:f61e8db0570d 83 break;
tom_coxon 0:f61e8db0570d 84 case 0x61746164:
tom_coxon 0:f61e8db0570d 85 slice_buf=(char *)malloc(wav_format.block_align*SLICE_BUF_SIZE);
tom_coxon 0:f61e8db0570d 86 if (!slice_buf) {
tom_coxon 0:f61e8db0570d 87 printf("Unable to malloc slice buffer");
tom_coxon 0:f61e8db0570d 88 exit(1);
tom_coxon 0:f61e8db0570d 89 }
tom_coxon 0:f61e8db0570d 90 num_slices=chunk_size/wav_format.block_align;
tom_coxon 0:f61e8db0570d 91 printf("DATA chunk\r\n");
tom_coxon 0:f61e8db0570d 92 printf(" chunk size %d (0x%x)\r\n",chunk_size,chunk_size);
tom_coxon 0:f61e8db0570d 93 printf(" %d slices\r\n",num_slices);
tom_coxon 0:f61e8db0570d 94 printf(" Ideal sample interval=%d\r\n",(unsigned)(1000000.0/wav_format.sample_rate));
tom_coxon 0:f61e8db0570d 95 samp_int=1000000/(wav_format.sample_rate);
tom_coxon 0:f61e8db0570d 96 printf(" programmed interrupt tick interval=%d\r\n",samp_int);
tom_coxon 0:f61e8db0570d 97
tom_coxon 0:f61e8db0570d 98 // starting up ticker to write samples out -- no printfs until tick.detach is called
tom_coxon 0:f61e8db0570d 99 tick.attach_us(this,&WavPlayer::dac_out, samp_int);
tom_coxon 0:f61e8db0570d 100 DAC_on=1;
tom_coxon 0:f61e8db0570d 101 for (slice=0;slice<num_slices;slice+=SLICE_BUF_SIZE) {
tom_coxon 0:f61e8db0570d 102
tom_coxon 0:f61e8db0570d 103 result = fread(slice_buf,wav_format.block_align*SLICE_BUF_SIZE,1,wavfile);
tom_coxon 0:f61e8db0570d 104 if (feof(wavfile)) {
tom_coxon 0:f61e8db0570d 105 printf("Oops -- not enough slices in the wave file\r\n");
tom_coxon 0:f61e8db0570d 106 if (result) ;//just to stop compiler complaining that we have not used result
tom_coxon 0:f61e8db0570d 107 break;
tom_coxon 0:f61e8db0570d 108 }
tom_coxon 0:f61e8db0570d 109
tom_coxon 0:f61e8db0570d 110 data_sptr=(short *)slice_buf;
tom_coxon 0:f61e8db0570d 111 for (i=0;i<SLICE_BUF_SIZE;i++) {
tom_coxon 0:f61e8db0570d 112 dac_data=0;
tom_coxon 0:f61e8db0570d 113
tom_coxon 0:f61e8db0570d 114 // for a stereo wave file average the two channels.
tom_coxon 0:f61e8db0570d 115 for (channel=0;channel<wav_format.num_channels;channel++) {
tom_coxon 0:f61e8db0570d 116 switch (wav_format.sig_bps) {
tom_coxon 0:f61e8db0570d 117 case 16:
tom_coxon 0:f61e8db0570d 118 dac_data+=( ((int)(*data_sptr++)) +32768 );
tom_coxon 0:f61e8db0570d 119 break;
tom_coxon 0:f61e8db0570d 120 case 8:
tom_coxon 0:f61e8db0570d 121 dac_data+=( ((int)(*data_sptr++)) +32768 <<8);
tom_coxon 0:f61e8db0570d 122 break;
tom_coxon 0:f61e8db0570d 123 }
tom_coxon 0:f61e8db0570d 124 }
tom_coxon 0:f61e8db0570d 125 DAC_fifo[DAC_wptr]=dac_data;
tom_coxon 0:f61e8db0570d 126 DAC_wptr=(DAC_wptr+1) & 0xff;
tom_coxon 0:f61e8db0570d 127 while (DAC_wptr==DAC_rptr) {
tom_coxon 0:f61e8db0570d 128 wait_us(10);
tom_coxon 0:f61e8db0570d 129 }
tom_coxon 0:f61e8db0570d 130 }
tom_coxon 0:f61e8db0570d 131 }
tom_coxon 0:f61e8db0570d 132 DAC_on=0;
tom_coxon 0:f61e8db0570d 133 tick.detach();
tom_coxon 0:f61e8db0570d 134 printf("Ticker detached\r\n");
tom_coxon 0:f61e8db0570d 135 free(slice_buf);
tom_coxon 0:f61e8db0570d 136 break;
tom_coxon 0:f61e8db0570d 137 case 0x5453494c:
tom_coxon 0:f61e8db0570d 138 printf("INFO chunk, size %d\r\n",chunk_size);
tom_coxon 0:f61e8db0570d 139 fseek(wavfile,chunk_size,SEEK_CUR);
tom_coxon 0:f61e8db0570d 140 break;
tom_coxon 0:f61e8db0570d 141 default:
tom_coxon 0:f61e8db0570d 142 printf("unknown chunk type 0x%x, size %d\r\n",chunk_id,chunk_size);
tom_coxon 0:f61e8db0570d 143 data=fseek(wavfile,chunk_size,SEEK_CUR);
tom_coxon 0:f61e8db0570d 144 break;
tom_coxon 0:f61e8db0570d 145 }
tom_coxon 0:f61e8db0570d 146 fread(&chunk_id,4,1,wavfile);
tom_coxon 0:f61e8db0570d 147 fread(&chunk_size,4,1,wavfile);
tom_coxon 0:f61e8db0570d 148 }
tom_coxon 0:f61e8db0570d 149 printf("++++++++++++ Done with wave file ++++++++++\r\n");
tom_coxon 0:f61e8db0570d 150 fclose(wavfile);
tom_coxon 0:f61e8db0570d 151 }
tom_coxon 0:f61e8db0570d 152