Skip to content

Commit 9603c87

Browse files
authored
Merge branch 'develop' into integer-formats
2 parents 5e607ea + 2ea1435 commit 9603c87

File tree

4 files changed

+799
-394
lines changed

4 files changed

+799
-394
lines changed

AudioFile.h

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,17 @@ bool AudioFile<T>::load (std::string filePath)
508508
return false;
509509
}
510510

511-
return loadFromMemory (fileData);
511+
// Handle very small files that will break our attempt to read the
512+
// first header info from them
513+
if (fileData.size() < 12)
514+
{
515+
reportError ("ERROR: File is not a valid audio file\n" + filePath);
516+
return false;
517+
}
518+
else
519+
{
520+
return loadFromMemory (fileData);
521+
}
512522
}
513523

514524
//=============================================================
@@ -650,10 +660,16 @@ bool AudioFile<T>::decodeWaveFile (std::vector<uint8_t>& fileData)
650660
int32_t sampleAsInt = fourBytesToInt (fileData, sampleIndex);
651661
T sample;
652662

653-
if (audioFormat == WavAudioFormat::IEEEFloat)
654-
sample = (T)reinterpret_cast<float&> (sampleAsInt);
663+
if (audioFormat == WavAudioFormat::IEEEFloat)
664+
{
665+
float f;
666+
memcpy (&f, &sampleAsInt, sizeof(int32_t));
667+
sample = (T)f;
668+
}
655669
else // assume PCM
670+
{
656671
sample = resampleIntegerSample<int32_t, T>(sampleAsInt);
672+
}
657673

658674
samples[channel].push_back (sample);
659675
}
@@ -1200,14 +1216,22 @@ AudioFileFormat AudioFile<T>::determineAudioFileFormat (std::vector<uint8_t>& fi
12001216
template <class T>
12011217
int32_t AudioFile<T>::fourBytesToInt (std::vector<uint8_t>& source, int startIndex, Endianness endianness)
12021218
{
1203-
int32_t result;
1204-
1205-
if (endianness == Endianness::LittleEndian)
1206-
result = (source[startIndex + 3] << 24) | (source[startIndex + 2] << 16) | (source[startIndex + 1] << 8) | source[startIndex];
1219+
if (source.size() >= (startIndex + 4))
1220+
{
1221+
int32_t result;
1222+
1223+
if (endianness == Endianness::LittleEndian)
1224+
result = (source[startIndex + 3] << 24) | (source[startIndex + 2] << 16) | (source[startIndex + 1] << 8) | source[startIndex];
1225+
else
1226+
result = (source[startIndex] << 24) | (source[startIndex + 1] << 16) | (source[startIndex + 2] << 8) | source[startIndex + 3];
1227+
1228+
return result;
1229+
}
12071230
else
1208-
result = (source[startIndex] << 24) | (source[startIndex + 1] << 16) | (source[startIndex + 2] << 8) | source[startIndex + 3];
1209-
1210-
return result;
1231+
{
1232+
assert (false && "Attempted to read four bytes from vector at position where out of bounds access would occur");
1233+
return 0; // this is a dummy value as we don't have one to return
1234+
}
12111235
}
12121236

12131237
//=============================================================
@@ -1250,6 +1274,7 @@ template <class T>
12501274
int AudioFile<T>::getIndexOfChunk (std::vector<uint8_t>& source, const std::string& chunkHeaderID, int startIndex, Endianness endianness)
12511275
{
12521276
constexpr int dataLen = 4;
1277+
12531278
if (chunkHeaderID.size() != dataLen)
12541279
{
12551280
assert (false && "Invalid chunk header ID string");
@@ -1265,6 +1290,11 @@ int AudioFile<T>::getIndexOfChunk (std::vector<uint8_t>& source, const std::stri
12651290
}
12661291

12671292
i += dataLen;
1293+
1294+
// If somehow we don't have 4 bytes left to read, then exit with -1
1295+
if ((i + 4) >= source.size())
1296+
return -1;
1297+
12681298
auto chunkSize = fourBytesToInt (source, i, endianness);
12691299
i += (dataLen + chunkSize);
12701300
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Usage
103103
audioFile.setNumSamplesPerChannel (numSamples);
104104

105105
// Set the number of channels
106-
audioFile.setNumChannels (int numChannels);
106+
audioFile.setNumChannels (numChannels);
107107

108108
### Set bit depth and sample rate
109109

examples/examples.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ namespace examples
140140
//---------------------------------------------------------------
141141
// 4. Write audio file to disk
142142

143-
std::string outputFilePath = "quieter-audio-filer.wav"; // change this to somewhere useful for you
143+
std::string outputFilePath = "quieter-audio-file.wav"; // change this to somewhere useful for you
144144
a.save (outputFilePath, AudioFileFormat::Aiff);
145145
}
146146
}

0 commit comments

Comments
 (0)