]> iEval git - audio-libsamplerate.git/blob - libsamplerate/tests/simple_test.c
b87dc109afac66544d63a9cdb820ee642dc62041
[audio-libsamplerate.git] / libsamplerate / tests / simple_test.c
1 /*
2 ** Copyright (c) 2002-2016, Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** All rights reserved.
4 **
5 ** This code is released under 2-clause BSD license. Please see the
6 ** file at : https://github.com/erikd/libsamplerate/blob/master/COPYING
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <math.h>
13
14 #include <samplerate.h>
15
16 #include "util.h"
17
18 #define BUFFER_LEN 2048
19
20 static void simple_test (int converter, double ratio) ;
21
22 int
23 main (void)
24 { static double src_ratios [] =
25 { 1.0001, 0.099, 0.1, 0.33333333, 0.789, 1.9, 3.1, 9.9
26 } ;
27
28 int k ;
29
30 puts ("") ;
31
32 puts (" Zero Order Hold interpolator :") ;
33 for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++)
34 simple_test (SRC_ZERO_ORDER_HOLD, src_ratios [k]) ;
35
36 puts (" Linear interpolator :") ;
37 for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++)
38 simple_test (SRC_LINEAR, src_ratios [k]) ;
39
40 puts (" Sinc interpolator :") ;
41 for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++)
42 simple_test (SRC_SINC_FASTEST, src_ratios [k]) ;
43
44 puts ("") ;
45
46 return 0 ;
47 } /* main */
48
49 static void
50 simple_test (int converter, double src_ratio)
51 { static float input [BUFFER_LEN], output [BUFFER_LEN] ;
52
53 SRC_DATA src_data ;
54
55 int input_len, output_len, error, terminate ;
56
57 printf ("\tsimple_test (SRC ratio = %6.4f) ........... ", src_ratio) ;
58 fflush (stdout) ;
59
60 /* Calculate maximun input and output lengths. */
61 if (src_ratio >= 1.0)
62 { output_len = BUFFER_LEN ;
63 input_len = (int) floor (BUFFER_LEN / src_ratio) ;
64 }
65 else
66 { input_len = BUFFER_LEN ;
67 output_len = (int) floor (BUFFER_LEN * src_ratio) ;
68 } ;
69
70 /* Reduce input_len by 10 so output is longer than necessary. */
71 input_len -= 10 ;
72
73 if (output_len > BUFFER_LEN)
74 { printf ("\n\nLine %d : output_len > BUFFER_LEN\n\n", __LINE__) ;
75 exit (1) ;
76 } ;
77
78 memset (&src_data, 0, sizeof (src_data)) ;
79
80 src_data.data_in = input ;
81 src_data.input_frames = input_len ;
82
83 src_data.src_ratio = src_ratio ;
84
85 src_data.data_out = output ;
86 src_data.output_frames = BUFFER_LEN ;
87
88 if ((error = src_simple (&src_data, converter, 1)))
89 { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
90 exit (1) ;
91 } ;
92
93 terminate = (int) ceil ((src_ratio >= 1.0) ? src_ratio : 1.0 / src_ratio) ;
94
95 if (fabs (src_data.output_frames_gen - src_ratio * input_len) > 2 * terminate)
96 { printf ("\n\nLine %d : bad output data length %ld should be %d.\n", __LINE__,
97 src_data.output_frames_gen, (int) floor (src_ratio * input_len)) ;
98 printf ("\tsrc_ratio : %.4f\n", src_ratio) ;
99 printf ("\tinput_len : %d\n\toutput_len : %d\n\n", input_len, output_len) ;
100 exit (1) ;
101 } ;
102
103 puts ("ok") ;
104
105 return ;
106 } /* simple_test */
107
This page took 0.05243 seconds and 5 git commands to generate.