OpenShot Audio Library | OpenShotAudio  0.3.1
juce_MidiDevices.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  The code included in this file is provided under the terms of the ISC license
11  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12  To use, copy, modify, and/or distribute this software for any purpose with or
13  without fee is hereby granted provided that the above copyright notice and
14  this permission notice appear in all copies.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 namespace juce
24 {
25 //==============================================================================
37 {
38  MidiDeviceInfo() = default;
39 
40  MidiDeviceInfo (const String& deviceName, const String& deviceIdentifier)
41  : name (deviceName), identifier (deviceIdentifier)
42  {
43  }
44 
55 
62 
63  //==============================================================================
64  bool operator== (const MidiDeviceInfo& other) const noexcept { return name == other.name && identifier == other.identifier; }
65  bool operator!= (const MidiDeviceInfo& other) const noexcept { return ! operator== (other); }
66 };
67 
68 class MidiInputCallback;
69 
70 //==============================================================================
81 class JUCE_API MidiInput final
82 {
83 public:
84  //==============================================================================
92 
95 
109  static std::unique_ptr<MidiInput> openDevice (const String& deviceIdentifier, MidiInputCallback* callback);
110 
111  #if JUCE_LINUX || JUCE_MAC || JUCE_IOS || DOXYGEN
125  static std::unique_ptr<MidiInput> createNewDevice (const String& deviceName, MidiInputCallback* callback);
126  #endif
127 
128  //==============================================================================
131 
139  void start();
140 
145  void stop();
146 
148  MidiDeviceInfo getDeviceInfo() const noexcept { return deviceInfo; }
149 
151  String getIdentifier() const noexcept { return deviceInfo.identifier; }
152 
154  String getName() const noexcept { return deviceInfo.name; }
155 
157  void setName (const String& newName) noexcept { deviceInfo.name = newName; }
158 
159  //==============================================================================
163  static int getDefaultDeviceIndex();
165  static std::unique_ptr<MidiInput> openDevice (int, MidiInputCallback*);
166 
167 private:
168  //==============================================================================
169  explicit MidiInput (const String&, const String&);
170 
171  MidiDeviceInfo deviceInfo;
172  void* internal = nullptr;
173 
174  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiInput)
175 };
176 
177 //==============================================================================
188 class JUCE_API MidiInputCallback
189 {
190 public:
192  virtual ~MidiInputCallback() = default;
193 
206  virtual void handleIncomingMidiMessage (MidiInput* source,
207  const MidiMessage& message) = 0;
208 
219  virtual void handlePartialSysexMessage (MidiInput* source,
220  const uint8* messageData,
221  int numBytesSoFar,
222  double timestamp)
223  {
224  ignoreUnused (source, messageData, numBytesSoFar, timestamp);
225  }
226 };
227 
228 //==============================================================================
239 class JUCE_API MidiOutput final : private Thread
240 {
241 public:
242  //==============================================================================
250 
253 
265  static std::unique_ptr<MidiOutput> openDevice (const String& deviceIdentifier);
266 
267  #if JUCE_LINUX || JUCE_MAC || JUCE_IOS || DOXYGEN
280  static std::unique_ptr<MidiOutput> createNewDevice (const String& deviceName);
281  #endif
282 
283  //==============================================================================
285  ~MidiOutput() override;
286 
288  MidiDeviceInfo getDeviceInfo() const noexcept { return deviceInfo; }
289 
291  String getIdentifier() const noexcept { return deviceInfo.identifier; }
292 
294  String getName() const noexcept { return deviceInfo.name; }
295 
297  void setName (const String& newName) noexcept { deviceInfo.name = newName; }
298 
299  //==============================================================================
301  void sendMessageNow (const MidiMessage& message);
302 
304  void sendBlockOfMessagesNow (const MidiBuffer& buffer);
305 
323  void sendBlockOfMessages (const MidiBuffer& buffer,
324  double millisecondCounterToStartAt,
325  double samplesPerSecondForBuffer);
326 
328  void clearAllPendingMessages();
329 
333  void startBackgroundThread();
334 
338  void stopBackgroundThread();
339 
340  //==============================================================================
344  static int getDefaultDeviceIndex();
346  static std::unique_ptr<MidiOutput> openDevice (int);
347 
348 private:
349  //==============================================================================
350  struct PendingMessage
351  {
352  PendingMessage (const void* data, int len, double timeStamp)
353  : message (data, len, timeStamp)
354  {
355  }
356 
357  MidiMessage message;
358  PendingMessage* next;
359  };
360 
361  //==============================================================================
362  explicit MidiOutput (const String&, const String&);
363  void run() override;
364 
365  MidiDeviceInfo deviceInfo;
366  void* internal = nullptr;
367  CriticalSection lock;
368  PendingMessage* firstMessage = nullptr;
369 
370  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiOutput)
371 };
372 
373 } // namespace juce
virtual ~MidiInputCallback()=default
virtual void handlePartialSysexMessage(MidiInput *source, const uint8 *messageData, int numBytesSoFar, double timestamp)
virtual void handleIncomingMidiMessage(MidiInput *source, const MidiMessage &message)=0
void setName(const String &newName) noexcept
static StringArray getDevices()
static std::unique_ptr< MidiInput > openDevice(int, MidiInputCallback *)
static std::unique_ptr< MidiInput > openDevice(const String &deviceIdentifier, MidiInputCallback *callback)
String getName() const noexcept
MidiDeviceInfo getDeviceInfo() const noexcept
static MidiDeviceInfo getDefaultDevice()
static int getDefaultDeviceIndex()
static Array< MidiDeviceInfo > getAvailableDevices()
String getIdentifier() const noexcept
static MidiDeviceInfo getDefaultDevice()
static Array< MidiDeviceInfo > getAvailableDevices()
String getName() const noexcept
static std::unique_ptr< MidiOutput > openDevice(int)
static int getDefaultDeviceIndex()
static std::unique_ptr< MidiOutput > openDevice(const String &deviceIdentifier)
void setName(const String &newName) noexcept
String getIdentifier() const noexcept
MidiDeviceInfo getDeviceInfo() const noexcept
void sendMessageNow(const MidiMessage &message)
static StringArray getDevices()
~MidiOutput() override