MusicEngine library ported from mBed to ESP8266 (tested in Arduino IDE v1.6.10)
MusicEngine class / Retro Music Engine
Original author: Chris Taylor (taylorza). Open source license: Apache 2.0
see https://developer.mbed.org/users/taylorza/code/MusicEngine/
Ported from mBed to ESP8266/Arduino by MMOLE (maxint-rd), inherited Apache license.
MusicEngine provides a means to play Music Macro Language sequences asynchronously. Where the tone() API-function allows for playing one single note, the MusicEngine.play() method can play an entire music score.
The music is played using a ticker-interrupt routine that changes the PWM frequency according the specific notes being played. This means we can do other things while the music keeps playing.
Learn more about Music Macro Language (MML) on wikipedia:
http://en.wikipedia.org/wiki/Music_Macro_Language
For downloadable MML music see http://www.archeagemmllibrary.com/
Extensive MML reference guide (not all commands supported):
http://woolyss.com/chipmusic/chipmusic-mml/ppmck_guide.php
Info about using PWM and other methods to generate sound:
https://developer.mbed.org/users/4180_1/notebook/using-a-speaker-for-audio-output/
================================
The current version can be downloaded as an Arduino library using the Sketch|Library menu. Just add the zipfile library and the enclosed examples should appear in the menu automatically.
Initialisation outside of Setup():
// define pin, include header and initialize class
#include <MusicEngine.h>
#define BUZ_PIN 14
MusicEngine music(BUZ_PIN);
Then to play music, call the play method where you want:
music.play("T240 L16 O6 C D E F G");
Note: the music will keep on playing using a Ticker interrupt.
Short syntax overview:
Command | Description |
---|---|
Tnnn | Set tempo [32-255]. Examples: T120, T240 |
Vnnn | Set volume [0-128]. Note: limited effect on PWM-volume. Examples: V1, T120 |
Lnn | Set default note length [1-64]. Examples: L8, L16 |
Mx | Set timing. Mn=default, Ml=legato, Ms=staccato |
On | Set octave [0-7]. Examples: O6, O7 |
A-G | Play whole note. Example: C |
Ann-Gnn | Play note of alternative length [1-64]. Example: C4, A16 |
Nnn | Play frequency [0-96]. Example: N48 |
# | Play sharp note. Example: C# |
+ | Alternative for # |
− | Play flat note. Example: D- |
R | Rest. Example: CDEC r CDEC |
P | Alternative for R. Example: CDEC p CDEC |
. | Longer note. Example: CDEC. |
> | shift octave up. Example: CDE>CDE. |
< | shift octave down. Example: CDE<CDE. |
The supported MML-commands are a subset that may not completely cover all available music scores. If notes seem missing, check your score against the syntax above and replace unknown commands by equivalent supported alternatives. The music notation is case-insensitive. Spaces are not required but can be used for readability.