File: midside_matrix.c

package info (click to toggle)
eq10q 2.2~repack0-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,344 kB
  • sloc: cpp: 8,036; ansic: 1,776; makefile: 6
file content (317 lines) | stat: -rw-r--r-- 9,243 bytes parent folder | download | duplicates (2)
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
313
314
315
316
317
/***************************************************************************
 *   Copyright (C) 2015 by Pere Ràfols Soler                               *
 *   sapista2@gmail.com                                                    *
 *                                                                         *
 *   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.             *
 ***************************************************************************/

/***************************************************************************
This file is the implementation of the MidSide Matrix plugin
This plugin is inside the Sapista Plugins Bundle
This file implements functionalities for lr2ms and ms2lr matrices
****************************************************************************/

#include <stdio.h>
#include <stdlib.h>

#include <lv2/lv2plug.in/ns/lv2core/lv2.h>


#include "dsp/vu.h"
#include "dsp/db.h"
#include "dsp/midside.h"

//Data from CMake
#define MATRIX_URI @Matrix_Uri@
#define IS_LR2MS @Matrix_Is_LR2MS@ //If is defined to 1 is in LR2MS mode otherwise is MS2LR

//Port definitions
#define PORT_AUDIO_IN_1 0
#define PORT_AUDIO_IN_2 1
#define PORT_AUDIO_OUT_1 2
#define PORT_AUDIO_OUT_2 3
#define PORT_GAIN_IN_1 4
#define PORT_GAIN_IN_2 5
#define PORT_GAIN_OUT_1 6
#define PORT_GAIN_OUT_2 7
#define PORT_SOLO_IN_1 8
#define PORT_SOLO_IN_2 9
#define PORT_SOLO_OUT_1 10
#define PORT_SOLO_OUT_2 11
#define PORT_VU_IN_1 12
#define PORT_VU_IN_2 13
#define PORT_VU_OUT_1 14
#define PORT_VU_OUT_2 15

typedef struct {
  //Plugin ports
  float *fInGain1; //Its L for lr2ms and M for ms2lr
  float *fInGain2; //Its R for lr2ms and S for ms2lr
  float *fOutGain1;  //Its M for lr2ms and L for ms2lr
  float *fOutGain2;  //Its S for lr2ms and R for ms2lr
  
  float *fSoloIn1; //Its L for lr2ms and M for ms2lr
  float *fSoloIn2; //Its R for lr2ms and S for ms2lr
  float *fSoloOut1; //Its M for lr2ms and L for ms2lr
  float *fSoloOut2; //Its S for lr2ms and R for ms2lr
  
  const float *input[2];
  float *output[2];

  float *fVuIn[2];
  float *fVuOut[2];
 
  //Internal data
  Vu *InputVu[2];
  Vu *OutputVu[2];
  float sample_rate;
  
  //Matrix routing vars
  double RG_in1, RG_in2, RG_out11, RG_out12, RG_out21, RG_out22; 

} MidSideMatrix;

static void cleanupMatrix(LV2_Handle instance)
{
  MidSideMatrix *plugin = (MidSideMatrix *)instance;
  int i;
  for(i=0; i<2; i++)
  {
    VuClean(plugin->InputVu[i]);
    VuClean(plugin->OutputVu[i]);
  }
  free(instance);
}


static void connectPortMatrix(LV2_Handle instance, uint32_t port, void *data)
{
  MidSideMatrix *plugin = (MidSideMatrix *)instance;
  switch (port) {
    case PORT_AUDIO_IN_1:
	  plugin->input[0] = (const float*)data;;
	  break;
	    
    case PORT_AUDIO_IN_2:
	  plugin->input[1] = (const float*)data;;
	  break;
	    
    case PORT_AUDIO_OUT_1:
	  plugin->output[0] = (float*)data;
	  break;  
	    
    case PORT_AUDIO_OUT_2:
	  plugin->output[1] = (float*)data;
	  break;    
	        
    case PORT_GAIN_IN_1:
	  plugin->fInGain1 = (float*)data;
	  break;
	  
    case PORT_GAIN_IN_2:
	  plugin->fInGain2 = (float*)data;
	  break;

    case PORT_GAIN_OUT_1:
	  plugin->fOutGain1 = (float*)data;
	  break;

    case PORT_GAIN_OUT_2:
	  plugin->fOutGain2 = (float*)data;
	  break;	  
	  
    case PORT_SOLO_IN_1:
	  plugin->fSoloIn1 = (float*)data;
	  break;
	  
    case PORT_SOLO_IN_2:
	  plugin->fSoloIn2 = (float*)data;
	  break;
	  
    case PORT_SOLO_OUT_1:
	  plugin->fSoloOut1 = (float*)data;
	  break;
	 
    case PORT_SOLO_OUT_2:
	  plugin->fSoloOut2 = (float*)data;
	  break;  
	  
    case PORT_VU_IN_1:
	  plugin->fVuIn[0]=(float*)data;
	  break;
	  
    case PORT_VU_IN_2:
	  plugin->fVuIn[1]=(float*)data;
	  break;
	  
    case PORT_VU_OUT_1:
	  plugin->fVuOut[0]=(float*)data;
	  break;
	  
    case PORT_VU_OUT_2:
	  plugin->fVuOut[1]=(float*)data;
	  break;
  } 
}

static LV2_Handle instantiateMatrix(const LV2_Descriptor *descriptor, double s_rate, const char *path, const LV2_Feature *const * features)
{
  MidSideMatrix *plugin_data = (MidSideMatrix *)malloc(sizeof(MidSideMatrix));  
  plugin_data->sample_rate = s_rate;
  plugin_data->InputVu[0] = VuInit(s_rate);
  plugin_data->InputVu[1] = VuInit(s_rate);
  plugin_data->OutputVu[0] = VuInit(s_rate);
  plugin_data->OutputVu[1] = VuInit(s_rate);
  plugin_data->RG_in1 = 0.0f;
  plugin_data->RG_in2 = 0.0f;
  plugin_data->RG_out11 = 0.0f;
  plugin_data->RG_out12 = 0.0f;
  plugin_data->RG_out21 = 0.0f;
  plugin_data->RG_out22 = 0.0f;
 
  return (LV2_Handle)plugin_data;
}

