-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiSample.cpp
executable file
·301 lines (266 loc) · 7.67 KB
/
PiSample.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "PiSample.h"
#include "Strings.h"
#include "Log.h"
#include <iostream>
#include <utility>
#include <stdexcept>
using namespace atom;
using namespace ps;
using namespace std;
namespace {
auto logger = Log("UI");
}
void PiSample::finalizeSample(optional<Sample>& sample, std::filesystem::path file)
{
auto& s = sample.value();
s.PlayerIndex = _player.load(file);
if (s.PlayerIndex < 0) {
logger.warn("Ignoring sample {} - player could not load it", s.Name);
sample = nullopt;
}
}
void PiSample::loadBanks(string_view fileName)
{
ifstream in(static_cast<string>(fileName));
if (not in) {
logger.throw_("Could not open sample files '{}' for reading. You can "
"pass an empty file if you do not wish to load any.", fileName);
}
optional<Sample>* currentSample = nullptr;
vector<string_view> parts;
string rawLine;
int index = 0;
string currentFile;
int sampleCount = 0;
int currentBank = 0;
while (getline(in, rawLine)) {
++index;
string_view line = trim(rawLine);
if (line.empty() || line[0] == '#') {
continue;
}
try { // to append some common info to any exception encountered.
if (line[0] == '[') {
if (currentSample != nullptr) {
finalizeSample(*currentSample, currentFile);
}
// new sample
if (line.back() != ']') {
logger.throw_("Malformed line: missing closing ']'");
}
line = line.substr(1, line.size() - 2);
split(parts, line, '.');
if (parts.size() != 2 or
parts[0].substr(0, 4) != "Bank" or
parts[1].substr(0, 3) != "Pad")
{
logger.throw_("[Malformed line: Must be [BankX.PadY]");
}
parts[0] = parts[0].substr(4); // remove "Bank"
parts[1] = parts[1].substr(3); // remove "Pad"
int bankNo = -1;
try {
bankNo = svtoi(parts[0]);
if (bankNo < 1) {
throw out_of_range(">0");
}
}
catch (...) {
logger.throw_("Could not convert {} to a valid bank number (> 0)",
parts[0]);
}
if (bankNo != currentBank and bankNo != currentBank + 1) {
logger.throw_("Bank is {} but last was {} - expecting the same "
"value or {}. Banks must be given in order in the file.",
bankNo, currentBank, currentBank + 1);
}
currentBank = bankNo;
if (_banks.size() < static_cast<unsigned>(bankNo)) {
_banks.resize(bankNo);
// filled with unset Sample for pads.
}
int padNo = -1;
try {
padNo = svtoi(parts[1]);
if (padNo < 1 or padNo > 16) {
throw out_of_range(">0 && <17");
}
}
catch (...) {
logger.throw_("Could not convert {} to a valid pad number "
"(1-16)", parts[1]);
}
if (_banks.back()[padNo - 1].has_value()) {
logger.throw_("Bank{}.Pad{} is present twice in the config",
bankNo, padNo);
}
currentSample = & _banks.back()[padNo - 1];
*currentSample = Sample(); // may be reset later in case on an error.
++sampleCount;
}
else {
if (currentSample == nullptr) {
logger.throw_("A property key=value was given before "
"[BankY.PadY]");
}
split(parts, line, '=');
if (parts.size() != 2 or parts[0].empty() or parts[1].empty()) {
logger.throw_("Expecting key=value but found {}. No '=' "
"allowed in the value.", line);
}
// TODO: check for repeated keys.
if (parts[0] == "Name") {
currentSample->value().Name = parts[1];
}
else if (parts[0] == "File") {
currentFile = parts[1];
}
else if (parts[0] == "Color") {
if (parts[1][0] != '#') { // note we checked it’s not empty above
logger.throw_("At line {} expecting an hex color formatted as "
"'#123456'", index);
}
currentSample->value().Color = atom::Color::fromString(parts[1]);
}
}
}
catch (const exception& ex) {
logger.throw_("samples file {}, line {}: {}", fileName, index, ex.what());
}
}
if (currentSample == nullptr) {
logger.info("No samples found in samples line {}", fileName);
return;
}
finalizeSample(*currentSample, currentFile);
logger.info("Loaded {} banks and {} samples, read {} lines in {}",
currentBank, sampleCount, index, fileName);
}
PiSample::PiSample(
const unordered_map<string, Argument>& args,
Device& device,
Pads& pads,
Recorder& recorder,
Player& player)
: PadsAccess(pads)
, _device(device)
, _recorder(recorder)
, _player(player)
{
loadBanks(*args.find("samples")->second.Value);
_currentView = 1;
cycleView(false); // activate ourselves as the pads accessor.
}
PiSample::~PiSample()
{
_device.sendControl(atom::switchButton(atom::Buttons::Stop, false));
_device.sendControl(atom::switchButton(atom::Buttons::Shift, false));
}
void PiSample::event(atom::Note n)
{
cout << "Note " << (n.OnOff ? "on" : "off")
<< ", channel: " << (int)n.Channel
<< ", note: " << (int)n.Note
<< ", velocity: " << (int)n.Velocity
<< "\n";
}
void PiSample::event(atom::Control c)
{
switch (static_cast<Buttons>(c.Param)) {
case Buttons::Record:
if (c.Value == 0x00) {
_recorder.toggle();
}
break;
case Buttons::Shift:
_shiftPressed = (c.Value != 0);
_device.sendControl(switchButton(Buttons::Shift, _shiftPressed));
_device.sendControl(switchButton(Buttons::Stop, _shiftPressed));
break;
case Buttons::Stop:
if (_shiftPressed) {
_willShutdown = true;
}
break;
case Buttons::Up:
if (c.Value == 0) { // release
cycleView(true);
}
break;
case Buttons::Down:
if (c.Value == 0) { // release
cycleView(false);
}
break;
default:
cout << "Control: "
<< ", channel: 0" // TODO : for now always 0, most likely need
<< ", param: " << (int)c.Param // something not an ATOM
<< ", value: " << (int)c.Value
<< '\n';
break;
}
}
void PiSample::cycleView(bool next)
{
if (next) {
_currentView = (_currentView + 1) % _views.size();
}
else {
_currentView = (_currentView - 1 + _views.size()) % _views.size();
}
// First display where we are going, before actually changing anything
this->receiveAccess();
pads().reset();
pads().setPad(
atom::Pad::One + _currentView,
atom::PadMode::On,
_viewColors[_currentView]
);
_viewAnimTimeout = c::milliseconds(500) + c::system_clock::now();
}
void PiSample::poll()
{
if (_viewAnimTimeout < c::system_clock::now()) {
_viewAnimTimeout = _viewAnimTimeout.max();
_views[_currentView]->receiveAccess();
}
if (not PadsAccess::isAccessing()) {
return;
}
if (_banks.size() == 0) {
return;
}
}
void PiSample::onAccess()
{
if (_banks.size() == 0) {
// if we don’t have any sample, play an animation
pads().startPlaying(caterpillar(), true);
return;
}
std::cout << "access" << std::endl;
bool allOff = true;
for (unsigned i = 0; i < _banks[_currentBank].size(); ++i) {
auto& p = _banks[_currentBank][i];
if (p.has_value()) {
std::cout << "coin " << i << std::endl;
pads().setPad(
atom::Pad::One + i,
atom::PadMode::On,
p->Color
);
allOff = false;
}
else {
pads().setPad(
atom::Pad::One + i,
atom::PadMode::Off,
atom::Color{}
);
}
}
if (allOff) {
pads().startPlaying(caterpillar(), true);
}
}