xref: /freebsd/contrib/libsamplerate/src_linear.c (revision 1480c0b3f2daa048fb3763f589302f613ff2ae54)
1*1480c0b3SChristos Margiolis /*
2*1480c0b3SChristos Margiolis ** Copyright (c) 2002-2021, Erik de Castro Lopo <erikd@mega-nerd.com>
3*1480c0b3SChristos Margiolis ** All rights reserved.
4*1480c0b3SChristos Margiolis **
5*1480c0b3SChristos Margiolis ** This code is released under 2-clause BSD license. Please see the
6*1480c0b3SChristos Margiolis ** file at : https://github.com/libsndfile/libsamplerate/blob/master/COPYING
7*1480c0b3SChristos Margiolis */
8*1480c0b3SChristos Margiolis 
9*1480c0b3SChristos Margiolis #ifdef HAVE_CONFIG_H
10*1480c0b3SChristos Margiolis #include "config.h"
11*1480c0b3SChristos Margiolis #endif
12*1480c0b3SChristos Margiolis 
13*1480c0b3SChristos Margiolis #include <assert.h>
14*1480c0b3SChristos Margiolis #include <stdio.h>
15*1480c0b3SChristos Margiolis #include <stdlib.h>
16*1480c0b3SChristos Margiolis #include <string.h>
17*1480c0b3SChristos Margiolis #include <math.h>
18*1480c0b3SChristos Margiolis 
19*1480c0b3SChristos Margiolis #include "common.h"
20*1480c0b3SChristos Margiolis 
21*1480c0b3SChristos Margiolis static SRC_ERROR linear_vari_process (SRC_STATE *state, SRC_DATA *data) ;
22*1480c0b3SChristos Margiolis static void linear_reset (SRC_STATE *state) ;
23*1480c0b3SChristos Margiolis static SRC_STATE *linear_copy (SRC_STATE *state) ;
24*1480c0b3SChristos Margiolis static void linear_close (SRC_STATE *state) ;
25*1480c0b3SChristos Margiolis 
26*1480c0b3SChristos Margiolis /*========================================================================================
27*1480c0b3SChristos Margiolis */
28*1480c0b3SChristos Margiolis 
29*1480c0b3SChristos Margiolis #define	LINEAR_MAGIC_MARKER	MAKE_MAGIC ('l', 'i', 'n', 'e', 'a', 'r')
30*1480c0b3SChristos Margiolis 
31*1480c0b3SChristos Margiolis #define	SRC_DEBUG	0
32*1480c0b3SChristos Margiolis 
33*1480c0b3SChristos Margiolis typedef struct
34*1480c0b3SChristos Margiolis {	int		linear_magic_marker ;
35*1480c0b3SChristos Margiolis 	bool	dirty ;
36*1480c0b3SChristos Margiolis 	long	in_count, in_used ;
37*1480c0b3SChristos Margiolis 	long	out_count, out_gen ;
38*1480c0b3SChristos Margiolis 	float	*last_value ;
39*1480c0b3SChristos Margiolis } LINEAR_DATA ;
40*1480c0b3SChristos Margiolis 
41*1480c0b3SChristos Margiolis static SRC_STATE_VT linear_state_vt =
42*1480c0b3SChristos Margiolis {
43*1480c0b3SChristos Margiolis 	linear_vari_process,
44*1480c0b3SChristos Margiolis 	linear_vari_process,
45*1480c0b3SChristos Margiolis 	linear_reset,
46*1480c0b3SChristos Margiolis 	linear_copy,
47*1480c0b3SChristos Margiolis 	linear_close
48*1480c0b3SChristos Margiolis } ;
49*1480c0b3SChristos Margiolis 
50*1480c0b3SChristos Margiolis /*----------------------------------------------------------------------------------------
51*1480c0b3SChristos Margiolis */
52*1480c0b3SChristos Margiolis 
53*1480c0b3SChristos Margiolis static SRC_ERROR
linear_vari_process(SRC_STATE * state,SRC_DATA * data)54*1480c0b3SChristos Margiolis linear_vari_process (SRC_STATE *state, SRC_DATA *data)
55*1480c0b3SChristos Margiolis {	LINEAR_DATA *priv ;
56*1480c0b3SChristos Margiolis 	double		src_ratio, input_index, rem ;
57*1480c0b3SChristos Margiolis 	int			ch ;
58*1480c0b3SChristos Margiolis 
59*1480c0b3SChristos Margiolis 	if (data->input_frames <= 0)
60*1480c0b3SChristos Margiolis 		return SRC_ERR_NO_ERROR ;
61*1480c0b3SChristos Margiolis 
62*1480c0b3SChristos Margiolis 	if (state->private_data == NULL)
63*1480c0b3SChristos Margiolis 		return SRC_ERR_NO_PRIVATE ;
64*1480c0b3SChristos Margiolis 
65*1480c0b3SChristos Margiolis 	priv = (LINEAR_DATA*) state->private_data ;
66*1480c0b3SChristos Margiolis 
67*1480c0b3SChristos Margiolis 	if (!priv->dirty)
68*1480c0b3SChristos Margiolis 	{	/* If we have just been reset, set the last_value data. */
69*1480c0b3SChristos Margiolis 		for (ch = 0 ; ch < state->channels ; ch++)
70*1480c0b3SChristos Margiolis 			priv->last_value [ch] = data->data_in [ch] ;
71*1480c0b3SChristos Margiolis 		priv->dirty = true ;
72*1480c0b3SChristos Margiolis 		} ;
73*1480c0b3SChristos Margiolis 
74*1480c0b3SChristos Margiolis 	priv->in_count = data->input_frames * state->channels ;
75*1480c0b3SChristos Margiolis 	priv->out_count = data->output_frames * state->channels ;
76*1480c0b3SChristos Margiolis 	priv->in_used = priv->out_gen = 0 ;
77*1480c0b3SChristos Margiolis 
78*1480c0b3SChristos Margiolis 	src_ratio = state->last_ratio ;
79*1480c0b3SChristos Margiolis 
80*1480c0b3SChristos Margiolis 	if (is_bad_src_ratio (src_ratio))
81*1480c0b3SChristos Margiolis 		return SRC_ERR_BAD_INTERNAL_STATE ;
82*1480c0b3SChristos Margiolis 
83*1480c0b3SChristos Margiolis 	input_index = state->last_position ;
84*1480c0b3SChristos Margiolis 
85*1480c0b3SChristos Margiolis 	/* Calculate samples before first sample in input array. */
86*1480c0b3SChristos Margiolis 	while (input_index < 1.0 && priv->out_gen < priv->out_count)
87*1480c0b3SChristos Margiolis 	{
88*1480c0b3SChristos Margiolis 		if (priv->in_used + state->channels * (1.0 + input_index) >= priv->in_count)
89*1480c0b3SChristos Margiolis 			break ;
90*1480c0b3SChristos Margiolis 
91*1480c0b3SChristos Margiolis 		if (priv->out_count > 0 && fabs (state->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
92*1480c0b3SChristos Margiolis 			src_ratio = state->last_ratio + priv->out_gen * (data->src_ratio - state->last_ratio) / priv->out_count ;
93*1480c0b3SChristos Margiolis 
94*1480c0b3SChristos Margiolis 		for (ch = 0 ; ch < state->channels ; ch++)
95*1480c0b3SChristos Margiolis 		{	data->data_out [priv->out_gen] = (float) (priv->last_value [ch] + input_index *
96*1480c0b3SChristos Margiolis 										((double) data->data_in [ch] - priv->last_value [ch])) ;
97*1480c0b3SChristos Margiolis 			priv->out_gen ++ ;
98*1480c0b3SChristos Margiolis 			} ;
99*1480c0b3SChristos Margiolis 
100*1480c0b3SChristos Margiolis 		/* Figure out the next index. */
101*1480c0b3SChristos Margiolis 		input_index += 1.0 / src_ratio ;
102*1480c0b3SChristos Margiolis 		} ;
103*1480c0b3SChristos Margiolis 
104*1480c0b3SChristos Margiolis 	rem = fmod_one (input_index) ;
105*1480c0b3SChristos Margiolis 	priv->in_used += state->channels * lrint (input_index - rem) ;
106*1480c0b3SChristos Margiolis 	input_index = rem ;
107*1480c0b3SChristos Margiolis 
108*1480c0b3SChristos Margiolis 	/* Main processing loop. */
109*1480c0b3SChristos Margiolis 	while (priv->out_gen < priv->out_count && priv->in_used + state->channels * input_index < priv->in_count)
110*1480c0b3SChristos Margiolis 	{
111*1480c0b3SChristos Margiolis 		if (priv->out_count > 0 && fabs (state->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
112*1480c0b3SChristos Margiolis 			src_ratio = state->last_ratio + priv->out_gen * (data->src_ratio - state->last_ratio) / priv->out_count ;
113*1480c0b3SChristos Margiolis 
114*1480c0b3SChristos Margiolis #if SRC_DEBUG
115*1480c0b3SChristos Margiolis 		if (priv->in_used < state->channels && input_index < 1.0)
116*1480c0b3SChristos Margiolis 		{	printf ("Whoops!!!!   in_used : %ld     channels : %d     input_index : %f\n", priv->in_used, state->channels, input_index) ;
117*1480c0b3SChristos Margiolis 			exit (1) ;
118*1480c0b3SChristos Margiolis 			} ;
119*1480c0b3SChristos Margiolis #endif
120*1480c0b3SChristos Margiolis 
121*1480c0b3SChristos Margiolis 		for (ch = 0 ; ch < state->channels ; ch++)
122*1480c0b3SChristos Margiolis 		{	data->data_out [priv->out_gen] = (float) (data->data_in [priv->in_used - state->channels + ch] + input_index *
123*1480c0b3SChristos Margiolis 						((double) data->data_in [priv->in_used + ch] - data->data_in [priv->in_used - state->channels + ch])) ;
124*1480c0b3SChristos Margiolis 			priv->out_gen ++ ;
125*1480c0b3SChristos Margiolis 			} ;
126*1480c0b3SChristos Margiolis 
127*1480c0b3SChristos Margiolis 		/* Figure out the next index. */
128*1480c0b3SChristos Margiolis 		input_index += 1.0 / src_ratio ;
129*1480c0b3SChristos Margiolis 		rem = fmod_one (input_index) ;
130*1480c0b3SChristos Margiolis 
131*1480c0b3SChristos Margiolis 		priv->in_used += state->channels * lrint (input_index - rem) ;
132*1480c0b3SChristos Margiolis 		input_index = rem ;
133*1480c0b3SChristos Margiolis 		} ;
134*1480c0b3SChristos Margiolis 
135*1480c0b3SChristos Margiolis 	if (priv->in_used > priv->in_count)
136*1480c0b3SChristos Margiolis 	{	input_index += (priv->in_used - priv->in_count) / state->channels ;
137*1480c0b3SChristos Margiolis 		priv->in_used = priv->in_count ;
138*1480c0b3SChristos Margiolis 		} ;
139*1480c0b3SChristos Margiolis 
140*1480c0b3SChristos Margiolis 	state->last_position = input_index ;
141*1480c0b3SChristos Margiolis 
142*1480c0b3SChristos Margiolis 	if (priv->in_used > 0)
143*1480c0b3SChristos Margiolis 		for (ch = 0 ; ch < state->channels ; ch++)
144*1480c0b3SChristos Margiolis 			priv->last_value [ch] = data->data_in [priv->in_used - state->channels + ch] ;
145*1480c0b3SChristos Margiolis 
146*1480c0b3SChristos Margiolis 	/* Save current ratio rather then target ratio. */
147*1480c0b3SChristos Margiolis 	state->last_ratio = src_ratio ;
148*1480c0b3SChristos Margiolis 
149*1480c0b3SChristos Margiolis 	data->input_frames_used = priv->in_used / state->channels ;
150*1480c0b3SChristos Margiolis 	data->output_frames_gen = priv->out_gen / state->channels ;
151*1480c0b3SChristos Margiolis 
152*1480c0b3SChristos Margiolis 	return SRC_ERR_NO_ERROR ;
153*1480c0b3SChristos Margiolis } /* linear_vari_process */
154*1480c0b3SChristos Margiolis 
155*1480c0b3SChristos Margiolis /*------------------------------------------------------------------------------
156*1480c0b3SChristos Margiolis */
157*1480c0b3SChristos Margiolis 
158*1480c0b3SChristos Margiolis #if 0
159*1480c0b3SChristos Margiolis LIBSAMPLERATE_DLL_PRIVATE const char*
160*1480c0b3SChristos Margiolis linear_get_name (int src_enum)
161*1480c0b3SChristos Margiolis {
162*1480c0b3SChristos Margiolis 	if (src_enum == SRC_LINEAR)
163*1480c0b3SChristos Margiolis 		return "Linear Interpolator" ;
164*1480c0b3SChristos Margiolis 
165*1480c0b3SChristos Margiolis 	return NULL ;
166*1480c0b3SChristos Margiolis } /* linear_get_name */
167*1480c0b3SChristos Margiolis 
168*1480c0b3SChristos Margiolis LIBSAMPLERATE_DLL_PRIVATE const char*
169*1480c0b3SChristos Margiolis linear_get_description (int src_enum)
170*1480c0b3SChristos Margiolis {
171*1480c0b3SChristos Margiolis 	if (src_enum == SRC_LINEAR)
172*1480c0b3SChristos Margiolis 		return "Linear interpolator, very fast, poor quality." ;
173*1480c0b3SChristos Margiolis 
174*1480c0b3SChristos Margiolis 	return NULL ;
175*1480c0b3SChristos Margiolis } /* linear_get_descrition */
176*1480c0b3SChristos Margiolis #endif
177*1480c0b3SChristos Margiolis 
178*1480c0b3SChristos Margiolis static LINEAR_DATA *
linear_data_new(int channels)179*1480c0b3SChristos Margiolis linear_data_new (int channels)
180*1480c0b3SChristos Margiolis {
181*1480c0b3SChristos Margiolis 	assert (channels > 0) ;
182*1480c0b3SChristos Margiolis 
183*1480c0b3SChristos Margiolis 	LINEAR_DATA *priv = (LINEAR_DATA *) calloc (1, sizeof (LINEAR_DATA)) ;
184*1480c0b3SChristos Margiolis 	if (priv)
185*1480c0b3SChristos Margiolis 	{
186*1480c0b3SChristos Margiolis 		priv->linear_magic_marker = LINEAR_MAGIC_MARKER ;
187*1480c0b3SChristos Margiolis 		priv->last_value = (float *) calloc (channels, sizeof (float)) ;
188*1480c0b3SChristos Margiolis 		if (!priv->last_value)
189*1480c0b3SChristos Margiolis 		{
190*1480c0b3SChristos Margiolis 			free (priv) ;
191*1480c0b3SChristos Margiolis 			priv = NULL ;
192*1480c0b3SChristos Margiolis 		}
193*1480c0b3SChristos Margiolis 	}
194*1480c0b3SChristos Margiolis 
195*1480c0b3SChristos Margiolis 	return priv ;
196*1480c0b3SChristos Margiolis }
197*1480c0b3SChristos Margiolis 
198*1480c0b3SChristos Margiolis LIBSAMPLERATE_DLL_PRIVATE SRC_STATE *
linear_state_new(int channels,SRC_ERROR * error)199*1480c0b3SChristos Margiolis linear_state_new (int channels, SRC_ERROR *error)
200*1480c0b3SChristos Margiolis {
201*1480c0b3SChristos Margiolis 	assert (channels > 0) ;
202*1480c0b3SChristos Margiolis 	assert (error != NULL) ;
203*1480c0b3SChristos Margiolis 
204*1480c0b3SChristos Margiolis 	SRC_STATE *state = (SRC_STATE *) calloc (1, sizeof (SRC_STATE)) ;
205*1480c0b3SChristos Margiolis 	if (!state)
206*1480c0b3SChristos Margiolis 	{
207*1480c0b3SChristos Margiolis 		*error = SRC_ERR_MALLOC_FAILED ;
208*1480c0b3SChristos Margiolis 		return NULL ;
209*1480c0b3SChristos Margiolis 	}
210*1480c0b3SChristos Margiolis 
211*1480c0b3SChristos Margiolis 	state->channels = channels ;
212*1480c0b3SChristos Margiolis 	state->mode = SRC_MODE_PROCESS ;
213*1480c0b3SChristos Margiolis 
214*1480c0b3SChristos Margiolis 	state->private_data = linear_data_new (state->channels) ;
215*1480c0b3SChristos Margiolis 	if (!state->private_data)
216*1480c0b3SChristos Margiolis 	{
217*1480c0b3SChristos Margiolis 		free (state) ;
218*1480c0b3SChristos Margiolis 		*error = SRC_ERR_MALLOC_FAILED ;
219*1480c0b3SChristos Margiolis 		return NULL ;
220*1480c0b3SChristos Margiolis 	}
221*1480c0b3SChristos Margiolis 
222*1480c0b3SChristos Margiolis 	state->vt = &linear_state_vt ;
223*1480c0b3SChristos Margiolis 
224*1480c0b3SChristos Margiolis 	linear_reset (state) ;
225*1480c0b3SChristos Margiolis 
226*1480c0b3SChristos Margiolis 	*error = SRC_ERR_NO_ERROR ;
227*1480c0b3SChristos Margiolis 
228*1480c0b3SChristos Margiolis 	return state ;
229*1480c0b3SChristos Margiolis }
230*1480c0b3SChristos Margiolis 
231*1480c0b3SChristos Margiolis /*===================================================================================
232*1480c0b3SChristos Margiolis */
233*1480c0b3SChristos Margiolis 
234*1480c0b3SChristos Margiolis static void
linear_reset(SRC_STATE * state)235*1480c0b3SChristos Margiolis linear_reset (SRC_STATE *state)
236*1480c0b3SChristos Margiolis {	LINEAR_DATA *priv = NULL ;
237*1480c0b3SChristos Margiolis 
238*1480c0b3SChristos Margiolis 	priv = (LINEAR_DATA*) state->private_data ;
239*1480c0b3SChristos Margiolis 	if (priv == NULL)
240*1480c0b3SChristos Margiolis 		return ;
241*1480c0b3SChristos Margiolis 
242*1480c0b3SChristos Margiolis 	priv->dirty = false ;
243*1480c0b3SChristos Margiolis 	memset (priv->last_value, 0, sizeof (priv->last_value [0]) * state->channels) ;
244*1480c0b3SChristos Margiolis 
245*1480c0b3SChristos Margiolis 	return ;
246*1480c0b3SChristos Margiolis } /* linear_reset */
247*1480c0b3SChristos Margiolis 
248*1480c0b3SChristos Margiolis SRC_STATE *
linear_copy(SRC_STATE * state)249*1480c0b3SChristos Margiolis linear_copy (SRC_STATE *state)
250*1480c0b3SChristos Margiolis {
251*1480c0b3SChristos Margiolis 	assert (state != NULL) ;
252*1480c0b3SChristos Margiolis 
253*1480c0b3SChristos Margiolis 	if (state->private_data == NULL)
254*1480c0b3SChristos Margiolis 		return NULL ;
255*1480c0b3SChristos Margiolis 
256*1480c0b3SChristos Margiolis 	SRC_STATE *to = (SRC_STATE *) calloc (1, sizeof (SRC_STATE)) ;
257*1480c0b3SChristos Margiolis 	if (!to)
258*1480c0b3SChristos Margiolis 		return NULL ;
259*1480c0b3SChristos Margiolis 	memcpy (to, state, sizeof (SRC_STATE)) ;
260*1480c0b3SChristos Margiolis 
261*1480c0b3SChristos Margiolis 	LINEAR_DATA* from_priv = (LINEAR_DATA*) state->private_data ;
262*1480c0b3SChristos Margiolis 	LINEAR_DATA *to_priv = (LINEAR_DATA *) calloc (1, sizeof (LINEAR_DATA)) ;
263*1480c0b3SChristos Margiolis 	if (!to_priv)
264*1480c0b3SChristos Margiolis 	{
265*1480c0b3SChristos Margiolis 		free (to) ;
266*1480c0b3SChristos Margiolis 		return NULL ;
267*1480c0b3SChristos Margiolis 	}
268*1480c0b3SChristos Margiolis 
269*1480c0b3SChristos Margiolis 	memcpy (to_priv, from_priv, sizeof (LINEAR_DATA)) ;
270*1480c0b3SChristos Margiolis 	to_priv->last_value = (float *) malloc (sizeof (float) * state->channels) ;
271*1480c0b3SChristos Margiolis 	if (!to_priv->last_value)
272*1480c0b3SChristos Margiolis 	{
273*1480c0b3SChristos Margiolis 		free (to) ;
274*1480c0b3SChristos Margiolis 		free (to_priv) ;
275*1480c0b3SChristos Margiolis 		return NULL ;
276*1480c0b3SChristos Margiolis 	}
277*1480c0b3SChristos Margiolis 	memcpy (to_priv->last_value, from_priv->last_value, sizeof (float) * state->channels) ;
278*1480c0b3SChristos Margiolis 
279*1480c0b3SChristos Margiolis 	to->private_data = to_priv ;
280*1480c0b3SChristos Margiolis 
281*1480c0b3SChristos Margiolis 	return to ;
282*1480c0b3SChristos Margiolis } /* linear_copy */
283*1480c0b3SChristos Margiolis 
284*1480c0b3SChristos Margiolis static void
linear_close(SRC_STATE * state)285*1480c0b3SChristos Margiolis linear_close (SRC_STATE *state)
286*1480c0b3SChristos Margiolis {
287*1480c0b3SChristos Margiolis 	if (state)
288*1480c0b3SChristos Margiolis 	{
289*1480c0b3SChristos Margiolis 		LINEAR_DATA *linear = (LINEAR_DATA *) state->private_data ;
290*1480c0b3SChristos Margiolis 		if (linear)
291*1480c0b3SChristos Margiolis 		{
292*1480c0b3SChristos Margiolis 			if (linear->last_value)
293*1480c0b3SChristos Margiolis 			{
294*1480c0b3SChristos Margiolis 				free (linear->last_value) ;
295*1480c0b3SChristos Margiolis 				linear->last_value = NULL ;
296*1480c0b3SChristos Margiolis 			}
297*1480c0b3SChristos Margiolis 			free (linear) ;
298*1480c0b3SChristos Margiolis 			linear = NULL ;
299*1480c0b3SChristos Margiolis 		}
300*1480c0b3SChristos Margiolis 		free (state) ;
301*1480c0b3SChristos Margiolis 		state = NULL ;
302*1480c0b3SChristos Margiolis 	}
303*1480c0b3SChristos Margiolis } /* linear_close */
304