audio_stream_mp3.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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_is_sample(bool p_is_sample) {
  125. _is_sample = p_is_sample;
  126. }
  127. bool AudioStreamPlaybackMP3::get_is_sample() const {
  128. return _is_sample;
  129. }
  130. Ref<AudioSamplePlayback> AudioStreamPlaybackMP3::get_sample_playback() const {
  131. return sample_playback;
  132. }
  133. void AudioStreamPlaybackMP3::set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) {
  134. sample_playback = p_playback;
  135. if (sample_playback.is_valid()) {
  136. sample_playback->stream_playback = Ref<AudioStreamPlayback>(this);
  137. }
  138. }
  139. void AudioStreamPlaybackMP3::set_parameter(const StringName &p_name, const Variant &p_value) {
  140. if (p_name == SNAME("looping")) {
  141. if (p_value == Variant()) {
  142. looping_override = false;
  143. looping = false;
  144. } else {
  145. looping_override = true;
  146. looping = p_value;
  147. }
  148. }
  149. }
  150. Variant AudioStreamPlaybackMP3::get_parameter(const StringName &p_name) const {
  151. if (looping_override && p_name == SNAME("looping")) {
  152. return looping;
  153. }
  154. return Variant();
  155. }
  156. AudioStreamPlaybackMP3::~AudioStreamPlaybackMP3() {
  157. mp3dec_ex_close(&mp3d);
  158. }
  159. Ref<AudioStreamPlayback> AudioStreamMP3::instantiate_playback() {
  160. Ref<AudioStreamPlaybackMP3> mp3s;
  161. ERR_FAIL_COND_V_MSG(data.is_empty(), mp3s,
  162. "This AudioStreamMP3 does not have an audio file assigned "
  163. "to it. AudioStreamMP3 should not be created from the "
  164. "inspector or with `.new()`. Instead, load an audio file.");
  165. mp3s.instantiate();
  166. mp3s->mp3_stream = Ref<AudioStreamMP3>(this);
  167. int errorcode = mp3dec_ex_open_buf(&mp3s->mp3d, data.ptr(), data_len, MP3D_SEEK_TO_SAMPLE);
  168. mp3s->frames_mixed = 0;
  169. mp3s->active = false;
  170. mp3s->loops = 0;
  171. if (errorcode) {
  172. ERR_FAIL_COND_V(errorcode, Ref<AudioStreamPlaybackMP3>());
  173. }
  174. return mp3s;
  175. }
  176. String AudioStreamMP3::get_stream_name() const {
  177. return ""; //return stream_name;
  178. }
  179. void AudioStreamMP3::clear_data() {
  180. data.clear();
  181. }
  182. void AudioStreamMP3::set_data(const Vector<uint8_t> &p_data) {
  183. int src_data_len = p_data.size();
  184. mp3dec_ex_t *mp3d = memnew(mp3dec_ex_t);
  185. int err = mp3dec_ex_open_buf(mp3d, p_data.ptr(), src_data_len, MP3D_SEEK_TO_SAMPLE);
  186. if (err || mp3d->info.hz == 0) {
  187. memdelete(mp3d);
  188. ERR_FAIL_MSG("Failed to decode mp3 file. Make sure it is a valid mp3 audio file.");
  189. }
  190. channels = mp3d->info.channels;
  191. sample_rate = mp3d->info.hz;
  192. length = float(mp3d->samples) / (sample_rate * float(channels));
  193. mp3dec_ex_close(mp3d);
  194. memdelete(mp3d);
  195. data = p_data;
  196. data_len = src_data_len;
  197. }
  198. Vector<uint8_t> AudioStreamMP3::get_data() const {
  199. return data;
  200. }
  201. void AudioStreamMP3::set_loop(bool p_enable) {
  202. loop = p_enable;
  203. }
  204. bool AudioStreamMP3::has_loop() const {
  205. return loop;
  206. }
  207. void AudioStreamMP3::set_loop_offset(double p_seconds) {
  208. loop_offset = p_seconds;
  209. }
  210. double AudioStreamMP3::get_loop_offset() const {
  211. return loop_offset;
  212. }
  213. double AudioStreamMP3::get_length() const {
  214. return length;
  215. }
  216. bool AudioStreamMP3::is_monophonic() const {
  217. return false;
  218. }
  219. void AudioStreamMP3::get_parameter_list(List<Parameter> *r_parameters) {
  220. r_parameters->push_back(Parameter(PropertyInfo(Variant::BOOL, "looping", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_CHECKABLE), Variant()));
  221. }
  222. void AudioStreamMP3::set_bpm(double p_bpm) {
  223. ERR_FAIL_COND(p_bpm < 0);
  224. bpm = p_bpm;
  225. emit_changed();
  226. }
  227. double AudioStreamMP3::get_bpm() const {
  228. return bpm;
  229. }
  230. void AudioStreamMP3::set_beat_count(int p_beat_count) {
  231. ERR_FAIL_COND(p_beat_count < 0);
  232. beat_count = p_beat_count;
  233. emit_changed();
  234. }
  235. int AudioStreamMP3::get_beat_count() const {
  236. return beat_count;
  237. }
  238. void AudioStreamMP3::set_bar_beats(int p_bar_beats) {
  239. ERR_FAIL_COND(p_bar_beats < 0);
  240. bar_beats = p_bar_beats;
  241. emit_changed();
  242. }
  243. int AudioStreamMP3::get_bar_beats() const {
  244. return bar_beats;
  245. }
  246. Ref<AudioSample> AudioStreamMP3::generate_sample() const {
  247. Ref<AudioSample> sample;
  248. sample.instantiate();
  249. sample->stream = this;
  250. sample->loop_mode = loop
  251. ? AudioSample::LoopMode::LOOP_FORWARD
  252. : AudioSample::LoopMode::LOOP_DISABLED;
  253. sample->loop_begin = loop_offset;
  254. sample->loop_end = 0;
  255. return sample;
  256. }
  257. void AudioStreamMP3::_bind_methods() {
  258. ClassDB::bind_method(D_METHOD("set_data", "data"), &AudioStreamMP3::set_data);
  259. ClassDB::bind_method(D_METHOD("get_data"), &AudioStreamMP3::get_data);
  260. ClassDB::bind_method(D_METHOD("set_loop", "enable"), &AudioStreamMP3::set_loop);
  261. ClassDB::bind_method(D_METHOD("has_loop"), &AudioStreamMP3::has_loop);
  262. ClassDB::bind_method(D_METHOD("set_loop_offset", "seconds"), &AudioStreamMP3::set_loop_offset);
  263. ClassDB::bind_method(D_METHOD("get_loop_offset"), &AudioStreamMP3::get_loop_offset);
  264. ClassDB::bind_method(D_METHOD("set_bpm", "bpm"), &AudioStreamMP3::set_bpm);
  265. ClassDB::bind_method(D_METHOD("get_bpm"), &AudioStreamMP3::get_bpm);
  266. ClassDB::bind_method(D_METHOD("set_beat_count", "count"), &AudioStreamMP3::set_beat_count);
  267. ClassDB::bind_method(D_METHOD("get_beat_count"), &AudioStreamMP3::get_beat_count);
  268. ClassDB::bind_method(D_METHOD("set_bar_beats", "count"), &AudioStreamMP3::set_bar_beats);
  269. ClassDB::bind_method(D_METHOD("get_bar_beats"), &AudioStreamMP3::get_bar_beats);
  270. ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_data", "get_data");
  271. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bpm", PROPERTY_HINT_RANGE, "0,400,0.01,or_greater"), "set_bpm", "get_bpm");
  272. ADD_PROPERTY(PropertyInfo(Variant::INT, "beat_count", PROPERTY_HINT_RANGE, "0,512,1,or_greater"), "set_beat_count", "get_beat_count");
  273. ADD_PROPERTY(PropertyInfo(Variant::INT, "bar_beats", PROPERTY_HINT_RANGE, "2,32,1,or_greater"), "set_bar_beats", "get_bar_beats");
  274. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
  275. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "loop_offset"), "set_loop_offset", "get_loop_offset");
  276. }
  277. AudioStreamMP3::AudioStreamMP3() {
  278. }
  279. AudioStreamMP3::~AudioStreamMP3() {
  280. clear_data();
  281. }