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.

132 lines
3.3KB

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