Old 07-13-2018, 12:25 PM   #1
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Location: Black Forest
Posts: 5,066
Default reaper.SnapToGrid(), which grid does it refer to?

Can anyone shed some light on this?
Does this function refer to the MIDI editor grid and does it also obey the grid setting (1/8, 1/16....)?
Or does it always snap to 1/16?
__________________
My Reascripts forum thread | My Reascripts on GitHub
If you like or use my scripts, please support the Ukraine: Ukraine Crisis Relief Fund | DirectRelief | Save The Children | Razom
_Stevie_ is offline   Reply With Quote
Old 07-13-2018, 12:59 PM   #2
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

It returns the time of the nearest "visible" gridline in the arrange view - if your grid is set to 1/128, but you zoom out far enough that the smallest division shown is 1/4, it will snap to 1/4.
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 07-13-2018, 01:11 PM   #3
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Location: Black Forest
Posts: 5,066
Default

Oh snap! (pun intended)
Is there a way to get the closest grid in dependency to the grid currently set in the MIDI editor?
I want to use this for a quantize script.

(I know it's possible to link the arrangement and MIDI editor grid, but I would
like to keep them seperate)
__________________
My Reascripts forum thread | My Reascripts on GitHub
If you like or use my scripts, please support the Ukraine: Ukraine Crisis Relief Fund | DirectRelief | Save The Children | Razom
_Stevie_ is offline   Reply With Quote
Old 07-13-2018, 01:30 PM   #4
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

You can get the current grid settings with:
Code:
 retval, swing, noteLen = reaper.MIDI_GetGrid( take )
Don't have time to try it, but I think if you got the grid, then got the item's start time, you could add multiples of the grid size until you pass the given position and then round up/down appropriately.
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 07-13-2018, 05:06 PM   #5
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Location: Black Forest
Posts: 5,066
Default

Ah that worked, thanks! But, I used the modulo operator. It somehow felt easier that way. But maybe, I'm overlooking something.
__________________
My Reascripts forum thread | My Reascripts on GitHub
If you like or use my scripts, please support the Ukraine: Ukraine Crisis Relief Fund | DirectRelief | Save The Children | Razom
_Stevie_ is offline   Reply With Quote
Old 07-13-2018, 06:21 PM   #6
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

That's pretty much what I meant. I use this:
Code:
    -- Returns 'val', rounded to the nearest multiple of 'snap'
    local function nearestmultiple(val, snap)
        
        local int, frac = math.modf(val / snap)
        return (math.floor( frac + 0.5 ) == 1 and int + 1 or int) * snap
        
    end
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 07-13-2018, 06:23 PM   #7
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Location: Black Forest
Posts: 5,066
Default

Thanks, have to check this out! Looks way more sophisticated than my solution
__________________
My Reascripts forum thread | My Reascripts on GitHub
If you like or use my scripts, please support the Ukraine: Ukraine Crisis Relief Fund | DirectRelief | Save The Children | Razom
_Stevie_ is offline   Reply With Quote
Old 07-13-2018, 09:10 PM   #8
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Location: Black Forest
Posts: 5,066
Default

Wow, I just tried it out and it works perfectly, thanks again man!
I'm far from understanding the whole algorithm, though

Do you have any idea, what I would need to add to create a humanize quantize of 50%?
__________________
My Reascripts forum thread | My Reascripts on GitHub
If you like or use my scripts, please support the Ukraine: Ukraine Crisis Relief Fund | DirectRelief | Save The Children | Razom
_Stevie_ is offline   Reply With Quote
Old 07-14-2018, 04:46 AM   #9
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

Scaling between two values is done by:
Code:
A = Current note position
B = 100% snapped position
S = .5

C = A + (B - A) * S
Works for pretty much anything - figuring out a certain position along a color gradient, for instance.
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 07-14-2018, 04:50 AM   #10
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

