File size: 11,075 Bytes
b03b6c6
e75a900
9f4af53
306f2c8
b4398df
 
cbd0e79
4fa7ba3
9f4af53
4fa7ba3
29529d7
9f4af53
 
 
 
29529d7
9f4af53
 
4fa7ba3
 
 
 
 
9f4af53
 
4fa7ba3
 
 
9f4af53
 
 
 
 
 
 
4fa7ba3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4398df
 
9f4af53
4fa7ba3
 
 
 
 
 
 
 
e75a900
9f4af53
 
 
 
 
e75a900
9f4af53
 
 
 
 
 
e75a900
b4398df
9f4af53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4fa7ba3
9f4af53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4fa7ba3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f4af53
4fa7ba3
 
 
c0b1af5
4fa7ba3
 
 
e75a900
 
4fa7ba3
e75a900
 
1490a6f
 
4fa7ba3
 
9f4af53
1490a6f
4fa7ba3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e75a900
9f4af53
 
 
 
 
4fa7ba3
9f4af53
e75a900
1490a6f
9f4af53
1490a6f
9f4af53
 
 
 
 
1490a6f
9f4af53
4fa7ba3
7e0f543
4fa7ba3
9f4af53
b4398df
 
9f4af53
b4398df
 
 
9f4af53
b02fc0d
b4398df
 
9f4af53
 
b4398df
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import gradio as gr
from fastapi import FastAPI, Request
import json

# ✅ Create FastAPI App
app = FastAPI()


# ✅ MIDI Processing Function in Python
@app.post("/midi_input")
async def process_midi(request: Request):
    try:
        midi_data = await request.json()
        note = midi_data["note"]
        velocity = midi_data["velocity"]

        print(f"🎹 Received MIDI Note: {note}, Velocity: {velocity}")

        # 🚀 Process MIDI data (example: Transpose + Generate New Notes)
        generated_note = (note + 5) % 128  # Transpose up by 3 semitones
        generated_velocity = min(velocity + 10, 127)  # Increase velocity slightly

        # ✅ Send MIDI Response Back to Client
        return {
            "status": "success",
            "generated_note": generated_note,
            "generated_velocity": generated_velocity,
            "original_note": note
        }

    except Exception as e:
        print(f"🚨 Error processing MIDI: {str(e)}")
        return {"status": "error", "message": str(e)}


@app.post("/midi_phrase")
async def process_midi_phrase(request: Request):
    try:
        data = await request.json()
        phrase = data["phrase"]  # List of MIDI notes

        print(f"🎹 Received MIDI Phrase ({len(phrase)} notes)")

        # 🚀 Process Phrase: Example - Transpose Each Note Up by 3 Semitones
        generated_phrase = [
            {
                "note": (note["note"] + 3) % 128,
                "velocity": note["velocity"],
                "duration": note["duration"],  # ✅ Keep original duration
                "inter_onset": note["inter_onset"]  # ✅ Keep inter-onset time
            }
            for note in phrase
        ]

        return {"status": "success", "generated_phrase": generated_phrase}

    except Exception as e:
        print(f"🚨 Error processing MIDI phrase: {str(e)}")
        return {"status": "error", "message": str(e)}


