Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, send_file, jsonify
|
| 2 |
+
from pydub import AudioSegment, effects
|
| 3 |
+
import numpy as np
|
| 4 |
+
import requests
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
@app.route("/result")
|
| 10 |
+
def process_song():
|
| 11 |
+
url = request.args.get("song")
|
| 12 |
+
if not url:
|
| 13 |
+
return jsonify({"error": "Please provide a song link with ?song=<url>"}), 400
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
# Download audio file
|
| 17 |
+
resp = requests.get(url, stream=True)
|
| 18 |
+
resp.raise_for_status()
|
| 19 |
+
|
| 20 |
+
# Load audio into pydub
|
| 21 |
+
sound = AudioSegment.from_file(io.BytesIO(resp.content), format="mp3")
|
| 22 |
+
sound = effects.normalize(sound)
|
| 23 |
+
|
| 24 |
+
# Split channels
|
| 25 |
+
channels = sound.split_to_mono()
|
| 26 |
+
left, right = channels[0], channels[1]
|
| 27 |
+
|
| 28 |
+
# Empty output
|
| 29 |
+
output = AudioSegment.silent(duration=len(sound), frame_rate=sound.frame_rate)
|
| 30 |
+
|
| 31 |
+
# Settings
|
| 32 |
+
chunk_ms = 10
|
| 33 |
+
pan_period = 6.0
|
| 34 |
+
base_depth = 0.7
|
| 35 |
+
depth_variation = 0.2
|
| 36 |
+
reverb_delay = 80
|
| 37 |
+
|
| 38 |
+
steps = len(sound) // chunk_ms
|
| 39 |
+
for i in range(steps):
|
| 40 |
+
t = (i * chunk_ms) / 1000.0
|
| 41 |
+
ratio = (np.sin(2 * np.pi * t / pan_period) + 1) / 2
|
| 42 |
+
depth = base_depth + depth_variation * np.sin(2 * np.pi * t / (pan_period * 2))
|
| 43 |
+
|
| 44 |
+
left_gain = (1 - depth * ratio) * -3
|
| 45 |
+
right_gain = (depth * ratio) * -3
|
| 46 |
+
|
| 47 |
+
left_chunk = left[i * chunk_ms:(i + 1) * chunk_ms] + left_gain
|
| 48 |
+
right_chunk = right[i * chunk_ms:(i + 1) * chunk_ms] + right_gain
|
| 49 |
+
|
| 50 |
+
stereo_chunk = AudioSegment.from_mono_audiosegments(left_chunk, right_chunk)
|
| 51 |
+
output += stereo_chunk
|
| 52 |
+
|
| 53 |
+
# --- Stereo widening ---
|
| 54 |
+
widened = output.overlay(output.pan(-0.5), gain_during_overlay=-6)
|
| 55 |
+
widened = widened.overlay(output.pan(0.5), gain_during_overlay=-6)
|
| 56 |
+
|
| 57 |
+
# --- Subtle reverb ---
|
| 58 |
+
reverb = widened.overlay(widened - 8, delay=reverb_delay)
|
| 59 |
+
reverb = reverb.overlay(widened - 14, delay=reverb_delay * 2)
|
| 60 |
+
|
| 61 |
+
# --- Final mastering ---
|
| 62 |
+
final = effects.normalize(reverb)
|
| 63 |
+
|
| 64 |
+
# Export to memory
|
| 65 |
+
buf = io.BytesIO()
|
| 66 |
+
final.export(buf, format="mp3")
|
| 67 |
+
buf.seek(0)
|
| 68 |
+
|
| 69 |
+
return send_file(buf, mimetype="audio/mpeg", as_attachment=True, download_name="panned_heavenly.mp3")
|
| 70 |
+
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return jsonify({"error": str(e)}), 500
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
app.run(host="0.0.0.0", port=7860)
|