Example #1
0
        /// <summary>
        /// Copies parameters from another instance
        /// </summary>
        /// <param name="__params">Instance to copy parameters from</param>
        /// <param name="__makeDirty"></param>
        public void CopyFrom(SfxrParams __params, bool __makeDirty = false)
        {
            bool wasDirty = paramsDirty;

            SetSettingsString(GetSettingsString());
            paramsDirty = wasDirty || __makeDirty;
        }
Example #2
0
        // Copying methods

        /// <summary>
        /// Returns a copy of this SfxrParams with all settings duplicated
        /// </summary>
        /// <returns>A copy of this SfxrParams</returns>
        public SfxrParams Clone()
        {
            SfxrParams outp = new SfxrParams();

            outp.CopyFrom(this);

            return(outp);
        }
Example #3
0
        /// <summary>
        /// Stops the currently playing sound
        /// </summary>
        public void Stop()
        {
            if (_audioPlayer != null)
            {
                _audioPlayer.Stop();
                _audioPlayer.Dispose();
                _audioPlayer = null;
            }

            if (_original != null)
            {
                _params.CopyFrom(_original);
                _original = null;
            }
        }
Example #4
0
        /**
         * Resets the runing variables from the params
         * Used once at the start (total reset) and for the repeat effect (partial reset)
         * @param	totalReset	If the reset is total
         */
        private void Reset(bool __totalReset)
        {
            // Shorter reference
            SfxrParams p = _params;

            _period    = 100.0f / (p.startFrequency * p.startFrequency + 0.001f);
            _maxPeriod = 100.0f / (p.minFrequency * p.minFrequency + 0.001f);

            _slide      = 1.0f - p.slide * p.slide * p.slide * 0.01f;
            _deltaSlide = -p.deltaSlide * p.deltaSlide * p.deltaSlide * 0.000001f;

            if (p.waveType == 0)
            {
                _squareDuty = 0.5f - p.squareDuty * 0.5f;
                _dutySweep  = -p.dutySweep * 0.00005f;
            }

            _changePeriod     = (((1f - p.changeRepeat) + 0.1f) / 1.1f) * 20000f + 32f;
            _changePeriodTime = 0;

            if (p.changeAmount > 0.0)
            {
                _changeAmount = 1.0f - p.changeAmount * p.changeAmount * 0.9f;
            }
            else
            {
                _changeAmount = 1.0f + p.changeAmount * p.changeAmount * 10.0f;
            }

            _changeTime    = 0;
            _changeReached = false;

            if (p.changeSpeed == 1.0f)
            {
                _changeLimit = 0;
            }
            else
            {
                _changeLimit = (int)((1f - p.changeSpeed) * (1f - p.changeSpeed) * 20000f + 32f);
            }

            if (p.changeAmount2 > 0f)
            {
                _changeAmount2 = 1f - p.changeAmount2 * p.changeAmount2 * 0.9f;
            }
            else
            {
                _changeAmount2 = 1f + p.changeAmount2 * p.changeAmount2 * 10f;
            }

            _changeTime2    = 0;
            _changeReached2 = false;

            if (p.changeSpeed2 == 1.0f)
            {
                _changeLimit2 = 0;
            }
            else
            {
                _changeLimit2 = (int)((1f - p.changeSpeed2) * (1f - p.changeSpeed2) * 20000f + 32f);
            }

            _changeLimit  = (int)(_changeLimit * ((1f - p.changeRepeat + 0.1f) / 1.1f));
            _changeLimit2 = (int)(_changeLimit2 * ((1f - p.changeRepeat + 0.1f) / 1.1f));

            if (__totalReset)
            {
                p.paramsDirty = false;

                _masterVolume = p.masterVolume * p.masterVolume;

                _waveType = p.waveType;

                if (p.sustainTime < 0.01)
                {
                    p.sustainTime = 0.01f;
                }

                float totalTime = p.attackTime + p.sustainTime + p.decayTime;
                if (totalTime < 0.18f)
                {
                    float multiplier = 0.18f / totalTime;
                    p.attackTime  *= multiplier;
                    p.sustainTime *= multiplier;
                    p.decayTime   *= multiplier;
                }

                _sustainPunch = p.sustainPunch;

                _phase = 0;

                _overtones       = (int)(p.overtones * 10f);
                _overtoneFalloff = p.overtoneFalloff;

                _minFrequency = p.minFrequency;

                _bitcrushFreq      = 1f - Mathf.Pow(p.bitCrush, 1f / 3f);
                _bitcrushFreqSweep = -p.bitCrushSweep * 0.000015f;
                _bitcrushPhase     = 0;
                _bitcrushLast      = 0;

                _compressionFactor = 1f / (1f + 4f * p.compressionAmount);

                _filters = p.lpFilterCutoff != 1.0 || p.hpFilterCutoff != 0.0;

                _lpFilterPos         = 0.0f;
                _lpFilterDeltaPos    = 0.0f;
                _lpFilterCutoff      = p.lpFilterCutoff * p.lpFilterCutoff * p.lpFilterCutoff * 0.1f;
                _lpFilterDeltaCutoff = 1.0f + p.lpFilterCutoffSweep * 0.0001f;
                _lpFilterDamping     = 5.0f / (1.0f + p.lpFilterResonance * p.lpFilterResonance * 20.0f) * (0.01f + _lpFilterCutoff);
                if (_lpFilterDamping > 0.8f)
                {
                    _lpFilterDamping = 0.8f;
                }
                _lpFilterDamping = 1.0f - _lpFilterDamping;
                _lpFilterOn      = p.lpFilterCutoff != 1.0f;

                _hpFilterPos         = 0.0f;
                _hpFilterCutoff      = p.hpFilterCutoff * p.hpFilterCutoff * 0.1f;
                _hpFilterDeltaCutoff = 1.0f + p.hpFilterCutoffSweep * 0.0003f;

                _vibratoPhase     = 0.0f;
                _vibratoSpeed     = p.vibratoSpeed * p.vibratoSpeed * 0.01f;
                _vibratoAmplitude = p.vibratoDepth * 0.5f;

                _envelopeVolume     = 0.0f;
                _envelopeStage      = 0;
                _envelopeTime       = 0;
                _envelopeLength0    = p.attackTime * p.attackTime * 100000.0f;
                _envelopeLength1    = p.sustainTime * p.sustainTime * 100000.0f;
                _envelopeLength2    = p.decayTime * p.decayTime * 100000.0f + 10f;
                _envelopeLength     = _envelopeLength0;
                _envelopeFullLength = (uint)(_envelopeLength0 + _envelopeLength1 + _envelopeLength2);

                _envelopeOverLength0 = 1.0f / _envelopeLength0;
                _envelopeOverLength1 = 1.0f / _envelopeLength1;
                _envelopeOverLength2 = 1.0f / _envelopeLength2;

                _phaser = p.phaserOffset != 0.0f || p.phaserSweep != 0.0f;

                _phaserOffset = p.phaserOffset * p.phaserOffset * 1020.0f;
                if (p.phaserOffset < 0.0f)
                {
                    _phaserOffset = -_phaserOffset;
                }
                _phaserDeltaOffset = p.phaserSweep * p.phaserSweep * p.phaserSweep * 0.2f;
                _phaserPos         = 0;

                if (_phaserBuffer == null)
                {
                    _phaserBuffer = new float[1024];
                }
                if (_noiseBuffer == null)
                {
                    _noiseBuffer = new float[32];
                }
                if (_pinkNoiseBuffer == null)
                {
                    _pinkNoiseBuffer = new float[32];
                }
                if (_pinkNumber == null)
                {
                    _pinkNumber = new PinkNumber();
                }
                if (_loResNoiseBuffer == null)
                {
                    _loResNoiseBuffer = new float[32];
                }

                uint i;
                for (i = 0; i < 1024; i++)
                {
                    _phaserBuffer[i] = 0.0f;
                }
                for (i = 0; i < 32; i++)
                {
                    _noiseBuffer[i] = getRandom() * 2.0f - 1.0f;
                }
                for (i = 0; i < 32; i++)
                {
                    _pinkNoiseBuffer[i] = _pinkNumber.getNextValue();
                }
                for (i = 0; i < 32; i++)
                {
                    _loResNoiseBuffer[i] = ((i % LO_RES_NOISE_PERIOD) == 0) ? getRandom() * 2.0f - 1.0f : _loResNoiseBuffer[i - 1];
                }

                _repeatTime = 0;

                if (p.repeatSpeed == 0.0)
                {
                    _repeatLimit = 0;
                }
                else
                {
                    _repeatLimit = (int)((1.0 - p.repeatSpeed) * (1.0 - p.repeatSpeed) * 20000) + 32;
                }
            }
        }