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.

InMgr.cpp 3.2KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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), time(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. work.init(PTHREAD_PROCESS_PRIVATE, 0);
  40. }
  41. InMgr::~InMgr()
  42. {
  43. //lets stop the consumer thread
  44. }
  45. void InMgr::putEvent(MidiEvent ev)
  46. {
  47. if(queue.push(ev)) //check for error
  48. cerr << "ERROR: Midi Ringbuffer is FULL" << endl;
  49. else
  50. work.post();
  51. }
  52. void InMgr::flush(unsigned frameStart, unsigned frameStop)
  53. {
  54. MidiEvent ev;
  55. while(!work.trywait()) {
  56. queue.peak(ev);
  57. if(ev.time < (int)frameStart || ev.time > (int)frameStop) {
  58. //Back out of transaction
  59. work.post();
  60. //printf("%d vs [%d..%d]\n",ev.time, frameStart, frameStop);
  61. break;
  62. }
  63. queue.pop(ev);
  64. //cout << ev << endl;
  65. switch(ev.type) {
  66. case M_NOTE:
  67. dump.dumpnote(ev.channel, ev.num, ev.value);
  68. if(ev.value)
  69. master.noteOn(ev.channel, ev.num, ev.value);
  70. else
  71. master.noteOff(ev.channel, ev.num);
  72. break;
  73. case M_CONTROLLER:
  74. dump.dumpcontroller(ev.channel, ev.num, ev.value);
  75. master.setController(ev.channel, ev.num, ev.value);
  76. break;
  77. case M_PGMCHANGE:
  78. master.setProgram(ev.channel, ev.num);
  79. break;
  80. case M_PRESSURE:
  81. master.polyphonicAftertouch(ev.channel, ev.num, ev.value);
  82. break;
  83. }
  84. }
  85. }
  86. bool InMgr::empty(void) const
  87. {
  88. int semvalue = work.getvalue();
  89. return semvalue <= 0;
  90. }
  91. bool InMgr::setSource(string name)
  92. {
  93. MidiIn *src = getIn(name);
  94. if(!src)
  95. return false;
  96. if(current)
  97. current->setMidiEn(false);
  98. current = src;
  99. current->setMidiEn(true);
  100. bool success = current->getMidiEn();
  101. //Keep system in a valid state (aka with a running driver)
  102. if(!success)
  103. (current = getIn("NULL"))->setMidiEn(true);
  104. return success;
  105. }
  106. string InMgr::getSource() const
  107. {
  108. if(current)
  109. return current->name;
  110. else
  111. return "ERROR";
  112. }
  113. MidiIn *InMgr::getIn(string name)
  114. {
  115. EngineMgr &eng = EngineMgr::getInstance();
  116. return dynamic_cast<MidiIn *>(eng.getEng(name));
  117. }