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