iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
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#include <cstdio>
14#include <cmath>
15
16#ifdef OS_LINUX
17#include "IPlugSWELLTimer.h"
18#if defined WEBVIEW_EDITOR_DELEGATE
19extern "C" void IPlugLinuxWebViewPump();
20#endif
21#endif
22
23#if defined OS_MAC || defined OS_LINUX
24#include <IPlugSWELL.h>
25#else
26extern float GetScaleForHWND(HWND hWnd);
27#endif
28
29using namespace iplug;
30
31extern HWND gHWND;
32
33IPlugAPP::IPlugAPP(const InstanceInfo& info, const Config& config)
34: IPlugAPIBase(config, kAPIAPP)
35, IPlugProcessor(config, kAPIAPP)
36{
37 mAppHost = (IPlugAPPHost*) info.pAppHost;
38
39 Trace(TRACELOC, "%s%s", config.pluginName, config.channelIOStr);
40
41 SetChannelConnections(ERoute::kInput, 0, MaxNChannels(ERoute::kInput), true);
42 SetChannelConnections(ERoute::kOutput, 0, MaxNChannels(ERoute::kOutput), true);
43
44 SetBlockSize(DEFAULT_BLOCK_SIZE);
45
46#ifdef OS_LINUX
47 mTimer.reset(SWELLTimer::Create([this](Timer&) {
48#ifndef NO_IGRAPHICS
49 CloseWindowIfRequested();
50#endif
51 OnTimer();
52#if defined WEBVIEW_EDITOR_DELEGATE
53 IPlugLinuxWebViewPump();
54#endif
55 }, IDLE_TIMER_RATE));
56#else
57 CreateTimer();
58#endif
59}
60
61bool IPlugAPP::EditorResize(int viewWidth, int viewHeight)
62{
63 bool parentResized = false;
64
65 if (viewWidth != GetEditorWidth() || viewHeight != GetEditorHeight())
66 {
67 #if defined OS_MAC || defined OS_LINUX || defined NO_IGRAPHICS
68 RECT rcClient, rcWindow;
69 POINT ptDiff;
70
71 GetClientRect(gHWND, &rcClient);
72 GetWindowRect(gHWND, &rcWindow);
73
74 ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right;
75 ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom;
76
77 int flags = 0;
78
79 #ifdef OS_WIN
80 flags = SWP_NOMOVE;
81 float ss = GetScaleForHWND(gHWND);
82 #elif defined OS_LINUX
83 flags = SWP_NOMOVE | SWP_NOZORDER;
84 float ss = 1.f;
85 // Convert editor units to the container's GDK units (see ResolveLinuxScreenScale).
86 const float containerScale = IPlugAPPHost::ResolveLinuxScreenScale(gHWND);
87 const int containerWidth = static_cast<int>(std::lround(viewWidth * containerScale));
88 const int containerHeight = static_cast<int>(std::lround(viewHeight * containerScale));
89 #else
90 float ss = 1.f;
91 #endif
92
93 #ifndef OS_LINUX
94 const int containerWidth = viewWidth;
95 const int containerHeight = viewHeight;
96 #endif
97
98 SetWindowPos(gHWND, 0,
99 static_cast<LONG>(rcWindow.left * ss),
100 static_cast<LONG>((rcWindow.bottom - containerHeight - ptDiff.y) * ss),
101 static_cast<LONG>((containerWidth + ptDiff.x) * ss),
102 static_cast<LONG>((containerHeight + ptDiff.y) * ss), flags);
103 parentResized = true;
104 #endif
105
106 SetEditorSize(viewWidth, viewHeight);
107 }
108
109 return parentResized;
110}
111
113{
114 if (DoesMIDIOut() && mAppHost->mMidiOut)
115 {
116 //TODO: midi out channel
117// uint8_t status;
118//
119// // if the midi channel out filter is set, reassign the status byte appropriately
120// if (mAppHost->mMidiOutChannel > -1)
121// status = mAppHost->mMidiOutChannel-1 | ((uint8_t) msg.StatusMsg() << 4) ;
122
123 std::vector<uint8_t> message;
124 message.push_back(msg.mStatus);
125 message.push_back(msg.mData1);
126 message.push_back(msg.mData2);
127
128 mAppHost->mMidiOut->sendMessage(&message);
129
130 return true;
131 }
132
133 return false;
134}
135
137{
138 if (DoesMIDIOut() && mAppHost->mMidiOut)
139 {
140 //TODO: midi out channel
141 std::vector<uint8_t> message;
142
143 for (int i = 0; i < msg.mSize; i++)
144 {
145 message.push_back(msg.mData[i]);
146 }
147
148 mAppHost->mMidiOut->sendMessage(&message);
149 return true;
150 }
151
152 return false;
153}
154
155void IPlugAPP::SendSysexMsgFromUI(const ISysEx& msg)
156{
157 SendSysEx(msg);
158}
159
160void IPlugAPP::AppProcess(double** inputs, double** outputs, int nFrames)
161{
162 // Inputs the user hasn't routed a device channel to are null, and disconnected, so that the plug-in
163 // can tell (IsChannelConnected). Their buffers are zeroed. Outputs are always connected
164 const int nInputs = MaxNChannels(ERoute::kInput);
165 bool allInputsConnected = true;
166
167 for (int i = 0; i < nInputs; i++)
168 {
169 const bool connected = inputs[i] != nullptr;
170 allInputsConnected &= connected;
171
172 if (IsChannelConnected(ERoute::kInput, i) != connected)
173 SetChannelConnections(ERoute::kInput, i, 1, connected);
174 }
175
176 if (!allInputsConnected)
177 ZeroScratchBuffers();
178
179 for (int i = 0; i < nInputs; i++)
180 {
181 if (inputs[i])
182 AttachBuffers(ERoute::kInput, i, 1, &inputs[i], GetBlockSize());
183 }
184
185 SetChannelConnections(ERoute::kOutput, 0, MaxNChannels(ERoute::kOutput), true);
186 AttachBuffers(ERoute::kOutput, 0, MaxNChannels(ERoute::kOutput), outputs, GetBlockSize());
187
188 // A MIDI input was closed or disconnected: stop notes it started
189 if (mAppHost->mSendAllNotesOff.exchange(false))
190 {
191 for (int chan = 0; chan < 16; chan++)
192 {
193 IMidiMsg msg;
194 msg.MakeControlChangeMsg(IMidiMsg::kAllNotesOff, 0., chan);
195 ProcessMidiMsg(msg);
196 }
197 }
198
199 if (mMidiMsgsFromCallback.ElementsAvailable())
200 {
201 IMidiMsg msg;
202
203 while (mMidiMsgsFromCallback.Pop(msg))
204 {
205 ProcessMidiMsg(msg);
206 mMidiMsgsFromProcessor.Push(msg); // queue incoming MIDI for UI
207 }
208 }
209
210 if (mSysExMsgsFromCallback.ElementsAvailable())
211 {
212 SysExData data;
213
214 while (mSysExMsgsFromCallback.Pop(data))
215 {
216 ISysEx msg { data.mOffset, data.mData, data.mSize };
217 ProcessSysEx(msg);
218 mSysExDataFromProcessor.Push(data); // queue incoming Sysex for UI
219 }
220 }
221
222 if (mMidiMsgsFromEditor.ElementsAvailable())
223 {
224 IMidiMsg msg;
225
226 while (mMidiMsgsFromEditor.Pop(msg))
227 {
228 ProcessMidiMsg(msg);
229 }
230 }
231
232 //Do not handle Sysex messages here - SendSysexMsgFromUI overridden
233
234 ENTER_PARAMS_MUTEX
235 ProcessBuffers(0.0, GetBlockSize());
236 LEAVE_PARAMS_MUTEX
237}
The base class of an IPlug plug-in, which interacts with the different plug-in APIs.
Definition: IPlugAPIBase.h:43
A class that hosts an IPlug as a standalone app and provides Audio/Midi I/O.
Definition: IPlugAPP_host.h:93
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:136
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:112
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:61
The base class for IPlug Audio Processing.
virtual void ProcessMidiMsg(const IMidiMsg &msg)
Override this method to handle incoming MIDI messages.
bool IsChannelConnected(ERoute direction, int chIdx) const
int GetBlockSize() const
virtual void ProcessSysEx(const ISysEx &msg)
Override this method to handle incoming MIDI System Exclusive (SysEx) messages.
int MaxNChannels(ERoute direction) const
bool DoesMIDIOut() const
bool Pop(T &item)
Removes and retrieves an item from the queue.
Definition: IPlugQueue.h:74
size_t ElementsAvailable() const
Returns the number of elements currently in the queue.
Definition: IPlugQueue.h:106
Encapsulates a MIDI message and provides helper functions.
Definition: IPlugMidi.h:31
void MakeControlChangeMsg(EControlChangeMsg idx, double value, int channel=0, int offset=0)
Create a CC message.
Definition: IPlugMidi.h:186
A struct for dealing with SysEx messages.
Definition: IPlugMidi.h:539
This structure is used when queueing Sysex messages.
Definition: IPlugStructs.h:45
Base class for timer.
Definition: IPlugTimer.h:43