-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
249 lines (206 loc) · 6.63 KB
/
main.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# main.py
#
# Author: Xiameng Chen
# Date: 12/04/2019
# Description: This is the main program. It creates the GUI and calls functions as user selects on touchscreen
import pygame
from pygame.locals import* #for event MOUSE variables
import os
import RPi.GPIO as GPIO
import time
import play_sound
import voice_recognition
import recordandplay
GPIO.setmode(GPIO.BCM)
GPIO.setup(27,GPIO.IN,pull_up_down=GPIO.PUD_UP)
os.putenv('SDL_VIDEODRIVER','fbcon') #Display on piTFT
os.putenv('SDL_FBDEV','/dev/fb1')
os.putenv('SDL_MOUSEDRV','TSLIB') #Track mouse clicks on piTFT
os.putenv('SDL_MOUSEDEV','/dev/input/touchscreen')
def GPIO27_callback(channel):
global code_running
print ("falling edge detected on 27")
code_running = False
GPIO.add_event_detect(27,GPIO.FALLING,callback=GPIO27_callback)
# Pygame initialization
pygame.init()
pygame.mouse.set_visible(False) # Set to False when display on piTFT
WHITE=255,255,255
BLACK=0,0,0
size=width,height=320,240
screen=pygame.display.set_mode((width,height))
background=pygame.image.load("/home/pi/soundpad/background.jpg")
ai_background=pygame.image.load("/home/pi/soundpad/voice_background.jpg")
backrect=background.get_rect()
backrect2=ai_background.get_rect()
title_font=pygame.font.Font(None,30)
button_font=pygame.font.Font(None,20)
flag_main = True
flag_preset = False
flag_ai = False
flag_record = False
context = ""
# Initialize main menu
def main_menu():
screen.fill(BLACK)
screen.blit(background,backrect)
title_text={'Soundpad':(160,25)}
button_text={'Preset mode':(50,220),'AI mode':(160,220),'Record mode':(270,220)}
for my_text,text_pos in title_text.items():
text_surface=title_font.render(my_text,True,WHITE)
rect=text_surface.get_rect(center=text_pos)
screen.blit(text_surface,rect)
for my_text,text_pos in button_text.items():
text_surface=button_font.render(my_text,True,WHITE)
rect=text_surface.get_rect(center=text_pos)
screen.blit(text_surface,rect)
pygame.display.flip()
# Initialize preset mode screen
def preset_menu():
screen.fill(BLACK)
screen.blit(background,backrect)
title_text={'Preset Mode':(160,25)}
button_text={'Back':(270,220)}
for my_text,text_pos in title_text.items():
text_surface=title_font.render(my_text,True,WHITE)
rect=text_surface.get_rect(center=text_pos)
screen.blit(text_surface,rect)
for my_text,text_pos in button_text.items():
text_surface=button_font.render(my_text,True,WHITE)
rect=text_surface.get_rect(center=text_pos)
screen.blit(text_surface,rect)
pygame.display.flip()
# Initialize preset mode screen
def ai_menu(string):
screen.fill(BLACK)
screen.blit(ai_background,backrect2)
title_text={'AI Mode':(160,25)}
button_text={'Start':(50,220),'Confirm':(160,220),'Back':(270,220)}
for my_text,text_pos in title_text.items():
text_surface=title_font.render(my_text,True,WHITE)
rect=text_surface.get_rect(center=text_pos)
screen.blit(text_surface,rect)
for my_text,text_pos in button_text.items():
text_surface=button_font.render(my_text,True,WHITE)
rect=text_surface.get_rect(center=text_pos)
screen.blit(text_surface,rect)
text=button_font.render(string,True,WHITE)
textRect=text.get_rect()
textRect.center=(160,120)
#screen.blit(text_surface,rect)
screen.blit(text,textRect)
pygame.display.flip()
# Initialize record mode screen
def record_menu(string):
screen.fill(BLACK)
screen.blit(background,backrect)
title_text={'Record Mode':(160,25)}
button_text={'Start':(50,220),'Play':(120,220),'Load':(200,220),'Back':(270,220)}
for my_text,text_pos in title_text.items():
text_surface=title_font.render(my_text,True,WHITE)
rect=text_surface.get_rect(center=text_pos)
screen.blit(text_surface,rect)
for my_text,text_pos in button_text.items():
text_surface=button_font.render(my_text,True,WHITE)
rect=text_surface.get_rect(center=text_pos)
screen.blit(text_surface,rect)
text=button_font.render(string,True,WHITE)
textRect=text.get_rect()
textRect.center=(160,180)
#screen.blit(text_surface,rect)
screen.blit(text,textRect)
pygame.display.flip()
code_running = True
main_menu()
while code_running:
if flag_main:
for event in pygame.event.get():
if(event.type is MOUSEBUTTONUP):
pos=pygame.mouse.get_pos()
x,y=pos
if y>200:
if x<80: # preset mode is selected
flag_main = False
flag_preset = True
preset_menu()
# Actions to initializaing preset menu
status = play_sound.run("sounds")
if status==0:
flag_main = True
flag_preset = False
main_menu()
elif x>140 and x<180: # ai mode is selected
flag_main = False
flag_ai = True
ai_menu("")
elif x>240: # record mode is selected
flag_main = False
flag_record = True
record_menu("")
if flag_preset:
for event in pygame.event.get():
if(event.type is MOUSEBUTTONUP):
pos=pygame.mouse.get_pos()
x,y=pos
if y>200:
if x>240: # back to main menu
flag_main = True
flag_preset = False
main_menu()
if flag_ai:
for event in pygame.event.get():
if(event.type is MOUSEBUTTONUP):
pos=pygame.mouse.get_pos()
x,y=pos
if y>200:
if x<80: # start recognizing human voice
ai_menu("Start to speak")
context = voice_recognition.run() # call voice recognition
print("Requesting "+context)
ai_menu(context)
elif x>140 and x<180: # confirm the result
if context=="":
ai_menu("Please specify instrument first:)")
else:
ai_menu("Now playing " + context + "...")
status = play_sound.run(context) # loading specific sound packs
if status==0:
context = ""
flag_main = True
flag_ai = False
main_menu()
elif status==1:
ai_menu("Start to speak")
elif x>240: # back to main menu
flag_main = True
flag_ai = False
main_menu()
if flag_record:
for event in pygame.event.get():
if(event.type is MOUSEBUTTONUP):
pos=pygame.mouse.get_pos()
x,y=pos
if y>200:
if x<80: # start recording
print("Start recording...")
record_menu("Recording...")
# add recording code
recordandplay.record()
time.sleep(3.0)
record_menu("Finish")
elif x>100 and x<140: # playback
print("Playback...")
record_menu("Playing...")
# add playback code
recordandplay.play()
record_menu("Finish")
elif x>180 and x<220: # load
print("Loading to Neotrellis...")
record_menu("Loaded^_^")
# add playback code
status = play_sound.run("record")
elif x>240: # back to main menu
flag_main = True
flag_record = False
main_menu()
GPIO.cleanup()