iPlug2 - C++ Audio Plug-in Framework
IPlugAPP.cpp
1 /*
2  ==============================================================================
3 
4  This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5 
6  See LICENSE.txt for more info.
7 
8  ==============================================================================
9 */
10 
11 #include "IPlugAPP.h"
12 #include "IPlugAPP_host.h"
13 
14 #if defined OS_MAC || defined OS_LINUX
15 #include <IPlugSWELL.h>
16 #endif
17 
18 #if defined OS_MAC
19 int GetTitleBarOffset()
20 {
21  int offset = GetSystemMetrics(SM_CYMENU);
22 
23  if(SWELL_GetOSXVersion() >= 0x1100)
24  offset += 4;
25 
26  return offset;
27 }
28 #endif
29 
30 using namespace iplug;
31 
32 extern HWND gHWND;
33 
34 IPlugAPP::IPlugAPP(const InstanceInfo& info, const Config& config)
35 : IPlugAPIBase(config, kAPIAPP)
36 , IPlugProcessor(config, kAPIAPP)
37 {
38  mAppHost = (IPlugAPPHost*) info.pAppHost;
39 
40  Trace(TRACELOC, "%s%s", config.pluginName, config.channelIOStr);
41 
42  SetChannelConnections(ERoute::kInput, 0, MaxNChannels(ERoute::kInput), true);
43  SetChannelConnections(ERoute::kOutput, 0, MaxNChannels(ERoute::kOutput), true);
44 
45  SetBlockSize(DEFAULT_BLOCK_SIZE);
46 
47  CreateTimer();
48 }
49 
50 bool IPlugAPP::EditorResize(int viewWidth, int viewHeight)
51 {
52  bool parentResized = false;
53 
54  if (viewWidth != GetEditorWidth() || viewHeight != GetEditorHeight())
55  {
56  #ifdef OS_MAC
57  const int titleBarOffset = GetTitleBarOffset();
58  RECT r;
59  GetWindowRect(gHWND, &r);
60  SetWindowPos(gHWND, 0, r.left, r.bottom - viewHeight - titleBarOffset, viewWidth, viewHeight + titleBarOffset, 0);
61  parentResized = true;
62  #endif
63  SetEditorSize(viewWidth, viewHeight);
64  }
65 
66  return parentResized;
67 }
68 
70 {
71  if (DoesMIDIOut() && mAppHost->mMidiOut)
72  {
73  //TODO: midi out channel
74 // uint8_t status;
75 //
76 // // if the midi channel out filter is set, reassign the status byte appropriately
77 // if(mAppHost->mMidiOutChannel > -1)
78 // status = mAppHost->mMidiOutChannel-1 | ((uint8_t) msg.StatusMsg() << 4) ;
79 
80  std::vector<uint8_t> message;
81  message.push_back(msg.mStatus);
82  message.push_back(msg.mData1);
83  message.push_back(msg.mData2);
84 
85  mAppHost->mMidiOut->sendMessage(&message);
86 
87  return true;
88  }
89 
90  return false;
91 }
92 
93 bool IPlugAPP::SendSysEx(const ISysEx& msg)
94 {
95  if (DoesMIDIOut() && mAppHost->mMidiOut)
96  {
97  //TODO: midi out channel
98  std::vector<uint8_t> message;
99 
100  for (int i = 0; i < msg.mSize; i++)
101  {
102  message.push_back(msg.mData[i]);
103  }
104 
105  mAppHost->mMidiOut->sendMessage(&message);
106  return true;
107  }
108 
109  return false;
110 }
111 
112 void IPlugAPP::SendSysexMsgFromUI(const ISysEx& msg)
113 {
114  SendSysEx(msg);
115 }
116 
117 void IPlugAPP::AppProcess(double** inputs, double** outputs, int nFrames)
118 {
119  SetChannelConnections(ERoute::kInput, 0, MaxNChannels(ERoute::kInput), !IsInstrument()); //TODO: go elsewhere - enable inputs
120  SetChannelConnections(ERoute::kOutput, 0, MaxNChannels(ERoute::kOutput), true); //TODO: go elsewhere
121  AttachBuffers(ERoute::kInput, 0, NChannelsConnected(ERoute::kInput), inputs, GetBlockSize());
122  AttachBuffers(ERoute::kOutput, 0, NChannelsConnected(ERoute::kOutput), outputs, GetBlockSize());
123 
124  if(mMidiMsgsFromCallback.ElementsAvailable())
125  {
126  IMidiMsg msg;
127 
128  while (mMidiMsgsFromCallback.Pop(msg))
129  {
130  ProcessMidiMsg(msg);
131  mMidiMsgsFromProcessor.Push(msg); // queue incoming MIDI for UI
132  }
133  }
134 
135  if(mSysExMsgsFromCallback.ElementsAvailable())
136  {
137  SysExData data;
138 
139  while (mSysExMsgsFromCallback.Pop(data))
140  {
141  ISysEx msg { data.mOffset, data.mData, data.mSize };
142  ProcessSysEx(msg);
143  mSysExDataFromProcessor.Push(data); // queue incoming Sysex for UI
144  }
145  }
146 
147  if(mMidiMsgsFromEditor.ElementsAvailable())
148  {
149  IMidiMsg msg;
150 
151  while (mMidiMsgsFromEditor.Pop(msg))
152  {
153  ProcessMidiMsg(msg);
154  }
155  }
156 
157  //Do not handle Sysex messages here - SendSysexMsgFromUI overridden
158 
159  ENTER_PARAMS_MUTEX
160  ProcessBuffers(0.0, GetBlockSize());
161  LEAVE_PARAMS_MUTEX
162 }
int MaxNChannels(ERoute direction) const
The base class of an IPlug plug-in, which interacts with the different plug-in APIs.
Definition: IPlugAPIBase.h:42
Standalone application base class for an IPlug plug-in.
int GetBlockSize() const
bool SendSysEx(const ISysEx &msg) override
Send a single MIDI System Exclusive (SysEx) message // TODO: info about what thread should this be ca...
Definition: IPlugAPP.cpp:93
Encapsulates a MIDI message and provides helper functions.
Definition: IPlugMidi.h:30
bool Push(const T &item)
Definition: IPlugQueue.h:55
bool EditorResize(int viewWidth, int viewHeight) override
Implementations call into the APIs resize hooks returns a bool to indicate whether the DAW or plugin ...
Definition: IPlugAPP.cpp:50
bool DoesMIDIOut() const
bool IsInstrument() const
size_t ElementsAvailable() const
Definition: IPlugQueue.h:86
bool SendSysEx(const ISysEx &msg) override
Send a single MIDI System Exclusive (SysEx) message // TODO: info about what thread should this be ca...
Definition: IPlugWAM.h:51
void CreateTimer()
Called by the API class to create the timer that pumps the parameter/message queues.
bool SendMidiMsg(const IMidiMsg &msg) override
Send a single MIDI message // TODO: info about what thread should this be called on or not called on!...
Definition: IPlugAPP.cpp:69
bool Pop(T &item)
Definition: IPlugQueue.h:72
virtual void ProcessSysEx(ISysEx &msg)
Override this method to handle incoming MIDI System Exclusive (SysEx) messages.
virtual void ProcessMidiMsg(const IMidiMsg &msg)
Override this method to handle incoming MIDI messages.
The base class for IPlug Audio Processing.
A struct for dealing with SysEx messages.
Definition: IPlugMidi.h:538
This structure is used when queueing Sysex messages.
Definition: IPlugStructs.h:44
A class that hosts an IPlug as a standalone app and provides Audio/Midi I/O.
Definition: IPlugAPP_host.h:81
int NChannelsConnected(ERoute direction) const