2
0

audio_stream_import_settings.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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::_audio_changed() {
  196. if (!is_visible()) {
  197. return;
  198. }
  199. _preview->queue_redraw();
  200. _indicator->queue_redraw();
  201. color_rect->queue_redraw();
  202. }
  203. void AudioStreamImportSettingsDialog::_play() {
  204. if (_player->is_playing()) {
  205. // '_pausing' variable indicates that we want to pause the audio player, not stop it. See '_on_finished()'.
  206. _pausing = true;
  207. _player->stop();
  208. _play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
  209. set_process(false);
  210. } else {
  211. _player->play(_current);
  212. _play_button->set_button_icon(get_editor_theme_icon(SNAME("Pause")));
  213. set_process(true);
  214. }
  215. }
  216. void AudioStreamImportSettingsDialog::_stop() {
  217. _player->stop();
  218. _play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
  219. _current = 0;
  220. _indicator->queue_redraw();
  221. set_process(false);
  222. }
  223. void AudioStreamImportSettingsDialog::_on_finished() {
  224. _play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
  225. if (!_pausing) {
  226. _current = 0;
  227. _indicator->queue_redraw();
  228. } else {
  229. _pausing = false;
  230. }
  231. set_process(false);
  232. }
  233. void AudioStreamImportSettingsDialog::_draw_indicator() {
  234. if (stream.is_null()) {
  235. return;
  236. }
  237. Rect2 rect = _preview->get_rect();
  238. Ref<Font> beat_font = get_theme_font(SNAME("main"), EditorStringName(EditorFonts));
  239. int main_size = get_theme_font_size(SNAME("main_size"), EditorStringName(EditorFonts));
  240. if (stream->get_bpm() > 0) {
  241. int y_ofs = beat_font->get_height(main_size) + 4 * EDSCALE;
  242. rect.position.y += y_ofs;
  243. rect.size.height -= y_ofs;
  244. }
  245. _current_label->set_text(String::num(_current, 2).pad_decimals(2) + " /");
  246. float ofs_x = (_current - zoom_bar->get_value()) * rect.size.width / zoom_bar->get_page();
  247. if (ofs_x < 0 || ofs_x >= rect.size.width) {
  248. return;
  249. }
  250. const Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  251. _indicator->draw_line(Point2(ofs_x, rect.position.y), Point2(ofs_x, rect.position.y + rect.size.height), color, Math::round(2 * EDSCALE));
  252. _indicator->draw_texture(
  253. get_editor_theme_icon(SNAME("TimelineIndicator")),
  254. Point2(ofs_x - get_editor_theme_icon(SNAME("TimelineIndicator"))->get_width() * 0.5, rect.position.y),
  255. color);
  256. if (stream->get_bpm() > 0 && _hovering_beat != -1) {
  257. // Draw hovered beat.
  258. float preview_offset = zoom_bar->get_value();
  259. float preview_len = zoom_bar->get_page();
  260. float beat_size = 60 / float(stream->get_bpm());
  261. int prev_beat = 0;
  262. for (int i = 0; i < rect.size.width; i++) {
  263. float ofs = preview_offset + i * preview_len / rect.size.width;
  264. int beat = int(ofs / beat_size);
  265. if (beat != prev_beat) {
  266. String text = itos(beat);
  267. int text_w = beat_font->get_string_size(text).width;
  268. if (i - text_w / 2 > 2 * EDSCALE && beat == _hovering_beat) {
  269. int x_ofs = i - text_w / 2;
  270. _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);
  271. break;
  272. }
  273. prev_beat = beat;
  274. }
  275. }
  276. }
  277. }
  278. void AudioStreamImportSettingsDialog::_on_indicator_mouse_exited() {
  279. _hovering_beat = -1;
  280. _indicator->queue_redraw();
  281. }
  282. void AudioStreamImportSettingsDialog::_on_input_indicator(Ref<InputEvent> p_event) {
  283. const Ref<InputEventMouseButton> mb = p_event;
  284. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  285. if (stream->get_bpm() > 0) {
  286. int main_size = get_theme_font_size(SNAME("main_size"), EditorStringName(EditorFonts));
  287. Ref<Font> beat_font = get_theme_font(SNAME("main"), EditorStringName(EditorFonts));
  288. int y_ofs = beat_font->get_height(main_size) + 4 * EDSCALE;
  289. if ((!_dragging && mb->get_position().y < y_ofs) || _beat_len_dragging) {
  290. if (mb->is_pressed()) {
  291. _set_beat_len_to(mb->get_position().x);
  292. _beat_len_dragging = true;
  293. } else {
  294. _beat_len_dragging = false;
  295. }
  296. return;
  297. }
  298. }
  299. if (mb->is_pressed()) {
  300. _seek_to(mb->get_position().x);
  301. }
  302. _dragging = mb->is_pressed();
  303. }
  304. const Ref<InputEventMouseMotion> mm = p_event;
  305. if (mm.is_valid()) {
  306. if (_dragging) {
  307. _seek_to(mm->get_position().x);
  308. }
  309. if (_beat_len_dragging) {
  310. _set_beat_len_to(mm->get_position().x);
  311. }
  312. if (stream->get_bpm() > 0) {
  313. int main_size = get_theme_font_size(SNAME("main_size"), EditorStringName(EditorFonts));
  314. Ref<Font> beat_font = get_theme_font(SNAME("main"), EditorStringName(EditorFonts));
  315. int y_ofs = beat_font->get_height(main_size) + 4 * EDSCALE;
  316. if (mm->get_position().y < y_ofs) {
  317. int new_hovering_beat = _get_beat_at_pos(mm->get_position().x);
  318. if (new_hovering_beat != _hovering_beat) {
  319. _hovering_beat = new_hovering_beat;
  320. _indicator->queue_redraw();
  321. }
  322. } else if (_hovering_beat != -1) {
  323. _hovering_beat = -1;
  324. _indicator->queue_redraw();
  325. }
  326. }
  327. }
  328. }
  329. int AudioStreamImportSettingsDialog::_get_beat_at_pos(real_t p_x) {
  330. float ofs_sec = zoom_bar->get_value() + p_x * zoom_bar->get_page() / _preview->get_size().width;
  331. ofs_sec = CLAMP(ofs_sec, 0, stream->get_length());
  332. float beat_size = 60 / float(stream->get_bpm());
  333. int beat = int(ofs_sec / beat_size + 0.5);
  334. if (beat * beat_size > stream->get_length() + 0.001) { // Stream may end few audio frames before but may still want to use full loop.
  335. beat--;
  336. }
  337. return beat;
  338. }
  339. void AudioStreamImportSettingsDialog::_set_beat_len_to(real_t p_x) {
  340. int beat = _get_beat_at_pos(p_x);
  341. if (beat < 1) {
  342. beat = 1; // Because 0 is disable.
  343. }
  344. updating_settings = true;
  345. beats_enabled->set_pressed(true);
  346. beats_edit->set_value(beat);
  347. updating_settings = false;
  348. _settings_changed();
  349. }
  350. void AudioStreamImportSettingsDialog::_seek_to(real_t p_x) {
  351. _current = zoom_bar->get_value() + p_x / _preview->get_rect().size.x * zoom_bar->get_page();
  352. _current = CLAMP(_current, 0, stream->get_length());
  353. _player->seek(_current);
  354. _indicator->queue_redraw();
  355. }
  356. void AudioStreamImportSettingsDialog::edit(const String &p_path, const String &p_importer, const Ref<AudioStream> &p_stream) {
  357. if (stream.is_valid()) {
  358. stream->disconnect_changed(callable_mp(this, &AudioStreamImportSettingsDialog::_audio_changed));
  359. }
  360. importer = p_importer;
  361. path = p_path;
  362. stream = p_stream;
  363. _player->set_stream(stream);
  364. _current = 0;
  365. String text = String::num(stream->get_length(), 2).pad_decimals(2) + "s";
  366. _duration_label->set_text(text);
  367. if (stream.is_valid()) {
  368. stream->connect_changed(callable_mp(this, &AudioStreamImportSettingsDialog::_audio_changed));
  369. _preview->queue_redraw();
  370. _indicator->queue_redraw();
  371. color_rect->queue_redraw();
  372. } else {
  373. hide();
  374. }
  375. params.clear();
  376. if (stream.is_valid()) {
  377. Ref<ConfigFile> config_file;
  378. config_file.instantiate();
  379. Error err = config_file->load(p_path + ".import");
  380. updating_settings = true;
  381. if (err == OK) {
  382. double bpm = config_file->get_value("params", "bpm", 0);
  383. int beats = config_file->get_value("params", "beat_count", 0);
  384. bpm_edit->set_value(bpm > 0 ? bpm : 120);
  385. bpm_enabled->set_pressed(bpm > 0);
  386. beats_edit->set_value(beats);
  387. beats_enabled->set_pressed(beats > 0);
  388. loop->set_pressed(config_file->get_value("params", "loop", false));
  389. loop_offset->set_value(config_file->get_value("params", "loop_offset", 0));
  390. bar_beats_edit->set_value(config_file->get_value("params", "bar_beats", 4));
  391. Vector<String> keys = config_file->get_section_keys("params");
  392. for (const String &K : keys) {
  393. params[K] = config_file->get_value("params", K);
  394. }
  395. } else {
  396. bpm_edit->set_value(false);
  397. bpm_enabled->set_pressed(false);
  398. beats_edit->set_value(0);
  399. beats_enabled->set_pressed(false);
  400. bar_beats_edit->set_value(4);
  401. loop->set_pressed(false);
  402. loop_offset->set_value(0);
  403. }
  404. _preview_zoom_reset();
  405. updating_settings = false;
  406. _settings_changed();
  407. set_title(vformat(TTR("Audio Stream Importer: %s"), p_path.get_file()));
  408. popup_centered();
  409. }
  410. }
  411. void AudioStreamImportSettingsDialog::_settings_changed() {
  412. if (updating_settings) {
  413. return;
  414. }
  415. updating_settings = true;
  416. stream->call("set_loop", loop->is_pressed());
  417. stream->call("set_loop_offset", loop_offset->get_value());
  418. if (loop->is_pressed()) {
  419. loop_offset->set_editable(true);
  420. } else {
  421. loop_offset->set_editable(false);
  422. }
  423. if (bpm_enabled->is_pressed()) {
  424. stream->call("set_bpm", bpm_edit->get_value());
  425. beats_enabled->set_disabled(false);
  426. beats_edit->set_editable(true);
  427. bar_beats_edit->set_editable(true);
  428. double bpm = bpm_edit->get_value();
  429. if (bpm > 0) {
  430. float beat_size = 60 / float(bpm);
  431. int beat_max = int((stream->get_length() + 0.001) / beat_size);
  432. int current_beat = beats_edit->get_value();
  433. beats_edit->set_max(beat_max);
  434. if (current_beat > beat_max) {
  435. beats_edit->set_value(beat_max);
  436. stream->call("set_beat_count", beat_max);
  437. }
  438. }
  439. stream->call("set_bar_beats", bar_beats_edit->get_value());
  440. } else {
  441. stream->call("set_bpm", 0);
  442. stream->call("set_bar_beats", 4);
  443. beats_enabled->set_disabled(true);
  444. beats_edit->set_editable(false);
  445. bar_beats_edit->set_editable(false);
  446. }
  447. if (bpm_enabled->is_pressed() && beats_enabled->is_pressed()) {
  448. stream->call("set_beat_count", beats_edit->get_value());
  449. } else {
  450. stream->call("set_beat_count", 0);
  451. }
  452. updating_settings = false;
  453. _preview->queue_redraw();
  454. _indicator->queue_redraw();
  455. color_rect->queue_redraw();
  456. }
  457. void AudioStreamImportSettingsDialog::_reimport() {
  458. params["loop"] = loop->is_pressed();
  459. params["loop_offset"] = loop_offset->get_value();
  460. params["bpm"] = bpm_enabled->is_pressed() ? double(bpm_edit->get_value()) : double(0);
  461. params["beat_count"] = (bpm_enabled->is_pressed() && beats_enabled->is_pressed()) ? int(beats_edit->get_value()) : int(0);
  462. params["bar_beats"] = (bpm_enabled->is_pressed()) ? int(bar_beats_edit->get_value()) : int(4);
  463. EditorFileSystem::get_singleton()->reimport_file_with_custom_parameters(path, importer, params);
  464. }
  465. AudioStreamImportSettingsDialog::AudioStreamImportSettingsDialog() {
  466. get_ok_button()->set_text(TTR("Reimport"));
  467. get_cancel_button()->set_text(TTR("Close"));
  468. VBoxContainer *main_vbox = memnew(VBoxContainer);
  469. add_child(main_vbox);
  470. HBoxContainer *loop_hb = memnew(HBoxContainer);
  471. loop_hb->add_theme_constant_override("separation", 4 * EDSCALE);
  472. loop = memnew(CheckBox);
  473. loop->set_text(TTR("Enable"));
  474. loop->set_tooltip_text(TTR("Enable looping."));
  475. loop->connect(SceneStringName(toggled), callable_mp(this, &AudioStreamImportSettingsDialog::_settings_changed).unbind(1));
  476. loop_hb->add_child(loop);
  477. loop_hb->add_spacer();
  478. loop_hb->add_child(memnew(Label(TTR("Offset:"))));
  479. loop_offset = memnew(SpinBox);
  480. loop_offset->set_accessibility_name(TTRC("Offset:"));
  481. loop_offset->set_max(10000);
  482. loop_offset->set_step(0.001);
  483. loop_offset->set_suffix("s");
  484. loop_offset->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  485. loop_offset->set_stretch_ratio(0.33);
  486. loop_offset->set_tooltip_text(TTR("Loop offset (from beginning). Note that if BPM is set, this setting will be ignored."));
  487. loop_offset->connect(SceneStringName(value_changed), callable_mp(this, &AudioStreamImportSettingsDialog::_settings_changed).unbind(1));
  488. loop_hb->add_child(loop_offset);
  489. main_vbox->add_margin_child(TTR("Loop:"), loop_hb);
  490. HBoxContainer *interactive_hb = memnew(HBoxContainer);
  491. interactive_hb->add_theme_constant_override("separation", 4 * EDSCALE);
  492. bpm_enabled = memnew(CheckBox);
  493. bpm_enabled->set_text((TTR("BPM:")));
  494. bpm_enabled->connect(SceneStringName(toggled), callable_mp(this, &AudioStreamImportSettingsDialog::_settings_changed).unbind(1));
  495. interactive_hb->add_child(bpm_enabled);
  496. bpm_edit = memnew(SpinBox);
  497. bpm_edit->set_max(400);
  498. bpm_edit->set_step(0.01);
  499. bpm_edit->set_accessibility_name(TTRC("BPM:"));
  500. 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."));
  501. bpm_edit->connect(SceneStringName(value_changed), callable_mp(this, &AudioStreamImportSettingsDialog::_settings_changed).unbind(1));
  502. interactive_hb->add_child(bpm_edit);
  503. interactive_hb->add_spacer();
  504. beats_enabled = memnew(CheckBox);
  505. beats_enabled->set_text(TTR("Beat Count:"));
  506. beats_enabled->connect(SceneStringName(toggled), callable_mp(this, &AudioStreamImportSettingsDialog::_settings_changed).unbind(1));
  507. interactive_hb->add_child(beats_enabled);
  508. beats_edit = memnew(SpinBox);
  509. 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."));
  510. beats_edit->set_max(99999);
  511. beats_edit->set_accessibility_name(TTRC("Beat Count:"));
  512. beats_edit->connect(SceneStringName(value_changed), callable_mp(this, &AudioStreamImportSettingsDialog::_settings_changed).unbind(1));
  513. interactive_hb->add_child(beats_edit);
  514. bar_beats_label = memnew(Label(TTR("Bar Beats:")));
  515. interactive_hb->add_child(bar_beats_label);
  516. bar_beats_edit = memnew(SpinBox);
  517. bar_beats_edit->set_tooltip_text(TTR("Configure the Beats Per Bar. This used for music-aware transitions between AudioStreams."));
  518. bar_beats_edit->set_min(2);
  519. bar_beats_edit->set_max(32);
  520. bar_beats_edit->set_accessibility_name(TTRC("Bar Beats:"));
  521. bar_beats_edit->connect(SceneStringName(value_changed), callable_mp(this, &AudioStreamImportSettingsDialog::_settings_changed).unbind(1));
  522. interactive_hb->add_child(bar_beats_edit);
  523. main_vbox->add_margin_child(TTR("Music Playback:"), interactive_hb);
  524. color_rect = memnew(ColorRect);
  525. main_vbox->add_margin_child(TTR("Preview:"), color_rect, true);
  526. color_rect->set_custom_minimum_size(Size2(600, 200) * EDSCALE);
  527. color_rect->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  528. _player = memnew(AudioStreamPlayer);
  529. _player->connect(SceneStringName(finished), callable_mp(this, &AudioStreamImportSettingsDialog::_on_finished));
  530. color_rect->add_child(_player);
  531. VBoxContainer *vbox = memnew(VBoxContainer);
  532. vbox->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  533. color_rect->add_child(vbox);
  534. vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  535. _preview = memnew(ColorRect);
  536. _preview->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  537. _preview->connect(SceneStringName(draw), callable_mp(this, &AudioStreamImportSettingsDialog::_draw_preview));
  538. _preview->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  539. vbox->add_child(_preview);
  540. zoom_bar = memnew(HScrollBar);
  541. zoom_bar->hide();
  542. vbox->add_child(zoom_bar);
  543. zoom_bar->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  544. zoom_bar->connect(SceneStringName(value_changed), callable_mp(this, &AudioStreamImportSettingsDialog::_preview_zoom_offset_changed));
  545. HBoxContainer *hbox = memnew(HBoxContainer);
  546. hbox->add_theme_constant_override("separation", 0);
  547. vbox->add_child(hbox);
  548. _indicator = memnew(Control);
  549. _indicator->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  550. _indicator->connect(SceneStringName(draw), callable_mp(this, &AudioStreamImportSettingsDialog::_draw_indicator));
  551. _indicator->connect(SceneStringName(gui_input), callable_mp(this, &AudioStreamImportSettingsDialog::_on_input_indicator));
  552. _indicator->connect(SceneStringName(mouse_exited), callable_mp(this, &AudioStreamImportSettingsDialog::_on_indicator_mouse_exited));
  553. _preview->add_child(_indicator);
  554. _play_button = memnew(Button);
  555. _play_button->set_accessibility_name(TTRC("Play"));
  556. _play_button->set_flat(true);
  557. hbox->add_child(_play_button);
  558. _play_button->connect(SceneStringName(pressed), callable_mp(this, &AudioStreamImportSettingsDialog::_play));
  559. _stop_button = memnew(Button);
  560. _stop_button->set_accessibility_name(TTRC("Stop"));
  561. _stop_button->set_flat(true);
  562. hbox->add_child(_stop_button);
  563. _stop_button->connect(SceneStringName(pressed), callable_mp(this, &AudioStreamImportSettingsDialog::_stop));
  564. _current_label = memnew(Label);
  565. _current_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  566. _current_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  567. _current_label->set_modulate(Color(1, 1, 1, 0.5));
  568. hbox->add_child(_current_label);
  569. _duration_label = memnew(Label);
  570. hbox->add_child(_duration_label);
  571. zoom_in = memnew(Button);
  572. zoom_in->set_accessibility_name(TTRC("Zoom In"));
  573. zoom_in->set_flat(true);
  574. zoom_reset = memnew(Button);
  575. zoom_reset->set_accessibility_name(TTRC("Reset Zoom"));
  576. zoom_reset->set_flat(true);
  577. zoom_out = memnew(Button);
  578. zoom_out->set_accessibility_name(TTRC("Zoom Out"));
  579. zoom_out->set_flat(true);
  580. hbox->add_child(zoom_out);
  581. hbox->add_child(zoom_reset);
  582. hbox->add_child(zoom_in);
  583. zoom_in->connect(SceneStringName(pressed), callable_mp(this, &AudioStreamImportSettingsDialog::_preview_zoom_in));
  584. zoom_reset->connect(SceneStringName(pressed), callable_mp(this, &AudioStreamImportSettingsDialog::_preview_zoom_reset));
  585. zoom_out->connect(SceneStringName(pressed), callable_mp(this, &AudioStreamImportSettingsDialog::_preview_zoom_out));
  586. singleton = this;
  587. }