Chris@0: /* Chris@0: ** Copyright (C) 2010-2011 Erik de Castro Lopo Chris@0: ** Chris@0: ** All rights reserved. Chris@0: ** Chris@0: ** Redistribution and use in source and binary forms, with or without Chris@0: ** modification, are permitted provided that the following conditions are Chris@0: ** met: Chris@0: ** Chris@0: ** * Redistributions of source code must retain the above copyright Chris@0: ** notice, this list of conditions and the following disclaimer. Chris@0: ** * Redistributions in binary form must reproduce the above copyright Chris@0: ** notice, this list of conditions and the following disclaimer in Chris@0: ** the documentation and/or other materials provided with the Chris@0: ** distribution. Chris@0: ** * Neither the author nor the names of any contributors may be used Chris@0: ** to endorse or promote products derived from this software without Chris@0: ** specific prior written permission. Chris@0: ** Chris@0: ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS Chris@0: ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED Chris@0: ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR Chris@0: ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR Chris@0: ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, Chris@0: ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, Chris@0: ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; Chris@0: ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, Chris@0: ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR Chris@0: ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF Chris@0: ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Chris@0: */ Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include Chris@0: Chris@0: #include "common.h" Chris@0: Chris@0: #define BUFFER_LEN (1 << 16) Chris@0: Chris@0: #define NOT(x) (! (x)) Chris@0: Chris@0: Chris@0: static void usage_exit (const char *progname) ; Chris@0: static void salvage_file (const char * broken_wav, const char * fixed_w64) ; Chris@0: Chris@0: int Chris@0: main (int argc, char *argv []) Chris@0: { Chris@0: if (argc != 3) Chris@0: usage_exit (program_name (argv [0])) ; Chris@0: Chris@0: salvage_file (argv [1], argv [2]) ; Chris@0: Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: Chris@0: /*============================================================================== Chris@0: */ Chris@0: Chris@0: static void lseek_or_die (int fd, off_t offset, int whence) ; Chris@0: static sf_count_t get_file_length (int fd, const char * name) ; Chris@0: static sf_count_t find_data_offset (int fd, int format) ; Chris@0: static void copy_data (int fd, SNDFILE * sndfile, int readsize) ; Chris@0: Chris@0: Chris@0: static void Chris@0: usage_exit (const char *progname) Chris@0: { printf ("Usage :\n\n %s \n\n", progname) ; Chris@0: puts ("Salvages the audio data from WAV files which are more than 4G in length.\n") ; Chris@0: printf ("Using %s.\n\n", sf_version_string ()) ; Chris@0: exit (0) ; Chris@0: } /* usage_exit */ Chris@0: Chris@0: static void Chris@0: salvage_file (const char * broken_wav, const char * fixed_w64) Chris@0: { SNDFILE * sndfile ; Chris@0: SF_INFO sfinfo ; Chris@0: sf_count_t broken_len, data_offset ; Chris@0: int fd, read_size ; Chris@0: Chris@0: if (strcmp (broken_wav, fixed_w64) == 0) Chris@0: { printf ("Error : Input and output files must be different.\n\n") ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if ((fd = open (broken_wav, O_RDONLY)) < 0) Chris@0: { printf ("Error : Not able to open file '%s' : %s\n", broken_wav, strerror (errno)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: broken_len = get_file_length (fd, broken_wav) ; Chris@0: if (broken_len <= 0xffffffff) Chris@0: printf ("File is not greater than 4Gig but salvaging anyway.\n") ; Chris@0: Chris@0: /* Grab the format info from the broken file. */ Chris@0: memset (&sfinfo, 0, sizeof (sfinfo)) ; Chris@0: if ((sndfile = sf_open (broken_wav, SFM_READ, &sfinfo)) == NULL) Chris@0: { printf ("sf_open ('%s') failed : %s\n", broken_wav, sf_strerror (NULL)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: sf_close (sndfile) ; Chris@0: Chris@0: data_offset = find_data_offset (fd, sfinfo.format & SF_FORMAT_TYPEMASK) ; Chris@0: Chris@0: printf ("Offset to audio data : %" PRId64 "\n", data_offset) ; Chris@0: Chris@0: switch (sfinfo.format & SF_FORMAT_TYPEMASK) Chris@0: { case SF_FORMAT_WAV : Chris@0: case SF_FORMAT_WAVEX : Chris@0: sfinfo.format = SF_FORMAT_W64 | (sfinfo.format & SF_FORMAT_SUBMASK) ; Chris@0: break ; Chris@0: Chris@0: default : Chris@0: printf ("Don't currently support this file type.\n") ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: switch (sfinfo.format & SF_FORMAT_SUBMASK) Chris@0: { case SF_FORMAT_PCM_U8 : Chris@0: case SF_FORMAT_PCM_S8 : Chris@0: read_size = 1 ; Chris@0: break ; Chris@0: Chris@0: case SF_FORMAT_PCM_16 : Chris@0: read_size = 2 ; Chris@0: break ; Chris@0: Chris@0: case SF_FORMAT_PCM_24 : Chris@0: read_size = 3 ; Chris@0: break ; Chris@0: Chris@0: case SF_FORMAT_PCM_32 : Chris@0: case SF_FORMAT_FLOAT : Chris@0: read_size = 4 ; Chris@0: break ; Chris@0: Chris@0: case SF_FORMAT_DOUBLE : Chris@0: read_size = 8 ; Chris@0: break ; Chris@0: Chris@0: default : Chris@0: printf ("Sorry, don't currently support this file encoding type.\n") ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: read_size *= sfinfo.channels ; Chris@0: Chris@0: if ((sndfile = sf_open (fixed_w64, SFM_WRITE, &sfinfo)) == NULL) Chris@0: { printf ("sf_open ('%s') failed : %s\n", broken_wav, sf_strerror (NULL)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: lseek_or_die (fd, data_offset, SEEK_SET) ; Chris@0: Chris@0: copy_data (fd, sndfile, read_size) ; Chris@0: Chris@0: sf_close (sndfile) ; Chris@0: Chris@0: puts ("Done!") ; Chris@0: } /* salvage_file */ Chris@0: Chris@0: /*------------------------------------------------------------------------------ Chris@0: */ Chris@0: Chris@0: static void Chris@0: lseek_or_die (int fd, off_t offset, int whence) Chris@0: { Chris@0: if (lseek (fd, offset, whence) < 0) Chris@0: { printf ("lseek failed : %s\n", strerror (errno)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: return ; Chris@0: } /* lseek_or_die */ Chris@0: Chris@0: Chris@0: static sf_count_t Chris@0: get_file_length (int fd, const char * name) Chris@0: { struct stat sbuf ; Chris@0: Chris@0: if (sizeof (sbuf.st_size) != 8) Chris@0: { puts ("Error : sizeof (sbuf.st_size) != 8. Was program compiled with\n" Chris@0: " 64 bit file offsets?\n") ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (fstat (fd, &sbuf) != 0) Chris@0: { printf ("Error : fstat ('%s') failed : %s\n", name, strerror (errno)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: return sbuf.st_size ; Chris@0: } /* get_file_length */ Chris@0: Chris@0: static sf_count_t Chris@0: find_data_offset (int fd, int format) Chris@0: { char buffer [8192], *cptr ; Chris@0: const char * target = "XXXX" ; Chris@0: sf_count_t offset = -1, extra ; Chris@0: int rlen, slen ; Chris@0: Chris@0: switch (format) Chris@0: { case SF_FORMAT_WAV : Chris@0: case SF_FORMAT_WAVEX : Chris@0: target = "data" ; Chris@0: extra = 8 ; Chris@0: break ; Chris@0: Chris@0: case SF_FORMAT_AIFF : Chris@0: target = "SSND" ; Chris@0: extra = 16 ; Chris@0: break ; Chris@0: Chris@0: default : Chris@0: puts ("Error : Sorry, don't handle this input file format.\n") ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: slen = strlen (target) ; Chris@0: Chris@0: lseek_or_die (fd, 0, SEEK_SET) ; Chris@0: Chris@0: printf ("Searching for '%s' maker.\n", target) ; Chris@0: Chris@0: if ((rlen = read (fd, buffer, sizeof (buffer))) < 0) Chris@0: { printf ("Error : failed read : %s\n", strerror (errno)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: cptr = memchr (buffer, target [0], rlen - slen) ; Chris@0: if (cptr && memcmp (cptr, target, slen) == 0) Chris@0: offset = cptr - buffer ; Chris@0: else Chris@0: { printf ("Error : Could not find data offset.\n") ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: return offset + extra ; Chris@0: } /* find_data_offset */ Chris@0: Chris@0: static void Chris@0: copy_data (int fd, SNDFILE * sndfile, int readsize) Chris@0: { static char * buffer ; Chris@0: sf_count_t readlen, count ; Chris@0: int bufferlen, done = 0 ; Chris@0: Chris@0: bufferlen = readsize * 1024 ; Chris@0: buffer = malloc (bufferlen) ; Chris@0: Chris@0: while (NOT (done) && (readlen = read (fd, buffer, bufferlen)) >= 0) Chris@0: { if (readlen < bufferlen) Chris@0: { readlen -= readlen % readsize ; Chris@0: done = 1 ; Chris@0: } ; Chris@0: Chris@0: if ((count = sf_write_raw (sndfile, buffer, readlen)) != readlen) Chris@0: { printf ("Error : sf_write_raw returned %" PRId64 " : %s\n", count, sf_strerror (sndfile)) ; Chris@0: return ; Chris@0: } ; Chris@0: } ; Chris@0: Chris@0: free (buffer) ; Chris@0: Chris@0: return ; Chris@0: } /* copy_data */ Chris@0: