]> git.0d.be Git - jack_mixer.git/commitdiff
Cleanup scale code by removing threshold objects in python part (replaced with mark...
authorNedko Arnaudov <nedko@arnaudov.name>
Wed, 16 May 2007 21:43:44 +0000 (21:43 +0000)
committerNedko Arnaudov <nedko@arnaudov.name>
Wed, 16 May 2007 21:43:44 +0000 (21:43 +0000)
git-svn-id: svn://svn.gna.org/svn/jackmixer/code@66 330dde89-2920-0410-a79a-889b863ea5ce

jack_mixer.h
scale.c
scale.py

index ab3bdb6aeb7aee5c3f77ef246536630b90e3c0ac..395bafda45d7d718d321521540a1c8cf38dc12cc 100644 (file)
@@ -147,35 +147,14 @@ channel_rename(
   jack_mixer_channel_t channel,
   const char * name);
 
-jack_mixer_threshold_t
-threshold_create(double db, double scale);
-
-void
-threshold_calculate_coefficients(
-  jack_mixer_threshold_t threshold,
-  jack_mixer_threshold_t prev);
-
-double
-threshold_db_to_scale(
-  jack_mixer_threshold_t threshold,
-  double db);
-
-double
-threshold_scale_to_db(
-  jack_mixer_threshold_t threshold,
-  double scale);
-
-void
-threshold_destroy(
-  jack_mixer_threshold_t threshold);
-
 jack_mixer_scale_t
 scale_create();
 
-void
+bool
 scale_add_threshold(
   jack_mixer_scale_t scale,
-  jack_mixer_threshold_t threshold);
+  float db,
+  float scale_value);
 
 void
 scale_calculate_coefficients(
diff --git a/scale.c b/scale.c
index e4e16693eb40fab7d472a8213393ee557062446a..5efba192241fdee66075ac4d497260a407aaeb98 100644 (file)
--- a/scale.c
+++ b/scale.c
@@ -43,71 +43,9 @@ struct threshold
 struct scale
 {
   struct list_head thresholds;
+  double max_db;
 };
 
-jack_mixer_threshold_t
-threshold_create(double db, double scale)
-{
-  struct threshold * threshold_ptr;
-
-  threshold_ptr = malloc(sizeof(struct threshold));
-  if (threshold_ptr == NULL)
-  {
-    return NULL;
-  }
-
-  threshold_ptr->db = db;
-  threshold_ptr->scale = scale;
-
-  LOG_DEBUG("Threshold %p created (%f dBFS -> %f)", threshold_ptr, db, scale);
-
-  return (jack_mixer_threshold_t)threshold_ptr;
-}
-
-void
-threshold_calculate_coefficients_internal(
-  struct threshold * threshold_ptr,
-  struct threshold * prev_ptr)
-{
-  threshold_ptr->a = (prev_ptr->scale - threshold_ptr->scale) / (prev_ptr->db - threshold_ptr->db);
-  threshold_ptr->b = threshold_ptr->scale - threshold_ptr->a * threshold_ptr->db;
-  LOG_DEBUG("%.0f dB - %.0f dB: scale = %f * dB + %f", prev_ptr->db, threshold_ptr->db, threshold_ptr->a, threshold_ptr->b);
-}
-
-#define threshold_ptr ((struct threshold *)threshold)
-
-void
-threshold_calculate_coefficients(
-  jack_mixer_threshold_t threshold,
-  jack_mixer_threshold_t prev)
-{
-  threshold_calculate_coefficients_internal(threshold_ptr, (struct threshold *)prev);
-}
-
-double
-threshold_db_to_scale(
-  jack_mixer_threshold_t threshold,
-  double db)
-{
-  return threshold_ptr->a * db + threshold_ptr->b;
-}
-
-double
-threshold_scale_to_db(
-  jack_mixer_threshold_t threshold,
-  double scale)
-{
-  return (scale - threshold_ptr->b) / threshold_ptr->a;
-}
-
-void
-threshold_destroy(
-  jack_mixer_threshold_t threshold)
-{
-  LOG_DEBUG("Destroying threshold %p (%f dBFS -> %f)", threshold_ptr, threshold_ptr->db, threshold_ptr->scale);
-  free(threshold_ptr);
-}
-
 jack_mixer_scale_t
 scale_create()
 {
@@ -120,6 +58,7 @@ scale_create()
   }
 
   INIT_LIST_HEAD(&scale_ptr->thresholds);
+  scale_ptr->max_db = -INFINITY;
 
   LOG_DEBUG("Scale %p created", scale_ptr);
 
@@ -135,13 +74,34 @@ scale_destroy(
   free(scale_ptr);
 }
 
-void
+bool
 scale_add_threshold(
   jack_mixer_scale_t scale,
-  jack_mixer_threshold_t threshold)
+  float db,
+  float scale_value)
 {
-  LOG_DEBUG("Adding threshold %p to scale %p", threshold_ptr, scale_ptr);
+  struct threshold * threshold_ptr;
+
+  LOG_DEBUG("Adding threshold (%f dBFS -> %f) to scale %p", db, scale, scale_ptr);
+  LOG_DEBUG("Threshold %p created ", threshold_ptr, db, scale);
+
+  threshold_ptr = malloc(sizeof(struct threshold));
+  if (threshold_ptr == NULL)
+  {
+    return false;
+  }
+
+  threshold_ptr->db = db;
+  threshold_ptr->scale = scale_value;
+
   list_add_tail(&threshold_ptr->scale_siblings, &scale_ptr->thresholds);
+
+  if (db > scale_ptr->max_db)
+  {
+    scale_ptr->max_db = db;
+  }
+
+  return true;
 }
 
 #undef threshold_ptr
