music — Music related functions
The music module is used in the same way as the micro:bit music module.
To use the music module, you need:
import music
Notes
The note format is as follows:
NOTE[octave][:duration]
NOTE- Represents the notes c, d, e, f, g, a, b. Note that “#” raises the base note by a semitone; “b” lowers the base note by a semitone. The noteRis treated as a rest (silence). For example, c# is C-sharp and ab is A-flat.
octave- Represents the pitch, defaulting to 4, which is the middle octave.
duration- Represents the number of beats the note lasts, defaulting to 4 beats.
For example, A1:4 is the note “A” at octave 1, lasting 4 beats (the beat can also be set by the set_tempo function - see below).
The opening of Beethoven’s Fifth Symphony:
['r4:2', 'g', 'g', 'g', 'eb:8', 'r:2', 'f', 'f', 'f', 'd:8']
The definition and range of octaves follow the table on scientific pitch notation listed on this page Scientific pitch notation. For example, middle “C” is c4 and concert “A” (440) is a4. Octave notes start from the note “C”.
Functions
- music.set_tempo(ticks=4, bpm=120)
Set the playback tempo.
ticks- The note value, an integer. Defaults to 4, meaning a quarter note is one beat.bpm- The tempo speed, an integer. Unit: bpm (beats per minute).
You can refer to the following tempo settings:
music.set_tempo()- Restore the playback tempo to ticks = 4, bpm = 120
music.set_tempo(ticks=8)- Change only the note value, an eighth note is one beat
music.set_tempo(bpm=180)- Change only the tempo speed
The beat duration (in milliseconds) is calculated as: 60000/bpm/ticks_per_beat
For the default values, 60000/120/4 = 125 milliseconds or 1 beat = 500 milliseconds
- music.get_tempo()
Get the current tempo as an integer tuple:
(ticks, bpm).
- music.play(music, pin=6, wait=True, loop=False)
musicPlay
music, which contains the music DSL defined above.If
musicis a string, it should be a single note, for example'c1:4'.If
musicis specified as a list of notes (as defined in the music DSL section above), they are played one after another to perform a melody.
pinDefaults to the P6 pin of the mPython BoardwaitBlocking: ifwaitis set toTrue, it blocks; otherwise it does not.loop: ifloopis set toTrue, it repeats until stop is called (see below) or the blocking call is interrupted.
- music.pitch(frequency, duration=-1, pin=Pin.P6, wait=True)
frequency,duration: Play a frequency at the given integer frequency for the specified number of milliseconds. For example, if the frequency is set to 440 and the duration to 1000, we will hear the standard A note for one second.If
durationis negative, the frequency plays continuously until it blocks or is interrupted, or, in the case of a background call, until a new frequency is set or stop is called (see below).pinpin=Pin.P6, defaults to the P6 pin of the mPython Board. It can be redefined to other pins.Note that you can only play a frequency on one pin at a time.
waitBlocking: ifwaitis set toTrue, it blocks; otherwise it does not.
- music.stop()
Stop all music playback on the given pin.
- music.reset()
Reset the state of the following attributes in the following way
ticks = 4bpm = 120duration = 4octave = 4
Built-in Melodies
For educational and entertainment purposes, this module contains several example tunes represented as Python lists. They can be used like this:
>>> import music
>>> music.play(music.NYAN)
All the music is free of copyright, written by Nicholas H.Tollervey and released into the public domain, or by an unknown composer and protected under fair (educational) use terms.
They are:
DADADADUM- The opening of Beethoven’s Fifth Symphony in C minor.
ENTERTAINER- The opening segment of Scott Joplin’s Ragtime classic “The Entertainer”.
PRELUDE- The opening of the first C major prelude from JS Bach’s 48 Preludes and Fugues.
ODE- The “Ode to Joy” theme from Beethoven’s Seventh Symphony in D minor.
NYAN- The Nyan Cat theme (http://www.nyan.cat/). Composer unknown.
RINGTONE- Something that sounds like a phone ringtone. Used to indicate an incoming message.
FUNK- A funky bass line for secret agents and criminal masterminds.
BLUES- A boogie-woogie 12-bar blues walking bass.
BIRTHDAY- “Happy Birthday”. For the copyright status see: http://www.bbc.co.uk/news/world-us-canada-34332853
WEDDING- The bridal chorus from Wagner’s opera “Lohengrin”.
FUNERAL- The “Funeral March”, also known as Frédéric Chopin’s Piano Sonata No. 2 in B minor, Op. 35.
PUNCHLINE- A fun segment indicating that a joke has been made.
PYTHON- John Philip Sousa’s march “Liberty Bell”, aka the theme of “Monty Python’s Flying Circus” (after which the Python programming language was named).
BADDY- A baddie entering from the silent movie era.
CHASE- A chase scene from the silent movie era.
BA_DING- A short signal indicating that something has happened.
WAWAWAWAA- A very sad trombone.
JUMP_UP- For games, indicating upward movement.
JUMP_DOWN- For games, indicating downward movement.
POWER_UP- A flourish indicating that an achievement has been unlocked.
POWER_DOWN- A sad sound indicating that an achievement has been lost.
GE_CHANG_ZU_GUO- Ode to the Motherland
DONG_FANG_HONG- The East Is Red
CAI_YUN_ZHUI_YUE- Colored Clouds Chasing the Moon
ZOU_JIN_XIN_SHI_DAI- Into the New Era
MO_LI_HUA- Jasmine Flower
YI_MENG_SHAN_XIAO_DIAO- Yimeng Mountain Folk Tune
Example:
"""
music.py
~~~~~~~~
Plays a simple tune using the Micropython music module.
This example requires a speaker/buzzer/headphones connected to P0 and GND.
"""
from mpython import *
import music
# play Prelude in C.
notes = [
'c4:1', 'e', 'g', 'c5', 'e5', 'g4', 'c5', 'e5', 'c4', 'e', 'g', 'c5', 'e5', 'g4', 'c5', 'e5',
'c4', 'd', 'a', 'd5', 'f5', 'a4', 'd5', 'f5', 'c4', 'd', 'a', 'd5', 'f5', 'a4', 'd5', 'f5',
'b3', 'd4', 'g', 'd5', 'f5', 'g4', 'd5', 'f5', 'b3', 'd4', 'g', 'd5', 'f5', 'g4', 'd5', 'f5',
'c4', 'e', 'g', 'c5', 'e5', 'g4', 'c5', 'e5', 'c4', 'e', 'g', 'c5', 'e5', 'g4', 'c5', 'e5',
'c4', 'e', 'a', 'e5', 'a5', 'a4', 'e5', 'a5', 'c4', 'e', 'a', 'e5', 'a5', 'a4', 'e5', 'a5',
'c4', 'd', 'f#', 'a', 'd5', 'f#4', 'a', 'd5', 'c4', 'd', 'f#', 'a', 'd5', 'f#4', 'a', 'd5',
'b3', 'd4', 'g', 'd5', 'g5', 'g4', 'd5', 'g5', 'b3', 'd4', 'g', 'd5', 'g5', 'g4', 'd5', 'g5',
'b3', 'c4', 'e', 'g', 'c5', 'e4', 'g', 'c5', 'b3', 'c4', 'e', 'g', 'c5', 'e4', 'g', 'c5',
'a3', 'c4', 'e', 'g', 'c5', 'e4', 'g', 'c5', 'a3', 'c4', 'e', 'g', 'c5', 'e4', 'g', 'c5',
'd3', 'a', 'd4', 'f#', 'c5', 'd4', 'f#', 'c5', 'd3', 'a', 'd4', 'f#', 'c5', 'd4', 'f#', 'c5',
'g3', 'b', 'd4', 'g', 'b', 'd', 'g', 'b', 'g3', 'b3', 'd4', 'g', 'b', 'd', 'g', 'b'
]
music.play(notes)