You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 lines
6.0KB

  1. /*
  2. ** Copyright (c) 2002-2016, Erik de Castro Lopo <erikd@mega-nerd.com>
  3. ** All rights reserved.
  4. **
  5. ** This code is released under 2-clause BSD license. Please see the
  6. ** file at : https://github.com/erikd/libsamplerate/blob/master/COPYING
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12. #include "config.h"
  13. #include <float_cast.h>
  14. #if (HAVE_SNDFILE)
  15. #include <samplerate.h>
  16. #include <sndfile.h>
  17. #include "audio_out.h"
  18. #define ARRAY_LEN(x) ((int) (sizeof (x) / sizeof ((x) [0])))
  19. #define BUFFER_LEN 4096
  20. #define VARISPEED_BLOCK_LEN 64
  21. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  22. #define SRC_MAGIC ((int) ('S' << 16) + ('R' << 8) + ('C'))
  23. #define SNDFILE_MAGIC ((int) ('s' << 24) + ('n' << 20) + ('d' << 16) + ('f' << 12) + ('i' << 8) + ('l' << 4) + 'e')
  24. #ifndef M_PI
  25. #define M_PI 3.14159265358979323846264338
  26. #endif
  27. typedef struct
  28. { int magic ;
  29. SNDFILE *sndfile ;
  30. SF_INFO sfinfo ;
  31. float buffer [BUFFER_LEN] ;
  32. } SNDFILE_CB_DATA ;
  33. typedef struct
  34. { int magic ;
  35. SNDFILE_CB_DATA sf ;
  36. int freq_point ;
  37. SRC_STATE *src_state ;
  38. } SRC_CB_DATA ;
  39. static int varispeed_get_data (SRC_CB_DATA *data, float *samples, int frames) ;
  40. static void varispeed_play (const char *filename, int converter) ;
  41. static long src_input_callback (void *cb_data, float **data) ;
  42. int
  43. main (int argc, char *argv [])
  44. { const char *cptr, *progname, *filename ;
  45. int k, converter ;
  46. converter = SRC_SINC_FASTEST ;
  47. progname = argv [0] ;
  48. if ((cptr = strrchr (progname, '/')) != NULL)
  49. progname = cptr + 1 ;
  50. if ((cptr = strrchr (progname, '\\')) != NULL)
  51. progname = cptr + 1 ;
  52. printf ("\n"
  53. " %s\n"
  54. "\n"
  55. " This is a demo program which plays the given file at a slowly \n"
  56. " varying speed. Lots of fun with drum loops and full mixes.\n"
  57. "\n"
  58. " It uses Secret Rabbit Code (aka libsamplerate) to perform the \n"
  59. " vari-speeding and libsndfile for file I/O.\n"
  60. "\n", progname) ;
  61. if (argc == 2)
  62. filename = argv [1] ;
  63. else if (argc == 4 && strcmp (argv [1], "-c") == 0)
  64. { filename = argv [3] ;
  65. converter = atoi (argv [2]) ;
  66. }
  67. else
  68. { printf (" Usage :\n\n %s [-c <number>] <input file>\n\n", progname) ;
  69. puts (
  70. " The optional -c argument allows the converter type to be chosen from\n"
  71. " the following list :"
  72. "\n"
  73. ) ;
  74. for (k = 0 ; (cptr = src_get_name (k)) != NULL ; k++)
  75. printf (" %d : %s\n", k, cptr) ;
  76. puts ("") ;
  77. exit (1) ;
  78. } ;
  79. varispeed_play (filename, converter) ;
  80. return 0 ;
  81. } /* main */
  82. /*==============================================================================
  83. */
  84. static void
  85. varispeed_play (const char *filename, int converter)
  86. { SRC_CB_DATA data ;
  87. AUDIO_OUT *audio_out ;
  88. int error ;
  89. memset (&data, 0, sizeof (data)) ;
  90. data.magic = SRC_MAGIC ;
  91. data.sf.magic = SNDFILE_MAGIC ;
  92. if ((data.sf.sndfile = sf_open (filename, SFM_READ, &data.sf.sfinfo)) == NULL)
  93. { puts (sf_strerror (NULL)) ;
  94. exit (1) ;
  95. } ;
  96. /* Initialize the sample rate converter. */
  97. if ((data.src_state = src_callback_new (src_input_callback, converter, data.sf.sfinfo.channels, &error, &data.sf)) == NULL)
  98. { printf ("\n\nError : src_new() failed : %s.\n\n", src_strerror (error)) ;
  99. exit (1) ;
  100. } ;
  101. printf (
  102. " Playing : %s\n"
  103. " Converter : %s\n"
  104. "\n"
  105. " Press <control-c> to exit.\n"
  106. "\n",
  107. filename, src_get_name (converter)) ;
  108. if ((audio_out = audio_open (data.sf.sfinfo.channels, data.sf.sfinfo.samplerate)) == NULL)
  109. { printf ("\n\nError : audio_open () failed.\n") ;
  110. exit (1) ;
  111. } ;
  112. /* Pass the data and the callbacl function to audio_play */
  113. audio_play ((get_audio_callback_t) varispeed_get_data, audio_out, &data) ;
  114. /* Cleanup */
  115. audio_close (audio_out) ;
  116. sf_close (data.sf.sndfile) ;
  117. src_delete (data.src_state) ;
  118. } /* varispeed_play */
  119. static long
  120. src_input_callback (void *cb_data, float **audio)
  121. { SNDFILE_CB_DATA * data = (SNDFILE_CB_DATA *) cb_data ;
  122. const int input_frames = ARRAY_LEN (data->buffer) / data->sfinfo.channels ;
  123. int read_frames ;
  124. if (data->magic != SNDFILE_MAGIC)
  125. { printf ("\n\n%s:%d Eeeek, something really bad happened!\n", __FILE__, __LINE__) ;
  126. exit (1) ;
  127. } ;
  128. for (read_frames = 0 ; read_frames < input_frames ; )
  129. { sf_count_t position ;
  130. read_frames += sf_readf_float (data->sndfile, data->buffer + read_frames * data->sfinfo.channels, input_frames - read_frames) ;
  131. position = sf_seek (data->sndfile, 0, SEEK_CUR) ;
  132. if (position < 0 || position == data->sfinfo.frames)
  133. sf_seek (data->sndfile, 0, SEEK_SET) ;
  134. } ;
  135. *audio = & (data->buffer [0]) ;
  136. return input_frames ;
  137. } /* src_input_callback */
  138. /*==============================================================================
  139. */
  140. static int
  141. varispeed_get_data (SRC_CB_DATA *data, float *samples, int out_frames)
  142. { float *output ;
  143. int rc, out_frame_count ;
  144. if (data->magic != SRC_MAGIC)
  145. { printf ("\n\n%s:%d Eeeek, something really bad happened!\n", __FILE__, __LINE__) ;
  146. exit (1) ;
  147. } ;
  148. for (out_frame_count = 0 ; out_frame_count < out_frames ; out_frame_count += VARISPEED_BLOCK_LEN)
  149. { double src_ratio = 1.0 - 0.5 * sin (data->freq_point * 2 * M_PI / 20000) ;
  150. data->freq_point ++ ;
  151. output = samples + out_frame_count * data->sf.sfinfo.channels ;
  152. if ((rc = src_callback_read (data->src_state, src_ratio, VARISPEED_BLOCK_LEN, output)) < VARISPEED_BLOCK_LEN)
  153. { printf ("\nError : src_callback_read short output (%d instead of %d)\n\n", rc, VARISPEED_BLOCK_LEN) ;
  154. exit (1) ;
  155. } ;
  156. } ;
  157. return out_frames ;
  158. } /* varispeed_get_data */
  159. /*==============================================================================
  160. */
  161. #else /* (HAVE_SNFILE == 0) */
  162. /* Alternative main function when libsndfile is not available. */
  163. int
  164. main (void)
  165. { puts (
  166. "\n"
  167. "****************************************************************\n"
  168. " This example program was compiled without libsndfile \n"
  169. " (http://www.zip.com.au/~erikd/libsndfile/).\n"
  170. " It is therefore completely broken and non-functional.\n"
  171. "****************************************************************\n"
  172. "\n"
  173. ) ;
  174. return 0 ;
  175. } /* main */
  176. #endif