Wave playing code, based on Big Mouth Billy Bass, but cleaned up and capable of playing more bitrates and sample sizes, and VOLUME CONTROL

Dependents:   MOVIE_MUSIC_TRIVIA_GAME

Fork of wave_player by Steve Ravet

Revision:
2:8d5ce55ed5de
Parent:
0:62c18ade9a60
--- a/wave_player.cpp	Tue Jan 18 03:57:27 2011 +0000
+++ b/wave_player.cpp	Wed Mar 11 20:11:54 2015 +0000
@@ -14,8 +14,9 @@
 #include <mbed.h>
 #include <stdio.h>
 #include <wave_player.h>
+#include <stdlib.h>
 
-
+extern int vol;  
 //-----------------------------------------------------------------------------
 // constructor -- accepts an mbed pin to use for AnalogOut.  Only p18 will work
 wave_player::wave_player(AnalogOut *_dac)
@@ -56,6 +57,11 @@
         int *data_wptr;
         FMT_STRUCT wav_format;
         long slice,num_slices;
+        extern bool playing;
+        extern int vol;
+        extern bool old_playing;
+        
+    
   DAC_wptr=0;
   DAC_rptr=0;
   for (i=0;i<256;i+=2) {
@@ -68,6 +74,7 @@
   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) {
@@ -129,11 +136,17 @@
 // while 16 and 32 bit wave files use signed data
 //
         for (slice=0;slice<num_slices;slice+=1) {
+            if (playing == false){
+         
+          break;
+        } 
           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
@@ -173,7 +186,9 @@
           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_fifo[DAC_wptr]=(((128-vol)* dac_data)>>7) ; 
+         DAC_fifo[DAC_wptr]=(((vol)* dac_data)>>7) ;
+         
           DAC_wptr=(DAC_wptr+1) & 0xff;
           while (DAC_wptr==DAC_rptr) {
           }
@@ -205,7 +220,6 @@
   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;
+    DAC_rptr=(DAC_rptr+1) & 0xff; 
   }
-}
-
+ }