/[svn]/linuxsampler/trunk/src/engines/gig/EngineChannel.cpp
ViewVC logotype

Annotation of /linuxsampler/trunk/src/engines/gig/EngineChannel.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1653 - (hide annotations) (download)
Wed Jan 30 01:51:46 2008 UTC (16 years, 2 months ago) by schoenebeck
File size: 34753 byte(s)
* added support for notifying instrument editors on note-on / note-off
  events (e.g. to highlight the pressed keys on the virtual keyboard
  of gigedit)
* fixed return value of recently added Thread::TestCancel() method
* be verbose on DLL load errors (on Linux)

1 schoenebeck 411 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5     * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 persson 1646 * Copyright (C) 2005 - 2008 Christian Schoenebeck *
7 schoenebeck 411 * *
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 schoenebeck 1424 #include "../../common/global_private.h"
27    
28 persson 438 namespace LinuxSampler { namespace gig {
29 schoenebeck 411
30 persson 1646 EngineChannel::EngineChannel() :
31     InstrumentChangeCommandReader(InstrumentChangeCommand) {
32 schoenebeck 411 pMIDIKeyInfo = new midi_key_info_t[128];
33     pEngine = NULL;
34     pInstrument = NULL;
35 schoenebeck 460 pEvents = NULL; // we allocate when we retrieve the right Engine object
36 schoenebeck 970 pEventQueue = new RingBuffer<Event,false>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0);
37 schoenebeck 411 pActiveKeys = new Pool<uint>(128);
38     for (uint i = 0; i < 128; i++) {
39     pMIDIKeyInfo[i].pActiveVoices = NULL; // we allocate when we retrieve the right Engine object
40     pMIDIKeyInfo[i].KeyPressed = false;
41     pMIDIKeyInfo[i].Active = false;
42     pMIDIKeyInfo[i].ReleaseTrigger = false;
43     pMIDIKeyInfo[i].pEvents = NULL; // we allocate when we retrieve the right Engine object
44 schoenebeck 473 pMIDIKeyInfo[i].VoiceTheftsQueued = 0;
45 persson 438 pMIDIKeyInfo[i].RoundRobinIndex = 0;
46 schoenebeck 411 }
47     InstrumentIdx = -1;
48     InstrumentStat = -1;
49 schoenebeck 1001 pChannelLeft = NULL;
50     pChannelRight = NULL;
51 schoenebeck 411 AudioDeviceChannelLeft = -1;
52     AudioDeviceChannelRight = -1;
53 schoenebeck 675 pMidiInputPort = NULL;
54     midiChannel = midi_chan_all;
55 schoenebeck 670 ResetControllers();
56 schoenebeck 829 SoloMode = false;
57     PortamentoMode = false;
58     PortamentoTime = CONFIG_PORTAMENTO_TIME_DEFAULT;
59 schoenebeck 411 }
60    
61     EngineChannel::~EngineChannel() {
62 schoenebeck 460 DisconnectAudioOutputDevice();
63 schoenebeck 411 if (pEventQueue) delete pEventQueue;
64     if (pActiveKeys) delete pActiveKeys;
65     if (pMIDIKeyInfo) delete[] pMIDIKeyInfo;
66 schoenebeck 1001 RemoveAllFxSends();
67 schoenebeck 411 }
68    
69     /**
70 schoenebeck 660 * Implementation of virtual method from abstract EngineChannel interface.
71     * This method will periodically be polled (e.g. by the LSCP server) to
72     * check if some engine channel parameter has changed since the last
73     * StatusChanged() call.
74     *
75 schoenebeck 670 * This method can also be used to mark the engine channel as changed
76     * from outside, e.g. by a MIDI input device. The optional argument
77     * \a nNewStatus can be used for this.
78     *
79 schoenebeck 660 * TODO: This "poll method" is just a lazy solution and might be
80     * replaced in future.
81 schoenebeck 670 * @param bNewStatus - (optional, default: false) sets the new status flag
82 schoenebeck 660 * @returns true if engine channel status has changed since last
83     * StatusChanged() call
84     */
85 schoenebeck 670 bool EngineChannel::StatusChanged(bool bNewStatus) {
86 schoenebeck 660 bool b = bStatusChanged;
87 schoenebeck 670 bStatusChanged = bNewStatus;
88 schoenebeck 660 return b;
89     }
90    
91 schoenebeck 670 void EngineChannel::Reset() {
92     if (pEngine) pEngine->DisableAndLock();
93     ResetInternal();
94     ResetControllers();
95     if (pEngine) {
96     pEngine->Enable();
97     pEngine->Reset();
98     }
99     }
100    
101 schoenebeck 660 /**
102 schoenebeck 411 * This method is not thread safe!
103     */
104     void EngineChannel::ResetInternal() {
105     CurrentKeyDimension = 0;
106    
107     // reset key info
108     for (uint i = 0; i < 128; i++) {
109     if (pMIDIKeyInfo[i].pActiveVoices)
110     pMIDIKeyInfo[i].pActiveVoices->clear();
111     if (pMIDIKeyInfo[i].pEvents)
112     pMIDIKeyInfo[i].pEvents->clear();
113     pMIDIKeyInfo[i].KeyPressed = false;
114     pMIDIKeyInfo[i].Active = false;
115     pMIDIKeyInfo[i].ReleaseTrigger = false;
116     pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator();
117 schoenebeck 473 pMIDIKeyInfo[i].VoiceTheftsQueued = 0;
118 schoenebeck 411 }
119 schoenebeck 829 SoloKey = -1; // no solo key active yet
120     PortamentoPos = -1.0f; // no portamento active yet
121 schoenebeck 411
122     // reset all key groups
123     std::map<uint,uint*>::iterator iter = ActiveKeyGroups.begin();
124     for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL;
125    
126     // free all active keys
127     pActiveKeys->clear();
128    
129     // delete all input events
130     pEventQueue->init();
131    
132     if (pEngine) pEngine->ResetInternal();
133 schoenebeck 660
134     // status of engine channel has changed, so set notify flag
135     bStatusChanged = true;
136 schoenebeck 411 }
137    
138     LinuxSampler::Engine* EngineChannel::GetEngine() {
139     return pEngine;
140     }
141    
142     /**
143     * More or less a workaround to set the instrument name, index and load
144     * status variable to zero percent immediately, that is without blocking
145     * the calling thread. It might be used in future for other preparations
146     * as well though.
147     *
148     * @param FileName - file name of the Gigasampler instrument file
149     * @param Instrument - index of the instrument in the .gig file
150     * @see LoadInstrument()
151     */
152     void EngineChannel::PrepareLoadInstrument(const char* FileName, uint Instrument) {
153     InstrumentFile = FileName;
154     InstrumentIdx = Instrument;
155     InstrumentStat = 0;
156     }
157    
158     /**
159     * Load an instrument from a .gig file. PrepareLoadInstrument() has to
160     * be called first to provide the information which instrument to load.
161     * This method will then actually start to load the instrument and block
162     * the calling thread until loading was completed.
163     *
164     * @see PrepareLoadInstrument()
165     */
166     void EngineChannel::LoadInstrument() {
167 persson 1646 // make sure we don't trigger any new notes with an old
168     // instrument
169     instrument_change_command_t& cmd = ChangeInstrument(0);
170     if (cmd.pInstrument) {
171     // give old instrument back to instrument manager, but
172     // keep the dimension regions and samples that are in use
173     Engine::instruments.HandBackInstrument(cmd.pInstrument, this, cmd.pDimRegionsInUse);
174 schoenebeck 411 }
175 persson 1646 cmd.pDimRegionsInUse->clear();
176 schoenebeck 411
177     // delete all key groups
178     ActiveKeyGroups.clear();
179    
180     // request gig instrument from instrument manager
181 persson 1038 ::gig::Instrument* newInstrument;
182 schoenebeck 411 try {
183 schoenebeck 947 InstrumentManager::instrument_id_t instrid;
184     instrid.FileName = InstrumentFile;
185     instrid.Index = InstrumentIdx;
186 persson 1038 newInstrument = Engine::instruments.Borrow(instrid, this);
187     if (!newInstrument) {
188 schoenebeck 1212 throw InstrumentManagerException("resource was not created");
189 schoenebeck 411 }
190     }
191     catch (RIFF::Exception e) {
192     InstrumentStat = -2;
193 iliev 1309 StatusChanged(true);
194 schoenebeck 411 String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message;
195 schoenebeck 880 throw Exception(msg);
196 schoenebeck 411 }
197 schoenebeck 1212 catch (InstrumentManagerException e) {
198 schoenebeck 411 InstrumentStat = -3;
199 iliev 1309 StatusChanged(true);
200 schoenebeck 411 String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message();
201 schoenebeck 880 throw Exception(msg);
202 schoenebeck 411 }
203     catch (...) {
204     InstrumentStat = -4;
205 iliev 1309 StatusChanged(true);
206 schoenebeck 880 throw Exception("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file.");
207 schoenebeck 411 }
208    
209     // rebuild ActiveKeyGroups map with key groups of current instrument
210 persson 1038 for (::gig::Region* pRegion = newInstrument->GetFirstRegion(); pRegion; pRegion = newInstrument->GetNextRegion())
211 schoenebeck 411 if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL;
212    
213 persson 1038 InstrumentIdxName = newInstrument->pInfo->Name;
214 schoenebeck 411 InstrumentStat = 100;
215    
216 persson 1646 ChangeInstrument(newInstrument);
217    
218 iliev 1298 StatusChanged(true);
219 schoenebeck 411 }
220    
221 persson 1646
222 schoenebeck 411 /**
223 persson 1646 * Changes the instrument for an engine channel.
224     *
225     * @param pInstrument - new instrument
226     * @returns the resulting instrument change command after the
227     * command switch, containing the old instrument and
228     * the dimregions it is using
229     */
230     EngineChannel::instrument_change_command_t& EngineChannel::ChangeInstrument(::gig::Instrument* pInstrument) {
231     instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate();
232     cmd.pInstrument = pInstrument;
233     cmd.bChangeInstrument = true;
234    
235     return InstrumentChangeCommand.SwitchConfig();
236     }
237    
238     /**
239 schoenebeck 411 * Will be called by the InstrumentResourceManager when the instrument
240 schoenebeck 517 * we are currently using on this EngineChannel is going to be updated,
241     * so we can stop playback before that happens.
242 schoenebeck 411 */
243     void EngineChannel::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) {
244     dmsg(3,("gig::Engine: Received instrument update message.\n"));
245     if (pEngine) pEngine->DisableAndLock();
246     ResetInternal();
247     this->pInstrument = NULL;
248     }
249    
250     /**
251     * Will be called by the InstrumentResourceManager when the instrument
252     * update process was completed, so we can continue with playback.
253     */
254     void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) {
255     this->pInstrument = pNewResource; //TODO: there are couple of engine parameters we should update here as well if the instrument was updated (see LoadInstrument())
256     if (pEngine) pEngine->Enable();
257 schoenebeck 660 bStatusChanged = true; // status of engine has changed, so set notify flag
258 schoenebeck 411 }
259    
260 schoenebeck 517 /**
261     * Will be called by the InstrumentResourceManager on progress changes
262     * while loading or realoading an instrument for this EngineChannel.
263     *
264     * @param fProgress - current progress as value between 0.0 and 1.0
265     */
266     void EngineChannel::OnResourceProgress(float fProgress) {
267     this->InstrumentStat = int(fProgress * 100.0f);
268     dmsg(7,("gig::EngineChannel: progress %d%", InstrumentStat));
269 schoenebeck 660 bStatusChanged = true; // status of engine has changed, so set notify flag
270 schoenebeck 517 }
271    
272 schoenebeck 412 void EngineChannel::Connect(AudioOutputDevice* pAudioOut) {
273 schoenebeck 460 if (pEngine) {
274     if (pEngine->pAudioOutputDevice == pAudioOut) return;
275 schoenebeck 412 DisconnectAudioOutputDevice();
276     }
277 schoenebeck 411 pEngine = Engine::AcquireEngine(this, pAudioOut);
278 persson 438 ResetInternal();
279 schoenebeck 738 pEvents = new RTList<Event>(pEngine->pEventPool);
280 persson 1646
281     // reset the instrument change command struct (need to be done
282     // twice, as it is double buffered)
283     {
284     instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate();
285     cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[0]);
286     cmd.pInstrument = 0;
287     cmd.bChangeInstrument = false;
288     }
289     {
290     instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig();
291     cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[1]);
292     cmd.pInstrument = 0;
293     cmd.bChangeInstrument = false;
294     }
295    
296 schoenebeck 411 for (uint i = 0; i < 128; i++) {
297     pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool);
298     pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool);
299     }
300     AudioDeviceChannelLeft = 0;
301     AudioDeviceChannelRight = 1;
302 schoenebeck 1001 if (fxSends.empty()) { // render directly into the AudioDevice's output buffers
303     pChannelLeft = pAudioOut->Channel(AudioDeviceChannelLeft);
304     pChannelRight = pAudioOut->Channel(AudioDeviceChannelRight);
305     } else { // use local buffers for rendering and copy later
306     // ensure the local buffers have the correct size
307     if (pChannelLeft) delete pChannelLeft;
308     if (pChannelRight) delete pChannelRight;
309     pChannelLeft = new AudioChannel(0, pAudioOut->MaxSamplesPerCycle());
310     pChannelRight = new AudioChannel(1, pAudioOut->MaxSamplesPerCycle());
311     }
312 persson 1039 if (pEngine->EngineDisabled.GetUnsafe()) pEngine->Enable();
313 persson 846 MidiInputPort::AddSysexListener(pEngine);
314 schoenebeck 411 }
315    
316     void EngineChannel::DisconnectAudioOutputDevice() {
317     if (pEngine) { // if clause to prevent disconnect loops
318 persson 1646
319     // delete the structures used for instrument change
320     RTList< ::gig::DimensionRegion*>* d = InstrumentChangeCommand.GetConfigForUpdate().pDimRegionsInUse;
321     if (d) delete d;
322     EngineChannel::instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig();
323     d = cmd.pDimRegionsInUse;
324    
325     if (cmd.pInstrument) {
326     // release the currently loaded instrument
327     Engine::instruments.HandBackInstrument(cmd.pInstrument, this, d);
328     }
329     if (d) delete d;
330    
331     // release all active dimension regions to resource
332     // manager
333     RTList<uint>::Iterator iuiKey = pActiveKeys->first();
334     RTList<uint>::Iterator end = pActiveKeys->end();
335     while (iuiKey != end) { // iterate through all active keys
336     midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey];
337     ++iuiKey;
338    
339     RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first();
340     RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();
341     for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
342     Engine::instruments.HandBackDimReg(itVoice->pDimRgn);
343     }
344     }
345    
346 schoenebeck 411 ResetInternal();
347 schoenebeck 460 if (pEvents) {
348     delete pEvents;
349     pEvents = NULL;
350     }
351 schoenebeck 411 for (uint i = 0; i < 128; i++) {
352 schoenebeck 420 if (pMIDIKeyInfo[i].pActiveVoices) {
353     delete pMIDIKeyInfo[i].pActiveVoices;
354     pMIDIKeyInfo[i].pActiveVoices = NULL;
355     }
356     if (pMIDIKeyInfo[i].pEvents) {
357     delete pMIDIKeyInfo[i].pEvents;
358     pMIDIKeyInfo[i].pEvents = NULL;
359     }
360 schoenebeck 411 }
361     Engine* oldEngine = pEngine;
362     AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice;
363     pEngine = NULL;
364     Engine::FreeEngine(this, oldAudioDevice);
365     AudioDeviceChannelLeft = -1;
366 persson 438 AudioDeviceChannelRight = -1;
367 schoenebeck 1001 if (!fxSends.empty()) { // free the local rendering buffers
368     if (pChannelLeft) delete pChannelLeft;
369     if (pChannelRight) delete pChannelRight;
370     }
371     pChannelLeft = NULL;
372     pChannelRight = NULL;
373 schoenebeck 411 }
374     }
375    
376 schoenebeck 1001 AudioOutputDevice* EngineChannel::GetAudioOutputDevice() {
377     return (pEngine) ? pEngine->pAudioOutputDevice : NULL;
378     }
379    
380 schoenebeck 411 void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {
381     if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");
382 persson 438
383 schoenebeck 411 AudioChannel* pChannel = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannel);
384     if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));
385     switch (EngineAudioChannel) {
386     case 0: // left output channel
387 schoenebeck 1001 if (fxSends.empty()) pChannelLeft = pChannel;
388 schoenebeck 411 AudioDeviceChannelLeft = AudioDeviceChannel;
389     break;
390     case 1: // right output channel
391 schoenebeck 1001 if (fxSends.empty()) pChannelRight = pChannel;
392 schoenebeck 411 AudioDeviceChannelRight = AudioDeviceChannel;
393     break;
394     default:
395     throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));
396     }
397 iliev 1267
398     bStatusChanged = true;
399 schoenebeck 411 }
400    
401     int EngineChannel::OutputChannel(uint EngineAudioChannel) {
402     switch (EngineAudioChannel) {
403     case 0: // left channel
404     return AudioDeviceChannelLeft;
405     case 1: // right channel
406     return AudioDeviceChannelRight;
407     default:
408     throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));
409     }
410     }
411    
412 schoenebeck 675 void EngineChannel::Connect(MidiInputPort* pMidiPort, midi_chan_t MidiChannel) {
413     if (!pMidiPort || pMidiPort == this->pMidiInputPort) return;
414     DisconnectMidiInputPort();
415     this->pMidiInputPort = pMidiPort;
416     this->midiChannel = MidiChannel;
417     pMidiPort->Connect(this, MidiChannel);
418     }
419    
420     void EngineChannel::DisconnectMidiInputPort() {
421     MidiInputPort* pOldPort = this->pMidiInputPort;
422     this->pMidiInputPort = NULL;
423     if (pOldPort) pOldPort->Disconnect(this);
424     }
425    
426     MidiInputPort* EngineChannel::GetMidiInputPort() {
427     return pMidiInputPort;
428     }
429    
430     midi_chan_t EngineChannel::MidiChannel() {
431     return midiChannel;
432     }
433    
434 schoenebeck 1001 FxSend* EngineChannel::AddFxSend(uint8_t MidiCtrl, String Name) throw (Exception) {
435     if (pEngine) pEngine->DisableAndLock();
436     FxSend* pFxSend = new FxSend(this, MidiCtrl, Name);
437     if (fxSends.empty()) {
438     if (pEngine && pEngine->pAudioOutputDevice) {
439     AudioOutputDevice* pDevice = pEngine->pAudioOutputDevice;
440     // create local render buffers
441     pChannelLeft = new AudioChannel(0, pDevice->MaxSamplesPerCycle());
442     pChannelRight = new AudioChannel(1, pDevice->MaxSamplesPerCycle());
443     } else {
444     // postpone local render buffer creation until audio device is assigned
445     pChannelLeft = NULL;
446     pChannelRight = NULL;
447     }
448     }
449     fxSends.push_back(pFxSend);
450     if (pEngine) pEngine->Enable();
451 iliev 1130 fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount());
452 persson 1646
453 schoenebeck 1001 return pFxSend;
454     }
455    
456     FxSend* EngineChannel::GetFxSend(uint FxSendIndex) {
457     return (FxSendIndex < fxSends.size()) ? fxSends[FxSendIndex] : NULL;
458     }
459    
460     uint EngineChannel::GetFxSendCount() {
461     return fxSends.size();
462     }
463    
464     void EngineChannel::RemoveFxSend(FxSend* pFxSend) {
465     if (pEngine) pEngine->DisableAndLock();
466     for (
467     std::vector<FxSend*>::iterator iter = fxSends.begin();
468     iter != fxSends.end(); iter++
469     ) {
470     if (*iter == pFxSend) {
471     delete pFxSend;
472     fxSends.erase(iter);
473     if (fxSends.empty()) {
474     // destroy local render buffers
475     if (pChannelLeft) delete pChannelLeft;
476     if (pChannelRight) delete pChannelRight;
477     // fallback to render directly into AudioOutputDevice's buffers
478     if (pEngine && pEngine->pAudioOutputDevice) {
479     pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft);
480     pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);
481     } else { // we update the pointers later
482     pChannelLeft = NULL;
483     pChannelRight = NULL;
484     }
485     }
486     break;
487     }
488     }
489     if (pEngine) pEngine->Enable();
490 iliev 1130 fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount());
491 schoenebeck 1001 }
492    
493 schoenebeck 411 /**
494     * Will be called by the MIDIIn Thread to let the audio thread trigger a new
495 schoenebeck 906 * voice for the given key. This method is meant for real time rendering,
496     * that is an event will immediately be created with the current system
497     * time as time stamp.
498 schoenebeck 411 *
499     * @param Key - MIDI key number of the triggered key
500     * @param Velocity - MIDI velocity value of the triggered key
501     */
502     void EngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity) {
503     if (pEngine) {
504     Event event = pEngine->pEventGenerator->CreateEvent();
505     event.Type = Event::type_note_on;
506     event.Param.Note.Key = Key;
507     event.Param.Note.Velocity = Velocity;
508 persson 438 event.pEngineChannel = this;
509 schoenebeck 411 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
510 schoenebeck 412 else dmsg(1,("EngineChannel: Input event queue full!"));
511 schoenebeck 1653 // inform instrument editor(s), if any ...
512     pEngine->instruments.TrySendNoteOnToEditors(Key, Velocity, pInstrument);
513 schoenebeck 411 }
514     }
515    
516     /**
517 schoenebeck 906 * Will be called by the MIDIIn Thread to let the audio thread trigger a new
518     * voice for the given key. This method is meant for offline rendering
519     * and / or for cases where the exact position of the event in the current
520     * audio fragment is already known.
521     *
522     * @param Key - MIDI key number of the triggered key
523     * @param Velocity - MIDI velocity value of the triggered key
524     * @param FragmentPos - sample point position in the current audio
525     * fragment to which this event belongs to
526     */
527     void EngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) {
528     if (FragmentPos < 0) {
529     dmsg(1,("EngineChannel::SendNoteOn(): negative FragmentPos! Seems MIDI driver is buggy!"));
530     }
531     else if (pEngine) {
532     Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
533     event.Type = Event::type_note_on;
534     event.Param.Note.Key = Key;
535     event.Param.Note.Velocity = Velocity;
536     event.pEngineChannel = this;
537     if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
538     else dmsg(1,("EngineChannel: Input event queue full!"));
539 schoenebeck 1653 // inform instrument editor(s), if any ...
540     pEngine->instruments.TrySendNoteOnToEditors(Key, Velocity, pInstrument);
541 schoenebeck 906 }
542     }
543    
544     /**
545 schoenebeck 411 * Will be called by the MIDIIn Thread to signal the audio thread to release
546 schoenebeck 906 * voice(s) on the given key. This method is meant for real time rendering,
547     * that is an event will immediately be created with the current system
548     * time as time stamp.
549 schoenebeck 411 *
550     * @param Key - MIDI key number of the released key
551     * @param Velocity - MIDI release velocity value of the released key
552     */
553     void EngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity) {
554     if (pEngine) {
555     Event event = pEngine->pEventGenerator->CreateEvent();
556     event.Type = Event::type_note_off;
557     event.Param.Note.Key = Key;
558     event.Param.Note.Velocity = Velocity;
559 schoenebeck 412 event.pEngineChannel = this;
560 schoenebeck 411 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
561 schoenebeck 412 else dmsg(1,("EngineChannel: Input event queue full!"));
562 schoenebeck 1653 // inform instrument editor(s), if any ...
563     pEngine->instruments.TrySendNoteOffToEditors(Key, Velocity, pInstrument);
564 schoenebeck 411 }
565     }
566    
567     /**
568 schoenebeck 906 * Will be called by the MIDIIn Thread to signal the audio thread to release
569     * voice(s) on the given key. This method is meant for offline rendering
570     * and / or for cases where the exact position of the event in the current
571     * audio fragment is already known.
572     *
573     * @param Key - MIDI key number of the released key
574     * @param Velocity - MIDI release velocity value of the released key
575     * @param FragmentPos - sample point position in the current audio
576     * fragment to which this event belongs to
577     */
578     void EngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) {
579     if (FragmentPos < 0) {
580     dmsg(1,("EngineChannel::SendNoteOff(): negative FragmentPos! Seems MIDI driver is buggy!"));
581     }
582     else if (pEngine) {
583     Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
584     event.Type = Event::type_note_off;
585     event.Param.Note.Key = Key;
586     event.Param.Note.Velocity = Velocity;
587     event.pEngineChannel = this;
588     if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
589     else dmsg(1,("EngineChannel: Input event queue full!"));
590 schoenebeck 1653 // inform instrument editor(s), if any ...
591     pEngine->instruments.TrySendNoteOffToEditors(Key, Velocity, pInstrument);
592 schoenebeck 906 }
593     }
594    
595     /**
596 schoenebeck 411 * Will be called by the MIDIIn Thread to signal the audio thread to change
597 schoenebeck 906 * the pitch value for all voices. This method is meant for real time
598     * rendering, that is an event will immediately be created with the
599     * current system time as time stamp.
600 schoenebeck 411 *
601     * @param Pitch - MIDI pitch value (-8192 ... +8191)
602     */
603     void EngineChannel::SendPitchbend(int Pitch) {
604 persson 438 if (pEngine) {
605 schoenebeck 411 Event event = pEngine->pEventGenerator->CreateEvent();
606     event.Type = Event::type_pitchbend;
607     event.Param.Pitch.Pitch = Pitch;
608 schoenebeck 412 event.pEngineChannel = this;
609 schoenebeck 411 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
610 schoenebeck 412 else dmsg(1,("EngineChannel: Input event queue full!"));
611 schoenebeck 411 }
612     }
613    
614     /**
615 schoenebeck 906 * Will be called by the MIDIIn Thread to signal the audio thread to change
616     * the pitch value for all voices. This method is meant for offline
617     * rendering and / or for cases where the exact position of the event in
618     * the current audio fragment is already known.
619     *
620     * @param Pitch - MIDI pitch value (-8192 ... +8191)
621     * @param FragmentPos - sample point position in the current audio
622     * fragment to which this event belongs to
623     */
624     void EngineChannel::SendPitchbend(int Pitch, int32_t FragmentPos) {
625     if (FragmentPos < 0) {
626     dmsg(1,("EngineChannel::SendPitchBend(): negative FragmentPos! Seems MIDI driver is buggy!"));
627     }
628     else if (pEngine) {
629     Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
630     event.Type = Event::type_pitchbend;
631     event.Param.Pitch.Pitch = Pitch;
632     event.pEngineChannel = this;
633     if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
634     else dmsg(1,("EngineChannel: Input event queue full!"));
635     }
636     }
637    
638     /**
639 schoenebeck 411 * Will be called by the MIDIIn Thread to signal the audio thread that a
640 schoenebeck 906 * continuous controller value has changed. This method is meant for real
641     * time rendering, that is an event will immediately be created with the
642     * current system time as time stamp.
643 schoenebeck 411 *
644     * @param Controller - MIDI controller number of the occured control change
645     * @param Value - value of the control change
646     */
647     void EngineChannel::SendControlChange(uint8_t Controller, uint8_t Value) {
648     if (pEngine) {
649     Event event = pEngine->pEventGenerator->CreateEvent();
650     event.Type = Event::type_control_change;
651     event.Param.CC.Controller = Controller;
652     event.Param.CC.Value = Value;
653 schoenebeck 412 event.pEngineChannel = this;
654 schoenebeck 411 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
655 schoenebeck 412 else dmsg(1,("EngineChannel: Input event queue full!"));
656 schoenebeck 411 }
657     }
658    
659 schoenebeck 906 /**
660     * Will be called by the MIDIIn Thread to signal the audio thread that a
661     * continuous controller value has changed. This method is meant for
662     * offline rendering and / or for cases where the exact position of the
663     * event in the current audio fragment is already known.
664     *
665     * @param Controller - MIDI controller number of the occured control change
666     * @param Value - value of the control change
667     * @param FragmentPos - sample point position in the current audio
668     * fragment to which this event belongs to
669     */
670     void EngineChannel::SendControlChange(uint8_t Controller, uint8_t Value, int32_t FragmentPos) {
671     if (FragmentPos < 0) {
672     dmsg(1,("EngineChannel::SendControlChange(): negative FragmentPos! Seems MIDI driver is buggy!"));
673     }
674     else if (pEngine) {
675     Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
676     event.Type = Event::type_control_change;
677     event.Param.CC.Controller = Controller;
678     event.Param.CC.Value = Value;
679     event.pEngineChannel = this;
680     if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
681     else dmsg(1,("EngineChannel: Input event queue full!"));
682     }
683     }
684    
685 schoenebeck 460 void EngineChannel::ClearEventLists() {
686     pEvents->clear();
687     // empty MIDI key specific event lists
688     {
689     RTList<uint>::Iterator iuiKey = pActiveKeys->first();
690     RTList<uint>::Iterator end = pActiveKeys->end();
691     for(; iuiKey != end; ++iuiKey) {
692     pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key
693     }
694     }
695     }
696    
697 schoenebeck 473 void EngineChannel::ResetControllers() {
698 schoenebeck 670 Pitch = 0;
699     SustainPedal = false;
700 iliev 776 SostenutoPedal = false;
701 schoenebeck 1005 GlobalVolume = 1.0f;
702 schoenebeck 947 MidiVolume = 1.0;
703 schoenebeck 670 GlobalPanLeft = 1.0f;
704     GlobalPanRight = 1.0f;
705 schoenebeck 1041 GlobalTranspose = 0;
706 schoenebeck 473 // set all MIDI controller values to zero
707 persson 903 memset(ControllerTable, 0x00, 129);
708 schoenebeck 1040 // reset all FX Send levels
709     for (
710     std::vector<FxSend*>::iterator iter = fxSends.begin();
711     iter != fxSends.end(); iter++
712     ) {
713     (*iter)->Reset();
714     }
715 schoenebeck 473 }
716    
717 schoenebeck 460 /**
718     * Copy all events from the engine channel's input event queue buffer to
719     * the internal event list. This will be done at the beginning of each
720     * audio cycle (that is each RenderAudio() call) to distinguish all
721     * events which have to be processed in the current audio cycle. Each
722     * EngineChannel has it's own input event queue for the common channel
723     * specific events (like NoteOn, NoteOff and ControlChange events).
724     * Beside that, the engine also has a input event queue for global
725     * events (usually SysEx messages).
726     *
727     * @param Samples - number of sample points to be processed in the
728     * current audio cycle
729     */
730     void EngineChannel::ImportEvents(uint Samples) {
731 schoenebeck 970 RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();
732 schoenebeck 460 Event* pEvent;
733     while (true) {
734     // get next event from input event queue
735     if (!(pEvent = eventQueueReader.pop())) break;
736     // if younger event reached, ignore that and all subsequent ones for now
737     if (pEvent->FragmentPos() >= Samples) {
738     eventQueueReader--;
739     dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples));
740     pEvent->ResetFragmentPos();
741     break;
742     }
743     // copy event to internal event list
744     if (pEvents->poolIsEmpty()) {
745     dmsg(1,("Event pool emtpy!\n"));
746     break;
747     }
748     *pEvents->allocAppend() = *pEvent;
749     }
750     eventQueueReader.free(); // free all copied events from input queue
751     }
752    
753 schoenebeck 1001 void EngineChannel::RemoveAllFxSends() {
754     if (pEngine) pEngine->DisableAndLock();
755     if (!fxSends.empty()) { // free local render buffers
756     if (pChannelLeft) {
757     delete pChannelLeft;
758     if (pEngine && pEngine->pAudioOutputDevice) {
759     // fallback to render directly to the AudioOutputDevice's buffer
760     pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft);
761     } else pChannelLeft = NULL;
762     }
763     if (pChannelRight) {
764     delete pChannelRight;
765     if (pEngine && pEngine->pAudioOutputDevice) {
766     // fallback to render directly to the AudioOutputDevice's buffer
767     pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);
768 persson 1646 } else pChannelRight = NULL;
769 schoenebeck 1001 }
770     }
771     for (int i = 0; i < fxSends.size(); i++) delete fxSends[i];
772     fxSends.clear();
773     if (pEngine) pEngine->Enable();
774     }
775    
776 schoenebeck 411 float EngineChannel::Volume() {
777     return GlobalVolume;
778     }
779    
780     void EngineChannel::Volume(float f) {
781     GlobalVolume = f;
782 schoenebeck 660 bStatusChanged = true; // status of engine channel has changed, so set notify flag
783 schoenebeck 411 }
784    
785     uint EngineChannel::Channels() {
786     return 2;
787     }
788    
789     String EngineChannel::InstrumentFileName() {
790     return InstrumentFile;
791     }
792    
793     String EngineChannel::InstrumentName() {
794     return InstrumentIdxName;
795     }
796    
797     int EngineChannel::InstrumentIndex() {
798     return InstrumentIdx;
799     }
800    
801     int EngineChannel::InstrumentStatus() {
802     return InstrumentStat;
803 persson 438 }
804 schoenebeck 411
805 schoenebeck 475 String EngineChannel::EngineName() {
806     return LS_GIG_ENGINE_NAME;
807     }
808 schoenebeck 660
809 schoenebeck 411 }} // namespace LinuxSampler::gig

  ViewVC Help
Powered by ViewVC