Path: blob/master/modules/minimp3/resource_importer_mp3.cpp
10277 views
/**************************************************************************/1/* resource_importer_mp3.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "resource_importer_mp3.h"3132#include "core/io/file_access.h"33#include "core/io/resource_saver.h"3435#ifdef TOOLS_ENABLED36#include "editor/import/audio_stream_import_settings.h"37#endif3839String ResourceImporterMP3::get_importer_name() const {40return "mp3";41}4243String ResourceImporterMP3::get_visible_name() const {44return "MP3";45}4647void ResourceImporterMP3::get_recognized_extensions(List<String> *p_extensions) const {48#ifndef MINIMP3_ONLY_MP349p_extensions->push_back("mp1");50p_extensions->push_back("mp2");51#endif52p_extensions->push_back("mp3");53}5455String ResourceImporterMP3::get_save_extension() const {56return "mp3str";57}5859String ResourceImporterMP3::get_resource_type() const {60return "AudioStreamMP3";61}6263bool ResourceImporterMP3::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {64return true;65}6667int ResourceImporterMP3::get_preset_count() const {68return 0;69}7071String ResourceImporterMP3::get_preset_name(int p_idx) const {72return String();73}7475void ResourceImporterMP3::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {76r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "loop"), false));77r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "loop_offset"), 0));78r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "bpm", PROPERTY_HINT_RANGE, "0,400,0.01,or_greater"), 0));79r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "beat_count", PROPERTY_HINT_RANGE, "0,512,or_greater"), 0));80r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "bar_beats", PROPERTY_HINT_RANGE, "2,32,or_greater"), 4));81}8283#ifdef TOOLS_ENABLED84bool ResourceImporterMP3::has_advanced_options() const {85return true;86}8788void ResourceImporterMP3::show_advanced_options(const String &p_path) {89Ref<AudioStreamMP3> mp3_stream = AudioStreamMP3::load_from_file(p_path);90if (mp3_stream.is_valid()) {91AudioStreamImportSettingsDialog::get_singleton()->edit(p_path, "mp3", mp3_stream);92}93}94#endif9596Error ResourceImporterMP3::import(ResourceUID::ID p_source_id, 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) {97bool loop = p_options["loop"];98float loop_offset = p_options["loop_offset"];99double bpm = p_options["bpm"];100float beat_count = p_options["beat_count"];101float bar_beats = p_options["bar_beats"];102103Ref<AudioStreamMP3> mp3_stream = AudioStreamMP3::load_from_file(p_source_file);104if (mp3_stream.is_null()) {105return ERR_CANT_OPEN;106}107108mp3_stream->set_loop(loop);109mp3_stream->set_loop_offset(loop_offset);110mp3_stream->set_bpm(bpm);111mp3_stream->set_beat_count(beat_count);112mp3_stream->set_bar_beats(bar_beats);113114return ResourceSaver::save(mp3_stream, p_save_path + ".mp3str");115}116117ResourceImporterMP3::ResourceImporterMP3() {118}119120121