Spaces:
Sleeping
Sleeping
added the style fix to the js
Browse files
app.py
CHANGED
|
@@ -6,74 +6,12 @@ import json
|
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
|
| 9 |
-
# ✅ MIDI Processing Function in Python
|
| 10 |
-
@app.post("/midi_input")
|
| 11 |
-
async def process_midi(request: Request):
|
| 12 |
-
try:
|
| 13 |
-
midi_data = await request.json()
|
| 14 |
-
note = midi_data["note"]
|
| 15 |
-
velocity = midi_data["velocity"]
|
| 16 |
-
|
| 17 |
-
print(f"🎹 Received MIDI Note: {note}, Velocity: {velocity}")
|
| 18 |
-
|
| 19 |
-
# 🚀 Process MIDI data (example: Transpose + Generate New Notes)
|
| 20 |
-
generated_note = (note + 5) % 128 # Transpose up by 3 semitones
|
| 21 |
-
generated_velocity = min(velocity + 10, 127) # Increase velocity slightly
|
| 22 |
-
|
| 23 |
-
# ✅ Send MIDI Response Back to Client
|
| 24 |
-
return {
|
| 25 |
-
"status": "success",
|
| 26 |
-
"generated_note": generated_note,
|
| 27 |
-
"generated_velocity": generated_velocity,
|
| 28 |
-
"original_note": note
|
| 29 |
-
}
|
| 30 |
-
|
| 31 |
-
except Exception as e:
|
| 32 |
-
print(f"🚨 Error processing MIDI: {str(e)}")
|
| 33 |
-
return {"status": "error", "message": str(e)}
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
@app.post("/midi_phrase")
|
| 37 |
-
async def process_midi_phrase(request: Request):
|
| 38 |
-
try:
|
| 39 |
-
data = await request.json()
|
| 40 |
-
phrase = data["phrase"] # List of MIDI notes
|
| 41 |
-
|
| 42 |
-
print(f"🎹 Received MIDI Phrase ({len(phrase)} notes)")
|
| 43 |
-
|
| 44 |
-
# 🚀 Process Phrase: Example - Transpose Each Note Up by 3 Semitones
|
| 45 |
-
generated_phrase = [
|
| 46 |
-
{
|
| 47 |
-
"note": (note["note"] + 3) % 128,
|
| 48 |
-
"velocity": note["velocity"],
|
| 49 |
-
"duration": note["duration"], # ✅ Keep original duration
|
| 50 |
-
"inter_onset": note["inter_onset"] # ✅ Keep inter-onset time
|
| 51 |
-
}
|
| 52 |
-
for note in phrase
|
| 53 |
-
]
|
| 54 |
-
|
| 55 |
-
return {"status": "success", "generated_phrase": generated_phrase}
|
| 56 |
-
|
| 57 |
-
except Exception as e:
|
| 58 |
-
print(f"🚨 Error processing MIDI phrase: {str(e)}")
|
| 59 |
-
return {"status": "error", "message": str(e)}
|
| 60 |
-
|
| 61 |
|
| 62 |
# ✅ JavaScript to Capture and Send MIDI Data
|
| 63 |
midi_js = """
|
| 64 |
<script>
|
| 65 |
|
| 66 |
-
let style = document.createElement("style");
|
| 67 |
-
style.innerHTML = `
|
| 68 |
-
* {
|
| 69 |
-
font-family: Arial, sans-serif !important;
|
| 70 |
-
}
|
| 71 |
-
`;
|
| 72 |
-
document.head.appendChild(style);
|
| 73 |
-
console.log("✅ Applied fallback font to prevent 404 error.");
|
| 74 |
-
|
| 75 |
</script>
|
| 76 |
-
|
| 77 |
"""
|
| 78 |
|
| 79 |
# ✅ Inject JavaScript and HTML
|
|
|
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# ✅ JavaScript to Capture and Send MIDI Data
|
| 11 |
midi_js = """
|
| 12 |
<script>
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
</script>
|
|
|
|
| 15 |
"""
|
| 16 |
|
| 17 |
# ✅ Inject JavaScript and HTML
|