r3907 - trunk/bse



Author: stw
Date: 2006-09-27 07:08:42 -0400 (Wed, 27 Sep 2006)
New Revision: 3907

Modified:
   trunk/bse/ChangeLog
   trunk/bse/bsedatahandle-resample.cc
Log:
Wed Sep 27 13:04:49 2006  Stefan Westerfeld  <stefan space twc de>

	* bsedatahandle-resample.cc: Fixes and cleanups. Upsampling Datahandle
	class Members are now prefixed with m_, all code that could possibly
	be moved into the class - such as the C vtable - is now present as
	static functions within the class, the destructor was made private
	- because reference counting is supposed to be used.
	Finally the class uses C style inheritance, by embedding m_dhandle.


Modified: trunk/bse/ChangeLog
===================================================================
--- trunk/bse/ChangeLog	2006-09-27 10:10:32 UTC (rev 3906)
+++ trunk/bse/ChangeLog	2006-09-27 11:08:42 UTC (rev 3907)
@@ -1,3 +1,12 @@
+Wed Sep 27 13:04:49 2006  Stefan Westerfeld  <stefan space twc de>
+
+	* bsedatahandle-resample.cc: Fixes and cleanups. Upsampling Datahandle
+	class Members are now prefixed with m_, all code that could possibly
+	be moved into the class - such as the C vtable - is now present as
+	static functions within the class, the destructor was made private
+	- because reference counting is supposed to be used.
+	Finally the class uses C style inheritance, by embedding m_dhandle.
+
 Wed Sep 27 11:30:27 2006  Stefan Westerfeld  <stefan space twc de>
 
 	* tests/Makefile.am:

Modified: trunk/bse/bsedatahandle-resample.cc
===================================================================
--- trunk/bse/bsedatahandle-resample.cc	2006-09-27 10:10:32 UTC (rev 3906)
+++ trunk/bse/bsedatahandle-resample.cc	2006-09-27 11:08:42 UTC (rev 3907)
@@ -26,95 +26,108 @@
 using Resampler::Resampler2;
 using std::vector;
 
