/[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 2086 - (hide annotations) (download)
Sun Apr 25 12:51:30 2010 UTC (14 years ago) by persson
File size: 8515 byte(s)
* sfz engine: added support for transpose
* sfz engine: fixed crash when using sw_down/up
* sfz engine: improved logic for sw_lokey/hikey/up/down/last
* sfz parser: added more v1 aliases
* fixed building with newer MinGW-w64

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 2072 * Copyright (C) 2009-2010 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    
31     /**
32     * Reacts on supported control change commands (e.g. pitch bend wheel,
33     * modulation wheel, aftertouch).
34     *
35     * @param pEngineChannel - engine channel on which this event occured on
36     * @param itControlChangeEvent - controller, value and time stamp of the event
37     */
38     void Engine::ProcessControlChange (
39     LinuxSampler::EngineChannel* pEngineChannel,
40     Pool<Event>::Iterator& itControlChangeEvent
41     ) {
42     dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", itControlChangeEvent->Param.CC.Controller, itControlChangeEvent->Param.CC.Value));
43    
44     EngineChannel* pChannel = dynamic_cast<EngineChannel*>(pEngineChannel);
45     // handle the "control triggered" MIDI rule: a control change
46     // event can trigger a new note on or note off event
47     if (pChannel->pInstrument) {
48    
49     // TODO:
50     }
51    
52     // update controller value in the engine channel's controller table
53     pChannel->ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value;
54    
55     ProcessHardcodedControllers(pEngineChannel, itControlChangeEvent);
56    
57     // handle FX send controllers
58     ProcessFxSendControllers(pChannel, itControlChangeEvent);
59     }
60    
61     DiskThread* Engine::CreateDiskThread() {
62     return new DiskThread (
63     iMaxDiskStreams,
64     ((pAudioOutputDevice->MaxSamplesPerCycle() << CONFIG_MAX_PITCH) << 1) + 6, //FIXME: assuming stereo
65     &instruments
66     );
67     }
68    
69     void Engine::TriggerNewVoices (
70     LinuxSampler::EngineChannel* pEngineChannel,
71     RTList<Event>::Iterator& itNoteOnEvent,
72     bool HandleKeyGroupConflicts
73     ) {
74     EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel);
75    
76     uint8_t chan = pChannel->MidiChannel();
77     int key = itNoteOnEvent->Param.Note.Key;
78     uint8_t vel = itNoteOnEvent->Param.Note.Velocity;
79     int bend = pChannel->Pitch;
80     uint8_t chanaft = pChannel->ControllerTable[128];
81     uint8_t* cc = pChannel->ControllerTable;
82 persson 2058 ::sfz::trigger_t trig = TRIGGER_ATTACK |
83     ((pChannel->LastKey != -1 &&
84     pChannel->PressedKeys[pChannel->LastKey] &&
85     pChannel->LastKey != key) ?
86     TRIGGER_LEGATO : TRIGGER_FIRST);
87 iliev 2012
88     pChannel->regionsTemp = pChannel->pInstrument->GetRegionsOnKey (
89 persson 2072 chan, key, vel, bend, 0, chanaft, 0, 0, Random(), trig, cc,
90     0.0f, pChannel->PressedKeys, pChannel->LastKeySwitch, pChannel->LastKey
91 iliev 2012 );
92    
93     for (int i = 0; i < pChannel->regionsTemp.size(); i++) {
94     if (!RegionSuspended(pChannel->regionsTemp[i])) {
95     LaunchVoice(pChannel, itNoteOnEvent, i, false, true, HandleKeyGroupConflicts);
96     }
97     }
98     }
99    
100     void Engine::TriggerReleaseVoices (
101     LinuxSampler::EngineChannel* pEngineChannel,
102     RTList<Event>::Iterator& itNoteOffEvent
103     ) {
104     EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel);
105     uint8_t chan = pChannel->MidiChannel();
106     int key = itNoteOffEvent->Param.Note.Key;
107 persson 2061
108     // MIDI note-on velocity is used instead of note-off velocity
109     uint8_t vel = pChannel->pMIDIKeyInfo[key].Velocity;
110     itNoteOffEvent->Param.Note.Velocity = vel;
111    
112 iliev 2012 int bend = pChannel->Pitch;
113     uint8_t chanaft = pChannel->ControllerTable[128];
114     uint8_t* cc = pChannel->ControllerTable;
115     ::sfz::trigger_t trig = TRIGGER_RELEASE;
116    
117     pChannel->regionsTemp = pChannel->pInstrument->GetRegionsOnKey (
118 persson 2086 chan, key, vel, bend, 0, chanaft, 0, 0, Random(), trig, cc,
119     0.0f, pChannel->PressedKeys, pChannel->LastKeySwitch, pChannel->LastKey
120 iliev 2012 );
121    
122     // now launch the required amount of voices
123     for (int i = 0; i < pChannel->regionsTemp.size(); i++) {
124     LaunchVoice(pChannel, itNoteOffEvent, i, true, false, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples
125     }
126     }
127    
128     Pool<Voice>::Iterator Engine::LaunchVoice (
129     LinuxSampler::EngineChannel* pEngineChannel,
130     Pool<Event>::Iterator& itNoteOnEvent,
131     int iLayer,
132     bool ReleaseTriggerVoice,
133     bool VoiceStealing,
134     bool HandleKeyGroupConflicts
135     ) {
136     EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel);
137     int key = itNoteOnEvent->Param.Note.Key;
138     EngineChannel::MidiKey* pKey = &pChannel->pMIDIKeyInfo[key];
139 persson 2061 Voice::type_t VoiceType = (ReleaseTriggerVoice) ? Voice::type_release_trigger : (!iLayer) ? Voice::type_release_trigger_required : Voice::type_normal;
140 iliev 2012
141     Pool<Voice>::Iterator itNewVoice;
142     ::sfz::Region* pRgn = pChannel->regionsTemp[iLayer];
143    
144     // no need to process if sample is silent
145 iliev 2027 if (!pRgn->GetSample() || !pRgn->GetSample()->GetTotalFrameCount()) return Pool<Voice>::Iterator();
146 iliev 2012
147 iliev 2027 // only mark the first voice of a layered voice (group) to be in a
148     // key group, so the layered voices won't kill each other
149     int iKeyGroup = (iLayer == 0 && !ReleaseTriggerVoice) ? pRgn->group : 0;
150 persson 2063 if (HandleKeyGroupConflicts) pChannel->HandleKeyGroupConflicts(iKeyGroup, itNoteOnEvent, pRgn->off_mode == ::sfz::OFF_NORMAL);
151 iliev 2027
152 iliev 2012 // allocate a new voice for the key
153     itNewVoice = pKey->pActiveVoices->allocAppend();
154 iliev 2027 int res = InitNewVoice (
155     pChannel, pRgn, itNoteOnEvent, VoiceType, iLayer,
156     iKeyGroup, ReleaseTriggerVoice, VoiceStealing, itNewVoice
157     );
158     if (!res) return itNewVoice;
159 iliev 2012
160     // return if this is a release triggered voice and there is no
161     // releasetrigger dimension (could happen if an instrument
162     // change has occured between note on and off)
163     //if (ReleaseTriggerVoice && VoiceType != Voice::type_release_trigger) return Pool<Voice>::Iterator();
164    
165 iliev 2027 return Pool<Voice>::Iterator(); // no free voice or error
166 iliev 2012 }
167    
168     bool Engine::DiskStreamSupported() {
169     return true;
170     }
171    
172     String Engine::Description() {
173     return "SFZ Format Engine";
174     }
175    
176     String Engine::Version() {
177 persson 2086 String s = "$Revision: 1.7 $";
178 iliev 2012 return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword
179     }
180    
181     }} // namespace LinuxSampler::sfz

  ViewVC Help
Powered by ViewVC