|
|
|
07-08-2021, 09:59 PM
|
#1
|
Human being with feelings
Join Date: Jan 2014
Posts: 5,207
|
Best way to insert media into track by ID?
I'm hitting wall after wall with this.
I'm trying to import media to specific tracks, within a function that is called several times by a for loop and given fresh parameters each time via arrays.
Here is the isolated function at the moment:
Code:
function PlaceMedia(SerialToLookFor, OriginalTrackID)
_, FilenamesArray = ultraschall.GetAllFilenamesInPath(reaper.GetProjectPath(0))
for j=1, #FilenamesArray do
if string.find(FilenamesArray[j], SerialToLookFor) then
Filename = FilenamesArray[j]
--Msg("Placing ".. Filename .. " on track " .. OriginalTrackID) -- for debug, working great
ultraschall.InsertMediaItemFromFile(Filename, OriginalTrackID, reaper.GetCursorPosition(), -1, 2)
break
end -- if
end -- for
end --PlaceMedia()
Problem 1:
Ultraschall.InsertMediaItemFromFile may have a bug, or I don't understand something, but using track IDs even if I intentionally write for e.g. '3' does not place media on track 3. Instead it places it on the last selected track. -1 and 0 behave properly but only if an offset is used.
Problem 2: Replacing that with reaper.InsertMedia presents the problem that I need to first use track selection to control where the media goes. Fine.. except that I run into this issue with 'bad argument' for reaper.SetTrackSelected(OriginalTrackIDs[i], 1). Again, replace this with literal integers and I still get the error.
Problem 3: Ok now I tried ultraschall.SetTracksSelected which expects a track string, so I used tostring(OriginalTrackID), and I get the same problem with InsertMedia that it just inserts on the last selected track.
What is the best approach?
|
|
|
07-08-2021, 10:08 PM
|
#2
|
Human being with feelings
Join Date: May 2015
Location: Québec, Canada
Posts: 4,937
|
Code:
function addItemToTrack(track, file, position)
local item = reaper.AddMediaItemToTrack(track)
local take = reaper.AddTakeToMediaItem(item)
local source = reaper.PCM_Source_CreateFromFile(file)
local length, isQN = reaper.GetMediaSourceLength(source)
if isQN then
local posQN = reaper.TimeMap2_timeToQN(nil, position)
length = reaper.TimeMap2_QNToTime(nil, posQN + length) - position
end
reaper.SetMediaItemTake_Source(take, source)
reaper.SetMediaItemInfo_Value(item, 'D_POSITION', position)
reaper.SetMediaItemInfo_Value(item, 'D_LENGTH', length)
reaper.UpdateArrange()
end
local track = reaper.GetTrack(nil, 2) -- track 3
addItemToTrack(track, 'file.wav', 1.234)
Quote:
Originally Posted by Fergler
Problem 2: Replacing that with reaper.InsertMedia presents the problem that I need to first use track selection to control where the media goes. Fine.. except that I run into this issue with 'bad argument' for reaper.SetTrackSelected(OriginalTrackIDs[i], 1). Again, replace this with literal integers and I still get the error.
|
SetTrackSelected, like most functions, expect a MediaTrack object. Use reaper.GetTrack to obtain this object from a track index (0 = track 1).
|
|
|
07-09-2021, 07:09 AM
|
#3
|
Human being with feelings
Join Date: May 2017
Location: Leipzig
Posts: 6,621
|
Please report issues with Ultraschall-Api functions in the official thread, so I can fix them if buggy.
Otherwise I might not stumble over the issue.
Edit: where and how do you get OriginalTrackID from, that PlaceMedia expects as a parameter?
I suspect, that the value for the parameter might become -1 somewhere. Maybe an error produced by the function that generates OriginalTrackID?
To check that, add SFEM() and SLEM() into the code, which will show the first and last error messages created by any Ultraschall-Api function.
Maybe this gives a hint.
Last edited by Meo-Ada Mespotine; 07-09-2021 at 07:15 AM.
|
|
|
07-09-2021, 09:43 AM
|
#4
|
Human being with feelings
Join Date: Jan 2014
Posts: 5,207
|
Quote:
Originally Posted by Meo-Ada Mespotine
Edit: where and how do you get OriginalTrackID from, that PlaceMedia expects as a parameter?
I suspect, that the value for the parameter might become -1 somewhere. Maybe an error produced by the function that generates OriginalTrackID?
|
It comes from an array generated by another function, I tested it with the Msg() in the quoted function and I get 1, 2, 3 as expected.
Thank you Cfillion, I thought MediaTrack was track id but I see now it looks more like a memory reference. I'll try that out.
|
|
|
07-21-2021, 07:42 AM
|
#5
|
Human being with feelings
Join Date: May 2017
Location: Leipzig
Posts: 6,621
|
Ok, I have checked my function again and you were right that it didn't work as you expected.
I have fixed it and it should work with the latest release.
|
|
|
07-25-2021, 06:04 PM
|
#6
|
Human being with feelings
Join Date: Jan 2014
Posts: 5,207
|
Woah,I was actually useful. Yay I feel better about scripting now ;p
|
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -7. The time now is 04:44 AM.
|