Skip to content

Commit 4554279

Browse files
committed
Merge branch 'release/1.1.4'
2 parents 573abb9 + 1ee557c commit 4554279

File tree

5 files changed

+57
-66
lines changed

5 files changed

+57
-66
lines changed

AudioFile.h

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ class AudioFile
207207

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

213212
//=============================================================
@@ -339,7 +338,7 @@ uint32_t AudioFile<T>::getSampleRate() const
339338
template <class T>
340339
int AudioFile<T>::getNumChannels() const
341340
{
342-
return (int)samples.size();
341+
return static_cast<int> (samples.size());
343342
}
344343

345344
//=============================================================
@@ -368,7 +367,7 @@ template <class T>
368367
int AudioFile<T>::getNumSamplesPerChannel() const
369368
{
370369
if (samples.size() > 0)
371-
return (int) samples[0].size();
370+
return static_cast<int> (samples[0].size());
372371
else
373372
return 0;
374373
}
@@ -377,7 +376,7 @@ int AudioFile<T>::getNumSamplesPerChannel() const
377376
template <class T>
378377
double AudioFile<T>::getLengthInSeconds() const
379378
{
380-
return (double)getNumSamplesPerChannel() / (double)sampleRate;
379+
return static_cast<double> (getNumSamplesPerChannel()) / static_cast<double> (sampleRate);
381380
}
382381

