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.

130 lines
2.8KB

  1. #include "InMgr.h"
  2. #include "MidiIn.h"
  3. #include "EngineMgr.h"
  4. #include "../Misc/Master.h"
  5. #include <iostream>
  6. using namespace std;
  7. ostream &operator<<(ostream &out, const MidiEvent &ev)
  8. {
  9. switch(ev.type) {
  10. case M_NOTE:
  11. out << "MidiNote: note(" << ev.num << ")\n"
  12. << " channel(" << ev.channel << ")\n"
  13. << " velocity(" << ev.value << ")";
  14. break;
  15. case M_CONTROLLER:
  16. out << "MidiCtl: controller(" << ev.num << ")\n"
  17. << " channel(" << ev.channel << ")\n"
  18. << " value(" << ev.value << ")";
  19. break;
  20. case M_PGMCHANGE:
  21. out << "PgmChange: program(" << ev.num << ")\n"
  22. << " channel(" << ev.channel << ")";
  23. break;
  24. }
  25. return out;
  26. }
  27. MidiEvent::MidiEvent()
  28. :channel(0), type(0), num(0), value(0)
  29. {}
  30. InMgr &InMgr::getInstance()
  31. {
  32. static InMgr instance;
  33. return instance;
  34. }
  35. InMgr::InMgr()
  36. :queue(100), master(Master::getInstance())
  37. {
  38. current = NULL;
  39. sem_init(&work, PTHREAD_PROCESS_PRIVATE, 0);
  40. }
  41. InMgr::~InMgr()
  42. {
  43. //lets stop the consumer thread
  44. sem_destroy(&work);
  45. }
  46. void InMgr::putEvent(MidiEvent ev)
  47. {
  48. if(queue.push(ev)) //check for error
  49. cerr << "ERROR: Midi Ringbuffer is FULL" << endl;
  50. else
  51. sem_post(&work);
  52. }
  53. void InMgr::flush()
  54. {
  55. MidiEvent ev;
  56. while(!sem_trywait(&work)) {
  57. queue.pop(ev);
  58. //cout << ev << endl;
  59. switch(ev.type) {
  60. case M_NOTE:
  61. dump.dumpnote(ev.channel, ev.num, ev.value);
  62. if(ev.value)
  63. master.noteOn(ev.channel, ev.num, ev.value);
  64. else
  65. master.noteOff(ev.channel, ev.num);
  66. break;
  67. case M_CONTROLLER:
  68. dump.dumpcontroller(ev.channel, ev.num, ev.value);
  69. master.setController(ev.channel, ev.num, ev.value);
  70. break;
  71. case M_PGMCHANGE:
  72. master.setProgram(ev.channel, ev.num);
  73. break;
  74. case M_PRESSURE:
  75. master.polyphonicAftertouch(ev.channel, ev.num, ev.value);
  76. break;
  77. }
  78. }
  79. }
  80. bool InMgr::setSource(string name)
  81. {
  82. MidiIn *src = getIn(name);
  83. if(!src)
  84. return false;
  85. if(current)
  86. current->setMidiEn(false);
  87. current = src;
  88. current->setMidiEn(true);
  89. bool success = current->getMidiEn();
  90. //Keep system in a valid state (aka with a running driver)
  91. if(!success)
  92. (current = getIn("NULL"))->setMidiEn(true);
  93. return success;
  94. }
  95. string InMgr::getSource() const
  96. {
  97. if(current)
  98. return current->name;
  99. else
  100. return "ERROR";
  101. }
  102. MidiIn *InMgr::getIn(string name)
  103. {
  104. EngineMgr &eng = EngineMgr::getInstance();
  105. return dynamic_cast<MidiIn *>(eng.getEng(name));
  106. }