audio_stream_mp3.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /**************************************************************************/
  2. /* audio_stream_mp3.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #define MINIMP3_FLOAT_OUTPUT
  31. #define MINIMP3_IMPLEMENTATION
  32. #define MINIMP3_NO_STDIO
  33. #include "audio_stream_mp3.h"
  34. #include "core/io/file_access.h"
  35. int AudioStreamPlaybackMP3::_mix_internal(AudioFrame *p_buffer, int p_frames) {
  36. if (!active) {
  37. return 0;
  38. }
  39. int todo = p_frames;
  40. int frames_mixed_this_step = p_frames;
  41. int beat_length_frames = -1;
  42. bool beat_loop = mp3_stream->has_loop() && mp3_stream->get_bpm() > 0 && mp3_stream->get_beat_count() > 0;
  43. if (beat_loop) {
  44. beat_length_frames = mp3_stream->get_beat_count() * mp3_stream->sample_rate * 60 / mp3_stream->get_bpm();
  45. }
  46. while (todo && active) {
  47. mp3dec_frame_info_t frame_info;
  48. mp3d_sample_t *buf_frame = nullptr;
  49. int samples_mixed = mp3dec_ex_read_frame(mp3d, &buf_frame, &frame_info, mp3_stream->channels);
  50. if (samples_mixed) {
  51. p_buffer[p_frames - todo] = AudioFrame(buf_frame[0], buf_frame[samples_mixed - 1]);
  52. if (loop_fade_remaining < FADE_SIZE) {
  53. p_buffer[p_frames - todo] += loop_fade[loop_fade_remaining] * (float(FADE_SIZE - loop_fade_remaining) / float(FADE_SIZE));
  54. loop_fade_remaining++;
  55. }
  56. --todo;
  57. ++frames_mixed;
  58. if (beat_loop && (int)frames_mixed >= beat_length_frames) {
  59. for (int i = 0; i < FADE_SIZE; i++) {
  60. samples_mixed = mp3dec_ex_read_frame(mp3d, &buf_frame, &frame_info, mp3_stream->channels);
  61. loop_fade[i] = AudioFrame(buf_frame[0], buf_frame[samples_mixed - 1]);
  62. if (!samples_mixed) {
  63. break;
  64. }
  65. }
  66. loop_fade_remaining = 0;
  67. seek(mp3_stream->loop_offset);
  68. loops++;
  69. }
  70. }
  71. else {
  72. //EOF
  73. if (mp3_stream->loop) {
  74. seek(mp3_stream->loop_offset);
  75. loops++;
  76. } else {
  77. frames_mixed_this_step = p_frames - todo;
  78. //fill remainder with silence
  79. for (int i = p_frames - todo; i < p_frames; i++) {
  80. p_buffer[i] = AudioFrame(0, 0);
  81. }
  82. active = false;
  83. todo = 0;
  84. }
  85. }
  86. }
  87. return frames_mixed_this_step;
  88. }
  89. float AudioStreamPlaybackMP3::get_stream_sampling_rate() {
  90. return mp3_stream->sample_rate;
  91. }
  92. void AudioStreamPlaybackMP3::start(double p_from_pos) {
  93. active = true;
  94. seek(p_from_pos);
  95. loops = 0;
  96. begin_resample();
  97. }
  98. void AudioStreamPlaybackMP3::stop() {
  99. active = false;
  100. }
  101. bool AudioStreamPlaybackMP3::is_playing() const {
  102. return active;
  103. }
  104. int AudioStreamPlaybackMP3::get_loop_count() const {
  105. return loops;
  106. }
  107. double AudioStreamPlaybackMP3::get_playback_position() const {
  108. return double(frames_mixed) / mp3_stream->sample_rate;
  109. }
  110. void AudioStreamPlaybackMP3::seek(double p_time) {
  111. if (!active) {
  112. return;
  113. }
  114. if (p_time >= mp3_stream->get_length()) {
  115. p_time = 0;
  116. }
  117. frames_mixed = uint32_t(mp3_stream->sample_rate * p_time);
  118. mp3dec_ex_seek(mp3d, (uint64_t)frames_mixed * mp3_stream->channels);
  119. }
  120. void AudioStreamPlaybackMP3::tag_used_streams() {
  121. mp3_stream->tag_used(get_playback_position());
  122. }
  123. AudioStreamPlaybackMP3::~AudioStreamPlaybackMP3() {
  124. if (mp3d) {
  125. mp3dec_ex_close(mp3d);
  126. memfree(mp3d);
  127. }
  128. }
  129. Ref<AudioStreamPlayback> AudioStreamMP3::instantiate_playback() {
  130. Ref<AudioStreamPlaybackMP3> mp3s;
  131. ERR_FAIL_COND_V_MSG(data.is_empty(), mp3s,
  132. "This AudioStreamMP3 does not have an audio file assigned "
  133. "to it. AudioStreamMP3 should not be created from the "
  134. "inspector or with `.new()`. Instead, load an audio file.");
  135. mp3s.instantiate();
  136. mp3s->mp3_stream = Ref<AudioStreamMP3>(this);
  137. mp3s->mp3d = (mp3dec_ex_t *)memalloc(sizeof(mp3dec_ex_t));
  138. int errorcode = mp3dec_ex_open_buf(mp3s->mp3d, data.ptr(), data_len, MP3D_SEEK_TO_SAMPLE);
  139. mp3s->frames_mixed = 0;
  140. mp3s->active = false;
  141. mp3s->loops = 0;
  142. if (errorcode) {
  143. ERR_FAIL_COND_V(errorcode, Ref<AudioStreamPlaybackMP3>());
  144. }
  145. return mp3s;
  146. }
  147. String AudioStreamMP3::get_stream_name() const {
  148. return ""; //return stream_name;
  149. }
  150. void AudioStreamMP3::clear_data() {
  151. data.clear();
  152. }
  153. void AudioStreamMP3::set_data(const Vector<uint8_t> &p_data) {
  154. int src_data_len = p_data.size();
  155. const uint8_t *src_datar = p_data.ptr();
  156. mp3dec_ex_t mp3d;
  157. int err = mp3dec_ex_open_buf(&mp3d, src_datar, src_data_len, MP3D_SEEK_TO_SAMPLE);
  158. ERR_FAIL_COND_MSG(err || mp3d.info.hz == 0, "Failed to decode mp3 file. Make sure it is a valid mp3 audio file.");
  159. channels = mp3d.info.channels;
  160. sample_rate = mp3d.info.hz;
  161. length = float(mp3d.samples) / (sample_rate * float(channels));
  162. mp3dec_ex_close(&mp3d);
  163. clear_data();
  164. data.resize(src_data_len);
  165. memcpy(data.ptrw(), src_datar, src_data_len);
  166. data_len = src_data_len;
  167. }
  168. Vector<uint8_t> AudioStreamMP3::get_data() const {
  169. return data;
  170. }
  171. void AudioStreamMP3::set_loop(bool p_enable) {
  172. loop = p_enable;
  173. }
  174. bool AudioStreamMP3::has_loop() const {
  175. return loop;
  176. }
  177. void AudioStreamMP3::set_loop_offset(double p_seconds) {
  178. loop_offset = p_seconds;
  179. }
  180. double AudioStreamMP3::get_loop_offset() const {
  181. return loop_offset;
  182. }
  183. double AudioStreamMP3::get_length() const {
  184. return length;
  185. }
  186. bool AudioStreamMP3::is_monophonic() const {
  187. return false;
  188. }
  189. void AudioStreamMP3::set_bpm(double p_bpm) {
  190. ERR_FAIL_COND(p_bpm < 0);
  191. bpm = p_bpm;
  192. emit_changed();
  193. }
  194. double AudioStreamMP3::get_bpm() const {
  195. return bpm;
  196. }
  197. void AudioStreamMP3::set_beat_count(int p_beat_count) {
  198. ERR_FAIL_COND(p_beat_count < 0);
  199. beat_count = p_beat_count;
  200. emit_changed();
  201. }
  202. int AudioStreamMP3::get_beat_count() const {
  203. return beat_count;
  204. }
  205. void AudioStreamMP3::set_bar_beats(int p_bar_beats) {
  206. ERR_FAIL_COND(p_bar_beats < 0);
  207. bar_beats = p_bar_beats;
  208. emit_changed();
  209. }
  210. int AudioStreamMP3::get_bar_beats() const {
  211. return bar_beats;
  212. }
  213. void AudioStreamMP3::_bind_methods() {
  214. ClassDB::bind_method(D_METHOD("set_data", "data"), &AudioStreamMP3::set_data);
  215. ClassDB::bind_method(D_METHOD("get_data"), &AudioStreamMP3::get_data);
  216. ClassDB::bind_method(D_METHOD("set_loop", "enable"), &AudioStreamMP3::set_loop);
  217. ClassDB::bind_method(D_METHOD("has_loop"), &AudioStreamMP3::has_loop);
  218. ClassDB::bind_method(D_METHOD("set_loop_offset", "seconds"), &AudioStreamMP3::set_loop_offset);
  219. ClassDB::bind_method(D_METHOD("get_loop_offset"), &AudioStreamMP3::get_loop_offset);
  220. ClassDB::bind_method(D_METHOD("set_bpm", "bpm"), &AudioStreamMP3::set_bpm);
  221. ClassDB::bind_method(D_METHOD("get_bpm"), &AudioStreamMP3::get_bpm);
  222. ClassDB::bind_method(D_METHOD("set_beat_count", "count"), &AudioStreamMP3::set_beat_count);
  223. ClassDB::bind_method(D_METHOD("get_beat_count"), &AudioStreamMP3::get_beat_count);
  224. ClassDB::bind_method(D_METHOD("set_bar_beats", "count"), &AudioStreamMP3::set_bar_beats);
  225. ClassDB::bind_method(D_METHOD("get_bar_beats"), &AudioStreamMP3::get_bar_beats);
  226. ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_data", "get_data");
  227. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bpm", PROPERTY_HINT_RANGE, "0,400,0.01,or_greater"), "set_bpm", "get_bpm");
  228. ADD_PROPERTY(PropertyInfo(Variant::INT, "beat_count", PROPERTY_HINT_RANGE, "0,512,1,or_greater"), "set_beat_count", "get_beat_count");
  229. ADD_PROPERTY(PropertyInfo(Variant::INT, "bar_beats", PROPERTY_HINT_RANGE, "2,32,1,or_greater"), "set_bar_beats", "get_bar_beats");
  230. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
  231. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "loop_offset"), "set_loop_offset", "get_loop_offset");
  232. }
  233. AudioStreamMP3::AudioStreamMP3() {
  234. }
  235. AudioStreamMP3::~AudioStreamMP3() {
  236. clear_data();
  237. }