HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
sound
Deutsch   English   

5. SOUND

 

 

YOU LEARN HERE...

 

how to play sounds, tone sequences and short melodies with the Calliope.

 

 

EXAMPLES

 

The Calliope has a built-in loudspeaker that is clearly visible on the board.
To play sounds and melodies, import the music module.

 

Playing a tone sequence

The pitch(f, 500)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:

A tone with the frequency 0 is equivalent to a pause.

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

 

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 calliope_mini 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 the two buttons together?

 

An acoustic position measuring device
Measuring devices with acoustic outputs are very popular. Here you generate a sound signal whose level depends on the lateral inclination of the Calliope. 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 calliope_mini 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

 

Microphone

The Calliope has a microphone that can detect sounds. The microphone is only 3x4 mm in size. The function
isClicked(level , rearm_time)
returns True if the sound level exceeds the specified level (level = 1...500). The detection is then suppressed during the rearm_time

.

 

In your program, the sound level is set to 20. The microphone already reacts to faint noises (whistling, clapping, table tapping, etc.). However, you must wait until the program has started and “starting” is displayed in the terminal window. If a sound is detected, several LEDs light up on the display for 500 milliseconds.

 

Program:

from calliope_mini import *
from cpmike import *
    
print("starting")
while True:
    if isClicked(level = 20, rearm_time = 800):
        display.show(Image.DIAMOND)
        sleep(500)
        display.clear()
► Copy to clipboard

 

Connecting headphones or speakers

Instead of the built-in buzzer, which does not reproduce the sounds very well, you can connect external headphones or active speakers (with a built-in amplifier) to the Calliope. 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 1 to the front part of the headphone plug.

 

To output the sound via pin1, you must import the calliope_mini module and set the parameter pin to pin1 in the pitch() or play() function.

  To hear the sound in both earpieces, you must try to attach the red crocodile clip so that it touches the two front parts of the plug.

Program:

from music import *
from calliope_mini import *

print("starting")
play(ENTERTAINER, pin = pin1)
► Copy to clipboard

 

 

REMEMBER ...

 

You must connect the headphones to the outputs labeled GND and P1. Play a tone 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.
The built-in microphone detects ambient noise.

 

 

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)

Play the tone sequence faster or slower.


2.

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

3.

play(BIRTHDAY, wait = False, loop = True)
The parameter wait = False causes the playback to run in the background and the program to continue running. You can therefore switch on LEDs or use buttons during playback, for example.
Write a program that plays the birthday song endlessly until you click button A. (Note: You can end a sound output with the stop() command).

4.

the microphone of your Calliope reacts to sounds as follows: Every time you knock on the table, the melody JUMP_UP is played.

 

 

ADDITIONAL MATERIAL

 

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)
► In Zwischenablage kopieren


Morse code

Morse code is used to transmit messages. Letters, punctuation marks and numbers are coded as a short signal, long signal and pause. You can easily create Morse messages using the built-in loudspeaker. It is useful to store the code in a Python dictionary.

Program:

from calliope_mini import *
from music import *

dt = 100 # adapt to your speed

morse = {
'a':'.-'   , 'b':'-...' , 'c':'-.-.' , 'd':'-..'  , 'e':'.'    ,
'f':'..-.' , 'g':'--.'  , 'h':'....' , 'i':'..'   , 'j':'.---' ,
'k':'-.-'  , 'l':'.-..' , 'm':'--'   , 'n':'-.'   , 'o':'---'  ,
'p':'.--.' , 'q':'--.-' , 'r':'.-.'  , 's':'...'  , 't':'-'    ,
'u':'..-'  , 'v':'...-' , 'w':'.--'  , 'x':'-..-' , 'y':'-.--' ,
'z':'--..' , '1':'.----', '2':'..---', '3':'...--', '4':'....-',
'5':'.....', '6':'-....', '7':'--...', '8':'---..', '9':'----.',
'0':'-----', '-':'-....-', '?':'..--..', ',':'--..--', ':':'---...',
'=':'-...-'}

def dot():
    pitch(1000, dt)
    sleep(dt)

def dash():
    pitch(1000, 3 * dt)
    sleep(dt)

def transmit(text):
    for c in text:
        if c == " ":
            sleep(4 * dt)
        else:
            c = c.lower()
            if c in morse:
                k = morse[c]
                for x in k:
                    if x == '.':
                        dot()
                    else:
                        dash()
            sleep(2 * dt)

transmit("komme morgen abend")
► In Zwischenablage kopieren

 

 

ZUM SELBST LÖSEN

 
5.

Write the following melody in musical notation and play it back.



6. Write a program that plays a secret message in Morse code so slowly that your companion can decipher it.

 


   
5-1
Technical notes:

Some audio frequences:

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
Technical Notes:

Melody built into the music module.

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

5-3
Technical notes

As the octave (doubling of the frequency) is divided into 12 semitones with the same frequency ratio r in the well-tempered tuning, the following applies r12 = 2
r =


Warning: include(a/5-4.html): Failed to open stream: No such file or directory in /var/www/html/engl/robotik/cp/sound.inc.php on line 327

Warning: include(): Failed opening 'a/5-4.html' for inclusion (include_path='.:/usr/local/lib/php') in /var/www/html/engl/robotik/cp/sound.inc.php on line 327