sndfile-deinterleave.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_CHANNELS 16
  39. typedef struct
  40. { SNDFILE * infile ;
  41. SNDFILE * outfile [MAX_CHANNELS] ;
  42. union
  43. { double d [MAX_CHANNELS * BUFFER_LEN] ;
  44. int i [MAX_CHANNELS * BUFFER_LEN] ;
  45. } din ;
  46. union
  47. { double d [BUFFER_LEN] ;
  48. int i [BUFFER_LEN] ;
  49. } dout ;
  50. int channels ;
  51. } STATE ;
  52. static void usage_exit (void) ;
  53. static void deinterleave_int (STATE * state) ;
  54. static void deinterleave_double (STATE * state) ;
  55. int
  56. main (int argc, char **argv)
  57. { STATE state ;
  58. SF_INFO sfinfo ;
  59. char pathname [512], ext [32], *cptr ;
  60. int ch, double_split ;
  61. if (argc != 2)
  62. { if (argc != 1)
  63. puts ("\nError : need a single input file.\n") ;
  64. usage_exit () ;
  65. } ;
  66. memset (&state, 0, sizeof (state)) ;
  67. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  68. if ((state.infile = sf_open (argv [1], SFM_READ, &sfinfo)) == NULL)
  69. { printf ("\nError : Not able to open input file '%s'\n%s\n", argv [1], sf_strerror (NULL)) ;
  70. exit (1) ;
  71. } ;
  72. if (sfinfo.channels < 2)
  73. { printf ("\nError : Input file '%s' only has one channel.\n", argv [1]) ;
  74. exit (1) ;
  75. } ;
  76. state.channels = sfinfo.channels ;
  77. sfinfo.channels = 1 ;
  78. snprintf (pathname, sizeof (pathname), "%s", argv [1]) ;
  79. if ((cptr = strrchr (pathname, '.')) == NULL)
  80. ext [0] = 0 ;
  81. else
  82. { snprintf (ext, sizeof (ext), "%s", cptr) ;
  83. cptr [0] = 0 ;
  84. } ;
  85. printf ("Input file : %s\n", pathname) ;
  86. puts ("Output files :") ;
  87. for (ch = 0 ; ch < state.channels ; ch++)
  88. { char filename [520] ;
  89. snprintf (filename, sizeof (filename), "%s_%02d%s", pathname, ch, ext) ;
  90. if ((state.outfile [ch] = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
  91. { printf ("Not able to open output file '%s'\n%s\n", filename, sf_strerror (NULL)) ;
  92. exit (1) ;
  93. } ;
  94. printf (" %s\n", filename) ;
  95. } ;
  96. switch (sfinfo.format & SF_FORMAT_SUBMASK)
  97. { case SF_FORMAT_FLOAT :
  98. case SF_FORMAT_DOUBLE :
  99. case SF_FORMAT_VORBIS :
  100. double_split = 1 ;
  101. break ;
  102. default :
  103. double_split = 0 ;
  104. break ;
  105. } ;
  106. if (double_split)
  107. deinterleave_double (&state) ;
  108. else
  109. deinterleave_int (&state) ;
  110. sf_close (state.infile) ;
  111. for (ch = 0 ; ch < MAX_CHANNELS ; ch++)
  112. if (state.outfile [ch] != NULL)
  113. sf_close (state.outfile [ch]) ;
  114. return 0 ;
  115. } /* main */
  116. /*------------------------------------------------------------------------------
  117. */
  118. static void
  119. usage_exit (void)
  120. { puts ("\nUsage : sndfile-deinterleave <filename>\n") ;
  121. puts (
  122. "Split a mutli-channel file into a set of mono files.\n"
  123. "\n"
  124. "If the input file is named 'a.wav', the output files will be named\n"
  125. "a_00.wav, a_01.wav and so on.\n"
  126. ) ;
  127. printf ("Using %s.\n\n", sf_version_string ()) ;
  128. exit (0) ;
  129. } /* usage_exit */
  130. static void
  131. deinterleave_int (STATE * state)
  132. { int read_len ;
  133. int ch, k ;
  134. do
  135. { read_len = sf_readf_int (state->infile, state->din.i, BUFFER_LEN) ;
  136. for (ch = 0 ; ch < state->channels ; ch ++)
  137. { for (k = 0 ; k < read_len ; k++)
  138. state->dout.i [k] = state->din.i [k * state->channels + ch] ;
  139. sf_write_int (state->outfile [ch], state->dout.i, read_len) ;
  140. } ;
  141. }
  142. while (read_len > 0) ;
  143. } /* deinterleave_int */
  144. static void
  145. deinterleave_double (STATE * state)
  146. { int read_len ;
  147. int ch, k ;
  148. do
  149. { read_len = sf_readf_double (state->infile, state->din.d, BUFFER_LEN) ;
  150. for (ch = 0 ; ch < state->channels ; ch ++)
  151. { for (k = 0 ; k < read_len ; k++)
  152. state->dout.d [k] = state->din.d [k * state->channels + ch] ;
  153. sf_write_double (state->outfile [ch], state->dout.d, read_len) ;
  154. } ;
  155. }
  156. while (read_len > 0) ;
  157. } /* deinterleave_double */