/[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 438 - (show annotations) (download)
Wed Mar 9 22:12:15 2005 UTC (19 years, 1 month ago) by persson
File size: 15876 byte(s)
* 24-bit decompression now supports the 20 and 18 bit formats
* support for "random" and "round robin" dimensions
* removed a warning printout for empty samples during instrument
  loading

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

  ViewVC Help
Powered by ViewVC