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.

139 lines
3.7KB

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