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