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.

Committer:
mdu7078
Date:
Mon Apr 29 03:57:42 2013 +0000
Revision:
0:07a4aa44fe9c
Child:
1:51cf7b1a96bd
First commit of the music library.  Parse function is not yet implemented.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mdu7078 0:07a4aa44fe9c 1 /* * * * * * * * * * * * * * * * * * * * * * * * * * *
mdu7078 0:07a4aa44fe9c 2 * This is a simple music library for the mbed. *
mdu7078 0:07a4aa44fe9c 3 * *
mdu7078 0:07a4aa44fe9c 4 * Created by: Michael Dushkoff (mad1841@rit.edu) *
mdu7078 0:07a4aa44fe9c 5 * * * * * * * * * * * * * * * * * * * * * * * * * * */
mdu7078 0:07a4aa44fe9c 6
mdu7078 0:07a4aa44fe9c 7 #ifndef MBED_MUSIC_H
mdu7078 0:07a4aa44fe9c 8 #define MBED_MUSIC_H
mdu7078 0:07a4aa44fe9c 9
mdu7078 0:07a4aa44fe9c 10 #include "mbed.h"
mdu7078 0:07a4aa44fe9c 11
mdu7078 0:07a4aa44fe9c 12 /*
mdu7078 0:07a4aa44fe9c 13 * Note definitions
mdu7078 0:07a4aa44fe9c 14 *
mdu7078 0:07a4aa44fe9c 15 * Referenced from:
mdu7078 0:07a4aa44fe9c 16 * http://www.phy.mtu.edu/~suits/notefreqs.html
mdu7078 0:07a4aa44fe9c 17 */
mdu7078 0:07a4aa44fe9c 18 #define NOTE_RST (0)
mdu7078 0:07a4aa44fe9c 19 #define NOTE_C0 (16.35)
mdu7078 0:07a4aa44fe9c 20 #define NOTE_CS0 (17.32)
mdu7078 0:07a4aa44fe9c 21 #define NOTE_D0 (18.35)
mdu7078 0:07a4aa44fe9c 22 #define NOTE_DS0 (19.45)
mdu7078 0:07a4aa44fe9c 23 #define NOTE_E0 (20.60)
mdu7078 0:07a4aa44fe9c 24 #define NOTE_F0 (21.83)
mdu7078 0:07a4aa44fe9c 25 #define NOTE_FS0 (23.12)
mdu7078 0:07a4aa44fe9c 26 #define NOTE_G0 (24.50)
mdu7078 0:07a4aa44fe9c 27 #define NOTE_GS0 (25.96)
mdu7078 0:07a4aa44fe9c 28 #define NOTE_A0 (27.50)
mdu7078 0:07a4aa44fe9c 29 #define NOTE_AS0 (29.14)
mdu7078 0:07a4aa44fe9c 30 #define NOTE_B0 (30.87)
mdu7078 0:07a4aa44fe9c 31 #define NOTE_C1 (32.70)
mdu7078 0:07a4aa44fe9c 32 #define NOTE_CS1 (34.65)
mdu7078 0:07a4aa44fe9c 33 #define NOTE_D1 (36.71)
mdu7078 0:07a4aa44fe9c 34 #define NOTE_DS1 (38.89)
mdu7078 0:07a4aa44fe9c 35 #define NOTE_E1 (41.20)
mdu7078 0:07a4aa44fe9c 36 #define NOTE_F1 (43.65)
mdu7078 0:07a4aa44fe9c 37 #define NOTE_FS1 (46.25)
mdu7078 0:07a4aa44fe9c 38 #define NOTE_G1 (49.00)
mdu7078 0:07a4aa44fe9c 39 #define NOTE_GS1 (51.91)
mdu7078 0:07a4aa44fe9c 40 #define NOTE_A1 (55.00)
mdu7078 0:07a4aa44fe9c 41 #define NOTE_AS1 (58.27)
mdu7078 0:07a4aa44fe9c 42 #define NOTE_B1 (61.74)
mdu7078 0:07a4aa44fe9c 43 #define NOTE_C2 (65.41)
mdu7078 0:07a4aa44fe9c 44 #define NOTE_CS2 (69.30)
mdu7078 0:07a4aa44fe9c 45 #define NOTE_D2 (73.42)
mdu7078 0:07a4aa44fe9c 46 #define NOTE_DS2 (77.78)
mdu7078 0:07a4aa44fe9c 47 #define NOTE_E2 (82.41)
mdu7078 0:07a4aa44fe9c 48 #define NOTE_F2 (87.31)
mdu7078 0:07a4aa44fe9c 49 #define NOTE_FS2 (92.50)
mdu7078 0:07a4aa44fe9c 50 #define NOTE_G2 (98.00)
mdu7078 0:07a4aa44fe9c 51 #define NOTE_GS2 (103.83)
mdu7078 0:07a4aa44fe9c 52 #define NOTE_A2 (110.00)
mdu7078 0:07a4aa44fe9c 53 #define NOTE_AS2 (116.54)
mdu7078 0:07a4aa44fe9c 54 #define NOTE_B2 (123.47)
mdu7078 0:07a4aa44fe9c 55 #define NOTE_C3 (130.81)
mdu7078 0:07a4aa44fe9c 56 #define NOTE_CS3 (138.59)
mdu7078 0:07a4aa44fe9c 57 #define NOTE_D3 (146.83)
mdu7078 0:07a4aa44fe9c 58 #define NOTE_DS3 (155.56)
mdu7078 0:07a4aa44fe9c 59 #define NOTE_E3 (164.81)
mdu7078 0:07a4aa44fe9c 60 #define NOTE_F3 (174.61)
mdu7078 0:07a4aa44fe9c 61 #define NOTE_FS3 (185.00)
mdu7078 0:07a4aa44fe9c 62 #define NOTE_G3 (196.00)
mdu7078 0:07a4aa44fe9c 63 #define NOTE_GS3 (207.65)
mdu7078 0:07a4aa44fe9c 64 #define NOTE_A3 (220.00)
mdu7078 0:07a4aa44fe9c 65 #define NOTE_AS3 (233.08)
mdu7078 0:07a4aa44fe9c 66 #define NOTE_B3 (246.94)
mdu7078 0:07a4aa44fe9c 67 #define NOTE_C4 (261.63)
mdu7078 0:07a4aa44fe9c 68 #define NOTE_CS4 (277.18)
mdu7078 0:07a4aa44fe9c 69 #define NOTE_D4 (293.66)
mdu7078 0:07a4aa44fe9c 70 #define NOTE_DS4 (311.13)
mdu7078 0:07a4aa44fe9c 71 #define NOTE_E4 (329.63)
mdu7078 0:07a4aa44fe9c 72 #define NOTE_F4 (349.23)
mdu7078 0:07a4aa44fe9c 73 #define NOTE_FS4 (369.99)
mdu7078 0:07a4aa44fe9c 74 #define NOTE_G4 (392.00)
mdu7078 0:07a4aa44fe9c 75 #define NOTE_GS4 (415.30)
mdu7078 0:07a4aa44fe9c 76 #define NOTE_A4 (440.00)
mdu7078 0:07a4aa44fe9c 77 #define NOTE_AS4 (466.16)
mdu7078 0:07a4aa44fe9c 78 #define NOTE_B4 (493.88)
mdu7078 0:07a4aa44fe9c 79 #define NOTE_C5 (523.25)
mdu7078 0:07a4aa44fe9c 80 #define NOTE_CS5 (554.37)
mdu7078 0:07a4aa44fe9c 81 #define NOTE_D5 (587.33)
mdu7078 0:07a4aa44fe9c 82 #define NOTE_DS5 (622.25)
mdu7078 0:07a4aa44fe9c 83 #define NOTE_E5 (659.26)
mdu7078 0:07a4aa44fe9c 84 #define NOTE_F5 (698.46)
mdu7078 0:07a4aa44fe9c 85 #define NOTE_FS5 (739.99)
mdu7078 0:07a4aa44fe9c 86 #define NOTE_G5 (783.99)
mdu7078 0:07a4aa44fe9c 87 #define NOTE_GS5 (830.61)
mdu7078 0:07a4aa44fe9c 88 #define NOTE_A5 (880.00)
mdu7078 0:07a4aa44fe9c 89 #define NOTE_AS5 (932.33)
mdu7078 0:07a4aa44fe9c 90 #define NOTE_B5 (987.77)
mdu7078 0:07a4aa44fe9c 91 #define NOTE_C6 (1046.50)
mdu7078 0:07a4aa44fe9c 92 #define NOTE_CS6 (1108.73)
mdu7078 0:07a4aa44fe9c 93 #define NOTE_D6 (1174.66)
mdu7078 0:07a4aa44fe9c 94 #define NOTE_DS6 (1244.51)
mdu7078 0:07a4aa44fe9c 95 #define NOTE_E6 (1318.51)
mdu7078 0:07a4aa44fe9c 96 #define NOTE_F6 (1396.91)
mdu7078 0:07a4aa44fe9c 97 #define NOTE_FS6 (1479.98)
mdu7078 0:07a4aa44fe9c 98 #define NOTE_G6 (1567.98)
mdu7078 0:07a4aa44fe9c 99 #define NOTE_GS6 (1661.22)
mdu7078 0:07a4aa44fe9c 100 #define NOTE_A6 (1760.00)
mdu7078 0:07a4aa44fe9c 101 #define NOTE_AS6 (1864.66)
mdu7078 0:07a4aa44fe9c 102 #define NOTE_B6 (1975.53)
mdu7078 0:07a4aa44fe9c 103 #define NOTE_C7 (2093.00)
mdu7078 0:07a4aa44fe9c 104 #define NOTE_CS7 (2217.46)
mdu7078 0:07a4aa44fe9c 105 #define NOTE_D7 (2349.32)
mdu7078 0:07a4aa44fe9c 106 #define NOTE_DS7 (2489.02)
mdu7078 0:07a4aa44fe9c 107 #define NOTE_E7 (2637.02)
mdu7078 0:07a4aa44fe9c 108 #define NOTE_F7 (2793.83)
mdu7078 0:07a4aa44fe9c 109 #define NOTE_FS7 (2959.96)
mdu7078 0:07a4aa44fe9c 110 #define NOTE_G7 (3135.96)
mdu7078 0:07a4aa44fe9c 111 #define NOTE_GS7 (3322.44)
mdu7078 0:07a4aa44fe9c 112 #define NOTE_A7 (3520.00)
mdu7078 0:07a4aa44fe9c 113 #define NOTE_AS7 (3729.31)
mdu7078 0:07a4aa44fe9c 114 #define NOTE_B7 (3951.07)
mdu7078 0:07a4aa44fe9c 115 #define NOTE_C8 (4186.01)
mdu7078 0:07a4aa44fe9c 116 #define NOTE_CS8 (4434.92)
mdu7078 0:07a4aa44fe9c 117 #define NOTE_D8 (4698.64)
mdu7078 0:07a4aa44fe9c 118 #define NOTE_DS8 (4978.03)
mdu7078 0:07a4aa44fe9c 119
mdu7078 0:07a4aa44fe9c 120 /* Define the music_note structure */
mdu7078 0:07a4aa44fe9c 121 typedef struct {
mdu7078 0:07a4aa44fe9c 122 double freq; //The frequency of the note
mdu7078 0:07a4aa44fe9c 123 int duration; //The duration is given by a number [1 - 64]
mdu7078 0:07a4aa44fe9c 124 } music_note;
mdu7078 0:07a4aa44fe9c 125
mdu7078 0:07a4aa44fe9c 126 /* Define an m_song structure */
mdu7078 0:07a4aa44fe9c 127 typedef struct {
mdu7078 0:07a4aa44fe9c 128 music_note* note; //The notes of the song
mdu7078 0:07a4aa44fe9c 129 int len; //The length of the song
mdu7078 0:07a4aa44fe9c 130 } m_song;
mdu7078 0:07a4aa44fe9c 131
mdu7078 0:07a4aa44fe9c 132 class music {
mdu7078 0:07a4aa44fe9c 133 public:
mdu7078 0:07a4aa44fe9c 134
mdu7078 0:07a4aa44fe9c 135 music(PinName M0, double freq); //music constructor
mdu7078 0:07a4aa44fe9c 136 void freq(double freq); //Frequency setter
mdu7078 0:07a4aa44fe9c 137 void play(char* song, double tempo, int num);
mdu7078 0:07a4aa44fe9c 138
mdu7078 0:07a4aa44fe9c 139 private:
mdu7078 0:07a4aa44fe9c 140
mdu7078 0:07a4aa44fe9c 141 /* Private functions */
mdu7078 0:07a4aa44fe9c 142 void flip(); //Flips the output of a pin at a fixed frequency
mdu7078 0:07a4aa44fe9c 143 m_song parse(char* song, int num_notes); //Parses a character array into notes
mdu7078 0:07a4aa44fe9c 144
mdu7078 0:07a4aa44fe9c 145 /* Private variables */
mdu7078 0:07a4aa44fe9c 146 Ticker _flipper;
mdu7078 0:07a4aa44fe9c 147 DigitalOut _M0;
mdu7078 0:07a4aa44fe9c 148 double _freq;
mdu7078 0:07a4aa44fe9c 149
mdu7078 0:07a4aa44fe9c 150 };
mdu7078 0:07a4aa44fe9c 151
mdu7078 0:07a4aa44fe9c 152 #endif