resource_importer_mp3.cpp 5.4 KB

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