Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
AudioFileManager Class Reference
Collaboration diagram for AudioFileManager:

Classes

struct  ClusterRead
 A Cluster the SD interrupt has finished reading, waiting for finishClusterReads(). More...
struct  Prefetch
 A Cluster that prefetchCluster() is holding. We hold one reason on it and one on its Sample, so the Sample can't be deleted while we still point at it. More...

Public Member Functions

void init ()
AudioFile * getAudioFileFromFilename (String &fileName, bool mayReadCard, Error *error, FilePointer *filePointer, AudioFileType type, bool makeWaveTableWorkAtAllCosts=false)
bool loadCluster (Cluster &cluster, int32_t minNumReasonsAfter=0)
void loadAnyEnqueuedClusters (bool mayProcessUserActionsBetween=false)
 Finishes off Clusters the SD interrupt has read, and gets it reading any that are enqueued.
bool loadEnqueuedClusterNow (Cluster &cluster)
 Loads cluster right now if it's in the loading queue, without loading what's ahead of it. If the SD interrupt's already reading it, waits for that instead.
void removeReasonFromCluster (Cluster &cluster, char const *errorCode, bool deletingSong=false)
bool prefetchCluster (Sample *sample, int32_t clusterIndex)
 Holds a Cluster that's likely to be requested soon, loading it first if need be.
uint32_t getPrefetchGeneration () const
 Changes whenever a Cluster is added to or released from the prefetch list. While it and what a caller would ask for are unchanged, asking again won't do anything.
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 it.
void releasePrefetchesForSample (Sample const *sample)
 Lets go of every prefetched Cluster of sample, e.g. because what was going to play it has stopped.
void releaseUnloadablePrefetches ()
 Lets go of prefetched Clusters that will never load because they're unloadable.
void releaseAllPrefetches ()
 Lets go of every prefetched Cluster and its Sample, loaded or not.
bool ensureEnoughMemoryForOneMoreAudioFile ()
void slowRoutine ()
Error setupAlternateAudioFilePath (String &newPath, int32_t dirPathLength, String &oldPath)
Error setupAlternateAudioFileDir (String &newPath, char const *rootDir, const char *songFilenameWithoutExtension)
bool loadingQueueHasAnySampleStarts ()
 Whether any Samples' first Clusters, which let them start instantly, are still waiting to load.
bool hasClusterLoadsPending ()
 Whether any Clusters are still waiting to load, or being loaded.
void forgetClusterRead (Cluster const *cluster)
 Makes sure the SD interrupt is done with cluster, and won't hand it back to finish loading, so it can be deleted. Waits if it's being read right now. ~Cluster() calls this.
Error getUnusedAudioRecordingFilePath (String &filePath, String *tempFilePathForRecording, AudioRecordingFolder folder, uint32_t *getNumber, const char *channelName, String *songName)
void deleteAnyTempRecordedSamplesFromMemory ()
void deleteUnusedAudioFileFromMemory (AudioFile &audioFile, int32_t i)
void deleteUnusedAudioFileFromMemoryIndexUnknown (AudioFile &audioFile)
bool tryToDeleteAudioFileFromMemoryIfItExists (char const *filePath)
void thingBeginningLoading (ThingType newThingType)
void thingFinishedLoading ()
void setCardRead ()
void setCardEjected ()
bool canLoadClusters () const
void firstCardRead ()

Public Attributes

AudioFileVector audioFiles
ClusterPriorityQueue loadingQueue
Cluster * clusterBeingLoaded
int32_t minNumReasonsForClusterBeingLoaded
String alternateAudioFileLoadPath
AlternateLoadDirStatus alternateLoadDirStatus = AlternateLoadDirStatus::NONE_SET
ThingType thingTypeBeingLoaded = ThingType::NONE
DIR alternateLoadDir
std::array< int32_t, kNumAudioRecordingFolders > highestUsedAudioRecordingNumber
std::bitset< kNumAudioRecordingFolders > highestUsedAudioRecordingNumberNeedsReChecking

Private Member Functions

size_t numClusterReads () const
bool isClusterBeingRead (Cluster const *cluster, const InterruptsDisabled &) const
bool nextClusterRead (SdAsyncRead &read, const InterruptsDisabled &)
 For the SD interrupt (see sd_async.h) - takes the next Cluster off the loading queue, to read.
