shithub: libsamplerate

Download patch

ref: 71b0a3b5e526fc6fa6ab955cefbd444110408751
parent: 26d92c369394bcd0b0cea488890edce1a0d757d5
author: evpobr <evpobr@gmail.com>
date: Wed Nov 11 10:28:53 EST 2020

Move SRC_STATE function pointers to virtual table

--- a/src/common.h
+++ b/src/common.h
@@ -96,18 +96,8 @@
 	SRC_ERR_MAX_ERROR
 } SRC_ERROR ;
 
-struct SRC_STATE_tag
-{	double	last_ratio, last_position ;
-
-	SRC_ERROR	error ;
-	int		channels ;
-
-	/* SRC_MODE_PROCESS or SRC_MODE_CALLBACK */
-	enum SRC_MODE	mode ;
-
-	/* Pointer to data to converter specific data. */
-	void	*private_data ;
-
+typedef struct SRC_STATE_VT_tag
+{
 	/* Varispeed process function. */
 	SRC_ERROR		(*vari_process) (SRC_STATE *state, SRC_DATA *data) ;
 
@@ -122,6 +112,22 @@
 
 	/* State private_data close. */
 	void			(*close) (SRC_STATE *state) ;
+} SRC_STATE_VT ;
+
+struct SRC_STATE_tag
+{
+	SRC_STATE_VT *vt ;
+
+	double	last_ratio, last_position ;
+
+	SRC_ERROR	error ;
+	int		channels ;
+
+	/* SRC_MODE_PROCESS or SRC_MODE_CALLBACK */
+	enum SRC_MODE	mode ;
+
+	/* Pointer to data to converter specific data. */
+	void	*private_data ;
 
 	/* Data specific to SRC_MODE_CALLBACK. */
 	src_callback_t	callback_func ;
--- a/src/samplerate.c
+++ b/src/samplerate.c
@@ -43,7 +43,7 @@
 	SRC_STATE *orig_state = orig ;
 	memcpy (state, orig_state, sizeof (SRC_STATE)) ;
 
-	if ((copy_error = orig_state->copy (orig_state, state)) != SRC_ERR_NO_ERROR)
+	if ((copy_error = orig_state->vt->copy (orig_state, state)) != SRC_ERR_NO_ERROR)
 	{	if (error)
 			*error = copy_error ;
 		free (state) ;
@@ -82,11 +82,11 @@
 src_delete (SRC_STATE *state)
 {
 	if (state)
-	{	if (state->close)
-			state->close (state) ;
+	{
+		state->vt->close (state) ;
 		memset (state, 0, sizeof (SRC_STATE)) ;
 		free (state) ;
-		} ;
+	}
 
 	return NULL ;
 } /* src_state */
@@ -98,8 +98,6 @@
 
 	if (state == NULL)
 		return SRC_ERR_BAD_STATE ;
-	if (state->vari_process == NULL || state->const_process == NULL)
-		return SRC_ERR_BAD_PROC_PTR ;
 
 	if (state->mode != SRC_MODE_PROCESS)
 		return SRC_ERR_BAD_MODE ;
@@ -147,9 +145,9 @@
 
 	/* Now process. */
 	if (fabs (state->last_ratio - data->src_ratio) < 1e-15)
-		error = state->const_process (state, data) ;
+		error = state->vt->const_process (state, data) ;
 	else
-		error = state->vari_process (state, data) ;
+		error = state->vt->vari_process (state, data) ;
 
 	return error ;
 } /* src_process */
@@ -254,8 +252,6 @@
 {
 	if (state == NULL)
 		return SRC_ERR_BAD_STATE ;
-	if (state->vari_process == NULL || state->const_process == NULL)
-		return SRC_ERR_BAD_PROC_PTR ;
 
 	if (is_bad_src_ratio (new_ratio))
 		return SRC_ERR_BAD_SRC_RATIO ;
@@ -270,8 +266,6 @@
 {
 	if (state == NULL)
 		return -SRC_ERR_BAD_STATE ;
-	if (state->vari_process == NULL || state->const_process == NULL)
-		return -SRC_ERR_BAD_PROC_PTR ;
 
 	return state->channels ;
 } /* src_get_channels */
@@ -282,8 +276,7 @@
 	if (state == NULL)
 		return SRC_ERR_BAD_STATE ;
 
-	if (state->reset != NULL)
-		state->reset (state) ;
+	state->vt->reset (state) ;
 
 	state->last_position = 0.0 ;
 	state->last_ratio = 0.0 ;
--- a/src/src_linear.c
+++ b/src/src_linear.c
@@ -35,6 +35,15 @@
 	float	*last_value ;
 } LINEAR_DATA ;
 
+static SRC_STATE_VT linear_state_vt =
+{
+	linear_vari_process,
+	linear_vari_process,
+	linear_reset,
+	linear_copy,
+	linear_close
+} ;
+
 /*----------------------------------------------------------------------------------------
 */
 
@@ -203,11 +212,7 @@
 		return NULL ;
 	}
 
-	state->const_process = linear_vari_process ;
-	state->vari_process = linear_vari_process ;
-	state->reset = linear_reset ;
-	state->copy = linear_copy ;
-	state->close = linear_close ;
+	state->vt = &linear_state_vt ;
 
 	linear_reset (state) ;
 
--- a/src/src_sinc.c
+++ b/src/src_sinc.c
@@ -69,6 +69,51 @@
 static SRC_ERROR sinc_copy (SRC_STATE *from, SRC_STATE *to) ;
 static void sinc_close (SRC_STATE *state) ;
 
+static SRC_STATE_VT sinc_multichan_state_vt =
+{
+	sinc_multichan_vari_process,
+	sinc_multichan_vari_process,
+	sinc_reset,
+	sinc_copy,
+	sinc_close
+} ;
+
+static SRC_STATE_VT sinc_hex_state_vt =
+{
+	sinc_hex_vari_process,
+	sinc_hex_vari_process,
+	sinc_reset,
+	sinc_copy,
+	sinc_close
+} ;
+
+static SRC_STATE_VT sinc_quad_state_vt =
+{
+	sinc_quad_vari_process,
+	sinc_quad_vari_process,
+	sinc_reset,
+	sinc_copy,
+	sinc_close
+} ;
+
+static SRC_STATE_VT sinc_stereo_state_vt =
+{
+	sinc_stereo_vari_process,
+	sinc_stereo_vari_process,
+	sinc_reset,
+	sinc_copy,
+	sinc_close
+} ;
+
+static SRC_STATE_VT sinc_mono_state_vt =
+{
+	sinc_mono_vari_process,
+	sinc_mono_vari_process,
+	sinc_reset,
+	sinc_copy,
+	sinc_close
+} ;
+
 static inline increment_t
 double_to_fp (double x)
 {	return (increment_t) (lrint ((x) * FP_ONE)) ;
@@ -225,33 +270,15 @@
 	state->mode = SRC_MODE_PROCESS ;
 
 	if (state->channels == 1)
-	{
-		state->const_process = sinc_mono_vari_process ;
-		state->vari_process = sinc_mono_vari_process ;
-	}
+		state->vt = &sinc_mono_state_vt ;
 	else if (state->channels == 2)
-	{
-		state->const_process = sinc_stereo_vari_process ;
-		state->vari_process = sinc_stereo_vari_process ;
-	}
+		state->vt = &sinc_stereo_state_vt ;
 	else if (state->channels == 4)
-	{
-		state->const_process = sinc_quad_vari_process ;
-		state->vari_process = sinc_quad_vari_process ;
-	}
+		state->vt = &sinc_quad_state_vt ;
 	else if (state->channels == 6)
-	{
-		state->const_process = sinc_hex_vari_process ;
-		state->vari_process = sinc_hex_vari_process ;
-	}
+		state->vt = &sinc_hex_state_vt ;
 	else
-	{
-		state->const_process = sinc_multichan_vari_process ;
-		state->vari_process = sinc_multichan_vari_process ;
-	}
-	state->reset = sinc_reset ;
-	state->copy = sinc_copy ;
-	state->close = sinc_close ;
+		state->vt = &sinc_multichan_state_vt ;
 
 	state->private_data = sinc_filter_new (converter_type, state->channels) ;
 	if (!state->private_data)
--- a/src/src_zoh.c
+++ b/src/src_zoh.c
@@ -33,6 +33,15 @@
 	float	*last_value ;
 } ZOH_DATA ;
 
+static SRC_STATE_VT zoh_state_vt =
+{
+	zoh_vari_process,
+	zoh_vari_process,
+	zoh_reset,
+	zoh_copy,
+	zoh_close
+} ;
+
 /*----------------------------------------------------------------------------------------
 */
 
@@ -194,11 +203,7 @@
 		return NULL ;
 	}
 
-	state->const_process = zoh_vari_process ;
-	state->vari_process = zoh_vari_process ;
-	state->reset = zoh_reset ;
-	state->copy = zoh_copy ;
-	state->close = zoh_close ;
+	state->vt = &zoh_state_vt ;
 
 	zoh_reset (state) ;