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 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 1:51cf7b1a96bd 11 #include "Notes.h"
mdu7078 0:07a4aa44fe9c 12
mdu7078 0:07a4aa44fe9c 13 /* Define the music_note structure */
mdu7078 0:07a4aa44fe9c 14 typedef struct {
mdu7078 0:07a4aa44fe9c 15 double freq; //The frequency of the note
mdu7078 0:07a4aa44fe9c 16 int duration; //The duration is given by a number [1 - 64]
mdu7078 0:07a4aa44fe9c 17 } music_note;
mdu7078 0:07a4aa44fe9c 18
mdu7078 0:07a4aa44fe9c 19 /* Define an m_song structure */
mdu7078 0:07a4aa44fe9c 20 typedef struct {
mdu7078 0:07a4aa44fe9c 21 music_note* note; //The notes of the song
mdu7078 0:07a4aa44fe9c 22 int len; //The length of the song
mdu7078 0:07a4aa44fe9c 23 } m_song;
mdu7078 0:07a4aa44fe9c 24
mdu7078 0:07a4aa44fe9c 25 class music {
mdu7078 0:07a4aa44fe9c 26 public:
mdu7078 0:07a4aa44fe9c 27
mdu7078 2:c33ed3d85f97 28 music(PinName M0); //basic music constructor
mdu7078 0:07a4aa44fe9c 29 music(PinName M0, double freq); //music constructor
mdu7078 0:07a4aa44fe9c 30 void freq(double freq); //Frequency setter
mdu7078 0:07a4aa44fe9c 31 void play(char* song, double tempo, int num);
mdu7078 0:07a4aa44fe9c 32
mdu7078 0:07a4aa44fe9c 33 private:
mdu7078 0:07a4aa44fe9c 34
mdu7078 0:07a4aa44fe9c 35 /* Private functions */
mdu7078 0:07a4aa44fe9c 36 void flip(); //Flips the output of a pin at a fixed frequency
mdu7078 0:07a4aa44fe9c 37 m_song parse(char* song, int num_notes); //Parses a character array into notes
mdu7078 2:c33ed3d85f97 38 void init_song(m_song *m, int num); //Initialize an m_song
mdu7078 2:c33ed3d85f97 39 void dal_song(m_song *m); //Deallocate an m_song
mdu7078 0:07a4aa44fe9c 40
mdu7078 0:07a4aa44fe9c 41 /* Private variables */
mdu7078 0:07a4aa44fe9c 42 Ticker _flipper;
mdu7078 0:07a4aa44fe9c 43 DigitalOut _M0;
mdu7078 0:07a4aa44fe9c 44 double _freq;
mdu7078 0:07a4aa44fe9c 45
mdu7078 0:07a4aa44fe9c 46 };
mdu7078 0:07a4aa44fe9c 47
mdu7078 0:07a4aa44fe9c 48 #endif