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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
/*
This LV2 plugin provides meters
(c) Fraser Stuart 2009
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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <lv2.h>
#include "library/common.h"
#include "inv_meter.h"
static LV2_Descriptor *IMeterDescriptor = NULL;
typedef struct {
/* Ports */
float *ControlBypass;
float *AudioInputBufferL;
float *AudioInputBufferR;
float *AudioOutputBufferL;
float *AudioOutputBufferR;
float *MeterL;
float *MeterR;
float *VuL;
float *VuR;
float *MeterPhase;
float *Spec[FILTER_COUNT];
/* stuff we need to remember to reduce cpu */
double SampleRate;
/* stuff we need to remember to reduce cpu */
float LastBypass;
float ConvertedBypass;
struct Envelope EnvAD[4];
float EnvMeterLLast;
float EnvMeterRLast;
float EnvVuLLast;
float EnvVuRLast;
float EnvPhaseLast;
float EnvSpecLast[FILTER_COUNT];
/* filters */
struct FilterP * filters;
} IMeter;
static LV2_Handle
instantiateIMeter(const LV2_Descriptor *descriptor, double s_rate, const char *path, const LV2_Feature * const* features)
{
IMeter *plugin = (IMeter *)malloc(sizeof(IMeter));
if(plugin==NULL)
return NULL;
/* set some initial params */
plugin->SampleRate=s_rate;
/* the delays */
if((plugin->filters = (struct FilterP *)malloc(sizeof(struct FilterP) * FILTER_COUNT))==NULL)
return NULL;
return (LV2_Handle)plugin;
}
static void
connectPortIMeter(LV2_Handle instance, uint32_t port, void *data)
{
IMeter *plugin = (IMeter *)instance;
switch (port) {
case IMETER_BYPASS:
plugin->ControlBypass = data;
break;
case IMETER_AUDIO_INL:
plugin->AudioInputBufferL = data;
break;
case IMETER_AUDIO_INR:
plugin->AudioInputBufferR = data;
break;
case IMETER_AUDIO_OUTL:
plugin->AudioOutputBufferL = data;
break;
case IMETER_AUDIO_OUTR:
plugin->AudioOutputBufferR = data;
break;
case IMETER_METER_L:
plugin->MeterL = data;
break;
case IMETER_METER_R:
plugin->MeterR = data;
break;
case IMETER_VU_L:
plugin->VuL = data;
break;
case IMETER_VU_R:
plugin->VuR = data;
break;
case IMETER_METER_PHASE:
plugin->MeterPhase = data;
break;
case IMETER_SPEC_20:
case IMETER_SPEC_25:
case IMETER_SPEC_31:
case IMETER_SPEC_40:
case IMETER_SPEC_50:
case IMETER_SPEC_63:
case IMETER_SPEC_80:
case IMETER_SPEC_100:
case IMETER_SPEC_125:
case IMETER_SPEC_160:
case IMETER_SPEC_200:
case IMETER_SPEC_250:
case IMETER_SPEC_315:
case IMETER_SPEC_400:
case IMETER_SPEC_500:
case IMETER_SPEC_630:
case IMETER_SPEC_800:
case IMETER_SPEC_1000:
case IMETER_SPEC_1250:
case IMETER_SPEC_1600:
case IMETER_SPEC_2000:
case IMETER_SPEC_2500:
case IMETER_SPEC_3150:
case IMETER_SPEC_4000:
case IMETER_SPEC_5000:
case IMETER_SPEC_6300:
case IMETER_SPEC_8000:
case IMETER_SPEC_10000:
case IMETER_SPEC_12500:
case IMETER_SPEC_16000:
case IMETER_SPEC_20000:
plugin->Spec[port-10] = data;
break;
}
}
static void
activateIMeter(LV2_Handle instance)
{
IMeter *plugin = (IMeter *)instance;
int i;
//defaults
plugin->LastBypass = 0;
plugin->EnvMeterLLast = 0;
plugin->EnvMeterRLast = 0;
plugin->EnvVuLLast = 0;
plugin->EnvVuRLast = 0;
plugin->EnvPhaseLast = 0;
for(i=0;i<FILTER_COUNT;i++) {
plugin->EnvSpecLast[i] = 0;
}
plugin->ConvertedBypass = convertParam(IMETER_BYPASS, plugin->LastBypass, plugin->SampleRate);
/* initialise envelopes */
initIEnvelope(&plugin->EnvAD[INVADA_METER_VU], INVADA_METER_VU, plugin->SampleRate);
initIEnvelope(&plugin->EnvAD[INVADA_METER_PEAK], INVADA_METER_PEAK, plugin->SampleRate);
initIEnvelope(&plugin->EnvAD[INVADA_METER_PHASE], INVADA_METER_PHASE, plugin->SampleRate);
initIEnvelope(&plugin->EnvAD[INVADA_METER_LAMP], INVADA_METER_LAMP, plugin->SampleRate);
/* initialise filters */
initBandpassFilter(&plugin->filters[0], plugin->SampleRate, 20.0, 0.33);
initBandpassFilter(&plugin->filters[1], plugin->SampleRate, 25.0, 0.33);
initBandpassFilter(&plugin->filters[2], plugin->SampleRate, 31.5, 0.33);
initBandpassFilter(&plugin->filters[3], plugin->SampleRate, 40.0, 0.33);
initBandpassFilter(&plugin->filters[4], plugin->SampleRate, 50.0, 0.33);
initBandpassFilter(&plugin->filters[5], plugin->SampleRate, 63.0, 0.33);
initBandpassFilter(&plugin->filters[6], plugin->SampleRate, 80.0, 0.33);
initBandpassFilter(&plugin->filters[7], plugin->SampleRate, 100.0, 0.33);
initBandpassFilter(&plugin->filters[8], plugin->SampleRate, 125.0, 0.33);
initBandpassFilter(&plugin->filters[9], plugin->SampleRate, 160.0, 0.33);
initBandpassFilter(&plugin->filters[10], plugin->SampleRate, 200.0, 0.33);
initBandpassFilter(&plugin->filters[11], plugin->SampleRate, 250.0, 0.33);
initBandpassFilter(&plugin->filters[12], plugin->SampleRate, 315.0, 0.33);
initBandpassFilter(&plugin->filters[13], plugin->SampleRate, 400.0, 0.33);
initBandpassFilter(&plugin->filters[14], plugin->SampleRate, 500.0, 0.33);
initBandpassFilter(&plugin->filters[15], plugin->SampleRate, 630.0, 0.33);
initBandpassFilter(&plugin->filters[16], plugin->SampleRate, 800.0, 0.33);
initBandpassFilter(&plugin->filters[17], plugin->SampleRate, 1000.0, 0.33);
initBandpassFilter(&plugin->filters[18], plugin->SampleRate, 1250.0, 0.33);
initBandpassFilter(&plugin->filters[19], plugin->SampleRate, 1600.0, 0.33);
initBandpassFilter(&plugin->filters[20], plugin->SampleRate, 2000.0, 0.33);
initBandpassFilter(&plugin->filters[21], plugin->SampleRate, 2500.0, 0.33);
initBandpassFilter(&plugin->filters[22], plugin->SampleRate, 3150.0, 0.33);
initBandpassFilter(&plugin->filters[23], plugin->SampleRate, 4000.0, 0.33);
initBandpassFilter(&plugin->filters[24], plugin->SampleRate, 5000.0, 0.33);
initBandpassFilter(&plugin->filters[25], plugin->SampleRate, 6300.0, 0.33);
initBandpassFilter(&plugin->filters[26], plugin->SampleRate, 8000.0, 0.33);
initBandpassFilter(&plugin->filters[27], plugin->SampleRate, 10000.0, 0.33);
initBandpassFilter(&plugin->filters[28], plugin->SampleRate, 12500.0, 0.33);
initBandpassFilter(&plugin->filters[29], plugin->SampleRate, 16000.0, 0.33);
initBandpassFilter(&plugin->filters[30], plugin->SampleRate, 20000.0, 0.33);
}
static void
runIMeter(LV2_Handle instance, uint32_t SampleCount)
{
float (*pParamFunc)(unsigned long, float, double) = NULL;
float * pfAudioInputL;
float * pfAudioInputR;
float * pfAudioOutputL;
float * pfAudioOutputR;
float fBypass;
float In;
float InL,EnvMeterL,EnvVuL;
float InR,EnvMeterR,EnvVuR;
float EnvPhase,CurrentPhase;
float EnvSpec[FILTER_COUNT];
int i;
struct FilterP *filter;
uint32_t lSampleIndex;
IMeter *plugin = (IMeter *)instance;
pParamFunc = &convertParam;
checkParamChange(IMETER_BYPASS, plugin->ControlBypass, &(plugin->LastBypass), &(plugin->ConvertedBypass), plugin->SampleRate, pParamFunc);
fBypass = plugin->ConvertedBypass;
pfAudioInputL = plugin->AudioInputBufferL;
pfAudioInputR = plugin->AudioInputBufferR;
pfAudioOutputL = plugin->AudioOutputBufferL;
pfAudioOutputR = plugin->AudioOutputBufferR;
EnvMeterL = plugin->EnvMeterLLast;
EnvMeterR = plugin->EnvMeterRLast;
EnvVuL = plugin->EnvVuLLast;
EnvVuR = plugin->EnvVuRLast;
EnvPhase = plugin->EnvPhaseLast;
for(i=0;i<FILTER_COUNT;i++) {
EnvSpec[i]=plugin->EnvSpecLast[i];
}
if(fBypass==0) {
for (lSampleIndex = 0; lSampleIndex < SampleCount; lSampleIndex++) {
InL=*(pfAudioInputL++);
InR=*(pfAudioInputR++);
In=(InL+InR)/2;
*(pfAudioOutputL++) = InL;
*(pfAudioOutputR++) = InR;
//evelope on in and out for meters
EnvMeterL += applyIEnvelope(&plugin->EnvAD[INVADA_METER_PEAK], InL, EnvMeterL);
EnvMeterR += applyIEnvelope(&plugin->EnvAD[INVADA_METER_PEAK], InR, EnvMeterR);
EnvVuL += applyIEnvelope(&plugin->EnvAD[INVADA_METER_VU], InL, EnvVuL);
EnvVuR += applyIEnvelope(&plugin->EnvAD[INVADA_METER_VU], InR, EnvVuR);
//envelope for phase
if(fabs(InL) > 0.001 || fabs(InR) > 0.001) { // -60 db
CurrentPhase = fabs(InL+InR) > 0.000001 ? atan(fabs((InL-InR)/(InL+InR))) : PI_ON_2;
} else {
CurrentPhase =0;
}
EnvPhase += applyIEnvelope(&plugin->EnvAD[INVADA_METER_PHASE], CurrentPhase, EnvPhase);
//envelop for spectrum
filter=plugin->filters;
for(i=0;i<FILTER_COUNT;i++) {
EnvSpec[i]+= applyIEnvelope(&plugin->EnvAD[INVADA_METER_PEAK], applyBandpassFilter(&filter[i],In), EnvSpec[i]);
}
}
} else {
for (lSampleIndex = 0; lSampleIndex < SampleCount; lSampleIndex++) {
*(pfAudioOutputL++) = *(pfAudioInputL++);
*(pfAudioOutputR++) = *(pfAudioInputR++);
}
//evelope on in and out for meters
EnvMeterL =0;
EnvMeterR =0;
EnvVuL =0;
EnvVuR =0;
EnvPhase =0;
for(i=0;i<FILTER_COUNT;i++) {
EnvSpec[i]=0;
}
}
// store values for next loop
plugin->EnvMeterLLast = (fabs(EnvMeterL)<1.0e-10) ? 0.f : EnvMeterL;
plugin->EnvMeterRLast = (fabs(EnvMeterR)<1.0e-10) ? 0.f : EnvMeterR;
plugin->EnvVuLLast = (fabs(EnvVuL)<1.0e-10) ? 0.f : EnvVuL;
plugin->EnvVuRLast = (fabs(EnvVuR)<1.0e-10) ? 0.f : EnvVuR;
plugin->EnvPhaseLast = (fabs(EnvPhase)<1.0e-10) ? 0.f : EnvPhase;
for(i=0;i<FILTER_COUNT;i++) {
plugin->EnvSpecLast[i] = (fabs(EnvSpec[i])<1.0e-10) ? 0.f : EnvSpec[i];
}
// update the meters
*(plugin->MeterL) = (EnvMeterL > 0.001) ? 20*log10(EnvMeterL) : -90.0;
*(plugin->MeterR) = (EnvMeterR > 0.001) ? 20*log10(EnvMeterR) : -90.0;
*(plugin->VuL) = EnvVuL;
*(plugin->VuR) = EnvVuR;
*(plugin->MeterPhase) = EnvPhase;
for(i=0;i<FILTER_COUNT;i++) {
*(plugin->Spec[i]) = (EnvSpec[i] > 0.001) ? 20*log10(EnvSpec[i]) : -90.0;
}
}
static void
cleanupIMeter(LV2_Handle instance)
{
free(instance);
}
static void
init()
{
IMeterDescriptor =
(LV2_Descriptor *)malloc(sizeof(LV2_Descriptor));
IMeterDescriptor->URI = IMETER_URI;
IMeterDescriptor->activate = activateIMeter;
IMeterDescriptor->cleanup = cleanupIMeter;
IMeterDescriptor->connect_port = connectPortIMeter;
IMeterDescriptor->deactivate = NULL;
IMeterDescriptor->instantiate = instantiateIMeter;
IMeterDescriptor->run = runIMeter;
IMeterDescriptor->extension_data = NULL;
}
LV2_SYMBOL_EXPORT
const LV2_Descriptor *lv2_descriptor(uint32_t index)
{
if (!IMeterDescriptor) init();
switch (index) {
case 0:
return IMeterDescriptor;
default:
return NULL;
}
}
/*****************************************************************************/
float
convertParam(unsigned long param, float value, double sr) {
float result;
switch(param) {
case IMETER_BYPASS:
if(value<=0.0)
result= 0;
else
result= 1;
break;
default:
result=0;
break;
}
return result;
}
|