resource_importer_mp3.cpp 5.5 KB

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