This is a simple sound library for mbed. This sound library uses a Ticker to simulate a 50% duty cycle pwm signal on any pin. The frequency of this signal can be varied to allow sound to be created by any piezoelectric device that is connected to that pin.

Dependents:   SuperMbedBall Tono

About

This library allows for a buzzer (or piezoelectric device) to output sound based on any inputted frequency. The library provides predefined note frequencies which make song writing convenient. These definitions can be found here. Some pins may not be able to drive output such as the AnalogIn pins on the LPC11U24 model. Only pin 21 seemed to work well so far on this model.

Hardware

One wire from a buzzer is connected to any pin on the mbed and the other wire is connected directly to ground. To reduce the volume of the buzzer, an optional resistor can be connected in series as shown. /media/uploads/mdu7078/buzzer001.png

Circuit diagram constructed using CircuitLab.

Usage

A music object is constructed in the following way:

Constructor

#include "Music.h"

music ms(p21);

This code snipped attaches a frequency modulating Ticker to the specified pin.

This frequency can be modified by setting:

Frequency

ms.freq(240);

To allow more complex behavior, a parsing function is included that can take in strings of formatted input and turn them into songs. This input must be formatted as specified below:

  • A capital letter must be present to specify the note, or R to specify a rest (Only letters A-G, and R are valid)
  • An octave must be specified which is any integer from 0 to 8 inclusive
  • An optional sharp sign ( # ) can be added to augment the note a half step
  • A colon ( : ) must separate the octave and note from the duration
  • The duration must be an integer greater than 0, and less than or equal to 64
  • To separate notes, a semicolon ( ; ) must be used

Furthermore, the length of the string must be specified in order for the entire string to be parsed and a tempo must be provided (beats per minute). An example of this is shown below:

Play a Song

/* This is a test song */
char s1[] = "E4:8; E4:8; R:8; E4:8; R:8; C4:8; E4:4; G4:4; R:4; G3:4; R:4;";
int len = 61;

/* Set up music pin on pin 21 */
music m1(p21);
double tempo = 180;

m1.play(s1,tempo,len);

This code plays the familiar 7-note opening to the Super Mario Brothers theme song. The possibilities for music creation are limited only by the hard memory limits of the mbed.

Revision:
0:07a4aa44fe9c
Child:
1:51cf7b1a96bd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Music.h	Mon Apr 29 03:57:42 2013 +0000
@@ -0,0 +1,152 @@
+/* * * * * * * * * * * * * * * * * * * * * * * * * * *
+ * This is a simple music library for the mbed.      *
+ *                                                   *
+ * Created by: Michael Dushkoff (mad1841@rit.edu)    *
+ * * * * * * * * * * * * * * * * * * * * * * * * * * */
+
+#ifndef MBED_MUSIC_H
+#define MBED_MUSIC_H
+ 
+#include "mbed.h"
+
+/* 
+ * Note definitions 
+ * 
+ * Referenced from:
+ * http://www.phy.mtu.edu/~suits/notefreqs.html
+ */
+#define NOTE_RST (0)
+#define NOTE_C0  (16.35)
+#define NOTE_CS0 (17.32)
+#define NOTE_D0  (18.35)
+#define NOTE_DS0 (19.45)
+#define NOTE_E0  (20.60)
+#define NOTE_F0  (21.83)
+#define NOTE_FS0 (23.12)
+#define NOTE_G0  (24.50)
+#define NOTE_GS0 (25.96)
+#define NOTE_A0  (27.50)
+#define NOTE_AS0 (29.14)
+#define NOTE_B0  (30.87)
+#define NOTE_C1  (32.70)
+#define NOTE_CS1 (34.65)
+#define NOTE_D1  (36.71)
+#define NOTE_DS1 (38.89)
+#define NOTE_E1  (41.20)
+#define NOTE_F1  (43.65)
+#define NOTE_FS1 (46.25)
+#define NOTE_G1  (49.00)
+#define NOTE_GS1 (51.91)
+#define NOTE_A1  (55.00)
+#define NOTE_AS1 (58.27)
+#define NOTE_B1  (61.74)
+#define NOTE_C2  (65.41)
+#define NOTE_CS2 (69.30)
+#define NOTE_D2  (73.42)
+#define NOTE_DS2 (77.78)
+#define NOTE_E2  (82.41)
+#define NOTE_F2  (87.31)
+#define NOTE_FS2 (92.50)
+#define NOTE_G2  (98.00)
+#define NOTE_GS2 (103.83)
+#define NOTE_A2  (110.00)
+#define NOTE_AS2 (116.54)
+#define NOTE_B2  (123.47)
+#define NOTE_C3  (130.81)
+#define NOTE_CS3 (138.59)
+#define NOTE_D3  (146.83)
+#define NOTE_DS3 (155.56)
+#define NOTE_E3  (164.81)
+#define NOTE_F3  (174.61)
+#define NOTE_FS3 (185.00)
+#define NOTE_G3  (196.00)
+#define NOTE_GS3 (207.65)
+#define NOTE_A3  (220.00)
+#define NOTE_AS3 (233.08)
+#define NOTE_B3  (246.94)
+#define NOTE_C4  (261.63)
+#define NOTE_CS4 (277.18)
+#define NOTE_D4  (293.66)
+#define NOTE_DS4 (311.13)
+#define NOTE_E4  (329.63)
+#define NOTE_F4  (349.23)
+#define NOTE_FS4 (369.99)
+#define NOTE_G4  (392.00)
+#define NOTE_GS4 (415.30)
+#define NOTE_A4  (440.00)
+#define NOTE_AS4 (466.16)
+#define NOTE_B4  (493.88)
+#define NOTE_C5  (523.25)
+#define NOTE_CS5 (554.37)
+#define NOTE_D5  (587.33)
+#define NOTE_DS5 (622.25)
+#define NOTE_E5  (659.26)
+#define NOTE_F5  (698.46)
+#define NOTE_FS5 (739.99)
+#define NOTE_G5  (783.99)
+#define NOTE_GS5 (830.61)
+#define NOTE_A5  (880.00)
+#define NOTE_AS5 (932.33)
+#define NOTE_B5  (987.77)
+#define NOTE_C6  (1046.50)
+#define NOTE_CS6 (1108.73)
+#define NOTE_D6  (1174.66)
+#define NOTE_DS6 (1244.51)
+#define NOTE_E6  (1318.51)
+#define NOTE_F6  (1396.91)
+#define NOTE_FS6 (1479.98)
+#define NOTE_G6  (1567.98)
+#define NOTE_GS6 (1661.22)
+#define NOTE_A6  (1760.00)
+#define NOTE_AS6 (1864.66)
+#define NOTE_B6  (1975.53)
+#define NOTE_C7  (2093.00)
+#define NOTE_CS7 (2217.46)
+#define NOTE_D7  (2349.32)
+#define NOTE_DS7 (2489.02)
+#define NOTE_E7  (2637.02)
+#define NOTE_F7  (2793.83)
+#define NOTE_FS7 (2959.96)
+#define NOTE_G7  (3135.96)
+#define NOTE_GS7 (3322.44)
+#define NOTE_A7  (3520.00)
+#define NOTE_AS7 (3729.31)
+#define NOTE_B7  (3951.07)
+#define NOTE_C8  (4186.01)
+#define NOTE_CS8 (4434.92)
+#define NOTE_D8  (4698.64)
+#define NOTE_DS8 (4978.03)
+
+/* Define the music_note structure */
+typedef struct {
+    double freq;  //The frequency of the note
+    int duration; //The duration is given by a number [1 - 64]
+} music_note;
+
+/* Define an m_song structure */
+typedef struct {
+    music_note* note; //The notes of the song
+    int len;           //The length of the song
+} m_song;
+
+class music {
+public:
+
+    music(PinName M0, double freq); //music constructor
+    void freq(double freq);  //Frequency setter
+    void play(char* song, double tempo, int num);
+    
+private:
+
+    /* Private functions */
+    void flip();  //Flips the output of a pin at a fixed frequency
+    m_song parse(char* song, int num_notes);  //Parses a character array into notes
+    
+    /* Private variables */
+    Ticker _flipper;
+    DigitalOut _M0;
+    double _freq;
+    
+};
+
+#endif
\ No newline at end of file