sndfile-concat.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. ** Copyright (C) 1999-2014 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. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <ctype.h>
  36. #include <sndfile.h>
  37. #include "common.h"
  38. #define BUFFER_LEN (1 << 16)
  39. static void concat_data_fp (SNDFILE *wfile, SNDFILE *rofile, int channels) ;
  40. static void concat_data_int (SNDFILE *wfile, SNDFILE *rofile, int channels) ;
  41. static void
  42. usage_exit (const char *progname)
  43. {
  44. printf ("\nUsage : %s <infile1> <infile2> ... <outfile>\n\n", progname) ;
  45. puts (
  46. " Create a new output file <outfile> containing the concatenated\n"
  47. " audio data from froms <infile1> <infile2> ....\n"
  48. "\n"
  49. " The joined file will be encoded in the same format as the data\n"
  50. " in infile1, with all the data in subsequent files automatically\n"
  51. " converted to the correct encoding.\n"
  52. "\n"
  53. " The only restriction is that the two files must have the same\n"
  54. " number of channels.\n"
  55. ) ;
  56. exit (1) ;
  57. } /* usage_exit */
  58. int
  59. main (int argc, char *argv [])
  60. { const char *progname, *outfilename ;
  61. SNDFILE *outfile, **infiles ;
  62. SF_INFO sfinfo_out, sfinfo_in ;
  63. void (*func) (SNDFILE*, SNDFILE*, int) ;
  64. int k ;
  65. progname = program_name (argv [0]) ;
  66. if (argc < 4)
  67. usage_exit (progname) ;
  68. argv ++ ;
  69. argc -- ;
  70. argc -- ;
  71. outfilename = argv [argc] ;
  72. if ((infiles = calloc (argc, sizeof (SNDFILE*))) == NULL)
  73. { printf ("\nError : Malloc failed.\n\n") ;
  74. exit (1) ;
  75. } ;
  76. memset (&sfinfo_in, 0, sizeof (sfinfo_in)) ;
  77. if ((infiles [0] = sf_open (argv [0], SFM_READ, &sfinfo_in)) == NULL)
  78. { printf ("\nError : failed to open file '%s'.\n\n", argv [0]) ;
  79. exit (1) ;
  80. } ;
  81. sfinfo_out = sfinfo_in ;
  82. for (k = 1 ; k < argc ; k++)
  83. { if ((infiles [k] = sf_open (argv [k], SFM_READ, &sfinfo_in)) == NULL)
  84. { printf ("\nError : failed to open file '%s'.\n\n", argv [k]) ;
  85. exit (1) ;
  86. } ;
  87. if (sfinfo_in.channels != sfinfo_out.channels)
  88. { printf ("\nError : File '%s' has %d channels (should have %d).\n\n", argv [k], sfinfo_in.channels, sfinfo_out.channels) ;
  89. exit (1) ;
  90. } ;
  91. } ;
  92. if ((outfile = sf_open (outfilename, SFM_WRITE, &sfinfo_out)) == NULL)
  93. { printf ("\nError : Not able to open input file %s.\n", outfilename) ;
  94. puts (sf_strerror (NULL)) ;
  95. exit (1) ;
  96. } ;
  97. if ((sfinfo_out.format & SF_FORMAT_SUBMASK) == SF_FORMAT_DOUBLE ||
  98. (sfinfo_out.format & SF_FORMAT_SUBMASK) == SF_FORMAT_FLOAT)
  99. func = concat_data_fp ;
  100. else
  101. func = concat_data_int ;
  102. for (k = 0 ; k < argc ; k++)
  103. { func (outfile, infiles [k], sfinfo_out.channels) ;
  104. sf_close (infiles [k]) ;
  105. } ;
  106. sf_close (outfile) ;
  107. free (infiles) ;
  108. return 0 ;
  109. } /* main */
  110. static void
  111. concat_data_fp (SNDFILE *wfile, SNDFILE *rofile, int channels)
  112. { static double data [BUFFER_LEN] ;
  113. int frames, readcount ;
  114. frames = BUFFER_LEN / channels ;
  115. readcount = frames ;
  116. sf_seek (wfile, 0, SEEK_END) ;
  117. while (readcount > 0)
  118. { readcount = (int) sf_readf_double (rofile, data, frames) ;
  119. sf_writef_double (wfile, data, readcount) ;
  120. } ;
  121. return ;
  122. } /* concat_data_fp */
  123. static void
  124. concat_data_int (SNDFILE *wfile, SNDFILE *rofile, int channels)
  125. { static int data [BUFFER_LEN] ;
  126. int frames, readcount ;
  127. frames = BUFFER_LEN / channels ;
  128. readcount = frames ;
  129. sf_seek (wfile, 0, SEEK_END) ;
  130. while (readcount > 0)
  131. { readcount = (int) sf_readf_int (rofile, data, frames) ;
  132. sf_writef_int (wfile, data, readcount) ;
  133. } ;
  134. return ;
  135. } /* concat_data_int */