cannam@126
|
1 /*
|
cannam@126
|
2 ** Copyright (c) 2005-2016, Erik de Castro Lopo <erikd@mega-nerd.com>
|
cannam@126
|
3 ** All rights reserved.
|
cannam@126
|
4 **
|
cannam@126
|
5 ** This code is released under 2-clause BSD license. Please see the
|
cannam@126
|
6 ** file at : https://github.com/erikd/libsamplerate/blob/master/COPYING
|
cannam@126
|
7 */
|
cannam@126
|
8
|
cannam@126
|
9 #include "config.h"
|
cannam@126
|
10
|
cannam@126
|
11 #include <stdio.h>
|
cannam@126
|
12 #include <stdlib.h>
|
cannam@126
|
13 #include <unistd.h>
|
cannam@126
|
14 #include <string.h>
|
cannam@126
|
15 #include <math.h>
|
cannam@126
|
16
|
cannam@126
|
17 #if (HAVE_SNDFILE)
|
cannam@126
|
18
|
cannam@126
|
19 #include <samplerate.h>
|
cannam@126
|
20 #include <sndfile.h>
|
cannam@126
|
21
|
cannam@126
|
22 #define ARRAY_LEN(x) ((int) (sizeof (x) / sizeof ((x) [0])))
|
cannam@126
|
23
|
cannam@126
|
24 #define DEFAULT_CONVERTER SRC_SINC_MEDIUM_QUALITY
|
cannam@126
|
25
|
cannam@126
|
26 #define BUFFER_LEN 1024
|
cannam@126
|
27 #define INPUT_STEP_SIZE 8
|
cannam@126
|
28
|
cannam@126
|
29 typedef struct
|
cannam@126
|
30 { sf_count_t index ;
|
cannam@126
|
31 double ratio ;
|
cannam@126
|
32 } TIMEWARP_FACTOR ;
|
cannam@126
|
33
|
cannam@126
|
34 static void usage_exit (const char *progname) ;
|
cannam@126
|
35 static sf_count_t timewarp_convert (SNDFILE *infile, SNDFILE *outfile, int converter, int channels) ;
|
cannam@126
|
36
|
cannam@126
|
37 int
|
cannam@126
|
38 main (int argc, char *argv [])
|
cannam@126
|
39 { SNDFILE *infile, *outfile ;
|
cannam@126
|
40 SF_INFO sfinfo ;
|
cannam@126
|
41 sf_count_t count ;
|
cannam@126
|
42
|
cannam@126
|
43 if (argc != 3)
|
cannam@126
|
44 usage_exit (argv [0]) ;
|
cannam@126
|
45
|
cannam@126
|
46 putchar ('\n') ;
|
cannam@126
|
47 printf ("Input File : %s\n", argv [argc - 2]) ;
|
cannam@126
|
48 if ((infile = sf_open (argv [argc - 2], SFM_READ, &sfinfo)) == NULL)
|
cannam@126
|
49 { printf ("Error : Not able to open input file '%s'\n", argv [argc - 2]) ;
|
cannam@126
|
50 exit (1) ;
|
cannam@126
|
51 } ;
|
cannam@126
|
52
|
cannam@126
|
53 if (INPUT_STEP_SIZE * sfinfo.channels > BUFFER_LEN)
|
cannam@126
|
54 { printf ("\n\nError : INPUT_STEP_SIZE * sfinfo.channels > BUFFER_LEN\n\n") ;
|
cannam@126
|
55 exit (1) ;
|
cannam@126
|
56 } ;
|
cannam@126
|
57
|
cannam@126
|
58
|
cannam@126
|
59 /* Delete the output file length to zero if already exists. */
|
cannam@126
|
60 remove (argv [argc - 1]) ;
|
cannam@126
|
61
|
cannam@126
|
62 if ((outfile = sf_open (argv [argc - 1], SFM_WRITE, &sfinfo)) == NULL)
|
cannam@126
|
63 { printf ("Error : Not able to open output file '%s'\n", argv [argc - 1]) ;
|
cannam@126
|
64 sf_close (infile) ;
|
cannam@126
|
65 exit (1) ;
|
cannam@126
|
66 } ;
|
cannam@126
|
67
|
cannam@126
|
68 sf_command (outfile, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
|
cannam@126
|
69
|
cannam@126
|
70 printf ("Output file : %s\n", argv [argc - 1]) ;
|
cannam@126
|
71 printf ("Converter : %s\n", src_get_name (DEFAULT_CONVERTER)) ;
|
cannam@126
|
72
|
cannam@126
|
73 count = timewarp_convert (infile, outfile, DEFAULT_CONVERTER, sfinfo.channels) ;
|
cannam@126
|
74
|
cannam@126
|
75 printf ("Output Frames : %ld\n\n", (long) count) ;
|
cannam@126
|
76
|
cannam@126
|
77 sf_close (infile) ;
|
cannam@126
|
78 sf_close (outfile) ;
|
cannam@126
|
79
|
cannam@126
|
80 return 0 ;
|
cannam@126
|
81 } /* main */
|
cannam@126
|
82
|
cannam@126
|
83 /*==============================================================================
|
cannam@126
|
84 */
|
cannam@126
|
85
|
cannam@126
|
86 static TIMEWARP_FACTOR warp [] =
|
cannam@126
|
87 { { 0 , 1.00000001 },
|
cannam@126
|
88 { 20000 , 1.01000000 },
|
cannam@126
|
89 { 20200 , 1.00000001 },
|
cannam@126
|
90 { 40000 , 1.20000000 },
|
cannam@126
|
91 { 40300 , 1.00000001 },
|
cannam@126
|
92 { 60000 , 1.10000000 },
|
cannam@126
|
93 { 60400 , 1.00000001 },
|
cannam@126
|
94 { 80000 , 1.50000000 },
|
cannam@126
|
95 { 81000 , 1.00000001 },
|
cannam@126
|
96 } ;
|
cannam@126
|
97
|
cannam@126
|
98 static sf_count_t
|
cannam@126
|
99 timewarp_convert (SNDFILE *infile, SNDFILE *outfile, int converter, int channels)
|
cannam@126
|
100 { static float input [BUFFER_LEN] ;
|
cannam@126
|
101 static float output [BUFFER_LEN] ;
|
cannam@126
|
102
|
cannam@126
|
103 SRC_STATE *src_state ;
|
cannam@126
|
104 SRC_DATA src_data ;
|
cannam@126
|
105 int error, warp_index = 0 ;
|
cannam@126
|
106 sf_count_t input_count = 0, output_count = 0 ;
|
cannam@126
|
107
|
cannam@126
|
108 sf_seek (infile, 0, SEEK_SET) ;
|
cannam@126
|
109 sf_seek (outfile, 0, SEEK_SET) ;
|
cannam@126
|
110
|
cannam@126
|
111 /* Initialize the sample rate converter. */
|
cannam@126
|
112 if ((src_state = src_new (converter, channels, &error)) == NULL)
|
cannam@126
|
113 { printf ("\n\nError : src_new() failed : %s.\n\n", src_strerror (error)) ;
|
cannam@126
|
114 exit (1) ;
|
cannam@126
|
115 } ;
|
cannam@126
|
116
|
cannam@126
|
117 src_data.end_of_input = 0 ; /* Set this later. */
|
cannam@126
|
118
|
cannam@126
|
119 /* Start with zero to force load in while loop. */
|
cannam@126
|
120 src_data.input_frames = 0 ;
|
cannam@126
|
121 src_data.data_in = input ;
|
cannam@126
|
122
|
cannam@126
|
123 if (warp [0].index > 0)
|
cannam@126
|
124 src_data.src_ratio = 1.0 ;
|
cannam@126
|
125 else
|
cannam@126
|
126 { src_data.src_ratio = warp [0].ratio ;
|
cannam@126
|
127 warp_index ++ ;
|
cannam@126
|
128 } ;
|
cannam@126
|
129
|
cannam@126
|
130 src_data.data_out = output ;
|
cannam@126
|
131 src_data.output_frames = BUFFER_LEN /channels ;
|
cannam@126
|
132
|
cannam@126
|
133 while (1)
|
cannam@126
|
134 {
|
cannam@126
|
135 if (warp_index < ARRAY_LEN (warp) - 1 && input_count >= warp [warp_index].index)
|
cannam@126
|
136 { src_data.src_ratio = warp [warp_index].ratio ;
|
cannam@126
|
137 warp_index ++ ;
|
cannam@126
|
138 } ;
|
cannam@126
|
139
|
cannam@126
|
140 /* If the input buffer is empty, refill it. */
|
cannam@126
|
141 if (src_data.input_frames == 0)
|
cannam@126
|
142 { src_data.input_frames = sf_readf_float (infile, input, INPUT_STEP_SIZE) ;
|
cannam@126
|
143 input_count += src_data.input_frames ;
|
cannam@126
|
144 src_data.data_in = input ;
|
cannam@126
|
145
|
cannam@126
|
146 /* The last read will not be a full buffer, so snd_of_input. */
|
cannam@126
|
147 if (src_data.input_frames < INPUT_STEP_SIZE)
|
cannam@126
|
148 src_data.end_of_input = SF_TRUE ;
|
cannam@126
|
149 } ;
|
cannam@126
|
150
|
cannam@126
|
151 /* Process current block. */
|
cannam@126
|
152 if ((error = src_process (src_state, &src_data)))
|
cannam@126
|
153 { printf ("\nError : %s\n", src_strerror (error)) ;
|
cannam@126
|
154 exit (1) ;
|
cannam@126
|
155 } ;
|
cannam@126
|
156
|
cannam@126
|
157 /* Terminate if done. */
|
cannam@126
|
158 if (src_data.end_of_input && src_data.output_frames_gen == 0)
|
cannam@126
|
159 break ;
|
cannam@126
|
160
|
cannam@126
|
161 /* Write output. */
|
cannam@126
|
162 sf_writef_float (outfile, output, src_data.output_frames_gen) ;
|
cannam@126
|
163 output_count += src_data.output_frames_gen ;
|
cannam@126
|
164
|
cannam@126
|
165 src_data.data_in += src_data.input_frames_used * channels ;
|
cannam@126
|
166 src_data.input_frames -= src_data.input_frames_used ;
|
cannam@126
|
167 } ;
|
cannam@126
|
168
|
cannam@126
|
169 src_delete (src_state) ;
|
cannam@126
|
170
|
cannam@126
|
171 return output_count ;
|
cannam@126
|
172 } /* timewarp_convert */
|
cannam@126
|
173
|
cannam@126
|
174 /*------------------------------------------------------------------------------
|
cannam@126
|
175 */
|
cannam@126
|
176
|
cannam@126
|
177 static void
|
cannam@126
|
178 usage_exit (const char *progname)
|
cannam@126
|
179 { const char *cptr ;
|
cannam@126
|
180
|
cannam@126
|
181 if ((cptr = strrchr (progname, '/')) != NULL)
|
cannam@126
|
182 progname = cptr + 1 ;
|
cannam@126
|
183
|
cannam@126
|
184 if ((cptr = strrchr (progname, '\\')) != NULL)
|
cannam@126
|
185 progname = cptr + 1 ;
|
cannam@126
|
186
|
cannam@126
|
187 printf ("\n"
|
cannam@126
|
188 " A program demonstrating the time warping capabilities of libsamplerate."
|
cannam@126
|
189 " It uses libsndfile for file I/O and Secret Rabbit Code (aka libsamplerate)"
|
cannam@126
|
190 " for performing the warping.\n"
|
cannam@126
|
191 " It works on any file format supported by libsndfile with any \n"
|
cannam@126
|
192 " number of channels (limited only by host memory).\n"
|
cannam@126
|
193 "\n"
|
cannam@126
|
194 " The warping is dependant on a table hard code into the source code.\n"
|
cannam@126
|
195 "\n"
|
cannam@126
|
196 " libsamplerate version : %s\n"
|
cannam@126
|
197 "\n"
|
cannam@126
|
198 " Usage : \n"
|
cannam@126
|
199 " %s <input file> <output file>\n"
|
cannam@126
|
200 "\n", src_get_version (), progname) ;
|
cannam@126
|
201
|
cannam@126
|
202 puts ("") ;
|
cannam@126
|
203
|
cannam@126
|
204 exit (1) ;
|
cannam@126
|
205 } /* usage_exit */
|
cannam@126
|
206
|
cannam@126
|
207 /*==============================================================================
|
cannam@126
|
208 */
|
cannam@126
|
209
|
cannam@126
|
210 #else /* (HAVE_SNFILE == 0) */
|
cannam@126
|
211
|
cannam@126
|
212 /* Alternative main function when libsndfile is not available. */
|
cannam@126
|
213
|
cannam@126
|
214 int
|
cannam@126
|
215 main (void)
|
cannam@126
|
216 { puts (
|
cannam@126
|
217 "\n"
|
cannam@126
|
218 "****************************************************************\n"
|
cannam@126
|
219 " This example program was compiled without libsndfile \n"
|
cannam@126
|
220 " (http://www.mega-nerd.com/libsndfile/).\n"
|
cannam@126
|
221 " It is therefore completely broken and non-functional.\n"
|
cannam@126
|
222 "****************************************************************\n"
|
cannam@126
|
223 "\n"
|
cannam@126
|
224 ) ;
|
cannam@126
|
225
|
cannam@126
|
226 return 0 ;
|
cannam@126
|
227 } /* main */
|
cannam@126
|
228
|
cannam@126
|
229 #endif
|
cannam@126
|
230
|