Create engine_lygo.py
Browse files- engine_lygo.py +73 -0
engine_lygo.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
engine_lygo.py – LYGO Resonance Engine Module
|
| 4 |
+
Importable by app.py. Returns (log, audio_path, downloadable_files).
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import os
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
from resonance_engine import ResonanceEngine, PRESETS
|
| 10 |
+
from utils import safe_sf_write, logger
|
| 11 |
+
|
| 12 |
+
def run_lygo_resonance(image_path, style, seed, duration, noise_filter,
|
| 13 |
+
enable_ldq, genre_manifold, percussion_mode, perceptual_polish,
|
| 14 |
+
bpm, swing, export_stems, export_midi):
|
| 15 |
+
"""
|
| 16 |
+
Run LYGO Resonance Engine with given parameters.
|
| 17 |
+
Returns (log_message, audio_path, list_of_files).
|
| 18 |
+
"""
|
| 19 |
+
if not image_path or not os.path.exists(image_path):
|
| 20 |
+
return "❌ No valid image provided.", None, []
|
| 21 |
+
|
| 22 |
+
# Build config
|
| 23 |
+
config = {
|
| 24 |
+
"duration": duration,
|
| 25 |
+
"random_seed": int(seed) if seed != 0 else None,
|
| 26 |
+
"verbose": False,
|
| 27 |
+
"use_ldq": enable_ldq,
|
| 28 |
+
"genre_manifold": genre_manifold,
|
| 29 |
+
"percussion_mode": percussion_mode,
|
| 30 |
+
"perceptual_polish": perceptual_polish,
|
| 31 |
+
"tempo_bpm": bpm,
|
| 32 |
+
"swing": swing,
|
| 33 |
+
"export_stems": export_stems,
|
| 34 |
+
"export_midi": export_midi,
|
| 35 |
+
}
|
| 36 |
+
if noise_filter > 0:
|
| 37 |
+
config["noise_lowpass_hz"] = noise_filter
|
| 38 |
+
|
| 39 |
+
preset = PRESETS.get(style, {})
|
| 40 |
+
config.update(preset)
|
| 41 |
+
|
| 42 |
+
# Initialize and run
|
| 43 |
+
engine = ResonanceEngine(config)
|
| 44 |
+
out_path = f"lygo_{Path(image_path).stem}.wav"
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
engine.process(image_path, out_path)
|
| 48 |
+
except Exception as e:
|
| 49 |
+
logger.error(f"LYGO engine error: {e}", exc_info=True)
|
| 50 |
+
return f"❌ LYGO Error: {str(e)}", None, []
|
| 51 |
+
|
| 52 |
+
# Collect files
|
| 53 |
+
downloadable = [out_path]
|
| 54 |
+
if export_stems:
|
| 55 |
+
base = out_path.replace(".wav", "")
|
| 56 |
+
for suffix in ["noise", "drone", "melody", "glitch"]:
|
| 57 |
+
stem_path = f"{base}_{suffix}.wav"
|
| 58 |
+
if os.path.exists(stem_path):
|
| 59 |
+
downloadable.append(stem_path)
|
| 60 |
+
if export_midi:
|
| 61 |
+
mid_path = out_path.replace(".wav", ".mid")
|
| 62 |
+
if os.path.exists(mid_path):
|
| 63 |
+
downloadable.append(mid_path)
|
| 64 |
+
|
| 65 |
+
log_msg = (
|
| 66 |
+
f"✅ LYGO Resonance Engine complete.\n"
|
| 67 |
+
f"Output: {out_path}\n"
|
| 68 |
+
f"LDQ enabled: {enable_ldq}\n"
|
| 69 |
+
f"Genre: {genre_manifold}\n"
|
| 70 |
+
f"Percussion: {percussion_mode}\n"
|
| 71 |
+
f"BPM: {bpm}, Swing: {swing}"
|
| 72 |
+
)
|
| 73 |
+
return log_msg, out_path, downloadable
|