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_soundfile.c 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <string.h>
  18. #include <strings.h>
  19. #include <unistd.h>
  20. #include <math.h>
  21. #include "ad_plugin.h"
  22. #ifdef HAVE_SNDFILE
  23. #include <sndfile.h>
  24. /* internal abstraction */
  25. typedef struct {
  26. SF_INFO sfinfo;
  27. SNDFILE *sffile;
  28. } sndfile_audio_decoder;
  29. static int parse_bit_depth(int format) {
  30. /* see http://www.mega-nerd.com/libsndfile/api.html */
  31. switch (format&0x0f) {
  32. case SF_FORMAT_PCM_S8: return 8;
  33. case SF_FORMAT_PCM_16: return 16; /* Signed 16 bit data */
  34. case SF_FORMAT_PCM_24: return 24; /* Signed 24 bit data */
  35. case SF_FORMAT_PCM_32: return 32; /* Signed 32 bit data */
  36. case SF_FORMAT_PCM_U8: return 8; /* Unsigned 8 bit data (WAV and RAW only) */
  37. case SF_FORMAT_FLOAT : return 32; /* 32 bit float data */
  38. case SF_FORMAT_DOUBLE: return 64; /* 64 bit float data */
  39. default: break;
  40. }
  41. return 0;
  42. }
  43. static int ad_info_sndfile(void *sf, struct adinfo *nfo) {
  44. sndfile_audio_decoder *priv = (sndfile_audio_decoder*) sf;
  45. if (!priv) return -1;
  46. if (nfo) {
  47. nfo->channels = priv->sfinfo.channels;
  48. nfo->frames = priv->sfinfo.frames;
  49. nfo->sample_rate = priv->sfinfo.samplerate;
  50. nfo->length = priv->sfinfo.samplerate ? (priv->sfinfo.frames * 1000) / priv->sfinfo.samplerate : 0;
  51. nfo->bit_depth = parse_bit_depth(priv->sfinfo.format);
  52. nfo->bit_rate = nfo->bit_depth * nfo->channels * nfo->sample_rate;
  53. nfo->meta_data = NULL;
  54. }
  55. return 0;
  56. }
  57. static void *ad_open_sndfile(const char *fn, struct adinfo *nfo) {
  58. sndfile_audio_decoder *priv = (sndfile_audio_decoder*) calloc(1, sizeof(sndfile_audio_decoder));
  59. priv->sfinfo.format=0;
  60. if(!(priv->sffile = sf_open(fn, SFM_READ, &priv->sfinfo))){
  61. dbg(0, "unable to open file '%s'.", fn);
  62. puts(sf_strerror(NULL));
  63. int e = sf_error(NULL);
  64. dbg(0, "error=%i", e);
  65. free(priv);
  66. return NULL;
  67. }
  68. ad_info_sndfile(priv, nfo);
  69. return (void*) priv;
  70. }
  71. static int ad_close_sndfile(void *sf) {
  72. sndfile_audio_decoder *priv = (sndfile_audio_decoder*) sf;
  73. if (!priv) return -1;
  74. if(!sf || sf_close(priv->sffile)) {
  75. dbg(0, "fatal: bad file close.\n");
  76. return -1;
  77. }
  78. free(priv);
  79. return 0;
  80. }
  81. static int64_t ad_seek_sndfile(void *sf, int64_t pos) {
  82. sndfile_audio_decoder *priv = (sndfile_audio_decoder*) sf;
  83. if (!priv) return -1;
  84. return sf_seek(priv->sffile, pos, SEEK_SET);
  85. }
  86. static ssize_t ad_read_sndfile(void *sf, float* d, size_t len) {
  87. sndfile_audio_decoder *priv = (sndfile_audio_decoder*) sf;
  88. if (!priv) return -1;
  89. return sf_read_float (priv->sffile, d, len);
  90. }
  91. static int ad_eval_sndfile(const char *f) {
  92. char *ext = strrchr(f, '.');
  93. if (strstr (f, "://")) return 0;
  94. if (!ext) return 5;
  95. /* see http://www.mega-nerd.com/libsndfile/ */
  96. if (!strcasecmp(ext, ".wav")) return 100;
  97. if (!strcasecmp(ext, ".aiff")) return 100;
  98. if (!strcasecmp(ext, ".aifc")) return 100;
  99. if (!strcasecmp(ext, ".snd")) return 100;
  100. if (!strcasecmp(ext, ".au")) return 100;
  101. if (!strcasecmp(ext, ".paf")) return 100;
  102. if (!strcasecmp(ext, ".iff")) return 100;
  103. if (!strcasecmp(ext, ".svx")) return 100;
  104. if (!strcasecmp(ext, ".sf")) return 100;
  105. if (!strcasecmp(ext, ".vcc")) return 100;
  106. if (!strcasecmp(ext, ".w64")) return 100;
  107. if (!strcasecmp(ext, ".mat4")) return 100;
  108. if (!strcasecmp(ext, ".mat5")) return 100;
  109. if (!strcasecmp(ext, ".pvf5")) return 100;
  110. if (!strcasecmp(ext, ".xi")) return 100;
  111. if (!strcasecmp(ext, ".htk")) return 100;
  112. if (!strcasecmp(ext, ".pvf")) return 100;
  113. if (!strcasecmp(ext, ".sd2")) return 100;
  114. // libsndfile >= 1.0.18
  115. if (!strcasecmp(ext, ".flac")) return 80;
  116. if (!strcasecmp(ext, ".ogg")) return 80;
  117. return 0;
  118. }
  119. #endif
  120. static const ad_plugin ad_sndfile = {
  121. #ifdef HAVE_SNDFILE
  122. &ad_eval_sndfile,
  123. &ad_open_sndfile,
  124. &ad_close_sndfile,
  125. &ad_info_sndfile,
  126. &ad_seek_sndfile,
  127. &ad_read_sndfile
  128. #else
  129. &ad_eval_null,
  130. &ad_open_null,
  131. &ad_close_null,
  132. &ad_info_null,
  133. &ad_seek_null,
  134. &ad_read_null
  135. #endif
  136. };
  137. /* dlopen handler */
  138. const ad_plugin * adp_get_sndfile() {
  139. return &ad_sndfile;
  140. }