2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2020-01-30 13:02:45 -03:00
|
|
|
* Copyright (c) 2019-2020, William McPherson <willmcpherson2@gmail.com>
|
2022-01-13 08:07:00 -03:00
|
|
|
* Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
|
2021-07-10 23:58:17 -03:00
|
|
|
* Copyright (c) 2021, JJ Roberts-White <computerfido@gmail.com>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2021-07-10 23:58:17 -03:00
|
|
|
#include "AudioPlayerLoop.h"
|
2020-01-30 13:02:45 -03:00
|
|
|
#include "MainWidget.h"
|
2020-06-15 02:33:53 -03:00
|
|
|
#include "TrackManager.h"
|
2021-07-05 09:48:59 -03:00
|
|
|
#include <AK/Queue.h>
|
2022-07-17 06:31:01 -03:00
|
|
|
#include <LibAudio/ConnectionToServer.h>
|
2020-02-06 10:32:46 -03:00
|
|
|
#include <LibAudio/WavWriter.h>
|
2020-02-16 05:17:49 -03:00
|
|
|
#include <LibCore/EventLoop.h>
|
2021-11-23 06:59:50 -03:00
|
|
|
#include <LibCore/System.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Action.h>
|
|
|
|
|
#include <LibGUI/Application.h>
|
|
|
|
|
#include <LibGUI/FilePicker.h>
|
2020-02-14 21:56:30 -03:00
|
|
|
#include <LibGUI/Menu.h>
|
2021-04-13 11:18:20 -03:00
|
|
|
#include <LibGUI/Menubar.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/MessageBox.h>
|
|
|
|
|
#include <LibGUI/Window.h>
|
2021-11-22 19:24:55 -03:00
|
|
|
#include <LibMain/Main.h>
|
2019-07-13 12:05:16 -03:00
|
|
|
|
2021-11-22 19:24:55 -03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-07-13 12:05:16 -03:00
|
|
|
{
|
2021-11-27 19:26:34 -03:00
|
|
|
TRY(Core::System::pledge("stdio thread rpath cpath wpath recvfd sendfd unix"));
|
2020-11-02 18:34:29 -03:00
|
|
|
|
2022-04-16 13:17:39 -03:00
|
|
|
auto app = TRY(GUI::Application::try_create(arguments));
|
2020-01-30 13:02:45 -03:00
|
|
|
|
2020-06-15 02:33:53 -03:00
|
|
|
TrackManager track_manager;
|
2020-01-30 13:02:45 -03:00
|
|
|
|
2020-02-06 10:32:46 -03:00
|
|
|
Audio::WavWriter wav_writer;
|
|
|
|
|
Optional<String> save_path;
|
|
|
|
|
bool need_to_write_wav = false;
|
|
|
|
|
|
2021-07-05 09:48:59 -03:00
|
|
|
auto audio_loop = AudioPlayerLoop::construct(track_manager, need_to_write_wav, wav_writer);
|
2019-07-13 12:05:16 -03:00
|
|
|
|
2022-07-11 14:32:29 -03:00
|
|
|
auto app_icon = GUI::Icon::default_icon("app-piano"sv);
|
2021-07-10 23:58:17 -03:00
|
|
|
auto window = GUI::Window::construct();
|
2022-01-07 11:09:02 -03:00
|
|
|
auto main_widget = TRY(window->try_set_main_widget<MainWidget>(track_manager, audio_loop));
|
2021-07-10 23:58:17 -03:00
|
|
|
window->set_title("Piano");
|
|
|
|
|
window->resize(840, 600);
|
|
|
|
|
window->set_icon(app_icon.bitmap_for_size(16));
|
|
|
|
|
|
2021-07-05 09:48:59 -03:00
|
|
|
auto main_widget_updater = Core::Timer::construct(static_cast<int>((1 / 60.0) * 1000), [&] {
|
2021-04-19 14:10:24 -03:00
|
|
|
Core::EventLoop::current().post_event(main_widget, make<Core::CustomEvent>(0));
|
|
|
|
|
});
|
|
|
|
|
main_widget_updater->start();
|
|
|
|
|
|
2021-07-21 16:21:03 -03:00
|
|
|
auto& file_menu = window->add_menu("&File");
|
2022-07-11 14:32:29 -03:00
|
|
|
file_menu.add_action(GUI::Action::create("Export", { Mod_Ctrl, Key_E }, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/file-export.png"sv)), [&](const GUI::Action&) {
|
2020-07-15 22:52:02 -03:00
|
|
|
save_path = GUI::FilePicker::get_save_filepath(window, "Untitled", "wav");
|
2020-02-06 10:32:46 -03:00
|
|
|
if (!save_path.has_value())
|
|
|
|
|
return;
|
|
|
|
|
wav_writer.set_file(save_path.value());
|
|
|
|
|
if (wav_writer.has_error()) {
|
2022-07-11 14:32:29 -03:00
|
|
|
GUI::MessageBox::show(window, String::formatted("Failed to export WAV file: {}", wav_writer.error_string()), "Error"sv, GUI::MessageBox::Type::Error);
|
2020-02-06 10:32:46 -03:00
|
|
|
wav_writer.clear_error();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
need_to_write_wav = true;
|
|
|
|
|
}));
|
2021-05-01 05:45:39 -03:00
|
|
|
file_menu.add_separator();
|
|
|
|
|
file_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
|
2020-07-07 14:48:25 -03:00
|
|
|
GUI::Application::the()->quit();
|
|
|
|
|
return;
|
|
|
|
|
}));
|
|
|
|
|
|
2021-07-21 16:21:03 -03:00
|
|
|
auto& edit_menu = window->add_menu("&Edit");
|
2022-01-07 11:09:02 -03:00
|
|
|
main_widget->add_track_actions(edit_menu);
|
2019-07-27 06:06:00 -03:00
|
|
|
|
2021-07-21 16:21:03 -03:00
|
|
|
auto& help_menu = window->add_menu("&Help");
|
2021-01-04 19:51:49 -03:00
|
|
|
help_menu.add_action(GUI::CommonActions::make_about_action("Piano", app_icon, window));
|
2019-12-30 19:12:52 -03:00
|
|
|
|
2021-07-28 07:32:14 -03:00
|
|
|
window->show();
|
|
|
|
|
|
2020-07-04 09:05:19 -03:00
|
|
|
return app->exec();
|
2019-07-13 12:05:16 -03:00
|
|
|
}
|