--- linuxsampler/trunk/src/engines/gig/Engine.cpp 2005/04/03 19:53:43 493 +++ linuxsampler/trunk/src/engines/gig/Engine.cpp 2005/06/17 19:49:30 660 @@ -103,10 +103,10 @@ pAudioOutputDevice = NULL; pDiskThread = NULL; pEventGenerator = NULL; - pSysexBuffer = new RingBuffer(SYSEX_BUFFER_SIZE, 0); - pEventQueue = new RingBuffer(MAX_EVENTS_PER_FRAGMENT, 0); - pEventPool = new Pool(MAX_EVENTS_PER_FRAGMENT); - pVoicePool = new Pool(MAX_AUDIO_VOICES); + pSysexBuffer = new RingBuffer(CONFIG_SYSEX_BUFFER_SIZE, 0); + pEventQueue = new RingBuffer(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0); + pEventPool = new Pool(CONFIG_MAX_EVENTS_PER_FRAGMENT); + pVoicePool = new Pool(CONFIG_MAX_VOICES); pVoiceStealingQueue = new RTList(pEventPool); pGlobalEvents = new RTList(pEventPool); for (RTList::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { @@ -119,6 +119,7 @@ pMainFilterParameters = NULL; ResetInternal(); + ResetScaleTuning(); } /** @@ -171,6 +172,7 @@ void Engine::Reset() { DisableAndLock(); ResetInternal(); + ResetScaleTuning(); Enable(); } @@ -184,12 +186,11 @@ // reset voice stealing parameters pVoiceStealingQueue->clear(); - itLastStolenVoice = RTList::Iterator(); - iuiLastStolenKey = RTList::Iterator(); - pLastStolenChannel = NULL; - - // reset to normal chromatic scale (means equal temper) - memset(&ScaleTuning[0], 0x00, 12); + itLastStolenVoice = RTList::Iterator(); + itLastStolenVoiceGlobally = RTList::Iterator(); + iuiLastStolenKey = RTList::Iterator(); + iuiLastStolenKeyGlobally = RTList::Iterator(); + pLastStolenChannel = NULL; // reset all voices for (RTList::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { @@ -205,6 +206,13 @@ } /** + * Reset to normal, chromatic scale (means equal tempered). + */ + void Engine::ResetScaleTuning() { + memset(&ScaleTuning[0], 0x00, 12); + } + + /** * Connect this engine instance with the given audio output device. * This method will be called when an Engine instance is created. * All of the engine's data structures which are dependant to the used @@ -231,9 +239,9 @@ this->SampleRate = pAudioOutputDevice->SampleRate(); // FIXME: audio drivers with varying fragment sizes might be a problem here - MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * EG_MIN_RELEASE_TIME) - 1; + MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * CONFIG_EG_MIN_RELEASE_TIME) - 1; if (MaxFadeOutPos < 0) - throw LinuxSamplerException("EG_MIN_RELEASE_TIME in EGADSR.h too big for current audio fragment size / sampling rate!"); + throw LinuxSamplerException("CONFIG_EG_MIN_RELEASE_TIME too big for current audio fragment size / sampling rate!"); // (re)create disk thread if (this->pDiskThread) { @@ -242,7 +250,7 @@ delete this->pDiskThread; dmsg(1,("OK\n")); } - this->pDiskThread = new DiskThread(((pAudioOut->MaxSamplesPerCycle() << MAX_PITCH) << 1) + 6); //FIXME: assuming stereo + this->pDiskThread = new DiskThread(((pAudioOut->MaxSamplesPerCycle() << CONFIG_MAX_PITCH) << 1) + 6); //FIXME: assuming stereo if (!pDiskThread) { dmsg(0,("gig::Engine new diskthread = NULL\n")); exit(EXIT_FAILURE); @@ -370,10 +378,10 @@ } } - // We only allow a maximum of MAX_AUDIO_VOICES voices to be stolen + // We only allow a maximum of CONFIG_MAX_VOICES voices to be stolen // in each audio fragment. All subsequent request for spawning new // voices in the same audio fragment will be ignored. - VoiceTheftsLeft = MAX_AUDIO_VOICES; + VoiceTheftsLeft = CONFIG_MAX_VOICES; // reset internal voice counter (just for statistic of active voices) ActiveVoiceCountTemp = 0; @@ -406,14 +414,13 @@ // reset voice stealing for the next audio fragment pVoiceStealingQueue->clear(); - itLastStolenVoice = RTList::Iterator(); - iuiLastStolenKey = RTList::Iterator(); - pLastStolenChannel = NULL; // just some statistics about this engine instance ActiveVoiceCount = ActiveVoiceCountTemp; if (ActiveVoiceCount > ActiveVoiceCountMax) ActiveVoiceCountMax = ActiveVoiceCount; + FrameTime += Samples; + return 0; } @@ -456,6 +463,13 @@ } } } + + // reset voice stealing for the next engine channel (or next audio fragment) + itLastStolenVoice = RTList::Iterator(); + itLastStolenVoiceGlobally = RTList::Iterator(); + iuiLastStolenKey = RTList::Iterator(); + iuiLastStolenKeyGlobally = RTList::Iterator(); + pLastStolenChannel = NULL; } /** @@ -539,8 +553,8 @@ midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey]; ++iuiKey; if (pKey->pActiveVoices->isEmpty()) FreeKey(pEngineChannel, pKey); - #if DEVMODE - else { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging) + #if CONFIG_DEVMODE + else { // just a sanity check for debugging RTList::Iterator itVoice = pKey->pActiveVoices->first(); RTList::Iterator itVoicesEnd = pKey->pActiveVoices->end(); for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key @@ -549,7 +563,7 @@ } } } - #endif // DEVMODE + #endif // CONFIG_DEVMODE } } @@ -584,7 +598,7 @@ // finally place sysex event into input event queue pEventQueue->push(&event); } - else dmsg(1,("Engine: Sysex message too large (%d byte) for input buffer (%d byte)!",Size,SYSEX_BUFFER_SIZE)); + else dmsg(1,("Engine: Sysex message too large (%d byte) for input buffer (%d byte)!",Size,CONFIG_SYSEX_BUFFER_SIZE)); } else dmsg(1,("Engine: Input event queue full!")); } @@ -610,6 +624,8 @@ midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[key]; pKey->KeyPressed = true; // the MIDI key was now pressed down + pKey->Velocity = itNoteOnEvent->Param.Note.Velocity; + pKey->NoteOnTime = FrameTime + itNoteOnEvent->FragmentPos(); // will be used to calculate note length // cancel release process of voices on this key if needed if (pKey->Active && !pEngineChannel->SustainPedal) { @@ -660,27 +676,31 @@ // release voices on this key if needed if (pKey->Active && !pEngineChannel->SustainPedal) { itNoteOffEvent->Type = Event::type_release; // transform event type - } - // move event to the key's own event list - RTList::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents); + // move event to the key's own event list + RTList::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents); - // spawn release triggered voice(s) if needed - if (pKey->ReleaseTrigger) { - // first, get total amount of required voices (dependant on amount of layers) - ::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(itNoteOffEventOnKeyList->Param.Note.Key); - if (pRegion) { - int voicesRequired = pRegion->Layers; - // now launch the required amount of voices - for (int i = 0; i < voicesRequired; i++) - LaunchVoice(pEngineChannel, itNoteOffEventOnKeyList, i, true, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples + // spawn release triggered voice(s) if needed + if (pKey->ReleaseTrigger) { + // first, get total amount of required voices (dependant on amount of layers) + ::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(itNoteOffEventOnKeyList->Param.Note.Key); + if (pRegion) { + int voicesRequired = pRegion->Layers; + + // MIDI note-on velocity is used instead of note-off velocity + itNoteOffEventOnKeyList->Param.Note.Velocity = pKey->Velocity; + + // now launch the required amount of voices + for (int i = 0; i < voicesRequired; i++) + LaunchVoice(pEngineChannel, itNoteOffEventOnKeyList, i, true, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples + } + pKey->ReleaseTrigger = false; } - pKey->ReleaseTrigger = false; - } - // if neither a voice was spawned or postponed then remove note off event from key again - if (!pKey->Active && !pKey->VoiceTheftsQueued) - pKey->pEvents->free(itNoteOffEventOnKeyList); + // if neither a voice was spawned or postponed then remove note off event from key again + if (!pKey->Active && !pKey->VoiceTheftsQueued) + pKey->pEvents->free(itNoteOffEventOnKeyList); + } } /** @@ -781,7 +801,7 @@ */ int Engine::StealVoice(EngineChannel* pEngineChannel, Pool::Iterator& itNoteOnEvent) { if (!VoiceTheftsLeft) { - dmsg(1,("Max. voice thefts per audio fragment reached (you may raise MAX_AUDIO_VOICES).\n")); + dmsg(1,("Max. voice thefts per audio fragment reached (you may raise CONFIG_MAX_VOICES).\n")); return -1; } if (!pEventPool->poolIsEmpty()) { @@ -789,45 +809,54 @@ RTList::Iterator itSelectedVoice; // Select one voice for voice stealing - switch (VOICE_STEAL_ALGORITHM) { + switch (CONFIG_VOICE_STEAL_ALGO) { // try to pick the oldest voice on the key where the new // voice should be spawned, if there is no voice on that - // key, or no voice left to kill there, then procceed with + // key, or no voice left to kill, then procceed with // 'oldestkey' algorithm case voice_steal_algo_oldestvoiceonkey: { - #if 0 // FIXME: broken midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; - if (this->itLastStolenVoice) { - itSelectedVoice = this->itLastStolenVoice; - ++itSelectedVoice; - } - else { // no voice stolen in this audio fragment cycle yet - itSelectedVoice = pSelectedKey->pActiveVoices->first(); - } - if (itSelectedVoice) { - iuiSelectedKey = pSelectedKey->itSelf; - break; // selection succeeded - } - #endif + itSelectedVoice = pSelectedKey->pActiveVoices->first(); + // proceed iterating if voice was created in this fragment cycle + while (itSelectedVoice && !itSelectedVoice->hasRendered()) ++itSelectedVoice; + // if we haven't found a voice then proceed with algorithm 'oldestkey' + if (itSelectedVoice && itSelectedVoice->hasRendered()) break; } // no break - intentional ! // try to pick the oldest voice on the oldest active key + // from the same engine channel // (caution: must stay after 'oldestvoiceonkey' algorithm !) case voice_steal_algo_oldestkey: { + // if we already stole in this fragment, try to proceed on same key if (this->itLastStolenVoice) { itSelectedVoice = this->itLastStolenVoice; - ++itSelectedVoice; - if (itSelectedVoice) break; // selection succeeded - RTList::Iterator iuiSelectedKey = this->iuiLastStolenKey; - ++iuiSelectedKey; - if (iuiSelectedKey) { - this->iuiLastStolenKey = iuiSelectedKey; - midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[*iuiSelectedKey]; - itSelectedVoice = pSelectedKey->pActiveVoices->first(); + do { + ++itSelectedVoice; + } while (itSelectedVoice && !itSelectedVoice->hasRendered()); // proceed iterating if voice was created in this fragment cycle + // found a "stealable" voice ? + if (itSelectedVoice && itSelectedVoice->hasRendered()) { + // remember which voice we stole, so we can simply proceed on next voice stealing + this->itLastStolenVoice = itSelectedVoice; break; // selection succeeded } } + // get (next) oldest key + RTList::Iterator iuiSelectedKey = (this->iuiLastStolenKey) ? ++this->iuiLastStolenKey : pEngineChannel->pActiveKeys->first(); + while (iuiSelectedKey) { + midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[*iuiSelectedKey]; + itSelectedVoice = pSelectedKey->pActiveVoices->first(); + // proceed iterating if voice was created in this fragment cycle + while (itSelectedVoice && !itSelectedVoice->hasRendered()) ++itSelectedVoice; + // found a "stealable" voice ? + if (itSelectedVoice && itSelectedVoice->hasRendered()) { + // remember which voice on which key we stole, so we can simply proceed on next voice stealing + this->iuiLastStolenKey = iuiSelectedKey; + this->itLastStolenVoice = itSelectedVoice; + break; // selection succeeded + } + ++iuiSelectedKey; // get next oldest key + } break; } @@ -839,36 +868,68 @@ } } - // steal oldest voice on the oldest key from this or any other engine channel - if (!itSelectedVoice) { - EngineChannel* pSelectedChannel = (pLastStolenChannel) ? pLastStolenChannel : pEngineChannel; - int iChannelIndex = pSelectedChannel->iEngineIndexSelf; + // if we couldn't steal a voice from the same engine channel then + // steal oldest voice on the oldest key from any other engine channel + // (the smaller engine channel number, the higher priority) + if (!itSelectedVoice || !itSelectedVoice->hasRendered()) { + EngineChannel* pSelectedChannel; + int iChannelIndex; + // select engine channel + if (pLastStolenChannel) { + pSelectedChannel = pLastStolenChannel; + iChannelIndex = pSelectedChannel->iEngineIndexSelf; + } else { // pick the engine channel followed by this engine channel + iChannelIndex = (pEngineChannel->iEngineIndexSelf + 1) % engineChannels.size(); + pSelectedChannel = engineChannels[iChannelIndex]; + } + // iterate through engine channels while (true) { - RTList::Iterator iuiSelectedKey = pSelectedChannel->pActiveKeys->first(); - if (iuiSelectedKey) { - midi_key_info_t* pSelectedKey = &pSelectedChannel->pMIDIKeyInfo[*iuiSelectedKey]; - itSelectedVoice = pSelectedKey->pActiveVoices->first(); - iuiLastStolenKey = iuiSelectedKey; - pLastStolenChannel = pSelectedChannel; - break; // selection succeeded + // if we already stole in this fragment, try to proceed on same key + if (this->itLastStolenVoiceGlobally) { + itSelectedVoice = this->itLastStolenVoiceGlobally; + do { + ++itSelectedVoice; + } while (itSelectedVoice && !itSelectedVoice->hasRendered()); // proceed iterating if voice was created in this fragment cycle + // break if selection succeeded + if (itSelectedVoice && itSelectedVoice->hasRendered()) { + // remember which voice we stole, so we can simply proceed on next voice stealing + this->itLastStolenVoiceGlobally = itSelectedVoice; + break; // selection succeeded + } } + // get (next) oldest key + RTList::Iterator iuiSelectedKey = (this->iuiLastStolenKey) ? ++this->iuiLastStolenKey : pSelectedChannel->pActiveKeys->first(); + while (iuiSelectedKey) { + midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[*iuiSelectedKey]; + itSelectedVoice = pSelectedKey->pActiveVoices->first(); + // proceed iterating if voice was created in this fragment cycle + while (itSelectedVoice && !itSelectedVoice->hasRendered()) ++itSelectedVoice; + // found a "stealable" voice ? + if (itSelectedVoice && itSelectedVoice->hasRendered()) { + // remember which voice on which key on which engine channel we stole, so we can simply proceed on next voice stealing + this->iuiLastStolenKeyGlobally = iuiSelectedKey; + this->itLastStolenVoiceGlobally = itSelectedVoice; + this->pLastStolenChannel = pSelectedChannel; + break; // selection succeeded + } + ++iuiSelectedKey; // get next key on current engine channel + } + // get next engine channel iChannelIndex = (iChannelIndex + 1) % engineChannels.size(); - pSelectedChannel = engineChannels[iChannelIndex]; + pSelectedChannel = engineChannels[iChannelIndex]; } } - //FIXME: can be removed, just a sanity check for debugging + #if CONFIG_DEVMODE if (!itSelectedVoice->IsActive()) { dmsg(1,("gig::Engine: ERROR, tried to steal a voice which was not active !!!\n")); return -1; } + #endif // CONFIG_DEVMODE // now kill the selected voice itSelectedVoice->Kill(itNoteOnEvent); - // remember which voice we stole, so we can simply proceed for the next voice stealing - itLastStolenVoice = itSelectedVoice; - --VoiceTheftsLeft; return 0; // success @@ -945,6 +1006,7 @@ case 7: { // volume //TODO: not sample accurate yet pEngineChannel->GlobalVolume = (float) itControlChangeEventOnCCList->Param.CC.Value / 127.0f; + pEngineChannel->bStatusChanged = true; // engine channel status has changed, so set notify flag break; } case 10: { // panpot @@ -1054,8 +1116,9 @@ if (reader.read(&scale_tunes[0], 12) != 12) goto free_sysex_data; uint8_t checksum; if (!reader.pop(&checksum)) goto free_sysex_data; - // some are not sending a GS checksum, so we ignore it for now - //if (GSCheckSum(checksum_reader, 12)) goto free_sysex_data; + #if CONFIG_ASSERT_GS_SYSEX_CHECKSUM + if (GSCheckSum(checksum_reader, 12)) goto free_sysex_data; + #endif // CONFIG_ASSERT_GS_SYSEX_CHECKSUM for (int i = 0; i < 12; i++) scale_tunes[i] -= 64; AdjustScale((int8_t*) scale_tunes); dmsg(3,("\t\t\tNew scale applied.\n")); @@ -1199,7 +1262,7 @@ } String Engine::Version() { - String s = "$Revision: 1.35 $"; + String s = "$Revision: 1.43 $"; return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword }