arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
AudioFaustDSP.h
1#pragma once
2
3#include "AudioConfig.h"
4#include "AudioTools/CoreAudio/AudioBasic/Collections.h"
5#include "AudioTools/CoreAudio/AudioBasic/Float16.h"
6#include "AudioTools/CoreAudio/AudioBasic/StrView.h"
7
8#ifndef FAUSTFLOAT
9#define FAUSTFLOAT float
10#endif
11
12#ifndef PSRAM_LIMIT
13#define PSRAM_LIMIT 1024
14#endif
15
16// forward declarations
17class UI;
18
19
25class dsp {
26 public:
27 virtual void init(int sample_rate) = 0;
28 virtual void compute(int count, FAUSTFLOAT** inputs, FAUSTFLOAT** outputs) = 0;
29 virtual void instanceClear() = 0;
30 virtual int getNumInputs() = 0;
31 virtual int getNumOutputs() = 0;
32 virtual void buildUserInterface(UI* ui_interface) = 0;
33};
34
40class Meta {
41 public:
42 void declare(const char*, const char*){}
43};
44
45typedef void Soundfile;
46
52class UI {
53 struct Entry {
54 const char* label=nullptr;
55 FAUSTFLOAT* zone=nullptr;
56 bool withLimits;
57 FAUSTFLOAT min;
58 FAUSTFLOAT max;
59 };
60
61 public:
62 // set and get values
63 virtual FAUSTFLOAT getValue(const char*label) {
64 Entry *e = findEntry(label);
65 if (e==nullptr){
66 LOGE("Label '%s' not found", label);
67 }
68 return e!=nullptr ? *(e->zone) :(FAUSTFLOAT) 0.0;
69 }
70 virtual bool setValue(const char* label, FAUSTFLOAT value){
71 bool result = false;
72 Entry* e = findEntry(label);
73 if (e!=nullptr){
74 if (e->withLimits){
75 if (value>=e->min && value<=e->max){
76 *(e->zone) = value;
77 result = true;
78 } else {
79 LOGE("Value '%s' outsde limits %f (%f-%f)", e->label, value, e->min, e->max);
80 }
81 } else {
82 *(e->zone) = value;
83 result = true;
84 }
85 } else {
86 LOGE("Label '%s' not found", label);
87 }
88 return result;
89 }
90
91
92
93 // -- widget's layouts
94 virtual void openTabBox(const char* label) {}
95 virtual void openHorizontalBox(const char* label) {}
96 virtual void openVerticalBox(const char* label) {}
97 virtual void closeBox() {}
98
99 // -- active widgets
100 virtual void addButton(const char* label, FAUSTFLOAT* zone) {
101 addEntry(label, zone);
102 }
103 virtual void addCheckButton(const char* label, FAUSTFLOAT* zone) {
104 addEntry(label, zone);
105 }
106 virtual void addVerticalSlider(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step) {
107 addEntry(label, zone, true, min, max);
108 *zone = init;
109 }
110 virtual void addHorizontalSlider(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step) {
111 addEntry(label, zone, true, min, max);
112 *zone = init;
113 }
114 virtual void addNumEntry(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step) {
115 addEntry(label, zone, true, min, max);
116 *zone = init;
117 }
118
119 // -- passive widgets
120 virtual void addHorizontalBargraph(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT min, FAUSTFLOAT max) {}
121 virtual void addVerticalBargraph(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT min, FAUSTFLOAT max) {}
122
123 // -- soundfiles
124 virtual void addSoundfile(const char* label, const char* filename, Soundfile** sf_zone) {}
125
126 // -- metadata declarations
127 virtual void declare(FAUSTFLOAT* zone, const char* key, const char* val) {}
128
130 virtual bool exists(const char*label){
131 return findEntry(label)!=nullptr;
132 }
133
135 virtual size_t size() {
136 return entries.size();
137 }
138
140 const char* label(int idx){
141 if (idx<size()){
142 return entries[idx].label;
143 }
144 return nullptr;
145 }
146
147 protected:
149
150 Entry *findEntry(const char* name){
151 StrView nameStr(name);
152 for (int j=0; j<entries.size();j++){
153 if (nameStr.equals(entries[j].label)){
154 return &entries[j];
155 }
156 }
157 return nullptr;
158 }
159
160 void addEntry(const char*label,FAUSTFLOAT* zone, bool withLimits=false, FAUSTFLOAT min=0, FAUSTFLOAT max=0){
161 Entry e;
162 e.label = label;
163 e.zone = zone;
164 e.withLimits = withLimits;
165 if (withLimits){
166 e.min = min;
167 e.max = max;
168 LOGI("Label: %s value: %f range: %f - %f", label, *zone, min, max);
169 } else {
170 LOGI("Label: %s value: %f", label, *zone);
171 }
172 entries.push_back(e);
173 }
174
175};
176
182public:
183 virtual ~dsp_memory_manager() {}
184
189 virtual bool begin(size_t count){
190 this->count = count;
191 total = 0;
192 return true;
193 }
194
201 virtual void info(size_t size, size_t reads, size_t writes) {
202 LOGD("info %d", size);
203 total+=size;
204 }
205
210 virtual void end(){
211#ifdef ESP32
212 is_psram = total>2000 && ESP.getFreePsram()>0;
213#endif
214 LOGI("use PSRAM: %s", is_psram?"true":"false");
215 }
216
221 virtual void* allocate(size_t size) {
222 LOGD("allocate %d", size);
223#ifdef ESP32
224 void* result = is_psram && size > PSRAM_LIMIT ? ps_malloc(size) : malloc(size);
225#else
226 void* result = malloc(size);
227#endif
228 if (result!=nullptr){
229 memset(result, size, 0);
230 } else {
231 LOGE("allocate %u bytes - failed", (unsigned) size);
232 }
233 return result;
234 };
235
236
241 virtual void destroy(void* ptr) {
242 LOGD("destroy");
243 free(ptr);
244 };
245
246private:
247 size_t count;
248 size_t total;
249 bool is_psram = false;
250
251
252};
253
254
255
minimial implementtion of Meta which just ignores the data
Definition AudioFaustDSP.h:40
Minimum implementation of UI parameters. We only support the setting and getting of values.
Definition AudioFaustDSP.h:52
const char * label(int idx)
Returns the label at the indicated position. nullptr is returned if the index is too big.
Definition AudioFaustDSP.h:140
virtual bool exists(const char *label)
checks if a label exists
Definition AudioFaustDSP.h:130
virtual size_t size()
Returns the number of label entries.
Definition AudioFaustDSP.h:135
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
Memory manager which uses psram when it is available.
Definition AudioFaustDSP.h:181
virtual void destroy(void *ptr)
Definition AudioFaustDSP.h:241
virtual void info(size_t size, size_t reads, size_t writes)
Definition AudioFaustDSP.h:201
virtual void * allocate(size_t size)
Definition AudioFaustDSP.h:221
virtual bool begin(size_t count)
Definition AudioFaustDSP.h:189
virtual void end()
Definition AudioFaustDSP.h:210
minimal dsp base class needed by Faust
Definition AudioFaustDSP.h:25