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.

159 lines
3.8KB

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