Chris@41: /* Chris@41: ** Copyright (c) 2005-2016, Erik de Castro Lopo Chris@41: ** All rights reserved. Chris@41: ** Chris@41: ** This code is released under 2-clause BSD license. Please see the Chris@41: ** file at : https://github.com/erikd/libsamplerate/blob/master/COPYING Chris@41: */ Chris@41: Chris@41: #include "config.h" Chris@41: Chris@41: #include Chris@41: #include Chris@41: #include Chris@41: #include Chris@41: #include Chris@41: Chris@41: #if (HAVE_SNDFILE) Chris@41: Chris@41: #include Chris@41: #include Chris@41: Chris@41: #define ARRAY_LEN(x) ((int) (sizeof (x) / sizeof ((x) [0]))) Chris@41: Chris@41: #define DEFAULT_CONVERTER SRC_SINC_MEDIUM_QUALITY Chris@41: Chris@41: #define BUFFER_LEN 1024 Chris@41: #define INPUT_STEP_SIZE 8 Chris@41: Chris@41: typedef struct Chris@41: { sf_count_t index ; Chris@41: double ratio ; Chris@41: } TIMEWARP_FACTOR ; Chris@41: Chris@41: static void usage_exit (const char *progname) ; Chris@41: static sf_count_t timewarp_convert (SNDFILE *infile, SNDFILE *outfile, int converter, int channels) ; Chris@41: Chris@41: int Chris@41: main (int argc, char *argv []) Chris@41: { SNDFILE *infile, *outfile ; Chris@41: SF_INFO sfinfo ; Chris@41: sf_count_t count ; Chris@41: Chris@41: if (argc != 3) Chris@41: usage_exit (argv [0]) ; Chris@41: Chris@41: putchar ('\n') ; Chris@41: printf ("Input File : %s\n", argv [argc - 2]) ; Chris@41: if ((infile = sf_open (argv [argc - 2], SFM_READ, &sfinfo)) == NULL) Chris@41: { printf ("Error : Not able to open input file '%s'\n", argv [argc - 2]) ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: Chris@41: if (INPUT_STEP_SIZE * sfinfo.channels > BUFFER_LEN) Chris@41: { printf ("\n\nError : INPUT_STEP_SIZE * sfinfo.channels > BUFFER_LEN\n\n") ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: Chris@41: Chris@41: /* Delete the output file length to zero if already exists. */ Chris@41: remove (argv [argc - 1]) ; Chris@41: Chris@41: if ((outfile = sf_open (argv [argc - 1], SFM_WRITE, &sfinfo)) == NULL) Chris@41: { printf ("Error : Not able to open output file '%s'\n", argv [argc - 1]) ; Chris@41: sf_close (infile) ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: Chris@41: sf_command (outfile, SFC_SET_CLIPPING, NULL, SF_TRUE) ; Chris@41: Chris@41: printf ("Output file : %s\n", argv [argc - 1]) ; Chris@41: printf ("Converter : %s\n", src_get_name (DEFAULT_CONVERTER)) ; Chris@41: Chris@41: count = timewarp_convert (infile, outfile, DEFAULT_CONVERTER, sfinfo.channels) ; Chris@41: Chris@41: printf ("Output Frames : %ld\n\n", (long) count) ; Chris@41: Chris@41: sf_close (infile) ; Chris@41: sf_close (outfile) ; Chris@41: Chris@41: return 0 ; Chris@41: } /* main */ Chris@41: Chris@41: /*============================================================================== Chris@41: */ Chris@41: Chris@41: static TIMEWARP_FACTOR warp [] = Chris@41: { { 0 , 1.00000001 }, Chris@41: { 20000 , 1.01000000 }, Chris@41: { 20200 , 1.00000001 }, Chris@41: { 40000 , 1.20000000 }, Chris@41: { 40300 , 1.00000001 }, Chris@41: { 60000 , 1.10000000 }, Chris@41: { 60400 , 1.00000001 }, Chris@41: { 80000 , 1.50000000 }, Chris@41: { 81000 , 1.00000001 }, Chris@41: } ; Chris@41: Chris@41: static sf_count_t Chris@41: timewarp_convert (SNDFILE *infile, SNDFILE *outfile, int converter, int channels) Chris@41: { static float input [BUFFER_LEN] ; Chris@41: static float output [BUFFER_LEN] ; Chris@41: Chris@41: SRC_STATE *src_state ; Chris@41: SRC_DATA src_data ; Chris@41: int error, warp_index = 0 ; Chris@41: sf_count_t input_count = 0, output_count = 0 ; Chris@41: Chris@41: sf_seek (infile, 0, SEEK_SET) ; Chris@41: sf_seek (outfile, 0, SEEK_SET) ; Chris@41: Chris@41: /* Initialize the sample rate converter. */ Chris@41: if ((src_state = src_new (converter, channels, &error)) == NULL) Chris@41: { printf ("\n\nError : src_new() failed : %s.\n\n", src_strerror (error)) ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: Chris@41: src_data.end_of_input = 0 ; /* Set this later. */ Chris@41: Chris@41: /* Start with zero to force load in while loop. */ Chris@41: src_data.input_frames = 0 ; Chris@41: src_data.data_in = input ; Chris@41: Chris@41: if (warp [0].index > 0) Chris@41: src_data.src_ratio = 1.0 ; Chris@41: else Chris@41: { src_data.src_ratio = warp [0].ratio ; Chris@41: warp_index ++ ; Chris@41: } ; Chris@41: Chris@41: src_data.data_out = output ; Chris@41: src_data.output_frames = BUFFER_LEN /channels ; Chris@41: Chris@41: while (1) Chris@41: { Chris@41: if (warp_index < ARRAY_LEN (warp) - 1 && input_count >= warp [warp_index].index) Chris@41: { src_data.src_ratio = warp [warp_index].ratio ; Chris@41: warp_index ++ ; Chris@41: } ; Chris@41: Chris@41: /* If the input buffer is empty, refill it. */ Chris@41: if (src_data.input_frames == 0) Chris@41: { src_data.input_frames = sf_readf_float (infile, input, INPUT_STEP_SIZE) ; Chris@41: input_count += src_data.input_frames ; Chris@41: src_data.data_in = input ; Chris@41: Chris@41: /* The last read will not be a full buffer, so snd_of_input. */ Chris@41: if (src_data.input_frames < INPUT_STEP_SIZE) Chris@41: src_data.end_of_input = SF_TRUE ; Chris@41: } ; Chris@41: Chris@41: /* Process current block. */ Chris@41: if ((error = src_process (src_state, &src_data))) Chris@41: { printf ("\nError : %s\n", src_strerror (error)) ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: Chris@41: /* Terminate if done. */ Chris@41: if (src_data.end_of_input && src_data.output_frames_gen == 0) Chris@41: break ; Chris@41: Chris@41: /* Write output. */ Chris@41: sf_writef_float (outfile, output, src_data.output_frames_gen) ; Chris@41: output_count += src_data.output_frames_gen ; Chris@41: Chris@41: src_data.data_in += src_data.input_frames_used * channels ; Chris@41: src_data.input_frames -= src_data.input_frames_used ; Chris@41: } ; Chris@41: Chris@41: src_delete (src_state) ; Chris@41: Chris@41: return output_count ; Chris@41: } /* timewarp_convert */ Chris@41: Chris@41: /*------------------------------------------------------------------------------ Chris@41: */ Chris@41: Chris@41: static void Chris@41: usage_exit (const char *progname) Chris@41: { const char *cptr ; Chris@41: Chris@41: if ((cptr = strrchr (progname, '/')) != NULL) Chris@41: progname = cptr + 1 ; Chris@41: Chris@41: if ((cptr = strrchr (progname, '\\')) != NULL) Chris@41: progname = cptr + 1 ; Chris@41: Chris@41: printf ("\n" Chris@41: " A program demonstrating the time warping capabilities of libsamplerate." Chris@41: " It uses libsndfile for file I/O and Secret Rabbit Code (aka libsamplerate)" Chris@41: " for performing the warping.\n" Chris@41: " It works on any file format supported by libsndfile with any \n" Chris@41: " number of channels (limited only by host memory).\n" Chris@41: "\n" Chris@41: " The warping is dependant on a table hard code into the source code.\n" Chris@41: "\n" Chris@41: " libsamplerate version : %s\n" Chris@41: "\n" Chris@41: " Usage : \n" Chris@41: " %s \n" Chris@41: "\n", src_get_version (), progname) ; Chris@41: Chris@41: puts ("") ; Chris@41: Chris@41: exit (1) ; Chris@41: } /* usage_exit */ Chris@41: Chris@41: /*============================================================================== Chris@41: */ Chris@41: Chris@41: #else /* (HAVE_SNFILE == 0) */ Chris@41: Chris@41: /* Alternative main function when libsndfile is not available. */ Chris@41: Chris@41: int Chris@41: main (void) Chris@41: { puts ( Chris@41: "\n" Chris@41: "****************************************************************\n" Chris@41: " This example program was compiled without libsndfile \n" Chris@41: " (http://www.mega-nerd.com/libsndfile/).\n" Chris@41: " It is therefore completely broken and non-functional.\n" Chris@41: "****************************************************************\n" Chris@41: "\n" Chris@41: ) ; Chris@41: Chris@41: return 0 ; Chris@41: } /* main */ Chris@41: Chris@41: #endif Chris@41: