annotate src/libsndfile-1.0.25/programs/sndfile-concat.c @ 0:c7265573341e

Import initial set of sources
author Chris Cannam
date Mon, 18 Mar 2013 14:12:14 +0000
parents
children
rev   line source
Chris@0 1 /*
Chris@0 2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
Chris@0 3 **
Chris@0 4 ** All rights reserved.
Chris@0 5 **
Chris@0 6 ** Redistribution and use in source and binary forms, with or without
Chris@0 7 ** modification, are permitted provided that the following conditions are
Chris@0 8 ** met:
Chris@0 9 **
Chris@0 10 ** * Redistributions of source code must retain the above copyright
Chris@0 11 ** notice, this list of conditions and the following disclaimer.
Chris@0 12 ** * Redistributions in binary form must reproduce the above copyright
Chris@0 13 ** notice, this list of conditions and the following disclaimer in
Chris@0 14 ** the documentation and/or other materials provided with the
Chris@0 15 ** distribution.
Chris@0 16 ** * Neither the author nor the names of any contributors may be used
Chris@0 17 ** to endorse or promote products derived from this software without
Chris@0 18 ** specific prior written permission.
Chris@0 19 **
Chris@0 20 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Chris@0 21 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
Chris@0 22 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
Chris@0 23 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
Chris@0 24 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Chris@0 25 ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Chris@0 26 ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
Chris@0 27 ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Chris@0 28 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
Chris@0 29 ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Chris@0 30 ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@0 31 */
Chris@0 32
Chris@0 33 #include <stdio.h>
Chris@0 34 #include <stdlib.h>
Chris@0 35 #include <string.h>
Chris@0 36 #include <ctype.h>
Chris@0 37
Chris@0 38 #include <sndfile.h>
Chris@0 39
Chris@0 40 #include "common.h"
Chris@0 41
Chris@0 42 #define BUFFER_LEN (1<<16)
Chris@0 43
Chris@0 44
Chris@0 45 static void concat_data_fp (SNDFILE *wfile, SNDFILE *rofile, int channels) ;
Chris@0 46 static void concat_data_int (SNDFILE *wfile, SNDFILE *rofile, int channels) ;
Chris@0 47
Chris@0 48 static void
Chris@0 49 usage_exit (const char *progname)
Chris@0 50 {
Chris@0 51 printf ("\nUsage : %s <infile1> <infile2> ... <outfile>\n\n", progname) ;
Chris@0 52 puts (
Chris@0 53 " Create a new output file <outfile> containing the concatenated\n"
Chris@0 54 " audio data from froms <infile1> <infile2> ....\n"
Chris@0 55 "\n"
Chris@0 56 " The joined file will be encoded in the same format as the data\n"
Chris@0 57 " in infile1, with all the data in subsequent files automatically\n"
Chris@0 58 " converted to the correct encoding.\n"
Chris@0 59 "\n"
Chris@0 60 " The only restriction is that the two files must have the same\n"
Chris@0 61 " number of channels.\n"
Chris@0 62 ) ;
Chris@0 63
Chris@0 64 exit (0) ;
Chris@0 65 } /* usage_exit */
Chris@0 66
Chris@0 67 int
Chris@0 68 main (int argc, char *argv [])
Chris@0 69 { const char *progname, *outfilename ;
Chris@0 70 SNDFILE *outfile, **infiles ;
Chris@0 71 SF_INFO sfinfo_out, sfinfo_in ;
Chris@0 72 void (*func) (SNDFILE*, SNDFILE*, int) ;
Chris@0 73 int k ;
Chris@0 74
Chris@0 75 progname = program_name (argv [0]) ;
Chris@0 76
Chris@0 77 if (argc < 4)
Chris@0 78 usage_exit (progname) ;
Chris@0 79
Chris@0 80 argv ++ ;
Chris@0 81 argc -- ;
Chris@0 82
Chris@0 83 argc -- ;
Chris@0 84 outfilename = argv [argc] ;
Chris@0 85
Chris@0 86 if ((infiles = calloc (argc, sizeof (SNDFILE*))) == NULL)
Chris@0 87 { printf ("\nError : Malloc failed.\n\n") ;
Chris@0 88 exit (1) ;
Chris@0 89 } ;
Chris@0 90
Chris@0 91 memset (&sfinfo_in, 0, sizeof (sfinfo_in)) ;
Chris@0 92
Chris@0 93 if ((infiles [0] = sf_open (argv [0], SFM_READ, &sfinfo_in)) == NULL)
Chris@0 94 { printf ("\nError : failed to open file '%s'.\n\n", argv [0]) ;
Chris@0 95 exit (1) ;
Chris@0 96 } ;
Chris@0 97
Chris@0 98 sfinfo_out = sfinfo_in ;
Chris@0 99
Chris@0 100 for (k = 1 ; k < argc ; k++)
Chris@0 101 { if ((infiles [k] = sf_open (argv [k], SFM_READ, &sfinfo_in)) == NULL)
Chris@0 102 { printf ("\nError : failed to open file '%s'.\n\n", argv [k]) ;
Chris@0 103 exit (1) ;
Chris@0 104 } ;
Chris@0 105
Chris@0 106 if (sfinfo_in.channels != sfinfo_out.channels)
Chris@0 107 { printf ("\nError : File '%s' has %d channels (should have %d).\n\n", argv [k], sfinfo_in.channels, sfinfo_out.channels) ;
Chris@0 108 exit (1) ;
Chris@0 109 } ;
Chris@0 110 } ;
Chris@0 111
Chris@0 112 if ((outfile = sf_open (outfilename, SFM_WRITE, &sfinfo_out)) == NULL)
Chris@0 113 { printf ("\nError : Not able to open input file %s.\n", outfilename) ;
Chris@0 114 puts (sf_strerror (NULL)) ;
Chris@0 115 exit (1) ;
Chris@0 116 } ;
Chris@0 117
Chris@0 118 if ((sfinfo_out.format & SF_FORMAT_SUBMASK) == SF_FORMAT_DOUBLE ||
Chris@0 119 (sfinfo_out.format & SF_FORMAT_SUBMASK) == SF_FORMAT_FLOAT)
Chris@0 120 func = concat_data_fp ;
Chris@0 121 else
Chris@0 122 func = concat_data_int ;
Chris@0 123
Chris@0 124 for (k = 0 ; k < argc ; k++)
Chris@0 125 { func (outfile, infiles [k], sfinfo_out.channels) ;
Chris@0 126 sf_close (infiles [k]) ;
Chris@0 127 } ;
Chris@0 128
Chris@0 129 sf_close (outfile) ;
Chris@0 130
Chris@0 131 return 0 ;
Chris@0 132 } /* main */
Chris@0 133
Chris@0 134 static void
Chris@0 135 concat_data_fp (SNDFILE *wfile, SNDFILE *rofile, int channels)
Chris@0 136 { static double data [BUFFER_LEN] ;
Chris@0 137 int frames, readcount ;
Chris@0 138
Chris@0 139 frames = BUFFER_LEN / channels ;
Chris@0 140 readcount = frames ;
Chris@0 141
Chris@0 142 sf_seek (wfile, 0, SEEK_END) ;
Chris@0 143
Chris@0 144 while (readcount > 0)
Chris@0 145 { readcount = sf_readf_double (rofile, data, frames) ;
Chris@0 146 sf_writef_double (wfile, data, readcount) ;
Chris@0 147 } ;
Chris@0 148
Chris@0 149 return ;
Chris@0 150 } /* concat_data_fp */
Chris@0 151
Chris@0 152 static void
Chris@0 153 concat_data_int (SNDFILE *wfile, SNDFILE *rofile, int channels)
Chris@0 154 { static int data [BUFFER_LEN] ;
Chris@0 155 int frames, readcount ;
Chris@0 156
Chris@0 157 frames = BUFFER_LEN / channels ;
Chris@0 158 readcount = frames ;
Chris@0 159
Chris@0 160 sf_seek (wfile, 0, SEEK_END) ;
Chris@0 161
Chris@0 162 while (readcount > 0)
Chris@0 163 { readcount = sf_readf_int (rofile, data, frames) ;
Chris@0 164 sf_writef_int (wfile, data, readcount) ;
Chris@0 165 } ;
Chris@0 166
Chris@0 167 return ;
Chris@0 168 } /* concat_data_int */
Chris@0 169