/
test.py
275 lines (230 loc) · 8.79 KB
/
test.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
#!/usr/bin/env python
"""
Mostitch is Marsyas Ostitch.
Ostitch was an audio collager. An audio collager takes input source
audio and produces output source audio which is composed of chunks of
the input. The output is usually is meant to mimic another signal
(like a song or speech).
Originally from in_out.py
(C) 2013 Graham Percival
a quick hack to demonstrate getting data between python and
marsyas.
Copyright (C) 2013 Abram Hindle, Marsyas authors for in_out.py
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
import sys
import numpy
#import pylab
import marsyas
import marsyas_util
import pdb
import pyflann
from pyflann import *
from numpy import *
#from numpy.random import *
import cPickle
import random
#PLOT = True
PLOT = False
pyflann.set_distance_type('kl')
flann = FLANN()
topn = 20
buffsize = 512
#texture = ["Rms/rms", "AubioYin/pitcher","ZeroCrossings/zcrs" ,"Series/lspbranch" ,"Series/lpccbranch" ,"MFCC/mfcc" ,"SCF/scf" ,"Rolloff/rf" ,"Flux/flux" ,"Centroid/cntrd" ,"Series/chromaPrSeries"]
texture = ["Rms/rms", "AubioYin/pitcher","ZeroCrossings/zcrs" ,"Rolloff/rf" ,"Flux/flux" ,"Centroid/cntrd","AbsMax/abs","Energy/energy"]
#"AimGammatone/aimgamma"]
detectors = ["Fanout/detectors", texture]
grainuri = "RealvecGrainSource/real_src"
class Slice:
stats = []
rv = 0
def __init__(self, rv, stats):
self.stats = stats
self.rv = rv
def stats(self):
return self.stats
def rv(self):
return self.rv
def str(self):
return "Slice:"+",".join([str(x) for x in self.stats])
class MetricExtractor:
# override this with your source
def get_source(self):
return "SoundFileSource/src"
# override this with your filename setup
def post_network_setup(self):
return 0
#override this
def callback( self, input_net_end, stats):
return 0
def setup( self ):
series = ["Series/input", [self.get_source(), detectors]]
self.input_net = marsyas_util.create( series )
# override
self.post_network_setup()
self.notempty = self.input_net.getControl(self.get_source() + "/mrs_bool/hasData")
self.input_net_end_control = self.input_net.getControl(self.get_source() + "/mrs_realvec/processedData")
self.metrics_control = self.input_net.getControl("mrs_realvec/processedData")
def has_data(self):
return self.notempty.to_bool()
def operate(self):
self.input_net.tick()
input_net_end = self.input_net_end_control.to_realvec()
stats = self.metrics_control.to_realvec()
return self.callback( input_net_end, stats )
def readeverything(self):
while self.has_data():
self.operate()
class FileMetricExtractor(MetricExtractor):
def __init__(self, filename):
self.filename_input = filename
self.slices = []
self.setup()
def get_source(self):
return "SoundFileSource/src"
def post_network_setup( self ):
self.input_net.updControl(
"SoundFileSource/src/mrs_string/filename",
self.filename_input)
def callback( self, input_net_end, stats):
stats = [x for x in stats]
rv = input_net_end.getSubVector(0,len(input_net_end))
self.slices.append( Slice( rv, stats ) )
def get_slices( self ):
return self.slices
class StreamMetricExtractor(MetricExtractor):
def __init__(self):
self.slices = []
self.setup()
def get_source(self):
return "AudioSource/src"
def post_network_setup(self):
self.input_net.updControl("mrs_natural/inSamples", buffsize)
self.input_net.updControl("mrs_real/israte", 44100.0)
self.input_net.updControl(self.get_source() + "/mrs_bool/initAudio", marsyas.MarControlPtr.from_bool(True));
def callback( self, input_net_end, stats):
stats = [x for x in stats]
rv = input_net_end.getSubVector(0,len(input_net_end))
s = Slice( rv, stats )
#self.slices.append( s )
return s
#def get_slices( self ):
# return self.slices
def read_in_file_with_stats(filename_input):
fm = FileMetricExtractor( filename_input )
fm.readeverything()
return fm.get_slices()
def make_output():
series = ["Series/output",
["RealvecGrainSource/real_src",
"AudioSink/dest"]]
this_net = marsyas_util.create(series)
this_net.updControl("mrs_natural/inSamples", buffsize)
this_net.updControl("mrs_real/israte", 44100.0)
return this_net
def play_slices(slices, output_net_begin_control, output_net):
for play_slice in slices:
output_net_begin_control.setValue_realvec(play_slice.rv)
output_net.tick()
def load_slice( net, id, slice ):
net.updControl(grainuri + "/mrs_natural/index", id)
net.updControl(grainuri + "/mrs_natural/index", id)
data_uri = grainuri + "/mrs_realvec/data"
control = net.getControl(data_uri)
control.setValue_realvec(slice.rv)
net.updControl(grainuri + "/mrs_bool/commit",
marsyas.MarControlPtr.from_bool(True))
def hann( n, N ):
return 0.5 * (1 - cos( (2 * math.pi * n) / (N - 1)))
def hann_window( size ):
v = marsyas.realvec( size )
for i in range(0,size):
v[i] = hann( i, size)
return v
def triangle( size ):
v = marsyas.realvec( size )
half = size/2
for i in range(0,size):
v[i] = 1.0 - abs(half - i)/half
return v
def saw(size):
v = marsyas.realvec( size )
for i in range(0,size):
v[i] = i/(size)
return v
def flat(size):
v = marsyas.realvec( size )
for i in range(0,size):
v[i] = 1
return v
def main():
try:
filename_input = sys.argv[1]
except:
print "USAGE: ./mostitch.py input_filename.wav"
exit(1)
# read the slices
slices = read_in_file_with_stats( filename_input )
print(len(slices))
# get NN
dataset = array([s.stats for s in slices])
#window = triangle(buffsize)
window = hann_window(buffsize)
#window = flat(buffsize)
#flat(buffsize)#saw(buffsize)#triangle(buffsize) #flat(buffsize) #triangle(buffsize)
for slice in slices:
slice.rv *= window
#params = flann.build_index(dataset, algorithm="autotuned", target_precision=0.9, log_level = "info")
params = flann.build_index(dataset, algorithm="kdtree", target_precision=0.9, log_level = "info")
output_net = make_output()
this_net = output_net
slicecnt = 0
for slice in slices:
load_slice( output_net, slicecnt, slice )
slicecnt += 1
sme = StreamMetricExtractor()
schedule_control = output_net.getControl(
grainuri + "/mrs_realvec/schedule")
schedsize = 3 # size of a schedule
nn = schedsize
i = 0
this_net.updControl("AudioSink/dest/mrs_bool/initAudio",marsyas.MarControlPtr.from_bool(True))
while 1:
schedule = marsyas.realvec(nn * buffsize)
for j in range(0,buffsize):
schedule[j*nn+0] = j
schedule[j*nn+1] = j + (i % slicecnt)
schedule[j*nn+2] = 0.25
# this sets a schedule
output_net_begin_control = this_net.getControl("RealvecGrainSource/real_src/mrs_realvec/schedule")
output_net_begin_control.setValue_realvec(schedule)
# this commits the schedule
this_net.updControl("RealvecGrainSource/real_src/mrs_bool/schedcommit",marsyas.MarControlPtr.from_bool(True))
print "\t".join([str(x) for x in schedule])
this_net.tick()
i = i + 1
# while sme.has_data():
# # tick is done here
# new_slice = sme.operate()
# ngrains = 512
# schedule = marsyas.realvec(nn * ngrains)
# for j in range(0,ngrains):
# schedule[j*nn + 0] = j#random.randint(0,buffsize) # in how many samples should be play it
# schedule[j*nn + 1] = random.randint(1,slicecnt)
# schedule[j*nn + 2] = 0.1
# print str(schedule[j*nn + 1])
# output_net_begin_control = output_net.getControl("RealvecGrainSource/real_src/mrs_realvec/schedule")
# output_net_begin_control.setValue_realvec(schedule)
# output_net.updControl("RealvecGrainSource/real_src/mrs_bool/schedcommit",marsyas.MarControlPtr.from_bool(True))
# output_net.tick()
main()