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.

154 lines
4.6KB

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