Jump to content

Guitar Strum Script for Logic Pro X


dentpuzz

Recommended Posts

I found myself in need of a guitar strum so I wrote this. 

Play your chord within the detection area and use strum up and down key to perform the strum.  velocity is set to how hard you hit the strum key, not the chord.

If this helps, please consider buying me a coffee...

//------------------------------------------------------------------------------
// Guitar Strummer 2.  Dennis Caunce 2023. https://www.buymeacoffee.com/dentpuzz
//------------------------------------------------------------------------------

//Set up keys to use here  Next version will have UI


var strumUpKey = 96; // Key to strum up (default: 96)
var strumDownKey = 97; // Key to strum down (default: 97)
var lowNote = 52; // Low note for the detection area
var highNote = 94; // High note for the detection area

//End of Set UI Section

var strumSpeed = 100; // Strumming speed (default: 100 ms)

var heldNotes = {}; // Object to store held notes
var triggerVelocity = 0; // Velocity to use for strumming

function HandleMIDI(e) {
    if (e instanceof NoteOn) {
        var note = e.pitch;
        var noteValue = parseInt(note);

        if (noteValue >= lowNote && noteValue <= highNote) {
            heldNotes[noteValue] = e.velocity;
            // Don't send the NoteOn events within the detection area
            return;
        }
    } else if (e instanceof NoteOff) {
        var note = e.pitch;
        var noteValue = parseInt(note);
        delete heldNotes[noteValue];
    }

    if (e instanceof NoteOn && (e.pitch === strumUpKey || e.pitch === strumDownKey)) {
        e.trace("Strum key pressed: " + e.pitch);
        triggerVelocity = e.velocity; // Store the trigger key velocity
        strumChord(heldNotes, e.pitch);
    }

    if (e.send()) {
        // Let all other events pass through
    }
}

function strumChord(chord, strumKey) {
    var notes = Object.keys(chord).map(function (x) {
        return parseInt(x);
    });

    var delay = 0;
    
    var strumSpeed = GetParameter("Strum Speed"); // Get the strum speed from the slider


    if (strumKey === strumUpKey) {
        notes.sort(function (a, b) {
            return a - b;
        });
    } else if (strumKey === strumDownKey) {
        notes.sort(function (a, b) {
            return b - a;
        });
    }

    for (var i = 0; i < notes.length; i++) {
        var noteToPlay = new NoteOn;
        noteToPlay.pitch = notes[i];
        noteToPlay.velocity = triggerVelocity; // Set the strum velocity to the trigger key velocity
        noteToPlay.sendAfterMilliseconds(delay);
        delay += strumSpeed;
    }
}

var PluginParameters = [
    {
        name: "Strum Speed",
        type: "lin",
        unit: "ms",
        minValue: 1,
        maxValue: 1000,
        numberOfSteps: 999,
        defaultValue: 8,
    }/*,
    {
        name: "Low Note",
        type: "lin",
        minValue: 21,
        maxValue: 106,
        numberOfSteps: 85,
        defaultValue: 52,
    },
    {
        name: "High Note",
        type: "lin",
        minValue: 21,
        maxValue: 106,
        numberOfSteps: 85,
        defaultValue: 94,
    },
    */
];

 

  • Like 4
Link to comment
Share on other sites

  • 2 weeks later...

There is, but it doesn't work in the same way.

Update to add octave layer for a thicker, more realistic sound.

//------------------------------------------------------------------------------
// Guitar Strummer 2.5 Dennis Caunce 2023. https://www.buymeacoffee.com/dentpuzz
//------------------------------------------------------------------------------

// Set up keys to use here Next version will have UI

var strumUpKey = 96; // Key to strum up (default: 96)
var strumDownKey = 97; // Key to strum down (default: 97)
var lowNote = 52; // Low note for the detection area
var highNote = 94; // High note for the detection area

// End of Set UI Section

var strumSpeed = 100; // Strumming speed (default: 100 ms)

var heldNotes = {}; // Object to store held notes
var triggerVelocity = 0; // Velocity to use for strumming

function HandleMIDI(e) {
    if (e instanceof NoteOn) {
        var note = e.pitch;
        var noteValue = parseInt(note);

        if (noteValue >= lowNote && noteValue <= highNote) {
            heldNotes[noteValue] = e.velocity;
            // Don't send the NoteOn events within the detection area
            return;
        }
    } else if (e instanceof NoteOff) {
        var note = e.pitch;
        var noteValue = parseInt(note);
        delete heldNotes[noteValue];

        var octaveDown = GetParameter("Octave Down"); // Get the value of the "Octave Down" checkbox

        if (octaveDown) {
            var noteOffLower = new NoteOff;
            noteOffLower.pitch = noteValue + 12; // Transpose one octave down
            noteOffLower.sendAfterMilliseconds(0);
        }
    }

    if (e instanceof NoteOn && (e.pitch === strumUpKey || e.pitch === strumDownKey)) {
        e.trace("Strum key pressed: " + e.pitch);
        triggerVelocity = e.velocity; // Store the trigger key velocity
        strumChord(heldNotes, e.pitch);
    }

    if (e.send()) {
        // Let all other events pass through
    }
}

function strumChord(chord, strumKey) {
    var notes = Object.keys(chord).map(function (x) {
        return parseInt(x);
    });

    var delay = 0;

    var strumSpeed = GetParameter("Strum Speed"); // Get the strum speed from the slider

    var octaveDown = GetParameter("Octave Down"); // Get the value of the "Octave Down" checkbox

    for (var i = 0; i < notes.length; i++) {
        var noteToPlay = new NoteOn;
        noteToPlay.pitch = notes;
        noteToPlay.velocity = triggerVelocity;
        noteToPlay.sendAfterMilliseconds(delay);
        delay += strumSpeed;

        if (octaveDown) {
            var noteLower = new NoteOn;
            noteLower.pitch = notes + 12; // Transpose one octave down
            noteLower.velocity = triggerVelocity;
            noteLower.sendAfterMilliseconds(delay);
        }
    }
}

var PluginParameters = [
    {
        name: "Strum Speed",
        type: "lin",
        unit: "ms",
        minValue: 1,
        maxValue: 1000,
        numberOfSteps: 999,
        defaultValue: 8,
    },
    {
        name: "Octave Down",
        type: "checkbox",
        defaultValue: 0,
    },
];
 

 
  • Like 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...