sndfile-interleave.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. ** Copyright (C) 2009-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
  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 <sndfile.h>
  36. #include "common.h"
  37. #define BUFFER_LEN 4096
  38. #define MAX_INPUTS 16
  39. typedef struct
  40. { SNDFILE * infile [MAX_INPUTS] ;
  41. SNDFILE * outfile ;
  42. union
  43. { double d [BUFFER_LEN] ;
  44. int i [BUFFER_LEN] ;
  45. } din ;
  46. union
  47. { double d [MAX_INPUTS * BUFFER_LEN] ;
  48. int i [MAX_INPUTS * BUFFER_LEN] ;
  49. } dout ;
  50. int channels ;
  51. } STATE ;
  52. static void usage_exit (void) ;
  53. static void interleave_int (STATE * state) ;
  54. static void interleave_double (STATE * state) ;
  55. int
  56. main (int argc, char **argv)
  57. { STATE state ;
  58. SF_INFO sfinfo ;
  59. int k, double_merge = 0 ;
  60. if (argc < 5)
  61. { if (argc > 1)
  62. puts ("\nError : need at least 2 input files.") ;
  63. usage_exit () ;
  64. } ;
  65. if (strcmp (argv [argc - 2], "-o") != 0)
  66. { puts ("\nError : second last command line parameter should be '-o'.\n") ;
  67. usage_exit () ;
  68. } ;
  69. if (argc - 3 > MAX_INPUTS)
  70. { printf ("\nError : Cannot handle more than %d input channels.\n\n", MAX_INPUTS) ;
  71. exit (1) ;
  72. } ;
  73. memset (&state, 0, sizeof (state)) ;
  74. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  75. for (k = 1 ; k < argc - 2 ; k++)
  76. {
  77. if ((state.infile [k - 1] = sf_open (argv [k], SFM_READ, &sfinfo)) == NULL)
  78. { printf ("\nError : Not able to open input file '%s'\n%s\n", argv [k], sf_strerror (NULL)) ;
  79. exit (1) ;
  80. } ;
  81. if (sfinfo.channels != 1)
  82. { printf ("\bError : Input file '%s' should be mono (has %d channels).\n", argv [k], sfinfo.channels) ;
  83. exit (1) ;
  84. } ;
  85. switch (sfinfo.format & SF_FORMAT_SUBMASK)
  86. { case SF_FORMAT_FLOAT :
  87. case SF_FORMAT_DOUBLE :
  88. case SF_FORMAT_VORBIS :
  89. double_merge = 1 ;
  90. break ;
  91. default :
  92. break ;
  93. } ;
  94. state.channels ++ ;
  95. } ;
  96. sfinfo.channels = state.channels ;
  97. sfinfo.format = sfe_file_type_of_ext (argv [argc - 1], sfinfo.format) ;
  98. if ((state.outfile = sf_open (argv [argc - 1], SFM_WRITE, &sfinfo)) == NULL)
  99. { printf ("Not able to open output file '%s'\n%s\n", argv [argc - 1], sf_strerror (NULL)) ;
  100. exit (1) ;
  101. } ;
  102. if (double_merge)
  103. interleave_double (&state) ;
  104. else
  105. interleave_int (&state) ;
  106. for (k = 0 ; k < MAX_INPUTS ; k++)
  107. if (state.infile [k] != NULL)
  108. sf_close (state.infile [k]) ;
  109. sf_close (state.outfile) ;
  110. return 0 ;
  111. } /* main */
  112. /*------------------------------------------------------------------------------
  113. */
  114. static void
  115. usage_exit (void)
  116. { puts ("\nUsage : sndfile-interleave <input 1> <input 2> ... -o <output file>\n") ;
  117. puts ("Merge two or more mono files into a single multi-channel file.\n") ;
  118. printf ("Using %s.\n\n", sf_version_string ()) ;
  119. exit (0) ;
  120. } /* usage_exit */
  121. static void
  122. interleave_int (STATE * state)
  123. { int max_read_len, read_len ;
  124. int ch, k ;
  125. do
  126. { max_read_len = 0 ;
  127. for (ch = 0 ; ch < state->channels ; ch ++)
  128. { read_len = sf_read_int (state->infile [ch], state->din.i, BUFFER_LEN) ;
  129. if (read_len < BUFFER_LEN)
  130. memset (state->din.i + read_len, 0, sizeof (state->din.i [0]) * (BUFFER_LEN - read_len)) ;
  131. for (k = 0 ; k < read_len ; k++)
  132. state->dout.i [k * state->channels + ch] = state->din.i [k] ;
  133. max_read_len = MAX (max_read_len, read_len) ;
  134. } ;
  135. sf_writef_int (state->outfile, state->dout.i, max_read_len) ;
  136. }
  137. while (max_read_len > 0) ;
  138. } /* interleave_int */
  139. static void
  140. interleave_double (STATE * state)
  141. { int max_read_len, read_len ;
  142. int ch, k ;
  143. do
  144. { max_read_len = 0 ;
  145. for (ch = 0 ; ch < state->channels ; ch ++)
  146. { read_len = sf_read_double (state->infile [ch], state->din.d, BUFFER_LEN) ;
  147. if (read_len < BUFFER_LEN)
  148. memset (state->din.d + read_len, 0, sizeof (state->din.d [0]) * (BUFFER_LEN - read_len)) ;
  149. for (k = 0 ; k < read_len ; k++)
  150. state->dout.d [k * state->channels + ch] = state->din.d [k] ;
  151. max_read_len = MAX (max_read_len, read_len) ;
  152. } ;
  153. sf_writef_double (state->outfile, state->dout.d, max_read_len) ;
  154. }
  155. while (max_read_len > 0) ;
  156. } /* interleave_double */