Old 02-12-2020, 11:23 AM   #1
Cedrik0s
Human being with feelings
 
Join Date: Oct 2014
Posts: 60
Default Shouldn't setting an item offline make reaper release the file?

Hi there.
Using Python on Reaper on Windows.

I have a script that launches a recording, then stops it when some conditions are met.
Right after stopping it tries to rename the newly recorded file. To do so I set the media item offline, rename the file using os.rename and then set the item to point at the renamed file with BR_SetTakeSourceFromFile2. Finally I put the item online again.

This sometimes fails because the filesystem says the item is in use by some process.
Shouldn't reaper release the file when I set the item offline?

I actually tried waiting even 5s before the renaming but it still can fail...

Any suggestion on how to fix/circumvent the problem?

Thanks a lot.

C
Cedrik0s is offline   Reply With Quote
Old 02-12-2020, 03:34 PM   #2
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 6,316
Default

Quote:
This sometimes fails because the filesystem says the item is in use by some process.
Check if the source item isn't use at other place in REAPER (other items, other takes, project media, Media explorer etc).
X-Raym is offline   Reply With Quote
Old 02-13-2020, 10:35 AM   #3
Cedrik0s
Human being with feelings
 
Join Date: Oct 2014
Posts: 60
Default

Hi X-Raym.

The source is supposedly not used by anything since I just recorded the clip.
Quite strangely it seems to always record and rename the first file correctly, but fail the second: it would record it but then it would fail the renaming.
I really have no clue at this point.
Cedrik0s is offline   Reply With Quote
Old 02-13-2020, 02:46 PM   #4
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 9,373
Default

Maybe post some code for others to look into?
nofish is offline   Reply With Quote
Old 02-14-2020, 01:53 AM   #5
Cedrik0s
Human being with feelings
 
Join Date: Oct 2014
Posts: 60
Default

You're right Nofish,
I'll try to prepare a clean example code that reproduces the problem when I find some time.

Cheers.
Cedrik0s is offline   Reply With Quote
Old 02-14-2020, 08:33 AM   #6
Cedrik0s
Human being with feelings
 
Join Date: Oct 2014
Posts: 60
Default

Hi there.
Spoiler: It seems I found how to make it work. I had to defer the renaming part after the recording stopped.

Anyway, find below a simplify (working) code.
Arm a track and launch the script. It will record on this track for little more than 0.50 sec, rename the source file to NewName_1.wav, then move the play head ahead and record another one, name it NewName_2.wav... Three times (gNumRecordings=3)
Delete these files from your disk if you want to run the script another time, otherwise a "file already exists" error will occur.


There are basically 3 functions calling each other:
- BeforeRec: starts recording and calls the wait function
- WaitTillPosition: defers itself until at least 0.5s has passed. Then stops the recording and calls AfterRec()
- AfterRec(): rename the source file then call BeforeRec again to restart the loop.


It is now working.
What I was doing wrong (without it being obviously wrong even afterwards) was that in the function WaitTillPosition(...) I was calling directly
Code:
AfterRec()
instead of defering it like this
Code:
RPR_defer("AfterRec()")
Here's the whole code:

Code:
import os
from sws_python import *

gNumRecordings = 3
gCounter = 0
gProjPath = RPR_GetProjectPath("", 512)[0]

def msg(str):
  RPR_ShowConsoleMsg(str + "\n")


###################################################################

def BeforeRec():
  global gCounter

  
  if gCounter >= gNumRecordings:
    return

  gCounter += 1
  
  msg(f"BeforeRec: counter = {gCounter}")
  
  #Transport: Record
  RPR_Main_OnCommand(1013, 0)

  WaitTillPosition(RPR_GetPlayPosition() + 0.5) #stop recording after roughly 0.5f
  
###################################################################

def WaitTillPosition(endPos): 
 
  pos = RPR_GetPlayPosition() 

  if pos >= endPos:
    msg(f"Stopping recording at {pos}")
    #Transport: Stop (save all recorded media)
    RPR_Main_OnCommand(40667, 0)  
    RPR_defer("AfterRec()")
    return
    
  RPR_defer(f"WaitTillPosition({endPos})")
  
###################################################################
 
def AfterRec():
  
  msg(f"AfterRec: Counter = {gCounter}")
  
  newfilepath = os.path.join(gProjPath, f"NewName_{gCounter}.wav")
  
  #### RENAMING HERE ####
  
  it = RPR_GetSelectedMediaItem(0,0)
  take = RPR_GetActiveTake(it)
  src    = RPR_GetMediaItemTake_Source(take)
  oldfile = " " * 256;
  (_, oldfile, _) = RPR_GetMediaSourceFileName(src, "", 256)

  #Item: Set selected media offline
  RPR_Main_OnCommand(40440, 0)
  
  if os.path.exists(newfilepath):
      msg("Error: Destination file already exists.")
      #Item: Set selected media online
      RPR_Main_OnCommand(40439, 0)
      return 0;
  
  msg(oldfile)
  msg(newfilepath)
  os.rename(oldfile, newfilepath)
    
  #RPR_SetMediaItemTake_Source(take, source)
  res = BR_SetTakeSourceFromFile(take, newfilepath, False) #, True)
    
  #Item: Set selected media online
  RPR_Main_OnCommand(40439, 0)
  
  RPR_GetSetMediaItemTakeInfo_String(take, "P_NAME", os.path.basename(newfilepath), True)
  
  ##### RENAMING DONE #####

  #Peaks: Rebuild peaks for selected items
  RPR_Main_OnCommand(40441, 0)     
  RPR_UpdateArrange()
  
  # move cursor ahead for next recording 
  RPR_SetEditCurPos(BR_GetNextGridDivision(RPR_GetPlayPosition()+1.0), True, False)
  
  BeforeRec()
  


BeforeRec()


The weird thing is that when it is not working (not defering the call to AfterRec() in WaitTillPosition()), only the second recording raises the error. The first one gets renamed successfully.

That's it.
Cedrik0s 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 08:32 PM.


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