sndfile-interleave.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. ** Copyright (C) 2009-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. #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 print_usage (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 = NULL ;
  58. SF_INFO sfinfo ;
  59. int k, double_merge = 0 ;
  60. int ret = 1 ;
  61. if (argc < 5)
  62. { if (argc > 1)
  63. puts ("\nError : need at least 2 input files.") ;
  64. print_usage () ;
  65. goto cleanup ;
  66. } ;
  67. if (strcmp (argv [argc - 2], "-o") != 0)
  68. { puts ("\nError : second last command line parameter should be '-o'.\n") ;
  69. print_usage () ;
  70. goto cleanup ;
  71. } ;
  72. if (argc - 3 > MAX_INPUTS)
  73. { printf ("\nError : Cannot handle more than %d input channels.\n\n", MAX_INPUTS) ;
  74. goto cleanup ;
  75. } ;
  76. state = calloc (1, sizeof (STATE)) ;
  77. if (state == NULL)
  78. { puts ("\nError : out of memory.\n") ;
  79. goto cleanup ;
  80. } ;
  81. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  82. for (k = 1 ; k < argc - 2 ; k++)
  83. {
  84. if ((state->infile [k - 1] = sf_open (argv [k], SFM_READ, &sfinfo)) == NULL)
  85. { printf ("\nError : Not able to open input file '%s'\n%s\n", argv [k], sf_strerror (NULL)) ;
  86. goto cleanup ;
  87. } ;
  88. if (sfinfo.channels != 1)
  89. { printf ("\bError : Input file '%s' should be mono (has %d channels).\n", argv [k], sfinfo.channels) ;
  90. goto cleanup ;
  91. } ;
  92. switch (sfinfo.format & SF_FORMAT_SUBMASK)
  93. { case SF_FORMAT_FLOAT :
  94. case SF_FORMAT_DOUBLE :
  95. case SF_FORMAT_VORBIS :
  96. double_merge = 1 ;
  97. break ;
  98. default :
  99. break ;
  100. } ;
  101. state->channels ++ ;
  102. } ;
  103. sfinfo.channels = state->channels ;
  104. sfinfo.format = sfe_file_type_of_ext (argv [argc - 1], sfinfo.format) ;
  105. if ((state->outfile = sf_open (argv [argc - 1], SFM_WRITE, &sfinfo)) == NULL)
  106. { printf ("Not able to open output file '%s'\n%s\n", argv [argc - 1], sf_strerror (NULL)) ;
  107. goto cleanup ;
  108. } ;
  109. if (double_merge)
  110. interleave_double (state) ;
  111. else
  112. interleave_int (state) ;
  113. ret = 0 ;
  114. cleanup :
  115. if (state != NULL)
  116. { for (k = 0 ; k < MAX_INPUTS ; k++)
  117. if (state->infile [k] != NULL)
  118. sf_close (state->infile [k]) ;
  119. sf_close (state->outfile) ;
  120. }
  121. free (state) ;
  122. return ret ;
  123. } /* main */
  124. /*------------------------------------------------------------------------------
  125. */
  126. static void
  127. print_usage (void)
  128. { puts ("\nUsage : sndfile-interleave <input 1> <input 2> ... -o <output file>\n") ;
  129. puts ("Merge two or more mono files into a single multi-channel file.\n") ;
  130. printf ("Using %s.\n\n", sf_version_string ()) ;
  131. } /* print_usage */
  132. static void
  133. interleave_int (STATE * state)
  134. { int max_read_len, read_len ;
  135. int ch, k ;
  136. do
  137. { max_read_len = 0 ;
  138. for (ch = 0 ; ch < state->channels ; ch ++)
  139. { read_len = (int) sf_read_int (state->infile [ch], state->din.i, BUFFER_LEN) ;
  140. if (read_len < BUFFER_LEN)
  141. memset (state->din.i + read_len, 0, sizeof (state->din.i [0]) * (BUFFER_LEN - read_len)) ;
  142. for (k = 0 ; k < BUFFER_LEN ; k++)
  143. state->dout.i [k * state->channels + ch] = state->din.i [k] ;
  144. max_read_len = MAX (max_read_len, read_len) ;
  145. } ;
  146. sf_writef_int (state->outfile, state->dout.i, max_read_len) ;
  147. }
  148. while (max_read_len > 0) ;
  149. } /* interleave_int */
  150. static void
  151. interleave_double (STATE * state)
  152. { int max_read_len, read_len ;
  153. int ch, k ;
  154. do
  155. { max_read_len = 0 ;
  156. for (ch = 0 ; ch < state->channels ; ch ++)
  157. { read_len = (int) sf_read_double (state->infile [ch], state->din.d, BUFFER_LEN) ;
  158. if (read_len < BUFFER_LEN)
  159. memset (state->din.d + read_len, 0, sizeof (state->din.d [0]) * (BUFFER_LEN - read_len)) ;
  160. for (k = 0 ; k < BUFFER_LEN ; k++)
  161. state->dout.d [k * state->channels + ch] = state->din.d [k] ;
  162. max_read_len = MAX (max_read_len, read_len) ;
  163. } ;
  164. sf_writef_double (state->outfile, state->dout.d, max_read_len) ;
  165. }
  166. while (max_read_len > 0) ;
  167. } /* interleave_double */