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