Chris@41: /* Chris@41: ** Copyright (c) 2002-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 Chris@41: #include Chris@41: #include Chris@41: #include Chris@41: Chris@41: #include "config.h" Chris@41: Chris@41: #include Chris@41: Chris@41: #if (HAVE_SNDFILE) Chris@41: Chris@41: #include Chris@41: #include Chris@41: Chris@41: #include "audio_out.h" Chris@41: Chris@41: #define ARRAY_LEN(x) ((int) (sizeof (x) / sizeof ((x) [0]))) Chris@41: Chris@41: #define BUFFER_LEN 4096 Chris@41: #define VARISPEED_BLOCK_LEN 64 Chris@41: Chris@41: #define MIN(a,b) ((a) < (b) ? (a) : (b)) Chris@41: Chris@41: #define SRC_MAGIC ((int) ('S' << 16) + ('R' << 8) + ('C')) Chris@41: #define SNDFILE_MAGIC ((int) ('s' << 24) + ('n' << 20) + ('d' << 16) + ('f' << 12) + ('i' << 8) + ('l' << 4) + 'e') Chris@41: Chris@41: #ifndef M_PI Chris@41: #define M_PI 3.14159265358979323846264338 Chris@41: #endif Chris@41: Chris@41: Chris@41: typedef struct Chris@41: { int magic ; Chris@41: SNDFILE *sndfile ; Chris@41: SF_INFO sfinfo ; Chris@41: Chris@41: float buffer [BUFFER_LEN] ; Chris@41: } SNDFILE_CB_DATA ; Chris@41: Chris@41: typedef struct Chris@41: { int magic ; Chris@41: Chris@41: SNDFILE_CB_DATA sf ; Chris@41: Chris@41: int freq_point ; Chris@41: Chris@41: SRC_STATE *src_state ; Chris@41: Chris@41: } SRC_CB_DATA ; Chris@41: Chris@41: static int varispeed_get_data (SRC_CB_DATA *data, float *samples, int frames) ; Chris@41: static void varispeed_play (const char *filename, int converter) ; Chris@41: Chris@41: static long src_input_callback (void *cb_data, float **data) ; Chris@41: Chris@41: int Chris@41: main (int argc, char *argv []) Chris@41: { const char *cptr, *progname, *filename ; Chris@41: int k, converter ; Chris@41: Chris@41: converter = SRC_SINC_FASTEST ; Chris@41: Chris@41: progname = argv [0] ; 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: " %s\n" Chris@41: "\n" Chris@41: " This is a demo program which plays the given file at a slowly \n" Chris@41: " varying speed. Lots of fun with drum loops and full mixes.\n" Chris@41: "\n" Chris@41: " It uses Secret Rabbit Code (aka libsamplerate) to perform the \n" Chris@41: " vari-speeding and libsndfile for file I/O.\n" Chris@41: "\n", progname) ; Chris@41: Chris@41: if (argc == 2) Chris@41: filename = argv [1] ; Chris@41: else if (argc == 4 && strcmp (argv [1], "-c") == 0) Chris@41: { filename = argv [3] ; Chris@41: converter = atoi (argv [2]) ; Chris@41: } Chris@41: else Chris@41: { printf (" Usage :\n\n %s [-c ] \n\n", progname) ; Chris@41: puts ( Chris@41: " The optional -c argument allows the converter type to be chosen from\n" Chris@41: " the following list :" Chris@41: "\n" Chris@41: ) ; Chris@41: Chris@41: for (k = 0 ; (cptr = src_get_name (k)) != NULL ; k++) Chris@41: printf (" %d : %s\n", k, cptr) ; Chris@41: Chris@41: puts ("") ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: Chris@41: varispeed_play (filename, converter) ; Chris@41: Chris@41: return 0 ; Chris@41: } /* main */ Chris@41: Chris@41: /*============================================================================== Chris@41: */ Chris@41: Chris@41: static void Chris@41: varispeed_play (const char *filename, int converter) Chris@41: { SRC_CB_DATA data ; Chris@41: AUDIO_OUT *audio_out ; Chris@41: int error ; Chris@41: Chris@41: memset (&data, 0, sizeof (data)) ; Chris@41: Chris@41: data.magic = SRC_MAGIC ; Chris@41: data.sf.magic = SNDFILE_MAGIC ; Chris@41: Chris@41: if ((data.sf.sndfile = sf_open (filename, SFM_READ, &data.sf.sfinfo)) == NULL) Chris@41: { puts (sf_strerror (NULL)) ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: Chris@41: /* Initialize the sample rate converter. */ Chris@41: if ((data.src_state = src_callback_new (src_input_callback, converter, data.sf.sfinfo.channels, &error, &data.sf)) == 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: printf ( Chris@41: Chris@41: " Playing : %s\n" Chris@41: " Converter : %s\n" Chris@41: "\n" Chris@41: " Press to exit.\n" Chris@41: "\n", Chris@41: filename, src_get_name (converter)) ; Chris@41: Chris@41: if ((audio_out = audio_open (data.sf.sfinfo.channels, data.sf.sfinfo.samplerate)) == NULL) Chris@41: { printf ("\n\nError : audio_open () failed.\n") ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: Chris@41: /* Pass the data and the callbacl function to audio_play */ Chris@41: audio_play ((get_audio_callback_t) varispeed_get_data, audio_out, &data) ; Chris@41: Chris@41: /* Cleanup */ Chris@41: audio_close (audio_out) ; Chris@41: sf_close (data.sf.sndfile) ; Chris@41: src_delete (data.src_state) ; Chris@41: Chris@41: } /* varispeed_play */ Chris@41: Chris@41: static long Chris@41: src_input_callback (void *cb_data, float **audio) Chris@41: { SNDFILE_CB_DATA * data = (SNDFILE_CB_DATA *) cb_data ; Chris@41: const int input_frames = ARRAY_LEN (data->buffer) / data->sfinfo.channels ; Chris@41: int read_frames ; Chris@41: Chris@41: if (data->magic != SNDFILE_MAGIC) Chris@41: { printf ("\n\n%s:%d Eeeek, something really bad happened!\n", __FILE__, __LINE__) ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: Chris@41: for (read_frames = 0 ; read_frames < input_frames ; ) Chris@41: { sf_count_t position ; Chris@41: Chris@41: read_frames += sf_readf_float (data->sndfile, data->buffer + read_frames * data->sfinfo.channels, input_frames - read_frames) ; Chris@41: Chris@41: position = sf_seek (data->sndfile, 0, SEEK_CUR) ; Chris@41: Chris@41: if (position < 0 || position == data->sfinfo.frames) Chris@41: sf_seek (data->sndfile, 0, SEEK_SET) ; Chris@41: } ; Chris@41: Chris@41: *audio = & (data->buffer [0]) ; Chris@41: Chris@41: return input_frames ; Chris@41: } /* src_input_callback */ Chris@41: Chris@41: Chris@41: /*============================================================================== Chris@41: */ Chris@41: Chris@41: static int Chris@41: varispeed_get_data (SRC_CB_DATA *data, float *samples, int out_frames) Chris@41: { float *output ; Chris@41: int rc, out_frame_count ; Chris@41: Chris@41: if (data->magic != SRC_MAGIC) Chris@41: { printf ("\n\n%s:%d Eeeek, something really bad happened!\n", __FILE__, __LINE__) ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: Chris@41: for (out_frame_count = 0 ; out_frame_count < out_frames ; out_frame_count += VARISPEED_BLOCK_LEN) Chris@41: { double src_ratio = 1.0 - 0.5 * sin (data->freq_point * 2 * M_PI / 20000) ; Chris@41: Chris@41: data->freq_point ++ ; Chris@41: Chris@41: output = samples + out_frame_count * data->sf.sfinfo.channels ; Chris@41: Chris@41: if ((rc = src_callback_read (data->src_state, src_ratio, VARISPEED_BLOCK_LEN, output)) < VARISPEED_BLOCK_LEN) Chris@41: { printf ("\nError : src_callback_read short output (%d instead of %d)\n\n", rc, VARISPEED_BLOCK_LEN) ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: } ; Chris@41: Chris@41: return out_frames ; Chris@41: } /* varispeed_get_data */ 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.zip.com.au/~erikd/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: