2
** Copyright (C) 2002,2003 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')
35
{ int linear_magic_marker ;
37
long in_count, in_used ;
38
long out_count, out_gen ;
39
float last_value [1] ;
42
/*----------------------------------------------------------------------------------------
45
linear_process (SRC_PRIVATE *psrc, SRC_DATA *data)
46
{ LINEAR_DATA *linear ;
47
double src_ratio, input_index ;
50
if (psrc->private_data == NULL)
51
return SRC_ERR_NO_PRIVATE ;
53
linear = (LINEAR_DATA*) psrc->private_data ;
55
linear->in_count = data->input_frames * linear->channels ;
56
linear->out_count = data->output_frames * linear->channels ;
57
linear->in_used = linear->out_gen = 0 ;
59
src_ratio = psrc->last_ratio ;
60
input_index = psrc->last_position ;
62
/* Calculate samples before first sample in input array. */
63
while (input_index > 0.0 && input_index < 1.0 && linear->out_gen < linear->out_count)
65
if (linear->in_used + input_index > linear->in_count)
68
if (fabs (psrc->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
69
src_ratio = psrc->last_ratio + linear->out_gen * (data->src_ratio - psrc->last_ratio) / (linear->out_count - 1) ;
71
for (ch = 0 ; ch < linear->channels ; ch++)
72
{ data->data_out [linear->out_gen] = linear->last_value [ch] + input_index *
73
(data->data_in [ch] - linear->last_value [ch]) ;
77
/* Figure out the next index. */
78
input_index += 1.0 / src_ratio ;
81
/* Main processing loop. */
82
while (linear->out_gen < linear->out_count)
84
linear->in_used += linear->channels * lrint (floor (input_index)) ;
85
input_index -= floor (input_index) ;
87
if (linear->in_used + input_index > linear->in_count)
90
if (fabs (psrc->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
91
src_ratio = psrc->last_ratio + linear->out_gen * (data->src_ratio - psrc->last_ratio) / (linear->out_count - 1) ;
93
for (ch = 0 ; ch < linear->channels ; ch++)
94
{ data->data_out [linear->out_gen] = data->data_in [linear->in_used + ch] + input_index *
95
(data->data_in [linear->in_used + linear->channels + ch] - data->data_in [linear->in_used + ch]) ;
99
/* Figure out the next index. */
100
input_index += 1.0 / src_ratio ;
103
/*- if (input_index > linear->in_count - linear->in_used)
104
{ input_index -= linear->in_count - linear->in_used ;
105
linear->in_used = linear->in_count ;
106
puts ("XXXXXXXXXX") ; /+*-exit (1) ;-*+/
110
psrc->last_position = input_index ;
112
for (ch = 0 ; ch < linear->channels ; ch++)
113
{ linear->last_value [ch] = data->data_in [linear->in_used - linear->channels + ch] ;
115
/*- data->data_out [0 + ch] = -0.9 ;
116
data->data_out [linear->out_gen - linear->channels + ch] = 0.9 ; -*/
119
/* Save current ratio rather then target ratio. */
120
psrc->last_ratio = src_ratio ;
122
data->input_frames_used = linear->in_used / linear->channels ;
123
data->output_frames_gen = linear->out_gen / linear->channels ;
125
return SRC_ERR_NO_ERROR ;
126
} /* linear_process */
128
/*------------------------------------------------------------------------------
132
linear_get_name (int src_enum)
134
if (src_enum == SRC_LINEAR)
135
return "Linear Interpolator" ;
138
} /* linear_get_name */
141
linear_get_description (int src_enum)
143
if (src_enum == SRC_LINEAR)
144
return "Linear interpolator, very fast, poor quality." ;
147
} /* linear_get_descrition */
150
linear_set_converter (SRC_PRIVATE *psrc, int src_enum)
151
{ LINEAR_DATA *linear ;
153
if (src_enum != SRC_LINEAR)
154
return SRC_ERR_BAD_CONVERTER ;
156
if (psrc->private_data != NULL)
157
{ linear = (LINEAR_DATA*) psrc->private_data ;
158
if (linear->linear_magic_marker != LINEAR_MAGIC_MARKER)
159
{ free (psrc->private_data) ;
160
psrc->private_data = NULL ;
164
if (psrc->private_data == NULL)
165
{ linear = calloc (1, sizeof (*linear) + psrc->channels * sizeof (float)) ;
167
return SRC_ERR_MALLOC_FAILED ;
168
psrc->private_data = linear ;
171
linear->linear_magic_marker = LINEAR_MAGIC_MARKER ;
172
linear->channels = psrc->channels ;
174
psrc->process = linear_process ;
175
psrc->reset = linear_reset ;
177
linear_reset (psrc) ;
179
return SRC_ERR_NO_ERROR ;
180
} /* linear_set_converter */
182
/*===================================================================================
186
linear_reset (SRC_PRIVATE *psrc)
187
{ LINEAR_DATA *linear ;
189
linear = (LINEAR_DATA*) psrc->private_data ;
193
memset (linear->last_value, 0, sizeof (linear->last_value [0]) * linear->channels) ;