blob: 388e0b444a9a1aedf5c3933e046c5e477a6b5cb2 [file] [log] [blame]
Emeric Vigiereebea672012-08-06 17:36:30 -04001/*
2** Copyright (C) 2002-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3**
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.
8**
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.
13**
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.
17*/
18
19/*
20** This code is part of Secret Rabbit Code aka libsamplerate. A commercial
21** use license for this code is available, please see:
22** http://www.mega-nerd.com/SRC/procedure.html
23*/
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
29#include "config.h"
30#include "float_cast.h"
31#include "common.h"
32
33static int linear_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data) ;
34static void linear_reset (SRC_PRIVATE *psrc) ;
35
36/*========================================================================================
37*/
38
39#define LINEAR_MAGIC_MARKER MAKE_MAGIC ('l', 'i', 'n', 'e', 'a', 'r')
40
41#define SRC_DEBUG 0
42
43typedef struct
44{ int linear_magic_marker ;
45 int channels ;
46 int reset ;
47 long in_count, in_used ;
48 long out_count, out_gen ;
49 float last_value [1] ;
50} LINEAR_DATA ;
51
52/*----------------------------------------------------------------------------------------
53*/
54
55static int
56linear_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data)
57{ LINEAR_DATA *priv ;
58 double src_ratio, input_index, rem ;
59 int ch ;
60
61 if (data->input_frames <= 0)
62 return SRC_ERR_NO_ERROR ;
63
64 if (psrc->private_data == NULL)
65 return SRC_ERR_NO_PRIVATE ;
66
67 priv = (LINEAR_DATA*) psrc->private_data ;
68
69 if (priv->reset)
70 { /* If we have just been reset, set the last_value data. */
71 for (ch = 0 ; ch < priv->channels ; ch++)
72 priv->last_value [ch] = data->data_in [ch] ;
73 priv->reset = 0 ;
74 } ;
75
76 priv->in_count = data->input_frames * priv->channels ;
77 priv->out_count = data->output_frames * priv->channels ;
78 priv->in_used = priv->out_gen = 0 ;
79
80 src_ratio = psrc->last_ratio ;
81 input_index = psrc->last_position ;
82
83 /* Calculate samples before first sample in input array. */
84 while (input_index < 1.0 && priv->out_gen < priv->out_count)
85 {
86 if (priv->in_used + priv->channels * (1.0 + input_index) >= priv->in_count)
87 break ;
88
89 if (priv->out_count > 0 && fabs (psrc->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
90 src_ratio = psrc->last_ratio + priv->out_gen * (data->src_ratio - psrc->last_ratio) / priv->out_count ;
91
92 for (ch = 0 ; ch < priv->channels ; ch++)
93 { data->data_out [priv->out_gen] = (float) (priv->last_value [ch] + input_index *
94 (data->data_in [ch] - priv->last_value [ch])) ;
95 priv->out_gen ++ ;
96 } ;
97
98 /* Figure out the next index. */
99 input_index += 1.0 / src_ratio ;
100 } ;
101
102 rem = fmod_one (input_index) ;
103 priv->in_used += priv->channels * lrint (input_index - rem) ;
104 input_index = rem ;
105
106 /* Main processing loop. */
107 while (priv->out_gen < priv->out_count && priv->in_used + priv->channels * input_index < priv->in_count)
108 {
109 if (priv->out_count > 0 && fabs (psrc->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
110 src_ratio = psrc->last_ratio + priv->out_gen * (data->src_ratio - psrc->last_ratio) / priv->out_count ;
111
112 if (SRC_DEBUG && priv->in_used < priv->channels && input_index < 1.0)
113 { printf ("Whoops!!!! in_used : %ld channels : %d input_index : %f\n", priv->in_used, priv->channels, input_index) ;
114 exit (1) ;
115 } ;
116
117 for (ch = 0 ; ch < priv->channels ; ch++)
118 { data->data_out [priv->out_gen] = (float) (data->data_in [priv->in_used - priv->channels + ch] + input_index *
119 (data->data_in [priv->in_used + ch] - data->data_in [priv->in_used - priv->channels + ch])) ;
120 priv->out_gen ++ ;
121 } ;
122
123 /* Figure out the next index. */
124 input_index += 1.0 / src_ratio ;
125 rem = fmod_one (input_index) ;
126
127 priv->in_used += priv->channels * lrint (input_index - rem) ;
128 input_index = rem ;
129 } ;
130
131 if (priv->in_used > priv->in_count)
132 { input_index += (priv->in_used - priv->in_count) / priv->channels ;
133 priv->in_used = priv->in_count ;
134 } ;
135
136 psrc->last_position = input_index ;
137
138 if (priv->in_used > 0)
139 for (ch = 0 ; ch < priv->channels ; ch++)
140 priv->last_value [ch] = data->data_in [priv->in_used - priv->channels + ch] ;
141
142 /* Save current ratio rather then target ratio. */
143 psrc->last_ratio = src_ratio ;
144
145 data->input_frames_used = priv->in_used / priv->channels ;
146 data->output_frames_gen = priv->out_gen / priv->channels ;
147
148 return SRC_ERR_NO_ERROR ;
149} /* linear_vari_process */
150
151/*------------------------------------------------------------------------------
152*/
153
154const char*
155linear_get_name (int src_enum)
156{
157 if (src_enum == SRC_LINEAR)
158 return "Linear Interpolator" ;
159
160 return NULL ;
161} /* linear_get_name */
162
163const char*
164linear_get_description (int src_enum)
165{
166 if (src_enum == SRC_LINEAR)
167 return "Linear interpolator, very fast, poor quality." ;
168
169 return NULL ;
170} /* linear_get_descrition */
171
172int
173linear_set_converter (SRC_PRIVATE *psrc, int src_enum)
174{ LINEAR_DATA *priv = NULL ;
175
176 if (src_enum != SRC_LINEAR)
177 return SRC_ERR_BAD_CONVERTER ;
178
179 if (psrc->private_data != NULL)
180 { free (psrc->private_data) ;
181 psrc->private_data = NULL ;
182 } ;
183
184 if (psrc->private_data == NULL)
185 { priv = calloc (1, sizeof (*priv) + psrc->channels * sizeof (float)) ;
186 if (priv == NULL)
187 return SRC_ERR_MALLOC_FAILED ;
188 psrc->private_data = priv ;
189 } ;
190
191 priv->linear_magic_marker = LINEAR_MAGIC_MARKER ;
192 priv->channels = psrc->channels ;
193
194 psrc->const_process = linear_vari_process ;
195 psrc->vari_process = linear_vari_process ;
196 psrc->reset = linear_reset ;
197
198 linear_reset (psrc) ;
199
200 return SRC_ERR_NO_ERROR ;
201} /* linear_set_converter */
202
203/*===================================================================================
204*/
205
206static void
207linear_reset (SRC_PRIVATE *psrc)
208{ LINEAR_DATA *priv = NULL ;
209
210 priv = (LINEAR_DATA*) psrc->private_data ;
211 if (priv == NULL)
212 return ;
213
214 priv->channels = psrc->channels ;
215 priv->reset = 1 ;
216 memset (priv->last_value, 0, sizeof (priv->last_value [0]) * priv->channels) ;
217
218 return ;
219} /* linear_reset */
220