Chris@0: /* Chris@0: ** Copyright (C) 2008-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: Chris@0: #include Chris@0: Chris@0: #define BLOCK_SIZE 512 Chris@0: Chris@0: static void Chris@0: print_usage (char *progname) Chris@0: { printf ("\nUsage : %s \n", progname) ; Chris@0: puts ("\n" Chris@0: " Where the output file will contain a line for each frame\n" Chris@0: " and a column for each channel.\n" Chris@0: ) ; Chris@0: Chris@0: } /* print_usage */ Chris@0: Chris@0: static void Chris@0: convert_to_text (SNDFILE * infile, FILE * outfile, int channels) Chris@0: { float buf [channels * BLOCK_SIZE] ; Chris@0: int k, m, readcount ; Chris@0: Chris@0: while ((readcount = sf_readf_float (infile, buf, BLOCK_SIZE)) > 0) Chris@0: { for (k = 0 ; k < readcount ; k++) Chris@0: { for (m = 0 ; m < channels ; m++) Chris@0: fprintf (outfile, " % 12.10f", buf [k * channels + m]) ; Chris@0: fprintf (outfile, "\n") ; Chris@0: } ; Chris@0: } ; Chris@0: Chris@0: return ; Chris@0: } /* convert_to_text */ Chris@0: Chris@0: int Chris@0: main (int argc, char * argv []) Chris@0: { char *progname, *infilename, *outfilename ; Chris@0: SNDFILE *infile = NULL ; Chris@0: FILE *outfile = NULL ; Chris@0: SF_INFO sfinfo ; Chris@0: Chris@0: progname = strrchr (argv [0], '/') ; Chris@0: progname = progname ? progname + 1 : argv [0] ; Chris@0: Chris@0: if (argc != 3) Chris@0: { print_usage (progname) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: infilename = argv [1] ; Chris@0: outfilename = argv [2] ; Chris@0: Chris@0: if (strcmp (infilename, outfilename) == 0) Chris@0: { printf ("Error : Input and output filenames are the same.\n\n") ; Chris@0: print_usage (progname) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: if (infilename [0] == '-') Chris@0: { printf ("Error : Input filename (%s) looks like an option.\n\n", infilename) ; Chris@0: print_usage (progname) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: if (outfilename [0] == '-') Chris@0: { printf ("Error : Output filename (%s) looks like an option.\n\n", outfilename) ; Chris@0: print_usage (progname) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: if ((infile = sf_open (infilename, SFM_READ, &sfinfo)) == NULL) Chris@0: { printf ("Not able to open input file %s.\n", infilename) ; Chris@0: puts (sf_strerror (NULL)) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: /* Open the output file. */ Chris@0: if ((outfile = fopen (outfilename, "w")) == NULL) Chris@0: { printf ("Not able to open output file %s : %s\n", outfilename, sf_strerror (NULL)) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: fprintf (outfile, "# Converted from file %s.\n", infilename) ; Chris@0: fprintf (outfile, "# Channels %d, Sample rate %d\n", sfinfo.channels, sfinfo.samplerate) ; Chris@0: Chris@0: convert_to_text (infile, outfile, sfinfo.channels) ; Chris@0: Chris@0: sf_close (infile) ; Chris@0: fclose (outfile) ; Chris@0: Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: