-
Notifications
You must be signed in to change notification settings - Fork 3
/
guitarherotkinter.py
executable file
·72 lines (55 loc) · 2.14 KB
/
guitarherotkinter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#-----------------------------------------------------------------------
# guitarhero.py
#-----------------------------------------------------------------------
import stdaudio
import stddrawtkinter as stddraw
import guitarstring
#-----------------------------------------------------------------------
_CONCERT_A = 440.0
_KEYBOARD = 'q2we4r5ty7u8i9op-[=zxdcfvgbnjmk,.;/\' '
#-----------------------------------------------------------------------
def main():
# Create keyboardDict, a dictionary relating each keyboard key
# to a guitar string.
keyboardDict = {}
i = 0
for key in _KEYBOARD:
factor = 2 ** ((i-24) / 12.0)
guitarString = guitarstring.GuitarString(_CONCERT_A * factor)
keyboardDict[key] = guitarString
i += 1
# pluckedGuitarStrings is the set of all guitar strings that have
# been plucked.
pluckedGuitarStrings = set()
# loopCount is used to control the frequency of calls of
# stddraw.show().
loopCount = 1023
# The main input loop.
while True:
# Call stddraw.show() occasionally to capture keyboard events.
if loopCount == 1023:
stddraw.show()
loopCount = 0
loopCount += 1
if stddraw.hasNextKeyTyped():
# Fetch the key that the user just typed.
key = stddraw.nextKeyTyped()
# Figure out which guitar string to pluck, and pluck it.
try:
guitarString = keyboardDict[key]
guitarString.pluck()
pluckedGuitarStrings.add(guitarString)
except KeyError:
pass
# Add up the samples from each plucked guitar string. Also
# advance the simulation of each plucked guitar string by
# one step.
sample = 0.0
for guitarString in pluckedGuitarStrings:
sample += guitarString.sample()
guitarString.tic()
# Play the total.
stdaudio.playSample(sample)
#-----------------------------------------------------------------------
if __name__ == '__main__':
main()