From adf81afbdbb3d10cd60044f136fe7a8e5c489e5e Mon Sep 17 00:00:00 2001 From: Matt <36444205+helloimmatt@users.noreply.github.com> Date: Sun, 29 Nov 2020 17:29:25 +0000 Subject: [PATCH 1/5] More efficient and *faster* way of reading the whole file. Previous owner's code was too slow when reading files. This was especially noticable when reading in bulk. With this optimization, reading files is much faster. --- AudioFile.h | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/AudioFile.h b/AudioFile.h index a9f26c6..c3fb934 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); From c9ebb00ed086c63928befecf5ff24c1424d5793a Mon Sep 17 00:00:00 2001 From: Adam Stark Date: Sun, 17 Jan 2021 22:51:09 +0000 Subject: [PATCH 2/5] Catch issue where the number of samples in the metadata is larger than the actual file data --- AudioFile.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/AudioFile.h b/AudioFile.h index a9f26c6..badd70f 100644 --- a/AudioFile.h +++ b/AudioFile.h @@ -564,6 +564,12 @@ bool AudioFile::decodeWaveFile (std::vector& fileData) { int sampleIndex = samplesStartIndex + (numBytesPerBlock * i) + channel * numBytesPerSample; + if ((sampleIndex + (bitDepth / 8)) >= 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 +708,12 @@ bool AudioFile::decodeAiffFile (std::vector& fileData) { int sampleIndex = samplesStartIndex + (numBytesPerFrame * i) + channel * numBytesPerSample; + if ((sampleIndex + (bitDepth / 8)) >= 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]; From 0e91c8614f5d219b20082b826e7013247b0146af Mon Sep 17 00:00:00 2001 From: Adam Stark Date: Sun, 17 Jan 2021 22:54:59 +0000 Subject: [PATCH 3/5] Fix bug in last check for out of range in file data --- AudioFile.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/AudioFile.h b/AudioFile.h index badd70f..8b3a27a 100644 --- a/AudioFile.h +++ b/AudioFile.h @@ -564,7 +564,7 @@ bool AudioFile::decodeWaveFile (std::vector& fileData) { int sampleIndex = samplesStartIndex + (numBytesPerBlock * i) + channel * numBytesPerSample; - if ((sampleIndex + (bitDepth / 8)) >= fileData.size()) + 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; @@ -708,7 +708,7 @@ bool AudioFile::decodeAiffFile (std::vector& fileData) { int sampleIndex = samplesStartIndex + (numBytesPerFrame * i) + channel * numBytesPerSample; - if ((sampleIndex + (bitDepth / 8)) >= fileData.size()) + 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; From 8984824abed4f5bb2153adbe6b78ca6f92de913a Mon Sep 17 00:00:00 2001 From: Adam Stark Date: Sat, 23 Jan 2021 18:59:10 +0000 Subject: [PATCH 4/5] Changes to match code style for new file loading code --- AudioFile.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/AudioFile.h b/AudioFile.h index 4b40ea2..b5d2e9b 100644 --- a/AudioFile.h +++ b/AudioFile.h @@ -460,21 +460,21 @@ bool AudioFile::load (std::string filePath) std::vector fileData; - file.unsetf(std::ios::skipws); + file.unsetf (std::ios::skipws); - file.seekg(0, std::ios::end); + file.seekg (0, std::ios::end); size_t length = file.tellg(); - file.seekg(0, std::ios::beg); + file.seekg (0, std::ios::beg); // allocate - fileData.resize(length); + fileData.resize (length); - file.read(reinterpret_cast(fileData.data()), length); + file.read(reinterpret_cast (fileData.data()), length); file.close(); if (file.gcount() != length) { - reportError("ERROR: Couldn't read entire file\n" + filePath); + reportError ("ERROR: Couldn't read entire file\n" + filePath); return false; } From e72f103b1cfd1e6563642da5bee5f4e0220004d0 Mon Sep 17 00:00:00 2001 From: Adam Stark Date: Sat, 23 Jan 2021 19:05:23 +0000 Subject: [PATCH 5/5] Updated version numbers to 1.0.9 --- CMakeLists.txt | 2 +- README.md | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) 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? -------