-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGPIOtest.py
187 lines (167 loc) · 4.83 KB
/
GPIOtest.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
def Kill(pro):
try:
os.killpg(os.getpgid(pro.pid), signal.SIGTERM)
except:
do='nothing'
def Run(command):
pro = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid)
return pro
def KillandRun(command,pro=''):
try:
os.killpg(os.getpgid(pro.pid), signal.SIGTERM)
except:
do='nothing'
finally:
pro = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid)
return pro
def Loop(command):
pro = subprocess.Popen('while true; do '+command+'; done', stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid)
return pro
def CheckPro(pro):
try:
poll=pro.poll()
if poll==None:
#Still Running
return True
else:
return False
except:
do='nothing'
def Start_Record(outputfile):
recid=Run('arecordmidi -p 14:0 '+str(outputfile))
return recid
def Play(inputfile):
playid=Run('aplaymidi -p 128:0 '+str(inputfile))
return playid
def LoopPlay(inputfile):
playid=Run('aplaymidi -p 128:0 '+str(inputfile))
return playid
def Shutdown():
print('sudo shutdown -h now')
def Speed(inputfile,outputfile,factor):
newscore = inputfile.scaleOffsets(factor).scaleDurations(factor)
newscore.write('midi',outputfile)
def Length(inputfile):
mid=MidiFile(inputfile)
return mid.length
def trap(pinnum):
while True:
if GPIO.input(pinnum):
continue
else:
break
from shutil import copyfile
import os
import signal
import subprocess
import music21
import GPIO2 as GPIO
import time
from mido import MidiFile
from time import time as time
#Set song id
songid=''
#Define pin numbers
Record_pin=0
PlayPause_pin=1
LoopPlay_pin=2
IncSpeed_pin=3
DecSpeed_pin=4
ResSpeed_pin=5
Shutdown_pin=6
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD())
GPIO.setup(Record_pin, GPIO.IN())
GPIO.setup(PlayPause_pin, GPIO.IN())
GPIO.setup(LoopPlay_pin, GPIO.IN())
GPIO.setup(IncSpeed_pin, GPIO.IN())
GPIO.setup(DecSpeed_pin, GPIO.IN())
GPIO.setup(ResSpeed_pin, GPIO.IN())
GPIO.setup(Shutdown_pin, GPIO.IN())
#set State values
playing=0
recording=0
looping=0
Factor=1
playid=0
RecordingOut='output.mid'
InFile='inputfile.mid'
#Copy test file as input
copyfile('test2.mid',InFile)
#Prepare score for rescale
score = music21.converter.parse(InFile)
loop_tweak=-0.1
while True:
#Check possibilities for record press
if GPIO.input(Record_pin):
#If not recording - start
if recording==0:
recid=Start_Record(RecordingOut)
recording=1
#Trap it here till release
trap(Record_pin)
#If it is recording - stop
elif recording==1:
Kill(recid)
score = music21.converter.parse(RecordingOut)
recording=0
#Trap it here till release
trap(Record_pin)
#Possibles for play press
elif GPIO.input(PlayPause_pin):
#if not playing - start
if playing==0 and recording==0 and looping==0:
playid=Play(InFile)
playing=1
#Trap it here till release
trap(PlayPause_pin)
#if it is playing stop
elif playing==1:
Kill(playid)
playing=0
#Trap it here till release
trap(PlayPause_pin)
elif GPIO.input(IncSpeed_pin):
if playing==0 and recording==0 and looping==0:
Factor=Factor*0.9
Speed(score,InFile,Factor)
#Trap it here till release
trap(IncSpeed_pin)
elif GPIO.input(DecSpeed_pin):
if playing==0 and recording==0 and looping==0:
Factor=Factor*1.1111
Speed(score,InFile,Factor)
#Trap it here till release
trap(DecSpeed_pin)
elif GPIO.input(ResSpeed_pin):
if playing==0 and recording==0 and looping==0:
Factor=1
Speed(score,InFile,Factor)
#Trap it here till release
trap(ResSpeed_pin)
elif GPIO.input(LoopPlay_pin):
#if normal playing isn't happening
if playing==0 and recording==0:
#If it's not looping - start looping
if looping==0:
looping=1
playid=LoopPlay(InFile)
start_time=time()
#Trap till release
trap(LoopPlay_pin)
#If it's looping - stop looping
elif looping==1:
looping=0
Kill(playid)
#Trap till release
trap(LoopPlay_pin)
#If it's looping
elif looping==1:
if float(Length(InFile))<float(time()-start_time-loop_tweak):
playid=LoopPlay(InFile)
start_time=time()
#If the music has stopped - it's stopped playing
elif not CheckPro(playid):
playing=0
else:
do='nothing'