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