Old 01-31-2019, 08:38 AM   #1
lexaproductions
Human being with feelings
 
Join Date: Jan 2013
Posts: 300
Default Wait Until Next Bar to do an action

Hey guys,

I'm sorry for being a newbie, but I'm trying to learn LUA.

I'm trying to find a way to "wait until the end of current measure" before doing something from within the script using some kind of
Code:
for i = 1
looping function.

Anybody can help me?

Thanks
__________________
MP 12 Core 3.46GHZ 48GB OSX10.11.6, MBP 17" 2011 OSX10.12, RME Fireface UFX, MCU,
Reaper 5, DP9, PT 11, Logic Pro X, Bidule, M5 3, BFD2, SD2, Omnisphere, Wave Altiverb, PSP, VB3, Izotope, Scuffham, Soundtoys 5, Slate All Plugins.
lexaproductions is offline   Reply With Quote
Old 01-31-2019, 09:18 AM   #2
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 2,469
Default

Use reaper.defer to schedule a function to be run later (~33ms). Unlike regular loops which freeze REAPER until they stop, reaper.defer allows the GUI to run in between.

Code:
function onNextMeasure(callback)
  local beatsSinceLastMeasure = reaper.TimeMap2_timeToBeats(0, reaper.GetPlayPosition())
  
  if beatsSinceLastMeasure < 0.1 then
    -- if we're at (or very near) the start of a measure, call the given function
    callback()
  else
    -- or, if we're not at the start of a measure, check again ~33ms later
    reaper.defer(function() onNextMeasure(callback) end)
  end
end

onNextMeasure(function()
  -- code to run at the next measure here
  reaper.ShowConsoleMsg("Hello Next Measure!\n")
end)

Last edited by cfillion; 01-31-2019 at 09:32 AM.
cfillion is offline   Reply With Quote
Old 01-31-2019, 11:18 AM   #3
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 5,438
Default

Or simply put an action in a custom action after SWS: Wait for next bar (if playing) :P (depengind on what function is required of course)
X-Raym is offline   Reply With Quote
Old 01-31-2019, 12:02 PM   #4
lexaproductions
Human being with feelings
 
Join Date: Jan 2013
Posts: 300
Default

Quote:
Originally Posted by X-Raym View Post
Or simply put an action in a custom action after SWS: Wait for next bar (if playing) :P (depengind on what function is required of course)
I thought that action was taken out? I can't find it in my action list.
__________________
MP 12 Core 3.46GHZ 48GB OSX10.11.6, MBP 17" 2011 OSX10.12, RME Fireface UFX, MCU,
Reaper 5, DP9, PT 11, Logic Pro X, Bidule, M5 3, BFD2, SD2, Omnisphere, Wave Altiverb, PSP, VB3, Izotope, Scuffham, Soundtoys 5, Slate All Plugins.
lexaproductions is offline   Reply With Quote
Old 01-31-2019, 12:03 PM   #5
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 5,438
Default

Do you have http://sws-extension.org/ ?
X-Raym is offline   Reply With Quote
Old 01-31-2019, 12:06 PM   #6
lexaproductions
Human being with feelings
 
Join Date: Jan 2013
Posts: 300
Default

Quote:
Originally Posted by cfillion View Post
Use reaper.defer to schedule a function to be run later (~33ms). Unlike regular loops which freeze REAPER until they stop, reaper.defer allows the GUI to run in between.
My only problem is that I need to use this to kill an already defered script. This Crashed Reaper.

If I test it using Reaper's programming window (Hitting CMS-S to run the script) it works. But if I close the Programming window and try to run the script from a key command. Reaper Crashes upon triggering the other Defered Script.

