Deutsch English |
YOU LEARN HERE... |
how the Turtle can play different sounds and melodies and even draw figures. |
TONE FREQEUENCIES |
To play different tones, use the function
In order to tune instruments, Tone a' with a frequency of 440 Hz was defined as the concert pitch.The other frequencies can be calculated. The frequencies in the table are rounded. |
EXAMPLES |
Example 1: Playing a short melody Program: from gturtle import * song = [262, 294, 330, 349, 392, 392, 392, 0, 440, 440, 440, 440, 392] for f in song: playTone(f, 250)
Example 2: Set tones with tone names Program: from gturtle import * song = ["c'","d'","e'","f'","g'","g'","g'",0,"a'","a'","a'","a'","g'"] song2 = ["c''","d''","e''","f''","g''","g''","g''",0,"a''","a''", "a''","a''","g''"] for f in song: playTone(f, 250) delay(1000) for f in song2: playTone(f, 250) Example 3: Playing tones and drawing at the same time
Program: from gturtle import * def step(): forward(20) right(90) forward(30) left(90) song = [262, 294, 330, 349, 392, 440, 494, 524] for f in song: playTone(f, 500, block = False) #playTone(f, 500) step()
Example 4: Play several tones simultaneously Program: from gturtle import * playTone(262, 2000, block = False) playTone(330, 2000, block = False) playTone(392, 2000, block = False)
Example 5: Choose different musical instruments Program: from gturtle import * song = ["c'", "e'", "g'", "c''"] for f in song: playTone(f, 600, instrument="piano") for f in song: playTone(f, 600, instrument="guitar") for f in song: playTone(f, 600, instrument="trumpet") for f in song: playTone(f, 600, instrument="violin") for f in song: playTone(f, 600, instrument="organ") Example 6: Simple piano
Program: from gturtle import * def drawPiano(): setPenColor("black") for x in range(-200, 160, 50): setPos(x, -100) for k in range(2): fd(216).rt(90).fd(50).rt(90) setPenWidth(32) for x in [-150, -100, 0, 50, 100, 200]: setPos(x, 0) fd(100) def onMousePressed(x, y): if x > -200 and x < 215 and y > -100 and y < 100: i = int((x + 200)/50) setPos(x, y) if getPixelColorStr() == "black": k = int((x + 215) / 50) f = blacktones[k] else: f = whitetones[i] playTone(f, 400, block = False) whitetones = [262, 294, 330, 349, 392, 440, 494, 524] blacktones = [0, 277, 311, 0, 360, 415, 466, 0, 555] makeTurtle(mousePressed = onMousePressed) hideTurtle() drawPiano() addStatusBar(20) setStatusText("Click a piano key to play!") |
REMEMBER... |
The Turtle can play tones using the function playTone(frequency, duration, instrument). If you want the Turtle to draw at the same time, or if you want to play several tones (chords) at the same time, use the non-blocking function playTone(frequency, duration, block = False). |
TO SOLVE BY YOURSELF |
|
Technical information:
These frequencies can be calculated. If you start from a fundamental tone (a‘ with f = 440 Hz), you obtain the frequency of the following semitone with the factor 1.05946 and the next whole tone with the factor 1.05946 * 1.05946 ≈ 1.122. (e.g. h’ = 440 * 1.122 = 494). The frequencies in the table are rounded.
Why these factors? An octave (frequency ratio c‘’ : c' = 2 : 1) is divided into twelve semitone steps.
One semitone = 1/12 octave. This corresponds to the frequency ratio = twelfth root of 2 = 1.05946.
In English notation, notes are indicated with capital letters.
c, d, e, f ... corresponds to C3, D3, E3, F3,...
c', d', e', f'... corresponds toC4, D4, E4, F4, ...
c'', d'', e'', f'', corresponds toC5, D6, E5, F5, ...