Skip to content

Commit 445a090

Browse files
committed
Introduce single precision float compilation option
This patch aims to add possibility for library to be compiled using single precision floating point numbers. This allows to use this library on embedded systems, where FPU may not support double precision floating point math, thus, is resource intensive and to slow for audio processing.
1 parent 20819b6 commit 445a090

File tree

6 files changed

+51
-40
lines changed

6 files changed

+51
-40
lines changed

include/samplerate.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
extern "C" {
1919
#endif /* __cplusplus */
2020

21+
#ifdef LIBSAMPLERATE_SINGLE_PRECISION
22+
typedef float fp_t;
23+
#else
24+
typedef double fp_t;
25+
#endif
2126

2227
/* Opaque data type SRC_STATE. */
2328
typedef struct SRC_STATE_tag SRC_STATE ;
@@ -32,7 +37,7 @@ typedef struct
3237

3338
int end_of_input ;
3439

35-
double src_ratio ;
40+
fp_t src_ratio ;
3641
} SRC_DATA ;
3742

3843
/*
@@ -89,7 +94,7 @@ int src_process (SRC_STATE *state, SRC_DATA *data) ;
8994
** Callback based processing function. Read up to frames worth of data from
9095
** the converter int *data and return frames read or -1 on error.
9196
*/
92-
long src_callback_read (SRC_STATE *state, double src_ratio, long frames, float *data) ;
97+
long src_callback_read (SRC_STATE *state, fp_t src_ratio, long frames, float *data) ;
9398

9499
/*
95100
** Simple interface for performing a single conversion from input buffer to
@@ -119,7 +124,7 @@ const char *src_get_version (void) ;
119124
** Returns non zero on error.
120125
*/
121126

122-
int src_set_ratio (SRC_STATE *state, double new_ratio) ;
127+
int src_set_ratio (SRC_STATE *state, fp_t new_ratio) ;
123128

124129
/*
125130
** Get the current channel count.
@@ -142,7 +147,7 @@ int src_reset (SRC_STATE *state) ;
142147
** otherwise.
143148
*/
144149

145-
int src_is_valid_ratio (double ratio) ;
150+
int src_is_valid_ratio (fp_t ratio) ;
146151

147152
/*
148153
** Return an error number.

src/common.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
#ifndef COMMON_H_INCLUDED
1010
#define COMMON_H_INCLUDED
1111

12+
#ifdef LIBSAMPLERATE_SINGLE_PRECISION
13+
typedef float fp_t;
14+
#else
15+
typedef double fp_t;
16+
#endif
17+
1218
#include <stdint.h>
1319
#ifdef HAVE_STDBOOL_H
1420
#include <stdbool.h>
@@ -156,7 +162,7 @@ struct SRC_STATE_tag
156162
{
157163
SRC_STATE_VT *vt ;
158164

159-
double last_ratio, last_position ;
165+
fp_t last_ratio, last_position ;
160166

161167
SRC_ERROR error ;
162168
int channels ;
@@ -209,7 +215,7 @@ static inline int
209215
#ifdef USE_TARGET_ATTRIBUTE
210216
__attribute__((target("sse2")))
211217
#endif
212-
psf_lrint (double x)
218+
psf_lrint (fp_t x)
213219
{
214220
return _mm_cvtsd_si32 (_mm_load_sd (&x)) ;
215221
}
@@ -221,7 +227,7 @@ static inline int psf_lrintf (float x)
221227
return lrintf (x) ;
222228
} /* psf_lrintf */
223229

224-
static inline int psf_lrint (double x)
230+
static inline int psf_lrint (fp_t x)
225231
{
226232
return lrint (x) ;
227233
} /* psf_lrint */
@@ -231,9 +237,9 @@ static inline int psf_lrint (double x)
231237
** Common static inline functions.
232238
*/
233239

