sndfile-loopify.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. ** Copyright (C) 1999-2015 Erik de Castro Lopo <[email protected]>
  3. **
  4. ** All rights reserved.
  5. **
  6. ** Redistribution and use in source and binary forms, with or without
  7. ** modification, are permitted provided that the following conditions are
  8. ** met:
  9. **
  10. ** * Redistributions of source code must retain the above copyright
  11. ** notice, this list of conditions and the following disclaimer.
  12. ** * Redistributions in binary form must reproduce the above copyright
  13. ** notice, this list of conditions and the following disclaimer in
  14. ** the documentation and/or other materials provided with the
  15. ** distribution.
  16. ** * Neither the author nor the names of any contributors may be used
  17. ** to endorse or promote products derived from this software without
  18. ** specific prior written permission.
  19. **
  20. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27. ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  30. ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. /*
  33. ** A quick/rough hack to add SF_INSTRUMENT data to a file. It compiles, but
  34. ** no guarantees beyond that. Happy to receive patches to fix/improve it.
  35. **
  36. ** Code for this was stolen from programs/sndfile-convert.c and related code.
  37. */
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <ctype.h>
  42. #include <sndfile.h>
  43. #define BUFFER_LEN (1 << 14)
  44. const char * program_name (const char * argv0) ;
  45. static void sfe_copy_data_int (SNDFILE *outfile, SNDFILE *infile, int channels) ;
  46. static void add_instrument_data (SNDFILE *outfile, const SF_INFO * in_info) ;
  47. static void
  48. usage_exit (const char *progname)
  49. {
  50. printf ("\nUsage : %s <input file> <output file>\n", progname) ;
  51. puts ("") ;
  52. exit (1) ;
  53. } /* usage_exit */
  54. int
  55. main (int argc, char * argv [])
  56. { const char *progname, *infilename, *outfilename ;
  57. SNDFILE *infile = NULL, *outfile = NULL ;
  58. SF_INFO in_sfinfo, out_sfinfo ;
  59. progname = program_name (argv [0]) ;
  60. if (argc < 3 || argc > 5)
  61. usage_exit (progname) ;
  62. infilename = argv [argc-2] ;
  63. outfilename = argv [argc-1] ;
  64. if (strcmp (infilename, outfilename) == 0)
  65. { printf ("Error : Input and output filenames are the same.\n\n") ;
  66. usage_exit (progname) ;
  67. } ;
  68. if (strlen (infilename) > 1 && infilename [0] == '-')
  69. { printf ("Error : Input filename (%s) looks like an option.\n\n", infilename) ;
  70. usage_exit (progname) ;
  71. } ;
  72. if (outfilename [0] == '-')
  73. { printf ("Error : Output filename (%s) looks like an option.\n\n", outfilename) ;
  74. usage_exit (progname) ;
  75. } ;
  76. memset (&in_sfinfo, 0, sizeof (in_sfinfo)) ;
  77. if ((infile = sf_open (infilename, SFM_READ, &in_sfinfo)) == NULL)
  78. { printf ("Not able to open input file %s.\n", infilename) ;
  79. puts (sf_strerror (NULL)) ;
  80. return 1 ;
  81. } ;
  82. memcpy (&out_sfinfo, &in_sfinfo, sizeof (out_sfinfo)) ;
  83. /* Open the output file. */
  84. if ((outfile = sf_open (outfilename, SFM_WRITE, &out_sfinfo)) == NULL)
  85. { printf ("Not able to open output file %s : %s\n", outfilename, sf_strerror (NULL)) ;
  86. sf_close (infile) ;
  87. return 1 ;
  88. } ;
  89. /* Add the loop data */
  90. add_instrument_data (outfile, &in_sfinfo) ;
  91. /* Copy the audio data */
  92. sfe_copy_data_int (outfile, infile, in_sfinfo.channels) ;
  93. sf_close (infile) ;
  94. sf_close (outfile) ;
  95. return 0 ;
  96. } /* main */
  97. const char *
  98. program_name (const char * argv0)
  99. { const char * tmp ;
  100. tmp = strrchr (argv0, '/') ;
  101. argv0 = tmp ? tmp + 1 : argv0 ;
  102. /* Remove leading libtool name mangling. */
  103. if (strstr (argv0, "lt-") == argv0)
  104. return argv0 + 3 ;
  105. return argv0 ;
  106. } /* program_name */
  107. static void
  108. sfe_copy_data_int (SNDFILE *outfile, SNDFILE *infile, int channels)
  109. { static int data [BUFFER_LEN] ;
  110. int frames, readcount ;
  111. frames = BUFFER_LEN / channels ;
  112. readcount = frames ;
  113. while (readcount > 0)
  114. { readcount = (int) sf_readf_int (infile, data, frames) ;
  115. sf_writef_int (outfile, data, readcount) ;
  116. } ;
  117. return ;
  118. } /* sfe_copy_data_int */
  119. static void
  120. add_instrument_data (SNDFILE *file, const SF_INFO *info)
  121. { SF_INSTRUMENT instr ;
  122. memset (&instr, 0, sizeof (instr)) ;
  123. instr.gain = 1 ;
  124. instr.basenote = 0 ;
  125. instr.detune = 0 ;
  126. instr.velocity_lo = 0 ;
  127. instr.velocity_hi = 0 ;
  128. instr.key_lo = 0 ;
  129. instr.key_hi = 0 ;
  130. instr.loop_count = 1 ;
  131. instr.loops [0].mode = SF_LOOP_FORWARD ;
  132. instr.loops [0].start = 0 ;
  133. instr.loops [0].end = info->frames ;
  134. instr.loops [0].count = 0 ;
  135. if (sf_command (file, SFC_SET_INSTRUMENT, &instr, sizeof (instr)) == SF_FALSE)
  136. { printf ("\n\nLine %d : sf_command (SFC_SET_INSTRUMENT) failed.\n\n", __LINE__) ;
  137. exit (1) ;
  138. } ;
  139. return ;
  140. } /* add_instrument_data */