shithub: libsamplerate

Download patch

ref: 32a6ce8ddcf4e1b18bb42094d12601d2cdcffe93
parent: 94966fc068cd2d38b92eab5769656e2434deecd7
parent: 1bb5e8adc3cd6686b492a369b83cb9eb440b42ef
author: Erik de Castro Lopo <erikd@mega-nerd.com>
date: Sat Dec 13 11:00:42 EST 2008

Merge from -optim branch.

--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2008-12-13  Erik de Castro Lopo  <erikd AT mega-nerd DOT com>
+
+    * tests/multichan_throughput_test.c
+    Do throughput test on all three SINC based converters.
+
+    * src/src_sinc.c
+    Rejig converter so filter coefficients are calculated once per frame and
+    special case channel counts of 1, 2 and 4.
+
 2008-12-12  Erik de Castro Lopo  <erikd AT mega-nerd DOT com>
 
     *  src/samplerate.c examples/audio_out.c tests/*.c
@@ -8,6 +17,9 @@
     Fix a potential memory leak. Thanks to Peter G. Vavaroutsos for point this
     out.
 
+    * tests/multi_channel_test.c
+    Robustify test.
+
 2008-12-11  Erik de Castro Lopo  <erikd AT mega-nerd DOT com>
 
     * reconfigure.mk autogen.sh
@@ -15,6 +27,9 @@
 
     * configure.ac
     Various updates.
+
+    * tests/multichan_throughput_test.c tests/Makefile.am
+    Add new test and hook into build.
 
 2008-11-11  Erik de Castro Lopo  <erikd AT mega-nerd DOT com>
 
--- a/src/src_sinc.c
+++ b/src/src_sinc.c
@@ -65,11 +65,15 @@
 	coeff_t const	*coeffs ;
 
 	int		b_current, b_end, b_real_end, b_len ;
-	float	buffer [1] ;
+
+	/* C99 struct flexible array. */
+	float	buffer [] ;
 } SINC_FILTER ;
 
 static int sinc_multichan_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data) ;
-static int sinc_1chan_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data) ;
+static int sinc_quad_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data) ;
+static int sinc_stereo_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data) ;
+static int sinc_mono_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data) ;
 
 static void prepare_data (SINC_FILTER *filter, SRC_DATA *data, int half_filter_chan_len) ;
 
@@ -164,10 +168,20 @@
 	temp_filter.channels = psrc->channels ;
 
 	if (psrc->channels == 1)
-	{	psrc->const_process = sinc_1chan_vari_process ;
-		psrc->vari_process = sinc_1chan_vari_process ;
+	{	psrc->const_process = sinc_mono_vari_process ;
+		psrc->vari_process = sinc_mono_vari_process ;
 		}
 	else
+	if (psrc->channels == 2)
+	{	psrc->const_process = sinc_stereo_vari_process ;
+		psrc->vari_process = sinc_stereo_vari_process ;
+		}
+	else
+	if (psrc->channels == 4)
+	{	psrc->const_process = sinc_quad_vari_process ;
+		psrc->vari_process = sinc_quad_vari_process ;
+		}
+	else
 	{	psrc->const_process = sinc_multichan_vari_process ;
 		psrc->vari_process = sinc_multichan_vari_process ;
 		} ;
@@ -300,9 +314,102 @@
 	return (left + right) ;
 } /* calc_output_single */
 
