1 /*
2  * ZamGrains Granular Delay
3  * Copyright (C) 2018  Damien Zammit <damien@zamaudio.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  */
15 
16 #ifndef ZAMGRAINSPLUGIN_HPP_INCLUDED
17 #define ZAMGRAINSPLUGIN_HPP_INCLUDED
18 
19 #include "DistrhoPlugin.hpp"
20 
21 // 1 second of delay at 192kHz
22 #define MAX_DELAY 192000
23 
24 START_NAMESPACE_DISTRHO
25 
26 // -----------------------------------------------------------------------
27 
28 class ZamGrainsPlugin : public Plugin
29 {
30 public:
31     enum Parameters
32     {
33         paramGain = 0,
34         paramGrains,
35         paramGrainspeed,
36         paramPlayspeed,
37         paramDelaytime,
38 	paramFreeze,
39         paramGrainpos,
40         paramPlaypos,
41         paramFinalpos,
42         paramCount
43     };
44 
45     ZamGrainsPlugin();
46 
47 protected:
48     // -------------------------------------------------------------------
49     // Information
50 
getLabel() const51     const char* getLabel() const noexcept override
52     {
53         return "ZamGrains";
54     }
55 
getDescription() const56     const char* getDescription() const noexcept override
57     {
58         return "A granular delay plugin";
59     }
60 
getMaker() const61     const char* getMaker() const noexcept override
62     {
63         return "Damien Zammit";
64     }
65 
getHomePage() const66     const char* getHomePage() const noexcept override
67     {
68         return "http://www.zamaudio.com";
69     }
70 
getLicense() const71     const char* getLicense() const noexcept override
72     {
73         return "GPL v2+";
74     }
75 
getVersion() const76     uint32_t getVersion() const noexcept override
77     {
78         return d_version(3, 14, 0);
79     }
80 
getUniqueId() const81     int64_t getUniqueId() const noexcept override
82     {
83         return d_cconst('Z', 'M', 'G', 'R');
84     }
85 
86     // -------------------------------------------------------------------
87     // Init
88 
89     void initParameter(uint32_t index, Parameter& parameter) override;
90     void initProgramName(uint32_t index, String& programName) override;
91 
92     // -------------------------------------------------------------------
93     // Internal data
94 
95     float getParameterValue(uint32_t index) const override;
96     void  setParameterValue(uint32_t index, float value) override;
97     void  loadProgram(uint32_t index);
98 
99     // -------------------------------------------------------------------
100     // Process
101 
102 	static inline float
sanitize_denormal(float v)103 	sanitize_denormal(float v) {
104 	        if(!std::isnormal(v))
105 	                return 0.f;
106 	        return v;
107 	}
108 
109 	static inline float
from_dB(float gdb)110 	from_dB(float gdb) {
111 	        return (expf(0.05f*gdb*logf(10.f)));
112 	}
113 
114 	static inline float
to_dB(float g)115 	to_dB(float g) {
116 	        return (20.f*log10f(g));
117 	}
118 
119     void activate() override;
120     void run(const float** inputs, float** outputs, uint32_t frames) override;
121     float sample_and_hold(int ctrl, float input, int *state);
122     float hanning(int pos, int windowsize);
123 
124     // -------------------------------------------------------------------
125 
126 private:
127     int currgrains, zidx, zidx2, zidxold, zidx2old, samphold, samphold2;
128     float freeze, grains, grainspeed, playspeed, delaytime, gain, delaytimeout, playpos, grainpos, finalpos;
129     float delaytimeold, grainsold, grainspeedold;
130     float z[MAX_DELAY];
131     unsigned int posz;
132     unsigned int posphasor;
133 };
134 
135 // -----------------------------------------------------------------------
136 
137 END_NAMESPACE_DISTRHO
138 
139 #endif  // ZAMGRAINS_HPP_INCLUDED
140