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.

191 lines
4.8KB

  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 <math.h>
  20. #include "ad_plugin.h"
  21. int ad_debug_level = 0;
  22. #define UNUSED(x) (void)(x)
  23. int ad_eval_null(const char *f) { UNUSED(f); return -1; }
  24. void * ad_open_null(const char *f, struct adinfo *n) { UNUSED(f); UNUSED(n); return NULL; }
  25. int ad_close_null(void *x) { UNUSED(x); return -1; }
  26. int ad_info_null(void *x, struct adinfo *n) { UNUSED(x); UNUSED(n); return -1; }
  27. int64_t ad_seek_null(void *x, int64_t p) { UNUSED(x); UNUSED(p); return -1; }
  28. ssize_t ad_read_null(void *x, float*d, size_t s) { UNUSED(x); UNUSED(d); UNUSED(s); return -1;}
  29. int ad_bitrate_null(void *x) { UNUSED(x); return -1;}
  30. typedef struct {
  31. ad_plugin const *b; ///< decoder back-end
  32. void *d; ///< backend data
  33. } adecoder;
  34. /* samplecat api */
  35. static ad_plugin const * choose_backend(const char *fn) {
  36. int max, val;
  37. ad_plugin const *b=NULL;
  38. max=0;
  39. val=adp_get_sndfile()->eval(fn);
  40. if (val>max) {max=val; b=adp_get_sndfile();}
  41. #if 0
  42. // NOTE seek is broken for minimp3
  43. val = adp_get_minimp3()->eval(fn);
  44. if (val > max) {max=val; b=adp_get_minimp3();}
  45. #else
  46. // NOTE dr_mp3 has memory corruption issues
  47. val = adp_get_dr_mp3()->eval(fn);
  48. if (val > max) {max=val; b=adp_get_dr_mp3();}
  49. #endif
  50. val=adp_get_ffmpeg()->eval(fn);
  51. if (val>max) {max=val; b=adp_get_ffmpeg();}
  52. return b;
  53. }
  54. void *ad_open(const char *fn, struct adinfo *nfo) {
  55. adecoder *d = (adecoder*) calloc(1, sizeof(adecoder));
  56. ad_clear_nfo(nfo);
  57. d->b = choose_backend(fn);
  58. if (!d->b) {
  59. dbg(0, "fatal: no decoder backend available");
  60. free(d);
  61. return NULL;
  62. }
  63. d->d = d->b->open(fn, nfo);
  64. if (!d->d) {
  65. free(d);
  66. return NULL;
  67. }
  68. return (void*)d;
  69. }
  70. int ad_info(void *sf, struct adinfo *nfo) {
  71. adecoder *d = (adecoder*) sf;
  72. if (!d) return -1;
  73. return d->b->info(d->d, nfo);
  74. }
  75. int ad_close(void *sf) {
  76. adecoder *d = (adecoder*) sf;
  77. if (!d) return -1;
  78. int rv = d->b->close(d->d);
  79. free(d);
  80. return rv;
  81. }
  82. int64_t ad_seek(void *sf, int64_t pos) {
  83. adecoder *d = (adecoder*) sf;
  84. if (!d) return -1;
  85. return d->b->seek(d->d, pos);
  86. }
  87. ssize_t ad_read(void *sf, float* out, size_t len){
  88. adecoder *d = (adecoder*) sf;
  89. if (!d) return -1;
  90. return d->b->read(d->d, out, len);
  91. }
  92. int ad_get_bitrate(void *sf) {
  93. adecoder *d = (adecoder*) sf;
  94. if (!d) return -1;
  95. return d->b->bitrate(d->d);
  96. }
  97. /*
  98. * side-effects: allocates buffer
  99. */
  100. ssize_t ad_read_mono_dbl(void *sf, struct adinfo *nfo, double* d, size_t len){
  101. unsigned int c,f;
  102. unsigned int chn = nfo->channels;
  103. if (len<1) return 0;
  104. static float *buf = NULL;
  105. static size_t bufsiz = 0;
  106. if (!buf || bufsiz != len*chn) {
  107. bufsiz=len*chn;
  108. buf = (float*) realloc((void*)buf, bufsiz * sizeof(float));
  109. }
  110. len = ad_read(sf, buf, bufsiz);
  111. for (f=0;f< (len/chn);f++) {
  112. double val=0.0;
  113. for (c=0;c<chn;c++) {
  114. val+=buf[f*chn + c];
  115. }
  116. d[f]= val/chn;
  117. }
  118. return len/chn;
  119. }
  120. int ad_finfo (const char *fn, struct adinfo *nfo) {
  121. ad_clear_nfo(nfo);
  122. void * sf = ad_open(fn, nfo);
  123. return ad_close(sf)?1:0;
  124. }
  125. void ad_clear_nfo(struct adinfo *nfo) {
  126. memset(nfo, 0, sizeof(struct adinfo));
  127. }
  128. void ad_free_nfo(struct adinfo *nfo) {
  129. if (nfo->meta_data) free(nfo->meta_data);
  130. }
  131. void ad_dump_nfo(int dbglvl, struct adinfo *nfo) {
  132. dbg(dbglvl, "sample_rate: %u", nfo->sample_rate);
  133. dbg(dbglvl, "channels: %u", nfo->channels);
  134. dbg(dbglvl, "length: %"PRIi64" ms", nfo->length);
  135. dbg(dbglvl, "frames: %"PRIi64, nfo->frames);
  136. dbg(dbglvl, "bit_rate: %d", nfo->bit_rate);
  137. dbg(dbglvl, "bit_depth: %d", nfo->bit_depth);
  138. dbg(dbglvl, "channels: %u", nfo->channels);
  139. dbg(dbglvl, "meta-data: %s", nfo->meta_data?nfo->meta_data:"-");
  140. }
  141. void ad_debug_printf(const char* func, int level, const char* format, ...) {
  142. va_list args;
  143. va_start(args, format);
  144. if (level <= ad_debug_level) {
  145. fprintf(stderr, "%s(): ", func);
  146. vfprintf(stderr, format, args);
  147. fprintf(stderr, "\n");
  148. }
  149. va_end(args);
  150. }
  151. void ad_set_debuglevel(int lvl) {
  152. ad_debug_level = lvl;
  153. if (ad_debug_level<-1) ad_debug_level=-1;
  154. if (ad_debug_level>3) ad_debug_level=3;
  155. }