-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
89 lines (67 loc) · 2.67 KB
/
app.py
File metadata and controls
89 lines (67 loc) · 2.67 KB
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
from flask import Flask
from flask import render_template, request, redirect, make_response, flash
from pydub import AudioSegment
import glob
import numpy
import os
import uuid
app = Flask(__name__)
app.secret_key = "secret_key_samplechain"
@app.route("/", methods=["GET", "POST"])
def upload():
# defining arrays
sound_array = []
sound_length_array = []
if request.method == "POST":
if request.files:
uploaded_files = request.files.getlist("file[]")
#print(uploaded_files)
# Check if there is no WAV file
# if len(uploaded_files) == 1:
# flash('Please provide more than one file!', category='error')
# return redirect(request.url)
for wav_file in uploaded_files:
if wav_file.content_type not in ("audio/x-wav", "audio/wav"):
print(wav_file.content_type)
flash('File type not supported!', category='error')
return redirect(request.url)
elif len(uploaded_files) == 1:
flash('Please provide more than one file!', category='error')
return redirect(request.url)
else:
# load file as audiosegment object
sound = AudioSegment.from_wav(wav_file)
# append to array
sound_array.append(sound)
sound_length_array.append(sound.duration_seconds)
# test printing attributes
#print(sound.duration_seconds)
# collect maximum duration of a sound
max_duration = numpy.max(sound_length_array)
# append silence to make all sounds the same length
for index in range(len(sound_array)):
silence_to_append = max_duration - sound_array[index].duration_seconds
#print(sound_array[index].duration_seconds * 1000, silence_to_append, max_duration, silence_to_append + sound_array[index].duration_seconds)
sound_array[index] = sound_array[index] + AudioSegment.silent(duration=silence_to_append*1000, frame_rate=44100)
#iterate and append audio to final clip
final_clip = sound_array[0]
for index in range(1,len(sound_array)):
#print(sound_array[index].duration_seconds)
final_clip = final_clip + sound_array[index]
# return final clip to client
final_clip_wav = final_clip.export(format="wav")
unique_filename = str(uuid.uuid4().hex)
print(unique_filename)
number_of_hits = str(len(sound_array))
response = make_response(final_clip_wav.read())
response.headers.set('Content-Type', 'audio/x-wav')
response.headers.set('Content-Disposition', 'attachment',
filename=unique_filename+"_"+number_of_hits)
return response
#return redirect(request.url)
#flash('File(s) successfully uploaded')
#return redirect(url_for('update'))
#return redirect('/')
return render_template("upload.html")
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)