Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "playbackcommandscontroller.h"
24 :
25 : #include <vector>
26 :
27 : #include "rcommand/commandtypes.h"
28 : #include "audio/common/audiotypes.h"
29 :
30 : #include "../playbackcommands.h"
31 :
32 : using namespace muse;
33 : using namespace muse::rcommand;
34 : using namespace muse::audio;
35 : using namespace mu::playback;
36 :
37 0 : void PlaybackCommandsController::init()
38 : {
39 0 : registerCommand(PLAY_TOGGLE_COMMAND, &IPlaybackController::togglePlay);
40 0 : registerCommand(PLAY_COMMAND, &IPlaybackController::play, false);
41 0 : registerCommand(PLAY_SELECTION_COMMAND, &IPlaybackController::playFromSelection, false);
42 0 : registerCommand(PAUSE_COMMAND, &IPlaybackController::pause, false);
43 0 : registerCommand(PAUSE_AND_SELECT_COMMAND, &IPlaybackController::pause, true);
44 0 : registerCommand(STOP_COMMAND, &IPlaybackController::stop);
45 0 : registerCommand(REWIND_COMMAND, [this](const rcommand::Params& params) { return rewind(params); });
46 0 : registerCommand(LOOP_TOGGLE_COMMAND, &IPlaybackController::toggleLoopPlayback);
47 0 : registerCommand(LOOP_IN_COMMAND, &IPlaybackController::addLoopBoundary, LoopBoundaryType::LoopIn);
48 0 : registerCommand(LOOP_OUT_COMMAND, &IPlaybackController::addLoopBoundary, LoopBoundaryType::LoopOut);
49 0 : registerCommand(METRONOME_TOGGLE_COMMAND, &IPlaybackController::toggleMetronome);
50 0 : registerCommand(MIDI_TOGGLE_COMMAND, &IPlaybackController::toggleMidiInput);
51 0 : registerCommand(MIDI_INPUT_WRITTEN_PITCH_COMMAND, &IPlaybackController::setMidiUseWrittenPitch, true);
52 0 : registerCommand(MIDI_INPUT_SOUNDING_PITCH_COMMAND, &IPlaybackController::setMidiUseWrittenPitch, false);
53 0 : registerCommand(REPEATS_TOGGLE_COMMAND, &IPlaybackController::togglePlayRepeats);
54 0 : registerCommand(CHORDSYMBOLS_TOGGLE_COMMAND, &IPlaybackController::togglePlayChordSymbols);
55 0 : registerCommand(HEAR_PLAYBACK_WHEN_EDITING_TOGGLE_COMMAND, &IPlaybackController::toggleHearPlaybackWhenEditing);
56 0 : registerCommand(PAN_TOGGLE_COMMAND, &IPlaybackController::toggleAutomaticallyPan);
57 0 : registerCommand(COUNTIN_TOGGLE_COMMAND, &IPlaybackController::toggleCountIn);
58 0 : registerCommand(OPEN_PLAYBACK_SETUP_COMMAND, [this]() { return showPlaybackSetup(); });
59 0 : registerCommand(RELOAD_PLAYBACK_CACHE_COMMAND, &IPlaybackController::reloadPlaybackCache);
60 :
61 0 : registerCommand(TOGGLE_MIXER_SECTION_COMMAND, [this](const rcommand::Params& params) { return toggleMixerSection(params); });
62 0 : registerCommand(TOGGLE_AUX_SEND_COMMAND, [this](const rcommand::Params& params) { return toggleAuxSend(params); });
63 0 : registerCommand(TOGGLE_AUX_CHANNEL_COMMAND, [this](const rcommand::Params& params) { return toggleAuxChannel(params); });
64 0 : }
65 :
66 0 : muse::Ret PlaybackCommandsController::rewind(const muse::rcommand::Params& params)
67 : {
68 0 : double secs = params.at("position").toDouble();
69 0 : return playbackController()->rewind(secs_t(secs));
70 : }
71 :
72 0 : muse::Ret PlaybackCommandsController::showPlaybackSetup()
73 : {
74 0 : interactive()->open("musescore://playback/soundprofiles");
75 0 : return make_ok();
76 : }
77 :
78 0 : muse::Ret PlaybackCommandsController::toggleMixerSection(const muse::rcommand::Params& params)
79 : {
80 0 : MixerSectionType section = str_conv(params.at("section").toString(), MixerSectionType::Unknown);
81 0 : if (section == MixerSectionType::Unknown) {
82 0 : return make_ret(Ret::Code::BadArgs);
83 : }
84 :
85 0 : bool visible = configuration()->isMixerSectionVisible(section);
86 0 : configuration()->setMixerSectionVisible(section, !visible);
87 0 : return make_ok();
88 : }
89 :
90 0 : muse::Ret PlaybackCommandsController::toggleAuxSend(const muse::rcommand::Params& params)
91 : {
92 0 : if (!params.contains("auxsend-index")) {
93 0 : return make_ret(Ret::Code::BadArgs);
94 : }
95 :
96 0 : aux_channel_idx_t auxSendIndex = static_cast<aux_channel_idx_t>(params.at("auxsend-index").toInt());
97 0 : bool visible = configuration()->isAuxSendVisible(auxSendIndex);
98 0 : configuration()->setAuxSendVisible(auxSendIndex, !visible);
99 0 : return make_ok();
100 : }
101 :
102 0 : muse::Ret PlaybackCommandsController::toggleAuxChannel(const muse::rcommand::Params& params)
103 : {
104 0 : if (!params.contains("auxchannel-index")) {
105 0 : return make_ret(Ret::Code::BadArgs);
106 : }
107 :
108 0 : aux_channel_idx_t auxChannelIndex = static_cast<aux_channel_idx_t>(params.at("auxchannel-index").toInt());
109 0 : bool visible = configuration()->isAuxChannelVisible(auxChannelIndex);
110 0 : configuration()->setAuxChannelVisible(auxChannelIndex, !visible);
111 0 : return make_ok();
112 : }
113 :
114 0 : void PlaybackCommandsController::registerCommand(const muse::rcommand::Command& command, const std::function<Ret()>& handler)
115 : {
116 0 : dispatcher()->onRequest(this, command, [this, command, handler]() {
117 0 : if (!commandsState()->commandState(command).enabled) {
118 0 : return muse::make_ret(Ret::Code::NotSupported);
119 : }
120 :
121 0 : return handler();
122 : });
123 0 : }
124 :
125 0 : void PlaybackCommandsController::registerCommand(const muse::rcommand::Command& command,
126 : const std::function<muse::Ret(const muse::rcommand::Params&)>& handler)
127 : {
128 0 : dispatcher()->onRequest(this, command, [this, command, handler](const muse::rcommand::Params& params) {
129 0 : if (!commandsState()->commandState(command).enabled) {
130 0 : return muse::make_ret(Ret::Code::NotSupported);
131 : }
132 :
133 0 : return handler(params);
134 : });
135 0 : }
136 :
137 0 : void PlaybackCommandsController::registerCommand(const muse::rcommand::Command& command, Ret (IPlaybackController::* handler)())
138 : {
139 0 : registerCommand(command, [this, handler]()
140 : {
141 0 : return (playbackController().get()->*handler)();
142 : });
143 0 : }
144 :
145 : template<typename P1>
146 0 : void PlaybackCommandsController::registerCommand(const muse::rcommand::Command& command, Ret (IPlaybackController::* handler)(P1), P1 p1)
147 : {
148 0 : registerCommand(command, [this, handler, p1]()
149 : {
150 0 : return (playbackController().get()->*handler)(p1);
151 : });
152 0 : }
|