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.

218 lines
4.2KB

  1. #include "rack.hpp"
  2. #include "midi.hpp"
  3. #include "rtmidi.hpp"
  4. #include "bridge.hpp"
  5. #include "gamepad.hpp"
  6. #include "keyboard.hpp"
  7. namespace rack {
  8. static std::vector<int> driverIds;
  9. static std::map<int, MidiDriver*> drivers;
  10. ////////////////////
  11. // MidiDevice
  12. ////////////////////
  13. void MidiInputDevice::subscribe(MidiInput *midiInput) {
  14. subscribed.insert(midiInput);
  15. }
  16. void MidiInputDevice::unsubscribe(MidiInput *midiInput) {
  17. // Remove MidiInput from subscriptions
  18. auto it = subscribed.find(midiInput);
  19. if (it != subscribed.end())
  20. subscribed.erase(it);
  21. }
  22. void MidiInputDevice::onMessage(MidiMessage message) {
  23. for (MidiInput *midiInput : subscribed) {
  24. midiInput->onMessage(message);
  25. }
  26. }
  27. ////////////////////
  28. // MidiDriver
  29. ////////////////////
  30. ////////////////////
  31. // MidiIO
  32. ////////////////////
  33. MidiIO::~MidiIO() {
  34. // Because of polymorphic destruction, descendants must call this in their own destructor
  35. // setDriverId(-1);
  36. }
  37. std::vector<int> MidiIO::getDriverIds() {
  38. return driverIds;
  39. }
  40. std::string MidiIO::getDriverName(int driverId) {
  41. auto it = drivers.find(driverId);
  42. if (it == drivers.end())
  43. return "";
  44. return it->second->getName();
  45. }
  46. void MidiIO::setDriverId(int driverId) {
  47. // Destroy driver
  48. setDeviceId(-1);
  49. if (driver) {
  50. driver = NULL;
  51. }
  52. this->driverId = -1;
  53. // Set driver
  54. auto it = drivers.find(driverId);
  55. if (it != drivers.end()) {
  56. driver = it->second;
  57. this->driverId = driverId;
  58. }
  59. }
  60. std::string MidiIO::getChannelName(int channel) {
  61. if (channel == -1)
  62. return "All channels";
  63. else
  64. return string::f("Channel %d", channel + 1);
  65. }
  66. json_t *MidiIO::toJson() {
  67. json_t *rootJ = json_object();
  68. json_object_set_new(rootJ, "driver", json_integer(driverId));
  69. std::string deviceName = getDeviceName(deviceId);
  70. if (!deviceName.empty())
  71. json_object_set_new(rootJ, "deviceName", json_string(deviceName.c_str()));
  72. json_object_set_new(rootJ, "channel", json_integer(channel));
  73. return rootJ;
  74. }
  75. void MidiIO::fromJson(json_t *rootJ) {
  76. json_t *driverJ = json_object_get(rootJ, "driver");
  77. if (driverJ)
  78. setDriverId(json_integer_value(driverJ));
  79. json_t *deviceNameJ = json_object_get(rootJ, "deviceName");
  80. if (deviceNameJ) {
  81. std::string deviceName = json_string_value(deviceNameJ);
  82. // Search for device with equal name
  83. for (int deviceId : getDeviceIds()) {
  84. if (getDeviceName(deviceId) == deviceName) {
  85. setDeviceId(deviceId);
  86. break;
  87. }
  88. }
  89. }
  90. json_t *channelJ = json_object_get(rootJ, "channel");
  91. if (channelJ)
  92. channel = json_integer_value(channelJ);
  93. }
  94. ////////////////////
  95. // MidiInput
  96. ////////////////////
  97. MidiInput::MidiInput() {
  98. if (driverIds.size() >= 1) {
  99. setDriverId(driverIds[0]);
  100. }
  101. }
  102. MidiInput::~MidiInput() {
  103. setDriverId(-1);
  104. }
  105. std::vector<int> MidiInput::getDeviceIds() {
  106. if (driver) {
  107. return driver->getInputDeviceIds();
  108. }
  109. return {};
  110. }
  111. std::string MidiInput::getDeviceName(int deviceId) {
  112. if (driver) {
  113. return driver->getInputDeviceName(deviceId);
  114. }
  115. return "";
  116. }
  117. void MidiInput::setDeviceId(int deviceId) {
  118. // Destroy device
  119. if (driver && this->deviceId >= 0) {
  120. driver->unsubscribeInputDevice(this->deviceId, this);
  121. }
  122. this->deviceId = -1;
  123. // Create device
  124. if (driver && deviceId >= 0) {
  125. driver->subscribeInputDevice(deviceId, this);
  126. this->deviceId = deviceId;
  127. }
  128. }
  129. void MidiInputQueue::onMessage(MidiMessage message) {
  130. // Filter channel
  131. if (channel >= 0) {
  132. if (message.status() != 0xf && message.channel() != channel)
  133. return;
  134. }
  135. // Push to queue
  136. if ((int) queue.size() < queueMaxSize)
  137. queue.push(message);
  138. }
  139. bool MidiInputQueue::shift(MidiMessage *message) {
  140. if (!message)
  141. return false;
  142. if (!queue.empty()) {
  143. *message = queue.front();
  144. queue.pop();
  145. return true;
  146. }
  147. return false;
  148. }
  149. ////////////////////
  150. // MidiOutput
  151. ////////////////////
  152. MidiOutput::MidiOutput() {
  153. }
  154. MidiOutput::~MidiOutput() {
  155. setDriverId(-1);
  156. }
  157. void MidiOutput::setDeviceId(int deviceId) {
  158. // TODO
  159. }
  160. ////////////////////
  161. // midi
  162. ////////////////////
  163. void midiDestroy() {
  164. driverIds.clear();
  165. for (auto &pair : drivers) {
  166. delete pair.second;
  167. }
  168. drivers.clear();
  169. }
  170. void midiDriverAdd(int driverId, MidiDriver *driver) {
  171. assert(driver);
  172. driverIds.push_back(driverId);
  173. drivers[driverId] = driver;
  174. }
  175. } // namespace rack