ReactOS 0.4.15-dev-7834-g00c4b3d
src_zoh.c
Go to the documentation of this file.
1/*
2** Copyright (c) 2002-2016, Erik de Castro Lopo <erikd@mega-nerd.com>
3** All rights reserved.
4**
5** This code is released under 2-clause BSD license. Please see the
6** file at : https://github.com/erikd/libsamplerate/blob/master/COPYING
7*/
8
9#include "precomp.h"
10
11static int zoh_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data) ;
12static void zoh_reset (SRC_PRIVATE *psrc) ;
13
14/*========================================================================================
15*/
16
17#define ZOH_MAGIC_MARKER MAKE_MAGIC ('s', 'r', 'c', 'z', 'o', 'h')
18
19typedef struct
22 int reset ;
23 long in_count, in_used ;
24 long out_count, out_gen ;
25 float last_value [1] ;
26} ZOH_DATA ;
27
28/*----------------------------------------------------------------------------------------
29*/
30
31static int
33{ ZOH_DATA *priv ;
34 double src_ratio, input_index, rem ;
35 int ch ;
36
37 if (data->input_frames <= 0)
38 return SRC_ERR_NO_ERROR ;
39
40 if (psrc->private_data == NULL)
41 return SRC_ERR_NO_PRIVATE ;
42
43 priv = (ZOH_DATA*) psrc->private_data ;
44
45 if (priv->reset)
46 { /* If we have just been reset, set the last_value data. */
47 for (ch = 0 ; ch < priv->channels ; ch++)
48 priv->last_value [ch] = data->data_in [ch] ;
49 priv->reset = 0 ;
50 } ;
51
52 priv->in_count = data->input_frames * priv->channels ;
53 priv->out_count = data->output_frames * priv->channels ;
54 priv->in_used = priv->out_gen = 0 ;
55
56 src_ratio = psrc->last_ratio ;
57
58 if (is_bad_src_ratio (src_ratio))
60
61 input_index = psrc->last_position ;
62
63 /* Calculate samples before first sample in input array. */
64 while (input_index < 1.0 && priv->out_gen < priv->out_count)
65 {
66 if (priv->in_used + priv->channels * input_index >= priv->in_count)
67 break ;
68
69 if (priv->out_count > 0 && fabs (psrc->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
70 src_ratio = psrc->last_ratio + priv->out_gen * (data->src_ratio - psrc->last_ratio) / priv->out_count ;
71
72 for (ch = 0 ; ch < priv->channels ; ch++)
73 { data->data_out [priv->out_gen] = priv->last_value [ch] ;
74 priv->out_gen ++ ;
75 } ;
76
77 /* Figure out the next index. */
78 input_index += 1.0 / src_ratio ;
79 } ;
80
81 rem = fmod_one (input_index) ;
82 priv->in_used += priv->channels * lrint (input_index - rem) ;
83 input_index = rem ;
84
85 /* Main processing loop. */
86 while (priv->out_gen < priv->out_count && priv->in_used + priv->channels * input_index <= priv->in_count)
87 {
88 if (priv->out_count > 0 && fabs (psrc->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
89 src_ratio = psrc->last_ratio + priv->out_gen * (data->src_ratio - psrc->last_ratio) / priv->out_count ;
90
91 for (ch = 0 ; ch < priv->channels ; ch++)
92 { data->data_out [priv->out_gen] = data->data_in [priv->in_used - priv->channels + ch] ;
93 priv->out_gen ++ ;
94 } ;
95
96 /* Figure out the next index. */
97 input_index += 1.0 / src_ratio ;
98 rem = fmod_one (input_index) ;
99
100 priv->in_used += priv->channels * lrint (input_index - rem) ;
101 input_index = rem ;
102 } ;
103
104 if (priv->in_used > priv->in_count)
105 { input_index += (priv->in_used - priv->in_count) / priv->channels ;
106 priv->in_used = priv->in_count ;
107 } ;
108
109 psrc->last_position = input_index ;
110
111 if (priv->in_used > 0)
112 for (ch = 0 ; ch < priv->channels ; ch++)
113 priv->last_value [ch] = data->data_in [priv->in_used - priv->channels + ch] ;
114
115 /* Save current ratio rather then target ratio. */
116 psrc->last_ratio = src_ratio ;
117
118 data->input_frames_used = priv->in_used / priv->channels ;
119 data->output_frames_gen = priv->out_gen / priv->channels ;
120
121 return SRC_ERR_NO_ERROR ;
122} /* zoh_vari_process */
123
124/*------------------------------------------------------------------------------
125*/
126
127const char*
128zoh_get_name (int src_enum)
129{
130 if (src_enum == SRC_ZERO_ORDER_HOLD)
131 return "ZOH Interpolator" ;
132
133 return NULL ;
134} /* zoh_get_name */
135
136const char*
138{
139 if (src_enum == SRC_ZERO_ORDER_HOLD)
140 return "Zero order hold interpolator, very fast, poor quality." ;
141
142 return NULL ;
143} /* zoh_get_descrition */
144
145int
146zoh_set_converter (SRC_PRIVATE *psrc, int src_enum)
147{ ZOH_DATA *priv = NULL ;
148
149 if (src_enum != SRC_ZERO_ORDER_HOLD)
150 return SRC_ERR_BAD_CONVERTER ;
151
152 if (psrc->private_data != NULL)
153 { free (psrc->private_data) ;
154 psrc->private_data = NULL ;
155 } ;
156
157 if (psrc->private_data == NULL)
158 { priv = calloc (1, sizeof (*priv) + psrc->channels * sizeof (float)) ;
159 psrc->private_data = priv ;
160 } ;
161
162 if (priv == NULL)
163 return SRC_ERR_MALLOC_FAILED ;
164
166 priv->channels = psrc->channels ;
167
170 psrc->reset = zoh_reset ;
171
172 zoh_reset (psrc) ;
173
174 return SRC_ERR_NO_ERROR ;
175} /* zoh_set_converter */
176
177/*===================================================================================
178*/
179
180static void
182{ ZOH_DATA *priv ;
183
184 priv = (ZOH_DATA*) psrc->private_data ;
185 if (priv == NULL)
186 return ;
187
188 priv->channels = psrc->channels ;
189 priv->reset = 1 ;
190 memset (priv->last_value, 0, sizeof (priv->last_value [0]) * priv->channels) ;
191
192 return ;
193} /* zoh_reset */
194
return
Definition: dirsup.c:529
#define free
Definition: debug_ros.c:5
#define NULL
Definition: types.h:112
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
_Check_return_ _CRT_JIT_INTRINSIC double __cdecl fabs(_In_ double x)
Definition: fabs.c:17
_Check_return_ __CRT_INLINE long lrint(_In_ double x)
Definition: math.h:292
if(dx< 0)
Definition: linetemp.h:194
#define for
Definition: utility.h:88
static UINT UINT * out_count
Definition: clipboard.c:35
#define calloc
Definition: rosglue.h:14
@ SRC_ZERO_ORDER_HOLD
Definition: samplerate.h:162
static int is_bad_src_ratio(double ratio)
Definition: common.h:154
static double fmod_one(double x)
Definition: common.h:143
@ SRC_ERR_NO_ERROR
Definition: common.h:63
@ SRC_ERR_BAD_INTERNAL_STATE
Definition: common.h:86
@ SRC_ERR_BAD_CONVERTER
Definition: common.h:74
@ SRC_ERR_NO_PRIVATE
Definition: common.h:69
@ SRC_ERR_MALLOC_FAILED
Definition: common.h:65
#define SRC_MIN_RATIO_DIFF
Definition: common.h:23
#define memset(x, y, z)
Definition: compat.h:39
const char * zoh_get_description(int src_enum)
Definition: src_zoh.c:137
#define ZOH_MAGIC_MARKER
Definition: src_zoh.c:17
static int zoh_vari_process(SRC_PRIVATE *psrc, SRC_DATA *data)
Definition: src_zoh.c:32
const char * zoh_get_name(int src_enum)
Definition: src_zoh.c:128
static void zoh_reset(SRC_PRIVATE *psrc)
Definition: src_zoh.c:181
int zoh_set_converter(SRC_PRIVATE *psrc, int src_enum)
Definition: src_zoh.c:146
double last_position
Definition: common.h:93
int(* const_process)(struct SRC_PRIVATE_tag *psrc, SRC_DATA *data)
Definition: common.h:108
int(* vari_process)(struct SRC_PRIVATE_tag *psrc, SRC_DATA *data)
Definition: common.h:105
double last_ratio
Definition: common.h:93
void(* reset)(struct SRC_PRIVATE_tag *psrc)
Definition: common.h:111
int channels
Definition: common.h:96
void * private_data
Definition: common.h:102
long in_count
Definition: src_zoh.c:23
long out_count
Definition: src_zoh.c:24
long in_used
Definition: src_zoh.c:23
long out_gen
Definition: src_zoh.c:24
int reset
Definition: src_zoh.c:22
int channels
Definition: src_zoh.c:21
float last_value[1]
Definition: src_zoh.c:25
int zoh_magic_marker
Definition: src_zoh.c:20
ActualNumberDriverObjects * sizeof(PDRIVER_OBJECT)) PDRIVER_OBJECT *DriverObjectList