Debian Codegraph @

tests/streaming_test.c

raw
1 /*
2 ** Copyright (c) 2002-2016, Erik de Castro Lopo <[email protected]>
3 ** All rights reserved.
4 **
5 ** This code is released under 2-clause BSD license. Please see the
6 ** file at : https://github.com/libsndfile/libsamplerate/blob/master/COPYING
7 */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <math.h>
16
17 #include <samplerate.h>
18
19 #include "util.h"
20
21 #define BUFFER_LEN (1<<15)
22
23 #define BLOCK_LEN 100
24
25 static void stream_test (int converter, double ratio) ;
26
27 int
28 main (void)
29 { static double src_ratios [] =
30 { 0.3, 0.9, 1.1, 3.0
31 } ;
32
33 int k ;
34
35 puts ("\n Zero Order Hold interpolator:") ;
36 for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++)
37 stream_test (SRC_ZERO_ORDER_HOLD, src_ratios [k]) ;
38
39 puts ("\n Linear interpolator:") ;
40 for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++)
41 stream_test (SRC_LINEAR, src_ratios [k]) ;
42
43 puts ("\n Sinc interpolator:") ;
44 for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++)
45 stream_test (SRC_SINC_FASTEST, src_ratios [k]) ;
46
47 puts ("") ;
48
49 return 0 ;
50 } /* main */
51
52 static void
53 stream_test (int converter, double src_ratio)
54 { static float input [BUFFER_LEN], output [BUFFER_LEN] ;
55
56 SRC_STATE *src_state ;
57 SRC_DATA src_data ;
58
59 int input_len, output_len, current_in, current_out ;
60 int error, terminate ;
61
62 printf ("\tstreaming_test (SRC ratio = %6.4f) ........... ", src_ratio) ;
63 fflush (stdout) ;
64
65 /* Calculate maximun input and output lengths. */
66 if (src_ratio >= 1.0)
67 { output_len = BUFFER_LEN ;
68 input_len = (int) floor (BUFFER_LEN / src_ratio) ;
69 }
70 else
71 { input_len = BUFFER_LEN ;
72 output_len = (int) floor (BUFFER_LEN * src_ratio) ;
73 } ;
74
75 /* Reduce input_len by 10 so output is longer than necessary. */
76 input_len -= 10 ;
77
78 if (output_len > BUFFER_LEN)
79 { printf ("\n\nLine %d : output_len > BUFFER_LEN\n\n", __LINE__) ;
80 exit (1) ;
81 } ;
82
83 current_in = current_out = 0 ;
84
85 /* Perform sample rate conversion. */
86 if ((src_state = src_new (converter, 1, &error)) == NULL)
87 { printf ("\n\nLine %d : src_new() failed : %s\n\n", __LINE__, src_strerror (error)) ;
88 exit (1) ;
89 } ;
90
91 src_data.end_of_input = 0 ; /* Set this later. */
92
93 src_data.data_in = input ;
94 src_data.input_frames = BLOCK_LEN ;
95
96 src_data.src_ratio = src_ratio ;
97
98 src_data.data_out = output ;
99 src_data.output_frames = BLOCK_LEN ;
100
101 while (1)
102 { if ((error = src_process (src_state, &src_data)))
103 { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
104
105 printf ("src_data.input_frames : %ld\n", src_data.input_frames) ;
106 printf ("src_data.output_frames : %ld\n", src_data.output_frames) ;
107
108 exit (1) ;
109 } ;
110
111 if (src_data.end_of_input && src_data.output_frames_gen == 0)
112 break ;
113
114 current_in += src_data.input_frames_used ;
115 current_out += src_data.output_frames_gen ;
116
117 src_data.data_in += src_data.input_frames_used ;
118 src_data.data_out += src_data.output_frames_gen ;
119
120 src_data.input_frames = MIN (BLOCK_LEN, input_len - current_in) ;
121 src_data.output_frames = MIN (BLOCK_LEN, output_len - current_out) ;
122
123 src_data.end_of_input = (current_in >= input_len) ? 1 : 0 ;
124 } ;
125
126 src_state = src_delete (src_state) ;
127
128 terminate = (int) ceil ((src_ratio >= 1.0) ? src_ratio : 1.0 / src_ratio) ;
129
130 if (fabs (current_out - src_ratio * input_len) > 2 * terminate)
131 { printf ("\n\nLine %d : bad output data length %d should be %d.\n", __LINE__,
132 current_out, (int) floor (src_ratio * input_len)) ;
133 printf ("\tsrc_ratio : %.4f\n", src_ratio) ;
134 printf ("\tinput_len : %d\n\toutput_len : %d\n\n", input_len, output_len) ;
135 exit (1) ;
136 } ;
137
138 if (current_in != input_len)
139 { printf ("\n\nLine %d : unused input.\n", __LINE__) ;
140 printf ("\tinput_len : %d\n", input_len) ;
141 printf ("\tinput_frames_used : %d\n\n", current_in) ;
142 exit (1) ;
143 } ;
144
145 puts ("ok") ;
146
147 return ;
148 } /* stream_test */
149