File: system_compressor_wrapper.cpp

package info (click to toggle)
radium-compressor 0.5.1%2Bgit20190909-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,064 kB
  • sloc: cpp: 8,708; ansic: 1,636; makefile: 91; sh: 42
file content (312 lines) | stat: -rw-r--r-- 9,444 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* Copyright 2013 Kjetil S. Matheussen

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */


#include <math.h>
#include <string>

#include <QSharedMemory>
#include <QDateTime>

#include "faudiostream/architecture/faust/gui/UI.h"
#include "faudiostream/architecture/faust/audio/dsp.h"
#ifdef COMPILING_STANDALONE
#include "myjack-dsp.h"
#endif

#include "../common/nsmtracker.h"

#include "system_compressor_wrapper_proc.h"



#include <vector>

inline int 	max (unsigned int a, unsigned int b) { return (a>b) ? a : b; }
inline int 	max (int a, int b)		{ return (a>b) ? a : b; }

inline long 	max (long a, long b) 		{ return (a>b) ? a : b; }
inline long 	max (int a, long b) 		{ return (a>b) ? a : b; }
inline long 	max (long a, int b) 		{ return (a>b) ? a : b; }

inline float 	max (float a, float b) 		{ return (a>b) ? a : b; }
inline float 	max (int a, float b) 		{ return (a>b) ? a : b; }
inline float 	max (float a, int b) 		{ return (a>b) ? a : b; }
inline float 	max (long a, float b) 		{ return (a>b) ? a : b; }
inline float 	max (float a, long b) 		{ return (a>b) ? a : b; }

inline double 	max (double a, double b) 	{ return (a>b) ? a : b; }
inline double 	max (int a, double b) 		{ return (a>b) ? a : b; }
inline double 	max (double a, int b) 		{ return (a>b) ? a : b; }
inline double 	max (long a, double b) 		{ return (a>b) ? a : b; }
inline double 	max (double a, long b) 		{ return (a>b) ? a : b; }
inline double 	max (float a, double b) 	{ return (a>b) ? a : b; }
inline double 	max (double a, float b) 	{ return (a>b) ? a : b; }


inline int	min (int a, int b)		{ return (a<b) ? a : b; }

inline long 	min (long a, long b) 		{ return (a<b) ? a : b; }
inline long 	min (int a, long b) 		{ return (a<b) ? a : b; }
inline long 	min (long a, int b) 		{ return (a<b) ? a : b; }

inline float 	min (float a, float b) 		{ return (a<b) ? a : b; }
inline float 	min (int a, float b) 		{ return (a<b) ? a : b; }
inline float 	min (float a, int b) 		{ return (a<b) ? a : b; }
inline float 	min (long a, float b) 		{ return (a<b) ? a : b; }
inline float 	min (float a, long b) 		{ return (a<b) ? a : b; }

inline double 	min (double a, double b) 	{ return (a<b) ? a : b; }
inline double 	min (int a, double b) 		{ return (a<b) ? a : b; }
inline double 	min (double a, int b) 		{ return (a<b) ? a : b; }
inline double 	min (long a, double b) 		{ return (a<b) ? a : b; }
inline double 	min (double a, long b) 		{ return (a<b) ? a : b; }
inline double 	min (float a, double b) 	{ return (a<b) ? a : b; }
inline double 	min (double a, float b) 	{ return (a<b) ? a : b; }


#ifdef COMPILING_STANDALONE
 extern const char *g_jack_client_name;
 extern bool g_autoconnect;
#endif



namespace{
struct Meta
{
    void declare (const char* key, const char* value) { }
};
}
#include "system_compressor.cpp"


namespace{
 
class MyUI : public UI
{

 public:
  void declare(float* control_port, const char* key, const char* value) {
  }

  const char *_curr_box_name;

  void openFrameBox(const char* label) {_curr_box_name = label;}
  void openTabBox(const char* label) {_curr_box_name = label;}
  void openHorizontalBox(const char* label) {_curr_box_name = label;}
  void openVerticalBox(const char* label) {_curr_box_name = label;}
  void closeBox() {_curr_box_name = NULL;}

  std::vector<float*> _controllers;
  std::vector<float*> _graphs;

  void remove_last_item(){
    printf("Popping last effect\n");
    _controllers.pop_back();
  }

  void addEffect(const char *name, float* control_port, float min_value, float default_value, float max_value){
    printf("Adding effect %s %p\n",name,control_port);
    _controllers.push_back(control_port);
  }

  void addButton(const char* label, float* zone) {
    addEffect(label, zone, 0, 0, 1);
  }
  void addToggleButton(const char* label, float* zone) {
    addEffect(label, zone, 0, 0, 1);
  }
  void addCheckButton(const char* label, float* zone) {
    addEffect(label, zone, 0, 0, 1);
  }
  void addVerticalSlider(const char* label, float* zone, float init, float min, float max, float step) {
    addEffect(label, zone,  min, init, max);
  }
  void addHorizontalSlider(const char* label, float* zone, float init, float min, float max, float step) {
    addEffect(label, zone,  min, init, max);
  }
  void addNumEntry(const char* label, float* zone, float init, float min, float max, float step) {
    addEffect(label, zone, min, init, max); // The INT effect format might not work. Need to go through the code first.
  }
  
