-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstrument_demo.cpp
150 lines (127 loc) · 4.52 KB
/
instrument_demo.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
#include <algorithm>
#include <array>
#include <cctype>
#include <chrono>
#include <iterator>
#include <optional>
#include <span>
#include <thread>
#include "barelymusician.h"
#include "common/audio_output.h"
#include "common/console_log.h"
#include "common/input_manager.h"
namespace {
using ::barely::ControlType;
using ::barely::Musician;
using ::barely::OscillatorShape;
using ::barely::examples::AudioOutput;
using ::barely::examples::ConsoleLog;
using ::barely::examples::InputManager;
// System audio settings.
constexpr int kSampleRate = 48000;
constexpr int kSampleCount = 256;
// Instrument settings.
constexpr float kGain = -18.0f;
constexpr OscillatorShape kOscillatorShape = OscillatorShape::kSaw;
constexpr float kAttack = 0.05f;
constexpr float kRelease = 0.125f;
constexpr int kVoiceCount = 16;
// Note settings.
constexpr std::array<char, 13> kOctaveKeys = {'A', 'W', 'S', 'E', 'D', 'F', 'T',
'G', 'Y', 'H', 'U', 'J', 'K'};
constexpr float kRootPitch = 0.0f;
constexpr int kMaxOctaveShift = 4;
// Returns the pitch for a given `key`.
std::optional<float> PitchFromKey(int octave_shift, const InputManager::Key& key) {
const auto it = std::find(kOctaveKeys.begin(), kOctaveKeys.end(), std::toupper(key));
if (it == kOctaveKeys.end()) {
return std::nullopt;
}
return kRootPitch + static_cast<float>(octave_shift) +
static_cast<float>(std::distance(kOctaveKeys.begin(), it)) / 12.0f;
}
} // namespace
// NOLINTNEXTLINE(bugprone-exception-escape)
int main(int /*argc*/, char* /*argv*/[]) {
InputManager input_manager;
AudioOutput audio_output(kSampleRate, kSampleCount);
Musician musician(kSampleRate);
auto instrument = musician.CreateInstrument();
instrument.SetControl(ControlType::kGain, kGain);
instrument.SetControl(ControlType::kOscillatorShape, kOscillatorShape);
instrument.SetControl(ControlType::kAttack, kAttack);
instrument.SetControl(ControlType::kRelease, kRelease);
instrument.SetControl(ControlType::kVoiceCount, kVoiceCount);
instrument.SetNoteOnCallback([](float pitch, float intensity) {
ConsoleLog() << "NoteOn(" << pitch << ", " << intensity << ")";
});
instrument.SetNoteOffCallback([](float pitch) { ConsoleLog() << "NoteOff(" << pitch << ") "; });
// Audio process callback.
audio_output.SetProcessCallback([&](std::span<float> output_samples) {
instrument.Process(output_samples, /*timestamp=*/0.0);
});
// Key down callback.
float intensity = 1.0f;
int octave_shift = 0;
bool quit = false;
const auto key_down_callback = [&](const InputManager::Key& key) {
if (static_cast<int>(key) == 27) {
// ESC pressed, quit the app.
quit = true;
return;
}
const auto upper_key = std::toupper(key);
if (upper_key == 'Z' || upper_key == 'X') {
// Shift octaves.
instrument.SetAllNotesOff();
if (upper_key == 'Z') {
--octave_shift;
} else {
++octave_shift;
}
octave_shift = std::clamp(octave_shift, -kMaxOctaveShift, kMaxOctaveShift);
ConsoleLog() << "Octave shift set to " << octave_shift;
return;
}
if (upper_key == 'C' || upper_key == 'V') {
// Change intensity.
if (upper_key == 'C') {
intensity -= 0.25f;
} else {
intensity += 0.25f;
}
intensity = std::clamp(intensity, 0.0f, 1.0f);
ConsoleLog() << "Note intensity set to " << intensity;
return;
}
// Play note.
if (const auto pitch_or = PitchFromKey(octave_shift, key)) {
instrument.SetNoteOn(*pitch_or, intensity);
}
};
input_manager.SetKeyDownCallback(key_down_callback);
// Key up callback.
const auto key_up_callback = [&](const InputManager::Key& key) {
// Stop note.
if (const auto pitch_or = PitchFromKey(octave_shift, key)) {
instrument.SetNoteOff(*pitch_or);
}
};
input_manager.SetKeyUpCallback(key_up_callback);
// Start the demo.
ConsoleLog() << "Starting audio stream";
audio_output.Start();
ConsoleLog() << "Play the instrument using the keyboard keys:";
ConsoleLog() << " * Use ASDFFGHJK keys to play the white notes in an octave";
ConsoleLog() << " * Use WETYU keys to play the black notes in an octave";
ConsoleLog() << " * Use ZX keys to set the octave up and down";
ConsoleLog() << " * Use CV keys to to set the note intensity up and down";
while (!quit) {
input_manager.Update();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
// Stop the demo.
ConsoleLog() << "Stopping audio stream";
audio_output.Stop();
return 0;
}