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 "notationmidiwriter.h"
24 :
25 : #include <QBuffer>
26 :
27 : #include "engraving/dom/score.h"
28 :
29 : #include "notation/inotation.h"
30 : #include "notation/inotationelements.h" // IWYU pragma: keep
31 :
32 : #include "midiexport/exportmidi.h"
33 :
34 : #include "log.h"
35 :
36 : using namespace mu::iex::midi;
37 : using namespace muse;
38 : using namespace muse::io;
39 : using namespace mu::project;
40 : using namespace mu::notation;
41 : using namespace mu::engraving;
42 :
43 0 : std::vector<INotationWriter::UnitType> NotationMidiWriter::supportedUnitTypes() const
44 : {
45 0 : return { UnitType::PER_PART };
46 : }
47 :
48 0 : bool NotationMidiWriter::supportsUnitType(UnitType unitType) const
49 : {
50 0 : std::vector<UnitType> unitTypes = supportedUnitTypes();
51 0 : return std::find(unitTypes.cbegin(), unitTypes.cend(), unitType) != unitTypes.cend();
52 0 : }
53 :
54 0 : Ret NotationMidiWriter::write(INotationPtr notation, io::IODevice& destinationDevice, const Options&)
55 : {
56 0 : IF_ASSERT_FAILED(notation) {
57 0 : return make_ret(Ret::Code::UnknownError);
58 : }
59 :
60 0 : Score* score = notation->elements()->msScore();
61 :
62 0 : IF_ASSERT_FAILED(score) {
63 0 : return make_ret(Ret::Code::UnknownError);
64 : }
65 :
66 0 : ExportMidi exportMidi(score);
67 :
68 0 : bool isPlayRepeatsEnabled = midiImportExportConfiguration()->isExpandRepeats();
69 0 : bool isMidiExportRpns = midiImportExportConfiguration()->isMidiExportRpns();
70 0 : SynthesizerState synthesizerState = score->synthesizerState();
71 :
72 0 : QByteArray qdata;
73 0 : QBuffer buf(&qdata);
74 0 : buf.open(QIODevice::WriteOnly);
75 :
76 0 : bool ok = exportMidi.write(&buf, isPlayRepeatsEnabled, isMidiExportRpns, synthesizerState);
77 0 : if (ok) {
78 0 : ByteArray data = ByteArray::fromQByteArrayNoCopy(qdata);
79 0 : destinationDevice.write(data);
80 0 : }
81 :
82 0 : return ok ? make_ret(Ret::Code::Ok) : make_ret(Ret::Code::InternalError);
83 0 : }
84 :
85 0 : Ret NotationMidiWriter::writeList(const notation::INotationPtrList&, io::IODevice&, const Options&)
86 : {
87 0 : NOT_SUPPORTED;
88 0 : return Ret(Ret::Code::NotSupported);
89 : }
|