audio_stream_mp3.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 use_loop = looping_override ? looping : mp3_stream->loop;
  43. bool beat_loop = use_loop && mp3_stream->get_bpm() > 0 && mp3_stream->get_beat_count() > 0;
  44. if (beat_loop) {
  45. beat_length_frames = mp3_stream->get_beat_count() * mp3_stream->sample_rate * 60 / mp3_stream->get_bpm();
  46. }
  47. while (todo && active) {
  48. mp3dec_frame_info_t frame_info;
  49. mp3d_sample_t *buf_frame = nullptr;
  50. int samples_mixed = mp3dec_ex_read_frame(mp3d, &buf_frame, &frame_info, mp3_stream->channels);
  51. if (samples_mixed) {
  52. p_buffer[p_frames - todo] = AudioFrame(buf_frame[0], buf_frame[samples_mixed - 1]);
  53. if (loop_fade_remaining < FADE_SIZE) {
  54. p_buffer[p_frames - todo] += loop_fade[loop_fade_remaining] * (float(FADE_SIZE - loop_fade_remaining) / float(FADE_SIZE));
  55. loop_fade_remaining++;
  56. }
  57. --todo;
  58. ++frames_mixed;
  59. if (beat_loop && (int)frames_mixed >= beat_length_frames) {
  60. for (int i = 0; i < FADE_SIZE; i++) {
  61. samples_mixed = mp3dec_ex_read_frame(mp3d, &buf_frame, &frame_info, mp3_stream->channels);
  62. loop_fade[i] = AudioFrame(buf_frame[0], buf_frame[samples_mixed - 1]);
  63. if (!samples_mixed) {
  64. break;
  65. }
  66. }
  67. loop_fade_remaining = 0;
  68. seek(mp3_stream->loop_offset);
  69. loops++;
  70. }
  71. }
  72. else {
  73. //EOF
  74. if (use_loop) {
  75. seek(mp3_stream->loop_offset);
  76. loops++;
  77. } else {
  78. frames_mixed_this_step = p_frames - todo;
  79. //fill remainder with silence
  80. for (int i = p_frames - todo; i < p_frames; i++) {
  81. p_buffer[i] = AudioFrame(0, 0);
  82. }
  83. active = false;
  84. todo = 0;
  85. }
  86. }
  87. }
  88. return frames_mixed_this_step;
  89. }
  90. float AudioStreamPlaybackMP3::get_stream_sampling_rate() {
  91. return mp3_stream->sample_rate;
  92. }
  93. void AudioStreamPlaybackMP3::start(double p_from_pos) {
  94. active = true;
  95. seek(p_from_pos);
  96. loops = 0;
  97. begin_resample();
  98. }
  99. void AudioStreamPlaybackMP3::stop() {
  100. active = false;
  101. }
  102. bool AudioStreamPlaybackMP3::is_playing() const {
  103. return active;
  104. }
  105. int AudioStreamPlaybackMP3::get_loop_count() const {
  106. return loops;
  107. }
  108. double AudioStreamPlaybackMP3::get_playback_position() const {
  109. return double(frames_mixed) / mp3_stream->sample_rate;
  110. }
  111. void AudioStreamPlaybackMP3::seek(double p_time) {
  112. if (!active) {
  113. return;
  114. }
  115. if (p_time >= mp3_stream->get_length()) {
  116. p_time = 0;
  117. }
  118. frames_mixed = uint32_t(mp3_stream->sample_rate * p_time);
  119. mp3dec_ex_seek(mp3d, (uint64_t)frames_mixed * mp3_stream->channels);
  120. }
  121. void AudioStreamPlaybackMP3::tag_used_streams() {
  122. mp3_stream->tag_used(get_playback_position());
  123. }
  124. void AudioStreamPlaybackMP3::set_parameter(const StringName &p_name, const Variant &p_value) {
  125. if (p_name == SNAME("looping")) {
  126. if (p_value == Variant()) {
  127. looping_override = false;
  128. looping = false;
  129. } else {
  130. looping_override = true;
  131. looping = p_value;
  132. }
  133. }
  134. }
  135. Variant AudioStreamPlaybackMP3::get_parameter(const StringName &p_name) const {
  136. if (looping_override && p_name == SNAME("looping")) {
  137. return looping;
  138. }
  139. return Variant();
  140. }
  141. AudioStreamPlaybackMP3::~AudioStreamPlaybackMP3() {
  142. if (mp3d) {
  143. mp3dec_ex_close(mp3d);
  144. memfree(mp3d);
  145. }
  146. }
  147. Ref<AudioStreamPlayback> AudioStreamMP3::instantiate_playback() {
  148. Ref<AudioStreamPlaybackMP3> mp3s;
  149. ERR_FAIL_COND_V_MSG(data.is_empty(), mp3s,
  150. "This AudioStreamMP3 does not have an audio file assigned "
  151. "to it. AudioStreamMP3 should not be created from the "
  152. "inspector or with `.new()`. Instead, load an audio file.");
  153. mp3s.instantiate();
  154. mp3s->mp3_stream = Ref<AudioStreamMP3>(this);
  155. mp3s->mp3d = (mp3dec_ex_t *)memalloc(sizeof(mp3dec_ex_t));
  156. int errorcode = mp3dec_ex_open_buf(mp3s->mp3d, data.ptr(), data_len, MP3D_SEEK_TO_SAMPLE);
  157. mp3s->frames_mixed = 0;
  158. mp3s->active = false;
  159. mp3s->loops = 0;
  160. if (errorcode) {
  161. ERR_FAIL_COND_V(errorcode, Ref<AudioStreamPlaybackMP3>());
  162. }
  163. return mp3s;
  164. }
  165. String AudioStreamMP3::get_stream_name() const {
  166. return ""; //return stream_name;
  167. }
  168. void AudioStreamMP3::clear_data() {
  169. data.clear();
  170. }
  171. void AudioStreamMP3::set_data(const Vector<uint8_t> &p_data) {
  172. int src_data_len = p_data.size();
  173. const uint8_t *src_datar = p_data.ptr();
  174. mp3dec_ex_t mp3d;
  175. int err = mp3dec_ex_open_buf(&mp3d, src_datar, src_data_len, MP3D_SEEK_TO_SAMPLE);
  176. ERR_FAIL_COND_MSG(err || mp3d.info.hz == 0, "Failed to decode mp3 file. Make sure it is a valid mp3 audio file.");
  177. channels = mp3d.info.channels;
  178. sample_rate = mp3d.info.hz;
  179. length = float(mp3d.samples) / (sample_rate * float(channels));
  180. mp3dec_ex_close(&mp3d);
  181. clear_data();
  182. data.resize(src_data_len);
  183. memcpy(data.ptrw(), src_datar, src_data_len);
  184. data_len = src_data_len;
  185. }
  186. Vector<uint8_t> AudioStreamMP3::get_data() const {
  187. return data;
  188. }
  189. void AudioStreamMP3::set_loop(bool p_enable) {
  190. loop = p_enable;
  191. }
  192. bool AudioStreamMP3::has_loop() const {
  193. return loop;
  194. }
  195. void AudioStreamMP3::set_loop_offset(double p_seconds) {
  196. loop_offset = p_seconds;
  197. }
  198. double AudioStreamMP3::get_loop_offset() const {
  199. return loop_offset;
  200. }
  201. double AudioStreamMP3::get_length() const {
  202. return length;
  203. }
  204. bool AudioStreamMP3::is_monophonic() const {
  205. return false;
  206. }
  207. void AudioStreamMP3::get_parameter_list(List<Parameter> *r_parameters) {
  208. r_parameters->push_back(Parameter(PropertyInfo(Variant::BOOL, "looping", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_CHECKABLE), Variant()));
  209. }
  210. void AudioStreamMP3::set_bpm(double p_bpm) {
  211. ERR_FAIL_COND(p_bpm < 0);
  212. bpm = p_bpm;
  213. emit_changed();
  214. }
  215. double AudioStreamMP3::get_bpm() const {
  216. return bpm;
  217. }
  218. void AudioStreamMP3::set_beat_count(int p_beat_count) {
  219. ERR_FAIL_COND(p_beat_count < 0);
  220. beat_count = p_beat_count;
  221. emit_changed();
  222. }
  223. int AudioStreamMP3::get_beat_count() const {
  224. return beat_count;
  225. }
  226. void AudioStreamMP3::set_bar_beats(int p_bar_beats) {
  227. ERR_FAIL_COND(p_bar_beats < 0);
  228. bar_beats = p_bar_beats;
  229. emit_changed();
  230. }
  231. int AudioStreamMP3::get_bar_beats() const {
  232. return bar_beats;
  233. }
  234. void AudioStreamMP3::_bind_methods() {
  235. ClassDB::bind_method(D_METHOD("set_data", "data"), &AudioStreamMP3::set_data);
  236. ClassDB::bind_method(D_METHOD("get_data"), &AudioStreamMP3::get_data);
  237. ClassDB::bind_method(D_METHOD("set_loop", "enable"), &AudioStreamMP3::set_loop);
  238. ClassDB::bind_method(D_METHOD("has_loop"), &AudioStreamMP3::has_loop);
  239. ClassDB::bind_method(D_METHOD("set_loop_offset", "seconds"), &AudioStreamMP3::set_loop_offset);
  240. ClassDB::bind_method(D_METHOD("get_loop_offset"), &AudioStreamMP3::get_loop_offset);
  241. ClassDB::bind_method(D_METHOD("set_bpm", "bpm"), &AudioStreamMP3::set_bpm);
  242. ClassDB::bind_method(D_METHOD("get_bpm"), &AudioStreamMP3::get_bpm);
  243. ClassDB::bind_method(D_METHOD("set_beat_count", "count"), &AudioStreamMP3::set_beat_count);
  244. ClassDB::bind_method(D_METHOD("get_beat_count"), &AudioStreamMP3::get_beat_count);
  245. ClassDB::bind_method(D_METHOD("set_bar_beats", "count"), &AudioStreamMP3::set_bar_beats);
  246. ClassDB::bind_method(D_METHOD("get_bar_beats"), &AudioStreamMP3::get_bar_beats);
  247. ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_data", "get_data");
  248. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bpm", PROPERTY_HINT_RANGE, "0,400,0.01,or_greater"), "set_bpm", "get_bpm");
  249. ADD_PROPERTY(PropertyInfo(Variant::INT, "beat_count", PROPERTY_HINT_RANGE, "0,512,1,or_greater"), "set_beat_count", "get_beat_count");
  250. ADD_PROPERTY(PropertyInfo(Variant::INT, "bar_beats", PROPERTY_HINT_RANGE, "2,32,1,or_greater"), "set_bar_beats", "get_bar_beats");
  251. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
  252. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "loop_offset"), "set_loop_offset", "get_loop_offset");
  253. }
  254. AudioStreamMP3::AudioStreamMP3() {
  255. }
  256. AudioStreamMP3::~AudioStreamMP3() {
  257. clear_data();
  258. }