Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/minimp3/audio_stream_mp3.h
10277 views
1
/**************************************************************************/
2
/* audio_stream_mp3.h */
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
#pragma once
32
33
#include "servers/audio/audio_stream.h"
34
35
#include <minimp3_ex.h>
36
37
class AudioStreamMP3;
38
39
class AudioStreamPlaybackMP3 : public AudioStreamPlaybackResampled {
40
GDCLASS(AudioStreamPlaybackMP3, AudioStreamPlaybackResampled);
41
42
enum {
43
FADE_SIZE = 256
44
};
45
AudioFrame loop_fade[FADE_SIZE];
46
int loop_fade_remaining = FADE_SIZE;
47
48
bool looping_override = false;
49
bool looping = false;
50
mp3dec_ex_t mp3d = {};
51
uint32_t frames_mixed = 0;
52
bool active = false;
53
int loops = 0;
54
55
friend class AudioStreamMP3;
56
57
Ref<AudioStreamMP3> mp3_stream;
58
59
bool _is_sample = false;
60
Ref<AudioSamplePlayback> sample_playback;
61
62
protected:
63
virtual int _mix_internal(AudioFrame *p_buffer, int p_frames) override;
64
virtual float get_stream_sampling_rate() override;
65
66
public:
67
virtual void start(double p_from_pos = 0.0) override;
68
virtual void stop() override;
69
virtual bool is_playing() const override;
70
71
virtual int get_loop_count() const override; //times it looped
72
73
virtual double get_playback_position() const override;
74
virtual void seek(double p_time) override;
75
76
virtual void tag_used_streams() override;
77
78
virtual void set_is_sample(bool p_is_sample) override;
79
virtual bool get_is_sample() const override;
80
virtual Ref<AudioSamplePlayback> get_sample_playback() const override;
81
virtual void set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) override;
82
83
virtual void set_parameter(const StringName &p_name, const Variant &p_value) override;
84
virtual Variant get_parameter(const StringName &p_name) const override;
85
86
AudioStreamPlaybackMP3() {}
87
~AudioStreamPlaybackMP3();
88
};
89
90
class AudioStreamMP3 : public AudioStream {
91
GDCLASS(AudioStreamMP3, AudioStream);
92
OBJ_SAVE_TYPE(AudioStream) //children are all saved as AudioStream, so they can be exchanged
93
RES_BASE_EXTENSION("mp3str");
94
95
friend class AudioStreamPlaybackMP3;
96
97
TightLocalVector<uint8_t> data;
98
uint32_t data_len = 0;
99
100
float sample_rate = 1.0;
101
int channels = 1;
102
float length = 0.0;
103
bool loop = false;
104
float loop_offset = 0.0;
105
void clear_data();
106
107
double bpm = 0;
108
int beat_count = 0;
109
int bar_beats = 4;
110
111
protected:
112
static void _bind_methods();
113
114
public:
115
static Ref<AudioStreamMP3> load_from_buffer(const Vector<uint8_t> &p_stream_data);
116
static Ref<AudioStreamMP3> load_from_file(const String &p_path);
117
118
void set_loop(bool p_enable);
119
virtual bool has_loop() const override;
120
121
void set_loop_offset(double p_seconds);
122
double get_loop_offset() const;
123
124
void set_bpm(double p_bpm);
125
virtual double get_bpm() const override;
126
127
void set_beat_count(int p_beat_count);
128
virtual int get_beat_count() const override;
129
130
void set_bar_beats(int p_bar_beats);
131
virtual int get_bar_beats() const override;
132
133
virtual Ref<AudioStreamPlayback> instantiate_playback() override;
134
virtual String get_stream_name() const override;
135
136
void set_data(const Vector<uint8_t> &p_data);
137
Vector<uint8_t> get_data() const;
138
139
virtual double get_length() const override;
140
141
virtual bool is_monophonic() const override;
142
143
virtual bool can_be_sampled() const override {
144
return true;
145
}
146
virtual Ref<AudioSample> generate_sample() const override;
147
148
virtual void get_parameter_list(List<Parameter> *r_parameters) override;
149
150
AudioStreamMP3();
151
virtual ~AudioStreamMP3();
152
};
153
154