Chris@0
|
1 /*
|
Chris@0
|
2 ** Copyright (C) 2006-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
|
Chris@0
|
3 **
|
Chris@0
|
4 ** This program is free software; you can redistribute it and/or modify
|
Chris@0
|
5 ** it under the terms of the GNU General Public License as published by
|
Chris@0
|
6 ** the Free Software Foundation; either version 2 of the License, or
|
Chris@0
|
7 ** (at your option) any later version.
|
Chris@0
|
8 **
|
Chris@0
|
9 ** This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 ** GNU General Public License for more details.
|
Chris@0
|
13 **
|
Chris@0
|
14 ** You should have received a copy of the GNU General Public License
|
Chris@0
|
15 ** along with this program; if not, write to the Free Software
|
Chris@0
|
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
|
Chris@0
|
17 */
|
Chris@0
|
18
|
Chris@0
|
19 #include <stdio.h>
|
Chris@0
|
20 #include <stdlib.h>
|
Chris@0
|
21 #include <math.h>
|
Chris@0
|
22 #include <string.h>
|
Chris@0
|
23
|
Chris@0
|
24 #include <samplerate.h>
|
Chris@0
|
25
|
Chris@0
|
26 #include "util.h"
|
Chris@0
|
27
|
Chris@0
|
28 #define BUFFER_LEN (1 << 16)
|
Chris@0
|
29
|
Chris@0
|
30 static void varispeed_test (int converter, double target_snr) ;
|
Chris@0
|
31
|
Chris@0
|
32 int
|
Chris@0
|
33 main (void)
|
Chris@0
|
34 {
|
Chris@0
|
35 puts ("") ;
|
Chris@0
|
36 printf (" Zero Order Hold interpolator : ") ;
|
Chris@0
|
37 varispeed_test (SRC_ZERO_ORDER_HOLD, 10.0) ;
|
Chris@0
|
38
|
Chris@0
|
39 printf (" Linear interpolator : ") ;
|
Chris@0
|
40 varispeed_test (SRC_LINEAR, 10.0) ;
|
Chris@0
|
41
|
Chris@0
|
42 printf (" Sinc interpolator : ") ;
|
Chris@0
|
43 varispeed_test (SRC_SINC_FASTEST, 115.0) ;
|
Chris@0
|
44
|
Chris@0
|
45 puts ("") ;
|
Chris@0
|
46
|
Chris@0
|
47 return 0 ;
|
Chris@0
|
48 } /* main */
|
Chris@0
|
49
|
Chris@0
|
50 static void
|
Chris@0
|
51 varispeed_test (int converter, double target_snr)
|
Chris@0
|
52 { static float input [BUFFER_LEN], output [BUFFER_LEN] ;
|
Chris@0
|
53 double sine_freq, snr ;
|
Chris@0
|
54
|
Chris@0
|
55 SRC_STATE *src_state ;
|
Chris@0
|
56 SRC_DATA src_data ;
|
Chris@0
|
57
|
Chris@0
|
58 int input_len, error ;
|
Chris@0
|
59
|
Chris@0
|
60 memset (input, 0, sizeof (input)) ;
|
Chris@0
|
61
|
Chris@0
|
62 input_len = ARRAY_LEN (input) / 2 ;
|
Chris@0
|
63
|
Chris@0
|
64 sine_freq = 0.0111 ;
|
Chris@0
|
65 gen_windowed_sines (1, &sine_freq, 1.0, input, input_len) ;
|
Chris@0
|
66
|
Chris@0
|
67 /* Perform sample rate conversion. */
|
Chris@0
|
68 if ((src_state = src_new (converter, 1, &error)) == NULL)
|
Chris@0
|
69 { printf ("\n\nLine %d : src_new() failed : %s\n\n", __LINE__, src_strerror (error)) ;
|
Chris@0
|
70 exit (1) ;
|
Chris@0
|
71 } ;
|
Chris@0
|
72
|
Chris@0
|
73 src_data.end_of_input = 1 ;
|
Chris@0
|
74
|
Chris@0
|
75 src_data.data_in = input ;
|
Chris@0
|
76 src_data.input_frames = input_len ;
|
Chris@0
|
77
|
Chris@0
|
78 src_data.src_ratio = 3.0 ;
|
Chris@0
|
79
|
Chris@0
|
80 src_data.data_out = output ;
|
Chris@0
|
81 src_data.output_frames = ARRAY_LEN (output) ;
|
Chris@0
|
82
|
Chris@0
|
83 if ((error = src_set_ratio (src_state, 1.0 / src_data.src_ratio)))
|
Chris@0
|
84 { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
|
Chris@0
|
85 exit (1) ;
|
Chris@0
|
86 } ;
|
Chris@0
|
87
|
Chris@0
|
88 if ((error = src_process (src_state, &src_data)))
|
Chris@0
|
89 { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
|
Chris@0
|
90 printf (" src_data.input_frames : %ld\n", src_data.input_frames) ;
|
Chris@0
|
91 printf (" src_data.output_frames : %ld\n\n", src_data.output_frames) ;
|
Chris@0
|
92 exit (1) ;
|
Chris@0
|
93 } ;
|
Chris@0
|
94
|
Chris@0
|
95 if (src_data.input_frames_used != input_len)
|
Chris@0
|
96 { printf ("\n\nLine %d : unused input.\n", __LINE__) ;
|
Chris@0
|
97 printf ("\tinput_len : %d\n", input_len) ;
|
Chris@0
|
98 printf ("\tinput_frames_used : %ld\n\n", src_data.input_frames_used) ;
|
Chris@0
|
99 exit (1) ;
|
Chris@0
|
100 } ;
|
Chris@0
|
101
|
Chris@0
|
102 /* Copy the last output to the input. */
|
Chris@0
|
103 memcpy (input, output, sizeof (input)) ;
|
Chris@0
|
104 reverse_data (input, src_data.output_frames_gen) ;
|
Chris@0
|
105
|
Chris@0
|
106 if ((error = src_reset (src_state)))
|
Chris@0
|
107 { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
|
Chris@0
|
108 exit (1) ;
|
Chris@0
|
109 } ;
|
Chris@0
|
110
|
Chris@0
|
111 src_data.end_of_input = 1 ;
|
Chris@0
|
112
|
Chris@0
|
113 src_data.data_in = input ;
|
Chris@0
|
114 input_len = src_data.input_frames = src_data.output_frames_gen ;
|
Chris@0
|
115
|
Chris@0
|
116 src_data.data_out = output ;
|
Chris@0
|
117 src_data.output_frames = ARRAY_LEN (output) ;
|
Chris@0
|
118
|
Chris@0
|
119 if ((error = src_set_ratio (src_state, 1.0 / src_data.src_ratio)))
|
Chris@0
|
120 { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
|
Chris@0
|
121 exit (1) ;
|
Chris@0
|
122 } ;
|
Chris@0
|
123
|
Chris@0
|
124 if ((error = src_process (src_state, &src_data)))
|
Chris@0
|
125 { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
|
Chris@0
|
126 printf (" src_data.input_frames : %ld\n", src_data.input_frames) ;
|
Chris@0
|
127 printf (" src_data.output_frames : %ld\n\n", src_data.output_frames) ;
|
Chris@0
|
128 exit (1) ;
|
Chris@0
|
129 } ;
|
Chris@0
|
130
|
Chris@0
|
131 if (src_data.input_frames_used != input_len)
|
Chris@0
|
132 { printf ("\n\nLine %d : unused input.\n", __LINE__) ;
|
Chris@0
|
133 printf ("\tinput_len : %d\n", input_len) ;
|
Chris@0
|
134 printf ("\tinput_frames_used : %ld\n\n", src_data.input_frames_used) ;
|
Chris@0
|
135 exit (1) ;
|
Chris@0
|
136 } ;
|
Chris@0
|
137
|
Chris@0
|
138 src_state = src_delete (src_state) ;
|
Chris@0
|
139
|
Chris@0
|
140 snr = calculate_snr (output, src_data.output_frames_gen, 1) ;
|
Chris@0
|
141
|
Chris@0
|
142 if (target_snr > snr)
|
Chris@0
|
143 { printf ("\n\nLine %d : snr (%3.1f) does not meet target (%3.1f)\n\n", __LINE__, snr, target_snr) ;
|
Chris@0
|
144 save_oct_float ("varispeed.mat", input, src_data.input_frames, output, src_data.output_frames_gen) ;
|
Chris@0
|
145 exit (1) ;
|
Chris@0
|
146 } ;
|
Chris@0
|
147
|
Chris@0
|
148 puts ("ok") ;
|
Chris@0
|
149
|
Chris@0
|
150 return ;
|
Chris@0
|
151 } /* varispeed_test */
|
Chris@0
|
152
|