@@ -337,9 +337,7 @@ bool AudioFile<T>::decodeWaveFile (std::vector<uint8_t>& fileData)
337
337
338
338
if (bitDepth == 8 )
339
339
{
340
- int32_t sampleAsInt = (int32_t ) fileData[sampleIndex];
341
- T sample = (T)(sampleAsInt - 128 ) / (T)128 .;
342
-
340
+ T sample = singleByteToSample (fileData[sampleIndex]);
343
341
samples[channel].push_back (sample);
344
342
}
345
343
else if (bitDepth == 16 )
@@ -584,13 +582,12 @@ bool AudioFile<T>::saveToWaveFile (std::string filePath)
584
582
{
585
583
if (bitDepth == 8 )
586
584
{
587
- int32_t sampleAsInt = ((samples[channel][i] * (T)128 .) + 128 .);
588
- uint8_t byte = (uint8_t )sampleAsInt;
585
+ uint8_t byte = sampleToSingleByte (samples[channel][i]);
589
586
fileData.push_back (byte);
590
587
}
591
588
else if (bitDepth == 16 )
592
589
{
593
- int16_t sampleAsInt = ( int16_t ) (samples[channel][i] * (T) 32768 . );
590
+ int16_t sampleAsInt = sampleToSixteenBitInt (samples[channel][i]);
594
591
addInt16ToFileData (fileData, sampleAsInt);
595
592
}
596
593
else if (bitDepth == 24 )
@@ -669,13 +666,12 @@ bool AudioFile<T>::saveToAiffFile (std::string filePath)
669
666
{
670
667
if (bitDepth == 8 )
671
668
{
672
- int32_t sampleAsInt = (int32_t )(samples[channel][i] * (T)128 .);
673
- uint8_t byte = (uint8_t )sampleAsInt;
669
+ uint8_t byte = sampleToSingleByte (samples[channel][i]);
674
670
fileData.push_back (byte);
675
671
}
676
672
else if (bitDepth == 16 )
677
673
{
678
- int16_t sampleAsInt = ( int16_t ) (samples[channel][i] * (T) 32768 . );
674
+ int16_t sampleAsInt = sampleToSixteenBitInt (samples[channel][i]);
679
675
addInt16ToFileData (fileData, sampleAsInt, Endianness::BigEndian);
680
676
}
681
677
else if (bitDepth == 24 )
@@ -865,7 +861,40 @@ int AudioFile<T>::getIndexOfString (std::vector<uint8_t>& source, std::string st
865
861
template <class T >
866
862
T AudioFile<T>::sixteenBitIntToSample (int16_t sample)
867
863
{
868
- return (T)sample / (T)32768 .;
864
+ return static_cast <T> (sample) / static_cast <T> (32768 .);
865
+ }
866
+
867
+ // =============================================================
868
+ template <class T >
869
+ int16_t AudioFile<T>::sampleToSixteenBitInt (T sample)
870
+ {
871
+ sample = clamp (sample, -1 ., 1 .);
872
+ return static_cast <int16_t > (sample * 32767 .);
873
+ }
874
+
875
+ // =============================================================
876
+ template <class T >
877
+ uint8_t AudioFile<T>::sampleToSingleByte (T sample)
878
+ {
879
+ sample = clamp (sample, -1 ., 1 .);
880
+ sample = (sample + 1 .) / 2 .;
881
+ return static_cast <uint8_t > (sample * 255 .);
882
+ }
883
+
884
+ // =============================================================
885
+ template <class T >
886
+ T AudioFile<T>::singleByteToSample (uint8_t sample)
887
+ {
888
+ return static_cast <T> (sample - 128 ) / static_cast <T> (128 .);
889
+ }
890
+
891
+ // =============================================================
892
+ template <class T >
893
+ T AudioFile<T>::clamp (T value, T minValue, T maxValue)
894
+ {
895
+ value = std::min (value, maxValue);
896
+ value = std::max (value, minValue);
897
+ return value;
869
898
}
870
899
871
900
// ===========================================================
0 commit comments