* Sync to trunk HEAD (r53473).
[reactos.git] / lib / 3rdparty / libsamplerate / src_zoh.c
1 /*
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
33 static int zoh_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data) ;
34 static void zoh_reset (SRC_PRIVATE *psrc) ;
35
36 /*========================================================================================
37 */
38
39 #define ZOH_MAGIC_MARKER MAKE_MAGIC ('s', 'r', 'c', 'z', 'o', 'h')
40
41 typedef struct
42 { int zoh_magic_marker ;
43 int channels ;
44 int reset ;
45 long in_count, in_used ;
46 long out_count, out_gen ;
47 float last_value [1] ;
48 } ZOH_DATA ;
49
50 /*----------------------------------------------------------------------------------------
51 */
52
53 static int
54 zoh_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data)
55 { ZOH_DATA *priv ;
56 double src_ratio, input_index, rem ;
57 int ch ;
58
59 if (data->input_frames <= 0)
60 return SRC_ERR_NO_ERROR ;
61
62 if (psrc->private_data == NULL)
63 return SRC_ERR_NO_PRIVATE ;
64
65 priv = (ZOH_DATA*) psrc->private_data ;
66
67 if (priv->reset)
68 { /* If we have just been reset, set the last_value data. */
69 for (ch = 0 ; ch < priv->channels ; ch++)
70 priv->last_value [ch] = data->data_in [ch] ;
71 priv->reset = 0 ;
72 } ;
73
74 priv->in_count = data->input_frames * priv->channels ;
75 priv->out_count = data->output_frames * priv->channels ;
76 priv->in_used = priv->out_gen = 0 ;
77
78 src_ratio = psrc->last_ratio ;
79 input_index = psrc->last_position ;
80
81 /* Calculate samples before first sample in input array. */
82 while (input_index < 1.0 && priv->out_gen < priv->out_count)
83 {
84 if (priv->in_used + priv->channels * input_index >= priv->in_count)
85 break ;
86
87 if (priv->out_count > 0 && fabs (psrc->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
88 src_ratio = psrc->last_ratio + priv->out_gen * (data->src_ratio - psrc->last_ratio) / priv->out_count ;
89
90 for (ch = 0 ; ch < priv->channels ; ch++)
91 { data->data_out [priv->out_gen] = priv->last_value [ch] ;
92 priv->out_gen ++ ;
93 } ;
94
95 /* Figure out the next index. */
96 input_index += 1.0 / src_ratio ;
97 } ;
98
99 rem = fmod_one (input_index) ;
100 priv->in_used += priv->channels * lrint (input_index - rem) ;
101 input_index = rem ;
102
103 /* Main processing loop. */
104 while (priv->out_gen < priv->out_count && priv->in_used + priv->channels * input_index <= priv->in_count)
105 {
106 if (priv->out_count > 0 && fabs (psrc->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
107 src_ratio = psrc->last_ratio + priv->out_gen * (data->src_ratio - psrc->last_ratio) / priv->out_count ;
108
109 for (ch = 0 ; ch < priv->channels ; ch++)
110 { data->data_out [priv->out_gen] = data->data_in [priv->in_used - priv->channels + ch] ;
111 priv->out_gen ++ ;
112 } ;
113
114 /* Figure out the next index. */
115 input_index += 1.0 / src_ratio ;
116 rem = fmod_one (input_index) ;
117
118 priv->in_used += priv->channels * lrint (input_index - rem) ;
119 input_index = rem ;
120 } ;
121
122 if (priv->in_used > priv->in_count)
123 { input_index += (priv->in_used - priv->in_count) / priv->channels ;
124 priv->in_used = priv->in_count ;
125 } ;
126
127 psrc->last_position = input_index ;
128
129 if (priv->in_used > 0)
130 for (ch = 0 ; ch < priv->channels ; ch++)
131 priv->last_value [ch] = data->data_in [priv->in_used - priv->channels + ch] ;
132
133 /* Save current ratio rather then target ratio. */
134 psrc->last_ratio = src_ratio ;
135
136 data->input_frames_used = priv->in_used / priv->channels ;
137 data->output_frames_gen = priv->out_gen / priv->channels ;
138
139 return SRC_ERR_NO_ERROR ;
140 } /* zoh_vari_process */
141
142 /*------------------------------------------------------------------------------
143 */
144
145 const char*
146 zoh_get_name (int src_enum)
147 {
148 if (src_enum == SRC_ZERO_ORDER_HOLD)
149 return "ZOH Interpolator" ;
150
151 return NULL ;
152 } /* zoh_get_name */
153
154 const char*
155 zoh_get_description (int src_enum)
156 {
157 if (src_enum == SRC_ZERO_ORDER_HOLD)
158 return "Zero order hold interpolator, very fast, poor quality." ;
159
160 return NULL ;
161 } /* zoh_get_descrition */
162
163 int
164 zoh_set_converter (SRC_PRIVATE *psrc, int src_enum)
165 { ZOH_DATA *priv = NULL ;
166
167 if (src_enum != SRC_ZERO_ORDER_HOLD)
168 return SRC_ERR_BAD_CONVERTER ;
169
170 if (psrc->private_data != NULL)
171 { free (psrc->private_data) ;
172 psrc->private_data = NULL ;
173 } ;
174
175 if (psrc->private_data == NULL)
176 { priv = calloc (1, sizeof (*priv) + psrc->channels * sizeof (float)) ;
177 if (priv == NULL)
178 return SRC_ERR_MALLOC_FAILED ;
179 psrc->private_data = priv ;
180 } ;
181
182 priv->zoh_magic_marker = ZOH_MAGIC_MARKER ;
183 priv->channels = psrc->channels ;
184
185 psrc->const_process = zoh_vari_process ;
186 psrc->vari_process = zoh_vari_process ;
187 psrc->reset = zoh_reset ;
188
189 zoh_reset (psrc) ;
190
191 return SRC_ERR_NO_ERROR ;
192 } /* zoh_set_converter */
193
194 /*===================================================================================
195 */
196
197 static void
198 zoh_reset (SRC_PRIVATE *psrc)
199 { ZOH_DATA *priv ;
200
201 priv = (ZOH_DATA*) psrc->private_data ;
202 if (priv == NULL)
203 return ;
204
205 priv->channels = psrc->channels ;
206 priv->reset = 1 ;
207 memset (priv->last_value, 0, sizeof (priv->last_value [0]) * priv->channels) ;
208
209 return ;
210 } /* zoh_reset */
211