HomeTurtlegraficsGPanelRobotics WebTigerPython |
Python - Online |
Deutsch English |
YOU LEARN HERE... |
how to play sounds, tone sequences and short melodies with the Calliope. |
EXAMPLES |
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)
Playing built-in melodies 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) What happens if you press and hold the two buttons together?
An acoustic position measuring device 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)
Microphone
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()
Connecting headphones or speakers
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.
Program: from music import * from calliope_mini import * print("starting") play(ENTERTAINER, pin = pin1) |
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. |
TO SOLVE BY YOURSELF |
|
ADDITIONAL MATERIAL |
Playing melodies in musical notation The notation adheres to the following rules:
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)
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") |
ZUM SELBST LÖSEN |
|
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 |
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
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 = |