]>
iEval git - audio-libsamplerate.git/blob - libsamplerate/examples/timewarp-file.c
2 ** Copyright (c) 2005-2016, Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** All rights reserved.
5 ** This code is released under 2-clause BSD license. Please see the
6 ** file at : https://github.com/erikd/libsamplerate/blob/master/COPYING
19 #include <samplerate.h>
22 #define ARRAY_LEN(x) ((int) (sizeof (x) / sizeof ((x) [0])))
24 #define DEFAULT_CONVERTER SRC_SINC_MEDIUM_QUALITY
26 #define BUFFER_LEN 1024
27 #define INPUT_STEP_SIZE 8
34 static void usage_exit (const char *progname
) ;
35 static sf_count_t
timewarp_convert (SNDFILE
*infile
, SNDFILE
*outfile
, int converter
, int channels
) ;
38 main (int argc
, char *argv
[])
39 { SNDFILE
*infile
, *outfile
;
44 usage_exit (argv
[0]) ;
47 printf ("Input File : %s\n", argv
[argc
- 2]) ;
48 if ((infile
= sf_open (argv
[argc
- 2], SFM_READ
, &sfinfo
)) == NULL
)
49 { printf ("Error : Not able to open input file '%s'\n", argv
[argc
- 2]) ;
53 if (INPUT_STEP_SIZE
* sfinfo
.channels
> BUFFER_LEN
)
54 { printf ("\n\nError : INPUT_STEP_SIZE * sfinfo.channels > BUFFER_LEN\n\n") ;
59 /* Delete the output file length to zero if already exists. */
60 remove (argv
[argc
- 1]) ;
62 if ((outfile
= sf_open (argv
[argc
- 1], SFM_WRITE
, &sfinfo
)) == NULL
)
63 { printf ("Error : Not able to open output file '%s'\n", argv
[argc
- 1]) ;
68 sf_command (outfile
, SFC_SET_CLIPPING
, NULL
, SF_TRUE
) ;
70 printf ("Output file : %s\n", argv
[argc
- 1]) ;
71 printf ("Converter : %s\n", src_get_name (DEFAULT_CONVERTER
)) ;
73 count
= timewarp_convert (infile
, outfile
, DEFAULT_CONVERTER
, sfinfo
.channels
) ;
75 printf ("Output Frames : %ld\n\n", (long) count
) ;
83 /*==============================================================================
86 static TIMEWARP_FACTOR warp
[] =
88 { 20000 , 1.01000000 },
89 { 20200 , 1.00000001 },
90 { 40000 , 1.20000000 },
91 { 40300 , 1.00000001 },
92 { 60000 , 1.10000000 },
93 { 60400 , 1.00000001 },
94 { 80000 , 1.50000000 },
95 { 81000 , 1.00000001 },
99 timewarp_convert (SNDFILE
*infile
, SNDFILE
*outfile
, int converter
, int channels
)
100 { static float input
[BUFFER_LEN
] ;
101 static float output
[BUFFER_LEN
] ;
103 SRC_STATE
*src_state
;
105 int error
, warp_index
= 0 ;
106 sf_count_t input_count
= 0, output_count
= 0 ;
108 sf_seek (infile
, 0, SEEK_SET
) ;
109 sf_seek (outfile
, 0, SEEK_SET
) ;
111 /* Initialize the sample rate converter. */
112 if ((src_state
= src_new (converter
, channels
, &error
)) == NULL
)
113 { printf ("\n\nError : src_new() failed : %s.\n\n", src_strerror (error
)) ;
117 src_data
.end_of_input
= 0 ; /* Set this later. */
119 /* Start with zero to force load in while loop. */
120 src_data
.input_frames
= 0 ;
121 src_data
.data_in
= input
;
123 if (warp
[0].index
> 0)
124 src_data
.src_ratio
= 1.0 ;
126 { src_data
.src_ratio
= warp
[0].ratio
;
130 src_data
.data_out
= output
;
131 src_data
.output_frames
= BUFFER_LEN
/channels
;
135 if (warp_index
< ARRAY_LEN (warp
) - 1 && input_count
>= warp
[warp_index
].index
)
136 { src_data
.src_ratio
= warp
[warp_index
].ratio
;
140 /* If the input buffer is empty, refill it. */
141 if (src_data
.input_frames
== 0)
142 { src_data
.input_frames
= sf_readf_float (infile
, input
, INPUT_STEP_SIZE
) ;
143 input_count
+= src_data
.input_frames
;
144 src_data
.data_in
= input
;
146 /* The last read will not be a full buffer, so snd_of_input. */
147 if (src_data
.input_frames
< INPUT_STEP_SIZE
)
148 src_data
.end_of_input
= SF_TRUE
;
151 /* Process current block. */
152 if ((error
= src_process (src_state
, &src_data
)))
153 { printf ("\nError : %s\n", src_strerror (error
)) ;
157 /* Terminate if done. */
158 if (src_data
.end_of_input
&& src_data
.output_frames_gen
== 0)
162 sf_writef_float (outfile
, output
, src_data
.output_frames_gen
) ;
163 output_count
+= src_data
.output_frames_gen
;
165 src_data
.data_in
+= src_data
.input_frames_used
* channels
;
166 src_data
.input_frames
-= src_data
.input_frames_used
;
169 src_delete (src_state
) ;
171 return output_count
;
172 } /* timewarp_convert */
174 /*------------------------------------------------------------------------------
178 usage_exit (const char *progname
)
181 if ((cptr
= strrchr (progname
, '/')) != NULL
)
182 progname
= cptr
+ 1 ;
184 if ((cptr
= strrchr (progname
, '\\')) != NULL
)
185 progname
= cptr
+ 1 ;
188 " A program demonstrating the time warping capabilities of libsamplerate."
189 " It uses libsndfile for file I/O and Secret Rabbit Code (aka libsamplerate)"
190 " for performing the warping.\n"
191 " It works on any file format supported by libsndfile with any \n"
192 " number of channels (limited only by host memory).\n"
194 " The warping is dependant on a table hard code into the source code.\n"
196 " libsamplerate version : %s\n"
199 " %s <input file> <output file>\n"
200 "\n", src_get_version (), progname
) ;
207 /*==============================================================================
210 #else /* (HAVE_SNFILE == 0) */
212 /* Alternative main function when libsndfile is not available. */
218 "****************************************************************\n"
219 " This example program was compiled without libsndfile \n"
220 " (http://www.mega-nerd.com/libsndfile/).\n"
221 " It is therefore completely broken and non-functional.\n"
222 "****************************************************************\n"
This page took 0.051975 seconds and 4 git commands to generate.