LMMS
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 "Plugin.h"
35#include "TimePos.h"
36
37#include <cmath>
38
39
40namespace lmms
41{
42
43// forward-declarations
44class InstrumentTrack;
45class MidiEvent;
46class NotePlayHandle;
47class Track;
48class SampleFrame;
49
50
51class LMMS_EXPORT Instrument : public Plugin
52{
53public:
54 enum class Flag
55 {
56 NoFlags = 0x00,
57 IsSingleStreamed = 0x01,
58 IsMidiBased = 0x02,
59 IsNotBendable = 0x04,
60 };
61
63
64 Instrument(InstrumentTrack * _instrument_track,
65 const Descriptor * _descriptor,
66 const Descriptor::SubPluginFeatures::Key * key = nullptr,
67 Flags flags = Flag::NoFlags);
68 ~Instrument() override = default;
69
70 // --------------------------------------------------------------------
71 // functions that can/should be re-implemented:
72 // --------------------------------------------------------------------
73
74 virtual bool hasNoteInput() const { return true; }
75
76 // if the plugin doesn't play each note, it can create an instrument-
77 // play-handle and re-implement this method, so that it mixes its
78 // output buffer only once per audio engine period
79 virtual void play( SampleFrame* _working_buffer );
80
81 // to be implemented by actual plugin
82 virtual void playNote( NotePlayHandle * /* _note_to_play */,
83 SampleFrame* /* _working_buf */ )
84 {
85 }
86
87 // needed for deleting plugin-specific-data of a note - plugin has to
88 // cast void-ptr so that the plugin-data is deleted properly
89 // (call of dtor if it's a class etc.)
90 virtual void deleteNotePluginData( NotePlayHandle * _note_to_play );
91
92 // Get number of sample-frames that should be used when playing beat
93 // (note with unspecified length)
94 // Per default this function returns 0. In this case, channel is using
95 // the length of the longest envelope (if one active).
96 virtual f_cnt_t beatLen( NotePlayHandle * _n ) const;
97
98
99 // This method can be overridden by instruments that need a certain
100 // release time even if no envelope is active. It returns the time
101 // in milliseconds that these instruments would like to have for
102 // their release stage.
103 virtual float desiredReleaseTimeMs() const
104 {
105 return 0.f;
106 }
107
108 // Converts the desired release time in milliseconds to the corresponding
109 // number of frames depending on the sample rate.
111 {
112 const sample_rate_t sampleRate = getSampleRate();
113
114 return static_cast<f_cnt_t>(std::ceil(desiredReleaseTimeMs() * sampleRate / 1000.f));
115 }
116
118
119 bool isSingleStreamed() const
120 {
122 }
123
124 bool isMidiBased() const
125 {
126 return m_flags.testFlag(Instrument::Flag::IsMidiBased);
127 }
128
129 bool isBendable() const
130 {
132 }
133
134 // sub-classes can re-implement this for receiving all incoming
135 // MIDI-events
136 inline virtual bool handleMidiEvent( const MidiEvent&, const TimePos& = TimePos(), f_cnt_t offset = 0 )
137 {
138 return true;
139 }
140
141 QString fullDisplayName() const override;
142
143 // --------------------------------------------------------------------
144 // provided functions:
145 // --------------------------------------------------------------------
146
149 static Instrument * instantiate(const QString & _plugin_name,
150 InstrumentTrack * _instrument_track,
152 bool keyFromDnd = false);
153
154 virtual bool isFromTrack( const Track * _track ) const;
155
157 {
158 return m_instrumentTrack;
159 }
160
161
162protected:
163 // fade in to prevent clicks
164 void applyFadeIn(SampleFrame* buf, NotePlayHandle * n);
165
166 // instruments may use this to apply a soft fade out at the end of
167 // notes - method does this only if really less or equal
168 // desiredReleaseFrames() frames are left
169 void applyRelease( SampleFrame* buf, const NotePlayHandle * _n );
170
171 float computeReleaseTimeMsByFrameCount(f_cnt_t frames) const;
172
173
174private:
177};
178
179
181
182
183} // namespace lmms
184
185#endif // LMMS_INSTRUMENT_H
#define LMMS_DECLARE_OPERATORS_FOR_FLAGS(type)
Definition Flags.h:77
static LV2_Handle instantiate(const LV2_Descriptor *descriptor, double rate, const char *path, const LV2_Feature *const *features)
Definition bad_syntax.c:57
Definition Flags.h:34
Definition Instrument.h:52
InstrumentTrack * instrumentTrack() const
Definition Instrument.h:156
Flags m_flags
Definition Instrument.h:176
lmms::Flags< Flag > Flags
Definition Instrument.h:62
f_cnt_t desiredReleaseFrames() const
Definition Instrument.h:110
bool isMidiBased() const
Definition Instrument.h:124
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:55
@ IsMidiBased
Definition Instrument.h:58
@ IsSingleStreamed
Definition Instrument.h:57
@ NoFlags
Definition Instrument.h:56
@ IsNotBendable
Definition Instrument.h:59
bool isSingleStreamed() const
Definition Instrument.h:119
virtual bool hasNoteInput() const
Definition Instrument.h:74
InstrumentTrack * m_instrumentTrack
Definition Instrument.h:175
~Instrument() override=default
virtual float desiredReleaseTimeMs() const
Definition Instrument.h:103
virtual void playNote(NotePlayHandle *, SampleFrame *)
Definition Instrument.h:82
bool isBendable() const
Definition Instrument.h:129
virtual bool handleMidiEvent(const MidiEvent &, const TimePos &=TimePos(), f_cnt_t offset=0)
Definition Instrument.h:136
Definition InstrumentTrack.h:62
Definition MidiEvent.h:37
Definition NotePlayHandle.h:48
Plugin(const Descriptor *descriptor, Model *parent, const Descriptor::SubPluginFeatures::Key *key=nullptr)
Definition Plugin.cpp:60
const Descriptor::SubPluginFeatures::Key & key() const
Definition Plugin.h:266
Definition SampleFrame.h:41
Definition TimePos.h:68
Base-class for all tracks.
Definition Track.h:68
virtual ASIOError getSampleRate(ASIOSampleRate *sampleRate)=0
Definition AudioAlsa.cpp:35
std::uint32_t sample_rate_t
Definition LmmsTypes.h:42
std::uint64_t f_cnt_t
Definition LmmsTypes.h:43
Definition Plugin.h:92
int n
Definition crypt.c:458
ZCONST char * key
Definition crypt.c:587