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