summaryrefslogtreecommitdiff
path: root/Userland/Applications/SoundPlayer/main.cpp
blob: 5441a538dcfc67ddd022c5aad57360e365dc93a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "NoVisualizationWidget.h"
#include "Player.h"
#include "SampleWidget.h"
#include "SoundPlayerWidgetAdvancedView.h"
#include <LibAudio/ClientConnection.h>
#include <LibGUI/Action.h>
#include <LibGUI/Application.h>
#include <LibGUI/FilePicker.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Menubar.h>
#include <LibGUI/Window.h>
#include <LibGfx/CharacterBitmap.h>
#include <stdio.h>

int main(int argc, char** argv)
{
    if (pledge("stdio recvfd sendfd rpath thread unix", nullptr) < 0) {
        perror("pledge");
        return 1;
    }

    auto app = GUI::Application::construct(argc, argv);
    auto audio_client = Audio::ClientConnection::construct();

    if (pledge("stdio recvfd sendfd rpath thread", nullptr) < 0) {
        perror("pledge");
        return 1;
    }

    PlaybackManager playback_manager(audio_client);
    PlayerState initial_player_state { true,
        true,
        false,
        false,
        false,
        44100,
        1.0,
        audio_client,
        playback_manager,
        "" };

    auto app_icon = GUI::Icon::default_icon("app-sound-player");

    auto window = GUI::Window::construct();
    window->set_title("Sound Player");
    window->set_icon(app_icon.bitmap_for_size(16));

    auto& file_menu = window->add_menu("&File");

    auto& playlist_menu = window->add_menu("Play&list");

    String path = argv[1];
    // start in advanced view by default
    Player* player = &window->set_main_widget<SoundPlayerWidgetAdvancedView>(window, initial_player_state);
    if (argc > 1) {
        player->open_file(path);
    }

    file_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
        Optional<String> path = GUI::FilePicker::get_open_filepath(window, "Open sound file...");
        if (path.has_value()) {
            player->open_file(path.value());
        }
    }));

    auto linear_volume_slider = GUI::Action::create_checkable("&Nonlinear Volume Slider", [&](auto& action) {
        static_cast<SoundPlayerWidgetAdvancedView*>(player)->set_nonlinear_volume_slider(action.is_checked());
    });
    file_menu.add_action(linear_volume_slider);

    auto playlist_toggle = GUI::Action::create_checkable("&Show Playlist", [&](auto& action) {
        static_cast<SoundPlayerWidgetAdvancedView*>(player)->set_playlist_visible(action.is_checked());
    });
    playlist_menu.add_action(playlist_toggle);
    if (path.ends_with(".m3u") || path.ends_with(".m3u8"))
        playlist_toggle->set_checked(true);
    playlist_menu.add_separator();

    auto playlist_loop_toggle = GUI::Action::create_checkable("&Loop Playlist", [&](auto& action) {
        static_cast<SoundPlayerWidgetAdvancedView*>(player)->set_looping_playlist(action.is_checked());
    });
    playlist_menu.add_action(playlist_loop_toggle);

    file_menu.add_separator();
    file_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) {
        app->quit();
    }));

    auto& playback_menu = window->add_menu("&Playback");

    auto loop = GUI::Action::create_checkable("&Loop", { Mod_Ctrl, Key_R }, [&](auto& action) {
        player->set_looping_file(action.is_checked());
    });

    playback_menu.add_action(move(loop));

    auto& visualization_menu = window->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;
        }
        checked_vis = &action;
        static_cast<SoundPlayerWidgetAdvancedView*>(player)->set_visualization<NoVisualizationWidget>();
    });

    visualization_menu.add_action(none);
    visualization_checkmarks.append(none);

    auto& help_menu = window->add_menu("&Help");
    help_menu.add_action(GUI::CommonActions::make_about_action("Sound Player", app_icon, window));

    window->show();
    return app->exec();
}