Пример #1
0
        public void QuickSetupSf2(int sampleRate, int note, short keyNumToHold, short keyNumToDecay, bool isVolumeEnvelope, EnvelopeDescriptor envelopeInfo)
        {
            Depth = envelopeInfo.Depth;
            // Delay
            _stages[0].Offset = 0;
            _stages[0].Scale = 0;
            _stages[0].Time = Math.Max(0, (int)(sampleRate * (envelopeInfo.DelayTime)));
            // Attack
            _stages[1].Offset = envelopeInfo.StartLevel;
            _stages[1].Scale = envelopeInfo.PeakLevel - envelopeInfo.StartLevel;
            _stages[1].Time = Math.Max(0, (int)(sampleRate * (envelopeInfo.AttackTime)));
            _stages[1].Graph = Tables.EnvelopeTables(envelopeInfo.AttackGraph);
            // Hold
            _stages[2].Offset = 0;
            _stages[2].Scale = envelopeInfo.PeakLevel;
            _stages[2].Time = (int) Math.Max(0, sampleRate * (envelopeInfo.HoldTime) * Math.Pow(2, ((60 - note) * keyNumToHold) / 1200.0));
            // Decay
            _stages[3].Offset = envelopeInfo.SustainLevel;
            _stages[3].Scale = envelopeInfo.PeakLevel - envelopeInfo.SustainLevel;
            if (envelopeInfo.SustainLevel == envelopeInfo.PeakLevel)
                _stages[3].Time = 0;
            else
                _stages[3].Time = Math.Max(0, (int)(sampleRate * (envelopeInfo.DecayTime) * Math.Pow(2, ((60 - note) * keyNumToDecay) / 1200.0)));
            _stages[3].Graph = Tables.EnvelopeTables(envelopeInfo.DecayGraph);
            // Sustain
            _stages[4].Offset = 0;
            _stages[4].Scale = envelopeInfo.SustainLevel;
            _stages[4].Time = (int) (sampleRate * envelopeInfo.SustainTime);
            // Release
            _stages[5].Scale = _stages[3].Time == 0 && _stages[4].Time == 0 ? envelopeInfo.PeakLevel : _stages[4].Scale;
            if (isVolumeEnvelope)
            {
                _stages[5].Offset = -100;
                _stages[5].Scale += 100;
                _stages[6].Scale = -100;
            }
            else
            {
                _stages[5].Offset = 0;
                _stages[6].Scale = 0;
            }
            _stages[5].Time = Math.Max(0, (int)(sampleRate * (envelopeInfo.ReleaseTime)));
            _stages[5].Graph = Tables.EnvelopeTables(envelopeInfo.ReleaseGraph); 

            _index = 0;
            Value = 0;
            CurrentStage = EnvelopeState.Delay;
            while (_stages[(int)CurrentStage].Time == 0)
            {
                CurrentStage++;
            }
            _stage = _stages[(int)CurrentStage];
        }
Пример #2
0
        private void LoadEnvelopes(Sf2Region region)
        {
            //
            //mod env
            mod_env = new EnvelopeDescriptor();
            mod_env.AttackTime = (float)Math.Pow(2, region.Generators[(int)GeneratorEnum.AttackModulationEnvelope] / 1200.0);
            mod_env.AttackGraph = 3;
            mod_env.DecayTime = (float)Math.Pow(2, region.Generators[(int)GeneratorEnum.DecayModulationEnvelope] / 1200.0);
            mod_env.DelayTime = (float)Math.Pow(2, region.Generators[(int)GeneratorEnum.DelayModulationEnvelope] / 1200.0);
            mod_env.HoldTime = (float)Math.Pow(2, region.Generators[(int)GeneratorEnum.HoldModulationEnvelope] / 1200.0);
            mod_env.PeakLevel = 1;
            mod_env.ReleaseTime = (float)Math.Pow(2, region.Generators[(int)GeneratorEnum.ReleaseModulationEnvelope] / 1200.0);
            mod_env.StartLevel = 0;
            mod_env.SustainLevel = 1f - SynthHelper.ClampS(region.Generators[(int)GeneratorEnum.SustainModulationEnvelope], (short)0, (short)1000) / 1000f;
            //checks
            if (mod_env.AttackTime < 0.001f)
                mod_env.AttackTime = 0.001f;
            else if (mod_env.AttackTime > 100f)
                mod_env.AttackTime = 100f;
            if (mod_env.DecayTime < 0.001f)
                mod_env.DecayTime = 0;
            else if (mod_env.DecayTime > 100f)
                mod_env.DecayTime = 100f;
            if (mod_env.DelayTime < 0.001f)
                mod_env.DelayTime = 0;
            else if (mod_env.DelayTime > 20f)
                mod_env.DelayTime = 20f;
            if (mod_env.HoldTime < 0.001f)
                mod_env.HoldTime = 0;
            else if (mod_env.HoldTime > 20f)
                mod_env.HoldTime = 20f;
            if (mod_env.ReleaseTime < 0.001f)
                mod_env.ReleaseTime = 0.001f;
            else if (mod_env.ReleaseTime > 100f)
                mod_env.ReleaseTime = 100f;

            //
            // volume env
            vel_env = new EnvelopeDescriptor();
            vel_env.AttackTime = (float)Math.Pow(2, region.Generators[(int)GeneratorEnum.AttackVolumeEnvelope] / 1200.0);
            vel_env.AttackGraph = 3;
            vel_env.DecayTime = (float)Math.Pow(2, region.Generators[(int)GeneratorEnum.DecayVolumeEnvelope] / 1200.0);
            vel_env.DelayTime = (float)Math.Pow(2, region.Generators[(int)GeneratorEnum.DelayVolumeEnvelope] / 1200.0);
            vel_env.HoldTime = (float)Math.Pow(2, region.Generators[(int)GeneratorEnum.HoldVolumeEnvelope] / 1200.0);
            vel_env.PeakLevel = 0;
            vel_env.ReleaseTime = (float)Math.Pow(2, region.Generators[(int)GeneratorEnum.ReleaseVolumeEnvelope] / 1200.0);
            vel_env.StartLevel = -100;
            vel_env.SustainLevel = SynthHelper.ClampS(region.Generators[(int)GeneratorEnum.SustainVolumeEnvelope], (short)0, (short)1000) / -10f;
            // checks
            if (vel_env.AttackTime < 0.001f)
                vel_env.AttackTime = 0.001f;
            else if (vel_env.AttackTime > 100f)
                vel_env.AttackTime = 100f;
            if (vel_env.DecayTime < 0.001f)
                vel_env.DecayTime = 0;
            else if (vel_env.DecayTime > 100f)
                vel_env.DecayTime = 100f;
            if (vel_env.DelayTime < 0.001f)
                vel_env.DelayTime = 0;
            else if (vel_env.DelayTime > 20f)
                vel_env.DelayTime = 20f;
            if (vel_env.HoldTime < 0.001f)
                vel_env.HoldTime = 0;
            else if (vel_env.HoldTime > 20f)
                vel_env.HoldTime = 20f;
            if (vel_env.ReleaseTime < 0.001f)
                vel_env.ReleaseTime = 0.001f;
            else if (vel_env.ReleaseTime > 100f)
                vel_env.ReleaseTime = 100f;
        }