DaisySP
Loading...
Searching...
No Matches
smooth_random.h
Go to the documentation of this file.
1/*
2Copyright (c) 2020 Electrosmith, Corp, Emilie Gillet
3
4Use of this source code is governed by an MIT-style
5license that can be found in the LICENSE file or at
6https://opensource.org/licenses/MIT.
7*/
8
9#pragma once
10#ifndef DSY_SMOOTHRANDOM_H
11#define DSY_SMOOTHRANDOM_H
12
13#include "dsp.h"
14#include <stdint.h>
15#include <stdlib.h>
16#ifdef __cplusplus
17
20namespace daisysp
21{
31{
32 public:
35
39 void Init(float sample_rate)
40 {
41 sample_rate_ = sample_rate;
42
43 SetFreq(1.f);
44 phase_ = 0.0f;
45 from_ = 0.0f;
46 interval_ = 0.0f;
47 }
48
50 float Process()
51 {
52 phase_ += frequency_;
53 if(phase_ >= 1.0f)
54 {
55 phase_ -= 1.0f;
56 from_ += interval_;
57 interval_ = rand() * kRandFrac * 2.0f - 1.0f - from_;
58 }
59 float t = phase_ * phase_ * (3.0f - 2.0f * phase_);
60 return from_ + interval_ * t;
61 }
62
66 void SetFreq(float freq)
67 {
68 freq = freq / sample_rate_;
69 frequency_ = fclamp(freq, 0.f, 1.f);
70 }
71
72 private:
73 float frequency_;
74 float phase_;
75 float from_;
76 float interval_;
77
78 float sample_rate_;
79
80 static constexpr float kRandFrac = 1.f / (float)RAND_MAX;
81};
82
83} // namespace daisysp
84#endif
85#endif
Smooth random generator for internal modulation. .
Definition smooth_random.h:31
float Process()
Definition smooth_random.h:50
void SetFreq(float freq)
Definition smooth_random.h:66
void Init(float sample_rate)
Definition smooth_random.h:39
FIR Filter implementation, generic and ARM CMSIS DSP based.
Definition adenv.h:16
float fclamp(float in, float min, float max)
Definition dsp.h:64