By exporting sheet music from MuseScore and other programs into uncompressed MusicXML format, I was able to easily convert it into a list usable in Sonic Pi using Python’s music21 library.
I used Gemini to assist with the programming.
I’m using macOS terminal.
Usage
python xml2noteC.py < MusicXMLtest.musicxml
xml2noteC.py
import sys
from music21 import converter, note, chorddef main():
input_binary = sys.stdin.buffer.read()
if not input_binary:
returntry: score = converter.parse(input_binary) except Exception as e: sys.stderr.write(f"Error parsing input: {e}\n") sys.exit(1) score_tied = score.flatten().stripTies() elements = score_tied.notesAndRests results = [] for el in elements: duration = float(el.duration.quarterLength) duration = round(duration, 3) if duration % 1 != 0 else int(duration) if isinstance(el, chord.Chord): pitches = [f":{p.nameWithOctave.replace('#', 's').replace('-', 'b')}" for p in el.pitches] content = f"[{','.join(pitches)}]" elif isinstance(el, note.Note): p_name = el.pitch.nameWithOctave.replace('#', 's').replace('-', 'b') content = f":{p_name}" elif isinstance(el, note.Rest): content = ":r" results.append(f"[{content},{duration}]") if results: print("[ ",end="") print(",".join(results)+" ]")if name == “main”:
main()
Output
[ [:C4,1],[:Cs5,1],[:Db4,1],[:G4,2],[:r,1],[[:C4,:E4,:G4,:C5],0.75],[[:D4,:Fs4,:A4,:D5],1.25] ]
#Usage on Sonic Pi
a=[ [:C4,1],[:Cs5,1],[:Db4,1],[:G4,2],[:r,1],[[:C4,:E4,:G4,:C5],0.75],[[:D4,:Fs4,:A4,:D5],1.25] ]
a.each do |i,j|
play i
sleep j
end
MusicXMLtest
Explanation in Japanese
https://shakuhachi.sub.jp/SonicPi/#MusicalNotatonToSonicPi
