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.

292 lines
5.5KB

  1. #include <midi.hpp>
  2. #include <string.hpp>
  3. #include <map>
  4. namespace rack {
  5. namespace midi {
  6. /** Preserves the order of IDs */
  7. static std::vector<int> driverIds;
  8. static std::map<int, Driver*> drivers;
  9. ////////////////////
  10. // Device
  11. ////////////////////
  12. void InputDevice::subscribe(Input* input) {
  13. subscribed.insert(input);
  14. }
  15. void InputDevice::unsubscribe(Input* input) {
  16. // Remove Input from subscriptions
  17. auto it = subscribed.find(input);
  18. if (it != subscribed.end())
  19. subscribed.erase(it);
  20. }
  21. void InputDevice::onMessage(Message message) {
  22. for (Input* input : subscribed) {
  23. // Filter channel
  24. if (input->channel < 0 || message.getStatus() == 0xf || message.getChannel() == input->channel) {
  25. input->onMessage(message);
  26. }
  27. }
  28. }
  29. void OutputDevice::subscribe(Output* output) {
  30. subscribed.insert(output);
  31. }
  32. void OutputDevice::unsubscribe(Output* output) {
  33. // Remove Output from subscriptions
  34. auto it = subscribed.find(output);
  35. if (it != subscribed.end())
  36. subscribed.erase(it);
  37. }
  38. ////////////////////
  39. // Port
  40. ////////////////////
  41. std::vector<int> Port::getDriverIds() {
  42. return driverIds;
  43. }
  44. std::string Port::getDriverName(int driverId) {
  45. auto it = drivers.find(driverId);
  46. if (it == drivers.end())
  47. return "";
  48. return it->second->getName();
  49. }
  50. void Port::setDriverId(int driverId) {
  51. // Unset device and driver
  52. setDeviceId(-1);
  53. if (driver) {
  54. driver = NULL;
  55. }
  56. this->driverId = -1;
  57. // Set driver
  58. auto it = drivers.find(driverId);
  59. if (it != drivers.end()) {
  60. driver = it->second;
  61. this->driverId = driverId;
  62. }
  63. }
  64. std::string Port::getChannelName(int channel) {
  65. if (channel == -1)
  66. return "All channels";
  67. else
  68. return string::f("Channel %d", channel + 1);
  69. }
  70. void Port::setChannel(int channel) {
  71. this->channel = channel;
  72. }
  73. json_t* Port::toJson() {
  74. json_t* rootJ = json_object();
  75. json_object_set_new(rootJ, "driver", json_integer(driverId));
  76. std::string deviceName = getDeviceName(deviceId);
  77. if (!deviceName.empty())
  78. json_object_set_new(rootJ, "deviceName", json_string(deviceName.c_str()));
  79. json_object_set_new(rootJ, "channel", json_integer(channel));
  80. return rootJ;
  81. }
  82. void Port::fromJson(json_t* rootJ) {
  83. json_t* driverJ = json_object_get(rootJ, "driver");
  84. if (driverJ)
  85. setDriverId(json_integer_value(driverJ));
  86. json_t* deviceNameJ = json_object_get(rootJ, "deviceName");
  87. if (deviceNameJ) {
  88. std::string deviceName = json_string_value(deviceNameJ);
  89. // Search for device with equal name
  90. for (int deviceId : getDeviceIds()) {
  91. if (getDeviceName(deviceId) == deviceName) {
  92. setDeviceId(deviceId);
  93. break;
  94. }
  95. }
  96. }
  97. json_t* channelJ = json_object_get(rootJ, "channel");
  98. if (channelJ)
  99. channel = json_integer_value(channelJ);
  100. }
  101. ////////////////////
  102. // Input
  103. ////////////////////
  104. Input::Input() {
  105. reset();
  106. }
  107. Input::~Input() {
  108. setDriverId(-1);
  109. }
  110. void Input::reset() {
  111. channel = -1;
  112. // Set first driver as default
  113. if (driverIds.size() >= 1) {
  114. setDriverId(driverIds[0]);
  115. }
  116. }
  117. std::vector<int> Input::getDeviceIds() {
  118. if (driver) {
  119. return driver->getInputDeviceIds();
  120. }
  121. return {};
  122. }
  123. std::string Input::getDeviceName(int deviceId) {
  124. if (driver) {
  125. return driver->getInputDeviceName(deviceId);
  126. }
  127. return "";
  128. }
  129. void Input::setDeviceId(int deviceId) {
  130. // Destroy device
  131. if (driver && this->deviceId >= 0) {
  132. driver->unsubscribeInput(this->deviceId, this);
  133. inputDevice = NULL;
  134. }
  135. this->deviceId = -1;
  136. // Create device
  137. if (driver && deviceId >= 0) {
  138. inputDevice = driver->subscribeInput(deviceId, this);
  139. this->deviceId = deviceId;
  140. }
  141. }
  142. std::vector<int> Input::getChannels() {
  143. std::vector<int> channels;
  144. for (int c = -1; c < 16; c++) {
  145. channels.push_back(c);
  146. }
  147. return channels;
  148. }
  149. void InputQueue::onMessage(Message message) {
  150. // Push to queue
  151. if ((int) queue.size() < queueMaxSize)
  152. queue.push(message);
  153. }
  154. bool InputQueue::shift(Message* message) {
  155. if (!message)
  156. return false;
  157. if (!queue.empty()) {
  158. *message = queue.front();
  159. queue.pop();
  160. return true;
  161. }
  162. return false;
  163. }
  164. ////////////////////
  165. // Output
  166. ////////////////////
  167. Output::Output() {
  168. reset();
  169. }
  170. Output::~Output() {
  171. setDriverId(-1);
  172. }
  173. void Output::reset() {
  174. channel = 0;
  175. // Set first driver as default
  176. if (driverIds.size() >= 1) {
  177. setDriverId(driverIds[0]);
  178. }
  179. }
  180. std::vector<int> Output::getDeviceIds() {
  181. if (driver) {
  182. return driver->getOutputDeviceIds();
  183. }
  184. return {};
  185. }
  186. std::string Output::getDeviceName(int deviceId) {
  187. if (driver) {
  188. return driver->getOutputDeviceName(deviceId);
  189. }
  190. return "";
  191. }
  192. void Output::setDeviceId(int deviceId) {
  193. // Destroy device
  194. if (driver && this->deviceId >= 0) {
  195. driver->unsubscribeOutput(this->deviceId, this);
  196. outputDevice = NULL;
  197. }
  198. this->deviceId = -1;
  199. // Create device
  200. if (driver && deviceId >= 0) {
  201. outputDevice = driver->subscribeOutput(deviceId, this);
  202. this->deviceId = deviceId;
  203. }
  204. }
  205. std::vector<int> Output::getChannels() {
  206. std::vector<int> channels;
  207. for (int c = 0; c < 16; c++) {
  208. channels.push_back(c);
  209. }
  210. return channels;
  211. }
  212. void Output::sendMessage(Message message) {
  213. // Set channel
  214. if (message.getStatus() != 0xf) {
  215. message.setChannel(channel);
  216. }
  217. // DEBUG("sendMessage %02x %02x %02x", message.cmd, message.data1, message.data2);
  218. if (outputDevice) {
  219. outputDevice->sendMessage(message);
  220. }
  221. }
  222. ////////////////////
  223. // midi
  224. ////////////////////
  225. void init() {
  226. }
  227. void destroy() {
  228. driverIds.clear();
  229. for (auto& pair : drivers) {
  230. delete pair.second;
  231. }
  232. drivers.clear();
  233. }
  234. void addDriver(int driverId, Driver* driver) {
  235. assert(driver);
  236. driverIds.push_back(driverId);
  237. drivers[driverId] = driver;
  238. }
  239. } // namespace midi
  240. } // namespace rack