Any idea?
__________________
MP 12 Core 3.46GHZ 48GB OSX10.11.6, MBP 17" 2011 OSX10.12, RME Fireface UFX, MCU,
Reaper 5, DP9, PT 11, Logic Pro X, Bidule, M5 3, BFD2, SD2, Omnisphere, Wave Altiverb, PSP, VB3, Izotope, Scuffham, Soundtoys 5, Slate All Plugins.
lexaproductions is offline   Reply With Quote
Old 01-31-2019, 12:07 PM   #7
lexaproductions
Human being with feelings
 
Join Date: Jan 2013
Posts: 300
Default

Quote:
Originally Posted by X-Raym View Post
Yes I do.
__________________
MP 12 Core 3.46GHZ 48GB OSX10.11.6, MBP 17" 2011 OSX10.12, RME Fireface UFX, MCU,
Reaper 5, DP9, PT 11, Logic Pro X, Bidule, M5 3, BFD2, SD2, Omnisphere, Wave Altiverb, PSP, VB3, Izotope, Scuffham, Soundtoys 5, Slate All Plugins.
lexaproductions is offline   Reply With Quote
Old 01-31-2019, 12:13 PM   #8
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 2,469
Default

Quote:
Originally Posted by lexaproductions View Post
Any idea?
Sounds like a bug (scripts should not be able to cause crashes)! How are you "killing an already defered script", which version of REAPER are you on, and can you post the crash report?

If you're using "ReaScript: Close all running reaScripts" (action 41898), this bug was fixed some time ago: https://forum.cockos.com/showthread.php?t=186699.

Quote:
Originally Posted by lexaproductions View Post
I thought that action was taken out? I can't find it in my action list.
It's only available on Windows.

Last edited by cfillion; 01-31-2019 at 12:32 PM.
cfillion is offline   Reply With Quote
Old 01-31-2019, 01:56 PM   #9
lexaproductions
Human being with feelings
 
Join Date: Jan 2013
Posts: 300
Default

Quote:
Originally Posted by cfillion View Post
If you're using "ReaScript: Close all running reaScripts" (action 41898), this bug was fixed some time ago:
I'm not using that function as I have other scripts running that I don't want to kill at that moment

Quote:
Originally Posted by cfillion View Post
Sounds like a bug (scripts should not be able to cause crashes)!
Well that's what I thought, because I'm pretty novice at this and I don't want to compromise Reaper's stability.

Quote:
Originally Posted by cfillion View Post
which version of REAPER are you on,
I tried Reaper 5.77 and 5.965, same result

Quote:
Originally Posted by cfillion View Post
How are you "killing an already defered script"
I might not have used the right terms when I said "deferred". Here's a full explanation:
I have "Script A" running in the background. It's a window showing the current region in a window.
Then I trigger "Script B" which monitors if "Script A" is running (using GetToggleCommandState), and kills it at the end of current bar. The Way Script A is kill is simple; I just re-instantiate it, and Reaper kills it instead of making a 2nd instance of the script.

The Problem is: When I trigger "Script B", as soon as it kills "Script A", Reaper Crashes. I've confirmed that, if I don't trigger the kill, Reaper doesn't crash.


Quote:
Originally Posted by cfillion View Post
can you post the crash report?
Code:
Process:               REAPER [29796]
Path:                  /Volumes/VOLUME/*/ReaLiveDev.app/Contents/MacOS/REAPER
Identifier:            com.cockos.reaper
Version:               5.96.500 (5.96.500)
Code Type:             X86-64 (Native)
Parent Process:        ??? [1]
Responsible:           REAPER [29796]
User ID:               501

Date/Time:             2019-01-31 15:37:48.833 -0500
OS Version:            Mac OS X 10.11.6 (15G31)
Report Version:        11
Anonymous UUID:        48421833-45CE-0A6E-E670-0E640B1D81D1


Time Awake Since Boot: 770000 seconds

System Integrity Protection: disabled

Crashed Thread:        0  reaper  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000020
Exception Note:        EXC_CORPSE_NOTIFY

VM Regions Near 0x20:
--> 
    __TEXT                 0000000100000000-0000000100ad7000 [ 10.8M] r-x/rwx SM=COW  /Volumes/VOLUME/*/ReaLiveDev.app/Contents/MacOS/REAPER