  // -- passive widgets

  void addNumDisplay(const char* label, float* zone, int precision) {remove_last_item();}
  void addTextDisplay(const char* label, float* zone, const char* names[], float min, float max) {remove_last_item();}
  void addHorizontalBargraph(const char* label, float* zone, float min, float max) {
    *zone = (min+max)/2; // init
    _graphs.push_back(zone);
    //remove_last_item(); // remove metadata
    //next_peak = zone;
  }
  void addVerticalBargraph(const char* label, float* zone, float min, float max) {
    *zone = (min+max)/2; // init
    _graphs.push_back(zone);
    //remove_last_item(); // remove metadata
    //next_peak = zone;
  }

};

struct Compressor_wrapper : public Faust_system_compressor {
  MyUI ui;

  QString key; // If not empty string, the memory for the object is placed in shared memory.
  QSharedMemory *shared;

#ifdef COMPILING_STANDALONE
  jackaudio audio;
#endif

  Compressor_wrapper(float sample_rate){
    shared=NULL;

#ifdef COMPILING_STANDALONE
    printf("\n\nCREATING 1\n\n");
    audio.init(g_jack_client_name, this);
    audio.start(g_autoconnect);
#endif

#ifdef COMPILING_RADIUM
    init(sample_rate);
#endif

    buildUserInterface(&ui);
  }

  
#ifdef COMPILING_STANDALONE
  ~Compressor_wrapper(){
    printf("Closing\n");
    audio.stop();
  }

#endif

#if 0
  void* operator new(size_t size){
    printf("Allocating %d bytes\n",(int)size);
    return malloc(size);
  }
  void operator delete(void *p){
    printf("Freeing %p.\n",p);
    free(p);
  }
#endif

  //#endif

};

} // anon. namespace


void *COMPRESSOR_create_shared(float sample_rate){
  QString key = "radium_crashreporter_" + QString::number(QDateTime::currentMSecsSinceEpoch());
  QSharedMemory *shared = new QSharedMemory(key);
  if(shared->create(sizeof(Compressor_wrapper))==false){
    fprintf(stderr,"COMPRESSOR_create_shared: Couldn't create... Error: %s\n",shared->error()==QSharedMemory::NoError?"No error (?)":shared->errorString().toUtf8().constData());
    return NULL;
  }

  void *memory = shared->data();

  Compressor_wrapper *wrapper = new(memory) Compressor_wrapper(sample_rate);
  printf("memory: %p, shared: %p\n",memory,shared);
  wrapper->key = key;
  wrapper->shared = shared;
  
  return wrapper;
}

void *COMPRESSOR_create(float sample_rate){
  return new Compressor_wrapper(sample_rate);
}


void *COMPRESSOR_create_from_shared_mem_key(const char *key){
  QSharedMemory *shared = new QSharedMemory(key);
  return shared->data();
}

/*
const char *COMPRESSOR_get_shared_mem_key(void *das_wrapper){
  Compressor_wrapper *wrapper = (Compressor_wrapper*)das_wrapper;
  return wrapper->key.toAscii();
}
*/

#if COMPILING_STANDALONE
extern char *g_shared_mem_key;
#endif

void COMPRESSOR_delete(void *das_wrapper){
#if COMPILING_STANDALONE
  if(g_shared_mem_key!=NULL){
    COMPRESSOR_delete_ladspa(das_wrapper);
    return;
  }
#endif

  Compressor_wrapper *wrapper = (Compressor_wrapper*)das_wrapper;
  if(wrapper->key=="")
    delete wrapper;
  else
    wrapper->shared->detach();
}

float COMPRESSOR_get_parameter(void *das_wrapper,int num){
#if COMPILING_STANDALONE
if(g_shared_mem_key!=NULL)
  return COMPRESSOR_get_ladspa_parameter(das_wrapper,num);
#endif

  Compressor_wrapper *wrapper = (Compressor_wrapper*)das_wrapper;
  float *controller = wrapper->ui._controllers.at(num);
  return *controller;
}

void COMPRESSOR_set_parameter(void *das_wrapper,int num,float value){
#if COMPILING_STANDALONE
if(g_shared_mem_key!=NULL){
COMPRESSOR_set_ladspa_parameter(das_wrapper,num,value);
return;
}
#endif

  Compressor_wrapper *wrapper = (Compressor_wrapper*)das_wrapper;
  float *controller = wrapper->ui._controllers.at(num);
  *controller = value;
  //printf("Setting controller %d (%p) to %f (%f)\n",num,controller,value,*controller);
}

float COMPRESSOR_get_graph_value(void *das_wrapper, int num){
#if COMPILING_STANDALONE
if(g_shared_mem_key!=NULL)
  return COMPRESSOR_get_ladspa_graph_value(das_wrapper,num);
#endif

  Compressor_wrapper *wrapper = (Compressor_wrapper*)das_wrapper;
  float *controller = wrapper->ui._graphs.at(num);
  return *controller;
}

void COMPRESSOR_process(void *das_wrapper, float **inputs, float **outputs, int num_frames){
  Compressor_wrapper *wrapper = (Compressor_wrapper*)das_wrapper;
  wrapper->compute(num_frames, inputs, outputs);
}