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 |
2114 |
* 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 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 |
schoenebeck |
2559 |
void Engine::ProcessChannelPressure(LinuxSampler::EngineChannel* pEngineChannel, Pool<Event>::Iterator& itChannelPressureEvent) { |
62 |
schoenebeck |
3015 |
// forward this to the CC routine, so it updates the current aftertouch value |
63 |
|
|
ProcessControlChange(pEngineChannel, itChannelPressureEvent); |
64 |
|
|
|
65 |
schoenebeck |
2559 |
// if required: engine global aftertouch handling (apart from the per voice handling) |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
void Engine::ProcessPolyphonicKeyPressure(LinuxSampler::EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNotePressureEvent) { |
69 |
|
|
// if required: engine global aftertouch handling (apart from the per voice handling) |
70 |
|
|
} |
71 |
|
|
|
72 |
iliev |
2012 |
DiskThread* Engine::CreateDiskThread() { |
73 |
|
|
return new DiskThread ( |
74 |
|
|
iMaxDiskStreams, |
75 |
|
|
((pAudioOutputDevice->MaxSamplesPerCycle() << CONFIG_MAX_PITCH) << 1) + 6, //FIXME: assuming stereo |
76 |
|
|
&instruments |
77 |
|
|
); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
void Engine::TriggerNewVoices ( |
81 |
|
|
LinuxSampler::EngineChannel* pEngineChannel, |
82 |
|
|
RTList<Event>::Iterator& itNoteOnEvent, |
83 |
|
|
bool HandleKeyGroupConflicts |
84 |
|
|
) { |
85 |
|
|
EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel); |
86 |
|
|
|
87 |
|
|
uint8_t chan = pChannel->MidiChannel(); |
88 |
|
|
int key = itNoteOnEvent->Param.Note.Key; |
89 |
|
|
uint8_t vel = itNoteOnEvent->Param.Note.Velocity; |
90 |
|
|
int bend = pChannel->Pitch; |
91 |
|
|
uint8_t chanaft = pChannel->ControllerTable[128]; |
92 |
|
|
uint8_t* cc = pChannel->ControllerTable; |
93 |
|
|
|
94 |
schoenebeck |
2879 |
NoteIterator itNote = GetNotePool()->fromID(itNoteOnEvent->Param.Note.ID); |
95 |
|
|
if (!itNote) { |
96 |
|
|
dmsg(1,("sf2::Engine: No Note object for triggering new voices!\n")); |
97 |
|
|
return; |
98 |
|
|
} |
99 |
|
|
|
100 |
persson |
2101 |
int layer = 0; |
101 |
|
|
::sf2::Query query(*pChannel->pInstrument); |
102 |
|
|
query.key = key; |
103 |
|
|
query.vel = vel; |
104 |
|
|
while (::sf2::Region* region = query.next()) { |
105 |
iliev |
2027 |
// TODO: Generators in the PGEN sub-chunk are applied relative to generators in the IGEN sub-chunk in an additive manner. In |
106 |
|
|
// other words, PGEN generators increase or decrease the value of an IGEN generator. |
107 |
persson |
2101 |
::sf2::Query subQuery(*region->pInstrument); |
108 |
|
|
subQuery.key = key; |
109 |
|
|
subQuery.vel = vel; |
110 |
|
|
while (::sf2::Region* r = subQuery.next()) { |
111 |
|
|
//std::cout << r->GetSample()->GetName(); |
112 |
|
|
//std::cout << " loKey: " << r->loKey << " hiKey: " << r->hiKey << " minVel: " << r->minVel << " maxVel: " << r->maxVel << " Vel: " << ((int)vel) << std::endl << std::endl; |
113 |
|
|
if (!RegionSuspended(r)) { |
114 |
|
|
itNoteOnEvent->Param.Note.pRegion = r; |
115 |
schoenebeck |
2879 |
VoiceIterator itNewVoice = |
116 |
|
|
LaunchVoice(pChannel, itNoteOnEvent, layer, false, true, HandleKeyGroupConflicts); |
117 |
|
|
if (itNewVoice) |
118 |
|
|
itNewVoice.moveToEndOf(itNote->pActiveVoices); |
119 |
persson |
2101 |
} |
120 |
|
|
layer++; |
121 |
iliev |
2027 |
} |
122 |
iliev |
2012 |
} |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
void Engine::TriggerReleaseVoices ( |
126 |
|
|
LinuxSampler::EngineChannel* pEngineChannel, |
127 |
|
|
RTList<Event>::Iterator& itNoteOffEvent |
128 |
|
|
) { |
129 |
persson |
2101 |
|
130 |
iliev |
2012 |
} |
131 |
|
|
|
132 |
|
|
Pool<Voice>::Iterator Engine::LaunchVoice ( |
133 |
|
|
LinuxSampler::EngineChannel* pEngineChannel, |
134 |
|
|
Pool<Event>::Iterator& itNoteOnEvent, |
135 |
|
|
int iLayer, |
136 |
|
|
bool ReleaseTriggerVoice, |
137 |
|
|
bool VoiceStealing, |
138 |
|
|
bool HandleKeyGroupConflicts |
139 |
|
|
) { |
140 |
|
|
EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel); |
141 |
|
|
int key = itNoteOnEvent->Param.Note.Key; |
142 |
schoenebeck |
2879 |
//EngineChannel::MidiKey* pKey = &pChannel->pMIDIKeyInfo[key]; |
143 |
iliev |
2012 |
|
144 |
|
|
Voice::type_t VoiceType = Voice::type_normal; |
145 |
|
|
|
146 |
|
|
Pool<Voice>::Iterator itNewVoice; |
147 |
persson |
2101 |
::sf2::Region* pRgn = static_cast< ::sf2::Region*>(itNoteOnEvent->Param.Note.pRegion); |
148 |
iliev |
2012 |
|
149 |
|
|
// no need to process if sample is silent |
150 |
iliev |
2027 |
if (!pRgn->GetSample() || !pRgn->GetSample()->GetTotalFrameCount()) return Pool<Voice>::Iterator(); |
151 |
iliev |
2012 |
|
152 |
persson |
2114 |
int iKeyGroup = pRgn->exclusiveClass; |
153 |
iliev |
2027 |
if (HandleKeyGroupConflicts) pChannel->HandleKeyGroupConflicts(iKeyGroup, itNoteOnEvent); |
154 |
|
|
|
155 |
iliev |
2012 |
// allocate a new voice for the key |
156 |
schoenebeck |
2879 |
itNewVoice = GetVoicePool()->allocAppend(); |
157 |
iliev |
2027 |
int res = InitNewVoice ( |
158 |
|
|
pChannel, pRgn, itNoteOnEvent, VoiceType, iLayer, |
159 |
|
|
iKeyGroup, ReleaseTriggerVoice, VoiceStealing, itNewVoice |
160 |
|
|
); |
161 |
|
|
if (!res) return itNewVoice; |
162 |
iliev |
2012 |
|
163 |
|
|
// return if this is a release triggered voice and there is no |
164 |
|
|
// releasetrigger dimension (could happen if an instrument |
165 |
|
|
// change has occured between note on and off) |
166 |
|
|
//if (ReleaseTriggerVoice && VoiceType != Voice::type_release_trigger) return Pool<Voice>::Iterator(); |
167 |
|
|
|
168 |
iliev |
2027 |
return Pool<Voice>::Iterator(); // no free voice or error |
169 |
iliev |
2012 |
} |
170 |
|
|
|
171 |
|
|
bool Engine::DiskStreamSupported() { |
172 |
|
|
return true; |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
String Engine::Description() { |
176 |
|
|
return "SoundFont Format Engine"; |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
String Engine::Version() { |
180 |
schoenebeck |
2494 |
String s = "$Revision$"; |
181 |
iliev |
2012 |
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
}} // namespace LinuxSampler::sf2 |