AbsInput.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Input.h
  3. *
  4. * Created on: 5 déc. 2020
  5. * Author: fanch
  6. */
  7. #ifndef SRC_INPUT_H_
  8. #define SRC_INPUT_H_
  9. #include "utils.h"
  10. #include "InputAction.h"
  11. #include "MidiMessage.h"
  12. #include "IInputEventListener.h"
  13. #include "MidiPort.h"
  14. enum InputType { BUTTON, CONTROLLER};
  15. class PadDefinition;
  16. class Button;
  17. class Controller;
  18. class PadSelection;
  19. class AbsInput
  20. {
  21. public:
  22. static AbsInput* from_json(PadDefinition* p, Json::Value& v);
  23. AbsInput(PadDefinition* p=NULL, InputType t=BUTTON){
  24. type=t;
  25. m_parent=p;
  26. m_selection=NULL;
  27. }
  28. AbsInput(const AbsInput& b)
  29. {
  30. operator=(b);
  31. }
  32. virtual const AbsInput& operator=(const AbsInput& b)
  33. {
  34. type=b.type;
  35. m_parent=b.m_parent;
  36. m_selection=b.m_selection;
  37. x=b.x;
  38. y=b.y;
  39. index=b.index;
  40. realkey=b.realkey;
  41. virtualkey=b.virtualkey;
  42. realchannel=b.realchannel;
  43. virtualchannel=b.virtualchannel;
  44. realvel=b.realvel;
  45. virtualvel=b.virtualvel;
  46. m_actions = b.m_actions;
  47. locked = b.locked;
  48. return *this;
  49. }
  50. virtual ~AbsInput(){}
  51. InputActionList* get_action(int i) {
  52. if(i>=0 && i<(int)m_actions.size()) return m_actions[i];
  53. return NULL;
  54. }
  55. InputActionList* get_action(const std::string& str) {
  56. int size = m_actions.size();
  57. for(int i=0; i<size; i++)
  58. {
  59. if(str==m_actions[i]->get_name())
  60. return m_actions[i];
  61. }
  62. return NULL;
  63. }
  64. MidiMessageList* get_message_action(int i) {
  65. InputActionList* b = get_action(i);
  66. return b?b->get_messages():NULL;
  67. }
  68. MidiMessageList* get_message_action(const std::string& i) {
  69. InputActionList* b = get_action(i);
  70. return b?b->get_messages():NULL;
  71. }
  72. virtual void on_input(double ts, MidiMessage* m)
  73. {
  74. printf("(%d, %d) : [%d, %d]\n", realchannel, realkey, x, y);
  75. }
  76. void send(MidiMessage* m){
  77. get_output_port().send(*m);
  78. }
  79. void control(const std::string& s, bool force=false){
  80. if(!force && is_locked()) return;
  81. MidiMessageList* mm = get_message_action(s);
  82. if(!mm) return;
  83. int ss = mm->size();
  84. for(int i=0; i<ss; i++)
  85. {
  86. MidiMessage* m = (*mm)[i];
  87. switch(m->type)
  88. {
  89. case MidiMessage::NOTE_OFF:
  90. case MidiMessage::NOTE_ON:
  91. case MidiMessage::CONTROLLER_CHANGE:
  92. {
  93. Note* note = dynamic_cast<Note*>(m)->copy();
  94. if(note->channel==255) note->channel=realchannel;
  95. if(note->key==255) note->key=realkey;
  96. if(note->velocity==255) note->velocity=realvel;
  97. MidiPortOut& port = get_control_port();
  98. port.send(*note);
  99. delete note;
  100. }
  101. }
  102. }
  103. }
  104. void control(MidiMessageList* mm, bool force=false){
  105. int ss = mm->size();
  106. for(int i=0; i<ss; i++)
  107. {
  108. MidiMessage* m = (*mm)[i];
  109. switch(m->type)
  110. {
  111. case MidiMessage::NOTE_OFF:
  112. case MidiMessage::NOTE_ON:
  113. case MidiMessage::CONTROLLER_CHANGE:
  114. {
  115. Note* note = dynamic_cast<Note*>(m)->copy();
  116. if(note->channel==255) note->channel=realchannel;
  117. if(note->key==255) note->key=realkey;
  118. if(note->velocity==255) note->velocity=realvel;
  119. MidiPortOut& port = get_control_port();
  120. port.send(*note);
  121. delete note;
  122. }
  123. }
  124. }
  125. }
  126. void control(int i, bool force=false){
  127. if(!force && is_locked()) return;
  128. MidiMessageList* m = get_message_action(i);
  129. if(m) get_control_port().send(*m);
  130. }
  131. virtual MidiPortIn& get_input_port();
  132. virtual MidiPortOut& get_control_port();
  133. virtual MidiPortOut& get_output_port();
  134. virtual void on_noteon(Button* b, double ts, MidiMessage* msg){}
  135. virtual void on_noteoff(Button* b, double ts, MidiMessage* msg){}
  136. bool is_locked() const {return locked!="";}
  137. InputType type;
  138. int x;
  139. int y;
  140. int index;
  141. int realkey;
  142. int virtualkey;
  143. int realchannel;
  144. int virtualchannel;
  145. int realvel;
  146. int virtualvel;
  147. std::string locked;
  148. PadSelection* get_selection() const {return m_selection;}
  149. void set_selection(PadSelection* p){m_selection=p;}
  150. protected:
  151. std::vector<InputActionList*> m_actions;
  152. std::vector<IInputEventListener*> m_listeners;
  153. PadDefinition* m_parent;
  154. PadSelection* m_selection;
  155. friend class PadDefinition;
  156. friend class InputDefinition;
  157. };
  158. #endif /* SRC_INPUT_H_ */