234-
static inline double
235-
fmod_one (double x)
236-
{ double res ;
240+
static inline fp_t
241+
fmod_one (fp_t x)
242+
{ fp_t res ;
237243

238244
res = x - psf_lrint (x) ;
239245
if (res < 0.0)
@@ -243,7 +249,7 @@ fmod_one (double x)
243249
} /* fmod_one */
244250

245251
static inline int
246-
is_bad_src_ratio (double ratio)
252+
is_bad_src_ratio (fp_t ratio)
247253
{ return (ratio < (1.0 / SRC_MAX_RATIO) || ratio > (1.0 * SRC_MAX_RATIO)) ;
248254
} /* is_bad_src_ratio */
249255

src/samplerate.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ src_process (SRC_STATE *state, SRC_DATA *data)
143143
} /* src_process */
144144

145145
long
146-
src_callback_read (SRC_STATE *state, double src_ratio, long frames, float *data)
146+
src_callback_read (SRC_STATE *state, fp_t src_ratio, long frames, float *data)
147147
{
148148
SRC_DATA src_data ;
149149

@@ -238,7 +238,7 @@ src_callback_read (SRC_STATE *state, double src_ratio, long frames, float *data)
238238
*/
239239

240240
int
241-
src_set_ratio (SRC_STATE *state, double new_ratio)
241+
src_set_ratio (SRC_STATE *state, fp_t new_ratio)
242242
{
243243
if (state == NULL)
244244
return SRC_ERR_BAD_STATE ;
@@ -321,7 +321,7 @@ src_get_version (void)
321321
} /* src_get_version */
322322

323323
int
324-
src_is_valid_ratio (double ratio)
324+
src_is_valid_ratio (fp_t ratio)
325325
{
326326
if (is_bad_src_ratio (ratio))
327327
return SRC_FALSE ;
@@ -461,7 +461,7 @@ src_int_to_float_array (const int *in, float *out, int len)
461461

462462
void
463463
src_float_to_int_array (const float *in, int *out, int len)
464-
{ double scaled_value ;
464+
{ fp_t scaled_value ;
465465

466466
for (int i = 0 ; i < len ; i++)
467467
{ scaled_value = in [i] * (8.0 * 0x10000000) ;

src/src_linear.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static SRC_STATE_VT linear_state_vt =
5353
static SRC_ERROR
5454
linear_vari_process (SRC_STATE *state, SRC_DATA *data)
5555
{ LINEAR_DATA *priv ;
56-
double src_ratio, input_index, rem ;
56+
fp_t src_ratio, input_index, rem ;
5757
int ch ;
5858

5959
if (data->input_frames <= 0)
@@ -93,7 +93,7 @@ linear_vari_process (SRC_STATE *state, SRC_DATA *data)
9393

9494
for (ch = 0 ; ch < state->channels ; ch++)
9595
{ data->data_out [priv->out_gen] = (float) (priv->last_value [ch] + input_index *
96-
((double) data->data_in [ch] - priv->last_value [ch])) ;
96+
((fp_t) data->data_in [ch] - priv->last_value [ch])) ;
9797
priv->out_gen ++ ;
9898
} ;
9999

@@ -120,7 +120,7 @@ linear_vari_process (SRC_STATE *state, SRC_DATA *data)
120120

121121
for (ch = 0 ; ch < state->channels ; ch++)
122122
{ data->data_out [priv->out_gen] = (float) (data->data_in [priv->in_used - state->channels + ch] + input_index *
123-
((double) data->data_in [priv->in_used + ch] - data->data_in [priv->in_used - state->channels + ch])) ;
123+
((fp_t) data->data_in [priv->in_used + ch] - data->data_in [priv->in_used - state->channels + ch])) ;
124124
priv->out_gen ++ ;
125125
} ;
126126

src/src_sinc.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#define MAKE_INCREMENT_T(x) ((increment_t) (x))
2727

2828
#define SHIFT_BITS 12
29-
#define FP_ONE ((double) (((increment_t) 1) << SHIFT_BITS))
29+
#define FP_ONE ((fp_t) (((increment_t) 1) << SHIFT_BITS))
3030
#define INV_FP_ONE (1.0 / FP_ONE)
3131

3232
/* Customixe max channls from Kconfig. */
@@ -61,14 +61,14 @@ typedef struct
6161

6262
int coeff_half_len, index_inc ;
6363

64-
double src_ratio, input_index ;
64+
fp_t src_ratio, input_index ;
6565

6666
coeff_t const *coeffs ;
6767

6868
int b_current, b_end, b_real_end, b_len ;
6969

7070
/* Sure hope noone does more than 128 channels at once. */
71-
double left_calc [MAX_CHANNELS], right_calc [MAX_CHANNELS] ;
71+
fp_t left_calc [MAX_CHANNELS], right_calc [MAX_CHANNELS] ;
7272

7373
float *buffer ;
7474
} SINC_FILTER ;
@@ -131,7 +131,7 @@ static SRC_STATE_VT sinc_mono_state_vt =
131131
} ;
132132

133133
static inline increment_t
134-
double_to_fp (double x)
134+
double_to_fp (fp_t x)
135135
{ return (increment_t) (psf_lrint ((x) * FP_ONE)) ;
136136
} /* double_to_fp */
137137

@@ -150,7 +150,7 @@ fp_fraction_part (increment_t x)
150150
{ return ((x) & ((((increment_t) 1) << SHIFT_BITS) - 1)) ;
151151
} /* fp_fraction_part */
152152

153-
static inline double
153+
static inline fp_t
154154
fp_to_double (increment_t x)
155155
{ return fp_fraction_part (x) * INV_FP_ONE ;
156156
} /* fp_to_double */
@@ -367,9 +367,9 @@ sinc_copy (SRC_STATE *state)
367367
** Beware all ye who dare pass this point. There be dragons here.
368368
*/
369369

370-
static inline double
370+
static inline fp_t
371371
calc_output_single (SINC_FILTER *filter, increment_t increment, increment_t start_filter_index)
372-
{ double fraction, left, right, icoeff ;
372+
{ fp_t fraction, left, right, icoeff ;
373373
increment_t filter_index, max_filter_index ;
374374
int data_index, coeff_count, indx ;
375375

@@ -430,7 +430,7 @@ calc_output_single (SINC_FILTER *filter, increment_t increment, increment_t star
430430
static SRC_ERROR
431431
sinc_mono_vari_process (SRC_STATE *state, SRC_DATA *data)
432432
{ SINC_FILTER *filter ;
433-
double input_index, src_ratio, count, float_increment, terminate, rem ;
433+
fp_t input_index, src_ratio, count, float_increment, terminate, rem ;
434434
increment_t increment, start_filter_index ;
435435
int half_filter_chan_len, samples_in_hand ;
436436

@@ -521,8 +521,8 @@ sinc_mono_vari_process (SRC_STATE *state, SRC_DATA *data)
521521
} /* sinc_mono_vari_process */
522522

523523
static inline void
524-
calc_output_stereo (SINC_FILTER *filter, int channels, increment_t increment, increment_t start_filter_index, double scale, float * output)
525-
{ double fraction, left [2], right [2], icoeff ;
524+
calc_output_stereo (SINC_FILTER *filter, int channels, increment_t increment, increment_t start_filter_index, fp_t scale, float * output)
525+
{ fp_t fraction, left [2], right [2], icoeff ;
526526
increment_t filter_index, max_filter_index ;
527527
int data_index, coeff_count, indx ;
528528

@@ -586,7 +586,7 @@ calc_output_stereo (SINC_FILTER *filter, int channels, increment_t increment, in
586586
SRC_ERROR
587587
sinc_stereo_vari_process (SRC_STATE *state, SRC_DATA *data)
588588
{ SINC_FILTER *filter ;
589-
double input_index, src_ratio, count, float_increment, terminate, rem ;
589+
fp_t input_index, src_ratio, count, float_increment, terminate, rem ;
590590
increment_t increment, start_filter_index ;
591591
int half_filter_chan_len, samples_in_hand ;
592592

@@ -676,8 +676,8 @@ sinc_stereo_vari_process (SRC_STATE *state, SRC_DATA *data)
676676
} /* sinc_stereo_vari_process */
677677

678678
static inline void
679-
calc_output_quad (SINC_FILTER *filter, int channels, increment_t increment, increment_t start_filter_index, double scale, float * output)
680-
{ double fraction, left [4], right [4], icoeff ;
679+
calc_output_quad (SINC_FILTER *filter, int channels, increment_t increment, increment_t start_filter_index, fp_t scale, float * output)
680+
{ fp_t fraction, left [4], right [4], icoeff ;
681681
increment_t filter_index, max_filter_index ;
682682
int data_index, coeff_count, indx ;
683683

@@ -742,7 +742,7 @@ calc_output_quad (SINC_FILTER *filter, int channels, increment_t increment, incr
742742
SRC_ERROR
743743
sinc_quad_vari_process (SRC_STATE *state, SRC_DATA *data)
744744
{ SINC_FILTER *filter ;
745-
double input_index, src_ratio, count, float_increment, terminate, rem ;
745+
fp_t input_index, src_ratio, count, float_increment, terminate, rem ;
746746
increment_t increment, start_filter_index ;
747747
int half_filter_chan_len, samples_in_hand ;
748748

@@ -832,8 +832,8 @@ sinc_quad_vari_process (SRC_STATE *state, SRC_DATA *data)
832832
} /* sinc_quad_vari_process */
833833

834834
static inline void
835-
calc_output_hex (SINC_FILTER *filter, int channels, increment_t increment, increment_t start_filter_index, double scale, float * output)
836-
{ double fraction, left [6], right [6], icoeff ;
835+
calc_output_hex (SINC_FILTER *filter, int channels, increment_t increment, increment_t start_filter_index, fp_t scale, float * output)
836+
{ fp_t fraction, left [6], right [6], icoeff ;
837837
increment_t filter_index, max_filter_index ;
838838
int data_index, coeff_count, indx ;
839839

@@ -897,7 +897,7 @@ calc_output_hex (SINC_FILTER *filter, int channels, increment_t increment, incre
897897
SRC_ERROR
898898
sinc_hex_vari_process (SRC_STATE *state, SRC_DATA *data)
899899
{ SINC_FILTER *filter ;
900-
double input_index, src_ratio, count, float_increment, terminate, rem ;
900+
fp_t input_index, src_ratio, count, float_increment, terminate, rem ;
901901
increment_t increment, start_filter_index ;
902902
int half_filter_chan_len, samples_in_hand ;
903903

@@ -987,10 +987,10 @@ sinc_hex_vari_process (SRC_STATE *state, SRC_DATA *data)
987987
} /* sinc_hex_vari_process */
988988

989989
static inline void
990-
calc_output_multi (SINC_FILTER *filter, increment_t increment, increment_t start_filter_index, int channels, double scale, float * output)
991-
{ double fraction, icoeff ;
990+
calc_output_multi (SINC_FILTER *filter, increment_t increment, increment_t start_filter_index, int channels, fp_t scale, float * output)
991+
{ fp_t fraction, icoeff ;
992992
/* The following line is 1999 ISO Standard C. If your compiler complains, get a better compiler. */
993-
double *left, *right ;
993+
fp_t *left, *right ;
994994
increment_t filter_index, max_filter_index ;
995995
int data_index, coeff_count, indx ;
996996

@@ -1062,7 +1062,7 @@ calc_output_multi (SINC_FILTER *filter, increment_t increment, increment_t start
10621062
static SRC_ERROR
10631063
sinc_multichan_vari_process (SRC_STATE *state, SRC_DATA *data)
10641064
{ SINC_FILTER *filter ;
1065-
double input_index, src_ratio, count, float_increment, terminate, rem ;
1065+
fp_t input_index, src_ratio, count, float_increment, terminate, rem ;
10661066
increment_t increment, start_filter_index ;
10671067
int half_filter_chan_len, samples_in_hand ;
10681068

src/src_zoh.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static SRC_STATE_VT zoh_state_vt =
5151
static SRC_ERROR
5252
zoh_vari_process (SRC_STATE *state, SRC_DATA *data)
5353
{ ZOH_DATA *priv ;
54-
double src_ratio, input_index, rem ;
54+
fp_t src_ratio, input_index, rem ;
5555
int ch ;
5656

5757
if (data->input_frames <= 0)

0 commit comments

Comments
 (0)