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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2101 - (hide annotations) (download)
Sun May 30 11:40:31 2010 UTC (13 years, 10 months ago) by persson
File size: 7684 byte(s)
* sfz/sf2 engines: RT-safeness: avoid malloc in audio thread
* sfz/sf2 engines: fixed a bug that could cause voice stealing to fail

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     * Copyright (C) 2009 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 sf2 {
29     Engine::Format Engine::GetEngineFormat() { return SF2; }
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 persson 2101 // TODO:
50 iliev 2012 }
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    
83 persson 2101 int layer = 0;
84     ::sf2::Query query(*pChannel->pInstrument);
85     query.key = key;
86     query.vel = vel;
87     while (::sf2::Region* region = query.next()) {
88 iliev 2027 // TODO: Generators in the PGEN sub-chunk are applied relative to generators in the IGEN sub-chunk in an additive manner. In
89     // other words, PGEN generators increase or decrease the value of an IGEN generator.
90 persson 2101 ::sf2::Query subQuery(*region->pInstrument);
91     subQuery.key = key;
92     subQuery.vel = vel;
93     while (::sf2::Region* r = subQuery.next()) {
94     //std::cout << r->GetSample()->GetName();
95     //std::cout << " loKey: " << r->loKey << " hiKey: " << r->hiKey << " minVel: " << r->minVel << " maxVel: " << r->maxVel << " Vel: " << ((int)vel) << std::endl << std::endl;
96     if (!RegionSuspended(r)) {
97     itNoteOnEvent->Param.Note.pRegion = r;
98     LaunchVoice(pChannel, itNoteOnEvent, layer, false, true, HandleKeyGroupConflicts);
99     }
100     layer++;
101 iliev 2027 }
102 iliev 2012 }
103     }
104    
105     void Engine::TriggerReleaseVoices (
106     LinuxSampler::EngineChannel* pEngineChannel,
107     RTList<Event>::Iterator& itNoteOffEvent
108     ) {
109 persson 2101
110 iliev 2012 }
111    
112     Pool<Voice>::Iterator Engine::LaunchVoice (
113     LinuxSampler::EngineChannel* pEngineChannel,
114     Pool<Event>::Iterator& itNoteOnEvent,
115     int iLayer,
116     bool ReleaseTriggerVoice,
117     bool VoiceStealing,
118     bool HandleKeyGroupConflicts
119     ) {
120     EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel);
121     int key = itNoteOnEvent->Param.Note.Key;
122 iliev 2027 EngineChannel::MidiKey* pKey = &pChannel->pMIDIKeyInfo[key];
123 iliev 2012
124     Voice::type_t VoiceType = Voice::type_normal;
125    
126     Pool<Voice>::Iterator itNewVoice;
127 persson 2101 ::sf2::Region* pRgn = static_cast< ::sf2::Region*>(itNoteOnEvent->Param.Note.pRegion);
128 iliev 2012
129     // no need to process if sample is silent
130 iliev 2027 if (!pRgn->GetSample() || !pRgn->GetSample()->GetTotalFrameCount()) return Pool<Voice>::Iterator();
131 iliev 2012
132 iliev 2027 // only mark the first voice of a layered voice (group) to be in a
133     // key group, so the layered voices won't kill each other
134     int iKeyGroup = (iLayer == 0 && !ReleaseTriggerVoice) ? pRgn->exclusiveClass : 0;
135     if (HandleKeyGroupConflicts) pChannel->HandleKeyGroupConflicts(iKeyGroup, itNoteOnEvent);
136    
137 iliev 2012 // allocate a new voice for the key
138     itNewVoice = pKey->pActiveVoices->allocAppend();
139 iliev 2027 int res = InitNewVoice (
140     pChannel, pRgn, itNoteOnEvent, VoiceType, iLayer,
141     iKeyGroup, ReleaseTriggerVoice, VoiceStealing, itNewVoice
142     );
143     if (!res) return itNewVoice;
144 iliev 2012
145     // return if this is a release triggered voice and there is no
146     // releasetrigger dimension (could happen if an instrument
147     // change has occured between note on and off)
148     //if (ReleaseTriggerVoice && VoiceType != Voice::type_release_trigger) return Pool<Voice>::Iterator();
149    
150 iliev 2027 return Pool<Voice>::Iterator(); // no free voice or error
151 iliev 2012 }
152    
153     bool Engine::DiskStreamSupported() {
154     return true;
155     }
156    
157     String Engine::Description() {
158     return "SoundFont Format Engine";
159     }
160    
161     String Engine::Version() {
162 persson 2101 String s = "$Revision: 1.3 $";
163 iliev 2012 return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword
164     }
165    
166     }} // namespace LinuxSampler::sf2

  ViewVC Help
Powered by ViewVC