This repository has been archived by the owner on Sep 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathpiano_menu.py
172 lines (142 loc) · 5.89 KB
/
piano_menu.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""
Copyright 2015 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import os
import pickle
import sys
import time
import keyboard
import midi
import piano_output
import piano_input
import piano_input_mock
import waterfall
def GetMidiFiles(path="./"):
return sorted(
filter(lambda filename: filename.endswith('.mid'),
os.listdir('./')),
key=lambda f: f.lower())
class Menu(object):
def __init__(self):
self.slowdown = 1.0
self.songs = GetMidiFiles()
self.current_song = 0
self.piano_display = piano_output.PianoOutput()
try:
self.piano_input_obj = piano_input.PianoInput()
except IOError:
print "Using mock input instead of usb one."
print "To install pyusb run:"
print " sudo apt-get install python libusb-1.0-0"
print " sudo pip install pyusb --pre"
self.piano_input_obj = piano_input_mock.PianoInput()
midi_file = midi.MidiFile(self.songs[self.current_song])
self.waterfall = waterfall.Waterfall(self.piano_input_obj,
self.piano_display, midi_file)
self.LoadHighScores()
def LoadHighScores(self):
try:
f = open('highscores.pickle', 'r')
self.high_scores = pickle.load(f)
except IOError:
print 'Warning: Could not open high scores file, creating new.'
self.high_scores = {}
def SaveHighScores(self):
try:
f = open('highscores.pickle', 'w')
pickle.dump(self.high_scores, f)
except IOError:
print 'Error: Failed to save high scores file highscores.pickle'
def ShowHighScore(self):
current_song_name = self.songs[self.current_song]
if current_song_name in self.high_scores:
self.piano_display.SetKeyText(
65, self.piano_display.KEYBOARD_HEIGHT + 200,
"Previous best: %.0f by %s" % self.high_scores[current_song_name])
if self.score:
self.piano_display.SetKeyText(
65, self.piano_display.KEYBOARD_HEIGHT + 150,
"Your score: %.0f" % self.score)
def CheckHighScore(self):
current_song_name = self.songs[self.current_song]
if not self.score:
return # No score, hence no new high score.
if (current_song_name in self.high_scores and
self.score <= self.high_scores[current_song_name][0]):
return # Current score is lower than high score.
self.piano_display.SetKeyText(
65, self.piano_display.KEYBOARD_HEIGHT + 100,
"New High Score! Enter your name:")
self.piano_display.Refresh()
k = keyboard.Keyboard(self.piano_input_obj, self.piano_display)
your_name = k.GetTypedString()
self.high_scores[current_song_name] = (self.score, your_name)
self.SaveHighScores()
def CreateWaterfall(self):
midi_file = midi.MidiFile(self.songs[self.current_song])
self.waterfall = waterfall.Waterfall(self.piano_input_obj,
self.piano_display, midi_file)
def MainLoop(self):
self.piano_input_obj.ClearInput()
self.score = None
while True:
self.piano_display.Clear()
self.piano_display.DrawPiano(False)
self.ShowHighScore()
self.piano_display.SetKeyText(36, 20, u"\u2212")
self.piano_display.SetKeyText(40, 20, u"\u2795")
self.piano_display.SetKeyText(38, self.piano_display.KEYBOARD_HEIGHT + 50,
" Slowdown")
self.piano_display.SetKeyText(38, self.piano_display.KEYBOARD_HEIGHT + 25,
str(self.slowdown))
self.piano_display.SetKeyText(36 + 12, 20, "<")
self.piano_display.SetKeyText(40 + 12, 20, ">")
self.piano_display.SetKeyText(38 + 12,
self.piano_display.KEYBOARD_HEIGHT + 50,
"Select Song")
self.piano_display.SetKeyText(38 + 12,
self.piano_display.KEYBOARD_HEIGHT + 25,
self.songs[self.current_song][:-4])
self.piano_display.SetKeyText(38 + 12, 20, u"\u266a")
self.piano_display.Refresh()
if self.piano_input_obj.user_input.empty():
time.sleep(0.1) # Avoid hogging the CPU when idle.
while not self.piano_input_obj.user_input.empty():
user_cmd = self.piano_input_obj.user_input.get()
if user_cmd[1] > 0:
if user_cmd[0] == 36:
self.slowdown = max(0.1, self.slowdown - 0.1)
if user_cmd[0] == 40:
self.slowdown = self.slowdown + 0.1
if user_cmd[0] == 40 + 12:
self.score = None
self.current_song = (self.current_song + 1) % len(self.songs)
self.CreateWaterfall()
if user_cmd[0] == 36 + 12:
self.score = None
self.current_song = (self.current_song + len(self.songs) - 1) % (
len(self.songs))
midi_file = midi.MidiFile(self.songs[self.current_song])
self.waterfall = waterfall.Waterfall(self.piano_input_obj,
self.piano_display, midi_file)
if user_cmd[0] == 38 + 12:
if self.waterfall.EndOfSong():
# Reload midi file, because it was destroyed during playback
self.CreateWaterfall()
self.score = self.waterfall.Continue(self.slowdown)
self.ShowHighScore()
self.CheckHighScore()
def main():
menu = Menu()
menu.MainLoop()
if __name__ == "__main__":
main()