[Solved] Loading,Saving,Removing TMP files during document flush

[s]Hi, I’m attempting to learn to save a song, then save a sample as a temporary file, create new song, and load the same-such sample in - all in one fell swoop.
so far i’m running into errors with:
renoise.song().instruments[renoise.song().selected_instrument_index].samples[1].sample_buffer:save_as(“file”, “wav”)
telling me
“Sample Export Failed” -> “Sample export failed with the error: ‘Failed to open the file ‘file’.’”

What am I doing wrong? I’ve tried “/file” too, but no go.
How do I go about doing this, please?
EDIT: What I was doing wrong was trying to save to /file.wav without having permissions. When I switched to saving to /tmp/file.wav - everything worked without a hitch.
EDIT2: Cannot seem to find how to Remove a temporary file (os.tmpname)[/s]
EDIT3: os.remove is missing from the Renoise documentation :(

Alright, I got it to work by saving to /tmp/file

renoise.song().instruments[renoise.song().selected_instrument_index].samples[1].sample_buffer:save_as("/tmp/test.wav", "wav")  

but loading this same sample after new_song does not seem to work, since the sample is loaded while renoise.song() doesn’t yet exist…

  
function WipeRetain()  
renoise.song().instruments[renoise.song().selected_instrument_index].samples[1].sample_buffer:save_as("/tmp/test.wav", "wav")  
renoise.app():new_song()  
renoise.song().instruments[renoise.song().selected_instrument_index].samples[1].sample_buffer:load_from("/tmp/test.wav", "wav")  
end  
  
*** No matching overload found, candidates:  
*** bool load_from(SampleBuffer&,custom [class String] const&)  
*** stack traceback:  
*** [C]: in function 'load_from'  
*** main.lua:6: in function 'WipeRetain'  
*** main.lua:9: in function <9><br>```

<br>
<br>
<br>
<br>
how do i delay the samplebuffer loading so that it only occurs after newsong has been created?</9>

Answer: By using a notifier.
This saves the current sample, wipes the song, detects there’s a new song-document, and loads the song from the place it was saved into. highly destructive, but works for me.
EDIT: Post#4 has the finished version with the os.tmpvariable

[details=“Click to view contents”]

function WipeRetain()  
renoise.song().instruments[renoise.song().selected_instrument_index].samples[1].sample_buffer:save_as("/tmp/test.wav", "wav")  
if not renoise.tool().app_new_document_observable:has_notifier(WipeRetain_)   
 then renoise.tool().app_new_document_observable:add_notifier(WipeRetain_)  
 else renoise.tool().app_new_document_observable:remove_notifier(WipeRetain_)  
end  
renoise.app():new_song()  
end  
  
function WipeRetain_()  
renoise.song().instruments[renoise.song().selected_instrument_index].samples[1].sample_buffer:load_from("/tmp/test.wav")  
renoise.tool().app_new_document_observable:remove_notifier(WipeRetain_)  
end  
  
renoise.tool():add_keybinding {name = "Global:Paketti:Wipe Song Retain Sample", invoke = function() WipeRetain() end}  
``` [/details]

thanks to heaps of help from mxb, this is now a OS-free thing and should therefore work anywhere:

  
local tmpvariable=nil  
  
function WipeRetain()  
tmpvariable=os.tmpname("wav")  
  
renoise.song().instruments[renoise.song().selected_instrument_index].samples[1].sample_buffer:save_as(tmpvariable, "wav")  
if not renoise.tool().app_new_document_observable:has_notifier(WipeRetain_)   
 then renoise.tool().app_new_document_observable:add_notifier(WipeRetain_)  
 else renoise.tool().app_new_document_observable:remove_notifier(WipeRetain_)  
end  
renoise.app():new_song()  
end  
  
function WipeRetain_()  
renoise.song().instruments[renoise.song().selected_instrument_index].samples[1].sample_buffer:load_from(tmpvariable)  
renoise.app().window.active_middle_frame=4  
  
renoise.app():show_status(tmpvariable)  
os.remove(tmpvariable)  
renoise.tool().app_new_document_observable:remove_notifier(WipeRetain_)  
end  
  
renoise.tool():add_keybinding {name = "Global:Paketti:Wipe Song Retain Sample", invoke = function() WipeRetain() end}  
  
  

A lot of the documentation for basic Lua-only stuff that can be found on the lua site itself… If you need basic functionality, check the Lua site itself ;)

Hmm, yeah… I guess I’d have to take some of those os.* commands and add them to my cat RenoiseDocumentation > Solution.txt thingo. or just be content with os.execute and carry on from there.