Diff of /trunk/src/LV2/gxamp.lv2/gxamp.cpp [2c6fb3] .. [0ed57a] Maximize Restore

  Switch to side-by-side view

--- a/trunk/src/LV2/gxamp.lv2/gxamp.cpp
+++ b/trunk/src/LV2/gxamp.lv2/gxamp.cpp
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2012 Hermann Meyer, Andreas Degert, Pete Shorthose
- * 
+ *
  * 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
@@ -25,83 +25,83 @@
    flags to avoid costly denormals */
 #ifdef __SSE3__
 #include <pmmintrin.h>
-inline void AVOIDDENORMALS() {
-    _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
-    _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
+inline void AVOIDDENORMALS()
+{
+  _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
+  _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
 }
 #else
 #include <xmmintrin.h>
-inline void AVOIDDENORMALS() { _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); }
+inline void AVOIDDENORMALS()
+{
+  _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
+}
 #endif //__SSE3__
 
 #else
 inline void AVOIDDENORMALS() {}
 #endif //__SSE__
 
-// faust support 
+// faust support
 #define FAUSTFLOAT float
 #ifndef N_
 #define N_(String) (String)
 #endif
 #define max(x, y) (((x) > (y)) ? (x) : (y))
 #define min(x, y) (((x) < (y)) ? (x) : (y))
-template <int N> inline float faustpower(float x)       {return powf(x, N);}
-template <int N> inline double faustpower(double x)     {return pow(x, N);}
-template <int N> inline int faustpower(int x) {return faustpower<N/2>(x) * faustpower<N-N/2>(x);}
-template <>      inline int faustpower<0>(int x)        {return 1;}
-template <>      inline int faustpower<1>(int x)        {return x;}
+template <int N> inline float faustpower(float x)
+{
+  return powf(x, N);
+}
+template <int N> inline double faustpower(double x)
+{
+  return pow(x, N);
+}
+template <int N> inline int faustpower(int x)
+{
+  return faustpower<N/2>(x) * faustpower<N-N/2>(x);
+}
+template <>      inline int faustpower<0>(int x)
+{
+  return 1;
+}
+template <>      inline int faustpower<1>(int x)
+{
+  return x;
+}
 
 struct GXPlugin;
 
 #include "gxamp.h"
 #include "gx_resampler.h"
 #include "gx_convolver.h"
+#include "gx_tonestack.h"
 #include "gx_amp.h"
-#include "gx_tonestack.h"
 #include "impulse_former.h"
 #include "ampulse_former.h"
 