void clusterReadDone (int error, const InterruptsDisabled &)
 For the SD interrupt - it's finished reading clusterBeingRead_.
void finishClusterReads ()
 Finishes off Clusters the SD interrupt has read.
void finishClusterRead (Cluster &cluster, int32_t error)
void finishLoadingCluster (Cluster &cluster)
 Converts cluster's data and swaps the bytes that overhang its neighbours, now that it's been read.
void recycleUnwantedCluster (Cluster &cluster)
 Recycles a Cluster whose last reason was removed while the SD interrupt was reading it.
void releasePrefetch (size_t i)
 Removes the reasons prefetches_[i] holds, and removes it from prefetches_, keeping the order.
void releasePrefetchesIf (auto shouldRelease)
 Releases every prefetch shouldRelease returns true for.
void cardReinserted ()
int32_t readBytes (char *buffer, int32_t num, int32_t *byteIndexWithinCluster, Cluster **currentCluster, uint32_t *currentClusterIndex, uint32_t fileSize, Sample *sample)
int32_t loadAiff (Sample *newSample, uint32_t fileSize, Cluster **currentCluster, uint32_t *currentClusterIndex)
int32_t loadWav (Sample *newSample, uint32_t fileSize, Cluster **currentCluster, uint32_t *currentClusterIndex)

Private Attributes

Cluster *volatile clusterBeingRead_ = nullptr
Priority priorityBeingRead_ = 0
std::array< ClusterRead, kMaxClusterReads > clusterReads_
volatile size_t clusterReadsStart_ = 0
 Oldest. Only written by the main thread.
volatile size_t clusterReadsEnd_ = 0
 One past the newest. Only written by the SD interrupt.
std::array< Prefetch, kMaxPrefetches > prefetches_
 Oldest first.
size_t numPrefetches_ = 0
uint32_t prefetchGeneration_ = 0
bool cardReadOnce {false}
bool cardEjected
bool cardDisabled = false
uint32_t clusterSizeAtBoot {0}

Static Private Attributes

static constexpr size_t kMaxClusterReads = 32
 Most reads finishClusterReads() can fall behind by. The SD interrupt stops reading once there are this many.
static constexpr size_t kMaxPrefetches = 32
 Most Clusters held at once. Bounds the RAM they can pin, and keeps them from crowding out Clusters something's actually waiting on.

Friends

bool sdAsyncNextRead (SdAsyncRead *read)
void sdAsyncReadDone (void *user, int error)

Member Function Documentation

◆ claimPrefetch()

void AudioFileManager::claimPrefetch ( Cluster const * cluster)

Lets go of a prefetched Cluster, now that something that's going to play it holds its own reason on it.

Does nothing if cluster isn't prefetched.

◆ getUnusedAudioRecordingFilePath()

Error AudioFileManager::getUnusedAudioRecordingFilePath ( String & filePath,
String * tempFilePathForRecording,
AudioRecordingFolder folder,
uint32_t * getNumber,
const char * channelName,
String * songName )

If songname isn't supplied the file is placed in the main recording folder and named as samples/folder/REC###. If song and channel are supplied then it's placed in samples/folder/song/channel_###

◆ loadEnqueuedClusterNow()

bool AudioFileManager::loadEnqueuedClusterNow ( Cluster & cluster)

Loads cluster right now if it's in the loading queue, without loading what's ahead of it. If the SD interrupt's already reading it, waits for that instead.

The caller must hold a reason on it. If it can't be loaded now, it's left in the queue as it was.

Returns
Whether it's loaded now.

◆ prefetchCluster()

bool AudioFileManager::prefetchCluster ( Sample * sample,
int32_t clusterIndex )

Holds a Cluster that's likely to be requested soon, loading it first if need be.

prefetches_ is a central list of the Clusters we expect to be asked for next. We hold a reason on each, so unlike a merely cached Cluster it can't be stolen before it's used. The reason is held until claimPrefetch() is called for it, or it's released some other way below. Does nothing if it's already held, or if the list is full of Clusters that haven't loaded yet.

Parameters
sampleThe Sample to load from.
clusterIndexWhich of its Clusters to load. Must be a valid index into sample->clusters.
Returns
Whether the Cluster is now held (or never needs to be, as the Sample is unloadable). False means it's worth asking again later.

The documentation for this class was generated from the following files: