private void LoadFilter(Sf2Region region) { fltr = new FilterDescriptor(); fltr.FilterMethod = FilterTypeEnum.BiquadLowpass; fltr.CutOff = (float)SynthHelper.KeyToFrequency(region.Generators[(int)GeneratorEnum.InitialFilterCutoffFrequency] / 100.0, 69); fltr.Resonance = (float)SynthHelper.DBtoLinear(region.Generators[(int)GeneratorEnum.InitialFilterQ] / 10.0); }
public override void Process(VoiceParameters voiceparams, int startIndex, int endIndex, bool isMuted) { //--Base pitch calculation var basePitchFrequency = SynthHelper.CentsToPitch(voiceparams.SynthParams.CurrentPitch) * gen.Frequency; var pitchWithBend = basePitchFrequency * SynthHelper.CentsToPitch(voiceparams.PitchOffset); var basePitch = pitchWithBend / voiceparams.SynthParams.Synth.SampleRate; float baseVolume = isMuted ? 0 : voiceparams.SynthParams.Synth.MasterVolume * voiceparams.SynthParams.CurrentVolume * SynthConstants.DefaultMixGain * voiceparams.SynthParams.MixVolume; //--Main Loop for (int x = startIndex; x < endIndex; x += SynthConstants.DefaultBlockSize * SynthConstants.AudioChannels) { voiceparams.Envelopes[0].Increment(SynthConstants.DefaultBlockSize); voiceparams.Envelopes[1].Increment(SynthConstants.DefaultBlockSize); voiceparams.Lfos[0].Increment(SynthConstants.DefaultBlockSize); voiceparams.Lfos[1].Increment(SynthConstants.DefaultBlockSize); //--Calculate pitch and get next block of samples gen.GetValues(voiceparams.GeneratorParams[0], voiceparams.BlockBuffer, basePitch * SynthHelper.CentsToPitch((int)(voiceparams.Envelopes[0].Value * modEnvToPitch + voiceparams.Lfos[0].Value * modLfoToPitch + voiceparams.Lfos[1].Value * vibLfoToPitch))); //--Filter if (voiceparams.Filters[0].Enabled) { double centsFc = voiceparams.PData[0].Int1 + voiceparams.Lfos[0].Value * modLfoToFilterFc + voiceparams.Envelopes[0].Value * modEnvToFilterFc; if (centsFc > 13500) { centsFc = 13500; } voiceparams.Filters[0].CutOff = SynthHelper.KeyToFrequency(centsFc / 100.0, 69); if (voiceparams.Filters[0].CoeffNeedsUpdating) { voiceparams.Filters[0].ApplyFilterInterp(voiceparams.BlockBuffer, voiceparams.SynthParams.Synth.SampleRate); } else { voiceparams.Filters[0].ApplyFilter(voiceparams.BlockBuffer); } } //--Volume calculation float volume = (float)SynthHelper.DBtoLinear(voiceparams.VolOffset + voiceparams.Envelopes[1].Value + voiceparams.Lfos[0].Value * modLfoToVolume) * baseVolume; //--Mix block based on number of channels if (SynthConstants.AudioChannels == 2) { voiceparams.MixMonoToStereoInterp(x, volume * pan.Left * voiceparams.SynthParams.CurrentPan.Left, volume * pan.Right * voiceparams.SynthParams.CurrentPan.Right); } else { voiceparams.MixMonoToMonoInterp(x, volume); } //--Check and end early if necessary if ((voiceparams.Envelopes[1].CurrentStage > EnvelopeState.Hold && volume <= SynthConstants.NonAudible) || voiceparams.GeneratorParams[0].CurrentState == GeneratorState.Finished) { voiceparams.State = VoiceStateEnum.Stopped; return; } } }
public override void Process(VoiceParameters voiceparams, int startIndex, int endIndex) { //--Base pitch calculation double basePitch = SynthHelper.CentsToPitch(voiceparams.pitchOffset + voiceparams.synth.totalPitch[voiceparams.channel]) * voiceparams.generators[0].Frequency / voiceparams.synth.SampleRate; //--Base volume calculation //float baseVolume = voiceparams.volOffset * voiceparams.synth.totalVolume[voiceparams.channel]; //--Main Loop for (int x = startIndex; x < endIndex; x += Synthesizer.DefaultBlockSize * voiceparams.synth.AudioChannels) { voiceparams.envelopes[0].Increment(Synthesizer.DefaultBlockSize); voiceparams.envelopes[1].Increment(Synthesizer.DefaultBlockSize); voiceparams.lfos[0].Increment(Synthesizer.DefaultBlockSize); voiceparams.lfos[1].Increment(Synthesizer.DefaultBlockSize); //--Calculate pitch and get next block of samples voiceparams.generators[0].GetValues(voiceparams.generatorParams[0], voiceparams.blockBuffer, basePitch * SynthHelper.CentsToPitch((int)(voiceparams.envelopes[0].Value * modEnvToPitch + voiceparams.lfos[0].Value * modLfoToPitch + voiceparams.lfos[1].Value * vibLfoToPitch))); //--Filter if (voiceparams.filters[0].Enabled) { double centsFc = iniFilterFc + voiceparams.lfos[0].Value * modLfoToFilterFc + voiceparams.envelopes[0].Value * modEnvToFilterFc; if (centsFc > 13500) { centsFc = 13500; } voiceparams.filters[0].UpdateCoefficients(SynthHelper.KeyToFrequency(centsFc / 100.0, 69) / voiceparams.synth.SampleRate, filterQ); voiceparams.filters[0].ApplyFilter(voiceparams.blockBuffer); } //--Volume calculation float volume = (float)SynthHelper.DBtoLinear(voiceparams.volOffset + voiceparams.lfos[0].Value * modLfoToVolume) * voiceparams.envelopes[1].Value * voiceparams.synth.totalVolume[voiceparams.channel] * voiceparams.synth.MixGain; //--Mix block based on number of channels if (voiceparams.synth.AudioChannels == 2) { SynthHelper.MixMonoToStereoInterpolation(x, volume * pan.Left * voiceparams.synth.panPositions[voiceparams.channel].Left, volume * pan.Right * voiceparams.synth.panPositions[voiceparams.channel].Right, voiceparams); } else { SynthHelper.MixMonoToMonoInterpolation(x, volume, voiceparams); } //--Check and end early if necessary if ((voiceparams.envelopes[1].CurrentState > EnvelopeStateEnum.Hold && volume <= Synthesizer.NonAudible) || voiceparams.generatorParams[0].currentState == GeneratorStateEnum.Finished) { voiceparams.state = VoiceStateEnum.Stopped; return; } } }