Jump to content

finding the current beat position in a script


AinTunez

Recommended Posts

Okay, here's my issue. I have this script (below) that calculates every half beat and adds "swing" to any notes falling there. The trouble is, the beat position isn't always accurate. I found a partial solution in this thread (viewtopic.php?t=128908), which seemed to work at first, but the values are all thrown off when a different track is selected. Help! What's the best way to find the current position so I can detect notes falling on half beats?

 

PluginParameters = [{name: 'Swing %', minValue: 0, maxValue: 30, numberOfSteps: 60, defaultValue: 20},];

NeedsTimingInfo = true;
var beat = 0; 

function HandleMIDI (e) {
 		if (e instanceof NoteOn) {
 		  	var delay = GetParameter('Swing %')/100;
 	  		processNoteOn(e, delay);
 	  } else {
 	  		e.send();
 	  }
}

function processNoteOn(e, delay) {
if (isSwingPosition() && delay > 0) {   
	e.sendAfterBeats(delay);
} else {
	e.send();
}
}

function isSwingPosition() {
Trace('BEAT: ' + beat);
return beat.toString().slice(-2) == '.5';
}

function ProcessMIDI () {
var info = GetTimingInfo();
var curr = Math.ceil(info.blockStartBeat * 24) / 24;
if (info.playing && beat !== curr) beat = curr;
}

Link to comment
Share on other sites

Try this:

 

PluginParameters = [{type: "lin", name: 'Swing %', minValue: 0, maxValue: 30, numberOfSteps: 60, defaultValue: 20},];

NeedsTimingInfo = true;

function HandleMIDI (e) {
   if (e instanceof NoteOn) {
      var delay = GetParameter('Swing %')/100;
      processNoteOn(e, delay);
   }
   else {
       e.send();
   }
}

function processNoteOn(e, delay) {
  if (isSwingPosition(e) && delay > 0) {   
     e.sendAfterBeats(delay);
  }
  else {
     e.send();
  }
}

function isSwingPosition(e) {
  var fraction = e.beatPos - Math.trunc(e.beatPos);
  // check to see if event is within a range close to .5
  if (fraction > .45 && fraction < .55) {
      return true;
  }
  else {
      return false;
  }
}

 

Also, you need to take care of the NoteOff's. Right now, your NoteOff's are not delayed...so the swung notes will be shorter duration. And if you ever set the swing to a large enough delay that the note off comes before the note on...then you will end up with hung notes...

Link to comment
Share on other sites

Try this:

 

PluginParameters = [{type: "lin", name: 'Swing %', minValue: 0, maxValue: 30, numberOfSteps: 60, defaultValue: 20},];

NeedsTimingInfo = true;

function HandleMIDI (e) {
   if (e instanceof NoteOn) {
      var delay = GetParameter('Swing %')/100;
      processNoteOn(e, delay);
   }
   else {
       e.send();
   }
}

function processNoteOn(e, delay) {
  if (isSwingPosition(e) && delay > 0) {   
     e.sendAfterBeats(delay);
  }
  else {
     e.send();
  }
}

function isSwingPosition(e) {
  var fraction = e.beatPos - Math.trunc(e.beatPos);
  // check to see if event is within a range close to .5
  if (fraction > .45 && fraction < .55) {
      return true;
  }
  else {
      return false;
  }
}

 

Also, you need to take care of the NoteOff's. Right now, your NoteOff's are not delayed...so the swung notes will be shorter duration. And if you ever set the swing to a large enough delay that the note off comes before the note on...then you will end up with hung notes...

 

Aha, the beatPos property is what I was looking for! I actually have a compensator for the NoteOffs in the full version, I just cut it down so you didn't have to dig through it for the relevant parts. Thanks much.

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...