| 1 | #define PERL_NO_GET_CONTEXT |
| 2 | #include "EXTERN.h" |
| 3 | #include "perl.h" |
| 4 | #include "XSUB.h" |
| 5 | |
| 6 | #include "ppport.h" |
| 7 | |
| 8 | /* op_free is both Perl_op_free and a function in opusfile */ |
| 9 | #undef op_free |
| 10 | #include <opus/opusfile.h> |
| 11 | |
| 12 | #include "const-c.inc" |
| 13 | |
| 14 | typedef OggOpusFile* Audio__Opusfile; |
| 15 | typedef const OpusHead* Audio__Opusfile__Head; |
| 16 | typedef const OpusTags* Audio__Opusfile__Tags; |
| 17 | typedef const OpusPictureTag* Audio__Opusfile__PictureTag; |
| 18 | |
| 19 | MODULE = Audio::Opusfile PACKAGE = Audio::Opusfile PREFIX = op_ |
| 20 | |
| 21 | PROTOTYPES: ENABLE |
| 22 | INCLUDE: const-xs.inc |
| 23 | |
| 24 | Audio::Opusfile |
| 25 | op_open_file(path) |
| 26 | const char *path; |
| 27 | PREINIT: |
| 28 | int err; |
| 29 | C_ARGS: |
| 30 | path, &err |
| 31 | POSTCALL: |
| 32 | if(err) |
| 33 | croak("op_open_file returned error %d\n", err); |
| 34 | |
| 35 | Audio::Opusfile |
| 36 | op_open_memory(const char *data, size_t length(data)) |
| 37 | PREINIT: |
| 38 | int err; |
| 39 | C_ARGS: |
| 40 | data, XSauto_length_of_data, &err |
| 41 | POSTCALL: |
| 42 | if(err) |
| 43 | croak("op_open_memory returned error %d\n", err); |
| 44 | |
| 45 | bool |
| 46 | op_test(const char *data, size_t length(data)) |
| 47 | PROTOTYPE: $ |
| 48 | PREINIT: |
| 49 | int ret; |
| 50 | CODE: |
| 51 | ret = op_test (NULL, data, XSauto_length_of_data); |
| 52 | if(ret < 0 && ret != OP_ENOTFORMAT && ret != OP_EBADHEADER) |
| 53 | croak("op_test returned error %d\n", RETVAL); |
| 54 | RETVAL = !ret; |
| 55 | OUTPUT: |
| 56 | RETVAL |
| 57 | |
| 58 | |
| 59 | void |
| 60 | DESTROY(of) |
| 61 | Audio::Opusfile of; |
| 62 | CODE: |
| 63 | op_free(of); |
| 64 | |
| 65 | bool |
| 66 | op_seekable(of) |
| 67 | Audio::Opusfile of; |
| 68 | |
| 69 | int |
| 70 | op_link_count(of) |
| 71 | Audio::Opusfile of; |
| 72 | |
| 73 | int |
| 74 | op_serialno(of, li = -1) |
| 75 | Audio::Opusfile of; |
| 76 | int li; |
| 77 | |
| 78 | # op_channel_count is not exported; it can be obtained via op_head |
| 79 | |
| 80 | long |
| 81 | op_raw_total(of, li = -1) |
| 82 | Audio::Opusfile of; |
| 83 | int li; |
| 84 | POSTCALL: |
| 85 | if(RETVAL < 0) |
| 86 | croak("op_current_link returned error %ld\n", RETVAL); |
| 87 | |
| 88 | long |
| 89 | op_pcm_total(of, li = -1) |
| 90 | Audio::Opusfile of; |
| 91 | int li; |
| 92 | POSTCALL: |
| 93 | if(RETVAL < 0) |
| 94 | croak("op_current_link returned error %ld\n", RETVAL); |
| 95 | |
| 96 | Audio::Opusfile::Head |
| 97 | op_head(of, li = -1) |
| 98 | Audio::Opusfile of; |
| 99 | int li; |
| 100 | |
| 101 | Audio::Opusfile::Tags |
| 102 | op_tags(of, li = -1) |
| 103 | Audio::Opusfile of; |
| 104 | int li; |
| 105 | |
| 106 | int |
| 107 | op_current_link(of) |
| 108 | Audio::Opusfile of; |
| 109 | POSTCALL: |
| 110 | if(RETVAL < 0) |
| 111 | croak("op_current_link returned error %ld\n", RETVAL); |
| 112 | |
| 113 | int |
| 114 | op_bitrate(of, li = -1) |
| 115 | Audio::Opusfile of; |
| 116 | int li; |
| 117 | POSTCALL: |
| 118 | if(RETVAL < 0) |
| 119 | croak("op_bitrate returned error %ld\n", RETVAL); |
| 120 | |
| 121 | long |
| 122 | op_bitrate_instant(of) |
| 123 | Audio::Opusfile of; |
| 124 | POSTCALL: |
| 125 | if(RETVAL < 0) |
| 126 | croak("op_bitrate_instant returned error %ld\n", RETVAL); |
| 127 | |
| 128 | long |
| 129 | op_raw_tell(of) |
| 130 | Audio::Opusfile of; |
| 131 | POSTCALL: |
| 132 | if(RETVAL < 0) |
| 133 | croak("op_raw_tell returned error %ld\n", RETVAL); |
| 134 | |
| 135 | long |
| 136 | op_pcm_tell(of) |
| 137 | Audio::Opusfile of; |
| 138 | POSTCALL: |
| 139 | if(RETVAL < 0) |
| 140 | croak("op_pcm_tell returned error %ld\n", RETVAL); |
| 141 | |
| 142 | NO_OUTPUT int |
| 143 | op_raw_seek(of, offset) |
| 144 | Audio::Opusfile of; |
| 145 | long offset; |
| 146 | POSTCALL: |
| 147 | if(RETVAL) |
| 148 | croak("op_raw_seek returned error %d\n", RETVAL); |
| 149 | |
| 150 | NO_OUTPUT int |
| 151 | op_pcm_seek(of, offset) |
| 152 | Audio::Opusfile of; |
| 153 | long offset; |
| 154 | POSTCALL: |
| 155 | if(RETVAL) |
| 156 | croak("op_pcm_seek returned error %d\n", RETVAL); |
| 157 | |
| 158 | NO_OUTPUT int |
| 159 | op_set_gain_offset(of, gain_type, gain_offset_q8) |
| 160 | Audio::Opusfile of; |
| 161 | int gain_type; |
| 162 | int gain_offset_q8; |
| 163 | POSTCALL: |
| 164 | if(RETVAL) |
| 165 | croak("op_set_gain_offset returned error %d\n", RETVAL); |
| 166 | |
| 167 | void |
| 168 | op_set_dither_enabled(of, enabled) |
| 169 | Audio::Opusfile of; |
| 170 | int enabled; |
| 171 | |
| 172 | |
| 173 | void |
| 174 | op_read(of, bufsize = 1024 * 1024) |
| 175 | Audio::Opusfile of; |
| 176 | int bufsize; |
| 177 | PREINIT: |
| 178 | opus_int16* buf; |
| 179 | int li, ret, chans, i; |
| 180 | PPCODE: |
| 181 | Newx(buf, bufsize, opus_int16); |
| 182 | ret = op_read(of, buf, bufsize, &li); |
| 183 | if(ret < 0) |
| 184 | croak("op_read returned error %d\n", ret); |
| 185 | chans = op_channel_count(of, li); |
| 186 | EXTEND(SP, chans * ret + 1); |
| 187 | PUSHs(sv_2mortal(newSViv(li))); |
| 188 | for(i = 0 ; i < chans * ret ; i++) |
| 189 | PUSHs(sv_2mortal(newSViv(buf[i]))); |
| 190 | |
| 191 | void |
| 192 | op_read_float(of, bufsize = 1024 * 1024) |
| 193 | Audio::Opusfile of; |
| 194 | int bufsize; |
| 195 | PREINIT: |
| 196 | float* buf; |
| 197 | int li, ret, chans, i; |
| 198 | PPCODE: |
| 199 | Newx(buf, bufsize, float); |
| 200 | ret = op_read_float(of, buf, bufsize, &li); |
| 201 | if(ret < 0) |
| 202 | croak("op_read_float returned error %d\n", ret); |
| 203 | chans = op_channel_count(of, li); |
| 204 | EXTEND(SP, chans * ret + 1); |
| 205 | PUSHs(sv_2mortal(newSViv(li))); |
| 206 | for(i = 0 ; i < chans * ret ; i++) |
| 207 | PUSHs(sv_2mortal(newSVnv(buf[i]))); |
| 208 | |
| 209 | void |
| 210 | op_read_stereo(of, bufsize = 1024 * 1024) |
| 211 | Audio::Opusfile of; |
| 212 | int bufsize; |
| 213 | PREINIT: |
| 214 | opus_int16* buf; |
| 215 | int ret, i; |
| 216 | PPCODE: |
| 217 | Newx(buf, bufsize, opus_int16); |
| 218 | ret = op_read_stereo(of, buf, bufsize); |
| 219 | if(ret < 0) |
| 220 | croak("op_read_stereo returned error %d\n", ret); |
| 221 | EXTEND(SP, 2 * ret); |
| 222 | for(i = 0 ; i < 2 * ret ; i++) |
| 223 | PUSHs(sv_2mortal(newSViv(buf[i]))); |
| 224 | |
| 225 | void |
| 226 | op_read_float_stereo(of, bufsize = 1024 * 1024) |
| 227 | Audio::Opusfile of; |
| 228 | int bufsize; |
| 229 | PREINIT: |
| 230 | float* buf; |
| 231 | int ret, i; |
| 232 | PPCODE: |
| 233 | Newx(buf, bufsize, float); |
| 234 | ret = op_read_float_stereo(of, buf, bufsize); |
| 235 | if(ret < 0) |
| 236 | croak("op_read_float_stereo returned error %d\n", ret); |
| 237 | EXTEND(SP, 2 * ret); |
| 238 | for(i = 0 ; i < 2 * ret ; i++) |
| 239 | PUSHs(sv_2mortal(newSVnv(buf[i]))); |
| 240 | |
| 241 | |
| 242 | |
| 243 | MODULE = Audio::Opusfile PACKAGE = Audio::Opusfile::Tags PREFIX = opus_tags_ |
| 244 | |
| 245 | int |
| 246 | opus_tags_query_count(tags, tag) |
| 247 | Audio::Opusfile::Tags tags; |
| 248 | const char* tag; |
| 249 | |
| 250 | const char* |
| 251 | opus_tags_query(tags, tag, count = 0) |
| 252 | Audio::Opusfile::Tags tags; |
| 253 | const char* tag; |
| 254 | int count; |
| 255 | |
| 256 | MODULE = Audio::Opusfile PACKAGE = Audio::Opusfile::PictureTag PREFIX = opus_picture_tag_ |
| 257 | |
| 258 | Audio::Opusfile::PictureTag |
| 259 | opus_picture_tag_parse(tag) |
| 260 | const char *tag; |
| 261 | PREINIT: |
| 262 | OpusPictureTag *pic; |
| 263 | int err; |
| 264 | CODE: |
| 265 | Newx(pic, 1, OpusPictureTag); |
| 266 | if(err = opus_picture_tag_parse(pic, tag)) |
| 267 | croak("opus_picture_tag_parse returned error %d\n", err); |
| 268 | RETVAL = pic; |
| 269 | OUTPUT: |
| 270 | RETVAL |
| 271 | |
| 272 | void |
| 273 | DESTROY(pic) |
| 274 | Audio::Opusfile::PictureTag pic |
| 275 | CODE: |
| 276 | Safefree(pic); |
| 277 | |
| 278 | int |
| 279 | type(pic) |
| 280 | Audio::Opusfile::PictureTag pic; |
| 281 | CODE: |
| 282 | RETVAL = pic->type; |
| 283 | OUTPUT: |
| 284 | RETVAL |
| 285 | |
| 286 | const char* |
| 287 | mime_type(pic) |
| 288 | Audio::Opusfile::PictureTag pic; |
| 289 | CODE: |
| 290 | RETVAL = pic->mime_type; |
| 291 | OUTPUT: |
| 292 | RETVAL |
| 293 | |
| 294 | const char* |
| 295 | description(pic) |
| 296 | Audio::Opusfile::PictureTag pic; |
| 297 | CODE: |
| 298 | RETVAL = pic->description; |
| 299 | OUTPUT: |
| 300 | RETVAL |
| 301 | |
| 302 | int |
| 303 | width(pic) |
| 304 | Audio::Opusfile::PictureTag pic; |
| 305 | CODE: |
| 306 | RETVAL = pic->width; |
| 307 | OUTPUT: |
| 308 | RETVAL |
| 309 | |
| 310 | int |
| 311 | height(pic) |
| 312 | Audio::Opusfile::PictureTag pic; |
| 313 | CODE: |
| 314 | RETVAL = pic->height; |
| 315 | OUTPUT: |
| 316 | RETVAL |
| 317 | |
| 318 | int |
| 319 | depth(pic) |
| 320 | Audio::Opusfile::PictureTag pic; |
| 321 | CODE: |
| 322 | RETVAL = pic->depth; |
| 323 | OUTPUT: |
| 324 | RETVAL |
| 325 | |
| 326 | int |
| 327 | colors(pic) |
| 328 | Audio::Opusfile::PictureTag pic; |
| 329 | CODE: |
| 330 | RETVAL = pic->colors; |
| 331 | OUTPUT: |
| 332 | RETVAL |
| 333 | |
| 334 | int |
| 335 | data_length(pic) |
| 336 | Audio::Opusfile::PictureTag pic; |
| 337 | CODE: |
| 338 | RETVAL = pic->data_length; |
| 339 | OUTPUT: |
| 340 | RETVAL |
| 341 | |
| 342 | SV* |
| 343 | data(pic) |
| 344 | Audio::Opusfile::PictureTag pic; |
| 345 | CODE: |
| 346 | RETVAL = newSVpvn(pic->data, pic->data_length); |
| 347 | OUTPUT: |
| 348 | RETVAL |
| 349 | |
| 350 | int |
| 351 | format(pic) |
| 352 | Audio::Opusfile::PictureTag pic; |
| 353 | CODE: |
| 354 | RETVAL = pic->format; |
| 355 | OUTPUT: |
| 356 | RETVAL |
| 357 | |
| 358 | MODULE = Audio::Opusfile PACKAGE = Audio::Opusfile::Head |
| 359 | |
| 360 | int |
| 361 | version(head) |
| 362 | Audio::Opusfile::Head head; |
| 363 | CODE: |
| 364 | RETVAL = head->version; |
| 365 | OUTPUT: |
| 366 | RETVAL |
| 367 | |
| 368 | int |
| 369 | channel_count(head) |
| 370 | Audio::Opusfile::Head head; |
| 371 | CODE: |
| 372 | RETVAL = head->channel_count; |
| 373 | OUTPUT: |
| 374 | RETVAL |
| 375 | |
| 376 | unsigned |
| 377 | pre_skip(head) |
| 378 | Audio::Opusfile::Head head; |
| 379 | CODE: |
| 380 | RETVAL = head->pre_skip; |
| 381 | OUTPUT: |
| 382 | RETVAL |
| 383 | |
| 384 | unsigned |
| 385 | input_sample_rate(head) |
| 386 | Audio::Opusfile::Head head; |
| 387 | CODE: |
| 388 | RETVAL = head->input_sample_rate; |
| 389 | OUTPUT: |
| 390 | RETVAL |
| 391 | |
| 392 | int |
| 393 | output_gain(head) |
| 394 | Audio::Opusfile::Head head; |
| 395 | CODE: |
| 396 | RETVAL = head->output_gain; |
| 397 | OUTPUT: |
| 398 | RETVAL |
| 399 | |
| 400 | int |
| 401 | mapping_family(head) |
| 402 | Audio::Opusfile::Head head; |
| 403 | CODE: |
| 404 | RETVAL = head->mapping_family; |
| 405 | OUTPUT: |
| 406 | RETVAL |
| 407 | |
| 408 | int |
| 409 | stream_count(head) |
| 410 | Audio::Opusfile::Head head; |
| 411 | CODE: |
| 412 | RETVAL = head->stream_count; |
| 413 | OUTPUT: |
| 414 | RETVAL |
| 415 | |
| 416 | int |
| 417 | coupled_count(head) |
| 418 | Audio::Opusfile::Head head; |
| 419 | CODE: |
| 420 | RETVAL = head->coupled_count; |
| 421 | OUTPUT: |
| 422 | RETVAL |
| 423 | |
| 424 | int |
| 425 | mapping(head, k) |
| 426 | Audio::Opusfile::Head head; |
| 427 | unsigned k; |
| 428 | CODE: |
| 429 | if(k >= OPUS_CHANNEL_COUNT_MAX) |
| 430 | croak("k must be less than %d\n", (int)OPUS_CHANNEL_COUNT_MAX); |
| 431 | RETVAL = (int) head->mapping[k]; |
| 432 | OUTPUT: |
| 433 | RETVAL |