diff options
author | reduz <reduzio@gmail.com> | 2022-07-21 01:00:58 +0200 |
---|---|---|
committer | Juan Linietsky <reduzio@gmail.com> | 2022-07-23 07:31:17 +0200 |
commit | d1ddee225830b28171de031bd1f1918ced21b38f (patch) | |
tree | 59fbad7454e47cfc0f746842108d3d6e10b83a40 /editor/import | |
parent | 976cb7ea9f59813f99e06c4c345c19ff68c2c591 (diff) |
Implement BPM support
Based on #62896, only implements the BPM support part.
* Implements BPM support in the AudioStreamOGG/MP3 importers.
* Can select BPM/Bar Size and total beats in a song file, as well as edit looping points.
* Looping is now BPM aware
* Added a special importer UI for configuring this.
* Added a special preview showing the audio waveform as well as the playback position in the resource picker.
* Renamed `AudioStream::instance` to `instantiate` for correctness.
Diffstat (limited to 'editor/import')
-rw-r--r-- | editor/import/audio_stream_import_settings.cpp | 650 | ||||
-rw-r--r-- | editor/import/audio_stream_import_settings.h | 114 |
2 files changed, 764 insertions, 0 deletions
diff --git a/editor/import/audio_stream_import_settings.cpp b/editor/import/audio_stream_import_settings.cpp new file mode 100644 index 0000000000..f3709efab6 --- /dev/null +++ b/editor/import/audio_stream_import_settings.cpp @@ -0,0 +1,650 @@ +/*************************************************************************/ +/* audio_stream_import_settings.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "audio_stream_import_settings.h" +#include "editor/audio_stream_preview.h" +#include "editor/editor_file_system.h" +#include "editor/editor_scale.h" + +AudioStreamImportSettings *AudioStreamImportSettings::singleton = nullptr; + +void AudioStreamImportSettings::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_READY: { + AudioStreamPreviewGenerator::get_singleton()->connect("preview_updated", callable_mp(this, &AudioStreamImportSettings::_preview_changed)); + connect("confirmed", callable_mp(this, &AudioStreamImportSettings::_reimport)); + } break; + + case NOTIFICATION_THEME_CHANGED: + case NOTIFICATION_ENTER_TREE: { + _play_button->set_icon(get_theme_icon(SNAME("MainPlay"), SNAME("EditorIcons"))); + _stop_button->set_icon(get_theme_icon(SNAME("Stop"), SNAME("EditorIcons"))); + _preview->set_color(get_theme_color(SNAME("dark_color_2"), SNAME("Editor"))); + color_rect->set_color(get_theme_color(SNAME("dark_color_1"), SNAME("Editor"))); + _current_label->add_theme_font_override("font", get_theme_font(SNAME("status_source"), SNAME("EditorFonts"))); + _current_label->add_theme_font_size_override("font_size", get_theme_font_size(SNAME("status_source_size"), SNAME("EditorFonts"))); + _duration_label->add_theme_font_override("font", get_theme_font(SNAME("status_source"), SNAME("EditorFonts"))); + _duration_label->add_theme_font_size_override("font_size", get_theme_font_size(SNAME("status_source_size"), SNAME("EditorFonts"))); + + zoom_in->set_icon(get_theme_icon(SNAME("ZoomMore"), SNAME("EditorIcons"))); + zoom_out->set_icon(get_theme_icon(SNAME("ZoomLess"), SNAME("EditorIcons"))); + zoom_reset->set_icon(get_theme_icon(SNAME("ZoomReset"), SNAME("EditorIcons"))); + + _indicator->update(); + _preview->update(); + } break; + + case NOTIFICATION_PROCESS: { + _current = _player->get_playback_position(); + _indicator->update(); + } break; + + case NOTIFICATION_VISIBILITY_CHANGED: { + if (!is_visible()) { + _stop(); + } + } break; + } +} + +void AudioStreamImportSettings::_draw_preview() { + Rect2 rect = _preview->get_rect(); + Size2 size = rect.size; + + Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream); + float preview_offset = zoom_bar->get_value(); + float preview_len = zoom_bar->get_page(); + + Ref<Font> beat_font = get_theme_font(SNAME("main"), SNAME("EditorFonts")); + int main_size = get_theme_font_size(SNAME("main_size"), SNAME("EditorFonts")); + Vector<Vector2> lines; + lines.resize(size.width * 2); + Color color_active = get_theme_color(SNAME("contrast_color_2"), SNAME("Editor")); + Color color_inactive = color_active; + color_inactive.a *= 0.5; + Vector<Color> color; + color.resize(lines.size()); + + float inactive_from = 1e20; + float beat_size = 0; + int last_beat = 0; + if (stream->get_bpm() > 0) { + beat_size = 60 / float(stream->get_bpm()); + int y_ofs = beat_font->get_height(main_size) + 4 * EDSCALE; + rect.position.y += y_ofs; + rect.size.y -= y_ofs; + + if (stream->get_beat_count() > 0) { + last_beat = stream->get_beat_count(); + inactive_from = last_beat * beat_size; + } + } + + for (int i = 0; i < size.width; i++) { + float ofs = preview_offset + i * preview_len / size.width; + float ofs_n = preview_offset + (i + 1) * preview_len / size.width; + float max = preview->get_max(ofs, ofs_n) * 0.5 + 0.5; + float min = preview->get_min(ofs, ofs_n) * 0.5 + 0.5; + + int idx = i; + lines.write[idx * 2 + 0] = Vector2(i + 1, rect.position.y + min * rect.size.y); + lines.write[idx * 2 + 1] = Vector2(i + 1, rect.position.y + max * rect.size.y); + + Color c = ofs > inactive_from ? color_inactive : color_active; + + color.write[idx * 2 + 0] = c; + color.write[idx * 2 + 1] = c; + } + + RS::get_singleton()->canvas_item_add_multiline(_preview->get_canvas_item(), lines, color); + + if (beat_size) { + Color beat_color = Color(1, 1, 1, 1); + Color final_beat_color = beat_color; + Color bar_color = beat_color; + beat_color.a *= 0.4; + bar_color.a *= 0.6; + + int prev_beat = 0; // Do not draw beat zero + Color color_bg = color_active; + color_bg.a *= 0.2; + _preview->draw_rect(Rect2(0, 0, rect.size.width, rect.position.y), color_bg); + int bar_beats = stream->get_bar_beats(); + + int last_text_end_x = 0; + for (int i = 0; i < size.width; i++) { + float ofs = preview_offset + i * preview_len / size.width; + int beat = int(ofs / beat_size); + if (beat != prev_beat) { + String text = itos(beat); + int text_w = beat_font->get_string_size(text).width; + if (i - text_w / 2 > last_text_end_x + 2 * EDSCALE) { + int x_ofs = i - text_w / 2; + _preview->draw_string(beat_font, Point2(x_ofs, 2 * EDSCALE + beat_font->get_ascent(main_size)), text, HORIZONTAL_ALIGNMENT_LEFT, rect.size.width - x_ofs, Font::DEFAULT_FONT_SIZE, color_active); + last_text_end_x = i + text_w / 2; + } + + if (beat == last_beat) { + _preview->draw_rect(Rect2i(i, rect.position.y, 2, rect.size.height), final_beat_color); + // Darken subsequent beats + beat_color.a *= 0.3; + color_active.a *= 0.3; + } else { + _preview->draw_rect(Rect2i(i, rect.position.y, 1, rect.size.height), (beat % bar_beats) == 0 ? bar_color : beat_color); + } + prev_beat = beat; + } + } + } +} + +void AudioStreamImportSettings::_preview_changed(ObjectID p_which) { + if (stream.is_valid() && stream->get_instance_id() == p_which) { + _preview->update(); + } +} + +void AudioStreamImportSettings::_preview_zoom_in() { + if (!stream.is_valid()) { + return; + } + float page_size = zoom_bar->get_page(); + zoom_bar->set_page(page_size * 0.5); + zoom_bar->set_value(zoom_bar->get_value() + page_size * 0.25); + + _preview->update(); + _indicator->update(); +} + +void AudioStreamImportSettings::_preview_zoom_out() { + if (!stream.is_valid()) { + return; + } + float page_size = zoom_bar->get_page(); + zoom_bar->set_page(MIN(zoom_bar->get_max(), page_size * 2.0)); + zoom_bar->set_value(zoom_bar->get_value() - page_size * 0.5); + + _preview->update(); + _indicator->update(); +} + +void AudioStreamImportSettings::_preview_zoom_reset() { + if (!stream.is_valid()) { + return; + } + zoom_bar->set_max(stream->get_length()); + zoom_bar->set_page(zoom_bar->get_max()); + zoom_bar->set_value(0); + _preview->update(); + _indicator->update(); +} + +void AudioStreamImportSettings::_preview_zoom_offset_changed(double) { + _preview->update(); + _indicator->update(); +} + +void AudioStreamImportSettings::_audio_changed() { + if (!is_visible()) { + return; + } + _preview->update(); + _indicator->update(); + color_rect->update(); +} + +void AudioStreamImportSettings::_play() { + if (_player->is_playing()) { + // '_pausing' variable indicates that we want to pause the audio player, not stop it. See '_on_finished()'. + _pausing = true; + _player->stop(); + _play_button->set_icon(get_theme_icon(SNAME("MainPlay"), SNAME("EditorIcons"))); + set_process(false); + } else { + _player->play(_current); + _play_button->set_icon(get_theme_icon(SNAME("Pause"), SNAME("EditorIcons"))); + set_process(true); + } +} + +void AudioStreamImportSettings::_stop() { + _player->stop(); + _play_button->set_icon(get_theme_icon(SNAME("MainPlay"), SNAME("EditorIcons"))); + _current = 0; + _indicator->update(); + set_process(false); +} + +void AudioStreamImportSettings::_on_finished() { + _play_button->set_icon(get_theme_icon(SNAME("MainPlay"), SNAME("EditorIcons"))); + if (!_pausing) { + _current = 0; + _indicator->update(); + } else { + _pausing = false; + } + set_process(false); +} + +void AudioStreamImportSettings::_draw_indicator() { + if (!stream.is_valid()) { + return; + } + + Rect2 rect = _preview->get_rect(); + + Ref<Font> beat_font = get_theme_font(SNAME("main"), SNAME("EditorFonts")); + int main_size = get_theme_font_size(SNAME("main_size"), SNAME("EditorFonts")); + + if (stream->get_bpm() > 0) { + int y_ofs = beat_font->get_height(main_size) + 4 * EDSCALE; + rect.position.y += y_ofs; + rect.size.height -= y_ofs; + } + + float ofs_x = (_current - zoom_bar->get_value()) * rect.size.width / zoom_bar->get_page(); + if (ofs_x < 0 || ofs_x >= rect.size.width) { + return; + } + + const Color color = get_theme_color(SNAME("accent_color"), SNAME("Editor")); + _indicator->draw_line(Point2(ofs_x, rect.position.y), Point2(ofs_x, rect.position.y + rect.size.height), color, Math::round(2 * EDSCALE)); + _indicator->draw_texture( + get_theme_icon(SNAME("TimelineIndicator"), SNAME("EditorIcons")), + Point2(ofs_x - get_theme_icon(SNAME("TimelineIndicator"), SNAME("EditorIcons"))->get_width() * 0.5, rect.position.y), + color); + + if (stream->get_bpm() > 0 && _hovering_beat != -1) { + // Draw hovered beat. + float preview_offset = zoom_bar->get_value(); + float preview_len = zoom_bar->get_page(); + float beat_size = 60 / float(stream->get_bpm()); + int prev_beat = 0; + int last_text_end_x = 0; + for (int i = 0; i < rect.size.width; i++) { + float ofs = preview_offset + i * preview_len / rect.size.width; + int beat = int(ofs / beat_size); + if (beat != prev_beat) { + String text = itos(beat); + int text_w = beat_font->get_string_size(text).width; + if (i - text_w / 2 > last_text_end_x + 2 * EDSCALE && beat == _hovering_beat) { + int x_ofs = i - text_w / 2; + _indicator->draw_string(beat_font, Point2(x_ofs, 2 * EDSCALE + beat_font->get_ascent(main_size)), text, HORIZONTAL_ALIGNMENT_LEFT, rect.size.width - x_ofs, Font::DEFAULT_FONT_SIZE, color); + last_text_end_x = i + text_w / 2; + break; + } + prev_beat = beat; + } + } + } + + _current_label->set_text(String::num(_current, 2).pad_decimals(2) + " /"); +} + +void AudioStreamImportSettings::_on_indicator_mouse_exited() { + _hovering_beat = -1; + _indicator->update(); +} + +void AudioStreamImportSettings::_on_input_indicator(Ref<InputEvent> p_event) { + const Ref<InputEventMouseButton> mb = p_event; + if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) { + if (stream->get_bpm() > 0) { + int main_size = get_theme_font_size(SNAME("main_size"), SNAME("EditorFonts")); + Ref<Font> beat_font = get_theme_font(SNAME("main"), SNAME("EditorFonts")); + int y_ofs = beat_font->get_height(main_size) + 4 * EDSCALE; + if ((!_dragging && mb->get_position().y < y_ofs) || _beat_len_dragging) { + if (mb->is_pressed()) { + _set_beat_len_to(mb->get_position().x); + _beat_len_dragging = true; + } else { + _beat_len_dragging = false; + } + return; + } + } + + if (mb->is_pressed()) { + _seek_to(mb->get_position().x); + } + _dragging = mb->is_pressed(); + } + + const Ref<InputEventMouseMotion> mm = p_event; + if (mm.is_valid()) { + if (_dragging) { + _seek_to(mm->get_position().x); + } + if (_beat_len_dragging) { + _set_beat_len_to(mm->get_position().x); + } + if (stream->get_bpm() > 0) { + int main_size = get_theme_font_size(SNAME("main_size"), SNAME("EditorFonts")); + Ref<Font> beat_font = get_theme_font(SNAME("main"), SNAME("EditorFonts")); + int y_ofs = beat_font->get_height(main_size) + 4 * EDSCALE; + if (mm->get_position().y < y_ofs) { + int new_hovering_beat = _get_beat_at_pos(mm->get_position().x); + if (new_hovering_beat != _hovering_beat) { + _hovering_beat = new_hovering_beat; + _indicator->update(); + } + } else if (_hovering_beat != -1) { + _hovering_beat = -1; + _indicator->update(); + } + } + } +} + +int AudioStreamImportSettings::_get_beat_at_pos(real_t p_x) { + float ofs_sec = zoom_bar->get_value() + p_x * zoom_bar->get_page() / _preview->get_size().width; + ofs_sec = CLAMP(ofs_sec, 0, stream->get_length()); + float beat_size = 60 / float(stream->get_bpm()); + int beat = int(ofs_sec / beat_size + 0.5); + + if (beat * beat_size > stream->get_length() + 0.001) { // Stream may end few audio frames before but may still want to use full loop. + beat--; + } + return beat; +} + +void AudioStreamImportSettings::_set_beat_len_to(real_t p_x) { + int beat = _get_beat_at_pos(p_x); + if (beat < 1) { + beat = 1; // Because 0 is disable. + } + updating_settings = true; + beats_enabled->set_pressed(true); + beats_edit->set_value(beat); + updating_settings = false; + _settings_changed(); +} + +void AudioStreamImportSettings::_seek_to(real_t p_x) { + _current = zoom_bar->get_value() + p_x / _preview->get_rect().size.x * zoom_bar->get_page(); + _current = CLAMP(_current, 0, stream->get_length()); + _player->seek(_current); + _indicator->update(); +} + +void AudioStreamImportSettings::edit(const String &p_path, const String &p_importer, const Ref<AudioStream> &p_stream) { + if (!stream.is_null()) { + stream->disconnect("changed", callable_mp(this, &AudioStreamImportSettings::_audio_changed)); + } + + importer = p_importer; + path = p_path; + + stream = p_stream; + _player->set_stream(stream); + _current = 0; + String text = String::num(stream->get_length(), 2).pad_decimals(2) + "s"; + _duration_label->set_text(text); + + if (!stream.is_null()) { + stream->connect("changed", callable_mp(this, &AudioStreamImportSettings::_audio_changed)); + _preview->update(); + _indicator->update(); + color_rect->update(); + } else { + hide(); + } + params.clear(); + + if (stream.is_valid()) { + Ref<ConfigFile> config_file; + config_file.instantiate(); + Error err = config_file->load(p_path + ".import"); + updating_settings = true; + if (err == OK) { + double bpm = config_file->get_value("params", "bpm", 0); + int beats = config_file->get_value("params", "beat_count", 0); + bpm_edit->set_value(bpm > 0 ? bpm : 120); + bpm_enabled->set_pressed(bpm > 0); + beats_edit->set_value(beats); + beats_enabled->set_pressed(beats > 0); + loop->set_pressed(config_file->get_value("params", "loop", false)); + loop_offset->set_value(config_file->get_value("params", "loop_offset", 0)); + bar_beats_edit->set_value(config_file->get_value("params", "bar_beats", 4)); + + List<String> keys; + config_file->get_section_keys("params", &keys); + for (const String &K : keys) { + params[K] = config_file->get_value("params", K); + } + } else { + bpm_edit->set_value(false); + bpm_enabled->set_pressed(false); + beats_edit->set_value(0); + beats_enabled->set_pressed(false); + bar_beats_edit->set_value(4); + loop->set_pressed(false); + loop_offset->set_value(0); + } + + _preview_zoom_reset(); + updating_settings = false; + _settings_changed(); + + set_title(vformat(TTR("Audio Stream Importer: %s"), p_path.get_file())); + popup_centered(); + } +} + +void AudioStreamImportSettings::_settings_changed() { + if (updating_settings) { + return; + } + + updating_settings = true; + stream->call("set_loop", loop->is_pressed()); + stream->call("set_loop_offset", loop_offset->get_value()); + if (bpm_enabled->is_pressed()) { + stream->call("set_bpm", bpm_edit->get_value()); + beats_enabled->show(); + beats_edit->show(); + bar_beats_label->show(); + bar_beats_edit->show(); + double bpm = bpm_edit->get_value(); + if (bpm > 0) { + float beat_size = 60 / float(bpm); + int beat_max = int((stream->get_length() + 0.001) / beat_size); + int current_beat = beats_edit->get_value(); + beats_edit->set_max(beat_max); + if (current_beat > beat_max) { + beats_edit->set_value(beat_max); + stream->call("set_beat_count", beat_max); + } + } + stream->call("set_bar_beats", bar_beats_edit->get_value()); + } else { + stream->call("set_bpm", 0); + stream->call("set_bar_beats", 4); + beats_enabled->hide(); + beats_edit->hide(); + bar_beats_label->hide(); + bar_beats_edit->hide(); + } + if (bpm_enabled->is_pressed() && beats_enabled->is_pressed()) { + stream->call("set_beat_count", beats_edit->get_value()); + } else { + stream->call("set_beat_count", 0); + } + + updating_settings = false; + + _preview->update(); + _indicator->update(); + color_rect->update(); +} + +void AudioStreamImportSettings::_reimport() { + params["loop"] = loop->is_pressed(); + params["loop_offset"] = loop_offset->get_value(); + params["bpm"] = bpm_enabled->is_pressed() ? double(bpm_edit->get_value()) : double(0); + params["beat_count"] = (bpm_enabled->is_pressed() && beats_enabled->is_pressed()) ? int(beats_edit->get_value()) : int(0); + params["bar_beats"] = (bpm_enabled->is_pressed()) ? int(bar_beats_edit->get_value()) : int(4); + + EditorFileSystem::get_singleton()->reimport_file_with_custom_parameters(path, importer, params); +} + +AudioStreamImportSettings::AudioStreamImportSettings() { + get_ok_button()->set_text(TTR("Reimport")); + get_cancel_button()->set_text(TTR("Close")); + + VBoxContainer *main_vbox = memnew(VBoxContainer); + add_child(main_vbox); + + HBoxContainer *loop_hb = memnew(HBoxContainer); + loop_hb->add_theme_constant_override("separation", 4 * EDSCALE); + loop = memnew(CheckBox); + loop->set_text(TTR("Enable")); + loop->set_tooltip(TTR("Enable looping.")); + loop->connect("toggled", callable_mp(this, &AudioStreamImportSettings::_settings_changed).unbind(1)); + loop_hb->add_child(loop); + loop_hb->add_spacer(); + loop_hb->add_child(memnew(Label(TTR("Offset:")))); + loop_offset = memnew(SpinBox); + loop_offset->set_max(10000); + loop_offset->set_step(0.001); + loop_offset->set_suffix("sec"); + loop_offset->set_tooltip(TTR("Loop offset (from beginning). Note that if BPM is set, this setting will be ignored.")); + loop_offset->connect("value_changed", callable_mp(this, &AudioStreamImportSettings::_settings_changed).unbind(1)); + loop_hb->add_child(loop_offset); + main_vbox->add_margin_child(TTR("Loop:"), loop_hb); + + HBoxContainer *interactive_hb = memnew(HBoxContainer); + interactive_hb->add_theme_constant_override("separation", 4 * EDSCALE); + bpm_enabled = memnew(CheckBox); + bpm_enabled->set_text((TTR("BPM:"))); + bpm_enabled->connect("toggled", callable_mp(this, &AudioStreamImportSettings::_settings_changed).unbind(1)); + interactive_hb->add_child(bpm_enabled); + bpm_edit = memnew(SpinBox); + bpm_edit->set_max(400); + bpm_edit->set_step(0.01); + bpm_edit->set_tooltip(TTR("Configure the Beats Per Measure (tempo) used for the interactive streams.\nThis is required in order to configure beat information.")); + bpm_edit->connect("value_changed", callable_mp(this, &AudioStreamImportSettings::_settings_changed).unbind(1)); + interactive_hb->add_child(bpm_edit); + interactive_hb->add_spacer(); + bar_beats_label = memnew(Label(TTR("Beats/Bar:"))); + interactive_hb->add_child(bar_beats_label); + bar_beats_edit = memnew(SpinBox); + bar_beats_edit->set_tooltip(TTR("Configure the Beats Per Bar. This used for music-aware transitions between AudioStreams.")); + bar_beats_edit->set_min(2); + bar_beats_edit->set_max(32); + bar_beats_edit->connect("value_changed", callable_mp(this, &AudioStreamImportSettings::_settings_changed).unbind(1)); + interactive_hb->add_child(bar_beats_edit); + interactive_hb->add_spacer(); + beats_enabled = memnew(CheckBox); + beats_enabled->set_text(TTR("Length (in beats):")); + beats_enabled->connect("toggled", callable_mp(this, &AudioStreamImportSettings::_settings_changed).unbind(1)); + interactive_hb->add_child(beats_enabled); + beats_edit = memnew(SpinBox); + beats_edit->set_tooltip(TTR("Configure the amount of Beats used for music-aware looping. If zero, it will be autodetected from the length.\nIt is recommended to set this value (either manually or by clicking on a beat number in the preview) to ensure looping works properly.")); + beats_edit->set_max(99999); + beats_edit->connect("value_changed", callable_mp(this, &AudioStreamImportSettings::_settings_changed).unbind(1)); + interactive_hb->add_child(beats_edit); + main_vbox->add_margin_child(TTR("Music Playback:"), interactive_hb); + + color_rect = memnew(ColorRect); + main_vbox->add_margin_child(TTR("Preview:"), color_rect); + + color_rect->set_custom_minimum_size(Size2(600, 200) * EDSCALE); + color_rect->set_v_size_flags(Control::SIZE_EXPAND_FILL); + + _player = memnew(AudioStreamPlayer); + _player->connect("finished", callable_mp(this, &AudioStreamImportSettings::_on_finished)); + color_rect->add_child(_player); + + VBoxContainer *vbox = memnew(VBoxContainer); + vbox->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0); + color_rect->add_child(vbox); + vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL); + + _preview = memnew(ColorRect); + _preview->set_v_size_flags(Control::SIZE_EXPAND_FILL); + _preview->connect("draw", callable_mp(this, &AudioStreamImportSettings::_draw_preview)); + _preview->set_v_size_flags(Control::SIZE_EXPAND_FILL); + vbox->add_child(_preview); + + HBoxContainer *zoom_hbox = memnew(HBoxContainer); + zoom_bar = memnew(HScrollBar); + zoom_in = memnew(Button); + zoom_in->set_flat(true); + zoom_reset = memnew(Button); + zoom_reset->set_flat(true); + zoom_out = memnew(Button); + zoom_out->set_flat(true); + zoom_hbox->add_child(zoom_bar); + zoom_bar->set_h_size_flags(Control::SIZE_EXPAND_FILL); + zoom_bar->set_v_size_flags(Control::SIZE_EXPAND_FILL); + zoom_hbox->add_child(zoom_out); + zoom_hbox->add_child(zoom_reset); + zoom_hbox->add_child(zoom_in); + zoom_in->connect("pressed", callable_mp(this, &AudioStreamImportSettings::_preview_zoom_in)); + zoom_reset->connect("pressed", callable_mp(this, &AudioStreamImportSettings::_preview_zoom_reset)); + zoom_out->connect("pressed", callable_mp(this, &AudioStreamImportSettings::_preview_zoom_out)); + zoom_bar->connect("value_changed", callable_mp(this, &AudioStreamImportSettings::_preview_zoom_offset_changed)); + vbox->add_child(zoom_hbox); + + _indicator = memnew(Control); + _indicator->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); + _indicator->connect("draw", callable_mp(this, &AudioStreamImportSettings::_draw_indicator)); + _indicator->connect("gui_input", callable_mp(this, &AudioStreamImportSettings::_on_input_indicator)); + _indicator->connect("mouse_exited", callable_mp(this, &AudioStreamImportSettings::_on_indicator_mouse_exited)); + _preview->add_child(_indicator); + + HBoxContainer *hbox = memnew(HBoxContainer); + hbox->add_theme_constant_override("separation", 0); + vbox->add_child(hbox); + + _play_button = memnew(Button); + _play_button->set_flat(true); + hbox->add_child(_play_button); + _play_button->set_focus_mode(Control::FOCUS_NONE); + _play_button->connect("pressed", callable_mp(this, &AudioStreamImportSettings::_play)); + + _stop_button = memnew(Button); + _stop_button->set_flat(true); + hbox->add_child(_stop_button); + _stop_button->set_focus_mode(Control::FOCUS_NONE); + _stop_button->connect("pressed", callable_mp(this, &AudioStreamImportSettings::_stop)); + + _current_label = memnew(Label); + _current_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT); + _current_label->set_h_size_flags(Control::SIZE_EXPAND_FILL); + _current_label->set_modulate(Color(1, 1, 1, 0.5)); + hbox->add_child(_current_label); + + _duration_label = memnew(Label); + hbox->add_child(_duration_label); + + singleton = this; +} diff --git a/editor/import/audio_stream_import_settings.h b/editor/import/audio_stream_import_settings.h new file mode 100644 index 0000000000..5e399237ca --- /dev/null +++ b/editor/import/audio_stream_import_settings.h @@ -0,0 +1,114 @@ +/*************************************************************************/ +/* audio_stream_import_settings.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef AUDIO_STREAM_IMPORT_SETTINGS_H +#define AUDIO_STREAM_IMPORT_SETTINGS_H + +#include "editor/editor_plugin.h" +#include "scene/audio/audio_stream_player.h" +#include "scene/gui/color_rect.h" +#include "scene/gui/spin_box.h" +#include "scene/resources/texture.h" + +class AudioStreamImportSettings : public ConfirmationDialog { + GDCLASS(AudioStreamImportSettings, ConfirmationDialog); + + CheckBox *bpm_enabled = nullptr; + SpinBox *bpm_edit = nullptr; + CheckBox *beats_enabled = nullptr; + SpinBox *beats_edit = nullptr; + Label *bar_beats_label = nullptr; + SpinBox *bar_beats_edit = nullptr; + CheckBox *loop = nullptr; + SpinBox *loop_offset = nullptr; + ColorRect *color_rect = nullptr; + Ref<AudioStream> stream; + AudioStreamPlayer *_player = nullptr; + ColorRect *_preview = nullptr; + Control *_indicator = nullptr; + Label *_current_label = nullptr; + Label *_duration_label = nullptr; + + HScrollBar *zoom_bar = nullptr; + Button *zoom_in = nullptr; + Button *zoom_reset = nullptr; + Button *zoom_out = nullptr; + + Button *_play_button = nullptr; + Button *_stop_button = nullptr; + + bool updating_settings = false; + + float _current = 0; + bool _dragging = false; + bool _beat_len_dragging = false; + bool _pausing = false; + int _hovering_beat = -1; + + HashMap<StringName, Variant> params; + String importer; + String path; + + void _audio_changed(); + + static AudioStreamImportSettings *singleton; + + void _settings_changed(); + + void _reimport(); + +protected: + void _notification(int p_what); + void _preview_changed(ObjectID p_which); + void _preview_zoom_in(); + void _preview_zoom_out(); + void _preview_zoom_reset(); + void _preview_zoom_offset_changed(double); + + void _play(); + void _stop(); + void _on_finished(); + void _draw_preview(); + void _draw_indicator(); + void _on_input_indicator(Ref<InputEvent> p_event); + void _seek_to(real_t p_x); + void _set_beat_len_to(real_t p_x); + void _on_indicator_mouse_exited(); + int _get_beat_at_pos(real_t p_x); + +public: + void edit(const String &p_path, const String &p_importer, const Ref<AudioStream> &p_stream); + + static AudioStreamImportSettings *get_singleton() { return singleton; } + + AudioStreamImportSettings(); +}; + +#endif // AUDIO_STREAM_IMPORT_SETTINGS_H |