Talk Watch system using NTP timer for JBB.

Dependencies:   EthernetNetIf FatFileSystem HTTPClient_ToBeRemoved HTTPServer NTPClient_NetServices TextLCD mbed

Fork of StarBoardOrangeTest3 by Yuji Notsu

Committer:
y_notsu
Date:
Sun Jun 22 05:12:34 2014 +0000
Revision:
1:8816ea8be54b
Parent:
0:ae31fe6f181c
Talk Watch using NTP client for JBB

Who changed what in which revision?

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