-static inline double
-calc_output_multi (SINC_FILTER *filter, increment_t increment, increment_t start_filter_index, int ch)
-{	double		fraction, left, right, icoeff ;
+static int
+sinc_mono_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data)
+{	SINC_FILTER *filter ;
+	double		input_index, src_ratio, count, float_increment, terminate, rem ;
+	increment_t	increment, start_filter_index ;
+	int			half_filter_chan_len, samples_in_hand ;
+
+	if (psrc->private_data == NULL)
+		return SRC_ERR_NO_PRIVATE ;
+
+	filter = (SINC_FILTER*) psrc->private_data ;
+
+	/* If there is not a problem, this will be optimised out. */
+	if (sizeof (filter->buffer [0]) != sizeof (data->data_in [0]))
+		return SRC_ERR_SIZE_INCOMPATIBILITY ;
+
+	filter->in_count = data->input_frames * filter->channels ;
+	filter->out_count = data->output_frames * filter->channels ;
+	filter->in_used = filter->out_gen = 0 ;
+
+	src_ratio = psrc->last_ratio ;
+
+	/* Check the sample rate ratio wrt the buffer len. */
+	count = (filter->coeff_half_len + 2.0) / filter->index_inc ;
+	if (MIN (psrc->last_ratio, data->src_ratio) < 1.0)
+		count /= MIN (psrc->last_ratio, data->src_ratio) ;
+
+	/* Maximum coefficientson either side of center point. */
+	half_filter_chan_len = filter->channels * (lrint (count) + 1) ;
+
+	input_index = psrc->last_position ;
+	float_increment = filter->index_inc ;
+
+	rem = fmod_one (input_index) ;
+	filter->b_current = (filter->b_current + filter->channels * lrint (input_index - rem)) % filter->b_len ;
+	input_index = rem ;
+
+	terminate = 1.0 / src_ratio + 1e-20 ;
+
+	/* Main processing loop. */
+	while (filter->out_gen < filter->out_count)
+	{
+		/* Need to reload buffer? */
+		samples_in_hand = (filter->b_end - filter->b_current + filter->b_len) % filter->b_len ;
+
+		if (samples_in_hand <= half_filter_chan_len)
+		{	prepare_data (filter, data, half_filter_chan_len) ;
+
+			samples_in_hand = (filter->b_end - filter->b_current + filter->b_len) % filter->b_len ;
+			if (samples_in_hand <= half_filter_chan_len)
+				break ;
+			} ;
+
+		/* This is the termination condition. */
+		if (filter->b_real_end >= 0)
+		{	if (filter->b_current + input_index + terminate >= filter->b_real_end)
+				break ;
+			} ;
+
+		if (filter->out_count > 0 && fabs (psrc->last_ratio - data->src_ratio) > 1e-10)
+			src_ratio = psrc->last_ratio + filter->out_gen * (data->src_ratio - psrc->last_ratio) / filter->out_count ;
+
+		float_increment = filter->index_inc * 1.0 ;
+		if (src_ratio < 1.0)
+			float_increment = filter->index_inc * src_ratio ;
+
+		increment = double_to_fp (float_increment) ;
+
+		start_filter_index = double_to_fp (input_index * float_increment) ;
+
+		data->data_out [filter->out_gen] = (float) ((float_increment / filter->index_inc) *
+										calc_output_single (filter, increment, start_filter_index)) ;
+		filter->out_gen ++ ;
+
+		/* Figure out the next index. */
+		input_index += 1.0 / src_ratio ;
+		rem = fmod_one (input_index) ;
+
+		filter->b_current = (filter->b_current + filter->channels * lrint (input_index - rem)) % filter->b_len ;
+		input_index = rem ;
+		} ;
+
+	psrc->last_position = input_index ;
+
+	/* Save current ratio rather then target ratio. */
+	psrc->last_ratio = src_ratio ;
+
+	data->input_frames_used = filter->in_used / filter->channels ;
+	data->output_frames_gen = filter->out_gen / filter->channels ;
+
+	return SRC_ERR_NO_ERROR ;
+} /* sinc_mono_vari_process */
+
+static inline void
+calc_output_stereo (SINC_FILTER *filter, increment_t increment, increment_t start_filter_index, double scale, float * output)
+{	double		fraction, left [2], right [2], icoeff ;
 	increment_t	filter_index, max_filter_index ;
 	int			data_index, coeff_count, indx ;
 
@@ -313,9 +420,9 @@
 	filter_index = start_filter_index ;
 	coeff_count = (max_filter_index - filter_index) / increment ;
 	filter_index = filter_index + coeff_count * increment ;
-	data_index = filter->b_current - filter->channels * coeff_count + ch ;
+	data_index = filter->b_current - filter->channels * coeff_count ;
 
-	left = 0.0 ;
+	left [0] = left [1] = 0.0 ;
 	do
 	{	fraction = fp_to_double (filter_index) ;
 		indx = fp_to_int (filter_index) ;
@@ -322,10 +429,11 @@
 
 		icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
 
-		left += icoeff * filter->buffer [data_index] ;
+		left [0] += icoeff * filter->buffer [data_index] ;
+		left [1] += icoeff * filter->buffer [data_index + 1] ;
 
 		filter_index -= increment ;
-		data_index = data_index + filter->channels ;
+		data_index = data_index + 2 ;
 		}
 	while (filter_index >= MAKE_INCREMENT_T (0)) ;
 
@@ -333,9 +441,9 @@
 	filter_index = increment - start_filter_index ;
 	coeff_count = (max_filter_index - filter_index) / increment ;
 	filter_index = filter_index + coeff_count * increment ;
-	data_index = filter->b_current + filter->channels * (1 + coeff_count) + ch ;
+	data_index = filter->b_current + filter->channels * (1 + coeff_count) ;
 
-	right = 0.0 ;
+	right [0] = right [1] = 0.0 ;
 	do
 	{	fraction = fp_to_double (filter_index) ;
 		indx = fp_to_int (filter_index) ;
@@ -342,7 +450,8 @@
 
 		icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
 
-		right += icoeff * filter->buffer [data_index] ;
+		right [0] += icoeff * filter->buffer [data_index] ;
+		right [1] += icoeff * filter->buffer [data_index + 1] ;
 
 		filter_index -= increment ;
 		data_index = data_index - filter->channels ;
@@ -349,11 +458,12 @@
 		}
 	while (filter_index > MAKE_INCREMENT_T (0)) ;
 
-	return (left + right) ;
-} /* calc_output_multi */
+	output [0] = scale * (left [0] + right [0]) ;
+	output [1] = scale * (left [1] + right [1]) ;
+} /* calc_output_stereo */
 
 static int
