Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void msg(string m) { //debug console
- ShowConsoleMsg(m.c_str()); //string
- ShowConsoleMsg("\n"); //newline
- }
- void main() {
- // define stuff at the top to make it easier for lambda functions
- vector<double> ts, tr, tro, slope, tp, tdss, tdpp;
- MediaItem_Take* active_take;
- int total_sm;
- double take_start_offset, take_playrate, item_pos, item_length;
- // lambda functions
- function<void(size_t)> create_sm = [&](size_t idx) { //use item_pos, item_playrate, item length, source_pos to get ruler positions of SM
- double ruler_pos = ((ts[idx] - take_start_offset)/take_playrate) + item_pos;
- double stored_ecp = GetCursorPosition(); //store cursor
- SetEditCurPos(ruler_pos, 0, 0); //move cursor to ruler-time S-pos
- // create SM(blank default, using S, S for P and no slope)
- Main_OnCommandEx(41842, 0, 0); //create SM at edit cursor
- SetEditCurPos(stored_ecp, 0, 0); //restore cursor
- };
- function<void(size_t)> modify_sm = [&](size_t idx) {
- //write to SM(idx) calculated P
- int retval = SetTakeStretchMarker(active_take, idx, tp[idx], nullptr); //sets P point, no slope yet
- // write slope to SM slope
- retval = SetTakeStretchMarkerSlope(active_take, idx, slope[idx]); //write corrected slopes
- };
- ts ={ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9 }; // 1-based, SM S-pos table
- tr ={ 0.97, 1.0, 0.99, 0.98, 1.0, 1.03, 1.07, 1.05, 0.98 }; // 1-based, SM rate table
- size_t num_ts = ts.size(); // length of array as input above, +1 to handle 1-based table, +1 for end anchor
- // check #ts == #tp at this point?
- tro.resize(num_ts+2); //SM ro table
- slope.resize(num_ts+2); //SM slope table
- tp.resize(num_ts+2); //SM P-pos table
- tdss.resize(num_ts+2); //SM ΔSS table
- tdpp.resize(num_ts+2); //SM ΔPP table
- ts.insert(ts.begin(), 0); // make one more index to deal with 1-based arrays
- tr.insert(tr.begin(), 0);
- ts.push_back(0.0); // add one more index to hold the end anchor
- tr.push_back(0.0);
- Undo_BeginBlock(); //Hold undo block to all of proj altering code...
- MediaItem* sel_item = GetSelectedMediaItem(0, 0); //selected item
- if (( (sel_item != nullptr) & CountSelectedMediaItems(0) ) == 1) {
- // collect parameter on sel item and active take for SM decisions and S-pos to ruler-time conversion
- active_take = GetActiveTake(sel_item); //active take in item
- total_sm = GetTakeNumStretchMarkers(active_take); //count SM in take
- item_pos = GetMediaItemInfo_Value(sel_item, "D_POSITION"); //item pos in timeline
- item_length = GetMediaItemInfo_Value(sel_item, "D_LENGTH"); //item length
- take_start_offset = GetMediaItemTakeInfo_Value(active_take, "D_STARTOFFS"); //take start offset
- take_playrate = GetMediaItemTakeInfo_Value(active_take, "D_PLAYRATE"); //take playrate(will combine with SM warping)
- if (total_sm == 0) {
- PreventUIRefresh(1); //prevent UI refresh(hold screen)
- //zero-anchor
- if (ts[1] <= 0) ts[0] = ts[1]-1; //zero-anchor SM to left of SM1
- else ts[0] = 0; // zero-anchor SM at 0 (source)
- tro[0] = tr[1]; //zero-anchor SM ro same rate as rate1(no slope)
- slope[0] = 0; //zero-anchor SM flat slope
- tp[0] = ts[0]; //zero-anchor SM P same as its S
- tdss[0] = ts[1]-ts[0]; //zero-anchor SM ΔSS(next S - this S)
- tdpp[0] = tdss[0]/tro[0]; //zero-anchor SM ΔPP = ΔSS/ro
- // end-anchor
- ts[num_ts+1] = ts[num_ts]+1; //end-anchor SM S at last SM S +1sec
- tr[num_ts+1] = tr[num_ts]; //end-anchor SM tr at last SM rate(for array)
- //tro[num_ts+1] = tr[num_ts] //end-anchor SM ro at last SM rate
- slope[num_ts+1] = 0; //end-anchor SM flat slope
- create_sm(0); //create zero-anchor SM using ts, ts as tp, no slope
- for (size_t i = 1; i <= num_ts; ++i) { //loop through table positions and rates, using <= instead of < for 1-based array
- tro[i] = (tr[i]+tr[i+1])/2; //overall rate = mean of r1 and r2 rates for marker
- slope[i] = 1-((2*tr[i])/(tr[i]+tr[i+1])); //slope = function of r1 and r2
- tp[i] = tp[i-1]+tdpp[i-1]; // this P = last P + last ΔPP
- tdss[i] = ts[i+1]-ts[i]; // this ΔSS = next S - this S
- tdpp[i] = tdss[i]/tro[i]; // this ΔPP = ΔSS/ro
- create_sm(i); //create SM using ts, ts as tp, no slope
- }
- tp[num_ts+1] = tp[num_ts]+tdpp[num_ts]; //end-anchor SM P = last P + last ΔPP
- create_sm(num_ts+1); //create end-anchor SM using ts, ts as tp, no slope
- for (size_t i = 0; i <= num_ts+1; ++i) { //using <= instead of < for 1-based array
- modify_sm(i); //modify all SM using ts, tp, slope
- }
- PreventUIRefresh(-1); //restore UI refresh
- UpdateArrange(); //redraws the arrange window
- }
- else msg("SM already exist"); //use console msg() for debug reports, msg: already SM in take
- }
- else if (!sel_item) msg("No selected item"); //msg: no sel item
- else if (CountSelectedMediaItems(0) > 1) msg("more than 1 selected items"); //msg: more than one item sel
- Undo_EndBlock("Create SMs from array", -1); //close undo block and give annotation to undo stack.
- }
Advertisement
Add Comment
Please, Sign In to add comment