-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
442 lines (373 loc) · 16.4 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
from flask import Flask, render_template, request, jsonify
from threading import Thread, Lock
from pydub import AudioSegment, generators
from pydub.utils import make_chunks
from queue import Queue, Empty
import os
import time
import numpy as np
import subprocess
import wave
from tinytag import TinyTag
from functools import reduce
from datetime import datetime
import json
import sounddevice as sd
app = Flask(__name__)
# Global variables and settings
is_playing = False
mic_volume = -90
music_volume = 0
fade_duration = 5
output_wav_path = "./stream_output.wav"
saved_wav_path = "./saved_show.wav"
liq_script_path = "./stream.liq"
lock = Lock()
buffer_duration_ms = 10
sample_rate = 44100
channels = 2
sample_width = 2
audio_queue = Queue(maxsize=2)
audio_folder = None
playlist = []
total_duration_seconds = 0
show_start_time = None
annotations_file_path = 'annotations.json'
mic_queue = Queue(maxsize=2)
mic_stream = None
blocksize = 441
@app.route('/')
def index():
"""Render the main page."""
return render_template('index.html')
@app.route('/load-folder', methods=['POST'])
def load_folder():
""" Return the valid tracks in directory."""
folder_path = request.json.get('folderPath')
if not folder_path or not os.path.isdir(folder_path):
return jsonify({'status': 'error', 'message': 'Invalid folder path.'})
files = [f for f in os.listdir(folder_path) if f.lower().endswith(('.mp3', '.wav'))]
return jsonify({'status': 'success', 'files': files, 'folderPath': folder_path})
@app.route('/list-files', methods=['GET'])
def list_files():
"""List all .wav files in the upload directory."""
files = [f for f in os.listdir(UPLOAD_FOLDER) if f.endswith('.wav')]
return jsonify({'files': files})
@app.route('/add-to-playlist', methods=['POST'])
def add_to_playlist():
""" Method that adds tracks to the playlist."""
global total_duration_seconds
file_name = request.json.get('fileName')
folder_path = request.json.get('folderPath')
if not file_name or not folder_path:
return jsonify({'status': 'error', 'message': 'Invalid file or folder path.'})
full_path = os.path.join(folder_path, file_name)
if not any(track['path'] == full_path for track in playlist):
duration_seconds = get_track_duration(full_path)
playlist.append({"path": full_path, "name": file_name, "duration": duration_seconds})
total_duration_seconds += duration_seconds
formatted_playlist = [
{
'name': os.path.basename(track['path']),
'path': track['path'],
'duration': format_duration(track['duration'])
}
for track in playlist
]
return jsonify({
'status': 'success',
'playlist': formatted_playlist,
'totalDuration': format_duration(total_duration_seconds),
'rawDuration': total_duration_seconds
})
@app.route('/get-playlist', methods=['GET'])
def get_playlist():
"""Retrieve the current playlist."""
return jsonify({'playlist': playlist})
@app.route('/start-show', methods=['POST'])
def start_show():
global is_playing, show_start_time
with lock:
if not is_playing:
is_playing = True
print("Show started, mixing audio.")
if not os.path.exists(output_wav_path):
os.mkfifo(output_wav_path)
show_start_time = datetime.now()
create_annotations_file()
if len(playlist) > 0:
first_track_path = playlist[0]['path']
first_genre = get_genre(first_track_path)
current_genre = first_genre
log_annotation('music', {'genre': first_genre}, timestamp="00:00:00")
print(f"Now playing: {playlist[0]['name']} - Genre: {first_genre}")
print("Show started, mixing audio.")
Thread(target=mix_audio, daemon=True).start()
Thread(target=write_to_outputs, daemon=True).start()
Thread(target=start_liquidsoap, daemon=True).start()
try:
subprocess.Popen(['aplay', '-f', 'S16_LE', '-c', '2', '-r', '44100', output_wav_path],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print("FFplay started for real-time monitoring.")
except Exception as e:
print(f"Error starting FFplay: {e}")
return jsonify({'status': 'Show started.'})
else:
return jsonify({'status': 'Show is already running.'})
@app.route('/switch-to-voice', methods=['POST'])
def switch_to_voice():
"""Switch from music to voice with a crossfade."""
log_annotation('transition')
start_mic_capture()
crossfade_volumes(fade_in_mic=True)
log_annotation('speech')
print("Switched to voice mode with crossfade.")
return jsonify({'status': 'Switched to voice mode with crossfade.'})
@app.route('/switch-to-music', methods=['POST'])
def switch_to_music():
"""Switch from voice to music with a crossfade."""
log_annotation('transition')
crossfade_volumes(fade_in_mic=False)
stop_mic_capture()
current_track = playlist[current_track_index]['path']
genre = get_genre(current_track)
log_annotation('music', {'genre': genre})
print("Switched back to music mode with crossfade.")
return jsonify({'status': 'Switched back to music mode with crossfade.'})
# This is where the mic_callback function is defined
def mic_callback(indata, frames, time, status):
"""Callback to capture mic input and push to queue during crossfade."""
if status:
print(f"Mic input status: {status}")
try:
mic_queue.put(indata.copy(), timeout=1) # Push mic data to queue
except Exception as e:
print(f"Error enqueuing mic data: {e}")
def create_annotations_file():
"""Create the annotations file at the start of the show."""
global show_start_time
try:
# Initialize the JSON structure with show start time
initial_data = {
"start_time": str(show_start_time),
"annotations": {}
}
with open(annotations_file_path, 'w') as json_file:
json.dump(initial_data, json_file, indent=4)
print(f"Annotations file created at {annotations_file_path}.")
except Exception as e:
print(f"Error creating annotations file: {e}")
def update_annotations_file(timestamp, annotation):
"""Update the annotations.json file with the new annotation."""
try:
# Read the existing data
with open(annotations_file_path, 'r') as json_file:
data = json.load(json_file)
# Update the annotations section with the new event
data['annotations'][timestamp] = annotation
# Write the updated data back to the JSON file
with open(annotations_file_path, 'w') as json_file:
json.dump(data, json_file, indent=4)
print(f"Annotation updated at {timestamp}: {annotation}")
except Exception as e:
print(f"Error updating annotations file: {e}")
def save_annotations_to_file():
"""Save the annotations to a JSON file."""
try:
with open('annotations.json', 'w') as json_file:
json.dump(annotations, json_file, indent=4)
print("Annotations saved to annotations.json.")
except Exception as e:
print(f"Error saving annotations: {e}")
def get_elapsed_time():
global show_start_time
if show_start_time is None:
return "00:00:00"
elapsed_seconds = int(time.time() - show_start_time)
return time.strftime("%H:%M:%S", time.gmtime(elapsed_seconds))
def log_annotation(event_type, additional_info=None, timestamp=None):
global show_start_time
if timestamp is None:
current_time = datetime.now()
elapsed_time = (current_time - show_start_time).total_seconds() # Calculate time since show started
# Format the elapsed time as HH:MM:SS
timestamp = str(datetime.utcfromtimestamp(elapsed_time).strftime('%H:%M:%S.%f')[:-3]) # Truncate to milliseconds
annotation_entry = {
"event": event_type
}
if additional_info:
annotation_entry.update(additional_info)
update_annotations_file(timestamp, annotation_entry)
print(f"Annotation logged at {timestamp}: {annotation_entry}")
def crossfade_volumes(fade_in_mic):
"""Gradually adjust the volumes of the music and voice channels to create a crossfade effect with overlap."""
global mic_volume, music_volume
steps = int(fade_duration / 0.1)
mic_start_dB = -90 if fade_in_mic else 0
mic_end_dB = 0 if fade_in_mic else -90
music_start_dB = 0 if fade_in_mic else -90
music_end_dB = -90 if fade_in_mic else 0
mic_start_amp = 10 ** (mic_start_dB / 20)
mic_end_amp = 10 ** (mic_end_dB / 20)
music_start_amp = 10 ** (music_start_dB / 20)
music_end_amp = 10 ** (music_end_dB / 20)
mic_amplitudes = np.linspace(mic_start_amp, mic_end_amp, steps)
music_amplitudes = np.linspace(music_start_amp, music_end_amp, steps)
mic_dB_values = 20 * np.log10(mic_amplitudes + 1e-10)
music_dB_values = 20 * np.log10(music_amplitudes + 1e-10)
for mic_dB, music_dB in zip(mic_dB_values, music_dB_values):
with lock:
mic_volume = mic_dB
music_volume = music_dB
print(f"Mic volume: {mic_volume:.1f} dB, Music volume: {music_volume:.1f} dB")
time.sleep(0.1)
print("Crossfade completed.")
def get_track_duration(file_path):
"""Retrieve the duration of a track in seconds."""
try:
tag = TinyTag.get(file_path)
return tag.duration or 0
except Exception as e:
print(f"Error getting track duration: {e}")
return 0
def get_genre(file_path):
try:
tag = TinyTag.get(file_path)
return tag.genre or None
except Exception as e:
print(f"Error getting track genre: {e}")
return 0
def format_duration(seconds):
"""Format the duration in seconds to HH:MM:SS."""
hours = int(seconds // 3600)
minutes = int((seconds % 3600) // 60)
seconds = int(seconds % 60)
return f"{hours:02}:{minutes:02}:{seconds:02}"
def match_target_amplitude(sound, target_dBFS):
"""Adjust sound segment to match target dBFS."""
change_in_dBFS = target_dBFS - sound.dBFS
return sound.apply_gain(change_in_dBFS)
def sound_slice_normalize(sound, sample_rate, target_dBFS):
"""Normalize each chunk of sound to fall within target dBFS range."""
def max_min_volume(min_dBFS, max_dBFS):
for chunk in make_chunks(sound, sample_rate):
if chunk.dBFS < min_dBFS:
yield match_target_amplitude(chunk, min_dBFS)
elif chunk.dBFS > max_dBFS:
yield match_target_amplitude(chunk, max_dBFS)
else:
yield chunk
return reduce(lambda x, y: x + y, max_min_volume(target_dBFS[0], target_dBFS[1]))
def mix_audio():
"""Continuously mix the music and mic input, adding the result to a queue."""
global current_track_index, mic_volume, music_volume, is_playing, playlist
current_track_index = 0
current_genre = None
while is_playing:
try:
# Get the current track's music segment
with lock:
if current_track_index >= len(playlist):
print("End of playlist.")
break
track_path = playlist[current_track_index]['path']
music_segment = AudioSegment.from_file(track_path).set_frame_rate(sample_rate).set_channels(1)
music_segment = sound_slice_normalize(music_segment, sample_rate, (-20, 0))
for i in range(0, len(music_segment), buffer_duration_ms):
music_chunk = music_segment[i:i + buffer_duration_ms]
adjusted_music_chunk = music_chunk.apply_gain(music_volume)
# Try to get mic data if capturing (during crossfade)
try:
mic_data = mic_queue.get_nowait()
mic_data = (mic_data * 32767).astype(np.int16)
# Convert the numpy array to raw bytes
mic_data = mic_data.tobytes()
voice_chunk = AudioSegment(
data=mic_data,
sample_width=2,
frame_rate=sample_rate,
channels=1
).apply_gain(mic_volume)
except Empty:
voice_chunk = AudioSegment.silent(duration=buffer_duration_ms, frame_rate=sample_rate)
# Mix the adjusted music and mic chunks
mixed_chunk = adjusted_music_chunk.overlay(voice_chunk)
mixed_chunk = mixed_chunk.set_channels(channels)
mixed_data = mixed_chunk.raw_data
audio_queue.put(mixed_data)
time.sleep(len(mixed_data) / (sample_rate * channels * sample_width))
with lock:
current_track_index += 1
except Exception as e:
print(f"Error during audio mixing: {e}")
break
def real_time_playback():
"""Real-time playback using sounddevice."""
def callback(outdata, frames, time, status):
"""Sounddevice callback function for real-time playback."""
if status:
print(status)
try:
# Get the next chunk of mixed audio data from the queue
mixed_data = audio_queue.get_nowait()
# Convert the mixed audio from bytes to float32 for sounddevice (normalized between -1 and 1)
audio_data = np.frombuffer(mixed_data, dtype=np.int16).astype(np.float32) / 32768.0
# Make sure the audio data fits the output format (stereo with `frames` number of samples)
expected_samples = frames * channels
print(len(audio_data), expected_samples)
if len(audio_data) < expected_samples:
# Pad with zeros if the data is less than expected
audio_data = np.pad(audio_data, (0, expected_samples - len(audio_data)), mode='constant')
elif len(audio_data) > expected_samples:
# Trim the data if there's more than expected
audio_data = audio_data[:expected_samples]
# Reshape the audio data to match stereo format (frames, channels)
outdata[:] = audio_data.reshape(-1, channels)
except Empty:
# If the queue is empty, output silence
outdata.fill(0)
# Open sounddevice output stream and start real-time playback
with sd.OutputStream(samplerate=sample_rate, channels=channels, callback=callback, dtype='float32', blocksize=882):
while is_playing or not audio_queue.empty():
pass # Keep the stream alive while audio is playing
def write_to_outputs():
"""Continuously write mixed audio data from the queue to the named pipe and a saved WAV file."""
try:
with open(output_wav_path, 'wb') as output_pipe, wave.open(saved_wav_path, 'wb') as output_wav:
output_wav.setnchannels(channels)
output_wav.setsampwidth(sample_width)
output_wav.setframerate(sample_rate)
while is_playing or not audio_queue.empty():
try:
mixed_data = audio_queue.get(timeout=1)
output_pipe.write(mixed_data)
output_pipe.flush()
output_wav.writeframes(mixed_data)
print(f"Written chunk to {output_wav_path} and {saved_wav_path}.")
except Empty:
print("Buffer underrun, waiting for data.")
except Exception as e:
print(f"Error writing to outputs: {e}")
def start_mic_capture():
"""Start capturing microphone input for crossfade."""
global mic_stream
if mic_stream is None:
mic_stream = sd.InputStream(samplerate=sample_rate, channels=1, callback=mic_callback, device = 9,blocksize=blocksize)
mic_stream.start()
print("Microphone capture started.")
def stop_mic_capture():
"""Stop capturing microphone input after crossfade."""
global mic_stream
if mic_stream is not None:
mic_stream.stop()
mic_stream.close()
mic_stream = None
print("Microphone capture stopped.")
def start_liquidsoap():
"""Start Liquidsoap for streaming."""
try:
subprocess.Popen(["liquidsoap", liq_script_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except Exception as e:
print(f"Error starting Liquidsoap: {e}")
if __name__ == '__main__':
app.run(debug=True, use_reloader=False)