cannam@125: /* cannam@125: ** Copyright (C) 2008-2012 Erik de Castro Lopo cannam@125: ** cannam@125: ** All rights reserved. cannam@125: ** cannam@125: ** Redistribution and use in source and binary forms, with or without cannam@125: ** modification, are permitted provided that the following conditions are cannam@125: ** met: cannam@125: ** cannam@125: ** * Redistributions of source code must retain the above copyright cannam@125: ** notice, this list of conditions and the following disclaimer. cannam@125: ** * Redistributions in binary form must reproduce the above copyright cannam@125: ** notice, this list of conditions and the following disclaimer in cannam@125: ** the documentation and/or other materials provided with the cannam@125: ** distribution. cannam@125: ** * Neither the author nor the names of any contributors may be used cannam@125: ** to endorse or promote products derived from this software without cannam@125: ** specific prior written permission. cannam@125: ** cannam@125: ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS cannam@125: ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED cannam@125: ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR cannam@125: ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR cannam@125: ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, cannam@125: ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, cannam@125: ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; cannam@125: ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, cannam@125: ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR cannam@125: ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF cannam@125: ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. cannam@125: */ cannam@125: cannam@125: #include cannam@125: #include cannam@125: #include cannam@125: #include cannam@125: cannam@125: #include cannam@125: cannam@125: #define BLOCK_SIZE 512 cannam@125: cannam@125: static void cannam@125: print_usage (char *progname) cannam@125: { printf ("\nUsage : %s \n", progname) ; cannam@125: puts ("\n" cannam@125: " Where the output file will contain a line for each frame\n" cannam@125: " and a column for each channel.\n" cannam@125: ) ; cannam@125: cannam@125: } /* print_usage */ cannam@125: cannam@125: static void cannam@125: convert_to_text (SNDFILE * infile, FILE * outfile, int channels) cannam@125: { float buf [channels * BLOCK_SIZE] ; cannam@125: int k, m, readcount ; cannam@125: cannam@125: while ((readcount = sf_readf_float (infile, buf, BLOCK_SIZE)) > 0) cannam@125: { for (k = 0 ; k < readcount ; k++) cannam@125: { for (m = 0 ; m < channels ; m++) cannam@125: fprintf (outfile, " % 12.10f", buf [k * channels + m]) ; cannam@125: fprintf (outfile, "\n") ; cannam@125: } ; cannam@125: } ; cannam@125: cannam@125: return ; cannam@125: } /* convert_to_text */ cannam@125: cannam@125: int cannam@125: main (int argc, char * argv []) cannam@125: { char *progname, *infilename, *outfilename ; cannam@125: SNDFILE *infile = NULL ; cannam@125: FILE *outfile = NULL ; cannam@125: SF_INFO sfinfo ; cannam@125: cannam@125: progname = strrchr (argv [0], '/') ; cannam@125: progname = progname ? progname + 1 : argv [0] ; cannam@125: cannam@125: if (argc != 3) cannam@125: { print_usage (progname) ; cannam@125: return 1 ; cannam@125: } ; cannam@125: cannam@125: infilename = argv [1] ; cannam@125: outfilename = argv [2] ; cannam@125: cannam@125: if (strcmp (infilename, outfilename) == 0) cannam@125: { printf ("Error : Input and output filenames are the same.\n\n") ; cannam@125: print_usage (progname) ; cannam@125: return 1 ; cannam@125: } ; cannam@125: cannam@125: if (infilename [0] == '-') cannam@125: { printf ("Error : Input filename (%s) looks like an option.\n\n", infilename) ; cannam@125: print_usage (progname) ; cannam@125: return 1 ; cannam@125: } ; cannam@125: cannam@125: if (outfilename [0] == '-') cannam@125: { printf ("Error : Output filename (%s) looks like an option.\n\n", outfilename) ; cannam@125: print_usage (progname) ; cannam@125: return 1 ; cannam@125: } ; cannam@125: cannam@125: memset (&sfinfo, 0, sizeof (sfinfo)) ; cannam@125: cannam@125: if ((infile = sf_open (infilename, SFM_READ, &sfinfo)) == NULL) cannam@125: { printf ("Not able to open input file %s.\n", infilename) ; cannam@125: puts (sf_strerror (NULL)) ; cannam@125: return 1 ; cannam@125: } ; cannam@125: cannam@125: /* Open the output file. */ cannam@125: if ((outfile = fopen (outfilename, "w")) == NULL) cannam@125: { printf ("Not able to open output file %s : %s\n", outfilename, sf_strerror (NULL)) ; cannam@125: return 1 ; cannam@125: } ; cannam@125: cannam@125: fprintf (outfile, "# Converted from file %s.\n", infilename) ; cannam@125: fprintf (outfile, "# Channels %d, Sample rate %d\n", sfinfo.channels, sfinfo.samplerate) ; cannam@125: cannam@125: convert_to_text (infile, outfile, sfinfo.channels) ; cannam@125: cannam@125: sf_close (infile) ; cannam@125: fclose (outfile) ; cannam@125: cannam@125: return 0 ; cannam@125: } /* main */ cannam@125: