ofxPDSP
MidiOut.h
1 
2 // Output.h
3 // ofxPDSP
4 // Nicola Pisanti, MIT License, 2016 - 2018
5 
6 #ifndef OFXPDSPMIDI_PDSPMIDIOUT_H_INCLUDED
7 #define OFXPDSPMIDI_PDSPMIDIOUT_H_INCLUDED
8 
9 #ifndef __ANDROID__
10 
11 #include "ofxMidi.h"
12 #include <chrono>
13 #include "helper/PositionedMidiMessage.h"
14 #include <algorithm>
15 #include <thread>
16 #include <mutex>
17 #include <condition_variable>
18 #include <atomic>
19 #include "../DSP/pdspCore.h"
20 #include "../sequencer/SequencerSection.h"
21 
26 namespace pdsp { namespace midi {
27 
28 class Output : public pdsp::ExtSequencer, public pdsp::Preparable {
29 
30 private:
31 
32  class ScheduledMidiMessage{
33  public:
34  ScheduledMidiMessage();
35  ScheduledMidiMessage(ofxMidiMessage message, std::chrono::high_resolution_clock::time_point schedule);
36  ScheduledMidiMessage(const ScheduledMidiMessage &other);
37  ScheduledMidiMessage& operator= (const ScheduledMidiMessage &other);
38  ~ScheduledMidiMessage();
39 
40  ofxMidiMessage midi;
41  std::chrono::high_resolution_clock::time_point scheduledTime;
42  };
43 
44 
45  static bool scheduledMidiSort(const ScheduledMidiMessage &lhs, const ScheduledMidiMessage &rhs );
46 
47 
48 public:
49  Output();
50  ~Output();
51 
56  void openPort(int index);
57 
61  void close() override;
62 
66  void listPorts();
67 
72  void linkToMidiOut(ofxMidiOut &midiOut);
73 
77  bool isConnected() { return connected; }
78 
83  void setVerbose( bool verbose );
84 
90  pdsp::ExtSequencer& gate(int midiChannel, int defaultNote = 60);
91 
97  pdsp::ExtSequencer& note();
98 
104  pdsp::ExtSequencer& cc(int midiChannel, int ccNumber);
105 
109  void process( int bufferSize ) noexcept override;
110 
111  void linkToMessageBuffer(pdsp::MessageBuffer &messageBuffer) override;
112  void unlinkMessageBuffer(pdsp::MessageBuffer &messageBuffer) override;
116 protected:
117  void prepareToPlay( int expectedBufferSize, double sampleRate ) override;
118  void releaseResources() override ;
119 
120 private:
121 
122  ofxMidiOut* midiOut_p;
123  ofxMidiOut midiOut;
124  bool connected;
125  bool verbose;
126 
127  std::vector<ScheduledMidiMessage> messagesToSend;
128 
129  std::vector<pdsp::MessageBuffer*> inputs [3];
130  std::vector<int> defaultNote;
131  std::vector<int> midiChannelsNote;
132  std::vector<int> ccNumbers;
133  std::vector<int> midiChannelsCC;
134 
135  int selectedType;
136  int selectedMidiChannel;
137  int selectedDefaultNote;
138  int selectedCC;
139 
140  static const int nullType = -1;
141  static const int gateType = 0;
142  static const int noteType = 1;
143  static const int ccType = 2;
144 
145  //MIDI DAEMON MEMBERS---------------------------------------------------------------
146  void startMidiDaemon();
147  void closeMidiDaemon();
148  void midiDaemonFunction() noexcept;
149  static void midiDaemonFunctionWrapper(Output* parent);
150 
151  std::thread midiDaemonThread;
152  std::atomic<bool> runMidiDaemon;
153 
154  //midi output processing members
155  bool chronoStarted;
156  std::chrono::time_point<std::chrono::high_resolution_clock> bufferChrono;
157  double usecPerSample;
158  std::vector<ScheduledMidiMessage> circularBuffer;
159 
160  std::atomic<int> writeindex;
161  int send;
162 
163 
164  //TESTING
165  int messageCount;
166 };
167 
168 }} // end namespaces
169 
170 #endif
171 
172 #endif //OFXPDSPMIDI_PDSPMIDIOUT_H_INCLUDED
void openPort(int index)
open the port with the given index
Definition: MidiOut.cpp:94
void close() override
close the opened port
Definition: MidiOut.cpp:112
bool isConnected()
return true if the port has been sucessfully opened
Definition: MidiOut.h:77
pdsp::ExtSequencer & note()
patch a pdsp::ScoreSection::out_message() method to the result of this method for controlling the mid...
Definition: MidiOut.cpp:132
void listPorts()
list the available ports
Definition: MidiOut.cpp:90
Thread-safe value control with smoothing.
Definition: SequencerBridge.h:11
void linkToMidiOut(ofxMidiOut &midiOut)
uses an already open ofxMidiOut instead of opening a port
Definition: MidiOut.cpp:72
pdsp::ExtSequencer & gate(int midiChannel, int defaultNote=60)
patch a pdsp::ScoreSection::out_message() method to the result of this method for sending midi note m...
Definition: MidiOut.cpp:125
void setVerbose(bool verbose)
enable or disable diagnostic messages
Definition: MidiOut.cpp:68
pdsp::ExtSequencer & cc(int midiChannel, int ccNumber)
patch a pdsp::ScoreSection::out_message() method to the result of this method for sending midi cc mes...
Definition: MidiOut.cpp:137
Definition: MidiOut.h:28
Definition: Preparable.h:36