TigerJython4Kids
 robotics
sound
Deutsch   English   

5. SOUND

 

 

YOU LEARN HERE...

 

how to connect a headphone or loudspeaker to the micro:bit to play sound sequences and short melodies.

 

 

SOUND OUTPUT

 

To hear sounds from the micro:bit, you need a pair of headphones and 2 cables with crocodile clips. Connect the first cable (here with yellow color) to the GND pin and the rear of the headphone plug. The second cable (here with red color) goes from P0 to the front end of the headphone plug. In order to hear the sound in both earpieces, you must try to place the crocodile clip so that it touches the two metallic parts of the plug.

 
 

 

 

EXAMPLES

 

Play a sequence of tones

The command pitch(f, 500) plays a tone with frequency f during 500 milliseconds. To play several notes one after the other, you put the corresponding frequencies in a list and iterate it with a for loop.

You can find a table with tones of the musical scale and their corresponding frequencies 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

 

Play built-in melodies
Some melodies are already installed in TigerJython. You can find their names here or in the documentation. In the next program you select two melodies to play by pressing the A or B button. If you press button A, the melody JUMP_UP will be played, while pressing button B plays the melody JUMP_DOWN.

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

 

Play melodies in musical notation

The notation uses the following rules:

  • The twelve semitones of an octave are written as C, C#, D, D#, E, F, F#, G, G#, A, A#, H  (also with lowercase letters). For a break you use the note "R"
  • To change the octave, you write an octave number behind the note, so C2 for the second octave.  The selected octave then applies to all subsequent notes until an octave number is again specified (default is the 4th octave)
  • Behind the note designator you may place a colon with an indication of the duration (in ticks). All subsequent notes will then be played with this duration until a new indication of duration follows.

To compose a melody, you 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

 

An acoustic position measuring device

Measuring devices with acoustic outputs are quite popular. In this application you generate a sound signal whose pitch depends on the lateral inclination of the micro:bit. Starting from an initial frequency f0 = 1000, you compute the rising or falling tone frequencies for semitones of the well-tempered scale and play each tone for 100 milliseconds.

With the button B you can turn off the sound.

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

 

 

MEMO

 

To play a sound you must connect a headphone (or a loudspeaker with a built-in amplifier) to the outputs labeled GND and P0. The command pitch(f, time).emits a tone of frequency f (in Hz) and duration time (in ms). With play(song) you can play whole melodies, either as a sequence of frequencies or in musical notation.

 

 

EXERCISES

 

 

1.
a)

Write a program that plays the following sequence:

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

Of course you can also compose your own tone sequences.

b)

You want to interactively change the playback speed:
- If you click button A, the song should be played twice as fast
- If you click button B, the playback will be slower.

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


2.

You can have a melody played repeatedly by using play() with additional parameters, such as the song birthday

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

Because wait = False, the playback is done in the background and the program continues with the next command.

Write a program that plays the song BIRTHDAY endlessly until you click the button A. (Note: You can stop a sound output with the command stop().)



3.

Play the following melody given in musical notation:

5-1
Professional hints:

Some sound frequencies:

Tone Frequency Tone 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
Professional hints:

Predifined song lists:

  • ADADADUM - the opening to Beethoven’s 5th Symphony in C minor
  • ENTERTAINER - the opening fragment of Scott Joplin’s Ragtime classic “The Entertainer”
  • PRELUDE - the opening of the first Prelude in C Major of J.S.Bach’s 48 Preludes and Fugues
  • ODE - the “Ode to Joy” theme from Beethoven’s 9th Symphony in D minor
  • NYAN - the Nyan Cat theme
  • RINGTONE - something that sounds like a mobile phone ringtone. To be 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 to You...”
  • WEDDING - the bridal chorus from Wagner’s opera “Lohengrin”
  • FUNERAL - the “funeral march” otherwise known as Frédéric Chopin’s Piano Sonata No. 2 in B♭ minor.
  • PUNCHLINE - a fun fragment that signifies a joke has been made
  • PYTHON - John Philip Sousa’s march “Liberty Bell” aka, the theme for “Monty Python’s Flying Circus”
  • BADDY - silent movie era entrance of a baddy
  • CHASE - silent movie era chase scene
  • BA_DING - a short signal to indicate something has happened
  • WAWAWAWAA - a very sad trombone
  • JUMP_UP - for use in a game, indicating upward movement
  • JUMP_DOWN - for use in a game, indicating downward movement
  • POWER_UP - a fanfare to indicate an achievement unlocked
  • POWER_DOWN - a sad fanfare to indicate an achievement lost

 

5-3
Professional hints:

In the well-tempered tuning the octave (doubling of the frequency) is divided into 12 semitones with the same frequency ratio r. So the following applies: r12 = 2 or
r =

5-4
Professional hints:

Instead of headphones, you can also use a cheap, amplifier-powered speaker (such as a "hamburger box") (but not a simle loud speaker without an amplifier).