1 | /**************************************************************************/ |
2 | /* audio_stream_ogg_vorbis.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_OGG_VORBIS_H |
32 | #define AUDIO_STREAM_OGG_VORBIS_H |
33 | |
34 | #include "core/variant/variant.h" |
35 | #include "modules/ogg/ogg_packet_sequence.h" |
36 | #include "servers/audio/audio_stream.h" |
37 | |
38 | #include <vorbis/codec.h> |
39 | |
40 | class AudioStreamOggVorbis; |
41 | |
42 | class AudioStreamPlaybackOggVorbis : public AudioStreamPlaybackResampled { |
43 | GDCLASS(AudioStreamPlaybackOggVorbis, AudioStreamPlaybackResampled); |
44 | |
45 | uint32_t frames_mixed = 0; |
46 | bool active = false; |
47 | int loops = 0; |
48 | |
49 | enum { |
50 | FADE_SIZE = 256 |
51 | }; |
52 | AudioFrame loop_fade[FADE_SIZE]; |
53 | int loop_fade_remaining = FADE_SIZE; |
54 | |
55 | vorbis_info info; |
56 | vorbis_comment ; |
57 | vorbis_dsp_state dsp_state; |
58 | vorbis_block block; |
59 | |
60 | bool info_is_allocated = false; |
61 | bool = false; |
62 | bool dsp_state_is_allocated = false; |
63 | bool block_is_allocated = false; |
64 | |
65 | bool ready = false; |
66 | |
67 | bool have_samples_left = false; |
68 | bool have_packets_left = false; |
69 | |
70 | friend class AudioStreamOggVorbis; |
71 | |
72 | Ref<OggPacketSequence> vorbis_data; |
73 | Ref<OggPacketSequencePlayback> vorbis_data_playback; |
74 | Ref<AudioStreamOggVorbis> vorbis_stream; |
75 | |
76 | int _mix_frames(AudioFrame *p_buffer, int p_frames); |
77 | int _mix_frames_vorbis(AudioFrame *p_buffer, int p_frames); |
78 | |
79 | // Allocates vorbis data structures. Returns true upon success, false on failure. |
80 | bool _alloc_vorbis(); |
81 | |
82 | protected: |
83 | virtual int _mix_internal(AudioFrame *p_buffer, int p_frames) override; |
84 | virtual float get_stream_sampling_rate() override; |
85 | |
86 | public: |
87 | virtual void start(double p_from_pos = 0.0) override; |
88 | virtual void stop() override; |
89 | virtual bool is_playing() const override; |
90 | |
91 | virtual int get_loop_count() const override; //times it looped |
92 | |
93 | virtual double get_playback_position() const override; |
94 | virtual void seek(double p_time) override; |
95 | |
96 | virtual void tag_used_streams() override; |
97 | |
98 | AudioStreamPlaybackOggVorbis() {} |
99 | ~AudioStreamPlaybackOggVorbis(); |
100 | }; |
101 | |
102 | class AudioStreamOggVorbis : public AudioStream { |
103 | GDCLASS(AudioStreamOggVorbis, AudioStream); |
104 | OBJ_SAVE_TYPE(AudioStream); // Saves derived classes with common type so they can be interchanged. |
105 | RES_BASE_EXTENSION("oggvorbisstr" ); |
106 | |
107 | friend class AudioStreamPlaybackOggVorbis; |
108 | |
109 | int channels = 1; |
110 | float length = 0.0; |
111 | bool loop = false; |
112 | float loop_offset = 0.0; |
113 | |
114 | // Performs a seek to the beginning of the stream, should not be called during playback! |
115 | // Also causes allocation and deallocation. |
116 | void maybe_update_info(); |
117 | |
118 | Ref<OggPacketSequence> packet_sequence; |
119 | |
120 | double bpm = 0; |
121 | int beat_count = 0; |
122 | int bar_beats = 4; |
123 | |
124 | protected: |
125 | static void _bind_methods(); |
126 | |
127 | public: |
128 | static Ref<AudioStreamOggVorbis> load_from_file(const String &p_path); |
129 | static Ref<AudioStreamOggVorbis> load_from_buffer(const Vector<uint8_t> &file_data); |
130 | void set_loop(bool p_enable); |
131 | virtual bool has_loop() const override; |
132 | |
133 | void set_loop_offset(double p_seconds); |
134 | double get_loop_offset() const; |
135 | |
136 | void set_bpm(double p_bpm); |
137 | virtual double get_bpm() const override; |
138 | |
139 | void set_beat_count(int p_beat_count); |
140 | virtual int get_beat_count() const override; |
141 | |
142 | void set_bar_beats(int p_bar_beats); |
143 | virtual int get_bar_beats() const override; |
144 | |
145 | virtual Ref<AudioStreamPlayback> instantiate_playback() override; |
146 | virtual String get_stream_name() const override; |
147 | |
148 | void set_packet_sequence(Ref<OggPacketSequence> p_packet_sequence); |
149 | Ref<OggPacketSequence> get_packet_sequence() const; |
150 | |
151 | virtual double get_length() const override; //if supported, otherwise return 0 |
152 | |
153 | virtual bool is_monophonic() const override; |
154 | |
155 | AudioStreamOggVorbis(); |
156 | virtual ~AudioStreamOggVorbis(); |
157 | }; |
158 | |
159 | #endif // AUDIO_STREAM_OGG_VORBIS_H |
160 | |