-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoscolarecorder.py
230 lines (206 loc) · 6.49 KB
/
oscolarecorder.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
#!/usr/bin/python
from OSC import OSCServer,OSCClient, OSCMessage, ThreadingOSCServer
import sys
from time import sleep
import time
import types
import os
import subprocess
import threading
from threading import Thread
from os import listdir
from os.path import isfile, join
from random import shuffle
from random import sample
# TODO: add code to create "/home/pi/myoscrecs" folder if not present.
os.chdir('/home/pi/myoscrecs')
time.sleep(5)
# TODO: Add code to retreive ip address automatically (ethernet IP prioritized, wifi IP if ethernet not connected)
server = ThreadingOSCServer( ("10.1.1.143", 7002) )# the ip adress of your pi/device goes here and the incoming OSC port you would like to use, i used 7002 for the port - the incoming port should be matched on your TouchOSC / OSC app
client = OSCClient()
#def handle_timeout(self):
# print ("I'm IDLE")
#This here is just to do something while the script recieves no information....
#server.handle_timeout = types.MethodType(handle_timeout, server)
# Record Global Var
r = None
# Playback Global Var
p = None
# Stop Recording Global Var
sr = 0
#Stop Playback Global Var
sp = 0
#Set to loop Global Var
loop = 1
#Duration Time Global Var
loopd = 10
#Set to Shuffle Global Var
shuffleloop = 1
#Function to record DMX input using ola_recorder program and save into banks numbered 1-XX - Recording starts and stops when triggered via OSC.
def record(path, tags, args, source):
split = path.split("/1/toggle")
x = split.pop()
state=int(args[0])
print "Record",x
global sr
global r
global sp
global p
if state == 1:
if sr == 0:
if sp == 0:
r = subprocess.Popen(['ola_recorder', '-r', x, '-u 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20'], stdout=subprocess.PIPE)
sr = 1
else:
p.kill()
r = subprocess.Popen(['ola_recorder', '-r', x, '-u 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20'], stdout=subprocess.PIPE)
sr = 1
sp = 0
else:
if sp == 0:
r.kill()
r = subprocess.Popen(['ola_recorder', '-r', x, '-u 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20'], stdout=subprocess.PIPE)
sr = 1
else:
p.kill()
r = subprocess.Popen(['ola_recorder', '-r', x, '-u 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20'], stdout=subprocess.PIPE)
sr = 1
sp = 0
else:
if sr == 0:
None
else:
r.kill()
sr = 0
#Function to Playback recorded DMX clips using ola_recorder program - Playback starts and stops when triggered via OSC.
def playback(path, tags, args, source):
split = path.split("/2/push")
y = split.pop()
state=int(args[0])
print "Playback:",y
global sp
global p
global sr
global r
if state == 1:
if sp == 0:
if sr == 0:
p = subprocess.Popen(['ola_recorder', '-p', y, '-i 0'], stdout=subprocess.PIPE)
sp = 1
else:
r.kill()
p = subprocess.Popen(['ola_recorder', '-p', y, '-i 0'], stdout=subprocess.PIPE)
sp = 1
sr = 0
else:
if sr == 0:
p.kill()
p = subprocess.Popen(['ola_recorder', '-p', y, '-i 0'], stdout=subprocess.PIPE)
sp = 1
else:
p.kill()
r.kill()
p = subprocess.Popen(['ola_recorder', '-p', y, '-i 0'], stdout=subprocess.PIPE)
sp = 1
sr = 0
# Function to kill all playback
def stopallplay(path, tags, args, source):
state=int(args[0])
print "Stop All Playback:", state;
global sp
global p
if state == 1:
p.kill()
sp = 0
#Function to kill all recording
def stopallrec(path, tags, args, source):
state=int(args[0])
print "Stop All Recording:", state;
global sr
global r
if state == 1:
r.kill()
sr = 0
#Function to set loop duration
def loopduration(path, tags, args, source):
global loopd
loopd=int(args[0])
# print "Loop Duration", loopd;
#Function to play all recorded clips in sequence
def playall(path, tags, args, source):
global loop
state = int(args[0])
loop=state
if loop == 1:
playallloop()
#Function to shuffle order of clips playing back
def shuffler(path, tags, args, source):
global shuffleloop
state = int(args[0])
shuffleloop=state
#if shuffle == 1:
#Function to loop single file
def playallloop():
global sp
global p
global sr
global r
global loop
global loopd
global shuffleloop
while loop == 1:
if loop == 0:
p.kill()
break
print "Play on Loop";
file_list = os.listdir(r"/home/pi/myoscrecs")
#file_list_sorted = sorted([file_list, key=int)
file_shuffle = sample(file_list, len(file_list))
if shuffleloop == 0:
files = sorted(file_list)
else:
files = file_shuffle
for i in files:
if loop == 0:
p.kill()
break
#i = ""
#i = str(x)
print "Loop Duration", loopd;
if sp == 0:
if sr == 0:
p = subprocess.Popen(['ola_recorder', '-p', i, '-i 0'], stdout=subprocess.PIPE)
sp = 1
else:
r.kill()
p = subprocess.Popen(['ola_recorder', '-p', i, '-i 0'], stdout=subprocess.PIPE)
sp = 1
sr = 0
else:
if sr == 0:
p.kill()
p = subprocess.Popen(['ola_recorder', '-p', i, '-i 0'], stdout=subprocess.PIPE)
sp = 1
else:
p.kill()
r.kill()
p = subprocess.Popen(['ola_recorder', '-p', i, '-i 0'], stdout=subprocess.PIPE)
sp = 1
sr = 0
time.sleep(loopd)
#OSC Client message handling -You can increase the amount of record and playback banks by increasing the number from 70 to whatever in the below lines.
for x in range(1,70):
server.addMsgHandler("/1/"+"toggle"+`x`, record)
for y in range(1,70):
server.addMsgHandler("/2/"+"push"+`y`, playback)
server.addMsgHandler("/3/stopallplay", stopallplay)
server.addMsgHandler("/3/stopallrec", stopallrec)
server.addMsgHandler("/3/playall", playall)
server.addMsgHandler("/3/loopduration", loopduration)
server.addMsgHandler("/3/shuffle", shuffler)
#The way that the MSG Handlers work is by taking the values from set accessory, then it puts them into a function
#The function then takes the values and separates them according to their class (args, source, path, and tags)
while True:
server.handle_request()
server.close()
#This will kill the server when the program ends