Skip to content

Commit

Permalink
Make some methods static and add const to some vector ref
Browse files Browse the repository at this point in the history
  • Loading branch information
alxarsenault committed Oct 28, 2023
1 parent bcb61a4 commit 5080af2
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions AudioFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class AudioFile

//=============================================================
/** Loads an audio file from data in memory */
bool loadFromMemory (std::vector<uint8_t>& fileData);
bool loadFromMemory (const std::vector<uint8_t>& fileData);

//=============================================================
/** @Returns the sample rate */
Expand Down Expand Up @@ -185,9 +185,8 @@ class AudioFile
};

//=============================================================
AudioFileFormat determineAudioFileFormat (std::vector<uint8_t>& fileData);
bool decodeWaveFile (std::vector<uint8_t>& fileData);
bool decodeAiffFile (std::vector<uint8_t>& fileData);
bool decodeWaveFile (const std::vector<uint8_t>& fileData);
bool decodeAiffFile (const std::vector<uint8_t>& fileData);

//=============================================================
bool saveToWaveFile (std::string filePath);
Expand All @@ -197,23 +196,25 @@ class AudioFile
void clearAudioBuffer();

//=============================================================
int32_t fourBytesToInt (std::vector<uint8_t>& source, int startIndex, Endianness endianness = Endianness::LittleEndian);
int16_t twoBytesToInt (std::vector<uint8_t>& source, int startIndex, Endianness endianness = Endianness::LittleEndian);
int getIndexOfString (std::vector<uint8_t>& source, std::string s);
int getIndexOfChunk (std::vector<uint8_t>& source, const std::string& chunkHeaderID, int startIndex, Endianness endianness = Endianness::LittleEndian);
static inline AudioFileFormat determineAudioFileFormat (const std::vector<uint8_t>& fileData);

static inline int32_t fourBytesToInt (const std::vector<uint8_t>& source, int startIndex, Endianness endianness = Endianness::LittleEndian);
static inline int16_t twoBytesToInt (const std::vector<uint8_t>& source, int startIndex, Endianness endianness = Endianness::LittleEndian);
static inline int getIndexOfString (const std::vector<uint8_t>& source, std::string s);
static inline int getIndexOfChunk (const std::vector<uint8_t>& source, const std::string& chunkHeaderID, int startIndex, Endianness endianness = Endianness::LittleEndian);

//=============================================================
uint32_t getAiffSampleRate (std::vector<uint8_t>& fileData, int sampleRateStartIndex);
bool tenByteMatch (std::vector<uint8_t>& v1, int startIndex1, std::vector<uint8_t>& v2, int startIndex2);
void addSampleRateToAiffData (std::vector<uint8_t>& fileData, uint32_t sampleRate);
static inline uint32_t getAiffSampleRate (const std::vector<uint8_t>& fileData, int sampleRateStartIndex);
static inline bool tenByteMatch (const std::vector<uint8_t>& v1, int startIndex1, const std::vector<uint8_t>& v2, int startIndex2);
static inline void addSampleRateToAiffData (std::vector<uint8_t>& fileData, uint32_t sampleRate);

//=============================================================
void addStringToFileData (std::vector<uint8_t>& fileData, std::string s);
void addInt32ToFileData (std::vector<uint8_t>& fileData, int32_t i, Endianness endianness = Endianness::LittleEndian);
void addInt16ToFileData (std::vector<uint8_t>& fileData, int16_t i, Endianness endianness = Endianness::LittleEndian);
static inline void addStringToFileData (std::vector<uint8_t>& fileData, std::string s);
static inline void addInt32ToFileData (std::vector<uint8_t>& fileData, int32_t i, Endianness endianness = Endianness::LittleEndian);
static inline void addInt16ToFileData (std::vector<uint8_t>& fileData, int16_t i, Endianness endianness = Endianness::LittleEndian);

//=============================================================
bool writeDataToFile (std::vector<uint8_t>& fileData, std::string filePath);
static inline bool writeDataToFile (const std::vector<uint8_t>& fileData, std::string filePath);

//=============================================================
void reportError (std::string errorMessage);
Expand Down Expand Up @@ -545,7 +546,7 @@ bool AudioFile<T>::load (std::string filePath)

