2
** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
4
** This program is free software; you can redistribute it and/or modify
5
** it under the terms of the GNU General Public License as published by
6
** the Free Software Foundation; either version 2 of the License, or
7
** (at your option) any later version.
9
** This program is distributed in the hope that it will be useful,
10
** but WITHOUT ANY WARRANTY; without even the implied warranty of
11
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
** GNU General Public License for more details.
14
** You should have received a copy of the GNU General Public License
15
** along with this program; if not, write to the Free Software
16
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
24
#include "float_cast.h"
27
static void linear_reset (SRC_PRIVATE *psrc) ;
29
/*========================================================================================
32
#define LINEAR_MAGIC_MARKER MAKE_MAGIC ('l', 'i', 'n', 'e', 'a', 'r')
37
{ int linear_magic_marker ;
39
long in_count, in_used ;
40
long out_count, out_gen ;
41
float last_value [1] ;
44
/*----------------------------------------------------------------------------------------
47
linear_process (SRC_PRIVATE *psrc, SRC_DATA *data)
48
{ LINEAR_DATA *linear ;
49
double src_ratio, input_index ;
52
if (psrc->private_data == NULL)
53
return SRC_ERR_NO_PRIVATE ;
55
linear = (LINEAR_DATA*) psrc->private_data ;
57
linear->in_count = data->input_frames * linear->channels ;
58
linear->out_count = data->output_frames * linear->channels ;
59
linear->in_used = linear->out_gen = 0 ;
61
src_ratio = psrc->last_ratio ;
62
input_index = psrc->last_position ;
64
/* Calculate samples before first sample in input array. */
65
while (input_index < 1.0 && linear->out_gen < linear->out_count)
67
if (linear->in_used + linear->channels * input_index > linear->in_count)
70
if (fabs (psrc->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
71
src_ratio = psrc->last_ratio + linear->out_gen * (data->src_ratio - psrc->last_ratio) / (linear->out_count - 1) ;
73
for (ch = 0 ; ch < linear->channels ; ch++)
74
{ data->data_out [linear->out_gen] = linear->last_value [ch] + input_index *
75
(data->data_in [ch] - linear->last_value [ch]) ;
79
/* Figure out the next index. */
80
input_index += 1.0 / src_ratio ;
83
linear->in_used += linear->channels * lrint (floor (input_index)) ;
84
input_index -= floor (input_index) ;
86
/* Main processing loop. */
87
while (linear->out_gen < linear->out_count && linear->in_used + linear->channels * input_index <= linear->in_count)
89
if (fabs (psrc->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
90
src_ratio = psrc->last_ratio + linear->out_gen * (data->src_ratio - psrc->last_ratio) / (linear->out_count - 1) ;
92
if (SRC_DEBUG && linear->in_used < linear->channels && input_index < 1.0)
93
{ printf ("Whoops!!!! in_used : %ld channels : %d input_index : %f\n", linear->in_used, linear->channels, input_index) ;
97
for (ch = 0 ; ch < linear->channels ; ch++)
98
{ data->data_out [linear->out_gen] = data->data_in [linear->in_used - linear->channels + ch] + input_index *
99
(data->data_in [linear->in_used + ch] - data->data_in [linear->in_used - linear->channels + ch]) ;
103
/* Figure out the next index. */
104
input_index += 1.0 / src_ratio ;
106
linear->in_used += linear->channels * lrint (floor (input_index)) ;
107
input_index -= floor (input_index) ;
110
if (linear->in_used > linear->in_count)
111
{ input_index += linear->in_used - linear->in_count ;
112
linear->in_used = linear->in_count ;
115
psrc->last_position = input_index ;
117
if (linear->in_used > 0)
118
for (ch = 0 ; ch < linear->channels ; ch++)
119
linear->last_value [ch] = data->data_in [linear->in_used - linear->channels + ch] ;
121
/* Save current ratio rather then target ratio. */
122
psrc->last_ratio = src_ratio ;
124
data->input_frames_used = linear->in_used / linear->channels ;
125
data->output_frames_gen = linear->out_gen / linear->channels ;
127
return SRC_ERR_NO_ERROR ;
128
} /* linear_process */
130
/*------------------------------------------------------------------------------
134
linear_get_name (int src_enum)
136
if (src_enum == SRC_LINEAR)
137
return "Linear Interpolator" ;
140
} /* linear_get_name */
143
linear_get_description (int src_enum)
145
if (src_enum == SRC_LINEAR)
146
return "Linear interpolator, very fast, poor quality." ;
149
} /* linear_get_descrition */
152
linear_set_converter (SRC_PRIVATE *psrc, int src_enum)
153
{ LINEAR_DATA *linear = NULL ;
155
if (src_enum != SRC_LINEAR)
156
return SRC_ERR_BAD_CONVERTER ;
158
if (psrc->private_data != NULL)
159
{ linear = (LINEAR_DATA*) psrc->private_data ;
160
if (linear->linear_magic_marker != LINEAR_MAGIC_MARKER)
161
{ free (psrc->private_data) ;
162
psrc->private_data = NULL ;
166
if (psrc->private_data == NULL)
167
{ linear = calloc (1, sizeof (*linear) + psrc->channels * sizeof (float)) ;
169
return SRC_ERR_MALLOC_FAILED ;
170
psrc->private_data = linear ;
173
linear->linear_magic_marker = LINEAR_MAGIC_MARKER ;
174
linear->channels = psrc->channels ;
176
psrc->process = linear_process ;
177
psrc->reset = linear_reset ;
179
linear_reset (psrc) ;
181
return SRC_ERR_NO_ERROR ;
182
} /* linear_set_converter */
184
/*===================================================================================
188
linear_reset (SRC_PRIVATE *psrc)
189
{ LINEAR_DATA *linear = NULL ;
191
linear = (LINEAR_DATA*) psrc->private_data ;
195
memset (linear->last_value, 0, sizeof (linear->last_value [0]) * linear->channels) ;
198
** Do not edit or modify anything in this comment block.
199
** The arch-tag line is a file identity tag for the GNU Arch
200
** revision control system.
202
** arch-tag: 7eac3103-3d84-45d3-8bbd-f409c2b2d1a9