resource_importer_mp3.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include "resource_importer_mp3.h"
  31. #include "core/io/file_access.h"
  32. #include "core/io/resource_saver.h"
  33. #ifdef TOOLS_ENABLED
  34. #include "editor/import/audio_stream_import_settings.h"
  35. #endif
  36. String ResourceImporterMP3::get_importer_name() const {
  37. return "mp3";
  38. }
  39. String ResourceImporterMP3::get_visible_name() const {
  40. return "MP3";
  41. }
  42. void ResourceImporterMP3::get_recognized_extensions(List<String> *p_extensions) const {
  43. #ifndef MINIMP3_ONLY_MP3
  44. p_extensions->push_back("mp1");
  45. p_extensions->push_back("mp2");
  46. #endif
  47. p_extensions->push_back("mp3");
  48. }
  49. String ResourceImporterMP3::get_save_extension() const {
  50. return "mp3str";
  51. }
  52. String ResourceImporterMP3::get_resource_type() const {
  53. return "AudioStreamMP3";
  54. }
  55. bool ResourceImporterMP3::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  56. return true;
  57. }
  58. int ResourceImporterMP3::get_preset_count() const {
  59. return 0;
  60. }
  61. String ResourceImporterMP3::get_preset_name(int p_idx) const {
  62. return String();
  63. }
  64. void ResourceImporterMP3::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
  65. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "loop"), false));
  66. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "loop_offset"), 0));
  67. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "bpm", PROPERTY_HINT_RANGE, "0,400,0.01,or_greater"), 0));
  68. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "beat_count", PROPERTY_HINT_RANGE, "0,512,or_greater"), 0));
  69. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "bar_beats", PROPERTY_HINT_RANGE, "2,32,or_greater"), 4));
  70. }
  71. #ifdef TOOLS_ENABLED
  72. bool ResourceImporterMP3::has_advanced_options() const {
  73. return true;
  74. }
  75. void ResourceImporterMP3::show_advanced_options(const String &p_path) {
  76. Ref<AudioStreamMP3> mp3_stream = AudioStreamMP3::load_from_file(p_path);
  77. if (mp3_stream.is_valid()) {
  78. AudioStreamImportSettingsDialog::get_singleton()->edit(p_path, "mp3", mp3_stream);
  79. }
  80. }
  81. #endif
  82. Error 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) {
  83. bool loop = p_options["loop"];
  84. float loop_offset = p_options["loop_offset"];
  85. double bpm = p_options["bpm"];
  86. float beat_count = p_options["beat_count"];
  87. float bar_beats = p_options["bar_beats"];
  88. Ref<AudioStreamMP3> mp3_stream = AudioStreamMP3::load_from_file(p_source_file);
  89. if (mp3_stream.is_null()) {
  90. return ERR_CANT_OPEN;
  91. }
  92. mp3_stream->set_loop(loop);
  93. mp3_stream->set_loop_offset(loop_offset);
  94. mp3_stream->set_bpm(bpm);
  95. mp3_stream->set_beat_count(beat_count);
  96. mp3_stream->set_bar_beats(bar_beats);
  97. return ResourceSaver::save(mp3_stream, p_save_path + ".mp3str");
  98. }
  99. ResourceImporterMP3::ResourceImporterMP3() {
  100. }