File: OSCParameterInterface.cpp

package info (click to toggle)
iem-plugin-suite 1.15.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,080 kB
  • sloc: cpp: 58,973; python: 269; sh: 79; makefile: 41
file content (281 lines) | stat: -rw-r--r-- 10,380 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 ==============================================================================
 This file is part of the IEM plug-in suite.
 Author: Daniel Rudrich
 Copyright (c) 2018 - Institute of Electronic Music and Acoustics (IEM)
 https://iem.at

 The IEM plug-in suite is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 The IEM plug-in suite is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this software.  If not, see <http://www.gnu.org/licenses/>.
 ==============================================================================
 */

#include "OSCParameterInterface.h"
#include "AudioProcessorBase.h"

OSCParameterInterface::OSCParameterInterface (OSCMessageInterceptor& i,
                                              juce::AudioProcessorValueTreeState& valueTreeState) :
    interceptor (i), parameters (valueTreeState)
{
#ifdef DEBUG_PARAMETERS_FOR_DOCUMENTATION
    auto& params = parameters.processor.getParameters();
    for (auto& item : params)
    {
        if (auto* ptr = dynamic_cast<juce::AudioProcessorParameterWithID*> (
                item)) // that's maybe not the best solution, but it does the job for now
        {
            auto parameterID = ptr->paramID;
            auto parameterName = ptr->name;
            auto range = parameters.getParameterRange (parameterID);
            DBG ("| " << parameterID << " | " << range.getRange().getStart() << " : "
                      << range.getRange().getEnd() << " | " << parameterName << " | |");
        }
    }
#endif

    lastSentValues.resize (parameters.processor.getParameters().size());
    lastSentValues.fill (-1);
    setOSCAddress (juce::String (JucePlugin_Name));

    oscReceiver.addListener (this);

    startTimer (100);
}

std::unique_ptr<juce::RangedAudioParameter> OSCParameterInterface::createParameterTheOldWay (
    const juce::String& parameterID,
    const juce::String& parameterName,
    const juce::String& labelText,
    juce::NormalisableRange<float> valueRange,
    float defaultValue,
    std::function<juce::String (float)> valueToTextFunction,
    std::function<float (const juce::String&)> textToValueFunction,
    bool isMetaParameter,
    bool isAutomatableParameter,
    bool isDiscrete,
    juce::AudioProcessorParameter::Category category,
    bool isBoolean)
{
    return std::make_unique<juce::AudioProcessorValueTreeState::Parameter> (parameterID,
                                                                            parameterName,
                                                                            labelText,
                                                                            valueRange,
                                                                            defaultValue,
                                                                            valueToTextFunction,
                                                                            textToValueFunction,
                                                                            isMetaParameter,
                                                                            isAutomatableParameter,
                                                                            isDiscrete,
                                                                            category,
                                                                            isBoolean);
}

const bool OSCParameterInterface::processOSCMessage (juce::OSCMessage oscMessage)
{
    auto pattern = oscMessage.getAddressPattern();
    if (pattern.containsWildcards())
    {
        auto& params = parameters.processor.getParameters();
        for (auto& item : params)
        {
            if (auto* ptr = dynamic_cast<juce::AudioProcessorParameterWithID*> (
                    item)) // that's maybe not the best solution, but it does the job for now
            {
                auto paramAddress = ptr->paramID;
                if (pattern.matches (juce::OSCAddress ("/" + paramAddress)))
                {
                    if (oscMessage.size() > 0)
                    {
                        auto arg = oscMessage[0];
                        float value = 0.0f;
                        if (arg.isInt32())
                            value = arg.getInt32();
                        else if (arg.isFloat32())
                            value = arg.getFloat32();
                        else
                            return true;

                        setValue (paramAddress, value);
                    }
                }
            }
        }
    }

    juce::String paramAddress =
        oscMessage.getAddressPattern().toString().substring (1); // trimming forward slash
    if (auto parameter = parameters.getParameter (paramAddress))
    {
        if (oscMessage.size() > 0)
        {
            auto arg = oscMessage[0];
            float value = 0.0f;
            if (arg.isInt32())
                value = arg.getInt32();
            else if (arg.isFloat32())
                value = arg.getFloat32();
            else
                return true;

            setValue (paramAddress, value);
        }
        return true;
    }
    else
        return false;
}

void OSCParameterInterface::setValue (juce::String paramID, float value)
{
    auto range (parameters.getParameterRange (paramID));
    parameters.getParameter (paramID)->setValueNotifyingHost (range.convertTo0to1 (value));
}

