29 |
pCommandQueue = new RingBuffer<command_t>(1024); |
pCommandQueue = new RingBuffer<command_t>(1024); |
30 |
pVoices = new Voice*[MAX_AUDIO_VOICES]; |
pVoices = new Voice*[MAX_AUDIO_VOICES]; |
31 |
// allocate the ActiveVoicePool (for each midi key there is a variable size linked list |
// allocate the ActiveVoicePool (for each midi key there is a variable size linked list |
32 |
// of pointers to Voice classes) |
// of pointers to Voice objects) |
33 |
ActiveVoicePool=new RTELMemoryPool<Voice *>(MAX_AUDIO_VOICES); |
ActiveVoicePool = new RTELMemoryPool<Voice*>(MAX_AUDIO_VOICES); |
34 |
for (uint i = 0; i < MAX_AUDIO_VOICES; i++) { |
for (uint i = 0; i < MAX_AUDIO_VOICES; i++) { |
35 |
pVoices[i] = new Voice(pDiskThread); |
pVoices[i] = new Voice(pDiskThread); |
36 |
} |
} |
37 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
38 |
pActiveVoices[i] = new RTEList<Voice *>; |
pMIDIKeyInfo[i].pActiveVoices = new RTEList<Voice*>; |
39 |
|
pMIDIKeyInfo[i].hSustainPtr = NULL; |
40 |
|
pMIDIKeyInfo[i].Sustained = false; |
41 |
} |
} |
42 |
|
SustainedKeyPool = new RTELMemoryPool<uint>(128); |
|
SustainedKeyPool=new RTELMemoryPool<sustained_key_t>(200); |
|
|
|
|
|
|
|
43 |
|
|
44 |
pAudioSumBuffer = new float[pAudioIO->FragmentSize * pAudioIO->Channels]; |
pAudioSumBuffer = new float[pAudioIO->FragmentSize * pAudioIO->Channels]; |
45 |
|
|
49 |
} |
} |
50 |
|
|
51 |
// cache initial samples points (for actually needed samples) |
// cache initial samples points (for actually needed samples) |
52 |
dmsg(("Caching initial samples...")); |
dmsg(1,("Caching initial samples...")); |
53 |
gig::Region* pRgn = this->pInstrument->GetFirstRegion(); |
gig::Region* pRgn = this->pInstrument->GetFirstRegion(); |
54 |
while (pRgn) { |
while (pRgn) { |
55 |
if (!pRgn->GetSample()->GetCache().Size) { |
if (!pRgn->GetSample()->GetCache().Size) { |
56 |
//printf("C"); |
dmsg(2,("C")); |
57 |
CacheInitialSamples(pRgn->GetSample()); |
CacheInitialSamples(pRgn->GetSample()); |
58 |
} |
} |
59 |
for (uint i = 0; i < pRgn->DimensionRegions; i++) { |
for (uint i = 0; i < pRgn->DimensionRegions; i++) { |
64 |
} |
} |
65 |
|
|
66 |
// sustain pedal value |
// sustain pedal value |
67 |
PrevHoldCCValue=0; |
PrevHoldCCValue = 0; |
68 |
SustainPedal=0; |
SustainPedal = 0; |
69 |
|
|
70 |
dmsg(("OK\n")); |
dmsg(1,("OK\n")); |
71 |
} |
} |
72 |
|
|
73 |
AudioThread::~AudioThread() { |
AudioThread::~AudioThread() { |
81 |
} |
} |
82 |
|
|
83 |
int AudioThread::Main() { |
int AudioThread::Main() { |
84 |
dmsg(("Audio thread running\n")); |
dmsg(2,("Audio thread running\n")); |
|
//int fifofd=open("/tmp/fifo1",O_WRONLY); |
|
85 |
|
|
86 |
while (true) { |
while (true) { |
87 |
|
|
92 |
|
|
93 |
switch (command.type) { |
switch (command.type) { |
94 |
case command_type_note_on: |
case command_type_note_on: |
95 |
dmsg(("Audio Thread: Note on received\n")); |
dmsg(5,("Audio Thread: Note on received\n")); |
96 |
ActivateVoice(command.pitch, command.velocity); |
ActivateVoice(command.pitch, command.velocity); |
97 |
break; |
break; |
98 |
case command_type_note_off: |
case command_type_note_off: |
99 |
dmsg(("Audio Thread: Note off received\n")); |
dmsg(5,("Audio Thread: Note off received\n")); |
100 |
ReleaseVoice(command.pitch, command.velocity); |
ReleaseVoice(command.pitch, command.velocity); |
101 |
break; |
break; |
102 |
case command_type_continuous_controller: |
case command_type_continuous_controller: |
103 |
dmsg(("Audio Thread: MIDI CC received\n")); |
dmsg(5,("Audio Thread: MIDI CC received\n")); |
104 |
ContinuousController(command.channel, command.number, command.value); |
ContinuousController(command.channel, command.number, command.value); |
105 |
break; |
break; |
106 |
} |
} |
114 |
|
|
115 |
|
|
116 |
// render audio from all active voices |
// render audio from all active voices |
117 |
int act_voices=0; |
int active_voices = 0; |
118 |
for (uint i = 0; i < MAX_AUDIO_VOICES; i++) { |
for (uint i = 0; i < MAX_AUDIO_VOICES; i++) { |
119 |
if (pVoices[i]->IsActive()) { |
if (pVoices[i]->IsActive()) { |
120 |
pVoices[i]->RenderAudio(); |
pVoices[i]->RenderAudio(); |
121 |
act_voices++; |
if (pVoices[i]->IsActive()) active_voices++; // still active |
122 |
|
else { // voice reached end, is now inactive |
123 |
|
ReleaseVoice(pVoices[i]); // remove voice from the list of active voices |
124 |
|
} |
125 |
} |
} |
126 |
} |
} |
127 |
// write that to the disk thread class so that it can print it |
// write that to the disk thread class so that it can print it |
128 |
// on the console for debugging purposes |
// on the console for debugging purposes |
129 |
ActiveVoiceCount=act_voices; |
ActiveVoiceCount = active_voices; |
130 |
|
if (ActiveVoiceCount > ActiveVoiceCountMax) ActiveVoiceCountMax = ActiveVoiceCount; |
131 |
|
|
132 |
|
|
133 |
// check clipping in the audio sum, convert to sample_type |
// check clipping in the audio sum, convert to sample_type |
144 |
// call audio driver to output sound |
// call audio driver to output sound |
145 |
int res = this->pAudioIO->Output(); |
int res = this->pAudioIO->Output(); |
146 |
if (res < 0) exit(EXIT_FAILURE); |
if (res < 0) exit(EXIT_FAILURE); |
|
|
|
|
// FIXME remove because we use it only to write to a fifo to save the audio |
|
|
//write(fifofd, pAudioIO->pOutputBuffer, pAudioIO->FragmentSize * pAudioIO->Channels * sizeof(short)); |
|
|
|
|
147 |
} |
} |
148 |
} |
} |
149 |
|
|
175 |
this->pCommandQueue->write(&cmd, 1); |
this->pCommandQueue->write(&cmd, 1); |
176 |
} |
} |
177 |
|
|
|
|
|
178 |
void AudioThread::ActivateVoice(uint8_t MIDIKey, uint8_t Velocity) { |
void AudioThread::ActivateVoice(uint8_t MIDIKey, uint8_t Velocity) { |
179 |
for (int i = 0; i < MAX_AUDIO_VOICES; i++) { |
for (int i = 0; i < MAX_AUDIO_VOICES; i++) { |
180 |
if (pVoices[i]->IsActive()) continue; |
if (pVoices[i]->IsActive()) continue; |
181 |
pVoices[i]->Trigger(MIDIKey, Velocity, this->pInstrument); |
|
182 |
// add (append) a new voice to the corresponding MIDIKey active voices list |
// add (append) a new voice to the corresponding MIDIKey active voices list |
183 |
Voice **new_voice_ptr=ActiveVoicePool->alloc_append(pActiveVoices[MIDIKey]); |
Voice** new_voice_ptr = ActiveVoicePool->alloc_append(pMIDIKeyInfo[MIDIKey].pActiveVoices); |
184 |
*new_voice_ptr=pVoices[i]; |
*new_voice_ptr = pVoices[i]; |
185 |
|
pVoices[i]->pSelfPtr = new_voice_ptr; // FIXME: hack to allow fast deallocation |
186 |
|
pVoices[i]->Trigger(MIDIKey, Velocity, this->pInstrument); |
187 |
|
|
188 |
|
if (!pMIDIKeyInfo[MIDIKey].hSustainPtr) { |
189 |
|
dmsg(4,("ActivateVoice(uint,uint): hSustainPtr == null, setting release pointer to the last voice on the key...\n")); |
190 |
|
pMIDIKeyInfo[MIDIKey].pActiveVoices->last(); |
191 |
|
pMIDIKeyInfo[MIDIKey].hSustainPtr = pMIDIKeyInfo[MIDIKey].pActiveVoices->current(); |
192 |
|
} |
193 |
return; |
return; |
194 |
} |
} |
195 |
std::cerr << "No free voice!" << std::endl << std::flush; |
std::cerr << "No free voice!" << std::endl << std::flush; |
196 |
} |
} |
197 |
|
|
198 |
|
/** |
199 |
|
* Releases the voices on the given key if sustain pedal is not pressed. |
200 |
|
* If sustain is pressed, the release of the note will be postponed until |
201 |
|
* sustain pedal will be released or voice turned inactive by itself (e.g. |
202 |
|
* due to completion of sample playback). |
203 |
|
*/ |
204 |
void AudioThread::ReleaseVoice(uint8_t MIDIKey, uint8_t Velocity) { |
void AudioThread::ReleaseVoice(uint8_t MIDIKey, uint8_t Velocity) { |
205 |
|
midi_key_info_t* pmidikey = &pMIDIKeyInfo[MIDIKey]; |
206 |
|
if (SustainPedal) { // if sustain pedal is pressed postpone the Note-Off |
207 |
|
if (pmidikey->hSustainPtr) { |
208 |
|
// stick the note-off information to the respective voice |
209 |
|
Voice** pVoiceToRelease = pmidikey->pActiveVoices->set_current(pmidikey->hSustainPtr); |
210 |
|
if (pVoiceToRelease) { |
211 |
|
(*pVoiceToRelease)->ReleaseVelocity = Velocity; |
212 |
|
// now increment the sustain pointer |
213 |
|
pmidikey->pActiveVoices->next(); |
214 |
|
pmidikey->hSustainPtr = pmidikey->pActiveVoices->current(); |
215 |
|
// if the key was not sustained yet, add it's MIDI key number to the sustained key pool |
216 |
|
if (!pmidikey->Sustained) { |
217 |
|
uint* sustainedmidikey = SustainedKeyPool->alloc(); |
218 |
|
*sustainedmidikey = MIDIKey; |
219 |
|
pmidikey->Sustained = true; |
220 |
|
} |
221 |
|
} |
222 |
|
else dmsg(3,("Ignoring NOTE OFF --> pVoiceToRelease == null!\n")); |
223 |
|
} |
224 |
|
else dmsg(3,("Ignoring NOTE OFF, seems like more Note-Offs than Note-Ons or no free voices available?\n")); |
225 |
|
} |
226 |
|
else { |
227 |
|
// get the first voice in the list of active voices on the MIDI Key |
228 |
|
Voice** pVoicePtr = pmidikey->pActiveVoices->first(); |
229 |
|
if (pVoicePtr) ReleaseVoice(*pVoicePtr); |
230 |
|
else dmsg(2,("Couldn't find active voice for note off command, maybe already released.\n")); |
231 |
|
} |
232 |
|
} |
233 |
|
|
234 |
|
/** |
235 |
// get the first voice in the list of active voices on the MIDI Key |
* Releases the voice given with pVoice (no matter if sustain is pressed or |
236 |
Voice** pVoicePtr = pActiveVoices[MIDIKey]->first(); |
* not). This method will e.g. be directly called if a voice went inactive |
237 |
Voice *pVoice=*pVoicePtr; |
* by itself. If susatain pedal is pressed the method takes care to free |
238 |
|
* those sustain informations of the voice. |
239 |
|
*/ |
240 |
|
void AudioThread::ReleaseVoice(Voice* pVoice) { |
241 |
if (pVoice) { |
if (pVoice) { |
242 |
|
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 |
243 |
|
|
244 |
// if sustain pedal is pressed postpone the Note-Off |
if (pMIDIKeyInfo[pVoice->MIDIKey].Sustained) { |
|
if(SustainPedal) { |
|
|
// alloc an element in the SustainedKeyPool and add the current midikey to it |
|
|
sustained_key_t *key=SustainedKeyPool->alloc(); |
|
|
if(key == NULL) { /* FIXME */ printf("ERROR: SustainedKeyPool FULL ! exiting\n"); exit(0); } |
|
|
key->midikey=MIDIKey; |
|
|
key->velocity=Velocity; |
|
|
return; |
|
|
} |
|
245 |
|
|
246 |
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 |
// check if the sustain pointer has to be moved, now that we release the voice |
247 |
|
RTEList<Voice*>::NodeHandle hSustainPtr = pMIDIKeyInfo[pVoice->MIDIKey].hSustainPtr; |
248 |
|
if (hSustainPtr) { |
249 |
|
Voice** pVoicePtr = pMIDIKeyInfo[pVoice->MIDIKey].pActiveVoices->set_current(hSustainPtr); |
250 |
|
if (pVoicePtr) { |
251 |
|
if (*pVoicePtr == pVoice) { // move sustain pointer to the next sustained voice |
252 |
|
dmsg(3,("Correcting sustain pointer\n")); |
253 |
|
pMIDIKeyInfo[pVoice->MIDIKey].pActiveVoices->next(); |
254 |
|
pMIDIKeyInfo[pVoice->MIDIKey].hSustainPtr = pMIDIKeyInfo[pVoice->MIDIKey].pActiveVoices->current(); |
255 |
|
} |
256 |
|
else dmsg(4,("ReleaseVoice(Voice*): *hSustain != pVoice\n")); |
257 |
|
} |
258 |
|
else dmsg(3,("ReleaseVoice(Voice*): pVoicePtr == null\n")); |
259 |
|
} |
260 |
|
else dmsg(3,("ReleaseVoice(Voice*): hSustainPtr == null\n")); |
261 |
|
|
262 |
// remove the voice from the list associated to this MIDI key |
// remove the voice from the list associated with this MIDI key |
263 |
ActiveVoicePool->free(pVoicePtr); |
ActiveVoicePool->free(pVoice->pSelfPtr); |
264 |
|
|
265 |
|
// check if there are no sustained voices left on the MIDI key and update the key info if so |
266 |
|
if (pMIDIKeyInfo[pVoice->MIDIKey].pActiveVoices->is_empty()) { |
267 |
|
pMIDIKeyInfo[pVoice->MIDIKey].hSustainPtr = NULL; |
268 |
|
pMIDIKeyInfo[pVoice->MIDIKey].Sustained = false; |
269 |
|
dmsg(3,("Key now not sustained\n")); |
270 |
|
} |
271 |
|
} |
272 |
|
else { |
273 |
|
// remove the voice from the list associated with this MIDI key |
274 |
|
ActiveVoicePool->free(pVoice->pSelfPtr); |
275 |
|
dmsg(4,("Key was not sustained\n")); |
276 |
|
} |
277 |
} |
} |
278 |
else std::cerr << "Couldn't find active voice for note off command!" << std::endl << std::flush; |
else std::cerr << "Couldn't release voice! (pVoice == NULL)\n" << std::flush; |
279 |
} |
} |
280 |
|
|
281 |
void AudioThread::ContinuousController(uint8_t Channel, uint8_t Number, uint8_t Value) { |
void AudioThread::ContinuousController(uint8_t Channel, uint8_t Number, uint8_t Value) { |
282 |
//printf("AudioThread::ContinuousController c=%d n=%d v=%d\n",Channel, Number, Value); |
dmsg(4,("AudioThread::ContinuousController c=%d n=%d v=%d\n", Channel, Number, Value)); |
283 |
if(Number == 64) { |
if (Number == 64) { |
284 |
if(Value >=64 && PrevHoldCCValue < 64) { |
if (Value >= 64 && PrevHoldCCValue < 64) { |
285 |
//printf("PEDAL DOWN\n"); |
dmsg(4,("PEDAL DOWN\n")); |
286 |
SustainPedal=1; |
SustainPedal = true; |
287 |
} |
} |
288 |
if(Value < 64 && PrevHoldCCValue >=64) { |
if (Value < 64 && PrevHoldCCValue >= 64) { |
289 |
//printf("PEDAL UP\n"); |
dmsg(4,("PEDAL UP\n")); |
290 |
SustainPedal=0; |
SustainPedal = false; |
291 |
sustained_key_t *key; |
// iterate through all keys that are currently sustained |
292 |
for(key = SustainedKeyPool->first(); key ; key=SustainedKeyPool->next() ) { |
for (uint* key = SustainedKeyPool->first(); key; key = SustainedKeyPool->next()) { |
293 |
ReleaseVoice(key->midikey, key->velocity); |
// release all active voices on the midi key |
294 |
} |
Voice** pVoicePtr = pMIDIKeyInfo[*key].pActiveVoices->first(); |
295 |
// empty the SustainedKeyPool (free all the elements) |
while (pVoicePtr) { |
296 |
SustainedKeyPool->empty(); |
Voice** pVoicePtrNext = pMIDIKeyInfo[*key].pActiveVoices->next(); |
297 |
|
dmsg(3,("Sustain CC: releasing voice on midi key %d\n", *key)); |
298 |
|
ReleaseVoice(*pVoicePtr); |
299 |
|
pVoicePtr = pVoicePtrNext; |
300 |
|
} |
301 |
|
} |
302 |
|
// empty the SustainedKeyPool (free all the elements) |
303 |
|
SustainedKeyPool->empty(); |
304 |
|
} |
305 |
|
PrevHoldCCValue = Value; |
306 |
} |
} |
|
PrevHoldCCValue=Value; |
|
|
} |
|
|
|
|
307 |
} |
} |
308 |
|
|
|
|
|
309 |
void AudioThread::CacheInitialSamples(gig::Sample* pSample) { |
void AudioThread::CacheInitialSamples(gig::Sample* pSample) { |
310 |
if (!pSample || pSample->GetCache().Size) return; |
if (!pSample || pSample->GetCache().Size) return; |
311 |
if (pSample->SamplesTotal <= NUM_RAM_PRELOAD_SAMPLES) { |
if (pSample->SamplesTotal <= NUM_RAM_PRELOAD_SAMPLES) { |
315 |
// border, to allow the interpolator do it's work even at the end of |
// border, to allow the interpolator do it's work even at the end of |
316 |
// the sample. |
// the sample. |
317 |
gig::buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(pAudioIO->FragmentSize << MAX_PITCH); |
gig::buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(pAudioIO->FragmentSize << MAX_PITCH); |
318 |
dmsg(("Cached %d Bytes, %d silence bytes.\n", buf.Size, buf.NullExtensionSize)); |
dmsg(4,("Cached %d Bytes, %d silence bytes.\n", buf.Size, buf.NullExtensionSize)); |
319 |
} |
} |
320 |
else { // we only cache NUM_RAM_PRELOAD_SAMPLES and stream the other sample points from disk |
else { // we only cache NUM_RAM_PRELOAD_SAMPLES and stream the other sample points from disk |
321 |
pSample->LoadSampleData(NUM_RAM_PRELOAD_SAMPLES); |
pSample->LoadSampleData(NUM_RAM_PRELOAD_SAMPLES); |