From 5080af28a3f176eccd960b2c9a6a5a100ce173a9 Mon Sep 17 00:00:00 2001 From: Alexandre Arsenault Date: Sat, 28 Oct 2023 17:06:10 -0400 Subject: [PATCH] Make some methods static and add const to some vector ref --- AudioFile.h | 53 +++++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/AudioFile.h b/AudioFile.h index fcd3fd5..e554196 100644 --- a/AudioFile.h +++ b/AudioFile.h @@ -105,7 +105,7 @@ class AudioFile //============================================================= /** Loads an audio file from data in memory */ - bool loadFromMemory (std::vector& fileData); + bool loadFromMemory (const std::vector& fileData); //============================================================= /** @Returns the sample rate */ @@ -185,9 +185,8 @@ class AudioFile }; //============================================================= - AudioFileFormat determineAudioFileFormat (std::vector& fileData); - bool decodeWaveFile (std::vector& fileData); - bool decodeAiffFile (std::vector& fileData); + bool decodeWaveFile (const std::vector& fileData); + bool decodeAiffFile (const std::vector& fileData); //============================================================= bool saveToWaveFile (std::string filePath); @@ -197,23 +196,25 @@ class AudioFile void clearAudioBuffer(); //============================================================= - int32_t fourBytesToInt (std::vector& source, int startIndex, Endianness endianness = Endianness::LittleEndian); - int16_t twoBytesToInt (std::vector& source, int startIndex, Endianness endianness = Endianness::LittleEndian); - int getIndexOfString (std::vector& source, std::string s); - int getIndexOfChunk (std::vector& source, const std::string& chunkHeaderID, int startIndex, Endianness endianness = Endianness::LittleEndian); + static inline AudioFileFormat determineAudioFileFormat (const std::vector& fileData); + + static inline int32_t fourBytesToInt (const std::vector& source, int startIndex, Endianness endianness = Endianness::LittleEndian); + static inline int16_t twoBytesToInt (const std::vector& source, int startIndex, Endianness endianness = Endianness::LittleEndian); + static inline int getIndexOfString (const std::vector& source, std::string s); + static inline int getIndexOfChunk (const std::vector& source, const std::string& chunkHeaderID, int startIndex, Endianness endianness = Endianness::LittleEndian); //============================================================= - uint32_t getAiffSampleRate (std::vector& fileData, int sampleRateStartIndex); - bool tenByteMatch (std::vector& v1, int startIndex1, std::vector& v2, int startIndex2); - void addSampleRateToAiffData (std::vector& fileData, uint32_t sampleRate); + static inline uint32_t getAiffSampleRate (const std::vector& fileData, int sampleRateStartIndex); + static inline bool tenByteMatch (const std::vector& v1, int startIndex1, const std::vector& v2, int startIndex2); + static inline void addSampleRateToAiffData (std::vector& fileData, uint32_t sampleRate); //============================================================= - void addStringToFileData (std::vector& fileData, std::string s); - void addInt32ToFileData (std::vector& fileData, int32_t i, Endianness endianness = Endianness::LittleEndian); - void addInt16ToFileData (std::vector& fileData, int16_t i, Endianness endianness = Endianness::LittleEndian); + static inline void addStringToFileData (std::vector& fileData, std::string s); + static inline void addInt32ToFileData (std::vector& fileData, int32_t i, Endianness endianness = Endianness::LittleEndian); + static inline void addInt16ToFileData (std::vector& fileData, int16_t i, Endianness endianness = Endianness::LittleEndian); //============================================================= - bool writeDataToFile (std::vector& fileData, std::string filePath); + static inline bool writeDataToFile (const std::vector& fileData, std::string filePath); //============================================================= void reportError (std::string errorMessage); @@ -545,7 +546,7 @@ bool AudioFile::load (std::string filePath) //============================================================= template -bool AudioFile::loadFromMemory (std::vector& fileData) +bool AudioFile::loadFromMemory (const std::vector& fileData) { // get audio file format audioFileFormat = determineAudioFileFormat (fileData); @@ -567,7 +568,7 @@ bool AudioFile::loadFromMemory (std::vector& fileData) //============================================================= template -bool AudioFile::decodeWaveFile (std::vector& fileData) +bool AudioFile::decodeWaveFile (const std::vector& fileData) { // ----------------------------------------------------------- // HEADER CHUNK @@ -726,7 +727,7 @@ bool AudioFile::decodeWaveFile (std::vector& fileData) //============================================================= template -bool AudioFile::decodeAiffFile (std::vector& fileData) +bool AudioFile::decodeAiffFile (const std::vector& fileData) { // ----------------------------------------------------------- // HEADER CHUNK @@ -881,7 +882,7 @@ bool AudioFile::decodeAiffFile (std::vector& fileData) //============================================================= template -uint32_t AudioFile::getAiffSampleRate (std::vector& fileData, int sampleRateStartIndex) +uint32_t AudioFile::getAiffSampleRate (const std::vector& fileData, int sampleRateStartIndex) { for (auto it : aiffSampleRateTable) { @@ -894,7 +895,7 @@ uint32_t AudioFile::getAiffSampleRate (std::vector& fileData, int sa //============================================================= template -bool AudioFile::tenByteMatch (std::vector& v1, int startIndex1, std::vector& v2, int startIndex2) +bool AudioFile::tenByteMatch (const std::vector& v1, int startIndex1, const std::vector& v2, int startIndex2) { for (int i = 0; i < 10; i++) { @@ -1156,7 +1157,7 @@ bool AudioFile::saveToAiffFile (std::string filePath) //============================================================= template -bool AudioFile::writeDataToFile (std::vector& fileData, std::string filePath) +bool AudioFile::writeDataToFile (const std::vector& fileData, std::string filePath) { std::ofstream outputFile (filePath, std::ios::binary); @@ -1244,7 +1245,7 @@ void AudioFile::clearAudioBuffer() //============================================================= template -AudioFileFormat AudioFile::determineAudioFileFormat (std::vector& fileData) +AudioFileFormat AudioFile::determineAudioFileFormat (const std::vector& fileData) { std::string header (fileData.begin(), fileData.begin() + 4); @@ -1258,7 +1259,7 @@ AudioFileFormat AudioFile::determineAudioFileFormat (std::vector& fi //============================================================= template -int32_t AudioFile::fourBytesToInt (std::vector& source, int startIndex, Endianness endianness) +int32_t AudioFile::fourBytesToInt (const std::vector& source, int startIndex, Endianness endianness) { if (source.size() >= (startIndex + 4)) { @@ -1280,7 +1281,7 @@ int32_t AudioFile::fourBytesToInt (std::vector& source, int startInd //============================================================= template -int16_t AudioFile::twoBytesToInt (std::vector& source, int startIndex, Endianness endianness) +int16_t AudioFile::twoBytesToInt (const std::vector& source, int startIndex, Endianness endianness) { int16_t result; @@ -1294,7 +1295,7 @@ int16_t AudioFile::twoBytesToInt (std::vector& source, int startInde //============================================================= template -int AudioFile::getIndexOfString (std::vector& source, std::string stringToSearchFor) +int AudioFile::getIndexOfString (const std::vector& source, std::string stringToSearchFor) { int index = -1; int stringLength = (int)stringToSearchFor.length(); @@ -1315,7 +1316,7 @@ int AudioFile::getIndexOfString (std::vector& source, std::string st //============================================================= template -int AudioFile::getIndexOfChunk (std::vector& source, const std::string& chunkHeaderID, int startIndex, Endianness endianness) +int AudioFile::getIndexOfChunk (const std::vector& source, const std::string& chunkHeaderID, int startIndex, Endianness endianness) { constexpr int dataLen = 4;