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

Dependents:   waveplayer_mbedOS_v5_helloworld

Revision:
2:106018dd7db9
Parent:
0:62c18ade9a60
--- a/wave_player.cpp	Tue Jan 18 03:57:27 2011 +0000
+++ b/wave_player.cpp	Tue Oct 01 00:41:24 2019 +0000
@@ -18,10 +18,12 @@
 
 //-----------------------------------------------------------------------------
 // constructor -- accepts an mbed pin to use for AnalogOut.  Only p18 will work
-wave_player::wave_player(AnalogOut *_dac)
+wave_player::wave_player(AnalogOut *_dac, PwmOut *_pwm)
 {
   wave_DAC=_dac;
   wave_DAC->write_u16(32768);        //DAC is 0-3.3V, so idles at ~1.6V
+  wave_PWM=_pwm;
+  wave_PWM->write(0.0f);
   verbosity=0;
 }
 
@@ -69,27 +71,27 @@
   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);
+      printf("Read chunk ID 0x%x, size 0x%x\r\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);
+          printf("RIFF chunk\r\n");
+          printf("  chunk size %d (0x%x)\r\n",chunk_size,chunk_size);
+          printf("  RIFF type 0x%x\r\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);
+          printf("FORMAT chunk\r\n");
+          printf("  chunk size %d (0x%x)\r\n",chunk_size,chunk_size);
+          printf("  compression code %d\r\n",wav_format.comp_code);
+          printf("  %d channels\r\n",wav_format.num_channels);
+          printf("  %d samples/sec\r\n",wav_format.sample_rate);
+          printf("  %d bytes/sec\r\n",wav_format.avg_Bps);
+          printf("  block align %d\r\n",wav_format.block_align);
+          printf("  %d bits per sample\r\n",wav_format.sig_bps);
         }
         if (chunk_size > sizeof(wav_format))
           fseek(wavefile,chunk_size-sizeof(wav_format),SEEK_CUR);
@@ -98,24 +100,24 @@
 // 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");
+          printf("Unable to malloc slice buffer\r\n");
           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);
+          printf("DATA chunk\r\n");
+          printf("  chunk size %d (0x%x)\r\n",chunk_size,chunk_size);
+          printf("  %d slices\r\n",num_slices);
+          printf("  Ideal sample interval=%d\r\n",(unsigned)(1000000.0/wav_format.sample_rate));
+          printf("  programmed interrupt tick interval=%d\r\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); 
+          tick.attach_us(callback(this, &wave_player::dac_out), 500000); 
         else
-          tick.attach_us(this,&wave_player::dac_out, samp_int); 
+          tick.attach_us(callback(this, &wave_player::dac_out), samp_int);
         DAC_on=1; 
 
 // start reading slices, which contain one sample each for however many channels
@@ -131,7 +133,7 @@
         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");
+            printf("Oops -- not enough slices in the wave file\r\n");
             exit(1);
           }
           data_sptr=(short *)slice_buf;     // 16 bit samples
@@ -142,17 +144,17 @@
             switch (wav_format.sig_bps) {
               case 16:
                 if (verbosity)
-                  printf("16 bit channel %d data=%d ",channel,data_sptr[channel]);
+                  printf("16 bit channel %d data=%d \r\n",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]);
+                  printf("32 bit channel %d data=%d \r\n",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]);
+                  printf("8 bit channel %d data=%d \r\n",channel,(int)data_bptr[channel]);
                 slice_value+=data_bptr[channel];
                 break;
             }
@@ -172,7 +174,7 @@
           }
           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);
+            printf("sample %d wptr %d slice_value %d dac_data %u\r\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) {
@@ -184,11 +186,11 @@
         break;
       case 0x5453494c:
         if (verbosity)
-          printf("INFO chunk, size %d\n",chunk_size);
+          printf("INFO chunk, size %d\r\n",chunk_size);
         fseek(wavefile,chunk_size,SEEK_CUR);
         break;
       default:
-        printf("unknown chunk type 0x%x, size %d\n",chunk_id,chunk_size);
+        printf("unknown chunk type 0x%x, size %d\r\n",chunk_id,chunk_size);
         data=fseek(wavefile,chunk_size,SEEK_CUR);
         break;
     }
@@ -201,11 +203,8 @@
 void wave_player::dac_out()
 {
   if (DAC_on) {
-#ifdef VERBOSE
-  printf("ISR rdptr %d got %u\n",DAC_rptr,DAC_fifo[DAC_rptr]);
-#endif
-    wave_DAC->write_u16(DAC_fifo[DAC_rptr]);
+    LPC_DAC->DACR = DAC_fifo[DAC_rptr] & 0XFFC0;
+    wave_PWM->write(float(DAC_fifo[DAC_rptr]/65536.0));
     DAC_rptr=(DAC_rptr+1) & 0xff;
-  }
-}
-
+  } 
+}
\ No newline at end of file