# ✅ JavaScript to Capture and Send MIDI Data
midi_js = """
<script>

let style = document.createElement("style");
style.innerHTML = `
    * {
        font-family: Arial, sans-serif !important;
    }
`;
document.head.appendChild(style);
console.log("✅ Applied fallback font to prevent 404 error.");

let midiAccess = null;
let selectedInput = null;
let selectedOutput = null;

// ✅ Request MIDI Access
navigator.requestMIDIAccess()
    .then(access => {
        console.log("✅ MIDI Access Granted!");
        midiAccess = access;
        updateMIDIDevices();
        midiAccess.onstatechange = updateMIDIDevices;
    })
    .catch(err => console.error("🚨 MIDI API Error:", err));

// ✅ Update MIDI Input & Output Menus
function updateMIDIDevices() {
    let inputSelect = document.getElementById("midiInput");
    let outputSelect = document.getElementById("midiOutput");

    if (!inputSelect || !outputSelect) {
        console.error("❌ MIDI dropdowns not found!");
        return;
    }

    // Clear existing options
    inputSelect.innerHTML = '<option value="">Select MIDI Input</option>';
    outputSelect.innerHTML = '<option value="">Select MIDI Output</option>';

    // Populate MIDI Input Devices
    midiAccess.inputs.forEach((input, key) => {
        let option = document.createElement("option");
        option.value = key;
        option.textContent = input.name || `MIDI Input ${key}`;
        inputSelect.appendChild(option);
    });

    // Populate MIDI Output Devices
    midiAccess.outputs.forEach((output, key) => {
        let option = document.createElement("option");
        option.value = key;
        option.textContent = output.name || `MIDI Output ${key}`;
        outputSelect.appendChild(option);
    });

    console.log("🎛 Updated MIDI Input & Output devices.");
}

// ✅ Handle MIDI Input Selection
function selectMIDIInput() {
    let inputSelect = document.getElementById("midiInput");
    let inputId = inputSelect.value;

    if (selectedInput) {
        selectedInput.onmidimessage = null;
    }

    if (midiAccess.inputs.has(inputId)) {
        selectedInput = midiAccess.inputs.get(inputId);
        selectedInput.onmidimessage = handleMIDIMessage;
        console.log(`🎤 MIDI Input Selected: ${selectedInput.name}`);
    }
}

// ✅ Handle MIDI Output Selection
function selectMIDIOutput() {
    let outputSelect = document.getElementById("midiOutput");
    let outputId = outputSelect.value;

    if (midiAccess.outputs.has(outputId)) {
        selectedOutput = midiAccess.outputs.get(outputId);
        console.log(`🎹 MIDI Output Selected: ${selectedOutput.name}`);
    }
}


// ✅ Play a MIDI Note Sent Back from Python
function playMIDINote(note, velocity) {
    if (!selectedOutput) {
        console.warn("⚠️ No MIDI output selected.");
        return;
    }

    let noteOnMessage = [0x90, note, velocity];  // Note On
    let noteOffMessage = [0x80, note, 0];  // Note Off

    console.log(`🎵 Playing Generated MIDI Note ${note}, Velocity ${velocity}`);

    try {
        selectedOutput.send(noteOnMessage);
        setTimeout(() => {
            selectedOutput.send(noteOffMessage);
            console.log(`🔇 Note Off ${note}`);
        }, 500);
    } catch (error) {
        console.error("🚨 Error playing MIDI note:", error);
    }
}

// ✅ Send MIDI Data to Python
// ✅ Handle Incoming MIDI Messages and Send to Python

let midiPhrase = [];  // ✅ Buffer to store incoming notes
let activeNotes = new Map();  // ✅ Track active notes with start times
let phraseTimeout = null;  // ✅ Timer to detect phrase end
let lastNoteOnTime = null;  // ✅ Track previous note-on time for inter-onset calculation

// ✅ Handle Incoming MIDI Messages (Phrase Buffering)
function handleMIDIMessage(event) {
    let command = event.data[0] & 0xf0; // Extract MIDI command
    let note = event.data[1];
    let velocity = event.data[2];
    let timestamp = performance.now(); // ✅ Get precise timing

    if (command === 0x90 && velocity > 0) {
        // ✅ Note On: Store start time and inter-onset interval
        let interOnsetTime = lastNoteOnTime ? timestamp - lastNoteOnTime : 0;
        lastNoteOnTime = timestamp;
        console.log(interOnsetTime);

        activeNotes.set(note, timestamp); // ✅ Store note start time

        midiPhrase.push({ note, velocity, start_time: timestamp, duration: null, inter_onset: interOnsetTime });

        console.log(`🎤 Note ON: ${note}, Velocity ${velocity}, IOT ${interOnsetTime}ms`);
    } 
    else if (command === 0x80 || (command === 0x90 && velocity === 0)) {
        // ✅ Note Off: Calculate duration and update phrase
        if (activeNotes.has(note)) {
            let startTime = activeNotes.get(note);
            let duration = timestamp - startTime;
            activeNotes.delete(note);

            // ✅ Find the note in the phrase and update duration
            let noteIndex = midiPhrase.findIndex(n => n.note === note && n.duration === null);
            if (noteIndex !== -1) {
                midiPhrase[noteIndex].duration = duration;
                console.log(`🎹 Note OFF: ${note}, Duration ${duration}ms`);
            }
        }

        // ✅ If no active notes, start phrase timeout (1s delay before sending)
        if (activeNotes.size === 0) {
            if (phraseTimeout) clearTimeout(phraseTimeout);
            phraseTimeout = setTimeout(sendPhraseToPython, 1000);
        }
    }
}

// ✅ Send the Phrase to Python After 1 Second of Silence
function sendPhraseToPython() {
    if (midiPhrase.length === 0 || activeNotes.size > 0) return; // ✅ Do not send if notes are still active

    console.log("📨 Sending MIDI phrase to Python:", midiPhrase);

    fetch("/midi_phrase", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ phrase: midiPhrase })
    })
    .then(response => response.json())
    .then(data => {
        console.log("📩 Python Response:", data);
        if (data.generated_phrase) {
            playGeneratedPhrase(data.generated_phrase);
        }
    })
    .catch(error => console.error("🚨 Error sending MIDI phrase:", error));

    // ✅ Clear the phrase buffer
    midiPhrase = [];
    lastNoteOnTime = null;

}

// ✅ Play Generated MIDI Phrase from Python (Keeping Timing)
function playGeneratedPhrase(phrase) {
    if (!selectedOutput) {
        console.warn("⚠️ No MIDI output selected.");
        return;
    }

    console.log("🎵 Playing Generated Phrase:", phrase);

let accumulatedDelay = 0;  // ✅ Track total delay from phrase start
phrase.forEach((noteData, index) => {
    accumulatedDelay += noteData.inter_onset; // ✅ Accumulate inter-onset time

    setTimeout(() => {
        let noteOnMessage = [0x90, noteData.note, noteData.velocity];
        let noteOffMessage = [0x80, noteData.note, 0];

        selectedOutput.send(noteOnMessage);
        console.log(`🎶 Note ON: ${noteData.note}, Velocity: ${noteData.velocity}, Start Delay: ${accumulatedDelay}ms`);

        setTimeout(() => {
            selectedOutput.send(noteOffMessage);
            console.log(`🔇 Note OFF: ${noteData.note}`);
        }, noteData.duration); // ✅ Use stored duration

    }, accumulatedDelay); // ✅ Use accumulated delay for precise timing
});
}


// ✅ Attach Generate Button Event
function attachButtonEvent() {
    let generateButton = document.getElementById("generateButton");

    if (generateButton) {
        console.log("✅ Generate button found! Attaching event listener...");

        generateButton.addEventListener("click", function () {
            console.log("🎹 Generate button clicked.");

            if (!selectedOutput) {
                alert("⚠️ Please select a MIDI Output first!");
                return;
            }

            let randomNote = 60 + Math.floor(Math.random() * 12);  // Random note from C4 to B4
            console.log(`🎵 Generating MIDI Note: ${randomNote}`);
            playMIDINote(randomNote, 100);
        });

    } else {
        console.log("⏳ Waiting for button to be available...");
        setTimeout(attachButtonEvent, 500);  // Try again in 500ms
    }
}

// ✅ Ensure the Button and Menus Are Loaded
window.onload = function() {
    console.log("✅ Page fully loaded. Checking for elements...");
    updateMIDIDevices();
    attachButtonEvent();
};
</script>

<!-- 🎛 MIDI Input & Output Selection -->
<div>
    <label for="midiInput">MIDI Input: </label>
    <select id="midiInput" onchange="selectMIDIInput()"></select>

    <label for="midiOutput">MIDI Output: </label>
    <select id="midiOutput" onchange="selectMIDIOutput()"></select>
</div>

<!-- 🎶 "Generate MIDI" Button -->

<button id="generateButton">🎵 Generate MIDI Note</button>

"""

# ✅ Inject JavaScript and HTML
with gr.Blocks() as demo:
    gr.HTML(midi_js)

# ✅ Mount FastAPI with Gradio
app = gr.mount_gradio_app(app, demo, path="/")

if __name__ == "__main__":
    import uvicorn

    print("🚀 Starting FastAPI with Gradio...")
    uvicorn.run(app, host="0.0.0.0", port=7860)