-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaglibogg.cpp
441 lines (394 loc) · 12.8 KB
/
taglibogg.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/**
* the ride never ends
*/
#include <oggfile.h>
#include <oggflacfile.h>
#include <oggpage.h>
#include <oggpageheader.h>
#include <vorbisfile.h>
#include <vorbisproperties.h>
#include <opusfile.h>
#include <opusproperties.h>
#include <flacfile.h>
#include <flacmetadatablock.h>
#include <flacpicture.h>
#include <flacproperties.h>
#include <xiphcomment.h>
/**
* TagLib handles oggs based on their codec
* so let's make some unnecessary hoops for us to jump through
* to abstract it into one TagLibOGG class for PHP rather than three
*/
#define _OGG_VORBIS_ 0x01
#define _OGG_OPUS_ 0x02
#define _OGG_FLAC_ 0x03
#define _OGG_SPEEX_ 0x04
struct tagliboggfile_object {
zend_object std;
unsigned long type;
bool initialized;
TagLib::Ogg::Vorbis::File *vorbisfile;
TagLib::Ogg::Opus::File *opusfile;
TagLib::Ogg::FLAC::File *flacfile;
TagLib::Ogg::XiphComment *xiphcomment;
};
/**
* Memory Management
*/
zend_object_handlers tagliboggfile_object_handlers;
void tagliboggfile_free_storage(void *object TSRMLS_DC) {
tagliboggfile_object *obj = (tagliboggfile_object *)object;
zend_hash_destroy(obj->std.properties);
FREE_HASHTABLE(obj->std.properties);
efree(obj);
}
zend_object_value tagliboggfile_create_handler(zend_class_entry *type
TSRMLS_DC) {
zval *tmp;
zend_object_value retval;
tagliboggfile_object *obj =
(tagliboggfile_object *)emalloc(sizeof(tagliboggfile_object));
memset(obj, 0, sizeof(tagliboggfile_object));
obj->std.ce = type;
ALLOC_HASHTABLE(obj->std.properties);
zend_hash_init(obj->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
#if PHP_VERSION_ID < 50399
zend_hash_copy(obj->std.properties, &type->default_properties,
(copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));
#else
object_properties_init((zend_object *)&(obj->std.properties), type);
#endif
retval.handle = zend_objects_store_put(obj, NULL, tagliboggfile_free_storage,
NULL TSRMLS_CC);
retval.handlers = &tagliboggfile_object_handlers;
return retval;
}
/**
* End Memory Management
*/
/*
* class entry
*/
zend_class_entry *taglibogg_class_entry;
/**
* Class Constants
* _defineclassconstant macro defined in taglib.cpp
*/
void taglibogg_register_constants(zend_class_entry *ce) {
_defineclassconstant(VORBIS, _OGG_VORBIS_);
_defineclassconstant(OPUS, _OGG_OPUS_);
_defineclassconstant(FLAC, _OGG_FLAC_);
_defineclassconstant(SPEEX, _OGG_SPEEX_);
}
/**
* public function __construct( $fileName, $type = TagLibOGG::VORBIS )
*/
PHP_METHOD(TagLibOGG, __construct) {
zval *fileName;
long codec = _OGG_VORBIS_;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &fileName,
&codec) == FAILURE) {
php_exception("Expected filename in constructor");
RETURN_FALSE;
}
if (Z_TYPE_P(fileName) != IS_STRING) {
php_exception("Expected filename in constructor to be a string.");
RETURN_FALSE;
}
tagliboggfile_object *thisobj =
(tagliboggfile_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
TagLib::FileName oggFileName = (TagLib::FileName)Z_STRVAL_P(fileName);
if (!TagLib::File::isReadable(oggFileName)) {
char msg[sizeof(oggFileName) + 25];
php_sprintf(msg, "%s cannot be open or read", oggFileName);
php_exception((const char *)msg);
RETURN_FALSE;
}
if (!TagLib::File::isWritable(oggFileName)) {
char msg[sizeof(oggFileName) + 22];
php_sprintf(msg, "%s cannot be written to", oggFileName);
php_exception((const char *)msg);
RETURN_FALSE;
}
switch (codec) {
case _OGG_VORBIS_: {
thisobj->type = _OGG_VORBIS_;
thisobj->vorbisfile = new TagLib::Ogg::Vorbis::File(oggFileName);
if (taglib_error() || !thisobj->vorbisfile->isValid()) {
delete thisobj->vorbisfile;
char msg[sizeof(oggFileName) + 25];
php_sprintf(msg, "%s cannot be open or read", oggFileName);
php_exception((const char *)msg);
RETURN_FALSE;
} else {
thisobj->xiphcomment = thisobj->vorbisfile->tag();
}
} break;
case _OGG_OPUS_: {
thisobj->type = _OGG_OPUS_;
thisobj->opusfile = new TagLib::Ogg::Opus::File(oggFileName);
if (taglib_error() || !thisobj->opusfile->isValid()) {
delete thisobj->opusfile;
char msg[sizeof(oggFileName) + 25];
php_sprintf(msg, "%s cannot be open or read", oggFileName);
php_exception((const char *)msg);
RETURN_FALSE;
} else {
thisobj->xiphcomment = thisobj->opusfile->tag();
}
} break;
case _OGG_FLAC_: {
thisobj->type = _OGG_FLAC_;
thisobj->flacfile = new TagLib::Ogg::FLAC::File(oggFileName);
if (taglib_error() || !thisobj->flacfile->isValid()) {
delete thisobj->flacfile;
char msg[sizeof(oggFileName) + 25];
php_sprintf(msg, "%s cannot be open or read", oggFileName);
php_exception((const char *)msg);
RETURN_FALSE;
} else {
thisobj->xiphcomment = thisobj->flacfile->tag();
}
} break;
case _OGG_SPEEX_:
php_error(E_DEPRECATED, "Speex is deprecated.");
/* fallthrough */
default:
php_exception("Unrecognized or unsupported option for $codec in "
"TagLibOGG::__construct()");
RETURN_FALSE;
}
if (taglib_error()) {
switch (codec) {
case _OGG_VORBIS_:
delete thisobj->vorbisfile;
break;
case _OGG_OPUS_:
delete thisobj->opusfile;
break;
case _OGG_FLAC_:
delete thisobj->flacfile;
break;
}
php_exception("taglib returned error");
RETURN_FALSE;
}
thisobj->initialized = true;
}
/**
* public function getAudioProperties()
*/
PHP_METHOD(TagLibOGG, getAudioProperties) {
tagliboggfile_object *thisobj =
(tagliboggfile_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (!thisobj->initialized) {
RETURN_FALSE;
}
array_init(return_value);
switch (thisobj->type) {
case _OGG_VORBIS_: {
TagLib::Vorbis::Properties *audioProperties =
thisobj->vorbisfile->audioProperties();
add_assoc_long(return_value, "length", audioProperties->length());
add_assoc_long(return_value, "bitrate", audioProperties->bitrate());
add_assoc_long(return_value, "sampleRate", audioProperties->sampleRate());
add_assoc_long(return_value, "channels", audioProperties->channels());
add_assoc_long(return_value, "vorbisVersion",
audioProperties->vorbisVersion());
add_assoc_long(return_value, "bitrateMaximum",
audioProperties->bitrateMaximum());
add_assoc_long(return_value, "bitrateNominal",
audioProperties->bitrateNominal());
add_assoc_long(return_value, "bitrateMinimum",
audioProperties->bitrateMinimum());
} break;
case _OGG_OPUS_: {
TagLib::Ogg::Opus::Properties *audioProperties =
thisobj->opusfile->audioProperties();
add_assoc_long(return_value, "length", audioProperties->length());
add_assoc_long(return_value, "bitrate", audioProperties->bitrate());
add_assoc_long(return_value, "sampleRate", audioProperties->sampleRate());
add_assoc_long(return_value, "channels", audioProperties->channels());
add_assoc_long(return_value, "opusVersion", audioProperties->opusVersion());
add_assoc_long(return_value, "inputSampleRate",
audioProperties->inputSampleRate());
} break;
case _OGG_FLAC_: {
TagLib::Ogg::FLAC::Properties *audioProperties =
thisobj->flacfile->audioProperties();
add_assoc_long(return_value, "length", audioProperties->length());
add_assoc_long(return_value, "bitrate", audioProperties->bitrate());
add_assoc_long(return_value, "sampleRate", audioProperties->sampleRate());
add_assoc_long(return_value, "channels", audioProperties->channels());
add_assoc_long(return_value, "sampleWidth", audioProperties->sampleWidth());
add_assoc_long(return_value, "sampleFrames",
audioProperties->sampleFrames());
// add_assoc_string(return_value, "signature",
// audioProperties->signature().data(), 1);
} break;
}
}
/**
* public function hasXiphComment()
*/
PHP_METHOD(TagLibOGG, hasXiphComment) {
tagliboggfile_object *thisobj =
(tagliboggfile_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (!thisobj->initialized) {
RETURN_FALSE;
}
if (thisobj->xiphcomment->fieldCount() > 0) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/**
* public function getXiphComment()
*/
PHP_METHOD(TagLibOGG, getXiphComment) {
tagliboggfile_object *thisobj =
(tagliboggfile_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (!thisobj->initialized) {
RETURN_FALSE;
}
array_init(return_value);
TagLib::PropertyMap propMap = thisobj->xiphcomment->properties();
for (TagLib::Map<TagLib::String, TagLib::StringList>::Iterator property =
propMap.begin();
property != propMap.end(); property++) {
add_assoc_string(return_value, property->first.toCString(),
(char *)(property->second.toString().toCString()), 1);
}
}
PHP_METHOD(TagLibOGG, stripTags) {
tagliboggfile_object *thisobj =
(tagliboggfile_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (!thisobj->initialized) {
RETURN_FALSE;
}
TagLib::PropertyMap propMap = thisobj->xiphcomment->properties();
int i = 0;
int size = propMap.size();
TagLib::String keys[size];
for (TagLib::Map<TagLib::String, TagLib::StringList>::Iterator property =
propMap.begin();
property != propMap.end(); property++) {
keys[i] = property->first;
i++;
}
for (i = 0; i < size; i++) {
propMap.erase(keys[i]);
}
thisobj->xiphcomment->setProperties(propMap);
switch (thisobj->type) {
case _OGG_VORBIS_:
if (thisobj->vorbisfile->save()) {
RETURN_TRUE;
return;
}
break;
case _OGG_OPUS_:
if (thisobj->opusfile->save()) {
RETURN_TRUE;
return;
}
break;
case _OGG_FLAC_:
if (thisobj->flacfile->save()) {
RETURN_TRUE;
return;
}
break;
}
RETURN_FALSE;
}
/**
* returns FALSE if something went wrong,
* returns TRUE if everything went through */
PHP_METHOD(TagLibOGG, setXiphComment) {
tagliboggfile_object *thisobj =
(tagliboggfile_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (!thisobj->initialized) {
RETURN_FALSE;
}
zval *newProperties;
zend_bool overwrite_existing_tags = true;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|b", &newProperties,
&overwrite_existing_tags) == FAILURE) {
RETURN_FALSE;
}
if (Z_TYPE_P(newProperties) != IS_ARRAY) {
php_error(
E_WARNING,
"TagLibOGG::setXiphComment expects associative array of strings!");
RETURN_FALSE;
}
HashTable *hIndex = Z_ARRVAL_P(newProperties);
HashPosition pointer;
zval **data;
for (zend_hash_internal_pointer_reset_ex(hIndex, &pointer);
zend_hash_get_current_data_ex(hIndex, (void **)&data, &pointer) ==
SUCCESS;
zend_hash_move_forward_ex(hIndex, &pointer)) {
char *key;
uint key_length, key_type;
ulong index;
key_type = zend_hash_get_current_key_ex(hIndex, &key, &key_length, &index,
0, &pointer);
if (key_type != HASH_KEY_IS_STRING) {
php_error(
E_WARNING,
"TagLibOGG::setXiphComment expects associative array of strings!");
RETURN_FALSE;
return;
}
if (Z_TYPE_PP(data) != IS_STRING) {
php_error(
E_WARNING,
"TagLibOGG::setXiphComment expects associative array of strings!");
RETURN_FALSE;
return;
}
TagLib::String *destKey = new TagLib::String((const char *)key);
TagLib::String *destValue = new TagLib::String(Z_STRVAL_PP(data));
thisobj->xiphcomment->addField(*destKey, *destValue,
(bool)overwrite_existing_tags);
if (taglib_error()) {
php_error(E_WARNING, "XiphComment::addField() failed.");
return;
}
}
switch (thisobj->type) {
case _OGG_VORBIS_:
if (thisobj->vorbisfile->save()) {
RETURN_TRUE;
return;
}
break;
case _OGG_OPUS_:
if (thisobj->opusfile->save()) {
RETURN_TRUE;
return;
}
break;
case _OGG_FLAC_:
if (thisobj->flacfile->save()) {
RETURN_TRUE;
return;
}
break;
}
taglib_error();
RETURN_FALSE;
}
/**
* Now we assemble the above defined methods into the class or something
*/
static zend_function_entry php_taglibogg_methods[] = {
PHP_ME(TagLibOGG, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(TagLibOGG, getAudioProperties, NULL, ZEND_ACC_PUBLIC)
PHP_ME(TagLibOGG, hasXiphComment, NULL, ZEND_ACC_PUBLIC)
PHP_ME(TagLibOGG, getXiphComment, NULL, ZEND_ACC_PUBLIC)
PHP_ME(TagLibOGG, setXiphComment, NULL, ZEND_ACC_PUBLIC)
PHP_ME(TagLibOGG, stripTags, NULL, ZEND_ACC_PUBLIC){NULL, NULL, NULL}};