Update readme, install and license files.
[Faustine.git] / interpretor / lib / src / libsndfile-1.0.25 / tests / virtual_io_test.c
1 /*
2 ** Copyright (C) 1999-2011 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
24 #include <fcntl.h>
25 #include <math.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <sys/stat.h>
29
30 #include <sndfile.h>
31
32 #include "utils.h"
33
34 static void vio_test (const char *fname, int format) ;
35
36 int
37 main (void)
38 {
39 vio_test ("vio_pcm16.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
40 vio_test ("vio_pcm24.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_24) ;
41 vio_test ("vio_float.au", SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
42 vio_test ("vio_pcm24.paf", SF_FORMAT_PAF | SF_FORMAT_PCM_24) ;
43
44 return 0 ;
45 } /* main */
46
47 /*==============================================================================
48 */
49
50 typedef struct
51 { sf_count_t offset, length ;
52 unsigned char data [16 * 1024] ;
53 } VIO_DATA ;
54
55 static sf_count_t
56 vfget_filelen (void *user_data)
57 { VIO_DATA *vf = (VIO_DATA *) user_data ;
58
59 return vf->length ;
60 } /* vfget_filelen */
61
62 static sf_count_t
63 vfseek (sf_count_t offset, int whence, void *user_data)
64 { VIO_DATA *vf = (VIO_DATA *) user_data ;
65
66 switch (whence)
67 { case SEEK_SET :
68 vf->offset = offset ;
69 break ;
70
71 case SEEK_CUR :
72 vf->offset = vf->offset + offset ;
73 break ;
74
75 case SEEK_END :
76 vf->offset = vf->length + offset ;
77 break ;
78 default :
79 break ;
80 } ;
81
82 return vf->offset ;
83 } /* vfseek */
84
85 static sf_count_t
86 vfread (void *ptr, sf_count_t count, void *user_data)
87 { VIO_DATA *vf = (VIO_DATA *) user_data ;
88
89 /*
90 ** This will brack badly for files over 2Gig in length, but
91 ** is sufficient for testing.
92 */
93 if (vf->offset + count > vf->length)
94 count = vf->length - vf->offset ;
95
96 memcpy (ptr, vf->data + vf->offset, count) ;
97 vf->offset += count ;
98
99 return count ;
100 } /* vfread */
101
102 static sf_count_t
103 vfwrite (const void *ptr, sf_count_t count, void *user_data)
104 { VIO_DATA *vf = (VIO_DATA *) user_data ;
105
106 /*
107 ** This will break badly for files over 2Gig in length, but
108 ** is sufficient for testing.
109 */
110 if (vf->offset >= SIGNED_SIZEOF (vf->data))
111 return 0 ;
112
113 if (vf->offset + count > SIGNED_SIZEOF (vf->data))
114 count = sizeof (vf->data) - vf->offset ;
115
116 memcpy (vf->data + vf->offset, ptr, (size_t) count) ;
117 vf->offset += count ;
118
119 if (vf->offset > vf->length)
120 vf->length = vf->offset ;
121
122 return count ;
123 } /* vfwrite */
124
125 static sf_count_t
126 vftell (void *user_data)
127 { VIO_DATA *vf = (VIO_DATA *) user_data ;
128
129 return vf->offset ;
130 } /* vftell */
131
132
133 /*==============================================================================
134 */
135
136 static void
137 gen_short_data (short * data, int len, int start)
138 { int k ;
139
140 for (k = 0 ; k < len ; k++)
141 data [k] = start + k ;
142 } /* gen_short_data */
143
144
145 static void
146 check_short_data (short * data, int len, int start, int line)
147 { int k ;
148
149 for (k = 0 ; k < len ; k++)
150 if (data [k] != start + k)
151 { printf ("\n\nLine %d : data [%d] = %d (should be %d).\n\n", line, k, data [k], start + k) ;
152 exit (1) ;
153 } ;
154 } /* gen_short_data */
155
156 /*------------------------------------------------------------------------------
157 */
158
159 static void
160 vio_test (const char *fname, int format)
161 { static VIO_DATA vio_data ;
162 static short data [256] ;
163
164 SF_VIRTUAL_IO vio ;
165 SNDFILE * file ;
166 SF_INFO sfinfo ;
167
168 print_test_name ("virtual i/o test", fname) ;
169
170 /* Set up pointers to the locally defined functions. */
171 vio.get_filelen = vfget_filelen ;
172 vio.seek = vfseek ;
173 vio.read = vfread ;
174 vio.write = vfwrite ;
175 vio.tell = vftell ;
176
177 /* Set virtual file offset and length to zero. */
178 vio_data.offset = 0 ;
179 vio_data.length = 0 ;
180
181 memset (&sfinfo, 0, sizeof (sfinfo)) ;
182 sfinfo.format = format ;
183 sfinfo.channels = 2 ;
184 sfinfo.samplerate = 44100 ;
185
186 if ((file = sf_open_virtual (&vio, SFM_WRITE, &sfinfo, &vio_data)) == NULL)
187 { printf ("\n\nLine %d : sf_open_write failed with error : ", __LINE__) ;
188 fflush (stdout) ;
189 puts (sf_strerror (NULL)) ;
190 exit (1) ;
191 } ;
192
193 if (vfget_filelen (&vio_data) < 0)
194 { printf ("\n\nLine %d : vfget_filelen returned negative length.\n\n", __LINE__) ;
195 exit (1) ;
196 } ;
197
198 gen_short_data (data, ARRAY_LEN (data), 0) ;
199 sf_write_short (file, data, ARRAY_LEN (data)) ;
200
201 gen_short_data (data, ARRAY_LEN (data), 1) ;
202 sf_write_short (file, data, ARRAY_LEN (data)) ;
203
204 gen_short_data (data, ARRAY_LEN (data), 2) ;
205 sf_write_short (file, data, ARRAY_LEN (data)) ;
206
207 sf_close (file) ;
208
209 /* Now test read. */
210 memset (&sfinfo, 0, sizeof (sfinfo)) ;
211
212 vio_data.offset = 0 ;
213
214 if ((file = sf_open_virtual (&vio, SFM_READ, &sfinfo, &vio_data)) == NULL)
215 { printf ("\n\nLine %d : sf_open_write failed with error : ", __LINE__) ;
216 fflush (stdout) ;
217 puts (sf_strerror (NULL)) ;
218
219 dump_data_to_file (fname, vio_data.data, vio_data.length) ;
220 exit (1) ;
221 } ;
222
223
224 sf_read_short (file, data, ARRAY_LEN (data)) ;
225 check_short_data (data, ARRAY_LEN (data), 0, __LINE__) ;
226
227 sf_read_short (file, data, ARRAY_LEN (data)) ;
228 check_short_data (data, ARRAY_LEN (data), 1, __LINE__) ;
229
230 sf_read_short (file, data, ARRAY_LEN (data)) ;
231 check_short_data (data, ARRAY_LEN (data), 2, __LINE__) ;
232
233 sf_close (file) ;
234
235 puts ("ok") ;
236 } /* vio_test */
237