elanhickler

create SM from array of rates v1

Dec 2nd, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.88 KB | None | 0 0
  1. void msg(string m) { //debug console
  2.     ShowConsoleMsg(m.c_str()); //string
  3.     ShowConsoleMsg("\n"); //newline
  4. }
  5.  
  6. void main() {
  7.     // define stuff at the top to make it easier for lambda functions
  8.     vector<double> ts, tr, tro, slope, tp, tdss, tdpp;
  9.     MediaItem_Take* active_take;
  10.     int total_sm;
  11.     double take_start_offset, take_playrate, item_pos, item_length;
  12.  
  13.     // lambda functions
  14.     function<void(size_t)> create_sm = [&](size_t idx) { //use item_pos, item_playrate, item length, source_pos to get ruler positions of SM
  15.         double ruler_pos = ((ts[idx] - take_start_offset)/take_playrate) + item_pos;
  16.         double stored_ecp = GetCursorPosition();    //store cursor
  17.         SetEditCurPos(ruler_pos, 0, 0); //move cursor to ruler-time S-pos
  18.         // create SM(blank default, using S, S for P and no slope)
  19.         Main_OnCommandEx(41842, 0, 0); //create SM at edit cursor
  20.         SetEditCurPos(stored_ecp, 0, 0); //restore cursor
  21.     };
  22.  
  23.     function<void(size_t)> modify_sm = [&](size_t idx) {
  24.         //write to SM(idx) calculated P
  25.         int retval = SetTakeStretchMarker(active_take, idx, tp[idx], nullptr); //sets P point, no slope yet
  26.         // write slope to SM slope
  27.         retval = SetTakeStretchMarkerSlope(active_take, idx, slope[idx]);  //write corrected slopes
  28.     };
  29.  
  30.     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
  31.     tr ={ 0.97, 1.0, 0.99, 0.98, 1.0, 1.03, 1.07, 1.05, 0.98 }; // 1-based, SM rate table
  32.     size_t num_ts = ts.size(); // length of array as input above, +1 to handle 1-based table, +1 for end anchor
  33.     // check #ts == #tp at this point?
  34.     tro.resize(num_ts+2); //SM ro table
  35.     slope.resize(num_ts+2); //SM slope table
  36.     tp.resize(num_ts+2); //SM P-pos table
  37.     tdss.resize(num_ts+2); //SM ΔSS table
  38.     tdpp.resize(num_ts+2); //SM ΔPP table
  39.    
  40.     ts.insert(ts.begin(), 0); // make one more index to deal with 1-based arrays
  41.     tr.insert(tr.begin(), 0);
  42.     ts.push_back(0.0); // add one more index to hold the end anchor
  43.     tr.push_back(0.0);
  44.  
  45.     Undo_BeginBlock(); //Hold undo block to all of proj altering code...
  46.     MediaItem* sel_item = GetSelectedMediaItem(0, 0); //selected item
  47.     if (( (sel_item != nullptr) & CountSelectedMediaItems(0) ) == 1) {
  48.         // collect parameter on sel item and active take for SM decisions and S-pos to ruler-time conversion
  49.         active_take = GetActiveTake(sel_item);  //active take in item
  50.         total_sm = GetTakeNumStretchMarkers(active_take); //count SM in take
  51.         item_pos = GetMediaItemInfo_Value(sel_item, "D_POSITION");  //item pos in timeline
  52.         item_length = GetMediaItemInfo_Value(sel_item, "D_LENGTH"); //item length
  53.         take_start_offset = GetMediaItemTakeInfo_Value(active_take, "D_STARTOFFS");  //take start offset
  54.         take_playrate = GetMediaItemTakeInfo_Value(active_take, "D_PLAYRATE"); //take playrate(will combine with SM warping)
  55.         if (total_sm == 0) {
  56.             PreventUIRefresh(1); //prevent UI refresh(hold screen)
  57.             //zero-anchor
  58.             if (ts[1] <= 0) ts[0] = ts[1]-1; //zero-anchor SM to left of SM1
  59.             else ts[0] = 0; // zero-anchor SM at 0 (source)
  60.  
  61.             tro[0] = tr[1]; //zero-anchor SM ro same rate as rate1(no slope)
  62.             slope[0] = 0;   //zero-anchor SM flat slope
  63.             tp[0] = ts[0];  //zero-anchor SM P same as its S
  64.             tdss[0] = ts[1]-ts[0]; //zero-anchor SM ΔSS(next S - this S)
  65.             tdpp[0] = tdss[0]/tro[0]; //zero-anchor SM ΔPP = ΔSS/ro
  66.             // end-anchor
  67.             ts[num_ts+1] = ts[num_ts]+1; //end-anchor SM S at last SM S +1sec
  68.             tr[num_ts+1] = tr[num_ts]; //end-anchor SM tr at last SM rate(for array)
  69.             //tro[num_ts+1] = tr[num_ts] //end-anchor SM ro at last SM rate
  70.             slope[num_ts+1] = 0; //end-anchor SM flat slope
  71.  
  72.             create_sm(0); //create zero-anchor SM using ts, ts as tp, no slope
  73.  
  74.             for (size_t i = 1; i <= num_ts; ++i) { //loop through table positions and rates, using <= instead of < for 1-based array
  75.                 tro[i] = (tr[i]+tr[i+1])/2; //overall rate = mean of r1 and r2 rates for marker
  76.                 slope[i] = 1-((2*tr[i])/(tr[i]+tr[i+1])); //slope = function of r1 and r2
  77.                 tp[i] = tp[i-1]+tdpp[i-1]; // this P = last P + last ΔPP
  78.                 tdss[i] = ts[i+1]-ts[i]; // this ΔSS = next S - this S
  79.                 tdpp[i] = tdss[i]/tro[i]; // this ΔPP = ΔSS/ro
  80.                 create_sm(i); //create SM using ts, ts as tp, no slope
  81.             }
  82.  
  83.             tp[num_ts+1] = tp[num_ts]+tdpp[num_ts]; //end-anchor SM P = last P + last ΔPP
  84.             create_sm(num_ts+1); //create end-anchor SM using ts, ts as tp, no slope
  85.  
  86.             for (size_t i = 0; i <= num_ts+1; ++i) { //using <= instead of < for 1-based array
  87.                 modify_sm(i); //modify all SM using ts, tp, slope
  88.             }
  89.             PreventUIRefresh(-1); //restore UI refresh
  90.             UpdateArrange(); //redraws the arrange window
  91.         }
  92.         else msg("SM already exist");   //use console msg() for debug reports, msg: already SM in take
  93.     }
  94.     else if (!sel_item) msg("No selected item"); //msg: no sel item
  95.     else if (CountSelectedMediaItems(0) > 1) msg("more than 1 selected items"); //msg: more than one item sel
  96.     Undo_EndBlock("Create SMs from array", -1); //close undo block and give annotation to undo stack.
  97. }
Advertisement
Add Comment
Please, Sign In to add comment