-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAudioDecoder.cpp
187 lines (163 loc) · 4.5 KB
/
AudioDecoder.cpp
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
#include "AudioDecoder.h"
#include "AudioStream.h"
#include "AudioDecoderWav.h"
#ifdef AUDIO_USE_LIBVORBIS
#include "AudioDecoderVorbis.h"
#endif
#include "AudioDecoderVorbisStb.h"
#include "AudioDecoderFlac.h"
#include "AudioDecoderMp3.h"
#include "AudioCommon.h"
#include "platform/CCFileUtils.h"
#include <algorithm>
#include <cctype>
using namespace audio;
static std::unordered_map<std::string, Decoder::DecoderType> DecoderTypeMap = {
{"wav", Decoder::DecoderType::WAV},
{"ogg", Decoder::DecoderType::VORBIS},
{"flac", Decoder::DecoderType::FLAC},
{"mp3", Decoder::DecoderType::MP3},
};
std::string Decoder::AudioInfo::toString()
{
return string_format("Frame: %d, Freq: %d, Time: %.3f, Channel: %d, BitDepth: %d",
(int)totalFrames, (int)sampleRate, (float)getTotalTime(),
(int)channelCount, (int)getBitDepth());
}
Decoder::Decoder(size_t bufferSize, DecoderType type)
: decoderType(type)
, bufferSize(bufferSize)
{
buffer = new int8_t[bufferSize];
}
Decoder::~Decoder()
{
delete[] buffer;
}
Decoder::DecoderType Decoder::getDecoderTypeFromPath(const std::string& path)
{
// get ext in lower case
auto ext = cocos2d::FileUtils::getInstance()->getFileExtension(path);
// remove dot
if (!ext.empty())
ext = ext.substr(1);
const auto it = DecoderTypeMap.find(ext);
if (it != DecoderTypeMap.end())
return it->second;
return Decoder::DecoderType::UNKNOWN;
}
Decoder* Decoder::createFromStream(Stream* s, size_t bufferSize, DecoderType type)
{
if (!s)
return nullptr;
switch (type)
{
case Decoder::DecoderType::WAV:
return DecoderWav::create(s, bufferSize);
case Decoder::DecoderType::VORBIS:
#ifdef AUDIO_USE_LIBVORBIS
return DecoderVorbis::create(s, bufferSize);
#else
return DecoderStbVorbis::create(s, bufferSize);
#endif
case Decoder::DecoderType::FLAC:
return DecoderFlac::create(s, bufferSize);
case Decoder::DecoderType::MP3:
return DecoderMp3::create(s, bufferSize);
case Decoder::DecoderType::UNKNOWN:
default:;
}
return nullptr;
}
Decoder* Decoder::createFromFile(const std::string& path, size_t bufferSize, DecoderType type)
{
if (type == DecoderType::UNKNOWN)
type = getDecoderTypeFromPath(path);
return createFromStream(Stream::createFromFile(path), bufferSize, type);
}
Decoder* Decoder::createFromLocalFile(const std::string& path, size_t bufferSize, DecoderType type)
{
if (type == DecoderType::UNKNOWN)
type = getDecoderTypeFromPath(path);
return createFromStream(Stream::createFromLocalFile(path), bufferSize, type);
}
Decoder* Decoder::createFromStringData(const std::string& str, size_t bufferSize, DecoderType type)
{
return createFromStream(Stream::createFromStringData(str), bufferSize, type);
}
Decoder* Decoder::createFromSoundData(SoundData* soundData, size_t bufferSize, DecoderType type)
{
return createFromStream(Stream::createFromSoundData(soundData), bufferSize, type);
}
int8_t* Decoder::getBuffer() const
{
return buffer;
}
bool Decoder::seekTime(double seconds)
{
return seek(seconds * getSampleRate());
}
Decoder* Decoder::clone()
{
//BUG: current implementation dose not support play from the same stream
return createFromStream(stream, bufferSize, decoderType);
}
int64_t Decoder::decode()
{
int64_t toReadSize = bufferSize;
const int64_t tCurSample = tell();
const int64_t tSampleToRead = bufferSize / getBytesPerFrame();
const int64_t exceed = tCurSample + tSampleToRead - getLoopEnd();
if (exceed > 0)
toReadSize -= exceed * getBytesPerFrame();
toReadSize = std::max<int64_t>(toReadSize, 0);
const int64_t size = decode((char*)buffer, toReadSize);
if (size > 0)
bufferPosition = tCurSample;
if (exceed > 0 && size == toReadSize)
eof = true;
return size;
}
int Decoder::getBufferSize() const
{
return bufferSize;
}
void Decoder::setLoopingPoint(double tStart, double tEnd)
{
if (tStart >= 0.0 && tEnd > 0.0 && tStart < tEnd)
{
const auto sampleRate = getSampleRate();
const auto totalFrames = getTotalFrames();
const auto start = std::min(int64_t(sampleRate * tStart), totalFrames);
const auto end = std::min(int64_t(sampleRate * tEnd), totalFrames);
if (start < end)
{
loopStart = start;
loopEnd = end;
}
}
}
double Decoder::getLoopingStart()
{
return (double)getLoopStart() / getSampleRate();
}
double Decoder::getLoopingEnd()
{
return (double)getLoopEnd() / getSampleRate();
}
int64_t Decoder::getLoopStart() const
{
return loopStart;
}
int64_t Decoder::getLoopEnd() const
{
return loopEnd == 0 ? getTotalFrames() : loopEnd;
}
bool Decoder::rewind()
{
return seek(getLoopStart());
}
bool Decoder::isFinished()
{
return eof;
}