-typedef enum {
-  AMP_MASTERGAIN   = 0,
-  AMP_PREGAIN,
-  AMP_WET_DRY,
-  AMP_DRIVE,
-  MID,
-  BASS,
-  TREBLE,
-  CLevel,
-  ALevel,
-  AMP_OUTPUT,
-  AMP_INPUT,
-  AMP_CONTROL,
-  AMP_NOTIFY,
-} PortIndex;
-
-struct GXPlugin {
-  // Port buffers
-  LV2_Atom_Sequence* c_notice;
-  LV2_Atom_Sequence* n_notice;
-  
-  LV2_Atom_Forge forge;
-  GXPluginURIs   uris;
-  LV2_Atom_Forge_Frame notify_frame;
-  LV2_Worker_Schedule* schedule;
-  LV2_Log_Log*         log;
-  
-  bool schedule_wait;
-  Tonestack *ts;
-  LV2_URID_Map*        map;
-  GxAmp *amplifier;
-  
+
+struct GXPlugin
+{
+  // LV2 stuff
+  LV2_Atom_Sequence*           c_notice;
+  LV2_Atom_Sequence*           n_notice;
+  LV2_URID_Map*                map;
+  LV2_Atom_Forge               forge;
+  GXPluginURIs                 uris;
+  LV2_Atom_Forge_Frame         notify_frame;
+  LV2_Worker_Schedule*         schedule;
+  // internal stuff
+  Tonestack                    *ts;
+  GxAmp                        *amplifier;
   gx_resample::BufferResampler resamp;
-  GxSimpleConvolver *cabconv;
-  Impf * impf;
-  
+  GxSimpleConvolver            *cabconv;
+  Impf                         *impf;
   gx_resample::BufferResampler resamp1;
-  GxSimpleConvolver *ampconv;
-  Ampf * ampf;
-  
-  uint32_t bufsize;
+  GxSimpleConvolver            *ampconv;
+  Ampf                         *ampf;
+  uint32_t                     bufsize;
+  bool                         schedule_wait;
   static void connect(uint32_t port,void* data, GXPlugin* self);
   GXPlugin() {}
 };
@@ -126,34 +126,30 @@
   GXPlugin* self = (GXPlugin*)instance;
   GXPluginURIs* uris        = &self->uris;
   const LV2_Atom_Object* obj = (LV2_Atom_Object*)data;
-  if (obj->body.otype == uris->gx_cab) {
-    //printf("worker run. %d id= %d type= %d\n", obj->body.otype, obj->body.id, obj->atom.type);
-    float cab_irdata_c[cab_data_HighGain.ir_count];
-    self->impf->compute(cab_data_HighGain.ir_count, cab_data_HighGain.ir_data, cab_irdata_c);
-   // cab_data_HighGain.ir_count, cab_data_HighGain.ir_data,cab_data_HighGain.ir_sr
-    if (!self->cabconv->update(cab_data_HighGain.ir_count, cab_irdata_c, cab_data_HighGain.ir_sr))
-    printf("cabconv->update fail.\n");
-    //printf("worker 1 ready.\n");
-  } else if (obj->body.otype == uris->gx_pre) {
-    //printf("worker run. %d id= %d type= %d\n", obj->body.otype, obj->body.id, obj->atom.type);
-    float pre_irdata_c[contrast_ir_desc.ir_count];
-    self->ampf->compute(contrast_ir_desc.ir_count,contrast_ir_desc.ir_data, pre_irdata_c);
-   // cab_data_HighGain.ir_count, cab_data_HighGain.ir_data,cab_data_HighGain.ir_sr
-    if (!self->ampconv->update(contrast_ir_desc.ir_count, pre_irdata_c, contrast_ir_desc.ir_sr))
-    printf("cabconv->update fail.\n");
-    //printf("worker 2 ready.\n");
-  }
+  if (obj->body.otype == uris->gx_cab)
+    {
+      //printf("worker run. %d id= %d type= %d\n", obj->body.otype, obj->body.id, obj->atom.type);
+      float cab_irdata_c[cab_data_HighGain.ir_count];
+      self->impf->compute(cab_data_HighGain.ir_count, cab_data_HighGain.ir_data, cab_irdata_c);
+      // cab_data_HighGain.ir_count, cab_data_HighGain.ir_data,cab_data_HighGain.ir_sr
+      if (!self->cabconv->update(cab_data_HighGain.ir_count, cab_irdata_c, cab_data_HighGain.ir_sr))
+        printf("cabconv->update fail.\n");
+      //printf("worker 1 ready.\n");
+    }
+  else if (obj->body.otype == uris->gx_pre)
+    {
+      //printf("worker run. %d id= %d type= %d\n", obj->body.otype, obj->body.id, obj->atom.type);
+      float pre_irdata_c[contrast_ir_desc.ir_count];
+      self->ampf->compute(contrast_ir_desc.ir_count,contrast_ir_desc.ir_data, pre_irdata_c);
+      // cab_data_HighGain.ir_count, cab_data_HighGain.ir_data,cab_data_HighGain.ir_sr
+      if (!self->ampconv->update(contrast_ir_desc.ir_count, pre_irdata_c, contrast_ir_desc.ir_sr))
+        printf("cabconv->update fail.\n");
+      //printf("worker 2 ready.\n");
+    }
   self->schedule_wait = false;
   return LV2_WORKER_SUCCESS;
 }
 
