Old 06-18-2019, 04:57 PM   #1
MusoBob
Human being with feelings
 
MusoBob's Avatar
 
Join Date: Sep 2014
Posts: 2,643
Default Move Cursor to Next Beat

There doesn't seem to be anything in the API's to go to measure/beat it all seems to be time.

I just need to move the cursor to the next beat

reaper.Main_OnCommand(41044, 0) -- Move edit cursor forward one beat

is moving 2 beats forward.


If I use as a command Script: spk77_Move edit cursor forward by 1 frame (or 1 beat).eel

it works but I don't want to call on it externally from the script so I would like to add it as a

function in the Lua script:


eel code

Code:
function move_cursor_right() local(snap, grid_vis, state, beat_in_meas, meas, closest_beat_in_secs)
(
  GetToggleCommandState(40370) ? state = 1 : state = 0;
  
  !state ? ( // ruler time unit is not "H:M:S:F"
    beat_in_meas = TimeMap2_timeToBeats(0, GetCursorPosition(), meas, 0, 0, 0);
    closest_beat_in_secs = TimeMap2_beatsToTime(0, floor(beat_in_meas + 0.5), meas);
    
    closest_beat_in_secs > GetCursorPosition() ? (
      SetEditCurPos(TimeMap2_beatsToTime(0, floor(beat_in_meas + 0.5), meas), 1, 0);
    ) : (
      SetEditCurPos(TimeMap2_beatsToTime(0, floor(beat_in_meas + 0.5) + 1, meas), 1, 0);
    );
  ) : ( // ruler time unit is "H:M:S:F"
    ApplyNudge(0, 2, 6, 18, 0.501, 0, 0); // nudge edit cursor right by 0.501 frames (snap to unit)
  );

  Undo_OnStateChange("Move edit cursor right by 1 frame (or 1 beat)");
);
but I can't get the Lua translation
Code:
  --GetToggleCommandState(40370) ? state = 1 : state = 0;
  state = reaper.GetToggleCommandState( 40370 )
  
  if state == 0 then  -- ruler time unit is not "H:M:S:F"
    beat_in_meas = reaper.TimeMap2_timeToBeats(0, reaper.GetCursorPosition(), meas, 0, 0, 0)
    closest_beat_in_secs = reaper.TimeMap2_beatsToTime(0, math.floor(beat_in_meas + 0.5), meas)
    
    if closest_beat_in_secs > reaper.GetCursorPosition() then
      reaper.SetEditCurPos(reaper.TimeMap2_beatsToTime(0, math.floor(beat_in_meas + 0.5), meas), 1, 0)
    
      reaper.SetEditCurPos(reaper.TimeMap2_beatsToTime(0, math.floor(beat_in_meas + 0.5) + 1, meas), 1, 0)
    end
  end  
  if state == 1 then -- ruler time unit is "H:M:S:F"
    reaper.ApplyNudge(0, 2, 6, 18, 0.501, 0, 0); -- nudge edit cursor right by 0.501 frames (snap to unit)
  end
__________________
ReaTrakStudio Chord Track for Reaper forum
www.reatrak.com
STASH Downloads https://stash.reaper.fm/u/ReaTrak
MusoBob is offline   Reply With Quote
Old 06-18-2019, 05:34 PM   #2
MusoBob
Human being with feelings
 
MusoBob's Avatar
 
Join Date: Sep 2014
Posts: 2,643
Default

it's simple when you find out how !


Code:
cur_pos = reaper.GetCursorPosition()
retval, measures, cml, fullbeats, cdenom = reaper.TimeMap2_timeToBeats(0, cur_pos)
current_measure = reaper.TimeMap2_beatsToTime(0, fullbeats +1)
reaper.SetEditCurPos2( 0, current_measure, 0, 0 )
__________________
ReaTrakStudio Chord Track for Reaper forum
www.reatrak.com
STASH Downloads https://stash.reaper.fm/u/ReaTrak
MusoBob is offline   Reply With Quote
Old 06-21-2019, 09:34 PM   #3
hopi
Human being with feelings
 
