fork/rust-sdks
git clone https://git.julia.blue/fork/rust-sdks
2e4d385
main
1/* SoX Resampler Library Copyright (c) 2007-18 robs@users.sourceforge.net 2* Licence for this file: LGPL v2.1 See LICENCE for details. */ 3 4/* Wrapper mostly compatible with `libsamplerate'. */ 5 6#include <assert.h> 7#include <stdlib.h> 8#include "soxr.h" 9#include "soxr-lsr.h" 10#include "rint.h" 11 12 13 14SRC_STATE * src_new (SRC_SRCTYPE id ,int channels ,SRC_ERROR * error ) 15{ 16return src_callback_new (0 ,id ,channels ,error ,0 ); 17} 18 19 20 21SRC_ERROR src_process (SRC_STATE * p ,SRC_DATA * io ) 22{ 23size_t idone ,odone ; 24 25if (!p || !io )return -1 ; 26 27soxr_set_error ( 28p ,soxr_set_io_ratio (p ,1 /io -> src_ratio , (size_t )io -> output_frames )); 29 30soxr_process (p ,io -> data_in ,/* hack: */ 31 (size_t )(io -> end_of_input ? ~io -> input_frames :io -> input_frames ), 32& idone ,io -> data_out , (size_t )io -> output_frames ,& odone ); 33 34io -> input_frames_used = (long )idone ,io -> output_frames_gen = (long )odone ; 35return - !!soxr_error (p ); 36} 37 38 39 40SRC_ERROR src_set_ratio (SRC_STATE * p ,double oi_ratio ) 41{ 42return - !!soxr_set_io_ratio (p ,1 /oi_ratio ,0 ); 43} 44 45 46 47SRC_ERROR src_reset (SRC_STATE * p ) 48{ 49return - !!soxr_clear (p ); 50} 51 52 53 54SRC_ERROR src_error (SRC_STATE * p ) 55{ 56return - !!soxr_error (p ); 57} 58 59 60 61SRC_STATE * src_delete (SRC_STATE * p ) 62{ 63soxr_delete (p ); 64return 0 ; 65} 66 67 68 69SRC_STATE * src_callback_new (src_callback_t fn , 70SRC_SRCTYPE id ,int channels ,SRC_ERROR * error0 ,void * p ) 71{ 72soxr_quality_spec_t q_spec = soxr_quality_spec (SOXR_LSR0Q + (unsigned )id ,0 ); 73char const * e = getenv ("SOXR_LSR_NUM_THREADS" ); 74soxr_runtime_spec_t r_spec = soxr_runtime_spec (!(e && atoi (e )!= 1 )); 75soxr_error_t error ; 76soxr_t soxr = 0 ; 77 78assert (channels > 0 ); 79soxr = soxr_create (0 ,0 , (unsigned )channels ,& error ,0 ,& q_spec ,& r_spec ); 80 81if (soxr ) 82error = soxr_set_input_fn (soxr , (soxr_input_fn_t )fn ,p ,0 ); 83 84if (error0 ) 85* error0 = - !!error ; 86 87return soxr ; 88} 89 90 91 92long src_callback_read (SRC_STATE * p ,double oi_ratio ,long olen ,float * obuf ) 93{ 94if (!p || olen < 0 )return -1 ; 95 96soxr_set_error (p ,soxr_set_io_ratio (p ,1 /oi_ratio , (size_t )olen )); 97return (long )soxr_output (p ,obuf , (size_t )olen ); 98} 99 100 101 102SRC_ERROR src_simple (SRC_DATA * io ,SRC_SRCTYPE id ,int channels ) 103{ 104size_t idone ,odone ; 105soxr_error_t error ; 106soxr_quality_spec_t q_spec = soxr_quality_spec (SOXR_LSR0Q + (unsigned )id ,0 ); 107char const * e = getenv ("SOXR_LSR_NUM_THREADS" ); 108soxr_runtime_spec_t r_spec = soxr_runtime_spec (!(e && atoi (e )!= 1 )); 109 110if (!io || channels <=0 || io -> input_frames < 0 || io -> output_frames < 0 )return -1 ; 111 112error = soxr_oneshot (1 ,io -> src_ratio , (unsigned )channels ,io -> data_in , 113 (size_t )io -> input_frames ,& idone ,io -> data_out , (size_t )io -> output_frames , 114& odone ,0 ,& q_spec ,& r_spec ); 115 116io -> input_frames_used = (long )idone ,io -> output_frames_gen = (long )odone ; 117 118return - !!error ; 119} 120 121 122 123char const * src_get_name (SRC_SRCTYPE id ) 124{ 125static char const * const names []= { 126"LSR best sinc" ,"LSR medium sinc" ,"LSR fastest sinc" , 127"LSR ZOH" ,"LSR linear" ,"SoX VHQ" }; 128 129return (unsigned )id < 5u + !getenv ("SOXR_LSR_STRICT" )?names [id ] :0 ; 130} 131 132 133 134char const * src_get_description (SRC_SRCTYPE id ) 135{ 136return src_get_name (id ); 137} 138 139 140 141char const * src_get_version (void ) 142{ 143return soxr_version (); 144} 145 146 147 148char const * src_strerror (SRC_ERROR error ) 149{ 150return error == 1 ?"Placeholder." :error ?"soxr error" :soxr_strerror (0 ); 151} 152 153 154 155int src_is_valid_ratio (double oi_ratio ) 156{ 157return getenv ("SOXR_LSR_STRICT" )? 158oi_ratio >=1. /256 && oi_ratio <=256 :oi_ratio > 0 ; 159} 160 161 162 163void src_short_to_float_array (short const * src ,float * dest ,int len ) 164{ 165assert (src && dest ); 166 167while (len -- )dest [len ]= (float )(src [len ]* (1 / (1. + SHRT_MAX ))); 168} 169 170 171 172void src_float_to_short_array (float const * src ,short * dest ,int len ) 173{ 174double d ,N = 1. + SHRT_MAX ; 175assert (src && dest ); 176 177while (len -- )d = src [len ]* N ,dest [len ]= 178 (short )(d > N - 1 ? (short )(N - 1 ) :d < - N ? (short )- N :rint16 (d )); 179} 180 181 182 183void src_int_to_float_array (int const * src ,float * dest ,int len ) 184{ 185assert (src && dest ); 186while (len -- )dest [len ]= (float )(src [len ]* (1 / (32768. * 65536. ))); 187} 188 189 190 191void src_float_to_int_array (float const * src ,int * dest ,int len ) 192{ 193double d ,N = 32768. * 65536. ;/* N.B. int32, not int! (Also above fn.) */ 194assert (src && dest ); 195 196while (len -- )d = src [len ]* N ,dest [len ]= 197d >=N - 1 ? (int )(N - 1 ) :d < - N ? (int )(- N ) :rint32 (d ); 198}