HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
sound
Deutsch   English   

5. SOUND

 

 

YOU LEARN HERE...

 

how you can use the built-in speaker to play sounds, tone sequences and short melodies. Under additional material you can see how you can even connect headphones or speakers to the micro:bit to get better sound quality.

 

 

SPEAKER (BUZZER)

 

A speaker is built into the back of the micro:bit V2. You can use it to play individual tones and short melodies.

The commands for the buzzer are in the music module. You must also import this.

 

 

EXAMPLES

 

Example 1: Play a tone sequence
The pitch(f, 500) command plays a tone with the frequency f for 500 milliseconds. To play several tones in succession, enter the corresponding frequencies in a list and run through them with a for loop.

A table with tones and their frequencies can be found in the overlay window:


Program:

from music import *

song = [262, 294, 330, 349, 392, 392, 392, 0, 440, 440, 440, 440, 392]
for f in song: 
   pitch(f, 500)
► Copy to clipboard

 

Example 2: Playing built-in melodies
Some melodies are already built in. You can find their names here or in the documentation. You can combine playing the melodies with using the buttons. If you press button A, the melody JUMP_UP is played, if you press button B, the melody JUMP_DOWN is played.

Program:

from microbit import *
from music import *

while True:
    if button_a.was_pressed():
        play(JUMP_UP)
    if button_b.was_pressed():
        play(JUMP_DOWN)
    sleep(10)
► Copy to clipboard

What happens if you press and hold both buttons together?

 

Example 3: Playing melodies in musical notation

The notation adheres to the following rules:

  • The twelve semitones of an octave are labeled C, C#, D, D#, E, F, F#, G, G#, A, A#, B (also with lowercase letters). The note “R” is used for the rest
  • If you want to change the octave, write an octave number after the note, i.e. C2 for the second octave (at the beginning you are in the 4th octave). This octave then applies to all subsequent notes until an octave number is entered again
  • A colon with an indication of the duration (in ticks) can be placed after the note. All subsequent notes are then played with this duration until a new duration is specified.

To compose a melody, write the notes in a list, for example in the following program for a major and subsequent minor chord.

Program:

from music import *

melody = ['e:4', 'f#', 'g#', 'r:2', 'e', 'f#', 'g:4']
play(melody)
► Copy to clipboard

 

Example 4: An acoustic position meter
Measuring devices with acoustic outputs are very popular. Here you generate a sound signal whose level depends on the lateral inclination of the micro:bit. Starting from an initial frequency f0 = 1000, you calculate the rising and falling tone frequencies for semitones of the well-tempered tuning and play each tone for 100 milliseconds.

You can switch off the tone signal with the B button.

Program:

from music import *
from microbit import *

def beep(n):
    freq = int(f0 * r**n)
    pitch(int(r**n * f0), 100)
    sleep(50)

f0 = 1000
r = 2**(1/12)
while not button_b.was_pressed():
    n = int(accelerometer.get_x() / 100)
    beep(n)
    sleep(10)
► Copy to clipboard

 

 

REMEMBER YOU...

 

You must connect the headphones to the outputs labeled GND and P0. You play a sound with the frequency f with the command pitch(f, time). With play(song) you can play entire melodies, either as a tone sequence or in musical notation.

 

 

TO SOLVE BY YOURSELF

 

 

1.
a)

Write a program that plays the following tone sequence:

song = [262, 294, 330, 262, 262, 294, 330, 262, 330, 349, 392, 330, 349, 392]

You can of course also compose your own tone sequences.



b)

You want to change the playback speed interactively:

- If you click button A, the song should be played twice as fast

- If you click on button B, the playback will be slower.

Note that the time parameter in the pitch(f, time) command must be an integer.


2.

You can play a melody repeatedly by using play() with additional parameters, for example for the melody BIRTHDAY

play(BIRTHDAY, wait = False, loop = True)

Because wait = False, playback takes place in the background and the program continues to run.

Write a program that plays the birthday song endlessly until button A is clicked. (Note: You can end a sound output with the stop() function).

3.

Write the following melody in musical notation and play it.

 

 

 

ADDITIONAL MATERIAL

 

SETTING UP THE SOUND SYSTEM

 

You can also connect headphones or a larger loudspeaker to the micro:bit You will need two cables with crocodile clips. Use the first cable (here yellow) to connect the GND pin to the rear part of the headphone plug. Use the second cable (here red) to connect pin 0 to the front part of the headphone plug. In order to hear the sound in both earcups, you must try to place the alligator clip so that it touches the two front parts of the plug. Now you can test all the previous examples with your sound system.

 
 

 

 

REMEMBER...

 

You must connect the headphones to the outputs labeled GND and P0.



   
5-1
Technical notes

 

Ton Frequency Ton Frequency    

h' 494 h'' 988    
a' 440 a'' 880    
g' 392 g'' 784    
f' 349 f'' 698    
e' 330 e'' 660    
d' 294 d'' 588    
c' 262 c'' 524 c''' 1048

5-2
Technical notes

Melody, at modul music:

ADADADUM - Beethoven 5. Sinfonie C Moll
ENTERTAINER - Scott Joplin's Ragtime
PRELUDE -J.S.Bach 48 Preludien aund Fugen
ODE - "Ode on Joy" Beethoven 9. Sinfonie D Moll
NYAN - Nyan Phone tone
FUNK - Secret agent
BLUES - Boogie-Woogie Blues
BIRTHDAY - Happy Birthday to You
WEDDING - Wagner Oper "Lohengrin"
FUNERAL - funeral procession, Chopin Klaviersonate
PUNCHLINE - a funny Tonclip
PYTHON - Monty Python Flying Circust;
BADDY - Filmclip The Baddy
CHASE - Filmclick Jagdszene
BA_DING - Signalton
WAWAWAWAA - Posaunenklang
JUMP_UP - for games, up movement
JUMP_DOWN - for games, down movement
POWER_UP - power up
POWER_DOWN -power downt