| 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 | |
| 31 | #define DR_MP3_FLOAT_OUTPUT |
| 32 | #define DR_MP3_IMPLEMENTATION |
| 33 | #define DR_MP3_NO_STDIO |
| 34 | |
| 35 | #include "audio_stream_mp3.h" |
| 36 | #include "core/io/file_access.h" |
| 37 | |
| 38 | #include "thirdparty/dr_libs/dr_bridge.h" |
| 39 | |
| 40 | int AudioStreamPlaybackMP3::_mix_internal(AudioFrame *p_buffer, int p_frames) { |
| 41 | if (!active) { |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | int todo = p_frames; |
| 46 | |
| 47 | int frames_mixed_this_step = p_frames; |
| 48 | |
| 49 | int beat_length_frames = -1; |
| 50 | bool use_loop = looping_override ? looping : mp3_stream->loop; |
| 51 | |
| 52 | bool beat_loop = use_loop && mp3_stream->get_bpm() > 0 && mp3_stream->get_beat_count() > 0; |
| 53 | if (beat_loop) { |
| 54 | beat_length_frames = mp3_stream->get_beat_count() * mp3_stream->sample_rate * 60 / mp3_stream->get_bpm(); |
| 55 | } |
| 56 | |
| 57 | while (todo && active) { |
| 58 | drmp3d_sample_t buf_frame[2]; |
| 59 | |
| 60 | int samples_mixed = drmp3_read_pcm_frames_f32(&mp3d, 1, buf_frame); |
| 61 | |
| 62 | if (samples_mixed) { |
| 63 | p_buffer[p_frames - todo] = AudioFrame(buf_frame[0], buf_frame[mp3d.channels - 1]); |
| 64 | if (loop_fade_remaining < FADE_SIZE) { |
| 65 | p_buffer[p_frames - todo] += loop_fade[loop_fade_remaining] * (float(FADE_SIZE - loop_fade_remaining) / float(FADE_SIZE)); |
| 66 | loop_fade_remaining++; |
| 67 | } |
| 68 | --todo; |
| 69 | ++frames_mixed; |
| 70 | |
| 71 | if (beat_loop && (int)frames_mixed >= beat_length_frames) { |
| 72 | for (int i = 0; i < FADE_SIZE; i++) { |
| 73 | samples_mixed = drmp3_read_pcm_frames_f32(&mp3d, 1, buf_frame); |
| 74 | loop_fade[i] = AudioFrame(buf_frame[0], buf_frame[mp3d.channels - 1]); |
| 75 | if (!samples_mixed) { |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | loop_fade_remaining = 0; |
| 80 | seek(mp3_stream->loop_offset); |
| 81 | loops++; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | else { |
| 86 | //EOF |
| 87 | if (use_loop) { |
| 88 | seek(mp3_stream->loop_offset); |
| 89 | loops++; |
| 90 | } else { |
| 91 | frames_mixed_this_step = p_frames - todo; |
| 92 | //fill remainder with silence |
| 93 | for (int i = p_frames - todo; i < p_frames; i++) { |
| 94 | p_buffer[i] = AudioFrame(0, 0); |
| 95 | } |
| 96 | active = false; |
| 97 | todo = 0; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | return frames_mixed_this_step; |
| 102 | } |
| 103 | |
| 104 | float AudioStreamPlaybackMP3::get_stream_sampling_rate() { |
| 105 | return mp3_stream->sample_rate; |
| 106 | } |
| 107 | |
| 108 | void AudioStreamPlaybackMP3::start(double p_from_pos) { |
| 109 | active = true; |
| 110 | seek(p_from_pos); |
| 111 | loops = 0; |
| 112 | begin_resample(); |
| 113 | } |
| 114 | |
| 115 | void AudioStreamPlaybackMP3::stop() { |
| 116 | active = false; |
| 117 | } |
| 118 | |
| 119 | bool AudioStreamPlaybackMP3::is_playing() const { |
| 120 | return active; |
| 121 | } |
| 122 | |
| 123 | int AudioStreamPlaybackMP3::get_loop_count() const { |
| 124 | return loops; |
| 125 | } |
| 126 | |
| 127 | double AudioStreamPlaybackMP3::get_playback_position() const { |
| 128 | return double(frames_mixed) / mp3_stream->sample_rate; |
| 129 | } |
| 130 | |
| 131 | void AudioStreamPlaybackMP3::seek(double p_time) { |
| 132 | if (!active) { |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | if (p_time >= mp3_stream->get_length()) { |
| 137 | p_time = 0; |
| 138 | } |
| 139 | |
| 140 | frames_mixed = uint32_t(mp3_stream->sample_rate * p_time); |
| 141 | drmp3_seek_to_pcm_frame(&mp3d, (uint64_t)frames_mixed); |
| 142 | } |
| 143 | |
| 144 | void AudioStreamPlaybackMP3::tag_used_streams() { |
| 145 | mp3_stream->tag_used(get_playback_position()); |
| 146 | } |
| 147 | |
| 148 | void AudioStreamPlaybackMP3::set_is_sample(bool p_is_sample) { |
| 149 | _is_sample = p_is_sample; |
| 150 | } |
| 151 | |
| 152 | bool AudioStreamPlaybackMP3::get_is_sample() const { |
| 153 | return _is_sample; |
| 154 | } |
| 155 | |
| 156 | Ref<AudioSamplePlayback> AudioStreamPlaybackMP3::get_sample_playback() const { |
| 157 | return sample_playback; |
| 158 | } |
| 159 | |
| 160 | void AudioStreamPlaybackMP3::set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) { |
| 161 | sample_playback = p_playback; |
| 162 | if (sample_playback.is_valid()) { |
| 163 | sample_playback->stream_playback = Ref<AudioStreamPlayback>(this); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void AudioStreamPlaybackMP3::set_parameter(const StringName &p_name, const Variant &p_value) { |
| 168 | if (p_name == SNAME("looping")) { |
| 169 | if (p_value == Variant()) { |
| 170 | looping_override = false; |
| 171 | looping = false; |
| 172 | } else { |
| 173 | looping_override = true; |
| 174 | looping = p_value; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | Variant AudioStreamPlaybackMP3::get_parameter(const StringName &p_name) const { |
| 180 | if (looping_override && p_name == SNAME("looping")) { |
| 181 | return looping; |
| 182 | } |
| 183 | return Variant(); |
| 184 | } |
| 185 | |
| 186 | AudioStreamPlaybackMP3::~AudioStreamPlaybackMP3() { |
| 187 | drmp3_uninit(&mp3d); |
| 188 | } |
| 189 | |
| 190 | Ref<AudioStreamPlayback> AudioStreamMP3::instantiate_playback() { |
| 191 | Ref<AudioStreamPlaybackMP3> mp3s; |
| 192 | |
| 193 | ERR_FAIL_COND_V_MSG(data.is_empty(), mp3s, |
| 194 | "This AudioStreamMP3 does not have an audio file assigned " |
| 195 | "to it. AudioStreamMP3 should not be created from the " |
| 196 | "inspector or with `.new()`. Instead, load an audio file."); |
| 197 | |
| 198 | mp3s.instantiate(); |
| 199 | mp3s->mp3_stream = Ref<AudioStreamMP3>(this); |
| 200 | |
| 201 | int success = drmp3_init_memory(&mp3s->mp3d, data.ptr(), data_len, (drmp3_allocation_callbacks *)&dr_alloc_calls); |
| 202 | |
| 203 | mp3s->frames_mixed = 0; |
| 204 | mp3s->active = false; |
| 205 | mp3s->loops = 0; |
| 206 | |
| 207 | ERR_FAIL_COND_V(!success, Ref<AudioStreamPlaybackMP3>()); |
| 208 | |
| 209 | return mp3s; |
| 210 | } |
| 211 | |
| 212 | String AudioStreamMP3::get_stream_name() const { |
| 213 | return ""; //return stream_name; |
| 214 | } |
| 215 | |
| 216 | void AudioStreamMP3::clear_data() { |
| 217 | data.clear(); |
| 218 | } |
| 219 | |
| 220 | void AudioStreamMP3::set_data(const Vector<uint8_t> &p_data) { |
| 221 | int src_data_len = p_data.size(); |
| 222 | |
| 223 | drmp3 *mp3d = memnew(drmp3); |
| 224 | int success = drmp3_init_memory(mp3d, p_data.ptr(), src_data_len, (drmp3_allocation_callbacks *)&dr_alloc_calls); |
| 225 | if (!success || mp3d->sampleRate == 0) { |
| 226 | memdelete(mp3d); |
| 227 | ERR_FAIL_MSG("Failed to decode mp3 file. Make sure it is a valid mp3 audio file."); |
| 228 | } |
| 229 | |
| 230 | channels = mp3d->channels; |
| 231 | sample_rate = mp3d->sampleRate; |
| 232 | length = float(drmp3_get_pcm_frame_count(mp3d)) / (mp3d->sampleRate); |
| 233 | |
| 234 | drmp3_uninit(mp3d); |
| 235 | memdelete(mp3d); |
| 236 | |
| 237 | data = p_data; |
| 238 | data_len = src_data_len; |
| 239 | } |
| 240 | |
| 241 | Vector<uint8_t> AudioStreamMP3::get_data() const { |
| 242 | return Vector<uint8_t>(data); |
| 243 | } |
| 244 | |
| 245 | void AudioStreamMP3::set_loop(bool p_enable) { |
| 246 | loop = p_enable; |
| 247 | } |
| 248 | |
| 249 | bool AudioStreamMP3::has_loop() const { |
| 250 | return loop; |
| 251 | } |
| 252 | |
| 253 | void AudioStreamMP3::set_loop_offset(double p_seconds) { |
| 254 | loop_offset = p_seconds; |
| 255 | } |
| 256 | |
| 257 | double AudioStreamMP3::get_loop_offset() const { |
| 258 | return loop_offset; |
| 259 | } |
| 260 | |
| 261 | double AudioStreamMP3::get_length() const { |
| 262 | return length; |
| 263 | } |
| 264 | |
| 265 | bool AudioStreamMP3::is_monophonic() const { |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | void AudioStreamMP3::get_parameter_list(List<Parameter> *r_parameters) { |
| 270 | r_parameters->push_back(Parameter(PropertyInfo(Variant::BOOL, "looping", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_CHECKABLE), Variant())); |
| 271 | } |
| 272 | |
| 273 | void AudioStreamMP3::set_bpm(double p_bpm) { |
| 274 | ERR_FAIL_COND(p_bpm < 0); |
| 275 | bpm = p_bpm; |
| 276 | emit_changed(); |
| 277 | } |
| 278 | |
| 279 | double AudioStreamMP3::get_bpm() const { |
| 280 | return bpm; |
| 281 | } |
| 282 | |
| 283 | void AudioStreamMP3::set_beat_count(int p_beat_count) { |
| 284 | ERR_FAIL_COND(p_beat_count < 0); |
| 285 | beat_count = p_beat_count; |
| 286 | emit_changed(); |
| 287 | } |
| 288 | |
| 289 | int AudioStreamMP3::get_beat_count() const { |
| 290 | return beat_count; |
| 291 | } |
| 292 | |
| 293 | void AudioStreamMP3::set_bar_beats(int p_bar_beats) { |
| 294 | ERR_FAIL_COND(p_bar_beats < 0); |
| 295 | bar_beats = p_bar_beats; |
| 296 | emit_changed(); |
| 297 | } |
| 298 | |
| 299 | int AudioStreamMP3::get_bar_beats() const { |
| 300 | return bar_beats; |
| 301 | } |
| 302 | |
| 303 | Ref<AudioSample> AudioStreamMP3::generate_sample() const { |
| 304 | Ref<AudioSample> sample; |
| 305 | sample.instantiate(); |
| 306 | sample->stream = this; |
| 307 | sample->loop_mode = loop |
| 308 | ? AudioSample::LoopMode::LOOP_FORWARD |
| 309 | : AudioSample::LoopMode::LOOP_DISABLED; |
| 310 | sample->loop_begin = loop_offset; |
| 311 | sample->loop_end = 0; |
| 312 | return sample; |
| 313 | } |
| 314 | |
| 315 | Ref<AudioStreamMP3> AudioStreamMP3::load_from_buffer(const Vector<uint8_t> &p_stream_data) { |
| 316 | Ref<AudioStreamMP3> mp3_stream; |
| 317 | mp3_stream.instantiate(); |
| 318 | mp3_stream->set_data(p_stream_data); |
| 319 | 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."); |
| 320 | return mp3_stream; |
| 321 | } |
| 322 | |
| 323 | Ref<AudioStreamMP3> AudioStreamMP3::load_from_file(const String &p_path) { |
| 324 | const Vector<uint8_t> stream_data = FileAccess::get_file_as_bytes(p_path); |
| 325 | ERR_FAIL_COND_V_MSG(stream_data.is_empty(), Ref<AudioStreamMP3>(), vformat("Cannot open file '%s'.", p_path)); |
| 326 | return load_from_buffer(stream_data); |
| 327 | } |
| 328 | |
| 329 | void AudioStreamMP3::_bind_methods() { |
| 330 | ClassDB::bind_static_method("AudioStreamMP3", D_METHOD("load_from_buffer", "stream_data"), &AudioStreamMP3::load_from_buffer); |
| 331 | ClassDB::bind_static_method("AudioStreamMP3", D_METHOD("load_from_file", "path"), &AudioStreamMP3::load_from_file); |
| 332 | |
| 333 | ClassDB::bind_method(D_METHOD("set_data", "data"), &AudioStreamMP3::set_data); |
| 334 | ClassDB::bind_method(D_METHOD("get_data"), &AudioStreamMP3::get_data); |
| 335 | |
| 336 | ClassDB::bind_method(D_METHOD("set_loop", "enable"), &AudioStreamMP3::set_loop); |
| 337 | ClassDB::bind_method(D_METHOD("has_loop"), &AudioStreamMP3::has_loop); |
| 338 | |
| 339 | ClassDB::bind_method(D_METHOD("set_loop_offset", "seconds"), &AudioStreamMP3::set_loop_offset); |
| 340 | ClassDB::bind_method(D_METHOD("get_loop_offset"), &AudioStreamMP3::get_loop_offset); |
| 341 | |
| 342 | ClassDB::bind_method(D_METHOD("set_bpm", "bpm"), &AudioStreamMP3::set_bpm); |
| 343 | ClassDB::bind_method(D_METHOD("get_bpm"), &AudioStreamMP3::get_bpm); |
| 344 | |
| 345 | ClassDB::bind_method(D_METHOD("set_beat_count", "count"), &AudioStreamMP3::set_beat_count); |
| 346 | ClassDB::bind_method(D_METHOD("get_beat_count"), &AudioStreamMP3::get_beat_count); |
| 347 | |
| 348 | ClassDB::bind_method(D_METHOD("set_bar_beats", "count"), &AudioStreamMP3::set_bar_beats); |
| 349 | ClassDB::bind_method(D_METHOD("get_bar_beats"), &AudioStreamMP3::get_bar_beats); |
| 350 | |
| 351 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_data", "get_data"); |
| 352 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bpm", PROPERTY_HINT_RANGE, "0,400,0.01,or_greater"), "set_bpm", "get_bpm"); |
| 353 | ADD_PROPERTY(PropertyInfo(Variant::INT, "beat_count", PROPERTY_HINT_RANGE, "0,512,1,or_greater"), "set_beat_count", "get_beat_count"); |
| 354 | ADD_PROPERTY(PropertyInfo(Variant::INT, "bar_beats", PROPERTY_HINT_RANGE, "2,32,1,or_greater"), "set_bar_beats", "get_bar_beats"); |
| 355 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop"); |
| 356 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "loop_offset"), "set_loop_offset", "get_loop_offset"); |
| 357 | } |
| 358 | |
| 359 | AudioStreamMP3::AudioStreamMP3() { |
| 360 | } |
| 361 | |
| 362 | AudioStreamMP3::~AudioStreamMP3() { |
| 363 | clear_data(); |
| 364 | } |