/* * Input.h * * Created on: 5 déc. 2020 * Author: fanch */ #ifndef SRC_INPUT_H_ #define SRC_INPUT_H_ #include "utils.h" #include "InputAction.h" #include "MidiMessage.h" #include "IInputEventListener.h" #include "MidiPort.h" enum InputType { BUTTON, CONTROLLER}; class PadDefinition; class Button; class Controller; class PadSelection; class AbsInput { public: static AbsInput* from_json(PadDefinition* p, Json::Value& v); AbsInput(PadDefinition* p=NULL, InputType t=BUTTON){ type=t; m_parent=p; m_selection=NULL; } AbsInput(const AbsInput& b) { operator=(b); } virtual const AbsInput& operator=(const AbsInput& b) { type=b.type; m_parent=b.m_parent; m_selection=b.m_selection; x=b.x; y=b.y; index=b.index; realkey=b.realkey; virtualkey=b.virtualkey; realchannel=b.realchannel; virtualchannel=b.virtualchannel; realvel=b.realvel; virtualvel=b.virtualvel; m_actions = b.m_actions; locked = b.locked; return *this; } virtual ~AbsInput(){} InputActionList* get_action(int i) { if(i>=0 && i<(int)m_actions.size()) return m_actions[i]; return NULL; } InputActionList* get_action(const std::string& str) { int size = m_actions.size(); for(int i=0; iget_name()) return m_actions[i]; } return NULL; } MidiMessageList* get_message_action(int i) { InputActionList* b = get_action(i); return b?b->get_messages():NULL; } MidiMessageList* get_message_action(const std::string& i) { InputActionList* b = get_action(i); return b?b->get_messages():NULL; } virtual void on_input(double ts, MidiMessage* m) { printf("(%d, %d) : [%d, %d]\n", realchannel, realkey, x, y); } void send(MidiMessage* m){ get_output_port().send(*m); } void control(const std::string& s, bool force=false){ if(!force && is_locked()) return; MidiMessageList* mm = get_message_action(s); if(!mm) return; int ss = mm->size(); for(int i=0; itype) { case MidiMessage::NOTE_OFF: case MidiMessage::NOTE_ON: case MidiMessage::CONTROLLER_CHANGE: { Note* note = dynamic_cast(m)->copy(); if(note->channel==255) note->channel=realchannel; if(note->key==255) note->key=realkey; if(note->velocity==255) note->velocity=realvel; MidiPortOut& port = get_control_port(); port.send(*note); delete note; } } } } void control(MidiMessageList* mm, bool force=false){ int ss = mm->size(); for(int i=0; itype) { case MidiMessage::NOTE_OFF: case MidiMessage::NOTE_ON: case MidiMessage::CONTROLLER_CHANGE: { Note* note = dynamic_cast(m)->copy(); if(note->channel==255) note->channel=realchannel; if(note->key==255) note->key=realkey; if(note->velocity==255) note->velocity=realvel; MidiPortOut& port = get_control_port(); port.send(*note); delete note; } } } } void control(int i, bool force=false){ if(!force && is_locked()) return; MidiMessageList* m = get_message_action(i); if(m) get_control_port().send(*m); } virtual MidiPortIn& get_input_port(); virtual MidiPortOut& get_control_port(); virtual MidiPortOut& get_output_port(); virtual void on_noteon(Button* b, double ts, MidiMessage* msg){} virtual void on_noteoff(Button* b, double ts, MidiMessage* msg){} bool is_locked() const {return locked!="";} InputType type; int x; int y; int index; int realkey; int virtualkey; int realchannel; int virtualchannel; int realvel; int virtualvel; std::string locked; PadSelection* get_selection() const {return m_selection;} void set_selection(PadSelection* p){m_selection=p;} protected: std::vector m_actions; std::vector m_listeners; PadDefinition* m_parent; PadSelection* m_selection; friend class PadDefinition; friend class InputDefinition; }; #endif /* SRC_INPUT_H_ */