-sinc_1chan_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data)
+sinc_stereo_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data)
 {	SINC_FILTER *filter ;
 	double		input_index, src_ratio, count, float_increment, terminate, rem ;
 	increment_t	increment, start_filter_index ;
@@ -422,9 +532,8 @@
 
 		start_filter_index = double_to_fp (input_index * float_increment) ;
 
-		data->data_out [filter->out_gen] = (float) ((float_increment / filter->index_inc) *
-										calc_output_single (filter, increment, start_filter_index)) ;
-		filter->out_gen ++ ;
+		calc_output_stereo (filter, increment, start_filter_index, float_increment / filter->index_inc, data->data_out + filter->out_gen) ;
+		filter->out_gen += 2 ;
 
 		/* Figure out the next index. */
 		input_index += 1.0 / src_ratio ;
@@ -443,14 +552,75 @@
 	data->output_frames_gen = filter->out_gen / filter->channels ;
 
 	return SRC_ERR_NO_ERROR ;
-} /* sinc_1chan_vari_process */
+} /* sinc_stereo_vari_process */
 
+static inline void
+calc_output_quad (SINC_FILTER *filter, increment_t increment, increment_t start_filter_index, double scale, float * output)
+{	double		fraction, left [4], right [4], icoeff ;
+	increment_t	filter_index, max_filter_index ;
+	int			data_index, coeff_count, indx ;
+
+	/* Convert input parameters into fixed point. */
+	max_filter_index = int_to_fp (filter->coeff_half_len) ;
+
+	/* First apply the left half of the filter. */
+	filter_index = start_filter_index ;
+	coeff_count = (max_filter_index - filter_index) / increment ;
+	filter_index = filter_index + coeff_count * increment ;
+	data_index = filter->b_current - filter->channels * coeff_count ;
+
+	left [0] = left [1] = left [2] = left [3] = 0.0 ;
+	do
+	{	fraction = fp_to_double (filter_index) ;
+		indx = fp_to_int (filter_index) ;
+
+		icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
+
+		left [0] += icoeff * filter->buffer [data_index] ;
+		left [1] += icoeff * filter->buffer [data_index + 1] ;
+		left [2] += icoeff * filter->buffer [data_index + 2] ;
+		left [3] += icoeff * filter->buffer [data_index + 3] ;
+
+		filter_index -= increment ;
+		data_index = data_index + 4 ;
+		}
+	while (filter_index >= MAKE_INCREMENT_T (0)) ;
+
+	/* Now apply the right half of the filter. */
+	filter_index = increment - start_filter_index ;
+	coeff_count = (max_filter_index - filter_index) / increment ;
+	filter_index = filter_index + coeff_count * increment ;
+	data_index = filter->b_current + filter->channels * (1 + coeff_count) ;
+
+	right [0] = right [1] = right [2] = right [3] = 0.0 ;
+	do
+	{	fraction = fp_to_double (filter_index) ;
+		indx = fp_to_int (filter_index) ;
+
+		icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
+
+		right [0] += icoeff * filter->buffer [data_index] ;
+		right [1] += icoeff * filter->buffer [data_index + 1] ;
+		right [2] += icoeff * filter->buffer [data_index + 2] ;
+		right [3] += icoeff * filter->buffer [data_index + 3] ;
+
+		filter_index -= increment ;
+		data_index = data_index - filter->channels ;
+		}
+	while (filter_index > MAKE_INCREMENT_T (0)) ;
+
+	output [0] = scale * (left [0] + right [0]) ;
+	output [1] = scale * (left [1] + right [1]) ;
+	output [2] = scale * (left [2] + right [2]) ;
+	output [3] = scale * (left [3] + right [3]) ;
+} /* calc_output_quad */
+
 static int
