-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLabSynthToy.cpp
238 lines (203 loc) · 8.55 KB
/
LabSynthToy.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
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
#include "LabSynthToy.h"
#include "TinySoundFontNode.h"
#include "LabSoundTemplateNode.h"
#include "PocketModNode.h"
#define TML_IMPLEMENTATION
#include "TinySoundFont/tml.h"
// SPDX-License-Identifier: BSD-2-Clause
// Copyright (C) 2020, The LabSound Authors. All rights reserved.
#if defined(_MSC_VER)
#if !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS
#endif
#if !defined(NOMINMAX)
#define NOMINMAX
#endif
#endif
#include "LabSound/LabSound.h"
#include <chrono>
#include <string>
#include <thread>
#include <vector>
using namespace lab;
// Returns input, output
inline std::pair<AudioStreamConfig, AudioStreamConfig> GetDefaultAudioDeviceConfiguration(const bool with_input = false)
{
AudioStreamConfig inputConfig;
AudioStreamConfig outputConfig;
const std::vector<AudioDeviceInfo> audioDevices = lab::MakeAudioDeviceList();
const AudioDeviceIndex default_output_device = lab::GetDefaultOutputAudioDeviceIndex();
const AudioDeviceIndex default_input_device = lab::GetDefaultInputAudioDeviceIndex();
AudioDeviceInfo defaultOutputInfo, defaultInputInfo;
for (auto& info : audioDevices)
{
if (info.index == default_output_device.index) defaultOutputInfo = info;
else if (info.index == default_input_device.index) defaultInputInfo = info;
}
if (defaultOutputInfo.index != -1)
{
outputConfig.device_index = defaultOutputInfo.index;
outputConfig.desired_channels = std::min(uint32_t(2), defaultOutputInfo.num_output_channels);
outputConfig.desired_samplerate = defaultOutputInfo.nominal_samplerate;
}
if (with_input)
{
if (defaultInputInfo.index != -1)
{
inputConfig.device_index = defaultInputInfo.index;
inputConfig.desired_channels = std::min(uint32_t(1), defaultInputInfo.num_input_channels);
inputConfig.desired_samplerate = defaultInputInfo.nominal_samplerate;
}
else
{
throw std::invalid_argument("the default audio input device was requested but none were found");
}
}
return { inputConfig, outputConfig };
}
std::shared_ptr<AudioBus> MakeBusFromSampleFile(char const* const name, int argc, char** argv)
{
std::string path_prefix = synth_toy_asset_base;
const std::string path = path_prefix + name;
std::shared_ptr<AudioBus> bus = MakeBusFromFile(path, false);
if (!bus)
throw std::runtime_error("couldn't open " + path);
return bus;
}
void tsf_two_notes(lab::AudioContext& ac)
{
std::shared_ptr<TinySoundFontNode> tsfNode(new TinySoundFontNode(ac));
ac.connect(ac.device(), tsfNode, 0, 0);
tsfNode->noteOn(0.f, 0, 48, 1.0f); //C2
tsfNode->noteOn(0.f, 0, 52, 1.0f); //E2
std::this_thread::sleep_for(std::chrono::seconds(1));
tsfNode->noteOff(0.f, 0, 48);
tsfNode->noteOn(0.f, 0, 55, 1.0f);
std::this_thread::sleep_for(std::chrono::seconds(1));
tsfNode->allNotesOff(0.f);
}
void tsf_test_sf2(lab::AudioContext& ac)
{
std::string sf2_file = std::string(synth_toy_asset_base) + "florestan-subset.sf2";
std::shared_ptr<TinySoundFontNode> tsfNode(new TinySoundFontNode(ac));
tsfNode->load_sf2(sf2_file.c_str());
ac.connect(ac.device(), tsfNode, 0, 0);
int i, Notes[7] = { 48, 50, 52, 53, 55, 57, 59 };
// Loop through all the presets in the loaded SoundFont
int preset_count = tsfNode->presetCount();
for (i = 0; i < preset_count; i++)
{
//printf("Play note %d with preset #%d '%s'\n", Notes[i % 7], i, tsf_get_presetname(g_TinySoundFont, i));
tsfNode->noteOff(0.f, i - 1, Notes[(i - 1) % 7]);
tsfNode->noteOn(0.f, i, Notes[i % 7], 1.0f);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
tsfNode->allNotesOff(0.f);
}
void tsf_test_tml(lab::AudioContext& ac)
{
std::string midi_file = std::string(synth_toy_asset_base) + "venture.mid";
tml_message* TinyMidiLoader = tml_load_filename(midi_file.c_str());
if (!TinyMidiLoader)
{
printf("Couldn't open %s\n", midi_file.c_str());
return;
}
std::string sf2_file = std::string(synth_toy_asset_base) + "florestan-subset.sf2";
std::shared_ptr<TinySoundFontNode> tsfNode(new TinySoundFontNode(ac));
tsfNode->load_sf2(sf2_file.c_str());
ac.connect(ac.device(), tsfNode, 0, 0);
double g_Msec = 0; //current playback time
tml_message* curr_MidiMessage = TinyMidiLoader; //next message to be played
auto start = std::chrono::system_clock::now();
while (curr_MidiMessage != NULL)
{
std::chrono::duration<double> elapsed_dur = std::chrono::system_clock::now() - start;
double elapsed_ms = elapsed_dur.count() * 1e3;
double until = elapsed_ms + 60000.; // queue up anything within the next second
while (curr_MidiMessage && curr_MidiMessage->time <= until)
{
double when = (float(curr_MidiMessage->time) - elapsed_ms) * 1e-3;
//printf("%f\n", when);
switch (curr_MidiMessage->type)
{
case TML_PROGRAM_CHANGE: //channel program (preset) change (special handling for 10th MIDI channel with drums)
tsfNode->channelSetPreset((float)when, curr_MidiMessage->channel, curr_MidiMessage->program, curr_MidiMessage->channel == 9);
break;
case TML_NOTE_ON: //play a note
tsfNode->channelNoteOn((float)when, curr_MidiMessage->channel, curr_MidiMessage->key, curr_MidiMessage->velocity / 127.0f);
break;
case TML_NOTE_OFF: //stop a note
tsfNode->channelNoteOff((float)when, curr_MidiMessage->channel, curr_MidiMessage->key);
break;
case TML_PITCH_BEND: //pitch wheel modification
tsfNode->channelSetPitchWheel((float)when, curr_MidiMessage->channel, curr_MidiMessage->pitch_bend);
break;
case TML_CONTROL_CHANGE: //MIDI controller messages
tsfNode->channelMidiControl((float)when, curr_MidiMessage->channel, curr_MidiMessage->control, curr_MidiMessage->control_value);
break;
}
curr_MidiMessage = curr_MidiMessage->next;
}
std::this_thread::sleep_for(std::chrono::milliseconds(60000));
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(10)); // wait for the last notes
tml_free(TinyMidiLoader);
}
void test_template_node(lab::AudioContext& ac)
{
// schedule some events for the future, they should produce a tick a second for 20 seconds.
std::shared_ptr<LabSoundTemplateNode> timing(new LabSoundTemplateNode(ac));
ac.connect(ac.device(), timing, 0, 0);
int id[20] = {
1,2,15,4,5,6,11,16,7,8,3,12,10,17,18,13,14,9,19,20
};
for (int i = 0; i < 20; ++i)
timing->realtimeEvent(float(id[i]), i);
std::this_thread::sleep_for(std::chrono::seconds(20));
}
void test_predictive_timing(lab::AudioContext& ac)
{
double start = ac.predictedCurrentTime();
double now = start;
auto reference_start = std::chrono::high_resolution_clock::now();
auto reference_now = reference_start;
std::chrono::duration<double> elapsed = reference_now - reference_start;
while (elapsed.count() < 180)
{
std::this_thread::sleep_for(std::chrono::milliseconds((rand() & 0xff) * 10));
now = ac.predictedCurrentTime();
reference_now = std::chrono::high_resolution_clock::now();
elapsed = reference_now - reference_start;
printf("chrono: %g : labsound: %g delta: %g\n", elapsed.count(), now - start, elapsed.count() - now + start);
}
}
void test_pocketmod(lab::AudioContext& ac)
{
std::shared_ptr<PocketModNode> pocketmod(new PocketModNode(ac));
ac.connect(ac.device(), pocketmod, 0, 0);
std::string path = synth_toy_asset_base;
path += "/elysium.mod";
pocketmod->loadMOD(path.c_str());
std::this_thread::sleep_for(std::chrono::seconds(180));
}
int main(int argc, char *argv[]) try
{
std::unique_ptr<lab::AudioContext> context;
const auto defaultAudioDeviceConfigurations = GetDefaultAudioDeviceConfiguration();
context = lab::MakeRealtimeAudioContext(defaultAudioDeviceConfigurations.second, defaultAudioDeviceConfigurations.first);
lab::AudioContext& ac = *context.get();
//tsf_two_notes(ac);
//tsf_test_sf2(ac);
//tsf_test_tml(ac);
//test_template_node(ac);
//test_predictive_timing(ac);
test_pocketmod(ac);
return EXIT_SUCCESS;
}
catch (const std::exception & e)
{
std::cerr << "unhandled fatal exception: " << e.what() << std::endl;
return EXIT_FAILURE;
}