/[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 420 - (show annotations) (download)
Thu Mar 3 03:25:17 2005 UTC (19 years, 1 month ago) by schoenebeck
File size: 15835 byte(s)
* fixed some segfaults (which occured on EngineChannel destruction)
* InstrumentResourceManager: recache small samples if their current
  number of silence sample points are not sufficient enough for the used
  audio output device
* src/linuxsampler.cpp: voice / streams statistics on the console is back
  again (can be turned on with command line switch --statistics)

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) {
238 delete pMIDIKeyInfo[i].pActiveVoices;
239 pMIDIKeyInfo[i].pActiveVoices = NULL;
240 }
241 if (pMIDIKeyInfo[i].pEvents) {
242 delete pMIDIKeyInfo[i].pEvents;
243 pMIDIKeyInfo[i].pEvents = NULL;
244 }
245 }
246 Engine* oldEngine = pEngine;
247 AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice;
248 pEngine = NULL;
249 Engine::FreeEngine(this, oldAudioDevice);
250 AudioDeviceChannelLeft = -1;
251 AudioDeviceChannelRight = -1;
252 }
253 }
254
255 void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {
256 if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");
257
258 AudioChannel* pChannel = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannel);
259 if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));
260 switch (EngineAudioChannel) {
261 case 0: // left output channel
262 pOutputLeft = pChannel->Buffer();
263 AudioDeviceChannelLeft = AudioDeviceChannel;
264 break;
265 case 1: // right output channel
266 pOutputRight = pChannel->Buffer();
267 AudioDeviceChannelRight = AudioDeviceChannel;
268 break;
269 default:
270 throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));
271 }
272 }
273
274 int EngineChannel::OutputChannel(uint EngineAudioChannel) {
275 switch (EngineAudioChannel) {
276 case 0: // left channel
277 return AudioDeviceChannelLeft;
278 case 1: // right channel
279 return AudioDeviceChannelRight;
280 default:
281 throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));
282 }
283 }
284
285 /**
286 * Will be called by the MIDIIn Thread to let the audio thread trigger a new
287 * voice for the given key.
288 *
289 * @param Key - MIDI key number of the triggered key
290 * @param Velocity - MIDI velocity value of the triggered key
291 */
292 void EngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity) {
293 if (pEngine) {
294 Event event = pEngine->pEventGenerator->CreateEvent();
295 event.Type = Event::type_note_on;
296 event.Param.Note.Key = Key;
297 event.Param.Note.Velocity = Velocity;
298 event.pEngineChannel = this;
299 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
300 else dmsg(1,("EngineChannel: Input event queue full!"));
301 }
302 }
303
304 /**
305 * Will be called by the MIDIIn Thread to signal the audio thread to release
306 * voice(s) on the given key.
307 *
308 * @param Key - MIDI key number of the released key
309 * @param Velocity - MIDI release velocity value of the released key
310 */
311 void EngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity) {
312 if (pEngine) {
313 Event event = pEngine->pEventGenerator->CreateEvent();
314 event.Type = Event::type_note_off;
315 event.Param.Note.Key = Key;
316 event.Param.Note.Velocity = Velocity;
317 event.pEngineChannel = this;
318 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
319 else dmsg(1,("EngineChannel: Input event queue full!"));
320 }
321 }
322
323 /**
324 * Will be called by the MIDIIn Thread to signal the audio thread to change
325 * the pitch value for all voices.
326 *
327 * @param Pitch - MIDI pitch value (-8192 ... +8191)
328 */
329 void EngineChannel::SendPitchbend(int Pitch) {
330 if (pEngine) {
331 Event event = pEngine->pEventGenerator->CreateEvent();
332 event.Type = Event::type_pitchbend;
333 event.Param.Pitch.Pitch = Pitch;
334 event.pEngineChannel = this;
335 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
336 else dmsg(1,("EngineChannel: Input event queue full!"));
337 }
338 }
339
340 /**
341 * Will be called by the MIDIIn Thread to signal the audio thread that a
342 * continuous controller value has changed.
343 *
344 * @param Controller - MIDI controller number of the occured control change
345 * @param Value - value of the control change
346 */
347 void EngineChannel::SendControlChange(uint8_t Controller, uint8_t Value) {
348 if (pEngine) {
349 Event event = pEngine->pEventGenerator->CreateEvent();
350 event.Type = Event::type_control_change;
351 event.Param.CC.Controller = Controller;
352 event.Param.CC.Value = Value;
353 event.pEngineChannel = this;
354 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
355 else dmsg(1,("EngineChannel: Input event queue full!"));
356 }
357 }
358
359 float EngineChannel::Volume() {
360 return GlobalVolume;
361 }
362
363 void EngineChannel::Volume(float f) {
364 GlobalVolume = f;
365 }
366
367 uint EngineChannel::Channels() {
368 return 2;
369 }
370
371 String EngineChannel::InstrumentFileName() {
372 return InstrumentFile;
373 }
374
375 String EngineChannel::InstrumentName() {
376 return InstrumentIdxName;
377 }
378
379 int EngineChannel::InstrumentIndex() {
380 return InstrumentIdx;
381 }
382
383 int EngineChannel::InstrumentStatus() {
384 return InstrumentStat;
385 }
386
387 }} // namespace LinuxSampler::gig

  ViewVC Help
Powered by ViewVC