-/**
-   Handle a response from work() in the audio thread.
-
-   When running normally, this will be called by the host after run().  When
-   freewheeling, this will be called immediately at the point the work was
-   scheduled.
-*/
 static LV2_Worker_Status
 work_response(LV2_Handle  instance,
               uint32_t    size,
@@ -171,57 +167,73 @@
             const LV2_Feature* const* features)
 {
   GXPlugin *self = new GXPlugin();
-  if (!self) {
-    return NULL;
-  }
+  if (!self)
+    {
+      return NULL;
+    }
 
   const LV2_Options_Option* options  = NULL;
   uint32_t bufsize = 0;
 
-  for (int i = 0; features[i]; ++i) {
-    if (!strcmp(features[i]->URI, LV2_URID__map)) {
-      self->map = (LV2_URID_Map*)features[i]->data;
-    } else if (!strcmp(features[i]->URI, LV2_WORKER__schedule)) {
-        self->schedule = (LV2_Worker_Schedule*)features[i]->data;
-    } else if (!strcmp(features[i]->URI, LV2_OPTIONS__options)) {
-      options = (const LV2_Options_Option*)features[i]->data;
-    }
-  }
-  if (!self->schedule) {
-    fprintf(stderr, "Missing feature work:schedule.\n");
-    delete self; 
-    return NULL;
-  }
-  if (!self->map) {
-    fprintf(stderr, "Missing feature uri:map.\n");
-  } else if (!options) {
-    fprintf(stderr, "Missing feature options.\n");
-  } else {
-    LV2_URID bufsz_max = self->map->map(self->map->handle, LV2_BUF_SIZE__maxBlockLength);
-    LV2_URID atom_Int = self->map->map(self->map->handle, LV2_ATOM__Int);
-  
-    for (const LV2_Options_Option* o = options; o->key; ++o) {
-      if (o->context == LV2_OPTIONS_INSTANCE &&
-        o->key == bufsz_max &&
-        o->type == atom_Int) {
-        bufsize = *(const int32_t*)o->value;
-      }
-    }
-
-    if (bufsize == 0) {
-      fprintf(stderr, "No maximum buffer size given.\n");
-    }
-
-    //if (bufsize < 64 || bufsize > 8192 ) {
-    //  fprintf(stderr, "unsupported block-size: %d\n", bufsize);
-    //  bufsize = 0;
-    //}
-    printf("using block size: %d\n", bufsize);
-  }
-  
-  
+  for (int i = 0; features[i]; ++i)
+    {
+      if (!strcmp(features[i]->URI, LV2_URID__map))
+        {
+          self->map = (LV2_URID_Map*)features[i]->data;
+        }
+      else if (!strcmp(features[i]->URI, LV2_WORKER__schedule))
+        {
+          self->schedule = (LV2_Worker_Schedule*)features[i]->data;
+        }
+      else if (!strcmp(features[i]->URI, LV2_OPTIONS__options))
+        {
+          options = (const LV2_Options_Option*)features[i]->data;
+        }
+    }
+  if (!self->schedule)
+    {
+      fprintf(stderr, "Missing feature work:schedule.\n");
+      delete self;
+      return NULL;
+    }
+  if (!self->map)
+    {
+      fprintf(stderr, "Missing feature uri:map.\n");
+    }
+  else if (!options)
+    {
+      fprintf(stderr, "Missing feature options.\n");
+    }
+  else
+    {
+      LV2_URID bufsz_max = self->map->map(self->map->handle, LV2_BUF_SIZE__maxBlockLength);
+      LV2_URID atom_Int = self->map->map(self->map->handle, LV2_ATOM__Int);
+
+      for (const LV2_Options_Option* o = options; o->key; ++o)
+        {
+          if (o->context == LV2_OPTIONS_INSTANCE &&
+              o->key == bufsz_max &&
+              o->type == atom_Int)
+            {
+              bufsize = *(const int32_t*)o->value;
+            }
+        }
+
+      if (bufsize == 0)
+        {
+          fprintf(stderr, "No maximum buffer size given.\n");
+        }
+
+      //if (bufsize < 64 || bufsize > 8192 ) {
+      //  fprintf(stderr, "unsupported block-size: %d\n", bufsize);
+      //  bufsize = 0;
+      //}
+      printf("using block size: %d\n", bufsize);
+    }
+
+
   AVOIDDENORMALS();
-  
+
   LV2_URID_Map*             map      = self->map;
   map_gx_uris(map, &self->uris);
   lv2_atom_forge_init(&self->forge, self->map);
@@ -232,64 +244,68 @@
   self->ts = new Tonestack();
   self->amplifier->init_static(rate, self->amplifier);
   self->ts->init_static(rate, self->ts);
-    
+
   self->cabconv = new GxSimpleConvolver(self->resamp);
   self->cabconv->set_samplerate(rate);
   self->impf = new Impf();
   self->impf->init_static(rate, self->impf);
-  
+
   self->ampconv = new GxSimpleConvolver(self->resamp1);
   self->ampconv->set_samplerate(rate);
   self->ampf = new Ampf();
   self->ampf->init_static(rate, self->ampf);
-  
-  if (self->bufsize ) {
-    self->cabconv->set_buffersize(self->bufsize);
-    self->cabconv->configure(cab_data_HighGain.ir_count, cab_data_HighGain.ir_data, cab_data_HighGain.ir_sr);
-    self->cabconv->start(0, SCHED_FIFO);
-    
-    self->ampconv->set_buffersize(self->bufsize);
-    self->ampconv->configure(contrast_ir_desc.ir_count, contrast_ir_desc.ir_data, contrast_ir_desc.ir_sr);
-    self->ampconv->start(0, SCHED_FIFO);
-  } else {
-    printf("convolver disabled\n");
-  }
+
+  if (self->bufsize )
+    {
+      self->cabconv->set_buffersize(self->bufsize);
+      self->cabconv->configure(cab_data_HighGain.ir_count, cab_data_HighGain.ir_data, cab_data_HighGain.ir_sr);
+      self->cabconv->start(0, SCHED_FIFO);
+
+      self->ampconv->set_buffersize(self->bufsize);
+      self->ampconv->configure(contrast_ir_desc.ir_count, contrast_ir_desc.ir_data, contrast_ir_desc.ir_sr);
+      self->ampconv->start(0, SCHED_FIFO);
+    }
+  else
+    {
+      printf("convolver disabled\n");
+    }
   return (LV2_Handle)self;
 }
 
 void GXPlugin::connect(uint32_t port,void* data, GXPlugin* self)
 {
-    switch ((PortIndex)port) {
-      case AMP_MASTERGAIN:
-        break;
-      case AMP_PREGAIN:
-        break;
-      case AMP_WET_DRY:
-        break;
-      case AMP_DRIVE:
-        break;
-      case MID:
-        break;
-      case BASS:
-        break;
-      case TREBLE:
-        break;
-      case CLevel:
-        break;
-      case ALevel:
-        break;
-      case AMP_OUTPUT:
-        break;
-      case AMP_INPUT:
-        break;
-      case AMP_CONTROL:
-        self->c_notice = (LV2_Atom_Sequence*)data;
-        break;
-      case AMP_NOTIFY:
-        self->n_notice = (LV2_Atom_Sequence*)data;
-        break;
-    }
-    
+  switch ((PortIndex)port)
+    {
+    case AMP_MASTERGAIN:
+      break;
+    case AMP_PREGAIN:
+      break;
+    case AMP_WET_DRY:
+      break;
+    case AMP_DRIVE:
+      break;
+    case MID:
+      break;
+    case BASS:
+      break;
+    case TREBLE:
+      break;
+    case CLevel:
+      break;
+    case ALevel:
+      break;
+    case AMP_OUTPUT:
+      break;
+    case AMP_INPUT:
+      break;
+    case AMP_CONTROL:
+      self->c_notice = (LV2_Atom_Sequence*)data;
+      break;
+    case AMP_NOTIFY:
+      self->n_notice = (LV2_Atom_Sequence*)data;
+      break;
+    }
+
 }
 
 static void
