/[svn]/linuxsampler/trunk/src/audiothread.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/audiothread.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 18 - (show annotations) (download)
Sun Dec 7 05:03:43 2003 UTC (20 years, 4 months ago) by schoenebeck
File size: 15858 byte(s)
* src/audioio.cpp: added support for Alsa 1.0.0
* src/audiothread.cpp: fixed several bugs in sustain pedal handling
* src/diskthread.cpp: fixed several bugs which occured under extreme
  conditions (endless loop in audiothread, freezing the whole application,
  outage of available disk streams)
* src/voice.cpp: fixed cubic interpolation (disabled by default; you can
  enable it by setting USE_LINEAR_INTERPOLATION to 0 in src/voice.h)
* src/configure.in: added check for Alsa version

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
20 * MA 02111-1307 USA *
21 ***************************************************************************/
22
23 #include "audiothread.h"
24
25 AudioThread::AudioThread(AudioIO* pAudioIO, DiskThread* pDiskThread, gig::Instrument* pInstrument) : Thread(true, 1, 0) {
26 this->pAudioIO = pAudioIO;
27 this->pDiskThread = pDiskThread;
28 this->pInstrument = pInstrument;
29 pCommandQueue = new RingBuffer<command_t>(1024);
30 pVoices = new Voice*[MAX_AUDIO_VOICES];
31 // allocate the ActiveVoicePool (for each midi key there is a variable size linked list
32 // of pointers to Voice objects)
33 ActiveVoicePool = new RTELMemoryPool<Voice*>(MAX_AUDIO_VOICES);
34 for (uint i = 0; i < MAX_AUDIO_VOICES; i++) {
35 pVoices[i] = new Voice(pDiskThread);
36 }
37 for (uint i = 0; i < 128; i++) {
38 pMIDIKeyInfo[i].pActiveVoices = new RTEList<Voice*>;
39 pMIDIKeyInfo[i].hSustainPtr = NULL;
40 pMIDIKeyInfo[i].Sustained = false;
41 pMIDIKeyInfo[i].KeyPressed = false;
42 pMIDIKeyInfo[i].pSustainPoolNode = NULL;
43 }
44 SustainedKeyPool = new RTELMemoryPool<uint>(128);
45
46 pAudioSumBuffer = new float[pAudioIO->FragmentSize * pAudioIO->Channels];
47
48 // set all voice outputs to the AudioSumBuffer
49 for (int i = 0; i < MAX_AUDIO_VOICES; i++) {
50 pVoices[i]->SetOutput(pAudioSumBuffer, pAudioIO->FragmentSize * 2); //FIXME: assuming stereo
51 }
52
53 // cache initial samples points (for actually needed samples)
54 dmsg(1,("Caching initial samples..."));
55 gig::Region* pRgn = this->pInstrument->GetFirstRegion();
56 while (pRgn) {
57 if (!pRgn->GetSample()->GetCache().Size) {
58 dmsg(2,("C"));
59 CacheInitialSamples(pRgn->GetSample());
60 }
61 for (uint i = 0; i < pRgn->DimensionRegions; i++) {
62 CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample);
63 }
64
65 pRgn = this->pInstrument->GetNextRegion();
66 }
67
68 // sustain pedal value
69 PrevHoldCCValue = 0;
70 SustainPedal = 0;
71
72 dmsg(1,("OK\n"));
73 }
74
75 AudioThread::~AudioThread() {
76 if (pCommandQueue) delete pCommandQueue;
77 if (pVoices) {
78 for (uint i = 0; i < MAX_AUDIO_VOICES; i++) {
79 if (pVoices[i]) delete pVoices[i];
80 }
81 }
82 delete[] pVoices;
83 }
84
85 int AudioThread::Main() {
86 dmsg(2,("Audio thread running\n"));
87
88 while (true) {
89
90 // read and process commands from the queue
91 while (true) {
92 command_t command;
93 if (!pCommandQueue->pop(&command)) break;
94
95 switch (command.type) {
96 case command_type_note_on:
97 dmsg(5,("Audio Thread: Note on received\n"));
98 ProcessNoteOn(command.pitch, command.velocity);
99 break;
100 case command_type_note_off:
101 dmsg(5,("Audio Thread: Note off received\n"));
102 ProcessNoteOff(command.pitch, command.velocity);
103 break;
104 case command_type_continuous_controller:
105 dmsg(5,("Audio Thread: MIDI CC received\n"));
106 ProcessControlChange(command.channel, command.number, command.value);
107 break;
108 }
109 }
110
111
112 // zero out the sum buffer
113 for (uint u = 0; u < pAudioIO->FragmentSize * pAudioIO->Channels; u++) {
114 pAudioSumBuffer[u] = 0.0;
115 }
116
117
118 // render audio from all active voices
119 int active_voices = 0;
120 for (uint i = 0; i < MAX_AUDIO_VOICES; i++) {
121 if (pVoices[i]->IsActive()) {
122 pVoices[i]->RenderAudio();
123 if (pVoices[i]->IsActive()) active_voices++; // still active
124 else { // voice reached end, is now inactive
125 ReleaseVoice(pVoices[i]); // remove voice from the list of active voices
126 }
127 }
128 }
129 // write that to the disk thread class so that it can print it
130 // on the console for debugging purposes
131 ActiveVoiceCount = active_voices;
132 if (ActiveVoiceCount > ActiveVoiceCountMax) ActiveVoiceCountMax = ActiveVoiceCount;
133
134
135 // check clipping in the audio sum, convert to sample_type
136 // (from 32bit to 16bit sample) and copy to output buffer
137 float sample_point;
138 for (uint u = 0; u < pAudioIO->FragmentSize * pAudioIO->Channels; u++) {
139 sample_point = this->pAudioSumBuffer[u] / 4; // FIXME division by 4 just for testing purposes (to give a bit of head room when mixing multiple voices together)
140 if (sample_point < -32768.0) sample_point = -32768.0;
141 if (sample_point > 32767.0) sample_point = 32767.0;
142 this->pAudioIO->pOutputBuffer[u] = (sample_t) sample_point;
143 }
144
145
146 // call audio driver to output sound
147 int res = this->pAudioIO->Output();
148 if (res < 0) exit(EXIT_FAILURE);
149 }
150 }
151
152 /// Will be called by the MIDIIn Thread to let the audio thread trigger a new voice.
153 void AudioThread::SendNoteOn(uint8_t Pitch, uint8_t Velocity) {
154 command_t cmd;
155 cmd.type = command_type_note_on;
156 cmd.pitch = Pitch;
157 cmd.velocity = Velocity;
158 if (this->pCommandQueue->write_space() > 0) this->pCommandQueue->push(&cmd);
159 else dmsg(1,("AudioThread: Command queue full!"));
160 }
161
162 /// Will be called by the MIDIIn Thread to signal the audio thread to release voice(s).
163 void AudioThread::SendNoteOff(uint8_t Pitch, uint8_t Velocity) {
164 command_t cmd;
165 cmd.type = command_type_note_off;
166 cmd.pitch = Pitch;
167 cmd.velocity = Velocity;
168 if (this->pCommandQueue->write_space() > 0) this->pCommandQueue->push(&cmd);
169 else dmsg(1,("AudioThread: Command queue full!"));
170 }
171
172 // Will be called by the MIDIIn Thread to signal the audio thread that a continuous controller value has changed.
173 void AudioThread::SendControlChange(uint8_t Channel, uint8_t Number, uint8_t Value) {
174 command_t cmd;
175 cmd.type = command_type_continuous_controller;
176 cmd.channel = Channel;
177 cmd.number = Number;
178 cmd.value = Value;
179 if (this->pCommandQueue->write_space() > 0) this->pCommandQueue->push(&cmd);
180 else dmsg(1,("AudioThread: Command queue full!"));
181 }
182
183 /**
184 * Assigns and triggers a new voice for the respective MIDI key.
185 */
186 void AudioThread::ProcessNoteOn(uint8_t MIDIKey, uint8_t Velocity) {
187 pMIDIKeyInfo[MIDIKey].KeyPressed = true; // the MIDI key was currently pressed down
188 for (int i = 0; i < MAX_AUDIO_VOICES; i++) {
189 if (pVoices[i]->IsActive()) continue; // search for a free voice
190
191 // launch the new voice
192 if (pVoices[i]->Trigger(MIDIKey, Velocity, this->pInstrument) < 0) {
193 return; // failed to trigger the new voice
194 }
195
196 // add (append) a new voice to the corresponding MIDIKey active voices list
197 Voice** new_voice_ptr = ActiveVoicePool->alloc_append(pMIDIKeyInfo[MIDIKey].pActiveVoices);
198 *new_voice_ptr = pVoices[i];
199 pVoices[i]->pSelfPtr = new_voice_ptr; // FIXME: hack to allow fast deallocation
200
201 // update key info
202 if (!pMIDIKeyInfo[MIDIKey].hSustainPtr) {
203 dmsg(4,("ActivateVoice(uint,uint): hSustainPtr == null, setting release pointer to the last voice on the key...\n"));
204 pMIDIKeyInfo[MIDIKey].pActiveVoices->last();
205 pMIDIKeyInfo[MIDIKey].hSustainPtr = pMIDIKeyInfo[MIDIKey].pActiveVoices->current();
206 }
207 return;
208 }
209 std::cerr << "No free voice!" << std::endl << std::flush;
210 }
211
212 /**
213 * Releases the voices on the given key if sustain pedal is not pressed.
214 * If sustain is pressed, the release of the note will be postponed until
215 * sustain pedal will be released or voice turned inactive by itself (e.g.
216 * due to completion of sample playback).
217 */
218 void AudioThread::ProcessNoteOff(uint8_t MIDIKey, uint8_t Velocity) {
219 pMIDIKeyInfo[MIDIKey].KeyPressed = false; // the MIDI key was currently released
220 midi_key_info_t* pmidikey = &pMIDIKeyInfo[MIDIKey];
221 if (SustainPedal) { // if sustain pedal is pressed postpone the Note-Off
222 if (pmidikey->hSustainPtr) {
223 // stick the note-off information to the respective voice
224 Voice** pVoiceToRelease = pmidikey->pActiveVoices->set_current(pmidikey->hSustainPtr);
225 if (pVoiceToRelease) {
226 (*pVoiceToRelease)->ReleaseVelocity = Velocity;
227 // now increment the sustain pointer
228 pmidikey->pActiveVoices->next();
229 pmidikey->hSustainPtr = pmidikey->pActiveVoices->current();
230 // if the key was not sustained yet, add it's MIDI key number to the sustained key pool
231 if (!pmidikey->Sustained) {
232 uint* sustainedmidikey = SustainedKeyPool->alloc();
233 *sustainedmidikey = MIDIKey;
234 pmidikey->pSustainPoolNode = sustainedmidikey;
235 pmidikey->Sustained = true;
236 }
237 }
238 else dmsg(3,("Ignoring NOTE OFF --> pVoiceToRelease == null!\n"));
239 }
240 else dmsg(3,("Ignoring NOTE OFF, seems like more Note-Offs than Note-Ons or no free voices available?\n"));
241 }
242 else {
243 // release all active voices on the midi key
244 Voice** pVoicePtr = pmidikey->pActiveVoices->first();
245 while (pVoicePtr) {
246 Voice** pVoicePtrNext = pMIDIKeyInfo[MIDIKey].pActiveVoices->next();
247 ReleaseVoice(*pVoicePtr);
248 pVoicePtr = pVoicePtrNext;
249 }
250 }
251 }
252
253 /**
254 * Releases the voice given with pVoice (no matter if sustain is pressed or
255 * not). This method will e.g. be directly called if a voice went inactive
256 * by itself. If sustain pedal is pressed the method takes care to free
257 * those sustain informations of the voice.
258 */
259 void AudioThread::ReleaseVoice(Voice* pVoice) {
260 if (pVoice) {
261 if (pVoice->IsActive()) pVoice->Kill(); //TODO: for now we're rude and just kill the poor, poor voice immediately :), later we add a Release() method to the Voice class and call it here to let the voice go through it's release phase
262
263 if (pMIDIKeyInfo[pVoice->MIDIKey].Sustained) {
264 // check if the sustain pointer has to be moved, now that we release the voice
265 RTEList<Voice*>::NodeHandle hSustainPtr = pMIDIKeyInfo[pVoice->MIDIKey].hSustainPtr;
266 if (hSustainPtr) {
267 Voice** pVoicePtr = pMIDIKeyInfo[pVoice->MIDIKey].pActiveVoices->set_current(hSustainPtr);
268 if (pVoicePtr) {
269 if (*pVoicePtr == pVoice) { // move sustain pointer to the next sustained voice
270 dmsg(3,("Correcting sustain pointer\n"));
271 pMIDIKeyInfo[pVoice->MIDIKey].pActiveVoices->next();
272 pMIDIKeyInfo[pVoice->MIDIKey].hSustainPtr = pMIDIKeyInfo[pVoice->MIDIKey].pActiveVoices->current();
273 }
274 else dmsg(4,("ReleaseVoice(Voice*): *hSustain != pVoice\n"));
275 }
276 else dmsg(3,("ReleaseVoice(Voice*): pVoicePtr == null\n"));
277 }
278 else dmsg(3,("ReleaseVoice(Voice*): hSustainPtr == null\n"));
279 }
280
281 // remove the voice from the list associated with this MIDI key
282 ActiveVoicePool->free(pVoice->pSelfPtr);
283
284 // check if there are no voices left on the MIDI key and update the key info if so
285 if (pMIDIKeyInfo[pVoice->MIDIKey].pActiveVoices->is_empty()) {
286 pMIDIKeyInfo[pVoice->MIDIKey].hSustainPtr = NULL;
287 if (pMIDIKeyInfo[pVoice->MIDIKey].Sustained) {
288 SustainedKeyPool->free(pMIDIKeyInfo[pVoice->MIDIKey].pSustainPoolNode);
289 pMIDIKeyInfo[pVoice->MIDIKey].pSustainPoolNode = NULL;
290 pMIDIKeyInfo[pVoice->MIDIKey].Sustained = false;
291 }
292 dmsg(3,("Key has no more voices now\n"));
293 }
294 }
295 else std::cerr << "Couldn't release voice! (pVoice == NULL)\n" << std::flush;
296 }
297
298 void AudioThread::ProcessControlChange(uint8_t Channel, uint8_t Number, uint8_t Value) {
299 dmsg(4,("AudioThread::ContinuousController c=%d n=%d v=%d\n", Channel, Number, Value));
300 if (Number == 64) {
301 if (Value >= 64 && PrevHoldCCValue < 64) {
302 dmsg(4,("PEDAL DOWN\n"));
303 SustainPedal = true;
304 }
305 if (Value < 64 && PrevHoldCCValue >= 64) {
306 dmsg(4,("PEDAL UP\n"));
307 SustainPedal = false;
308 // iterate through all keys that are currently sustained
309 for (uint* key = SustainedKeyPool->first(); key; key = SustainedKeyPool->next()) {
310 if (!pMIDIKeyInfo[*key].KeyPressed) { // release the voices on the key, if the key is not pressed anymore
311 // release all active voices on the midi key
312 Voice** pVoicePtr = pMIDIKeyInfo[*key].pActiveVoices->first();
313 while (pVoicePtr) {
314 Voice** pVoicePtrNext = pMIDIKeyInfo[*key].pActiveVoices->next();
315 dmsg(3,("Sustain CC: releasing voice on midi key %d\n", *key));
316 ReleaseVoice(*pVoicePtr);
317 pVoicePtr = pVoicePtrNext;
318 }
319 }
320 }
321 }
322 PrevHoldCCValue = Value;
323 }
324 }
325
326 void AudioThread::CacheInitialSamples(gig::Sample* pSample) {
327 if (!pSample || pSample->GetCache().Size) return;
328 if (pSample->SamplesTotal <= NUM_RAM_PRELOAD_SAMPLES) {
329 // Sample is too short for disk streaming, so we load the whole
330 // sample into RAM and place 'pAudioIO->FragmentSize << MAX_PITCH'
331 // number of '0' samples (silence samples) behind the official buffer
332 // border, to allow the interpolator do it's work even at the end of
333 // the sample.
334 gig::buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension((pAudioIO->FragmentSize << MAX_PITCH) + 3);
335 dmsg(4,("Cached %d Bytes, %d silence bytes.\n", buf.Size, buf.NullExtensionSize));
336 }
337 else { // we only cache NUM_RAM_PRELOAD_SAMPLES and stream the other sample points from disk
338 pSample->LoadSampleData(NUM_RAM_PRELOAD_SAMPLES);
339 }
340
341 if (!pSample->GetCache().Size) std::cerr << "Unable to cache sample - maybe memory full!" << std::endl << std::flush;
342 }

  ViewVC Help
Powered by ViewVC