History

Add ability to clone a SRC_STATE* handle

Peter Hoyes committed 9 years ago

Commit 945b9931 parent b2f7a326 files · +100 −2Browse files

Unified · Split

6 files
  1. src/common.h +3 −0
  2. src/samplerate.c +29 −1
  3. src/samplerate.h +7 −1
  4. src/src_linear.c +20 −0
  5. src/src_sinc.c +21 −0
  6. src/src_zoh.c +20 −0
src/common.h+3−0
@@ -110,6 +110,9 @@ typedef struct SRC_PRIVATE_tag · 109 unmodified lines
110110 /* State reset. */
111111 void (*reset) (struct SRC_PRIVATE_tag *psrc) ;
112112
113+ /* State clone. */
114+ int (*copy) (struct SRC_PRIVATE_tag *from, struct SRC_PRIVATE_tag *to) ;
115+
113116 /* Data specific to SRC_MODE_CALLBACK. */
114117 src_callback_t callback_func ;
115118 void *user_callback_data ;
src/samplerate.c+29−1
@@ -53,6 +53,34 @@ src_new (int converter_type, int channels, int *error) · 52 unmodified lines
5353 return (SRC_STATE*) psrc ;
5454 } /* src_new */
5555
56+SRC_STATE*
57+src_clone (SRC_STATE* orig, int *error)
58+{
59+ SRC_PRIVATE *psrc ;
60+ int copy_error ;
61+
62+ if (error)
63+ *error = SRC_ERR_NO_ERROR ;
64+
65+ if ((psrc = calloc (1, sizeof (*psrc))) == NULL)
66+ { if (error)
67+ *error = SRC_ERR_MALLOC_FAILED ;
68+ return NULL ;
69+ } ;
70+
71+ SRC_PRIVATE *orig_priv = (SRC_PRIVATE*) orig ;
72+ memcpy (psrc, orig_priv, sizeof (SRC_PRIVATE)) ;
73+
74+ if ((copy_error = orig_priv->copy (orig_priv, psrc)) != SRC_ERR_NO_ERROR)
75+ { if (error)
76+ *error = copy_error ;
77+ free (psrc) ;
78+ psrc = NULL ;
79+ } ;
80+
81+ return (SRC_STATE*) psrc ;
82+}
83+
5684 SRC_STATE*
5785 src_callback_new (src_callback_t func, int converter_type, int channels, int *error, void* cb_data)
5886 { SRC_STATE *src_state ;
@@ -245,7 +273,7 @@ src_callback_read (SRC_STATE *state, double src_ratio, long frames, float *data) · 186 unmodified lines
245273
246274 if (error != 0)
247275 { psrc->error = error ;
248− return 0 ;
276+ return 0 ;
249277 } ;
250278
251279 return output_frames_gen ;
src/samplerate.h+7−1
@@ -25,7 +25,7 @@ typedef struct SRC_STATE_tag SRC_STATE ; · 24 unmodified lines
2525 /* SRC_DATA is used to pass data to src_simple() and src_process(). */
2626 typedef struct
2727 { const float *data_in ;
28− float *data_out ;
28+ float *data_out ;
2929
3030 long input_frames, output_frames ;
3131 long input_frames_used, output_frames_gen ;
@@ -54,6 +54,12 @@ typedef long (*src_callback_t) (void *cb_data, float **data) ; · 22 unmodified lines
5454
5555 SRC_STATE* src_new (int converter_type, int channels, int *error) ;
5656
57+/*
58+** Clone a handle : return an anonymous pointer to a new converter
59+** containing the same internal state as orig. Error returned in *error.
60+*/
61+SRC_STATE* src_clone (SRC_STATE* orig, int *error) ;
62+
5763 /*
5864 ** Initilisation for callback based API : return an anonymous pointer to the
5965 ** internal state of the converter. Choose a converter from the enums below.
src/src_linear.c+20−0
@@ -16,6 +16,7 @@ · 15 unmodified lines
1616
1717 static int linear_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data) ;
1818 static void linear_reset (SRC_PRIVATE *psrc) ;
19+static int linear_copy (SRC_PRIVATE *from, SRC_PRIVATE *to) ;
1920
2021 /*========================================================================================
2122 */
@@ -183,6 +184,7 @@ linear_set_converter (SRC_PRIVATE *psrc, int src_enum) · 161 unmodified lines
183184 psrc->const_process = linear_vari_process ;
184185 psrc->vari_process = linear_vari_process ;
185186 psrc->reset = linear_reset ;
187+ psrc->copy = linear_copy ;
186188
187189 linear_reset (psrc) ;
188190
@@ -207,3 +209,21 @@ linear_reset (SRC_PRIVATE *psrc) · 18 unmodified lines
207209 return ;
208210 } /* linear_reset */
209211
212+static int
213+linear_copy (SRC_PRIVATE *from, SRC_PRIVATE *to)
214+{
215+ if (from->private_data == NULL)
216+ return SRC_ERR_NO_PRIVATE ;
217+
218+ LINEAR_DATA *to_priv = NULL ;
219+ LINEAR_DATA* from_priv = (LINEAR_DATA*) from->private_data ;
220+ size_t private_size = sizeof (*to_priv) + from_priv->channels * sizeof (float) ;
221+
222+ if ((to_priv = calloc (1, private_size)) == NULL)
223+ return SRC_ERR_MALLOC_FAILED ;
224+
225+ memcpy (to_priv, from_priv, private_size) ;
226+ to->private_data = to_priv ;
227+
228+ return SRC_ERR_NO_ERROR ;
229+} /* linear_copy */
src/src_sinc.c+21−0
@@ -66,6 +66,7 @@ static int sinc_mono_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data) ; · 65 unmodified lines
6666 static int prepare_data (SINC_FILTER *filter, SRC_DATA *data, int half_filter_chan_len) WARN_UNUSED ;
6767
6868 static void sinc_reset (SRC_PRIVATE *psrc) ;
69+static int sinc_copy (SRC_PRIVATE *from, SRC_PRIVATE *to) ;
6970
7071 static inline increment_t
7172 double_to_fp (double x)
@@ -181,6 +182,7 @@ sinc_set_converter (SRC_PRIVATE *psrc, int src_enum) · 109 unmodified lines
181182 psrc->vari_process = sinc_multichan_vari_process ;
182183 } ;
183184 psrc->reset = sinc_reset ;
185+ psrc->copy = sinc_copy ;
184186
185187 switch (src_enum)
186188 { case SRC_SINC_FASTEST :
@@ -253,6 +255,25 @@ sinc_reset (SRC_PRIVATE *psrc) · 66 unmodified lines
253255 memset (filter->buffer + filter->b_len, 0xAA, filter->channels * sizeof (filter->buffer [0])) ;
254256 } /* sinc_reset */
255257
258+static int
259+sinc_copy (SRC_PRIVATE *from, SRC_PRIVATE *to)
260+{
261+ if (from->private_data == NULL)
262+ return SRC_ERR_NO_PRIVATE ;
263+
264+ SINC_FILTER *to_filter = NULL ;
265+ SINC_FILTER* from_filter = (SINC_FILTER*) from->private_data ;
266+ size_t private_length = sizeof (SINC_FILTER) + sizeof (from_filter->buffer [0]) * (from_filter->b_len + from_filter->channels) ;
267+
268+ if ((to_filter = calloc (1, private_length)) == NULL)
269+ return SRC_ERR_MALLOC_FAILED ;
270+
271+ memcpy (to_filter, from_filter, private_length) ;
272+ to->private_data = to_filter ;
273+
274+ return SRC_ERR_NO_ERROR ;
275+} /* sinc_copy */
276+
256277 /*========================================================================================
257278 ** Beware all ye who dare pass this point. There be dragons here.
258279 */
src/src_zoh.c+20−0
@@ -16,6 +16,7 @@ · 15 unmodified lines
1616
1717 static int zoh_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data) ;
1818 static void zoh_reset (SRC_PRIVATE *psrc) ;
19+static int zoh_copy (SRC_PRIVATE *from, SRC_PRIVATE *to) ;
1920
2021 /*========================================================================================
2122 */
@@ -174,6 +175,7 @@ zoh_set_converter (SRC_PRIVATE *psrc, int src_enum) · 152 unmodified lines
174175 psrc->const_process = zoh_vari_process ;
175176 psrc->vari_process = zoh_vari_process ;
176177 psrc->reset = zoh_reset ;
178+ psrc->copy = zoh_copy ;
177179
178180 zoh_reset (psrc) ;
179181
@@ -198,3 +200,21 @@ zoh_reset (SRC_PRIVATE *psrc) · 18 unmodified lines
198200 return ;
199201 } /* zoh_reset */
200202
203+static int
204+zoh_copy (SRC_PRIVATE *from, SRC_PRIVATE *to)
205+{
206+ if (from->private_data == NULL)
207+ return SRC_ERR_NO_PRIVATE ;
208+
209+ ZOH_DATA *to_priv = NULL ;
210+ ZOH_DATA* from_priv = (ZOH_DATA*) from->private_data ;
211+ size_t private_size = sizeof (*to_priv) + from_priv->channels * sizeof (float) ;
212+
213+ if ((to_priv = calloc (1, private_size)) == NULL)
214+ return SRC_ERR_MALLOC_FAILED ;
215+
216+ memcpy (to_priv, from_priv, private_size) ;
217+ to->private_data = to_priv ;
218+
219+ return SRC_ERR_NO_ERROR ;
220+} /* zoh_copy */

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