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 : #pragma once
24 :
25 : #include "engraving/dom/drumset.h"
26 : #include "engraving/dom/instrument.h"
27 :
28 : #include <QQmlListProperty>
29 :
30 : // api
31 : #include "apistructs.h"
32 : #include "scoreelement.h"
33 :
34 : #include "log.h"
35 :
36 : namespace mu::engraving {
37 : class Instrument;
38 : }
39 :
40 : namespace mu::engraving::apiv1 {
41 : class Instrument;
42 :
43 : //---------------------------------------------------------
44 : // Channel
45 : /// Provides an access to channel properties. Similar to
46 : /// Mixer, changes to some of the playback-related
47 : /// properties are not recorded to undo stack and are not
48 : /// revertable with standard user-visible "undo" action.
49 : /// Changing MIDI patch though is undoable in normal way
50 : /// ("dock" type plugins may need to call
51 : /// \ref Score.startCmd / \ref Score.endCmd for that to
52 : /// work properly).
53 : ///
54 : /// Iterating over all channels in the current score can
55 : /// be done as follows:
56 : /// \code
57 : /// var parts = curScore.parts;
58 : /// for (var i = 0; i < parts.length; ++i) {
59 : /// var part = parts[i];
60 : /// var instrs = part.instruments;
61 : /// for (var j = 0; j < instrs.length; ++j) {
62 : /// var instr = instrs[j];
63 : /// var channels = instr.channels;
64 : /// for (var k = 0; k < channels.length; ++k) {
65 : /// var channel = channels[k];
66 : /// channel.volume = 64; // just for example, changing the channel's volume
67 : /// }
68 : /// }
69 : /// }
70 : /// \endcode
71 : /// \since MuseScore 3.5
72 : //---------------------------------------------------------
73 :
74 : class Channel : public QObject
75 : {
76 : Q_OBJECT
77 :
78 : mu::engraving::InstrChannel* m_channel;
79 : mu::engraving::Part* m_part;
80 :
81 : /// Name of this channel
82 : Q_PROPERTY(QString name READ name)
83 : /// Whether this channel controls playback of chord symbols
84 : /// \since MuseScore 4.6
85 : Q_PROPERTY(bool isHarmonyChannel READ isHarmonyChannel)
86 :
87 : /// Channel volume, from 0 to 127.
88 : /// \note Changing this property is **not** revertable with a standard "undo"
89 : /// action. Plugins may need to handle reverting this property change if
90 : /// necessary.
91 : Q_PROPERTY(int volume READ volume WRITE setVolume)
92 : /// Channel pan, from 0 to 127.
93 : /// \note Changing this property is **not** revertable with a standard "undo"
94 : /// action. Plugins may need to handle reverting this property change if
95 : /// necessary.
96 : Q_PROPERTY(int pan READ pan WRITE setPan)
97 : /// Channel chorus, from 0 to 127.
98 : /// \note Changing this property is **not** revertable with a standard "undo"
99 : /// action. Plugins may need to handle reverting this property change if
100 : /// necessary.
101 : Q_PROPERTY(int chorus READ chorus WRITE setChorus)
102 : /// Channel reverb, from 0 to 127.
103 : /// \note Changing this property is **not** revertable with a standard "undo"
104 : /// action. Plugins may need to handle reverting this property change if
105 : /// necessary.
106 : Q_PROPERTY(int reverb READ reverb WRITE setReverb)
107 : /// Whether this channel is muted.
108 : /// \note Changing this property is **not** revertable with a standard "undo"
109 : /// action. Plugins may need to handle reverting this property change if
110 : /// necessary.
111 : Q_PROPERTY(bool mute READ mute WRITE setMute)
112 :
113 : /// MIDI program number, from 0 to 127. Changing this property is recorded
114 : /// to the program's undo stack and can be reverted with a standard "undo"
115 : /// action.
116 : Q_PROPERTY(int midiProgram READ midiProgram WRITE setMidiProgram)
117 : /// MIDI patch bank number. Changing this property is recorded
118 : /// to the program's undo stack and can be reverted with a standard "undo"
119 : /// action.
120 : Q_PROPERTY(int midiBank READ midiBank WRITE setMidiBank)
121 :
122 : mu::engraving::InstrChannel* activeChannel();
123 :
124 : void setMidiBankAndProgram(int bank, int program, bool setUserBankController);
125 :
126 : public:
127 : /// \cond MS_INTERNAL
128 0 : Channel(mu::engraving::InstrChannel* ch, mu::engraving::Part* p, QObject* parent = nullptr)
129 0 : : QObject(parent), m_channel(ch), m_part(p) {}
130 :
131 0 : QString name() const { return m_channel->name(); }
132 0 : bool isHarmonyChannel() const { return m_channel->isHarmonyChannel(); }
133 :
134 0 : int volume() const { return m_channel->volume(); }
135 0 : void setVolume(int val) { activeChannel()->setVolume(qBound(0, val, 127)); }
136 0 : int pan() const { return m_channel->pan(); }
137 0 : void setPan(int val) { activeChannel()->setPan(qBound(0, val, 127)); }
138 0 : int chorus() const { return m_channel->chorus(); }
139 0 : void setChorus(int val) { activeChannel()->setChorus(qBound(0, val, 127)); }
140 0 : int reverb() const { return m_channel->reverb(); }
141 0 : void setReverb(int val) { activeChannel()->setReverb(qBound(0, val, 127)); }
142 :
143 0 : bool mute() const
144 : {
145 0 : DEPRECATED;
146 : //!@NOTE since MuseScore 4.0 mixer doen/t work via Channel
147 0 : return false;
148 : }
149 :
150 0 : void setMute(bool)
151 : {
152 0 : DEPRECATED;
153 : //!@NOTE since MuseScore 4.0 mixer doen/t work via Channel
154 0 : }
155 :
156 0 : int midiProgram() const { return m_channel->program(); }
157 : void setMidiProgram(int prog);
158 0 : int midiBank() const { return m_channel->bank(); }
159 : void setMidiBank(int bank);
160 : /// \endcond
161 : };
162 :
163 : //---------------------------------------------------------
164 : // StringData
165 : /// \since MuseScore 3.5
166 : //---------------------------------------------------------
167 :
168 : class StringData : public QObject
169 : {
170 : Q_OBJECT
171 :
172 : /// List of strings in this instrument.
173 : /// \returns A list of objects representing strings. Each
174 : /// object has the following fields:
175 : /// - \p pitch - pitch of this string on fret 0 (integer).
176 : /// - \p open - if \p true, this string is not fretted and
177 : /// always open. If \p false, the string **is**
178 : /// fretted. For example, for classical guitar
179 : /// all strings are fretted, and for them
180 : /// \p open value will always be \p false.
181 : Q_PROPERTY(QVariantList strings READ stringList)
182 :
183 : /// Number of frets in this instrument
184 : Q_PROPERTY(int frets READ frets)
185 :
186 : mu::engraving::StringData m_data;
187 :
188 : public:
189 : /// \cond MS_INTERNAL
190 0 : StringData(const mu::engraving::StringData* d, QObject* parent = nullptr)
191 0 : : QObject(parent), m_data(*d) {}
192 :
193 : QVariantList stringList() const;
194 0 : int frets() const { return m_data.frets(); }
195 : /// \endcond
196 : };
197 :
198 : //---------------------------------------------------------
199 : // Drumset
200 : /// \since MuseScore 4.6
201 : //---------------------------------------------------------
202 :
203 : class Drumset : public QObject
204 : {
205 : Q_OBJECT
206 :
207 : mu::engraving::Drumset* m_drumset;
208 : bool m_owned = false;
209 :
210 : public:
211 : /// \cond MS_INTERNAL
212 0 : Drumset(mu::engraving::Drumset* d, QObject* parent = nullptr)
213 0 : : QObject(parent), m_drumset(d) {}
214 :
215 1 : Drumset(mu::engraving::Drumset* d, bool owned, QObject* parent = nullptr)
216 1 : : QObject(parent), m_drumset(d), m_owned(owned) {}
217 :
218 2 : ~Drumset() override
219 1 : {
220 1 : if (m_owned) {
221 1 : delete m_drumset;
222 : }
223 2 : }
224 :
225 1 : mu::engraving::Drumset* drumset() { return m_drumset; }
226 : const mu::engraving::Drumset* drumset() const { return m_drumset; }
227 : /// \endcond
228 :
229 : /// Whether the given MIDI pitch corresponds to a note in the drumset.
230 : /// \param pitch The pitch to test for.
231 0 : Q_INVOKABLE bool isValid(int pitch) { return drumset()->isValid(pitch); }
232 :
233 : /// The notehead group for the given pitch, corresponding to one of the
234 : /// PluginAPI::PluginAPI::NoteHeadGroup values. If the value corresponds to
235 : /// NoteHeadGroup.HEAD_CUSTOM, the actual notehead must be found using
236 : /// PluginAPI::Drumset::noteHeads(pitch, NoteHeadType)
237 : /// \see PluginAPI::PluginAPI::NoteHeadGroup
238 : /// \see PluginAPI::Drumset::noteHeads
239 : /// \param pitch The pitch to find the notehead group for.
240 0 : Q_INVOKABLE int noteHead(int pitch) { return int(drumset()->noteHead(pitch)); }
241 :
242 : /// The notehead symbol for the given pitch and NoteHeadType, corresponding to
243 : /// one of the PluginAPI::PluginAPI::SymId values.
244 : /// \see PluginAPI::PluginAPI::SymId
245 : /// \see PluginAPI::PluginAPI::NoteHeadType
246 : /// \param pitch The pitch to find the notehead symbol for.
247 : /// \param type The NoteHeadType to find the notehead symbol for.
248 0 : Q_INVOKABLE int noteHeads(int pitch, int type)
249 : {
250 0 : return int(drumset()->noteHeads(pitch, mu::engraving::NoteHeadType(type)));
251 : }
252 :
253 : /// The line a note with the given pitch would be displayed on.
254 : /// \param pitch The pitch of the note to find the line for.
255 0 : Q_INVOKABLE int line(int pitch) { return drumset()->line(pitch); }
256 :
257 : /// The voice a note with the given pitch would be added to.
258 : /// \param pitch The pitch of the note to find the voice for.
259 0 : Q_INVOKABLE int voice(int pitch) { return drumset()->voice(pitch); }
260 :
261 : /// The default stem direction a note with the given pitch would have.
262 : /// One of the PluginAPI::PluginAPI::Direction values
263 : /// \see PluginAPI::PluginAPI::Direction
264 : /// \param pitch The pitch to find the stem direction for.
265 0 : Q_INVOKABLE int stemDirection(int pitch) { return int(drumset()->stemDirection(pitch)); }
266 :
267 : /// The name (untranslated) of the drumset note for a given pitch.
268 : /// \param pitch The pitch of the note to find the name for.
269 0 : Q_INVOKABLE QString name(int pitch) { return drumset()->name(pitch); }
270 :
271 : /// The translated name of the drumset note for a given pitch.
272 : /// \param pitch The pitch of the note to find the name for.
273 0 : Q_INVOKABLE QString translatedName(int pitch) { return drumset()->translatedName(pitch); }
274 :
275 : /// The shortcut of the drumset note for a given pitch.
276 : /// \param pitch The pitch of the note to find the shortcut for.
277 0 : Q_INVOKABLE QString shortcut(int pitch) { return drumset()->shortcut(pitch); }
278 :
279 : /// List of variants for a given pitch in this drumset.
280 : /// \returns A list of objects representing variants.
281 : /// Each object has the following fields:
282 : /// - \p pitch - pitch of this variant.
283 : /// - \p tremolo - tremolo type of this variant, one of
284 : /// PluginAPI::PLUGIN_API::TremoloType values.
285 : /// - \p articulationName - the name of the articulation
286 : /// for this variant.
287 : /// \param pitch Pitch to find variants for.
288 : Q_INVOKABLE QVariantList variants(int pitch);
289 :
290 : /// The row the given pitch appears in the percussion panel.
291 : /// \param pitch The pitch of the note to find the row for.
292 0 : Q_INVOKABLE int panelRow(int pitch) { return drumset()->panelRow(pitch); }
293 :
294 : /// The column the given pitch appears in the percussion panel.
295 : /// \param pitch The pitch of the note to find the column for.
296 0 : Q_INVOKABLE int panelColumn(int pitch) { return drumset()->panelColumn(pitch); }
297 :
298 : /// Tries to find the pitch of a normal notehead at "line". If a
299 : /// normal notehead can't be found it will instead return the
300 : /// "first valid pitch" (i.e. the lowest used midi note) in the drumset.
301 : /// \param line The line to find the default pitch on.
302 0 : Q_INVOKABLE int defaultPitchForLine(int line) { return drumset()->defaultPitchForLine(line); }
303 :
304 : /// The next used pitch from a given starting point.
305 : /// \param pitch The pitch from which to find the next used pitch from.
306 0 : Q_INVOKABLE int nextPitch(int pitch) { return drumset()->nextPitch(pitch); }
307 :
308 : /// The previous used pitch from a given starting point.
309 : /// \param pitch The pitch from which to find the previous used pitch from.
310 0 : Q_INVOKABLE int prevPitch(int pitch) { return drumset()->prevPitch(pitch); }
311 :
312 : /// Sets the name of the drum note at the given pitch.
313 : /// These setters allow modifying a cloned drumset before passing it
314 : /// to Score.replaceDrumset(). The Drumset obtained from an Instrument
315 : /// is read-only; use Instrument.cloneDrumset() to get a writable copy.
316 : /// \param pitch The MIDI pitch (0 to 127).
317 : /// \param name The new name for this drum note.
318 : /// \since MuseScore 4.7
319 : Q_INVOKABLE void setName(int pitch, const QString& name);
320 :
321 : /// Sets the notehead group for the drum note at the given pitch.
322 : /// \param pitch The MIDI pitch (0 to 127).
323 : /// \param noteHead The notehead group (NoteHeadGroup value).
324 : /// \since MuseScore 4.7
325 : Q_INVOKABLE void setNoteHead(int pitch, int noteHead);
326 :
327 : /// Sets the staff line for the drum note at the given pitch.
328 : /// \param pitch The MIDI pitch (0 to 127).
329 : /// \param line The staff line for this note.
330 : /// \since MuseScore 4.7
331 : Q_INVOKABLE void setLine(int pitch, int line);
332 :
333 : /// Sets the voice for the drum note at the given pitch.
334 : /// \param pitch The MIDI pitch (0 to 127).
335 : /// \param voice The voice index (0 to 3).
336 : /// \since MuseScore 4.7
337 : Q_INVOKABLE void setVoice(int pitch, int voice);
338 :
339 : /// Sets the stem direction for the drum note at the given pitch.
340 : /// \param pitch The MIDI pitch (0 to 127).
341 : /// \param stemDirection The stem direction (Direction value).
342 : /// \since MuseScore 4.7
343 : Q_INVOKABLE void setStemDirection(int pitch, int stemDirection);
344 :
345 : /// Sets the keyboard shortcut for the drum note at the given pitch.
346 : /// \param pitch The MIDI pitch (0 to 127).
347 : /// \param shortcut The keyboard shortcut character.
348 : /// \since MuseScore 4.7
349 : Q_INVOKABLE void setShortcut(int pitch, const QString& shortcut);
350 :
351 : /// Checks whether two drumsets represent the same object.
352 0 : Q_INVOKABLE bool is(apiv1::Drumset* other) { return other && drumset() == other->drumset(); }
353 : };
354 :
355 : //---------------------------------------------------------
356 : // ChannelListProperty
357 : /// \cond PLUGIN_API \private \endcond
358 : //---------------------------------------------------------
359 :
360 : class ChannelListProperty : public QQmlListProperty<Channel>
361 : {
362 : public:
363 : ChannelListProperty(Instrument* i);
364 :
365 : static qsizetype count(QQmlListProperty<Channel>* l);
366 : static Channel* at(QQmlListProperty<Channel>* l, qsizetype i);
367 : };
368 :
369 : //---------------------------------------------------------
370 : // Instrument
371 : /// \since MuseScore 3.5
372 : //---------------------------------------------------------
373 :
374 : class Instrument : public QObject
375 : {
376 : Q_OBJECT
377 :
378 : /// The MuseScore string identifier
379 : /// for this instrument.
380 : /// \see \ref apiv1::Part::instrumentId "Part.instrumentId"
381 : Q_PROPERTY(QString instrumentId READ instrumentId)
382 : /// The string identifier
383 : /// ([MusicXML Sound ID](https://www.musicxml.com/for-developers/standard-sounds/))
384 : /// for this instrument.
385 : /// \see \ref apiv1::Part::musicXmlId "Part.musicXmlId"
386 : Q_PROPERTY(QString musicXmlId READ musicXmlId)
387 : // mu::engraving::Instrument supports multiple short/long names (for aeolus instruments?)
388 : // but in practice only one is actually used. If this gets changed this API could
389 : // be expanded.
390 : /// The long name for this instrument.
391 : Q_PROPERTY(QString longName READ longName)
392 : /// The short name for this instrument.
393 : Q_PROPERTY(QString shortName READ shortName)
394 :
395 : /// This instrument's written-to-sounding transposition, in diatonic steps
396 : /// and chromatic semitones. Negative values indicate downward transposition.
397 : /// \see apiv1::IntervalWrapper
398 : /// \since MuseScore 5.0
399 : Q_PROPERTY(apiv1::IntervalWrapper * transpose READ transpose)
400 :
401 : /// For fretted instruments, an information about this
402 : /// instrument's strings.
403 : Q_PROPERTY(apiv1::StringData * stringData READ stringData)
404 :
405 : /// For unpitched percussion instruments, information about
406 : /// this instrument's percussion.
407 : /// \since MuseScore 4.6
408 : Q_PROPERTY(apiv1::Drumset * drumset READ drumset)
409 :
410 : /// The list of channels for this instrument.
411 : Q_PROPERTY(QQmlListProperty<apiv1::Channel> channels READ channels)
412 :
413 : mu::engraving::Instrument* m_instrument;
414 : mu::engraving::Part* m_part;
415 :
416 : public:
417 : /// \cond MS_INTERNAL
418 1 : Instrument(mu::engraving::Instrument* i, mu::engraving::Part* p)
419 1 : : QObject(), m_instrument(i), m_part(p) {}
420 :
421 1 : mu::engraving::Instrument* instrument() { return m_instrument; }
422 0 : const mu::engraving::Instrument* instrument() const { return m_instrument; }
423 :
424 0 : mu::engraving::Part* part() { return m_part; }
425 :
426 0 : QString instrumentId() const { return instrument()->id(); }
427 0 : QString musicXmlId() const { return instrument()->musicXmlId(); }
428 : QString longName() const;
429 : QString shortName() const;
430 : apiv1::IntervalWrapper* transpose() const;
431 :
432 0 : apiv1::StringData* stringData() { return customWrap<StringData>(instrument()->stringData()); }
433 :
434 0 : apiv1::Drumset* drumset() { return customWrap<Drumset>(instrument()->drumset()); }
435 :
436 : ChannelListProperty channels();
437 : /// \endcond
438 :
439 : /// Creates and returns a copy of this instrument's drumset.
440 : /// The returned Drumset is owned by the caller and can be modified
441 : /// without affecting the original instrument until replaceDrumset is called.
442 : /// Returns null if the instrument has no drumset.
443 : /// \since MuseScore 4.7
444 : Q_INVOKABLE apiv1::Drumset* cloneDrumset();
445 :
446 : /// Checks whether two instruments represent the same object.
447 0 : Q_INVOKABLE bool is(apiv1::Instrument* other) { return other && instrument() == other->instrument(); }
448 : };
449 : }
|