-
Notifications
You must be signed in to change notification settings - Fork 0
/
olcPGEX_AudioConverter.h
214 lines (190 loc) · 6.23 KB
/
olcPGEX_AudioConverter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#pragma once
#include "olcPixelGameEngine.h"
#include "olcPGEX_Sound.h"
namespace olc {
//HEADER
class AudioConvert : public PGEX {
public:
static int LoadAudioSample(std::string sWavFile, olc::ResourcePack *pack = nullptr);
private:
static olc::SOUND::AudioSample LoadFromFile(std::string sWavFile, olc::ResourcePack *pack);
static float convertPCMSample(uint64_t sample, int bitDepth);
static float convertALAWSample(uint8_t sample);
static float convertULAWSample(uint8_t sample);
};
}
//IMPLEMENTATION
#ifdef OLC_PGEX_AUDIOCONVERT
#undef OLC_PGEX_AUDIOCONVERT
int olc::AudioConvert::LoadAudioSample(std::string sWavFile, olc::ResourcePack *pack) {
olc::SOUND::AudioSample a = LoadFromFile(sWavFile, pack);
if (a.bSampleValid)
{
olc::vecAudioSamples.push_back(a);
return (unsigned int)vecAudioSamples.size();
}
else
return -1;
}
olc::SOUND::AudioSample olc::AudioConvert::LoadFromFile(std::string sWavFile, olc::ResourcePack *pack) {
auto ReadWave = [&](std::istream &is)
{
SOUND::AudioSample sample;
sample.bSampleValid = false;
char dump[4];
is.read(dump, sizeof(char) * 4); // Read "RIFF"
if (strncmp(dump, "RIFF", 4) != 0) return sample;
is.read(dump, sizeof(char) * 4); // Not Interested
is.read(dump, sizeof(char) * 4); // Read "WAVE"
if (strncmp(dump, "WAVE", 4) != 0) return sample;
// Read Wave description chunk
is.read(dump, sizeof(char) * 4); // Read "fmt "
unsigned int nHeaderSize = 0;
is.read((char*)&nHeaderSize, sizeof(unsigned int)); // Not Interested
is.read((char*)&sample.wavHeader, nHeaderSize);// sizeof(WAVEFORMATEX)); // Read Wave Format Structure chunk
// Note the -2, because the structure has 2 bytes to indicate its own size
// which are not in the wav file
// Search for audio data chunk
uint32_t nChunksize = 0;
is.read(dump, sizeof(char) * 4); // Read chunk header
is.read((char*)&nChunksize, sizeof(uint32_t)); // Read chunk size
while (strncmp(dump, "data", 4) != 0)
{
// Not audio data, so just skip it
//std::fseek(f, nChunksize, SEEK_CUR);
is.seekg(nChunksize, std::istream::cur);
is.read(dump, sizeof(char) * 4);
is.read((char*)&nChunksize, sizeof(uint32_t));
}
// Finally got to data, so read it all in and convert to float samples
sample.nSamples = nChunksize / (sample.wavHeader.nChannels * (sample.wavHeader.wBitsPerSample >> 3));
sample.nChannels = sample.wavHeader.nChannels;
// Create floating point buffer to hold audio sample
float *fSample = new float[sample.nSamples * sample.nChannels];
float *pSample = fSample;
// Read in audio data and normalise
for (long i = 0; i < sample.nSamples; i++)
{
for (int c = 0; c < sample.nChannels; c++)
{
if (!is.eof())
{
if (sample.wavHeader.wFormatTag == 0x1) { //PCM
uint64_t s = 0;
is.read((char*)&s, sample.wavHeader.wBitsPerSample / 8);
*pSample = convertPCMSample(s, sample.wavHeader.wBitsPerSample);
pSample++;
}
else if (sample.wavHeader.wFormatTag == 0x3) { //IEEE_FLOAT
if (sample.wavHeader.wBitsPerSample == 32) {
float s = 0;
is.read((char*)&s, 4);
*pSample = s;
pSample++;
}
else if (sample.wavHeader.wBitsPerSample == 64) {
double s = 0;
is.read((char*)&s, 8);
*pSample = float(s);
pSample++;
}
}
else if (sample.wavHeader.wFormatTag == 0x6) { //A-LAW
uint8_t s = 0;
is.read((char*)&s, 1);
*pSample = convertALAWSample(s);
pSample++;
}
else if (sample.wavHeader.wFormatTag == 0x7) {
uint8_t s = 0;
is.read((char*)&s, 1);
*pSample = convertULAWSample(s);
pSample++;
}
}
}
}
int resampledLength = sample.nSamples / float(sample.wavHeader.nSamplesPerSec) * 44100;
sample.fSample = new float[resampledLength * sample.nChannels];
for (int i = 0; i < resampledLength; i++) {
float samplePos = i / 44100.f * float(sample.wavHeader.nSamplesPerSec);
int samplePosI = floor(samplePos);
float samplePosF = samplePos - samplePosI;
for (int c = 0; c < sample.nChannels; c++) {
float sampleA = fSample[samplePosI * sample.nChannels + c];
float sampleB = sampleA;
if (i != resampledLength - 1)
sampleB = fSample[(samplePosI + 1) * sample.nChannels + c];
sample.fSample[i * sample.nChannels + c] = sampleA * (1 - samplePosF) + sampleB * samplePosF;
}
}
sample.nSamples = resampledLength;
delete[] fSample;
// All done, flag sound as valid
sample.bSampleValid = true;
return sample;
};
if (pack != nullptr)
{
olc::ResourcePack::sEntry entry = pack->GetStreamBuffer(sWavFile);
std::istream is(&entry);
return ReadWave(is);
}
else
{
// Read from file
std::ifstream ifs(sWavFile, std::ifstream::binary);
if (ifs.is_open())
{
return ReadWave(ifs);
}
else {
olc::SOUND::AudioSample sample;
sample.bSampleValid = false;
return sample;
}
}
}
inline float olc::AudioConvert::convertPCMSample(uint64_t sample, int bitDepth)
{
if (bitDepth == 8)
return float(sample) / UCHAR_MAX;
bool sign = sample & (1 << (bitDepth - 1));
uint64_t newSample = sample & ~(1 << (bitDepth - 1));
if (sign)
newSample = ((~newSample) & (((1 << (bitDepth - 1)) - 1))) + 1;
float result = (!sign * 2 - 1) * float(newSample) / ((1 << bitDepth - 1) - 1);
if (result < -1)
return -1;
if (result > 1)
return 1;
return result;
}
inline float olc::AudioConvert::convertALAWSample(uint8_t sample)
{
uint16_t t, seg;
sample ^= 0x55;
t = sample & 0x7f;
if (t < 16)
t = (t << 4) + 8;
else {
seg = (t >> 4) & 0x07;
t = ((t & 0x0f) << 4) + 0x108;
t <<= seg - 1;
}
return ((sample & 0x80) ? t : -t) / float(SHRT_MAX);
}
inline float olc::AudioConvert::convertULAWSample(uint8_t sample)
{
uint8_t sign = 0, position = 0;
int16_t decoded = 0;
sample = ~sample;
if (sample & 0x80) {
sample &= ~(1 << 7);
sign = -1;
}
position = ((sample & 0xf0) >> 4) + 5;
decoded = ((1 << position) | ((sample & 0x0f) << (position - 4)) | (1 << (position - 5))) - 33;
return ((sign == 0) ? decoded : -decoded) / float((1 << 13) - 1);
}
#endif