xref: /third_party/libsnd/tests/dither_test.c (revision b815c7f3)
1/*
2** Copyright (C) 2003-2013 Erik de Castro Lopo <erikd@mega-nerd.com>
3**
4** This program is free software; you can redistribute it and/or modify
5** it under the terms of the GNU General Public License as published by
6** the Free Software Foundation; either version 2 of the License, or
7** (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12** GNU General Public License for more details.
13**
14** You should have received a copy of the GNU General Public License
15** along with this program; if not, write to the Free Software
16** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19
20#include	"sfconfig.h"
21#include	<stdio.h>
22#include	<stdlib.h>
23#if HAVE_UNISTD_H
24#include	<unistd.h>
25#else
26#include	"sf_unistd.h"
27#endif
28#include	<string.h>
29#include	<math.h>
30
31#include	<sndfile.h>
32
33#include	"utils.h"
34
35#define	BUFFER_LEN		(1 << 16)
36#define LOG_BUFFER_SIZE	1024
37
38static void	dither_test (const char *filename, int filetype) ;
39
40/* Force the start of this buffer to be double aligned. Sparc-solaris will
41** choke if its not.
42*/
43static	short	data_out [BUFFER_LEN] ;
44
45int
46main (int argc, char *argv [])
47{	int		do_all = 0 ;
48	int		test_count = 0 ;
49
50	if (argc != 2)
51	{	printf ("Usage : %s <test>\n", argv [0]) ;
52		printf ("    Where <test> is one of the following:\n") ;
53		printf ("           wav  - test WAV file peak chunk\n") ;
54		printf ("           aiff - test AIFF file PEAK chunk\n") ;
55		printf ("           all  - perform all tests\n") ;
56		exit (1) ;
57		} ;
58
59	do_all = ! strcmp (argv [1], "all") ;
60
61	if (do_all || ! strcmp (argv [1], "wav"))
62	{	dither_test ("dither.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8) ;
63		test_count++ ;
64		} ;
65
66	if (do_all || ! strcmp (argv [1], "aiff"))
67	{	dither_test ("dither.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_S8) ;
68		test_count++ ;
69		} ;
70
71	if (do_all || ! strcmp (argv [1], "au"))
72	{	dither_test ("dither.au", SF_FORMAT_AU | SF_FORMAT_PCM_S8) ;
73		test_count++ ;
74		} ;
75
76	if (do_all || ! strcmp (argv [1], "svx"))
77	{	dither_test ("dither.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_S8) ;
78		test_count++ ;
79		} ;
80
81	if (do_all || ! strcmp (argv [1], "nist"))
82	{	dither_test ("dither.nist", SF_FORMAT_NIST | SF_FORMAT_PCM_S8) ;
83		test_count++ ;
84		} ;
85
86	if (do_all || ! strcmp (argv [1], "paf"))
87	{	dither_test ("dither.paf", SF_FORMAT_PAF | SF_FORMAT_PCM_S8) ;
88		test_count++ ;
89		} ;
90
91	if (do_all || ! strcmp (argv [1], "ircam"))
92	{	dither_test ("dither.ircam", SF_FORMAT_IRCAM | SF_FORMAT_PCM_S8) ;
93		test_count++ ;
94		} ;
95
96	if (do_all || ! strcmp (argv [1], "voc"))
97	{	dither_test ("dither.voc", SF_FORMAT_VOC | SF_FORMAT_PCM_S8) ;
98		test_count++ ;
99		} ;
100
101	if (do_all || ! strcmp (argv [1], "w64"))
102	{	dither_test ("dither.w64", SF_FORMAT_W64 | SF_FORMAT_PCM_S8) ;
103		test_count++ ;
104		} ;
105
106	if (do_all || ! strcmp (argv [1], "mat4"))
107	{	dither_test ("dither.mat4", SF_FORMAT_MAT4 | SF_FORMAT_PCM_S8) ;
108		test_count++ ;
109		} ;
110
111	if (do_all || ! strcmp (argv [1], "mat5"))
112	{	dither_test ("dither.mat5", SF_FORMAT_MAT5 | SF_FORMAT_PCM_S8) ;
113		test_count++ ;
114		} ;
115
116	if (do_all || ! strcmp (argv [1], "pvf"))
117	{	dither_test ("dither.pvf", SF_FORMAT_PVF | SF_FORMAT_PCM_S8) ;
118		test_count++ ;
119		} ;
120
121	if (test_count == 0)
122	{	printf ("Mono : ************************************\n") ;
123		printf ("Mono : *  No '%s' test defined.\n", argv [1]) ;
124		printf ("Mono : ************************************\n") ;
125		return 1 ;
126		} ;
127
128	return 0 ;
129} /* main */
130
131
132/*============================================================================================
133**	Here are the test functions.
134*/
135
136static void
137dither_test (const char *filename, int filetype)
138{	SNDFILE			*file ;
139	SF_INFO			sfinfo ;
140	SF_DITHER_INFO	dither ;
141
142	print_test_name ("dither_test", filename) ;
143
144	sfinfo.samplerate	= 44100 ;
145	sfinfo.format		= filetype ;
146	sfinfo.channels		= 1 ;
147	sfinfo.frames		= 0 ;
148
149	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
150
151	/* Check for old version of the dither API. */
152	if (sf_command (file, SFC_SET_DITHER_ON_WRITE, NULL, SF_TRUE) == 0)
153	{	printf ("\n\nLine %d: Should have an error here but don't.\n\n", __LINE__) ;
154		exit (1) ;
155		} ;
156
157	memset (&dither, 0, sizeof (dither)) ;
158	dither.type = SFD_WHITE ;
159	dither.level = 0 ;
160
161	if (sf_command (file, SFC_SET_DITHER_ON_WRITE, &dither, sizeof (dither)) != 0)
162	{	printf ("\n\nLine %d: sf_command (SFC_SET_DITHER_ON_WRITE) returned error : %s\n\n",
163			__LINE__, sf_strerror (file)) ;
164		exit (1) ;
165		} ;
166
167	/* Write data to file. */
168	test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ;
169	test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
170
171	sf_close (file) ;
172
173	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
174
175	if (sfinfo.frames != BUFFER_LEN)
176	{	printf ("\n\nLine %d: Bad frame count %d (should be %d)\n\n", __LINE__, (int) sfinfo.frames, BUFFER_LEN) ;
177		} ;
178
179	sf_close (file) ;
180	/*-unlink (filename) ;-*/
181
182	puts ("ok") ;
183} /* dither_test */
184
185