Test program for PwmSound class. Provides tests (demos) for the various sound generating functions in the class. Includes several MML melodies and music clips to test the play() function. Tested on an mbed LPC1768.

Dependencies:   PwmSound mbed

Files at this revision

API Documentation at this revision

Comitter:
paulg
Date:
Tue May 06 13:23:02 2014 +0000
Parent:
0:41640fc98a43
Commit message:
Play music written in Music Macro Language (MML) with your mbed! This program fully tests the revised PwmSound library including the new play() function. It includes several MML melodies and music clips.

Changed in this revision

FastOut/FastOut.h Show annotated file Show diff for this revision Revisions of this file
PwmSound.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FastOut/FastOut.h	Tue May 06 13:23:02 2014 +0000
@@ -0,0 +1,97 @@
+/******************************************************************************
+ * File:      FastOut.h
+ * Author:    Paul Griffith
+ * Created:   18 Jun 2011
+ * Last Edit: 29 Sep 2013
+ * Version:   1.10
+ *
+ * Description:
+ * High-speed digital output class for mbed
+ *
+ * Usage: FastOut<PinName> name;
+ * PinName uses the same assignment operators as with DigitalOut
+ * Example: FastOut<LED2> led2;
+ * A simple loop as below takes 4 clocks. This gives a 24MHz output waveform
+ *      while (1) { out = 1; out = 0; } 
+ *
+ * Class code by Igor Skochinsky. Copied from posting by Sylvain Azerian:
+ *     http://sylvain.azarian.org/doku.php?id=mbed.
+ *
+ * Copyright (c) 2011-2013 Paul Griffith, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is 
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Modifications:
+ * Ver  Date    By  Details
+ * 1.00 18Jun11 PG  File created.
+ * 1.01 20Jun11 PG  Tidied up, added header block.
+ * 1.10 29Sep13 PG  Replaced link to MIT Licence with full MIT Licence text.
+ *
+ ******************************************************************************/
+
+#ifndef MBED_FASTOUT_H
+#define MBED_FASTOUT_H
+
+#include "mbed.h"
+
+template <PinName pin> class FastOut {
+// pin = LPC_GPIO0_BASE + port * 32 + bit
+// port = (pin - LPC_GPIO0_BASE) / 32
+// bit  = (pin - LPC_GPIO0_BASE) % 32
+
+    // helper function to calculate the GPIO port definition for the pin
+    // we rely on the fact that port structs are 0x20 bytes apart
+    inline LPC_GPIO_TypeDef* portdef() {
+        return (LPC_GPIO_TypeDef*)(LPC_GPIO0_BASE + ((pin - P0_0)/32)*0x20);
+    };
+
+    // helper function to calculate the mask for the pin's bit in the port
+    inline uint32_t mask() {
+        return 1UL<<((pin - LPC_GPIO0_BASE) % 32);
+    };
+
+public:
+    FastOut() {
+        // set FIODIR bit to 1 (output)
+        portdef()->FIODIR |= mask();
+    }
+    void write(int value) {
+        if ( value )
+            portdef()->FIOSET = mask();
+        else
+            portdef()->FIOCLR = mask();
+    }
+    int read() {
+        return (portdef()->FIOPIN) & mask() != 0;
+    }
+    FastOut& operator= (int value) {
+        write(value);
+        return *this;
+    };
+    FastOut& operator= (FastOut& rhs) {
+        return write(rhs.read());
+    };
+    operator int() {
+        return read();
+    };
+};
+
+#endif
+
+// END of FastOut.h
--- a/PwmSound.lib	Sun Mar 30 22:54:52 2014 +0000
+++ b/PwmSound.lib	Tue May 06 13:23:02 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/paulg/code/PwmSound/#185bcd9f8e19
+http://mbed.org/users/paulg/code/PwmSound/#67056b9df9ff
--- a/main.cpp	Sun Mar 30 22:54:52 2014 +0000
+++ b/main.cpp	Tue May 06 13:23:02 2014 +0000
@@ -32,6 +32,7 @@
  * Ver  Date    By  Details
  * 0.00 25Mar14 PG  File created.
  * 1.00 30Mar14 PG  Initial release.
+ * 2.00 06May14 PG  Many changes and additions to test V2.00 library.
  *
  ******************************************************************************/
 
@@ -41,83 +42,222 @@
 PwmSound mySpeaker(p24);    
 Serial pc(USBTX, USBRX);
 
