1 /*
2 ** Copyright (C) 2002-2014 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_LEN (1 << 10)
38 #define LOG_BUFFER_SIZE 1024
39
40 static void raw_offset_test (const char *filename, int typeminor) ;
41 static void bad_raw_test (void) ;
42
43 /* Force the start of this buffer to be double aligned. Sparc-solaris will
44 ** choke if its not.
45 */
46 static short data [BUFFER_LEN] ;
47
48 int
main(void)49 main (void)
50 {
51 raw_offset_test ("offset.raw", SF_FORMAT_PCM_16) ;
52 bad_raw_test () ;
53
54 return 0 ;
55 } /* main */
56
57 /*============================================================================================
58 ** Here are the test functions.
59 */
60
61 static void
raw_offset_test(const char * filename,int typeminor)62 raw_offset_test (const char *filename, int typeminor)
63 { SNDFILE *sndfile ;
64 SF_INFO sfinfo ;
65 sf_count_t start ;
66 int k ;
67
68 print_test_name ("raw_offset_test", filename) ;
69
70 sfinfo.samplerate = 44100 ;
71 sfinfo.format = SF_FORMAT_RAW | typeminor ;
72 sfinfo.channels = 1 ;
73 sfinfo.frames = 0 ;
74
75 sndfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
76
77 start = 0 ;
78 sf_command (sndfile, SFC_FILE_TRUNCATE, &start, sizeof (start)) ;
79
80 for (k = 0 ; k < BUFFER_LEN ; k++)
81 data [k] = k ;
82 test_write_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ;
83
84 sf_close (sndfile) ;
85
86 sndfile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
87 check_log_buffer_or_die (sndfile, __LINE__) ;
88
89 if (ABS (BUFFER_LEN - sfinfo.frames) > 1)
90 { printf ("\n\nLine %d : Incorrect sample count (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, BUFFER_LEN) ;
91 dump_log_buffer (sndfile) ;
92 exit (1) ;
93 } ;
94
95 memset (data, 0 , sizeof (data)) ;
96 test_read_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ;
97 for (k = 0 ; k < BUFFER_LEN ; k++)
98 if (data [k] != k)
99 printf ("Error : line %d\n", __LINE__) ;
100
101 /* Set dataoffset to 2 bytes from beginning of file. */
102 start = 2 ;
103 sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ;
104
105 /* Seek to new start */
106 test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
107
108 memset (data, 0 , sizeof (data)) ;
109 test_read_short_or_die (sndfile, 0, data, BUFFER_LEN - 1, __LINE__) ;
110 for (k = 0 ; k < BUFFER_LEN - 1 ; k++)
111 if (data [k] != k + 1)
112 { printf ("Error : line %d\n", __LINE__) ;
113 exit (1) ;
114 } ;
115
116 /* Set dataoffset to 4 bytes from beginning of file. */
117 start = 4 ;
118 sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ;
119
120 /* Seek to new start */
121 test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
122
123 memset (data, 0 , sizeof (data)) ;
124 test_read_short_or_die (sndfile, 0, data, BUFFER_LEN - 2, __LINE__) ;
125 for (k = 0 ; k < BUFFER_LEN - 2 ; k++)
126 if (data [k] != k + 2)
127 { printf ("Error : line %d\n", __LINE__) ;
128 exit (1) ;
129 } ;
130
131 /* Set dataoffset back to 0 bytes from beginning of file. */
132 start = 0 ;
133 sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ;
134
135 /* Seek to new start */
136 test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
137
138 memset (data, 0 , sizeof (data)) ;
139 test_read_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ;
140 for (k = 0 ; k < BUFFER_LEN ; k++)
141 if (data [k] != k)
142 { printf ("Error : line %d\n", __LINE__) ;
143 exit (1) ;
144 } ;
145
146 sf_close (sndfile) ;
147 unlink (filename) ;
148
149 puts ("ok") ;
150 } /* raw_offset_test */
151
152 static void
bad_raw_test(void)153 bad_raw_test (void)
154 { FILE *textfile ;
155 SNDFILE *file ;
156 SF_INFO sfinfo ;
157 const char *errorstr, *filename = "bad.raw" ;
158
159 print_test_name ("bad_raw_test", filename) ;
160
161 if ((textfile = fopen (filename, "w")) == NULL)
162 { printf ("\n\nLine %d : not able to open text file for write.\n", __LINE__) ;
163 exit (1) ;
164 } ;
165
166 fprintf (textfile, "This is not a valid file.\n") ;
167 fclose (textfile) ;
168
169 sfinfo.samplerate = 44100 ;
170 sfinfo.format = SF_FORMAT_RAW | 0xABCD ;
171 sfinfo.channels = 1 ;
172
173 if ((file = sf_open (filename, SFM_READ, &sfinfo)) != NULL)
174 { printf ("\n\nLine %d : Error, file should not have opened.\n", __LINE__ - 1) ;
175 exit (1) ;
176 } ;
177
178 errorstr = sf_strerror (file) ;
179
180 if (strstr (errorstr, "Bad format field in SF_INFO struct") == NULL)
181 { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__ - 1, errorstr) ;
182 exit (1) ;
183 } ;
184
185 unlink (filename) ;
186
187 puts ("ok") ;
188 } /* bad_raw_test */
189
190