Repository navigation
Expand file tree
/
Copy pathpersistent.hh
More file actions
166 lines (142 loc) · 5.28 KB
/
Copy pathpersistent.hh
File metadata and controls
166 lines (142 loc) · 5.28 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
// Copyright 2016 Matthias Puech.
//
// Author: Matthias Puech <matthias.puech@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// See http://creativecommons.org/licenses/MIT/ for more information.
//
// -----------------------------------------------------------------------------
//
// Persistent data
#ifndef PERSISTENT_H_
#define PERSISTENT_H_
#include "parameters.hh"
#include "resources.h"
#include "stmlib/system/storage.h"
const int kNumSlots = 6 * 4; // 6 buttons, 4 banks
struct CalibrationData {
float offset[4];
};
class Persistent
{
public:
struct Data {
uint8_t velocity_parameter;
uint8_t current_bank;
uint8_t panning_mode;
uint8_t sequencer_mode;
uint8_t current_slot;
uint8_t repeat;
uint8_t sync;
uint8_t padding[1];
CalibrationData calibration_data;
};
void Init(size_t buffer_size) {
if (!settings_storage_.ParsimoniousLoad(&data_, &settings_token_)) {
for (size_t i=0; i<4; i++) {
data_.calibration_data.offset[i] = 0.5f;
}
// default settings:
data_.velocity_parameter = 2;
data_.current_bank = 0;
data_.panning_mode = 1;
data_.sequencer_mode = 0;
data_.current_slot = 0;
data_.repeat = 0;
data_.sync = 0;
SaveData();
}
// sanitize settings
CONSTRAIN(data_.velocity_parameter, 0, 4);
CONSTRAIN(data_.current_bank, 0, 3);
CONSTRAIN(data_.panning_mode, 0, 2);
CONSTRAIN(data_.sequencer_mode, 0, 1);
CONSTRAIN(data_.current_slot, 0, 6 * 4);
if (data_.repeat != 1) data_.repeat = 0;
if (data_.sync != 1) data_.sync = 0;
if (!bank0_.ParsimoniousLoad(&slots_[6 * 0], 6 * sizeof(Slot), &token_[0]) ||
!bank1_.ParsimoniousLoad(&slots_[6 * 1], 6 * sizeof(Slot), &token_[1]) ||
!bank2_.ParsimoniousLoad(&slots_[6 * 2], 6 * sizeof(Slot), &token_[2]) ||
!bank3_.ParsimoniousLoad(&slots_[6 * 3], 6 * sizeof(Slot), &token_[3])) {
for (int i=0; i<4; i++)
ResetBank(i);
}
// sanitize slots
for (int slot=0; slot<kNumSlots; slot++) {
CONSTRAIN(slots_[slot].size, 0, kMaxTaps);
for (int tap=0; tap<slots_[slot].size; tap++) {
TapParameters *t = &slots_[slot].taps[tap];
CONSTRAIN(t->velocity, 0.0f, 1.0f);
CONSTRAIN(t->time, 0.0f, buffer_size);
CONSTRAIN(t->velocity_type, VELOCITY_AMP, VELOCITY_BP);
CONSTRAIN(t->panning, 0.0f, 1.0f);
}
}
}
void SaveData() {
settings_storage_.ParsimoniousSave(data_, &settings_token_);
}
Data* mutable_data() { return &data_; }
void SaveBank(int bank) {
if (bank == 0) bank0_.ParsimoniousSave(&slots_[6 * 0], 6 * sizeof(Slot), &token_[0]);
if (bank == 1) bank1_.ParsimoniousSave(&slots_[6 * 1], 6 * sizeof(Slot), &token_[1]);
if (bank == 2) bank2_.ParsimoniousSave(&slots_[6 * 2], 6 * sizeof(Slot), &token_[2]);
if (bank == 3) bank3_.ParsimoniousSave(&slots_[6 * 3], 6 * sizeof(Slot), &token_[3]);
}
void SaveSlot(int slot_nr) {
SaveBank(slot_nr / 6);
}
void ResetSlot(int slot) {
slots_[slot].size = lut_preset_sizes[slot];
for (int tap=0; tap<slots_[slot].size; tap++) {
int index = tap + kMaxTaps * slot;
TapParameters *t = &slots_[slot].taps[tap];
t->time = lut_preset_times[index];
t->velocity = lut_preset_velos[index];
t->velocity_type = static_cast<VelocityType>(lut_preset_types[index]);
t->panning = lut_preset_pans[index];
}
}
void ResetBank(int bank) {
for(int slot=bank*6; slot<(bank+1)*6; slot++)
ResetSlot(slot);
SaveBank(bank);
}
uint8_t current_bank() { return data_.current_bank; }
uint8_t current_slot() { return data_.current_slot; }
uint8_t velocity_parameter() { return data_.velocity_parameter; };
uint8_t panning_mode() { return data_.panning_mode; };
uint8_t sequencer_mode() { return data_.sequencer_mode; };
uint8_t repeat() { return data_.repeat; };
uint8_t sync() { return data_.sync; };
void ResetCurrentBank() { ResetBank(current_bank()); }
Slot* mutable_slot(int nr) { return &slots_[nr]; }
private:
Data data_;
uint16_t settings_token_;
stmlib::Storage<7> settings_storage_;
Slot slots_[kNumSlots];
stmlib::Storage<8> bank0_;
stmlib::Storage<9> bank1_;
stmlib::Storage<10> bank2_;
stmlib::Storage<11> bank3_;
uint16_t token_[4];
};
#endif