audio_stream_mp3.cpp 12 KB

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