Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ad_plugin.c 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. Copyright (C) 2011-2013 Robin Gareus <robin@gareus.org>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser Public License as published by
  5. the Free Software Foundation; either version 2.1, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <stdarg.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <math.h>
  21. #include "audio_decoder/ad_plugin.h"
  22. int ad_debug_level = 0;
  23. #define UNUSED(x) (void)(x)
  24. int ad_eval_null(const char *f) { UNUSED(f); return -1; }
  25. void * ad_open_null(const char *f, struct adinfo *n) { UNUSED(f); UNUSED(n); return NULL; }
  26. int ad_close_null(void *x) { UNUSED(x); return -1; }
  27. int ad_info_null(void *x, struct adinfo *n) { UNUSED(x); UNUSED(n); return -1; }
  28. int64_t ad_seek_null(void *x, int64_t p) { UNUSED(x); UNUSED(p); return -1; }
  29. ssize_t ad_read_null(void *x, float*d, size_t s) { UNUSED(x); UNUSED(d); UNUSED(s); return -1;}
  30. typedef struct {
  31. ad_plugin const *b; ///< decoder back-end
  32. void *d; ///< backend data
  33. } adecoder;
  34. /* samplecat api */
  35. void ad_init() { /* global init */ }
  36. static ad_plugin const * choose_backend(const char *fn) {
  37. int max, val;
  38. ad_plugin const *b=NULL;
  39. max=0;
  40. val=adp_get_sndfile()->eval(fn);
  41. if (val>max) {max=val; b=adp_get_sndfile();}
  42. val=adp_get_ffmpeg()->eval(fn);
  43. if (val>max) {max=val; b=adp_get_ffmpeg();}
  44. return b;
  45. }
  46. void *ad_open(const char *fn, struct adinfo *nfo) {
  47. adecoder *d = (adecoder*) calloc(1, sizeof(adecoder));
  48. ad_clear_nfo(nfo);
  49. d->b = choose_backend(fn);
  50. if (!d->b) {
  51. dbg(0, "fatal: no decoder backend available");
  52. free(d);
  53. return NULL;
  54. }
  55. d->d = d->b->open(fn, nfo);
  56. if (!d->d) {
  57. free(d);
  58. return NULL;
  59. }
  60. return (void*)d;
  61. }
  62. int ad_info(void *sf, struct adinfo *nfo) {
  63. adecoder *d = (adecoder*) sf;
  64. if (!d) return -1;
  65. return d->b->info(d->d, nfo);
  66. }
  67. int ad_close(void *sf) {
  68. adecoder *d = (adecoder*) sf;
  69. if (!d) return -1;
  70. int rv = d->b->close(d->d);
  71. free(d);
  72. return rv;
  73. }
  74. int64_t ad_seek(void *sf, int64_t pos) {
  75. adecoder *d = (adecoder*) sf;
  76. if (!d) return -1;
  77. return d->b->seek(d->d, pos);
  78. }
  79. ssize_t ad_read(void *sf, float* out, size_t len){
  80. adecoder *d = (adecoder*) sf;
  81. if (!d) return -1;
  82. return d->b->read(d->d, out, len);
  83. }
  84. /*
  85. * side-effects: allocates buffer
  86. */
  87. ssize_t ad_read_mono_dbl(void *sf, struct adinfo *nfo, double* d, size_t len){
  88. unsigned int c,f;
  89. unsigned int chn = nfo->channels;
  90. if (len<1) return 0;
  91. static float *buf = NULL;
  92. static size_t bufsiz = 0;
  93. if (!buf || bufsiz != len*chn) {
  94. bufsiz=len*chn;
  95. buf = (float*) realloc((void*)buf, bufsiz * sizeof(float));
  96. }
  97. len = ad_read(sf, buf, bufsiz);
  98. for (f=0;f< (len/chn);f++) {
  99. double val=0.0;
  100. for (c=0;c<chn;c++) {
  101. val+=buf[f*chn + c];
  102. }
  103. d[f]= val/chn;
  104. }
  105. return len/chn;
  106. }
  107. int ad_finfo (const char *fn, struct adinfo *nfo) {
  108. ad_clear_nfo(nfo);
  109. void * sf = ad_open(fn, nfo);
  110. return ad_close(sf)?1:0;
  111. }
  112. void ad_clear_nfo(struct adinfo *nfo) {
  113. memset(nfo, 0, sizeof(struct adinfo));
  114. }
  115. void ad_free_nfo(struct adinfo *nfo) {
  116. if (nfo->meta_data) free(nfo->meta_data);
  117. }
  118. void ad_dump_nfo(int dbglvl, struct adinfo *nfo) {
  119. dbg(dbglvl, "sample_rate: %u", nfo->sample_rate);
  120. dbg(dbglvl, "channels: %u", nfo->channels);
  121. dbg(dbglvl, "length: %"PRIi64" ms", nfo->length);
  122. dbg(dbglvl, "frames: %"PRIi64, nfo->frames);
  123. dbg(dbglvl, "bit_rate: %d", nfo->bit_rate);
  124. dbg(dbglvl, "bit_depth: %d", nfo->bit_depth);
  125. dbg(dbglvl, "channels: %u", nfo->channels);
  126. dbg(dbglvl, "meta-data: %s", nfo->meta_data?nfo->meta_data:"-");
  127. }
  128. void ad_debug_printf(const char* func, int level, const char* format, ...) {
  129. va_list args;
  130. va_start(args, format);
  131. if (level <= ad_debug_level) {
  132. fprintf(stderr, "%s(): ", func);
  133. vfprintf(stderr, format, args);
  134. fprintf(stderr, "\n");
  135. }
  136. va_end(args);
  137. }
  138. void ad_set_debuglevel(int lvl) {
  139. ad_debug_level = lvl;
  140. if (ad_debug_level<-1) ad_debug_level=-1;
  141. if (ad_debug_level>3) ad_debug_level=3;
  142. }