1b815c7f3Sopenharmony_ci/*
2b815c7f3Sopenharmony_ci** Copyright (C) 1999-2012 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#include "sfconfig.h"
20b815c7f3Sopenharmony_ci
21b815c7f3Sopenharmony_ci#include <stdio.h>
22b815c7f3Sopenharmony_ci#include <stdlib.h>
23b815c7f3Sopenharmony_ci#include <string.h>
24b815c7f3Sopenharmony_ci#include <math.h>
25b815c7f3Sopenharmony_ci#include <inttypes.h>
26b815c7f3Sopenharmony_ci
27b815c7f3Sopenharmony_ci#if HAVE_UNISTD_H
28b815c7f3Sopenharmony_ci#include <unistd.h>
29b815c7f3Sopenharmony_ci#else
30b815c7f3Sopenharmony_ci#include "sf_unistd.h"
31b815c7f3Sopenharmony_ci#endif
32b815c7f3Sopenharmony_ci
33b815c7f3Sopenharmony_ci#include <sndfile.h>
34b815c7f3Sopenharmony_ci
35b815c7f3Sopenharmony_ci#include "utils.h"
36b815c7f3Sopenharmony_ci
37b815c7f3Sopenharmony_ci#define	BUFFER_SIZE		(2000)
38b815c7f3Sopenharmony_ci
39b815c7f3Sopenharmony_cistatic void old_test (void) ;
40b815c7f3Sopenharmony_cistatic void headerless_test (const char * filename, int format, int expected) ;
41b815c7f3Sopenharmony_ci
42b815c7f3Sopenharmony_ciint
43b815c7f3Sopenharmony_cimain (void)
44b815c7f3Sopenharmony_ci{
45b815c7f3Sopenharmony_ci	old_test () ;
46b815c7f3Sopenharmony_ci
47b815c7f3Sopenharmony_ci	headerless_test ("headerless.vox", SF_FORMAT_VOX_ADPCM, SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM) ;
48b815c7f3Sopenharmony_ci	headerless_test ("headerless.gsm", SF_FORMAT_GSM610, SF_FORMAT_RAW | SF_FORMAT_GSM610) ;
49b815c7f3Sopenharmony_ci
50b815c7f3Sopenharmony_ci	headerless_test ("headerless.snd", SF_FORMAT_ULAW, SF_FORMAT_RAW | SF_FORMAT_ULAW) ;
51b815c7f3Sopenharmony_ci	headerless_test ("headerless.au" , SF_FORMAT_ULAW, SF_FORMAT_RAW | SF_FORMAT_ULAW) ;
52b815c7f3Sopenharmony_ci
53b815c7f3Sopenharmony_ci	return 0 ;
54b815c7f3Sopenharmony_ci} /* main */
55b815c7f3Sopenharmony_ci
56b815c7f3Sopenharmony_cistatic void
57b815c7f3Sopenharmony_ciheaderless_test (const char * filename, int format, int expected)
58b815c7f3Sopenharmony_ci{	static	short	buffer [BUFFER_SIZE] ;
59b815c7f3Sopenharmony_ci	SNDFILE		*file ;
60b815c7f3Sopenharmony_ci	SF_INFO		sfinfo ;
61b815c7f3Sopenharmony_ci	int			k ;
62b815c7f3Sopenharmony_ci
63b815c7f3Sopenharmony_ci	format &= SF_FORMAT_SUBMASK ;
64b815c7f3Sopenharmony_ci
65b815c7f3Sopenharmony_ci	print_test_name (__func__, filename) ;
66b815c7f3Sopenharmony_ci
67b815c7f3Sopenharmony_ci	for (k = 0 ; k < BUFFER_SIZE ; k++)
68b815c7f3Sopenharmony_ci		buffer [k] = k ;
69b815c7f3Sopenharmony_ci
70b815c7f3Sopenharmony_ci	sfinfo.samplerate	= 8000 ;
71b815c7f3Sopenharmony_ci	sfinfo.frames		= 0 ;
72b815c7f3Sopenharmony_ci	sfinfo.channels		= 1 ;
73b815c7f3Sopenharmony_ci	sfinfo.format		= SF_FORMAT_RAW | format ;
74b815c7f3Sopenharmony_ci
75b815c7f3Sopenharmony_ci	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
76b815c7f3Sopenharmony_ci
77b815c7f3Sopenharmony_ci	if ((k = (int) sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE)
78b815c7f3Sopenharmony_ci	{	printf ("Line %d: sf_write_short failed with short write (%d => %d).\n", __LINE__, BUFFER_SIZE, k) ;
79b815c7f3Sopenharmony_ci		fflush (stdout) ;
80b815c7f3Sopenharmony_ci		puts (sf_strerror (file)) ;
81b815c7f3Sopenharmony_ci		exit (1) ;
82b815c7f3Sopenharmony_ci		} ;
83b815c7f3Sopenharmony_ci
84b815c7f3Sopenharmony_ci	sf_close (file) ;
85b815c7f3Sopenharmony_ci
86b815c7f3Sopenharmony_ci	memset (buffer, 0, sizeof (buffer)) ;
87b815c7f3Sopenharmony_ci
88b815c7f3Sopenharmony_ci	/* We should be able to detect these so clear sfinfo. */
89b815c7f3Sopenharmony_ci	memset (&sfinfo, 0, sizeof (sfinfo)) ;
90b815c7f3Sopenharmony_ci
91b815c7f3Sopenharmony_ci	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
92b815c7f3Sopenharmony_ci
93b815c7f3Sopenharmony_ci	if (sfinfo.format != expected)
94b815c7f3Sopenharmony_ci	{	printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, expected, sfinfo.format) ;
95b815c7f3Sopenharmony_ci		exit (1) ;
96b815c7f3Sopenharmony_ci		} ;
97b815c7f3Sopenharmony_ci
98b815c7f3Sopenharmony_ci	if (sfinfo.frames < BUFFER_SIZE)
99b815c7f3Sopenharmony_ci	{	printf ("Line %d: Incorrect number of.frames in file. (%d => %" PRId64 ")\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
100b815c7f3Sopenharmony_ci		exit (1) ;
101b815c7f3Sopenharmony_ci		} ;
102b815c7f3Sopenharmony_ci
103b815c7f3Sopenharmony_ci	if (sfinfo.channels != 1)
104b815c7f3Sopenharmony_ci	{	printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
105b815c7f3Sopenharmony_ci		exit (1) ;
106b815c7f3Sopenharmony_ci		} ;
107b815c7f3Sopenharmony_ci
108b815c7f3Sopenharmony_ci	check_log_buffer_or_die (file, __LINE__) ;
109b815c7f3Sopenharmony_ci
110b815c7f3Sopenharmony_ci	sf_close (file) ;
111b815c7f3Sopenharmony_ci
112b815c7f3Sopenharmony_ci	printf ("ok\n") ;
113b815c7f3Sopenharmony_ci	unlink (filename) ;
114b815c7f3Sopenharmony_ci} /* headerless_test */
115b815c7f3Sopenharmony_ci
116b815c7f3Sopenharmony_cistatic void
117b815c7f3Sopenharmony_ciold_test (void)
118b815c7f3Sopenharmony_ci{	static	short	buffer [BUFFER_SIZE] ;
119b815c7f3Sopenharmony_ci	SNDFILE		*file ;
120b815c7f3Sopenharmony_ci	SF_INFO		sfinfo ;
121b815c7f3Sopenharmony_ci	int			k, filetype ;
122b815c7f3Sopenharmony_ci	const char	*filename = "headerless.wav" ;
123b815c7f3Sopenharmony_ci
124b815c7f3Sopenharmony_ci	print_test_name (__func__, "") ;
125b815c7f3Sopenharmony_ci
126b815c7f3Sopenharmony_ci	for (k = 0 ; k < BUFFER_SIZE ; k++)
127b815c7f3Sopenharmony_ci		buffer [k] = k ;
128b815c7f3Sopenharmony_ci
129b815c7f3Sopenharmony_ci	filetype = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
130b815c7f3Sopenharmony_ci
131b815c7f3Sopenharmony_ci	sfinfo.samplerate	= 32000 ;
132b815c7f3Sopenharmony_ci	sfinfo.frames		= 123456789 ; /* Wrong length. Library should correct this on sf_close. */
133b815c7f3Sopenharmony_ci	sfinfo.channels		= 1 ;
134b815c7f3Sopenharmony_ci	sfinfo.format		= filetype ;
135b815c7f3Sopenharmony_ci
136b815c7f3Sopenharmony_ci	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
137b815c7f3Sopenharmony_ci
138b815c7f3Sopenharmony_ci	if ((k = (int) sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE)
139b815c7f3Sopenharmony_ci	{	printf ("Line %d: sf_write_short failed with short write (%d => %d).\n", __LINE__, BUFFER_SIZE, k) ;
140b815c7f3Sopenharmony_ci		fflush (stdout) ;
141b815c7f3Sopenharmony_ci		puts (sf_strerror (file)) ;
142b815c7f3Sopenharmony_ci		exit (1) ;
143b815c7f3Sopenharmony_ci		} ;
144b815c7f3Sopenharmony_ci
145b815c7f3Sopenharmony_ci	sf_close (file) ;
146b815c7f3Sopenharmony_ci
147b815c7f3Sopenharmony_ci	memset (buffer, 0, sizeof (buffer)) ;
148b815c7f3Sopenharmony_ci
149b815c7f3Sopenharmony_ci	/* Read as RAW but get the bit width and endian-ness correct. */
150b815c7f3Sopenharmony_ci	sfinfo.format = filetype = SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_16 ;
151b815c7f3Sopenharmony_ci
152b815c7f3Sopenharmony_ci	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
153b815c7f3Sopenharmony_ci
154b815c7f3Sopenharmony_ci	if (sfinfo.format != filetype)
155b815c7f3Sopenharmony_ci	{	printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
156b815c7f3Sopenharmony_ci		exit (1) ;
157b815c7f3Sopenharmony_ci		} ;
158b815c7f3Sopenharmony_ci
159b815c7f3Sopenharmony_ci	if (sfinfo.frames < BUFFER_SIZE)
160b815c7f3Sopenharmony_ci	{	printf ("Line %d: Incorrect number of.frames in file. (%d => %" PRId64 ")\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
161b815c7f3Sopenharmony_ci		exit (1) ;
162b815c7f3Sopenharmony_ci		} ;
163b815c7f3Sopenharmony_ci
164b815c7f3Sopenharmony_ci	if (sfinfo.channels != 1)
165b815c7f3Sopenharmony_ci	{	printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
166b815c7f3Sopenharmony_ci		exit (1) ;
167b815c7f3Sopenharmony_ci		} ;
168b815c7f3Sopenharmony_ci
169b815c7f3Sopenharmony_ci	check_log_buffer_or_die (file, __LINE__) ;
170b815c7f3Sopenharmony_ci
171b815c7f3Sopenharmony_ci	if ((k = (int) sf_read_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE)
172b815c7f3Sopenharmony_ci	{	printf ("Line %d: short read (%d).\n", __LINE__, k) ;
173b815c7f3Sopenharmony_ci		exit (1) ;
174b815c7f3Sopenharmony_ci		} ;
175b815c7f3Sopenharmony_ci
176b815c7f3Sopenharmony_ci	for (k = 0 ; k < BUFFER_SIZE - 22 ; k++)
177b815c7f3Sopenharmony_ci		if (buffer [k + 22] != k)
178b815c7f3Sopenharmony_ci		{	printf ("Line %d: Incorrect sample (#%d : 0x%x => 0x%x).\n", __LINE__, k, k, buffer [k]) ;
179b815c7f3Sopenharmony_ci			exit (1) ;
180b815c7f3Sopenharmony_ci			} ;
181b815c7f3Sopenharmony_ci
182b815c7f3Sopenharmony_ci	sf_close (file) ;
183b815c7f3Sopenharmony_ci
184b815c7f3Sopenharmony_ci	printf ("ok\n") ;
185b815c7f3Sopenharmony_ci	unlink (filename) ;
186b815c7f3Sopenharmony_ci} /* old_test */
187b815c7f3Sopenharmony_ci
188