RPR AddMediaItemToTrack

From CockosWiki

Jump to: navigation, search

Main Page > REAPER Documentation > ReaScript > ReaScript API, Functions > RPR AddMediaItemToTrack
Main Page > REAPER Documentation > REAPER API > REAPER API Functions > RPR AddMediaItemToTrack

Contents

AddMediaItemToTrack()

MediaItem* AddMediaItemToTrack(MediaTrack* tr) Creates a new media item in the specified track, the new item is created right at the beginning of the track.

Parameters

Parameter typeNameDescription
MediaTrack*trackA pointer to the track that will contain the new item

Returns

Returns a scalar value because there are no modifiable variables in the parameter list.

Parameter typeDescription
MediaItem*A pointer to the new Media Item

Caveats

After using AddMediaItemToTrack() and then setting the contents of the new item with GetSetItemState() you might experience some "graphical glitches" - involving Items apparently disappearing after Zooming in or out, Waveforms (Peaks images) not always displaying.

These odd things resolve after saving the project, closing and then reloading it but that's not something you'll want to do in a script. The Item's display will also be corrected if you use another (Non-GetSet*State function) to update the Item, e.g You could use SetMediaItemInfo_Value() to set the position.

Examples

C++

void DoInsRndFileEx(bool RndLen,bool RndOffset,bool UseTimeSel)
{
	if (g_filenames->GetSize()>0)
	{
		int filenameindex=rand() % g_filenames->GetSize();
 
		t_vect_of_Reaper_tracks TheTracks;
		XenGetProjectTracks(TheTracks,true);
		if (TheTracks.size()>0)
		{
		PCM_source *NewPCM=PCM_Source_CreateFromFile(g_filenames->Get(filenameindex));
		MediaItem *NewItem=AddMediaItemToTrack(TheTracks[0]);		MediaItem_Take *NewTake=AddTakeToMediaItem(NewItem);
		double TimeSelStart=0.0;
		double TimeSelEnd=NewPCM->GetLength();
		GetSet_LoopTimeRange(false,false,&TimeSelStart,&TimeSelEnd,false);
		double ItemPos=TimeSelStart;
		double ItemLen=NewPCM->GetLength();
		double MediaOffset=0.0;
		if (RndOffset) 
		{
			MediaOffset=(NewPCM->GetLength()/RAND_MAX)*rand();
			ItemLen-=MediaOffset;
		}
		if (RndLen) ItemLen=((NewPCM->GetLength()-MediaOffset)/RAND_MAX)*rand();
		if (UseTimeSel) ItemLen=TimeSelEnd-TimeSelStart;
		if (!UseTimeSel) ItemPos=GetCursorPosition();
		GetSetMediaItemTakeInfo(NewTake,"P_SOURCE",NewPCM);
		GetSetMediaItemTakeInfo(NewTake,"D_STARTOFFS",&MediaOffset);
 
		GetSetMediaItemInfo(NewItem,"D_POSITION",&ItemPos);
		GetSetMediaItemInfo(NewItem,"D_LENGTH",&ItemLen);
		Main_OnCommand(40047,0); // build any missing peaks
		SetEditCurPos(ItemPos+ItemLen,false,false);
		Undo_OnStateChangeEx("Insert random file (Extended)",4,-1);
 
		UpdateTimeline();
		}
	}	
}

Perl

#
# AddMediaItemToTrack.pl
#
 
use strict;
use warnings;
 
use constant MSGBOX_JUSTOK => 0;
use constant CURR_PROJ => 0;
 
# Check to make sure there's a track selected
if(RPR_CountSelectedTracks(CURR_PROJ) > 0){
    # Get a reference to the selected track
    my $MediaTrack = RPR_GetSelectedTrack(CURR_PROJ,0);
    # And then use that reference to add a new, very blank, media item
    # We get a reference to the new item returned to us, if it went ok
    if (my $MediaItem = RPR_AddMediaItemToTrack($MediaTrack)){
        # so we can use that reference to make the item long enough to see
        RPR_SetMediaItemInfo_Value($MediaItem, "D_LENGTH", 1.0);
    }
}

