Skip to content

File decimator.h

File List > DaisySP > Source > Effects > decimator.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 DECIMATOR_H
#define DECIMATOR_H
#include <stdint.h>
#ifdef __cplusplus

namespace daisysp
{
class Decimator
{
  public:
    Decimator() {}
    ~Decimator() {}
    void Init();

    float Process(float input);


    inline void SetDownsampleFactor(float downsample_factor)
    {
        downsample_factor_ = downsample_factor;
    }

    inline void SetBitcrushFactor(float bitcrush_factor)
    {
        bitcrush_factor_ = bitcrush_factor;
        bits_to_crush_   = (uint32_t)(bitcrush_factor * kMaxBitsToCrush);
        bit_overflow_
            = 2.0f - (bitcrush_factor * 16.0f) + (float)(bits_to_crush_);
    }

    inline void SetBitsToCrush(const uint8_t &bits)
    {
        bits_to_crush_   = bits <= kMaxBitsToCrush ? bits : kMaxBitsToCrush;
        smooth_crushing_ = false;
    }

    inline void SetSmoothCrushing(bool smooth_crushing)
    {
        smooth_crushing_ = smooth_crushing;
    }

    inline bool GetSmoothCrushing() { return smooth_crushing_; }
    inline float GetDownsampleFactor() { return downsample_factor_; }
    inline float GetBitcrushFactor() { return bitcrush_factor_; }
    inline int GetBitsToCrush() { return bits_to_crush_; }

  private:
    const uint8_t kMaxBitsToCrush = 16;
    float         downsample_factor_, bitcrush_factor_;
    uint32_t      bits_to_crush_;
    float         downsampled_, bitcrushed_;
    uint32_t      inc_, threshold_;
    bool          smooth_crushing_;
    float         bit_overflow_;
};
} // namespace daisysp
#endif
#endif