comparison src/libsndfile-1.0.27/tests/headerless_test.c @ 40:1df64224f5ac

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