Add new programs sndfile-interleave and sndfile-deinterleave.

This commit is contained in:
Erik de Castro Lopo 2009-12-02 22:15:42 +11:00
parent 218024ef7d
commit ab9966e4ef
5 changed files with 403 additions and 1 deletions

View File

@ -57,7 +57,9 @@ mntest.sh
programs/sndfile-cmp
programs/sndfile-convert
programs/sndfile-data-trim
programs/sndfile-deinterleave
programs/sndfile-info
programs/sndfile-interleave
programs/sndfile-jackplay
programs/sndfile-metadata-get
programs/sndfile-metadata-set

View File

@ -6,6 +6,12 @@
* programs/sndfile-convert.c programs/common.[ch]
Move some code from convert to common for reuse.
* programs/sndfile-interleave.c programs/sndfile-interleave.c
Add new programs sndfile-interleave and sndfile-deinterleave.
* programs/Makefile.am
Hook new programs into build.
2009-12-01 Erik de Castro Lopo <erikd AT mega-nerd DOT com>
* src/create_symbols_file.py tests/stdio_test.c tests/win32_test.c

View File

@ -1,7 +1,8 @@
## Process this file with automake to produce Makefile.in
bin_PROGRAMS = sndfile-info sndfile-play sndfile-convert sndfile-jackplay sndfile-cmp \
sndfile-metadata-set sndfile-metadata-get
sndfile-metadata-set sndfile-metadata-get sndfile-interleave \
sndfile-deinterleave
OS_SPECIFIC_CFLAGS = @OS_SPECIFIC_CFLAGS@
OS_SPECIFIC_LINKS = @OS_SPECIFIC_LINKS@
@ -34,3 +35,9 @@ sndfile_metadata_set_LDADD = $(top_builddir)/src/libsndfile.la
sndfile_metadata_get_SOURCES = sndfile-metadata-get.c
sndfile_metadata_get_LDADD = $(top_builddir)/src/libsndfile.la
sndfile_interleave_SOURCES = sndfile-interleave.c common.c common.h
sndfile_interleave_LDADD = $(top_builddir)/src/libsndfile.la
sndfile_deinterleave_SOURCES = sndfile-deinterleave.c
sndfile_deinterleave_LDADD = $(top_builddir)/src/libsndfile.la

View File

