240 |
|
|
241 |
// FIXME: audio drivers with varying fragment sizes might be a problem here |
// FIXME: audio drivers with varying fragment sizes might be a problem here |
242 |
MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * CONFIG_EG_MIN_RELEASE_TIME) - 1; |
MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * CONFIG_EG_MIN_RELEASE_TIME) - 1; |
243 |
if (MaxFadeOutPos < 0) |
if (MaxFadeOutPos < 0) { |
244 |
throw LinuxSamplerException("CONFIG_EG_MIN_RELEASE_TIME too big for current audio fragment size / sampling rate!"); |
std::cerr << "gig::Engine: WARNING, CONFIG_EG_MIN_RELEASE_TIME " |
245 |
|
<< "too big for current audio fragment size & sampling rate! " |
246 |
|
<< "May lead to click sounds if voice stealing chimes in!\n" << std::flush; |
247 |
|
// force volume ramp downs at the beginning of each fragment |
248 |
|
MaxFadeOutPos = 0; |
249 |
|
// lower minimum release time |
250 |
|
const float minReleaseTime = (float) MaxSamplesPerCycle / (float) SampleRate; |
251 |
|
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
252 |
|
iterVoice->pEG1->CalculateFadeOutCoeff(minReleaseTime, SampleRate); |
253 |
|
} |
254 |
|
pVoicePool->clear(); |
255 |
|
} |
256 |
|
|
257 |
// (re)create disk thread |
// (re)create disk thread |
258 |
if (this->pDiskThread) { |
if (this->pDiskThread) { |
492 |
* this audio fragment cycle |
* this audio fragment cycle |
493 |
*/ |
*/ |
494 |
void Engine::RenderActiveVoices(EngineChannel* pEngineChannel, uint Samples) { |
void Engine::RenderActiveVoices(EngineChannel* pEngineChannel, uint Samples) { |
495 |
|
if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted |
496 |
|
|
497 |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
498 |
RTList<uint>::Iterator end = pEngineChannel->pActiveKeys->end(); |
RTList<uint>::Iterator end = pEngineChannel->pActiveKeys->end(); |
499 |
while (iuiKey != end) { // iterate through all active keys |
while (iuiKey != end) { // iterate through all active keys |
532 |
for (; itVoiceStealEvent != end; ++itVoiceStealEvent) { |
for (; itVoiceStealEvent != end; ++itVoiceStealEvent) { |
533 |
EngineChannel* pEngineChannel = (EngineChannel*) itVoiceStealEvent->pEngineChannel; |
EngineChannel* pEngineChannel = (EngineChannel*) itVoiceStealEvent->pEngineChannel; |
534 |
Pool<Voice>::Iterator itNewVoice = |
Pool<Voice>::Iterator itNewVoice = |
535 |
LaunchVoice(pEngineChannel, itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false); |
LaunchVoice(pEngineChannel, itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false, false); |
536 |
if (itNewVoice) { |
if (itNewVoice) { |
537 |
itNewVoice->Render(Samples); |
itNewVoice->Render(Samples); |
538 |
if (itNewVoice->IsActive()) ActiveVoiceCountTemp++; // still active |
if (itNewVoice->IsActive()) ActiveVoiceCountTemp++; // still active |
622 |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
623 |
*/ |
*/ |
624 |
void Engine::ProcessNoteOn(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) { |
void Engine::ProcessNoteOn(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) { |
625 |
|
if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted |
626 |
|
|
627 |
const int key = itNoteOnEvent->Param.Note.Key; |
const int key = itNoteOnEvent->Param.Note.Key; |
628 |
|
|
661 |
int voicesRequired = pRegion->Layers; |
int voicesRequired = pRegion->Layers; |
662 |
// now launch the required amount of voices |
// now launch the required amount of voices |
663 |
for (int i = 0; i < voicesRequired; i++) |
for (int i = 0; i < voicesRequired; i++) |
664 |
LaunchVoice(pEngineChannel, itNoteOnEventOnKeyList, i, false, true); |
LaunchVoice(pEngineChannel, itNoteOnEventOnKeyList, i, false, true, true); |
665 |
} |
} |
666 |
} |
} |
667 |
|
|
682 |
* @param itNoteOffEvent - key, velocity and time stamp of the event |
* @param itNoteOffEvent - key, velocity and time stamp of the event |
683 |
*/ |
*/ |
684 |
void Engine::ProcessNoteOff(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOffEvent) { |
void Engine::ProcessNoteOff(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOffEvent) { |
685 |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key]; |
if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted |
686 |
|
|
687 |
|
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key]; |
688 |
pKey->KeyPressed = false; // the MIDI key was now released |
pKey->KeyPressed = false; // the MIDI key was now released |
689 |
|
|
690 |
// release voices on this key if needed |
// release voices on this key if needed |
706 |
|
|
707 |
// now launch the required amount of voices |
// now launch the required amount of voices |
708 |
for (int i = 0; i < voicesRequired; i++) |
for (int i = 0; i < voicesRequired; i++) |
709 |
LaunchVoice(pEngineChannel, itNoteOffEventOnKeyList, i, true, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples |
LaunchVoice(pEngineChannel, itNoteOffEventOnKeyList, i, true, false, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples |
710 |
} |
} |
711 |
pKey->ReleaseTrigger = false; |
pKey->ReleaseTrigger = false; |
712 |
} |
} |
743 |
* @param VoiceStealing - if voice stealing should be performed |
* @param VoiceStealing - if voice stealing should be performed |
744 |
* when there is no free voice |
* when there is no free voice |
745 |
* (optional, default = true) |
* (optional, default = true) |
746 |
|
* @param HandleKeyGroupConflicts - if voices should be killed due to a |
747 |
|
* key group conflict |
748 |
* @returns pointer to new voice or NULL if there was no free voice or |
* @returns pointer to new voice or NULL if there was no free voice or |
749 |
* if the voice wasn't triggered (for example when no region is |
* if the voice wasn't triggered (for example when no region is |
750 |
* defined for the given key). |
* defined for the given key). |
751 |
*/ |
*/ |
752 |
Pool<Voice>::Iterator Engine::LaunchVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing) { |
Pool<Voice>::Iterator Engine::LaunchVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing, bool HandleKeyGroupConflicts) { |
753 |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
int MIDIKey = itNoteOnEvent->Param.Note.Key; |
754 |
|
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[MIDIKey]; |
755 |
|
::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(MIDIKey); |
756 |
|
|
757 |
|
// if nothing defined for this key |
758 |
|
if (!pRegion) return Pool<Voice>::Iterator(); // nothing to do |
759 |
|
|
760 |
|
// only mark the first voice of a layered voice (group) to be in a |
761 |
|
// key group, so the layered voices won't kill each other |
762 |
|
int iKeyGroup = (iLayer == 0 && !ReleaseTriggerVoice) ? pRegion->KeyGroup : 0; |
763 |
|
|
764 |
|
// handle key group (a.k.a. exclusive group) conflicts |
765 |
|
if (HandleKeyGroupConflicts) { |
766 |
|
if (iKeyGroup) { // if this voice / key belongs to a key group |
767 |
|
uint** ppKeyGroup = &pEngineChannel->ActiveKeyGroups[iKeyGroup]; |
768 |
|
if (*ppKeyGroup) { // if there's already an active key in that key group |
769 |
|
midi_key_info_t* pOtherKey = &pEngineChannel->pMIDIKeyInfo[**ppKeyGroup]; |
770 |
|
// kill all voices on the (other) key |
771 |
|
RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first(); |
772 |
|
RTList<Voice>::Iterator end = pOtherKey->pActiveVoices->end(); |
773 |
|
for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) { |
774 |
|
if (itVoiceToBeKilled->Type != Voice::type_release_trigger) { |
775 |
|
itVoiceToBeKilled->Kill(itNoteOnEvent); |
776 |
|
--VoiceSpawnsLeft; //FIXME: just a hack, we should better check in StealVoice() if the voice was killed due to key conflict |
777 |
|
} |
778 |
|
} |
779 |
|
} |
780 |
|
} |
781 |
|
} |
782 |
|
|
783 |
|
Voice::type_t VoiceType = Voice::type_normal; |
784 |
|
|
785 |
|
// get current dimension values to select the right dimension region |
786 |
|
//TODO: for stolen voices this dimension region selection block is processed twice, this should be changed |
787 |
|
//FIXME: controller values for selecting the dimension region here are currently not sample accurate |
788 |
|
uint DimValues[8] = { 0 }; |
789 |
|
for (int i = pRegion->Dimensions - 1; i >= 0; i--) { |
790 |
|
switch (pRegion->pDimensionDefinitions[i].dimension) { |
791 |
|
case ::gig::dimension_samplechannel: |
792 |
|
DimValues[i] = 0; //TODO: we currently ignore this dimension |
793 |
|
break; |
794 |
|
case ::gig::dimension_layer: |
795 |
|
DimValues[i] = iLayer; |
796 |
|
break; |
797 |
|
case ::gig::dimension_velocity: |
798 |
|
DimValues[i] = itNoteOnEvent->Param.Note.Velocity; |
799 |
|
break; |
800 |
|
case ::gig::dimension_channelaftertouch: |
801 |
|
DimValues[i] = 0; //TODO: we currently ignore this dimension |
802 |
|
break; |
803 |
|
case ::gig::dimension_releasetrigger: |
804 |
|
VoiceType = (ReleaseTriggerVoice) ? Voice::type_release_trigger : (!iLayer) ? Voice::type_release_trigger_required : Voice::type_normal; |
805 |
|
DimValues[i] = (uint) ReleaseTriggerVoice; |
806 |
|
break; |
807 |
|
case ::gig::dimension_keyboard: |
808 |
|
DimValues[i] = (uint) pEngineChannel->CurrentKeyDimension; |
809 |
|
break; |
810 |
|
case ::gig::dimension_roundrobin: |
811 |
|
DimValues[i] = (uint) pEngineChannel->pMIDIKeyInfo[MIDIKey].RoundRobinIndex; // incremented for each note on |
812 |
|
break; |
813 |
|
case ::gig::dimension_random: |
814 |
|
RandomSeed = RandomSeed * 1103515245 + 12345; // classic pseudo random number generator |
815 |
|
DimValues[i] = (uint) RandomSeed >> (32 - pRegion->pDimensionDefinitions[i].bits); // highest bits are most random |
816 |
|
break; |
817 |
|
case ::gig::dimension_modwheel: |
818 |
|
DimValues[i] = pEngineChannel->ControllerTable[1]; |
819 |
|
break; |
820 |
|
case ::gig::dimension_breath: |
821 |
|
DimValues[i] = pEngineChannel->ControllerTable[2]; |
822 |
|
break; |
823 |
|
case ::gig::dimension_foot: |
824 |
|
DimValues[i] = pEngineChannel->ControllerTable[4]; |
825 |
|
break; |
826 |
|
case ::gig::dimension_portamentotime: |
827 |
|
DimValues[i] = pEngineChannel->ControllerTable[5]; |
828 |
|
break; |
829 |
|
case ::gig::dimension_effect1: |
830 |
|
DimValues[i] = pEngineChannel->ControllerTable[12]; |
831 |
|
break; |
832 |
|
case ::gig::dimension_effect2: |
833 |
|
DimValues[i] = pEngineChannel->ControllerTable[13]; |
834 |
|
break; |
835 |
|
case ::gig::dimension_genpurpose1: |
836 |
|
DimValues[i] = pEngineChannel->ControllerTable[16]; |
837 |
|
break; |
838 |
|
case ::gig::dimension_genpurpose2: |
839 |
|
DimValues[i] = pEngineChannel->ControllerTable[17]; |
840 |
|
break; |
841 |
|
case ::gig::dimension_genpurpose3: |
842 |
|
DimValues[i] = pEngineChannel->ControllerTable[18]; |
843 |
|
break; |
844 |
|
case ::gig::dimension_genpurpose4: |
845 |
|
DimValues[i] = pEngineChannel->ControllerTable[19]; |
846 |
|
break; |
847 |
|
case ::gig::dimension_sustainpedal: |
848 |
|
DimValues[i] = pEngineChannel->ControllerTable[64]; |
849 |
|
break; |
850 |
|
case ::gig::dimension_portamento: |
851 |
|
DimValues[i] = pEngineChannel->ControllerTable[65]; |
852 |
|
break; |
853 |
|
case ::gig::dimension_sostenutopedal: |
854 |
|
DimValues[i] = pEngineChannel->ControllerTable[66]; |
855 |
|
break; |
856 |
|
case ::gig::dimension_softpedal: |
857 |
|
DimValues[i] = pEngineChannel->ControllerTable[67]; |
858 |
|
break; |
859 |
|
case ::gig::dimension_genpurpose5: |
860 |
|
DimValues[i] = pEngineChannel->ControllerTable[80]; |
861 |
|
break; |
862 |
|
case ::gig::dimension_genpurpose6: |
863 |
|
DimValues[i] = pEngineChannel->ControllerTable[81]; |
864 |
|
break; |
865 |
|
case ::gig::dimension_genpurpose7: |
866 |
|
DimValues[i] = pEngineChannel->ControllerTable[82]; |
867 |
|
break; |
868 |
|
case ::gig::dimension_genpurpose8: |
869 |
|
DimValues[i] = pEngineChannel->ControllerTable[83]; |
870 |
|
break; |
871 |
|
case ::gig::dimension_effect1depth: |
872 |
|
DimValues[i] = pEngineChannel->ControllerTable[91]; |
873 |
|
break; |
874 |
|
case ::gig::dimension_effect2depth: |
875 |
|
DimValues[i] = pEngineChannel->ControllerTable[92]; |
876 |
|
break; |
877 |
|
case ::gig::dimension_effect3depth: |
878 |
|
DimValues[i] = pEngineChannel->ControllerTable[93]; |
879 |
|
break; |
880 |
|
case ::gig::dimension_effect4depth: |
881 |
|
DimValues[i] = pEngineChannel->ControllerTable[94]; |
882 |
|
break; |
883 |
|
case ::gig::dimension_effect5depth: |
884 |
|
DimValues[i] = pEngineChannel->ControllerTable[95]; |
885 |
|
break; |
886 |
|
case ::gig::dimension_none: |
887 |
|
std::cerr << "gig::Engine::LaunchVoice() Error: dimension=none\n" << std::flush; |
888 |
|
break; |
889 |
|
default: |
890 |
|
std::cerr << "gig::Engine::LaunchVoice() Error: Unknown dimension\n" << std::flush; |
891 |
|
} |
892 |
|
} |
893 |
|
::gig::DimensionRegion* pDimRgn = pRegion->GetDimensionRegionByValue(DimValues); |
894 |
|
|
895 |
|
// no need to continue if sample is silent |
896 |
|
if (!pDimRgn->pSample || !pDimRgn->pSample->SamplesTotal) return Pool<Voice>::Iterator(); |
897 |
|
|
898 |
// allocate a new voice for the key |
// allocate a new voice for the key |
899 |
Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend(); |
Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend(); |
900 |
if (itNewVoice) { |
if (itNewVoice) { |
901 |
// launch the new voice |
// launch the new voice |
902 |
if (itNewVoice->Trigger(pEngineChannel, itNoteOnEvent, pEngineChannel->Pitch, pEngineChannel->pInstrument, iLayer, ReleaseTriggerVoice, VoiceStealing) < 0) { |
if (itNewVoice->Trigger(pEngineChannel, itNoteOnEvent, pEngineChannel->Pitch, pDimRgn, VoiceType, iKeyGroup) < 0) { |
903 |
dmsg(4,("Voice not triggered\n")); |
dmsg(4,("Voice not triggered\n")); |
904 |
pKey->pActiveVoices->free(itNewVoice); |
pKey->pActiveVoices->free(itNewVoice); |
905 |
} |
} |
906 |
else { // on success |
else { // on success |
907 |
--VoiceSpawnsLeft; |
--VoiceSpawnsLeft; |
|
uint** ppKeyGroup = NULL; |
|
|
if (itNewVoice->KeyGroup) { // if this voice / key belongs to a key group |
|
|
ppKeyGroup = &pEngineChannel->ActiveKeyGroups[itNewVoice->KeyGroup]; |
|
|
if (*ppKeyGroup) { // if there's already an active key in that key group |
|
|
midi_key_info_t* pOtherKey = &pEngineChannel->pMIDIKeyInfo[**ppKeyGroup]; |
|
|
// kill all voices on the (other) key |
|
|
RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first(); |
|
|
RTList<Voice>::Iterator end = pOtherKey->pActiveVoices->end(); |
|
|
for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) { |
|
|
if (itVoiceToBeKilled->Type != Voice::type_release_trigger) { |
|
|
itVoiceToBeKilled->Kill(itNoteOnEvent); |
|
|
--VoiceSpawnsLeft; //FIXME: just a hack, we should better check in StealVoice() if the voice was killed due to key conflict |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
908 |
if (!pKey->Active) { // mark as active key |
if (!pKey->Active) { // mark as active key |
909 |
pKey->Active = true; |
pKey->Active = true; |
910 |
pKey->itSelf = pEngineChannel->pActiveKeys->allocAppend(); |
pKey->itSelf = pEngineChannel->pActiveKeys->allocAppend(); |
911 |
*pKey->itSelf = itNoteOnEvent->Param.Note.Key; |
*pKey->itSelf = itNoteOnEvent->Param.Note.Key; |
912 |
} |
} |
913 |
if (itNewVoice->KeyGroup) { |
if (itNewVoice->KeyGroup) { |
914 |
|
uint** ppKeyGroup = &pEngineChannel->ActiveKeyGroups[itNewVoice->KeyGroup]; |
915 |
*ppKeyGroup = &*pKey->itSelf; // put key as the (new) active key to its key group |
*ppKeyGroup = &*pKey->itSelf; // put key as the (new) active key to its key group |
916 |
} |
} |
917 |
if (itNewVoice->Type == Voice::type_release_trigger_required) pKey->ReleaseTrigger = true; // mark key for the need of release triggered voice(s) |
if (itNewVoice->Type == Voice::type_release_trigger_required) pKey->ReleaseTrigger = true; // mark key for the need of release triggered voice(s) |
1062 |
this->iuiLastStolenKeyGlobally = iuiSelectedKey; |
this->iuiLastStolenKeyGlobally = iuiSelectedKey; |
1063 |
this->itLastStolenVoiceGlobally = itSelectedVoice; |
this->itLastStolenVoiceGlobally = itSelectedVoice; |
1064 |
this->pLastStolenChannel = pSelectedChannel; |
this->pLastStolenChannel = pSelectedChannel; |
1065 |
break; // selection succeeded |
goto stealable_voice_found; // selection succeeded |
1066 |
} |
} |
1067 |
++iuiSelectedKey; // get next key on current engine channel |
++iuiSelectedKey; // get next key on current engine channel |
1068 |
} |
} |
1081 |
} |
} |
1082 |
} |
} |
1083 |
|
|
1084 |
|
// jump point if a 'stealable' voice was found |
1085 |
|
stealable_voice_found: |
1086 |
|
|
1087 |
#if CONFIG_DEVMODE |
#if CONFIG_DEVMODE |
1088 |
if (!itSelectedVoice->IsActive()) { |
if (!itSelectedVoice->IsActive()) { |
1089 |
dmsg(1,("gig::Engine: ERROR, tried to steal a voice which was not active !!!\n")); |
dmsg(1,("gig::Engine: ERROR, tried to steal a voice which was not active !!!\n")); |
1185 |
dmsg(4,("PEDAL DOWN\n")); |
dmsg(4,("PEDAL DOWN\n")); |
1186 |
pEngineChannel->SustainPedal = true; |
pEngineChannel->SustainPedal = true; |
1187 |
|
|
1188 |
|
if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted |
1189 |
|
|
1190 |
// cancel release process of voices if necessary |
// cancel release process of voices if necessary |
1191 |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
1192 |
for (; iuiKey; ++iuiKey) { |
for (; iuiKey; ++iuiKey) { |
1205 |
dmsg(4,("PEDAL UP\n")); |
dmsg(4,("PEDAL UP\n")); |
1206 |
pEngineChannel->SustainPedal = false; |
pEngineChannel->SustainPedal = false; |
1207 |
|
|
1208 |
|
if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted |
1209 |
|
|
1210 |
// release voices if their respective key is not pressed |
// release voices if their respective key is not pressed |
1211 |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
1212 |
for (; iuiKey; ++iuiKey) { |
for (; iuiKey; ++iuiKey) { |
1431 |
} |
} |
1432 |
|
|
1433 |
String Engine::Version() { |
String Engine::Version() { |
1434 |
String s = "$Revision: 1.44 $"; |
String s = "$Revision: 1.50 $"; |
1435 |
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
1436 |
} |
} |
1437 |
|
|