audio_stream_import_settings.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /**************************************************************************/
  2. /* audio_stream_import_settings.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 "audio_stream_import_settings.h"
  31. #include "editor/audio/audio_stream_preview.h"
  32. #include "editor/editor_string_names.h"
  33. #include "editor/file_system/editor_file_system.h"
  34. #include "editor/themes/editor_scale.h"
  35. #include "scene/gui/check_box.h"
  36. AudioStreamImportSettingsDialog *AudioStreamImportSettingsDialog::singleton = nullptr;
  37. void AudioStreamImportSettingsDialog::_notification(int p_what) {
  38. switch (p_what) {
  39. case NOTIFICATION_READY: {
  40. AudioStreamPreviewGenerator::get_singleton()->connect("preview_updated", callable_mp(this, &AudioStreamImportSettingsDialog::_preview_changed));
  41. connect(SceneStringName(confirmed), callable_mp(this, &AudioStreamImportSettingsDialog::_reimport));
  42. } break;
  43. case NOTIFICATION_THEME_CHANGED: {
  44. _play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
  45. _stop_button->set_button_icon(get_editor_theme_icon(SNAME("Stop")));
  46. _preview->set_color(get_theme_color(SNAME("dark_color_2"), EditorStringName(Editor)));
  47. color_rect->set_color(get_theme_color(SNAME("dark_color_1"), EditorStringName(Editor)));
  48. _current_label->begin_bulk_theme_override();
  49. _current_label->add_theme_font_override(SceneStringName(font), get_theme_font(SNAME("status_source"), EditorStringName(EditorFonts)));
  50. _current_label->add_theme_font_size_override(SceneStringName(font_size), get_theme_font_size(SNAME("status_source_size"), EditorStringName(EditorFonts)));
  51. _current_label->end_bulk_theme_override();
  52. _duration_label->begin_bulk_theme_override();
  53. _duration_label->add_theme_font_override(SceneStringName(font), get_theme_font(SNAME("status_source"), EditorStringName(EditorFonts)));
  54. _duration_label->add_theme_font_size_override(SceneStringName(font_size), get_theme_font_size(SNAME("status_source_size"), EditorStringName(EditorFonts)));
  55. _duration_label->end_bulk_theme_override();
  56. zoom_in->set_button_icon(get_editor_theme_icon(SNAME("ZoomMore")));
  57. zoom_out->set_button_icon(get_editor_theme_icon(SNAME("ZoomLess")));
  58. zoom_reset->set_button_icon(get_editor_theme_icon(SNAME("ZoomReset")));
  59. _indicator->queue_redraw();
  60. _preview->queue_redraw();
  61. } break;
  62. case NOTIFICATION_PROCESS: {
  63. _current = _player->get_playback_position();
  64. _indicator->queue_redraw();
  65. } break;
  66. case NOTIFICATION_VISIBILITY_CHANGED: {
  67. if (!is_visible()) {
  68. _stop();
  69. }
  70. } break;
  71. }
  72. }
  73. void AudioStreamImportSettingsDialog::_draw_preview() {
  74. Rect2 rect = _preview->get_rect();
  75. Size2 rect_size = rect.size;
  76. int width = rect_size.width;
  77. Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
  78. float preview_offset = zoom_bar->get_value();
  79. float preview_len = zoom_bar->get_page();
  80. Ref<Font> beat_font = get_theme_font(SNAME("main"), EditorStringName(EditorFonts));
  81. int main_size = get_theme_font_size(SNAME("main_size"), EditorStringName(EditorFonts));
  82. Vector<Vector2> points;
  83. points.resize(width * 2);
  84. Color color_active = get_theme_color(SNAME("contrast_color_2"), EditorStringName(Editor));
  85. Color color_inactive = color_active;
  86. color_inactive.a *= 0.5;
  87. Vector<Color> colors;
  88. colors.resize(width);
  89. float inactive_from = 1e20;
  90. float beat_size = 0;
  91. int last_beat = 0;
  92. if (stream->get_bpm() > 0) {
  93. beat_size = 60 / float(stream->get_bpm());
  94. int y_ofs = beat_font->get_height(main_size) + 4 * EDSCALE;
  95. rect.position.y += y_ofs;
  96. rect.size.y -= y_ofs;
  97. if (stream->get_beat_count() > 0) {
  98. last_beat = stream->get_beat_count();
  99. inactive_from = last_beat * beat_size;
  100. }
  101. }
  102. for (int i = 0; i < width; i++) {
  103. float ofs = preview_offset + i * preview_len / rect_size.width;
  104. float ofs_n = preview_offset + (i + 1) * preview_len / rect_size.width;
  105. float max = preview->get_max(ofs, ofs_n) * 0.5 + 0.5;
  106. float min = preview->get_min(ofs, ofs_n) * 0.5 + 0.5;
  107. int idx = i;
  108. points.write[idx * 2 + 0] = Vector2(i + 1, rect.position.y + min * rect.size.y);
  109. points.write[idx * 2 + 1] = Vector2(i + 1, rect.position.y + max * rect.size.y);
  110. colors.write[idx] = ofs > inactive_from ? color_inactive : color_active;
  111. }
  112. if (!points.is_empty()) {
  113. RS::get_singleton()->canvas_item_add_multiline(_preview->get_canvas_item(), points, colors);
  114. }
  115. if (beat_size) {
  116. Color beat_color = Color(1, 1, 1, 1);
  117. Color final_beat_color = beat_color;
  118. Color bar_color = beat_color;
  119. beat_color.a *= 0.4;
  120. bar_color.a *= 0.6;
  121. int prev_beat = 0; // Do not draw beat zero
  122. Color color_bg = color_active;
  123. color_bg.a *= 0.2;
  124. _preview->draw_rect(Rect2(0, 0, rect.size.width, rect.position.y), color_bg);
  125. int bar_beats = stream->get_bar_beats();
  126. int last_text_end_x = 0;
  127. for (int i = 0; i < width; i++) {
  128. float ofs = preview_offset + i * preview_len / rect_size.width;
  129. int beat = int(ofs / beat_size);
  130. if (beat != prev_beat) {
  131. String text = itos(beat);
  132. int text_w = beat_font->get_string_size(text).width;
  133. if (i - text_w / 2 > last_text_end_x + 2 * EDSCALE) {
  134. int x_ofs = i - text_w / 2;
  135. _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);
  136. last_text_end_x = i + text_w / 2;
  137. }
  138. if (beat == last_beat) {
  139. _preview->draw_rect(Rect2i(i, rect.position.y, 2, rect.size.height), final_beat_color);
  140. // Darken subsequent beats
  141. beat_color.a *= 0.3;
  142. color_active.a *= 0.3;
  143. } else {
  144. _preview->draw_rect(Rect2i(i, rect.position.y, 1, rect.size.height), (beat % bar_beats) == 0 ? bar_color : beat_color);
  145. }
  146. prev_beat = beat;
  147. }
  148. }
  149. }
  150. }
  151. void AudioStreamImportSettingsDialog::_preview_changed(ObjectID p_which) {
  152. if (stream.is_valid() && stream->get_instance_id() == p_which) {
  153. _preview->queue_redraw();
  154. }
  155. }
  156. void AudioStreamImportSettingsDialog::_preview_zoom_in() {
  157. if (stream.is_null()) {
  158. return;
  159. }
  160. float page_size = zoom_bar->get_page();
  161. zoom_bar->set_page(page_size * 0.5);
  162. zoom_bar->set_value(zoom_bar->get_value() + page_size * 0.25);
  163. zoom_bar->show();
  164. _preview->queue_redraw();
  165. _indicator->queue_redraw();
  166. }
  167. void AudioStreamImportSettingsDialog::_preview_zoom_out() {
  168. if (stream.is_null()) {
  169. return;
  170. }
  171. float page_size = zoom_bar->get_page();
  172. zoom_bar->set_page(MIN(zoom_bar->get_max(), page_size * 2.0));
  173. zoom_bar->set_value(zoom_bar->get_value() - page_size * 0.5);
  174. if (zoom_bar->get_value() == 0) {
  175. zoom_bar->hide();
  176. }
  177. _preview->queue_redraw();
  178. _indicator->queue_redraw();
  179. }
  180. void AudioStreamImportSettingsDialog::_preview_zoom_reset() {
  181. if (stream.is_null()) {
  182. return;
  183. }
  184. zoom_bar->set_max(stream->get_length());
  185. zoom_bar->set_page(zoom_bar->get_max());
  186. zoom_bar->set_value(0);
  187. zoom_bar->hide();
  188. _preview->queue_redraw();
  189. _indicator->queue_redraw();
  190. }
  191. void AudioStreamImportSettingsDialog::_preview_zoom_offset_changed(double) {
  192. _preview->queue_redraw();
  193. _indicator->queue_redraw();
  194. }
  195. void AudioStreamImportSettingsDialog::_reset_master() {
  196. master_state.bypass = AudioServer::get_singleton()->is_bus_bypassing_effects(0);
  197. master_state.mute = AudioServer::get_singleton()->is_bus_mute(0);
  198. master_state.volume = AudioServer::get_singleton()->get_bus_volume_db(0);
  199. AudioServer::get_singleton()->set_bus_bypass_effects(0, true); // We don't want effects interfering.
  200. AudioServer::get_singleton()->set_bus_mute(0, false);
  201. AudioServer::get_singleton()->set_bus_volume_db(0, 0);
  202. // Prevent the modifications from being saved.
  203. AudioServer::get_singleton()->set_edited(false);
  204. }
  205. void AudioStreamImportSettingsDialog::_load_master_state() {
  206. AudioServer::get_singleton()->set_bus_bypass_effects(0, master_state.bypass);
  207. AudioServer::get_singleton()->set_bus_mute(0, master_state.mute);
  208. AudioServer::get_singleton()->set_bus_volume_db(0, master_state.volume);
  209. // Prevent the modifications from being saved.
  210. AudioServer::get_singleton()->set_edited(false);
  211. }
  212. void AudioStreamImportSettingsDialog::_audio_changed() {
  213. if (!is_visible()) {
  214. return;
  215. }
  216. _preview->queue_redraw();
  217. _indicator->queue_redraw();
  218. color_rect->queue_redraw();
  219. }
  220. void AudioStreamImportSettingsDialog::_play() {
  221. if (_player->is_playing()) {
  222. _load_master_state();
  223. // '_pausing' variable indicates that we want to pause the audio player, not stop it. See '_on_finished()'.
  224. _pausing = true;
  225. _player->stop();
  226. _play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
  227. set_process(false);
  228. } else {
  229. _reset_master();
  230. _player->play(_current);
  231. _play_button->set_button_icon(get_editor_theme_icon(SNAME("Pause")));
  232. set_process(true);
  233. }
  234. }
  235. void AudioStreamImportSettingsDialog::_stop() {
  236. _load_master_state();
  237. _player->stop();
  238. _play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
  239. _current = 0;
  240. _indicator->queue_redraw();
  241. set_process(false);
  242. }
  243. void AudioStreamImportSettingsDialog::_on_finished() {
  244. _play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
  245. if (!_pausing) {
  246. _current = 0;
  247. _indicator->queue_redraw();
  248. } else {
  249. _pausing = false;
  250. }
  251. set_process(false);
  252. }
  253. void AudioStreamImportSettingsDialog::_draw_indicator() {
  254. if (stream.is_null()) {
  255. return;
  256. }
  257. Rect2 rect = _preview->get_rect();
  258. Ref<Font> beat_font = get_theme_font(SNAME("main"), EditorStringName(EditorFonts));
  259. int main_size = get_theme_font_size(SNAME("main_size"), EditorStringName(EditorFonts));
  260. if (stream->get_bpm() > 0) {
  261. int y_ofs = beat_font->get_height(main_size) + 4 * EDSCALE;
  262. rect.position.y += y_ofs;
  263. rect.size.height -= y_ofs;
  264. }
  265. _current_label->set_text(String::num(_current, 2).pad_decimals(2) + " /");
  266. float ofs_x = (_current - zoom_bar->get_value()) * rect.size.width / zoom_bar->get_page();
  267. if (ofs_x < 0 || ofs_x >= rect.size.width) {
  268. return;
  269. }
  270. const Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  271. _indicator->draw_line(Point2(ofs_x, rect.position.y), Point2(ofs_x, rect.position.y + rect.size.height), color, Math::round(2 * EDSCALE));
  272. _indicator->draw_texture(
  273. get_editor_theme_icon(SNAME("TimelineIndicator")),
  274. Point2(ofs_x - get_editor_theme_icon(SNAME("TimelineIndicator"))->get_width() * 0.5, rect.position.y),
  275. color);
  276. if (stream->get_bpm() > 0 && _hovering_beat != -1) {
  277. // Draw hovered beat.
  278. float preview_offset = zoom_bar->get_value();
  279. float preview_len = zoom_bar->get_page();
  280. float beat_size = 60 / float(stream->get_bpm());
  281. int prev_beat = 0;
  282. for (int i = 0; i < rect.size.width; i++) {
  283. float ofs = preview_offset + i * preview_len / rect.size.width;
  284. int beat = int(ofs / beat_size);
  285. if (beat != prev_beat) {
  286. String text = itos(beat);
  287. int text_w = beat_font->get_string_size(text).width;
  288. if (i - text_w / 2 > 2 * EDSCALE && beat == _hovering_beat) {
  289. int x_ofs = i - text_w / 2;
  290. _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);
  291. break;
  292. }
  293. prev_beat = beat;
  294. }
  295. }
  296. }
  297. }
  298. void AudioStreamImportSettingsDialog::_on_indicator_mouse_exited() {
  299. _hovering_beat = -1;
  300. _indicator->queue_redraw();
  301. }
  302. void AudioStreamImportSettingsDialog::_on_input_indicator(Ref<InputEvent> p_event) {
  303. const Ref<InputEventMouseButton> mb = p_event;
  304. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  305. if (stream->get_bpm() > 0) {
  306. int main_size = get_theme_font_size(SNAME("main_size"), EditorStringName(EditorFonts));
  307. Ref<Font> beat_font = get_theme_font(SNAME("main"), EditorStringName(EditorFonts));
  308. int y_ofs = beat_font->get_height(main_size) + 4 * EDSCALE;
  309. if ((!_dragging && mb->get_position().y < y_ofs) || _beat_len_dragging) {
  310. if (mb->is_pressed()) {
  311. _set_beat_len_to(mb->get_position().x);
  312. _beat_len_dragging = true;
  313. } else {
  314. _beat_len_dragging = false;
  315. }
  316. return;
  317. }
  318. }
  319. if (mb->is_pressed()) {
  320. _seek_to(mb->get_position().x);
  321. }
  322. _dragging = mb->is_pressed();
  323. }
  324. const Ref<InputEventMouseMotion> mm = p_event;
  325. if (mm.is_valid()) {
  326. if (_dragging) {
  327. _seek_to(mm->get_position().x);
  328. }
  329. if (_beat_len_dragging) {
  330. _set_beat_len_to(mm->get_position().x);
  331. }
  332. if (stream->get_bpm() > 0) {
  333. int main_size = get_theme_font_size(SNAME("main_size"), EditorStringName(EditorFonts));
  334. Ref<Font> beat_font = get_theme_font(SNAME("main"), EditorStringName(EditorFonts));
  335. int y_ofs = beat_font->get_height(main_size) + 4 * EDSCALE;
  336. if (mm->get_position().y < y_ofs) {
  337. int new_hovering_beat = _get_beat_at_pos(mm->get_position().x);
  338. if (new_hovering_beat != _hovering_beat) {
  339. _hovering_beat = new_hovering_beat;
  340. _indicator->queue_redraw();
  341. }
  342. } else if (_hovering_beat != -1) {
  343. _hovering_beat = -1;
  344. _indicator->queue_redraw();
  345. }
  346. }
  347. }
  348. }
  349. int AudioStreamImportSettingsDialog::_get_beat_at_pos(real_t p_x) {
  350. float ofs_sec = zoom_bar->get_value() + p_x * zoom_bar->get_page() / _preview->get_size().width;
  351. ofs_sec = CLAMP(ofs_sec, 0, stream->get_length());
  352. float beat_size = 60 / float(stream->get_bpm());
  353. int beat = int(ofs_sec / beat_size + 0.5);
  354. if (beat * beat_size > stream->get_length() + 0.001) { // Stream may end few audio frames before but may still want to use full loop.
  355. beat--;
  356. }
  357. return beat;
  358. }
  359. void AudioStreamImportSettingsDialog::_set_beat_len_to(real_t p_x) {
  360. int beat = _get_beat_at_pos(p_x);
  361. if (beat < 1) {
  362. beat = 1; // Because 0 is disable.
  363. }
  364. updating_settings = true;
  365. beats_enabled->set_pressed(true);
  366. beats_edit->set_value(beat);
  367. updating_settings = false;
  368. _settings_changed();
  369. }
  370. void AudioStreamImportSettingsDialog::_seek_to(real_t p_x) {
  371. _current = zoom_bar->get_value() + p_x / _preview->get_rect().size.x * zoom_bar->get_page();
  372. _current = CLAMP(_current, 0, stream->get_length());
  373. _player->seek(_current);
  374. _indicator->queue_redraw();
  375. }
  376. void AudioStreamImportSettingsDialog::edit(const String &p_path, const String &p_importer, const Ref<AudioStream> &p_stream) {
  377. if (stream.is_valid()) {
  378. stream->disconnect_changed(callable_mp(this, &AudioStreamImportSettingsDialog::_audio_changed));
  379. }
  380. importer = p_importer;
  381. path = p_path;
  382. stream = p_stream;
  383. _player->set_stream(stream);
  384. _current = 0;
  385. String text = String::num(stream->get_length(), 2).pad_decimals(2) + "s";
  386. _duration_label->set_text(text);
  387. if (stream.is_valid()) {
  388. stream->connect_changed(callable_mp(this, &AudioStreamImportSettingsDialog::_audio_changed));
  389. _preview->queue_redraw();
  390. _indicator->queue_redraw();
  391. color_rect->queue_redraw();
  392. } else {
  393. hide();
  394. }
  395. params.clear();
  396. if (stream.is_valid()) {
  397. Ref<ConfigFile> config_file;
  398. config_file.instantiate();
  399. Error err = config_file->load(p_path + ".import");
  400. updating_settings = true;
  401. if (err == OK) {
  402. double bpm = config_file->get_value("params", "bpm", 0);
  403. int beats = config_file->get_value("params", "beat_count", 0);
  404. bpm_edit->set_value(bpm > 0 ? bpm : 120);
  405. bpm_enabled->set_pressed(bpm > 0);
  406. beats_edit->set_value(beats);
  407. beats_enabled->set_pressed(beats > 0);
  408. loop->set_pressed(config_file->get_value("params", "loop", false));
  409. loop_offset->set_value(config_file->get_value("params", "loop_offset", 0));
  410. bar_beats_edit->set_value(config_file->get_value("params", "bar_beats", 4));
  411. Vector<String> keys = config_file->get_section_keys("params");
  412. for (const String &K : keys) {
  413. params[K] = config_file->get_value("params", K);
  414. }
  415. } else {
  416. bpm_edit->set_value(false);
  417. bpm_enabled->set_pressed(false);
  418. beats_edit->set_value(0);
  419. beats_enabled->set_pressed(false);
  420. bar_beats_edit->set_value(4);
  421. loop->set_pressed(false);
  422. loop_offset->set_value(0);
  423. }
  424. _preview_zoom_reset();
  425. updating_settings = false;
  426. _settings_changed();
  427. set_title(vformat(TTR("Audio Stream Importer: %s"), p_path.get_file()));
  428. popup_centered();
  429. }
  430. }
  431. void AudioStreamImportSettingsDialog::_settings_changed() {
  432. if (updating_settings) {
  433. return;
  434. }
  435. updating_settings = true;
  436. stream->call("set_loop", loop->is_pressed());
  437. stream->call("set_loop_offset", loop_offset->get_value());
  438. if (loop->is_pressed()) {
  439. loop_offset->set_editable(true);
  440. } else {
  441. loop_offset->set_editable(false);
  442. }
  443. if (bpm_enabled->is_pressed()) {
  444. stream->call("set_bpm", bpm_edit->get_value());
  445. beats_enabled->set_disabled(false);
  446. beats_edit->set_editable(true);
  447. bar_beats_edit->set_editable(true);
  448. double bpm = bpm_edit->get_value();
  449. if (bpm > 0) {
  450. float beat_size = 60 / float(bpm);
  451. int beat_max = int((stream->get_length() + 0.001) / beat_size);
  452. int current_beat = beats_edit->get_value();
  453. beats_edit->set_max(beat_max);
  454. if (current_beat > beat_max) {
  455. beats_edit->set_value(beat_max);
  456. stream->call("set_beat_count", beat_max);
  457. }
  458. }
  459. stream->call("set_bar_beats", bar_beats_edit->get_value());
  460. } else {
  461. stream->call("set_bpm", 0);
  462. stream->call("set_bar_beats", 4);
  463. beats_enabled->set_disabled(true);
  464. beats_edit->set_editable(false);
  465. bar_beats_edit->set_editable(false);
  466. }
  467. if (bpm_enabled->is_pressed() && beats_enabled->is_pressed()) {
  468. stream->call("set_beat_count", beats_edit->get_value());
  469. } else {
  470. stream->call("set_beat_count", 0);
  471. }
  472. updating_settings = false;
  473. _preview->queue_redraw();
  474. _indicator->queue_redraw();
  475. color_rect->queue_redraw();
  476. }
  477. void AudioStreamImportSettingsDialog::_reimport() {
  478. params["loop"] = loop->is_pressed();
  479. params["loop_offset"] = loop_offset->get_value();
  480. params["bpm"] = bpm_enabled->is_pressed() ? double(bpm_edit->get_value()) : double(0);
  481. params["beat_count"] = (bpm_enabled->is_pressed() && beats_enabled->is_pressed()) ? int(beats_edit->get_value()) : int(0);
  482. params["bar_beats"] = (bpm_enabled->is_pressed()) ? int(bar_beats_edit->get_value()) : int(4);
  483. EditorFileSystem::get_singleton()->reimport_file_with_custom_parameters(path, importer, params);
  484. }
  485. AudioStreamImportSettingsDialog::AudioStreamImportSettingsDialog() {
  486. get_ok_button()->set_text(TTR("Reimport"));
  487. get_cancel_button()->set_text(TTR("Close"));
  488. VBoxContainer *main_vbox = memnew(VBoxContainer);
  489. add_child(main_vbox);
  490. HBoxContainer *loop_hb = memnew(HBoxContainer);
  491. loop_hb->add_theme_constant_override("separation", 4 * EDSCALE);
  492. loop = memnew(CheckBox);
  493. loop->set_text(TTR("Enable"));
  494. loop->set_tooltip_text(TTR("Enable looping."));
  495. loop->connect(SceneStringName(toggled), callable_mp(this, &AudioStreamImportSettingsDialog::_settings_changed).unbind(1));
  496. loop_hb->add_child(loop);
  497. loop_hb->add_spacer();
  498. loop_hb->add_child(memnew(Label(TTR("Offset:"))));
  499. loop_offset = memnew(SpinBox);
  500. loop_offset->set_accessibility_name(TTRC("Offset:"));
  501. loop_offset->set_max(10000);
  502. loop_offset->set_step(0.001);
  503. loop_offset->set_suffix("s");
  504. loop_offset->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  505. loop_offset->set_stretch_ratio(0.33);
  506. loop_offset->set_tooltip_text(TTR("Loop offset (from beginning). Note that if BPM is set, this setting will be ignored."));
  507. loop_offset->connect(SceneStringName(value_changed), callable_mp(this, &AudioStreamImportSettingsDialog::_settings_changed).unbind(1));
  508. loop_hb->add_child(loop_offset);
  509. main_vbox->add_margin_child(TTR("Loop:"), loop_hb);
  510. HBoxContainer *interactive_hb = memnew(HBoxContainer);
  511. interactive_hb->add_theme_constant_override("separation", 4 * EDSCALE);
  512. bpm_enabled = memnew(CheckBox);
  513. bpm_enabled->set_text((TTR("BPM:")));
  514. bpm_enabled->connect(SceneStringName(toggled), callable_mp(this, &AudioStreamImportSettingsDialog::_settings_changed).unbind(1));
  515. interactive_hb->add_child(bpm_enabled);
  516. bpm_edit = memnew(SpinBox);
  517. bpm_edit->set_max(400);
  518. bpm_edit->set_step(0.01);
  519. bpm_edit->set_accessibility_name(TTRC("BPM:"));
  520. bpm_edit->set_tooltip_text(TTR("Configure the Beats Per Measure (tempo) used for the interactive streams.\nThis is required in order to configure beat information."));
  521. bpm_edit->connect(SceneStringName(value_changed), callable_mp(this, &AudioStreamImportSettingsDialog::_settings_changed).unbind(1));
  522. interactive_hb->add_child(bpm_edit);
  523. interactive_hb->add_spacer();
  524. beats_enabled = memnew(CheckBox);
  525. beats_enabled->set_text(TTR("Beat Count:"));
  526. beats_enabled->connect(SceneStringName(toggled), callable_mp(this, &AudioStreamImportSettingsDialog::_settings_changed).unbind(1));
  527. interactive_hb->add_child(beats_enabled);
  528. beats_edit = memnew(SpinBox);
  529. beats_edit->set_tooltip_text(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."));
  530. beats_edit->set_max(99999);
  531. beats_edit->set_accessibility_name(TTRC("Beat Count:"));
  532. beats_edit->connect(SceneStringName(value_changed), callable_mp(this, &AudioStreamImportSettingsDialog::_settings_changed).unbind(1));
  533. interactive_hb->add_child(beats_edit);
  534. bar_beats_label = memnew(Label(TTR("Bar Beats:")));
  535. interactive_hb->add_child(bar_beats_label);
  536. bar_beats_edit = memnew(SpinBox);
  537. bar_beats_edit->set_tooltip_text(TTR("Configure the Beats Per Bar. This used for music-aware transitions between AudioStreams."));
  538. bar_beats_edit->set_min(2);
  539. bar_beats_edit->set_max(32);
  540. bar_beats_edit->set_accessibility_name(TTRC("Bar Beats:"));
  541. bar_beats_edit->connect(SceneStringName(value_changed), callable_mp(this, &AudioStreamImportSettingsDialog::_settings_changed).unbind(1));
  542. interactive_hb->add_child(bar_beats_edit);
  543. main_vbox->add_margin_child(TTR("Music Playback:"), interactive_hb);
  544. color_rect = memnew(ColorRect);
  545. main_vbox->add_margin_child(TTR("Preview:"), color_rect, true);
  546. color_rect->set_custom_minimum_size(Size2(600, 200) * EDSCALE);
  547. color_rect->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  548. _player = memnew(AudioStreamPlayer);
  549. _player->connect(SceneStringName(finished), callable_mp(this, &AudioStreamImportSettingsDialog::_on_finished));
  550. color_rect->add_child(_player);
  551. VBoxContainer *vbox = memnew(VBoxContainer);
  552. vbox->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  553. color_rect->add_child(vbox);
  554. vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  555. _preview = memnew(ColorRect);
  556. _preview->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  557. _preview->connect(SceneStringName(draw), callable_mp(this, &AudioStreamImportSettingsDialog::_draw_preview));
  558. _preview->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  559. vbox->add_child(_preview);
  560. zoom_bar = memnew(HScrollBar);
  561. zoom_bar->hide();
  562. vbox->add_child(zoom_bar);
  563. zoom_bar->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  564. zoom_bar->connect(SceneStringName(value_changed), callable_mp(this, &AudioStreamImportSettingsDialog::_preview_zoom_offset_changed));
  565. HBoxContainer *hbox = memnew(HBoxContainer);
  566. hbox->add_theme_constant_override("separation", 0);
  567. vbox->add_child(hbox);
  568. _indicator = memnew(Control);
  569. _indicator->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  570. _indicator->connect(SceneStringName(draw), callable_mp(this, &AudioStreamImportSettingsDialog::_draw_indicator));
  571. _indicator->connect(SceneStringName(gui_input), callable_mp(this, &AudioStreamImportSettingsDialog::_on_input_indicator));
  572. _indicator->connect(SceneStringName(mouse_exited), callable_mp(this, &AudioStreamImportSettingsDialog::_on_indicator_mouse_exited));
  573. _preview->add_child(_indicator);
  574. _play_button = memnew(Button);
  575. _play_button->set_accessibility_name(TTRC("Play"));
  576. _play_button->set_flat(true);
  577. hbox->add_child(_play_button);
  578. _play_button->connect(SceneStringName(pressed), callable_mp(this, &AudioStreamImportSettingsDialog::_play));
  579. _stop_button = memnew(Button);
  580. _stop_button->set_accessibility_name(TTRC("Stop"));
  581. _stop_button->set_flat(true);
  582. hbox->add_child(_stop_button);
  583. _stop_button->connect(SceneStringName(pressed), callable_mp(this, &AudioStreamImportSettingsDialog::_stop));
  584. _current_label = memnew(Label);
  585. _current_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  586. _current_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  587. _current_label->set_modulate(Color(1, 1, 1, 0.5));
  588. hbox->add_child(_current_label);
  589. _duration_label = memnew(Label);
  590. hbox->add_child(_duration_label);
  591. zoom_in = memnew(Button);
  592. zoom_in->set_accessibility_name(TTRC("Zoom In"));
  593. zoom_in->set_flat(true);
  594. zoom_reset = memnew(Button);
  595. zoom_reset->set_accessibility_name(TTRC("Reset Zoom"));
  596. zoom_reset->set_flat(true);
  597. zoom_out = memnew(Button);
  598. zoom_out->set_accessibility_name(TTRC("Zoom Out"));
  599. zoom_out->set_flat(true);
  600. hbox->add_child(zoom_out);
  601. hbox->add_child(zoom_reset);
  602. hbox->add_child(zoom_in);
  603. zoom_in->connect(SceneStringName(pressed), callable_mp(this, &AudioStreamImportSettingsDialog::_preview_zoom_in));
  604. zoom_reset->connect(SceneStringName(pressed), callable_mp(this, &AudioStreamImportSettingsDialog::_preview_zoom_reset));
  605. zoom_out->connect(SceneStringName(pressed), callable_mp(this, &AudioStreamImportSettingsDialog::_preview_zoom_out));
  606. singleton = this;
  607. }