sndfile-salvage.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. ** Copyright (C) 2010-2012 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 <inttypes.h>
  36. #include <ctype.h>
  37. #include <math.h>
  38. #include <errno.h>
  39. #include <unistd.h>
  40. #include <fcntl.h>
  41. #include <sys/stat.h>
  42. #include <sys/types.h>
  43. #include <sndfile.h>
  44. #include "common.h"
  45. #define BUFFER_LEN (1 << 16)
  46. #define NOT(x) (! (x))
  47. static void usage_exit (const char *progname) ;
  48. static void salvage_file (const char * broken_wav, const char * fixed_w64) ;
  49. int
  50. main (int argc, char *argv [])
  51. {
  52. if (argc != 3)
  53. usage_exit (program_name (argv [0])) ;
  54. salvage_file (argv [1], argv [2]) ;
  55. return 0 ;
  56. } /* main */
  57. /*==============================================================================
  58. */
  59. static void lseek_or_die (int fd, off_t offset, int whence) ;
  60. static sf_count_t get_file_length (int fd, const char * name) ;
  61. static sf_count_t find_data_offset (int fd, int format) ;
  62. static void copy_data (int fd, SNDFILE * sndfile, int readsize) ;
  63. static void
  64. usage_exit (const char *progname)
  65. { printf ("Usage :\n\n %s <broken wav file> <fixed w64 file>\n\n", progname) ;
  66. puts ("Salvages the audio data from WAV files which are more than 4G in length.\n") ;
  67. printf ("Using %s.\n\n", sf_version_string ()) ;
  68. exit (0) ;
  69. } /* usage_exit */
  70. static void
  71. salvage_file (const char * broken_wav, const char * fixed_w64)
  72. { SNDFILE * sndfile ;
  73. SF_INFO sfinfo ;
  74. sf_count_t broken_len, data_offset ;
  75. int fd, read_size ;
  76. if (strcmp (broken_wav, fixed_w64) == 0)
  77. { printf ("Error : Input and output files must be different.\n\n") ;
  78. exit (1) ;
  79. } ;
  80. if ((fd = open (broken_wav, O_RDONLY)) < 0)
  81. { printf ("Error : Not able to open file '%s' : %s\n", broken_wav, strerror (errno)) ;
  82. exit (1) ;
  83. } ;
  84. broken_len = get_file_length (fd, broken_wav) ;
  85. if (broken_len <= 0xffffffff)
  86. printf ("File is not greater than 4Gig but salvaging anyway.\n") ;
  87. /* Grab the format info from the broken file. */
  88. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  89. if ((sndfile = sf_open (broken_wav, SFM_READ, &sfinfo)) == NULL)
  90. { printf ("sf_open ('%s') failed : %s\n", broken_wav, sf_strerror (NULL)) ;
  91. exit (1) ;
  92. } ;
  93. sf_close (sndfile) ;
  94. data_offset = find_data_offset (fd, sfinfo.format & SF_FORMAT_TYPEMASK) ;
  95. printf ("Offset to audio data : %" PRId64 "\n", data_offset) ;
  96. switch (sfinfo.format & SF_FORMAT_TYPEMASK)
  97. { case SF_FORMAT_WAV :
  98. case SF_FORMAT_WAVEX :
  99. sfinfo.format = SF_FORMAT_W64 | (sfinfo.format & SF_FORMAT_SUBMASK) ;
  100. break ;
  101. default :
  102. printf ("Don't currently support this file type.\n") ;
  103. exit (1) ;
  104. } ;
  105. switch (sfinfo.format & SF_FORMAT_SUBMASK)
  106. { case SF_FORMAT_PCM_U8 :
  107. case SF_FORMAT_PCM_S8 :
  108. read_size = 1 ;
  109. break ;
  110. case SF_FORMAT_PCM_16 :
  111. read_size = 2 ;
  112. break ;
  113. case SF_FORMAT_PCM_24 :
  114. read_size = 3 ;
  115. break ;
  116. case SF_FORMAT_PCM_32 :
  117. case SF_FORMAT_FLOAT :
  118. read_size = 4 ;
  119. break ;
  120. case SF_FORMAT_DOUBLE :
  121. read_size = 8 ;
  122. break ;
  123. default :
  124. printf ("Sorry, don't currently support this file encoding type.\n") ;
  125. exit (1) ;
  126. } ;
  127. read_size *= sfinfo.channels ;
  128. if ((sndfile = sf_open (fixed_w64, SFM_WRITE, &sfinfo)) == NULL)
  129. { printf ("sf_open ('%s') failed : %s\n", broken_wav, sf_strerror (NULL)) ;
  130. exit (1) ;
  131. } ;
  132. lseek_or_die (fd, data_offset, SEEK_SET) ;
  133. copy_data (fd, sndfile, read_size) ;
  134. sf_close (sndfile) ;
  135. puts ("Done!") ;
  136. } /* salvage_file */
  137. /*------------------------------------------------------------------------------
  138. */
  139. static void
  140. lseek_or_die (int fd, off_t offset, int whence)
  141. {
  142. if (lseek (fd, offset, whence) < 0)
  143. { printf ("lseek failed : %s\n", strerror (errno)) ;
  144. exit (1) ;
  145. } ;
  146. return ;
  147. } /* lseek_or_die */
  148. static sf_count_t
  149. get_file_length (int fd, const char * name)
  150. { struct stat sbuf ;
  151. if (sizeof (sbuf.st_size) != 8)
  152. { puts ("Error : sizeof (sbuf.st_size) != 8. Was program compiled with\n"
  153. " 64 bit file offsets?\n") ;
  154. exit (1) ;
  155. } ;
  156. if (fstat (fd, &sbuf) != 0)
  157. { printf ("Error : fstat ('%s') failed : %s\n", name, strerror (errno)) ;
  158. exit (1) ;
  159. } ;
  160. return sbuf.st_size ;
  161. } /* get_file_length */
  162. static sf_count_t
  163. find_data_offset (int fd, int format)
  164. { char buffer [8192], *cptr ;
  165. const char * target = "XXXX" ;
  166. sf_count_t offset = -1, extra ;
  167. int rlen, slen ;
  168. switch (format)
  169. { case SF_FORMAT_WAV :
  170. case SF_FORMAT_WAVEX :
  171. target = "data" ;
  172. extra = 8 ;
  173. break ;
  174. case SF_FORMAT_AIFF :
  175. target = "SSND" ;
  176. extra = 16 ;
  177. break ;
  178. default :
  179. puts ("Error : Sorry, don't handle this input file format.\n") ;
  180. exit (1) ;
  181. } ;
  182. slen = strlen (target) ;
  183. lseek_or_die (fd, 0, SEEK_SET) ;
  184. printf ("Searching for '%s' maker.\n", target) ;
  185. if ((rlen = read (fd, buffer, sizeof (buffer))) < 0)
  186. { printf ("Error : failed read : %s\n", strerror (errno)) ;
  187. exit (1) ;
  188. } ;
  189. cptr = memchr (buffer, target [0], rlen - slen) ;
  190. if (cptr && memcmp (cptr, target, slen) == 0)
  191. offset = cptr - buffer ;
  192. else
  193. { printf ("Error : Could not find data offset.\n") ;
  194. exit (1) ;
  195. } ;
  196. return offset + extra ;
  197. } /* find_data_offset */
  198. static void
  199. copy_data (int fd, SNDFILE * sndfile, int readsize)
  200. { static char * buffer ;
  201. sf_count_t readlen, count ;
  202. int bufferlen, done = 0 ;
  203. bufferlen = readsize * 1024 ;
  204. buffer = malloc (bufferlen) ;
  205. while (NOT (done) && (readlen = read (fd, buffer, bufferlen)) >= 0)
  206. { if (readlen < bufferlen)
  207. { readlen -= readlen % readsize ;
  208. done = 1 ;
  209. } ;
  210. if ((count = sf_write_raw (sndfile, buffer, readlen)) != readlen)
  211. { printf ("Error : sf_write_raw returned %" PRId64 " : %s\n", count, sf_strerror (sndfile)) ;
  212. return ;
  213. } ;
  214. } ;
  215. free (buffer) ;
  216. return ;
  217. } /* copy_data */