19#include "definitions_cxx.hpp"
20#include "storage/audio/audio_file_vector.h"
21#include "storage/cluster/cluster.h"
22#include "storage/cluster/cluster_priority_queue.h"
27#include "RZA1/sdhi/inc/sd_async.h"
37enum class AlternateLoadDirStatus {
44char const*
const audioRecordingFolderNames[] = {
88class AudioFileManager {
95 AudioFile* getAudioFileFromFilename(
String& fileName,
bool mayReadCard, Error* error, FilePointer* filePointer,
96 AudioFileType type,
bool makeWaveTableWorkAtAllCosts =
false);
97 bool loadCluster(
Cluster& cluster, int32_t minNumReasonsAfter = 0);
108 void removeReasonFromCluster(
Cluster& cluster,
char const* errorCode,
bool deletingSong =
false);
141 bool ensureEnoughMemoryForOneMoreAudioFile();
145 Error setupAlternateAudioFilePath(
String& newPath, int32_t dirPathLength,
String& oldPath);
146 Error setupAlternateAudioFileDir(
String& newPath,
char const* rootDir,
const char* songFilenameWithoutExtension);
158 AudioRecordingFolder folder, uint32_t* getNumber,
const char* channelName,
160 void deleteAnyTempRecordedSamplesFromMemory();
161 void deleteUnusedAudioFileFromMemory(
AudioFile& audioFile, int32_t i);
162 void deleteUnusedAudioFileFromMemoryIndexUnknown(
AudioFile& audioFile);
163 bool tryToDeleteAudioFileFromMemoryIfItExists(
char const* filePath);
165 void thingBeginningLoading(ThingType newThingType);
166 void thingFinishedLoading();
168 void setCardRead() { cardReadOnce =
true; }
169 void setCardEjected() { cardEjected =
true; }
171 bool canLoadClusters()
const {
return !cardEjected && !cardDisabled; }
173 ClusterPriorityQueue loadingQueue;
175 Cluster* clusterBeingLoaded;
176 int32_t minNumReasonsForClusterBeingLoaded;
179 String alternateAudioFileLoadPath;
180 AlternateLoadDirStatus alternateLoadDirStatus = AlternateLoadDirStatus::NONE_SET;
181 ThingType thingTypeBeingLoaded = ThingType::NONE;
182 DIR alternateLoadDir;
184 std::array<int32_t, kNumAudioRecordingFolders> highestUsedAudioRecordingNumber;
185 std::bitset<kNumAudioRecordingFolders> highestUsedAudioRecordingNumberNeedsReChecking;
186 void firstCardRead();
201 Cluster*
volatile clusterBeingRead_ =
nullptr;
202 Priority priorityBeingRead_ = 0;
203 std::array<ClusterRead, kMaxClusterReads> clusterReads_;
208 [[nodiscard]]
bool isClusterBeingRead(
Cluster const* cluster,
const InterruptsDisabled&)
const;
211 friend bool sdAsyncNextRead(SdAsyncRead* read);
212 friend void sdAsyncReadDone(
void* user,
int error);
221 void finishClusterRead(
Cluster& cluster, int32_t error);
239 size_t numPrefetches_ = 0;
240 uint32_t prefetchGeneration_ = 0;
247 bool cardReadOnce{
false};
249 bool cardDisabled =
false;
251 uint32_t clusterSizeAtBoot{0};
253 void cardReinserted();
254 int32_t readBytes(
char* buffer, int32_t num, int32_t* byteIndexWithinCluster, Cluster** currentCluster,
255 uint32_t* currentClusterIndex, uint32_t fileSize, Sample* sample);
256 int32_t loadAiff(Sample* newSample, uint32_t fileSize, Cluster** currentCluster, uint32_t* currentClusterIndex);
257 int32_t loadWav(Sample* newSample, uint32_t fileSize, Cluster** currentCluster, uint32_t* currentClusterIndex);
Definition audio_file_manager.h:88
std::array< Prefetch, kMaxPrefetches > prefetches_
Oldest first.
Definition audio_file_manager.h:238
void releasePrefetch(size_t i)
Removes the reasons prefetches_[i] holds, and removes it from prefetches_, keeping the order.
Definition audio_file_manager.cpp:1631
volatile size_t clusterReadsEnd_
One past the newest. Only written by the SD interrupt.
Definition audio_file_manager.h:205
void releaseUnloadablePrefetches()
Lets go of prefetched Clusters that will never load because they're unloadable.
Definition audio_file_manager.cpp:1660
volatile size_t clusterReadsStart_
Oldest. Only written by the main thread.
Definition audio_file_manager.h:204
bool nextClusterRead(SdAsyncRead &read, const InterruptsDisabled &)
For the SD interrupt (see sd_async.h) - takes the next Cluster off the loading queue,...
Definition audio_file_manager.cpp:1357
void forgetClusterRead(Cluster const *cluster)
Makes sure the SD interrupt is done with cluster, and won't hand it back to finish loading,...
Definition audio_file_manager.cpp:1407
void releasePrefetchesForSample(Sample const *sample)
Lets go of every prefetched Cluster of sample, e.g. because what was going to play it has stopped.
Definition audio_file_manager.cpp:1656
void releasePrefetchesIf(auto shouldRelease)
Releases every prefetch shouldRelease returns true for.
Definition audio_file_manager.cpp:1641
void recycleUnwantedCluster(Cluster &cluster)
Recycles a Cluster whose last reason was removed while the SD interrupt was reading it.
Definition audio_file_manager.cpp:1474
uint32_t getPrefetchGeneration() const
Changes whenever a Cluster is added to or released from the prefetch list. While it and what a caller...
Definition audio_file_manager.h:125
void finishClusterReads()
Finishes off Clusters the SD interrupt has read.
Definition audio_file_manager.cpp:1426
void claimPrefetch(Cluster const *cluster)
Lets go of a prefetched Cluster, now that something that's going to play it holds its own reason on i...
Definition audio_file_manager.cpp:1652
static constexpr size_t kMaxPrefetches
Most Clusters held at once. Bounds the RAM they can pin, and keeps them from crowding out Clusters so...
Definition audio_file_manager.h:236
bool prefetchCluster(Sample *sample, int32_t clusterIndex)
Holds a Cluster that's likely to be requested soon, loading it first if need be.
Definition audio_file_manager.cpp:1588
void loadAnyEnqueuedClusters(bool mayProcessUserActionsBetween=false)
Finishes off Clusters the SD interrupt has read, and gets it reading any that are enqueued.
Definition audio_file_manager.cpp:1325
void clusterReadDone(int error, const InterruptsDisabled &)
For the SD interrupt - it's finished reading clusterBeingRead_.
Definition audio_file_manager.cpp:1385
static constexpr size_t kMaxClusterReads
Most reads finishClusterReads() can fall behind by. The SD interrupt stops reading once there are thi...
Definition audio_file_manager.h:197
void finishLoadingCluster(Cluster &cluster)
Converts cluster's data and swaps the bytes that overhang its neighbours, now that it's been read.
Definition audio_file_manager.cpp:1111
bool hasClusterLoadsPending()
Whether any Clusters are still waiting to load, or being loaded.
Definition audio_file_manager.cpp:1687
bool loadEnqueuedClusterNow(Cluster &cluster)
Loads cluster right now if it's in the loading queue, without loading what's ahead of it....
Definition audio_file_manager.cpp:1486
Error getUnusedAudioRecordingFilePath(String &filePath, String *tempFilePathForRecording, AudioRecordingFolder folder, uint32_t *getNumber, const char *channelName, String *songName)
Definition audio_file_manager.cpp:270
bool loadingQueueHasAnySampleStarts()
Whether any Samples' first Clusters, which let them start instantly, are still waiting to load.
Definition audio_file_manager.cpp:1673
void releaseAllPrefetches()
Lets go of every prefetched Cluster and its Sample, loaded or not.
Definition audio_file_manager.cpp:1667
Definition audio_file_vector.h:24
Definition audio_file.h:26
Definition sample_cache.h:25
Definition sample_recorder.h:53
A Cluster the SD interrupt has finished reading, waiting for finishClusterReads().
Definition audio_file_manager.h:190
Cluster * cluster
nullptr if forgetClusterRead() was called for it
Definition audio_file_manager.h:191
int32_t error
SD_OK, or an SD_ERR_* code.
Definition audio_file_manager.h:193
A Cluster that prefetchCluster() is holding. We hold one reason on it and one on its Sample,...
Definition audio_file_manager.h:229