-class DataHandleUpsample2 : public GslDataHandle,
-			    public Sfi::GNewable /* 0 initialization */
+class DataHandleUpsample2
 {
-  GslDataHandle	       *src_handle;
-  int                   precision_bits;
-  vector<Resampler2 *>  upsamplers;
-  int64			pcm_frame;
-  vector<float>		pcm_data;
-  int64			frame_size;
-  int64                 filter_delay;
-  int64                 filter_order;
+  GslDataHandle         m_dhandle;
+  GslDataHandle	       *m_src_handle;
+  int                   m_precision_bits;
+  vector<Resampler2 *>  m_upsamplers;
+  int64			m_pcm_frame;
+  vector<float>		m_pcm_data;
+  int64			m_frame_size;
+  int64                 m_filter_delay;
+  int64                 m_filter_order;
+  bool   		m_init_ok;
+
+  /* private destructor: (use reference counting instead) */
+  ~DataHandleUpsample2()
+  {
+    if (m_init_ok)
+      {
+	gsl_data_handle_unref (m_src_handle);
+	gsl_data_handle_common_free (&m_dhandle);
+      }
+  }
 public:
-  bool   		init_ok;
-
   DataHandleUpsample2 (GslDataHandle *src_handle,
                        int            precision_bits) :
-    src_handle (src_handle),
-    precision_bits (precision_bits),
-    init_ok (false)
+    m_src_handle (src_handle),
+    m_precision_bits (precision_bits),
+    m_pcm_frame (0),			  // unnecessary, but makes debugging easier - just in case
+    m_frame_size (0),			  // unnecessary, but makes debugging easier - just in case
+    m_filter_delay (0),			  // unnecessary, but makes debugging easier - just in case
+    m_filter_order (0),			  // unnecessary, but makes debugging easier - just in case
+    m_init_ok (false)
   {
     g_return_if_fail (src_handle != NULL);
-  
-    init_ok = gsl_data_handle_common_init (this, NULL);
-    if (init_ok)
+ 
+    memset (&m_dhandle, 0, sizeof (m_dhandle));
+
+    m_init_ok = gsl_data_handle_common_init (&m_dhandle, NULL);
+    if (m_init_ok)
       {
-	name = g_strconcat (src_handle->name, "// #upsample2 /", NULL);
-	src_handle = gsl_data_handle_ref (src_handle);
+	m_dhandle.name = g_strconcat (m_src_handle->name, "// #upsample2 /", NULL);
+	gsl_data_handle_ref (m_src_handle);
       }
   }
-  ~DataHandleUpsample2()
-  {
-    gsl_data_handle_unref (src_handle);
-    gsl_data_handle_common_free (this);
-  }
   BseErrorType
   open (GslDataHandleSetup *setup)
   {
-    BseErrorType error = gsl_data_handle_open (src_handle);
+    BseErrorType error = gsl_data_handle_open (m_src_handle);
     if (error != BSE_ERROR_NONE)
       return error;
 
-    *setup = src_handle->setup; /* copies setup.xinfos by pointer */
+    /* !not! m_dhandle.setup; the framework magically ensures that *m_dhandle.setup
+     * is initialized by whatever we write into *setup if open is successful
+     */
+    *setup = m_src_handle->setup; /* copies setup.xinfos by pointer */
     setup->mix_freq *= 2.0;
     setup->n_values *= 2;
 
-    frame_size = 1024 * setup->n_channels;
-    pcm_frame = -2;
-    pcm_data.resize (frame_size);
+    m_frame_size = 1024 * setup->n_channels;
+    m_pcm_frame = -2;
+    m_pcm_data.resize (m_frame_size);
 
     for (guint i = 0; i < setup->n_channels; i++)
       {
-	BseResampler2Precision precision = static_cast<BseResampler2Precision> (precision_bits);
+	BseResampler2Precision precision = static_cast<BseResampler2Precision> (m_precision_bits);
 	Resampler2 *resampler = Resampler2::create (BSE_RESAMPLER2_MODE_UPSAMPLE, precision);
 	g_assert (resampler); /* FIXME: better error handling */
 
-	upsamplers.push_back (resampler);
-	filter_order = resampler->order();
+	m_upsamplers.push_back (resampler);
+	m_filter_order = resampler->order();
 
-	g_assert (filter_order % 2 == 0);
-	filter_delay = filter_order / 2 + 1;
+	g_assert (m_filter_order % 2 == 0);
+	m_filter_delay = m_filter_order / 2 + 1;
       }
     return BSE_ERROR_NONE;
   }
   void
   close()
   {
-    for (guint i = 0; i < setup.n_channels; i++)
-      delete upsamplers[i];
+    for (guint i = 0; i < m_dhandle.setup.n_channels; i++)
+      delete m_upsamplers[i];
 
-    upsamplers.clear();
-    pcm_data.clear();
+    m_upsamplers.clear();
+    m_pcm_data.clear();
     
-    setup.xinfos = NULL;	/* cleanup pointer reference */
-    gsl_data_handle_close (src_handle);
+    m_dhandle.setup.xinfos = NULL;	/* cleanup pointer reference */
+    gsl_data_handle_close (m_src_handle);
   }
   int64
   src_read (int64   voffset,
 	    int64   n_values,
 	    gfloat *values)
   {
-    voffset += filter_delay * setup.n_channels;	/* compensate filter delay */
+    voffset += m_filter_delay * m_dhandle.setup.n_channels;	/* compensate filter delay */
 
     int64 left = n_values;
     do
       {
 	int64 l;
-	if (voffset >= 0 && voffset < src_handle->setup.n_values)
+	if (voffset >= 0 && voffset < m_src_handle->setup.n_values)
 	  {
-	    l = gsl_data_handle_read (src_handle, voffset, std::min (left, src_handle->setup.n_values - voffset), values);
+	    l = gsl_data_handle_read (m_src_handle, voffset, std::min (left, m_src_handle->setup.n_values - voffset), values);
 	    if (l < 0)
 	      return l;	/* pass on errors */
 	  }
@@ -141,7 +154,7 @@
                 float *dest,
                 int64  n_values)
   {
-    const int64 n_channels = setup.n_channels;
+    const int64 n_channels = m_dhandle.setup.n_channels;
 
     for (int64 ch = 0; ch < n_channels; ch++)
       for (int64 v = ch; v < n_values; v += n_channels)
@@ -152,7 +165,7 @@
               float *dest,
               int64  n_values)
   {
-    const int64 n_channels = setup.n_channels;
+    const int64 n_channels = m_dhandle.setup.n_channels;
 
     for (int64 ch = 0; ch < n_channels; ch++)
       for (int64 v = ch; v < n_values; v += n_channels)
@@ -161,25 +174,25 @@
   int64
   prepare_filter_history (int64 frame)
   {
-    const int64 n_channels = setup.n_channels;
-    const int64 n_input_samples = filter_order;
+    const int64 n_channels = m_dhandle.setup.n_channels;
+    const int64 n_input_samples = m_filter_order;
 
     float input_interleaved[n_input_samples * n_channels];
     float input[n_input_samples * n_channels];
 
-    int64 l = src_read (frame * frame_size / 2 - n_input_samples * n_channels,
+    int64 l = src_read (frame * m_frame_size / 2 - n_input_samples * n_channels,
 	                n_input_samples * n_channels, input_interleaved);
     if (l < 0)
       return l; /* pass on errors */
 
-    deinterleave (input_interleaved, input, n_input_samples * setup.n_channels);
+    deinterleave (input_interleaved, input, n_input_samples * m_dhandle.setup.n_channels);
 
-    for (guint ch = 0; ch < setup.n_channels; ch++)
+    for (guint ch = 0; ch < m_dhandle.setup.n_channels; ch++)
       {
 	/* we don't need the output, this is just for filling the filter history */
 	float output[n_input_samples * 2];
 
-	upsamplers[ch]->process_block (input + ch * n_input_samples, n_input_samples, output);
+	m_upsamplers[ch]->process_block (input + ch * n_input_samples, n_input_samples, output);
       }
     return 1;
   }
@@ -190,32 +203,32 @@
      * if we're seeking (not reading data linearily), we need to reinitialize
      * the filter history with new values
      */
-    if (frame != pcm_frame + 1)
+    if (frame != m_pcm_frame + 1)
       {
 	int64 l = prepare_filter_history (frame);
 	if (l < 0)
 	  return l; /* pass on errors */
       }
 
-    float input_interleaved[frame_size / 2];
-    float input[frame_size / 2];
-    float output[frame_size];
+    float input_interleaved[m_frame_size / 2];
+    float input[m_frame_size / 2];
+    float output[m_frame_size];
 
-    int64 l = src_read (frame * frame_size / 2, frame_size / 2, input_interleaved);
+    int64 l = src_read (frame * m_frame_size / 2, m_frame_size / 2, input_interleaved);
     if (l < 0)
       return l; /* pass on errors */
 
-    deinterleave (input_interleaved, input, frame_size / 2);
-    for (guint ch = 0; ch < setup.n_channels; ch++)
+    deinterleave (input_interleaved, input, m_frame_size / 2);
+    for (guint ch = 0; ch < m_dhandle.setup.n_channels; ch++)
       {
-	const int64 output_per_channel = frame_size / setup.n_channels;
+	const int64 output_per_channel = m_frame_size / m_dhandle.setup.n_channels;
 	const int64 input_per_channel = output_per_channel / 2;
 
-	upsamplers[ch]->process_block (input + ch * input_per_channel, input_per_channel, output + ch * output_per_channel);
+	m_upsamplers[ch]->process_block (input + ch * input_per_channel, input_per_channel, output + ch * output_per_channel);
       }
-    interleave (output, &pcm_data[0], frame_size);
+    interleave (output, &m_pcm_data[0], m_frame_size);
 
-    pcm_frame = frame;
+    m_pcm_frame = frame;
     return 1;
   }
   int64
@@ -223,77 +236,88 @@
 	int64  n_values,
 	float *values)
   {
-    int64 frame = voffset / pcm_data.size();
-    if (frame != pcm_frame)
+    int64 frame = voffset / m_pcm_data.size();
+    if (frame != m_pcm_frame)
       {
 	int64 l = read_frame (frame);
 	if (l < 0)
 	  return l;
       }
-    g_assert (pcm_frame == frame);
+    g_assert (m_pcm_frame == frame);
 
-    voffset -= pcm_frame * frame_size;
+    voffset -= m_pcm_frame * m_frame_size;
     g_assert (voffset >= 0);
 
-    n_values = std::min (n_values, frame_size - voffset);
+    n_values = std::min (n_values, m_frame_size - voffset);
     for (int64 i = 0; i < n_values; i++)
-      values[i] = pcm_data[voffset + i];
+      values[i] = m_pcm_data[voffset + i];
 
     return n_values;
   }
+
+private:
+/* for the "C" API (vtable) */
+  static DataHandleUpsample2*
+  dh_cast (GslDataHandle *dhandle)
+  {
+    return reinterpret_cast<DataHandleUpsample2 *> (dhandle);
+  }
+  static BseErrorType
+  dh_open (GslDataHandle *dhandle, GslDataHandleSetup *setup)
+  {
+    return dh_cast (dhandle)->open (setup);
+  }
+  static void
+  dh_close (GslDataHandle *dhandle)
+  {
+    dh_cast (dhandle)->close();
+  }
+  static void
+  dh_destroy (GslDataHandle *dhandle)
+  {
+    delete dh_cast (dhandle);
+  }
+  static int64
+  dh_read (GslDataHandle *dhandle,
+	   int64          voffset,
+	   int64          n_values,
+	   gfloat        *values)
+  {
+    return dh_cast (dhandle)->read (voffset, n_values, values);
+  }
+
+public:
+  static GslDataHandle*
+  dh_create (GslDataHandle *src_handle,
+	     int            precision_bits)
+  {
+    static GslDataHandleFuncs dh_vtable =
+    {
+      dh_open,
+      dh_read,
+      dh_close,
+      NULL,
+      dh_destroy,
+    };
+    DataHandleUpsample2 *dhandle = new DataHandleUpsample2 (src_handle, precision_bits);
+    if (dhandle->m_init_ok)
+      {
+	dhandle->m_dhandle.vtable = &dh_vtable;
+	return &dhandle->m_dhandle;
+      }
+    else
+      {
+	delete dhandle;
+	return NULL;
+      }
+  }
 };
 
 } // Bse
 
