diff --git a/AudioFile.h b/AudioFile.h index a9f26c6..b5d2e9b 100644 --- a/AudioFile.h +++ b/AudioFile.h @@ -458,9 +458,25 @@ bool AudioFile::load (std::string filePath) return false; } - file.unsetf (std::ios::skipws); - std::istream_iterator begin (file), end; - std::vector fileData (begin, end); + std::vector fileData; + + file.unsetf (std::ios::skipws); + + file.seekg (0, std::ios::end); + size_t length = file.tellg(); + file.seekg (0, std::ios::beg); + + // allocate + fileData.resize (length); + + file.read(reinterpret_cast (fileData.data()), length); + file.close(); + + if (file.gcount() != length) + { + reportError ("ERROR: Couldn't read entire file\n" + filePath); + return false; + } // get audio file format audioFileFormat = determineAudioFileFormat (fileData); @@ -564,6 +580,12 @@ bool AudioFile::decodeWaveFile (std::vector& fileData) { int sampleIndex = samplesStartIndex + (numBytesPerBlock * i) + channel * numBytesPerSample; + if ((sampleIndex + (bitDepth / 8) - 1) >= fileData.size()) + { + reportError ("ERROR: read file error as the metadata indicates more samples than there are in the file data"); + return false; + } + if (bitDepth == 8) { T sample = singleByteToSample (fileData[sampleIndex]); @@ -702,6 +724,12 @@ bool AudioFile::decodeAiffFile (std::vector& fileData) { int sampleIndex = samplesStartIndex + (numBytesPerFrame * i) + channel * numBytesPerSample; + if ((sampleIndex + (bitDepth / 8) - 1) >= fileData.size()) + { + reportError ("ERROR: read file error as the metadata indicates more samples than there are in the file data"); + return false; + } + if (bitDepth == 8) { int8_t sampleAsSigned8Bit = (int8_t)fileData[sampleIndex]; diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d7b825..d43c0c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ #=============================================================================== cmake_minimum_required (VERSION 3.12) -project ("AudioFile" VERSION 1.0.8 +project ("AudioFile" VERSION 1.0.9 DESCRIPTION "A simple C++ library for reading and writing audio files." HOMEPAGE_URL "https://github.com/adamstark/AudioFile") diff --git a/README.md b/README.md index ab1843c..8b4d6bd 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # AudioFile -![Version](https://img.shields.io/badge/version-1.0.8-green.svg?style=flat-square) +![Version](https://img.shields.io/badge/version-1.0.9-green.svg?style=flat-square) ![License](https://img.shields.io/badge/license-GPL-blue.svg?style=flat-square) ![Language](https://img.shields.io/badge/language-C++-yellow.svg?style=flat-square) @@ -156,6 +156,11 @@ If you prefer not to see these messages, you can disable this error logging beha Versions ------- +##### 1.0.9 - 23rd January 2021 + +- Faster loading of audio files +- Bug fixes + ##### 1.0.8 - 18th October 2020 - CMake support @@ -198,6 +203,7 @@ Contributions * Read/write of iXML data chunks ([mynameisjohn](https://github.com/mynameisjohn)) * Remove warnings ([Abhinav1997](https://github.com/Abhinav1997)) * Better support on Ubuntu ([BenjaminHinchliff](https://github.com/BenjaminHinchliff)) +* Faster loading of audio files ([helloimmatt](https://github.com/helloimmatt/)) Want to Contribute? -------