Global Trace Buffer (reverse chronological seconds):
12.349460    libextension.dylib        	0x00007fff866fa533 tearing down extension request for pid 29799
23.491569    libextension.dylib        	0x00007fff866fa533 tearing down extension request for pid 29799

Thread 0 Crashed:: reaper  Dispatch queue: com.apple.main-thread
0   com.cockos.reaper             	0x00000001006dd41e ReaScript_RunDeferred() + 238
1   com.cockos.reaper             	0x00000001000e067b runMiscTimers() + 1947
2   com.cockos.reaper             	0x00000001000df446 Main_OnTimer(HWND__*, unsigned long) + 166
3   com.cockos.reaper             	0x000000010063ba09 MainProc(HWND__*, unsigned int, unsigned long, long) + 8089
4   ???                           	0x5fbfd33000000001 0 + 6899465357266780161

========================================================>
Crash Log Ending
========================================================>


Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x0000000000000000  rbx: 0x000000010b188600  rcx: 0x0000000000000000  rdx: 0x0000000000000000
  rdi: 0x0000000108135410  rsi: 0x0000000000000000  rbp: 0x00007fff5fbfbcc0  rsp: 0x00007fff5fbfbc70
   r8: 0x000000010b1c2800   r9: 0x000000000000000a  r10: 0x0000000000234200  r11: 0x0000000000000000
  r12: 0x0000000000000001  r13: 0x0000000000000010  r14: 0x000000010b188608  r15: 0x00000000ffffffff
  rip: 0x00000001006dd41e  rfl: 0x0000000000010246  cr2: 0x0000000000000020
  
Logical CPU:     8
Error Code:      0x00000004
Trap Number:     14