@ -0,0 +1,187 @@
/*
** Copyright (C) 2009 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the author nor the names of any contributors may be used
** to endorse or promote products derived from this software without
** specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sndfile.h>
#include "common.h"
#define BUFFER_LEN 4096
#define MAX_CHANNELS 16
typedef struct
{ SNDFILE * infile ;
SNDFILE * outfile [MAX_CHANNELS] ;
union
{ double d [MAX_CHANNELS * BUFFER_LEN] ;
int i [MAX_CHANNELS * BUFFER_LEN] ;
} din ;
union
{ double d [BUFFER_LEN] ;
int i [BUFFER_LEN] ;
} dout ;
int channels ;
} STATE ;
static void usage_exit (void) ;
static void deinterleave_int (STATE * state) ;
static void deinterleave_double (STATE * state) ;
int
main (int argc, char **argv)
{ STATE state ;
SF_INFO sfinfo ;
char pathname [512], ext [32], *cptr ;
int ch, double_split ;
if (argc != 2)
{ puts ("\nError : need a single input file.\n") ;
usage_exit () ;
} ;
memset (&state, 0, sizeof (state)) ;
memset (&sfinfo, 0, sizeof (sfinfo)) ;
if ((state.infile = sf_open (argv [1], SFM_READ, &sfinfo)) == NULL)
{ printf ("\nError : Not able to open input file '%s'\n%s\n", argv [1], sf_strerror (NULL)) ;
exit (1) ;
} ;
if (sfinfo.channels < 2)
{ printf ("\nError : Input file '%s' only has one channel.\n", argv [1]) ;
exit (1) ;
} ;
state.channels = sfinfo.channels ;
sfinfo.channels = 1 ;
snprintf (pathname, sizeof (pathname), "%s", argv [1]) ;
if ((cptr = strrchr (pathname, '.')) == NULL)
ext [0] = 0 ;
else
{ snprintf (ext, sizeof (ext), "%s", cptr) ;
cptr [0] = 0 ;
} ;
printf ("Input file : %s\n", pathname) ;
puts ("Output files :") ;
for (ch = 0 ; ch < state.channels ; ch++)
{ char filename [520] ;
snprintf (filename, sizeof (filename), "%s_%02d%s", pathname, ch, ext) ;
if ((state.outfile [ch] = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
{ printf ("Not able to open output file '%s'\n%s\n", filename, sf_strerror (NULL)) ;
exit (1) ;
} ;
printf (" %s\n", filename) ;
} ;
switch (sfinfo.format & SF_FORMAT_SUBMASK)
{ case SF_FORMAT_FLOAT :
case SF_FORMAT_DOUBLE :
case SF_FORMAT_VORBIS :
double_split = 1 ;
break ;
default :
double_split = 0 ;
break ;
} ;
if (double_split)
deinterleave_double (&state) ;
else
deinterleave_int (&state) ;
sf_close (state.infile) ;
for (ch = 0 ; ch < MAX_CHANNELS ; ch++)
if (state.outfile [ch] != NULL)
sf_close (state.outfile [ch]) ;
return 0 ;
} /* main */
/*------------------------------------------------------------------------------
*/
static void
usage_exit (void)
{ puts ("\nUsage : sndfile-deinterleave <filename>\n") ;
puts ("Split a mutli channel file into a set of mon files.\n") ;
exit (0) ;
} /* usage_exit */
static void
deinterleave_int (STATE * state)
{ int read_len ;
int ch, k ;
do
{ read_len = sf_readf_int (state->infile, state->din.i, BUFFER_LEN) ;
for (ch = 0 ; ch < state->channels ; ch ++)
{ for (k = 0 ; k < read_len ; k++)
state->dout.i [k] = state->din.i [k * state->channels + ch] ;
sf_write_int (state->outfile [ch], state->dout.i, read_len) ;
} ;
}
while (read_len > 0) ;
} /* deinterleave_int */
static void
deinterleave_double (STATE * state)
{ int read_len ;
int ch, k ;
do
{ read_len = sf_readf_double (state->infile, state->din.d, BUFFER_LEN) ;
for (ch = 0 ; ch < state->channels ; ch ++)
{ for (k = 0 ; k < read_len ; k++)
state->dout.d [k] = state->din.d [k * state->channels + ch] ;
sf_write_double (state->outfile [ch], state->dout.d, read_len) ;
} ;
}
while (read_len > 0) ;
} /* deinterleave_double */

View File

