Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/minimp3/audio_stream_mp3.cpp
11352 views
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_FLOAT_OUTPUT
32
#define MINIMP3_IMPLEMENTATION
33
#define MINIMP3_NO_STDIO
34
35
#include "audio_stream_mp3.h"
36
#include "core/io/file_access.h"
37
38
int AudioStreamPlaybackMP3::_mix_internal(AudioFrame *p_buffer, int p_frames) {
39
if (!active) {
40
return 0;
41
}
42
43
int todo = p_frames;
44
45
int frames_mixed_this_step = p_frames;
46
47
int beat_length_frames = -1;
48
bool use_loop = looping_override ? looping : mp3_stream->loop;
49
50
bool beat_loop = use_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 (use_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
103
float AudioStreamPlaybackMP3::get_stream_sampling_rate() {
104
return mp3_stream->sample_rate;
105
}
106
107
void AudioStreamPlaybackMP3::start(double p_from_pos) {
108
active = true;
109
seek(p_from_pos);
110
loops = 0;
111
begin_resample();
112
}
113
114
void AudioStreamPlaybackMP3::stop() {
115
active = false;
116
}
117
118
bool AudioStreamPlaybackMP3::is_playing() const {
119
return active;
120
}
121
122
int AudioStreamPlaybackMP3::get_loop_count() const {
123
return loops;
124
}
125
126
double AudioStreamPlaybackMP3::get_playback_position() const {
127
return double(frames_mixed) / mp3_stream->sample_rate;
128
}
129
130
void 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
143
void AudioStreamPlaybackMP3::tag_used_streams() {
144
mp3_stream->tag_used(get_playback_position());
145
}
146
147
void AudioStreamPlaybackMP3::set_is_sample(bool p_is_sample) {
148
_is_sample = p_is_sample;
149
}
150
151
bool AudioStreamPlaybackMP3::get_is_sample() const {
152
return _is_sample;
153
}
154
155
Ref<AudioSamplePlayback> AudioStreamPlaybackMP3::get_sample_playback() const {
156
return sample_playback;
157
}
158
159
void AudioStreamPlaybackMP3::set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) {
160
sample_playback = p_playback;
161
if (sample_playback.is_valid()) {
162
sample_playback->stream_playback = Ref<AudioStreamPlayback>(this);
163
}
164
}
165
166
void AudioStreamPlaybackMP3::set_parameter(const StringName &p_name, const Variant &p_value) {
167
if (p_name == SNAME("looping")) {
168
if (p_value == Variant()) {
169
looping_override = false;
170
looping = false;
171
} else {
172
looping_override = true;
173
looping = p_value;
174
}
175
}
176
}
177
178
Variant AudioStreamPlaybackMP3::get_parameter(const StringName &p_name) const {
179
if (looping_override && p_name == SNAME("looping")) {
180
return looping;
181
}
182
return Variant();
183
}
184
185
AudioStreamPlaybackMP3::~AudioStreamPlaybackMP3() {
186
mp3dec_ex_close(&mp3d);
187
}
188
189
Ref<AudioStreamPlayback> AudioStreamMP3::instantiate_playback() {
190
Ref<AudioStreamPlaybackMP3> mp3s;
191
192
ERR_FAIL_COND_V_MSG(data.is_empty(), mp3s,
193
"This AudioStreamMP3 does not have an audio file assigned "
194
"to it. AudioStreamMP3 should not be created from the "
195
"inspector or with `.new()`. Instead, load an audio file.");
196
197
mp3s.instantiate();
198
mp3s->mp3_stream = Ref<AudioStreamMP3>(this);
199
200
int errorcode = mp3dec_ex_open_buf(&mp3s->mp3d, data.ptr(), data_len, MP3D_SEEK_TO_SAMPLE);
201
202
mp3s->frames_mixed = 0;
203
mp3s->active = false;
204
mp3s->loops = 0;
205
206
if (errorcode) {
207
ERR_FAIL_COND_V(errorcode, Ref<AudioStreamPlaybackMP3>());
208
}
209
210
return mp3s;
211
}
212
213
String AudioStreamMP3::get_stream_name() const {
214
return ""; //return stream_name;
215
}
216
217
void AudioStreamMP3::clear_data() {
218
data.clear();
219
}
220
221
void AudioStreamMP3::set_data(const Vector<uint8_t> &p_data) {
222
int src_data_len = p_data.size();
223
224
mp3dec_ex_t *mp3d = memnew(mp3dec_ex_t);
225
int err = mp3dec_ex_open_buf(mp3d, p_data.ptr(), src_data_len, MP3D_SEEK_TO_SAMPLE);
226
if (err || mp3d->info.hz == 0) {
227
memdelete(mp3d);
228
ERR_FAIL_MSG("Failed to decode mp3 file. Make sure it is a valid mp3 audio file.");
229
}
230
231
channels = mp3d->info.channels;
232
sample_rate = mp3d->info.hz;
233
length = float(mp3d->samples) / (sample_rate * float(channels));
234
235
mp3dec_ex_close(mp3d);
236
memdelete(mp3d);
237
238
data = p_data;
239
data_len = src_data_len;
240
}
241
242
Vector<uint8_t> AudioStreamMP3::get_data() const {
243
return Vector<uint8_t>(data);
244
}
245
246
void AudioStreamMP3::set_loop(bool p_enable) {
247
loop = p_enable;
248
}
249
250
bool AudioStreamMP3::has_loop() const {
251
return loop;
252
}
253
254
void AudioStreamMP3::set_loop_offset(double p_seconds) {
255
loop_offset = p_seconds;
256
}
257
258
double AudioStreamMP3::get_loop_offset() const {
259
return loop_offset;
260
}
261
262
double AudioStreamMP3::get_length() const {
263
return length;
264
}
265
266
bool AudioStreamMP3::is_monophonic() const {
267
return false;
268
}
269
270
void AudioStreamMP3::get_parameter_list(List<Parameter> *r_parameters) {
271
r_parameters->push_back(Parameter(PropertyInfo(Variant::BOOL, "looping", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_CHECKABLE), Variant()));
272
}
273
274
void AudioStreamMP3::set_bpm(double p_bpm) {
275
ERR_FAIL_COND(p_bpm < 0);
276
bpm = p_bpm;
277
emit_changed();
278
}
279
280
double AudioStreamMP3::get_bpm() const {
281
return bpm;
282
}
283
284
void AudioStreamMP3::set_beat_count(int p_beat_count) {
285
ERR_FAIL_COND(p_beat_count < 0);
286
beat_count = p_beat_count;
287
emit_changed();
288
}
289
290
int AudioStreamMP3::get_beat_count() const {
291
return beat_count;
292
}
293
294
void AudioStreamMP3::set_bar_beats(int p_bar_beats) {
295
ERR_FAIL_COND(p_bar_beats < 0);
296
bar_beats = p_bar_beats;
297
emit_changed();
298
}
299
300
int AudioStreamMP3::get_bar_beats() const {
301
return bar_beats;
302
}
303
304
Ref<AudioSample> AudioStreamMP3::generate_sample() const {
305
Ref<AudioSample> sample;
306
sample.instantiate();
307
sample->stream = this;
308
sample->loop_mode = loop
309
? AudioSample::LoopMode::LOOP_FORWARD
310
: AudioSample::LoopMode::LOOP_DISABLED;
311
sample->loop_begin = loop_offset;
312
sample->loop_end = 0;
313
return sample;
314
}
315
316
Ref<AudioStreamMP3> AudioStreamMP3::load_from_buffer(const Vector<uint8_t> &p_stream_data) {
317
Ref<AudioStreamMP3> mp3_stream;
318
mp3_stream.instantiate();
319
mp3_stream->set_data(p_stream_data);
320
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.");
321
return mp3_stream;
322
}
323
324
Ref<AudioStreamMP3> AudioStreamMP3::load_from_file(const String &p_path) {
325
const Vector<uint8_t> stream_data = FileAccess::get_file_as_bytes(p_path);
326
ERR_FAIL_COND_V_MSG(stream_data.is_empty(), Ref<AudioStreamMP3>(), vformat("Cannot open file '%s'.", p_path));
327
return load_from_buffer(stream_data);
328
}
329
330
void AudioStreamMP3::_bind_methods() {
331
ClassDB::bind_static_method("AudioStreamMP3", D_METHOD("load_from_buffer", "stream_data"), &AudioStreamMP3::load_from_buffer);
332
ClassDB::bind_static_method("AudioStreamMP3", D_METHOD("load_from_file", "path"), &AudioStreamMP3::load_from_file);
333
334
ClassDB::bind_method(D_METHOD("set_data", "data"), &AudioStreamMP3::set_data);
335
ClassDB::bind_method(D_METHOD("get_data"), &AudioStreamMP3::get_data);
336
337
ClassDB::bind_method(D_METHOD("set_loop", "enable"), &AudioStreamMP3::set_loop);
338
ClassDB::bind_method(D_METHOD("has_loop"), &AudioStreamMP3::has_loop);
339
340
ClassDB::bind_method(D_METHOD("set_loop_offset", "seconds"), &AudioStreamMP3::set_loop_offset);
341
ClassDB::bind_method(D_METHOD("get_loop_offset"), &AudioStreamMP3::get_loop_offset);
342
343
ClassDB::bind_method(D_METHOD("set_bpm", "bpm"), &AudioStreamMP3::set_bpm);
344
ClassDB::bind_method(D_METHOD("get_bpm"), &AudioStreamMP3::get_bpm);
345
346
ClassDB::bind_method(D_METHOD("set_beat_count", "count"), &AudioStreamMP3::set_beat_count);
347
ClassDB::bind_method(D_METHOD("get_beat_count"), &AudioStreamMP3::get_beat_count);
348
349
ClassDB::bind_method(D_METHOD("set_bar_beats", "count"), &AudioStreamMP3::set_bar_beats);
350
ClassDB::bind_method(D_METHOD("get_bar_beats"), &AudioStreamMP3::get_bar_beats);
351
352
ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_data", "get_data");
353
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bpm", PROPERTY_HINT_RANGE, "0,400,0.01,or_greater"), "set_bpm", "get_bpm");
354
ADD_PROPERTY(PropertyInfo(Variant::INT, "beat_count", PROPERTY_HINT_RANGE, "0,512,1,or_greater"), "set_beat_count", "get_beat_count");
355
ADD_PROPERTY(PropertyInfo(Variant::INT, "bar_beats", PROPERTY_HINT_RANGE, "2,32,1,or_greater"), "set_bar_beats", "get_bar_beats");
356
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
357
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "loop_offset"), "set_loop_offset", "get_loop_offset");
358
}
359
360
AudioStreamMP3::AudioStreamMP3() {
361
}
362
363
AudioStreamMP3::~AudioStreamMP3() {
364
clear_data();
365
}
366
367