Skip to content

Commit b7dd84a

Browse files
committed
Merge branch 'release/1.0.9'
2 parents e703a38 + e72f103 commit b7dd84a

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

AudioFile.h

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,25 @@ bool AudioFile<T>::load (std::string filePath)
458458
return false;
459459
}
460460

461-
file.unsetf (std::ios::skipws);
462-
std::istream_iterator<uint8_t> begin (file), end;
463-
std::vector<uint8_t> fileData (begin, end);
461+
std::vector<uint8_t> fileData;
462+
463+
file.unsetf (std::ios::skipws);
464+
465+
file.seekg (0, std::ios::end);
466+
size_t length = file.tellg();
467+
file.seekg (0, std::ios::beg);
468+
469+
// allocate
470+
fileData.resize (length);
471+
472+
file.read(reinterpret_cast<char*> (fileData.data()), length);
473+
file.close();
474+
475+
if (file.gcount() != length)
476+
{
477+
reportError ("ERROR: Couldn't read entire file\n" + filePath);
478+
return false;
479+
}
464480

465481
// get audio file format
466482
audioFileFormat = determineAudioFileFormat (fileData);
@@ -564,6 +580,12 @@ bool AudioFile<T>::decodeWaveFile (std::vector<uint8_t>& fileData)
564580
{
565581
int sampleIndex = samplesStartIndex + (numBytesPerBlock * i) + channel * numBytesPerSample;
566582

583+
if ((sampleIndex + (bitDepth / 8) - 1) >= fileData.size())
584+
{
585+
reportError ("ERROR: read file error as the metadata indicates more samples than there are in the file data");
586+
return false;
587+
}
588+
567589
if (bitDepth == 8)
568590
{
569591
T sample = singleByteToSample (fileData[sampleIndex]);
@@ -702,6 +724,12 @@ bool AudioFile<T>::decodeAiffFile (std::vector<uint8_t>& fileData)
702724
{
703725
int sampleIndex = samplesStartIndex + (numBytesPerFrame * i) + channel * numBytesPerSample;
704726

727+
if ((sampleIndex + (bitDepth / 8) - 1) >= fileData.size())
728+
{
729+
reportError ("ERROR: read file error as the metadata indicates more samples than there are in the file data");
730+
return false;
731+
}
732+
705733
if (bitDepth == 8)
706734
{
707735
int8_t sampleAsSigned8Bit = (int8_t)fileData[sampleIndex];

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#===============================================================================
22
cmake_minimum_required (VERSION 3.12)
33

4-
project ("AudioFile" VERSION 1.0.8
4+
project ("AudioFile" VERSION 1.0.9
55
DESCRIPTION "A simple C++ library for reading and writing audio files."
66
HOMEPAGE_URL "https://github.com/adamstark/AudioFile")
77

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# AudioFile
22

33
<!-- Version and License Badges -->
4-
![Version](https://img.shields.io/badge/version-1.0.8-green.svg?style=flat-square)
4+
![Version](https://img.shields.io/badge/version-1.0.9-green.svg?style=flat-square)
55
![License](https://img.shields.io/badge/license-GPL-blue.svg?style=flat-square)
66
![Language](https://img.shields.io/badge/language-C++-yellow.svg?style=flat-square)
77

@@ -156,6 +156,11 @@ If you prefer not to see these messages, you can disable this error logging beha
156156
Versions
157157
-------
158158

159+
##### 1.0.9 - 23rd January 2021
160+
161+
- Faster loading of audio files
162+
- Bug fixes
163+
159164
##### 1.0.8 - 18th October 2020
160165

161166
- CMake support
@@ -198,6 +203,7 @@ Contributions
198203
* Read/write of iXML data chunks ([mynameisjohn](https://github.com/mynameisjohn))
199204
* Remove warnings ([Abhinav1997](https://github.com/Abhinav1997))
200205
* Better support on Ubuntu ([BenjaminHinchliff](https://github.com/BenjaminHinchliff))
206+
* Faster loading of audio files ([helloimmatt](https://github.com/helloimmatt/))
201207

202208
Want to Contribute?
203209
-------

0 commit comments

Comments
 (0)