Blame examples/sndfile-to-text.c

Packit Service 17f94a
/*
Packit Service 17f94a
** Copyright (C) 2008-2016 Erik de Castro Lopo <erikd@mega-nerd.com>
Packit Service 17f94a
**
Packit Service 17f94a
** All rights reserved.
Packit Service 17f94a
**
Packit Service 17f94a
** Redistribution and use in source and binary forms, with or without
Packit Service 17f94a
** modification, are permitted provided that the following conditions are
Packit Service 17f94a
** met:
Packit Service 17f94a
**
Packit Service 17f94a
**     * Redistributions of source code must retain the above copyright
Packit Service 17f94a
**       notice, this list of conditions and the following disclaimer.
Packit Service 17f94a
**     * Redistributions in binary form must reproduce the above copyright
Packit Service 17f94a
**       notice, this list of conditions and the following disclaimer in
Packit Service 17f94a
**       the documentation and/or other materials provided with the
Packit Service 17f94a
**       distribution.
Packit Service 17f94a
**     * Neither the author nor the names of any contributors may be used
Packit Service 17f94a
**       to endorse or promote products derived from this software without
Packit Service 17f94a
**       specific prior written permission.
Packit Service 17f94a
**
Packit Service 17f94a
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit Service 17f94a
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
Packit Service 17f94a
** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
Packit Service 17f94a
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
Packit Service 17f94a
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Packit Service 17f94a
** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Packit Service 17f94a
** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
Packit Service 17f94a
** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Packit Service 17f94a
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
Packit Service 17f94a
** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Packit Service 17f94a
** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 17f94a
*/
Packit Service 17f94a
Packit Service 17f94a
#include <stdio.h>
Packit Service 17f94a
#include <stdlib.h>
Packit Service 17f94a
#include <string.h>
Packit Service 17f94a
#include <ctype.h>
Packit Service 17f94a
#include <float.h>
Packit Service 17f94a
Packit Service 17f94a
#include <sndfile.h>
Packit Service 17f94a
Packit Service 17f94a
#define	BLOCK_SIZE 4096
Packit Service 17f94a
Packit Service 17f94a
#ifdef DBL_DECIMAL_DIG
Packit Service 17f94a
	#define OP_DBL_Digs (DBL_DECIMAL_DIG)
Packit Service 17f94a
#else
Packit Service 17f94a
	#ifdef DECIMAL_DIG
Packit Service 17f94a
		#define OP_DBL_Digs (DECIMAL_DIG)
Packit Service 17f94a
	#else
Packit Service 17f94a
		#define OP_DBL_Digs (DBL_DIG + 3)
Packit Service 17f94a
	#endif
