annotate src/libsndfile-1.0.27/examples/sndfile-to-text.c @ 127:7867fa7e1b6b

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