Skip to content

Commit 85077a6

Browse files
committed
Merge branch 'release/1.0.3'
2 parents a6430a0 + a611cca commit 85077a6

File tree

4 files changed

+80
-12
lines changed

4 files changed

+80
-12
lines changed

AudioFile.cpp

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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
865861
template <class T>
866862
T 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
//===========================================================

AudioFile.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,19 @@ class AudioFile
150150
int32_t fourBytesToInt (std::vector<uint8_t>& source, int startIndex, Endianness endianness = Endianness::LittleEndian);
151151
int16_t twoBytesToInt (std::vector<uint8_t>& source, int startIndex, Endianness endianness = Endianness::LittleEndian);
152152
int getIndexOfString (std::vector<uint8_t>& source, std::string s);
153+
154+
//=============================================================
153155
T sixteenBitIntToSample (int16_t sample);
156+
int16_t sampleToSixteenBitInt (T sample);
157+
158+
//=============================================================
159+
uint8_t sampleToSingleByte (T sample);
160+
T singleByteToSample (uint8_t sample);
161+
154162
uint32_t getAiffSampleRate (std::vector<uint8_t>& fileData, int sampleRateStartIndex);
155163
bool tenByteMatch (std::vector<uint8_t>& v1, int startIndex1, std::vector<uint8_t>& v2, int startIndex2);
156164
void addSampleRateToAiffData (std::vector<uint8_t>& fileData, uint32_t sampleRate);
165+
T clamp (T v1, T minValue, T maxValue);
157166

158167
//=============================================================
159168
void addStringToFileData (std::vector<uint8_t>& fileData, std::string s);

README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# AudioFile
22

33
<!-- Version and License Badges -->
4-
![Version](https://img.shields.io/badge/version-1.0.2-green.svg?style=flat-square)
4+
![Version](https://img.shields.io/badge/version-1.0.3-green.svg?style=flat-square)
55
![License](https://img.shields.io/badge/license-GPL-blue.svg?style=flat-square)
66
![Language](https://img.shields.io/badge/language-C++-yellow.svg?style=flat-square)
77

@@ -71,7 +71,24 @@ Usage
7171
buffer[0].resize (100000);
7272
buffer[1].resize (100000);
7373

74-
// 4. do something here to fill the buffer with samples
74+
// 4. do something here to fill the buffer with samples, e.g.
75+
76+
#include <math.h> // somewhere earler (for M_PI and sinf())
77+
78+
// then...
79+
80+
int numChannels = 2;
81+
int numSamplesPerChannel = 100000;
82+
float sampleRate = 44100.f;
83+
float frequency = 440.f;
84+
85+
for (int i = 0; i < numSamplesPerChannel; i++)
86+
{
87+
float sample = sinf (2. * M_PI * ((float) i / sampleRate) * frequency) ;
88+
89+
for (int channel = 0; channel < numChannels; channel++)
90+
buffer[channel][i] = sample * 0.5;
91+
}
7592

7693
// 5. Put into the AudioFile object
7794
bool ok = audioFile.setAudioBuffer (buffer);
@@ -121,6 +138,11 @@ This simply reflects the data type you would like to use to store the underlying
121138
Versions
122139
-------
123140

141+
##### 1.0.3 - 28th October 2018
142+
143+
- Bug fixes
144+
- Documentation updates
145+
124146
##### 1.0.2 - 6th June 2017
125147

126148
- Bug fixes
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

0 commit comments

Comments
 (0)