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

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