-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultitrack-naudio-pythonnet-asio-poly-wav-recorder.py
344 lines (315 loc) · 13 KB
/
multitrack-naudio-pythonnet-asio-poly-wav-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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#import pythomcom
#pythomcom.CoInitialize()
import ctypes
ctypes.windll.ole32.CoInitialize(None)
import clr #pythonnet
import sys
print(sys.path)
import pdb
import threading
import wx
#poly wav version info:
#check just one ASIO device and click the third toolbar button to record a timestamped wav to the local directory.
#USAGE: python 3+, pip install wxpython, pip install pythonnet, pip install pdb, cmd /k python.exe -i %thisscript%
#recording inputs are displayed, check boxes for which ones will be recorded, timestamped wavs recorded to same directory, close app to finish recording.
#haven't implemented the monitoring things that are reflected on the UI yet... although it isn't really necessary
#if you go into the windows audio control panel there is an checkbox to toggle to monitor called "listen to this device"
#compared to the earlier version the difference in behavior is that this supports up to 11 inputs and doesn't record anything for unchecked inputs.
#TODO: add config for stereo/mono & sampling rate.. currently just stereo and 44.1 khz
from System import Environment
name = Environment.MachineName
print(name)
from System import Console
#clr.AddReference('Microsoft.VisualStudio.Tools.Office.Excel.HostAdapter.v10.0.dll')
clr.AddReference('NAudio') #ildasm used on NAudio.dll showed this was the namespace
import NAudio as NAudio
from System import EventHandler, EventArgs
#https://stackoverflow.com/questions/52612651/pythonnet-delegate-method-with-generics-not-being-called
########
class FileMgr(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self,parent,569,title,(50,50),(800,300))
self.sb = self.CreateStatusBar()
tb = self.CreateToolBar(wx.TB_HORIZONTAL | wx.NO_BORDER | wx.TB_FLAT | wx.TB_TEXT)
bt1 = tb.AddTool(701,"Rec",wx.Bitmap("icon.png"))
bt2 = tb.AddTool(702,"PDB",wx.Bitmap("icon.png"))
bt3 = tb.AddTool(703,"DBG",wx.Bitmap("icon.png"))
self.Bind(wx.EVT_TOOL,gorecordA,bt1)
self.Bind(wx.EVT_TOOL,gorecordA,bt2)
self.Bind(wx.EVT_TOOL,mytest2,bt3)
tb.Realize()
self.topsplitter = wx.SplitterWindow(self,808,pos=wx.Point(0,0),size=wx.Size(400,400),style=wx.SP_BORDER,name="TopSplitter")
self.AudioChecker = AudioCheckList(self.topsplitter, 777)
self.recdevct = self.AudioChecker.Count
#self.MonitorChoice = MonitorChoice(self.topsplitter, 888)
self.rightside = MonitorPanel(self.topsplitter,999,self.recdevct)
#self.splitter = wx.SplitterWindow(self.topsplitter, ID_SPLITTER,pos=wx.Point(0,0),size=wx.Size(400,400),style=wx.SP_BORDER,name="Splitter")
self.topsplitter.SplitVertically(self.AudioChecker,self.rightside)
def OnExit(self,e):
self.Close(True)
class AudioCheckList(wx.CheckListBox):
def __init__(self, parent, id):
devicecount = NAudio.Wave.WaveIn.DeviceCount
devicelist = []
for n in range(devicecount):
devicelist.append(NAudio.Wave.WaveIn.GetCapabilities(n).ProductName)
wx.CheckListBox.__init__(self, parent, id,(0,0),(50,50),devicelist)
#https://docs.wxpython.org/gallery.html shows most widgets
#class ASIOChoice(wx.Choice):
#lol
class ASIOChoice(wx.CheckListBox):
def __init__(self, parent, id, pos, size):
global availables
availables = NAudio.Wave.AsioOut.GetDriverNames()
#global asiolist
global asiolist
asiolist = []
for n in range(10):
try:
asiolist.append(availables.Get(n))
except:
pass
print('asiolist?')
print(asiolist)
wx.CheckListBox.__init__(self, parent, id, pos,size, asiolist)
class MonitorChoice(wx.Choice):
#def __init__(self,parent,id):
def __init__(self,parent,id,pos,size):
devicecount = NAudio.Wave.WaveOut.DeviceCount
devicelist = []
for n in range(devicecount):
devicelist.append(NAudio.Wave.WaveOut.GetCapabilities(n).ProductName)
#wx.Choice.__init__(self, parent, id, (0,0), (50,50), devicelist)
wx.Choice.__init__(self, parent, id, pos, size, devicelist)
class MonitorPanel(wx.Panel):
def __init__(self,parent,id, countt):
wx.Panel.__init__(self,parent,id, (0,0), (50,50), 0, "Monitor-Panel")
for xx in range(countt):
globals()["mon" + str(xx)] = MonitorChoice(self, 1200 + xx, (0+(xx*5),0+(xx*23)), (120,23))
for xx in range(countt):
globals()["monbt" + str(xx)] = wx.CheckBox(self,1400 + xx, "mon"+str(xx), (127+(xx*5),0+(xx*23)), (70,23))
global asiochoice
asiochoice = ASIOChoice(self, 5678,(220,0),(200,200))
########
def gorecordA(shelf):
devicecount = NAudio.Wave.WaveIn.DeviceCount
devicelist = []
for n in range(devicecount):
devicelist.append(NAudio.Wave.WaveIn.GetCapabilities(n).ProductName)
print(devicelist)
traceyesno = 0
if traceyesno == 0:
pass
elif traceyesno == 1:
pdb.set_trace()
checkedlist = filething.AudioChecker.GetChecked()
print(checkedlist)
#pdb.set_trace()
#filething.rightside.
#for xx in range(devicecount):
#global globals()["waveIn" + str(xx)]
## replacing range devicecount with for xx in checkedlist:
for xx in checkedlist:
globals()["waveIn" + str(xx)] = NAudio.Wave.WaveIn()
for xx in checkedlist:
globals()["waveIn" + str(xx)].DeviceNumber = xx
##global waveIn
##global waveIn2
##global waveIn3
###waveIn = NAudio.Wave.WaveIn()
###waveIn2 = NAudio.Wave.WaveIn()
###waveIn3 = NAudio.Wave.WaveIn()
####waveIn.DeviceNumber = 0
####waveIn2.DeviceNumber = 1
####waveIn3.DeviceNumber = 2
fourfour = 44100
foureight = 48000
channels = 2
for xx in checkedlist:
globals()["waveIn" + str(xx)].WaveFormat = NAudio.Wave.WaveFormat(fourfour,channels)
#####waveIn.WaveFormat = NAudio.Wave.WaveFormat(fourfour,channels)
#####waveIn2.WaveFormat = NAudio.Wave.WaveFormat(fourfour,channels)
#####waveIn3.WaveFormat = NAudio.Wave.WaveFormat(fourfour,channels)
import datetime
timestampz = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
for xx in checkedlist:
globals()["writer" + str(xx)] = NAudio.Wave.WaveFileWriter(timestampz + "." + str(xx) + '.wav', globals()["waveIn" + str(xx)].WaveFormat)
##writer1 = NAudio.Wave.WaveFileWriter(timestampz + ".1.wav", waveIn.WaveFormat)
##writer2 = NAudio.Wave.WaveFileWriter(timestampz + ".2.wav", waveIn2.WaveFormat)
##writer3 = NAudio.Wave.WaveFileWriter(timestampz + ".3.wav", waveIn3.WaveFormat)
def wave0write(sender, e):
if 1 == 1:
writer0.WriteData(e.Buffer,0,e.BytesRecorded)
writer0.Flush()
def wave1write(sender, e):
if 1 == 1:
writer1.WriteData(e.Buffer,0,e.BytesRecorded)
writer1.Flush()
def wave2write(sender, e):
if 1 == 1:
writer2.WriteData(e.Buffer,0,e.BytesRecorded)
writer2.Flush()
def wave3write(sender, e):
if 1 == 1:
writer3.WriteData(e.Buffer,0,e.BytesRecorded)
writer3.Flush()
def wave4write(sender, e):
if 1 == 1:
writer4.WriteData(e.Buffer,0,e.BytesRecorded)
writer4.Flush()
def wave5write(sender, e):
if 1 == 1:
writer5.WriteData(e.Buffer,0,e.BytesRecorded)
writer5.Flush()
def wave6write(sender, e):
if 1 == 1:
writer6.WriteData(e.Buffer,0,e.BytesRecorded)
writer6.Flush()
def wave7write(sender, e):
if 1 == 1:
writer7.WriteData(e.Buffer,0,e.BytesRecorded)
writer7.Flush()
def wave8write(sender, e):
if 1 == 1:
writer8.WriteData(e.Buffer,0,e.BytesRecorded)
writer8.Flush()
def wave9write(sender, e):
if 1 == 1:
writer9.WriteData(e.Buffer,0,e.BytesRecorded)
writer9.Flush()
def wave10write(sender, e):
if 1 == 1:
writer10.WriteData(e.Buffer,0,e.BytesRecorded)
writer10.Flush()
#for xx in range(devicecount):
#globals()["waveIn" + str(xx)].DataAvailable += locals()["wave"+str(xx)+"write"]
##for xx in range(devicecount):
'''
if 'waveIn0' in globals():
waveIn0.DataAvailable += wave0write
waveIn0.StartRecording()
'''
if 0 in checkedlist:
waveIn0.DataAvailable += wave0write
waveIn0.StartRecording()
if 1 in checkedlist:
waveIn1.DataAvailable += wave1write
waveIn1.StartRecording()
if 2 in checkedlist:
waveIn2.DataAvailable += wave2write
waveIn2.StartRecording()
if 3 in checkedlist:
waveIn3.DataAvailable += wave3write
waveIn3.StartRecording()
if 4 in checkedlist:
waveIn4.DataAvailable += wave4write
waveIn4.StartRecording()
if 5 in checkedlist:
waveIn5.DataAvailable += wave5write
waveIn5.StartRecording()
if 6 in checkedlist:
waveIn6.DataAvailable += wave6write
waveIn6.StartRecording()
if 7 in checkedlist:
waveIn7.DataAvailable += wave7write
waveIn7.StartRecording()
if 8 in checkedlist:
waveIn8.DataAvailable += wave8write
waveIn8.StartRecording()
if 9 in checkedlist:
waveIn9.DataAvailable += wave9write
waveIn9.StartRecording()
if 10 in checkedlist:
waveIn10.DataAvailable += wave10write
waveIn10.StartRecording()
#waveIn.DataAvailable += wave1write
#waveIn2.DataAvailable += wave2write
#waveIn3.DataAvailable += wave3write
#waveIn.StartRecording()
#waveIn2.StartRecording()
#waveIn3.StartRecording()
filething.sb.SetStatusText("recording started")
'''
def gorecord(shelf): #unused. for reference.
devicecount = NAudio.Wave.WaveIn.DeviceCount
devicelist = []
for n in range(devicecount):
devicelist.append(NAudio.Wave.WaveIn.GetCapabilities(n).ProductName)
print(devicelist)
waveIn = NAudio.Wave.WaveIn()
waveIn2 = NAudio.Wave.WaveIn()
waveIn3 = NAudio.Wave.WaveIn()
waveIn.DeviceNumber = 0
waveIn2.DeviceNumber = 1
waveIn3.DeviceNumber = 2
fourfour = 44100
foureight = 48000
channels = 2
waveIn.WaveFormat = NAudio.Wave.WaveFormat(fourfour,channels)
waveIn2.WaveFormat = NAudio.Wave.WaveFormat(fourfour,channels)
waveIn3.WaveFormat = NAudio.Wave.WaveFormat(fourfour,channels)
import datetime
timestampz = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
writer1 = NAudio.Wave.WaveFileWriter(timestampz + ".1.wav", waveIn.WaveFormat)
writer2 = NAudio.Wave.WaveFileWriter(timestampz + ".2.wav", waveIn2.WaveFormat)
writer3 = NAudio.Wave.WaveFileWriter(timestampz + ".3.wav", waveIn3.WaveFormat)
def wave1write(sender, e):
if 1 == 1:
#print('ifone')
##print(e.Buffer)
##print(e.BytesRecorded)
writer1.WriteData(e.Buffer,0,e.BytesRecorded)
writer1.Flush()
def wave2write(sender, e):
#print('okay2')
if 1 == 1:
writer2.WriteData(e.Buffer,0,e.BytesRecorded)
writer2.Flush()
def wave3write(sender, e):
#print('okay3')
if 1 == 1:
writer3.WriteData(e.Buffer,0,e.BytesRecorded)
writer3.Flush()
waveIn.DataAvailable += wave1write
waveIn2.DataAvailable += wave2write
waveIn3.DataAvailable += wave3write
waveIn.StartRecording()
waveIn2.StartRecording()
waveIn3.StartRecording()
'''
def debog(shelf):
pdb.set_trace()
#https://markheath.net/category/naudio
def mytest(shelf):
filething.sb.SetStatusText(str(filething.AudioChecker.GetCheckedItems()))
def asio1write(sender, e):
samples = e.GetAsInterleavedSamples()
asio1writer.WriteSamples(samples,0,samples.Length)
asio1writer.Flush()
def mytest2(shelf):
filething.sb.SetStatusText(str(asiochoice.GetCheckedStrings() ) )
global asiodev
asiodev = NAudio.Wave.AsioOut(asiochoice.GetCheckedStrings()[0])
#stathreadattribute needed > https://github.com/pythonnet/pythonnet/issues/108
asiodev_channelcount = asiodev.DriverInputChannelCount #6 for Zoom H6
#https://github.com/naudio/NAudio/blob/master/Docs/AsioRecording.md
asiodev.InitRecordAndPlayback(None, asiodev_channelcount, 44100)
import datetime
timestampz = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
#asiowaveformat = NAudio.Wave.WaveFormat(44100,24,asiodev_channelcount)
asiowaveformat = NAudio.Wave.WaveFormat(44100,16,asiodev_channelcount)
global asio1writer
asio1writer = NAudio.Wave.WaveFileWriter(timestampz + "." + 'poly' + '.wav', asiowaveformat)
asiodev.AudioAvailable += asio1write
asiodev.Play()
#pdb.set_trace()
#https://markheath.net/post/how-to-record-and-play-audio-at-same
#https://archive.codeplex.com/?p=naudio
#https://github.com/naudio/NAudio/wiki
app=wx.App(0)
filething = FileMgr(None,420,'Simultaneous Multi Wav Recorder')
filething.Show(True)
##pdb.set_trace()
app.MainLoop()
#http://pythonnet.github.io/
#https://channel9.msdn.com/coding4fun/articles/NET-Voice-Recorder