/[svn]/linuxsampler/trunk/src/engines/common/Note.h
ViewVC logotype

Contents of /linuxsampler/trunk/src/engines/common/Note.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3452 - (show annotations) (download) (as text)
Thu Jan 3 12:08:48 2019 UTC (2 weeks, 3 days ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 11304 byte(s)
* Fixed potential endless recursion introduced by SVN r3444
  (patch by Ivan Maguidhir).
* Bumped version (2.1.0.svn4).

1 /*
2 * Copyright (c) 2016 - 2018 Christian Schoenebeck
3 *
4 * http://www.linuxsampler.org
5 *
6 * This file is part of LinuxSampler and released under the same terms.
7 * See README file for details.
8 */
9
10 #ifndef LS_NOTE_H
11 #define LS_NOTE_H
12
13 #include "../../common/Pool.h"
14 #include "Event.h"
15 #include "Fade.h"
16
17 #define DEFAULT_NOTE_VOLUME_TIME_S 0.013f /* 13ms */
18 #define DEFAULT_NOTE_PITCH_TIME_S 0.013f /* 13ms */
19 #define DEFAULT_NOTE_PAN_TIME_S 0.013f /* 13ms */
20
21 namespace LinuxSampler {
22
23 /// Whether release trigger sample(s) should be played and if yes under which circumstance(s). Options are bit flags to be able to combine them bitwise.
24 enum release_trigger_t {
25 release_trigger_none = 0, ///< Don't play release trigger sample.
26 release_trigger_noteoff = 1, ///< Play release trigger sample on MIDI note-off event.
27 release_trigger_sustain_maxvelocity = (1 << 1), ///< Play release trigger sample on sustain pedal up, use 127 as MIDI velocity.
28 release_trigger_sustain_keyvelocity = (1 << 2) ///< Play release trigger sample on sustain pedal up, use latest MIDI note-on velocity on key.
29 };
30
31 /// convenience macro for checking playing release trigger sample by sustain pedal in general
32 #define release_trigger_sustain \
33 (release_trigger_sustain_maxvelocity | release_trigger_sustain_keyvelocity)
34
35 // remove strictness of C++ regarding raw bitwise operations (on type release_trigger_t)
36 inline release_trigger_t operator|(release_trigger_t a, release_trigger_t b) {
37 return (release_trigger_t) ((int)a | (int)b);
38 }
39 inline release_trigger_t& operator|=(release_trigger_t& a, release_trigger_t b) {
40 a = (release_trigger_t) ((int)a | (int)b);
41 return a;
42 }
43
44 /**
45 * Abstract base class of its deriving @c Note class, this class (NoteBase)
46 * is not intended to be instantiated directly. It just provides access to
47 * the parts of a Note object which do not depend on any C++ template
48 * parameter.
49 */
50 class NoteBase {
51 public:
52 int hostKey; ///< Key on which this is @c Note is allocated on. This is usually the note-on event's note number, however in case of a child note this will rather be the parent note's key instead!
53 note_id_t parentNoteID; ///< If not null: unique ID of the parent note of this note (see comments of field @c pChildNotes).
54 RTList<note_id_t>* pChildNotes; ///< Note ID list of "child" notes of this note. These are special notes that must be released once this note gets released.
55 Event cause; ///< Copy of the original event (usually a note-on event) which caused this note.
56 event_id_t eventID; ///< Unique ID of the actual original @c Event which caused this note.
57 sched_time_t triggerSchedTime; ///< Engine's scheduler time when this note was launched.
58 /// Optional synthesis parameters that might be overridden (by calling real-time instrument script functions like change_vol(), change_pitch(), etc.).
59 struct _Override {
60 float Volume; ///< as linear amplification ratio (1.0 being neutral)
61 float VolumeTime; ///< Transition duration (in seconds) for changes to @c Volume.
62 float Pitch; ///< as linear frequency ratio (1.0 being neutral)
63 float PitchTime; ///< Transition duration (in seconds) for changes to @c Pitch.
64 float Pan; ///< between -1.0 (most left) and +1.0 (most right) and 0.0 being neutral.
65 float PanTime; ///< Transition duration (in seconds) for changes to @c Pan.
66 int64_t PanSources; ///< Might be used for calculating an average pan value in differential way: amount of times the Pan value had been changed and shall be calculated relatively upon.
67 float Cutoff; ///< between 0.0 and 1.0
68 float Resonance; ///< between 0.0 and 1.0
69 float Attack; ///< between 0.0 and 1.0
70 float Decay; ///< between 0.0 and 1.0
71 float Sustain; ///< between 0.0 and 1.0
72 float Release; ///< between 0.0 and 1.0
73 float CutoffAttack; ///< between 0.0 and 1.0
74 float CutoffDecay; ///< between 0.0 and 1.0
75 float CutoffSustain;///< between 0.0 and 1.0
76 float CutoffRelease;///< between 0.0 and 1.0
77 float AmpLFODepth; ///< between 0.0 and 1.0
78 float AmpLFOFreq; ///< between 0.0 and 1.0
79 float CutoffLFODepth;///< between 0.0 and 1.0
80 float CutoffLFOFreq; ///< between 0.0 and 1.0
81 float PitchLFODepth; ///< between 0.0 and 1.0
82 float PitchLFOFreq; ///< between 0.0 and 1.0
83 fade_curve_t VolumeCurve;
84 fade_curve_t PitchCurve;
85 fade_curve_t PanCurve;
86 int SampleOffset; ///< Where the sample shall start playback in microseconds (otherwise this is -1 for being ignored).
87 } Override;
88 /// Sampler format specific informations and variables.
89 union _Format {
90 /// Gigasampler/GigaStudio format specifics.
91 struct _Gig {
92 uint8_t DimMask; ///< May be used to override the Dimension zone to be selected for a new voice: each 1 bit means that respective bit shall be overridden by taking the respective bit from DimBits instead.
93 uint8_t DimBits; ///< Used only in conjunction with DimMask: Dimension bits that shall be selected.
94 } Gig;
95 } Format;
96 int userPar[4]; ///< Used only for real-time instrument script functions set_event_par() and get_event_par() to store script author's user specific data ($EVENT_PAR_0 to $EVENT_PAR_3).
97 protected:
98 NoteBase() : hostKey(0), parentNoteID(0), pChildNotes(NULL) {
99 Override.Volume = 1.f;
100 Override.VolumeTime = DEFAULT_NOTE_VOLUME_TIME_S;
101 Override.Pitch = 1.f;
102 Override.PitchTime = DEFAULT_NOTE_PITCH_TIME_S;
103 Override.Pan = 0.f;
104 Override.PanTime = DEFAULT_NOTE_PAN_TIME_S;
105 Override.PanSources = 0;
106 Override.Cutoff = 1.f;
107 Override.Resonance = 1.f;
108 Override.Attack = 1.f;
109 Override.Decay = 1.f;
110 Override.Sustain = 1.f;
111 Override.Release = 1.f;
112 Override.CutoffAttack = 1.f;
113 Override.CutoffDecay = 1.f;
114 Override.CutoffSustain = 1.f;
115 Override.CutoffRelease = 1.f;
116 Override.AmpLFODepth = 1.f;
117 Override.AmpLFOFreq = 1.f;
118 Override.CutoffLFODepth = 1.f;
119 Override.CutoffLFOFreq = 1.f;
120 Override.PitchLFODepth = 1.f;
121 Override.PitchLFOFreq = 1.f;
122 Override.VolumeCurve = DEFAULT_FADE_CURVE;
123 Override.PitchCurve = DEFAULT_FADE_CURVE;
124 Override.PanCurve = DEFAULT_FADE_CURVE;
125 Override.SampleOffset = -1;
126
127 Format = _Format();
128
129 userPar[0] = 0;
130 userPar[1] = 0;
131 userPar[2] = 0;
132 userPar[3] = 0;
133 }
134 };
135
136 /**
137 * Contains the voices caused by one specific note, as well as basic
138 * information about the note itself. You can see a Note object as one
139 * specific event in time where one or more voices were spawned at the same
140 * time and all those voices due to the same cause.
141 *
142 * For example when you press down and hold the sustain pedal, and then
143 * trigger the same note on the keyboard multiple times, for each key
144 * strokes a separate Note instance is created. Assuming you have a layered
145 * sound with 4 layers, then for each note that is triggered 4 voices will
146 * be spawned and assigned to the same Note object. By grouping those voices
147 * to one specific Note object, it allows to control the synthesis paramters
148 * of those layered voices simultaniously.
149 *
150 * If your instrument contains a real-time instrument script, then that
151 * script might also trigger additional voices programmatically (by
152 * calling the built-in script function play_note()). Each time the script
153 * calls play_note() a new Note instance is created and the script may then
154 * further control the voices of specific notes independently from each
155 * other. For example for each key stroke on your keyboard the instrument
156 * script might trigger 3 additional notes programmatically and assign a
157 * different tuning filter parameters for each one of the 3 notes
158 * independently.
159 */
160 template<class V>
161 class Note : public NoteBase {
162 public:
163 RTList<V>* pActiveVoices; ///< Contains the active voices associated with this note.
164
165 Note() : NoteBase(), pActiveVoices(NULL) {}
166
167 virtual ~Note() {
168 if (pChildNotes) delete pChildNotes;
169 if (pActiveVoices) delete pActiveVoices;
170 }
171
172 void init(Pool<V>* pVoicePool, Pool<note_id_t>* pNoteIDPool) {
173 if (pActiveVoices) delete pActiveVoices;
174 pActiveVoices = new RTList<V>(pVoicePool);
175 if (pChildNotes) delete pChildNotes;
176 pChildNotes = new RTList<note_id_t>(pNoteIDPool);
177 }
178
179 void reset() {
180 hostKey = 0;
181 parentNoteID = 0;
182 if (pChildNotes)
183 pChildNotes->clear();
184 cause = Event();
185 eventID = 0;
186 Override.Volume = 1.f;
187 Override.VolumeTime = DEFAULT_NOTE_VOLUME_TIME_S;
188 Override.Pitch = 1.f;
189 Override.PitchTime = DEFAULT_NOTE_PITCH_TIME_S;
190 Override.Pan = 0.f;
191 Override.PanTime = DEFAULT_NOTE_PAN_TIME_S;
192 Override.PanSources = 0;
193 Override.Cutoff = 1.f;
194 Override.Resonance = 1.f;
195 Override.Attack = 1.f;
196 Override.Decay = 1.f;
197 Override.Sustain = 1.f;
198 Override.Release = 1.f;
199 Override.CutoffAttack = 1.f;
200 Override.CutoffDecay = 1.f;
201 Override.CutoffSustain = 1.f;
202 Override.CutoffRelease = 1.f;
203 Override.AmpLFODepth = 1.f;
204 Override.AmpLFOFreq = 1.f;
205 Override.CutoffLFODepth = 1.f;
206 Override.CutoffLFOFreq = 1.f;
207 Override.PitchLFODepth = 1.f;
208 Override.PitchLFOFreq = 1.f;
209 Override.VolumeCurve = DEFAULT_FADE_CURVE;
210 Override.PitchCurve = DEFAULT_FADE_CURVE;
211 Override.PanCurve = DEFAULT_FADE_CURVE;
212 Override.SampleOffset = -1;
213 Format = _Format();
214 userPar[0] = 0;
215 userPar[1] = 0;
216 userPar[2] = 0;
217 userPar[3] = 0;
218 if (pActiveVoices) {
219 typename RTList<V>::Iterator itVoice = pActiveVoices->first();
220 typename RTList<V>::Iterator itVoicesEnd = pActiveVoices->end();
221 for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
222 itVoice->VoiceFreed();
223 }
224 pActiveVoices->clear();
225 }
226 }
227 };
228
229 } // namespace LinuxSampler
230
231 #endif // LS_NOTE_H

  ViewVC Help
Powered by ViewVC