dentpuzz Posted October 25, 2023 Share Posted October 25, 2023 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, }, */ ]; 4 Quote Link to comment Share on other sites More sharing options...
Atlas007 Posted October 25, 2023 Share Posted October 25, 2023 Thanx for sharing! I think there’s already one provided… Quote Link to comment Share on other sites More sharing options...
dentpuzz Posted November 7, 2023 Author Share Posted November 7, 2023 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, }, ]; 2 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.