Skip to content

File adenv.h

File List > Control > adenv.h

Go to the documentation of this file

Source Code

/*
Copyright (c) 2020 Electrosmith, Corp

Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

#pragma once
#ifndef ADENV_H
#define ADENV_H
#include <stdint.h>
#ifdef __cplusplus

namespace daisysp
{
enum AdEnvSegment
{
    ADENV_SEG_IDLE,
    ADENV_SEG_ATTACK,
    ADENV_SEG_DECAY,
    ADENV_SEG_LAST,
};

class AdEnv
{
  public:
    AdEnv() {}
    ~AdEnv() {}
    void Init(float sample_rate);

    float Process();

    inline void Trigger() { trigger_ = 1; }
    inline void SetTime(uint8_t seg, float time) { segment_time_[seg] = time; }
    inline void SetCurve(float scalar) { curve_scalar_ = scalar; }
    inline void SetMin(float min) { min_ = min; }
    inline void SetMax(float max) { max_ = max; }
    inline float GetValue() const { return (output_ * (max_ - min_)) + min_; }
    inline uint8_t GetCurrentSegment() { return current_segment_; }
    inline bool IsRunning() const { return current_segment_ != ADENV_SEG_IDLE; }

  private:
    uint8_t  current_segment_, prev_segment_;
    float    segment_time_[ADENV_SEG_LAST];
    float    sample_rate_, min_, max_, output_, curve_scalar_;
    float    c_inc_, curve_x_, retrig_val_;
    uint32_t phase_;
    uint8_t  trigger_;
};

} // namespace daisysp
#endif
#endif