-
Notifications
You must be signed in to change notification settings - Fork 4
/
sfizz_webaudio.cpp
196 lines (173 loc) · 7.16 KB
/
sfizz_webaudio.cpp
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
// Copyright 2021 Paul Ferrand
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#include <emscripten/bind.h>
#include <sstream>
#include <string_view>
#include <charconv>
#include "sfizz.hpp"
#include "sfizz/utility/bit_array/BitArray.h"
using namespace emscripten;
class SfizzWrapper
{
public:
SfizzWrapper(int32_t sampleRate)
{
synth.setReceiveCallback(*client, &SfizzWrapper::staticCallback);
synth.setSampleRate(static_cast<float>(sampleRate));
load("<region> sample=*saw");
}
void load(std::string file)
{
synth.loadSfzString("file.sfz", file);
synth.getCCLabels();
}
void noteOn(int delay, uint8_t number, float value) { synth.hdNoteOn(delay, number, value); }
void noteOff(int delay, uint8_t number, float value) { synth.hdNoteOff(delay, number, value); }
void cc(int delay, uint8_t number, float value) { synth.hdcc(delay, number, value); }
void aftertouch(int delay, float value) { synth.hdChannelAftertouch(delay, value); }
void pitchWheel(int delay, float value) { synth.hdPitchWheel(delay, value); }
int numRegions() const { return synth.getNumRegions(); }
int numActiveVoices() const { return synth.getNumActiveVoices(); }
std::vector<int> usedCCs() const
{
const_cast<SfizzWrapper*>(this)->synth.sendMessage(*client, 0, "/cc/slots", "", nullptr);
return usedCCs_;
}
std::string ccLabel(int number) const
{
if (number < 0)
return {};
lastLabel.clear();
std::stringstream path;
path << "/cc" << number << "/label";
const_cast<SfizzWrapper*>(this)->synth.sendMessage(*client, 0, path.str().c_str(), "", nullptr);
return lastLabel;
}
float ccValue(int number) const
{
if (number < 0)
return 0.0f;
lastCCValue = 0.0f;
std::stringstream path;
path << "/cc" << number << "/value";
const_cast<SfizzWrapper*>(this)->synth.sendMessage(*client, 0, path.str().c_str(), "", nullptr);
return lastCCValue;
}
float ccDefault(int number) const
{
if (number < 0)
return 0.0f;
lastCCDefault = 0.0f;
std::stringstream path;
path << "/cc" << number << "/default";
const_cast<SfizzWrapper*>(this)->synth.sendMessage(*client, 0, path.str().c_str(), "", nullptr);
return lastCCDefault;
}
void render(uintptr_t left, uintptr_t right, int32_t numFrames)
{
// Use type cast to hide the raw pointer in function arguments.
float *output_array[2] = {
reinterpret_cast<float *>(left),
reinterpret_cast<float *>(right)};
synth.renderBlock(output_array, numFrames);
}
static void staticCallback(void *data, int delay, const char *path, const char *sig, const sfizz_arg_t *args)
{
auto self = reinterpret_cast<SfizzWrapper *>(data);
self->clientCallback(delay, path, sig, args);
}
void clientCallback(int delay, const char *path, const char *sig, const sfizz_arg_t *args)
{
unsigned indices[8];
if (!strcmp(path, "/cc/slots") && !strcmp(sig, "b"))
{
usedCCs_.clear();
ConstBitSpan bits(args[0].b->data, 8 * args[0].b->size);
for (unsigned cc = 0; cc < 512 && cc < bits.bit_size(); ++cc)
{
if (bits.test(cc))
{
usedCCs_.push_back(cc);
std::stringstream path;
path << "/cc" << cc << "/label";
synth.sendMessage(*client, delay, path.str().c_str(), "", nullptr);
path.clear();
path << "/cc" << cc << "/value";
synth.sendMessage(*client, delay, path.str().c_str(), "", nullptr);
path.clear();
path << "/cc" << cc << "/default";
synth.sendMessage(*client, delay, path.str().c_str(), "", nullptr);
}
}
} else if (matchOSC("/cc&/label", path, indices) && !strcmp(sig, "s")) {
lastLabel = args[0].s;
} else if (matchOSC("/cc&/value", path, indices) && !strcmp(sig, "f")) {
lastCCValue = args[0].f;
} else if (matchOSC("/cc&/default", path, indices) && !strcmp(sig, "f")) {
lastCCDefault = args[0].f;
}
}
private:
sfz::Sfizz synth;
sfz::ClientPtr client = synth.createClient(this);
std::vector<int> usedCCs_;
mutable std::string lastLabel;
mutable float lastCCValue;
mutable float lastCCDefault;
bool matchOSC(const char *pattern, const char *path, unsigned *indices)
{
unsigned nthIndex = 0;
while (const char *endp = std::strchr(pattern, '&'))
{
size_t length = endp - pattern;
if (std::strncmp(pattern, path, length))
return false;
pattern += length;
path += length;
length = 0;
while (std::isdigit(path[length]))
++length;
auto result = std::from_chars(path, path + length, indices[nthIndex++]);
if (result.ec != std::errc())
return false;
pattern += 1;
path += length;
}
return !std::strcmp(path, pattern);
}
};
EMSCRIPTEN_BINDINGS(CLASS_Synthesizer)
{
register_vector<int>("vector<int>");
class_<SfizzWrapper>("SfizzWrapper")
.constructor<int32_t>()
.function("load", &SfizzWrapper::load)
.function("noteOff", &SfizzWrapper::noteOff)
.function("noteOn", &SfizzWrapper::noteOn)
.function("cc", &SfizzWrapper::cc)
.function("aftertouch", &SfizzWrapper::aftertouch)
.function("pitchWheel", &SfizzWrapper::pitchWheel)
.function("numRegions", &SfizzWrapper::numRegions)
.function("numActiveVoices", &SfizzWrapper::numActiveVoices)
.function("usedCCs", &SfizzWrapper::usedCCs)
.function("ccLabel", &SfizzWrapper::ccLabel)
.function("ccValue", &SfizzWrapper::ccValue)
.function("ccDefault", &SfizzWrapper::ccDefault)
.function("render", &SfizzWrapper::render, allow_raw_pointers());
}