/[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 1005 - (hide annotations) (download)
Fri Dec 29 20:06:14 2006 UTC (17 years, 3 months ago) by schoenebeck
File size: 31188 byte(s)
* global volume can now be altered at runtime
  (added two new LSCP commands for this, LSCP specs updated)

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

  ViewVC Help
Powered by ViewVC