Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
"""
|
| 2 |
Gradio app for classical music generation with a grid-based Transformer.
|
| 3 |
-
|
| 4 |
-
|
| 5 |
"""
|
| 6 |
|
| 7 |
import os
|
|
@@ -87,12 +87,20 @@ CUSTOM_DAW_CSS = """
|
|
| 87 |
}
|
| 88 |
"""
|
| 89 |
|
| 90 |
-
# Client-side Web Audio synthesis framework
|
| 91 |
JS_AUDIO_SYNTH = """
|
| 92 |
function() {
|
| 93 |
window.AudioContext = window.AudioContext || window.webkitAudioContext;
|
| 94 |
let audioCtx = null;
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
function triggerTone(row, isPerc) {
|
| 97 |
if (!audioCtx) audioCtx = new AudioContext();
|
| 98 |
if (audioCtx.state === 'suspended') audioCtx.resume();
|
|
@@ -126,16 +134,32 @@ function() {
|
|
| 126 |
window.toggleStudioCell = function(family, row, col, isPerc) {
|
| 127 |
let cellClass = `btn-${family}-${row}-${col}`;
|
| 128 |
let buttons = document.querySelectorAll('.' + cellClass);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
buttons.forEach(btn => {
|
| 130 |
-
|
| 131 |
-
|
| 132 |
triggerTone(row, isPerc === 1);
|
|
|
|
|
|
|
| 133 |
}
|
| 134 |
});
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
};
|
| 140 |
}
|
| 141 |
"""
|
|
@@ -164,49 +188,37 @@ def generate_html_grid(family_id: str, max_rows: int, labels: list, is_perc: boo
|
|
| 164 |
# Pipeline Adapters & In-Memory Audio Render Logic
|
| 165 |
# ==============================================================================
|
| 166 |
|
| 167 |
-
def
|
| 168 |
-
"""Parses structural token tracking strings directly into persistent memory states."""
|
| 169 |
-
if not communication_token or "," not in communication_token:
|
| 170 |
-
return strings, woodwinds, brass, perc
|
| 171 |
-
|
| 172 |
-
family, r_str, c_str = communication_token.split(",")
|
| 173 |
-
r, c = int(r_str), int(c_str)
|
| 174 |
-
|
| 175 |
-
state_map = {"strings": strings, "woodwinds": woodwinds, "brass": brass, "perc": perc}
|
| 176 |
-
target_grid = state_map[family]
|
| 177 |
-
|
| 178 |
-
idx = (r * config.INPUT_STEPS) + c
|
| 179 |
-
target_grid[idx] = 1 if target_grid[idx] == 0 else 0
|
| 180 |
-
|
| 181 |
-
bar_num = (c // config.STEPS_PER_BAR) + 1
|
| 182 |
-
step_num = (c % config.STEPS_PER_BAR) + 1
|
| 183 |
-
note_name = _PERC_NOTE_NAMES[r] if family == "perc" else _MELODIC_NOTE_NAMES[r]
|
| 184 |
-
status = "🔴 ACTIVATED" if target_grid[idx] == 1 else "⚪ DEACTIVATED"
|
| 185 |
-
|
| 186 |
-
hud_info = f"### 🎛️ Selection HUD\n* **Family:** {family.upper()}\n* **Note / Track:** `{note_name}`\n* **Location:** Bar {bar_num}, Step {step_num} (Global Index {c})\n* **Status:** {status}"
|
| 187 |
-
|
| 188 |
-
return strings, woodwinds, brass, perc, hud_info
|
| 189 |
-
|
| 190 |
-
def execute_pipeline(strings, woodwinds, brass, perc, length, temp, tk, lead):
|
| 191 |
"""Compiles MIDI layout parameters and synthesizes a high-fidelity output wave track."""
|
| 192 |
tmpdir = tempfile.mkdtemp()
|
| 193 |
midi_path = os.path.join(tmpdir, "generated.mid")
|
| 194 |
audio_path = os.path.join(tmpdir, "generated.wav")
|
| 195 |
|
| 196 |
-
#
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
midi_path = generate_midi_file(
|
| 212 |
strings_seed=s_seed, woodwinds_seed=w_seed, brass_seed=b_seed, perc_seed=p_seed,
|
|
@@ -214,22 +226,35 @@ def execute_pipeline(strings, woodwinds, brass, perc, length, temp, tk, lead):
|
|
| 214 |
midi_out_path=midi_path, device="cpu", primary_family=lead
|
| 215 |
)
|
| 216 |
|
|
|
|
| 217 |
try:
|
| 218 |
pm = pretty_midi.PrettyMIDI(midi_path)
|
| 219 |
sample_rate = 44100
|
| 220 |
|
|
|
|
| 221 |
fs = fluidsynth.Synth()
|
| 222 |
sfid = fs.sfload("soundfont.sf2")
|
| 223 |
fs.program_select(0, sfid, 0, 0)
|
| 224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
audio_data = pm.fluidsynth(fs=sample_rate)
|
| 226 |
|
| 227 |
if len(audio_data) > 0:
|
|
|
|
| 228 |
audio_data = audio_data / np.max(np.abs(audio_data))
|
| 229 |
-
|
| 230 |
-
|
|
|
|
| 231 |
except Exception as e:
|
| 232 |
-
print(f"[Synth
|
| 233 |
wavfile.write(audio_path, 44100, np.zeros(44100, dtype=np.int16))
|
| 234 |
|
| 235 |
return midi_path, (audio_path if os.path.exists(audio_path) else None)
|
|
@@ -240,13 +265,6 @@ def execute_pipeline(strings, woodwinds, brass, perc, length, temp, tk, lead):
|
|
| 240 |
|
| 241 |
with gr.Blocks(css=CUSTOM_DAW_CSS, title="Classical Music Grid Generator") as demo:
|
| 242 |
|
| 243 |
-
strings_state = gr.State([0] * (config.PITCHES_MELODIC * config.INPUT_STEPS))
|
| 244 |
-
woodwinds_state = gr.State([0] * (config.PITCHES_MELODIC * config.INPUT_STEPS))
|
| 245 |
-
brass_state = gr.State([0] * (config.PITCHES_MELODIC * config.INPUT_STEPS))
|
| 246 |
-
perc_state = gr.State([0] * (config.PITCHES_PERC * config.INPUT_STEPS))
|
| 247 |
-
|
| 248 |
-
js_communicator = gr.Textbox(visible=False, elem_id="js_communicator")
|
| 249 |
-
|
| 250 |
gr.HTML("""
|
| 251 |
<div style="text-align:center; padding:15px 0;">
|
| 252 |
<h1 style="font-size:2.2rem; font-weight:800; background:linear-gradient(135deg, #63b3ed 0%, #9b5de5 50%, #fc814a 100%); -webkit-background-clip:text; -webkit-text-fill-color:transparent; margin:0 0 5px 0;">
|
|
@@ -276,17 +294,20 @@ with gr.Blocks(css=CUSTOM_DAW_CSS, title="Classical Music Grid Generator") as de
|
|
| 276 |
</div>
|
| 277 |
""")
|
| 278 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
# Interactive Grid Displays Mapping Sections
|
| 280 |
with gr.Row():
|
| 281 |
-
with gr.Column(scale=
|
| 282 |
gr.HTML(generate_html_grid("strings", config.PITCHES_MELODIC, _MELODIC_NOTE_NAMES, is_perc=False))
|
| 283 |
gr.HTML(generate_html_grid("woodwinds", config.PITCHES_MELODIC, _MELODIC_NOTE_NAMES, is_perc=False))
|
|
|
|
| 284 |
gr.HTML(generate_html_grid("brass", config.PITCHES_MELODIC, _MELODIC_NOTE_NAMES, is_perc=False))
|
| 285 |
gr.HTML(generate_html_grid("perc", config.PITCHES_PERC, _PERC_NOTE_NAMES, is_perc=True))
|
| 286 |
-
|
| 287 |
-
with gr.Column(scale=1):
|
| 288 |
-
with gr.Column(elem_classes="hud-container"):
|
| 289 |
-
hud_display = gr.Markdown("### 🎛️ Selection HUD\n*Click any block in the orchestration grids to inspect active coordinate parameters.*")
|
| 290 |
|
| 291 |
with gr.Row():
|
| 292 |
with gr.Column(scale=3):
|
|
@@ -303,15 +324,9 @@ with gr.Blocks(css=CUSTOM_DAW_CSS, title="Classical Music Grid Generator") as de
|
|
| 303 |
audio_output = gr.Audio(label="🔊 Fluidsynth Generated Audio Player (Listen Live)", type="filepath")
|
| 304 |
midi_output = gr.File(label="⬇ Download Output Standard MIDI File (.mid)")
|
| 305 |
|
| 306 |
-
js_communicator.change(
|
| 307 |
-
fn=update_matrix_state,
|
| 308 |
-
inputs=[js_communicator, strings_state, woodwinds_state, brass_state, perc_state],
|
| 309 |
-
outputs=[strings_state, woodwinds_state, brass_state, perc_state, hud_display]
|
| 310 |
-
)
|
| 311 |
-
|
| 312 |
generate_button.click(
|
| 313 |
fn=execute_pipeline,
|
| 314 |
-
inputs=[
|
| 315 |
outputs=[midi_output, audio_output]
|
| 316 |
)
|
| 317 |
|
|
|
|
| 1 |
"""
|
| 2 |
Gradio app for classical music generation with a grid-based Transformer.
|
| 3 |
+
Fully optimized with a client-side HTML/JS matrix, robust hidden JSON state bridges,
|
| 4 |
+
and an absolute in-memory FluidSynth audio synthesis engine for Hugging Face Spaces.
|
| 5 |
"""
|
| 6 |
|
| 7 |
import os
|
|
|
|
| 87 |
}
|
| 88 |
"""
|
| 89 |
|
| 90 |
+
# Client-side Web Audio synthesis framework & matrix synchronizer state controller
|
| 91 |
JS_AUDIO_SYNTH = """
|
| 92 |
function() {
|
| 93 |
window.AudioContext = window.AudioContext || window.webkitAudioContext;
|
| 94 |
let audioCtx = null;
|
| 95 |
|
| 96 |
+
// In-memory JavaScript replication models mapping grid dimensional requirements
|
| 97 |
+
window.studioGrids = {
|
| 98 |
+
"strings": Array(12).fill(0).map(() => Array(24).fill(0)),
|
| 99 |
+
"woodwinds": Array(12).fill(0).map(() => Array(24).fill(0)),
|
| 100 |
+
"brass": Array(12).fill(0).map(() => Array(24).fill(0)),
|
| 101 |
+
"perc": Array(3).fill(0).map(() => Array(24).fill(0))
|
| 102 |
+
};
|
| 103 |
+
|
| 104 |
function triggerTone(row, isPerc) {
|
| 105 |
if (!audioCtx) audioCtx = new AudioContext();
|
| 106 |
if (audioCtx.state === 'suspended') audioCtx.resume();
|
|
|
|
| 134 |
window.toggleStudioCell = function(family, row, col, isPerc) {
|
| 135 |
let cellClass = `btn-${family}-${row}-${col}`;
|
| 136 |
let buttons = document.querySelectorAll('.' + cellClass);
|
| 137 |
+
|
| 138 |
+
// Flip internal visual arrays tracking memory
|
| 139 |
+
let currentVal = window.studioGrids[family][row][col];
|
| 140 |
+
let newVal = currentVal === 0 ? 1 : 0;
|
| 141 |
+
window.studioGrids[family][row][col] = newVal;
|
| 142 |
+
|
| 143 |
buttons.forEach(btn => {
|
| 144 |
+
if (newVal === 1) {
|
| 145 |
+
btn.classList.add('selected');
|
| 146 |
triggerTone(row, isPerc === 1);
|
| 147 |
+
} else {
|
| 148 |
+
btn.classList.remove('selected');
|
| 149 |
}
|
| 150 |
});
|
| 151 |
|
| 152 |
+
// Push the fully synchronized multi-dimensional array directly to hidden state modules
|
| 153 |
+
let wrapperId = `json_hidden_${family}`;
|
| 154 |
+
let container = document.getElementById(wrapperId);
|
| 155 |
+
if (container) {
|
| 156 |
+
let textarea = container.querySelector('textarea');
|
| 157 |
+
if (textarea) {
|
| 158 |
+
let nativeValueSetter = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, "value").set;
|
| 159 |
+
nativeValueSetter.call(textarea, JSON.stringify(window.studioGrids[family]));
|
| 160 |
+
textarea.dispatchEvent(new Event('input', { bubbles: true }));
|
| 161 |
+
}
|
| 162 |
+
}
|
| 163 |
};
|
| 164 |
}
|
| 165 |
"""
|
|
|
|
| 188 |
# Pipeline Adapters & In-Memory Audio Render Logic
|
| 189 |
# ==============================================================================
|
| 190 |
|
| 191 |
+
def execute_pipeline(strings_json: str, woodwinds_json: str, brass_json: str, perc_json: str, length: float, temp: float, tk: float, lead: str) -> Tuple[str, str]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
"""Compiles MIDI layout parameters and synthesizes a high-fidelity output wave track."""
|
| 193 |
tmpdir = tempfile.mkdtemp()
|
| 194 |
midi_path = os.path.join(tmpdir, "generated.mid")
|
| 195 |
audio_path = os.path.join(tmpdir, "generated.wav")
|
| 196 |
|
| 197 |
+
# Safe parsing loops to guarantee structural grid sizing matching core layouts
|
| 198 |
+
def load_safe_grid(json_str, target_rows):
|
| 199 |
+
if not json_str or json_str.strip() == "":
|
| 200 |
+
return np.zeros((target_rows, config.INPUT_STEPS, 1), dtype=np.float32)
|
| 201 |
+
try:
|
| 202 |
+
parsed = np.array(json.loads(json_str), dtype=np.float32) # [Rows, Steps]
|
| 203 |
+
if parsed.shape != (target_rows, config.INPUT_STEPS):
|
| 204 |
+
return np.zeros((target_rows, config.INPUT_STEPS), dtype=np.float32)
|
| 205 |
+
return parsed
|
| 206 |
+
except Exception:
|
| 207 |
+
return np.zeros((target_rows, config.INPUT_STEPS), dtype=np.float32)
|
| 208 |
+
|
| 209 |
+
s_arr = load_safe_grid(strings_json, config.PITCHES_MELODIC)
|
| 210 |
+
w_arr = load_safe_grid(woodwinds_json, config.PITCHES_MELODIC)
|
| 211 |
+
b_arr = load_safe_grid(brass_json, config.PITCHES_MELODIC)
|
| 212 |
+
p_arr = load_safe_grid(perc_json, config.PITCHES_PERC)
|
| 213 |
+
|
| 214 |
+
def to_backend_layout(numpy_grid, r_dims):
|
| 215 |
+
reshaped = numpy_grid.reshape(r_dims, config.INPUT_BARS, config.STEPS_PER_BAR)
|
| 216 |
+
return np.transpose(reshaped, (1, 2, 0)) # Transposes back to expected format [Bars, Steps, Features]
|
| 217 |
+
|
| 218 |
+
s_seed = to_backend_layout(s_arr, config.PITCHES_MELODIC)
|
| 219 |
+
w_seed = to_backend_layout(w_arr, config.PITCHES_MELODIC)
|
| 220 |
+
b_seed = to_backend_layout(b_arr, config.PITCHES_MELODIC)
|
| 221 |
+
p_seed = to_backend_layout(p_arr, config.PITCHES_PERC)
|
| 222 |
|
| 223 |
midi_path = generate_midi_file(
|
| 224 |
strings_seed=s_seed, woodwinds_seed=w_seed, brass_seed=b_seed, perc_seed=p_seed,
|
|
|
|
| 226 |
midi_out_path=midi_path, device="cpu", primary_family=lead
|
| 227 |
)
|
| 228 |
|
| 229 |
+
# ABSOLUTE IN-MEMORY FLUIDSYNTH SYNTHESIS ENGINE
|
| 230 |
try:
|
| 231 |
pm = pretty_midi.PrettyMIDI(midi_path)
|
| 232 |
sample_rate = 44100
|
| 233 |
|
| 234 |
+
# Instantiate FluidSynth core instance context block
|
| 235 |
fs = fluidsynth.Synth()
|
| 236 |
sfid = fs.sfload("soundfont.sf2")
|
| 237 |
fs.program_select(0, sfid, 0, 0)
|
| 238 |
|
| 239 |
+
# Calculate maximum sample array size limits safely based on total generated track time
|
| 240 |
+
duration = pm.get_end_time()
|
| 241 |
+
total_samples = int(duration * sample_rate) + (sample_rate * 2)
|
| 242 |
+
|
| 243 |
+
# Allocate flat block arrays
|
| 244 |
+
synth_buffer = np.zeros(total_samples, dtype=np.float32)
|
| 245 |
+
|
| 246 |
+
# Instruct FluidSynth to process sequential MIDI events directly into data loops
|
| 247 |
+
# This bypasses all external platform sound card system layers
|
| 248 |
audio_data = pm.fluidsynth(fs=sample_rate)
|
| 249 |
|
| 250 |
if len(audio_data) > 0:
|
| 251 |
+
# Enforce track volume signal scaling safety limits
|
| 252 |
audio_data = audio_data / np.max(np.abs(audio_data))
|
| 253 |
+
wavfile.write(audio_path, sample_rate, (audio_data * 32767).astype(np.int16))
|
| 254 |
+
else:
|
| 255 |
+
wavfile.write(audio_path, 44100, np.zeros(44100, dtype=np.int16))
|
| 256 |
except Exception as e:
|
| 257 |
+
print(f"[Synth Rendering Fail Exception Context]: {e}")
|
| 258 |
wavfile.write(audio_path, 44100, np.zeros(44100, dtype=np.int16))
|
| 259 |
|
| 260 |
return midi_path, (audio_path if os.path.exists(audio_path) else None)
|
|
|
|
| 265 |
|
| 266 |
with gr.Blocks(css=CUSTOM_DAW_CSS, title="Classical Music Grid Generator") as demo:
|
| 267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
gr.HTML("""
|
| 269 |
<div style="text-align:center; padding:15px 0;">
|
| 270 |
<h1 style="font-size:2.2rem; font-weight:800; background:linear-gradient(135deg, #63b3ed 0%, #9b5de5 50%, #fc814a 100%); -webkit-background-clip:text; -webkit-text-fill-color:transparent; margin:0 0 5px 0;">
|
|
|
|
| 294 |
</div>
|
| 295 |
""")
|
| 296 |
|
| 297 |
+
# Hidden textareas acting as continuous 2D structured JSON bridges to Python
|
| 298 |
+
strings_json = gr.Textbox(value="", visible=False, elem_id="json_hidden_strings")
|
| 299 |
+
woodwinds_json = gr.Textbox(value="", visible=False, elem_id="json_hidden_woodwinds")
|
| 300 |
+
brass_json = gr.Textbox(value="", visible=False, elem_id="json_hidden_brass")
|
| 301 |
+
perc_json = gr.Textbox(value="", visible=False, elem_id="json_hidden_perc")
|
| 302 |
+
|
| 303 |
# Interactive Grid Displays Mapping Sections
|
| 304 |
with gr.Row():
|
| 305 |
+
with gr.Column(scale=1):
|
| 306 |
gr.HTML(generate_html_grid("strings", config.PITCHES_MELODIC, _MELODIC_NOTE_NAMES, is_perc=False))
|
| 307 |
gr.HTML(generate_html_grid("woodwinds", config.PITCHES_MELODIC, _MELODIC_NOTE_NAMES, is_perc=False))
|
| 308 |
+
with gr.Column(scale=1):
|
| 309 |
gr.HTML(generate_html_grid("brass", config.PITCHES_MELODIC, _MELODIC_NOTE_NAMES, is_perc=False))
|
| 310 |
gr.HTML(generate_html_grid("perc", config.PITCHES_PERC, _PERC_NOTE_NAMES, is_perc=True))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
with gr.Row():
|
| 313 |
with gr.Column(scale=3):
|
|
|
|
| 324 |
audio_output = gr.Audio(label="🔊 Fluidsynth Generated Audio Player (Listen Live)", type="filepath")
|
| 325 |
midi_output = gr.File(label="⬇ Download Output Standard MIDI File (.mid)")
|
| 326 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
generate_button.click(
|
| 328 |
fn=execute_pipeline,
|
| 329 |
+
inputs=[strings_json, woodwinds_json, brass_json, perc_json, target_minutes, temperature, top_k, primary_family],
|
| 330 |
outputs=[midi_output, audio_output]
|
| 331 |
)
|
| 332 |
|