@@ -164,7 +124,9 @@ scale_calculate_coefficients(
 
     if (prev_ptr != NULL)
     {
-      threshold_calculate_coefficients_internal(threshold_ptr, prev_ptr);
+      threshold_ptr->a = (prev_ptr->scale - threshold_ptr->scale) / (prev_ptr->db - threshold_ptr->db);
+      threshold_ptr->b = threshold_ptr->scale - threshold_ptr->a * threshold_ptr->db;
+      LOG_DEBUG("%.0f dB - %.0f dB: scale = %f * dB + %f", prev_ptr->db, threshold_ptr->db, threshold_ptr->a, threshold_ptr->b);
     }
 
     prev_ptr = threshold_ptr;
@@ -195,7 +157,7 @@ scale_db_to_scale(
         return 0.0;
       }
 
-      return threshold_db_to_scale((jack_mixer_threshold_t)threshold_ptr, db);
+      return threshold_ptr->a * db + threshold_ptr->b;
     }
 
     prev_ptr = threshold_ptr;
@@ -227,11 +189,11 @@ scale_scale_to_db(
         return -INFINITY;
       }
 
-      return threshold_scale_to_db((jack_mixer_threshold_t)threshold_ptr, scale_value);
+      return (scale_value - threshold_ptr->b) / threshold_ptr->a;
     }
 
     prev_ptr = threshold_ptr;
   }
 
-  return threshold_scale_to_db((jack_mixer_threshold_t)list_entry(scale_ptr->thresholds.prev, struct threshold, scale_siblings), scale_value);
+  return scale_ptr->max_db;
 }
index 16e0ee65db0b648a26343941f11ceda62283e312..9d0dd3d75dea03961e2d2431fc434120805e5b2f 100644 (file)
--- a/scale.py
+++ b/scale.py
@@ -21,45 +21,25 @@ import math
 import fpconst
 import jack_mixer_c
 
-class threshold:
+class mark:
     '''Encapsulates scale linear function edge and coefficients for scale = a * dB + b formula'''
     def __init__(self, db, scale):
         self.db = db
         self.scale = scale
-        self.threshold = jack_mixer_c.threshold_create(db, scale)
         self.text = "%.0f" % math.fabs(db)
-        if not self.threshold:
-            raise "creating new threshold failed"
-
-    def calculate_coefficients(self, prev):
-        jack_mixer_c.threshold_calculate_coefficients(self.threshold, prev.threshold)
-
-    def db_to_scale(self, db):
-        return jack_mixer_c.threshold_db_to_scale(self.threshold, db)
-
-    def scale_to_db(self, scale):
-        return jack_mixer_c.threshold_scale_to_db(self.threshold, scale)
-
-    def __del__(self):
-        if self.threshold:
-            jack_mixer_c.threshold_destroy(self.threshold)
 
 class base:
     '''Scale abstraction, various scale implementation derive from this class'''
     def __init__(self, scale_id, description):
         self.marks = []
-        self.thresholds = []
         self.scale_id = scale_id
         self.description = description
         self.scale = jack_mixer_c.scale_create()
 
-    def add_threshold(self, db, scale, mark):
-        thr = threshold(db, scale)
-        #print repr(thr.threshold)
-        self.thresholds.append(thr)
-        jack_mixer_c.scale_add_threshold(self.scale, thr.threshold)
-        if mark:
-            self.marks.append(thr)
+    def add_threshold(self, db, scale, is_mark):
+        jack_mixer_c.scale_add_threshold(self.scale, db, scale)
+        if is_mark:
+            self.marks.append(mark(db, scale))
 
     def calculate_coefficients(self):
         jack_mixer_c.scale_calculate_coefficients(self.scale)
@@ -74,8 +54,7 @@ class base:
         return jack_mixer_c.scale_scale_to_db(self.scale, scale)
 
     def add_mark(self, db):
-        mark = threshold(db, -1.0)
-        self.marks.append(mark)
+        self.marks.append(mark(db, -1.0))
 
     def get_marks(self):
         return self.marks