-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatch.py
executable file
·309 lines (226 loc) · 8.4 KB
/
match.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
#!/usr/bin/env python2
import argparse
import numpy as np
import os
import os.path
from os.path import join, isfile
import soundfile as sf
import sys
outdir = join(os.path.dirname(__file__), 'output')
def __exit(error_msg):
sys.exit(os.path.basename(__file__) + ": Error: " + error_msg)
def syncronize(raw_audio, raw_sync, verbose):
"""Truncate audio and obtain index for syncing sensor data."""
sync_index = np.argmin(raw_sync)
if verbose:
print 'Syncing at index: ' + str(sync_index)
d_audio = raw_audio[sync_index:]
d_sync = raw_sync[sync_index:]
return sync_index, d_audio, d_sync
def truncate(d_audio, d_sensor, verbose):
"""Truncate audio or sensor data, resulting in them having equal length."""
sensor_l = len(d_sensor)
sensor_l64 = sensor_l * 64
d_l = len(d_audio)
if verbose:
print 'Sensor data length: ' + str(sensor_l)
print 'Sensor data length * 64: ' + str(sensor_l64)
print 'Audio data length: ' + str(d_l)
if sensor_l64 > d_l:
if verbose:
print 'Truncating sensor data...'
sensor_l = d_l / 64
sensor_l64 = sensor_l * 64
d_sensor = d_sensor[:sensor_l]
if sensor_l64 < d_l:
if verbose:
print 'Truncating audio data...'
d_audio = d_audio[:sensor_l64]
return d_audio, d_sensor
def cut_parts(cut_list, d_audio, sensor, outdir, title, verbose):
"""Cut audio into subparts as indicated by supplied cutlist."""
if verbose:
print "Cutting into parts using cutlist from file..."
names = cut_list[0]
ins = cut_list[1]
outs = cut_list[2]
if names.size > 1:
newnames = []
for i, elem in enumerate(names):
newnames.append(title + "_" + names[i])
names = np.asarray(newnames)
# ensure unique names
for elem in names:
if sum(names == elem) > 1:
indices = np.where(names == elem)[0]
count = 0
for i in indices:
count += 1
names[i] = names[i] + "_" + str(count)
else:
names = [title + "_" + str(names)]
ins = [int(ins)]
outs = [int(outs)]
cuts = zip(names, ins, outs)
return cut(cuts, d_audio, sensor, outdir, verbose)
def cut(cuts, audio, sensor, outdir, verbose):
"""Cut audio into subparts as indicated by passed list of cuts."""
# choose correct poti
data_p1 = sensor[1]
data_p2 = sensor[2]
if data_p1.mean() > data_p2.mean():
data_p = data_p1
if verbose:
print "Picked p1 for poti data."
else:
data_p = data_p2
if verbose:
print "Picked p2 for poti data."
for n, i, o in cuts:
i = int(i)
o = int(o)
d_audio = audio[i:o]
i64 = i / 64
o64 = o / 64
sensor_t = sensor[0][i64:o64]
sensor_p = data_p[i64:o64]
sensor_x = sensor[3][i64:o64]
sensor_y = sensor[4][i64:o64]
sensor_z = sensor[5][i64:o64]
truncate(d_audio, sensor_t, False)
truncate(d_audio, sensor_p, False)
truncate(d_audio, sensor_x, False)
truncate(d_audio, sensor_y, False)
truncate(d_audio, sensor_z, False)
n_audio = np.arange(0, len(sensor_t)) * 64
if verbose:
print "Exporting subpart: %d - %d" % (i, o)
name_audio = join(outdir, n + '.wav')
name_sensor = join(outdir, n + '_sensor.csv')
if not isfile(name_audio):
sf_version = int(sf.__version__.replace(".", ""))
if sf_version < 80:
sf.write(d_audio, name_audio, samplerate=fs, subtype='PCM_16')
else:
sf.write(name_audio, d_audio, samplerate=fs, subtype='PCM_16')
data = np.column_stack((sensor_t,
n_audio,
sensor_p,
sensor_x,
sensor_y,
sensor_z))
np.savetxt(name_sensor,
data,
fmt=['%3.5f',
'%d',
'%d',
'%d',
'%d',
'%d'],
header='time,n_audio,fret_pos,acc_x,acc_y,acc_z',
delimiter=',',
comments='')
return cuts
if __name__ == '__main__':
# cmd line args
parser = argparse.ArgumentParser(
description='Match audio and data files from vibrato experiment.')
parser.add_argument(dest='audio_file', type=str)
parser.add_argument(dest='sync_file', type=str)
parser.add_argument(dest='sensor_file', type=str)
parser.add_argument(dest='output_title', type=str)
parser.add_argument(dest='outdir', type=str)
parser.add_argument('-c',
dest='cutlist_file',
action='store',
type=str,
default=None,
required=True,
help='File containing cut frames.')
parser.add_argument('-v',
dest='verbose',
action='store_true',
default=False,
help='Be verbose.')
args = parser.parse_args()
# audio file: contains audio data
audio_file = args.audio_file
# sync file: contains pulse syncronizing audio and sensor data
sync_file = args.sync_file
# sensor file: contains recorded data from sensor
sensor_file = args.sensor_file
# cutlist file: contains positions of recording subparts
cutlist_file = args.cutlist_file
title = args.output_title
verbose = args.verbose
if verbose:
print "Starting matching with title: " + title
print "Input audio file: " + audio_file
print "Input sync file: " + sync_file
print "Input sensor file: " + sensor_file
print "Input cutlist file: " + cutlist_file
if not os.path.exists(audio_file):
__exit("audio_file doesn't exist.")
if not os.path.exists(sync_file):
__exit("sync_file doesn't exist.")
if not os.path.exists(sensor_file):
__exit("sensor_file doesn't exist.")
if not os.path.exists(cutlist_file):
__exit("cutlist_file doesn't exist.")
# create output directory if necessary
outdir = args.outdir
if not os.path.exists(outdir):
os.makedirs(outdir)
# read data
# audio data
raw_audio, fs = sf.read(audio_file, always_2d=False)
# sync data
raw_sync, fs = sf.read(sync_file, always_2d=False)
# sensor data
raw_sensor = np.hsplit(np.loadtxt(sensor_file), 6)
sensor_p1 = np.ravel(raw_sensor[1]) # fretboard poti 1
sensor_p2 = np.ravel(raw_sensor[2]) # fretboard poti 2
sensor_x = np.ravel(raw_sensor[3]) # acceleration sensor x
sensor_y = np.ravel(raw_sensor[4]) # acceleration sensor y
sensor_z = np.ravel(raw_sensor[5]) # acceleration sensor z
# syncronize audio and sensor according to sync pulse
sync_index, d_audio, d_sync = syncronize(raw_audio, raw_sync, verbose)
# truncate sensor or audio to obtain equal lengths
d_audio, sensor_p1 = truncate(d_audio, sensor_p1, verbose)
d_audio, sensor_p2 = truncate(d_audio, sensor_p2, False)
d_audio, sensor_x = truncate(d_audio, sensor_x, False)
d_audio, sensor_y = truncate(d_audio, sensor_y, False)
d_audio, sensor_z = truncate(d_audio, sensor_z, False)
d_sync = d_sync[:len(d_audio)]
# create time vectors
t = np.arange(0, len(d_audio))
t = t * 1.0 / fs
sensor_t = t[::64]
# check lengths
assert(len(d_audio) == len(d_sync)
== len(t)
== len(sensor_t) * 64
== len(sensor_p1) * 64
== len(sensor_p2) * 64
== len(sensor_x) * 64
== len(sensor_y) * 64
== len(sensor_z) * 64)
sensor = (sensor_t,
sensor_p1,
sensor_p2,
sensor_x,
sensor_y,
sensor_z)
# read cutlist from file
cut_list = np.loadtxt(cutlist_file, comments='#', unpack=True,
dtype={'names': ['name', 'start', 'end'],
'formats': ['|S15', np.float, np.float]})
# cut data
cut_list = cut_parts(
cut_list,
d_audio,
sensor,
outdir,
title,
verbose
)