-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_ram_manager.cpp
More file actions
244 lines (207 loc) · 7.19 KB
/
Copy pathsample_ram_manager.cpp
File metadata and controls
244 lines (207 loc) · 7.19 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
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
#include "sample_ram_manager.h"
#include <SD.h>
#include <esp_heap_caps.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <stdlib.h>
#include <string.h>
namespace {
// Reads larger than the 4 KiB stdio buffer go straight to FATFS as
// multi-sector transfers, which matters at the 4 MHz fallback SD clock.
constexpr size_t kReadChunkBytes = 32 * 1024;
// PSRAM left for everything else (JSON documents, FATFS state, buffers).
constexpr uint32_t kPsramReserveBytes = 512UL * 1024UL;
struct LoadedEntry {
String path;
uint32_t dataBytes = 0;
uint32_t poolOffset = 0;
bool valid = false;
bool head = false; // Only the start of a streamed sample.
};
uint8_t *gPool = nullptr;
uint32_t gPoolCapacity = 0;
LoadedEntry gLoadedEntries[SettingsStore::SamplerSettings::kMaxAssignments];
bool gPoolBudgetLocked = false;
uint32_t gFixedPoolBudget = 0;
void clearLoadedEntries() {
for (int i = 0; i < SettingsStore::SamplerSettings::kMaxAssignments; i++) {
gLoadedEntries[i].path = "";
gLoadedEntries[i].dataBytes = 0;
gLoadedEntries[i].poolOffset = 0;
gLoadedEntries[i].valid = false;
gLoadedEntries[i].head = false;
}
}
void freePool() {
if (gPool) {
free(gPool);
gPool = nullptr;
}
gPoolCapacity = 0;
}
bool allocatePool(uint32_t budgetBytes) {
if (budgetBytes == 0) return true;
if (gPool && gPoolCapacity == budgetBytes) return true;
gPool = static_cast<uint8_t *>(heap_caps_malloc(budgetBytes, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
if (!gPool) {
gPool = static_cast<uint8_t *>(malloc(budgetBytes));
}
if (!gPool) {
return false;
}
gPoolCapacity = budgetBytes;
return true;
}
int findLoadedEntryByPath(const String &path, bool head) {
for (int i = 0; i < SettingsStore::SamplerSettings::kMaxAssignments; i++) {
if (gLoadedEntries[i].valid && gLoadedEntries[i].head == head && gLoadedEntries[i].path == path) {
return i;
}
}
return -1;
}
bool loadedDataAt(int idx, SampleRamManager::LoadedSampleData &data) {
data = SampleRamManager::LoadedSampleData{};
if (idx < 0 || !gPool) return false;
const uint32_t offset = gLoadedEntries[idx].poolOffset;
const uint32_t bytes = gLoadedEntries[idx].dataBytes;
if (bytes == 0 || offset > gPoolCapacity || (gPoolCapacity - offset) < bytes) {
return false;
}
data.data = gPool + offset;
data.dataBytes = bytes;
return true;
}
int findFreeLoadedEntrySlot() {
for (int i = 0; i < SettingsStore::SamplerSettings::kMaxAssignments; i++) {
if (!gLoadedEntries[i].valid) return i;
}
return -1;
}
bool readFileRangeToBuffer(const String &path, uint32_t offset, uint32_t size, uint8_t *dst) {
File file = SD.open(path, FILE_READ);
if (!file) return false;
if (!file.seek(offset)) {
file.close();
return false;
}
uint32_t totalRead = 0;
while (totalRead < size) {
const uint32_t remaining = size - totalRead;
const size_t toRead = (remaining < kReadChunkBytes) ? remaining : kReadChunkBytes;
const int readNow = file.read(dst + totalRead, toRead);
if (readNow <= 0) {
file.close();
return false;
}
totalRead += static_cast<uint32_t>(readNow);
// SPI SD transfers busy-wait. Loading several MiB would otherwise keep
// this core from IDLE long enough to trip the task watchdog.
vTaskDelay(1);
}
file.close();
return true;
}
} // namespace
namespace SampleRamManager {
uint32_t budgetBytes() {
if (!gPoolBudgetLocked) {
// The pool is allocated once and never resized, so size it from PSRAM
// available at the first preparation rather than a fixed setting.
const size_t largest = heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
gFixedPoolBudget = largest > kPsramReserveBytes
? static_cast<uint32_t>(largest - kPsramReserveBytes)
: SettingsStore::SamplerSettings::kDefaultSampleRamBudgetBytes;
gPoolBudgetLocked = true;
}
return gFixedPoolBudget;
}
bool prepare(const SettingsStore::SamplerSettings &settings,
const SampleClassifier::ClassificationReport &classification,
LoadReport &report) {
report = LoadReport{};
report.budgetBytes = settings.sampleRamBudgetBytes;
report.effectiveBudgetBytes = budgetBytes();
if (settings.sampleRamBudgetBytes != gFixedPoolBudget) {
report.fixedBudgetMismatch = true;
}
for (int i = 0; i < classification.itemCount; i++) {
const SampleClassifier::AssignedSampleClassification &item = classification.items[i];
if (item.mode == SampleClassifier::StorageMode::Ram) {
report.requestedRamCount++;
} else if (item.mode == SampleClassifier::StorageMode::Stream && item.headBytes > 0) {
report.requestedHeadCount++;
}
}
if (report.requestedRamCount == 0 && report.requestedHeadCount == 0) {
clearLoadedEntries();
report.allocatedBytes = gPoolCapacity;
return true;
}
clearLoadedEntries();
if (!allocatePool(gFixedPoolBudget)) {
report.allocatedBytes = 0;
report.fallbackToStreamCount = report.requestedRamCount;
return false;
}
report.allocatedBytes = gPoolCapacity;
uint32_t used = 0;
for (int i = 0; i < classification.itemCount; i++) {
const SampleClassifier::AssignedSampleClassification &item = classification.items[i];
const bool head = item.mode == SampleClassifier::StorageMode::Stream && item.headBytes > 0;
if (item.mode != SampleClassifier::StorageMode::Ram && !head) {
continue;
}
const uint32_t bytes = head ? item.headBytes : item.dataBytes;
if (findLoadedEntryByPath(item.path, head) >= 0) {
if (head) report.loadedHeadCount++;
else report.loadedRamCount++;
continue;
}
// A missing head only delays that sample's start; it still streams.
if (bytes == 0 || bytes > gPoolCapacity || (gPoolCapacity - used) < bytes) {
if (!head) report.fallbackToStreamCount++;
continue;
}
if (!readFileRangeToBuffer(item.path, item.dataOffset, bytes, gPool + used)) {
report.readErrorCount++;
if (!head) report.fallbackToStreamCount++;
continue;
}
const int slot = findFreeLoadedEntrySlot();
if (slot < 0) {
if (!head) report.fallbackToStreamCount++;
continue;
}
gLoadedEntries[slot].path = item.path;
gLoadedEntries[slot].dataBytes = bytes;
gLoadedEntries[slot].poolOffset = used;
gLoadedEntries[slot].valid = true;
gLoadedEntries[slot].head = head;
used += bytes;
if (head) report.loadedHeadCount++;
else report.loadedRamCount++;
}
report.usedBytes = used;
return true;
}
void release() {
clearLoadedEntries();
freePool();
gPoolBudgetLocked = false;
gFixedPoolBudget = 0;
}
bool getLoadedSampleByPath(const String &path, LoadedSampleInfo &info) {
const int idx = findLoadedEntryByPath(path, false);
if (idx < 0) return false;
info.dataBytes = gLoadedEntries[idx].dataBytes;
info.poolOffset = gLoadedEntries[idx].poolOffset;
return true;
}
bool getLoadedSampleDataByPath(const String &path, LoadedSampleData &data) {
return loadedDataAt(findLoadedEntryByPath(path, false), data);
}
bool getLoadedHeadByPath(const String &path, LoadedSampleData &data) {
return loadedDataAt(findLoadedEntryByPath(path, true), data);
}
} // namespace SampleRamManager