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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 12 - (show annotations) (download)
Sun Nov 16 19:01:50 2003 UTC (20 years, 5 months ago) by schoenebeck
File size: 10848 byte(s)
* src/gig.cpp: fixed bug in decompression algorithm which caused it not to
  detect the end of a stream and let the disk streams reload forever also
  resulting in strange sounds at the end of disk voices (concerned only
  playback of compressed gig files)
* src/audiothread.cpp: deallocation of voices when they reached the end of
  playback (thus e.g. when sustain pedal is pressed and a disk stream
  reached it's end)
* various endian corrections needed for non intel systems
* introduced debug level, you can set the debug level and thus the
  verbosity of LinuxSampler in src/global.h

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 pActiveVoices[i] = new RTEList<Voice*>;
39 }
40 SustainedKeyPool = new RTELMemoryPool<sustained_key_t>(200);
41
42 pAudioSumBuffer = new float[pAudioIO->FragmentSize * pAudioIO->Channels];
43
44 // set all voice outputs to the AudioSumBuffer
45 for (int i = 0; i < MAX_AUDIO_VOICES; i++) {
46 pVoices[i]->SetOutput(pAudioSumBuffer, pAudioIO->FragmentSize * 2); //FIXME: assuming stereo
47 }
48
49 // cache initial samples points (for actually needed samples)
50 dmsg(1,("Caching initial samples..."));
51 gig::Region* pRgn = this->pInstrument->GetFirstRegion();
52 while (pRgn) {
53 if (!pRgn->GetSample()->GetCache().Size) {
54 dmsg(2,("C"));
55 CacheInitialSamples(pRgn->GetSample());
56 }
57 for (uint i = 0; i < pRgn->DimensionRegions; i++) {
58 CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample);
59 }
60
61 pRgn = this->pInstrument->GetNextRegion();
62 }
63
64 // sustain pedal value
65 PrevHoldCCValue = 0;
66 SustainPedal = 0;
67
68 dmsg(1,("OK\n"));
69 }
70
71 AudioThread::~AudioThread() {
72 if (pCommandQueue) delete pCommandQueue;
73 if (pVoices) {
74 for (uint i = 0; i < MAX_AUDIO_VOICES; i++) {
75 if (pVoices[i]) delete pVoices[i];
76 }
77 }
78 delete[] pVoices;
79 }
80
81 int AudioThread::Main() {
82 dmsg(2,("Audio thread running\n"));
83
84 while (true) {
85
86 // read and process commands from the queue
87 while (true) {
88 command_t command;
89 if (pCommandQueue->read(&command, 1) == 0) break;
90
91 switch (command.type) {
92 case command_type_note_on:
93 dmsg(5,("Audio Thread: Note on received\n"));
94 ActivateVoice(command.pitch, command.velocity);
95 break;
96 case command_type_note_off:
97 dmsg(5,("Audio Thread: Note off received\n"));
98 ReleaseVoice(command.pitch, command.velocity);
99 break;
100 case command_type_continuous_controller:
101 dmsg(5,("Audio Thread: MIDI CC received\n"));
102 ContinuousController(command.channel, command.number, command.value);
103 break;
104 }
105 }
106
107
108 // zero out the sum buffer
109 for (uint u = 0; u < pAudioIO->FragmentSize * pAudioIO->Channels; u++) {
110 pAudioSumBuffer[u] = 0.0;
111 }
112
113
114 // render audio from all active voices
115 int active_voices = 0;
116 for (uint i = 0; i < MAX_AUDIO_VOICES; i++) {
117 if (pVoices[i]->IsActive()) {
118 pVoices[i]->RenderAudio();
119 if (pVoices[i]->IsActive()) active_voices++; // still active
120 else { // voice reached end, is now inactive
121 ReleaseVoice(pVoices[i]); // remove voice from the list of active voices
122 }
123 }
124 }
125 // write that to the disk thread class so that it can print it
126 // on the console for debugging purposes
127 ActiveVoiceCount = active_voices;
128
129
130 // check clipping in the audio sum, convert to sample_type
131 // (from 32bit to 16bit sample) and copy to output buffer
132 float sample_point;
133 for (uint u = 0; u < pAudioIO->FragmentSize * pAudioIO->Channels; u++) {
134 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)
135 if (sample_point < -32768.0) sample_point = -32768.0;
136 if (sample_point > 32767.0) sample_point = 32767.0;
137 this->pAudioIO->pOutputBuffer[u] = (sample_t) sample_point;
138 }
139
140
141 // call audio driver to output sound
142 int res = this->pAudioIO->Output();
143 if (res < 0) exit(EXIT_FAILURE);
144 }
145 }
146
147 /// Will be called by the MIDIIn Thread to let the audio thread trigger a new voice.
148 void AudioThread::ProcessNoteOn(uint8_t Pitch, uint8_t Velocity) {
149 command_t cmd;
150 cmd.type = command_type_note_on;
151 cmd.pitch = Pitch;
152 cmd.velocity = Velocity;
153 this->pCommandQueue->write(&cmd, 1);
154 }
155
156 /// Will be called by the MIDIIn Thread to signal the audio thread to release a voice.
157 void AudioThread::ProcessNoteOff(uint8_t Pitch, uint8_t Velocity) {
158 command_t cmd;
159 cmd.type = command_type_note_off;
160 cmd.pitch = Pitch;
161 cmd.velocity = Velocity;
162 this->pCommandQueue->write(&cmd, 1);
163 }
164
165 // Will be called by the MIDIIn Thead to send MIDI continuos controller events
166 void AudioThread::ProcessContinuousController(uint8_t Channel, uint8_t Number, uint8_t Value) {
167 command_t cmd;
168 cmd.type = command_type_continuous_controller;
169 cmd.channel = Channel;
170 cmd.number = Number;
171 cmd.value = Value;
172 this->pCommandQueue->write(&cmd, 1);
173 }
174
175 void AudioThread::ActivateVoice(uint8_t MIDIKey, uint8_t Velocity) {
176 for (int i = 0; i < MAX_AUDIO_VOICES; i++) {
177 if (pVoices[i]->IsActive()) continue;
178 pVoices[i]->Trigger(MIDIKey, Velocity, this->pInstrument);
179 // add (append) a new voice to the corresponding MIDIKey active voices list
180 Voice** new_voice_ptr = ActiveVoicePool->alloc_append(pActiveVoices[MIDIKey]);
181 *new_voice_ptr = pVoices[i];
182 pVoices[i]->pSelfPtr = new_voice_ptr; // FIXME: hack to allow fast deallocation
183 return;
184 }
185 std::cerr << "No free voice!" << std::endl << std::flush;
186 }
187
188 void AudioThread::ReleaseVoice(uint8_t MIDIKey, uint8_t Velocity) {
189 // if sustain pedal is pressed postpone the Note-Off
190 if (SustainPedal) {
191 // alloc an element in the SustainedKeyPool and add the current midikey to it
192 sustained_key_t* key = SustainedKeyPool->alloc();
193 if (key == NULL) printf("ERROR: SustainedKeyPool FULL ! exiting\n"); // FIXME
194 key->midikey = MIDIKey;
195 key->velocity = Velocity;
196 }
197 else {
198 // get the first voice in the list of active voices on the MIDI Key
199 Voice** pVoicePtr = pActiveVoices[MIDIKey]->first();
200 if (pVoicePtr) ReleaseVoice(*pVoicePtr);
201 else std::cerr << "Couldn't find active voice for note off command!" << std::endl << std::flush;
202 }
203 }
204
205 void AudioThread::ReleaseVoice(Voice* pVoice) {
206 if (pVoice) {
207 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
208
209 // remove the voice from the list associated to this MIDI key
210 ActiveVoicePool->free(pVoice->pSelfPtr);
211 }
212 else std::cerr << "Couldn't find active voice to release!" << std::endl << std::flush;
213 }
214
215 void AudioThread::ContinuousController(uint8_t Channel, uint8_t Number, uint8_t Value) {
216 dmsg(4,("AudioThread::ContinuousController c=%d n=%d v=%d\n", Channel, Number, Value));
217 if (Number == 64) {
218 if (Value >= 64 && PrevHoldCCValue < 64) {
219 dmsg(4,("PEDAL DOWN\n"));
220 SustainPedal = true;
221 }
222 if (Value < 64 && PrevHoldCCValue >= 64) {
223 dmsg(4,("PEDAL UP\n"));
224 SustainPedal = false;
225 for (sustained_key_t* key = SustainedKeyPool->first(); key; key = SustainedKeyPool->next()) {
226 ReleaseVoice(key->midikey, key->velocity);
227 }
228 // empty the SustainedKeyPool (free all the elements)
229 SustainedKeyPool->empty();
230 }
231 PrevHoldCCValue = Value;
232 }
233 }
234
235 void AudioThread::CacheInitialSamples(gig::Sample* pSample) {
236 if (!pSample || pSample->GetCache().Size) return;
237 if (pSample->SamplesTotal <= NUM_RAM_PRELOAD_SAMPLES) {
238 // Sample is too short for disk streaming, so we load the whole
239 // sample into RAM and place 'pAudioIO->FragmentSize << MAX_PITCH'
240 // number of '0' samples (silence samples) behind the official buffer
241 // border, to allow the interpolator do it's work even at the end of
242 // the sample.
243 gig::buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(pAudioIO->FragmentSize << MAX_PITCH);
244 dmsg(4,("Cached %d Bytes, %d silence bytes.\n", buf.Size, buf.NullExtensionSize));
245 }
246 else { // we only cache NUM_RAM_PRELOAD_SAMPLES and stream the other sample points from disk
247 pSample->LoadSampleData(NUM_RAM_PRELOAD_SAMPLES);
248 }
249
250 if (!pSample->GetCache().Size) std::cerr << "Unable to cache sample - maybe memory full!" << std::endl << std::flush;
251 }

  ViewVC Help
Powered by ViewVC