elanhickler

getsetchannel

Jan 9th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. struct chantype{ enum { normal, mono, stereo }; };
  2.  
  3. // Returns the leftmost channel number of the take, 0-based, enum of type is normal, mono, stereo
  4. int GetTakeChannel(MediaItem_Take* take, int* chanmode, int* type) {
  5.     int chm, t;
  6.  
  7.     chm = GetMediaItemTakeInfo_Value(take, "I_CHANMODE");
  8.     int ch = chm;
  9.  
  10.     // Get channel type
  11.     if (chm == 0) t = chantype::normal;
  12.     else if (in_range(chm, 2, 66)) t = chantype::mono;
  13.     else t = chantype::stereo;
  14.  
  15.     // Get leftmost channel
  16.     if (chm >= 67) ch = chm - 67;
  17.     else if (chm >= 3) ch = chm - 3;
  18.     else if (chm == 2) ch = chm = 0;
  19.  
  20.     if (chanmode != nullptr) *chanmode = chm;
  21.     if (type != nullptr) *type = t;
  22.     return ch;
  23. }
  24.  
  25. void SetTakeChannel(MediaItem_Take* take, int type, int channel) {
  26.     switch (type) {
  27.     case chantype::normal:
  28.         SetMediaItemTakeInfo_Value(take, "I_CHANMODE", 0);
  29.         break;
  30.     case chantype::mono:
  31.         if (channel < 0) channel = GetTakeChannel(take);
  32.         SetMediaItemTakeInfo_Value(take, "I_CHANMODE", clamp(channel+3, 3, 66));
  33.         break;
  34.     case chantype::stereo:
  35.         if (channel < 0) channel = GetTakeChannel(take);
  36.         SetMediaItemTakeInfo_Value(take, "I_CHANMODE", clamp(channel+67, 67, 129));
  37.         break;
  38.     }
  39. }
  40.  
  41. vector<vector<double>> MultichannelToChannelMode(vector<vector<double>> audio, MediaItem_Take* take) {
  42.     int nch = audio.size();
  43.     vector< vector<double> > v;
  44.     int chanmode;
  45.  
  46.     size_t frames = 0;
  47.     for (int i = 0; i < audio.size(); ++i) frames = max(frames, audio[i].size());
  48.  
  49.     size_t ch = GetTakeChannel(take, &chanmode);
  50.  
  51.     // Stereo
  52.     if (chanmode >= 67) {
  53.         if (nch > ch) v.push_back(audio[ch]); else v.push_back(vector<double>(frames, 0));
  54.         if (nch > ch+1) v.push_back(audio[ch+1]); else v.push_back(vector<double>(frames, 0));
  55.     }
  56.  
  57.     // Mono
  58.     else if (chanmode >= 3) {
  59.         if (nch > ch) v.push_back(audio[ch]); else v.push_back(vector<double>(frames, 0));
  60.     }
  61.  
  62.     // Mono Mix (L + R)
  63.     else if (chanmode == 2) {
  64.         vector<double> temp(frames, 0);
  65.         if (nch > 1) for (int c = 0; c < nch; ++c) {
  66.             for (int s = 0; s < audio[c].size(); ++s) temp[s] = audio[0][s] + audio[1][s];
  67.             v.push_back(temp);
  68.         }
  69.         else v.push_back(audio[0]);
  70.     }
  71.  
  72.     //Reverse Stereo
  73.     else if (chanmode == 1) {
  74.         if (nch > 1) v.push_back(audio[1]);
  75.         v.push_back(audio[0]);
  76.         for (int c = 2; c < nch; ++c) v.push_back(audio[c]);
  77.     }
  78.  
  79.     //Normal
  80.     else if (chanmode == 0) v = audio;
  81.  
  82.     return v;
  83. }
Add Comment
Please, Sign In to add comment