cannam@125: /*
cannam@125: ** Copyright (C) 2001-2016 Erik de Castro Lopo <erikd@mega-nerd.com>
cannam@125: **
cannam@125: ** This program is free software; you can redistribute it and/or modify
cannam@125: ** it under the terms of the GNU General Public License as published by
cannam@125: ** the Free Software Foundation; either version 2 of the License, or
cannam@125: ** (at your option) any later version.
cannam@125: **
cannam@125: ** This program is distributed in the hope that it will be useful,
cannam@125: ** but WITHOUT ANY WARRANTY; without even the implied warranty of
cannam@125: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
cannam@125: ** GNU General Public License for more details.
cannam@125: **
cannam@125: ** You should have received a copy of the GNU General Public License
cannam@125: ** along with this program; if not, write to the Free Software
cannam@125: ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
cannam@125: */
cannam@125: 
cannam@125: #include "sfconfig.h"
cannam@125: 
cannam@125: #include <stdio.h>
cannam@125: #include <stdlib.h>
cannam@125: #include <stdint.h>
cannam@125: #include <inttypes.h>
cannam@125: #include <string.h>
cannam@125: #include <time.h>
cannam@125: 
cannam@125: #if HAVE_UNISTD_H
cannam@125: #include <unistd.h>
cannam@125: #endif
cannam@125: 
cannam@125: #include <math.h>
cannam@125: 
cannam@125: #include <sndfile.h>
cannam@125: 
cannam@125: #include "sfendian.h"
cannam@125: #include "utils.h"
cannam@125: 
cannam@125: #define	BUFFER_LEN		(1 << 10)
cannam@125: #define LOG_BUFFER_SIZE	1024
cannam@125: #define data_MARKER		MAKE_MARKER ('d', 'a', 't', 'a')
cannam@125: 
cannam@125: static	void	float_norm_test			(const char *filename) ;
cannam@125: static	void	double_norm_test		(const char *filename) ;
cannam@125: static	void	format_tests			(void) ;
cannam@125: static	void	calc_peak_test			(int filetype, const char *filename, int channels) ;
cannam@125: static	void	truncate_test			(const char *filename, int filetype) ;
cannam@125: static	void	instrument_test			(const char *filename, int filetype) ;
cannam@125: static	void	cue_test			(const char *filename, int filetype) ;
cannam@125: static	void	channel_map_test		(const char *filename, int filetype) ;
cannam@125: static	void	current_sf_info_test	(const char *filename) ;
cannam@125: static	void	raw_needs_endswap_test	(const char *filename, int filetype) ;
cannam@125: 
cannam@125: static	void	broadcast_test			(const char *filename, int filetype) ;
cannam@125: static	void	broadcast_rdwr_test		(const char *filename, int filetype) ;
cannam@125: static	void	broadcast_coding_history_test	(const char *filename) ;
cannam@125: static	void	broadcast_coding_history_size	(const char *filename) ;
cannam@125: 
cannam@125: /* Cart Chunk tests */
cannam@125: static void	cart_test			(const char *filename, int filetype) ;
cannam@125: static void	cart_rdwr_test			(const char *filename, int filetype) ;
cannam@125: 
cannam@125: /* Force the start of this buffer to be double aligned. Sparc-solaris will
cannam@125: ** choke if its not.
cannam@125: */
cannam@125: 
cannam@125: static	int		int_data	[BUFFER_LEN] ;
cannam@125: static	float	float_data	[BUFFER_LEN] ;
cannam@125: static	double	double_data	[BUFFER_LEN] ;
cannam@125: 
cannam@125: int
cannam@125: main (int argc, char *argv [])
cannam@125: {	int		do_all = 0 ;
cannam@125: 	int		test_count = 0 ;
cannam@125: 
cannam@125: 	if (argc != 2)
cannam@125: 	{	printf ("Usage : %s <test>\n", argv [0]) ;
cannam@125: 		printf ("    Where <test> is one of the following:\n") ;
cannam@125: 		printf ("           ver     - test sf_command (SFC_GETLIB_VERSION)\n") ;
cannam@125: 		printf ("           norm    - test floating point normalisation\n") ;
cannam@125: 		printf ("           format  - test format string commands\n") ;
cannam@125: 		printf ("           peak    - test peak calculation\n") ;
cannam@125: 		printf ("           trunc   - test file truncation\n") ;
cannam@125: 		printf ("           inst    - test set/get of SF_INSTRUMENT.\n") ;
cannam@125: 		printf ("           cue     - test set/get of SF_CUES.\n") ;
cannam@125: 		printf ("           chanmap - test set/get of channel map data..\n") ;
cannam@125: 		printf ("           bext    - test set/get of SF_BROADCAST_INFO.\n") ;
cannam@125: 		printf ("           bextch  - test set/get of SF_BROADCAST_INFO coding_history.\n") ;
cannam@125: 		printf ("           cart    - test set/get of SF_CART_INFO.\n") ;
cannam@125: 		printf ("           rawend  - test SFC_RAW_NEEDS_ENDSWAP.\n") ;
cannam@125: 		printf ("           all     - perform all tests\n") ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	do_all = ! strcmp (argv [1], "all") ;
cannam@125: 
cannam@125: 	if (do_all || strcmp (argv [1], "ver") == 0)
cannam@125: 	{	char buffer [128] ;
cannam@125: 
cannam@125: 		print_test_name ("version_test", "(none)") ;
cannam@125: 		buffer [0] = 0 ;
cannam@125: 		sf_command (NULL, SFC_GET_LIB_VERSION, buffer, sizeof (buffer)) ;
cannam@125: 		if (strlen (buffer) < 1)
cannam@125: 		{	printf ("Line %d: could not retrieve lib version.\n", __LINE__) ;
cannam@125: 			exit (1) ;
cannam@125: 			} ;
cannam@125: 		puts ("ok") ;
cannam@125: 		test_count ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (do_all || strcmp (argv [1], "norm") == 0)
cannam@125: 	{	/*	Preliminary float/double normalisation tests. More testing
cannam@125: 		**	is done in the program 'floating_point_test'.
cannam@125: 		*/
cannam@125: 		float_norm_test		("float.wav") ;
cannam@125: 		double_norm_test	("double.wav") ;
cannam@125: 		test_count ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (do_all || strcmp (argv [1], "peak") == 0)
cannam@125: 	{	calc_peak_test (SF_ENDIAN_BIG		| SF_FORMAT_RAW, "be-peak.raw", 1) ;
cannam@125: 		calc_peak_test (SF_ENDIAN_LITTLE	| SF_FORMAT_RAW, "le-peak.raw", 1) ;
cannam@125: 		calc_peak_test (SF_ENDIAN_BIG		| SF_FORMAT_RAW, "be-peak.raw", 7) ;
cannam@125: 		calc_peak_test (SF_ENDIAN_LITTLE	| SF_FORMAT_RAW, "le-peak.raw", 7) ;
cannam@125: 		test_count ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (do_all || ! strcmp (argv [1], "format"))
cannam@125: 	{	format_tests () ;
cannam@125: 		test_count ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (do_all || strcmp (argv [1], "trunc") == 0)
cannam@125: 	{	truncate_test ("truncate.raw", SF_FORMAT_RAW | SF_FORMAT_PCM_32) ;
cannam@125: 		truncate_test ("truncate.au" , SF_FORMAT_AU | SF_FORMAT_PCM_16) ;
cannam@125: 		test_count ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (do_all || strcmp (argv [1], "inst") == 0)
cannam@125: 	{	instrument_test ("instrument.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
cannam@125: 		instrument_test ("instrument.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_24) ;
cannam@125: 		/*-instrument_test ("instrument.xi", SF_FORMAT_XI | SF_FORMAT_DPCM_16) ;-*/
cannam@125: 		test_count ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (do_all || strcmp (argv [1], "cue") == 0)
cannam@125: 	{	cue_test ("cue.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
cannam@125: 		cue_test ("cue.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_24) ;
cannam@125: 		test_count ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (do_all || strcmp (argv [1], "current_sf_info") == 0)
cannam@125: 	{	current_sf_info_test ("current.wav") ;
cannam@125: 		test_count ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (do_all || strcmp (argv [1], "bext") == 0)
cannam@125: 	{	broadcast_test ("broadcast.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
cannam@125: 		broadcast_rdwr_test	("broadcast.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
cannam@125: 
cannam@125: 		broadcast_test ("broadcast.wavex", SF_FORMAT_WAVEX | SF_FORMAT_PCM_16) ;
cannam@125: 		broadcast_rdwr_test	("broadcast.wavex", SF_FORMAT_WAVEX | SF_FORMAT_PCM_16) ;
cannam@125: 
cannam@125: 		broadcast_test ("broadcast.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
cannam@125: 		broadcast_rdwr_test	("broadcast.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
cannam@125: 		test_count ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (do_all || strcmp (argv [1], "cart") == 0)
cannam@125: 	{	cart_test ("cart.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
cannam@125: 		cart_rdwr_test ("cart.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
cannam@125: 		cart_test ("cart.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
cannam@125: 		cart_rdwr_test ("cart.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
cannam@125: 		test_count ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (do_all || strcmp (argv [1], "bextch") == 0)
cannam@125: 	{	broadcast_coding_history_test ("coding_history.wav") ;
cannam@125: 		broadcast_coding_history_size ("coding_hist_size.wav") ;
cannam@125: 		test_count ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (do_all || strcmp (argv [1], "chanmap") == 0)
cannam@125: 	{	channel_map_test ("chanmap.wavex", SF_FORMAT_WAVEX | SF_FORMAT_PCM_16) ;
cannam@125: 		channel_map_test ("chanmap.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
cannam@125: 		channel_map_test ("chanmap.aifc" , SF_FORMAT_AIFF | SF_FORMAT_PCM_16) ;
cannam@125: 		channel_map_test ("chanmap.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_16) ;
cannam@125: 		test_count ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (do_all || strcmp (argv [1], "rawend") == 0)
cannam@125: 	{	raw_needs_endswap_test ("raw_end.wav", SF_FORMAT_WAV) ;
cannam@125: 		raw_needs_endswap_test ("raw_end.wavex", SF_FORMAT_WAVEX) ;
cannam@125: 		raw_needs_endswap_test ("raw_end.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV) ;
cannam@125: 		raw_needs_endswap_test ("raw_end.aiff", SF_FORMAT_AIFF) ;
cannam@125: 		raw_needs_endswap_test ("raw_end.aiff_le", SF_ENDIAN_LITTLE | SF_FORMAT_AIFF) ;
cannam@125: 		test_count ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (test_count == 0)
cannam@125: 	{	printf ("Mono : ************************************\n") ;
cannam@125: 		printf ("Mono : *  No '%s' test defined.\n", argv [1]) ;
cannam@125: 		printf ("Mono : ************************************\n") ;
cannam@125: 		return 1 ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	return 0 ;
cannam@125: } /* main */
cannam@125: 
cannam@125: /*============================================================================================
cannam@125: **	Here are the test functions.
cannam@125: */
cannam@125: 
cannam@125: static void
cannam@125: float_norm_test (const char *filename)
cannam@125: {	SNDFILE			*file ;
cannam@125: 	SF_INFO			sfinfo ;
cannam@125: 	unsigned int	k ;
cannam@125: 
cannam@125: 	print_test_name ("float_norm_test", filename) ;
cannam@125: 
cannam@125: 	sfinfo.samplerate	= 44100 ;
cannam@125: 	sfinfo.format		= (SF_FORMAT_RAW | SF_FORMAT_PCM_16) ;
cannam@125: 	sfinfo.channels		= 1 ;
cannam@125: 	sfinfo.frames		= BUFFER_LEN ;
cannam@125: 
cannam@125: 	/* Create float_data with all values being less than 1.0. */
cannam@125: 	for (k = 0 ; k < BUFFER_LEN / 2 ; k++)
cannam@125: 		float_data [k] = (k + 5) / (2.0 * BUFFER_LEN) ;
cannam@125: 	for (k = BUFFER_LEN / 2 ; k < BUFFER_LEN ; k++)
cannam@125: 		float_data [k] = (k + 5) ;
cannam@125: 
cannam@125: 	if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
cannam@125: 	{	printf ("Line %d: sf_open_write failed with error : ", __LINE__) ;
cannam@125: 		fflush (stdout) ;
cannam@125: 		puts (sf_strerror (NULL)) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	/* Normalisation is on by default so no need to do anything here. */
cannam@125: 
cannam@125: 	if ((k = sf_write_float (file, float_data, BUFFER_LEN / 2)) != BUFFER_LEN / 2)
cannam@125: 	{	printf ("Line %d: sf_write_float failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	/* Turn normalisation off. */
cannam@125: 	sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
cannam@125: 
cannam@125: 	if ((k = sf_write_float (file, float_data + BUFFER_LEN / 2, BUFFER_LEN / 2)) != BUFFER_LEN / 2)
cannam@125: 	{	printf ("Line %d: sf_write_float failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	/* sfinfo struct should still contain correct data. */
cannam@125: 	if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
cannam@125: 	{	printf ("Line %d: sf_open_read failed with error : ", __LINE__) ;
cannam@125: 		fflush (stdout) ;
cannam@125: 		puts (sf_strerror (NULL)) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (sfinfo.format != (SF_FORMAT_RAW | SF_FORMAT_PCM_16))
cannam@125: 	{	printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, (SF_FORMAT_RAW | SF_FORMAT_PCM_16), sfinfo.format) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (sfinfo.frames != BUFFER_LEN)
cannam@125: 	{	printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %" PRId64 ")\n", __LINE__, BUFFER_LEN, sfinfo.frames) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (sfinfo.channels != 1)
cannam@125: 	{	printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	/* Read float_data and check that it is normalised (ie default). */
cannam@125: 	if ((k = sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN)
cannam@125: 	{	printf ("\n\nLine %d: sf_read_float failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	for (k = 0 ; k < BUFFER_LEN ; k++)
cannam@125: 		if (float_data [k] >= 1.0)
cannam@125: 		{	printf ("\n\nLine %d: float_data [%d] == %f which is greater than 1.0\n", __LINE__, k, float_data [k]) ;
cannam@125: 			exit (1) ;
cannam@125: 			} ;
cannam@125: 
cannam@125: 	/* Seek to start of file, turn normalisation off, read float_data and check again. */
cannam@125: 	sf_seek (file, 0, SEEK_SET) ;
cannam@125: 	sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
cannam@125: 
cannam@125: 	if ((k = sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN)
cannam@125: 	{	printf ("\n\nLine %d: sf_read_float failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	for (k = 0 ; k < BUFFER_LEN ; k++)
cannam@125: 		if (float_data [k] < 1.0)
cannam@125: 		{	printf ("\n\nLine %d: float_data [%d] == %f which is less than 1.0\n", __LINE__, k, float_data [k]) ;
cannam@125: 			exit (1) ;
cannam@125: 			} ;
cannam@125: 
cannam@125: 	/* Seek to start of file, turn normalisation on, read float_data and do final check. */
cannam@125: 	sf_seek (file, 0, SEEK_SET) ;
cannam@125: 	sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_TRUE) ;
cannam@125: 
cannam@125: 	if ((k = sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN)
cannam@125: 	{	printf ("\n\nLine %d: sf_read_float failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	for (k = 0 ; k < BUFFER_LEN ; k++)
cannam@125: 		if (float_data [k] > 1.0)
cannam@125: 		{	printf ("\n\nLine %d: float_data [%d] == %f which is greater than 1.0\n", __LINE__, k, float_data [k]) ;
cannam@125: 			exit (1) ;
cannam@125: 			} ;
cannam@125: 
cannam@125: 
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	unlink (filename) ;
cannam@125: 
cannam@125: 	printf ("ok\n") ;
cannam@125: } /* float_norm_test */
cannam@125: 
cannam@125: static void
cannam@125: double_norm_test (const char *filename)
cannam@125: {	SNDFILE			*file ;
cannam@125: 	SF_INFO			sfinfo ;
cannam@125: 	unsigned int	k ;
cannam@125: 
cannam@125: 	print_test_name ("double_norm_test", filename) ;
cannam@125: 
cannam@125: 	sfinfo.samplerate	= 44100 ;
cannam@125: 	sfinfo.format		= (SF_FORMAT_RAW | SF_FORMAT_PCM_16) ;
cannam@125: 	sfinfo.channels		= 1 ;
cannam@125: 	sfinfo.frames		= BUFFER_LEN ;
cannam@125: 
cannam@125: 	/* Create double_data with all values being less than 1.0. */
cannam@125: 	for (k = 0 ; k < BUFFER_LEN / 2 ; k++)
cannam@125: 		double_data [k] = (k + 5) / (2.0 * BUFFER_LEN) ;
cannam@125: 	for (k = BUFFER_LEN / 2 ; k < BUFFER_LEN ; k++)
cannam@125: 		double_data [k] = (k + 5) ;
cannam@125: 
cannam@125: 	if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
cannam@125: 	{	printf ("Line %d: sf_open_write failed with error : ", __LINE__) ;
cannam@125: 		fflush (stdout) ;
cannam@125: 		puts (sf_strerror (NULL)) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	/* Normailsation is on by default so no need to do anything here. */
cannam@125: 	/*-sf_command (file, "set-norm-double", "true", 0) ;-*/
cannam@125: 
cannam@125: 	if ((k = sf_write_double (file, double_data, BUFFER_LEN / 2)) != BUFFER_LEN / 2)
cannam@125: 	{	printf ("Line %d: sf_write_double failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	/* Turn normalisation off. */
cannam@125: 	sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
cannam@125: 
cannam@125: 	if ((k = sf_write_double (file, double_data + BUFFER_LEN / 2, BUFFER_LEN / 2)) != BUFFER_LEN / 2)
cannam@125: 	{	printf ("Line %d: sf_write_double failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
cannam@125: 	{	printf ("Line %d: sf_open_read failed with error : ", __LINE__) ;
cannam@125: 		fflush (stdout) ;
cannam@125: 		puts (sf_strerror (NULL)) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (sfinfo.format != (SF_FORMAT_RAW | SF_FORMAT_PCM_16))
cannam@125: 	{	printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, (SF_FORMAT_RAW | SF_FORMAT_PCM_16), sfinfo.format) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (sfinfo.frames != BUFFER_LEN)
cannam@125: 	{	printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %" PRId64 ")\n", __LINE__, BUFFER_LEN, sfinfo.frames) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (sfinfo.channels != 1)
cannam@125: 	{	printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	/* Read double_data and check that it is normalised (ie default). */
cannam@125: 	if ((k = sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN)
cannam@125: 	{	printf ("\n\nLine %d: sf_read_double failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	for (k = 0 ; k < BUFFER_LEN ; k++)
cannam@125: 		if (double_data [k] >= 1.0)
cannam@125: 		{	printf ("\n\nLine %d: double_data [%d] == %f which is greater than 1.0\n", __LINE__, k, double_data [k]) ;
cannam@125: 			exit (1) ;
cannam@125: 			} ;
cannam@125: 
cannam@125: 	/* Seek to start of file, turn normalisation off, read double_data and check again. */
cannam@125: 	sf_seek (file, 0, SEEK_SET) ;
cannam@125: 	sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
cannam@125: 
cannam@125: 	if ((k = sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN)
cannam@125: 	{	printf ("\n\nLine %d: sf_read_double failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	for (k = 0 ; k < BUFFER_LEN ; k++)
cannam@125: 		if (double_data [k] < 1.0)
cannam@125: 		{	printf ("\n\nLine %d: double_data [%d] == %f which is less than 1.0\n", __LINE__, k, double_data [k]) ;
cannam@125: 			exit (1) ;
cannam@125: 			} ;
cannam@125: 
cannam@125: 	/* Seek to start of file, turn normalisation on, read double_data and do final check. */
cannam@125: 	sf_seek (file, 0, SEEK_SET) ;
cannam@125: 	sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_TRUE) ;
cannam@125: 
cannam@125: 	if ((k = sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN)
cannam@125: 	{	printf ("\n\nLine %d: sf_read_double failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	for (k = 0 ; k < BUFFER_LEN ; k++)
cannam@125: 		if (double_data [k] > 1.0)
cannam@125: 		{	printf ("\n\nLine %d: double_data [%d] == %f which is greater than 1.0\n", __LINE__, k, double_data [k]) ;
cannam@125: 			exit (1) ;
cannam@125: 			} ;
cannam@125: 
cannam@125: 
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	unlink (filename) ;
cannam@125: 
cannam@125: 	printf ("ok\n") ;
cannam@125: } /* double_norm_test */
cannam@125: 
cannam@125: static	void
cannam@125: format_tests	(void)
cannam@125: {	SF_FORMAT_INFO format_info ;
cannam@125: 	SF_INFO		sfinfo ;
cannam@125: 	const char	*last_name ;
cannam@125: 	int 		k, count ;
cannam@125: 
cannam@125: 	print_test_name ("format_tests", "(null)") ;
cannam@125: 
cannam@125: 	/* Clear out SF_INFO struct and set channels > 0. */
cannam@125: 	memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@125: 	sfinfo.channels = 1 ;
cannam@125: 
cannam@125: 	/* First test simple formats. */
cannam@125: 
cannam@125: 	sf_command (NULL, SFC_GET_SIMPLE_FORMAT_COUNT, &count, sizeof (int)) ;
cannam@125: 
cannam@125: 	if (count < 0 || count > 30)
cannam@125: 	{	printf ("Line %d: Weird count.\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	format_info.format = 0 ;
cannam@125: 	sf_command (NULL, SFC_GET_SIMPLE_FORMAT, &format_info, sizeof (format_info)) ;
cannam@125: 
cannam@125: 	last_name = format_info.name ;
cannam@125: 	for (k = 1 ; k < count ; k ++)
cannam@125: 	{	format_info.format = k ;
cannam@125: 		sf_command (NULL, SFC_GET_SIMPLE_FORMAT, &format_info, sizeof (format_info)) ;
cannam@125: 		if (strcmp (last_name, format_info.name) >= 0)
cannam@125: 		{	printf ("\n\nLine %d: format names out of sequence `%s' < `%s'.\n", __LINE__, last_name, format_info.name) ;
cannam@125: 			exit (1) ;
cannam@125: 			} ;
cannam@125: 		sfinfo.format = format_info.format ;
cannam@125: 
cannam@125: 		if (! sf_format_check (&sfinfo))
cannam@125: 		{	printf ("\n\nLine %d: sf_format_check failed.\n", __LINE__) ;
cannam@125: 			printf ("        Name : %s\n", format_info.name) ;
cannam@125: 			printf ("        Format      : 0x%X\n", sfinfo.format) ;
cannam@125: 			printf ("        Channels    : 0x%X\n", sfinfo.channels) ;
cannam@125: 			printf ("        Sample Rate : 0x%X\n", sfinfo.samplerate) ;
cannam@125: 			exit (1) ;
cannam@125: 			} ;
cannam@125: 		last_name = format_info.name ;
cannam@125: 		} ;
cannam@125: 	format_info.format = 666 ;
cannam@125: 	sf_command (NULL, SFC_GET_SIMPLE_FORMAT, &format_info, sizeof (format_info)) ;
cannam@125: 
cannam@125: 	/* Now test major formats. */
cannam@125: 	sf_command (NULL, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof (int)) ;
cannam@125: 
cannam@125: 	if (count < 0 || count > 30)
cannam@125: 	{	printf ("Line %d: Weird count.\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	format_info.format = 0 ;
cannam@125: 	sf_command (NULL, SFC_GET_FORMAT_MAJOR, &format_info, sizeof (format_info)) ;
cannam@125: 
cannam@125: 	last_name = format_info.name ;
cannam@125: 	for (k = 1 ; k < count ; k ++)
cannam@125: 	{	format_info.format = k ;
cannam@125: 		sf_command (NULL, SFC_GET_FORMAT_MAJOR, &format_info, sizeof (format_info)) ;
cannam@125: 		if (strcmp (last_name, format_info.name) >= 0)
cannam@125: 		{	printf ("\n\nLine %d: format names out of sequence (%d) `%s' < `%s'.\n", __LINE__, k, last_name, format_info.name) ;
cannam@125: 			exit (1) ;
cannam@125: 			} ;
cannam@125: 
cannam@125: 		last_name = format_info.name ;
cannam@125: 		} ;
cannam@125: 	format_info.format = 666 ;
cannam@125: 	sf_command (NULL, SFC_GET_FORMAT_MAJOR, &format_info, sizeof (format_info)) ;
cannam@125: 
cannam@125: 	/* Now test subtype formats. */
cannam@125: 	sf_command (NULL, SFC_GET_FORMAT_SUBTYPE_COUNT, &count, sizeof (int)) ;
cannam@125: 
cannam@125: 	if (count < 0 || count > 30)
cannam@125: 	{	printf ("Line %d: Weird count.\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	format_info.format = 0 ;
cannam@125: 	sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &format_info, sizeof (format_info)) ;
cannam@125: 
cannam@125: 	last_name = format_info.name ;
cannam@125: 	for (k = 1 ; k < count ; k ++)
cannam@125: 	{	format_info.format = k ;
cannam@125: 		sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &format_info, sizeof (format_info)) ;
cannam@125: 		} ;
cannam@125: 	format_info.format = 666 ;
cannam@125: 	sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &format_info, sizeof (format_info)) ;
cannam@125: 
cannam@125: 
cannam@125: 	printf ("ok\n") ;
cannam@125: } /* format_tests */
cannam@125: 
cannam@125: static	void
cannam@125: calc_peak_test (int filetype, const char *filename, int channels)
cannam@125: {	SNDFILE		*file ;
cannam@125: 	SF_INFO		sfinfo ;
cannam@125: 	char		label [128] ;
cannam@125: 	int			k, format ;
cannam@125: 	sf_count_t	buffer_len, frame_count ;
cannam@125: 	double		peak ;
cannam@125: 
cannam@125: 	snprintf (label, sizeof (label), "calc_peak_test (%d channels)", channels) ;
cannam@125: 	print_test_name (label, filename) ;
cannam@125: 
cannam@125: 	format = filetype | SF_FORMAT_PCM_16 ;
cannam@125: 
cannam@125: 	buffer_len = BUFFER_LEN - (BUFFER_LEN % channels) ;
cannam@125: 	frame_count = buffer_len / channels ;
cannam@125: 
cannam@125: 	sfinfo.samplerate	= 44100 ;
cannam@125: 	sfinfo.format		= format ;
cannam@125: 	sfinfo.channels		= channels ;
cannam@125: 	sfinfo.frames		= frame_count ;
cannam@125: 
cannam@125: 	/* Create double_data with max value of 0.5. */
cannam@125: 	for (k = 0 ; k < buffer_len ; k++)
cannam@125: 		double_data [k] = (k + 1) / (2.0 * buffer_len) ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 
cannam@125: 	test_writef_double_or_die (file, 0, double_data, frame_count, __LINE__) ;
cannam@125: 
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 
cannam@125: 	if (sfinfo.format != format)
cannam@125: 	{	printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (sfinfo.frames != frame_count)
cannam@125: 	{	printf ("\n\nLine %d: Incorrect number of frames in file. (%" PRId64 " => %" PRId64 ")\n", __LINE__, frame_count, sfinfo.frames) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (sfinfo.channels != channels)
cannam@125: 	{	printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	sf_command (file, SFC_CALC_SIGNAL_MAX, &peak, sizeof (peak)) ;
cannam@125: 	if (fabs (peak - (1 << 14)) > 1.0)
cannam@125: 	{	printf ("Line %d : Peak value should be %d (is %f).\n", __LINE__, (1 << 14), peak) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	sf_command (file, SFC_CALC_NORM_SIGNAL_MAX, &peak, sizeof (peak)) ;
cannam@125: 	if (fabs (peak - 0.5) > 4e-5)
cannam@125: 	{	printf ("Line %d : Peak value should be %f (is %f).\n", __LINE__, 0.5, peak) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	format = (filetype | SF_FORMAT_FLOAT) ;
cannam@125: 	sfinfo.samplerate	= 44100 ;
cannam@125: 	sfinfo.format		= format ;
cannam@125: 	sfinfo.channels		= channels ;
cannam@125: 	sfinfo.frames		= frame_count ;
cannam@125: 
cannam@125: 	/* Create double_data with max value of 0.5. */
cannam@125: 	for (k = 0 ; k < buffer_len ; k++)
cannam@125: 		double_data [k] = (k + 1) / (2.0 * buffer_len) ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 
cannam@125: 	test_writef_double_or_die (file, 0, double_data, frame_count, __LINE__) ;
cannam@125: 
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 
cannam@125: 	if (sfinfo.format != format)
cannam@125: 	{	printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (sfinfo.frames != frame_count)
cannam@125: 	{	printf ("\n\nLine %d: Incorrect number of.frames in file. (%" PRId64 " => %" PRId64 ")\n", __LINE__, frame_count, sfinfo.frames) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (sfinfo.channels != channels)
cannam@125: 	{	printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	sf_command (file, SFC_CALC_SIGNAL_MAX, &peak, sizeof (peak)) ;
cannam@125: 	if (fabs (peak - 0.5) > 1e-5)
cannam@125: 	{	printf ("Line %d : Peak value should be %f (is %f).\n", __LINE__, 0.5, peak) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	sf_command (file, SFC_CALC_NORM_SIGNAL_MAX, &peak, sizeof (peak)) ;
cannam@125: 	if (fabs (peak - 0.5) > 1e-5)
cannam@125: 	{	printf ("Line %d : Peak value should be %f (is %f).\n", __LINE__, 0.5, peak) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	unlink (filename) ;
cannam@125: 
cannam@125: 	printf ("ok\n") ;
cannam@125: } /* calc_peak_test */
cannam@125: 
cannam@125: static void
cannam@125: truncate_test (const char *filename, int filetype)
cannam@125: {	SNDFILE 	*file ;
cannam@125: 	SF_INFO		sfinfo ;
cannam@125: 	sf_count_t	len ;
cannam@125: 
cannam@125: 	print_test_name ("truncate_test", filename) ;
cannam@125: 
cannam@125: 	sfinfo.samplerate	= 11025 ;
cannam@125: 	sfinfo.format		= filetype ;
cannam@125: 	sfinfo.channels		= 2 ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 
cannam@125: 	test_write_int_or_die (file, 0, int_data, BUFFER_LEN, __LINE__) ;
cannam@125: 
cannam@125: 	len = 100 ;
cannam@125: 	if (sf_command (file, SFC_FILE_TRUNCATE, &len, sizeof (len)))
cannam@125: 	{	printf ("Line %d: sf_command (SFC_FILE_TRUNCATE) returned error.\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	test_seek_or_die (file, 0, SEEK_CUR, len, 2, __LINE__) ;
cannam@125: 	test_seek_or_die (file, 0, SEEK_END, len, 2, __LINE__) ;
cannam@125: 
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	unlink (filename) ;
cannam@125: 	puts ("ok") ;
cannam@125: } /* truncate_test */
cannam@125: 
cannam@125: /*------------------------------------------------------------------------------
cannam@125: */
cannam@125: 
cannam@125: static void
cannam@125: instrumet_rw_test (const char *filename)
cannam@125: {	SNDFILE *sndfile ;
cannam@125: 	SF_INFO sfinfo ;
cannam@125: 	SF_INSTRUMENT inst ;
cannam@125: 	memset (&sfinfo, 0, sizeof (SF_INFO)) ;
cannam@125: 
cannam@125: 	sndfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ;
cannam@125: 
cannam@125: 	if (sf_command (sndfile, SFC_GET_INSTRUMENT, &inst, sizeof (inst)) == SF_TRUE)
cannam@125: 	{	inst.basenote = 22 ;
cannam@125: 
cannam@125: 		if (sf_command (sndfile, SFC_SET_INSTRUMENT, &inst, sizeof (inst)) == SF_TRUE)
cannam@125: 			printf ("Sucess: [%s] updated\n", filename) ;
cannam@125: 		else
cannam@125: 			printf ("Error: SFC_SET_INSTRUMENT on [%s] [%s]\n", filename, sf_strerror (sndfile)) ;
cannam@125: 		}
cannam@125: 	else
cannam@125: 		printf ("Error: SFC_GET_INSTRUMENT on [%s] [%s]\n", filename, sf_strerror (sndfile)) ;
cannam@125: 
cannam@125: 
cannam@125: 	if (sf_command (sndfile, SFC_UPDATE_HEADER_NOW, NULL, 0) != 0)
cannam@125: 		printf ("Error: SFC_UPDATE_HEADER_NOW on [%s] [%s]\n", filename, sf_strerror (sndfile)) ;
cannam@125: 
cannam@125: 	sf_write_sync (sndfile) ;
cannam@125: 	sf_close (sndfile) ;
cannam@125: 
cannam@125: 	return ;
cannam@125: } /* instrumet_rw_test */
cannam@125: 
cannam@125: static void
cannam@125: instrument_test (const char *filename, int filetype)
cannam@125: {	static SF_INSTRUMENT write_inst =
cannam@125: 	{	2,		/* gain */
cannam@125: 		3, 		/* detune */
cannam@125: 		4, 		/* basenote */
cannam@125: 		5, 6,	/* key low and high */
cannam@125: 		7, 8,	/* velocity low and high */
cannam@125: 		2,		/* loop_count */
cannam@125: 		{	{	801, 2, 3, 0 },
cannam@125: 			{	801, 3, 4, 0 },
cannam@125: 		}
cannam@125: 	} ;
cannam@125: 	SF_INSTRUMENT read_inst ;
cannam@125: 	SNDFILE	*file ;
cannam@125: 	SF_INFO	sfinfo ;
cannam@125: 
cannam@125: 	print_test_name ("instrument_test", filename) ;
cannam@125: 
cannam@125: 	sfinfo.samplerate	= 11025 ;
cannam@125: 	sfinfo.format		= filetype ;
cannam@125: 	sfinfo.channels		= 1 ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	if (sf_command (file, SFC_SET_INSTRUMENT, &write_inst, sizeof (write_inst)) == SF_FALSE)
cannam@125: 	{	printf ("\n\nLine %d : sf_command (SFC_SET_INSTRUMENT) failed.\n\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 	test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	memset (&read_inst, 0, sizeof (read_inst)) ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	if (sf_command (file, SFC_GET_INSTRUMENT, &read_inst, sizeof (read_inst)) == SF_FALSE)
cannam@125: 	{	printf ("\n\nLine %d : sf_command (SFC_GET_INSTRUMENT) failed.\n\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		return ;
cannam@125: 		} ;
cannam@125: 	check_log_buffer_or_die (file, __LINE__) ;
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	if ((filetype & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV)
cannam@125: 	{	/*
cannam@125: 		**	For all the fields that WAV doesn't support, modify the
cannam@125: 		**	write_inst struct to hold the default value that the WAV
cannam@125: 		**	module should hold.
cannam@125: 		*/
cannam@125: 		write_inst.detune = 0 ;
cannam@125: 		write_inst.key_lo = write_inst.velocity_lo = 0 ;
cannam@125: 		write_inst.key_hi = write_inst.velocity_hi = 127 ;
cannam@125: 		write_inst.gain = 1 ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if ((filetype & SF_FORMAT_TYPEMASK) == SF_FORMAT_XI)
cannam@125: 	{	/*
cannam@125: 		**	For all the fields that XI doesn't support, modify the
cannam@125: 		**	write_inst struct to hold the default value that the XI
cannam@125: 		**	module should hold.
cannam@125: 		*/
cannam@125: 		write_inst.basenote = 0 ;
cannam@125: 		write_inst.detune = 0 ;
cannam@125: 		write_inst.key_lo = write_inst.velocity_lo = 0 ;
cannam@125: 		write_inst.key_hi = write_inst.velocity_hi = 127 ;
cannam@125: 		write_inst.gain = 1 ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (memcmp (&write_inst, &read_inst, sizeof (write_inst)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : instrument comparison failed.\n\n", __LINE__) ;
cannam@125: 		printf ("W  Base Note : %u\n"
cannam@125: 			"   Detune    : %u\n"
cannam@125: 			"   Low  Note : %u\tHigh Note : %u\n"
cannam@125: 			"   Low  Vel. : %u\tHigh Vel. : %u\n"
cannam@125: 			"   Gain      : %d\tCount     : %d\n"
cannam@125: 			"   mode      : %d\n"
cannam@125: 			"   start     : %d\tend       : %d\tcount  :%d\n"
cannam@125: 			"   mode      : %d\n"
cannam@125: 			"   start     : %d\tend       : %d\tcount  :%d\n\n",
cannam@125: 			write_inst.basenote,
cannam@125: 			write_inst.detune,
cannam@125: 			write_inst.key_lo, write_inst.key_hi,
cannam@125: 			write_inst.velocity_lo, write_inst.velocity_hi,
cannam@125: 			write_inst.gain, write_inst.loop_count,
cannam@125: 			write_inst.loops [0].mode, write_inst.loops [0].start,
cannam@125: 			write_inst.loops [0].end, write_inst.loops [0].count,
cannam@125: 			write_inst.loops [1].mode, write_inst.loops [1].start,
cannam@125: 			write_inst.loops [1].end, write_inst.loops [1].count) ;
cannam@125: 		printf ("R  Base Note : %u\n"
cannam@125: 			"   Detune    : %u\n"
cannam@125: 			"   Low  Note : %u\tHigh Note : %u\n"
cannam@125: 			"   Low  Vel. : %u\tHigh Vel. : %u\n"
cannam@125: 			"   Gain      : %d\tCount     : %d\n"
cannam@125: 			"   mode      : %d\n"
cannam@125: 			"   start     : %d\tend       : %d\tcount  :%d\n"
cannam@125: 			"   mode      : %d\n"
cannam@125: 			"   start     : %d\tend       : %d\tcount  :%d\n\n",
cannam@125: 			read_inst.basenote,
cannam@125: 			read_inst.detune,
cannam@125: 			read_inst.key_lo, read_inst.key_hi,
cannam@125: 			read_inst.velocity_lo, read_inst.velocity_hi,
cannam@125: 			read_inst.gain,	read_inst.loop_count,
cannam@125: 			read_inst.loops [0].mode, read_inst.loops [0].start,
cannam@125: 			read_inst.loops [0].end, read_inst.loops [0].count,
cannam@125: 			read_inst.loops [1].mode, read_inst.loops [1].start,
cannam@125: 			read_inst.loops [1].end, read_inst.loops [1].count) ;
cannam@125: 
cannam@125: 		if ((filetype & SF_FORMAT_TYPEMASK) != SF_FORMAT_XI)
cannam@125: 			exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (0) instrumet_rw_test (filename) ;
cannam@125: 
cannam@125: 	unlink (filename) ;
cannam@125: 	puts ("ok") ;
cannam@125: } /* instrument_test */
cannam@125: 
cannam@125: static void
cannam@125: cue_rw_test (const char *filename)
cannam@125: {	SNDFILE *sndfile ;
cannam@125: 	SF_INFO sfinfo ;
cannam@125: 	SF_CUES cues ;
cannam@125: 	memset (&sfinfo, 0, sizeof (SF_INFO)) ;
cannam@125: 
cannam@125: 	sndfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ;
cannam@125: 
cannam@125: 	exit_if_true (
cannam@125: 		sf_command (sndfile, SFC_GET_CUE_COUNT, &cues.cue_count, sizeof (cues.cue_count)) != SF_TRUE,
cannam@125: 		"\nLine %d: SFC_GET_CUE_COUNT command failed.\n\n", __LINE__
cannam@125: 		) ;
cannam@125: 
cannam@125: 	exit_if_true (
cannam@125: 		cues.cue_count != 3,
cannam@125: 		"\nLine %d: Expected cue_count (%u) to be 3.\n\n", __LINE__, cues.cue_count
cannam@125: 		) ;
cannam@125: 
cannam@125: 	if (sf_command (sndfile, SFC_GET_CUE, &cues, sizeof (cues)) == SF_TRUE)
cannam@125: 	{	cues.cue_points [1].sample_offset = 3 ;
cannam@125: 
cannam@125: 		if (sf_command (sndfile, SFC_SET_CUE, &cues, sizeof (cues)) == SF_TRUE)
cannam@125: 			printf ("Sucess: [%s] updated\n", filename) ;
cannam@125: 		else
cannam@125: 			printf ("Error: SFC_SET_CUE on [%s] [%s]\n", filename, sf_strerror (sndfile)) ;
cannam@125: 		}
cannam@125: 	else
cannam@125: 		printf ("Error: SFC_GET_CUE on [%s] [%s]\n", filename, sf_strerror (sndfile)) ;
cannam@125: 
cannam@125: 
cannam@125: 	if (sf_command (sndfile, SFC_UPDATE_HEADER_NOW, NULL, 0) != 0)
cannam@125: 		printf ("Error: SFC_UPDATE_HEADER_NOW on [%s] [%s]\n", filename, sf_strerror (sndfile)) ;
cannam@125: 
cannam@125: 	sf_write_sync (sndfile) ;
cannam@125: 	sf_close (sndfile) ;
cannam@125: 
cannam@125: 	return ;
cannam@125: } /* cue_rw_test */
cannam@125: 
cannam@125: static void
cannam@125: cue_test (const char *filename, int filetype)
cannam@125: {	SF_CUES write_cue ;
cannam@125: 	SF_CUES read_cue ;
cannam@125: 	SNDFILE	*file ;
cannam@125: 	SF_INFO	sfinfo ;
cannam@125: 
cannam@125: 	if (filetype == (SF_FORMAT_WAV | SF_FORMAT_PCM_16))
cannam@125: 	{	write_cue = (SF_CUES)
cannam@125: 		{	2,		/* cue_count */
cannam@125: 			{	{	1, 0, data_MARKER, 0, 0, 1, "" },
cannam@125: 				{	2, 0, data_MARKER, 0, 0, 2, "" },
cannam@125: 			}
cannam@125: 		} ;
cannam@125: 	}
cannam@125: 	else
cannam@125: 	{	write_cue = (SF_CUES)
cannam@125: 		{	2,		/* cue_count */
cannam@125: 			{	{	1, 0, data_MARKER, 0, 0, 1, "Cue1" },
cannam@125: 				{	2, 0, data_MARKER, 0, 0, 2, "Cue2" },
cannam@125: 			}
cannam@125: 		} ;
cannam@125: 	}
cannam@125: 
cannam@125: 	print_test_name ("cue_test", filename) ;
cannam@125: 
cannam@125: 	sfinfo.samplerate	= 11025 ;
cannam@125: 	sfinfo.format		= filetype ;
cannam@125: 	sfinfo.channels		= 1 ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	if (sf_command (file, SFC_SET_CUE, &write_cue, sizeof (write_cue)) == SF_FALSE)
cannam@125: 	{	printf ("\n\nLine %d : sf_command (SFC_SET_CUE) failed.\n\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 	test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	memset (&read_cue, 0, sizeof (read_cue)) ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	if (sf_command (file, SFC_GET_CUE, &read_cue, sizeof (read_cue)) == SF_FALSE)
cannam@125: 	{	printf ("\n\nLine %d : sf_command (SFC_GET_CUE) failed.\n\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		return ;
cannam@125: 		} ;
cannam@125: 	check_log_buffer_or_die (file, __LINE__) ;
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	if (memcmp (&write_cue, &read_cue, sizeof (write_cue)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : cue comparison failed.\n\n", __LINE__) ;
cannam@125: 		printf ("W  Cue count      : %d\n"
cannam@125: 			"   indx          : %d\n"
cannam@125: 			"   position      : %u\n"
cannam@125: 			"   fcc_chunk     : %x\n"
cannam@125: 			"   chunk_start   : %d\n"
cannam@125: 			"   block_start   : %d\n"
cannam@125: 			"   sample_offset : %u\n"
cannam@125: 			"   name          : %s\n"
cannam@125: 			"   indx          : %d\n"
cannam@125: 			"   position      : %u\n"
cannam@125: 			"   fcc_chunk     : %x\n"
cannam@125: 			"   chunk_start   : %d\n"
cannam@125: 			"   block_start   : %d\n"
cannam@125: 			"   sample_offset : %u\n"
cannam@125: 			"   name           : %s\n",
cannam@125: 			write_cue.cue_count,
cannam@125: 			write_cue.cue_points [0].indx,
cannam@125: 			write_cue.cue_points [0].position,
cannam@125: 			write_cue.cue_points [0].fcc_chunk,
cannam@125: 			write_cue.cue_points [0].chunk_start,
cannam@125: 			write_cue.cue_points [0].block_start,
cannam@125: 			write_cue.cue_points [0].sample_offset,
cannam@125: 			write_cue.cue_points [0].name,
cannam@125: 			write_cue.cue_points [1].indx,
cannam@125: 			write_cue.cue_points [1].position,
cannam@125: 			write_cue.cue_points [1].fcc_chunk,
cannam@125: 			write_cue.cue_points [1].chunk_start,
cannam@125: 			write_cue.cue_points [1].block_start,
cannam@125: 			write_cue.cue_points [1].sample_offset,
cannam@125: 			write_cue.cue_points [1].name) ;
cannam@125: 		printf ("R  Cue count      : %d\n"
cannam@125: 			"   indx          : %d\n"
cannam@125: 			"   position      : %u\n"
cannam@125: 			"   fcc_chunk     : %x\n"
cannam@125: 			"   chunk_start   : %d\n"
cannam@125: 			"   block_start   : %d\n"
cannam@125: 			"   sample_offset : %u\n"
cannam@125: 			"   name          : %s\n"
cannam@125: 			"   indx          : %d\n"
cannam@125: 			"   position      : %u\n"
cannam@125: 			"   fcc_chunk     : %x\n"
cannam@125: 			"   chunk_start   : %d\n"
cannam@125: 			"   block_start   : %d\n"
cannam@125: 			"   sample_offset : %u\n"
cannam@125: 			"   name          : %s\n",
cannam@125: 			read_cue.cue_count,
cannam@125: 			read_cue.cue_points [0].indx,
cannam@125: 			read_cue.cue_points [0].position,
cannam@125: 			read_cue.cue_points [0].fcc_chunk,
cannam@125: 			read_cue.cue_points [0].chunk_start,
cannam@125: 			read_cue.cue_points [0].block_start,
cannam@125: 			read_cue.cue_points [0].sample_offset,
cannam@125: 			read_cue.cue_points [0].name,
cannam@125: 			read_cue.cue_points [1].indx,
cannam@125: 			read_cue.cue_points [1].position,
cannam@125: 			read_cue.cue_points [1].fcc_chunk,
cannam@125: 			read_cue.cue_points [1].chunk_start,
cannam@125: 			read_cue.cue_points [1].block_start,
cannam@125: 			read_cue.cue_points [1].sample_offset,
cannam@125: 			read_cue.cue_points [1].name) ;
cannam@125: 
cannam@125: 			exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (0) cue_rw_test (filename) ;
cannam@125: 
cannam@125: 	unlink (filename) ;
cannam@125: 	puts ("ok") ;
cannam@125: } /* cue_test */
cannam@125: 
cannam@125: static	void
cannam@125: current_sf_info_test	(const char *filename)
cannam@125: {	SNDFILE *outfile, *infile ;
cannam@125: 	SF_INFO outinfo, ininfo ;
cannam@125: 
cannam@125: 	print_test_name ("current_sf_info_test", filename) ;
cannam@125: 
cannam@125: 	outinfo.samplerate	= 44100 ;
cannam@125: 	outinfo.format		= (SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
cannam@125: 	outinfo.channels	= 1 ;
cannam@125: 	outinfo.frames		= 0 ;
cannam@125: 
cannam@125: 	outfile = test_open_file_or_die (filename, SFM_WRITE, &outinfo, SF_TRUE, __LINE__) ;
cannam@125: 	sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, 0) ;
cannam@125: 
cannam@125: 	exit_if_true (outinfo.frames != 0,
cannam@125: 		"\n\nLine %d : Initial sfinfo.frames is not zero.\n\n", __LINE__
cannam@125: 		) ;
cannam@125: 
cannam@125: 	test_write_double_or_die (outfile, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@125: 	sf_command (outfile, SFC_GET_CURRENT_SF_INFO, &outinfo, sizeof (outinfo)) ;
cannam@125: 
cannam@125: 	exit_if_true (outinfo.frames != BUFFER_LEN,
cannam@125: 		"\n\nLine %d : Initial sfinfo.frames (%" PRId64 ") should be %d.\n\n", __LINE__,
cannam@125: 		outinfo.frames, BUFFER_LEN
cannam@125: 		) ;
cannam@125: 
cannam@125: 	/* Read file making sure no channel map exists. */
cannam@125: 	memset (&ininfo, 0, sizeof (ininfo)) ;
cannam@125: 	infile = test_open_file_or_die (filename, SFM_READ, &ininfo, SF_TRUE, __LINE__) ;
cannam@125: 
cannam@125: 	test_write_double_or_die (outfile, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@125: 
cannam@125: 	sf_command (infile, SFC_GET_CURRENT_SF_INFO, &ininfo, sizeof (ininfo)) ;
cannam@125: 
cannam@125: 	exit_if_true (ininfo.frames != BUFFER_LEN,
cannam@125: 		"\n\nLine %d : Initial sfinfo.frames (%" PRId64 ") should be %d.\n\n", __LINE__,
cannam@125: 		ininfo.frames, BUFFER_LEN
cannam@125: 		) ;
cannam@125: 
cannam@125: 	sf_close (outfile) ;
cannam@125: 	sf_close (infile) ;
cannam@125: 
cannam@125: 	unlink (filename) ;
cannam@125: 	puts ("ok") ;
cannam@125: } /* current_sf_info_test */
cannam@125: 
cannam@125: static void
cannam@125: broadcast_test (const char *filename, int filetype)
cannam@125: {	static SF_BROADCAST_INFO bc_write, bc_read ;
cannam@125: 	SNDFILE	*file ;
cannam@125: 	SF_INFO	sfinfo ;
cannam@125: 	int errors = 0 ;
cannam@125: 
cannam@125: 	print_test_name ("broadcast_test", filename) ;
cannam@125: 
cannam@125: 	sfinfo.samplerate	= 11025 ;
cannam@125: 	sfinfo.format		= filetype ;
cannam@125: 	sfinfo.channels		= 1 ;
cannam@125: 
cannam@125: 	memset (&bc_write, 0, sizeof (bc_write)) ;
cannam@125: 
cannam@125: 	snprintf (bc_write.description, sizeof (bc_write.description), "Test description") ;
cannam@125: 	snprintf (bc_write.originator, sizeof (bc_write.originator), "Test originator") ;
cannam@125: 	snprintf (bc_write.originator_reference, sizeof (bc_write.originator_reference), "%08x-%08x", (unsigned int) time (NULL), (unsigned int) (~ time (NULL))) ;
cannam@125: 	snprintf (bc_write.origination_date, sizeof (bc_write.origination_date), "%d/%02d/%02d", 2006, 3, 30) ;
cannam@125: 	snprintf (bc_write.origination_time, sizeof (bc_write.origination_time), "%02d:%02d:%02d", 20, 27, 0) ;
cannam@125: 	snprintf (bc_write.umid, sizeof (bc_write.umid), "Some umid") ;
cannam@125: 	bc_write.coding_history_size = 0 ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	if (sf_command (file, SFC_SET_BROADCAST_INFO, &bc_write, sizeof (bc_write)) == SF_FALSE)
cannam@125: 	{	printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 	test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	memset (&bc_read, 0, sizeof (bc_read)) ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	if (sf_command (file, SFC_GET_BROADCAST_INFO, &bc_read, sizeof (bc_read)) == SF_FALSE)
cannam@125: 	{	printf ("\n\nLine %d : sf_command (SFC_GET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		return ;
cannam@125: 		} ;
cannam@125: 	check_log_buffer_or_die (file, __LINE__) ;
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	if (bc_read.version != 1)
cannam@125: 	{	printf ("\n\nLine %d : Read bad version number %d.\n\n", __LINE__, bc_read.version) ;
cannam@125: 		exit (1) ;
cannam@125: 		return ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	bc_read.version = bc_write.version = 0 ;
cannam@125: 
cannam@125: 	if (memcmp (bc_write.description, bc_read.description, sizeof (bc_write.description)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : description mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, bc_write.description, bc_read.description) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (memcmp (bc_write.originator, bc_read.originator, sizeof (bc_write.originator)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : originator mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, bc_write.originator, bc_read.originator) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (memcmp (bc_write.originator_reference, bc_read.originator_reference, sizeof (bc_write.originator_reference)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : originator_reference mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, bc_write.originator_reference, bc_read.originator_reference) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (memcmp (bc_write.origination_date, bc_read.origination_date, sizeof (bc_write.origination_date)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : origination_date mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, bc_write.origination_date, bc_read.origination_date) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (memcmp (bc_write.origination_time, bc_read.origination_time, sizeof (bc_write.origination_time)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : origination_time mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, bc_write.origination_time, bc_read.origination_time) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (memcmp (bc_write.umid, bc_read.umid, sizeof (bc_write.umid)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : umid mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, bc_write.umid, bc_read.umid) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (errors)
cannam@125: 		exit (1) ;
cannam@125: 
cannam@125: 	unlink (filename) ;
cannam@125: 	puts ("ok") ;
cannam@125: } /* broadcast_test */
cannam@125: 
cannam@125: static	void
cannam@125: broadcast_rdwr_test (const char *filename, int filetype)
cannam@125: {	SF_BROADCAST_INFO binfo ;
cannam@125: 	SNDFILE *file ;
cannam@125: 	SF_INFO sfinfo ;
cannam@125: 	sf_count_t frames ;
cannam@125: 
cannam@125: 	print_test_name (__func__, filename) ;
cannam@125: 
cannam@125: 	create_short_sndfile (filename, filetype, 2) ;
cannam@125: 
cannam@125: 	memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@125: 	memset (&binfo, 0, sizeof (binfo)) ;
cannam@125: 
cannam@125: 	snprintf (binfo.description, sizeof (binfo.description), "Test description") ;
cannam@125: 	snprintf (binfo.originator, sizeof (binfo.originator), "Test originator") ;
cannam@125: 	snprintf (binfo.originator_reference, sizeof (binfo.originator_reference), "%08x-%08x", (unsigned int) time (NULL), (unsigned int) (~ time (NULL))) ;
cannam@125: 	snprintf (binfo.origination_date, sizeof (binfo.origination_date), "%d/%02d/%02d", 2006, 3, 30) ;
cannam@125: 	snprintf (binfo.origination_time, sizeof (binfo.origination_time), "%02d:%02d:%02d", 20, 27, 0) ;
cannam@125: 	snprintf (binfo.umid, sizeof (binfo.umid), "Some umid") ;
cannam@125: 	binfo.coding_history_size = 0 ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	frames = sfinfo.frames ;
cannam@125: 	if (sf_command (file, SFC_SET_BROADCAST_INFO, &binfo, sizeof (binfo)) != SF_FALSE)
cannam@125: 	{	printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) should have failed but didn't.\n\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	sf_close (file) ;
cannam@125: 	exit_if_true (frames != sfinfo.frames, "\n\nLine %d : Frame count %" PRId64 " should be %" PRId64 ".\n", __LINE__, sfinfo.frames, frames) ;
cannam@125: 
cannam@125: 	unlink (filename) ;
cannam@125: 	puts ("ok") ;
cannam@125: } /* broadcast_rdwr_test */
cannam@125: 
cannam@125: static void
cannam@125: check_coding_history_newlines (const char *filename)
cannam@125: {	static SF_BROADCAST_INFO bc_write, bc_read ;
cannam@125: 	SNDFILE	*file ;
cannam@125: 	SF_INFO	sfinfo ;
cannam@125: 	unsigned k ;
cannam@125: 
cannam@125: 	sfinfo.samplerate	= 22050 ;
cannam@125: 	sfinfo.format		= SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
cannam@125: 	sfinfo.channels		= 1 ;
cannam@125: 
cannam@125: 	memset (&bc_write, 0, sizeof (bc_write)) ;
cannam@125: 
cannam@125: 	snprintf (bc_write.description, sizeof (bc_write.description), "Test description") ;
cannam@125: 	snprintf (bc_write.originator, sizeof (bc_write.originator), "Test originator") ;
cannam@125: 	snprintf (bc_write.originator_reference, sizeof (bc_write.originator_reference), "%08x-%08x", (unsigned int) time (NULL), (unsigned int) (~ time (NULL))) ;
cannam@125: 	snprintf (bc_write.origination_date, sizeof (bc_write.origination_date), "%d/%02d/%02d", 2006, 3, 30) ;
cannam@125: 	snprintf (bc_write.origination_time, sizeof (bc_write.origination_time), "%02d:%02d:%02d", 20, 27, 0) ;
cannam@125: 	snprintf (bc_write.umid, sizeof (bc_write.umid), "Some umid") ;
cannam@125: 	bc_write.coding_history_size = snprintf (bc_write.coding_history, sizeof (bc_write.coding_history), "This has\nUnix\nand\rMac OS9\rline endings.\nLast line") ; ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	if (sf_command (file, SFC_SET_BROADCAST_INFO, &bc_write, sizeof (bc_write)) == SF_FALSE)
cannam@125: 	{	printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	memset (&bc_read, 0, sizeof (bc_read)) ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	if (sf_command (file, SFC_GET_BROADCAST_INFO, &bc_read, sizeof (bc_read)) == SF_FALSE)
cannam@125: 	{	printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 	check_log_buffer_or_die (file, __LINE__) ;
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	if (bc_read.coding_history_size == 0)
cannam@125: 	{	printf ("\n\nLine %d : missing coding history.\n\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (strstr (bc_read.coding_history, "Last line") == NULL)
cannam@125: 	{	printf ("\n\nLine %d : coding history truncated.\n\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	for (k = 1 ; k < bc_read.coding_history_size ; k++)
cannam@125: 	{	if (bc_read.coding_history [k] == '\n' && bc_read.coding_history [k - 1] != '\r')
cannam@125: 		{	printf ("\n\nLine %d : '\\n' without '\\r' before.\n\n", __LINE__) ;
cannam@125: 			exit (1) ;
cannam@125: 			} ;
cannam@125: 
cannam@125: 		if (bc_read.coding_history [k] == '\r' && bc_read.coding_history [k + 1] != '\n')
cannam@125: 		{	printf ("\n\nLine %d : '\\r' without '\\n' after.\n\n", __LINE__) ;
cannam@125: 			exit (1) ;
cannam@125: 			} ;
cannam@125: 
cannam@125: 		if (bc_read.coding_history [k] == 0 && k < bc_read.coding_history_size - 1)
cannam@125: 		{	printf ("\n\nLine %d : '\\0' within coding history at index %d of %d.\n\n", __LINE__, k, bc_read.coding_history_size) ;
cannam@125: 			exit (1) ;
cannam@125: 			} ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	return ;
cannam@125: } /* check_coding_history_newlines */
cannam@125: 
cannam@125: static void
cannam@125: broadcast_coding_history_test (const char *filename)
cannam@125: {	static SF_BROADCAST_INFO bc_write, bc_read ;
cannam@125: 	SNDFILE	*file ;
cannam@125: 	SF_INFO	sfinfo ;
cannam@125: 	const char *default_history = "A=PCM,F=22050,W=16,M=mono" ;
cannam@125: 	const char *supplied_history =
cannam@125: 					"A=PCM,F=44100,W=24,M=mono,T=other\r\n"
cannam@125: 					"A=PCM,F=22050,W=16,M=mono,T=yet_another\r\n" ;
cannam@125: 
cannam@125: 	print_test_name ("broadcast_coding_history_test", filename) ;
cannam@125: 
cannam@125: 	sfinfo.samplerate	= 22050 ;
cannam@125: 	sfinfo.format		= SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
cannam@125: 	sfinfo.channels		= 1 ;
cannam@125: 
cannam@125: 	memset (&bc_write, 0, sizeof (bc_write)) ;
cannam@125: 
cannam@125: 	snprintf (bc_write.description, sizeof (bc_write.description), "Test description") ;
cannam@125: 	snprintf (bc_write.originator, sizeof (bc_write.originator), "Test originator") ;
cannam@125: 	snprintf (bc_write.originator_reference, sizeof (bc_write.originator_reference), "%08x-%08x", (unsigned int) time (NULL), (unsigned int) (~ time (NULL))) ;
cannam@125: 	snprintf (bc_write.origination_date, sizeof (bc_write.origination_date), "%d/%02d/%02d", 2006, 3, 30) ;
cannam@125: 	snprintf (bc_write.origination_time, sizeof (bc_write.origination_time), "%02d:%02d:%02d", 20, 27, 0) ;
cannam@125: 	snprintf (bc_write.umid, sizeof (bc_write.umid), "Some umid") ;
cannam@125: 	/* Coding history will be filled in by the library. */
cannam@125: 	bc_write.coding_history_size = 0 ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	if (sf_command (file, SFC_SET_BROADCAST_INFO, &bc_write, sizeof (bc_write)) == SF_FALSE)
cannam@125: 	{	printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	memset (&bc_read, 0, sizeof (bc_read)) ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	if (sf_command (file, SFC_GET_BROADCAST_INFO, &bc_read, sizeof (bc_read)) == SF_FALSE)
cannam@125: 	{	printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 	check_log_buffer_or_die (file, __LINE__) ;
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	if (bc_read.coding_history_size == 0)
cannam@125: 	{	printf ("\n\nLine %d : missing coding history.\n\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (bc_read.coding_history_size < strlen (default_history) || memcmp (bc_read.coding_history, default_history, strlen (default_history)) != 0)
cannam@125: 	{	printf ("\n\n"
cannam@125: 				"Line %d : unexpected coding history '%.*s',\n"
cannam@125: 				"            should be '%s'\n\n", __LINE__, bc_read.coding_history_size, bc_read.coding_history, default_history) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	bc_write.coding_history_size = strlen (supplied_history) ;
cannam@125: 	bc_write.coding_history_size = snprintf (bc_write.coding_history, sizeof (bc_write.coding_history), "%s", supplied_history) ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	if (sf_command (file, SFC_SET_BROADCAST_INFO, &bc_write, sizeof (bc_write)) == SF_FALSE)
cannam@125: 	{	printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	memset (&bc_read, 0, sizeof (bc_read)) ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	if (sf_command (file, SFC_GET_BROADCAST_INFO, &bc_read, sizeof (bc_read)) == SF_FALSE)
cannam@125: 	{	printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	check_log_buffer_or_die (file, __LINE__) ;
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	if (strstr (bc_read.coding_history, supplied_history) != bc_read.coding_history)
cannam@125: 	{	printf ("\n\nLine %d : unexpected coding history :\n"
cannam@125: 				"----------------------------------------------------\n%s"
cannam@125: 				"----------------------------------------------------\n"
cannam@125: 				"should be this :\n"
cannam@125: 				"----------------------------------------------------\n%s"
cannam@125: 				"----------------------------------------------------\n"
cannam@125: 				"with one more line at the end.\n\n",
cannam@125: 				__LINE__, bc_read.coding_history, supplied_history) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	check_coding_history_newlines (filename) ;
cannam@125: 
cannam@125: 	unlink (filename) ;
cannam@125: 	puts ("ok") ;
cannam@125: } /* broadcast_coding_history_test */
cannam@125: 
cannam@125: /*==============================================================================
cannam@125: */
cannam@125: 
cannam@125: static void
cannam@125: broadcast_coding_history_size (const char *filename)
cannam@125: {	/* SF_BROADCAST_INFO struct with coding_history field of 1024 bytes. */
cannam@125: 	static SF_BROADCAST_INFO_VAR (1024) bc_write ;
cannam@125: 	static SF_BROADCAST_INFO_VAR (1024) bc_read ;
cannam@125: 	SNDFILE	*file ;
cannam@125: 	SF_INFO	sfinfo ;
cannam@125: 	int k ;
cannam@125: 
cannam@125: 	print_test_name (__func__, filename) ;
cannam@125: 
cannam@125: 	sfinfo.samplerate	= 22050 ;
cannam@125: 	sfinfo.format		= SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
cannam@125: 	sfinfo.channels		= 1 ;
cannam@125: 
cannam@125: 	memset (&bc_write, 0, sizeof (bc_write)) ;
cannam@125: 
cannam@125: 	snprintf (bc_write.description, sizeof (bc_write.description), "Test description") ;
cannam@125: 	snprintf (bc_write.originator, sizeof (bc_write.originator), "Test originator") ;
cannam@125: 	snprintf (bc_write.originator_reference, sizeof (bc_write.originator_reference), "%08x-%08x", (unsigned int) time (NULL), (unsigned int) (~ time (NULL))) ;
cannam@125: 	snprintf (bc_write.origination_date, sizeof (bc_write.origination_date), "%d/%02d/%02d", 2006, 3, 30) ;
cannam@125: 	snprintf (bc_write.origination_time, sizeof (bc_write.origination_time), "%02d:%02d:%02d", 20, 27, 0) ;
cannam@125: 	snprintf (bc_write.umid, sizeof (bc_write.umid), "Some umid") ;
cannam@125: 	bc_write.coding_history_size = 0 ;
cannam@125: 
cannam@125: 	for (k = 0 ; bc_write.coding_history_size < 512 ; k++)
cannam@125: 	{	snprintf (bc_write.coding_history + bc_write.coding_history_size,
cannam@125: 			sizeof (bc_write.coding_history) - bc_write.coding_history_size, "line %4d\n", k) ;
cannam@125: 		bc_write.coding_history_size = strlen (bc_write.coding_history) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	exit_if_true (bc_write.coding_history_size < 512,
cannam@125: 			"\n\nLine %d : bc_write.coding_history_size (%d) should be > 512.\n\n", __LINE__, bc_write.coding_history_size) ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	if (sf_command (file, SFC_SET_BROADCAST_INFO, &bc_write, sizeof (bc_write)) == SF_FALSE)
cannam@125: 	{	printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	memset (&bc_read, 0, sizeof (bc_read)) ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	if (sf_command (file, SFC_GET_BROADCAST_INFO, &bc_read, sizeof (bc_read)) == SF_FALSE)
cannam@125: 	{	printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 	check_log_buffer_or_die (file, __LINE__) ;
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	exit_if_true (bc_read.coding_history_size < 512,
cannam@125: 			"\n\nLine %d : unexpected coding history size %d (should be > 512).\n\n", __LINE__, bc_read.coding_history_size) ;
cannam@125: 
cannam@125: 	exit_if_true (strstr (bc_read.coding_history, "libsndfile") == NULL,
cannam@125: 			"\n\nLine %d : coding history incomplete (should contain 'libsndfile').\n\n", __LINE__) ;
cannam@125: 
cannam@125: 	unlink (filename) ;
cannam@125: 	puts ("ok") ;
cannam@125: } /* broadcast_coding_history_size */
cannam@125: 
cannam@125: /*==============================================================================
cannam@125: */
cannam@125: static void
cannam@125: cart_test (const char *filename, int filetype)
cannam@125: {	static SF_CART_INFO ca_write, ca_read ;
cannam@125: 	SNDFILE	*file ;
cannam@125: 	SF_INFO	sfinfo ;
cannam@125: 	int errors = 0 ;
cannam@125: 
cannam@125: 	print_test_name ("cart_test", filename) ;
cannam@125: 
cannam@125: 	sfinfo.samplerate	= 11025 ;
cannam@125: 	sfinfo.format		= filetype ;
cannam@125: 	sfinfo.channels		= 1 ;
cannam@125: 	memset (&ca_write, 0, sizeof (ca_write)) ;
cannam@125: 
cannam@125: 	// example test data
cannam@125: 	snprintf (ca_write.artist, sizeof (ca_write.artist), "Test artist") ;
cannam@125: 	snprintf (ca_write.version, sizeof (ca_write.version), "Test version") ;
cannam@125: 	snprintf (ca_write.cut_id, sizeof (ca_write.cut_id), "Test cut ID") ;
cannam@125: 	snprintf (ca_write.client_id, sizeof (ca_write.client_id), "Test client ID") ;
cannam@125: 	snprintf (ca_write.category, sizeof (ca_write.category), "Test category") ;
cannam@125: 	snprintf (ca_write.classification, sizeof (ca_write.classification), "Test classification") ;
cannam@125: 	snprintf (ca_write.out_cue, sizeof (ca_write.out_cue), "Test out cue") ;
cannam@125: 	snprintf (ca_write.start_date, sizeof (ca_write.start_date), "%d/%02d/%02d", 2006, 3, 30) ;
cannam@125: 	snprintf (ca_write.start_time, sizeof (ca_write.start_time), "%02d:%02d:%02d", 20, 27, 0) ;
cannam@125: 	snprintf (ca_write.end_date, sizeof (ca_write.end_date), "%d/%02d/%02d", 2006, 3, 30) ;
cannam@125: 	snprintf (ca_write.end_time, sizeof (ca_write.end_time), "%02d:%02d:%02d", 20, 27, 0) ;
cannam@125: 	snprintf (ca_write.producer_app_id, sizeof (ca_write.producer_app_id), "Test producer app id") ;
cannam@125: 	snprintf (ca_write.producer_app_version, sizeof (ca_write.producer_app_version), "Test producer app version") ;
cannam@125: 	snprintf (ca_write.user_def, sizeof (ca_write.user_def), "test user def test test") ;
cannam@125: 	ca_write.level_reference = 42 ;
cannam@125: 	snprintf (ca_write.url, sizeof (ca_write.url), "http://www.test.com/test_url") ;
cannam@125: 	snprintf (ca_write.tag_text, sizeof (ca_write.tag_text), "tag text test! \r\n") ; // must be terminated \r\n to be valid
cannam@125: 	ca_write.tag_text_size = strlen (ca_write.tag_text) ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	if (sf_command (file, SFC_SET_CART_INFO, &ca_write, sizeof (ca_write)) == SF_FALSE)
cannam@125: 		exit (1) ;
cannam@125: 
cannam@125: 	test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	memset (&ca_read, 0, sizeof (ca_read)) ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	if (sf_command (file, SFC_GET_CART_INFO, &ca_read, sizeof (ca_read)) == SF_FALSE)
cannam@125: 	{	printf ("\n\nLine %d : sf_command (SFC_GET_CART_INFO) failed.\n\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		return ;
cannam@125: 		} ;
cannam@125: 	check_log_buffer_or_die (file, __LINE__) ;
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 
cannam@125: 	if (memcmp (ca_write.artist, ca_read.artist, sizeof (ca_write.artist)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : artist mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, ca_write.artist, ca_read.artist) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (memcmp (ca_write.version, ca_read.version, sizeof (ca_write.version)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : version mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, ca_write.version, ca_read.version) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (memcmp (ca_write.title, ca_read.title, sizeof (ca_write.title)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : title mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, ca_write.title, ca_read.title) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (memcmp (ca_write.cut_id, ca_read.cut_id, sizeof (ca_write.cut_id)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : cut_id mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, ca_write.cut_id, ca_read.cut_id) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (memcmp (ca_write.client_id, ca_read.client_id, sizeof (ca_write.client_id)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : client_id mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, ca_write.client_id, ca_read.client_id) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (memcmp (ca_write.category, ca_read.category, sizeof (ca_write.category)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : category mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, ca_write.category, ca_read.category) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (memcmp (ca_write.out_cue, ca_read.out_cue, sizeof (ca_write.out_cue)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : out_cue mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, ca_write.out_cue, ca_read.out_cue) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (memcmp (ca_write.start_date, ca_read.start_date, sizeof (ca_write.start_date)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : start_date mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, ca_write.start_date, ca_read.start_date) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 
cannam@125: 	if (memcmp (ca_write.start_time, ca_read.start_time, sizeof (ca_write.start_time)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : start_time mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, ca_write.start_time, ca_read.start_time) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 
cannam@125: 	if (memcmp (ca_write.end_date, ca_read.end_date, sizeof (ca_write.end_date)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : end_date mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, ca_write.end_date, ca_read.end_date) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 
cannam@125: 	if (memcmp (ca_write.end_time, ca_read.end_time, sizeof (ca_write.end_time)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : end_time mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, ca_write.end_time, ca_read.end_time) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 
cannam@125: 	if (memcmp (ca_write.producer_app_id, ca_read.producer_app_id, sizeof (ca_write.producer_app_id)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : producer_app_id mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, ca_write.producer_app_id, ca_read.producer_app_id) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 
cannam@125: 	if (memcmp (ca_write.producer_app_version, ca_read.producer_app_version, sizeof (ca_write.producer_app_version)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : producer_app_version mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, ca_write.producer_app_version, ca_read.producer_app_version) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 
cannam@125: 	if (memcmp (ca_write.user_def, ca_read.user_def, sizeof (ca_write.user_def)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : user_def mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, ca_write.user_def, ca_read.user_def) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 
cannam@125: 	if (ca_write.level_reference != ca_read.level_reference)
cannam@125: 	{	printf ("\n\nLine %d : level_reference mismatch :\n\twrite : '%d'\n\tread  : '%d'\n\n", __LINE__, ca_write.level_reference, ca_read.level_reference) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	// TODO: make this more helpful
cannam@125: 	if (memcmp (ca_write.post_timers, ca_read.post_timers, sizeof (ca_write.post_timers)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : post_timers mismatch :\n'\n\n", __LINE__) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	if (memcmp (ca_write.url, ca_read.url, sizeof (ca_write.url)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : url mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, ca_write.url, ca_read.url) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 
cannam@125: 	if (memcmp (ca_write.tag_text, ca_read.tag_text, (size_t) (ca_read.tag_text_size)) != 0)
cannam@125: 	{	printf ("\n\nLine %d : tag_text mismatch :\n\twrite : '%s'\n\tread  : '%s'\n\n", __LINE__, ca_write.tag_text, ca_read.tag_text) ;
cannam@125: 		errors ++ ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 
cannam@125: 	if (errors)
cannam@125: 		exit (1) ;
cannam@125: 
cannam@125: 	unlink (filename) ;
cannam@125: 	puts ("ok") ;
cannam@125: } /* cart_test */
cannam@125: 
cannam@125: static	void
cannam@125: cart_rdwr_test (const char *filename, int filetype)
cannam@125: {	SF_CART_INFO cinfo ;
cannam@125: 	SNDFILE *file ;
cannam@125: 	SF_INFO sfinfo ;
cannam@125: 	sf_count_t frames ;
cannam@125: 
cannam@125: 	print_test_name (__func__, filename) ;
cannam@125: 
cannam@125: 	create_short_sndfile (filename, filetype, 2) ;
cannam@125: 
cannam@125: 	memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@125: 	memset (&cinfo, 0, sizeof (cinfo)) ;
cannam@125: 
cannam@125: 	snprintf (cinfo.artist, sizeof (cinfo.artist), "Test artist") ;
cannam@125: 	snprintf (cinfo.version, sizeof (cinfo.version), "Test version") ;
cannam@125: 	snprintf (cinfo.cut_id, sizeof (cinfo.cut_id), "Test cut ID") ;
cannam@125: 	snprintf (cinfo.client_id, sizeof (cinfo.client_id), "Test client ID") ;
cannam@125: 	snprintf (cinfo.category, sizeof (cinfo.category), "Test category") ;
cannam@125: 	snprintf (cinfo.classification, sizeof (cinfo.classification), "Test classification") ;
cannam@125: 	snprintf (cinfo.out_cue, sizeof (cinfo.out_cue), "Test out cue") ;
cannam@125: 	snprintf (cinfo.start_date, sizeof (cinfo.start_date), "%d/%02d/%02d", 2006, 3, 30) ;
cannam@125: 	snprintf (cinfo.start_time, sizeof (cinfo.start_time), "%02d:%02d:%02d", 20, 27, 0) ;
cannam@125: 	snprintf (cinfo.end_date, sizeof (cinfo.end_date), "%d/%02d/%02d", 2006, 3, 30) ;
cannam@125: 	snprintf (cinfo.end_time, sizeof (cinfo.end_time), "%02d:%02d:%02d", 20, 27, 0) ;
cannam@125: 	snprintf (cinfo.producer_app_id, sizeof (cinfo.producer_app_id), "Test producer app id") ;
cannam@125: 	snprintf (cinfo.producer_app_version, sizeof (cinfo.producer_app_version), "Test producer app version") ;
cannam@125: 	snprintf (cinfo.user_def, sizeof (cinfo.user_def), "test user def test test") ;
cannam@125: 	cinfo.level_reference = 42 ;
cannam@125: 	snprintf (cinfo.url, sizeof (cinfo.url), "http://www.test.com/test_url") ;
cannam@125: 	snprintf (cinfo.tag_text, sizeof (cinfo.tag_text), "tag text test!\r\n") ;
cannam@125: 	cinfo.tag_text_size = strlen (cinfo.tag_text) ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	frames = sfinfo.frames ;
cannam@125: 	if (sf_command (file, SFC_SET_CART_INFO, &cinfo, sizeof (cinfo)) != SF_FALSE)
cannam@125: 	{	printf ("\n\nLine %d : sf_command (SFC_SET_CART_INFO) should have failed but didn't.\n\n", __LINE__) ;
cannam@125: 		exit (1) ;
cannam@125: 		} ;
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	sf_close (file) ;
cannam@125: 	exit_if_true (frames != sfinfo.frames, "\n\nLine %d : Frame count %" PRId64 " should be %" PRId64 ".\n", __LINE__, sfinfo.frames, frames) ;
cannam@125: 
cannam@125: 	unlink (filename) ;
cannam@125: 	puts ("ok") ;
cannam@125: } /* cart_rdwr_test */
cannam@125: 
cannam@125: /*==============================================================================
cannam@125: */
cannam@125: 
cannam@125: static	void
cannam@125: channel_map_test (const char *filename, int filetype)
cannam@125: {	SNDFILE	*file ;
cannam@125: 	SF_INFO	sfinfo ;
cannam@125: 	int channel_map_read [4], channel_map_write [4] =
cannam@125: 	{	SF_CHANNEL_MAP_LEFT, SF_CHANNEL_MAP_RIGHT, SF_CHANNEL_MAP_LFE,
cannam@125: 		SF_CHANNEL_MAP_REAR_CENTER
cannam@125: 		} ;
cannam@125: 
cannam@125: 	print_test_name ("channel_map_test", filename) ;
cannam@125: 
cannam@125: 	memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@125: 	sfinfo.samplerate	= 11025 ;
cannam@125: 	sfinfo.format		= filetype ;
cannam@125: 	sfinfo.channels		= ARRAY_LEN (channel_map_read) ;
cannam@125: 
cannam@125: 	switch (filetype & SF_FORMAT_TYPEMASK)
cannam@125: 	{	/* WAVEX and RF64 have a default channel map, even if you don't specify one. */
cannam@125: 		case SF_FORMAT_WAVEX :
cannam@125: 		case SF_FORMAT_RF64 :
cannam@125: 			/* Write file without channel map. */
cannam@125: 			file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 			test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@125: 			sf_close (file) ;
cannam@125: 
cannam@125: 			/* Read file making default channel map exists. */
cannam@125: 			file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 			exit_if_true (
cannam@125: 				sf_command (file, SFC_GET_CHANNEL_MAP_INFO, channel_map_read, sizeof (channel_map_read)) == SF_FALSE,
cannam@125: 				"\n\nLine %d : sf_command (SFC_GET_CHANNEL_MAP_INFO) should not have failed.\n\n", __LINE__
cannam@125: 				) ;
cannam@125: 			check_log_buffer_or_die (file, __LINE__) ;
cannam@125: 			sf_close (file) ;
cannam@125: 			break ;
cannam@125: 
cannam@125: 		default :
cannam@125: 			break ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	/* Write file with a channel map. */
cannam@125: 	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	exit_if_true (
cannam@125: 		sf_command (file, SFC_SET_CHANNEL_MAP_INFO, channel_map_write, sizeof (channel_map_write)) == SF_FALSE,
cannam@125: 		"\n\nLine %d : sf_command (SFC_SET_CHANNEL_MAP_INFO) failed.\n\n", __LINE__
cannam@125: 		) ;
cannam@125: 	test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	/* Read file making sure no channel map exists. */
cannam@125: 	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 	exit_if_true (
cannam@125: 		sf_command (file, SFC_GET_CHANNEL_MAP_INFO, channel_map_read, sizeof (channel_map_read)) != SF_TRUE,
cannam@125: 		"\n\nLine %d : sf_command (SFC_GET_CHANNEL_MAP_INFO) failed.\n\n", __LINE__
cannam@125: 		) ;
cannam@125: 	check_log_buffer_or_die (file, __LINE__) ;
cannam@125: 	sf_close (file) ;
cannam@125: 
cannam@125: 	exit_if_true (
cannam@125: 		memcmp (channel_map_read, channel_map_write, sizeof (channel_map_read)) != 0,
cannam@125: 		"\n\nLine %d : Channel map read does not match channel map written.\n\n", __LINE__
cannam@125: 		) ;
cannam@125: 
cannam@125: 	unlink (filename) ;
cannam@125: 	puts ("ok") ;
cannam@125: } /* channel_map_test */
cannam@125: 
cannam@125: static	void
cannam@125: raw_needs_endswap_test (const char *filename, int filetype)
cannam@125: {	static int subtypes [] =
cannam@125: 	{	SF_FORMAT_FLOAT, SF_FORMAT_DOUBLE,
cannam@125: 		SF_FORMAT_PCM_16, SF_FORMAT_PCM_24, SF_FORMAT_PCM_32
cannam@125: 		} ;
cannam@125: 	SNDFILE	*file ;
cannam@125: 	SF_INFO	sfinfo ;
cannam@125: 	unsigned k ;
cannam@125: 	int needs_endswap ;
cannam@125: 
cannam@125: 	print_test_name (__func__, filename) ;
cannam@125: 
cannam@125: 	for (k = 0 ; k < ARRAY_LEN (subtypes) ; k++)
cannam@125: 	{
cannam@125: 		if (filetype == (SF_ENDIAN_LITTLE | SF_FORMAT_AIFF))
cannam@125: 			switch (subtypes [k])
cannam@125: 			{	/* Little endian AIFF does not AFAIK support fl32 and fl64. */
cannam@125: 				case SF_FORMAT_FLOAT :
cannam@125: 				case SF_FORMAT_DOUBLE :
cannam@125: 					continue ;
cannam@125: 				default :
cannam@125: 					break ;
cannam@125: 				} ;
cannam@125: 
cannam@125: 		memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@125: 		sfinfo.samplerate	= 11025 ;
cannam@125: 		sfinfo.format		= filetype | subtypes [k] ;
cannam@125: 		sfinfo.channels		= 1 ;
cannam@125: 
cannam@125: 		file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 		test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
cannam@125: 		sf_close (file) ;
cannam@125: 
cannam@125: 		memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@125: 		file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
cannam@125: 
cannam@125: 		needs_endswap = sf_command (file, SFC_RAW_DATA_NEEDS_ENDSWAP, NULL, 0) ;
cannam@125: 
cannam@125: 		switch (filetype)
cannam@125: 		{	case SF_FORMAT_WAV :
cannam@125: 			case SF_FORMAT_WAVEX :
cannam@125: 			case SF_FORMAT_AIFF | SF_ENDIAN_LITTLE :
cannam@125: 				exit_if_true (needs_endswap != CPU_IS_BIG_ENDIAN,
cannam@125: 					"\n\nLine %d : SFC_RAW_DATA_NEEDS_ENDSWAP failed for (%d | %d).\n\n", __LINE__, filetype, k) ;
cannam@125: 				break ;
cannam@125: 
cannam@125: 			case SF_FORMAT_AIFF :
cannam@125: 			case SF_FORMAT_WAV | SF_ENDIAN_BIG :
cannam@125: 				exit_if_true (needs_endswap != CPU_IS_LITTLE_ENDIAN,
cannam@125: 					"\n\nLine %d : SFC_RAW_DATA_NEEDS_ENDSWAP failed for (%d | %d).\n\n", __LINE__, filetype, k) ;
cannam@125: 				break ;
cannam@125: 
cannam@125: 			default :
cannam@125: 				printf ("\n\nLine %d : bad format value %d.\n\n", __LINE__, filetype) ;
cannam@125: 				exit (1) ;
cannam@125: 				break ;
cannam@125: 			} ;
cannam@125: 
cannam@125: 		sf_close (file) ;
cannam@125: 		} ;
cannam@125: 
cannam@125: 	unlink (filename) ;
cannam@125: 	puts ("ok") ;
cannam@125: } /* raw_needs_endswap_test */