Packit Service 17f94a
#endif
Packit Service 17f94a
Packit Service 17f94a
static void
Packit Service 17f94a
print_usage (char *progname)
Packit Service 17f94a
{	printf ("\nUsage : %s [--full-precision] <input file> <output file>\n", progname) ;
Packit Service 17f94a
	puts ("\n"
Packit Service 17f94a
		"    Where the output file will contain a line for each frame\n"
Packit Service 17f94a
		"    and a column for each channel.\n"
Packit Service 17f94a
		) ;
Packit Service 17f94a
Packit Service 17f94a
} /* print_usage */
Packit Service 17f94a
Packit Service 17f94a
static void
Packit Service 17f94a
convert_to_text (SNDFILE * infile, FILE * outfile, int channels, int full_precision)
Packit Service 17f94a
{	float buf [BLOCK_SIZE] ;
Packit Service 17f94a
	sf_count_t frames ;
Packit Service 17f94a
	int k, m, readcount ;
Packit Service 17f94a
Packit Service 17f94a
	frames = BLOCK_SIZE / channels ;
Packit Service 17f94a
Packit Service 17f94a
	while ((readcount = sf_readf_float (infile, buf, frames)) > 0)
Packit Service 17f94a
	{	for (k = 0 ; k < readcount ; k++)
Packit Service 17f94a
		{	for (m = 0 ; m < channels ; m++)
Packit Service 17f94a
				if (full_precision)
Packit Service 17f94a
					fprintf (outfile, " %.*e", OP_DBL_Digs - 1, buf [k * channels + m]) ;
Packit Service 17f94a
				else
Packit Service 17f94a
					fprintf (outfile, " % 12.10f", buf [k * channels + m]) ;
Packit Service 17f94a
			fprintf (outfile, "\n") ;
Packit Service 17f94a
			} ;
Packit Service 17f94a
		} ;
Packit Service 17f94a
Packit Service 17f94a
	return ;
Packit Service 17f94a
} /* convert_to_text */
Packit Service 17f94a
Packit Service 17f94a
int
Packit Service 17f94a
main (int argc, char * argv [])
Packit Service 17f94a
{	char 		*progname, *infilename, *outfilename ;
Packit Service 17f94a
	SNDFILE		*infile = NULL ;
Packit Service 17f94a
	FILE		*outfile = NULL ;
Packit Service 17f94a
	SF_INFO		sfinfo ;
Packit Service 17f94a
	int		full_precision = 0 ;
Packit Service 17f94a
Packit Service 17f94a
	progname = strrchr (argv [0], '/') ;
Packit Service 17f94a
	progname = progname ? progname + 1 : argv [0] ;
Packit Service 17f94a
Packit Service 17f94a
	switch (argc)
Packit Service 17f94a
	{	case 4 :
Packit Service 17f94a
			if (!strcmp ("--full-precision", argv [3]))
Packit Service 17f94a
			{	print_usage (progname) ;
Packit Service 17f94a
				return 1 ;
Packit Service 17f94a
				} ;
Packit Service 17f94a
			full_precision = 1 ;
Packit Service 17f94a
			argv++ ;
Packit Service 17f94a
		case 3 :
Packit Service 17f94a
			break ;
Packit Service 17f94a
		default:
Packit Service 17f94a
			print_usage (progname) ;
Packit Service 17f94a
			return 1 ;
Packit Service 17f94a
		} ;
Packit Service 17f94a
Packit Service 17f94a
	infilename = argv [1] ;
Packit Service 17f94a
	outfilename = argv [2] ;
Packit Service 17f94a
Packit Service 17f94a
	if (strcmp (infilename, outfilename) == 0)
Packit Service 17f94a
	{	printf ("Error : Input and output filenames are the same.\n\n") ;
Packit Service 17f94a
		print_usage (progname) ;
Packit Service 17f94a
		return 1 ;
Packit Service 17f94a
		} ;
Packit Service 17f94a
Packit Service 17f94a
	if (infilename [0] == '-')
Packit Service 17f94a
	{	printf ("Error : Input filename (%s) looks like an option.\n\n", infilename) ;
Packit Service 17f94a
		print_usage (progname) ;
Packit Service 17f94a
		return 1 ;
Packit Service 17f94a
		} ;
Packit Service 17f94a
Packit Service 17f94a
	if (outfilename [0] == '-')
Packit Service 17f94a
	{	printf ("Error : Output filename (%s) looks like an option.\n\n", outfilename) ;
Packit Service 17f94a
		print_usage (progname) ;
Packit Service 17f94a
		return 1 ;
Packit Service 17f94a
		} ;
Packit Service 17f94a
Packit Service 17f94a
	memset (&sfinfo, 0, sizeof (sfinfo)) ;
Packit Service 17f94a
Packit Service 17f94a
	if ((infile = sf_open (infilename, SFM_READ, &sfinfo)) == NULL)
Packit Service 17f94a
	{	printf ("Not able to open input file %s.\n", infilename) ;
Packit Service 17f94a
		puts (sf_strerror (NULL)) ;
Packit Service 17f94a
		return 1 ;
Packit Service 17f94a
		} ;
Packit Service 17f94a
Packit Service 17f94a
	/* Open the output file. */
Packit Service 17f94a
	if ((outfile = fopen (outfilename, "w")) == NULL)
Packit Service 17f94a
	{	printf ("Not able to open output file %s : %s\n", outfilename, sf_strerror (NULL)) ;
Packit Service 17f94a
		return 1 ;
Packit Service 17f94a
		} ;
Packit Service 17f94a
Packit Service 17f94a
	fprintf (outfile, "# Converted from file %s.\n", infilename) ;
Packit Service 17f94a
	fprintf (outfile, "# Channels %d, Sample rate %d\n", sfinfo.channels, sfinfo.samplerate) ;
Packit Service 17f94a
Packit Service 17f94a
	convert_to_text (infile, outfile, sfinfo.channels, full_precision) ;
Packit Service 17f94a
Packit Service 17f94a
	sf_close (infile) ;
Packit Service 17f94a
	fclose (outfile) ;
Packit Service 17f94a
Packit Service 17f94a
	return 0 ;
Packit Service 17f94a
} /* main */
Packit Service 17f94a