Chris@0
|
1 /*
|
Chris@0
|
2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
|
Chris@0
|
3 **
|
Chris@0
|
4 ** This program is free software; you can redistribute it and/or modify
|
Chris@0
|
5 ** it under the terms of the GNU General Public License as published by
|
Chris@0
|
6 ** the Free Software Foundation; either version 2 of the License, or
|
Chris@0
|
7 ** (at your option) any later version.
|
Chris@0
|
8 **
|
Chris@0
|
9 ** This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 ** GNU General Public License for more details.
|
Chris@0
|
13 **
|
Chris@0
|
14 ** You should have received a copy of the GNU General Public License
|
Chris@0
|
15 ** along with this program; if not, write to the Free Software
|
Chris@0
|
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
Chris@0
|
17 */
|
Chris@0
|
18
|
Chris@0
|
19 #include "sfconfig.h"
|
Chris@0
|
20
|
Chris@0
|
21 #include <stdio.h>
|
Chris@0
|
22 #include <stdlib.h>
|
Chris@0
|
23 #include <string.h>
|
Chris@0
|
24 #include <math.h>
|
Chris@0
|
25
|
Chris@0
|
26 #if HAVE_UNISTD_H
|
Chris@0
|
27 #include <unistd.h>
|
Chris@0
|
28 #endif
|
Chris@0
|
29
|
Chris@0
|
30 #include <sndfile.h>
|
Chris@0
|
31
|
Chris@0
|
32 #include "utils.h"
|
Chris@0
|
33
|
Chris@0
|
34 #define BUFFER_SIZE (2000)
|
Chris@0
|
35
|
Chris@0
|
36 static void old_test (void) ;
|
Chris@0
|
37 static void headerless_test (const char * filename, int format, int expected) ;
|
Chris@0
|
38
|
Chris@0
|
39 int
|
Chris@0
|
40 main (void)
|
Chris@0
|
41 {
|
Chris@0
|
42 old_test () ;
|
Chris@0
|
43
|
Chris@0
|
44 headerless_test ("raw.vox", SF_FORMAT_VOX_ADPCM, SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM) ;
|
Chris@0
|
45 headerless_test ("raw.gsm", SF_FORMAT_GSM610, SF_FORMAT_RAW | SF_FORMAT_GSM610) ;
|
Chris@0
|
46
|
Chris@0
|
47 headerless_test ("raw.snd", SF_FORMAT_ULAW, SF_FORMAT_RAW | SF_FORMAT_ULAW) ;
|
Chris@0
|
48 headerless_test ("raw.au" , SF_FORMAT_ULAW, SF_FORMAT_RAW | SF_FORMAT_ULAW) ;
|
Chris@0
|
49
|
Chris@0
|
50 return 0 ;
|
Chris@0
|
51 } /* main */
|
Chris@0
|
52
|
Chris@0
|
53 static void
|
Chris@0
|
54 headerless_test (const char * filename, int format, int expected)
|
Chris@0
|
55 { static short buffer [BUFFER_SIZE] ;
|
Chris@0
|
56 SNDFILE *file ;
|
Chris@0
|
57 SF_INFO sfinfo ;
|
Chris@0
|
58 int k ;
|
Chris@0
|
59
|
Chris@0
|
60 format &= SF_FORMAT_SUBMASK ;
|
Chris@0
|
61
|
Chris@0
|
62 print_test_name (__func__, filename) ;
|
Chris@0
|
63
|
Chris@0
|
64 for (k = 0 ; k < BUFFER_SIZE ; k++)
|
Chris@0
|
65 buffer [k] = k ;
|
Chris@0
|
66
|
Chris@0
|
67 sfinfo.samplerate = 8000 ;
|
Chris@0
|
68 sfinfo.frames = 0 ;
|
Chris@0
|
69 sfinfo.channels = 1 ;
|
Chris@0
|
70 sfinfo.format = SF_FORMAT_RAW | format ;
|
Chris@0
|
71
|
Chris@0
|
72 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
|
Chris@0
|
73
|
Chris@0
|
74 if ((k = sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE)
|
Chris@0
|
75 { printf ("Line %d: sf_write_short failed with short write (%d => %d).\n", __LINE__, BUFFER_SIZE, k) ;
|
Chris@0
|
76 fflush (stdout) ;
|
Chris@0
|
77 puts (sf_strerror (file)) ;
|
Chris@0
|
78 exit (1) ;
|
Chris@0
|
79 } ;
|
Chris@0
|
80
|
Chris@0
|
81 sf_close (file) ;
|
Chris@0
|
82
|
Chris@0
|
83 memset (buffer, 0, sizeof (buffer)) ;
|
Chris@0
|
84
|
Chris@0
|
85 /* We should be able to detect these so clear sfinfo. */
|
Chris@0
|
86 memset (&sfinfo, 0, sizeof (sfinfo)) ;
|
Chris@0
|
87
|
Chris@0
|
88 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
|
Chris@0
|
89
|
Chris@0
|
90 if (sfinfo.format != expected)
|
Chris@0
|
91 { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, expected, sfinfo.format) ;
|
Chris@0
|
92 exit (1) ;
|
Chris@0
|
93 } ;
|
Chris@0
|
94
|
Chris@0
|
95 if (sfinfo.frames < BUFFER_SIZE)
|
Chris@0
|
96 { printf ("Line %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
|
Chris@0
|
97 exit (1) ;
|
Chris@0
|
98 } ;
|
Chris@0
|
99
|
Chris@0
|
100 if (sfinfo.channels != 1)
|
Chris@0
|
101 { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
|
Chris@0
|
102 exit (1) ;
|
Chris@0
|
103 } ;
|
Chris@0
|
104
|
Chris@0
|
105 check_log_buffer_or_die (file, __LINE__) ;
|
Chris@0
|
106
|
Chris@0
|
107 sf_close (file) ;
|
Chris@0
|
108
|
Chris@0
|
109 printf ("ok\n") ;
|
Chris@0
|
110 unlink (filename) ;
|
Chris@0
|
111 } /* headerless_test */
|
Chris@0
|
112
|
Chris@0
|
113 static void
|
Chris@0
|
114 old_test (void)
|
Chris@0
|
115 { static short buffer [BUFFER_SIZE] ;
|
Chris@0
|
116 SNDFILE *file ;
|
Chris@0
|
117 SF_INFO sfinfo ;
|
Chris@0
|
118 int k, filetype ;
|
Chris@0
|
119 const char *filename = "headerless.wav" ;
|
Chris@0
|
120
|
Chris@0
|
121 print_test_name (__func__, "") ;
|
Chris@0
|
122
|
Chris@0
|
123 for (k = 0 ; k < BUFFER_SIZE ; k++)
|
Chris@0
|
124 buffer [k] = k ;
|
Chris@0
|
125
|
Chris@0
|
126 filetype = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
|
Chris@0
|
127
|
Chris@0
|
128 sfinfo.samplerate = 32000 ;
|
Chris@0
|
129 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
|
Chris@0
|
130 sfinfo.channels = 1 ;
|
Chris@0
|
131 sfinfo.format = filetype ;
|
Chris@0
|
132
|
Chris@0
|
133 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
|
Chris@0
|
134
|
Chris@0
|
135 if ((k = sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE)
|
Chris@0
|
136 { printf ("Line %d: sf_write_short failed with short write (%d => %d).\n", __LINE__, BUFFER_SIZE, k) ;
|
Chris@0
|
137 fflush (stdout) ;
|
Chris@0
|
138 puts (sf_strerror (file)) ;
|
Chris@0
|
139 exit (1) ;
|
Chris@0
|
140 } ;
|
Chris@0
|
141
|
Chris@0
|
142 sf_close (file) ;
|
Chris@0
|
143
|
Chris@0
|
144 memset (buffer, 0, sizeof (buffer)) ;
|
Chris@0
|
145
|
Chris@0
|
146 /* Read as RAW but get the bit width and endian-ness correct. */
|
Chris@0
|
147 sfinfo.format = filetype = SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_16 ;
|
Chris@0
|
148
|
Chris@0
|
149 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
|
Chris@0
|
150
|
Chris@0
|
151 if (sfinfo.format != filetype)
|
Chris@0
|
152 { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
|
Chris@0
|
153 exit (1) ;
|
Chris@0
|
154 } ;
|
Chris@0
|
155
|
Chris@0
|
156 if (sfinfo.frames < BUFFER_SIZE)
|
Chris@0
|
157 { printf ("Line %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
|
Chris@0
|
158 exit (1) ;
|
Chris@0
|
159 } ;
|
Chris@0
|
160
|
Chris@0
|
161 if (sfinfo.channels != 1)
|
Chris@0
|
162 { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
|
Chris@0
|
163 exit (1) ;
|
Chris@0
|
164 } ;
|
Chris@0
|
165
|
Chris@0
|
166 check_log_buffer_or_die (file, __LINE__) ;
|
Chris@0
|
167
|
Chris@0
|
168 if ((k = sf_read_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE)
|
Chris@0
|
169 { printf ("Line %d: short read (%d).\n", __LINE__, k) ;
|
Chris@0
|
170 exit (1) ;
|
Chris@0
|
171 } ;
|
Chris@0
|
172
|
Chris@0
|
173 for (k = 0 ; k < BUFFER_SIZE - 22 ; k++)
|
Chris@0
|
174 if (buffer [k + 22] != k)
|
Chris@0
|
175 { printf ("Line %d: Incorrect sample (#%d : 0x%x => 0x%x).\n", __LINE__, k, k, buffer [k]) ;
|
Chris@0
|
176 exit (1) ;
|
Chris@0
|
177 } ;
|
Chris@0
|
178
|
Chris@0
|
179 sf_close (file) ;
|
Chris@0
|
180
|
Chris@0
|
181 printf ("ok\n") ;
|
Chris@0
|
182 unlink (filename) ;
|
Chris@0
|
183 } /* old_test */
|
Chris@0
|
184
|