Blame examples/generate.c

Packit 4aff17
/*
Packit 4aff17
** Copyright (C) 2002-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
Packit 4aff17
**
Packit 4aff17
** All rights reserved.
Packit 4aff17
**
Packit 4aff17
** Redistribution and use in source and binary forms, with or without
Packit 4aff17
** modification, are permitted provided that the following conditions are
Packit 4aff17
** met:
Packit 4aff17
**
Packit 4aff17
**     * Redistributions of source code must retain the above copyright
Packit 4aff17
**       notice, this list of conditions and the following disclaimer.
Packit 4aff17
**     * Redistributions in binary form must reproduce the above copyright
Packit 4aff17
**       notice, this list of conditions and the following disclaimer in
Packit 4aff17
**       the documentation and/or other materials provided with the
Packit 4aff17
**       distribution.
Packit 4aff17
**     * Neither the author nor the names of any contributors may be used
Packit 4aff17
**       to endorse or promote products derived from this software without
Packit 4aff17
**       specific prior written permission.
Packit 4aff17
**
Packit 4aff17
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 4aff17
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
Packit 4aff17
** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
Packit 4aff17
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
Packit 4aff17
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Packit 4aff17
** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Packit 4aff17
** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
Packit 4aff17
** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Packit 4aff17
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
Packit 4aff17
** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Packit 4aff17
** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 4aff17
*/
Packit 4aff17
Packit 4aff17
#include "sfconfig.h"
Packit 4aff17
Packit 4aff17
#include <stdio.h>
Packit 4aff17
#include <stdlib.h>
Packit 4aff17
#include <string.h>
Packit 4aff17
#include <math.h>
Packit 4aff17
Packit 4aff17
#include <sndfile.h>
Packit 4aff17
Packit 4aff17
#define	BUFFER_LEN			4096
Packit 4aff17
Packit 4aff17
static void encode_file (const char *infilename, const char *outfilename, int filetype) ;
Packit 4aff17
Packit 4aff17
int
Packit 4aff17
main (int argc, char **argv)
Packit 4aff17
{
Packit 4aff17
	if (argc != 2)
Packit 4aff17
	{	puts ("\nEncode a single input file into a number of different output ") ;
Packit 4aff17
		puts ("encodings. These output encodings can then be moved to another ") ;
Packit 4aff17
		puts ("OS for testing.\n") ;
Packit 4aff17
		puts ("    Usage : generate <filename>\n") ;
Packit 4aff17
		exit (1) ;
Packit 4aff17
		} ;
Packit 4aff17
Packit 4aff17
	/* A couple of standard WAV files. Make sure Win32 plays these. */
Packit 4aff17
	encode_file (argv [1], "pcmu8.wav"	, SF_FORMAT_WAV | SF_FORMAT_PCM_U8) ;
Packit 4aff17
	encode_file (argv [1], "pcm16.wav"	, SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
Packit 4aff17
	encode_file (argv [1], "imaadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM) ;
Packit 4aff17
	encode_file (argv [1], "msadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM) ;
Packit 4aff17
	encode_file (argv [1], "gsm610.wav"	, SF_FORMAT_WAV | SF_FORMAT_GSM610) ;
Packit 4aff17
Packit 4aff17
	/* Soundforge W64. */
Packit 4aff17
	encode_file (argv [1], "pcmu8.w64"	, SF_FORMAT_W64 | SF_FORMAT_PCM_U8) ;
Packit 4aff17
	encode_file (argv [1], "pcm16.w64"	, SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
Packit 4aff17
	encode_file (argv [1], "imaadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_MS_ADPCM) ;
Packit 4aff17
	encode_file (argv [1], "msadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_IMA_ADPCM) ;
Packit 4aff17
	encode_file (argv [1], "gsm610.w64"	, SF_FORMAT_W64 | SF_FORMAT_GSM610) ;
Packit 4aff17
Packit 4aff17
	return 0 ;
Packit 4aff17
} /* main */
Packit 4aff17
Packit 4aff17
/*============================================================================================
Packit 4aff17
**	Helper functions and macros.
Packit 4aff17
*/
Packit 4aff17
Packit 4aff17
#define PUT_DOTS(k)					\
Packit 4aff17
			{	while (k--)			\
Packit 4aff17
					putchar ('.') ;	\
Packit 4aff17
				putchar (' ') ;		\
Packit 4aff17
				}
Packit 4aff17
Packit 4aff17
/*========================================================================================
Packit 4aff17
*/
Packit 4aff17
Packit 4aff17
static void
Packit 4aff17
encode_file (const char *infilename, const char *outfilename, int filetype)
Packit 4aff17
{	static float buffer [BUFFER_LEN] ;
Packit 4aff17
Packit 4aff17
	SNDFILE		*infile, *outfile ;
Packit 4aff17
	SF_INFO		sfinfo ;
Packit 4aff17
	int			k, readcount ;
Packit 4aff17
Packit 4aff17
	printf ("    %s -> %s ", infilename, outfilename) ;
Packit 4aff17
	fflush (stdout) ;
Packit 4aff17
Packit 4aff17
	k = 16 - strlen (outfilename) ;
Packit 4aff17
	PUT_DOTS (k) ;
Packit 4aff17
Packit 4aff17
	memset (&sfinfo, 0, sizeof (sfinfo)) ;
Packit 4aff17
Packit 4aff17
	if (! (infile = sf_open (infilename, SFM_READ, &sfinfo)))
Packit 4aff17
	{	printf ("Error : could not open file : %s\n", infilename) ;
Packit 4aff17
		puts (sf_strerror (NULL)) ;
Packit 4aff17
		exit (1) ;
Packit 4aff17
		}
Packit 4aff17
Packit 4aff17
	sfinfo.format = filetype ;
Packit 4aff17
Packit 4aff17
	if (! sf_format_check (&sfinfo))
Packit 4aff17
	{	sf_close (infile) ;
Packit 4aff17
		printf ("Invalid encoding\n") ;
Packit 4aff17
		return ;
Packit 4aff17
		} ;
Packit 4aff17
Packit 4aff17
	if (! (outfile = sf_open (outfilename, SFM_WRITE, &sfinfo)))
Packit 4aff17
	{	printf ("Error : could not open file : %s\n", outfilename) ;
Packit 4aff17
		puts (sf_strerror (NULL)) ;
Packit 4aff17
		exit (1) ;
Packit 4aff17
		} ;
Packit 4aff17
Packit 4aff17
	while ((readcount = sf_read_float (infile, buffer, BUFFER_LEN)) > 0)
Packit 4aff17
		sf_write_float (outfile, buffer, readcount) ;
Packit 4aff17
Packit 4aff17
	sf_close (infile) ;
Packit 4aff17
	sf_close (outfile) ;
Packit 4aff17
Packit 4aff17
	printf ("ok\n") ;
Packit 4aff17
Packit 4aff17
	return ;
Packit 4aff17
} /* encode_file */
Packit 4aff17