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