-
Notifications
You must be signed in to change notification settings - Fork 1
/
recorder.py
324 lines (254 loc) · 7.22 KB
/
recorder.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import sys
import time
import mido
import pyaudio
import wave
import os
from pathlib import Path
#
# initialize midi
#
midi_portname = "IAC"
#midi_portname = "K-Mix Control Surface"
note_rec_button = 84
#note_rec_button = 26
#
# initialize soundcard
#
#soundcard_name = "USB Advanced Audio Device"
soundcard_name = "Soundflower (64ch)"
#soundcard_name = "K-Mix"
channels = 8
#channels = 2
#
# initialize folder variables
#
waves_path = "waves/"
sess_path = ""
session_id = 1
rec_counter = 1
#
# initialize audio stream variables
#
pa = pyaudio.PyAudio()
stream = None
wavefile = None
#
# initialize state variables
#
is_recording = False
midi_monitor = False
#
# Make sure waves/ folder exists
#
def ensure_waves_folder():
Path(waves_path).mkdir(parents=True, exist_ok=True)
#
# Create waves/001/ ... folder every time the script is started,
# reuse last folder if it is still empty
#
def create_session_folder():
global sess_path, waves_path, session_id
# read only directories (sessions folders)
sessions = next(os.walk(waves_path))[1]
# filter out hidden directories
sessions = filter(lambda x: not x.startswith("."), sessions)
# sort directories
sessions = sorted(sessions)
# initialize
last_session = 1
last_session_contents = []
# grab last session and contents
if(len(sessions) > 0):
last_session = int(sessions[-1])
# retrieve contents to check for emptyness
last_session_contents = os.listdir(waves_path + sessions[-1])
# reuse last session folder if it was empty
session_id = last_session
# if not empty increase by 1
if (len(last_session_contents) > 0):
session_id += 1
# turn into zero padded string of 3 digits
session_id = f'{session_id:03}'
# save session path
sess_path = waves_path + session_id + "/"
print ("Current session directory: ")
print ("- " + sess_path + "\n")
#
# Switch rec light on K-Mix
#
def update_rec_led(outport):
return
# build midi message to send to switch rec LED
message = mido.Message('note_on')
message.velocity = 127
if not is_recording:
message = mido.Message('note_off')
message.velocity = 0
message.note = note_rec_button
# send rec LED midi message
outport.send(message)
#
# When record button was pressed
#
def on_rec_pressed(outport):
global is_recording, wavefile
# toggle rec state
is_recording = not is_recording
# print rec state
print("Rec " + ("On" if is_recording else "Off"))
# update LED
update_rec_led(outport)
if is_recording:
# prepare wave file
wavefile = prepare_file()
# grab soundcard and start recording
device_index = select_soundcard(soundcard_name)
# abort if device not found
if (device_index == -1):
print("Device '" + soundcard_name + "' not found.")
# wait a second and switch LED back off
time.sleep(1)
is_recording = False
update_rec_led(outport)
return
# start to record if found
start_recording(device_index)
else:
stop_recording()
#
# handle incoming midi messages
#
def on_midi_message(message, outport):
# mido monitor mode - print all midi messages
if (midi_monitor):
print("Received {}".format(message))
# filter out cc messages n stuff
if message.type != "note_on" and message.type != "note_off":
return
# identify rec button presses
if message.note == note_rec_button:
# execute record switching on press down
if message.type == "note_on":
on_rec_pressed(outport)
# just update LED again because not off will disable it on the device
if message.type == "note_off":
update_rec_led(outport)
# letting you know in case you forgot to press shift-record ;-)
if message.note == 95:
print("Gotta move to midi mode!")
#
# print all midi devices
#
def print_midi_devices():
print("Midi devices:")
devices = mido.get_output_names()
for device in devices:
print("- " + device)
print("")
#
# keep trying to connect to midi device and pass messages to listener
#
def midi_loop():
global midi_portname
try:
port_names = mido.get_output_names()
# iterate midi ports
for port_name in port_names:
# if port name contains string that was configured, use it
if midi_portname in port_name:
midi_portname = port_name
# setup output midi port
outport = mido.open_output(midi_portname)
# using 'with' will execute 'close' on the automatically
with mido.open_input(midi_portname) as inport:
print("Using {}".format(inport))
while True:
for message in inport.iter_pending():
on_midi_message(message, outport)
# print('Doing something else for a while...')
time.sleep(0.5)
except OSError:
print("Couldnt find MIDI device (" + midi_portname + "). Retrying..")
time.sleep(2)
#
# print all soundcards
#
def print_soundcards():
print("Soundcards:")
for i in range(pa.get_device_count()):
print("- " + pa.get_device_info_by_index(i)['name'])
print("")
#
# return index of given sound card name
#
def select_soundcard(device_name):
for i in range(pa.get_device_count()):
if device_name in pa.get_device_info_by_index(i)['name']:
return i
else:
return -1
#
# audio stream callback function when recording
#
def audio_callback(in_data, frame_count, time_info, status):
global wavefile
wavefile.writeframes(in_data)
return in_data, pyaudio.paContinue
#
# Start stream for recording
#
def start_recording(device_index):
global channels, stream
# possible formats paFloat32,paInt32,paInt16,paInt24,paInt8,paUInt8
# Use a stream with a callback in non-blocking mode
stream = pa.open(format=pyaudio.paInt16, channels=channels, rate=44100, input=True, frames_per_buffer=4096, stream_callback=audio_callback, input_device_index=device_index)
stream.start_stream()
#
# Stop recording stream
#
def stop_recording():
global stream
stream.stop_stream()
stream.close()
#
# Prepare a new multiwave file for recording
#
def prepare_file():
global channels, rec_counter, session_id, sess_path
# ensure session directory
Path(sess_path).mkdir(parents=True, exist_ok=True)
fname = "wavedeck-" + session_id + "-" + f'{rec_counter:03}' + ".wav"
wavefile = wave.open(sess_path + fname, 'wb')
wavefile.setnchannels(channels)
wavefile.setsampwidth(pa.get_sample_size(pyaudio.paInt16))
wavefile.setframerate(44100)
rec_counter += 1
return wavefile
#
# software shutdown routine
#
def shutdown():
is_recording = False
if stream:
stream.close()
if pa:
pa.terminate()
if wavefile:
wavefile.close()
print("\nGoodbye <3")
#
# main execution routine
#
try:
print("Welcome to wavedeck recorder! \n")
print_midi_devices()
print_soundcards()
ensure_waves_folder()
create_session_folder()
while True:
midi_loop()
except KeyboardInterrupt:
pass
finally:
shutdown()