//=============================================================
template <class T>
bool AudioFile<T>::loadFromMemory (std::vector<uint8_t>& fileData)
bool AudioFile<T>::loadFromMemory (const std::vector<uint8_t>& fileData)
{
// get audio file format
audioFileFormat = determineAudioFileFormat (fileData);
Expand All @@ -567,7 +568,7 @@ bool AudioFile<T>::loadFromMemory (std::vector<uint8_t>& fileData)

//=============================================================
template <class T>
bool AudioFile<T>::decodeWaveFile (std::vector<uint8_t>& fileData)
bool AudioFile<T>::decodeWaveFile (const std::vector<uint8_t>& fileData)
{
// -----------------------------------------------------------
// HEADER CHUNK
Expand Down Expand Up @@ -726,7 +727,7 @@ bool AudioFile<T>::decodeWaveFile (std::vector<uint8_t>& fileData)

//=============================================================
template <class T>
bool AudioFile<T>::decodeAiffFile (std::vector<uint8_t>& fileData)
bool AudioFile<T>::decodeAiffFile (const std::vector<uint8_t>& fileData)
{
// -----------------------------------------------------------
// HEADER CHUNK
Expand Down Expand Up @@ -881,7 +882,7 @@ bool AudioFile<T>::decodeAiffFile (std::vector<uint8_t>& fileData)

//=============================================================
template <class T>
uint32_t AudioFile<T>::getAiffSampleRate (std::vector<uint8_t>& fileData, int sampleRateStartIndex)
uint32_t AudioFile<T>::getAiffSampleRate (const std::vector<uint8_t>& fileData, int sampleRateStartIndex)
{
for (auto it : aiffSampleRateTable)
{
Expand All @@ -894,7 +895,7 @@ uint32_t AudioFile<T>::getAiffSampleRate (std::vector<uint8_t>& fileData, int sa

//=============================================================
template <class T>
bool AudioFile<T>::tenByteMatch (std::vector<uint8_t>& v1, int startIndex1, std::vector<uint8_t>& v2, int startIndex2)
bool AudioFile<T>::tenByteMatch (const std::vector<uint8_t>& v1, int startIndex1, const std::vector<uint8_t>& v2, int startIndex2)
{
for (int i = 0; i < 10; i++)
{
Expand Down Expand Up @@ -1156,7 +1157,7 @@ bool AudioFile<T>::saveToAiffFile (std::string filePath)

//=============================================================
template <class T>
bool AudioFile<T>::writeDataToFile (std::vector<uint8_t>& fileData, std::string filePath)
bool AudioFile<T>::writeDataToFile (const std::vector<uint8_t>& fileData, std::string filePath)
{
std::ofstream outputFile (filePath, std::ios::binary);

Expand Down Expand Up @@ -1244,7 +1245,7 @@ void AudioFile<T>::clearAudioBuffer()

//=============================================================
template <class T>
AudioFileFormat AudioFile<T>::determineAudioFileFormat (std::vector<uint8_t>& fileData)
AudioFileFormat AudioFile<T>::determineAudioFileFormat (const std::vector<uint8_t>& fileData)
{
std::string header (fileData.begin(), fileData.begin() + 4);

Expand All @@ -1258,7 +1259,7 @@ AudioFileFormat AudioFile<T>::determineAudioFileFormat (std::vector<uint8_t>& fi

//=============================================================
template <class T>
int32_t AudioFile<T>::fourBytesToInt (std::vector<uint8_t>& source, int startIndex, Endianness endianness)
int32_t AudioFile<T>::fourBytesToInt (const std::vector<uint8_t>& source, int startIndex, Endianness endianness)
{
if (source.size() >= (startIndex + 4))
{
Expand All @@ -1280,7 +1281,7 @@ int32_t AudioFile<T>::fourBytesToInt (std::vector<uint8_t>& source, int startInd

//=============================================================
template <class T>
int16_t AudioFile<T>::twoBytesToInt (std::vector<uint8_t>& source, int startIndex, Endianness endianness)
int16_t AudioFile<T>::twoBytesToInt (const std::vector<uint8_t>& source, int startIndex, Endianness endianness)
{
int16_t result;

Expand All @@ -1294,7 +1295,7 @@ int16_t AudioFile<T>::twoBytesToInt (std::vector<uint8_t>& source, int startInde

//=============================================================
template <class T>
int AudioFile<T>::getIndexOfString (std::vector<uint8_t>& source, std::string stringToSearchFor)
int AudioFile<T>::getIndexOfString (const std::vector<uint8_t>& source, std::string stringToSearchFor)
{
int index = -1;
int stringLength = (int)stringToSearchFor.length();
Expand All @@ -1315,7 +1316,7 @@ int AudioFile<T>::getIndexOfString (std::vector<uint8_t>& source, std::string st

//=============================================================
template <class T>
int AudioFile<T>::getIndexOfChunk (std::vector<uint8_t>& source, const std::string& chunkHeaderID, int startIndex, Endianness endianness)
int AudioFile<T>::getIndexOfChunk (const std::vector<uint8_t>& source, const std::string& chunkHeaderID, int startIndex, Endianness endianness)
{
constexpr int dataLen = 4;

Expand Down

0 comments on commit 5080af2

Please sign in to comment.