Python

#
# AddMediaItemToTrack.py
#
 
MSGBOX_JUSTOK, CURR_PROJ = 0, 0
 
# Check to make sure there's a track selected
if RPR_CountSelectedTracks(CURR_PROJ) > 0:
 
    # Get a reference to the selected track
    MediaTrack = RPR_GetSelectedTrack(CURR_PROJ, 0)
 
    # And then use that reference to add a new, very blank, media item
    # We get a reference to the new item returned to us, if it went ok
    if (MediaItem = RPR_AddMediaItemToTrack(MediaTrack)):
 
        # so we can use that reference to make the item long enough to see
        RPR_SetMediaItemInfo_Value(MediaItem, "D_LENGTH", 1.0)

See Also

item: AddMediaItemToTrack, AddTakeToMediaItem, GetTrackMediaItem, GetTrackNumMediaItems, CountMediaItems, CountSelectedMediaItems, GetItemProjectContext, SetMediaItemInfo_Value, CountTrackMediaItems, SetMediaItemTakeInfo_Value, GetMediaItem, GetMediaItem_Track, GetMediaItemInfo_Value, GetMediaItemNumTakes, GetMediaItemTake, GetMediaItemTake_Item, GetMediaItemTake_Source, GetMediaItemTake_Track, GetMediaItemTakeInfo_Value, SplitMediaItem, MoveMediaItemToTrack, GetSelectedMediaItem, DeleteTrackMediaItem, GetSetItemState, GetSetMediaItemTakeInfo_String, Undo_OnStateChange_Item, UpdateItemInProject,

track: GetLastTouchedTrack, GetMediaItemTake_Track, GetMediaItem_Track, GetMediaTrackInfo_Value, GetNumTracks, GetSelectedTrack, GetSelectedTrackEnvelope, GetSetMediaTrackInfo_String, GetSetTrackState, GetTrack, GetTrackAutomationMode, GetTrackEnvelope, GetTrackEnvelopeByName, GetTrackGUID, GetTrackMIDINoteName, GetTrackMIDINoteNameEx, GetTrackMediaItem, GetTrackNumMediaItems, GetTrackState, GetTrackUIVolPan, HasTrackMIDIPrograms, HasTrackMIDIProgramsEx, InsertTrackAtIndex, MoveMediaItemToTrack, MuteAllTracks, PlayTrackPreview, PlayTrackPreview2, SetMediaTrackInfo_Value, SetTrackAutomationMode, SetTrackMIDINoteName, SetTrackMIDINoteNameEx, SetTrackSelected, SoloAllTracks, StopTrackPreview, StopTrackPreview2, TrackFX_FormatParamValue, TrackFX_GetCount, TrackFX_GetFXName, TrackFX_GetNumParams, TrackFX_GetParam, TrackFX_GetParamName, TrackFX_SetParam, TrackList_AdjustWindows, TrackList_UpdateAllExternalSurfaces, Track_GetPeakInfo, AddMediaItemToTrack, BypassFxAllTracks, CSurf_NumTracks, CSurf_OnTrackSelection, CSurf_SetTrackListChange, CSurf_TrackFromID, CSurf_TrackToID, CountSelectedTracks, CountTrackEnvelopes, CountTrackMediaItems, CountTracks, DeleteTrack, DeleteTrackMediaItem, EnumTrackMIDIProgramNames, EnumTrackMIDIProgramNamesEx,

(Section automatically generated, edits may be overwritten.)

Stuff

Main Page > REAPER Documentation > ReaScript > ReaScript API, Functions > RPR AddMediaItemToTrack
Main Page > REAPER Documentation > REAPER API > REAPER API Functions > RPR AddMediaItemToTrack

Personal tools