My own wave player but better

Files at this revision

API Documentation at this revision

Comitter:
javenho14
Date:
Wed Apr 27 01:34:00 2022 +0000
Parent:
1:acc3e18e77ad
Commit message:
Add volume control

Changed in this revision

wave_player.cpp Show annotated file Show diff for this revision Revisions of this file
wave_player.h Show annotated file Show diff for this revision Revisions of this file
--- a/wave_player.cpp	Tue Jan 18 03:57:27 2011 +0000
+++ b/wave_player.cpp	Wed Apr 27 01:34:00 2022 +0000
@@ -23,6 +23,7 @@
   wave_DAC=_dac;
   wave_DAC->write_u16(32768);        //DAC is 0-3.3V, so idles at ~1.6V
   verbosity=0;
+  volume = 0.5;
 }
 
 //-----------------------------------------------------------------------------
@@ -44,7 +45,7 @@
 // SDcard filesystem can be hotrodded by increasing the SPI frequency it uses
 // internally.
 //-----------------------------------------------------------------------------
-void wave_player::play(FILE *wavefile)
+void wave_player::play(FILE *wavefile, float _volume)
 {
         unsigned chunk_id,chunk_size,channel;
         unsigned data,samp_int,i;
@@ -56,6 +57,7 @@
         int *data_wptr;
         FMT_STRUCT wav_format;
         long slice,num_slices;
+  volume = _volume;  
   DAC_wptr=0;
   DAC_rptr=0;
   for (i=0;i<256;i+=2) {
@@ -204,7 +206,7 @@
 #ifdef VERBOSE
   printf("ISR rdptr %d got %u\n",DAC_rptr,DAC_fifo[DAC_rptr]);
 #endif
-    wave_DAC->write_u16(DAC_fifo[DAC_rptr]);
+    wave_DAC->write_u16(DAC_fifo[DAC_rptr]*volume);
     DAC_rptr=(DAC_rptr+1) & 0xff;
   }
 }
--- a/wave_player.h	Tue Jan 18 03:57:27 2011 +0000
+++ b/wave_player.h	Wed Apr 27 01:34:00 2022 +0000
@@ -43,7 +43,7 @@
  *
  * @param wavefile  A pointer to an opened wave file
  */
-void play(FILE *wavefile);
+void play(FILE *wavefile, float _volume);
 
 /** Set the printf verbosity of the wave player.  A nonzero verbosity level
  * will put wave_player in a mode where the complete contents of the wave
@@ -67,6 +67,7 @@
 short DAC_wptr;
 volatile short DAC_rptr;
 short DAC_on;
+float volume;
 };