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_dr_mp3.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #define DR_MP3_FLOAT_OUTPUT
  22. #define DR_MP3_IMPLEMENTATION
  23. #define DRMP3_DATA_CHUNK_SIZE DRMP3_MIN_DATA_CHUNK_SIZE*40
  24. #include "dr_mp3.h"
  25. /* internal abstraction */
  26. #define DR_MP3_MAX_SEEK_POINTS 500
  27. #ifdef _MSC_VER
  28. #define strcasecmp stricmp
  29. #endif
  30. typedef struct {
  31. drmp3 mp3;
  32. drmp3_seek_point seekPoints[DR_MP3_MAX_SEEK_POINTS];
  33. } drmp3_audio_decoder;
  34. static int ad_info_dr_mp3(void *sf, struct adinfo *nfo) {
  35. drmp3_audio_decoder *priv = (drmp3_audio_decoder*) sf;
  36. if (!priv) return -1;
  37. if (nfo) {
  38. nfo->channels = priv->mp3.channels;
  39. nfo->frames = drmp3_get_pcm_frame_count(&priv->mp3);
  40. nfo->sample_rate = priv->mp3.sampleRate;
  41. nfo->length = nfo->sample_rate ? (nfo->frames * 1000) / nfo->sample_rate : 0;
  42. nfo->bit_depth = 16;
  43. nfo->bit_rate = priv->mp3.frameInfo.bitrate_kbps * 1000;
  44. nfo->meta_data = NULL;
  45. nfo->can_seek = 1;
  46. }
  47. return 0;
  48. }
  49. static void *ad_open_dr_mp3(const char *filename, struct adinfo *nfo) {
  50. drmp3_audio_decoder *priv = (drmp3_audio_decoder*)calloc (1, sizeof(drmp3_audio_decoder));
  51. drmp3_bool32 res = drmp3_init_file(&priv->mp3, filename, NULL);
  52. if (res == DRMP3_FALSE) {
  53. dbg(0, "unable to open file '%s'.", filename);
  54. free (priv);
  55. return NULL;
  56. }
  57. drmp3_uint32 seekPointCount = DR_MP3_MAX_SEEK_POINTS;
  58. drmp3_calculate_seek_points (&priv->mp3, &seekPointCount, priv->seekPoints);
  59. drmp3_bind_seek_table (&priv->mp3, seekPointCount, priv->seekPoints);
  60. ad_info_dr_mp3 (priv, nfo);
  61. return (void*) priv;
  62. }
  63. static int ad_close_dr_mp3(void *sf) {
  64. drmp3_audio_decoder *priv = (drmp3_audio_decoder*) sf;
  65. if (!priv) return -1;
  66. if (!sf) {
  67. dbg (0, "fatal: bad file close.\n");
  68. return -1;
  69. }
  70. drmp3_uninit (&priv->mp3);
  71. free (priv);
  72. return 0;
  73. }
  74. static int64_t ad_seek_dr_mp3(void *sf, int64_t pos)
  75. {
  76. drmp3_audio_decoder *priv = (drmp3_audio_decoder*) sf;
  77. if (!priv) return -1;
  78. return drmp3_seek_to_pcm_frame (&priv->mp3, pos);
  79. }
  80. static ssize_t ad_read_dr_mp3(void *sf, float* d, size_t len)
  81. {
  82. drmp3_audio_decoder *priv = (drmp3_audio_decoder*) sf;
  83. if (!priv) return -1;
  84. return drmp3_read_pcm_frames_f32 (&priv->mp3, len / priv->mp3.channels, d) * priv->mp3.channels;
  85. }
  86. static int ad_get_bitrate_dr_mp3(void *sf) {
  87. drmp3_audio_decoder *priv = (drmp3_audio_decoder*) sf;
  88. if (!priv) return -1;
  89. return priv->mp3.frameInfo.bitrate_kbps * 1000;
  90. }
  91. static int ad_eval_dr_mp3(const char *f)
  92. {
  93. char *ext = strrchr(f, '.');
  94. if (strstr (f, "://")) return 0;
  95. if (!ext) return 5;
  96. if (!strcasecmp(ext, ".mp3")) return 100;
  97. return 0;
  98. }
  99. static const ad_plugin ad_dr_mp3 = {
  100. #if 1
  101. &ad_eval_dr_mp3,
  102. &ad_open_dr_mp3,
  103. &ad_close_dr_mp3,
  104. &ad_info_dr_mp3,
  105. &ad_seek_dr_mp3,
  106. &ad_read_dr_mp3,
  107. &ad_get_bitrate_dr_mp3
  108. #else
  109. &ad_eval_null,
  110. &ad_open_null,
  111. &ad_close_null,
  112. &ad_info_null,
  113. &ad_seek_null,
  114. &ad_read_null,
  115. &ad_bit_rate_null
  116. #endif
  117. };
  118. /* dlopen handler */
  119. const ad_plugin * adp_get_dr_mp3() {
  120. return &ad_dr_mp3;
  121. }