Labplus Knowledge Base

Product Documentation

  • mPython Board 3.0 Documentation
    • 1. Hardware
    • 2. Software Programming (MicroPython)
    • 3. Example Tutorials
    • 4. mPython Board 3.0 with Xiaozhi
    • 5. API Documentation
      • 5.1. MicroPython Syntax
      • 5.2. API Library
        • mPython Libraries
        • mPython Extension Libraries
        • Python Standard Library
        • MicroPython Libraries
      • cmath – mathematical functions for complex numbers
      • esp — functions related to the ESP8266
      • lcd160cr — control of LCD160CR display
      • wipy – WiPy specific features
      • 5. Arithmetic instructions
      • 6. Comparison instructions
      • 11. Assembler directives
      • 10. Floating point instructions
      • 1. Hints and tips
      • Inline assembler for Thumb2 architectures
      • 7. Branch instructions
      • 2. Load register from memory
      • 4. Logical & bitwise instructions
      • 9. Miscellaneous instructions
      • 1. Register move instructions
      • 8. Stack push and pop
      • 3. Store register to memory
      • MicroPython on microcontrollers
      • Glossary
      • Writing interrupt handlers
      • Package management
      • The MicroPython Interactive Interpreter Mode (aka REPL)
      • Maximising MicroPython speed
    • 6. mPython Board C/C++ Programming
    • 7. Technical Support
    • 8. Copyright Notice
  • mPython Core 2.0 Documentation
  • AI Camera 4.0 Documentation
  • mPython IoT Documentation
  • mPython Module Documentation

Knowledge Base

  • Labplus Knowledge Base

About

  • Product Selection Guide
Labplus Knowledge Base
  • mPython Board 3.0 Documentation
  • 5. API Documentation
  • 5.2. API Library
  • mPython Libraries
  • music — Music related functions
  • View page source

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 note R is 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)
  • music

    • Play music, which contains the music DSL defined above.

    • If music is a string, it should be a single note, for example 'c1:4'.

    • If music is specified as a list of notes (as defined in the music DSL section above), they are played one after another to perform a melody.

  • pin Defaults to the P6 pin of the mPython Board

  • wait Blocking: if wait is set to True, it blocks; otherwise it does not.

  • loop: if loop is set to True, 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 duration is 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).

  • pin pin=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.

  • wait Blocking: if wait is set to True, 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 = 4

  • bpm = 120

  • duration = 4

  • octave = 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)
Previous Next

© Copyright 2026, Labplus.

Built with Sphinx using a theme provided by Read the Docs.