Upload 3 files
Browse files- Dockerfile +23 -0
- app.py +139 -0
- requirements.txt +10 -0
Dockerfile
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
# install system dependencies
|
| 4 |
+
RUN apt-get update && apt-get install -y \
|
| 5 |
+
ffmpeg \
|
| 6 |
+
libsndfile1 \
|
| 7 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 8 |
+
|
| 9 |
+
# set working directory
|
| 10 |
+
WORKDIR /app
|
| 11 |
+
|
| 12 |
+
# copy requirements and install
|
| 13 |
+
COPY requirements.txt .
|
| 14 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 15 |
+
|
| 16 |
+
# copy application code
|
| 17 |
+
COPY . .
|
| 18 |
+
|
| 19 |
+
# expose the port flask will run on
|
| 20 |
+
EXPOSE 7860
|
| 21 |
+
|
| 22 |
+
# run the application
|
| 23 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
+
import uuid
|
| 4 |
+
import shutil
|
| 5 |
+
import numpy as np
|
| 6 |
+
import librosa
|
| 7 |
+
import soundfile as sf
|
| 8 |
+
import pyloudnorm as pyln
|
| 9 |
+
from flask import Flask, request, send_file, jsonify
|
| 10 |
+
from flask_cors import CORS
|
| 11 |
+
from scipy.signal import butter, lfilter
|
| 12 |
+
from pedalboard import Pedalboard, Compressor, Limiter, HighpassFilter, LowpassFilter, Gain
|
| 13 |
+
import subprocess
|
| 14 |
+
from pydub import AudioSegment
|
| 15 |
+
|
| 16 |
+
app = Flask(__name__)
|
| 17 |
+
CORS(app)
|
| 18 |
+
|
| 19 |
+
SR = 44100
|
| 20 |
+
TARGET_LOUDNESS = -9.0
|
| 21 |
+
|
| 22 |
+
def convert_to_wav(input_path):
|
| 23 |
+
if input_path.lower().endswith(".mp3"):
|
| 24 |
+
wav_path = input_path.rsplit(".", 1)[0] + f"_{uuid.uuid4().hex}.wav"
|
| 25 |
+
AudioSegment.from_mp3(input_path).export(wav_path, format="wav")
|
| 26 |
+
return wav_path
|
| 27 |
+
return input_path
|
| 28 |
+
|
| 29 |
+
def load_mono(file):
|
| 30 |
+
y, _ = librosa.load(file, sr=SR, mono=True)
|
| 31 |
+
return y
|
| 32 |
+
|
| 33 |
+
def normalize_audio(y):
|
| 34 |
+
return y / (np.max(np.abs(y)) + 1e-9)
|
| 35 |
+
|
| 36 |
+
def highpass(data, cutoff):
|
| 37 |
+
b, a = butter(4, cutoff / (SR / 2), btype='high')
|
| 38 |
+
return lfilter(b, a, data)
|
| 39 |
+
|
| 40 |
+
def lowpass(data, cutoff):
|
| 41 |
+
b, a = butter(4, cutoff / (SR / 2), btype='low')
|
| 42 |
+
return lfilter(b, a, data)
|
| 43 |
+
|
| 44 |
+
def detect_key(y):
|
| 45 |
+
chroma = librosa.feature.chroma_cqt(y=y, sr=SR)
|
| 46 |
+
return np.argmax(np.sum(chroma, axis=1))
|
| 47 |
+
|
| 48 |
+
def match_key(source, target):
|
| 49 |
+
key_s = detect_key(source)
|
| 50 |
+
key_t = detect_key(target)
|
| 51 |
+
shift = key_t - key_s
|
| 52 |
+
return librosa.effects.pitch_shift(source, sr=SR, n_steps=float(shift))
|
| 53 |
+
|
| 54 |
+
def beat_sync_warp(source, target):
|
| 55 |
+
tempo_t, _ = librosa.beat.beat_track(y=target, sr=SR)
|
| 56 |
+
tempo_s, _ = librosa.beat.beat_track(y=source, sr=SR)
|
| 57 |
+
tempo_t = float(np.atleast_1d(tempo_t)[0])
|
| 58 |
+
tempo_s = float(np.atleast_1d(tempo_s)[0])
|
| 59 |
+
if tempo_t <= 0 or tempo_s <= 0:
|
| 60 |
+
return librosa.util.fix_length(source, size=len(target))
|
| 61 |
+
rate = tempo_s / tempo_t
|
| 62 |
+
warped = librosa.effects.time_stretch(source, rate=float(rate))
|
| 63 |
+
warped = librosa.util.fix_length(warped, size=len(target))
|
| 64 |
+
return warped
|
| 65 |
+
|
| 66 |
+
def separate_stems(input_file, job_id):
|
| 67 |
+
out_dir = f"separated_{job_id}"
|
| 68 |
+
subprocess.run(["demucs", "-n", "htdemucs", "--out", out_dir, input_file], check=True)
|
| 69 |
+
base = os.path.splitext(os.path.basename(input_file))[0]
|
| 70 |
+
stem_dir = f"{out_dir}/htdemucs/{base}"
|
| 71 |
+
stems = {
|
| 72 |
+
"drums": f"{stem_dir}/drums.wav",
|
| 73 |
+
"bass": f"{stem_dir}/bass.wav",
|
| 74 |
+
"other": f"{stem_dir}/other.wav",
|
| 75 |
+
"vocals": f"{stem_dir}/vocals.wav"
|
| 76 |
+
}
|
| 77 |
+
return stems, out_dir
|
| 78 |
+
|
| 79 |
+
@app.route('/', methods=['GET'])
|
| 80 |
+
def health():
|
| 81 |
+
return jsonify({"status": "ready"}), 200
|
| 82 |
+
|
| 83 |
+
@app.route('/fuse', methods=['POST'])
|
| 84 |
+
def fuse_api():
|
| 85 |
+
job_id = str(uuid.uuid4())
|
| 86 |
+
temp_files = []
|
| 87 |
+
cleanup_dirs = []
|
| 88 |
+
try:
|
| 89 |
+
trad_req = request.files.get('melody')
|
| 90 |
+
modern_req = request.files.get('style')
|
| 91 |
+
if not trad_req or not modern_req:
|
| 92 |
+
return jsonify({"error": "missing files"}), 400
|
| 93 |
+
t_path = f"trad_{job_id}.wav"
|
| 94 |
+
m_path = f"mod_{job_id}.wav"
|
| 95 |
+
trad_req.save(t_path)
|
| 96 |
+
modern_req.save(m_path)
|
| 97 |
+
temp_files.extend([t_path, m_path])
|
| 98 |
+
t_wav = convert_to_wav(t_path)
|
| 99 |
+
m_wav = convert_to_wav(m_path)
|
| 100 |
+
if t_wav != t_path: temp_files.append(t_wav)
|
| 101 |
+
if m_wav != m_path: temp_files.append(m_wav)
|
| 102 |
+
t_stems, t_dir = separate_stems(t_wav, f"t_{job_id}")
|
| 103 |
+
m_stems, m_dir = separate_stems(m_wav, f"m_{job_id}")
|
| 104 |
+
cleanup_dirs.extend([t_dir, m_dir])
|
| 105 |
+
t_other = load_mono(t_stems["other"])
|
| 106 |
+
t_bass = load_mono(t_stems["bass"])
|
| 107 |
+
m_drums = load_mono(m_stems["drums"])
|
| 108 |
+
m_bass = load_mono(m_stems["bass"])
|
| 109 |
+
m_other = load_mono(m_stems["other"])
|
| 110 |
+
t_other = match_key(t_other, m_other)
|
| 111 |
+
t_bass = match_key(t_bass, m_bass)
|
| 112 |
+
t_other = beat_sync_warp(t_other, m_drums)
|
| 113 |
+
t_bass = beat_sync_warp(t_bass, m_drums)
|
| 114 |
+
t_other = highpass(t_other, 120)
|
| 115 |
+
t_bass = highpass(t_bass, 60)
|
| 116 |
+
m_bass = lowpass(m_bass, 250)
|
| 117 |
+
m_drums = lowpass(m_drums, 12000)
|
| 118 |
+
m_other = highpass(m_other, 150)
|
| 119 |
+
fusion = (1.0 * m_drums + 1.0 * m_bass + 1.2 * t_other + 0.5 * m_other + 0.8 * t_bass)
|
| 120 |
+
fusion = normalize_audio(fusion)
|
| 121 |
+
board = Pedalboard([HighpassFilter(30), LowpassFilter(18000), Compressor(threshold_db=-20, ratio=2), Gain(2.0), Limiter(threshold_db=-0.5)])
|
| 122 |
+
fusion_mastered = board(fusion, SR)
|
| 123 |
+
meter = pyln.Meter(SR)
|
| 124 |
+
loudness = meter.integrated_loudness(fusion_mastered)
|
| 125 |
+
fusion_mastered = pyln.normalize.loudness(fusion_mastered, loudness, TARGET_LOUDNESS)
|
| 126 |
+
buf = io.BytesIO()
|
| 127 |
+
sf.write(buf, fusion_mastered, SR, format='WAV')
|
| 128 |
+
buf.seek(0)
|
| 129 |
+
return send_file(buf, mimetype="audio/wav", as_attachment=True, download_name="fusion_output.wav")
|
| 130 |
+
except Exception as e:
|
| 131 |
+
return jsonify({"error": str(e)}), 500
|
| 132 |
+
finally:
|
| 133 |
+
for f in temp_files:
|
| 134 |
+
if os.path.exists(f): os.remove(f)
|
| 135 |
+
for d in cleanup_dirs:
|
| 136 |
+
if os.path.exists(d): shutil.rmtree(d, ignore_errors=True)
|
| 137 |
+
|
| 138 |
+
if __name__ == "__main__":
|
| 139 |
+
app.run(host='0.0.0.0', port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
flask-cors
|
| 3 |
+
numpy
|
| 4 |
+
librosa
|
| 5 |
+
soundfile
|
| 6 |
+
pyloudnorm
|
| 7 |
+
scipy
|
| 8 |
+
pedalboard
|
| 9 |
+
demucs
|
| 10 |
+
pydub
|