Fork me on GitHub

Extending MirChord

A short guide to extend the notation functionality with music "logic".

We have seen in the notation language page that the interpreter provides by default a lot of built-in useful tranformations but sometimes we could have the necessity to create our own functions.
In MirChord it is possible to extend the interpreter processing capabilities with custom transformations by means of groovy methods marked with the special annotation @MirChord.

This section requires a basic knowledge of the Groovy programming language (or Java since most Java code is also valid Groovy code)

Example

Transposition is available from the standard set of built-in functions but as a little exercise we now define it again as a custom transformation. First, you should create in the src folder a groovy file named Transformations.groovy and paste the following code.


    @MirChord 
    public List<MusicElement> transpose(int halfSteps, List<MusicElement> phrase) {
        List<MusicElement> newPhrase = phrase.stream()
                .filter({ el -> el.isCopyable() })
                .map({ el -> el.copy() })
                .collect(Collectors.toList()) as List<MusicElement>
        for(MusicElement el : newPhrase) {
            if (el.getMusicElementType() == "Chord") {
                Chord chord = (Chord)el
                for(Pitch pitch : chord.getPitches()) {
                    pitch.setMidiValue(pitch.getMidiValue() + halfSteps)
                }
            }
        }
        return newPhrase
    }
    

Now we can use the new defined transformation inside our MirChord scores using the include command.


    =1 ~1
    (instr "flute")
    (key "D" "min")

    ^4 f4 g a f | f4 g a f | a4 b c2 | a4 b c2
                
    ^5 c8 d c b a4 f | ^5 c8 d c b a4 f

    ^4 f4 c f2 | f4 c f2  

    (include "src/Transformations.groovy")

    (myTranspose 3 { ^4 f4 c f2 | f4 c f2 })