@ -0,0 +1,200 @@
/*
** Copyright (C) 2009 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the author nor the names of any contributors may be used
** to endorse or promote products derived from this software without
** specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sndfile.h>
#include "common.h"
#define BUFFER_LEN 4096
#define MAX_INPUTS 16
typedef struct
{ SNDFILE * infile [MAX_INPUTS] ;
SNDFILE * outfile ;
union
{ double d [BUFFER_LEN] ;
int i [BUFFER_LEN] ;
} din ;
union
{ double d [MAX_INPUTS * BUFFER_LEN] ;
int i [MAX_INPUTS * BUFFER_LEN] ;
} dout ;
int channels ;
} STATE ;
static void usage_exit (void) ;
static void interleave_int (STATE * state) ;
static void interleave_double (STATE * state) ;
int
main (int argc, char **argv)
{ STATE state ;
SF_INFO sfinfo ;
int k, double_merge = 0 ;
if (strcmp (argv [argc - 2], "-o") != 0)
{ puts ("\nError : second last command line parameter should be '-o'.\n") ;
usage_exit () ;
} ;
if (argc < 5)
{ puts ("\nError : need at least 2 input files.\n") ;
usage_exit () ;
} ;
if (argc - 2 > MAX_INPUTS)
{ printf ("\nError : Cannot handle more than %d input channels.\n\n", MAX_INPUTS) ;
exit (1) ;
} ;
memset (&state, 0, sizeof (state)) ;
memset (&sfinfo, 0, sizeof (sfinfo)) ;
for (k = 1 ; k < argc - 2 ; k++)
{
if ((state.infile [k] = sf_open (argv [k], SFM_READ, &sfinfo)) == NULL)
{ printf ("\nError : Not able to open input file '%s'\n%s\n", argv [k], sf_strerror (NULL)) ;
exit (1) ;
} ;
if (sfinfo.channels != 1)
{ printf ("\bError : Input file '%s' should be mono (has %d channels).\n", argv [k], sfinfo.channels) ;
exit (1) ;
} ;
switch (sfinfo.format & SF_FORMAT_SUBMASK)
{ case SF_FORMAT_FLOAT :
case SF_FORMAT_DOUBLE :
case SF_FORMAT_VORBIS :
double_merge = 1 ;
break ;
default :
break ;
} ;
state.channels ++ ;
} ;
sfinfo.channels = state.channels ;
sfinfo.format = sfe_file_type_of_ext (argv [argc - 1], sfinfo.format) ;
if ((state.outfile = sf_open (argv [argc - 1], SFM_WRITE, &sfinfo)) == NULL)
{ printf ("Not able to open output file '%s'\n%s\n", argv [argc - 1], sf_strerror (NULL)) ;
exit (1) ;
} ;
if (double_merge)
interleave_double (&state) ;
else
interleave_int (&state) ;
for (k = 0 ; k < MAX_INPUTS ; k++)
if (state.infile [k] != NULL)
sf_close (state.infile [k]) ;
sf_close (state.outfile) ;
return 0 ;
} /* main */
/*------------------------------------------------------------------------------
*/
static void
usage_exit (void)
{ puts ("\nUsage : sndfile-interleave <input 1> <input 2> ... -o <output file>\n") ;
puts ("Merge two mono files to one stereo file\n") ;
exit (0) ;
} /* usage_exit */
static void
interleave_int (STATE * state)
{ int max_read_len, read_len ;
int ch, k ;
do
{ max_read_len = 0 ;
for (ch = 0 ; ch < state->channels ; ch ++)
{ read_len = sf_read_int (state->infile [ch], state->din.i, BUFFER_LEN) ;
if (read_len < BUFFER_LEN)
memset (state->din.i + read_len, 0, sizeof (state->din.i [0]) * (BUFFER_LEN - read_len)) ;
for (k = 0 ; k < read_len ; k++)
state->dout.i [k * state->channels + ch] = state->din.i [k] ;
max_read_len = MAX (max_read_len, read_len) ;
} ;
sf_writef_int (state->outfile, state->dout.i, max_read_len) ;
}
while (max_read_len > 0) ;
} /* interleave_int */
static void
interleave_double (STATE * state)
{ int max_read_len, read_len ;
int ch, k ;
do
{ max_read_len = 0 ;
for (ch = 0 ; ch < state->channels ; ch ++)
{ read_len = sf_read_double (state->infile [ch], state->din.d, BUFFER_LEN) ;
if (read_len < BUFFER_LEN)
memset (state->din.d + read_len, 0, sizeof (state->din.d [0]) * (BUFFER_LEN - read_len)) ;
for (k = 0 ; k < read_len ; k++)
state->dout.d [k * state->channels + ch] = state->din.d [k] ;
max_read_len = MAX (max_read_len, read_len) ;
} ;
sf_writef_double (state->outfile, state->dout.d, max_read_len) ;
}
while (max_read_len > 0) ;
} /* interleave_double */