This library is replaced by the MmlMusicPWM library, which is an extension of the new device independant MML Music library. That library supports not only the ESP8266, but also various Atmel MCUs, such as the ATmega328 and Atmega168 and even the ATtiny85. The MmlMusic base library also supports playing polyphone MML music from multiple tracks on multi-channel sound devices such as the SN76489 complex sound generator.
MusicEngine library ported from mBed to Arduino (tested IDE v1.6.10 and v1.8.2). Supports ESP8266 and ATmega (tested 328 and 168)
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 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 an 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. On the ESP8266 the Ticker library is used. For ATmega a Timer2 interrupt is used.
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.
- The current version of this library supports ESP8266 and Atmel ATmega328 and ATmega168 MCUs. Support for ATtiny85 is being added, so stay tuned!
- This version currently supports only single channel playback on a buzzer of speaker. Playback using a complex sound generator is under investigation.
- Known bug: when ending the play-string with a number (eg. "T120 O4 G16") the player may read beyond the end of the string and play whatever is next in memory. Workaround: use alternative notation (eg. "T120 O4 L16 G") or an addional terminator (eg. "T120 O4 G16\0").