-sinc_multichan_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data)
+sinc_quad_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data)
 {	SINC_FILTER *filter ;
 	double		input_index, src_ratio, count, float_increment, terminate, rem ;
 	increment_t	increment, start_filter_index ;
-	int			half_filter_chan_len, samples_in_hand, ch ;
+	int			half_filter_chan_len, samples_in_hand ;
 
 	if (psrc->private_data == NULL)
 		return SRC_ERR_NO_PRIVATE ;
@@ -515,11 +685,161 @@
 
 		start_filter_index = double_to_fp (input_index * float_increment) ;
 
-		for (ch = 0 ; ch < filter->channels ; ch++)
-		{	data->data_out [filter->out_gen] = (float) ((float_increment / filter->index_inc) *
-											calc_output_multi (filter, increment, start_filter_index, ch)) ;
-			filter->out_gen ++ ;
+		calc_output_quad (filter, increment, start_filter_index, float_increment / filter->index_inc, data->data_out + filter->out_gen) ;
+		filter->out_gen += 4 ;
+
+		/* Figure out the next index. */
+		input_index += 1.0 / src_ratio ;
+		rem = fmod_one (input_index) ;
+
+		filter->b_current = (filter->b_current + filter->channels * lrint (input_index - rem)) % filter->b_len ;
+		input_index = rem ;
+		} ;
+
+	psrc->last_position = input_index ;
+
+	/* Save current ratio rather then target ratio. */
+	psrc->last_ratio = src_ratio ;
+
+	data->input_frames_used = filter->in_used / filter->channels ;
+	data->output_frames_gen = filter->out_gen / filter->channels ;
+
+	return SRC_ERR_NO_ERROR ;
+} /* sinc_quad_vari_process */
+
+static inline void
+calc_output_multi (SINC_FILTER *filter, increment_t increment, increment_t start_filter_index, int channels, double scale, float * output)
+{	double		fraction, left [channels], right [channels], icoeff ;
+	increment_t	filter_index, max_filter_index ;
+	int			data_index, coeff_count, indx, ch ;
+
+	/* Convert input parameters into fixed point. */
+	max_filter_index = int_to_fp (filter->coeff_half_len) ;
+
+	/* First apply the left half of the filter. */
+	filter_index = start_filter_index ;
+	coeff_count = (max_filter_index - filter_index) / increment ;
+	filter_index = filter_index + coeff_count * increment ;
+	data_index = filter->b_current - channels * coeff_count ;
+
+	memset (left, 0, sizeof (left)) ;
+
+	do
+	{	fraction = fp_to_double (filter_index) ;
+		indx = fp_to_int (filter_index) ;
+
+		icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
+
+		ch = channels ;
+		while (-- ch >= 0)
+			left [ch] += icoeff * filter->buffer [data_index + ch] ;
+
+		filter_index -= increment ;
+		data_index = data_index + channels ;
+		}
+	while (filter_index >= MAKE_INCREMENT_T (0)) ;
+
+	/* Now apply the right half of the filter. */
+	filter_index = increment - start_filter_index ;
+	coeff_count = (max_filter_index - filter_index) / increment ;
+	filter_index = filter_index + coeff_count * increment ;
+	data_index = filter->b_current + channels * (1 + coeff_count) ;
+
+	memset (right, 0, sizeof (right)) ;
+	do
+	{	fraction = fp_to_double (filter_index) ;
+		indx = fp_to_int (filter_index) ;
+
+		icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
+
+		ch = channels ;
+		while (-- ch >= 0)
+			right [ch] += icoeff * filter->buffer [data_index + ch] ;
+
+		filter_index -= increment ;
+		data_index = data_index - channels ;
+		}
+	while (filter_index > MAKE_INCREMENT_T (0)) ;
+
+	ch = channels ;
+	while (-- ch >= 0)
+		output [ch] = scale * (left [ch] + right [ch]) ;
+
+	return ;
+} /* calc_output_multi */
+
+static int
+sinc_multichan_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data)
+{	SINC_FILTER *filter ;
+	double		input_index, src_ratio, count, float_increment, terminate, rem ;
+	increment_t	increment, start_filter_index ;
+	int			half_filter_chan_len, samples_in_hand ;
+
+	if (psrc->private_data == NULL)
+		return SRC_ERR_NO_PRIVATE ;
+
+	filter = (SINC_FILTER*) psrc->private_data ;
+
+	/* If there is not a problem, this will be optimised out. */
+	if (sizeof (filter->buffer [0]) != sizeof (data->data_in [0]))
+		return SRC_ERR_SIZE_INCOMPATIBILITY ;
+
+	filter->in_count = data->input_frames * filter->channels ;
+	filter->out_count = data->output_frames * filter->channels ;
+	filter->in_used = filter->out_gen = 0 ;
+
+	src_ratio = psrc->last_ratio ;
+
+	/* Check the sample rate ratio wrt the buffer len. */
+	count = (filter->coeff_half_len + 2.0) / filter->index_inc ;
+	if (MIN (psrc->last_ratio, data->src_ratio) < 1.0)
+		count /= MIN (psrc->last_ratio, data->src_ratio) ;
+
+	/* Maximum coefficientson either side of center point. */
+	half_filter_chan_len = filter->channels * (lrint (count) + 1) ;
+
+	input_index = psrc->last_position ;
+	float_increment = filter->index_inc ;
+
+	rem = fmod_one (input_index) ;
+	filter->b_current = (filter->b_current + filter->channels * lrint (input_index - rem)) % filter->b_len ;
+	input_index = rem ;
+
+	terminate = 1.0 / src_ratio + 1e-20 ;
+
+	/* Main processing loop. */
+	while (filter->out_gen < filter->out_count)
+	{
+		/* Need to reload buffer? */
+		samples_in_hand = (filter->b_end - filter->b_current + filter->b_len) % filter->b_len ;
+
+		if (samples_in_hand <= half_filter_chan_len)
+		{	prepare_data (filter, data, half_filter_chan_len) ;
+
+			samples_in_hand = (filter->b_end - filter->b_current + filter->b_len) % filter->b_len ;
+			if (samples_in_hand <= half_filter_chan_len)
+				break ;
 			} ;
+
+		/* This is the termination condition. */
+		if (filter->b_real_end >= 0)
+		{	if (filter->b_current + input_index + terminate >= filter->b_real_end)
+				break ;
+			} ;
+
+		if (filter->out_count > 0 && fabs (psrc->last_ratio - data->src_ratio) > 1e-10)
+			src_ratio = psrc->last_ratio + filter->out_gen * (data->src_ratio - psrc->last_ratio) / filter->out_count ;
+
+		float_increment = filter->index_inc * 1.0 ;
+		if (src_ratio < 1.0)
+			float_increment = filter->index_inc * src_ratio ;
+
+		increment = double_to_fp (float_increment) ;
+
+		start_filter_index = double_to_fp (input_index * float_increment) ;
+
+		calc_output_multi (filter, increment, start_filter_index, filter->channels, float_increment / filter->index_inc, data->data_out + filter->out_gen) ;
+		filter->out_gen += psrc->channels ;
 
 		/* Figure out the next index. */
 		input_index += 1.0 / src_ratio ;
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,7 +1,8 @@
 noinst_PROGRAMS = misc_test termination_test simple_test callback_test \
 					reset_test multi_channel_test snr_bw_test \
 					float_short_test varispeed_test callback_hang_test \
-					src-evaluate throughput_test downsample_test
+					src-evaluate throughput_test multichan_throughput_test \
+					downsample_test
 
 SAMPLRATEDIR =../src
 INCLUDES = -I$(srcdir)/$(SAMPLRATEDIR)
@@ -48,13 +49,17 @@
 
 # This program is for evaluating other sample rate converters.
 
-src_evaluate_SOURCES = src-evaluate.c calc_snr.c util.c
-src_evaluate_CFLAGS = @SNDFILE_CFLAGS@ @FFTW3_CFLAGS@
-src_evaluate_LDADD = $(SNDFILE_LIBS) $(FFTW3_LIBS)
-
 throughput_test_CFLAGS = $(AM_CFLAGS) @FFTW3_CFLAGS@
 throughput_test_SOURCES = throughput_test.c  util.c calc_snr.c
 throughput_test_LDADD  = $(SAMPLRATEDIR)/libsamplerate.la $(FFTW3_LIBS)
+
+multichan_throughput_test_CFLAGS = $(AM_CFLAGS) @FFTW3_CFLAGS@
+multichan_throughput_test_SOURCES = multichan_throughput_test.c  util.c calc_snr.c
+multichan_throughput_test_LDADD  = $(SAMPLRATEDIR)/libsamplerate.la $(FFTW3_LIBS)
+
+src_evaluate_SOURCES = src-evaluate.c calc_snr.c util.c
+src_evaluate_CFLAGS = @SNDFILE_CFLAGS@ @FFTW3_CFLAGS@
+src_evaluate_LDADD = $(SNDFILE_LIBS) $(FFTW3_LIBS)
 
 #===============================================================================
 
--- a/tests/multi_channel_test.c
+++ b/tests/multi_channel_test.c
@@ -22,13 +22,16 @@
 #include <stdlib.h>
 #include <string.h>
 #include <math.h>
+#include <assert.h>
 
 #include <samplerate.h>
 
 #include "util.h"
-#define	BUFFER_LEN		(1<<15)
+#define	BUFFER_LEN		50000
 #define	BLOCK_LEN		(12)
 
+#define	MAX_CHANNELS	4
+
 static void simple_test (int converter, int channel_count, double target_snr) ;
 static void process_test (int converter, int channel_count, double target_snr) ;
 static void callback_test (int converter, int channel_count, double target_snr) ;
@@ -35,39 +38,52 @@
 
 int
 main (void)
-{
+{	double target ;
+
 	puts ("\n    Zero Order Hold interpolator :") ;
-	simple_test		(SRC_ZERO_ORDER_HOLD, 1, 45.0) ;
-	process_test	(SRC_ZERO_ORDER_HOLD, 1, 45.0) ;
-	callback_test	(SRC_ZERO_ORDER_HOLD, 1, 45.0) ;
-	simple_test		(SRC_ZERO_ORDER_HOLD, 2, 44.0) ;
-	process_test	(SRC_ZERO_ORDER_HOLD, 2, 44.0) ;
-	callback_test	(SRC_ZERO_ORDER_HOLD, 2, 44.0) ;
-	simple_test		(SRC_ZERO_ORDER_HOLD, 3, 44.0) ;
-	process_test	(SRC_ZERO_ORDER_HOLD, 3, 44.0) ;
-	callback_test	(SRC_ZERO_ORDER_HOLD, 3, 44.0) ;
+	target = 38.0 ;
+	simple_test		(SRC_ZERO_ORDER_HOLD, 1, target) ;
+	process_test	(SRC_ZERO_ORDER_HOLD, 1, target) ;
+	callback_test	(SRC_ZERO_ORDER_HOLD, 1, target) ;
+	simple_test		(SRC_ZERO_ORDER_HOLD, 2, target) ;
+	process_test	(SRC_ZERO_ORDER_HOLD, 2, target) ;
+	callback_test	(SRC_ZERO_ORDER_HOLD, 2, target) ;
+	simple_test		(SRC_ZERO_ORDER_HOLD, 3, target) ;
+	process_test	(SRC_ZERO_ORDER_HOLD, 3, target) ;
+	callback_test	(SRC_ZERO_ORDER_HOLD, 3, target) ;
+	simple_test		(SRC_ZERO_ORDER_HOLD, 4, target) ;
+	process_test	(SRC_ZERO_ORDER_HOLD, 4, target) ;
+	callback_test	(SRC_ZERO_ORDER_HOLD, 4, target) ;
 
 	puts ("\n    Linear interpolator :") ;
-	simple_test		(SRC_LINEAR, 1, 92.0) ;
-	process_test	(SRC_LINEAR, 1, 92.0) ;
-	callback_test	(SRC_LINEAR, 1, 92.0) ;
-	simple_test		(SRC_LINEAR, 2, 90.0) ;
-	process_test	(SRC_LINEAR, 2, 90.0) ;
-	callback_test	(SRC_LINEAR, 2, 90.0) ;
-	simple_test		(SRC_LINEAR, 3, 88.0) ;
-	process_test	(SRC_LINEAR, 3, 88.0) ;
-	callback_test	(SRC_LINEAR, 3, 88.0) ;
+	target = 79.0 ;
+	simple_test		(SRC_LINEAR, 1, target) ;
+	process_test	(SRC_LINEAR, 1, target) ;
+	callback_test	(SRC_LINEAR, 1, target) ;
+	simple_test		(SRC_LINEAR, 2, target) ;
+	process_test	(SRC_LINEAR, 2, target) ;
+	callback_test	(SRC_LINEAR, 2, target) ;
+	simple_test		(SRC_LINEAR, 3, target) ;
+	process_test	(SRC_LINEAR, 3, target) ;
+	callback_test	(SRC_LINEAR, 3, target) ;
+	simple_test		(SRC_LINEAR, 4, target) ;
+	process_test	(SRC_LINEAR, 4, target) ;
+	callback_test	(SRC_LINEAR, 4, target) ;
 
 	puts ("\n    Sinc interpolator :") ;
-	simple_test		(SRC_SINC_FASTEST, 1, 100.0) ;
-	process_test	(SRC_SINC_FASTEST, 1, 100.0) ;
-	callback_test	(SRC_SINC_FASTEST, 1, 100.0) ;
-	simple_test		(SRC_SINC_FASTEST, 2, 100.0) ;
-	process_test	(SRC_SINC_FASTEST, 2, 100.0) ;
-	callback_test	(SRC_SINC_FASTEST, 2, 100.0) ;
-	simple_test		(SRC_SINC_FASTEST, 3, 100.0) ;
-	process_test	(SRC_SINC_FASTEST, 3, 100.0) ;
-	callback_test	(SRC_SINC_FASTEST, 3, 100.0) ;
+	target = 100.0 ;
+	simple_test		(SRC_SINC_FASTEST, 1, target) ;
+	process_test	(SRC_SINC_FASTEST, 1, target) ;
+	callback_test	(SRC_SINC_FASTEST, 1, target) ;
+	simple_test		(SRC_SINC_FASTEST, 2, target) ;
+	process_test	(SRC_SINC_FASTEST, 2, target) ;
+	callback_test	(SRC_SINC_FASTEST, 2, target) ;
+	simple_test		(SRC_SINC_FASTEST, 3, target) ;
+	process_test	(SRC_SINC_FASTEST, 3, target) ;
+	callback_test	(SRC_SINC_FASTEST, 3, target) ;
+	simple_test		(SRC_SINC_FASTEST, 4, target) ;
+	process_test	(SRC_SINC_FASTEST, 4, target) ;
+	callback_test	(SRC_SINC_FASTEST, 4, target) ;
 
 	puts ("") ;
 
@@ -77,10 +93,10 @@
 /*==============================================================================
 */
 
-static float input_serial		[BUFFER_LEN] ;
-static float input_interleaved	[BUFFER_LEN] ;
-static float output_interleaved	[BUFFER_LEN] ;
-static float output_serial		[BUFFER_LEN] ;
+static float input_serial		[BUFFER_LEN * MAX_CHANNELS] ;
+static float input_interleaved	[BUFFER_LEN * MAX_CHANNELS] ;
+static float output_interleaved	[BUFFER_LEN * MAX_CHANNELS] ;
+static float output_serial		[BUFFER_LEN * MAX_CHANNELS] ;
 
 static void
 simple_test (int converter, int channel_count, double target_snr)
@@ -92,12 +108,14 @@
 	printf ("\t%-22s (%d channel%c) ............. ", "simple_test", channel_count, channel_count > 1 ? 's' : ' ') ;
 	fflush (stdout) ;
 
+	assert (channel_count <= MAX_CHANNELS) ;
+
 	memset (input_serial, 0, sizeof (input_serial)) ;
 	memset (input_interleaved, 0, sizeof (input_interleaved)) ;
 	memset (output_interleaved, 0, sizeof (output_interleaved)) ;
 	memset (output_serial, 0, sizeof (output_serial)) ;
 
-	frames = MIN (ARRAY_LEN (input_serial) / channel_count, 1 << 16) ;
+	frames = BUFFER_LEN ;
 
 	/* Calculate channel_count separate windowed sine waves. */
 	for (ch = 0 ; ch < channel_count ; ch++)
@@ -162,16 +180,18 @@
 	printf ("\t%-22s (%d channel%c) ............. ", "process_test", channel_count, channel_count > 1 ? 's' : ' ') ;
 	fflush (stdout) ;
 
+	assert (channel_count <= MAX_CHANNELS) ;
+
 	memset (input_serial, 0, sizeof (input_serial)) ;
 	memset (input_interleaved, 0, sizeof (input_interleaved)) ;
 	memset (output_interleaved, 0, sizeof (output_interleaved)) ;
 	memset (output_serial, 0, sizeof (output_serial)) ;
 
-	frames = MIN (ARRAY_LEN (input_serial) / channel_count, 1 << 16) ;
+	frames = BUFFER_LEN ;
 
 	/* Calculate channel_count separate windowed sine waves. */
 	for (ch = 0 ; ch < channel_count ; ch++)
-	{	freq = (200.0 + 33.333333333 * ch) / 44100.0 ;
+	{	freq = (400.0 + 11.333333333 * ch) / 44100.0 ;
 		gen_windowed_sines (1, &freq, 1.0, input_serial + ch * frames, frames) ;
 		} ;
 
@@ -288,6 +308,8 @@
 	printf ("\t%-22s (%d channel%c) ............. ", "callback_test", channel_count, channel_count > 1 ? 's' : ' ') ;
 	fflush (stdout) ;
 
+	assert (channel_count <= MAX_CHANNELS) ;
+
 	memset (input_serial, 0, sizeof (input_serial)) ;
 	memset (input_interleaved, 0, sizeof (input_interleaved)) ;
 	memset (output_interleaved, 0, sizeof (output_interleaved)) ;
@@ -294,7 +316,7 @@
 	memset (output_serial, 0, sizeof (output_serial)) ;
 	memset (&test_callback_data, 0, sizeof (test_callback_data)) ;
 
-	frames = MIN (ARRAY_LEN (input_serial) / channel_count, 1 << 16) ;
+	frames = BUFFER_LEN ;
 
 	/* Calculate channel_count separate windowed sine waves. */
 	for (ch = 0 ; ch < channel_count ; ch++)
--- /dev/null
+++ b/tests/multichan_throughput_test.c
@@ -1,0 +1,217 @@
+/*
+** Copyright (C) 2008 Erik de Castro Lopo <erikd@mega-nerd.com>
+**
+** 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <samplerate.h>
+
+#include "config.h"
+
+#include "util.h"
+#include "float_cast.h"
+
+#define BUFFER_LEN	(1<<17)
+
+static float input [BUFFER_LEN] ;
+static float output [BUFFER_LEN] ;
+
+static long
+throughput_test (int converter, int channels, long best_throughput)
+{	SRC_DATA src_data ;
+	clock_t start_time, clock_time ;
+	double duration ;
+	long total_frames = 0, throughput ;
+	int error ;
+
+	printf ("    %-30s     %2d         ", src_get_name (converter), channels) ;
+	fflush (stdout) ;
+
+	src_data.data_in = input ;
+	src_data.input_frames = ARRAY_LEN (input) / channels ;
+
+	src_data.data_out = output ;
+	src_data.output_frames = ARRAY_LEN (output) / channels ;
+
+	src_data.src_ratio = 0.99 ;
+
+	sleep (2) ;
+
+	start_time = clock () ;
+
+	do
+	{
+		if ((error = src_simple (&src_data, converter, channels)) != 0)
+		{	puts (src_strerror (error)) ;
+			exit (1) ;
+			} ;
+
+		total_frames += src_data.output_frames_gen ;
+
+		clock_time = clock () - start_time ;
+		duration = (1.0 * clock_time) / CLOCKS_PER_SEC ;
+	}
+	while (duration < 5.0) ;
+
+	if (src_data.input_frames_used != src_data.input_frames)
+	{	printf ("\n\nLine %d : input frames used %ld should be %ld\n", __LINE__, src_data.input_frames_used, src_data.input_frames) ;
+		exit (1) ;
+		} ;
+
+	if (fabs (src_data.src_ratio * src_data.input_frames_used - src_data.output_frames_gen) > 2)
+	{	printf ("\n\nLine %d : input / output length mismatch.\n\n", __LINE__) ;
+		printf ("    input len  : %d\n", ARRAY_LEN (input) / channels) ;
+		printf ("    output len : %ld (should be %g +/- 2)\n\n", src_data.output_frames_gen,
+				floor (0.5 + src_data.src_ratio * src_data.input_frames_used)) ;
+		exit (1) ;
+		} ;
+
+	throughput = lrint (floor (total_frames / duration)) ;
+
+	if (best_throughput == 0)
+	{	best_throughput = MAX (throughput, best_throughput) ;
+		printf ("%5.2f      %10ld\n", duration, throughput) ;
+		}
+	else
+	{	best_throughput = MAX (throughput, best_throughput) ;
+		printf ("%5.2f      %10ld       %10ld\n", duration, throughput, best_throughput) ;
+		}
+
+	return best_throughput ;
+} /* throughput_test */
+
+static void
+single_run (void)
+{	int k ;
+
+	printf ("\n    CPU : ") ;
+	print_cpu_name () ;
+
+	puts (
+		"\n"
+		"    Converter                        Channels    Duration      Throughput\n"
+		"    ---------------------------------------------------------------------"
+		) ;
+
+	for (k = 1 ; k <= 5 ; k++)
+		throughput_test (SRC_SINC_FASTEST, k, 0) ;
+
+	puts ("") ;
+	for (k = 1 ; k <= 5 ; k++)
+		throughput_test (SRC_SINC_MEDIUM_QUALITY, k, 0) ;
+
+	puts ("") ;
+	for (k = 1 ; k <= 5 ; k++)
+		throughput_test (SRC_SINC_BEST_QUALITY, k, 0) ;
+
+	puts ("") ;
+	return ;
+} /* single_run */
+
+static void
+multi_run (int run_count)
+{	int k, ch ;
+
+	printf ("\n    CPU name : ") ;
+	print_cpu_name () ;
+
+	puts (
+		"\n"
+		"    Converter                        Channels    Duration      Throughput    Best Throughput\n"
+		"    ----------------------------------------------------------------------------------------"
+		) ;
+
+	for (ch = 1 ; ch <= 5 ; ch++)
+	{	long sinc_fastest = 0, sinc_medium = 0, sinc_best = 0 ;
+
+		for (k = 0 ; k < run_count ; k++)
+		{	sinc_fastest =		throughput_test (SRC_SINC_FASTEST, ch, sinc_fastest) ;
+			sinc_medium =		throughput_test (SRC_SINC_MEDIUM_QUALITY, ch, sinc_medium) ;
+			sinc_best =			throughput_test (SRC_SINC_BEST_QUALITY, ch, sinc_best) ;
+
+			puts ("") ;
+
+			/* Let the CPU cool down. We might be running on a laptop. */
+			sleep (10) ;
+			} ;
+
+		puts (
+			"\n"
+			"    Converter                        Best Throughput\n"
+			"    ------------------------------------------------"
+			) ;
+
+		printf ("    %-30s    %10ld\n", src_get_name (SRC_SINC_FASTEST), sinc_fastest) ;
+		printf ("    %-30s    %10ld\n", src_get_name (SRC_SINC_MEDIUM_QUALITY), sinc_medium) ;
+		printf ("    %-30s    %10ld\n", src_get_name (SRC_SINC_BEST_QUALITY), sinc_best) ;
+		} ;
+
+	puts ("") ;
+} /* multi_run */
+
+static void
+usage_exit (const char * argv0)
+{	const char * cptr ;
+
+	if ((cptr = strrchr (argv0, '/')) != NULL)
+		argv0 = cptr ;
+
+	printf (
+		"Usage :\n"
+	 	"    %s                 - Single run of the throughput test.\n"
+		"    %s --best-of N     - Do N runs of test a print bext result.\n"
+		"\n",
+		argv0, argv0) ;
+
+	exit (0) ;
+} /* usage_exit */
+
+int
+main (int argc, char ** argv)
+{	double freq ;
+
+	memset (input, 0, sizeof (input)) ;
+	freq = 0.01 ;
+	gen_windowed_sines (1, &freq, 1.0, input, BUFFER_LEN) ;
+
+	if (argc == 1)
+		single_run () ;
+	else if (argc == 3 && strcmp (argv [1], "--best-of") == 0)
+	{	int run_count = atoi (argv [2]) ;
+
+		if (run_count < 1 || run_count > 20)
+		{	printf ("Please be sensible. Run count should be in range (1, 10].\n") ;
+			exit (1) ;
+			} ;
+
+		multi_run (run_count) ;
+		}
+	else
+		usage_exit (argv [0]) ;
+
+	puts (
+		"            Duration is in seconds.\n"
+		"            Throughput is in frames/sec (more is better).\n"
+		) ;
+
+	return 0 ;
+} /* main */
+