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
9696 SRC_ERR_MAX_ERROR
9797 } SRC_ERROR ;
9898
99−struct SRC_STATE_tag
100−{ double last_ratio, last_position ;
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−
99+typedef struct SRC_STATE_VT_tag
100+{
111101 /* Varispeed process function. */
112102 SRC_ERROR (*vari_process) (SRC_STATE *state, SRC_DATA *data) ;
113103
@@ -122,6 +112,22 @@ struct SRC_STATE_tag · 8 unmodified lines
122112
123113 /* State private_data close. */
124114 void (*close) (SRC_STATE *state) ;
115+} SRC_STATE_VT ;
116+
117+struct 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 ;
125131
126132 /* Data specific to SRC_MODE_CALLBACK. */
127133 src_callback_t callback_func ;
src/samplerate.c+7−14
@@ -43,7 +43,7 @@ src_clone (SRC_STATE* orig, int *error) · 42 unmodified lines
4343 SRC_STATE *orig_state = orig ;
4444 memcpy (state, orig_state, sizeof (SRC_STATE)) ;
4545
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)
4747 { if (error)
4848 *error = copy_error ;
4949 free (state) ;
@@ -82,11 +82,11 @@ SRC_STATE * · 32 unmodified lines
8282 src_delete (SRC_STATE *state)
8383 {
8484 if (state)
85− { if (state->close)
86− state->close (state) ;
85+ {
86+ state->vt->close (state) ;
8787 memset (state, 0, sizeof (SRC_STATE)) ;
8888 free (state) ;
89− } ;
89+ }
9090
9191 return NULL ;
9292 } /* src_state */
@@ -98,8 +98,6 @@ src_process (SRC_STATE *state, SRC_DATA *data) · 5 unmodified lines
9898
9999 if (state == NULL)
100100 return SRC_ERR_BAD_STATE ;
101− if (state->vari_process == NULL || state->const_process == NULL)
102− return SRC_ERR_BAD_PROC_PTR ;
103101
104102 if (state->mode != SRC_MODE_PROCESS)
105103 return SRC_ERR_BAD_MODE ;
@@ -147,9 +145,9 @@ src_process (SRC_STATE *state, SRC_DATA *data) · 41 unmodified lines
147145
148146 /* Now process. */
149147 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) ;
151149 else
152− error = state->vari_process (state, data) ;
150+ error = state->vt->vari_process (state, data) ;
153151
154152 return error ;
155153 } /* src_process */
@@ -254,8 +252,6 @@ src_set_ratio (SRC_STATE *state, double new_ratio) · 98 unmodified lines
254252 {
255253 if (state == NULL)
256254 return SRC_ERR_BAD_STATE ;
257− if (state->vari_process == NULL || state->const_process == NULL)
258− return SRC_ERR_BAD_PROC_PTR ;
259255
260256 if (is_bad_src_ratio (new_ratio))
261257 return SRC_ERR_BAD_SRC_RATIO ;
@@ -270,8 +266,6 @@ src_get_channels (SRC_STATE *state) · 8 unmodified lines
270266 {
271267 if (state == NULL)
272268 return -SRC_ERR_BAD_STATE ;
273− if (state->vari_process == NULL || state->const_process == NULL)
274− return -SRC_ERR_BAD_PROC_PTR ;
275269
276270 return state->channels ;
277271 } /* src_get_channels */
@@ -282,8 +276,7 @@ src_reset (SRC_STATE *state) · 4 unmodified lines
282276 if (state == NULL)
283277 return SRC_ERR_BAD_STATE ;
284278
285− if (state->reset != NULL)
286− state->reset (state) ;
279+ state->vt->reset (state) ;
287280
288281 state->last_position = 0.0 ;
289282 state->last_ratio = 0.0 ;
src/src_linear.c+10−5
@@ -35,6 +35,15 @@ typedef struct · 34 unmodified lines
3535 float *last_value ;
3636 } LINEAR_DATA ;
3737
38+static 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+
3847 /*----------------------------------------------------------------------------------------
3948 */
4049
@@ -203,11 +212,7 @@ linear_state_new (int channels, SRC_ERROR *error) · 162 unmodified lines
203212 return NULL ;
204213 }
205214
206− state->const_process = linear_vari_process ;
207− state->vari_process = linear_vari_process ;
208− state->reset = linear_reset ;
209− state->copy = linear_copy ;
210− state->close = linear_close ;
215+ state->vt = &linear_state_vt ;
211216
212217 linear_reset (state) ;
213218
src/src_sinc.c+50−23
@@ -69,6 +69,51 @@ static void sinc_reset (SRC_STATE *state) ; · 68 unmodified lines
6969 static SRC_ERROR sinc_copy (SRC_STATE *from, SRC_STATE *to) ;
7070 static void sinc_close (SRC_STATE *state) ;
7171
72+static 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+
81+static 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+
90+static 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+
99+static 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+
108+static 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+
72117 static inline increment_t
73118 double_to_fp (double x)
74119 { 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
225270 state->mode = SRC_MODE_PROCESS ;
226271
227272 if (state->channels == 1)
228− {
229− state->const_process = sinc_mono_vari_process ;
230− state->vari_process = sinc_mono_vari_process ;
231− }
273+ state->vt = &sinc_mono_state_vt ;
232274 else if (state->channels == 2)
233− {
234− state->const_process = sinc_stereo_vari_process ;
235− state->vari_process = sinc_stereo_vari_process ;
236− }
275+ state->vt = &sinc_stereo_state_vt ;
237276 else if (state->channels == 4)
238− {
239− state->const_process = sinc_quad_vari_process ;
240− state->vari_process = sinc_quad_vari_process ;
241− }
277+ state->vt = &sinc_quad_state_vt ;
242278 else if (state->channels == 6)
243− {
244− state->const_process = sinc_hex_vari_process ;
245− state->vari_process = sinc_hex_vari_process ;
246− }
279+ state->vt = &sinc_hex_state_vt ;
247280 else
248− {
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 ;
281+ state->vt = &sinc_multichan_state_vt ;
255282
256283 state->private_data = sinc_filter_new (converter_type, state->channels) ;
257284 if (!state->private_data)
src/src_zoh.c+10−5
@@ -33,6 +33,15 @@ typedef struct · 32 unmodified lines
3333 float *last_value ;
3434 } ZOH_DATA ;
3535
36+static 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+
3645 /*----------------------------------------------------------------------------------------
3746 */
3847
@@ -194,11 +203,7 @@ zoh_state_new (int channels, SRC_ERROR *error) · 155 unmodified lines
194203 return NULL ;
195204 }
196205
197− state->const_process = zoh_vari_process ;
198− state->vari_process = zoh_vari_process ;
199− state->reset = zoh_reset ;
200− state->copy = zoh_copy ;
201− state->close = zoh_close ;
206+ state->vt = &zoh_state_vt ;
202207
203208 zoh_reset (state) ;
204209

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