/[svn]/linuxsampler/trunk/src/engines/sfz/Engine.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/engines/sfz/Engine.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2317 - (show annotations) (download)
Sun Feb 19 12:13:19 2012 UTC (12 years, 1 month ago) by persson
File size: 11194 byte(s)
* sfz engine bugfix: looping was disabled if loop_start was set to 0
* sfz engine: allow regions with end=-1 to turn off other regions
  using the group and off_by opcodes (#168)
* sfz engine: made end=0 play the whole sample
* sfz engine: fixed support for lochan and hichan opcodes (#155)
* bumped version to 1.0.0.svn17

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck *
6 * Copyright (C) 2005-2009 Christian Schoenebeck *
7 * Copyright (C) 2009-2012 Grigor Iliev *
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (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 *
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, write to the Free Software *
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
22 * MA 02111-1307 USA *
23 ***************************************************************************/
24
25 #include "Engine.h"
26 #include "EngineChannel.h"
27
28 namespace LinuxSampler { namespace sfz {
29 Engine::Format Engine::GetEngineFormat() { return SFZ; }
30
31 Engine::Engine() {
32 pCCPool = new Pool<CCSignalUnit::CC>(GLOBAL_MAX_VOICES * MaxCCPerVoice);
33 pSmootherPool = new Pool<Smoother>(GLOBAL_MAX_VOICES * MaxCCPerVoice);
34 for (VoiceIterator iterVoice = GetVoicePool()->allocAppend(); iterVoice == GetVoicePool()->last(); iterVoice = GetVoicePool()->allocAppend()) {
35 (static_cast<SfzSignalUnitRack*>(iterVoice->pSignalUnitRack))->InitRTLists();
36 }
37 GetVoicePool()->clear();
38 }
39
40 Engine::~Engine() {
41 if (pCCPool) {
42 pCCPool->clear();
43 delete pCCPool;
44 }
45
46 if (pSmootherPool) {
47 pSmootherPool->clear();
48 delete pSmootherPool;
49 }
50 }
51
52 void Engine::PostSetMaxVoices(int iVoices) {
53 pCCPool->resizePool(iVoices * MaxCCPerVoice);
54 pSmootherPool->resizePool(iVoices * MaxCCPerVoice);
55
56 for (VoiceIterator iterVoice = GetVoicePool()->allocAppend(); iterVoice == GetVoicePool()->last(); iterVoice = GetVoicePool()->allocAppend()) {
57 (static_cast<SfzSignalUnitRack*>(iterVoice->pSignalUnitRack))->InitRTLists();
58 }
59 GetVoicePool()->clear();
60 }
61
62 /**
63 * Reacts on supported control change commands (e.g. pitch bend wheel,
64 * modulation wheel, aftertouch).
65 *
66 * @param pEngineChannel - engine channel on which this event occured on
67 * @param itControlChangeEvent - controller, value and time stamp of the event
68 */
69 void Engine::ProcessControlChange (
70 LinuxSampler::EngineChannel* pEngineChannel,
71 Pool<Event>::Iterator& itControlChangeEvent
72 ) {
73 uint8_t cc = itControlChangeEvent->Param.CC.Controller;
74 dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", cc, itControlChangeEvent->Param.CC.Value));
75
76 EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel);
77
78 // update controller value in the engine channel's controller table
79 pChannel->ControllerTable[cc] = itControlChangeEvent->Param.CC.Value;
80
81 ProcessHardcodedControllers(pEngineChannel, itControlChangeEvent);
82
83 // handle FX send controllers
84 ProcessFxSendControllers(pChannel, itControlChangeEvent);
85
86 // handle control triggered regions: a control change event
87 // can trigger a new voice
88 if (pChannel->pInstrument && cc < 128) {
89
90 ::sfz::Query q;
91 q.chan = itControlChangeEvent->Param.CC.Channel + 1;
92 q.key = 60;
93 q.vel = 127;
94 q.bend = pChannel->Pitch;
95 q.bpm = 0;
96 q.chanaft = pChannel->ControllerTable[128];
97 q.polyaft = 0;
98 q.prog = 0;
99 q.rand = Random();
100 q.cc = pChannel->ControllerTable;
101 q.timer = 0;
102 q.sw = pChannel->PressedKeys;
103 q.last_sw_key = pChannel->LastKeySwitch;
104 q.prev_sw_key = pChannel->LastKey;
105 q.trig = TRIGGER_ATTACK | TRIGGER_FIRST;
106
107 q.search(pChannel->pInstrument, cc);
108
109 int i = 0;
110 while (::sfz::Region* region = q.next()) {
111 if (!RegionSuspended(region)) {
112 itControlChangeEvent->Param.Note.Key = 60;
113 itControlChangeEvent->Param.Note.Velocity = 127;
114 itControlChangeEvent->Param.Note.pRegion = region;
115 LaunchVoice(pChannel, itControlChangeEvent, i, false, false, true);
116 }
117 i++;
118 }
119 }
120 }
121
122 DiskThread* Engine::CreateDiskThread() {
123 return new DiskThread (
124 iMaxDiskStreams,
125 ((pAudioOutputDevice->MaxSamplesPerCycle() << CONFIG_MAX_PITCH) << 1) + 6, //FIXME: assuming stereo
126 &instruments
127 );
128 }
129
130 void Engine::TriggerNewVoices (
131 LinuxSampler::EngineChannel* pEngineChannel,
132 RTList<Event>::Iterator& itNoteOnEvent,
133 bool HandleKeyGroupConflicts
134 ) {
135 EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel);
136 ::sfz::Query q;
137 q.chan = itNoteOnEvent->Param.Note.Channel + 1;
138 q.key = itNoteOnEvent->Param.Note.Key;
139 q.vel = itNoteOnEvent->Param.Note.Velocity;
140 q.bend = pChannel->Pitch;
141 q.bpm = 0;
142 q.chanaft = pChannel->ControllerTable[128];
143 q.polyaft = 0;
144 q.prog = 0;
145 q.rand = Random();
146 q.cc = pChannel->ControllerTable;
147 q.timer = 0;
148 q.sw = pChannel->PressedKeys;
149 q.last_sw_key = pChannel->LastKeySwitch;
150 q.prev_sw_key = pChannel->LastKey;
151 q.trig = TRIGGER_ATTACK |
152 ((pChannel->LastKey != -1 &&
153 pChannel->PressedKeys[pChannel->LastKey] &&
154 pChannel->LastKey != q.key) ?
155 TRIGGER_LEGATO : TRIGGER_FIRST);
156
157 q.search(pChannel->pInstrument);
158
159 int i = 0;
160 while (::sfz::Region* region = q.next()) {
161 if (!RegionSuspended(region)) {
162 itNoteOnEvent->Param.Note.pRegion = region;
163 LaunchVoice(pChannel, itNoteOnEvent, i, false, true, HandleKeyGroupConflicts);
164 }
165 i++;
166 }
167 }
168
169 void Engine::TriggerReleaseVoices (
170 LinuxSampler::EngineChannel* pEngineChannel,
171 RTList<Event>::Iterator& itNoteOffEvent
172 ) {
173 EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel);
174 ::sfz::Query q;
175 q.chan = itNoteOffEvent->Param.Note.Channel + 1;
176 q.key = itNoteOffEvent->Param.Note.Key;
177
178 // MIDI note-on velocity is used instead of note-off velocity
179 q.vel = pChannel->pMIDIKeyInfo[q.key].Velocity;
180 itNoteOffEvent->Param.Note.Velocity = q.vel;
181
182 q.bend = pChannel->Pitch;
183 q.bpm = 0;
184 q.chanaft = pChannel->ControllerTable[128];
185 q.polyaft = 0;
186 q.prog = 0;
187 q.rand = Random();
188 q.cc = pChannel->ControllerTable;
189 q.timer = 0;
190 q.sw = pChannel->PressedKeys;
191 q.last_sw_key = pChannel->LastKeySwitch;
192 q.prev_sw_key = pChannel->LastKey;
193 q.trig = TRIGGER_RELEASE;
194
195 q.search(pChannel->pInstrument);
196
197 // now launch the required amount of voices
198 int i = 0;
199 while (::sfz::Region* region = q.next()) {
200 itNoteOffEvent->Param.Note.pRegion = region;
201 LaunchVoice(pChannel, itNoteOffEvent, i, true, false, true); //FIXME: for the moment we don't perform voice stealing for release triggered samples
202 i++;
203 }
204 }
205
206 Pool<Voice>::Iterator Engine::LaunchVoice (
207 LinuxSampler::EngineChannel* pEngineChannel,
208 Pool<Event>::Iterator& itNoteOnEvent,
209 int iLayer,
210 bool ReleaseTriggerVoice,
211 bool VoiceStealing,
212 bool HandleKeyGroupConflicts
213 ) {
214 EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel);
215 int key = itNoteOnEvent->Param.Note.Key;
216 EngineChannel::MidiKey* pKey = &pChannel->pMIDIKeyInfo[key];
217 ::sfz::Region* pRgn = static_cast< ::sfz::Region*>(itNoteOnEvent->Param.Note.pRegion);
218
219 Voice::type_t VoiceType =
220 itNoteOnEvent->Type == Event::type_control_change ? Voice::type_controller_triggered :
221 ReleaseTriggerVoice ? Voice::type_release_trigger :
222 iLayer == 0 ? Voice::type_release_trigger_required :
223 Voice::type_normal;
224 if (pRgn->loop_mode == ::sfz::ONE_SHOT) VoiceType |= Voice::type_one_shot;
225
226 Pool<Voice>::Iterator itNewVoice;
227
228 if (HandleKeyGroupConflicts) pChannel->HandleKeyGroupConflicts(pRgn->group, itNoteOnEvent);
229
230 // no need to process if sample is silent
231 if (!pRgn->GetSample(false) || !pRgn->GetSample()->GetTotalFrameCount()) return Pool<Voice>::Iterator();
232
233 // allocate a new voice for the key
234 itNewVoice = pKey->pActiveVoices->allocAppend();
235 int res = InitNewVoice (
236 pChannel, pRgn, itNoteOnEvent, VoiceType, iLayer,
237 pRgn->off_by, ReleaseTriggerVoice, VoiceStealing, itNewVoice
238 );
239 if (!res) return itNewVoice;
240
241 // return if this is a release triggered voice and there is no
242 // releasetrigger dimension (could happen if an instrument
243 // change has occured between note on and off)
244 //if (ReleaseTriggerVoice && VoiceType != Voice::type_release_trigger) return Pool<Voice>::Iterator();
245
246 return Pool<Voice>::Iterator(); // no free voice or error
247 }
248
249 bool Engine::DiskStreamSupported() {
250 return true;
251 }
252
253 String Engine::Description() {
254 return "SFZ Format Engine";
255 }
256
257 String Engine::Version() {
258 String s = "$Revision: 1.11 $";
259 return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword
260 }
261
262 }} // namespace LinuxSampler::sfz

  ViewVC Help
Powered by ViewVC