elanhickler

create SM from array of rates v2

Dec 2nd, 2015
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void msg(string m) { //debug console
  2.     ShowConsoleMsg(m.c_str()); //string
  3.     ShowConsoleMsg("\n"); //newline
  4. }
  5.  
  6. void main() {
  7.     vector<double> ts, tr, tro, slope, tp, tdss, tdpp;
  8.     MediaItem_Take* active_take;
  9.     int total_sm;
  10.     double take_start_offset, take_playrate, item_pos, item_length;
  11.  
  12.     auto create_sm = [&](size_t idx) {
  13.         double ruler_pos = ((ts[idx] - take_start_offset)/take_playrate) + item_pos;
  14.         double stored_ecp = GetCursorPosition();
  15.         SetEditCurPos(ruler_pos, 0, 0);
  16.  
  17.         Main_OnCommandEx(41842, 0, 0);
  18.         SetEditCurPos(stored_ecp, 0, 0);
  19.     };
  20.  
  21.     ts ={ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9 };
  22.     tr ={ 0.97, 1.0, 0.99, 0.98, 1.0, 1.03, 1.07, 1.05, 0.98 };
  23.  
  24.     size_t num_ts = ts.size();
  25.  
  26.     tro.resize(num_ts+2);
  27.     slope.resize(num_ts+2);
  28.     tp.resize(num_ts+2);
  29.     tdss.resize(num_ts+2);
  30.     tdpp.resize(num_ts+2);
  31.  
  32.     ts.insert(ts.begin(), 0.0);
  33.     tr.insert(tr.begin(), 0.0);
  34.  
  35.     Undo_BeginBlock();
  36.  
  37.     MediaItem* sel_item = GetSelectedMediaItem(0, 0);
  38.  
  39.     if (sel_item == nullptr) { msg("No selected item"); return; }
  40.  
  41.     active_take = GetActiveTake(sel_item);
  42.  
  43.     if (GetTakeNumStretchMarkers(active_take)) { msg("SM already exist"); return; }
  44.  
  45.     item_pos = GetMediaItemInfo_Value(sel_item, "D_POSITION");
  46.     item_length = GetMediaItemInfo_Value(sel_item, "D_LENGTH");
  47.     take_start_offset = GetMediaItemTakeInfo_Value(active_take, "D_STARTOFFS");
  48.     take_playrate = GetMediaItemTakeInfo_Value(active_take, "D_PLAYRATE");
  49.  
  50.     //zero-anchor
  51.     if (ts[1] <= 0) ts[0] = ts[1]-1; //zero-anchor SM to left of SM1
  52.     else ts[0] = 0; // zero-anchor SM at 0 (source)
  53.     tro[0] = tr[1]; //zero-anchor SM ro same rate as rate1(no slope)
  54.     slope[0] = 0;   //zero-anchor SM flat slope
  55.     tp[0] = ts[0];  //zero-anchor SM P same as its S
  56.     tdss[0] = ts[1]-ts[0]; //zero-anchor SM ΔSS(next S - this S)
  57.     tdpp[0] = tdss[0]/tro[0]; //zero-anchor SM ΔPP = ΔSS/ro
  58.  
  59.     PreventUIRefresh(1); //prevent UI refresh(hold screen)
  60.  
  61.     // end-anchor
  62.     ts.push_back(ts[num_ts]+1); //end-anchor SM S at last SM S +1sec
  63.     tr.push_back(tr[num_ts]); //end-anchor SM tr at last SM rate(for array)
  64.     //tro[num_ts+1] = tr[num_ts] //end-anchor SM ro at last SM rate
  65.     slope[num_ts+1] = 0; //end-anchor SM flat slope
  66.     create_sm(0); //create zero-anchor SM using ts, ts as tp, no slope
  67.  
  68.     for (size_t i = 1; i <= num_ts; ++i) { //loop through table positions and rates, using <= instead of < for 1-based array
  69.         tro[i] = (tr[i]+tr[i+1])/2; //overall rate = mean of r1 and r2 rates for marker
  70.         slope[i] = 1-((2*tr[i])/(tr[i]+tr[i+1])); //slope = function of r1 and r2
  71.         tp[i] = tp[i-1]+tdpp[i-1]; // this P = last P + last ΔPP
  72.         tdss[i] = ts[i+1]-ts[i]; // this ΔSS = next S - this S
  73.         tdpp[i] = tdss[i]/tro[i]; // this ΔPP = ΔSS/ro
  74.         create_sm(i); //create SM using ts, ts as tp, no slope
  75.     }
  76.  
  77.     tp[num_ts+1] = tp[num_ts]+tdpp[num_ts]; //end-anchor SM P = last P + last ΔPP
  78.     create_sm(num_ts+1); //create end-anchor SM using ts, ts as tp, no slope
  79.  
  80.     //modify all SM using ts, tp, slope
  81.     for (size_t i = 0; i <= num_ts+1; ++i) {
  82.         SetTakeStretchMarker(active_take, i, tp[i], nullptr);
  83.         SetTakeStretchMarkerSlope(active_take, i, slope[i]);
  84.     }
  85.  
  86.     PreventUIRefresh(-1); //restore UI refresh
  87.     UpdateArrange(); //redraws the arrange window
  88.     Undo_EndBlock("Create SMs from array", -1); //close undo block and give annotation to undo stack.
  89. }
Advertisement
Add Comment
Please, Sign In to add comment