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.

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