]>
Commit | Line | Data |
---|---|---|
8529da43 MG |
1 | /* |
2 | ** Copyright (c) 2006-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 "config.h" | |
10 | ||
11 | #include <stdio.h> | |
12 | #include <stdlib.h> | |
13 | #include <math.h> | |
14 | #include <string.h> | |
15 | ||
16 | #include <samplerate.h> | |
17 | ||
18 | #include "util.h" | |
19 | ||
20 | #if HAVE_FFTW3 | |
21 | #include <fftw3.h> | |
22 | #else | |
23 | #define fftw_cleanup() | |
24 | #endif | |
25 | ||
26 | #define BUFFER_LEN (1 << 16) | |
27 | ||
28 | static void varispeed_test (int converter, double target_snr) ; | |
29 | ||
30 | int | |
31 | main (void) | |
32 | { | |
33 | puts ("") ; | |
34 | printf (" Zero Order Hold interpolator : ") ; | |
35 | varispeed_test (SRC_ZERO_ORDER_HOLD, 10.0) ; | |
36 | ||
37 | printf (" Linear interpolator : ") ; | |
38 | varispeed_test (SRC_LINEAR, 10.0) ; | |
39 | ||
40 | printf (" Sinc interpolator : ") ; | |
41 | varispeed_test (SRC_SINC_FASTEST, 115.0) ; | |
42 | ||
43 | fftw_cleanup () ; | |
44 | puts ("") ; | |
45 | ||
46 | return 0 ; | |
47 | } /* main */ | |
48 | ||
49 | static void | |
50 | varispeed_test (int converter, double target_snr) | |
51 | { static float input [BUFFER_LEN], output [BUFFER_LEN] ; | |
52 | double sine_freq, snr ; | |
53 | ||
54 | SRC_STATE *src_state ; | |
55 | SRC_DATA src_data ; | |
56 | ||
57 | int input_len, error ; | |
58 | ||
59 | memset (input, 0, sizeof (input)) ; | |
60 | ||
61 | input_len = ARRAY_LEN (input) / 2 ; | |
62 | ||
63 | sine_freq = 0.0111 ; | |
64 | gen_windowed_sines (1, &sine_freq, 1.0, input, input_len) ; | |
65 | ||
66 | /* Perform sample rate conversion. */ | |
67 | if ((src_state = src_new (converter, 1, &error)) == NULL) | |
68 | { printf ("\n\nLine %d : src_new() failed : %s\n\n", __LINE__, src_strerror (error)) ; | |
69 | exit (1) ; | |
70 | } ; | |
71 | ||
72 | src_data.end_of_input = 1 ; | |
73 | ||
74 | src_data.data_in = input ; | |
75 | src_data.input_frames = input_len ; | |
76 | ||
77 | src_data.src_ratio = 3.0 ; | |
78 | ||
79 | src_data.data_out = output ; | |
80 | src_data.output_frames = ARRAY_LEN (output) ; | |
81 | ||
82 | if ((error = src_set_ratio (src_state, 1.0 / src_data.src_ratio))) | |
83 | { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; | |
84 | exit (1) ; | |
85 | } ; | |
86 | ||
87 | if ((error = src_process (src_state, &src_data))) | |
88 | { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; | |
89 | printf (" src_data.input_frames : %ld\n", src_data.input_frames) ; | |
90 | printf (" src_data.output_frames : %ld\n\n", src_data.output_frames) ; | |
91 | exit (1) ; | |
92 | } ; | |
93 | ||
94 | if (src_data.input_frames_used != input_len) | |
95 | { printf ("\n\nLine %d : unused input.\n", __LINE__) ; | |
96 | printf ("\tinput_len : %d\n", input_len) ; | |
97 | printf ("\tinput_frames_used : %ld\n\n", src_data.input_frames_used) ; | |
98 | exit (1) ; | |
99 | } ; | |
100 | ||
101 | /* Copy the last output to the input. */ | |
102 | memcpy (input, output, sizeof (input)) ; | |
103 | reverse_data (input, src_data.output_frames_gen) ; | |
104 | ||
105 | if ((error = src_reset (src_state))) | |
106 | { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; | |
107 | exit (1) ; | |
108 | } ; | |
109 | ||
110 | src_data.end_of_input = 1 ; | |
111 | ||
112 | src_data.data_in = input ; | |
113 | input_len = src_data.input_frames = src_data.output_frames_gen ; | |
114 | ||
115 | src_data.data_out = output ; | |
116 | src_data.output_frames = ARRAY_LEN (output) ; | |
117 | ||
118 | if ((error = src_set_ratio (src_state, 1.0 / src_data.src_ratio))) | |
119 | { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; | |
120 | exit (1) ; | |
121 | } ; | |
122 | ||
123 | if ((error = src_process (src_state, &src_data))) | |
124 | { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; | |
125 | printf (" src_data.input_frames : %ld\n", src_data.input_frames) ; | |
126 | printf (" src_data.output_frames : %ld\n\n", src_data.output_frames) ; | |
127 | exit (1) ; | |
128 | } ; | |
129 | ||
130 | if (src_data.input_frames_used != input_len) | |
131 | { printf ("\n\nLine %d : unused input.\n", __LINE__) ; | |
132 | printf ("\tinput_len : %d\n", input_len) ; | |
133 | printf ("\tinput_frames_used : %ld\n\n", src_data.input_frames_used) ; | |
134 | exit (1) ; | |
135 | } ; | |
136 | ||
137 | src_state = src_delete (src_state) ; | |
138 | ||
139 | snr = calculate_snr (output, src_data.output_frames_gen, 1) ; | |
140 | ||
141 | if (target_snr > snr) | |
142 | { printf ("\n\nLine %d : snr (%3.1f) does not meet target (%3.1f)\n\n", __LINE__, snr, target_snr) ; | |
143 | save_oct_float ("varispeed.mat", input, src_data.input_frames, output, src_data.output_frames_gen) ; | |
144 | exit (1) ; | |
145 | } ; | |
146 | ||
147 | puts ("ok") ; | |
148 | ||
149 | return ; | |
150 | } /* varispeed_test */ | |
151 |