Phase 2: native_render Python client (bridge --render) + SF_RENDER_ENGINE flag cho preview/midi-render

This commit is contained in:
2026-08-17 10:49:54 +07:00
parent bbb11f8066
commit 235dee103d
5 changed files with 238 additions and 4 deletions
+16 -3
View File
@@ -874,7 +874,8 @@ def _write_carla_project(plugin_name: str, plugin_path: str) -> str:
@router.post("/preview")
async def preview_instrument(req: PreviewRequest):
"""Quick-render preview VSTi (âm thật, cùng code path với export)."""
if not HAS_PEDALBOARD:
# SF_RENDER_ENGINE=bridge: preview qua native_bridge, không cần pedalboard
if settings.RENDER_ENGINE != "bridge" and not HAS_PEDALBOARD:
raise HTTPException(status_code=501, detail="pedalboard không khả dụng trên máy này")
if not req.notes:
raise HTTPException(status_code=400, detail="Chưa có nốt nhạc để preview")
@@ -950,7 +951,7 @@ async def midi_render(req: MidiRenderRequest, current_user: dict = Depends(get_c
"url": f"/static/audio/processed/{os.path.basename(out_path)}",
"path": out_path,
"duration_sec": round(duration_sec, 3),
"render_mode": "pedalboard",
"render_mode": settings.RENDER_ENGINE,
}
except HTTPException:
# Không để lại file rác nếu thất bại giữa chừng
@@ -980,9 +981,21 @@ def _render_midi_notes_pedalboard(instrument_id: str, notes: list, bpm: float,
sample_rate: int, preset_id=None, preset_path=None,
preset_data_b64=None, soundfont_bank=None,
soundfont_program=None) -> tuple:
"""Render MIDI notes qua pedalboard (VSTi + preset) → WAV trong PROCESSED_DIR.
"""Render MIDI notes (VSTi + preset) → WAV trong PROCESSED_DIR — qua
pedalboard (mặc định) hoặc native_bridge --render (SF_RENDER_ENGINE=bridge).
Trả (out_path, duration_sec). Ném HTTPException khi plugin không load được."""
if settings.RENDER_ENGINE == "bridge":
from app.core import native_render
return native_render.render_offline(
instrument_id=instrument_id,
notes=notes,
bpm=bpm,
sample_rate=sample_rate,
preset_id=preset_id,
preset_path=preset_path,
preset_data_b64=preset_data_b64,
)
if not HAS_PEDALBOARD:
raise HTTPException(status_code=501, detail="pedalboard không khả dụng trên máy này")
pm = PluginManager()