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.

279 lines
8.2KB

  1. /*
  2. * Copyright (c) 2012 Mark McCurry
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. #pragma once
  25. #include "ports.h"
  26. #include <string.h>
  27. #include <algorithm>
  28. #include <map>
  29. #include <sstream>
  30. #include <deque>
  31. #include <utility>
  32. #include <cassert>
  33. namespace rtosc {
  34. /**
  35. * Module Overview
  36. *
  37. * Actions:
  38. * - Add a mapping {coarse/fine} [nRT]
  39. * - Delete a mapping {coarse/fine} [nRT]
  40. * - Transform mapping value based on passive observation [nRT]
  41. * - Find unused CC numbers [RT]
  42. * - Transform CC into event {coarse/fine} [RT]
  43. */
  44. class MidiMapperStorage
  45. {
  46. public:
  47. //Almost immutable short vector class
  48. template<class T>
  49. class TinyVector {
  50. int n;
  51. T *t;
  52. public:
  53. TinyVector(void):n(0),t(0){}
  54. TinyVector(int i):n(i),t(new T[i]){}
  55. T&operator[](int i) {assert(i>=0 && i<n);return t[i];}
  56. T operator[](int i) const {assert(i>=0 && i<n);return t[i];}
  57. TinyVector insert(const T &t_)
  58. {TinyVector next(n+1); for(int i=0;i<n; ++i) next.t[i]=t[i]; next.t[n] = t_;return std::move(next);}
  59. TinyVector one_larger(void)
  60. {TinyVector next(n+1); for(int i=0;i<n + 1; ++i) next.t[i]=0; return std::move(next);}
  61. TinyVector sized_clone(void)
  62. {TinyVector next(n); for(int i=0;i<n; ++i) next.t[i]=0; return std::move(next);}
  63. TinyVector clone(void)
  64. {TinyVector next(n); for(int i=0;i<n; ++i) next.t[i]=t[i]; return std::move(next);}
  65. int size(void) const{return n;}
  66. };
  67. typedef std::function<void(const char*)> write_cb;
  68. typedef std::function<void(int16_t,write_cb)> callback_t;
  69. //RT Read Only
  70. TinyVector<std::tuple<int, bool, int>> mapping;//CC->{coarse, val-cb offset}
  71. TinyVector<callback_t> callbacks;
  72. //RT RW
  73. TinyVector<int> values;
  74. bool handleCC(int ID, int val, write_cb write);
  75. //TODO try to change O(n^2) algorithm to O(n)
  76. void cloneValues(const MidiMapperStorage &storage);
  77. MidiMapperStorage *clone(void);
  78. };
  79. struct MidiBijection
  80. {
  81. int mode;//0:linear,1:log
  82. float min;
  83. float max;
  84. int operator()(float x) const;
  85. float operator()(int x) const;
  86. };
  87. #include <cassert>
  88. class MidiMappernRT
  89. {
  90. public:
  91. MidiMappernRT(void);
  92. void map(const char *addr, bool coarse = true);
  93. MidiMapperStorage *generateNewBijection(const Port &port, std::string);
  94. void addNewMapper(int ID, const Port &port, std::string addr);
  95. void addFineMapper(int ID, const Port &port, std::string addr);
  96. void useFreeID(int ID);
  97. void unMap(const char *addr, bool coarse);
  98. void delMapping(int ID, bool coarse, const char *addr);
  99. void replaceMapping(int, bool, const char *);
  100. std::map<std::string, std::string> getMidiMappingStrings(void);
  101. //unclear if this should be be here as a helper or not
  102. std::string getMappedString(std::string addr);
  103. MidiBijection getBijection(std::string s);
  104. void snoop(const char *msg);
  105. void apply_high(int v, int ID);
  106. void apply_low(int v, int ID);
  107. void apply_midi(int val, int ID);
  108. void setBounds(const char *str, float low, float high);
  109. std::tuple<float,float,float,float> getBounds(const char *str);
  110. bool has(std::string addr);
  111. bool hasPending(std::string addr);
  112. bool hasCoarse(std::string addr);
  113. bool hasFine(std::string addr);
  114. bool hasCoarsePending(std::string addr);
  115. bool hasFinePending(std::string addr);
  116. int getCoarse(std::string addr);
  117. int getFine(std::string addr);
  118. //(Location, Coarse, Fine, Bijection)
  119. std::map<std::string, std::tuple<int, int, int, MidiBijection>> inv_map;
  120. std::deque<std::pair<std::string,bool>> learnQueue;
  121. std::function<void(const char *)> rt_cb;
  122. MidiMapperStorage *storage;
  123. const Ports *base_ports;
  124. };
  125. class MidiMapperRT
  126. {
  127. public:
  128. MidiMapperRT(void);
  129. void setBackendCb(std::function<void(const char*)> cb);
  130. void setFrontendCb(std::function<void(const char*)> cb);
  131. void handleCC(int ID, int val);
  132. void addWatch(void);
  133. void remWatch(void);
  134. Port addWatchPort(void);
  135. Port removeWatchPort(void);
  136. Port bindPort(void);
  137. //Fixed upper bounded size set of integer IDs
  138. class PendingQueue
  139. {
  140. public:
  141. PendingQueue()
  142. :pos_r(0), pos_w(0), size(0)
  143. {
  144. for(int i=0; i<32; ++i)
  145. vals[i] = -1;
  146. }
  147. void insert(int x)
  148. {
  149. if(has(x) || size > 31)
  150. return;
  151. vals[pos_w] = x;
  152. size++;
  153. pos_w = (pos_w+1)%32;
  154. }
  155. void pop(void)
  156. {
  157. if(size == 0)
  158. return;
  159. size--;
  160. vals[pos_r] = -1;
  161. pos_r = (1+pos_r)%32;
  162. }
  163. bool has(int x)
  164. {
  165. for(int i=0; i<32; ++i)
  166. if(vals[i] == x)
  167. return true;
  168. return false;
  169. }
  170. int vals[32];
  171. int pos_r;
  172. int pos_w;
  173. int size;
  174. };
  175. /***************
  176. * Member Data *
  177. ***************/
  178. PendingQueue pending;
  179. MidiMapperStorage *storage;
  180. unsigned watchSize;
  181. std::function<void(const char*)> backend;
  182. std::function<void(const char*)> frontend;
  183. };
  184. struct MidiAddr
  185. {
  186. //The midi values that map to the specified action
  187. uint8_t ch, ctl;
  188. //The type of the event 'f', 'i', 'T', 'c'
  189. char type;
  190. //The path of the event
  191. char *path;
  192. //The conversion function for 'f' types
  193. const char *conversion;
  194. };
  195. /**
  196. * Table of midi mappings - Deprecated
  197. *
  198. */
  199. class MidiTable
  200. {
  201. public:
  202. const Ports &dispatch_root;
  203. short unhandled_ch;
  204. short unhandled_ctl;
  205. char *unhandled_path;
  206. void (*error_cb)(const char *, const char *);
  207. void (*event_cb)(const char *);
  208. void (*modify_cb)(const char *, const char *, const char *, int, int);
  209. MidiTable(const Ports &_dispatch_root);
  210. ~MidiTable();
  211. bool has(uint8_t ch, uint8_t ctl) const;
  212. MidiAddr *get(uint8_t ch, uint8_t ctl);
  213. const MidiAddr *get(uint8_t ch, uint8_t ctl) const;
  214. bool mash_port(MidiAddr &e, const Port &port);
  215. void addElm(uint8_t ch, uint8_t ctl, const char *path);
  216. void check_learn(void);
  217. void learn(const char *s);
  218. void clear_entry(const char *s);
  219. void process(uint8_t ch, uint8_t ctl, uint8_t val);
  220. Port learnPort(void);
  221. Port unlearnPort(void);
  222. Port registerPort(void);
  223. //TODO generalize to an addScalingFunction() system
  224. static float translate(uint8_t val, const char *meta);
  225. private:
  226. class MidiTable_Impl *impl;
  227. };
  228. };