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) 2021 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 "mixerpanelmodel.h"
24 :
25 : #include "async/notifylist.h"
26 : #include "defer.h"
27 : #include "log.h"
28 : #include "translation.h"
29 :
30 : #include "notation/imasternotation.h"
31 : #include "notation/inotationautomation.h"
32 : #include "notation/inotationparts.h"
33 : #include "notation/inotationplayback.h"
34 :
35 : #include "project/inotationproject.h"
36 :
37 : using namespace muse;
38 : using namespace mu::playback;
39 : using namespace muse::audio;
40 : using namespace mu::engraving;
41 : using namespace mu::notation;
42 : using namespace mu::project;
43 :
44 : static constexpr int INVALID_INDEX = -1;
45 :
46 0 : static ControlParams withEnvelopesOf(ControlParams params, const ControlParams& current)
47 : {
48 : // Automated controls are disabled, so an envelope is never the param being edited
49 0 : if (current.volume.hasAutomation()) {
50 0 : params.volume = current.volume;
51 : }
52 0 : if (current.balance.hasAutomation()) {
53 0 : params.balance = current.balance;
54 : }
55 0 : return params;
56 : }
57 :
58 0 : MixerPanelModel::MixerPanelModel(QObject* parent)
59 0 : : QAbstractListModel(parent), muse::Contextable(muse::iocCtxForQmlObject(this))
60 : {
61 0 : }
62 :
63 0 : void MixerPanelModel::componentComplete()
64 : {
65 0 : init();
66 0 : }
67 :
68 0 : void MixerPanelModel::init()
69 : {
70 : //! NOTE Must be set from Qml
71 0 : DO_ASSERT(m_navigationSection);
72 :
73 0 : controller()->playbackInitedChanged().onReceive(this, [this](bool) {
74 0 : reload();
75 0 : });
76 :
77 0 : controller()->trackAdded().onReceive(this, [this](const TrackId trackId) {
78 0 : onTrackAdded(trackId);
79 0 : });
80 :
81 0 : controller()->trackRemoved().onReceive(this, [this](const TrackId trackId) {
82 0 : removeItem(trackId);
83 0 : });
84 :
85 0 : reload();
86 0 : }
87 :
88 0 : void MixerPanelModel::reload()
89 : {
90 0 : TRACEFUNC;
91 :
92 0 : reloadItems();
93 0 : }
94 :
95 0 : QVariantMap MixerPanelModel::get(int index)
96 : {
97 0 : QVariantMap result;
98 :
99 0 : QHash<int, QByteArray> names = roleNames();
100 0 : QHashIterator<int, QByteArray> i(names);
101 0 : while (i.hasNext()) {
102 0 : i.next();
103 0 : QModelIndex idx = this->index(index, 0);
104 0 : QVariant data = idx.data(i.key());
105 0 : result[i.value()] = data;
106 0 : }
107 :
108 0 : return result;
109 0 : }
110 :
111 0 : QVariant MixerPanelModel::data(const QModelIndex& index, int role) const
112 : {
113 0 : if (!index.isValid() || index.row() >= rowCount() || role != ChannelItemRole) {
114 0 : return QVariant();
115 : }
116 :
117 0 : return QVariant::fromValue(m_mixerChannelList.at(index.row()));
118 : }
119 :
120 0 : int MixerPanelModel::rowCount(const QModelIndex&) const
121 : {
122 0 : return m_mixerChannelList.count();
123 : }
124 :
125 0 : QHash<int, QByteArray> MixerPanelModel::roleNames() const
126 : {
127 : static const QHash<int, QByteArray> roles = {
128 0 : { ChannelItemRole, "channelItem" }
129 0 : };
130 :
131 0 : return roles;
132 0 : }
133 :
134 0 : void MixerPanelModel::reloadItems()
135 : {
136 0 : TRACEFUNC;
137 :
138 0 : beginResetModel();
139 :
140 0 : DEFER {
141 0 : endResetModel();
142 0 : emit rowCountChanged();
143 0 : };
144 :
145 0 : clear();
146 :
147 0 : if (!controller()->isPlaybackInited()) {
148 0 : return;
149 : }
150 :
151 0 : const auto& instrumentTrackIdMap = controller()->instrumentTrackIdMap();
152 :
153 0 : auto addInstrumentTrack = [this, &instrumentTrackIdMap](const InstrumentTrackId& instrumentTrackId, bool isPrimary = true) {
154 0 : auto search = instrumentTrackIdMap.find(instrumentTrackId);
155 0 : if (search == instrumentTrackIdMap.cend()) {
156 0 : return;
157 : }
158 :
159 0 : m_mixerChannelList.push_back(buildInstrumentChannelItem(search->second, instrumentTrackId, isPrimary));
160 0 : };
161 :
162 0 : async::NotifyList<const Part*> partList = masterNotationParts()->partList();
163 0 : for (const Part* part : partList) {
164 0 : std::string primaryInstrId = part->instrument()->id().toStdString();
165 :
166 0 : for (const InstrumentTrackId& instrumentTrackId : part->instrumentTrackIdList()) {
167 0 : bool isPrimary = instrumentTrackId.instrumentId == primaryInstrId;
168 0 : addInstrumentTrack(instrumentTrackId, isPrimary);
169 0 : }
170 0 : }
171 0 : for (auto it = instrumentTrackIdMap.cbegin(); it != instrumentTrackIdMap.cend(); ++it) {
172 0 : if (notationPlayback()->isChordSymbolsTrack(it->first)) {
173 0 : addInstrumentTrack(it->first);
174 : }
175 : }
176 :
177 0 : addInstrumentTrack(notationPlayback()->metronomeTrackId());
178 :
179 0 : const auto& auxTrackIdMap = controller()->auxTrackIdMap();
180 0 : for (auto it = auxTrackIdMap.cbegin(); it != auxTrackIdMap.cend(); ++it) {
181 0 : if (configuration()->isAuxChannelVisible(it->first)) {
182 0 : m_mixerChannelList.push_back(buildAuxChannelItem(it->first, it->second));
183 : }
184 : }
185 :
186 0 : m_masterChannelItem = buildMasterChannelItem();
187 0 : m_mixerChannelList.append(m_masterChannelItem);
188 :
189 0 : updateItemsPanelsOrder();
190 0 : setupConnections();
191 0 : }
192 :
193 0 : void MixerPanelModel::onTrackAdded(const TrackId& trackId)
194 : {
195 0 : TRACEFUNC;
196 :
197 0 : const IPlaybackController::InstrumentTrackIdMap& instrumentTracks = controller()->instrumentTrackIdMap();
198 0 : auto instrumentIt = std::find_if(instrumentTracks.cbegin(), instrumentTracks.cend(), [trackId](const auto& pair) {
199 0 : return pair.second == trackId;
200 : });
201 :
202 0 : if (instrumentIt != instrumentTracks.end()) {
203 0 : const InstrumentTrackId& instrumentTrackId = instrumentIt->first;
204 0 : const Part* part = masterNotationParts()->part(instrumentTrackId.partId);
205 0 : bool isPrimary = part ? part->instrument()->id() == instrumentTrackId.instrumentId : true;
206 0 : MixerChannelItem* item = buildInstrumentChannelItem(trackId, instrumentTrackId, isPrimary);
207 0 : int index = resolveInsertIndex(instrumentTrackId);
208 :
209 0 : addItem(item, index);
210 0 : return;
211 : }
212 :
213 0 : const IPlaybackController::AuxTrackIdMap& auxTracks = controller()->auxTrackIdMap();
214 0 : auto auxIt = std::find_if(auxTracks.begin(), auxTracks.end(), [trackId](const auto& pair) {
215 0 : return pair.second == trackId;
216 : });
217 :
218 0 : if (auxIt != auxTracks.end()) {
219 0 : if (configuration()->isAuxChannelVisible(auxIt->first)) {
220 0 : addItem(buildAuxChannelItem(auxIt->first, trackId), m_mixerChannelList.size() - 1);
221 : }
222 : }
223 0 : }
224 :
225 0 : void MixerPanelModel::addItem(MixerChannelItem* item, int index)
226 : {
227 0 : TRACEFUNC;
228 :
229 0 : IF_ASSERT_FAILED(item) {
230 0 : return;
231 : }
232 :
233 0 : beginInsertRows(QModelIndex(), index, index);
234 0 : m_mixerChannelList.insert(index, item);
235 0 : updateItemsPanelsOrder();
236 0 : endInsertRows();
237 :
238 0 : emit rowCountChanged();
239 0 : }
240 :
241 0 : void MixerPanelModel::removeItem(const TrackId trackId)
242 : {
243 0 : TRACEFUNC;
244 :
245 0 : int index = indexOf(trackId);
246 0 : if (index == INVALID_INDEX) {
247 0 : return;
248 : }
249 :
250 0 : beginRemoveRows(QModelIndex(), index, index);
251 :
252 0 : m_mixerChannelList.removeAt(index);
253 0 : updateItemsPanelsOrder();
254 :
255 0 : endRemoveRows();
256 :
257 0 : updateOutputResourceItemCount();
258 :
259 0 : emit rowCountChanged();
260 0 : }
261 :
262 0 : void MixerPanelModel::updateItemsPanelsOrder()
263 : {
264 0 : TRACEFUNC;
265 :
266 0 : for (int i = 0; i < m_mixerChannelList.size(); i++) {
267 0 : m_mixerChannelList[i]->setPanelOrder(m_navigationOrderStart + i);
268 : }
269 0 : }
270 :
271 0 : void MixerPanelModel::clear()
272 : {
273 0 : TRACEFUNC;
274 :
275 0 : m_masterChannelItem = nullptr;
276 0 : qDeleteAll(m_mixerChannelList);
277 0 : m_mixerChannelList.clear();
278 0 : }
279 :
280 0 : void MixerPanelModel::setupConnections()
281 : {
282 0 : audioSettings()->auxSoloMuteStateChanged().onReceive(
283 0 : this, [this](const aux_channel_idx_t index,
284 : notation::INotationSoloMuteState::SoloMuteState newSoloMuteState) {
285 0 : const IPlaybackController::AuxTrackIdMap& auxTrackIdMap = controller()->auxTrackIdMap();
286 0 : TrackId trackId = muse::value(auxTrackIdMap, index);
287 :
288 0 : if (MixerChannelItem* item = findChannelItem(trackId)) {
289 0 : item->loadSoloMuteState(newSoloMuteState);
290 : }
291 0 : });
292 :
293 0 : playback()->sourceParamsChanged().onReceive(this, [this](const TrackId trackId, const AudioSourceParams& params) {
294 0 : if (MixerChannelItem* item = findChannelItem(trackId)) {
295 0 : item->loadInputParams(params);
296 : }
297 0 : });
298 :
299 0 : playback()->fxChainParamsChanged().onReceive(this, [this](const TrackId trackId, const AudioFxChain& params) {
300 0 : if (MixerChannelItem* item = findChannelItem(trackId)) {
301 0 : AudioOutputParams outParams = audioSettings()->trackOutputParams(item->instrumentTrackId());
302 0 : outParams.fxChain = params;
303 0 : loadOutputParams(item, outParams);
304 0 : }
305 0 : });
306 :
307 0 : playback()->masterFxChainParamsChanged().onReceive(this, [this](const AudioFxChain& params) {
308 0 : if (m_masterChannelItem) {
309 0 : AudioOutputParams outParams = audioSettings()->masterAudioOutputParams();
310 0 : outParams.fxChain = params;
311 0 : loadOutputParams(m_masterChannelItem, outParams);
312 0 : }
313 0 : }, Asyncable::Mode::SetReplace);
314 :
315 0 : controller()->auxChannelNameChanged().onReceive(this, [this](aux_channel_idx_t index, const std::string& name) {
316 0 : for (MixerChannelItem* item : m_mixerChannelList) {
317 0 : const QMap<aux_channel_idx_t, AuxSendItem*>& items = item->auxSendItems();
318 0 : auto it = items.find(index);
319 :
320 0 : if (it != items.end()) {
321 0 : it.value()->setTitle(QString::fromStdString(name));
322 : }
323 : }
324 0 : });
325 :
326 0 : configuration()->isAuxChannelVisibleChanged().onReceive(this, [this](aux_channel_idx_t index, bool visible) {
327 0 : const auto& auxMap = controller()->auxTrackIdMap();
328 0 : TrackId trackId = muse::value(auxMap, index);
329 0 : if (visible) {
330 0 : int visibleAuxesOnRight = 0;
331 :
332 0 : for (const auto& aux : auxMap) {
333 0 : if (configuration()->isAuxChannelVisible(aux.first) && (aux.first > index)) {
334 0 : visibleAuxesOnRight++;
335 : }
336 : }
337 0 : addItem(buildAuxChannelItem(index, trackId), masterChannelIndex() - visibleAuxesOnRight);
338 : } else {
339 0 : removeItem(trackId);
340 : }
341 0 : });
342 :
343 0 : subscribeOnAutomationChanges();
344 0 : }
345 :
346 0 : void MixerPanelModel::subscribeOnAutomationChanges()
347 : {
348 0 : if (!currentProject()) {
349 0 : return;
350 : }
351 :
352 0 : AutomationDataConstPtr automation = currentProject()->masterNotation()->automation()->automationData();
353 0 : if (!automation) {
354 0 : return;
355 : }
356 :
357 0 : automation->changed().onReceive(this, [this](const AutomationChanges& changes) {
358 0 : if (changes.isFullReset) {
359 0 : for (MixerChannelItem* item : m_mixerChannelList) {
360 0 : item->updateHasAutomationFlags();
361 : }
362 0 : return;
363 : }
364 :
365 0 : InstrumentTrackIdSet affectedTrackIds;
366 0 : bool masterAffected = false;
367 :
368 0 : for (const AutomationCurveKey& key : changes.affectedKeys) {
369 0 : if (key.type != AutomationType::Volume && key.type != AutomationType::Pan) {
370 0 : continue;
371 : }
372 :
373 0 : if (key.isGlobal()) {
374 0 : masterAffected = true;
375 0 : } else if (const std::optional<InstrumentTrackId> trackId = key.trackId()) {
376 0 : affectedTrackIds.insert(*trackId);
377 0 : }
378 : }
379 :
380 0 : for (MixerChannelItem* item : m_mixerChannelList) {
381 0 : const bool affected = item->type() == MixerChannelItem::Type::Master
382 0 : ? masterAffected
383 0 : : muse::contains(affectedTrackIds, item->instrumentTrackId());
384 0 : if (affected) {
385 0 : item->updateHasAutomationFlags();
386 : }
387 : }
388 0 : });
389 0 : }
390 :
391 0 : int MixerPanelModel::resolveInsertIndex(const engraving::InstrumentTrackId& newInstrumentTrackId) const
392 : {
393 0 : const InstrumentTrackId& metronomeTrackId = notationPlayback()->metronomeTrackId();
394 0 : if (newInstrumentTrackId == metronomeTrackId) {
395 0 : return masterChannelIndex();
396 : }
397 :
398 : // Assumptions:
399 : // - the last channel is always the master channel
400 : // - metronome channel is placed to the immediate left of the master (or auxes if visible)
401 : // - the InstrumentTrackIds from the mixer channel items are always a correctly
402 : // sorted subset of the InstrumentTrackIds from NotationParts
403 0 : if (notationPlayback()->isChordSymbolsTrack(newInstrumentTrackId)) {
404 0 : int metronomeIdx = 0;
405 0 : for (const MixerChannelItem* channelItem : m_mixerChannelList) {
406 0 : const engraving::InstrumentTrackId& instrumentTrackId = channelItem->instrumentTrackId();
407 0 : if (instrumentTrackId.isValid() && instrumentTrackId != metronomeTrackId) {
408 0 : metronomeIdx++;
409 : }
410 : }
411 0 : return metronomeIdx;
412 : }
413 :
414 0 : int mixerChannelListIdx = 0;
415 :
416 0 : async::NotifyList<const Part*> partList = masterNotationParts()->partList();
417 0 : for (const Part* part : partList) {
418 0 : for (const InstrumentTrackId& instrumentTrackId : part->instrumentTrackIdList()) {
419 0 : if (instrumentTrackId == newInstrumentTrackId) {
420 0 : return mixerChannelListIdx;
421 : }
422 :
423 0 : const MixerChannelItem* mixerChannelItem = m_mixerChannelList[mixerChannelListIdx];
424 0 : MixerChannelItem::Type itemType = mixerChannelItem->type();
425 :
426 0 : if (itemType == MixerChannelItem::Type::Master) {
427 0 : return mixerChannelListIdx;
428 : }
429 :
430 0 : const InstrumentTrackId& itemInstrumentTrackId = mixerChannelItem->instrumentTrackId();
431 :
432 0 : if (itemInstrumentTrackId == metronomeTrackId) {
433 0 : return mixerChannelListIdx;
434 : }
435 :
436 0 : if (notationPlayback()->isChordSymbolsTrack(itemInstrumentTrackId)) {
437 0 : return mixerChannelListIdx;
438 : }
439 :
440 0 : if (itemInstrumentTrackId == instrumentTrackId) {
441 0 : if (instrumentTrackId == newInstrumentTrackId) {
442 0 : return INVALID_INDEX;
443 : }
444 :
445 0 : ++mixerChannelListIdx;
446 : }
447 0 : }
448 : }
449 :
450 0 : return INVALID_INDEX;
451 0 : }
452 :
453 0 : int MixerPanelModel::indexOf(const TrackId trackId) const
454 : {
455 0 : for (int i = 0; i < m_mixerChannelList.size(); ++i) {
456 0 : if (trackId == m_mixerChannelList[i]->trackId()) {
457 0 : return i;
458 : }
459 : }
460 :
461 0 : return INVALID_INDEX;
462 : }
463 :
464 0 : MixerChannelItem* MixerPanelModel::buildInstrumentChannelItem(const TrackId trackId,
465 : const engraving::InstrumentTrackId& instrumentTrackId,
466 : bool isPrimary)
467 : {
468 0 : MixerChannelItem::Type type = isPrimary ? MixerChannelItem::Type::PrimaryInstrument
469 : : MixerChannelItem::Type::SecondaryInstrument;
470 :
471 0 : const InstrumentTrackId& metronomeTrackId = notationPlayback()->metronomeTrackId();
472 0 : if (instrumentTrackId == metronomeTrackId) {
473 0 : type = MixerChannelItem::Type::Metronome;
474 : }
475 :
476 0 : MixerChannelItem* item = new MixerChannelItem(this, type, false /*outputOnly*/, trackId);
477 0 : item->setInstrumentTrackId(instrumentTrackId);
478 0 : item->setPanelSection(m_navigationSection);
479 0 : item->loadSoloMuteState(controller()->trackSoloMuteState(instrumentTrackId));
480 :
481 0 : playback()->params(trackId)
482 0 : .onResolve(this, [this, trackId, instrumentTrackId](const TrackParams& params) {
483 0 : if (MixerChannelItem* item = findChannelItem(trackId)) {
484 0 : item->loadInputParams(params.source);
485 :
486 0 : AudioOutputParams outParams = audioSettings()->trackOutputParams(instrumentTrackId);
487 0 : outParams.fxChain = params.fxChain;
488 0 : outParams.auxSends = params.auxSends;
489 0 : outParams.setControl(params.control);
490 0 : loadOutputParams(item, outParams);
491 0 : }
492 0 : })
493 0 : .onReject(this, [](int errCode, std::string text) {
494 0 : LOGE() << "unable to get track output parameters, error code: " << errCode
495 0 : << ", " << text;
496 0 : });
497 :
498 0 : playback()->trackName(trackId)
499 0 : .onResolve(this, [this, trackId](const RetVal<TrackName>& trackName) {
500 0 : if (trackName.ret) {
501 0 : if (MixerChannelItem* item = findChannelItem(trackId)) {
502 0 : item->setTitle(QString::fromStdString(trackName.val));
503 : }
504 : } else {
505 0 : LOGE() << "unable to get track name, error: " << trackName.ret.toString();
506 : }
507 0 : });
508 :
509 0 : playback()->signalChanges(trackId)
510 0 : .onResolve(this, [this, trackId](AudioSignalChanges signalChanges) {
511 0 : if (MixerChannelItem* item = findChannelItem(trackId)) {
512 0 : item->subscribeOnAudioSignalChanges(signalChanges);
513 : }
514 0 : })
515 0 : .onReject(this, [](int errCode, std::string text) {
516 0 : LOGE() << "unable to subscribe on audio signal changes from mixer channel, error code: " << errCode
517 0 : << ", " << text;
518 0 : });
519 :
520 0 : playback()->automatedControlParamsChanges(trackId)
521 0 : .onResolve(this, [this, trackId](AutomatedControlParamsChanges changes) {
522 0 : if (MixerChannelItem* item = findChannelItem(trackId)) {
523 0 : item->subscribeOnAutomatedControlParamsChanges(changes);
524 : }
525 0 : })
526 0 : .onReject(this, [](int errCode, std::string text) {
527 0 : LOGE() << "unable to subscribe on automated control params changes from mixer channel, error code: " << errCode
528 0 : << ", " << text;
529 0 : });
530 :
531 0 : connect(item, &MixerChannelItem::inputParamsChanged, this, [this, trackId](const AudioInputParams& params) {
532 0 : playback()->setSourceParams(trackId, params);
533 0 : });
534 :
535 0 : connect(item, &MixerChannelItem::controlParamsChanged, this,
536 0 : [this, trackId, instrumentTrackId](const AudioOutputParams& params) {
537 0 : const ControlParams current = controller()->currentTrackControlParams(instrumentTrackId);
538 0 : playback()->setControlParams(trackId, withEnvelopesOf(params.control(), current));
539 0 : });
540 :
541 0 : connect(item, &MixerChannelItem::fxChainParamsChanged, this, [this, trackId](const AudioOutputParams& params) {
542 0 : playback()->setFxChainParams(trackId, params.fxChain);
543 0 : });
544 :
545 0 : connect(item, &MixerChannelItem::auxSendsParamsChanged, this, [this, trackId](const AudioOutputParams& params) {
546 0 : playback()->setAuxSendsParams(trackId, params.auxSends);
547 0 : });
548 :
549 0 : connect(item, &MixerChannelItem::auxSendItemListChanged, this, [this, item]() {
550 0 : const QMap<aux_channel_idx_t, AuxSendItem*>& auxSendItems = item->auxSendItems();
551 0 : for (auto it = auxSendItems.begin(); it != auxSendItems.end(); ++it) {
552 0 : it.value()->setTitle(QString::fromStdString(controller()->auxChannelName(it.key())));
553 : }
554 0 : });
555 :
556 0 : connect(item, &MixerChannelItem::soloMuteStateChanged, this,
557 0 : [this, instrumentTrackId](const notation::INotationSoloMuteState::SoloMuteState& state) {
558 0 : controller()->setTrackSoloMuteState(instrumentTrackId, state);
559 0 : });
560 :
561 0 : return item;
562 : }
563 :
564 0 : MixerChannelItem* MixerPanelModel::buildAuxChannelItem(aux_channel_idx_t index, const TrackId trackId)
565 : {
566 0 : MixerChannelItem* item = new MixerChannelItem(this, MixerChannelItem::Type::Aux, true /*outputOnly*/, trackId);
567 0 : item->setPanelSection(m_navigationSection);
568 0 : item->loadSoloMuteState(audioSettings()->auxSoloMuteState(index));
569 :
570 0 : playback()->trackName(trackId)
571 0 : .onResolve(this, [this, trackId](const RetVal<TrackName>& trackName) {
572 0 : if (trackName.ret) {
573 0 : if (MixerChannelItem* item = findChannelItem(trackId)) {
574 0 : item->setTitle(QString::fromStdString(trackName.val));
575 : }
576 : } else {
577 0 : LOGE() << "unable to get track name, error: " << trackName.ret.toString();
578 : }
579 0 : });
580 :
581 0 : AudioOutputParams outParams = audioSettings()->auxOutputParams(index);
582 0 : loadOutputParams(item, outParams);
583 :
584 0 : playback()->signalChanges(trackId)
585 0 : .onResolve(this, [this, trackId](AudioSignalChanges signalChanges) {
586 0 : if (MixerChannelItem* item = findChannelItem(trackId)) {
587 0 : item->subscribeOnAudioSignalChanges(signalChanges);
588 : }
589 0 : })
590 0 : .onReject(this, [](int errCode, std::string text) {
591 0 : LOGE() << "unable to subscribe on audio signal changes from mixer channel, error code: " << errCode
592 0 : << ", " << text;
593 0 : });
594 :
595 0 : connect(item, &MixerChannelItem::controlParamsChanged, this, [this, trackId](const AudioOutputParams& params) {
596 0 : playback()->setControlParams(trackId, params.control());
597 0 : });
598 0 : connect(item, &MixerChannelItem::fxChainParamsChanged, this, [this, trackId](const AudioOutputParams& params) {
599 0 : playback()->setFxChainParams(trackId, params.fxChain);
600 0 : });
601 0 : connect(item, &MixerChannelItem::auxSendsParamsChanged, this, [this, trackId](const AudioOutputParams& params) {
602 0 : playback()->setAuxSendsParams(trackId, params.auxSends);
603 0 : });
604 :
605 0 : connect(item, &MixerChannelItem::soloMuteStateChanged, this,
606 0 : [this, index](const notation::INotationSoloMuteState::SoloMuteState& state) {
607 0 : audioSettings()->setAuxSoloMuteState(index, state);
608 0 : });
609 :
610 0 : return item;
611 0 : }
612 :
613 0 : MixerChannelItem* MixerPanelModel::buildMasterChannelItem()
614 : {
615 0 : MixerChannelItem* item = new MixerChannelItem(this, MixerChannelItem::Type::Master, true /*outputOnly*/, MASTER_TRACK_ID);
616 0 : item->setPanelSection(m_navigationSection);
617 0 : item->setTitle(muse::qtrc("playback", "Master"));
618 :
619 0 : AudioOutputParams outParams = audioSettings()->masterAudioOutputParams();
620 0 : item->updateHasAutomationFlags();
621 0 : loadOutputParams(item, outParams);
622 :
623 0 : playback()->masterSignalChanges()
624 0 : .onResolve(this, [this, item](AudioSignalChanges signalChanges) {
625 0 : if (m_masterChannelItem && item == m_masterChannelItem) {
626 0 : item->subscribeOnAudioSignalChanges(signalChanges);
627 : }
628 0 : })
629 0 : .onReject(this, [](int errCode, std::string text) {
630 0 : LOGE() << "unable to subscribe on audio signal changes from master channel, error code: " << errCode
631 0 : << ", " << text;
632 0 : });
633 :
634 0 : playback()->automatedControlParamsChanges(MASTER_TRACK_ID)
635 0 : .onResolve(this, [this, item](AutomatedControlParamsChanges changes) {
636 0 : if (m_masterChannelItem && item == m_masterChannelItem) {
637 0 : item->subscribeOnAutomatedControlParamsChanges(changes);
638 : }
639 0 : })
640 0 : .onReject(this, [](int errCode, std::string text) {
641 0 : LOGE() << "unable to subscribe on automated control params changes from master channel, error code: " << errCode
642 0 : << ", " << text;
643 0 : });
644 :
645 0 : connect(item, &MixerChannelItem::controlParamsChanged, this, [this](const AudioOutputParams& params) {
646 0 : const ControlParams current = controller()->currentMasterControlParams();
647 0 : playback()->setMasterControlParams(withEnvelopesOf(params.control(), current));
648 0 : });
649 :
650 0 : connect(item, &MixerChannelItem::fxChainParamsChanged, this, [this](const AudioOutputParams& params) {
651 0 : playback()->setMasterFxChainParams(params.fxChain);
652 0 : });
653 :
654 0 : connect(item, &MixerChannelItem::auxSendsParamsChanged, this, [this](const AudioOutputParams& params) {
655 0 : playback()->setMasterAuxSendsParams(params.auxSends);
656 0 : });
657 :
658 0 : return item;
659 0 : }
660 :
661 0 : int MixerPanelModel::masterChannelIndex() const
662 : {
663 0 : return m_mixerChannelList.size() - 1;
664 : }
665 :
666 0 : MixerChannelItem* MixerPanelModel::findChannelItem(const TrackId& trackId) const
667 : {
668 0 : for (MixerChannelItem* item : m_mixerChannelList) {
669 0 : if (item->trackId() == trackId) {
670 0 : return item;
671 : }
672 : }
673 :
674 0 : return nullptr;
675 : }
676 :
677 0 : void MixerPanelModel::loadOutputParams(MixerChannelItem* item, const AudioOutputParams& params)
678 : {
679 0 : IF_ASSERT_FAILED(item) {
680 0 : return;
681 : }
682 :
683 0 : item->loadOutputParams(params);
684 0 : updateOutputResourceItemCount();
685 : }
686 :
687 0 : void MixerPanelModel::updateOutputResourceItemCount()
688 : {
689 0 : size_t maxFxCount = 0;
690 :
691 0 : for (const MixerChannelItem* item : m_mixerChannelList) {
692 0 : const AudioFxChain& chain = item->outputParams().fxChain;
693 0 : if (chain.empty()) {
694 0 : continue;
695 : }
696 :
697 0 : AudioFxChainOrder order = std::prev(chain.end())->first;
698 0 : maxFxCount = std::max(maxFxCount, static_cast<size_t>(order) + 1);
699 : }
700 :
701 0 : for (MixerChannelItem* item : m_mixerChannelList) {
702 0 : item->setOutputResourceItemCount(maxFxCount + 1 /* + 1 blank slot */);
703 : }
704 0 : }
705 :
706 0 : INotationProjectPtr MixerPanelModel::currentProject() const
707 : {
708 0 : return context()->project();
709 : }
710 :
711 0 : IProjectAudioSettingsPtr MixerPanelModel::audioSettings() const
712 : {
713 0 : return currentProject() ? currentProject()->audioSettings() : nullptr;
714 : }
715 :
716 0 : INotationPlaybackPtr MixerPanelModel::notationPlayback() const
717 : {
718 0 : return currentProject() ? currentProject()->masterNotation()->playback() : nullptr;
719 : }
720 :
721 0 : INotationPartsPtr MixerPanelModel::masterNotationParts() const
722 : {
723 0 : return currentProject() ? currentProject()->masterNotation()->parts() : nullptr;
724 : }
725 :
726 0 : muse::ui::NavigationSection* MixerPanelModel::navigationSection() const
727 : {
728 0 : return m_navigationSection;
729 : }
730 :
731 0 : void MixerPanelModel::setNavigationSection(muse::ui::NavigationSection* navigationSection)
732 : {
733 0 : if (m_navigationSection == navigationSection) {
734 0 : return;
735 : }
736 :
737 0 : m_navigationSection = navigationSection;
738 0 : emit navigationSectionChanged();
739 : }
740 :
741 0 : int MixerPanelModel::navigationOrderStart() const
742 : {
743 0 : return m_navigationOrderStart;
744 : }
745 :
746 0 : void MixerPanelModel::setNavigationOrderStart(int navigationOrderStart)
747 : {
748 0 : if (m_navigationOrderStart == navigationOrderStart) {
749 0 : return;
750 : }
751 :
752 0 : m_navigationOrderStart = navigationOrderStart;
753 0 : emit navigationOrderStartChanged();
754 :
755 0 : updateItemsPanelsOrder();
756 : }
|