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.

281 lines
8.3KB

  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. #ifndef RTOSC_MIDITABLE_H
  25. #define RTOSC_MIDITABLE_H
  26. #include <rtosc/ports.h>
  27. #include <string.h>
  28. #include <algorithm>
  29. #include <map>
  30. #include <sstream>
  31. #include <deque>
  32. #include <utility>
  33. #include <cassert>
  34. namespace rtosc {
  35. /**
  36. * Module Overview
  37. *
  38. * Actions:
  39. * - Add a mapping {coarse/fine} [nRT]
  40. * - Delete a mapping {coarse/fine} [nRT]
  41. * - Transform mapping value based on passive observation [nRT]
  42. * - Find unused CC numbers [RT]
  43. * - Transform CC into event {coarse/fine} [RT]
  44. */
  45. class MidiMapperStorage
  46. {
  47. public:
  48. //Almost immutable short vector class
  49. template<class T>
  50. class TinyVector {
  51. int n;
  52. T *t;
  53. public:
  54. TinyVector(void):n(0),t(0){}
  55. TinyVector(int i):n(i),t(new T[i]){}
  56. T&operator[](int i) {assert(i>=0 && i<n);return t[i];}
  57. T operator[](int i) const {assert(i>=0 && i<n);return t[i];}
  58. TinyVector insert(const T &t_)
  59. {TinyVector next(n+1); for(int i=0;i<n; ++i) next.t[i]=t[i]; next.t[n] = t_;return std::move(next);}
  60. TinyVector one_larger(void)
  61. {TinyVector next(n+1); for(int i=0;i<n + 1; ++i) next.t[i]=0; return std::move(next);}
  62. TinyVector sized_clone(void)
  63. {TinyVector next(n); for(int i=0;i<n; ++i) next.t[i]=0; return std::move(next);}
  64. TinyVector clone(void)
  65. {TinyVector next(n); for(int i=0;i<n; ++i) next.t[i]=t[i]; return std::move(next);}
  66. int size(void) const{return n;}
  67. };
  68. typedef std::function<void(const char*)> write_cb;
  69. typedef std::function<void(int16_t,write_cb)> callback_t;
  70. //RT Read Only
  71. TinyVector<std::tuple<int, bool, int>> mapping;//CC->{coarse, val-cb offset}
  72. TinyVector<callback_t> callbacks;
  73. //RT RW
  74. TinyVector<int> values;
  75. bool handleCC(int ID, int val, write_cb write);
  76. //TODO try to change O(n^2) algorithm to O(n)
  77. void cloneValues(const MidiMapperStorage &storage);
  78. MidiMapperStorage *clone(void);
  79. };
  80. struct MidiBijection
  81. {
  82. int mode;//0:linear,1:log
  83. float min;
  84. float max;
  85. int operator()(float x) const;
  86. float operator()(int x) const;
  87. };
  88. #include <cassert>
  89. class MidiMappernRT
  90. {
  91. public:
  92. MidiMappernRT(void);
  93. void map(const char *addr, bool coarse = true);
  94. MidiMapperStorage *generateNewBijection(const Port &port, std::string);
  95. void addNewMapper(int ID, const Port &port, std::string addr);
  96. void addFineMapper(int ID, const Port &port, std::string addr);
  97. void useFreeID(int ID);
  98. void unMap(const char *addr, bool coarse);
  99. void delMapping(int ID, bool coarse, const char *addr);
  100. void replaceMapping(int, bool, const char *);
  101. std::map<std::string, std::string> getMidiMappingStrings(void);
  102. //unclear if this should be be here as a helper or not
  103. std::string getMappedString(std::string addr);
  104. MidiBijection getBijection(std::string s);
  105. void snoop(const char *msg);
  106. void apply_high(int v, int ID);
  107. void apply_low(int v, int ID);
  108. void apply_midi(int val, int ID);
  109. void setBounds(const char *str, float low, float high);
  110. std::tuple<float,float,float,float> getBounds(const char *str);
  111. bool has(std::string addr);
  112. bool hasPending(std::string addr);
  113. bool hasCoarse(std::string addr);
  114. bool hasFine(std::string addr);
  115. bool hasCoarsePending(std::string addr);
  116. bool hasFinePending(std::string addr);
  117. int getCoarse(std::string addr);
  118. int getFine(std::string addr);
  119. //(Location, Coarse, Fine, Bijection)
  120. std::map<std::string, std::tuple<int, int, int, MidiBijection>> inv_map;
  121. std::deque<std::pair<std::string,bool>> learnQueue;
  122. std::function<void(const char *)> rt_cb;
  123. MidiMapperStorage *storage;
  124. const Ports *base_ports;
  125. };
  126. class MidiMapperRT
  127. {
  128. public:
  129. MidiMapperRT(void);
  130. void setBackendCb(std::function<void(const char*)> cb);
  131. void setFrontendCb(std::function<void(const char*)> cb);
  132. void handleCC(int ID, int val);
  133. void addWatch(void);
  134. void remWatch(void);
  135. Port addWatchPort(void);
  136. Port removeWatchPort(void);
  137. Port bindPort(void);
  138. //Fixed upper bounded size set of integer IDs
  139. class PendingQueue
  140. {
  141. public:
  142. PendingQueue()
  143. :pos_r(0), pos_w(0), size(0)
  144. {
  145. for(int i=0; i<32; ++i)
  146. vals[i] = -1;
  147. }
  148. void insert(int x)
  149. {
  150. if(has(x) || size > 31)
  151. return;
  152. vals[pos_w] = x;
  153. size++;
  154. pos_w = (pos_w+1)%32;
  155. }
  156. void pop(void)
  157. {
  158. if(size == 0)
  159. return;
  160. size--;
  161. vals[pos_r] = -1;
  162. pos_r = (1+pos_r)%32;
  163. }
  164. bool has(int x)
  165. {
  166. for(int i=0; i<32; ++i)
  167. if(vals[i] == x)
  168. return true;
  169. return false;
  170. }
  171. int vals[32];
  172. int pos_r;
  173. int pos_w;
  174. int size;
  175. };
  176. /***************
  177. * Member Data *
  178. ***************/
  179. PendingQueue pending;
  180. MidiMapperStorage *storage;
  181. unsigned watchSize;
  182. std::function<void(const char*)> backend;
  183. std::function<void(const char*)> frontend;
  184. };
  185. struct MidiAddr
  186. {
  187. //The midi values that map to the specified action
  188. uint8_t ch, ctl;
  189. //The type of the event 'f', 'i', 'T', 'c'
  190. char type;
  191. //The path of the event
  192. char *path;
  193. //The conversion function for 'f' types
  194. const char *conversion;
  195. };
  196. /**
  197. * Table of midi mappings - Deprecated
  198. *
  199. */
  200. class MidiTable
  201. {
  202. public:
  203. const Ports &dispatch_root;
  204. short unhandled_ch;
  205. short unhandled_ctl;
  206. char *unhandled_path;
  207. void (*error_cb)(const char *, const char *);
  208. void (*event_cb)(const char *);
  209. void (*modify_cb)(const char *, const char *, const char *, int, int);
  210. MidiTable(const Ports &_dispatch_root);
  211. ~MidiTable();
  212. bool has(uint8_t ch, uint8_t ctl) const;
  213. MidiAddr *get(uint8_t ch, uint8_t ctl);
  214. const MidiAddr *get(uint8_t ch, uint8_t ctl) const;
  215. bool mash_port(MidiAddr &e, const Port &port);
  216. void addElm(uint8_t ch, uint8_t ctl, const char *path);
  217. void check_learn(void);
  218. void learn(const char *s);
  219. void clear_entry(const char *s);
  220. void process(uint8_t ch, uint8_t ctl, uint8_t val);
  221. Port learnPort(void);
  222. Port unlearnPort(void);
  223. Port registerPort(void);
  224. //TODO generalize to an addScalingFunction() system
  225. static float translate(uint8_t val, const char *meta);
  226. private:
  227. class MidiTable_Impl *impl;
  228. };
  229. };
  230. #endif