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 | #ifndef AUDIO_STREAM_MP3_H |
32 | #define AUDIO_STREAM_MP3_H |
33 | |
34 | #include "core/io/resource_loader.h" |
35 | #include "servers/audio/audio_stream.h" |
36 | |
37 | #include <minimp3_ex.h> |
38 | |
39 | class AudioStreamMP3; |
40 | |
41 | class AudioStreamPlaybackMP3 : public AudioStreamPlaybackResampled { |
42 | GDCLASS(AudioStreamPlaybackMP3, AudioStreamPlaybackResampled); |
43 | |
44 | enum { |
45 | FADE_SIZE = 256 |
46 | }; |
47 | AudioFrame loop_fade[FADE_SIZE]; |
48 | int loop_fade_remaining = FADE_SIZE; |
49 | |
50 | mp3dec_ex_t *mp3d = nullptr; |
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 | protected: |
60 | virtual int _mix_internal(AudioFrame *p_buffer, int p_frames) override; |
61 | virtual float get_stream_sampling_rate() override; |
62 | |
63 | public: |
64 | virtual void start(double p_from_pos = 0.0) override; |
65 | virtual void stop() override; |
66 | virtual bool is_playing() const override; |
67 | |
68 | virtual int get_loop_count() const override; //times it looped |
69 | |
70 | virtual double get_playback_position() const override; |
71 | virtual void seek(double p_time) override; |
72 | |
73 | virtual void tag_used_streams() override; |
74 | |
75 | AudioStreamPlaybackMP3() {} |
76 | ~AudioStreamPlaybackMP3(); |
77 | }; |
78 | |
79 | class AudioStreamMP3 : public AudioStream { |
80 | GDCLASS(AudioStreamMP3, AudioStream); |
81 | OBJ_SAVE_TYPE(AudioStream) //children are all saved as AudioStream, so they can be exchanged |
82 | RES_BASE_EXTENSION("mp3str" ); |
83 | |
84 | friend class AudioStreamPlaybackMP3; |
85 | |
86 | PackedByteArray data; |
87 | uint32_t data_len = 0; |
88 | |
89 | float sample_rate = 1.0; |
90 | int channels = 1; |
91 | float length = 0.0; |
92 | bool loop = false; |
93 | float loop_offset = 0.0; |
94 | void clear_data(); |
95 | |
96 | double bpm = 0; |
97 | int beat_count = 0; |
98 | int bar_beats = 4; |
99 | |
100 | protected: |
101 | static void _bind_methods(); |
102 | |
103 | public: |
104 | void set_loop(bool p_enable); |
105 | virtual bool has_loop() const override; |
106 | |
107 | void set_loop_offset(double p_seconds); |
108 | double get_loop_offset() const; |
109 | |
110 | void set_bpm(double p_bpm); |
111 | virtual double get_bpm() const override; |
112 | |
113 | void set_beat_count(int p_beat_count); |
114 | virtual int get_beat_count() const override; |
115 | |
116 | void set_bar_beats(int p_bar_beats); |
117 | virtual int get_bar_beats() const override; |
118 | |
119 | virtual Ref<AudioStreamPlayback> instantiate_playback() override; |
120 | virtual String get_stream_name() const override; |
121 | |
122 | void set_data(const Vector<uint8_t> &p_data); |
123 | Vector<uint8_t> get_data() const; |
124 | |
125 | virtual double get_length() const override; |
126 | |
127 | virtual bool is_monophonic() const override; |
128 | |
129 | AudioStreamMP3(); |
130 | virtual ~AudioStreamMP3(); |
131 | }; |
132 | |
133 | #endif // AUDIO_STREAM_MP3_H |
134 | |