ReactOS 0.4.15-dev-7924-g5949c20
src_linear.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 linear_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data) ;
12static void linear_reset (SRC_PRIVATE *psrc) ;
13
14/*========================================================================================
15*/
16
17#define LINEAR_MAGIC_MARKER MAKE_MAGIC ('l', 'i', 'n', 'e', 'a', 'r')
18
19#define SRC_DEBUG 0
20
21typedef struct
24 int reset ;
25 long in_count, in_used ;
26 long out_count, out_gen ;
27 float last_value [1] ;
29
30/*----------------------------------------------------------------------------------------
31*/
32
33static int
35{ LINEAR_DATA *priv ;
36 double src_ratio, input_index, rem ;
37 int ch ;
38
39 if (data->input_frames <= 0)
40 return SRC_ERR_NO_ERROR ;
41
42 if (psrc->private_data == NULL)
43 return SRC_ERR_NO_PRIVATE ;
44
45 priv = (LINEAR_DATA*) psrc->private_data ;
46
47 if (priv->reset)
48 { /* If we have just been reset, set the last_value data. */
49 for (ch = 0 ; ch < priv->channels ; ch++)
50 priv->last_value [ch] = data->data_in [ch] ;
51 priv->reset = 0 ;
52 } ;
53
54 priv->in_count = data->input_frames * priv->channels ;
55 priv->out_count = data->output_frames * priv->channels ;
56 priv->in_used = priv->out_gen = 0 ;
57
58 src_ratio = psrc->last_ratio ;
59
60 if (is_bad_src_ratio (src_ratio))
62
63 input_index = psrc->last_position ;
64
65 /* Calculate samples before first sample in input array. */
66 while (input_index < 1.0 && priv->out_gen < priv->out_count)
67 {
68 if (priv->in_used + priv->channels * (1.0 + input_index) >= priv->in_count)
69 break ;
70
71 if (priv->out_count > 0 && fabs (psrc->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
72 src_ratio = psrc->last_ratio + priv->out_gen * (data->src_ratio - psrc->last_ratio) / priv->out_count ;
73
74 for (ch = 0 ; ch < priv->channels ; ch++)
75 { data->data_out [priv->out_gen] = (float) (priv->last_value [ch] + input_index *
76 (data->data_in [ch] - priv->last_value [ch])) ;
77 priv->out_gen ++ ;
78 } ;
79
80 /* Figure out the next index. */
81 input_index += 1.0 / src_ratio ;
82 } ;
83
84 rem = fmod_one (input_index) ;
85 priv->in_used += priv->channels * lrint (input_index - rem) ;
86 input_index = rem ;
87
88 /* Main processing loop. */
89 while (priv->out_gen < priv->out_count && priv->in_used + priv->channels * input_index < priv->in_count)
90 {
91 if (priv->out_count > 0 && fabs (psrc->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
92 src_ratio = psrc->last_ratio + priv->out_gen * (data->src_ratio - psrc->last_ratio) / priv->out_count ;
93
94 if (SRC_DEBUG && priv->in_used < priv->channels && input_index < 1.0)
95 { printf ("Whoops!!!! in_used : %ld channels : %d input_index : %f\n", priv->in_used, priv->channels, input_index) ;
96 exit (1) ;
97 } ;
98
99 for (ch = 0 ; ch < priv->channels ; ch++)
100 { data->data_out [priv->out_gen] = (float) (data->data_in [priv->in_used - priv->channels + ch] + input_index *
101 (data->data_in [priv->in_used + ch] - data->data_in [priv->in_used - priv->channels + ch])) ;
102 priv->out_gen ++ ;
103 } ;
104
105 /* Figure out the next index. */
106 input_index += 1.0 / src_ratio ;
107 rem = fmod_one (input_index) ;
108
109 priv->in_used += priv->channels * lrint (input_index - rem) ;
110 input_index = rem ;
111 } ;
112
113 if (priv->in_used > priv->in_count)
114 { input_index += (priv->in_used - priv->in_count) / priv->channels ;
115 priv->in_used = priv->in_count ;
116 } ;
117
118 psrc->last_position = input_index ;
119
120 if (priv->in_used > 0)
121 for (ch = 0 ; ch < priv->channels ; ch++)
122 priv->last_value [ch] = data->data_in [priv->in_used - priv->channels + ch] ;
123
124 /* Save current ratio rather then target ratio. */
125 psrc->last_ratio = src_ratio ;
126
127 data->input_frames_used = priv->in_used / priv->channels ;
128 data->output_frames_gen = priv->out_gen / priv->channels ;
129
130 return SRC_ERR_NO_ERROR ;
131} /* linear_vari_process */
132
133/*------------------------------------------------------------------------------
134*/
135
136const char*
137linear_get_name (int src_enum)
138{
139 if (src_enum == SRC_LINEAR)
140 return "Linear Interpolator" ;
141
142 return NULL ;
143} /* linear_get_name */
144
145const char*
147{
148 if (src_enum == SRC_LINEAR)
149 return "Linear interpolator, very fast, poor quality." ;
150
151 return NULL ;
152} /* linear_get_descrition */
153
154int
156{ LINEAR_DATA *priv = NULL ;
157
158 if (src_enum != SRC_LINEAR)
159 return SRC_ERR_BAD_CONVERTER ;
160
161 if (psrc->private_data != NULL)
162 { free (psrc->private_data) ;
163 psrc->private_data = NULL ;
164 } ;
165
166 if (psrc->private_data == NULL)
167 { priv = calloc (1, sizeof (*priv) + psrc->channels * sizeof (float)) ;
168 psrc->private_data = priv ;
169 } ;
170
171 if (priv == NULL)
172 return SRC_ERR_MALLOC_FAILED ;
173
175 priv->channels = psrc->channels ;
176
179 psrc->reset = linear_reset ;
180
181 linear_reset (psrc) ;
182
183 return SRC_ERR_NO_ERROR ;
184} /* linear_set_converter */
185
186/*===================================================================================
187*/
188
189static void
191{ LINEAR_DATA *priv = NULL ;
192
193 priv = (LINEAR_DATA*) psrc->private_data ;
194 if (priv == NULL)
195 return ;
196
197 priv->channels = psrc->channels ;
198 priv->reset = 1 ;
199 memset (priv->last_value, 0, sizeof (priv->last_value [0]) * priv->channels) ;
200
201 return ;
202} /* linear_reset */
203
return
Definition: dirsup.c:529
#define free
Definition: debug_ros.c:5
#define NULL
Definition: types.h:112
#define printf
Definition: freeldr.h:93
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
static float(__cdecl *square_half_float)(float x
static UINT UINT * out_count
Definition: clipboard.c:35
#define calloc
Definition: rosglue.h:14
@ SRC_LINEAR
Definition: samplerate.h:163
static int is_bad_src_ratio(double ratio)
Definition: common.h:154
@ 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
static double fmod_one(double x)
Definition: common.h:143
#define SRC_MIN_RATIO_DIFF
Definition: common.h:23
#define exit(n)
Definition: config.h:202
#define memset(x, y, z)
Definition: compat.h:39
#define LINEAR_MAGIC_MARKER
Definition: src_linear.c:17
static int linear_vari_process(SRC_PRIVATE *psrc, SRC_DATA *data)
Definition: src_linear.c:34
int linear_set_converter(SRC_PRIVATE *psrc, int src_enum)
Definition: src_linear.c:155
#define SRC_DEBUG
Definition: src_linear.c:19
const char * linear_get_name(int src_enum)
Definition: src_linear.c:137
static void linear_reset(SRC_PRIVATE *psrc)
Definition: src_linear.c:190
const char * linear_get_description(int src_enum)
Definition: src_linear.c:146
int linear_magic_marker
Definition: src_linear.c:22
long out_count
Definition: src_linear.c:26
long in_count
Definition: src_linear.c:25
int channels
Definition: src_linear.c:23
long out_gen
Definition: src_linear.c:26
float last_value[1]
Definition: src_linear.c:27
long in_used
Definition: src_linear.c:25
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
ActualNumberDriverObjects * sizeof(PDRIVER_OBJECT)) PDRIVER_OBJECT *DriverObjectList