2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2021-03-21 15:01:33 +03:00
|
|
|
#include "NoVisualizationWidget.h"
|
|
|
|
#include "Player.h"
|
2021-03-26 03:28:56 +03:00
|
|
|
#include "SampleWidget.h"
|
2021-03-21 15:01:33 +03:00
|
|
|
#include "SoundPlayerWidgetAdvancedView.h"
|
2020-02-06 17:18:03 +03:00
|
|
|
#include <LibAudio/ClientConnection.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/Action.h>
|
|
|
|
#include <LibGUI/Application.h>
|
|
|
|
#include <LibGUI/FilePicker.h>
|
|
|
|
#include <LibGUI/Menu.h>
|
2021-04-13 17:18:20 +03:00
|
|
|
#include <LibGUI/Menubar.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/Window.h>
|
2020-09-18 10:49:51 +03:00
|
|
|
#include <LibGfx/CharacterBitmap.h>
|
2019-09-04 21:18:41 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2021-01-17 10:43:58 +03:00
|
|
|
if (pledge("stdio recvfd sendfd accept rpath thread unix cpath fattr", nullptr) < 0) {
|
2020-01-12 04:28:26 +03:00
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-07-04 15:05:19 +03:00
|
|
|
auto app = GUI::Application::construct(argc, argv);
|
2019-09-04 21:18:41 +03:00
|
|
|
|
2021-01-17 10:43:58 +03:00
|
|
|
if (pledge("stdio recvfd sendfd accept rpath thread unix", nullptr) < 0) {
|
2020-01-12 04:28:26 +03:00
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-02-06 12:40:02 +03:00
|
|
|
auto audio_client = Audio::ClientConnection::construct();
|
2019-09-22 01:17:53 +03:00
|
|
|
audio_client->handshake();
|
2021-03-26 03:28:56 +03:00
|
|
|
|
|
|
|
if (pledge("stdio recvfd sendfd accept rpath thread", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-03-21 15:01:33 +03:00
|
|
|
PlaybackManager playback_manager(audio_client);
|
2021-03-24 02:12:50 +03:00
|
|
|
PlayerState initial_player_state { true,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
false,
|
2021-03-26 03:28:56 +03:00
|
|
|
false,
|
|
|
|
44100,
|
2021-03-24 02:12:50 +03:00
|
|
|
1.0,
|
|
|
|
audio_client,
|
|
|
|
playback_manager,
|
|
|
|
"" };
|
2021-03-21 15:01:33 +03:00
|
|
|
|
2020-11-02 22:30:17 +03:00
|
|
|
auto app_icon = GUI::Icon::default_icon("app-sound-player");
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
auto window = GUI::Window::construct();
|
2021-01-05 01:51:49 +03:00
|
|
|
window->set_title("Sound Player");
|
2020-11-02 22:30:17 +03:00
|
|
|
window->set_icon(app_icon.bitmap_for_size(16));
|
2019-11-09 19:15:16 +03:00
|
|
|
|
2021-04-13 17:18:20 +03:00
|
|
|
auto menubar = GUI::Menubar::construct();
|
2019-11-09 19:15:16 +03:00
|
|
|
|
2021-03-21 15:01:33 +03:00
|
|
|
auto& app_menu = menubar->add_menu("File");
|
2021-03-26 03:28:56 +03:00
|
|
|
|
|
|
|
auto& playlist_menu = menubar->add_menu("Playlist");
|
|
|
|
|
|
|
|
String path = argv[1];
|
|
|
|
// start in advanced view by default
|
|
|
|
Player* player = &window->set_main_widget<SoundPlayerWidgetAdvancedView>(window, initial_player_state);
|
2019-11-09 19:15:16 +03:00
|
|
|
if (argc > 1) {
|
2021-03-21 15:01:33 +03:00
|
|
|
player->open_file(path);
|
2019-11-09 19:15:16 +03:00
|
|
|
}
|
|
|
|
|
2020-04-04 13:18:40 +03:00
|
|
|
app_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
|
2020-12-03 01:58:00 +03:00
|
|
|
Optional<String> path = GUI::FilePicker::get_open_filepath(window, "Open sound file...");
|
2019-11-09 19:15:16 +03:00
|
|
|
if (path.has_value()) {
|
2021-03-21 15:01:33 +03:00
|
|
|
player->open_file(path.value());
|
2019-11-09 19:15:16 +03:00
|
|
|
}
|
|
|
|
}));
|
2021-03-21 15:01:33 +03:00
|
|
|
|
2021-03-26 03:26:12 +03:00
|
|
|
auto linear_volume_slider = GUI::Action::create_checkable("Nonlinear volume slider", [&](auto& action) {
|
|
|
|
static_cast<SoundPlayerWidgetAdvancedView*>(player)->set_nonlinear_volume_slider(action.is_checked());
|
2021-03-21 15:01:33 +03:00
|
|
|
});
|
2021-03-26 03:26:12 +03:00
|
|
|
app_menu.add_action(linear_volume_slider);
|
2021-03-21 15:01:33 +03:00
|
|
|
|
2021-03-26 03:26:12 +03:00
|
|
|
auto playlist_toggle = GUI::Action::create_checkable("Show playlist", [&](auto& action) {
|
|
|
|
static_cast<SoundPlayerWidgetAdvancedView*>(player)->set_playlist_visible(action.is_checked());
|
2021-03-21 15:01:33 +03:00
|
|
|
});
|
2021-03-26 03:26:12 +03:00
|
|
|
playlist_menu.add_action(playlist_toggle);
|
|
|
|
if (path.ends_with(".m3u") || path.ends_with(".m3u8"))
|
|
|
|
playlist_toggle->set_checked(true);
|
|
|
|
playlist_menu.add_separator();
|
2021-03-21 15:01:33 +03:00
|
|
|
|
2021-03-26 03:26:12 +03:00
|
|
|
auto playlist_loop_toggle = GUI::Action::create_checkable("Loop playlist", [&](auto& action) {
|
|
|
|
static_cast<SoundPlayerWidgetAdvancedView*>(player)->set_looping_playlist(action.is_checked());
|
2021-03-21 15:01:33 +03:00
|
|
|
});
|
2021-03-26 03:26:12 +03:00
|
|
|
playlist_menu.add_action(playlist_loop_toggle);
|
2021-03-21 15:01:33 +03:00
|
|
|
|
2020-04-04 13:18:40 +03:00
|
|
|
app_menu.add_separator();
|
|
|
|
app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) {
|
2020-07-04 15:05:19 +03:00
|
|
|
app->quit();
|
2019-11-04 21:45:19 +03:00
|
|
|
}));
|
2019-09-04 21:18:41 +03:00
|
|
|
|
2020-11-01 09:19:49 +03:00
|
|
|
auto& playback_menu = menubar->add_menu("Playback");
|
|
|
|
|
|
|
|
auto loop = GUI::Action::create_checkable("Loop", { Mod_Ctrl, Key_R }, [&](auto& action) {
|
2021-03-26 03:28:56 +03:00
|
|
|
player->set_looping_file(action.is_checked());
|
2020-11-01 09:19:49 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
playback_menu.add_action(move(loop));
|
|
|
|
|
2021-03-21 15:01:33 +03:00
|
|
|
auto& visualization_menu = menubar->add_menu("Visualization");
|
|
|
|
Vector<NonnullRefPtr<GUI::Action>> visualization_checkmarks;
|
|
|
|
GUI::Action* checked_vis = nullptr;
|
|
|
|
auto uncheck_all_but = [&](GUI::Action& one) {for (auto& a : visualization_checkmarks) if (a != &one) a->set_checked(false); };
|
|
|
|
|
|
|
|
auto bars = GUI::Action::create_checkable("Bars", [&](auto& action) {
|
|
|
|
uncheck_all_but(action);
|
|
|
|
if (checked_vis == &action) {
|
|
|
|
action.set_checked(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
checked_vis = &action;
|
|
|
|
static_cast<SoundPlayerWidgetAdvancedView*>(player)->set_visualization<BarsVisualizationWidget>();
|
|
|
|
});
|
|
|
|
bars->set_checked(true);
|
|
|
|
|
|
|
|
visualization_menu.add_action(bars);
|
|
|
|
visualization_checkmarks.append(bars);
|
|
|
|
|
|
|
|
auto samples = GUI::Action::create_checkable("Samples", [&](auto& action) {
|
|
|
|
uncheck_all_but(action);
|
|
|
|
if (checked_vis == &action) {
|
|
|
|
action.set_checked(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
checked_vis = &action;
|
|
|
|
static_cast<SoundPlayerWidgetAdvancedView*>(player)->set_visualization<SampleWidget>();
|
|
|
|
});
|
|
|
|
|
|
|
|
visualization_menu.add_action(samples);
|
|
|
|
visualization_checkmarks.append(samples);
|
|
|
|
|
|
|
|
auto none = GUI::Action::create_checkable("None", [&](auto& action) {
|
|
|
|
uncheck_all_but(action);
|
|
|
|
if (checked_vis == &action) {
|
|
|
|
action.set_checked(true);
|
|
|
|
return;
|
|
|
|
}
|
2021-03-26 03:26:12 +03:00
|
|
|
checked_vis = &action;
|
2021-03-21 15:01:33 +03:00
|
|
|
static_cast<SoundPlayerWidgetAdvancedView*>(player)->set_visualization<NoVisualizationWidget>();
|
|
|
|
});
|
|
|
|
|
|
|
|
visualization_menu.add_action(none);
|
|
|
|
visualization_checkmarks.append(none);
|
|
|
|
|
2020-04-04 13:18:40 +03:00
|
|
|
auto& help_menu = menubar->add_menu("Help");
|
2021-01-05 01:51:49 +03:00
|
|
|
help_menu.add_action(GUI::CommonActions::make_about_action("Sound Player", app_icon, window));
|
2019-11-09 20:03:12 +03:00
|
|
|
|
2021-03-25 23:41:39 +03:00
|
|
|
window->set_menubar(move(menubar));
|
2019-09-04 21:18:41 +03:00
|
|
|
|
|
|
|
window->show();
|
2020-07-04 15:05:19 +03:00
|
|
|
return app->exec();
|
2019-09-04 21:18:41 +03:00
|
|
|
}
|