static void runMatrix(LV2_Handle instance, uint32_t sample_count)
{
  MidSideMatrix *plugin_data = (MidSideMatrix *)instance;
     
  const double gain_in_1 = (double) dB2Lin(*(plugin_data->fInGain1)); 
  const double gain_in_2 = (double) dB2Lin(*(plugin_data->fInGain2));
  const double gain_out_1 = (double) dB2Lin(*(plugin_data->fOutGain1));
  const double gain_out_2 = (double) dB2Lin(*(plugin_data->fOutGain2));
  
  const float solo_in_1 =  *(plugin_data->fSoloIn1);
  const float solo_in_2 =  *(plugin_data->fSoloIn2);
  const float solo_out_1 = *(plugin_data->fSoloOut1);
  const float solo_out_2 = *(plugin_data->fSoloOut2);

  //Set RG constants acording SOLO state
  plugin_data-> RG_in1 = 0.0;
  plugin_data-> RG_in2 = 0.0;
  plugin_data->RG_out11 = 1.0;
  plugin_data->RG_out12 = 0.0;
  plugin_data->RG_out21 = 0.0;
  plugin_data->RG_out22 = 1.0;
  
  if( solo_out_1 > 0.5)
  {
    plugin_data-> RG_in1 = 0.0;
    plugin_data-> RG_in2 = 0.0;
    plugin_data->RG_out11 = 1.0;
    plugin_data->RG_out12 = 1.0;
    plugin_data->RG_out21 = 0.0;
    plugin_data->RG_out22 = 0.0;
  }
  
  if( solo_out_2 > 0.5)
  {
    plugin_data-> RG_in1 = 0.0;
    plugin_data-> RG_in2 = 0.0;
    plugin_data->RG_out11 = 0.0;
    plugin_data->RG_out12 = 0.0;
    plugin_data->RG_out21 = 1.0;
    plugin_data->RG_out22 = 1.0;
  }
  
  if( solo_in_1 > 0.5 )
  {
    plugin_data-> RG_in1 = 1.0;
    plugin_data-> RG_in2 = 0.0;
    plugin_data->RG_out11 = 0.0;
    plugin_data->RG_out12 = 0.0;
    plugin_data->RG_out21 = 0.0;
    plugin_data->RG_out22 = 0.0;
  }
  
  if( solo_in_2 > 0.5)
  {
    plugin_data-> RG_in1 = 0.0;
    plugin_data-> RG_in2 = 1.0;
    plugin_data->RG_out11 = 0.0;
    plugin_data->RG_out12 = 0.0;
    plugin_data->RG_out21 = 0.0;
    plugin_data->RG_out22 = 0.0;
  }
  
  double sgn1_pre = 0.0, sgn2_pre = 0.0, sgn1_post = 0.0, sgn2_post = 0.0; //Internal signals
  
  for (uint32_t i = 0; i < sample_count; ++i) 
  {
    //Input Gains
    sgn1_pre = (double) (plugin_data->input[0][i]) * gain_in_1;
    sgn2_pre = (double) (plugin_data->input[1][i]) * gain_in_2;

 
    
    sgn1_post = sgn1_pre;
    sgn2_post = sgn2_pre;
    #if IS_LR2MS == 1
      LR2MS(&sgn1_post, &sgn2_post, 1.0);
    #else
      MS2LR(&sgn1_post, &sgn2_post, 1.0);
    #endif
   
    //Output Gains  
    sgn1_post *=   gain_out_1;
    sgn2_post *=   gain_out_2;
    
    //Update VU's
    SetSample(plugin_data->InputVu[0], (float)sgn1_pre);    
    SetSample(plugin_data->InputVu[1], (float)sgn2_pre);   
    SetSample(plugin_data->OutputVu[0], (float)sgn1_post);  
    SetSample(plugin_data->OutputVu[1], (float)sgn2_post);  
    
    //Set outputs
    plugin_data->output[0][i] = plugin_data->RG_in1 * sgn1_pre + 
				plugin_data->RG_in2 * sgn2_pre + 
				plugin_data->RG_out11 * sgn1_post + 
				plugin_data->RG_out21 * sgn2_post;
				
    plugin_data->output[1][i] = plugin_data->RG_in1 * sgn1_pre + 
				plugin_data->RG_in2 * sgn2_pre + 
				plugin_data->RG_out12 * sgn1_post + 
				plugin_data->RG_out22 * sgn2_post;
  }

  //Refresh VU's
  *(plugin_data->fVuIn[0]) = ComputeVu(plugin_data->InputVu[0], sample_count);
  *(plugin_data->fVuIn[1]) = ComputeVu(plugin_data->InputVu[1], sample_count);
  *(plugin_data->fVuOut[0]) = ComputeVu(plugin_data->OutputVu[0], sample_count);
  *(plugin_data->fVuOut[1]) = ComputeVu(plugin_data->OutputVu[1], sample_count);
}

static const LV2_Descriptor msMatrixDescriptor = {
  MATRIX_URI,
  instantiateMatrix,
  connectPortMatrix,
  NULL,
  runMatrix,
  NULL,
  cleanupMatrix,
  NULL
};

LV2_SYMBOL_EXPORT
const LV2_Descriptor *lv2_descriptor(uint32_t index)
{
  switch (index) {
  case 0:
    return &msMatrixDescriptor;
  default:
    return NULL;
  }
}