Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/minimp3/audio_stream_mp3.cpp
10277 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
37
int AudioStreamPlaybackMP3::_mix_internal(AudioFrame *p_buffer, int p_frames) {
38
if (!active) {
39
return 0;
40
}
41
42
int todo = p_frames;
43
44
int frames_mixed_this_step = p_frames;
45
46
int beat_length_frames = -1;
47
bool use_loop = looping_override ? looping : mp3_stream->loop;
48
49
bool beat_loop = use_loop && mp3_stream->get_bpm() > 0 && mp3_stream->get_beat_count() > 0;
50
if (beat_loop) {
51
beat_length_frames = mp3_stream->get_beat_count() * mp3_stream->sample_rate * 60 / mp3_stream->get_bpm();
52
}
53
54
while (todo && active) {
55
mp3dec_frame_info_t frame_info;
56
mp3d_sample_t *buf_frame = nullptr;
57
58
int samples_mixed = mp3dec_ex_read_frame(&mp3d, &buf_frame, &frame_info, mp3_stream->channels);
59
60
if (samples_mixed) {
61
p_buffer[p_frames - todo] = AudioFrame(buf_frame[0], buf_frame[samples_mixed - 1]);
62
if (loop_fade_remaining < FADE_SIZE) {
63
p_buffer[p_frames - todo] += loop_fade[loop_fade_remaining] * (float(FADE_SIZE - loop_fade_remaining) / float(FADE_SIZE));
64
loop_fade_remaining++;
65
}
66
--todo;
67
++frames_mixed;
68
69
if (beat_loop && (int)frames_mixed >= beat_length_frames) {
70
for (int i = 0; i < FADE_SIZE; i++) {
71
samples_mixed = mp3dec_ex_read_frame(&mp3d, &buf_frame, &frame_info, mp3_stream->channels);
72
loop_fade[i] = AudioFrame(buf_frame[0], buf_frame[samples_mixed - 1]);
73
if (!samples_mixed) {
74
break;
75
}
76
}
77
loop_fade_remaining = 0;
78
seek(mp3_stream->loop_offset);
79
loops++;
80
}
81
}
82
83
else {
84
//EOF
85
if (use_loop) {
86
seek(mp3_stream->loop_offset);
87
loops++;
88
} else {
89
frames_mixed_this_step = p_frames - todo;
90
//fill remainder with silence
91
for (int i = p_frames - todo; i < p_frames; i++) {
92
p_buffer[i] = AudioFrame(0, 0);
93
}
94
active = false;
95
todo = 0;
96
}
97
}
98
}
99
return frames_mixed_this_step;
100
}
101
102
float AudioStreamPlaybackMP3::get_stream_sampling_rate() {
103
return mp3_stream->sample_rate;
104
}
105
106
void AudioStreamPlaybackMP3::start(double p_from_pos) {
107
active = true;
108
seek(p_from_pos);
109
loops = 0;
110
begin_resample();
111
}
112
113
void AudioStreamPlaybackMP3::stop() {
114
active = false;
115
}
116
117
bool AudioStreamPlaybackMP3::is_playing() const {
118
return active;
119
}
120
121
int AudioStreamPlaybackMP3::get_loop_count() const {
122
return loops;
123
}
124
125
double AudioStreamPlaybackMP3::get_playback_position() const {
126
return double(frames_mixed) / mp3_stream->sample_rate;
127
}
128
129
void AudioStreamPlaybackMP3::seek(double p_time) {
130
if (!active) {
131
return;
132
}
133
134
if (p_time >= mp3_stream->get_length()) {
135
p_time = 0;
136
}
137
138
frames_mixed = uint32_t(mp3_stream->sample_rate * p_time);
139
mp3dec_ex_seek(&mp3d, (uint64_t)frames_mixed * mp3_stream->channels);
140
}
141
142
void AudioStreamPlaybackMP3::tag_used_streams() {
143
mp3_stream->tag_used(get_playback_position());
144
}
145
146
void AudioStreamPlaybackMP3::set_is_sample(bool p_is_sample) {
147
_is_sample = p_is_sample;
148
}
149
150
bool AudioStreamPlaybackMP3::get_is_sample() const {
151
return _is_sample;
152
}
153
154
Ref<AudioSamplePlayback> AudioStreamPlaybackMP3::get_sample_playback() const {
155
return sample_playback;
156
}
157
158
void AudioStreamPlaybackMP3::set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) {
159
sample_playback = p_playback;
160
if (sample_playback.is_valid()) {
161
sample_playback->stream_playback = Ref<AudioStreamPlayback>(this);
162
}
163
}
164
165
void AudioStreamPlaybackMP3::set_parameter(const StringName &p_name, const Variant &p_value) {
166
if (p_name == SNAME("looping")) {
167
if (p_value == Variant()) {
168
looping_override = false;
169
looping = false;
170
} else {
171
looping_override = true;
172
looping = p_value;
173
}
174
}
175
}
176
177
Variant AudioStreamPlaybackMP3::get_parameter(const StringName &p_name) const {
178
if (looping_override && p_name == SNAME("looping")) {
179
return looping;
180
}
181
return Variant();
182
}
183
184
AudioStreamPlaybackMP3::~AudioStreamPlaybackMP3() {
185
mp3dec_ex_close(&mp3d);
186
}
187
188
Ref<AudioStreamPlayback> AudioStreamMP3::instantiate_playback() {
189
Ref<AudioStreamPlaybackMP3> mp3s;
190
191
ERR_FAIL_COND_V_MSG(data.is_empty(), mp3s,
192
"This AudioStreamMP3 does not have an audio file assigned "
193
"to it. AudioStreamMP3 should not be created from the "
194
"inspector or with `.new()`. Instead, load an audio file.");
195
196
mp3s.instantiate();
197
mp3s->mp3_stream = Ref<AudioStreamMP3>(this);
198
199
int errorcode = mp3dec_ex_open_buf(&mp3s->mp3d, data.ptr(), data_len, MP3D_SEEK_TO_SAMPLE);
200
201
mp3s->frames_mixed = 0;
202
mp3s->active = false;
203
mp3s->loops = 0;
204
205
if (errorcode) {
206
ERR_FAIL_COND_V(errorcode, Ref<AudioStreamPlaybackMP3>());
207
}
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
mp3dec_ex_t *mp3d = memnew(mp3dec_ex_t);
224
int err = mp3dec_ex_open_buf(mp3d, p_data.ptr(), src_data_len, MP3D_SEEK_TO_SAMPLE);
225
if (err || mp3d->info.hz == 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->info.channels;
231
sample_rate = mp3d->info.hz;
232
length = float(mp3d->samples) / (sample_rate * float(channels));
233
234
mp3dec_ex_close(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 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
}
365
366