1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 Christian Schoenebeck * |
7 |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
10 |
* the Free Software Foundation; either version 2 of the License, or * |
11 |
* (at your option) any later version. * |
12 |
* * |
13 |
* This program is distributed in the hope that it will be useful, * |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
* GNU General Public License for more details. * |
17 |
* * |
18 |
* You should have received a copy of the GNU General Public License * |
19 |
* along with this program; if not, write to the Free Software * |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
23 |
|
24 |
#include "EngineChannel.h" |
25 |
|
26 |
namespace LinuxSampler { namespace gig { |
27 |
|
28 |
EngineChannel::EngineChannel() { |
29 |
pMIDIKeyInfo = new midi_key_info_t[128]; |
30 |
pEngine = NULL; |
31 |
pInstrument = NULL; |
32 |
pEventQueue = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0); |
33 |
pActiveKeys = new Pool<uint>(128); |
34 |
for (uint i = 0; i < 128; i++) { |
35 |
pMIDIKeyInfo[i].pActiveVoices = NULL; // we allocate when we retrieve the right Engine object |
36 |
pMIDIKeyInfo[i].KeyPressed = false; |
37 |
pMIDIKeyInfo[i].Active = false; |
38 |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
39 |
pMIDIKeyInfo[i].pEvents = NULL; // we allocate when we retrieve the right Engine object |
40 |
} |
41 |
InstrumentIdx = -1; |
42 |
InstrumentStat = -1; |
43 |
AudioDeviceChannelLeft = -1; |
44 |
AudioDeviceChannelRight = -1; |
45 |
} |
46 |
|
47 |
EngineChannel::~EngineChannel() { |
48 |
if (pInstrument) Engine::instruments.HandBack(pInstrument, this); |
49 |
for (uint i = 0; i < 128; i++) { |
50 |
if (pMIDIKeyInfo[i].pActiveVoices) { |
51 |
pMIDIKeyInfo[i].pActiveVoices->clear(); |
52 |
delete pMIDIKeyInfo[i].pActiveVoices; |
53 |
} |
54 |
if (pMIDIKeyInfo[i].pEvents) { |
55 |
pMIDIKeyInfo[i].pEvents->clear(); |
56 |
delete pMIDIKeyInfo[i].pEvents; |
57 |
} |
58 |
} |
59 |
if (pEventQueue) delete pEventQueue; |
60 |
if (pActiveKeys) delete pActiveKeys; |
61 |
if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; |
62 |
} |
63 |
|
64 |
/** |
65 |
* This method is not thread safe! |
66 |
*/ |
67 |
void EngineChannel::ResetInternal() { |
68 |
Pitch = 0; |
69 |
SustainPedal = false; |
70 |
GlobalVolume = 1.0; |
71 |
GlobalPanLeft = 1.0f; |
72 |
GlobalPanRight = 1.0f; |
73 |
CurrentKeyDimension = 0; |
74 |
|
75 |
// set all MIDI controller values to zero |
76 |
memset(ControllerTable, 0x00, 128); |
77 |
|
78 |
// reset voice stealing parameters |
79 |
itLastStolenVoice = RTList<Voice>::Iterator(); |
80 |
iuiLastStolenKey = RTList<uint>::Iterator(); |
81 |
|
82 |
// reset key info |
83 |
for (uint i = 0; i < 128; i++) { |
84 |
if (pMIDIKeyInfo[i].pActiveVoices) |
85 |
pMIDIKeyInfo[i].pActiveVoices->clear(); |
86 |
if (pMIDIKeyInfo[i].pEvents) |
87 |
pMIDIKeyInfo[i].pEvents->clear(); |
88 |
pMIDIKeyInfo[i].KeyPressed = false; |
89 |
pMIDIKeyInfo[i].Active = false; |
90 |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
91 |
pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator(); |
92 |
} |
93 |
|
94 |
// reset all key groups |
95 |
std::map<uint,uint*>::iterator iter = ActiveKeyGroups.begin(); |
96 |
for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL; |
97 |
|
98 |
// free all active keys |
99 |
pActiveKeys->clear(); |
100 |
|
101 |
// delete all input events |
102 |
pEventQueue->init(); |
103 |
|
104 |
if (pEngine) pEngine->ResetInternal(); |
105 |
} |
106 |
|
107 |
LinuxSampler::Engine* EngineChannel::GetEngine() { |
108 |
return pEngine; |
109 |
} |
110 |
|
111 |
/** |
112 |
* More or less a workaround to set the instrument name, index and load |
113 |
* status variable to zero percent immediately, that is without blocking |
114 |
* the calling thread. It might be used in future for other preparations |
115 |
* as well though. |
116 |
* |
117 |
* @param FileName - file name of the Gigasampler instrument file |
118 |
* @param Instrument - index of the instrument in the .gig file |
119 |
* @see LoadInstrument() |
120 |
*/ |
121 |
void EngineChannel::PrepareLoadInstrument(const char* FileName, uint Instrument) { |
122 |
InstrumentFile = FileName; |
123 |
InstrumentIdx = Instrument; |
124 |
InstrumentStat = 0; |
125 |
} |
126 |
|
127 |
/** |
128 |
* Load an instrument from a .gig file. PrepareLoadInstrument() has to |
129 |
* be called first to provide the information which instrument to load. |
130 |
* This method will then actually start to load the instrument and block |
131 |
* the calling thread until loading was completed. |
132 |
* |
133 |
* @returns detailed description of the method call result |
134 |
* @see PrepareLoadInstrument() |
135 |
*/ |
136 |
void EngineChannel::LoadInstrument() { |
137 |
|
138 |
if (pEngine) pEngine->DisableAndLock(); |
139 |
|
140 |
ResetInternal(); |
141 |
|
142 |
// free old instrument |
143 |
if (pInstrument) { |
144 |
// give old instrument back to instrument manager |
145 |
Engine::instruments.HandBack(pInstrument, this); |
146 |
} |
147 |
|
148 |
// delete all key groups |
149 |
ActiveKeyGroups.clear(); |
150 |
|
151 |
// request gig instrument from instrument manager |
152 |
try { |
153 |
instrument_id_t instrid; |
154 |
instrid.FileName = InstrumentFile; |
155 |
instrid.iInstrument = InstrumentIdx; |
156 |
pInstrument = Engine::instruments.Borrow(instrid, this); |
157 |
if (!pInstrument) { |
158 |
InstrumentStat = -1; |
159 |
dmsg(1,("no instrument loaded!!!\n")); |
160 |
exit(EXIT_FAILURE); |
161 |
} |
162 |
} |
163 |
catch (RIFF::Exception e) { |
164 |
InstrumentStat = -2; |
165 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; |
166 |
throw LinuxSamplerException(msg); |
167 |
} |
168 |
catch (InstrumentResourceManagerException e) { |
169 |
InstrumentStat = -3; |
170 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); |
171 |
throw LinuxSamplerException(msg); |
172 |
} |
173 |
catch (...) { |
174 |
InstrumentStat = -4; |
175 |
throw LinuxSamplerException("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file."); |
176 |
} |
177 |
|
178 |
// rebuild ActiveKeyGroups map with key groups of current instrument |
179 |
for (::gig::Region* pRegion = pInstrument->GetFirstRegion(); pRegion; pRegion = pInstrument->GetNextRegion()) |
180 |
if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; |
181 |
|
182 |
InstrumentIdxName = pInstrument->pInfo->Name; |
183 |
InstrumentStat = 100; |
184 |
|
185 |
// inform audio driver for the need of two channels |
186 |
try { |
187 |
if (pEngine && pEngine->pAudioOutputDevice) |
188 |
pEngine->pAudioOutputDevice->AcquireChannels(2); // gig Engine only stereo |
189 |
} |
190 |
catch (AudioOutputException e) { |
191 |
String msg = "Audio output device unable to provide 2 audio channels, cause: " + e.Message(); |
192 |
throw LinuxSamplerException(msg); |
193 |
} |
194 |
|
195 |
if (pEngine) pEngine->Enable(); |
196 |
} |
197 |
|
198 |
/** |
199 |
* Will be called by the InstrumentResourceManager when the instrument |
200 |
* we are currently using in this engine is going to be updated, so we |
201 |
* can stop playback before that happens. |
202 |
*/ |
203 |
void EngineChannel::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) { |
204 |
dmsg(3,("gig::Engine: Received instrument update message.\n")); |
205 |
if (pEngine) pEngine->DisableAndLock(); |
206 |
ResetInternal(); |
207 |
this->pInstrument = NULL; |
208 |
} |
209 |
|
210 |
/** |
211 |
* Will be called by the InstrumentResourceManager when the instrument |
212 |
* update process was completed, so we can continue with playback. |
213 |
*/ |
214 |
void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) { |
215 |
this->pInstrument = pNewResource; //TODO: there are couple of engine parameters we should update here as well if the instrument was updated (see LoadInstrument()) |
216 |
if (pEngine) pEngine->Enable(); |
217 |
} |
218 |
|
219 |
void EngineChannel::Connect(AudioOutputDevice* pAudioOut) { |
220 |
if (pEngine && pEngine->pAudioOutputDevice != pAudioOut) { |
221 |
DisconnectAudioOutputDevice(); |
222 |
} |
223 |
pEngine = Engine::AcquireEngine(this, pAudioOut); |
224 |
ResetInternal(); |
225 |
for (uint i = 0; i < 128; i++) { |
226 |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool); |
227 |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool); |
228 |
} |
229 |
AudioDeviceChannelLeft = 0; |
230 |
AudioDeviceChannelRight = 1; |
231 |
pOutputLeft = pAudioOut->Channel(0)->Buffer(); |
232 |
pOutputRight = pAudioOut->Channel(1)->Buffer(); |
233 |
} |
234 |
|
235 |
void EngineChannel::DisconnectAudioOutputDevice() { |
236 |
if (pEngine) { // if clause to prevent disconnect loops |
237 |
ResetInternal(); |
238 |
for (uint i = 0; i < 128; i++) { |
239 |
if (pMIDIKeyInfo[i].pActiveVoices) { |
240 |
delete pMIDIKeyInfo[i].pActiveVoices; |
241 |
pMIDIKeyInfo[i].pActiveVoices = NULL; |
242 |
} |
243 |
if (pMIDIKeyInfo[i].pEvents) { |
244 |
delete pMIDIKeyInfo[i].pEvents; |
245 |
pMIDIKeyInfo[i].pEvents = NULL; |
246 |
} |
247 |
} |
248 |
Engine* oldEngine = pEngine; |
249 |
AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice; |
250 |
pEngine = NULL; |
251 |
Engine::FreeEngine(this, oldAudioDevice); |
252 |
AudioDeviceChannelLeft = -1; |
253 |
AudioDeviceChannelRight = -1; |
254 |
} |
255 |
} |
256 |
|
257 |
void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { |
258 |
if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet."); |
259 |
|
260 |
AudioChannel* pChannel = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannel); |
261 |
if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel)); |
262 |
switch (EngineAudioChannel) { |
263 |
case 0: // left output channel |
264 |
pOutputLeft = pChannel->Buffer(); |
265 |
AudioDeviceChannelLeft = AudioDeviceChannel; |
266 |
break; |
267 |
case 1: // right output channel |
268 |
pOutputRight = pChannel->Buffer(); |
269 |
AudioDeviceChannelRight = AudioDeviceChannel; |
270 |
break; |
271 |
default: |
272 |
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
273 |
} |
274 |
} |
275 |
|
276 |
int EngineChannel::OutputChannel(uint EngineAudioChannel) { |
277 |
switch (EngineAudioChannel) { |
278 |
case 0: // left channel |
279 |
return AudioDeviceChannelLeft; |
280 |
case 1: // right channel |
281 |
return AudioDeviceChannelRight; |
282 |
default: |
283 |
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
284 |
} |
285 |
} |
286 |
|
287 |
/** |
288 |
* Will be called by the MIDIIn Thread to let the audio thread trigger a new |
289 |
* voice for the given key. |
290 |
* |
291 |
* @param Key - MIDI key number of the triggered key |
292 |
* @param Velocity - MIDI velocity value of the triggered key |
293 |
*/ |
294 |
void EngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity) { |
295 |
if (pEngine) { |
296 |
Event event = pEngine->pEventGenerator->CreateEvent(); |
297 |
event.Type = Event::type_note_on; |
298 |
event.Param.Note.Key = Key; |
299 |
event.Param.Note.Velocity = Velocity; |
300 |
event.pEngineChannel = this; |
301 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
302 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
303 |
} |
304 |
} |
305 |
|
306 |
/** |
307 |
* Will be called by the MIDIIn Thread to signal the audio thread to release |
308 |
* voice(s) on the given key. |
309 |
* |
310 |
* @param Key - MIDI key number of the released key |
311 |
* @param Velocity - MIDI release velocity value of the released key |
312 |
*/ |
313 |
void EngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity) { |
314 |
if (pEngine) { |
315 |
Event event = pEngine->pEventGenerator->CreateEvent(); |
316 |
event.Type = Event::type_note_off; |
317 |
event.Param.Note.Key = Key; |
318 |
event.Param.Note.Velocity = Velocity; |
319 |
event.pEngineChannel = this; |
320 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
321 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
322 |
} |
323 |
} |
324 |
|
325 |
/** |
326 |
* Will be called by the MIDIIn Thread to signal the audio thread to change |
327 |
* the pitch value for all voices. |
328 |
* |
329 |
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
330 |
*/ |
331 |
void EngineChannel::SendPitchbend(int Pitch) { |
332 |
if (pEngine) { |
333 |
Event event = pEngine->pEventGenerator->CreateEvent(); |
334 |
event.Type = Event::type_pitchbend; |
335 |
event.Param.Pitch.Pitch = Pitch; |
336 |
event.pEngineChannel = this; |
337 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
338 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
339 |
} |
340 |
} |
341 |
|
342 |
/** |
343 |
* Will be called by the MIDIIn Thread to signal the audio thread that a |
344 |
* continuous controller value has changed. |
345 |
* |
346 |
* @param Controller - MIDI controller number of the occured control change |
347 |
* @param Value - value of the control change |
348 |
*/ |
349 |
void EngineChannel::SendControlChange(uint8_t Controller, uint8_t Value) { |
350 |
if (pEngine) { |
351 |
Event event = pEngine->pEventGenerator->CreateEvent(); |
352 |
event.Type = Event::type_control_change; |
353 |
event.Param.CC.Controller = Controller; |
354 |
event.Param.CC.Value = Value; |
355 |
event.pEngineChannel = this; |
356 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
357 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
358 |
} |
359 |
} |
360 |
|
361 |
float EngineChannel::Volume() { |
362 |
return GlobalVolume; |
363 |
} |
364 |
|
365 |
void EngineChannel::Volume(float f) { |
366 |
GlobalVolume = f; |
367 |
} |
368 |
|
369 |
uint EngineChannel::Channels() { |
370 |
return 2; |
371 |
} |
372 |
|
373 |
String EngineChannel::InstrumentFileName() { |
374 |
return InstrumentFile; |
375 |
} |
376 |
|
377 |
String EngineChannel::InstrumentName() { |
378 |
return InstrumentIdxName; |
379 |
} |
380 |
|
381 |
int EngineChannel::InstrumentIndex() { |
382 |
return InstrumentIdx; |
383 |
} |
384 |
|
385 |
int EngineChannel::InstrumentStatus() { |
386 |
return InstrumentStat; |
387 |
} |
388 |
|
389 |
}} // namespace LinuxSampler::gig |