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.

137 lines
3.6KB

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