hopi's Avatar
 
Join Date: Oct 2008
Location: Right Hear
Posts: 15,618
Default

now Bob... there is a 'signature quote'

Quote:
it's simple when you find out how !
so how about the reverse lua? previous beat
__________________
...should be fixed for the next build... http://tinyurl.com/cr7o7yl
https://soundcloud.com/hopikiva

Last edited by hopi; 06-21-2019 at 09:45 PM.
hopi is offline   Reply With Quote
Old 06-21-2019, 10:09 PM   #4
MusoBob
Human being with feelings
 
MusoBob's Avatar
 
Join Date: Sep 2014
Posts: 2,643
Default Move Cursor to Prevoius Beat

Code:
cur_pos = reaper.GetCursorPosition()
retval, measures, cml, fullbeats, cdenom = reaper.TimeMap2_timeToBeats(0, cur_pos)
current_measure = reaper.TimeMap2_beatsToTime(0, fullbeats -1)
reaper.SetEditCurPos2( 0, current_measure, 0, 0 )
__________________
ReaTrakStudio Chord Track for Reaper forum
www.reatrak.com
STASH Downloads https://stash.reaper.fm/u/ReaTrak
MusoBob is offline   Reply With Quote
Old 06-22-2019, 06:17 AM   #5
hopi
Human being with feelings
 
hopi's Avatar
 
Join Date: Oct 2008
Location: Right Hear
Posts: 15,618
Default

great! thanks Bob
__________________
...should be fixed for the next build... http://tinyurl.com/cr7o7yl
https://soundcloud.com/hopikiva
hopi is offline   Reply With Quote
Old 06-22-2019, 06:49 AM   #6
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 12,109
Default

Note that TimeMap2_timeToBeats() / TimeMap2_beatsToTime() don't take into account if a swing grid is set:
https://forum.cockos.com/showthread.php?t=220305

Justin posted some code for it in that thread.

Last edited by nofish; 06-22-2019 at 07:13 AM.
nofish is offline   Reply With Quote
Old 06-22-2019, 07:26 PM   #7
MusoBob
Human being with feelings
 
MusoBob's Avatar
 
Join Date: Sep 2014
Posts: 2,643
Default

Quote:
Originally Posted by nofish View Post
Note that TimeMap2_timeToBeats() / TimeMap2_beatsToTime() don't take into account if a swing grid is set...
Thanks I'll look into that more. I just created another script below to insert a stretch marker @ every beat depending what the grid is set to then it will snap the first stretch marker to the first beat and so on to fit the constant tempo item to the project's tempo map. If you can try it with your swing grid as it may not work if the stretch markers are not offset for the swing in the source item ??
Quote:
Originally Posted by MusoBob View Post
Here's an updated script to try, just set the item on the start beat, run the script and set the items original tempo in the popup.
Let me know if it works for you.

right click Save Link/Target As
ReaTrak set item bpm and fit to project tempo map.lua

Watch Video
__________________
ReaTrakStudio Chord Track for Reaper forum
www.reatrak.com
STASH Downloads https://stash.reaper.fm/u/ReaTrak
MusoBob is offline   Reply With Quote
Old 06-23-2019, 08:29 AM   #8
hopi
Human being with feelings
 
hopi's Avatar
 
Join Date: Oct 2008
Location: Right Hear
Posts: 15,618
Default

Bob... it would be nice if you had your own repository on Reapack... so we can find just your scripts in a folder, like say, "MusoBob"... within the Scripts folder

OH... and it would be way nice if these scripts were OK in the MIDI Ed. actions list... must be a small thing to add???
__________________
...should be fixed for the next build... http://tinyurl.com/cr7o7yl
https://soundcloud.com/hopikiva

Last edited by hopi; 06-24-2019 at 12:01 PM.
hopi is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 07:26 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.