Old 07-08-2021, 09:59 PM   #1
Fergler
Human being with feelings
 
Fergler's Avatar
 
Join Date: Jan 2014
Posts: 5,207
Default 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?
Fergler is offline   Reply With Quote
Old 07-08-2021, 10:08 PM   #2
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,937
Default

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 View Post
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).
cfillion is offline   Reply With Quote
Old 07-09-2021, 07:09 AM   #3
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

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.
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...

Last edited by Meo-Ada Mespotine; 07-09-2021 at 07:15 AM.
Meo-Ada Mespotine is online now   Reply With Quote
Old 07-09-2021, 09:43 AM   #4
Fergler
Human being with feelings
 
Fergler's Avatar
 
Join Date: Jan 2014
Posts: 5,207
Default

Quote:
Originally Posted by Meo-Ada Mespotine View Post
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.
Fergler is offline   Reply With Quote
Old 07-21-2021, 07:42 AM   #5
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

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.
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is online now   Reply With Quote
Old 07-25-2021, 06:04 PM   #6
Fergler
Human being with feelings
 
Fergler's Avatar
 
Join Date: Jan 2014
Posts: 5,207
Default

Woah,I was actually useful. Yay I feel better about scripting now ;p
Fergler 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 04:44 AM.


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