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