Chris@0: /* Chris@0: ** Copyright (C) 2005-2011 Erik de Castro Lopo Chris@0: ** Chris@0: ** This program is free software; you can redistribute it and/or modify Chris@0: ** it under the terms of the GNU General Public License as published by Chris@0: ** the Free Software Foundation; either version 2 of the License, or Chris@0: ** (at your option) any later version. Chris@0: ** Chris@0: ** This program is distributed in the hope that it will be useful, Chris@0: ** but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: ** GNU General Public License for more details. Chris@0: ** Chris@0: ** You should have received a copy of the GNU General Public License Chris@0: ** along with this program; if not, write to the Free Software Chris@0: ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. Chris@0: */ Chris@0: Chris@0: #include "config.h" Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #if (HAVE_SNDFILE) Chris@0: Chris@0: #include Chris@0: #include Chris@0: Chris@0: #define ARRAY_LEN(x) ((int) (sizeof (x) / sizeof ((x) [0]))) Chris@0: Chris@0: #define DEFAULT_CONVERTER SRC_SINC_MEDIUM_QUALITY Chris@0: Chris@0: #define BUFFER_LEN 1024 Chris@0: #define INPUT_STEP_SIZE 8 Chris@0: Chris@0: typedef struct Chris@0: { sf_count_t index ; Chris@0: double ratio ; Chris@0: } TIMEWARP_FACTOR ; Chris@0: Chris@0: static void usage_exit (const char *progname) ; Chris@0: static sf_count_t timewarp_convert (SNDFILE *infile, SNDFILE *outfile, int converter, int channels) ; Chris@0: Chris@0: int Chris@0: main (int argc, char *argv []) Chris@0: { SNDFILE *infile, *outfile ; Chris@0: SF_INFO sfinfo ; Chris@0: sf_count_t count ; Chris@0: Chris@0: if (argc != 3) Chris@0: usage_exit (argv [0]) ; Chris@0: Chris@0: putchar ('\n') ; Chris@0: printf ("Input File : %s\n", argv [argc - 2]) ; Chris@0: if ((infile = sf_open (argv [argc - 2], SFM_READ, &sfinfo)) == NULL) Chris@0: { printf ("Error : Not able to open input file '%s'\n", argv [argc - 2]) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (INPUT_STEP_SIZE * sfinfo.channels > BUFFER_LEN) Chris@0: { printf ("\n\nError : INPUT_STEP_SIZE * sfinfo.channels > BUFFER_LEN\n\n") ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: Chris@0: /* Delete the output file length to zero if already exists. */ Chris@0: remove (argv [argc - 1]) ; Chris@0: Chris@0: if ((outfile = sf_open (argv [argc - 1], SFM_WRITE, &sfinfo)) == NULL) Chris@0: { printf ("Error : Not able to open output file '%s'\n", argv [argc - 1]) ; Chris@0: sf_close (infile) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: sf_command (outfile, SFC_SET_CLIPPING, NULL, SF_TRUE) ; Chris@0: Chris@0: printf ("Output file : %s\n", argv [argc - 1]) ; Chris@0: printf ("Converter : %s\n", src_get_name (DEFAULT_CONVERTER)) ; Chris@0: Chris@0: count = timewarp_convert (infile, outfile, DEFAULT_CONVERTER, sfinfo.channels) ; Chris@0: Chris@0: printf ("Output Frames : %ld\n\n", (long) count) ; Chris@0: Chris@0: sf_close (infile) ; Chris@0: sf_close (outfile) ; Chris@0: Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: Chris@0: /*============================================================================== Chris@0: */ Chris@0: Chris@0: static TIMEWARP_FACTOR warp [] = Chris@0: { { 0 , 1.00000001 }, Chris@0: { 20000 , 1.01000000 }, Chris@0: { 20200 , 1.00000001 }, Chris@0: { 40000 , 1.20000000 }, Chris@0: { 40300 , 1.00000001 }, Chris@0: { 60000 , 1.10000000 }, Chris@0: { 60400 , 1.00000001 }, Chris@0: { 80000 , 1.50000000 }, Chris@0: { 81000 , 1.00000001 }, Chris@0: } ; Chris@0: Chris@0: static sf_count_t Chris@0: timewarp_convert (SNDFILE *infile, SNDFILE *outfile, int converter, int channels) Chris@0: { static float input [BUFFER_LEN] ; Chris@0: static float output [BUFFER_LEN] ; Chris@0: Chris@0: SRC_STATE *src_state ; Chris@0: SRC_DATA src_data ; Chris@0: int error, warp_index = 0 ; Chris@0: sf_count_t input_count = 0, output_count = 0 ; Chris@0: Chris@0: sf_seek (infile, 0, SEEK_SET) ; Chris@0: sf_seek (outfile, 0, SEEK_SET) ; Chris@0: Chris@0: /* Initialize the sample rate converter. */ Chris@0: if ((src_state = src_new (converter, channels, &error)) == NULL) Chris@0: { printf ("\n\nError : src_new() failed : %s.\n\n", src_strerror (error)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: src_data.end_of_input = 0 ; /* Set this later. */ Chris@0: Chris@0: /* Start with zero to force load in while loop. */ Chris@0: src_data.input_frames = 0 ; Chris@0: src_data.data_in = input ; Chris@0: Chris@0: if (warp [0].index > 0) Chris@0: src_data.src_ratio = 1.0 ; Chris@0: else Chris@0: { src_data.src_ratio = warp [0].ratio ; Chris@0: warp_index ++ ; Chris@0: } ; Chris@0: Chris@0: src_data.data_out = output ; Chris@0: src_data.output_frames = BUFFER_LEN /channels ; Chris@0: Chris@0: while (1) Chris@0: { Chris@0: if (warp_index < ARRAY_LEN (warp) - 1 && input_count >= warp [warp_index].index) Chris@0: { src_data.src_ratio = warp [warp_index].ratio ; Chris@0: warp_index ++ ; Chris@0: } ; Chris@0: Chris@0: /* If the input buffer is empty, refill it. */ Chris@0: if (src_data.input_frames == 0) Chris@0: { src_data.input_frames = sf_readf_float (infile, input, INPUT_STEP_SIZE) ; Chris@0: input_count += src_data.input_frames ; Chris@0: src_data.data_in = input ; Chris@0: Chris@0: /* The last read will not be a full buffer, so snd_of_input. */ Chris@0: if (src_data.input_frames < INPUT_STEP_SIZE) Chris@0: src_data.end_of_input = SF_TRUE ; Chris@0: } ; Chris@0: Chris@0: /* Process current block. */ Chris@0: if ((error = src_process (src_state, &src_data))) Chris@0: { printf ("\nError : %s\n", src_strerror (error)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: /* Terminate if done. */ Chris@0: if (src_data.end_of_input && src_data.output_frames_gen == 0) Chris@0: break ; Chris@0: Chris@0: /* Write output. */ Chris@0: sf_writef_float (outfile, output, src_data.output_frames_gen) ; Chris@0: output_count += src_data.output_frames_gen ; Chris@0: Chris@0: src_data.data_in += src_data.input_frames_used * channels ; Chris@0: src_data.input_frames -= src_data.input_frames_used ; Chris@0: } ; Chris@0: Chris@0: src_state = src_delete (src_state) ; Chris@0: Chris@0: return output_count ; Chris@0: } /* timewarp_convert */ Chris@0: Chris@0: /*------------------------------------------------------------------------------ Chris@0: */ Chris@0: Chris@0: static void Chris@0: usage_exit (const char *progname) Chris@0: { const char *cptr ; Chris@0: Chris@0: if ((cptr = strrchr (progname, '/')) != NULL) Chris@0: progname = cptr + 1 ; Chris@0: Chris@0: if ((cptr = strrchr (progname, '\\')) != NULL) Chris@0: progname = cptr + 1 ; Chris@0: Chris@0: printf ("\n" Chris@0: " A program demonstrating the time warping capabilities of libsamplerate." Chris@0: " It uses libsndfile for file I/O and Secret Rabbit Code (aka libsamplerate)" Chris@0: " for performing the warping.\n" Chris@0: " It works on any file format supported by libsndfile with any \n" Chris@0: " number of channels (limited only by host memory).\n" Chris@0: "\n" Chris@0: " The warping is dependant on a table hard code into the source code.\n" Chris@0: "\n" Chris@0: " libsamplerate version : %s\n" Chris@0: "\n" Chris@0: " Usage : \n" Chris@0: " %s \n" Chris@0: "\n", src_get_version (), progname) ; Chris@0: Chris@0: puts ("") ; Chris@0: Chris@0: exit (1) ; Chris@0: } /* usage_exit */ Chris@0: Chris@0: /*============================================================================== Chris@0: */ Chris@0: Chris@0: #else /* (HAVE_SNFILE == 0) */ Chris@0: Chris@0: /* Alternative main function when libsndfile is not available. */ Chris@0: Chris@0: int Chris@0: main (void) Chris@0: { puts ( Chris@0: "\n" Chris@0: "****************************************************************\n" Chris@0: " This example program was compiled without libsndfile \n" Chris@0: " (http://www.mega-nerd.com/libsndfile/).\n" Chris@0: " It is therefore completely broken and non-functional.\n" Chris@0: "****************************************************************\n" Chris@0: "\n" Chris@0: ) ; Chris@0: Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: Chris@0: #endif Chris@0: