Reading ABC Notation

ABC Notation is a popular text-based notation for music. Many software applications already exist to e.g. convert to MIDI or typeset a musical score.

We can use ABC in scripts to create a Midi.Pattern or an entire Midi.Tune. In this example we'll create a Midi.Tune from a string that contains music written in ABC, in this case a short phrase by J.S.Bach.

We do this by putting the ABC in a multiline string and then using the Midi.ABCReader readTune method:


local abcTune = Midi.ABCReader().readTune(@"
X:1
T:Minuet (Bach)
C:J.S. Bach
M:3/4
L:1/8
K:D
A2DEFG|A2D2D2|B2GABc|d2D2D2|G2AGFE|F2GFED|E2DEFD|E2AGFE|
A2DEFG|A2D2D2|B2GABc|d2D2D2|G2AGFE|F2GFED|E2FEDE|D6||
")


Querying a Tune

Now the abcTune variable holds our new Midi.Tune, we can get information on the tune by calling various methods.

First we'll call for the title, and print it out:



local title = abcTune.title
print("Now playing: " + title + "\n")


Time Signature

Now we call the timeSignature method to get the time signature which is 3/4 time as designated in the "M:" field of the original notation and set the default clock to use this time signature:


Time.def.signature = abcTune.timeSignature


Playing Notes

To actually play the notes from the tune, we'll first create a plugin synthesizer and connect it to the system outputs:


local synth = Lv2.Plugin("http://calf.sourceforge.net/plugins/Monosynth", "Square Worm")
	=> Audio.StereoOutput("main", true)


We can retrieve the notes from the tune as via the track method which returns the given track as a Midi.Pattern:


local pattern = abcTune.track(1)

Now we can schedule the pattern to play on the synthesizer starting from the first measure of the transport:


synth.schedule(pattern, 1)
Time.def.start()



Complete Script

local abcTune = Midi.ABCReader().readTune(@"
X:1
T:Minuet (Bach)
C:J.S. Bach
M:3/4
L:1/8
K:D
A2DEFG|A2D2D2|B2GABc|d2D2D2|G2AGFE|F2GFED|E2DEFD|E2AGFE|
A2DEFG|A2D2D2|B2GABc|d2D2D2|G2AGFE|F2GFED|E2FEDE|D6||
")

local title = abcTune.title
print("Now playing: " + title + "\n")

Time.def.signature = abcTune.timeSignature

local synth = Lv2.Plugin("http://calf.sourceforge.net/plugins/Monosynth", "Square Worm")
	=> Audio.StereoOutput("main", true)

local pattern = abcTune.track(1)

synth.schedule(pattern, 1)
Time.def.start()


List of All ExamplesDemo Applications


Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.