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.

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