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