annotate src/libsamplerate-0.1.8/examples/timewarp-file.c @ 169:223a55898ab9 tip default

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