@@ -337,9 +337,7 @@ bool AudioFile<T>::decodeWaveFile (std::vector<uint8_t>& fileData)
337337
338338 if (bitDepth == 8 )
339339 {
340- int32_t sampleAsInt = (int32_t ) fileData[sampleIndex];
341- T sample = (T)(sampleAsInt - 128 ) / (T)128 .;
342-
340+ T sample = singleByteToSample (fileData[sampleIndex]);
343341 samples[channel].push_back (sample);
344342 }
345343 else if (bitDepth == 16 )
@@ -584,13 +582,12 @@ bool AudioFile<T>::saveToWaveFile (std::string filePath)
584582 {
585583 if (bitDepth == 8 )
586584 {
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]);
589586 fileData.push_back (byte);
590587 }
591588 else if (bitDepth == 16 )
592589 {
593- int16_t sampleAsInt = ( int16_t ) (samples[channel][i] * (T) 32768 . );
590+ int16_t sampleAsInt = sampleToSixteenBitInt (samples[channel][i]);
594591 addInt16ToFileData (fileData, sampleAsInt);
595592 }
596593 else if (bitDepth == 24 )
@@ -669,13 +666,12 @@ bool AudioFile<T>::saveToAiffFile (std::string filePath)
669666 {
670667 if (bitDepth == 8 )
671668 {
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]);
674670 fileData.push_back (byte);
675671 }
676672 else if (bitDepth == 16 )
677673 {
678- int16_t sampleAsInt = ( int16_t ) (samples[channel][i] * (T) 32768 . );
674+ int16_t sampleAsInt = sampleToSixteenBitInt (samples[channel][i]);
679675 addInt16ToFileData (fileData, sampleAsInt, Endianness::BigEndian);
680676 }
681677 else if (bitDepth == 24 )
@@ -865,7 +861,40 @@ int AudioFile<T>::getIndexOfString (std::vector<uint8_t>& source, std::string st
865861template <class T >
866862T AudioFile<T>::sixteenBitIntToSample (int16_t sample)
867863{
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;
869898}
870899
871900// ===========================================================
0 commit comments