+void printHelps(void);
 void badArgs(void);
 void nyi(void);
 void pause(void);
 
-// a simple melody (8 notes)
+// Tone fragments
+
+#define DOODLY "O3>CG8G8AGPB>C"
+#define YAY "O4L32MLCDEFG"		//success
+#define WAH "T80O2L32GFEDC"		//failure
+
+// Preset melodies in MML format
+// Melodies 0-3 are taken from Michael J. Mefford's MS-DOS PLAY utility which
+// was published as part of a PC Magazine cover disk in 1992.
+
+// Super Mario Brothers theme (11 notes)
 
-int melody[8] = {
-    37, 32, 32, 34, 32, 0, 36, 37
+char* smb = {
+	"T180 O3 E8 E8 P8 E8 P8 C8 D#4 G4 P4 <G4 P4"
+};
+
+char* preset0 = {       //short Dragnet theme
+    "T90 O2 L8E. L16F# L8G E P2 L8E. L16F# L8G E L3 B-"
+};
+
+char* preset1 = {       //long Dragnet theme
+    "T190O2L4EL8EL4MLG.MNL4G.EL8GL4AL8AL4MLG.MNL2G.L4EL8EL4MLG.MNL4G.EL8GL4AL8AL4MLG."
+    "MNL2G.T90L8E.L16F#L8GEP2L8E.L16F#L8GEL3B-"
+};
+
+char* preset2 = {       //Greensleeves
+    "O3A O4L2CL4D E.L8FL4E L2DL4O3B G.L8AL4B O4L2CO3L4A A.L8G#L4A L2BL4G# O3L2EL4A"
+    "O4L2CL4D E.L8FL4E L2DL4O3B G.L8AL4B O4L4C.O3L8BL4A L4G#.L8F#L4G# L2A. L2A."
+    "O4L2G. L4G.L8F#L4E L2DO3L4B G.L8AL4B L2O4CO3L4A A.L8G#L4A L2BL4G# L2E."
+    "O4L2G. L4G.L8F#L4E L2DO3L4B G.L8AL4B O4L4C.O3L8BL4A G#.L8F#L4G# L2A. A."
 };
 
-int melodyDurations[] = {
-    4, 8, 8, 4, 4, 4, 4, 4
+char* preset3 = {		//The Entertainer by Scott Joplin
+	":The Entertainer by Scott Joplin\n"	//comment lines need a \n
+	""
+	"T80            :Prelude\n"
+	""
+	"O4MLL64C#MNL16DECO3L8AL16BMSL8G"
+	"MLL64C#MNL16DECO2L8AL16BL8MSGMN"
+	"O2MLL64C#MNL16DECO1L8AL16BAA-"
+	"MSL8GMNP4O3L8GO2L16MLDMND#"
+	""
+	"MLO2L16EMSL8O3CMLO2L16EO3MSL8C"
+	"MNO2L16EO3L4C.L16O4CDD#"
+	"ECDL8EL16O3BO4MSL8D"
+	"MNCL16O3MLEGO4MSL8CMLO2L16DMND#"
+	"MLO2L16EMSL8O3CMLO2L16EO3MSL8C"
+	"MLO2L16EO3L4C.L16MLAMNG"
+	"F#AO4CL8EL16DCO3A"
+	"O4L4D.O1L16MLDMND#"
+	"MLO2L16EMSL8O3CMLO2L16EO3MSL8C"
+	"MNO2L16EO3L4C.L16O4CDD#"
+	"ECDL8EL16O3BO4MSL8D"
+	"MNCL16O3MLEGO4MSL8CMNL16CD"
+	"ECDL8EL16CDC"
+	"ECDL8EL16CDC"
+	"ECDL8EL16O3BO4L8MSDMN"
+	"L5MLC.MNL16O3EFF#"
+	""
+	"O3L8MSGMNL16AL8GL16EFF#"
+	"MSL8GMNL16AL8GL16MSECO2G"
+	"MLL16ABO3CDEDCD"
+	"MNO2GMLO3EFGAGEMNF"
+	"L8MSGMNL16AL8GL16EFF#"
+	"L8MSGMNL16AL8GL16GAA#"
+	"BL8BL16L8BL16AF#D"
+	"L5MLG.MNL16EFF#"
+	"MSL8GMNL16AL8GL16EFF#"
+	"L8MSGMNL16AL8GL16MLECO3MNG"
+	"MLABO4CDEDCD"
+	"MSL8MLCL16EGMSO5CO4MNGF#G"
+	"MSO5L8CO4MNL16AO5L8CL16O4AO5CO4A"
+	"GO5CEL8GL16ECO4G"
+	"MSL8AO5CMNL16EL8D"
+	"L4C..L16O4EFF#"
+	"MSL8GMNL16AL8GL16EFF#"
+	"L8GL16AL8GL16MSECO3G"
+	"MNABO4CDEDCD"
+	"O3GO4EFGAGEF"
+	"MSL8GMNL16AL8GL16EFF#"
+	"L8GL16AL8GL16GAA#"
+	""
+	"BL8BBL16AF#D"
+	"MLL64GAGAGAGAGAGAGAGAGL16MNEFF#"
+	"L8MSGL16AL8GL16EFF#"
+	"L8GL16AL8GL16MSECO3G"
+	"MNABO4CDEDCD"
+	"MSL8CL16MLEGO5MNCO4GF#G"
+	"O5MSL8CO4MNL16AMLO5L8CL16O4AO5"
+	"CO4AGO5CEL8GL16ECO4G"
+	"L8MSAO5CMNL16EL8DL16MLC"
+	"L4CL8C"
 };
 
-// Dragnet theme (9 notes)
+// The M.GAKKOU KOUKA
+// copyright "Music Composed by Kenkichi Motoi 2009 Wikimedia version 2012"
 
-unsigned char dragnet[2+(9*2)+2] = {
-    90,     //tempo
-    PwmSound::NORMAL,   //style
-    29, 5,  31, 16,  32, 8,  0, 8,  29, 5,  31, 16,  32, 8,  29, 8,  35, 3,
-    0, 0    //terminator
+char* preset4 = {	//play with sticky shift?
+	"T160 O3L4"
+	"ED8CE8 GG8ER8 AA8>C<A8 G2R"
+	"AA8GA8 >CC8D<R8 EE8DE8 C2R"
+	"DD8DD8 DD8DR8 ED8EF8 G2R"
+	"AA8GA8 >CC8<AR8 >DC8DE8 D2<R"
+	">EE8DC8< AB8>CC8< GG8EA8 G2R"
+	">CC8<GE8 CD8EA8 GG8DE8 C2R"
 };
 
 int main()
 {
+	char buf[40];
     int i, n;
+    Timer t;
 
-    pc.printf("\nPWM Sound Demo 7, %s\n", __DATE__);
+    pc.printf("\nPWM Sound Demo 8, %s %s\n", __DATE__, __TIME__);
+    mySpeaker.play(DOODLY);
 
     while(1) {
-        pc.printf("0 = 2-tone, 1 = b/g, 2x = melody, 3x = trill, 4x = phone, 5 = Dragnet, 6x-9x = beeps: ");
-        n = pc.getc() - '0';
-        pc.printf("%d", n);
-        if (n == 0) {
-            pc.printf("(= to stop)");
+        pc.printf("Enter command (? for help): ");
+        n = pc.getc();
+        pc.printf("%c", n);
+        if (n == '?') {
+            pc.printf("\n");
+            printHelps();
+        } else if (n =='0') {
             mySpeaker.siren(0);
-        }
-        else if (n == 1) {
-            mySpeaker.tone(440.0, 0.0);
-            pc.getc();
-            mySpeaker.stop();
-        } else if (n == 2) {
+        } else if (n == '1') {
             i = pc.getc() - '0';
             pc.printf("%d", i);
-            mySpeaker.tempo(240);
-            mySpeaker.style( (PwmSound::MusicStyle) (i % 3));
-            for (int i = 0; i < 8; i++) {
-                mySpeaker.note(melody[i], melodyDurations[i]);
-            }           
-        } else if (n == 3) {
+            mySpeaker.play(YAY);
+        } else if (n == '2') {
+            i = pc.getc() - '0';
+            pc.printf("%d", i);
+            mySpeaker.play(WAH);
+        } else if (n == '3') {
             i = pc.getc() - '0';
             pc.printf("%d", i);
             mySpeaker.trill(i);
-        } else if (n == 4) {
+        } else if (n == '4') {
             i = pc.getc() - '0';
             pc.printf("%d", i);
             mySpeaker.phone(i);
-        } else if (n == 5) {
-            mySpeaker.tune(dragnet);
-        } else if (n == 6) {
+        } else if (n == '5') {
             i = pc.getc() - '0';
             pc.printf("%d", i);
             mySpeaker.bip(i);
-        } else if (n == 7) {
+            wait(1);
+            mySpeaker.bop(i);
+        } else if (n == '6') {
+            i = pc.getc() - '0';
+            pc.printf("%d", i);
+            mySpeaker.bop(i);
+            wait(1);
+            mySpeaker.bop(i);
+        } else if (n == '7') {
             i = pc.getc() - '0';
             pc.printf("%d", i);
             mySpeaker.beep(i);
-        } else if (n == 8) {
+        } else if (n == '8') {
             i = pc.getc() - '0';
             pc.printf("%d", i);
             mySpeaker.bleep(i);
-        } else if (n == 9) {
+        } else if (n =='9') {
             i = pc.getc() - '0';
             pc.printf("%d", i);
             mySpeaker.buzz(i);
-        } else if (n == 13) {       //press = to stop a continuous sound
+        } else if (n == '=') {       //press = to stop a continuous sound
             mySpeaker.stop();
+        } else if (n == 'm') {       //press m for SMB theme
+            mySpeaker.play(smb);
+        } else if (n == 'p') {       //press pn to play a tune in MML format
+            i = pc.getc() - '0';
+            pc.printf("%d", i);
+            if (i == 0) {
+                 n = mySpeaker.play(preset0);
+            } else if (i == 1) {
+                n = mySpeaker.play(preset1);
+            } else if (i == 2) {
+                n = mySpeaker.play(preset2);
+            } else if (i == 3) {
+                n = mySpeaker.play(preset3);
+            } else if (i == 4) {
+                n = mySpeaker.play(preset4, 0);
+            } else if (i == 5) {
+                n = mySpeaker.play(preset4, 1);
+            } else if (i == 6) {
+                n = mySpeaker.play(preset4, 2);
+            } else if (i == 7) {
+                n = mySpeaker.play(preset4, 3);
+            }
+            if (n > 0) {
+            	pc.printf(" ??input error at char %d", n);
+            }
+        } else if (n == 'q') {		 //play quick tune
+        	scanf("%39s", buf);
+        	pc.printf("%s", buf);
+        	n = mySpeaker.play(buf);
+            if (n > 0) {
+            	pc.printf(" ??input error at char %d", n);
+            }
+        } else if (n == 't') {       //press tn to set timbre
+        	i = pc.getc() - '0';
+            pc.printf("%d", i);
+            mySpeaker.timbre(i);
+        } else if (n == 'z') {       //press z to time tone() call
+            t.reset();
+            t.start();
+            for (int i = 0; i < 1000000; i++) {
+                mySpeaker.tone(440.0, 0.0);
+            }
+            t.stop();
+            pc.printf("<1 millon calls took %f seconds>", t.read());
         } else {
             badArgs();            
         }
@@ -125,6 +265,24 @@
     }   //end of while
 }
 
+void printHelps(void) {
+    pc.printf("0     Two-tone (police siren) in background (press = to stop)\n");
+    pc.printf("1x    Yay (success) x times\n");
+    pc.printf("2x    Wah (failure) x times\n");
+    pc.printf("3x    Trill x times (0 = continuous in background)\n");
+    pc.printf("4x    Phone x times\n");
+    pc.printf("5     Bip x times (0 = continuous in background)\n");
+    pc.printf("6x    Bop x times (0 = continuous in background)\n");
+    pc.printf("7x    Beep x times (0 = continuous in background)\n");
+    pc.printf("8x    Bleep x times (0 = continuous in background)\n");
+    pc.printf("9x    Buzz x times (0 = continuous in background)\n");
+    pc.printf("=     Stop background sound or music\n");
+    pc.printf("m     Super Mario Brothers theme\n");
+    pc.printf("px    Play preset tune x in MML format (x = 0-7)\n");
+    pc.printf("q abc Play quick tune as entered on the command line\n");
+    pc.printf("tx    Set timbre (x = 1-4)\n");
+}
+
 //************************
 // Miscellaneous functions
 //************************
@@ -143,4 +301,4 @@
     pc.getc();
 }
 
-//END of main.cpp
\ No newline at end of file
+//END of main.cpp