change tone sounds by altering the waveforms

Dependencies:   mbed

Fork of 5_songs by MakingMusicWorkshop

Revision:
8:77227ecac915
Parent:
7:4730853634bf
Child:
9:bf01a27b649f
--- a/main.cpp	Sat May 21 16:07:43 2016 +0000
+++ b/main.cpp	Sat May 21 16:13:12 2016 +0000
@@ -14,7 +14,7 @@
 static void play_tone(int tone, int duration) {
 
    int tone_period_us = 1000000/tone;
-    
+/*    
    for (long i = 0; i < duration * 1000L; i += tone_period_us * 2) {
 
         // let's change the buzzer's volume depending on how far we are
@@ -25,6 +25,22 @@
         buzzer = 0.0f;
         wait_us(tone_period_us);
     }
+*/
+#define PI 3.14159265
+
+    for (long i = 0; i < duration * 1000L; i += tone_period_us * 2) {
+        // let's change the buzzer's volume depending on how far we are
+        float pct = (float)i / (float)(duration * 1000L);
+
+        // make 4 sinus forms (8 * PI), then map it from -1..1 to 0.1..0.6,
+        // then divide by 8 to not make too much sound
+        buzzer = (sin(pct * (8 * PI)) / 2 + 0.6) / 8;
+        wait_us(tone_period_us);
+        buzzer = 0.0f;
+        wait_us(tone_period_us);
+    }
+
+    buzzer = 0.0f;
 
 }