-using namespace Bse;
-
-static BseErrorType
-dh_upsample2_open (GslDataHandle *dhandle, GslDataHandleSetup *setup)
+extern "C" GslDataHandle*
+bse_data_handle_new_upsample2 (GslDataHandle *src_handle,
+                               int            precision_bits)
 {
-  return reinterpret_cast<DataHandleUpsample2 *> (dhandle)->open (setup);
+  return Bse::DataHandleUpsample2::dh_create (src_handle, precision_bits);
 }
-
-static void
-dh_upsample2_close (GslDataHandle *dhandle)
-{
-  reinterpret_cast<DataHandleUpsample2 *> (dhandle)->close();
-}
-
-
-static void
-dh_upsample2_destroy (GslDataHandle *dhandle)
-{
-  delete reinterpret_cast<DataHandleUpsample2 *> (dhandle);
-}
-
-static int64
-dh_upsample2_read (GslDataHandle *dhandle,
-		   int64          voffset,
-		   int64          n_values,
-		   gfloat        *values)
-{
-  return reinterpret_cast<DataHandleUpsample2 *> (dhandle)->read (voffset, n_values, values);
-}
-
-GslDataHandle*
-bse_data_handle_new_upsample2 (GslDataHandle *src_handle, int precision_bits)
-{
-  static GslDataHandleFuncs dh_upsample2_vtable = {
-    dh_upsample2_open,
-    dh_upsample2_read,
-    dh_upsample2_close,
-    NULL,
-    dh_upsample2_destroy,
-  };
-  DataHandleUpsample2 *dhandle = new DataHandleUpsample2 (src_handle, precision_bits);
-  if (dhandle->init_ok)
-    {
-      dhandle->vtable = &dh_upsample2_vtable;
-      return dhandle;
-    }
-  else
-    {
-      delete dhandle;
-      return NULL;
-    }
-}




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]