forked from ucbvislab/speecheditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
605 lines (458 loc) · 19.9 KB
/
app.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
try:
import simplejson as json
except:
import json
import urllib
import subprocess
import os
import glob
from mutagen.id3 import ID3
import shutil
import sys
sys.path.append("/home/ubuntu/speecheditor")
sys.path.append("/var/www/html/srubin/speecheditor")
import numpy as N
import click
import reauthor_speech
import duplicate_lines
from music_remix.music_remix import MusicGraph
from music_remix.novelty_simple import novelty
from music_remix.changepoint_paths import best_changepoint_path
from cubic_spline import MonotonicCubicSpline
from radiotool.composer import\
Track, Song, Speech, Composition, Segment, RawVolume
try:
from app_path import APP_PATH
except:
APP_PATH = ''
from flask import Flask, request, make_response, jsonify, Response, render_template
from werkzeug import secure_filename
app = Flask(__name__)
app.debug = True
# load the app for music browsing
@app.route('/')
def index():
return render_template(
'index.html',
music_browser=app.config["MUSIC_BROWSER"],
speechtracks=app.config["SPEECH_TRACKS"])
@app.route('/reauthor', methods=['POST'])
def reauthor():
if request.method == 'POST':
post_data = urllib.unquote(request.data)
dat = json.loads(post_data)
tracks = dat["exportedTimeline"]
result = {}
comp = Composition(channels=1)
print "Start: comp segments", len(comp.segments)
speech_is_done = False
json.dump(dat["usageData"],
open(APP_PATH + "static/tmp/" + dat["outfile"] + '-usage.json', 'w'),
indent=4)
for t in tracks:
if t["waveformClass"] == "textAlignedWaveform":
# TODO: Fix this hack! This is to ensure that we only generate
# the speech track once. We need to do this because we're
# sending the speech twice, once with each interview
# track waveform.
if speech_is_done:
continue
speech_is_done = True
score_start = t["scoreStart"]
with open(APP_PATH + 'static/speechtracks/' + dat["speechText"], 'r') as f:
af = json.loads(f.read())["words"]
ef = dat["speechReauthor"]["words"]
json.dump(dat["speechReauthor"],
open(APP_PATH + "static/tmp/" + dat["outfile"] + '.json', 'w'),
indent=4)
# crossfades = True
# if "crossfades" in dat:
# crossfades = dat["crossfades"]
args = {
"cut_to_zc": True,
"samplerate": dat["speechSampleRate"],
"tracks_and_segments": True,
"score_start": score_start,
"crossfades": True,
"render_from_all_tracks": False
}
speech_audio_path = APP_PATH + 'static/speechtracks/' + dat["speechAudio"]
if os.path.splitext(speech_audio_path)[-1] == ".mp3":
wav_speech_audio_path = os.path.splitext(speech_audio_path)[0] + ".wav"
if os.path.exists(wav_speech_audio_path):
speech_audio_path = wav_speech_audio_path
print "# Rebuilding speech"
result = reauthor_speech.rebuild_audio(
speech_audio_path, af, ef, **args)
print "# Done rebuilding speech"
comp.add_tracks(result["tracks"])
comp.add_segments(result["segments"])
elif t["waveformClass"] == "musicWaveform":
# handle music authored per beat
starts = t["extra"]["starts"]
durs = t["extra"]["durations"]
dists = t["extra"]["distances"]
if "globalVolume" in t:
global_vol = t["globalVolume"]
else:
global_vol = 1.0
vx = N.array(t["extra"]["volume"]["x"])
vy = N.array(t["extra"]["volume"]["y"])
score_start = t["scoreStart"]
filename = APP_PATH + "static/" + t["filename"]
wav_fn = filename
if filename.lower().endswith('.mp3'):
wav_fn = ".".join(filename.split('.')[:-1]) + ".wav"
if not os.path.isfile(wav_fn):
subprocess.call('lame --decode "%s"'
% filename, shell=True)
track = Track(wav_fn, t["name"])
comp.add_track(track)
current_loc = float(score_start)
# create the spline interpolator
vx = vx / 1000.0 * track.samplerate
# cdf = MonotonicCubicSpline(vx, vy)
segments = []
cf_durations = []
seg_start = starts[0]
seg_start_loc = current_loc
# with open ("tmp-starts", 'w') as tf:
# for i, start in enumerate(starts):
# tf.write('%s,%s,%s\n' % (start, dists[i], durs[i]))
for i, start in enumerate(starts):
if i == 0 or dists[i - 1] == 0:
dur = durs[i]
current_loc += dur
else:
seg = Segment(track, seg_start_loc, seg_start,
current_loc - seg_start_loc)
comp.add_segment(seg)
segments.append(seg)
# track = Track(wav_fn, t["name"])
# comp.add_track(track)
dur = durs[i]
cf_durations.append(dur)
seg_start_loc = current_loc
seg_start = start
current_loc += dur
print "cf_durations", cf_durations
last_seg = Segment(track, seg_start_loc, seg_start,
current_loc - seg_start_loc)
comp.add_segment(last_seg)
segments.append(last_seg)
all_segs = []
# no repeated values
if vx[0] == vx[1]:
vx = vx[1:]
vy = vy[1:]
for i, seg in enumerate(segments[:-1]):
rawseg = comp.cross_fade(seg, segments[i + 1], cf_durations[i])
all_segs.extend([seg, rawseg])
all_segs.append(segments[-1])
first_loc = all_segs[0].comp_location
vx[-1] = all_segs[-1].comp_location -\
first_loc + all_segs[-1].duration
cdf = MonotonicCubicSpline(vx, vy)
for seg in all_segs:
vol_frames = N.empty(seg.duration)
samplex = N.arange(seg.comp_location - first_loc,
seg.comp_location - first_loc + seg.duration,
10000)
sampley = N.array([cdf.interpolate(x) for x in samplex])
lasty = cdf.interpolate(
seg.comp_location - first_loc + seg.duration)
for i, sy in enumerate(sampley):
# print i, sy
if i != len(samplex) - 1:
vol_frames[i * 10000:(i + 1) * 10000] =\
N.linspace(sy, sampley[i + 1], num=10000)
else:
vol_frames[i * 10000:] =\
N.linspace(sy, lasty,
num=seg.duration - i * 10000)
vol_frames *= global_vol
vol = RawVolume(seg, vol_frames)
comp.add_dynamic(vol)
elif t["waveformClass"] == "waveform":
score_start = t["scoreStart"]
track_start = t["wfStart"]
duration = t["duration"]
filename = APP_PATH + "static/speechtracks/" + t["filename"]
wav_fn = filename
if filename.lower().endswith('.mp3'):
wav_fn = ".".join(filename.split('.')[:-1]) + ".wav"
if not os.path.isfile(wav_fn):
subprocess.call('lame --decode "%s"'
% filename, shell=True)
track = Track(wav_fn, t["name"])
seg = Segment(track, score_start, track_start, duration)
comp.add_track(track)
comp.add_segment(seg)
print "\n\n### SEGMENT COUNT", len(comp.segments), "\n\n"
comp.export(
adjust_dynamics=False,
filename=APP_PATH + "static/tmp/" + dat["outfile"],
channels=1,
filetype='wav',
samplerate=result["samplerate"],
separate_tracks=False)
subprocess.call('rm ' + APP_PATH + 'static/tmp/' +
dat["outfile"] + '.mp3', shell=True)
subprocess.call('lame --quiet -q 5 ' + APP_PATH + 'static/tmp/'
+ dat["outfile"] + '.wav', shell=True)
# get the new wav2json data, maybe
subprocess.call('rm ' + APP_PATH + 'static/tmp/' +
dat["outfile"] + '.wav', shell=True)
return jsonify(url='static/tmp/' + dat["outfile"] + '.mp3',
timing=result["timing"])
@app.route('/ping')
def ping():
return str(sys.path)
@app.route('/download/<name>')
def download(name):
# should investigate flask's send_file
resp = make_response(
open(APP_PATH + 'static/tmp/' + name + '.mp3', 'r').read())
resp.headers['Content-Type'] = 'audio/mpeg'
resp.headers['Pragma'] = 'public'
resp.headers['Expires'] = '0'
resp.headers['Cache-Control'] = 'must-revalidat, post-check=0, pre-check=0'
resp.headers['Cache-Control'] = 'public'
resp.headers['Content-Description'] = 'File Transfer'
resp.headers['Content-Disposition'] =\
'attachment; filename=' + name + '.mp3'
resp.headers['Content-Transfer-Encoding'] = 'binary'
resp.headers['Content-Length'] =\
os.stat(APP_PATH + 'static/tmp/' + name + '.mp3').st_size
return resp
@app.route('/dupes', methods=['POST'])
def dupes():
if request.method == 'POST':
post_data = urllib.unquote(request.data)
dat = json.loads(post_data)
with open(APP_PATH + 'static/speechtracks/' + dat["speechText"], 'r') as f:
af = json.load(f)["words"]
return Response(json.dumps(duplicate_lines.get_dupes(af)),
mimetype="application/json")
return
@app.route('/changepoints/<song_name>')
def find_change_points(song_name):
# song_name = secure_filename(urllib.unquote(song_name))
song_name = urllib.unquote(song_name)
wav_fn = APP_PATH + 'static/uploads/' + song_name + '.wav'
print wav_fn
try:
cpraw = subprocess.check_output([
APP_PATH + 'music_changepoints/novelty',
wav_fn, 'rms'])
cp = [float(c) for c in cpraw.split('\n') if len(c) > 0]
except:
cp = novelty(wav_fn, k=64, nchangepoints=4)
out = {
"changepoints": cp
}
return jsonify(**out)
@app.route('/underlayRetarget/<song_name>/<float:length>/<float:before>/<float:after>')
def retargeted_underlay(song_name, length, before, after):
solo_length = 6.0
song_name = urllib.unquote(song_name)
wav_fn = APP_PATH + 'static/uploads/' + song_name + '.wav'
npz_fn = APP_PATH + 'static/uploads/' + song_name + '.npz'
beat_path, best_cost, cps, durs = best_changepoint_path(
wav_fn,
npz_fn,
length + solo_length,
APP_PATH=APP_PATH,
nchangepoints=4)
print "changepoints", cps
npz = N.load(npz_fn)
all_beats = npz["markers"]
avg_duration = npz["avg_duration"]
first_target = float(beat_path[0]) - before
last_target = float(beat_path[-1]) + after + solo_length
first_beat = min(all_beats, key=lambda b: N.abs(b - first_target))
last_beat = min(all_beats, key=lambda b: N.abs(b - last_target))
beat_path_start = [str(b) for b in all_beats
if b >= first_beat and b < float(beat_path[0])]
beat_path_end = [str(b) for b in all_beats
if b > float(beat_path[-1]) and b <= last_beat]
middle = round(avg_duration[0] * len(beat_path), 5)
# I think these word as they're supposed to, but I'm not 100% sure
cp1_delta = round(float(beat_path[0]) - float(cps[0]), 5)
cp2_delta = round(float(beat_path[-1]) - float(cps[1]), 5)
between_dur = N.sum(durs) + cp1_delta + cp2_delta
print "deltas", cp1_delta, cp2_delta
# try:
# # before_dur = round(float(beat_path[0]) - float(beat_path_start[0]), 5)
# before_dur = round(float(cps[0]) - float(beat_path_start[0]), 5)
# except:
# before_dur = 0
# out = {
# "beats": beat_path_start + beat_path + beat_path_end,
# "before": before_dur,
# "middle": round(length, 5),
# "solo1": round(middle - (length + cp1_delta), 5),
# "solo2": float(solo_length + cp2_delta),
# "after": round(float(beat_path_end[-1]) - float(beat_path[-1]) - solo_length, 5),
# "changepoints": cps
# #"changepoints": [beat_path[0], beat_path[-1]],
# }
try:
# before_dur = round(float(beat_path[0]) - float(beat_path_start[0]), 5)
before_dur = round(float(cps[0]) - float(beat_path_start[0]), 5)
except:
before_dur = 0
# print "lens", last_beat, beat_path_end, cps, solo_length, len(beat_path_end), len(cps)
out = {
"beats": beat_path_start + beat_path + beat_path_end,
"before": before_dur,
"solo1": round(between_dur - length - .5, 5),
"middle": round(length, 5),
"solo2": solo_length,
# "after": round(float(beat_path_end[-1]) - float(cps[1]) - solo_length, 5),
"changepoints": cps,
}
try:
out["after"] = round(float(beat_path_end[-1]) - float(cps[1]) - solo_length, 5)
except:
out["after"] = 0.0
# quick check! just in case
while out["before"] > before:
print out["beats"]
out["beats"] = out["beats"][1:]
# out["before"] = round(float(beat_path[0]) - float(out["beats"][0]), 5)
out["before"] = round(float(cps[0]) - float(out["beats"][0]), 5)
out["total"] = out["before"] + out["solo1"] + out["middle"] +\
out["solo2"] + out["after"]
out["total"] = round(out["total"], 5)
print out
print "\n\n### Target:", (length + solo_length) / avg_duration
print "### Cost:", best_cost
return jsonify(**out)
# @app.route('/analyzeSpeech', methods=['POST'])
# def analyze_speech():
# upload_path = APP_PATH + 'static/speechtracks/'
# if request.method == 'POST':
# post_data = urllib.unquote(request.data)
# dat = json.loads(post_data)
# short_name = dat["name"]
# text = dat["text"]
# f = request.files['speech']
# # save the speech file
# filename = short_name + '.mp3'
# full_name = upload_path + filename
# f.save(full_name)
@app.route('/uploadSong', methods=['POST', 'GET'])
def upload_song():
upload_path = APP_PATH + 'static/uploads/'
if request.method == 'POST':
f = request.files['song']
file_path = f.filename.replace('\\', '/')
basename = os.path.basename(file_path)
filename = f.filename
full_name = upload_path + filename
f.save(full_name)
if request.method == 'GET':
"""
upload a file from the local file browser --
in static/musicbrowser/fullmp3s
"""
filename = request.args.get('filename')
sec_fn = filename
basename = os.path.basename(sec_fn)
full_name = os.path.join(upload_path, basename)
try:
with open(basename): pass
except IOError:
orig_name = os.path.join(
APP_PATH + 'static/musicbrowser/fullmp3s',
os.path.basename(filename))
print orig_name, full_name
shutil.copyfile(orig_name, full_name)
filename = sec_fn
# get id3 tags
try:
song = ID3(full_name)
song_title = song["TIT2"].text[0]
song_artist = song["TPE1"].text[0]
except:
song_title = os.path.basename(full_name)
song_artist = "Unknown"
wav_name = ".".join(full_name.split('.')[:-1]) + '.wav'
# convert to wav if necessary
try:
with open(wav_name): pass
except IOError:
subprocess.call(
'lame --decode "%s"' % full_name, shell=True)
# wav2json if necessary
print "wav2json if necessary"
try:
with open(upload_path + 'wfData/' + filename + '.json'): pass
except IOError:
cmd = 'wav2json -p 2 -s 10000 --channels mid -n -o "%s" "%s"' %\
(upload_path + 'wfData/' + filename + '.json', wav_name)
print cmd
subprocess.call(cmd, shell=True)
out = {
"path": "uploads/" + filename,
"name": basename.split('.')[0],
"title": song_title,
"artist": song_artist,
"basename": os.path.splitext(filename)[0]
}
# get length of song upload
track = Track(wav_name, "track")
out["dur"] = track.duration / float(track.samplerate) * 1000.0
# get song graph
mg = MusicGraph(full_name, cache_path=upload_path, verbose=True)
out["graph"] = mg.json_graph()
print "Got music graph"
# delete wav
#
# actually, don't delete the wav for now.
# subprocess.call('rm "%s"' % wav_name, shell=True)
# read waveform data json
with open(upload_path + 'wfData/' + filename + '.json', 'r') as wf:
out["wfData"] = json.load(wf)["mid"]
return jsonify(**out)
@app.route('/alignment/<name>')
def alignment(name):
breath_fn = "%s-breaths.json" % name
breath_full_path = "{}static/speechtracks/{}-breaths.json".format(APP_PATH, name)
nobreath_fn = name + ".json"
nobreath_full_path = "{}static/speechtracks/{}.json".format(APP_PATH, name)
if os.path.isfile(breath_full_path):
out = json.load(open(breath_full_path, 'r'))
out["speechText"] = breath_fn
else:
out = json.load(open(nobreath_full_path, 'r'))
out["speechText"] = nobreath_fn
return jsonify(**out)
from werkzeug.serving import run_simple
from werkzeug.wsgi import DispatcherMiddleware
@click.command()
@click.option('--music-browser/--no-browser', default=False,
help='do not load the music browser app')
def run_app(music_browser):
if music_browser:
# Figure out how to do these searches without requiring MySQL
# from music_browser_app.browser_flask import app as browserapp
# application = DispatcherMiddleware(app, {
# '/musicbrowser': browserapp
# })
app.config["MUSIC_BROWSER"] = True
application = app
else:
app.config["MUSIC_BROWSER"] = False
application = app
app.config["SPEECH_TRACKS"] = set()
for fn in glob.glob(APP_PATH + 'static/speechtracks/*.json'):
name = os.path.splitext(os.path.basename(fn))[0]
if name.endswith("-breaths"):
name = name.split('-breaths')[0]
app.config["SPEECH_TRACKS"].add(name)
run_simple('0.0.0.0', 5000, application,
use_reloader=True, use_debugger=True, use_evalex=True)
if __name__ == '__main__':
run_app()