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_minimp3.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. Copyright (C) 2011-2013 Robin Gareus <robin@gareus.org>
  3. Copyright (C) 2014-2023 Filipe Coelho <falktx@falktx.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser Public License as published by
  6. the Free Software Foundation; either version 2.1, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <math.h>
  20. #include "ad_plugin.h"
  21. // disable SIMD for macos-old builds
  22. #include "CarlaDefines.h"
  23. #if defined(CARLA_OS_WASM)
  24. # define MINIMP3_NO_SIMD
  25. #elif defined(CARLA_OS_MAC) && !defined(CARLA_PROPER_CPP11_SUPPORT)
  26. # define MINIMP3_NO_SIMD
  27. #endif
  28. #define MINIMP3_FLOAT_OUTPUT
  29. #define MINIMP3_IMPLEMENTATION
  30. #include "minimp3_ex.h"
  31. #ifdef _MSC_VER
  32. #define strcasecmp stricmp
  33. #endif
  34. /* internal abstraction */
  35. typedef struct {
  36. mp3dec_ex_t dec_ex;
  37. } minimp3_audio_decoder;
  38. static void err_to_string(int err_code, char *buf) {
  39. switch (err_code)
  40. {
  41. case MP3D_E_PARAM:
  42. strcpy (buf, "Parameter error");
  43. break;
  44. case MP3D_E_MEMORY:
  45. strcpy (buf, "Memory error");
  46. break;
  47. case MP3D_E_IOERROR:
  48. strcpy (buf, "IO error");
  49. break;
  50. case MP3D_E_USER:
  51. strcpy (buf, "User error");
  52. break;
  53. case MP3D_E_DECODE:
  54. strcpy (buf, "Decode error");
  55. break;
  56. default:
  57. strcpy (buf, "Unknown error");
  58. break;
  59. }
  60. }
  61. static int ad_info_minimp3(void *sf, struct adinfo *nfo) {
  62. minimp3_audio_decoder *priv = (minimp3_audio_decoder*) sf;
  63. if (!priv) return -1;
  64. if (nfo) {
  65. nfo->channels = priv->dec_ex.info.channels;
  66. nfo->frames = priv->dec_ex.samples / nfo->channels;
  67. nfo->sample_rate = priv->dec_ex.info.hz;
  68. nfo->length = nfo->sample_rate ? (nfo->frames * 1000) / nfo->sample_rate : 0;
  69. nfo->bit_depth = 16;
  70. nfo->bit_rate = priv->dec_ex.info.bitrate_kbps;
  71. nfo->meta_data = NULL;
  72. nfo->can_seek = 0;
  73. }
  74. return 0;
  75. }
  76. static void *ad_open_minimp3(const char *filename, struct adinfo *nfo) {
  77. minimp3_audio_decoder *priv = (minimp3_audio_decoder*)calloc (1, sizeof(minimp3_audio_decoder));
  78. int res = mp3dec_ex_open(&priv->dec_ex, filename, MP3D_SEEK_TO_SAMPLE);
  79. if (res) {
  80. dbg(0, "unable to open file '%s'.", filename);
  81. char err_str[600];
  82. err_to_string (res, err_str);
  83. puts (err_str);
  84. dbg (0, "error=%i", res);
  85. free (priv);
  86. return NULL;
  87. }
  88. ad_info_minimp3 (priv, nfo);
  89. return (void*) priv;
  90. }
  91. static int ad_close_minimp3(void *sf) {
  92. minimp3_audio_decoder *priv = (minimp3_audio_decoder*) sf;
  93. if (!priv) return -1;
  94. if (!sf) {
  95. dbg (0, "fatal: bad file close.\n");
  96. return -1;
  97. }
  98. mp3dec_ex_close (&priv->dec_ex);
  99. free (priv);
  100. return 0;
  101. }
  102. static int64_t ad_seek_minimp3(void *sf, int64_t pos)
  103. {
  104. minimp3_audio_decoder *priv = (minimp3_audio_decoder*) sf;
  105. if (!priv) return -1;
  106. return mp3dec_ex_seek (&priv->dec_ex, pos);
  107. }
  108. static ssize_t ad_read_minimp3(void *sf, float* d, size_t len)
  109. {
  110. minimp3_audio_decoder *priv = (minimp3_audio_decoder*) sf;
  111. if (!priv) return -1;
  112. return mp3dec_ex_read (&priv->dec_ex, d, len);
  113. }
  114. static int ad_get_bitrate_minimp3(void *sf) {
  115. minimp3_audio_decoder *priv = (minimp3_audio_decoder*) sf;
  116. if (!priv) return -1;
  117. return priv->dec_ex.info.bitrate_kbps;
  118. }
  119. static int ad_eval_minimp3(const char *f)
  120. {
  121. char *ext = strrchr(f, '.');
  122. if (strstr (f, "://")) return 0;
  123. if (!ext) return 5;
  124. if (!strcasecmp(ext, ".mp3")) return 100;
  125. return 0;
  126. }
  127. static const ad_plugin ad_minimp3 = {
  128. #if 1
  129. &ad_eval_minimp3,
  130. &ad_open_minimp3,
  131. &ad_close_minimp3,
  132. &ad_info_minimp3,
  133. &ad_seek_minimp3,
  134. &ad_read_minimp3,
  135. &ad_get_bitrate_minimp3
  136. #else
  137. &ad_eval_null,
  138. &ad_open_null,
  139. &ad_close_null,
  140. &ad_info_null,
  141. &ad_seek_null,
  142. &ad_read_null,
  143. &ad_bitrate_null
  144. #endif
  145. };
  146. /* dlopen handler */
  147. const ad_plugin * adp_get_minimp3() {
  148. return &ad_minimp3;
  149. }