Allolib  1.0
C++ Components For Interactive Multimedia
al_ParameterMIDI.hpp
1 #ifndef AL_PARAMETERMIDI_H
2 #define AL_PARAMETERMIDI_H
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology,
8  UCSB. Copyright (C) 2012-2015. The Regents of the University of California.
9  All rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are
13  met:
14 
15  Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17 
18  Redistributions in binary form must reproduce the above
19  copyright notice, this list of conditions and the following disclaimer in the
20  documentation and/or other materials provided with the
21  distribution.
22 
23  Neither the name of the University of California nor the names
24  of its contributors may be used to endorse or promote products derived from
25  this software without specific prior written permission.
26 
27  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
31  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
34  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 
39  File description:
40  Class to connect MIDI Input to Parameter objects
41  File author(s):
42  AndrĂ©s Cabrera mantaraya36@gmail.com
43 */
44 
45 #include "al/io/al_MIDI.hpp"
46 #include "al/ui/al_Parameter.hpp"
47 
48 #undef min
49 #undef max
50 namespace al {
51 
71  public:
72  ParameterMIDI() {}
73 
74  ParameterMIDI(unsigned int deviceIndex, bool verbose = false);
75 
76  void open(unsigned int deviceIndex = 0);
77 
78  void open(int deviceIndex, bool verbose);
79 
80  [[deprecated("Use open()")]] void init(int deviceIndex = 0,
81  bool verbose = false);
82 
83  void close();
84 
85  void connectControl(Parameter &param, int controlNumber, int channel) {
86  connectControl(param, controlNumber, channel, param.min(), param.max());
87  }
88 
89  void connectControl(Parameter &param, int controlNumber, int channel,
90  float min, float max);
91 
99  void connectControls(ParameterMeta &param, std::vector<int> controlNumbers,
100  int channel = 1, std::vector<float> min = {},
101  std::vector<float> max = {});
102 
112  void connectNoteToValue(Parameter &param, int channel, float min, int low,
113  float max = -1, int high = -1);
114 
115  void connectNoteToToggle(ParameterBool &param, int channel, int note);
116 
117  void connectNoteToIncrement(Parameter &param, int channel, int note,
118  float increment);
119 
120  bool isOpen() { return mRtMidiIn.isPortOpen(); }
121 
122  virtual void onMIDIMessage(const MIDIMessage &m) override;
123 
124  void verbose(bool v) { mVerbose = v; }
125 
126  struct ControlBinding {
127  int controlNumber;
128  int channel;
129  Parameter *param;
130  float min, max;
131  };
132 
134  std::vector<int> controlNumbers;
135  int channel;
136  ParameterMeta *param;
137  std::vector<float> min;
138  std::vector<float> max;
139  };
140 
141  struct NoteBinding {
142  int noteNumber;
143  int channel;
144  float value;
145  Parameter *param;
146  };
147 
148  struct ToggleBinding {
149  int noteNumber;
150  int channel;
151  bool toggle;
152  ParameterBool *param;
153  };
154 
156  int noteNumber;
157  int channel;
158  float increment;
159  Parameter *param;
160  };
161 
162  std::vector<ControlBinding> getCurrentControlBindings() {
163  return mControlBindings;
164  }
165  std::vector<NoteBinding> getCurrentNoteBindings() { return mNoteBindings; }
166 
167  private:
168  RtMidiIn mRtMidiIn;
169  bool mVerbose;
170  std::vector<ControlBinding> mControlBindings;
171  std::vector<NoteBinding> mNoteBindings;
172  std::vector<ToggleBinding> mToggleBindings;
173  std::vector<IncrementBinding> mIncrementBindings;
174  std::vector<AbstractBinding> mAbstractBindings;
175 };
176 
177 } // namespace al
178 
179 #endif // AL_PARAMETERMIDI_H
The Parameter class.
The ParameterMIDI class connects Parameter objects to MIDI messages.
void connectNoteToValue(Parameter &param, int channel, float min, int low, float max=-1, int high=-1)
connectNoteToValue
void connectControls(ParameterMeta &param, std::vector< int > controlNumbers, int channel=1, std::vector< float > min={}, std::vector< float > max={})
virtual void onMIDIMessage(const MIDIMessage &m) override
Called when a MIDI message is received.
The ParameterMeta class defines the base interface for Parameter metadata.
void min(ParameterType minValue, ValueSource *src=nullptr)
set the minimum value for the parameter
void max(ParameterType maxValue, ValueSource *src=nullptr)
set the maximum value for the parameter
T min(const T &v1, const T &v2, const T &v3)
T max(const T &v1, const T &v2, const T &v3)
Definition: al_App.hpp:23