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:
Tue Apr 30 03:49:24 2013 +0000
Revision:
2:c33ed3d85f97
Parent:
1:51cf7b1a96bd
All functions finalized and working!

Who changed what in which revision?

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