-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathEngine.hpp
More file actions
98 lines (77 loc) · 2.51 KB
/
Engine.hpp
File metadata and controls
98 lines (77 loc) · 2.51 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
// Copyright (C) 2013 Samplecount S.L.
// Copyright (C) 2026 Methcla contributors
//
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <methcla/engine.hpp>
#include <string>
#include <unordered_map>
#include <vector>
namespace Methcla { namespace Examples { namespace Sampler {
class Sound
{
public:
Sound(const Methcla::Engine& engine, const std::string& path);
const std::string& path() const
{
return m_path;
}
float duration() const
{
return m_duration;
}
private:
std::string m_path;
float m_duration;
};
template <typename T> T linmap(T outMin, T outMax, T inMin, T inMax, T x)
{
return (x - inMin) / (inMax - inMin) * (outMax - outMin) + outMin;
}
template <typename T> T expmap(T outMin, T outMax, T inMin, T inMax, T x)
{
return outMin *
std::pow(outMax / outMin, (x - inMin) / (inMax - inMin));
}
template <typename T> T dbamp(T db)
{
return std::pow(T(10), db / T(20));
}
class Engine
{
public:
struct Options
{
Methcla::EngineOptions engineOptions;
Methcla_AudioDriver* audioDriver = nullptr;
std::vector<std::string> sounds;
std::string soundDir;
};
Engine(Options options);
~Engine();
Engine(const Engine& other) = delete;
Engine& operator=(const Engine& other) = delete;
size_t numSounds() const;
typedef intptr_t VoiceId;
void useDisk(bool flag);
// Start a voice with a certain sound and amplitude.
void startVoice(VoiceId voice, size_t sound, float amp = 1.f,
float rate = 1.f);
// Update a voice's amplitude while playing.
void updateVoice(VoiceId voice, float amp, float rate = 1.f);
// Stop a voice.
void stopVoice(VoiceId voice);
private:
Methcla::Engine& engine()
{
return *m_engine;
}
private:
std::vector<Sound> m_sounds;
Methcla::Engine* m_engine;
bool m_useDisk;
Methcla::GroupId m_voiceGroup;
std::vector<Methcla::SynthId> m_patchCables;
std::unordered_map<VoiceId, Methcla::SynthId> m_voices;
};
}}} // namespace Methcla::Examples::Sampler