1/**************************************************************************/
2/* resource_importer_ogg_vorbis.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#include "resource_importer_ogg_vorbis.h"
32
33#include "core/io/file_access.h"
34#include "core/io/resource_saver.h"
35#include "scene/resources/texture.h"
36
37#ifdef TOOLS_ENABLED
38#include "editor/import/audio_stream_import_settings.h"
39#endif
40
41#include <ogg/ogg.h>
42#include <vorbis/codec.h>
43
44String ResourceImporterOggVorbis::get_importer_name() const {
45 return "oggvorbisstr";
46}
47
48String ResourceImporterOggVorbis::get_visible_name() const {
49 return "oggvorbisstr";
50}
51
52void ResourceImporterOggVorbis::get_recognized_extensions(List<String> *p_extensions) const {
53 p_extensions->push_back("ogg");
54}
55
56String ResourceImporterOggVorbis::get_save_extension() const {
57 return "oggvorbisstr";
58}
59
60String ResourceImporterOggVorbis::get_resource_type() const {
61 return "AudioStreamOggVorbis";
62}
63
64bool ResourceImporterOggVorbis::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
65 return true;
66}
67
68int ResourceImporterOggVorbis::get_preset_count() const {
69 return 0;
70}
71
72String ResourceImporterOggVorbis::get_preset_name(int p_idx) const {
73 return String();
74}
75
76void ResourceImporterOggVorbis::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
77 r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "loop"), false));
78 r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "loop_offset"), 0));
79 r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "bpm", PROPERTY_HINT_RANGE, "0,400,0.01,or_greater"), 0));
80 r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "beat_count", PROPERTY_HINT_RANGE, "0,512,or_greater"), 0));
81 r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "bar_beats", PROPERTY_HINT_RANGE, "2,32,or_greater"), 4));
82}
83
84#ifdef TOOLS_ENABLED
85
86bool ResourceImporterOggVorbis::has_advanced_options() const {
87 return true;
88}
89
90void ResourceImporterOggVorbis::show_advanced_options(const String &p_path) {
91 Ref<AudioStreamOggVorbis> ogg_stream = load_from_file(p_path);
92 if (ogg_stream.is_valid()) {
93 AudioStreamImportSettings::get_singleton()->edit(p_path, "oggvorbisstr", ogg_stream);
94 }
95}
96#endif
97
98Error ResourceImporterOggVorbis::import(const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
99 bool loop = p_options["loop"];
100 float loop_offset = p_options["loop_offset"];
101 double bpm = p_options["bpm"];
102 int beat_count = p_options["beat_count"];
103 int bar_beats = p_options["bar_beats"];
104
105 Ref<AudioStreamOggVorbis> ogg_vorbis_stream = load_from_file(p_source_file);
106 if (ogg_vorbis_stream.is_null()) {
107 return ERR_CANT_OPEN;
108 }
109
110 ogg_vorbis_stream->set_loop(loop);
111 ogg_vorbis_stream->set_loop_offset(loop_offset);
112 ogg_vorbis_stream->set_bpm(bpm);
113 ogg_vorbis_stream->set_beat_count(beat_count);
114 ogg_vorbis_stream->set_bar_beats(bar_beats);
115
116 return ResourceSaver::save(ogg_vorbis_stream, p_save_path + ".oggvorbisstr");
117}
118
119ResourceImporterOggVorbis::ResourceImporterOggVorbis() {
120}
121
122void ResourceImporterOggVorbis::_bind_methods() {
123 ClassDB::bind_static_method("ResourceImporterOggVorbis", D_METHOD("load_from_buffer", "buffer"), &ResourceImporterOggVorbis::load_from_buffer);
124 ClassDB::bind_static_method("ResourceImporterOggVorbis", D_METHOD("load_from_file", "path"), &ResourceImporterOggVorbis::load_from_file);
125}
126
127Ref<AudioStreamOggVorbis> ResourceImporterOggVorbis::load_from_buffer(const Vector<uint8_t> &file_data) {
128 Ref<AudioStreamOggVorbis> ogg_vorbis_stream;
129 ogg_vorbis_stream.instantiate();
130
131 Ref<OggPacketSequence> ogg_packet_sequence;
132 ogg_packet_sequence.instantiate();
133
134 ogg_stream_state stream_state;
135 ogg_sync_state sync_state;
136 ogg_page page;
137 ogg_packet packet;
138 bool initialized_stream = false;
139
140 ogg_sync_init(&sync_state);
141 int err;
142 size_t cursor = 0;
143 size_t packet_count = 0;
144 bool done = false;
145 while (!done) {
146 err = ogg_sync_check(&sync_state);
147 ERR_FAIL_COND_V_MSG(err != 0, Ref<AudioStreamOggVorbis>(), "Ogg sync error " + itos(err));
148 while (ogg_sync_pageout(&sync_state, &page) != 1) {
149 if (cursor >= size_t(file_data.size())) {
150 done = true;
151 break;
152 }
153 err = ogg_sync_check(&sync_state);
154 ERR_FAIL_COND_V_MSG(err != 0, Ref<AudioStreamOggVorbis>(), "Ogg sync error " + itos(err));
155 char *sync_buf = ogg_sync_buffer(&sync_state, OGG_SYNC_BUFFER_SIZE);
156 err = ogg_sync_check(&sync_state);
157 ERR_FAIL_COND_V_MSG(err != 0, Ref<AudioStreamOggVorbis>(), "Ogg sync error " + itos(err));
158 ERR_FAIL_COND_V(cursor > size_t(file_data.size()), Ref<AudioStreamOggVorbis>());
159 size_t copy_size = file_data.size() - cursor;
160 if (copy_size > OGG_SYNC_BUFFER_SIZE) {
161 copy_size = OGG_SYNC_BUFFER_SIZE;
162 }
163 memcpy(sync_buf, &file_data[cursor], copy_size);
164 ogg_sync_wrote(&sync_state, copy_size);
165 cursor += copy_size;
166 err = ogg_sync_check(&sync_state);
167 ERR_FAIL_COND_V_MSG(err != 0, Ref<AudioStreamOggVorbis>(), "Ogg sync error " + itos(err));
168 }
169 if (done) {
170 break;
171 }
172 err = ogg_sync_check(&sync_state);
173 ERR_FAIL_COND_V_MSG(err != 0, Ref<AudioStreamOggVorbis>(), "Ogg sync error " + itos(err));
174
175 // Have a page now.
176 if (!initialized_stream) {
177 if (ogg_stream_init(&stream_state, ogg_page_serialno(&page))) {
178 ERR_FAIL_V_MSG(Ref<AudioStreamOggVorbis>(), "Failed allocating memory for Ogg Vorbis stream.");
179 }
180 initialized_stream = true;
181 }
182 ogg_stream_pagein(&stream_state, &page);
183 err = ogg_stream_check(&stream_state);
184 ERR_FAIL_COND_V_MSG(err != 0, Ref<AudioStreamOggVorbis>(), "Ogg stream error " + itos(err));
185 int desync_iters = 0;
186
187 Vector<Vector<uint8_t>> packet_data;
188 int64_t granule_pos = 0;
189
190 while (true) {
191 err = ogg_stream_packetout(&stream_state, &packet);
192 if (err == -1) {
193 // According to the docs this is usually recoverable, but don't sit here spinning forever.
194 desync_iters++;
195 ERR_FAIL_COND_V_MSG(desync_iters > 100, Ref<AudioStreamOggVorbis>(), "Packet sync issue during Ogg import");
196 continue;
197 } else if (err == 0) {
198 // Not enough data to fully reconstruct a packet. Go on to the next page.
199 break;
200 }
201 if (packet_count == 0 && vorbis_synthesis_idheader(&packet) == 0) {
202 print_verbose("Found a non-vorbis-header packet in a header position");
203 // Clearly this logical stream is not a vorbis stream, so destroy it and try again with the next page.
204 if (initialized_stream) {
205 ogg_stream_clear(&stream_state);
206 initialized_stream = false;
207 }
208 break;
209 }
210 granule_pos = packet.granulepos;
211
212 PackedByteArray data;
213 data.resize(packet.bytes);
214 memcpy(data.ptrw(), packet.packet, packet.bytes);
215 packet_data.push_back(data);
216 packet_count++;
217 }
218 if (initialized_stream) {
219 ogg_packet_sequence->push_page(granule_pos, packet_data);
220 }
221 }
222 if (initialized_stream) {
223 ogg_stream_clear(&stream_state);
224 }
225 ogg_sync_clear(&sync_state);
226
227 if (ogg_packet_sequence->get_packet_granule_positions().is_empty()) {
228 ERR_FAIL_V_MSG(Ref<AudioStreamOggVorbis>(), "Ogg Vorbis decoding failed. Check that your data is a valid Ogg Vorbis audio stream.");
229 }
230
231 ogg_vorbis_stream->set_packet_sequence(ogg_packet_sequence);
232
233 return ogg_vorbis_stream;
234}
235
236Ref<AudioStreamOggVorbis> ResourceImporterOggVorbis::load_from_file(const String &p_path) {
237 Vector<uint8_t> file_data = FileAccess::get_file_as_bytes(p_path);
238 ERR_FAIL_COND_V_MSG(file_data.is_empty(), Ref<AudioStreamOggVorbis>(), "Cannot open file '" + p_path + "'.");
239 return load_from_buffer(file_data);
240}
241