<commit_before>//
//  EnvelopeGeneratorBase.cpp
//  AudioKit Core
//
//  Created by <NAME>, revision history on Github.
//  Copyright © 2018 AudioKit. All rights reserved.
//
#include "EnvelopeGeneratorBase.hpp"
#include <cmath>

namespace AudioKitCore
{

    void ExponentialSegmentGenerator::reset(double initialValue, double targetValue, double tco, int segmentLengthSamples)
    {
        output = segmentLengthSamples > 0 ? initialValue : targetValue;
        target = targetValue;
        isHorizontal = targetValue == initialValue;
        isLinear = tco <= 0.0;
        isRising = targetValue > initialValue;

        printf("initVal %f targetVal %f output %f target %f  len %i\n\n", initialValue, targetValue, output, target, segmentLengthSamples);
        if (isHorizontal)
        {
            tcount = 0;
            segLength = segmentLengthSamples;
        }
        else if (isLinear)
        {
            if (segmentLengthSamples <= 0)
                coefficient = target - output;
            else
                coefficient = (targetValue - initialValue) / segmentLengthSamples;
        }
        else
        {
            if (segmentLengthSamples == 0)
            {
                coefficient = 0.0;
                offset = target;
            }
            else
            {
                // Correction to Pirkle (who uses delta = 1.0 always)
                // According to Redmon (who only discusses the delta = 1.0 case), delta should be defined thus
                double delta = abs(targetValue - initialValue);
                coefficient = exp(-log((delta + tco) / tco) / segmentLengthSamples);
                if (isRising)
                    offset = (target + tco) * (1.0 - coefficient);
                else
                    offset = (target - tco) * (1.0 - coefficient);
            }
        }
    }

    void MultiSegmentEnvelopeGenerator::setupCurSeg()
    {
        SegmentDescriptor& seg = (*segments)[curSegIndex];
        double initValue = output;
        double targetValue = seg.finalValue;
        bool isHorizontal = seg.initialValue == seg.finalValue;
        if (seg.lengthSamples == 0) {
            initValue = output;
            targetValue = output;
        }
        if (!isHorizontal) {
            targetValue = seg.finalValue;
        }
//        if (seg.lengthSamples > -1) {
            printf("auto advance seg: %i : len: %i, init: %f final %f\n", curSegIndex, seg.lengthSamples, seg.initialValue, seg.finalValue);
//        }
        ExponentialSegmentGenerator::reset(initValue, targetValue, seg.tco, seg.lengthSamples);
    }

    void MultiSegmentEnvelopeGenerator::setupCurSeg(double initValue)
    {
        SegmentDescriptor& seg = (*segments)[curSegIndex];
        double initVal = initValue;
        double targetValue = seg.finalValue;
        bool isHorizontal = seg.initialValue == seg.finalValue;
        if (seg.lengthSamples == 0) {
            initVal = output;
            targetValue = output;
        }
        if (!isHorizontal) {
            targetValue = seg.finalValue;
        }
        if (seg.lengthSamples > -1) {
            printf("manual advance seg: %i: len: %i, init: %f final %f target %f\n", curSegIndex, seg.lengthSamples, seg.initialValue, seg.finalValue, targetValue);
//            printf("manual advance: current seg is %i, target set %f\n", curSegIndex, targetValue);
        }
        ExponentialSegmentGenerator::reset(initVal, targetValue, seg.tco, seg.lengthSamples);
    }

    void MultiSegmentEnvelopeGenerator::reset(Descriptor* pDesc, int initialSegmentIndex)
    {
        segments = pDesc;
        curSegIndex = initialSegmentIndex;
        setupCurSeg();
    }

    void MultiSegmentEnvelopeGenerator::advanceToSegment(int segIndex)
    {
        curSegIndex = segIndex;
        setupCurSeg(output);
    }

}<commit_msg>reduced code - always calculate envelope from current output level<commit_after>//
//  EnvelopeGeneratorBase.cpp
//  AudioKit Core
//
//  Created by <NAME>, revision history on Github.
//  Copyright © 2018 AudioKit. All rights reserved.
//
#include "EnvelopeGeneratorBase.hpp"
#include <cmath>

namespace AudioKitCore
{

    void ExponentialSegmentGenerator::reset(double initialValue, double targetValue, double tco, int segmentLengthSamples)
    {
        output = segmentLengthSamples > 0 ? initialValue : targetValue;
        target = targetValue;
        isHorizontal = targetValue == initialValue;
        isLinear = tco <= 0.0;
        isRising = targetValue > initialValue;

        printf("initVal %f targetVal %f output %f target %f  len %i\n\n", initialValue, targetValue, output, target, segmentLengthSamples);
        if (isHorizontal)
        {
            tcount = 0;
            segLength = segmentLengthSamples;
        }
        else if (isLinear)
        {
            if (segmentLengthSamples <= 0)
                coefficient = target - output;
            else
                coefficient = (targetValue - initialValue) / segmentLengthSamples;
        }
        else
        {
            if (segmentLengthSamples == 0)
            {
                coefficient = 0.0;
                offset = target;
            }
            else
            {
                // Correction to Pirkle (who uses delta = 1.0 always)
                // According to Redmon (who only discusses the delta = 1.0 case), delta should be defined thus
                double delta = abs(targetValue - initialValue);
                coefficient = exp(-log((delta + tco) / tco) / segmentLengthSamples);
                if (isRising)
                    offset = (target + tco) * (1.0 - coefficient);
                else
                    offset = (target - tco) * (1.0 - coefficient);
            }
        }
    }

    void MultiSegmentEnvelopeGenerator::setupCurSeg()
    {
        SegmentDescriptor& seg = (*segments)[curSegIndex];
        double targetValue = seg.finalValue;
        bool isHorizontal = seg.initialValue == seg.finalValue;
        if (seg.lengthSamples == 0) {
            targetValue = output;
        }
        if (!isHorizontal) {
            targetValue = seg.finalValue;
        }
        printf("auto advance seg: %i : len: %i, init: %f final %f\n", curSegIndex, seg.lengthSamples, seg.initialValue, seg.finalValue);
        ExponentialSegmentGenerator::reset(output, targetValue, seg.tco, seg.lengthSamples);
    }

//    void MultiSegmentEnvelopeGenerator::setupCurSeg(double initValue)
//    {
//        SegmentDescriptor& seg = (*segments)[curSegIndex];
//        double targetValue = seg.finalValue;
//        bool isHorizontal = seg.initialValue == seg.finalValue;
//        if (seg.lengthSamples == 0) {
//            targetValue = output;
//        }
//        if (!isHorizontal) {
//            targetValue = seg.finalValue;
//        }
//        printf("manual advance seg: %i: len: %i, init: %f final %f target %f\n", curSegIndex, seg.lengthSamples, seg.initialValue, seg.finalValue, targetValue);
//        ExponentialSegmentGenerator::reset(initValue, targetValue, seg.tco, seg.lengthSamples);
//    }

    void MultiSegmentEnvelopeGenerator::reset(Descriptor* pDesc, int initialSegmentIndex)
    {
        segments = pDesc;
        curSegIndex = initialSegmentIndex;
        setupCurSeg();
    }

    void MultiSegmentEnvelopeGenerator::advanceToSegment(int segIndex)
    {
        curSegIndex = segIndex;
        setupCurSeg();
    }

}
{'fraction_non_alphanumeric': 0.10171089855853428, 'fraction_numerical': 0.0061969554088643405, 'mean_word_length': 2.0264981655116183, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '5449168', 'n_tokens_mistral': 2054, 'n_tokens_neox': 1957, 'n_words': 507}