383382
//=============================================================
@@ -397,7 +396,7 @@ void AudioFile<T>::printSummary() const
397396
template <class T>
398397
bool AudioFile<T>::setAudioBuffer (const AudioBuffer& newBuffer)
399398
{
400-
int numChannels = (int)newBuffer.size();
399+
int numChannels = static_cast<int> (newBuffer.size());
401400

402401
if (numChannels <= 0)
403402
{
@@ -437,16 +436,8 @@ void AudioFile<T>::setAudioBufferSize (int numChannels, int numSamples)
437436
template <class T>
438437
void AudioFile<T>::setNumSamplesPerChannel (int numSamples)
439438
{
440-
int originalSize = getNumSamplesPerChannel();
441-
442439
for (int i = 0; i < getNumChannels();i++)
443-
{
444-
samples[i].resize (numSamples);
445-
446-
// set any new samples to zero
447-
if (numSamples > originalSize)
448-
std::fill (samples[i].begin() + originalSize, samples[i].end(), (T)0.);
449-
}
440+
samples[i].resize (numSamples, static_cast<T> (0));
450441
}
451442

452443
//=============================================================
@@ -460,14 +451,8 @@ void AudioFile<T>::setNumChannels (int numChannels)
460451

461452
// make sure any new channels are set to the right size
462453
// and filled with zeros
463-
if (numChannels > originalNumChannels)
464-
{
465-
for (int i = originalNumChannels; i < numChannels; i++)
466-
{
467-
samples[i].resize (originalNumSamplesPerChannel);
468-
std::fill (samples[i].begin(), samples[i].end(), (T)0.);
469-
}
470-
}
454+
for (int i = originalNumChannels; i < numChannels; i++)
455+
samples[i].resize (originalNumSamplesPerChannel, static_cast<T> (0));
471456
}
472457

473458
//=============================================================
@@ -590,10 +575,10 @@ bool AudioFile<T>::decodeWaveFile (const std::vector<uint8_t>& fileData)
590575
//int32_t formatChunkSize = fourBytesToInt (fileData, f + 4);
591576
uint16_t audioFormat = twoBytesToInt (fileData, f + 8);
592577
uint16_t numChannels = twoBytesToInt (fileData, f + 10);
593-
sampleRate = (uint32_t) fourBytesToInt (fileData, f + 12);
578+
sampleRate = static_cast<uint32_t> (fourBytesToInt (fileData, f + 12));
594579
uint32_t numBytesPerSecond = fourBytesToInt (fileData, f + 16);
595580
uint16_t numBytesPerBlock = twoBytesToInt (fileData, f + 20);
596-
bitDepth = (int) twoBytesToInt (fileData, f + 22);
581+
bitDepth = static_cast<int> (twoBytesToInt (fileData, f + 22));
597582

598583
if (bitDepth > sizeof (T) * 8)
599584
{
@@ -652,7 +637,7 @@ bool AudioFile<T>::decodeWaveFile (const std::vector<uint8_t>& fileData)
652637
{
653638
for (int channel = 0; channel < numChannels; channel++)
654639
{
655-
int sampleIndex = samplesStartIndex + (numBytesPerBlock * i) + channel * numBytesPerSample;
640+
size_t sampleIndex = samplesStartIndex + (numBytesPerBlock * i) + channel * numBytesPerSample;
656641

657642
if ((sampleIndex + (bitDepth / 8) - 1) >= fileData.size())
658643
{
@@ -689,9 +674,9 @@ bool AudioFile<T>::decodeWaveFile (const std::vector<uint8_t>& fileData)
689674

690675
if (audioFormat == WavAudioFormat::IEEEFloat && std::is_floating_point_v<T>)
691676
{
692-
float f;
693-
memcpy (&f, &sampleAsInt, sizeof(int32_t));
694-
sample = (T)f;
677+
float floatValue;
678+
memcpy (&floatValue, &sampleAsInt, sizeof(int32_t));
679+
sample = static_cast<T> (floatValue);
695680
}
696681
else // assume PCM
697682
{
@@ -712,7 +697,7 @@ bool AudioFile<T>::decodeWaveFile (const std::vector<uint8_t>& fileData)
712697
if (indexOfXMLChunk != -1)
713698
{
714699
int32_t chunkSize = fourBytesToInt (fileData, indexOfXMLChunk + 4);
715-
iXMLChunk = std::string ((const char*) &fileData[indexOfXMLChunk + 8], chunkSize);
700+
iXMLChunk = std::string (reinterpret_cast<const char*> (&fileData[indexOfXMLChunk + 8]), chunkSize);
716701
}
717702

718703
return true;
@@ -751,7 +736,7 @@ bool AudioFile<T>::decodeAiffFile (const std::vector<uint8_t>& fileData)
751736
//int32_t commChunkSize = fourBytesToInt (fileData, p + 4, Endianness::BigEndian);
752737
int16_t numChannels = twoBytesToInt (fileData, p + 8, Endianness::BigEndian);
753738
int32_t numSamplesPerChannel = fourBytesToInt (fileData, p + 10, Endianness::BigEndian);
754-
bitDepth = (int) twoBytesToInt (fileData, p + 14, Endianness::BigEndian);
739+
bitDepth = static_cast<int> (twoBytesToInt (fileData, p + 14, Endianness::BigEndian));
755740
sampleRate = getAiffSampleRate (fileData, p + 16);
756741

757742
if (bitDepth > sizeof (T) * 8)
@@ -797,10 +782,10 @@ bool AudioFile<T>::decodeAiffFile (const std::vector<uint8_t>& fileData)
797782
int numBytesPerSample = bitDepth / 8;
798783
int numBytesPerFrame = numBytesPerSample * numChannels;
799784
int totalNumAudioSampleBytes = numSamplesPerChannel * numBytesPerFrame;
800-
int samplesStartIndex = s + 16 + (int)offset;
785+
int samplesStartIndex = s + 16 + static_cast<int> (offset);
801786

802787
// sanity check the data
803-
if ((soundDataChunkSize - 8) != totalNumAudioSampleBytes || totalNumAudioSampleBytes > static_cast<long>(fileData.size() - samplesStartIndex))
788+
if ((soundDataChunkSize - 8) != totalNumAudioSampleBytes || totalNumAudioSampleBytes > static_cast<long> (fileData.size() - samplesStartIndex))
804789
{
805790
reportError ("ERROR: the metadatafor this file doesn't seem right");
806791
return false;
@@ -813,7 +798,7 @@ bool AudioFile<T>::decodeAiffFile (const std::vector<uint8_t>& fileData)
813798
{
814799
for (int channel = 0; channel < numChannels; channel++)
815800
{
816-
int sampleIndex = samplesStartIndex + (numBytesPerFrame * i) + channel * numBytesPerSample;
801+
size_t sampleIndex = samplesStartIndex + (numBytesPerFrame * i) + channel * numBytesPerSample;
817802

818803
if ((sampleIndex + (bitDepth / 8) - 1) >= fileData.size())
819804
{
@@ -849,7 +834,7 @@ bool AudioFile<T>::decodeAiffFile (const std::vector<uint8_t>& fileData)
849834
T sample;
850835

851836
if (audioFormat == AIFFAudioFormat::Compressed)
852-
sample = (T)reinterpret_cast<float&> (sampleAsInt);
837+
sample = static_cast<T> (reinterpret_cast<float&> (sampleAsInt));
853838
else // assume PCM
854839
sample = AudioSampleConverter<T>::thirtyTwoBitIntToSample (sampleAsInt);
855840

@@ -867,7 +852,7 @@ bool AudioFile<T>::decodeAiffFile (const std::vector<uint8_t>& fileData)
867852
if (indexOfXMLChunk != -1)
868853
{
869854
int32_t chunkSize = fourBytesToInt (fileData, indexOfXMLChunk + 4);
870-
iXMLChunk = std::string ((const char*) &fileData[indexOfXMLChunk + 8], chunkSize);
855+
iXMLChunk = std::string (reinterpret_cast<const char*> (&fileData[indexOfXMLChunk + 8]), chunkSize);
871856
}
872857

873858
return true;
@@ -944,16 +929,16 @@ bool AudioFile<T>::encodeWaveFile (std::vector<uint8_t>& fileData)
944929
addStringToFileData (fileData, "fmt ");
945930
addInt32ToFileData (fileData, formatChunkSize); // format chunk size (16 for PCM)
946931
addInt16ToFileData (fileData, audioFormat); // audio format
947-
addInt16ToFileData (fileData, (int16_t)getNumChannels()); // num channels
948-
addInt32ToFileData (fileData, (int32_t)sampleRate); // sample rate
932+
addInt16ToFileData (fileData, static_cast<int16_t> (getNumChannels())); // num channels
933+
addInt32ToFileData (fileData, static_cast<int32_t> (sampleRate)); // sample rate
949934

950-
int32_t numBytesPerSecond = (int32_t) ((getNumChannels() * sampleRate * bitDepth) / 8);
935+
int32_t numBytesPerSecond = static_cast<int32_t> ((getNumChannels() * sampleRate * bitDepth) / 8);
951936
addInt32ToFileData (fileData, numBytesPerSecond);
952937

953938
int16_t numBytesPerBlock = getNumChannels() * (bitDepth / 8);
954939
addInt16ToFileData (fileData, numBytesPerBlock);
955940

956-
addInt16ToFileData (fileData, (int16_t)bitDepth);
941+
addInt16ToFileData (fileData, static_cast<int16_t> (bitDepth));
957942

958943
if (audioFormat == WavAudioFormat::IEEEFloat)
959944
addInt16ToFileData (fileData, 0); // extension size
@@ -982,29 +967,29 @@ bool AudioFile<T>::encodeWaveFile (std::vector<uint8_t>& fileData)
982967
int32_t sampleAsIntAgain = AudioSampleConverter<T>::sampleToTwentyFourBitInt (samples[channel][i]);
983968

984969
uint8_t bytes[3];
985-
bytes[2] = (uint8_t) (sampleAsIntAgain >> 16) & 0xFF;
986-
bytes[1] = (uint8_t) (sampleAsIntAgain >> 8) & 0xFF;
987-
bytes[0] = (uint8_t) sampleAsIntAgain & 0xFF;
970+
bytes[2] = static_cast<uint8_t> ((sampleAsIntAgain >> 16) & 0xFF);
971+
bytes[1] = static_cast<uint8_t> ((sampleAsIntAgain >> 8) & 0xFF);
972+
bytes[0] = static_cast<uint8_t> (sampleAsIntAgain & 0xFF);
988973

989974
fileData.push_back (bytes[0]);
990975
fileData.push_back (bytes[1]);
991976
fileData.push_back (bytes[2]);
992977
}
993978
else if (bitDepth == 32)
994979
{
995-
int32_t sampleAsInt;
980+
int32_t sampleAsInt = 0;
996981

997982
if (audioFormat == WavAudioFormat::IEEEFloat)
998983
{
999984
if constexpr (std::is_same_v<T, float>)
1000985
{
1001-
sampleAsInt = (int32_t) reinterpret_cast<int32_t&> (samples[channel][i]);
986+
sampleAsInt = reinterpret_cast<int32_t&> (samples[channel][i]);
1002987
}
1003988
else if constexpr (std::is_same_v<T, double>)
1004989
{
1005-
auto sampleAsFloat = (float) samples[channel][i];
990+
auto sampleAsFloat = static_cast<float> (samples[channel][i]);
1006991
float& referenceToSample = sampleAsFloat;
1007-
sampleAsInt = (int32_t) reinterpret_cast<int32_t&> (referenceToSample);
992+
sampleAsInt = reinterpret_cast<int32_t&> (referenceToSample);
1008993
}
1009994
}
1010995
else // assume PCM
@@ -1102,9 +1087,9 @@ bool AudioFile<T>::encodeAiffFile (std::vector<uint8_t>& fileData)
11021087
int32_t sampleAsIntAgain = AudioSampleConverter<T>::sampleToTwentyFourBitInt (samples[channel][i]);
11031088

11041089
uint8_t bytes[3];
1105-
bytes[0] = (uint8_t) (sampleAsIntAgain >> 16) & 0xFF;
1106-
bytes[1] = (uint8_t) (sampleAsIntAgain >> 8) & 0xFF;
1107-
bytes[2] = (uint8_t) sampleAsIntAgain & 0xFF;
1090+
bytes[0] = static_cast<uint8_t> ((sampleAsIntAgain >> 16) & 0xFF);
1091+
bytes[1] = static_cast<uint8_t> ((sampleAsIntAgain >> 8) & 0xFF);
1092+
bytes[2] = static_cast<uint8_t> (sampleAsIntAgain & 0xFF);
11081093

11091094
fileData.push_back (bytes[0]);
11101095
fileData.push_back (bytes[1]);
@@ -1154,7 +1139,7 @@ bool AudioFile<T>::writeDataToFile (const std::vector<uint8_t>& fileData, std::s
11541139
return false;
11551140
}
11561141

1157-
outputFile.write ((const char*)fileData.data(), fileData.size());
1142+
outputFile.write (reinterpret_cast<const char*> (fileData.data()), fileData.size());
11581143
outputFile.close();
11591144
return true;
11601145
}
@@ -1164,7 +1149,7 @@ template <class T>
11641149
void AudioFile<T>::addStringToFileData (std::vector<uint8_t>& fileData, std::string s)
11651150
{
11661151
for (size_t i = 0; i < s.length();i++)
1167-
fileData.push_back ((uint8_t) s[i]);
1152+
fileData.push_back (static_cast<uint8_t> (s[i]));
11681153
}
11691154

11701155
//=============================================================
@@ -1423,7 +1408,7 @@ int32_t AudioSampleConverter<T>::sampleToTwentyFourBitInt (T sample)
14231408
if constexpr (std::is_floating_point<T>::value)
14241409
{
14251410
sample = clamp (sample, -1., 1.);
1426-
return static_cast<int32_t> (sample * 8388607.);
1411+
return static_cast<int32_t> (sample * static_cast<T> (8388607.));
14271412
}
14281413
else
14291414
{
@@ -1458,7 +1443,7 @@ int16_t AudioSampleConverter<T>::sampleToSixteenBitInt (T sample)
14581443
if constexpr (std::is_floating_point<T>::value)
14591444
{
14601445
sample = clamp (sample, -1., 1.);
1461-
return static_cast<int16_t> (sample * 32767.);
1446+
return static_cast<int16_t> (sample * static_cast<T> (32767.));
14621447
}
14631448
else
14641449
{
@@ -1476,7 +1461,7 @@ uint8_t AudioSampleConverter<T>::sampleToUnsignedByte (T sample)
14761461
if constexpr (std::is_floating_point<T>::value)
14771462
{
14781463
sample = clamp (sample, -1., 1.);
1479-
sample = (sample + 1.) / 2.;
1464+
sample = (sample + static_cast<T> (1.)) / static_cast<T> (2.);
14801465
return static_cast<uint8_t> (1 + (sample * 254));
14811466
}
14821467
else
@@ -1495,7 +1480,7 @@ int8_t AudioSampleConverter<T>::sampleToSignedByte (T sample)
14951480
if constexpr (std::is_floating_point<T>::value)
14961481
{
14971482
sample = clamp (sample, -1., 1.);
1498-
return static_cast<int8_t> (sample * (T)0x7F);
1483+
return static_cast<int8_t> (sample * static_cast<T> (0x7F));
14991484
}
15001485
else
15011486
{

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.1.3
4+
project ("AudioFile" VERSION 1.1.4
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
@@ -2,7 +2,7 @@
22

33
<!-- Version and License Badges -->
44

5-
![Version](https://img.shields.io/badge/version-1.1.3-green.svg?style=flat-square)
5+
![Version](https://img.shields.io/badge/version-1.1.4-green.svg?style=flat-square)
66
![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)
77
![Language](https://img.shields.io/badge/language-C++-yellow.svg?style=flat-square)
88

@@ -189,6 +189,10 @@ If you prefer not to see these messages, you can disable this error logging beha
189189

190190
## Versions
191191

192+
##### 1.1.4 - 15th Oct 2025
193+
194+
- Bug fixes and code improvements
195+
192196
##### 1.1.3 - 31st May 2025
193197

194198
- Added saveToMemory() function to save audio data to bytes
@@ -263,6 +267,7 @@ Many thanks to the following people for their contributions to this library:
263267
- [BenjaminHinchliff](https://github.com/BenjaminHinchliff)
264268
- [BesselJ](https://github.com/BesselJ)
265269
- [cgraf78](https://github.com/cgraf78)
270+
- [danielzgtg](https://github.com/danielzgtg)
266271
- [emiro85](https://github.com/emiro85)
267272
- [encoded](https://github.com/encoded)
268273
- [gitelope](https://github.com/gitelope)
@@ -277,6 +282,7 @@ Many thanks to the following people for their contributions to this library:
277282
- [Sidelobe](https://github.com/Sidelobe)
278283
- [sschaetz](https://github.com/sschaetz)
279284
- [Yhcrown](https://github.com/Yhcrown)
285+
- [zhouchanglin-rr](https://github.com/zhouchanglin-rr)
280286

281287
## Want to Contribute?
282288

tests/FileWritingTests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ template <typename T>
1414
void writeTestAudioFile (int numChannels, int sampleRate, int bitDepth, AudioFileFormat format)
1515
{
1616
std::string sampleType;
17-
float sampleRateAsFloat = (float) sampleRate;
17+
float sampleRateAsFloat = static_cast<float> (sampleRate);
1818

1919
AudioFile<T> audioFileWriter;
2020

@@ -47,12 +47,12 @@ void writeTestAudioFile (int numChannels, int sampleRate, int bitDepth, AudioFil
4747
T sample;
4848

4949
if constexpr (std::numeric_limits<T>::is_integer && std::is_unsigned_v<T>)
50-
sample = (T)(((sinf (2. * M_PI * ((double) i / sampleRateAsFloat) * 440.) + 1.) / 2.) * maxValue);
50+
sample = static_cast<T> (((sin (2. * M_PI * (static_cast<double> (i) / static_cast<double> (sampleRateAsFloat)) * 440.) + 1.) / 2.) * maxValue);
5151
else
52-
sample = (T)(sinf (2. * M_PI * ((double) i / sampleRateAsFloat) * 440.) * maxValue);
52+
sample = static_cast<T> (sin (2. * M_PI * (static_cast<double> (i) / static_cast<double> (sampleRateAsFloat)) * 440.) * static_cast<double> (maxValue));
5353

5454
for (int k = 0; k < audioFileWriter.getNumChannels(); k++)
55-
audioFileWriter.samples[k][i] = sample * 0.5f;
55+
audioFileWriter.samples[k][i] = sample * static_cast<T> (0.5);
5656
}
5757

5858
audioFileWriter.setSampleRate (sampleRate);

tests/SampleConversionTests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ TEST_SUITE ("SampleConversionTests::8-bit Conversions - sampleToSignedByte()")
103103
REQUIRE_EQ (AudioSampleConverter<float>::sampleToSignedByte (-1.f), -127);
104104
REQUIRE_EQ (AudioSampleConverter<float>::sampleToSignedByte (std::numeric_limits<float>::lowest()), -127);
105105

106-
REQUIRE_EQ (AudioSampleConverter<double>::sampleToSignedByte (std::numeric_limits<float>::max()), 127);
106+
REQUIRE_EQ (AudioSampleConverter<double>::sampleToSignedByte (std::numeric_limits<double>::max()), 127);
107107
REQUIRE_EQ (AudioSampleConverter<double>::sampleToSignedByte (1.), 127);
108108
REQUIRE_EQ (AudioSampleConverter<double>::sampleToSignedByte (0.5), 63);
109109
REQUIRE_EQ (AudioSampleConverter<double>::sampleToSignedByte (0.), 0);
@@ -717,10 +717,10 @@ TEST_SUITE ("SampleConversionTests::32-bit Conversions")
717717
REQUIRE_EQ (AudioSampleConverter<float>::sampleToThirtyTwoBitInt (std::numeric_limits<float>::lowest()), -2147483647);
718718

719719
REQUIRE_EQ (AudioSampleConverter<double>::sampleToThirtyTwoBitInt (std::numeric_limits<double>::max()), 2147483647);
720-
REQUIRE_EQ (AudioSampleConverter<double>::sampleToThirtyTwoBitInt (1.f), 2147483647);
721-
REQUIRE_EQ (AudioSampleConverter<double>::sampleToThirtyTwoBitInt (0.5f), 1073741823);
722-
REQUIRE_EQ (AudioSampleConverter<double>::sampleToThirtyTwoBitInt (0.f), 0);
723-
REQUIRE_EQ (AudioSampleConverter<double>::sampleToThirtyTwoBitInt (-0.5f), -1073741823);
720+
REQUIRE_EQ (AudioSampleConverter<double>::sampleToThirtyTwoBitInt (1.), 2147483647);
721+
REQUIRE_EQ (AudioSampleConverter<double>::sampleToThirtyTwoBitInt (0.5), 1073741823);
722+
REQUIRE_EQ (AudioSampleConverter<double>::sampleToThirtyTwoBitInt (0.), 0);
723+
REQUIRE_EQ (AudioSampleConverter<double>::sampleToThirtyTwoBitInt (-0.5), -1073741823);
724724
REQUIRE_EQ (AudioSampleConverter<double>::sampleToThirtyTwoBitInt (-1.), -2147483647);
725725
REQUIRE_EQ (AudioSampleConverter<double>::sampleToThirtyTwoBitInt (std::numeric_limits<double>::lowest()), -2147483647);
726726
}

0 commit comments

Comments
 (0)