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