History

Move SRC_STATE function pointers to virtual table

evpobr committed 6 years ago

Commit 71b0a3b1 parent 26d92c35 files · +95 −59Browse files

Unified · Split

5 files
  1. src/common.h +18 −12
  2. src/samplerate.c +7 −14
  3. src/src_linear.c +10 −5
  4. src/src_sinc.c +50 −23
  5. src/src_zoh.c +10 −5
src/common.h+18−12
@@ -96,18 +96,8 @@ typedef enum SRC_ERROR · 95 unmodified lines
96 SRC_ERR_MAX_ERROR96 SRC_ERR_MAX_ERROR
97} SRC_ERROR ;97} SRC_ERROR ;
98 98
99struct SRC_STATE_tag99typedef struct SRC_STATE_VT_tag
100{ double last_ratio, last_position ;100{
101
102 SRC_ERROR error ;
103 int channels ;
104
105 /* SRC_MODE_PROCESS or SRC_MODE_CALLBACK */
106 enum SRC_MODE mode ;
107
108 /* Pointer to data to converter specific data. */
109 void *private_data ;
110
111 /* Varispeed process function. */101 /* Varispeed process function. */
112 SRC_ERROR (*vari_process) (SRC_STATE *state, SRC_DATA *data) ;102 SRC_ERROR (*vari_process) (SRC_STATE *state, SRC_DATA *data) ;
113 103
@@ -122,6 +112,22 @@ struct SRC_STATE_tag · 8 unmodified lines
122 112
123 /* State private_data close. */113 /* State private_data close. */
124 void (*close) (SRC_STATE *state) ;114 void (*close) (SRC_STATE *state) ;
115} SRC_STATE_VT ;
116
117struct SRC_STATE_tag
118{
119 SRC_STATE_VT *vt ;
120
121 double last_ratio, last_position ;
122
123 SRC_ERROR error ;
124 int channels ;
125
126 /* SRC_MODE_PROCESS or SRC_MODE_CALLBACK */
127 enum SRC_MODE mode ;
128
129 /* Pointer to data to converter specific data. */
130 void *private_data ;
125 131
126 /* Data specific to SRC_MODE_CALLBACK. */132 /* Data specific to SRC_MODE_CALLBACK. */
127 src_callback_t callback_func ;133 src_callback_t callback_func ;
src/samplerate.c+7−14
@@ -43,7 +43,7 @@ src_clone (SRC_STATE* orig, int *error) · 42 unmodified lines
43 SRC_STATE *orig_state = orig ;43 SRC_STATE *orig_state = orig ;
44 memcpy (state, orig_state, sizeof (SRC_STATE)) ;44 memcpy (state, orig_state, sizeof (SRC_STATE)) ;
45 45
46 if ((copy_error = orig_state->copy (orig_state, state)) != SRC_ERR_NO_ERROR)46 if ((copy_error = orig_state->vt->copy (orig_state, state)) != SRC_ERR_NO_ERROR)
47 { if (error)47 { if (error)
48 *error = copy_error ;48 *error = copy_error ;
49 free (state) ;49 free (state) ;
@@ -82,11 +82,11 @@ SRC_STATE * · 32 unmodified lines
82src_delete (SRC_STATE *state)82src_delete (SRC_STATE *state)
83{83{
84 if (state)84 if (state)
85 { if (state->close)85 {
86 state->close (state) ;86 state->vt->close (state) ;
87 memset (state, 0, sizeof (SRC_STATE)) ;87 memset (state, 0, sizeof (SRC_STATE)) ;
88 free (state) ;88 free (state) ;
89 } ;89 }
90 90
91 return NULL ;91 return NULL ;
92} /* src_state */92} /* src_state */
@@ -98,8 +98,6 @@ src_process (SRC_STATE *state, SRC_DATA *data) · 5 unmodified lines
98 98
99 if (state == NULL)99 if (state == NULL)
100 return SRC_ERR_BAD_STATE ;100 return SRC_ERR_BAD_STATE ;
101 if (state->vari_process == NULL || state->const_process == NULL)
102 return SRC_ERR_BAD_PROC_PTR ;
103 101
104 if (state->mode != SRC_MODE_PROCESS)102 if (state->mode != SRC_MODE_PROCESS)
105 return SRC_ERR_BAD_MODE ;103 return SRC_ERR_BAD_MODE ;
@@ -147,9 +145,9 @@ src_process (SRC_STATE *state, SRC_DATA *data) · 41 unmodified lines
147 145
148 /* Now process. */146 /* Now process. */
149 if (fabs (state->last_ratio - data->src_ratio) < 1e-15)147 if (fabs (state->last_ratio - data->src_ratio) < 1e-15)
150 error = state->const_process (state, data) ;148 error = state->vt->const_process (state, data) ;
151 else149 else
152 error = state->vari_process (state, data) ;150 error = state->vt->vari_process (state, data) ;
153 151
154 return error ;152 return error ;
155} /* src_process */153} /* src_process */
@@ -254,8 +252,6 @@ src_set_ratio (SRC_STATE *state, double new_ratio) · 98 unmodified lines
254{252{
255 if (state == NULL)253 if (state == NULL)
256 return SRC_ERR_BAD_STATE ;254 return SRC_ERR_BAD_STATE ;
257 if (state->vari_process == NULL || state->const_process == NULL)
258 return SRC_ERR_BAD_PROC_PTR ;
259 255
260 if (is_bad_src_ratio (new_ratio))256 if (is_bad_src_ratio (new_ratio))
261 return SRC_ERR_BAD_SRC_RATIO ;257 return SRC_ERR_BAD_SRC_RATIO ;
@@ -270,8 +266,6 @@ src_get_channels (SRC_STATE *state) · 8 unmodified lines
270{266{
271 if (state == NULL)267 if (state == NULL)
272 return -SRC_ERR_BAD_STATE ;268 return -SRC_ERR_BAD_STATE ;
273 if (state->vari_process == NULL || state->const_process == NULL)
274 return -SRC_ERR_BAD_PROC_PTR ;
275 269
276 return state->channels ;270 return state->channels ;
277} /* src_get_channels */271} /* src_get_channels */
@@ -282,8 +276,7 @@ src_reset (SRC_STATE *state) · 4 unmodified lines
282 if (state == NULL)276 if (state == NULL)
283 return SRC_ERR_BAD_STATE ;277 return SRC_ERR_BAD_STATE ;
284 278
285 if (state->reset != NULL)279 state->vt->reset (state) ;
286 state->reset (state) ;
287 280
288 state->last_position = 0.0 ;281 state->last_position = 0.0 ;
289 state->last_ratio = 0.0 ;282 state->last_ratio = 0.0 ;
src/src_linear.c+10−5
@@ -35,6 +35,15 @@ typedef struct · 34 unmodified lines
35 float *last_value ;35 float *last_value ;
36} LINEAR_DATA ;36} LINEAR_DATA ;
37 37
38static SRC_STATE_VT linear_state_vt =
39{
40 linear_vari_process,
41 linear_vari_process,
42 linear_reset,
43 linear_copy,
44 linear_close
45} ;
46
38/*----------------------------------------------------------------------------------------47/*----------------------------------------------------------------------------------------
39*/48*/
40 49
@@ -203,11 +212,7 @@ linear_state_new (int channels, SRC_ERROR *error) · 162 unmodified lines
203 return NULL ;212 return NULL ;
204 }213 }
205 214
206 state->const_process = linear_vari_process ;215 state->vt = &linear_state_vt ;
207 state->vari_process = linear_vari_process ;
208 state->reset = linear_reset ;
209 state->copy = linear_copy ;
210 state->close = linear_close ;
211 216
212 linear_reset (state) ;217 linear_reset (state) ;
213 218
src/src_sinc.c+50−23
@@ -69,6 +69,51 @@ static void sinc_reset (SRC_STATE *state) ; · 68 unmodified lines
69static SRC_ERROR sinc_copy (SRC_STATE *from, SRC_STATE *to) ;69static SRC_ERROR sinc_copy (SRC_STATE *from, SRC_STATE *to) ;
70static void sinc_close (SRC_STATE *state) ;70static void sinc_close (SRC_STATE *state) ;
71 71
72static SRC_STATE_VT sinc_multichan_state_vt =
73{
74 sinc_multichan_vari_process,
75 sinc_multichan_vari_process,
76 sinc_reset,
77 sinc_copy,
78 sinc_close
79} ;
80
81static SRC_STATE_VT sinc_hex_state_vt =
82{
83 sinc_hex_vari_process,
84 sinc_hex_vari_process,
85 sinc_reset,
86 sinc_copy,
87 sinc_close
88} ;
89
90static SRC_STATE_VT sinc_quad_state_vt =
91{
92 sinc_quad_vari_process,
93 sinc_quad_vari_process,
94 sinc_reset,
95 sinc_copy,
96 sinc_close
97} ;
98
99static SRC_STATE_VT sinc_stereo_state_vt =
100{
101 sinc_stereo_vari_process,
102 sinc_stereo_vari_process,
103 sinc_reset,
104 sinc_copy,
105 sinc_close
106} ;
107
108static SRC_STATE_VT sinc_mono_state_vt =
109{
110 sinc_mono_vari_process,
111 sinc_mono_vari_process,
112 sinc_reset,
113 sinc_copy,
114 sinc_close
115} ;
116
72static inline increment_t117static inline increment_t
73double_to_fp (double x)118double_to_fp (double x)
74{ return (increment_t) (lrint ((x) * FP_ONE)) ;119{ return (increment_t) (lrint ((x) * FP_ONE)) ;
@@ -225,33 +270,15 @@ sinc_state_new (int converter_type, int channels, SRC_ERROR *error) · 150 unmodified lines
225 state->mode = SRC_MODE_PROCESS ;270 state->mode = SRC_MODE_PROCESS ;
226 271
227 if (state->channels == 1)272 if (state->channels == 1)
228 {273 state->vt = &sinc_mono_state_vt ;
229 state->const_process = sinc_mono_vari_process ;
230 state->vari_process = sinc_mono_vari_process ;
231 }
232 else if (state->channels == 2)274 else if (state->channels == 2)
233 {275 state->vt = &sinc_stereo_state_vt ;
234 state->const_process = sinc_stereo_vari_process ;
235 state->vari_process = sinc_stereo_vari_process ;
236 }
237 else if (state->channels == 4)276 else if (state->channels == 4)
238 {277 state->vt = &sinc_quad_state_vt ;
239 state->const_process = sinc_quad_vari_process ;
240 state->vari_process = sinc_quad_vari_process ;
241 }
242 else if (state->channels == 6)278 else if (state->channels == 6)
243 {279 state->vt = &sinc_hex_state_vt ;
244 state->const_process = sinc_hex_vari_process ;
245 state->vari_process = sinc_hex_vari_process ;
246 }
247 else280 else
248 {281 state->vt = &sinc_multichan_state_vt ;
249 state->const_process = sinc_multichan_vari_process ;
250 state->vari_process = sinc_multichan_vari_process ;
251 }
252 state->reset = sinc_reset ;
253 state->copy = sinc_copy ;
254 state->close = sinc_close ;
255 282
256 state->private_data = sinc_filter_new (converter_type, state->channels) ;283 state->private_data = sinc_filter_new (converter_type, state->channels) ;
257 if (!state->private_data)284 if (!state->private_data)
src/src_zoh.c+10−5
@@ -33,6 +33,15 @@ typedef struct · 32 unmodified lines
33 float *last_value ;33 float *last_value ;
34} ZOH_DATA ;34} ZOH_DATA ;
35 35
36static SRC_STATE_VT zoh_state_vt =
37{
38 zoh_vari_process,
39 zoh_vari_process,
40 zoh_reset,
41 zoh_copy,
42 zoh_close
43} ;
44
36/*----------------------------------------------------------------------------------------45/*----------------------------------------------------------------------------------------
37*/46*/
38 47
@@ -194,11 +203,7 @@ zoh_state_new (int channels, SRC_ERROR *error) · 155 unmodified lines
194 return NULL ;203 return NULL ;
195 }204 }
196 205
197 state->const_process = zoh_vari_process ;206 state->vt = &zoh_state_vt ;
198 state->vari_process = zoh_vari_process ;
199 state->reset = zoh_reset ;
200 state->copy = zoh_copy ;
201 state->close = zoh_close ;
202 207
203 zoh_reset (state) ;208 zoh_reset (state) ;
204 209

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t