123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- /*
- * 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; i<size; i++)
- {
- if(str==m_actions[i]->get_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; i<ss; i++)
- {
- MidiMessage* m = (*mm)[i];
- switch(m->type)
- {
- case MidiMessage::NOTE_OFF:
- case MidiMessage::NOTE_ON:
- case MidiMessage::CONTROLLER_CHANGE:
- {
- Note* note = dynamic_cast<Note*>(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; i<ss; i++)
- {
- MidiMessage* m = (*mm)[i];
- switch(m->type)
- {
- case MidiMessage::NOTE_OFF:
- case MidiMessage::NOTE_ON:
- case MidiMessage::CONTROLLER_CHANGE:
- {
- Note* note = dynamic_cast<Note*>(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<InputActionList*> m_actions;
- std::vector<IInputEventListener*> m_listeners;
- PadDefinition* m_parent;
- PadSelection* m_selection;
- friend class PadDefinition;
- friend class InputDefinition;
- };
- #endif /* SRC_INPUT_H_ */
|