1/**************************************************************************/
2/* resource_importer_mp3.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_mp3.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
41String ResourceImporterMP3::get_importer_name() const {
42 return "mp3";
43}
44
45String ResourceImporterMP3::get_visible_name() const {
46 return "MP3";
47}
48
49void ResourceImporterMP3::get_recognized_extensions(List<String> *p_extensions) const {
50 p_extensions->push_back("mp3");
51}
52
53String ResourceImporterMP3::get_save_extension() const {
54 return "mp3str";
55}
56
57String ResourceImporterMP3::get_resource_type() const {
58 return "AudioStreamMP3";
59}
60
61bool ResourceImporterMP3::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
62 return true;
63}
64
65int ResourceImporterMP3::get_preset_count() const {
66 return 0;
67}
68
69String ResourceImporterMP3::get_preset_name(int p_idx) const {
70 return String();
71}
72
73void ResourceImporterMP3::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
74 r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "loop"), false));
75 r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "loop_offset"), 0));
76 r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "bpm", PROPERTY_HINT_RANGE, "0,400,0.01,or_greater"), 0));
77 r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "beat_count", PROPERTY_HINT_RANGE, "0,512,or_greater"), 0));
78 r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "bar_beats", PROPERTY_HINT_RANGE, "2,32,or_greater"), 4));
79}
80
81#ifdef TOOLS_ENABLED
82bool ResourceImporterMP3::has_advanced_options() const {
83 return true;
84}
85void ResourceImporterMP3::show_advanced_options(const String &p_path) {
86 Ref<AudioStreamMP3> mp3_stream = import_mp3(p_path);
87 if (mp3_stream.is_valid()) {
88 AudioStreamImportSettings::get_singleton()->edit(p_path, "mp3", mp3_stream);
89 }
90}
91#endif
92
93Ref<AudioStreamMP3> ResourceImporterMP3::import_mp3(const String &p_path) {
94 Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
95 ERR_FAIL_COND_V(f.is_null(), Ref<AudioStreamMP3>());
96
97 uint64_t len = f->get_length();
98
99 Vector<uint8_t> data;
100 data.resize(len);
101 uint8_t *w = data.ptrw();
102
103 f->get_buffer(w, len);
104
105 Ref<AudioStreamMP3> mp3_stream;
106 mp3_stream.instantiate();
107
108 mp3_stream->set_data(data);
109 ERR_FAIL_COND_V(!mp3_stream->get_data().size(), Ref<AudioStreamMP3>());
110
111 return mp3_stream;
112}
113
114Error ResourceImporterMP3::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) {
115 bool loop = p_options["loop"];
116 float loop_offset = p_options["loop_offset"];
117 double bpm = p_options["bpm"];
118 float beat_count = p_options["beat_count"];
119 float bar_beats = p_options["bar_beats"];
120
121 Ref<AudioStreamMP3> mp3_stream = import_mp3(p_source_file);
122 if (mp3_stream.is_null()) {
123 return ERR_CANT_OPEN;
124 }
125 mp3_stream->set_loop(loop);
126 mp3_stream->set_loop_offset(loop_offset);
127 mp3_stream->set_bpm(bpm);
128 mp3_stream->set_beat_count(beat_count);
129 mp3_stream->set_bar_beats(bar_beats);
130
131 return ResourceSaver::save(mp3_stream, p_save_path + ".mp3str");
132}
133
134ResourceImporterMP3::ResourceImporterMP3() {
135}
136