Repository navigation
Expand file tree
/
Copy pathrnbo-lib-wrapper.cpp
More file actions
174 lines (120 loc) · 4.07 KB
/
Copy pathrnbo-lib-wrapper.cpp
File metadata and controls
174 lines (120 loc) · 4.07 KB
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
/*
python wrapper for Max RNBO exported C++
rama gottfried, icst zhdk 2023
*/
#include <iostream>
#include <memory>
#include "RNBO.h"
#define WIN_DECLSPEC
#ifdef WIN32
#define WIN_DECLSPEC __declspec(dllexport)
#endif
/** globals **/
int sampleRate = 48000;
int vectorSize = 64;
std::unique_ptr<RNBO::CoreObject> rnboObject; // the RNBO object
std::vector<RNBO::SampleValue> retVec; // state holder for return vector to python
extern "C" {
WIN_DECLSPEC void init(int sr, int vs)
{
rnboObject = std::make_unique<RNBO::CoreObject>();
sampleRate = sr;
vectorSize = vs;
retVec.resize(vs);
rnboObject->prepareToProcess(sr, vs);
}
WIN_DECLSPEC bool validObject()
{
if(!rnboObject.get()){
std::cout << "error: RNBO object must be initialized before use -- to initialize call: init(samplerate, vectorsize)";
return 0;
}
return 1;
}
// https://rnbo.cycling74.com/learn/getting-and-setting-parameters-cpp
WIN_DECLSPEC void printInfo()
{
if( !validObject() ) return;
int n = rnboObject->getNumParameters();
for (int i = 0; i < n; i++) {
const char *id = rnboObject->getParameterId(i);
RNBO::ParameterInfo info;
rnboObject->getParameterInfo(i, &info);
std::cout << i << ":\t" << id <<
"\n\t(min:" << info.min << ", max:" << info.max << ")\n" <<
"\tcurrent value: " << rnboObject->getParameterValue(i) << "\n\n";
}
// add number of channels
// what else?
}
WIN_DECLSPEC void clearState() {
if( !validObject() ) return;
rnboObject->prepareToProcess(sampleRate, vectorSize);
}
WIN_DECLSPEC RNBO::SampleValue* rnbo_process_vec()
{
if( !validObject() ) return nullptr;
RNBO::SampleValue* ptrVec = retVec.data();
rnboObject->process<RNBO::SampleValue**, RNBO::SampleValue**>(nullptr, 0, &ptrVec, 1, vectorSize);
return retVec.data();
}
// set-get by Id
WIN_DECLSPEC void setParamValueById(const char* id, double val)
{
if( !validObject() ) return;
int parameterIndex = rnboObject->getParameterIndexForID(id);
rnboObject->setParameterValue(parameterIndex, val);
}
WIN_DECLSPEC double getParamValueById(const char* id)
{
if( !validObject() ) return 0;
int parameterIndex = rnboObject->getParameterIndexForID(id);
return rnboObject->getParameterValue(parameterIndex);
}
WIN_DECLSPEC void setNormParamValueById(const char* id, double val)
{
if( !validObject() ) return;
int parameterIndex = rnboObject->getParameterIndexForID(id);
rnboObject->setParameterValueNormalized(parameterIndex, val);
}
WIN_DECLSPEC double convertFromNormParamValueById(const char* id, double val)
{
if( !validObject() ) return 0;
int parameterIndex = rnboObject->getParameterIndexForID(id);
return rnboObject->convertFromNormalizedParameterValue(parameterIndex, val);
}
// set-get by Idx
WIN_DECLSPEC void setParamValueByIdx(int idx, double val)
{
if( !validObject() ) return;
rnboObject->setParameterValue(idx, val);
}
WIN_DECLSPEC double getParamValueByIdx(int idx)
{
if( !validObject() ) return 0;
return rnboObject->getParameterValue(idx);
}
WIN_DECLSPEC void setNormParamValueByIdx(int idx, double val)
{
if( !validObject() ) return;
rnboObject->setParameterValueNormalized(idx, val);
}
WIN_DECLSPEC double convertFromNormParamValueByIdx(int idx, double val)
{
if( !validObject() ) return 0;
return rnboObject->convertFromNormalizedParameterValue(idx, val);
}
// https://rnbo.cycling74.com/learn/getting-and-setting-parameters-cpp
// https://rnbo.cycling74.com/learn/sending-and-receiving-midi-cpp
WIN_DECLSPEC void midiEvent(int midiNote, int midiVelocity)
{
if( !validObject() ) return;
const uint8_t noteOn = 0x90;
const uint8_t midiChannel = 0;
const uint8_t noteOnLeadByte = noteOn | midiChannel;
const uint8_t pitch = midiNote;
const uint8_t velocity = midiVelocity; // probably need to clamp 0-127
const uint8_t midiBytes[3] = { noteOnLeadByte, pitch, velocity };
rnboObject->scheduleEvent(RNBO::MidiEvent(RNBO::RNBOTimeNow, 0, midiBytes, 3));
}
}