@@ -315,7 +331,6 @@
 run(LV2_Handle instance, uint32_t n_samples)
 {
   GXPlugin* self = (GXPlugin*)instance;
-  //GXPluginURIs* uris        = &self->uris;
   /* Set up forge to write directly to notify output port. */
   const uint32_t notify_capacity = self->n_notice->atom.size;
   lv2_atom_forge_set_buffer(&self->forge,
@@ -325,15 +340,17 @@
   /* Start a sequence in the notify output port. */
   lv2_atom_forge_sequence_head(&self->forge, &self->notify_frame, 0);
 
-  /* Read incoming events */
-  if (self->schedule_wait == false) {
-    LV2_ATOM_SEQUENCE_FOREACH(self->c_notice, ev) {
-        
-      self->schedule_wait = true;
-      self->schedule->schedule_work(self->schedule->handle, 
-            lv2_atom_total_size(&ev->body), &ev->body);
-    }
-  }
+  /* Read incoming events if scheduler is free*/
+  if (self->schedule_wait == false)
+    {
+      LV2_ATOM_SEQUENCE_FOREACH(self->c_notice, ev)
+      {
+
+        self->schedule_wait = true;
+        self->schedule->schedule_work(self->schedule->handle,
+                                      lv2_atom_total_size(&ev->body), &ev->body);
+      }
+    }
   self->amplifier->run_static(n_samples, self);
 }
 
@@ -348,26 +365,29 @@
 {
   GXPlugin* self = (GXPlugin*)instance;
   self->cabconv->stop_process();
+  self->ampconv->stop_process();
   delete self->amplifier;
   delete self->ts;
   delete self->cabconv;
   delete self->impf;
   delete self->ampconv;
   delete self->ampf;
-  delete self; 
+  delete self;
 }
 
 const void*
 extension_data(const char* uri)
 {
   static const LV2_Worker_Interface worker = { work, work_response, NULL };
-  if (!strcmp(uri, LV2_WORKER__interface)) {
-    return &worker;
-  }
+  if (!strcmp(uri, LV2_WORKER__interface))
+    {
+      return &worker;
+    }
   return NULL;
 }
 
-static const LV2_Descriptor descriptor = {
+static const LV2_Descriptor descriptor =
+{
   GXPLUGIN_URI,
   instantiate,
   connect_port,
@@ -383,10 +403,11 @@
 const LV2_Descriptor*
 lv2_descriptor(uint32_t index)
 {
-  switch (index) {
-  case 0:
-    return &descriptor;
-  default:
-    return NULL;
-  }
-}
+  switch (index)
+    {
+    case 0:
+      return &descriptor;
+    default:
+      return NULL;
+    }
+}