-
Notifications
You must be signed in to change notification settings - Fork 6
/
SoundFile.h
287 lines (202 loc) · 4.8 KB
/
SoundFile.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#ifndef WIN32
#include <string.h>
#endif
#include <stdio.h>
#include <math.h>
#include <IHandleSys.h>
#define TAGLIB_STATIC
#include <fileref.h>
#include <tag.h>
#include "mpeg/mpegfile.h"
#include "mpeg/xingheader.h"
#include "riff/wav/wavfile.h"
#include <tbytevector.h>
#define SOUNDTYPE_WAVE 0
#define SOUNDTYPE_MP3 1
HandleType_t g_SoundFileType;
// Because TagLib is hiding members from us we have to trick a little bit...
class SoundLib_WavFile : public TagLib::RIFF::WAV::File {
public:
float getSoundLength() {
unsigned int streamLength = 0;
for (TagLib::uint i = 0; i < chunkCount(); i++) {
if (chunkName(i) == "data") {
streamLength = chunkDataSize(i);
}
}
float byteRate = float(audioProperties()->bitrate()) * 125.0f;
return byteRate > 0.0f ? float(streamLength) / byteRate : 0.0f;
}
};
class SoundFile {
private:
TagLib::File* file;
TagLib::Tag* tag;
size_t type;
public:
SoundFile(char *path) {
file = NULL;
tag = NULL;
char *file_extension = strrchr(path, '.');
if (file_extension == NULL) {
return;
}
if (strcmp(file_extension, ".wav") == 0) {
file = new TagLib::RIFF::WAV::File(path);
type = SOUNDTYPE_WAVE;
}
else if (strcmp(file_extension, ".mp3") == 0) {
file = new TagLib::MPEG::File(path);
type = SOUNDTYPE_MP3;
}
else {
return;
}
loadTag();
}
~SoundFile() {
close();
}
bool isOpen() {
if (file == NULL) {
return false;
}
if (!file->isValid()) {
return false;
}
return true;
}
bool loadTag() {
if (tag == NULL) {
tag = file->tag();
return true;
}
return false;
}
size_t getSoundDuration() {
TagLib::AudioProperties *properties = file->audioProperties();
if (!properties) {
return -1;
}
if (type == SOUNDTYPE_WAVE) {
SoundLib_WavFile* f = (SoundLib_WavFile*)file;
return f->audioProperties()->length();
}
else {
TagLib::MPEG::File* f = (TagLib::MPEG::File*)file;
return f->audioProperties()->length();
}
return 0;
}
float getSoundDurationFloat() {
TagLib::AudioProperties *properties = file->audioProperties();
if (!properties) {
return -1;
}
if (type == SOUNDTYPE_WAVE) {
SoundLib_WavFile* f = (SoundLib_WavFile*)file;
return f->getSoundLength();
}
else {
TagLib::MPEG::File* f = (TagLib::MPEG::File*)file;
long first = f->firstFrameOffset();
f->seek(first);
TagLib::MPEG::Header firstHeader(f->readBlock(4));
int xingHeaderOffset = TagLib::MPEG::XingHeader::xingHeaderOffset(firstHeader.version(), firstHeader.channelMode());
f->seek(first + xingHeaderOffset);
TagLib::MPEG::XingHeader xingHeader(f->readBlock(16));
// Read the length and the bitrate from the Xing header.
if (xingHeader.isValid() && firstHeader.sampleRate() > 0 && xingHeader.totalFrames() > 0) {
double timePerFrame = double(firstHeader.samplesPerFrame()) / firstHeader.sampleRate();
return float(timePerFrame * xingHeader.totalFrames());
}
else {
float byteRate = (float)f->audioProperties()->bitrate() * 125.0f; // 1000 / 8 = 125 (optimization)
float length = (float)(f->length() - f->firstFrameOffset()) / byteRate;
return length;
}
}
return 0.0;
}
size_t getSoundBitRate() {
TagLib::AudioProperties *properties = file->audioProperties();
if (!properties) {
return -1;
}
return properties->bitrate();
}
size_t getSoundSamplingRate() {
TagLib::AudioProperties *properties = file->audioProperties();
if (!properties) {
return -1;
}
return properties->sampleRate();
}
void getSoundArtist(char *buf, size_t size) {
if (!tag) {
buf[0] = '\0';
return;
}
TagLib::String tl_str = tag->artist();
const char *str = tl_str.toCString(true);
strncpy(buf, str, size);
return;
}
void getSoundTitle(char *buf, size_t size) {
if (!tag) {
buf[0] = '\0';
return;
}
TagLib::String tl_str = tag->title();
const char *str = tl_str.toCString(true);
strncpy(buf, str, size);
return;
}
size_t getSoundNum() {
if (!tag) {
return -1;
}
return tag->track();
}
void getSoundAlbum(char *buf, size_t size) {
if (!tag) {
buf[0] = '\0';
return;
}
TagLib::String tl_str = tag->album();
const char *str = tl_str.toCString(true);
strncpy(buf, str, size);
return;
}
size_t getSoundYear() {
if (!tag) {
return -1;
}
return tag->year();
}
void getSoundComment(char *buf, size_t size) {
if (!tag) {
buf[0] = '\0';
return;
}
TagLib::String tl_str = tag->comment();
const char *str = tl_str.toCString(true);
strncpy(buf, str, size);
return;
}
void getSoundGenre(char *buf, size_t size) {
if (!tag) {
buf[0] = '\0';
return;
}
TagLib::String tl_str = tag->genre();
const char *str = tl_str.toCString(true);
strncpy(buf, str, size);
return;
}
private:
void close() {
delete file;
return;
}
};