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.

167 lines
4.0KB

  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. // disable SIMD for macos-old builds
  23. #include "CarlaDefines.h"
  24. #if defined(CARLA_OS_WASM)
  25. # define MINIMP3_NO_SIMD
  26. #elif defined(CARLA_OS_MAC) && !defined(CARLA_PROPER_CPP11_SUPPORT)
  27. # define MINIMP3_NO_SIMD
  28. #endif
  29. #define MINIMP3_FLOAT_OUTPUT
  30. #define MINIMP3_IMPLEMENTATION
  31. #include "minimp3_ex.h"
  32. /* internal abstraction */
  33. typedef struct {
  34. mp3dec_ex_t dec_ex;
  35. } minimp3_audio_decoder;
  36. static void err_to_string(int err_code, char *buf) {
  37. switch (err_code)
  38. {
  39. case MP3D_E_PARAM:
  40. strcpy (buf, "Parameter error");
  41. break;
  42. case MP3D_E_MEMORY:
  43. strcpy (buf, "Memory error");
  44. break;
  45. case MP3D_E_IOERROR:
  46. strcpy (buf, "IO error");
  47. break;
  48. case MP3D_E_USER:
  49. strcpy (buf, "User error");
  50. break;
  51. case MP3D_E_DECODE:
  52. strcpy (buf, "Decode error");
  53. break;
  54. default:
  55. strcpy (buf, "Unknown error");
  56. break;
  57. }
  58. }
  59. static int ad_info_minimp3(void *sf, struct adinfo *nfo) {
  60. minimp3_audio_decoder *priv = (minimp3_audio_decoder*) sf;
  61. if (!priv) return -1;
  62. if (nfo) {
  63. nfo->channels = priv->dec_ex.info.channels;
  64. nfo->frames = priv->dec_ex.samples / nfo->channels;
  65. nfo->sample_rate = priv->dec_ex.info.hz;
  66. nfo->length = nfo->sample_rate ? (nfo->frames * 1000) / nfo->sample_rate : 0;
  67. nfo->bit_depth = 16;
  68. nfo->bit_rate = priv->dec_ex.info.bitrate_kbps;
  69. nfo->meta_data = NULL;
  70. nfo->can_seek = 0;
  71. }
  72. return 0;
  73. }
  74. static void *ad_open_minimp3(const char *filename, struct adinfo *nfo) {
  75. minimp3_audio_decoder *priv = (minimp3_audio_decoder*)calloc (1, sizeof(minimp3_audio_decoder));
  76. int res = mp3dec_ex_open(&priv->dec_ex, filename, MP3D_SEEK_TO_SAMPLE);
  77. if (res) {
  78. dbg(0, "unable to open file '%s'.", filename);
  79. char err_str[600];
  80. err_to_string (res, err_str);
  81. puts (err_str);
  82. dbg (0, "error=%i", res);
  83. free (priv);
  84. return NULL;
  85. }
  86. ad_info_minimp3 (priv, nfo);
  87. return (void*) priv;
  88. }
  89. static int ad_close_minimp3(void *sf) {
  90. minimp3_audio_decoder *priv = (minimp3_audio_decoder*) sf;
  91. if (!priv) return -1;
  92. if (!sf) {
  93. dbg (0, "fatal: bad file close.\n");
  94. return -1;
  95. }
  96. mp3dec_ex_close (&priv->dec_ex);
  97. free (priv);
  98. return 0;
  99. }
  100. static int64_t ad_seek_minimp3(void *sf, int64_t pos)
  101. {
  102. minimp3_audio_decoder *priv = (minimp3_audio_decoder*) sf;
  103. if (!priv) return -1;
  104. return mp3dec_ex_seek (&priv->dec_ex, pos);
  105. }
  106. static ssize_t ad_read_minimp3(void *sf, float* d, size_t len)
  107. {
  108. minimp3_audio_decoder *priv = (minimp3_audio_decoder*) sf;
  109. if (!priv) return -1;
  110. return mp3dec_ex_read (&priv->dec_ex, d, len);
  111. }
  112. static int ad_get_bitrate_minimp3(void *sf) {
  113. minimp3_audio_decoder *priv = (minimp3_audio_decoder*) sf;
  114. if (!priv) return -1;
  115. return priv->dec_ex.info.bitrate_kbps;
  116. }
  117. static int ad_eval_minimp3(const char *f)
  118. {
  119. char *ext = strrchr(f, '.');
  120. if (strstr (f, "://")) return 0;
  121. if (!ext) return 5;
  122. if (!strcasecmp(ext, ".mp3")) return 100;
  123. return 0;
  124. }
  125. static const ad_plugin ad_minimp3 = {
  126. #if 1
  127. &ad_eval_minimp3,
  128. &ad_open_minimp3,
  129. &ad_close_minimp3,
  130. &ad_info_minimp3,
  131. &ad_seek_minimp3,
  132. &ad_read_minimp3,
  133. &ad_get_bitrate_minimp3
  134. #else
  135. &ad_eval_null,
  136. &ad_open_null,
  137. &ad_close_null,
  138. &ad_info_null,
  139. &ad_seek_null,
  140. &ad_read_null,
  141. &ad_bitrate_null
  142. #endif
  143. };
  144. /* dlopen handler */
  145. const ad_plugin * adp_get_minimp3() {
  146. return &ad_minimp3;
  147. }