void OSCParameterInterface::oscMessageReceived (const juce::OSCMessage& message)
{
    juce::OSCMessage messageCopy (message);
    if (! interceptor.interceptOSCMessage (messageCopy))
    {
        juce::String prefix ("/" + juce::String (JucePlugin_Name));
        if (message.getAddressPattern().toString().startsWith (prefix))
        {
            juce::OSCMessage msg (message);
            msg.setAddressPattern (message.getAddressPattern().toString().substring (
                juce::String (JucePlugin_Name).length() + 1));

            if (processOSCMessage (msg))
                return;
        }

        if (interceptor.processNotYetConsumedOSCMessage (message))
            return;

        // open/change osc port
        if (message.getAddressPattern().toString().equalsIgnoreCase ("/openOSCPort")
            && message.size() == 1)
        {
            int newPort = -1;

            if (message[0].isInt32())
                newPort = message[0].getInt32();
            else if (message[0].isFloat32())
                newPort = static_cast<int> (message[0].getFloat32());

            if (newPort > 0)
                juce::MessageManager::callAsync ([this, newPort]()
                                                 { oscReceiver.connect (newPort); });
        }

        if (message.getAddressPattern().toString().equalsIgnoreCase ("/flushParams"))
            juce::MessageManager::callAsync ([this]() { sendParameterChanges (true); });
    }
}

void OSCParameterInterface::oscBundleReceived (const juce::OSCBundle& bundle)
{
    for (int i = 0; i < bundle.size(); ++i)
    {
        auto elem = bundle[i];
        if (elem.isMessage())
            oscMessageReceived (elem.getMessage());
        else if (elem.isBundle())
            oscBundleReceived (elem.getBundle());
    }
}

void OSCParameterInterface::timerCallback()
{
    sendParameterChanges();
}

void OSCParameterInterface::sendParameterChanges (const bool forceSend)
{
    if (! oscSender.isConnected())
        return;

    auto& params = parameters.processor.getParameters();
    const int nParams = params.size();
    for (int i = 0; i < nParams; ++i)
    {
        auto item = params[i];
        if (auto* ptr = dynamic_cast<juce::AudioProcessorParameterWithID*> (
                item)) // that's maybe not the best solution, but it does the job for now
        {
            const auto normValue = ptr->getValue();

            if (forceSend || lastSentValues[i] != normValue)
            {
                lastSentValues.set (i, normValue);

                const auto paramID = ptr->paramID;
                auto range (parameters.getParameterRange (paramID));

                try
                {
                    juce::OSCMessage message (address + paramID, range.convertFrom0to1 (normValue));
                    oscSender.send (message);
                }
                catch (...)
                {
                };
            }
        }
    }

    interceptor.sendAdditionalOSCMessages (oscSender, address);
}

void OSCParameterInterface::setInterval (const int interValInMilliseconds)
{
    startTimer (juce::jlimit (1, 1000, interValInMilliseconds));
}

void OSCParameterInterface::setOSCAddress (juce::String newAddress)
{
    if (newAddress.isEmpty())
        address = "/";
    else
    {
        newAddress = newAddress.trimCharactersAtStart ("/");
        newAddress = newAddress.trimCharactersAtEnd ("/");
        newAddress = newAddress.removeCharacters (" öäü#*,?[]{}");

        if (newAddress.isEmpty())
            address = "/";
        else
            address = "/" + newAddress + "/";
    }
}

juce::ValueTree OSCParameterInterface::getConfig() const
{
    juce::ValueTree config ("OSCConfig");

    config.setProperty ("ReceiverPort", oscReceiver.getPortNumber(), nullptr);
    config.setProperty ("SenderIP", oscSender.getHostName(), nullptr);
    config.setProperty ("SenderPort", oscSender.getPortNumber(), nullptr);
    config.setProperty ("SenderOSCAddress", getOSCAddress(), nullptr);
    config.setProperty ("SenderInterval", getInterval(), nullptr);

    return config;
}

void OSCParameterInterface::setConfig (juce::ValueTree config)
{
    jassert (config.hasType ("OSCConfig"));

    oscReceiver.connect (config.getProperty ("ReceiverPort", -1));
    setOSCAddress (config.getProperty ("SenderOSCAddress", juce::String (JucePlugin_Name)));
    setInterval (config.getProperty ("SenderInterval", 100));
    oscSender.connect (config.getProperty ("SenderIP", ""), config.getProperty ("SenderPort", -1));
}