annotate src/libsndfile-1.0.27/programs/sndfile-interleave.c @ 84:08ae793730bd

Add null config files
author Chris Cannam
date Mon, 02 Mar 2020 14:03:47 +0000
parents 1df64224f5ac
children
rev   line source
Chris@40 1 /*
Chris@40 2 ** Copyright (C) 2009-2015 Erik de Castro Lopo <erikd@mega-nerd.com>
Chris@40 3 **
Chris@40 4 ** All rights reserved.
Chris@40 5 **
Chris@40 6 ** Redistribution and use in source and binary forms, with or without
Chris@40 7 ** modification, are permitted provided that the following conditions are
Chris@40 8 ** met:
Chris@40 9 **
Chris@40 10 ** * Redistributions of source code must retain the above copyright
Chris@40 11 ** notice, this list of conditions and the following disclaimer.
Chris@40 12 ** * Redistributions in binary form must reproduce the above copyright
Chris@40 13 ** notice, this list of conditions and the following disclaimer in
Chris@40 14 ** the documentation and/or other materials provided with the
Chris@40 15 ** distribution.
Chris@40 16 ** * Neither the author nor the names of any contributors may be used
Chris@40 17 ** to endorse or promote products derived from this software without
Chris@40 18 ** specific prior written permission.
Chris@40 19 **
Chris@40 20 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Chris@40 21 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
Chris@40 22 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
Chris@40 23 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
Chris@40 24 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Chris@40 25 ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Chris@40 26 ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
Chris@40 27 ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Chris@40 28 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
Chris@40 29 ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Chris@40 30 ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@40 31 */
Chris@40 32
Chris@40 33 #include <stdio.h>
Chris@40 34 #include <stdlib.h>
Chris@40 35 #include <string.h>
Chris@40 36 #include <sndfile.h>
Chris@40 37
Chris@40 38 #include "common.h"
Chris@40 39
Chris@40 40 #define BUFFER_LEN 4096
Chris@40 41 #define MAX_INPUTS 16
Chris@40 42
Chris@40 43
Chris@40 44 typedef struct
Chris@40 45 { SNDFILE * infile [MAX_INPUTS] ;
Chris@40 46 SNDFILE * outfile ;
Chris@40 47
Chris@40 48 union
Chris@40 49 { double d [BUFFER_LEN] ;
Chris@40 50 int i [BUFFER_LEN] ;
Chris@40 51 } din ;
Chris@40 52
Chris@40 53 union
Chris@40 54
Chris@40 55 { double d [MAX_INPUTS * BUFFER_LEN] ;
Chris@40 56 int i [MAX_INPUTS * BUFFER_LEN] ;
Chris@40 57 } dout ;
Chris@40 58
Chris@40 59 int channels ;
Chris@40 60 } STATE ;
Chris@40 61
Chris@40 62
Chris@40 63 static void usage_exit (void) ;
Chris@40 64 static void interleave_int (STATE * state) ;
Chris@40 65 static void interleave_double (STATE * state) ;
Chris@40 66
Chris@40 67
Chris@40 68 int
Chris@40 69 main (int argc, char **argv)
Chris@40 70 { STATE state ;
Chris@40 71 SF_INFO sfinfo ;
Chris@40 72 int k, double_merge = 0 ;
Chris@40 73
Chris@40 74 if (argc < 5)
Chris@40 75 { if (argc > 1)
Chris@40 76 puts ("\nError : need at least 2 input files.") ;
Chris@40 77 usage_exit () ;
Chris@40 78 } ;
Chris@40 79
Chris@40 80 if (strcmp (argv [argc - 2], "-o") != 0)
Chris@40 81 { puts ("\nError : second last command line parameter should be '-o'.\n") ;
Chris@40 82 usage_exit () ;
Chris@40 83 } ;
Chris@40 84
Chris@40 85 if (argc - 3 > MAX_INPUTS)
Chris@40 86 { printf ("\nError : Cannot handle more than %d input channels.\n\n", MAX_INPUTS) ;
Chris@40 87 exit (1) ;
Chris@40 88 } ;
Chris@40 89
Chris@40 90 memset (&state, 0, sizeof (state)) ;
Chris@40 91 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 92
Chris@40 93 for (k = 1 ; k < argc - 2 ; k++)
Chris@40 94 {
Chris@40 95 if ((state.infile [k - 1] = sf_open (argv [k], SFM_READ, &sfinfo)) == NULL)
Chris@40 96 { printf ("\nError : Not able to open input file '%s'\n%s\n", argv [k], sf_strerror (NULL)) ;
Chris@40 97 exit (1) ;
Chris@40 98 } ;
Chris@40 99
Chris@40 100 if (sfinfo.channels != 1)
Chris@40 101 { printf ("\bError : Input file '%s' should be mono (has %d channels).\n", argv [k], sfinfo.channels) ;
Chris@40 102 exit (1) ;
Chris@40 103 } ;
Chris@40 104
Chris@40 105 switch (sfinfo.format & SF_FORMAT_SUBMASK)
Chris@40 106 { case SF_FORMAT_FLOAT :
Chris@40 107 case SF_FORMAT_DOUBLE :
Chris@40 108 case SF_FORMAT_VORBIS :
Chris@40 109 double_merge = 1 ;
Chris@40 110 break ;
Chris@40 111
Chris@40 112 default :
Chris@40 113 break ;
Chris@40 114 } ;
Chris@40 115
Chris@40 116 state.channels ++ ;
Chris@40 117 } ;
Chris@40 118
Chris@40 119 sfinfo.channels = state.channels ;
Chris@40 120 sfinfo.format = sfe_file_type_of_ext (argv [argc - 1], sfinfo.format) ;
Chris@40 121
Chris@40 122 if ((state.outfile = sf_open (argv [argc - 1], SFM_WRITE, &sfinfo)) == NULL)
Chris@40 123 { printf ("Not able to open output file '%s'\n%s\n", argv [argc - 1], sf_strerror (NULL)) ;
Chris@40 124 exit (1) ;
Chris@40 125 } ;
Chris@40 126
Chris@40 127 if (double_merge)
Chris@40 128 interleave_double (&state) ;
Chris@40 129 else
Chris@40 130 interleave_int (&state) ;
Chris@40 131
Chris@40 132 for (k = 0 ; k < MAX_INPUTS ; k++)
Chris@40 133 if (state.infile [k] != NULL)
Chris@40 134 sf_close (state.infile [k]) ;
Chris@40 135 sf_close (state.outfile) ;
Chris@40 136
Chris@40 137 return 0 ;
Chris@40 138 } /* main */
Chris@40 139
Chris@40 140 /*------------------------------------------------------------------------------
Chris@40 141 */
Chris@40 142
Chris@40 143
Chris@40 144 static void
Chris@40 145 usage_exit (void)
Chris@40 146 { puts ("\nUsage : sndfile-interleave <input 1> <input 2> ... -o <output file>\n") ;
Chris@40 147 puts ("Merge two or more mono files into a single multi-channel file.\n") ;
Chris@40 148 printf ("Using %s.\n\n", sf_version_string ()) ;
Chris@40 149 exit (1) ;
Chris@40 150 } /* usage_exit */
Chris@40 151
Chris@40 152
Chris@40 153 static void
Chris@40 154 interleave_int (STATE * state)
Chris@40 155 { int max_read_len, read_len ;
Chris@40 156 int ch, k ;
Chris@40 157
Chris@40 158 do
Chris@40 159 { max_read_len = 0 ;
Chris@40 160
Chris@40 161 for (ch = 0 ; ch < state->channels ; ch ++)
Chris@40 162 { read_len = sf_read_int (state->infile [ch], state->din.i, BUFFER_LEN) ;
Chris@40 163 if (read_len < BUFFER_LEN)
Chris@40 164 memset (state->din.i + read_len, 0, sizeof (state->din.i [0]) * (BUFFER_LEN - read_len)) ;
Chris@40 165
Chris@40 166 for (k = 0 ; k < BUFFER_LEN ; k++)
Chris@40 167 state->dout.i [k * state->channels + ch] = state->din.i [k] ;
Chris@40 168
Chris@40 169 max_read_len = MAX (max_read_len, read_len) ;
Chris@40 170 } ;
Chris@40 171
Chris@40 172 sf_writef_int (state->outfile, state->dout.i, max_read_len) ;
Chris@40 173 }
Chris@40 174 while (max_read_len > 0) ;
Chris@40 175
Chris@40 176 } /* interleave_int */
Chris@40 177
Chris@40 178
Chris@40 179 static void
Chris@40 180 interleave_double (STATE * state)
Chris@40 181 { int max_read_len, read_len ;
Chris@40 182 int ch, k ;
Chris@40 183
Chris@40 184 do
Chris@40 185 { max_read_len = 0 ;
Chris@40 186
Chris@40 187 for (ch = 0 ; ch < state->channels ; ch ++)
Chris@40 188 { read_len = sf_read_double (state->infile [ch], state->din.d, BUFFER_LEN) ;
Chris@40 189 if (read_len < BUFFER_LEN)
Chris@40 190 memset (state->din.d + read_len, 0, sizeof (state->din.d [0]) * (BUFFER_LEN - read_len)) ;
Chris@40 191
Chris@40 192 for (k = 0 ; k < BUFFER_LEN ; k++)
Chris@40 193 state->dout.d [k * state->channels + ch] = state->din.d [k] ;
Chris@40 194
Chris@40 195 max_read_len = MAX (max_read_len, read_len) ;
Chris@40 196 } ;
Chris@40 197
Chris@40 198 sf_writef_double (state->outfile, state->dout.d, max_read_len) ;
Chris@40 199 }
Chris@40 200 while (max_read_len > 0) ;
Chris@40 201
Chris@40 202 } /* interleave_double */