Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

midimapper.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. #include "ports.h"
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <map>
  5. #include <sstream>
  6. #include <deque>
  7. #include <utility>
  8. #include <cassert>
  9. #include "miditable.h"
  10. using namespace rtosc;
  11. using std::string;
  12. using std::get;
  13. using std::tuple;
  14. using std::make_tuple;
  15. /********************
  16. * Helper Templates *
  17. ********************/
  18. template<class T, class U>
  19. bool has_t(const T &t, const U&u)
  20. {return t.find(u) != t.end();}
  21. template<class T, class U>
  22. bool has2(const T &t, const U&u) {
  23. for(const U&uu:t)
  24. if(uu==u)
  25. return true;
  26. return false;
  27. }
  28. template<class T,class U>
  29. int getInd(const T&t,const U&u){
  30. int i=0;
  31. for(const U&uu:t) {
  32. if(uu==u)
  33. return i;
  34. else
  35. i++;
  36. }
  37. return -1;
  38. }
  39. /***********
  40. * Storage *
  41. ***********/
  42. bool MidiMapperStorage::handleCC(int ID, int val, write_cb write)
  43. {
  44. for(int i=0; i<mapping.size(); ++i)
  45. {
  46. if(std::get<0>(mapping[i]) == ID)
  47. {
  48. bool coarse = std::get<1>(mapping[i]);
  49. int ind = std::get<2>(mapping[i]);
  50. if(coarse)
  51. values[ind] = (val<<7)|(values[ind]&0x7f);
  52. else
  53. values[ind] = val|(values[ind]&0x3f80);
  54. callbacks[ind](values[ind],write);
  55. return true;
  56. }
  57. }
  58. return false;
  59. }
  60. //TODO try to change O(n^2) algorithm to O(n)
  61. void MidiMapperStorage::cloneValues(const MidiMapperStorage &storage)
  62. {
  63. //XXX this method is SUPER error prone
  64. for(int i=0; i<values.size(); ++i)
  65. values[i] = 0;
  66. for(int i=0; i<mapping.size(); ++i) {
  67. for(int j=0; j<storage.mapping.size(); ++j) {
  68. if(std::get<0>(mapping[i]) == std::get<0>(storage.mapping[j])) {
  69. bool coarse_src = std::get<1>(storage.mapping[j]);
  70. int ind_src = std::get<2>(storage.mapping[j]);
  71. bool coarse_dest = std::get<1>(mapping[i]);
  72. int ind_dest = std::get<2>(mapping[i]);
  73. int val = 0;
  74. //Extract
  75. if(coarse_src)
  76. val = storage.values[ind_src]>>7;
  77. else
  78. val = storage.values[ind_src]&0x7f;
  79. //Blit
  80. if(coarse_dest)
  81. values[ind_dest] = (val<<7)|(values[ind_dest]&0x7f);
  82. else
  83. values[ind_dest] = val|(values[ind_dest]&0x3f80);
  84. }
  85. }
  86. }
  87. }
  88. MidiMapperStorage *MidiMapperStorage::clone(void)
  89. {
  90. MidiMapperStorage *nstorage = new MidiMapperStorage();
  91. nstorage->values = values.sized_clone();
  92. nstorage->mapping = mapping.clone();
  93. nstorage->callbacks = callbacks.clone();
  94. return nstorage;
  95. }
  96. int MidiBijection::operator()(float x) const {
  97. if(mode == 0)
  98. return ((x-min)/(max-min))*(1<<14);
  99. else
  100. return 0;
  101. }
  102. float MidiBijection::operator()(int x) const {
  103. if(mode == 0)
  104. return x/((1<<14)*1.0)*(max-min)+min;
  105. else
  106. return 0;
  107. }
  108. /************************
  109. * Non realtime portion *
  110. ************************/
  111. MidiMappernRT::MidiMappernRT(void)
  112. :storage(0),base_ports(0)
  113. {}
  114. void MidiMappernRT::map(const char *addr, bool coarse)
  115. {
  116. for(auto x:learnQueue)
  117. if(x.first == addr && x.second == coarse)
  118. return;
  119. unMap(addr, coarse);
  120. learnQueue.push_back(std::make_pair(addr,coarse));
  121. char buf[1024];
  122. rtosc_message(buf, 1024, "/midi-add-watch","");
  123. rt_cb(buf);
  124. }
  125. MidiMapperStorage *MidiMappernRT::generateNewBijection(const Port &port, std::string addr)
  126. {
  127. MidiBijection bi;
  128. bi.mode = 0;
  129. bi.min = atof(port.meta()["min"]);
  130. bi.max = atof(port.meta()["max"]);
  131. auto tmp = [bi,addr](int16_t x, MidiMapperStorage::write_cb cb) {
  132. float out = bi(x);
  133. char buf[1024];
  134. rtosc_message(buf, 1024, addr.c_str(), "f", out);
  135. cb(buf);
  136. };
  137. MidiMapperStorage *nstorage = new MidiMapperStorage();
  138. if(storage) {
  139. //XXX not quite
  140. nstorage->values = storage->values.one_larger();
  141. nstorage->mapping = storage->mapping.clone();//insert(std::make_tuple(ID, true, storage->callbacks.size()));
  142. nstorage->callbacks = storage->callbacks.insert(tmp);
  143. } else {
  144. nstorage->values = nstorage->values.insert(0);
  145. nstorage->mapping = nstorage->mapping.clone();//insert(std::make_tuple(ID, true, 0));
  146. nstorage->callbacks = nstorage->callbacks.insert(tmp);
  147. }
  148. inv_map[addr] = std::make_tuple(nstorage->callbacks.size()-1, -1,-1,bi);
  149. return nstorage;
  150. }
  151. void MidiMappernRT::addNewMapper(int ID, const Port &port, std::string addr)
  152. {
  153. MidiBijection bi;
  154. bi.mode = 0;
  155. bi.min = atof(port.meta()["min"]);
  156. bi.max = atof(port.meta()["max"]);
  157. auto tmp = [bi,addr](int16_t x, MidiMapperStorage::write_cb cb) {
  158. float out = bi(x);
  159. char buf[1024];
  160. rtosc_message(buf, 1024, addr.c_str(), "f", out);
  161. cb(buf);
  162. };
  163. MidiMapperStorage *nstorage = new MidiMapperStorage();
  164. if(storage) {
  165. //XXX not quite
  166. nstorage->values = storage->values.one_larger();
  167. nstorage->mapping = storage->mapping.insert(std::make_tuple(ID, true, storage->callbacks.size()));
  168. nstorage->callbacks = storage->callbacks.insert(tmp);
  169. } else {
  170. nstorage->values = nstorage->values.insert(0);
  171. nstorage->mapping = nstorage->mapping.insert(std::make_tuple(ID, true, 0));
  172. nstorage->callbacks = nstorage->callbacks.insert(tmp);
  173. }
  174. storage = nstorage;
  175. inv_map[addr] = std::make_tuple(storage->callbacks.size()-1, ID,-1,bi);
  176. }
  177. void MidiMappernRT::addFineMapper(int ID, const Port &port, std::string addr)
  178. {
  179. (void) port;
  180. //TODO asserts
  181. //Bijection already created
  182. //Coarse node already active
  183. //Value already allocated
  184. //Find mapping
  185. int mapped_ID = std::get<0>(inv_map[addr]);
  186. std::get<2>(inv_map[addr]) = ID;
  187. MidiMapperStorage *nstorage = new MidiMapperStorage();
  188. nstorage->values = storage->values.sized_clone();
  189. nstorage->mapping = storage->mapping.insert(std::make_tuple(ID, false, mapped_ID));
  190. nstorage->callbacks = storage->callbacks.insert(storage->callbacks[mapped_ID]);
  191. storage = nstorage;
  192. }
  193. void killMap(int ID, MidiMapperStorage &m)
  194. {
  195. MidiMapperStorage::TinyVector<tuple<int, bool, int>> nmapping(m.mapping.size()-1);
  196. int j=0;
  197. for(int i=0; i<m.mapping.size(); i++)
  198. if(get<0>(m.mapping[i]) != ID)
  199. nmapping[j++] = m.mapping[i];
  200. assert(j == nmapping.size());
  201. m.mapping = nmapping;
  202. }
  203. void MidiMappernRT::useFreeID(int ID)
  204. {
  205. if(learnQueue.empty())
  206. return;
  207. std::string addr = std::get<0>(learnQueue.front());
  208. bool coarse = std::get<1>(learnQueue.front());
  209. learnQueue.pop_front();
  210. assert(base_ports);
  211. const rtosc::Port *p = base_ports->apropos(addr.c_str());
  212. assert(p);
  213. MidiMapperStorage *nstorage;
  214. if(inv_map.find(addr) == inv_map.end())
  215. nstorage = generateNewBijection(*p, addr);
  216. else
  217. nstorage = storage->clone();
  218. auto imap = inv_map[addr];
  219. int mapped_ID = std::get<0>(imap);
  220. nstorage->mapping = nstorage->mapping.insert(make_tuple(ID, coarse, mapped_ID));
  221. if(coarse) {
  222. if(get<1>(imap) != -1)
  223. killMap(get<1>(imap), *nstorage);
  224. inv_map[addr] = make_tuple(get<0>(imap), ID, get<2>(imap), get<3>(imap));
  225. } else {
  226. if(get<2>(imap) != -1)
  227. killMap(get<1>(imap), *nstorage);
  228. inv_map[addr] = make_tuple(get<0>(imap), get<1>(imap), ID, get<3>(imap));
  229. }
  230. storage = nstorage;
  231. //TODO clean up unused value and callback objects
  232. char buf[1024];
  233. rtosc_message(buf, 1024, "/midi-bind", "b", sizeof(storage), &storage);
  234. rt_cb(buf);
  235. };
  236. void MidiMappernRT::unMap(const char *addr, bool coarse)
  237. {
  238. printf("Unmapping('%s',%d)\n",addr,coarse);
  239. if(inv_map.find(addr) == inv_map.end())
  240. return;
  241. auto imap = inv_map[addr];
  242. int kill_id = -1;
  243. if(coarse) {
  244. kill_id = get<1>(imap);
  245. inv_map[addr] = make_tuple(get<0>(imap), -1, get<2>(imap), get<3>(imap));
  246. } else {
  247. kill_id = get<2>(imap);
  248. inv_map[addr] = make_tuple(get<0>(imap), get<1>(imap), -1, get<3>(imap));
  249. }
  250. if(kill_id == -1)
  251. return;
  252. MidiMapperStorage *nstorage = storage->clone();
  253. killMap(kill_id, *nstorage);
  254. storage = nstorage;
  255. //TODO clean up unused value and callback objects
  256. char buf[1024];
  257. rtosc_message(buf, 1024, "/midi-bind", "b", sizeof(storage), &storage);
  258. rt_cb(buf);
  259. }
  260. void MidiMappernRT::delMapping(int ID, bool coarse, const char *addr){
  261. (void) ID;
  262. (void) coarse;
  263. (void) addr;
  264. };
  265. void MidiMappernRT::replaceMapping(int, bool, const char *){};
  266. std::map<std::string, std::string> MidiMappernRT::getMidiMappingStrings(void)
  267. {
  268. std::map<std::string, std::string> result;
  269. for(auto s:inv_map)
  270. result[s.first] = getMappedString(s.first);
  271. char ID = 'A';
  272. for(auto s:learnQueue)
  273. {
  274. if(s.second == false)
  275. result[s.first] += std::string(":")+ID++;
  276. else
  277. result[s.first] = ID++;
  278. }
  279. return result;
  280. }
  281. //unclear if this should be be here as a helper or not
  282. std::string MidiMappernRT::getMappedString(std::string addr)
  283. {
  284. std::stringstream out;
  285. //find coarse
  286. if(has_t(inv_map,addr)) {
  287. if(std::get<1>(inv_map[addr]) != -1)
  288. out << std::get<1>(inv_map[addr]);
  289. }else if(has2(learnQueue, make_pair(addr,true)))
  290. out << getInd(learnQueue,std::make_pair(addr,true));
  291. //find Fine
  292. if(has_t(inv_map,addr)) {
  293. if(std::get<2>(inv_map[addr]) != -1)
  294. out << ":" << std::get<2>(inv_map[addr]);
  295. } else if(has2(learnQueue, make_pair(addr,false)))
  296. out << getInd(learnQueue,std::make_pair(addr,false));
  297. return out.str();
  298. }
  299. MidiBijection MidiMappernRT::getBijection(std::string s)
  300. {
  301. return std::get<3>(inv_map[s]);
  302. }
  303. void MidiMappernRT::snoop(const char *msg)
  304. {
  305. if(inv_map.find(msg) != inv_map.end())
  306. {
  307. auto apple = inv_map[msg];
  308. MidiBijection bi = getBijection(msg);
  309. float value = 0;
  310. std::string args = rtosc_argument_string(msg);
  311. if(args == "f")
  312. value = rtosc_argument(msg, 0).f;
  313. else if(args == "i")
  314. value = rtosc_argument(msg, 0).i;
  315. else if(args == "T")
  316. value = 1.0;
  317. else if(args == "F")
  318. value = 0.0;
  319. else
  320. return;
  321. int new_midi = bi(value);
  322. //printf("--------------------------------------------\n");
  323. //printf("msg = '%s'\n", msg);
  324. //printf("--------------------------------------------\n");
  325. //printf("new midi value: %f->'%x'\n", value, new_midi);
  326. if(std::get<1>(apple) != -1)
  327. apply_high(new_midi,std::get<1>(apple));
  328. if(std::get<2>(apple) != -1)
  329. apply_low(new_midi,std::get<2>(apple));
  330. }
  331. };
  332. void MidiMappernRT::apply_high(int v, int ID) { apply_midi(v>>7,ID); }
  333. void MidiMappernRT::apply_low(int v, int ID) { apply_midi(0x7f&v,ID);}
  334. void MidiMappernRT::apply_midi(int val, int ID)
  335. {
  336. char buf[1024];
  337. rtosc_message(buf,1024,"/virtual_midi_cc","ii",val,ID);
  338. rt_cb(buf);
  339. }
  340. void MidiMappernRT::setBounds(const char *str, float low, float high)
  341. {
  342. if(inv_map.find(str) == inv_map.end())
  343. return;
  344. string addr = str;
  345. auto imap = inv_map[str];
  346. auto newBi = MidiBijection{0,low,high};
  347. inv_map[str] = make_tuple(get<0>(imap),get<1>(imap),get<2>(imap),newBi);
  348. MidiMapperStorage *nstorage = storage->clone();
  349. nstorage->callbacks[get<0>(imap)] = [newBi,addr](int16_t x, MidiMapperStorage::write_cb cb) {
  350. float out = newBi(x);
  351. char buf[1024];
  352. rtosc_message(buf, 1024, addr.c_str(), "f", out);
  353. cb(buf);
  354. };
  355. storage = nstorage;
  356. char buf[1024];
  357. rtosc_message(buf, 1024, "/midi-bind", "b", sizeof(storage), &storage);
  358. rt_cb(buf);
  359. }
  360. std::tuple<float,float,float,float> MidiMappernRT::getBounds(const char *str)
  361. {
  362. const rtosc::Port *p = base_ports->apropos(str);
  363. assert(p);
  364. float min_val = atof(p->meta()["min"]);
  365. float max_val = atof(p->meta()["max"]);
  366. if(inv_map.find(str) != inv_map.end()) {
  367. auto elm = std::get<3>(inv_map[str]);
  368. return std::make_tuple(min_val, max_val,elm.min,elm.max);
  369. }
  370. return std::make_tuple(min_val, max_val,-1.0f,-1.0f);
  371. }
  372. bool MidiMappernRT::has(std::string addr)
  373. {
  374. return inv_map.find(addr) != inv_map.end();
  375. }
  376. bool MidiMappernRT::hasPending(std::string addr)
  377. {
  378. for(auto s:learnQueue)
  379. if(s.first == addr)
  380. return true;
  381. return false;
  382. }
  383. bool MidiMappernRT::hasCoarse(std::string addr)
  384. {
  385. if(!has(addr))
  386. return false;
  387. auto e = inv_map[addr];
  388. return std::get<1>(e) != -1;
  389. }
  390. bool MidiMappernRT::hasFine(std::string addr)
  391. {
  392. if(!has(addr))
  393. return false;
  394. auto e = inv_map[addr];
  395. return std::get<2>(e) != -1;
  396. }
  397. bool MidiMappernRT::hasCoarsePending(std::string addr)
  398. {
  399. for(auto s:learnQueue)
  400. if(s.first == addr && s.second)
  401. return true;
  402. return false;
  403. }
  404. bool MidiMappernRT::hasFinePending(std::string addr)
  405. {
  406. for(auto s:learnQueue)
  407. if(s.first == addr && !s.second)
  408. return true;
  409. return false;
  410. }
  411. int MidiMappernRT::getCoarse(std::string addr)
  412. {
  413. if(!has(addr))
  414. return -1;
  415. auto e = inv_map[addr];
  416. return std::get<1>(e);
  417. }
  418. int MidiMappernRT::getFine(std::string addr)
  419. {
  420. if(!has(addr))
  421. return -1;
  422. auto e = inv_map[addr];
  423. return std::get<2>(e);
  424. }
  425. /*****************
  426. * Realtime code *
  427. *****************/
  428. MidiMapperRT::MidiMapperRT(void)
  429. :storage(NULL), watchSize(0)
  430. {}
  431. void MidiMapperRT::setBackendCb(std::function<void(const char*)> cb) {backend = cb;}
  432. void MidiMapperRT::setFrontendCb(std::function<void(const char*)> cb) {frontend = cb;}
  433. void MidiMapperRT::handleCC(int ID, int val) {
  434. if((!storage || !storage->handleCC(ID, val, backend)) && !pending.has(ID) && watchSize) {
  435. watchSize--;
  436. pending.insert(ID);
  437. char msg[1024];
  438. rtosc_message(msg, 1024, "/midi-use-CC", "i", ID);
  439. frontend(msg);
  440. }
  441. }
  442. void MidiMapperRT::addWatch(void) {watchSize++;}
  443. void MidiMapperRT::remWatch(void) {if(watchSize) watchSize--;}
  444. Port MidiMapperRT::addWatchPort(void) {
  445. return Port{"midi-add-watch","",0, [this](msg_t, RtData&) {
  446. this->addWatch();
  447. }};
  448. }
  449. Port MidiMapperRT::removeWatchPort(void) {
  450. return Port{"midi-remove-watch","",0, [this](msg_t, RtData&) {
  451. this->remWatch();
  452. }};
  453. }
  454. Port MidiMapperRT::bindPort(void) {
  455. return Port{"midi-bind:b","",0, [this](msg_t msg, RtData&) {
  456. pending.pop();
  457. MidiMapperStorage *nstorage =
  458. *(MidiMapperStorage**)rtosc_argument(msg,0).b.data;
  459. if(storage) {
  460. nstorage->cloneValues(*storage);
  461. storage = nstorage;
  462. } else
  463. storage = nstorage;
  464. //TODO memory deallocation
  465. }};
  466. }