Skip to content

Instantly share code, notes, and snippets.

@tomaslin
Created July 18, 2026 17:59
Show Gist options
  • Select an option

  • Save tomaslin/cd1657e48dd42841a1dc731922a95b51 to your computer and use it in GitHub Desktop.

Select an option

Save tomaslin/cd1657e48dd42841a1dc731922a95b51 to your computer and use it in GitHub Desktop.
Music transcription from MP3
#!/bin/bash
# transcribe.sh
# Usage: ./transcribe.sh path/to/input.mp3
set -euo pipefail
INPUT_AUDIO="${1:-}"
if [ -z "$INPUT_AUDIO" ]; then
echo "❌ Error: Missing input audio file target."
echo "Usage: $0 <path_to_audio_file.mp3>"
exit 1
fi
echo "=== 🎼 Deploying Masterpiece-Grade Context-Aware Orchestration Engine ==="
if [ -d "/opt/homebrew/bin" ]; then export PATH="/opt/homebrew/bin:$PATH"; fi
if command -v python3 &> /dev/null; then PY_CMD="python3"; else echo "❌ Error: Python 3 required."; exit 1; fi
OUT_DIR="./output_profound"
mkdir -p "$OUT_DIR"
cleanup() { rm -f run_profound.py; }
trap cleanup EXIT
ENV_DIR=".venv_profound"
if [ ! -d "$ENV_DIR" ]; then $PY_CMD -m venv "$ENV_DIR"; fi
source "$ENV_DIR/bin/activate"
pip install --upgrade pip --quiet
pip install coremltools onnxruntime librosa music21 scipy scikit-learn --quiet
pip install "demucs-mlx[convert]" --quiet
pip install basic-pitch --no-deps --quiet
pip install piano-transcription-inference --quiet
# ==========================================
# ADVANCED CONTEXTUAL ENGINE CORE
# ==========================================
cat << 'EOF' > run_profound.py
import sys
import numpy as np
from pathlib import Path
import scipy.signal
import librosa
from demucs_mlx.api import DemucsPredictor
from basic_pitch.inference import predict
from basic_pitch import ICASSP_2022_MODEL_PATH
from piano_transcription_inference import PianoTranscription
from music21 import converter, instrument, stream, tempo, key, note, chord, articulations, metadata, clef, harmony, expressions
def profile_ensemble_interaction(audio_path):
"""Profiles the tracking audio to determine structural style maps and interactive grids."""
y, sr = librosa.load(str(audio_path), duration=45.0, sr=None)
# Track beat alignments to measure performance fluidity
tempo_bpm, beat_frames = librosa.beat.beat_track(y=y, sr=sr)
beat_times = librosa.frames_to_time(beat_frames, sr=sr)
intervals = np.diff(beat_times)
jitter = np.std(intervals) if len(intervals) > 2 else 0.05
stft = np.abs(librosa.stft(y))
freqs = librosa.fft_frequencies(sr=sr)
sub_bass = np.sum(stft[(freqs >= 25) & (freqs <= 65), :])
mid_range = np.sum(stft[(freqs >= 150) & (freqs <= 400), :]) + 1e-6
ratio = sub_bass / mid_range
profile = {
"bpm": round(float(tempo_bpm[0]) if isinstance(tempo_bpm, (np.ndarray, list)) else float(tempo_bpm), 1),
"is_electronic": jitter < 0.0008 or ratio > 4.5,
"is_tango_or_latin": ratio > 1.8 and (0.008 < jitter < 0.025),
"context_tags": []
}
if profile["is_electronic"]:
profile["context_tags"] = ["Electronic Sequencer Grid", "Strict Quantization Map"]
elif profile["is_tango_or_latin"]:
profile["context_tags"] = ["Afro-Latin/Tango Syncopated Anchor", "Marcato Phrasing Grid"]
else:
profile["context_tags"] = ["Fluid Human Ensemble Contour", "Rubato Performance Timeline"]
return profile
def optimize_piano_hand_tracking(raw_piano_stream):
"""Orchestrates piano layers using dynamic travel distances across the staves."""
right_hand = stream.Part(id="Piano_Right")
right_hand.insert(0, instrument.Piano())
left_hand = stream.Part(id="Piano_Left")
left_hand.insert(0, instrument.Piano())
left_hand.insert(0, clef.BassClef())
flat_notes = sorted(list(raw_piano_stream.flatten().notes), key=lambda n: n.offset)
rh_center, lh_center = 72, 48
for el in flat_notes:
if isinstance(el, note.Note):
p_midi = el.pitch.midi
cost_rh = abs(p_midi - rh_center)
cost_lh = abs(p_midi - lh_center)
if cost_rh < cost_lh or p_midi >= 60:
right_hand.insert(el.offset, el)
rh_center = 0.7 * rh_center + 0.3 * p_midi
else:
left_hand.insert(el.offset, el)
lh_center = 0.7 * lh_center + 0.3 * p_midi
elif isinstance(el, chord.Chord):
pitches = [p.midi for p in el.pitches]
if (max(pitches) - min(pitches)) > 14: # Reach threshold limit rule
rh_p = [p for p in el.pitches if p.midi >= 60]
lh_p = [p for p in el.pitches if p.midi < 60]
if rh_p: right_hand.insert(el.offset, chord.Chord(rh_p, duration=el.duration, offset=el.offset))
if lh_p: left_hand.insert(el.offset, chord.Chord(lh_p, duration=el.duration, offset=el.offset))
else:
if np.mean(pitches) >= 57: right_hand.insert(el.offset, el)
else: left_hand.insert(el.offset, el)
right_hand.makeVoices(inPlace=True)
left_hand.makeVoices(inPlace=True)
return right_hand, left_hand
def main():
audio_path = Path(sys.argv[1]).resolve()
out_dir = Path("./output_profound").resolve()
# 1. GENERATE MUSICOLOGICAL INTERACTION PROFILE
profile = profile_ensemble_interaction(audio_path)
print("\n[1/5] Splitting audio tracks via htdemucs_6s...")
predictor = DemucsPredictor(model="htdemucs_6s")
stems = predictor.separate(audio_path)
stem_paths = {}
for name in ['bass', 'drums', 'piano', 'guitar']:
stem_paths[name] = out_dir / f"raw_{audio_path.stem}_{name}.wav"
predictor.save_stem(stems.get(name), stem_paths[name])
master_score = stream.Score()
# Inject profound musicological headers directly into layout metadata
desc_title = f"Context Map: {', '.join(profile['context_tags'])} | Conductor Baseline: {profile['bpm']} BPM"
master_score.metadata = metadata.Metadata(
title=f"{audio_path.stem.replace('_', ' ').title()}",
composer="Transcribed via Deep Profound Orchestration Engine",
alternativeTitle=desc_title
)
master_score.insert(0, tempo.MetronomeMark(number=round(profile["bpm"], 1)))
# --- TRACK 1: PERFORMANCE-AWARE BASS SYSTEM ---
print("\n[2/5] Running bass tracking passes...")
y_bass, sr_bass = librosa.load(str(stem_paths['bass']), sr=None)
min_b, max_b = (25.0, 220.0) if not profile["is_electronic"] else (30.0, 140.0)
_, bass_midi, _ = predict(str(stem_paths['bass']), ICASSP_2022_MODEL_PATH, minimum_frequency=min_b, maximum_frequency=max_b)
tmp_b = out_dir / "tmp_b.mid"; bass_midi.write(str(tmp_b))
bass_part = converter.parse(tmp_b).getElementsByClass(stream.Part)[0]
bass_part.id = "BassLine"
bass_part.insert(0, instrument.BassGuitar())
tmp_b.unlink()
# Inject instrument-specific layout string suggestions to sheet layout
for n in bass_part.flatten().notes:
if isinstance(n, note.Note):
if n.pitch.midi <= 32: n.articulations.append(note.Lyric(text="Str: E"))
elif n.pitch.midi <= 37: n.articulations.append(note.Lyric(text="Str: A"))
elif n.pitch.midi <= 42: n.articulations.append(note.Lyric(text="Str: D"))
else: n.articulations.append(note.Lyric(text="Str: G"))
master_score.insert(0, bass_part)
# --- TRACK 2 & 3: ADAPTIVE PIANO STAVES ---
print("[3/5] Deploying piano grand staff voice sorting algorithms...")
transcriptor = PianoTranscription(device='cpu', checkpoint_path=None)
p_mid = out_dir / "tmp_p.mid"
transcriptor.transcribe(str(stem_paths['piano']), str(p_mid))
raw_piano = converter.parse(p_mid)
p_right, p_left = optimize_piano_hand_tracking(raw_piano)
master_score.insert(0, p_right)
master_score.insert(0, p_left)
p_mid.unlink()
# --- TRACK 4 & 5: GUITAR STRUCTURAL POSITION SORTING ---
print("[4/5] Separating guitar tracks with physical position layout markers...")
_, guit_midi, _ = predict(str(stem_paths['guitar']), ICASSP_2022_MODEL_PATH, minimum_frequency=75.0, maximum_frequency=1000.0)
tmp_g = out_dir / "tmp_g.mid"; guit_midi.write(str(tmp_g))
raw_guit = converter.parse(tmp_g).getElementsByClass(stream.Part)[0]
tmp_g.unlink()
lead_guitar = stream.Part(id="LeadGuitar")
lead_guitar.insert(0, instrument.Guitar())
rhythm_guitar = stream.Part(id="RhythmGuitar")
rhythm_guitar.insert(0, instrument.Guitar())
for el in raw_guit.flatten().notes:
if isinstance(el, chord.Chord):
rhythm_guitar.insert(el.offset, el)
else:
# Inject physical hand fret location hints directly onto the lead staff
if el.pitch.midi >= 69: el.articulations.append(note.Lyric(text="V pos."))
elif el.pitch.midi >= 74: el.articulations.append(note.Lyric(text="VIII pos."))
lead_guitar.insert(el.offset, el)
master_score.insert(0, lead_guitar)
master_score.insert(0, rhythm_guitar)
# --- TRACK 6: DYNAMIC PERCUSSION INTERACTION KIT ---
y_drums, sr_drums = librosa.load(str(stem_paths['drums']), sr=None)
drum_part = stream.Part(id="DrumKit")
drum_part.insert(0, instrument.Drums())
stft = np.abs(librosa.stft(y_drums))
freqs = librosa.fft_frequencies(sr=sr_drums)
kick_env = np.sum(stft[freqs < 85, :], axis=0)
snare_env = np.sum(stft[(freqs >= 180) & (freqs <= 1800), :], axis=0)
for env, drum_note in [(kick_env, "C4"), (snare_env, "D4")]:
prom_val = 0.35 if profile["is_electronic"] else 0.15
peaks, _ = scipy.signal.find_peaks(env, distance=12, prominence=np.max(env)*prom_val)
times = librosa.frames_to_time(peaks, sr=sr_drums)
for idx, t in enumerate(times):
offset_pos = (t * profile["bpm"]) / 60.0
d_note = note.Note(drum_note)
d_note.duration.quarterLength = 1.0
drum_part.insert(offset_pos, d_note)
# Structural Rehearsal Mark Breaks: drop visual numbers at phrase shifts
if idx > 0 and idx % 32 == 0:
master_score.getElementsByClass(stream.Part)[0].insert(offset_pos, expressions.RehearsalMark(int(idx // 32)))
master_score.insert(0, drum_part)
# --- 5. AUTOMATED QUANTIZATION & NOTATION GRID ---
print("\n[5/5] Aligning structural layout grids and master engraver templates...")
detected_key = master_score.analyze('key')
try:
merged_chords = master_score.chordify()
for el in merged_chords.flatten().notes:
if isinstance(el, chord.Chord):
master_score.getElementsByClass(stream.Part)[0].insert(el.offset, harmony.ChordSymbol(el))
except:
pass
for p in master_score.getElementsByClass(stream.Part):
p.insert(0, key.Key(detected_key.tonic.name, detected_key.mode))
# Apply specific performance expressions based on stylistic signatures
if profile["is_tango_or_latin"]:
p.insert(0, expressions.TextExpression("Marcato Phrasing Grid - Keep Syncopations Locked"))
p.makeRests(fillGaps=True, inPlace=True)
p.makeBeams(inPlace=True)
p.makeTies(inPlace=True)
divs = (4, 8, 16) if profile["is_electronic"] else (2, 4, 8)
p.quantize(quarterLengthDivisors=divs, inPlace=True)
final_xml = out_dir / f"{audio_path.stem}_ProfoundMasterScore.musicxml"
master_score.write('musicxml', fp=str(final_xml))
print(f"\n🎉 Process Complete! Definitive contextual score delivered:\n👉 {final_xml}\n")
if __name__ == "__main__":
main()
EOF
python run_profound.py "$INPUT_AUDIO"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment