Allolib  1.0
C++ Components For Interactive Multimedia
al_PresetMIDI.hpp
1 #ifndef AL_PRESETMIDI_H
2 #define AL_PRESETMIDI_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 PresetHandler 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_PresetHandler.hpp"
47 
48 namespace al {
49 
60  public:
61  PresetMIDI() {}
62 
63  PresetMIDI(int deviceIndex) : mPresetHandler(nullptr) {
64  MIDIMessageHandler::bindTo(mRtMidiIn);
65  try {
66  mRtMidiIn.openPort(deviceIndex);
67  printf("PresetMIDI: Opened port to %s\n",
68  mRtMidiIn.getPortName(deviceIndex).c_str());
69  } catch (RtMidiError &error) {
70  std::cerr << error.getMessage() << std::endl;
71  std::cerr << "PresetMIDI Warning: Could not open MIDI port "
72  << deviceIndex << std::endl;
73  }
74  mMorphBinding.channel = mMorphBinding.controlNumber = -1;
75  }
76 
77  PresetMIDI(int deviceIndex, PresetHandler &presetHandler)
78  : PresetMIDI(deviceIndex) {
79  mPresetHandler = &presetHandler;
80  setPresetHandler(*mPresetHandler);
81  }
82 
83  void enable() { mEnabled = true; }
84  void disable() { mEnabled = false; }
85 
86  void init(int deviceIndex, PresetHandler &presetHandler) {
87  open(deviceIndex, presetHandler);
88  }
89 
90  void open(int deviceIndex, PresetHandler &presetHandler) {
91  open(deviceIndex);
92  setPresetHandler(presetHandler);
93  }
94 
95  void open(int deviceIndex) {
96  MIDIMessageHandler::bindTo(mRtMidiIn);
97 
98  if (mRtMidiIn.isPortOpen()) {
99  mRtMidiIn.closePort();
100  }
101  try {
102  if (deviceIndex >= 0 && deviceIndex < (int)mRtMidiIn.getPortCount()) {
103  mRtMidiIn.openPort(deviceIndex);
104  printf("PresetMIDI: Opened port to %s\n",
105  mRtMidiIn.getPortName(deviceIndex).c_str());
106  } else {
107  std::cerr << "PresetMIDI Warning: Could not open MIDI port "
108  << deviceIndex << std::endl;
109  }
110  } catch (RtMidiError &error) {
111  std::cerr << error.getMessage() << std::endl;
112  std::cerr << "PresetMIDI Warning: Could not open MIDI port "
113  << deviceIndex << std::endl;
114  }
115  }
116 
117  void close() {
118  mRtMidiIn.closePort();
119  MIDIMessageHandler::clearBindings();
120  }
121 
122  bool isOpen() { return mRtMidiIn.isPortOpen(); }
123 
124  void setPresetHandler(PresetHandler &presetHandler) {
125  mPresetHandler = &presetHandler;
126  }
127 
128  struct NoteBinding {
129  int noteNumber;
130  int channel;
131  int presetIndex;
132  };
133 
134  struct ProgramBinding {
135  int programNumber;
136  int channel;
137  int presetIndex;
138  };
139 
140  struct MorphBinding {
141  int controlNumber;
142  int channel;
143  float min;
144  float max;
145  };
146 
147  void connectNoteToPreset(int channel, float presetLow, int noteLow,
148  float presetHigh = -1, int noteHigh = -1);
149 
150  void connectProgramToPreset(int channel, float presetLow, int programLow,
151  float presetHigh = -1, int programHigh = -1);
152 
153  void setMorphControl(int controlNumber, int channel, float min, float max);
154 
155  virtual void onMIDIMessage(const MIDIMessage &m) override;
156 
157  std::vector<NoteBinding> getCurrentNoteBindings() { return mNoteBindings; }
158  std::vector<ProgramBinding> getCurrentProgramBindings() {
159  return mProgramBindings;
160  }
161 
162  private:
163  bool mEnabled{true};
164  MorphBinding mMorphBinding;
165 
166  PresetHandler *mPresetHandler;
167 
168  RtMidiIn mRtMidiIn;
169  std::vector<NoteBinding> mNoteBindings;
170  std::vector<ProgramBinding> mProgramBindings;
171 };
172 
173 } // namespace al
174 
175 #endif // AL_PRESETMIDI_H
void bindTo(RtMidiIn &RtMidiIn, unsigned port=0)
Bind handler to a MIDI input.
The PresetHandler class handles sorting and recalling of presets.
The PresetMIDI class connects PresetHandler objects to MIDI messages.
virtual void onMIDIMessage(const MIDIMessage &m) override
Called when a MIDI message is received.
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