-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathEngine.cpp
More file actions
240 lines (207 loc) · 7.99 KB
/
Engine.cpp
File metadata and controls
240 lines (207 loc) · 7.99 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
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
// Copyright (C) 2013 Samplecount S.L.
// Copyright (C) 2026 Methcla contributors
//
// SPDX-License-Identifier: Apache-2.0
#include "Engine.hpp"
#include <methcla/file.hpp>
#include <methcla/plugins/disksampler.h>
#include <methcla/plugins/node-control.h>
#include <methcla/plugins/patch-cable.h>
#include <methcla/plugins/sampler.h>
#include <filesystem>
#include <sstream>
#include <stdexcept>
using namespace Methcla::Examples::Sampler;
Sound::Sound(const Methcla::Engine& engine, const std::string& path)
: m_path(path)
{
Methcla::SoundFile file(engine, path);
m_duration = (double)file.info().frames / (double)file.info().samplerate;
}
// Return a list of sounds in directory path.
static std::vector<Sound> loadSounds(Methcla::Engine& engine,
const std::string& path)
{
engine.logLine(kMethcla_LogDebug,
std::string("Loading sounds from ") + path);
std::vector<Sound> result;
for (auto& entry : std::filesystem::directory_iterator(path))
{
std::stringstream s;
s << "readfile: " << entry.path().string() << " "
<< entry.path().filename().string() << " " << entry.is_directory()
<< " " << entry.is_regular_file();
engine.logLine(kMethcla_LogDebug, s.str());
if (!entry.is_directory())
{
try
{
engine.logLine(kMethcla_LogDebug,
std::string("Loading sound ") +
entry.path().string());
result.push_back(Sound(engine, entry.path().string()));
}
catch (std::exception& e)
{
std::stringstream s;
s << "Exception while registering sound "
<< entry.path().filename().string() << ": " << e.what();
engine.logLine(kMethcla_LogError, s.str());
}
}
}
return result;
}
static const char* samplerPlugin(bool useDisk)
{
return useDisk ? METHCLA_PLUGINS_DISKSAMPLER_URI
: METHCLA_PLUGINS_SAMPLER_URI;
}
Engine::Engine(Options inOptions)
: m_engine(nullptr)
, m_useDisk(true)
{
Methcla::EngineOptions options(inOptions.engineOptions);
options.audioDriver.bufferSize = 256;
options.addLibrary(methcla_plugins_node_control)
.addLibrary(methcla_plugins_patch_cable)
.addLibrary(methcla_plugins_sampler)
.addLibrary(methcla_plugins_disksampler);
// Create the engine with a set of plugins.
m_engine = new Methcla::Engine(options, inOptions.audioDriver);
if (!inOptions.soundDir.empty())
m_sounds = loadSounds(*m_engine, inOptions.soundDir);
for (auto file : inOptions.sounds)
m_sounds.push_back(Sound(*m_engine, file));
// Start the engine.
engine().start();
m_voiceGroup = engine().group(engine().root());
std::cout << "Using " << samplerPlugin(m_useDisk) << " for sample playback"
<< std::endl;
// for (auto bus : { 0, 1 })
// {
// Methcla::Request request(engine());
// request.openBundle(Methcla::immediately);
// auto synth = request.synth(METHCLA_PLUGINS_PATCH_CABLE_URI,
// engine().root(), {}); request.activate(synth);
// request.mapInput(synth, 0, Methcla::AudioBusId(bus));
// request.mapOutput(synth, 0, Methcla::AudioBusId(bus),
// Methcla::kBusMappingExternal); request.closeBundle();
// request.send();
// m_patchCables.push_back(synth);
// }
}
Engine::~Engine()
{
engine().free(m_voiceGroup);
for (auto synth : m_patchCables)
{
engine().free(synth);
}
delete m_engine;
}
size_t Engine::numSounds() const
{
return m_sounds.size();
}
void Engine::useDisk(bool flag)
{
m_useDisk = flag;
}
static Methcla_Time kLatency = 0.003;
static float mapRate(float value)
{
const float numOctaves = 4.f;
return expmap(1.f / numOctaves, numOctaves, 0.f, 1.f, value);
}
void Engine::startVoice(VoiceId voice, size_t soundIndex, float amp, float rate)
{
if (m_voices.find(voice) != m_voices.end())
stopVoice(voice);
if (soundIndex < m_sounds.size())
{
const Sound& sound = m_sounds[soundIndex];
Methcla::Request request(engine());
request.openBundle(Methcla::immediately);
// Allocate two buses
Methcla::AudioBusId bus1 = m_engine->audioBusId().alloc();
Methcla::AudioBusId bus2 = m_engine->audioBusId().alloc();
// Create synth and map outputs to buses
const Methcla::SynthId synth = request.synth(
samplerPlugin(m_useDisk), m_voiceGroup, {1, 48000. / 44100.},
{Methcla::Value(sound.path()), Methcla::Value(false)});
request.mapOutput(synth, 0, bus1);
request.mapOutput(synth, 1, bus2);
// request.mapOutput(synth, 0, Methcla::AudioBusId(0),
// Methcla::kBusMappingExternal); request.mapOutput(synth, 1,
// Methcla::AudioBusId(1), Methcla::kBusMappingExternal);
// Envelope options
const std::list<Methcla::Value> envOptions = {
Methcla::Value(0.05f), Methcla::Value(1.f), Methcla::Value(1.f),
Methcla::Value(1.5f)};
// auto envelope1 =
// request.synth(METHCLA_PLUGINS_ASR_ENVELOPE_URI,
// m_voiceGroup, {}, envOptions);
auto envelope1 =
request.synth(METHCLA_PLUGINS_PATCH_CABLE_URI, m_voiceGroup, {});
request.mapInput(envelope1, 0, bus1);
request.mapOutput(envelope1, 0, Methcla::AudioBusId(0),
Methcla::kBusMappingExternal);
request.whenDone(envelope1, Methcla::kNodeDoneFreeSelf |
Methcla::kNodeDoneFreePreceeding);
// auto envelope2 =
// request.synth(METHCLA_PLUGINS_ASR_ENVELOPE_URI,
// m_voiceGroup, {}, envOptions);
auto envelope2 =
request.synth(METHCLA_PLUGINS_PATCH_CABLE_URI, m_voiceGroup, {});
request.mapInput(envelope2, 0, bus2);
request.mapOutput(envelope2, 0, Methcla::AudioBusId(1),
Methcla::kBusMappingExternal);
request.whenDone(envelope2, Methcla::kNodeDoneFreeSelf);
request.openBundle(engine().currentTime() + kLatency);
request.activate(synth);
request.activate(envelope1);
request.activate(envelope2);
request.closeBundle();
request.closeBundle();
m_engine->addNotificationHandler(m_engine->freeNodeIdHandler(synth));
// m_engine->addNotificationHandler(m_engine->freeNodeIdHandler(envelope1));
// m_engine->addNotificationHandler(m_engine->freeNodeIdHandler(envelope2,
// [this,bus1,bus2](Methcla::NodeId){
// m_engine->audioBusId().free(bus1);
// m_engine->audioBusId().free(bus2);
// }));
request.send();
m_voices[voice] = synth;
// std::cout << "Synth " << synth.id()
// << sound.path()
// << " duration=" << sound.duration()
// << " param=" << param
// << " rate=" << mapRate(param)
// << std::endl;
}
}
void Engine::updateVoice(VoiceId voice, float amp, float rate)
{
// auto it = m_voices.find(voice);
// assert( it != m_voices.end() );
// const float rate = mapRate(param);
// m_engine->set(it->second, 1, rate);
// std::cout << "Synth " << it->second.id()
// << " param=" << param
// << " rate=" << rate
// << std::endl;
}
void Engine::stopVoice(VoiceId voice)
{
auto it = m_voices.find(voice);
if (it != m_voices.end())
{
Methcla::Request request(engine());
request.openBundle(engine().currentTime() + kLatency);
request.free(it->second);
request.closeBundle();
request.send();
m_voices.erase(it);
}
}