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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 412 - (show annotations) (download)
Sat Feb 26 22:44:51 2005 UTC (19 years, 1 month ago) by schoenebeck
File size: 15651 byte(s)
* gig::Engine: fixed silence (engine channels' events were not imported
  into the engine, fixed undesired creation of new gig::Engine instances
  (and disk threads)
* AudioOutputDevice: reverted behavior to render per Engine instance (and
  not per EngineChannel instance)

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

  ViewVC Help
Powered by ViewVC