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,
},
*/
];