annotate src/libsndfile-1.0.25/examples/sndfile-to-text.c @ 85:545efbb81310

Import initial set of sources
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 18 Mar 2013 14:12:14 +0000
parents
children
rev   line source
cannam@85 1 /*
cannam@85 2 ** Copyright (C) 2008-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
cannam@85 3 **
cannam@85 4 ** All rights reserved.
cannam@85 5 **
cannam@85 6 ** Redistribution and use in source and binary forms, with or without
cannam@85 7 ** modification, are permitted provided that the following conditions are
cannam@85 8 ** met:
cannam@85 9 **
cannam@85 10 ** * Redistributions of source code must retain the above copyright
cannam@85 11 ** notice, this list of conditions and the following disclaimer.
cannam@85 12 ** * Redistributions in binary form must reproduce the above copyright
cannam@85 13 ** notice, this list of conditions and the following disclaimer in
cannam@85 14 ** the documentation and/or other materials provided with the
cannam@85 15 ** distribution.
cannam@85 16 ** * Neither the author nor the names of any contributors may be used
cannam@85 17 ** to endorse or promote products derived from this software without
cannam@85 18 ** specific prior written permission.
cannam@85 19 **
cannam@85 20 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
cannam@85 21 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
cannam@85 22 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
cannam@85 23 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
cannam@85 24 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
cannam@85 25 ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
cannam@85 26 ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
cannam@85 27 ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
cannam@85 28 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
cannam@85 29 ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
cannam@85 30 ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cannam@85 31 */
cannam@85 32
cannam@85 33 #include <stdio.h>
cannam@85 34 #include <stdlib.h>
cannam@85 35 #include <string.h>
cannam@85 36 #include <ctype.h>
cannam@85 37
cannam@85 38 #include <sndfile.h>
cannam@85 39
cannam@85 40 #define BLOCK_SIZE 512
cannam@85 41
cannam@85 42 static void
cannam@85 43 print_usage (char *progname)
cannam@85 44 { printf ("\nUsage : %s <input file> <output file>\n", progname) ;
cannam@85 45 puts ("\n"
cannam@85 46 " Where the output file will contain a line for each frame\n"
cannam@85 47 " and a column for each channel.\n"
cannam@85 48 ) ;
cannam@85 49
cannam@85 50 } /* print_usage */
cannam@85 51
cannam@85 52 static void
cannam@85 53 convert_to_text (SNDFILE * infile, FILE * outfile, int channels)
cannam@85 54 { float buf [channels * BLOCK_SIZE] ;
cannam@85 55 int k, m, readcount ;
cannam@85 56
cannam@85 57 while ((readcount = sf_readf_float (infile, buf, BLOCK_SIZE)) > 0)
cannam@85 58 { for (k = 0 ; k < readcount ; k++)
cannam@85 59 { for (m = 0 ; m < channels ; m++)
cannam@85 60 fprintf (outfile, " % 12.10f", buf [k * channels + m]) ;
cannam@85 61 fprintf (outfile, "\n") ;
cannam@85 62 } ;
cannam@85 63 } ;
cannam@85 64
cannam@85 65 return ;
cannam@85 66 } /* convert_to_text */
cannam@85 67
cannam@85 68 int
cannam@85 69 main (int argc, char * argv [])
cannam@85 70 { char *progname, *infilename, *outfilename ;
cannam@85 71 SNDFILE *infile = NULL ;
cannam@85 72 FILE *outfile = NULL ;
cannam@85 73 SF_INFO sfinfo ;
cannam@85 74
cannam@85 75 progname = strrchr (argv [0], '/') ;
cannam@85 76 progname = progname ? progname + 1 : argv [0] ;
cannam@85 77
cannam@85 78 if (argc != 3)
cannam@85 79 { print_usage (progname) ;
cannam@85 80 return 1 ;
cannam@85 81 } ;
cannam@85 82
cannam@85 83 infilename = argv [1] ;
cannam@85 84 outfilename = argv [2] ;
cannam@85 85
cannam@85 86 if (strcmp (infilename, outfilename) == 0)
cannam@85 87 { printf ("Error : Input and output filenames are the same.\n\n") ;
cannam@85 88 print_usage (progname) ;
cannam@85 89 return 1 ;
cannam@85 90 } ;
cannam@85 91
cannam@85 92 if (infilename [0] == '-')
cannam@85 93 { printf ("Error : Input filename (%s) looks like an option.\n\n", infilename) ;
cannam@85 94 print_usage (progname) ;
cannam@85 95 return 1 ;
cannam@85 96 } ;
cannam@85 97
cannam@85 98 if (outfilename [0] == '-')
cannam@85 99 { printf ("Error : Output filename (%s) looks like an option.\n\n", outfilename) ;
cannam@85 100 print_usage (progname) ;
cannam@85 101 return 1 ;
cannam@85 102 } ;
cannam@85 103
cannam@85 104 if ((infile = sf_open (infilename, SFM_READ, &sfinfo)) == NULL)
cannam@85 105 { printf ("Not able to open input file %s.\n", infilename) ;
cannam@85 106 puts (sf_strerror (NULL)) ;
cannam@85 107 return 1 ;
cannam@85 108 } ;
cannam@85 109
cannam@85 110 /* Open the output file. */
cannam@85 111 if ((outfile = fopen (outfilename, "w")) == NULL)
cannam@85 112 { printf ("Not able to open output file %s : %s\n", outfilename, sf_strerror (NULL)) ;
cannam@85 113 return 1 ;
cannam@85 114 } ;
cannam@85 115
cannam@85 116 fprintf (outfile, "# Converted from file %s.\n", infilename) ;
cannam@85 117 fprintf (outfile, "# Channels %d, Sample rate %d\n", sfinfo.channels, sfinfo.samplerate) ;
cannam@85 118
cannam@85 119 convert_to_text (infile, outfile, sfinfo.channels) ;
cannam@85 120
cannam@85 121 sf_close (infile) ;
cannam@85 122 fclose (outfile) ;
cannam@85 123
cannam@85 124 return 0 ;
cannam@85 125 } /* main */
cannam@85 126