Creating and Scheduling MIDI Notes

In this example we will make a simple drum beat by scheduling MIDI notes on an LV2 sampler.

Audio Setup

The first step is to create the sampler, in this case we have chosen the Fabla sampler from OpenAV Productions. The plugin URI alone is enough to create any LV2 plugin with its default configuration but we can also optionally specify a plugin preset at creation time, which is a configuration that has been predefined by the plugin creators.

In this case we will use the "fabla808" preset which loads fabla with a set of classic 808-sounding samples:



local sampler = Lv2.Plugin("http://www.openavproductions.com/fabla", "fabla808")
sampler.control("volume", 0.7)



Now we connect the sampler to the system outputs:



sampler => Audio.StereoOutput("main", true)


Now we have a sampler, samples loaded, connected directly to the system playback channels. This configuration is a simpler than that in the basic connections example, for instance we have not connected any sort of input.

With no inputs we cannot play the sampler with a keyboard or other controller. How can we play it? We can create MIDI notes and send them to the sampler programmatically, from within the script.

Creating Notes

When we create a MIDI note we specify the following values:

Here's how we create the notes that we need:



local kickNote = Midi.Note(37, 127, 0.25)
local clapNote = Midi.Note(38, 127, 0.25)
local maracasNote = Midi.Note(47, 127, 0.25)


In this case we have created three new variables and assigned them to three new notes, all of which have a velocity of 127 and a length of one quarter note. The notes differ in pitch value, e.g. the "clap" sound is note number 38 in the fabla808 kit.

Scheduling Notes

Now in order to play these notes we use the schedule method of the LV2 plugin. As the name implies we are not playing the notes in real time but rather scheduling them to play at a particular time in the transport.

We want to play the same notes for several measures so we make an outer for loop to count the measures and then schedule the notes for each measure within this loop. Similarly we make an inner for loop to count the beats within each measure and schedule notes on each beat.



for(local measure = 1; measure <= 16; measure++) {
    // clap on 2nd and 4th beat
    sampler.schedule(clapNote, measure + 0.25)
    sampler.schedule(clapNote, measure + 0.75)
    // kick on each beat, maracas on each offbeat
    for(local b = 0; b < 4; b++) {
        sampler.schedule(kickNote, measure + b/4.0)
        sampler.schedule(maracasNote, measure + (b * 2 + 1)/8.0)
    }
}


Playing the Notes

Now to start the default clock:



Time.def.start()



Complete Script

local sampler = Lv2.Plugin("http://www.openavproductions.com/fabla", "fabla808")
sampler.control("volume", 0.7)

sampler => Audio.StereoOutput("main", true)

local kickNote = Midi.Note(37, 127, 0.25)
local clapNote = Midi.Note(38, 127, 0.25)
local maracasNote = Midi.Note(47, 127, 0.25)

for(local measure = 1; measure <= 16; measure++) {
    // clap on 2nd and 4th beat
    sampler.schedule(clapNote, measure + 0.25)
    sampler.schedule(clapNote, measure + 0.75)
    // kick on each beat, maracas on each offbeat
    for(local b = 0; b < 4; b++) {
        sampler.schedule(kickNote, measure + b/4.0)
        sampler.schedule(maracasNote, measure + (b * 2 + 1)/8.0)
    }
}

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.