Readable version of the algorithm above, with more comments:
Code:
        -- math.modf just gives the whole and decimal parts of a number
        local int, frac = math.modf(val / snap)

        -- Simple rounding logic. If you can add 0.5 and it still rounds down (math.floor) 
        -- to 0 then it should round down. If adding 0.5 causes it to round down to 1 then
        -- it was >= 0.5 and should round up.
        local updown = math.floor( frac + 0.5 )

        if updown == 1 then
            int = int + 1
        end

        -- "int" is just the number of multiples of "snap" that we have, so we have to multiply
        -- snap back in.
        return int * snap
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate

Last edited by Lokasenna; 07-14-2018 at 05:16 AM.
Lokasenna is offline   Reply With Quote
Old 07-14-2018, 05:13 AM   #11
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Location: Black Forest
Posts: 5,066
Default

You rock man, thanks for all that useful info! That definitely sheds a lot of light.
Gotta love the Reaper community
__________________
My Reascripts forum thread | My Reascripts on GitHub
If you like or use my scripts, please support the Ukraine: Ukraine Crisis Relief Fund | DirectRelief | Save The Children | Razom
_Stevie_ is offline   Reply With Quote
Old 07-14-2018, 10:29 AM   #12
Thonex
Human being with feelings
 
Join Date: May 2018
Location: Los Angeles
Posts: 1,721
Default

Nice Loka!!

Yeah... the +.5 is a an elegant way to round to the nearest integer. My friend did coding for the military where they were using some language that had limited math functions... and the Integer Val of X + .5 would yield the correct rounding. I always thought that was a clever solution.

Also, he was working on heads-up displays for the air force, and had to find a simple solution for if an object was inside or outside the circle in the display.

His solution was to calculate a ray (straight line) from the point (in any direction... didn't matter) and if the line intersected the circle only once, it was inside the circle. Conversely, if it intersected the circle twice or zero times, it was outside the circle. But only the first calculation was needed. I thought that was clever.

Cheers,

Andrew K
Thonex is offline   Reply With Quote
Old 07-14-2018, 11:15 AM   #13
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

I wish I could take credit for it. Found it a couple of years ago after discovering that Lua doesn't have a round function.

That circle solution is pretty clever. I'm spoiled by having trig functions, I guess.
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 07-16-2018, 11:21 AM   #14
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Location: Black Forest
Posts: 5,066
Default

For getting the snap ticks I'm using this method here (to advance the note by one grid)

Code:
editorGridQN = reaper.MIDI_GetGrid(take)
ticksPerBeat = reaper.SNM_GetIntConfigVar('miditicksperbeat', 0) -- get ticks per beat from Reaper project settings
grid = ticksPerBeat * editorGridQN -- calculate grid steps in ticks
Am I overcomplicating things, is there a simpler method?
__________________
My Reascripts forum thread | My Reascripts on GitHub
If you like or use my scripts, please support the Ukraine: Ukraine Crisis Relief Fund | DirectRelief | Save The Children | Razom
_Stevie_ is offline   Reply With Quote
Old 07-16-2018, 11:29 AM   #15
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

I can't see a more direct way to do it.

Depending on the task, it's worth looking at the various PPQ functions in the API - Project time from PPQ, PPQ from project time, that sort of stuff. Handy to have.
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 07-16-2018, 01:09 PM   #16
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Location: Black Forest
Posts: 5,066
Default

Okay, good to know! Then I'll keep it that way
Yeah, I tried the PPQ conversion stuff. It's very handy indeed! But it's only helpful for specific positions in the timeline.
__________________
My Reascripts forum thread | My Reascripts on GitHub
If you like or use my scripts, please support the Ukraine: Ukraine Crisis Relief Fund | DirectRelief | Save The Children | Razom
_Stevie_ is offline   Reply With Quote
Old 10-03-2020, 07:18 AM   #17
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Location: Black Forest
Posts: 5,066
Default

So snapToGrid() does not only refer to the visible grid. It depends on the setting in the grid dialog. Leaving this here for everyone:

https://forum.cockos.com/showpost.ph...7&postcount=23
__________________
My Reascripts forum thread | My Reascripts on GitHub
If you like or use my scripts, please support the Ukraine: Ukraine Crisis Relief Fund | DirectRelief | Save The Children | Razom
_Stevie_ 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 11:41 PM.


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