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.

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