<commit_before>﻿using UnityEngine;

namespace DerelictComputer
{
    public class VolumeEnvelopeFilter : MonoBehaviour
    {
        public bool Enabled;
        public float AttackTime = 0f;
        public float SustainTime = 10f;
        public float ReleaseTime = 0f;

        private bool _triggered;
        private double _startTime;
        private int _frequency;
        private int _attackSamples;
        private int _sustainSamples;
        private int _releaseSamples;

        public void Trigger(double triggerTime)
        {
            _triggered = true;
            _startTime = AudioSettings.dspTime;
            _frequency = AudioSettings.outputSampleRate;
            _attackSamples = (int) (AttackTime* _frequency);
            _sustainSamples = (int) (SustainTime* _frequency);
            _releaseSamples = (int) (ReleaseTime* _frequency);
        }

        private void OnAudioFilterRead(float[] data, int channels)
        {
            // if not enabled, don't do any attenuation
            if (!Enabled)
            {
                return;
            }

            // if enabled, but outside the range of the envelope, attenuate fully
            if (!_triggered || AudioSettings.dspTime < _startTime)
            {
                for (int i = 0; i < data.Length; i++)
                {
                    data[i] = 0f;
                }

                return;
            }

            var samplesSinceTrigger = (int) (_frequency*(AudioSettings.dspTime - _startTime));

            for (int i = 0; i < data.Length; i++)
            {
                var samplesElapsed = samplesSinceTrigger + i/channels;

                var volume = 0f;

                if (samplesElapsed < _attackSamples)
                {
                    volume = Mathf.Lerp(0f, 1f, samplesElapsed / (float)_attackSamples);
                }
                else if (samplesElapsed < _sustainSamples + _attackSamples)
                {
                    volume = 1f;
                }
                else if (samplesElapsed < _releaseSamples + _sustainSamples + _attackSamples)
                {
                    volume = Mathf.Lerp(1f, 0f, (samplesElapsed - _attackSamples - _sustainSamples) / (float)_releaseSamples);
                }

                data[i] *= Mathf.Clamp(volume, 0f, 1f);
            }
        }
    }
}<commit_msg>fix bug in envelope that would cause it to never reset<commit_after>﻿using UnityEngine;

namespace DerelictComputer
{
    public class VolumeEnvelopeFilter : MonoBehaviour
    {
        public bool Enabled;
        public float AttackTime = 0f;
        public float SustainTime = 10f;
        public float ReleaseTime = 0f;

        private bool _triggered;
        private double _startTime;
        private int _frequency;
        private int _attackSamples;
        private int _sustainSamples;
        private int _releaseSamples;

        public void Trigger(double triggerTime)
        {
            _triggered = true;
            _startTime = AudioSettings.dspTime;
            _frequency = AudioSettings.outputSampleRate;
            _attackSamples = (int) (AttackTime* _frequency);
            _sustainSamples = (int) (SustainTime* _frequency);
            _releaseSamples = (int) (ReleaseTime* _frequency);
        }

        private void OnAudioFilterRead(float[] data, int channels)
        {
            // if not enabled, don't do any attenuation
            if (!Enabled)
            {
                return;
            }

            // if enabled, but outside the range of the envelope, attenuate fully
            if (!_triggered || AudioSettings.dspTime < _startTime)
            {
                for (int i = 0; i < data.Length; i++)
                {
                    data[i] = 0f;
                }

                return;
            }

            var samplesSinceTrigger = (int) (_frequency*(AudioSettings.dspTime - _startTime));

            for (int i = 0; i < data.Length; i++)
            {
                var samplesElapsed = samplesSinceTrigger + i/channels;

                var volume = 0f;

                if (samplesElapsed < _attackSamples)
                {
                    volume = Mathf.Lerp(0f, 1f, samplesElapsed / (float)_attackSamples);
                }
                else if (samplesElapsed < _sustainSamples + _attackSamples)
                {
                    volume = 1f;
                }
                else if (samplesElapsed < _releaseSamples + _sustainSamples + _attackSamples)
                {
                    volume = Mathf.Lerp(1f, 0f, (samplesElapsed - _attackSamples - _sustainSamples) / (float)_releaseSamples);
                }
                else
                {
                    volume = 0f;
                    _triggered = false;
                }

                data[i] *= Mathf.Clamp(volume, 0f, 1f);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.07646698016765488, 'fraction_numerical': 0.00633817215293396, 'mean_word_length': 1.4314115308151094, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3312921', 'n_tokens_mistral': 1369, 'n_tokens_neox': 1211, 'n_words': 325}