Binary Images:
       0x100000000 -        0x100ad6ff7 +com.cockos.reaper (5.96.500 - 5.96.500) <9096C7B2-2841-2A61-15BB-E7BFCA4059C4> /Volumes/VOLUME/*/ReaLiveDev.app/Contents/MacOS/REAPER
       0x100dc1000 -        0x100df8fff  com.apple.audio.midi.CoreMIDI (1.10 - 88) <51E79766-51D8-3A95-A7FD-18E7E11528F3>
__________________
MP 12 Core 3.46GHZ 48GB OSX10.11.6, MBP 17" 2011 OSX10.12, RME Fireface UFX, MCU,
Reaper 5, DP9, PT 11, Logic Pro X, Bidule, M5 3, BFD2, SD2, Omnisphere, Wave Altiverb, PSP, VB3, Izotope, Scuffham, Soundtoys 5, Slate All Plugins.
lexaproductions is offline   Reply With Quote
Old 02-04-2019, 06:52 AM   #10
lexaproductions
Human being with feelings
 
Join Date: Jan 2013
Posts: 300
Default

Hey CF, I was wondering if you had a second to check out my last post?
__________________
MP 12 Core 3.46GHZ 48GB OSX10.11.6, MBP 17" 2011 OSX10.12, RME Fireface UFX, MCU,
Reaper 5, DP9, PT 11, Logic Pro X, Bidule, M5 3, BFD2, SD2, Omnisphere, Wave Altiverb, PSP, VB3, Izotope, Scuffham, Soundtoys 5, Slate All Plugins.
lexaproductions is offline   Reply With Quote
Old 02-04-2019, 07:24 AM   #11
mespotine
Human being with feelings
 
mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig, Germany
Posts: 735
Default

Which scripts do you run? We could try to recreate the crash to help the devs fixing it, if we'd know the scripts you use...
__________________
Ultraschall-API: https://forum.cockos.com/showthread....98#post2067798
Reaper Internals - Developerdocs for Reaper: https://forum.cockos.com/showthread.php?t=207635
mespotine is offline   Reply With Quote
Old 02-04-2019, 01:32 PM   #12
lexaproductions
Human being with feelings
 
Join Date: Jan 2013
Posts: 300
Default

Thanks for replying to this.
The scripts I'm using are here:
https://www.dropbox.com/s/h0nh637jbf...Crash.zip?dl=0
Here are my steps:
1- Start the first Script which is a window showing the current Region. This was inspired by a script XRaym Made.
2- Start Playback
3- Forcing a smooth seek to edit cursor (I have it setup to "2 bars")
4- The Seek is initiated but upon arriving to the next bar and executing CFillion's function, The whole thing crashes. You can see the data in the Cap on in one of my previous posts in this thread.

I hope somebody can help me.

Here's a screen Capture.
__________________
MP 12 Core 3.46GHZ 48GB OSX10.11.6, MBP 17" 2011 OSX10.12, RME Fireface UFX, MCU,
Reaper 5, DP9, PT 11, Logic Pro X, Bidule, M5 3, BFD2, SD2, Omnisphere, Wave Altiverb, PSP, VB3, Izotope, Scuffham, Soundtoys 5, Slate All Plugins.

Last edited by lexaproductions; 02-04-2019 at 01:44 PM. Reason: fixing the screen capture link
lexaproductions is offline   Reply With Quote
Old 02-04-2019, 02:52 PM   #13
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 2,469
Default

I've reduced it to the following steps and minimal scripts:
  1. Run Script A.lua:
    Code:
    function loop() reaper.defer(loop) end
    loop()
  2. Run Script B.lua (with the correct command ID for Script A.lua):
    Code:
    reaper.defer(function()
      local cmd = reaper.NamedCommandLookup("command ID of script A here")
      reaper.Main_OnCommand(cmd, 0)
    end)
  3. Select "Terminate instances"

Maybe you could message the main script to terminate itself using a temporary extstate instead of re-running it?

Last edited by cfillion; 02-04-2019 at 03:40 PM.
cfillion is offline   Reply With Quote
Old 02-04-2019, 02:58 PM   #14
lexaproductions
Human being with feelings
 
Join Date: Jan 2013
Posts: 300
Default

I'm glad I'm not crazy.

Now I have to understand how to use extstate

I'll file a bug report on the Forum, thanks
__________________
MP 12 Core 3.46GHZ 48GB OSX10.11.6, MBP 17" 2011 OSX10.12, RME Fireface UFX, MCU,
Reaper 5, DP9, PT 11, Logic Pro X, Bidule, M5 3, BFD2, SD2, Omnisphere, Wave Altiverb, PSP, VB3, Izotope, Scuffham, Soundtoys 5, Slate All Plugins.
lexaproductions is offline   Reply With Quote
Old 02-04-2019, 03:08 PM   #15
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 2,469
Default

Code:
function runRegionWindow()
  if reaper.HasExtState('lexa_Name of the script', 'exit') then
    reaper.DeleteExtState('lexa_Name of the script', 'exit', false);
    return
  end

  -- rest of the code...
end
Code:
onNextMeasure(function()
  reaper.SetExtState('lexa_Name of the script', 'exit', '', false)
end)
cfillion is offline   Reply With Quote
Old 02-05-2019, 06:19 AM   #16
lexaproductions
Human being with feelings
 
Join Date: Jan 2013
Posts: 300
Default

Thanks a lot.
It works great
__________________
MP 12 Core 3.46GHZ 48GB OSX10.11.6, MBP 17" 2011 OSX10.12, RME Fireface UFX, MCU,
Reaper 5, DP9, PT 11, Logic Pro X, Bidule, M5 3, BFD2, SD2, Omnisphere, Wave Altiverb, PSP, VB3, Izotope, Scuffham, Soundtoys 5, Slate All Plugins.
lexaproductions 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 09:06 AM.


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