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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2494 - (hide annotations) (download)
Wed Jan 1 17:48:01 2014 UTC (10 years, 3 months ago) by schoenebeck
File size: 11187 byte(s)
* Enabled automatic svn "Revision" macro expansion on certain files.
* Bumped version to 1.0.0.svn24.

1 iliev 2012 /***************************************************************************
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 persson 2317 * Copyright (C) 2009-2012 Grigor Iliev *
8 iliev 2012 * *
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 iliev 2244
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 iliev 2012
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 persson 2115 uint8_t cc = itControlChangeEvent->Param.CC.Controller;
74     dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", cc, itControlChangeEvent->Param.CC.Value));
75 iliev 2012
76 persson 2106 EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel);
77 iliev 2012
78     // update controller value in the engine channel's controller table
79 persson 2115 pChannel->ControllerTable[cc] = itControlChangeEvent->Param.CC.Value;
80 iliev 2012
81     ProcessHardcodedControllers(pEngineChannel, itControlChangeEvent);
82    
83     // handle FX send controllers
84     ProcessFxSendControllers(pChannel, itControlChangeEvent);
85 persson 2115
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 persson 2317 q.chan = itControlChangeEvent->Param.CC.Channel + 1;
92 persson 2115 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 iliev 2012 }
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 persson 2106 ::sfz::Query q;
137 persson 2317 q.chan = itNoteOnEvent->Param.Note.Channel + 1;
138 persson 2101 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 iliev 2012
157 persson 2106 q.search(pChannel->pInstrument);
158    
159 persson 2101 int i = 0;
160     while (::sfz::Region* region = q.next()) {
161     if (!RegionSuspended(region)) {
162     itNoteOnEvent->Param.Note.pRegion = region;
163 iliev 2012 LaunchVoice(pChannel, itNoteOnEvent, i, false, true, HandleKeyGroupConflicts);
164     }
165 persson 2101 i++;
166 iliev 2012 }
167     }
168    
169     void Engine::TriggerReleaseVoices (
170     LinuxSampler::EngineChannel* pEngineChannel,
171     RTList<Event>::Iterator& itNoteOffEvent
172     ) {
173     EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel);
174 persson 2106 ::sfz::Query q;
175 persson 2317 q.chan = itNoteOffEvent->Param.Note.Channel + 1;
176 persson 2101 q.key = itNoteOffEvent->Param.Note.Key;
177 persson 2061
178     // MIDI note-on velocity is used instead of note-off velocity
179 persson 2101 q.vel = pChannel->pMIDIKeyInfo[q.key].Velocity;
180     itNoteOffEvent->Param.Note.Velocity = q.vel;
181 persson 2061
182 persson 2101 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 iliev 2012
195 persson 2106 q.search(pChannel->pInstrument);
196    
197 iliev 2012 // now launch the required amount of voices
198 persson 2101 int i = 0;
199     while (::sfz::Region* region = q.next()) {
200     itNoteOffEvent->Param.Note.pRegion = region;
201 persson 2114 LaunchVoice(pChannel, itNoteOffEvent, i, true, false, true); //FIXME: for the moment we don't perform voice stealing for release triggered samples
202 persson 2101 i++;
203 iliev 2012 }
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 persson 2115 ::sfz::Region* pRgn = static_cast< ::sfz::Region*>(itNoteOnEvent->Param.Note.pRegion);
218 iliev 2012
219 persson 2115 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 iliev 2012 Pool<Voice>::Iterator itNewVoice;
227    
228 persson 2317 if (HandleKeyGroupConflicts) pChannel->HandleKeyGroupConflicts(pRgn->group, itNoteOnEvent);
229    
230 iliev 2012 // no need to process if sample is silent
231 persson 2317 if (!pRgn->GetSample(false) || !pRgn->GetSample()->GetTotalFrameCount()) return Pool<Voice>::Iterator();
232 iliev 2012
233     // allocate a new voice for the key
234     itNewVoice = pKey->pActiveVoices->allocAppend();
235 iliev 2027 int res = InitNewVoice (
236     pChannel, pRgn, itNoteOnEvent, VoiceType, iLayer,
237 persson 2114 pRgn->off_by, ReleaseTriggerVoice, VoiceStealing, itNewVoice
238 iliev 2027 );
239     if (!res) return itNewVoice;
240 iliev 2012
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 iliev 2027 return Pool<Voice>::Iterator(); // no free voice or error
247 iliev 2012 }
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 schoenebeck 2494 String s = "$Revision$";
259 iliev 2012 return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword
260     }
261    
262     }} // namespace LinuxSampler::sfz

Properties

Name Value
svn:keywords Revision

  ViewVC Help
Powered by ViewVC