lmms
Cross-platform music production software
Loading...
Searching...
No Matches
Instrument.h
Go to the documentation of this file.
1/*
2 * Instrument.h - declaration of class Instrument, which provides a
3 * standard interface for all instrument plugins
4 *
5 * Copyright (c) 2005-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
6 *
7 * This file is part of LMMS - https://lmms.io
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
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 GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public
20 * License along with this program (see COPYING); if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301 USA.
23 *
24 */
25
26#ifndef LMMS_INSTRUMENT_H
27#define LMMS_INSTRUMENT_H
28
29#include <QString>
30
31#include "Flags.h"
32#include "lmms_export.h"
33#include "LmmsTypes.h"
34#include "MidiPatch.h"
35#include "Plugin.h"
36#include "TimePos.h"
37
38#include <cmath>
39#include <optional>
40#include <string_view>
41
42namespace lmms
43{
44
45// forward-declarations
46class InstrumentTrack;
47class MidiEvent;
48class NotePlayHandle;
49class Track;
50class SampleFrame;
51
52
53class LMMS_EXPORT Instrument : public Plugin
54{
55public:
56 enum class Flag
57 {
58 NoFlags = 0x00,
59 IsSingleStreamed = 0x01,
60 IsMidiBased = 0x02,
61 IsNotBendable = 0x04,
62 };
63
65
66 Instrument(InstrumentTrack * _instrument_track,
67 const Descriptor * _descriptor,
68 const Descriptor::SubPluginFeatures::Key * key = nullptr,
69 Flags flags = Flag::NoFlags);
70 ~Instrument() override = default;
71
72 // --------------------------------------------------------------------
73 // functions that can/should be re-implemented:
74 // --------------------------------------------------------------------
75
76 virtual bool hasNoteInput() const { return true; }
77
78 // if the plugin doesn't play each note, it can create an instrument-
79 // play-handle and re-implement this method, so that it mixes its
80 // output buffer only once per audio engine period
81 virtual void play( SampleFrame* _working_buffer );
82
83 // to be implemented by actual plugin
84 virtual void playNote( NotePlayHandle * /* _note_to_play */,
85 SampleFrame* /* _working_buf */ )
86 {
87 }
88
89 // needed for deleting plugin-specific-data of a note - plugin has to
90 // cast void-ptr so that the plugin-data is deleted properly
91 // (call of dtor if it's a class etc.)
92 virtual void deleteNotePluginData( NotePlayHandle * _note_to_play );
93
95 virtual auto midiPatch() const -> std::optional<MidiPatch>
96 {
97 return std::nullopt;
98 }
99
102 virtual auto childModel(std::string_view modelName) -> AutomatableModel*
103 {
104 return nullptr;
105 }
106
107 // Get number of sample-frames that should be used when playing beat
108 // (note with unspecified length)
109 // Per default this function returns 0. In this case, channel is using
110 // the length of the longest envelope (if one active).
111 virtual f_cnt_t beatLen( NotePlayHandle * _n ) const;
112
113
114 // This method can be overridden by instruments that need a certain
115 // release time even if no envelope is active. It returns the time
116 // in milliseconds that these instruments would like to have for
117 // their release stage.
118 virtual float desiredReleaseTimeMs() const
119 {
120 return 0.f;
121 }
122
123 // Converts the desired release time in milliseconds to the corresponding
124 // number of frames depending on the sample rate.
126 {
127 const sample_rate_t sampleRate = getSampleRate();
128
129 return static_cast<f_cnt_t>(std::ceil(desiredReleaseTimeMs() * sampleRate / 1000.f));
130 }
131
132 sample_rate_t getSampleRate() const;
133
134 bool isSingleStreamed() const
135 {
137 }
138
139 bool isMidiBased() const
140 {
141 return m_flags.testFlag(Instrument::Flag::IsMidiBased);
142 }
143
144 bool isBendable() const
145 {
147 }
148
149 // sub-classes can re-implement this for receiving all incoming
150 // MIDI-events
151 inline virtual bool handleMidiEvent( const MidiEvent&, const TimePos& = TimePos(), f_cnt_t offset = 0 )
152 {
153 return true;
154 }
155
156 QString fullDisplayName() const override;
157
158 // --------------------------------------------------------------------
159 // provided functions:
160 // --------------------------------------------------------------------
161
164 static Instrument * instantiate(const QString & _plugin_name,
165 InstrumentTrack * _instrument_track,
167 bool keyFromDnd = false);
168
169 virtual bool isFromTrack( const Track * _track ) const;
170
172 {
173 return m_instrumentTrack;
174 }
175
176
177protected:
178 // fade in to prevent clicks
179 void applyFadeIn(SampleFrame* buf, NotePlayHandle * n);
180
181 // instruments may use this to apply a soft fade out at the end of
182 // notes - method does this only if really less or equal
183 // desiredReleaseFrames() frames are left
184 void applyRelease( SampleFrame* buf, const NotePlayHandle * _n );
185
186 float computeReleaseTimeMsByFrameCount(f_cnt_t frames) const;
187
188
189private:
192};
193
194
196
197
198} // namespace lmms
199
200#endif // LMMS_INSTRUMENT_H
#define LMMS_DECLARE_OPERATORS_FOR_FLAGS(type)
Definition Flags.h:77
Definition AutomatableModel.h:77
Definition Flags.h:34
Definition Instrument.h:54
InstrumentTrack * instrumentTrack() const
Definition Instrument.h:171
virtual auto childModel(std::string_view modelName) -> AutomatableModel *
Definition Instrument.h:102
Flags m_flags
Definition Instrument.h:191
lmms::Flags< Flag > Flags
Definition Instrument.h:64
f_cnt_t desiredReleaseFrames() const
Definition Instrument.h:125
bool isMidiBased() const
Definition Instrument.h:139
Instrument(InstrumentTrack *_instrument_track, const Descriptor *_descriptor, const Descriptor::SubPluginFeatures::Key *key=nullptr, Flags flags=Flag::NoFlags)
Definition Instrument.cpp:38
Flag
Definition Instrument.h:57
@ IsMidiBased
Definition Instrument.h:60
@ IsSingleStreamed
Definition Instrument.h:59
@ NoFlags
Definition Instrument.h:58
@ IsNotBendable
Definition Instrument.h:61
bool isSingleStreamed() const
Definition Instrument.h:134
virtual bool hasNoteInput() const
Definition Instrument.h:76
InstrumentTrack * m_instrumentTrack
Definition Instrument.h:190
sample_rate_t getSampleRate() const
Definition Instrument.cpp:206
~Instrument() override=default
virtual float desiredReleaseTimeMs() const
Definition Instrument.h:118
virtual void playNote(NotePlayHandle *, SampleFrame *)
Definition Instrument.h:84
bool isBendable() const
Definition Instrument.h:144
virtual bool handleMidiEvent(const MidiEvent &, const TimePos &=TimePos(), f_cnt_t offset=0)
Definition Instrument.h:151
virtual auto midiPatch() const -> std::optional< MidiPatch >
For applicable instruments: Provides host with current MIDI bank and program.
Definition Instrument.h:95
Definition InstrumentTrack.h:62
Definition MidiEvent.h:37
Definition NotePlayHandle.h:48
Plugin(const Descriptor *descriptor, Model *parent, const Descriptor::SubPluginFeatures::Key *key=nullptr)
Constructor of a plugin.
Definition Plugin.cpp:60
const Descriptor::SubPluginFeatures::Key & key() const
Definition Plugin.h:272
Definition SampleFrame.h:41
Definition TimePos.h:68
Base-class for all tracks.
Definition Track.h:67
Definition RemotePluginBase.cpp:34
std::uint32_t sample_rate_t
Definition LmmsTypes.h:42
std::uint64_t f_cnt_t
Definition LmmsTypes.h:43
Definition MidiPatch.h:34
Definition Plugin.h:92