2 ** Copyright (C) 1999-2009 Erik de Castro Lopo <erikd@mega-nerd.com>
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.
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.
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.
34 static void vio_test (const char *fname, int format) ;
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) ;
47 /*==============================================================================
51 { sf_count_t offset, length ;
52 unsigned char data [16 * 1024] ;
56 vfget_filelen (void *user_data)
57 { VIO_DATA *vf = (VIO_DATA *) user_data ;
63 vfseek (sf_count_t offset, int whence, void *user_data)
64 { VIO_DATA *vf = (VIO_DATA *) user_data ;
72 vf->offset = vf->offset + offset ;
76 vf->offset = vf->length + offset ;
86 vfread (void *ptr, sf_count_t count, void *user_data)
87 { VIO_DATA *vf = (VIO_DATA *) user_data ;
90 ** This will brack badly for files over 2Gig in length, but
91 ** is sufficient for testing.
93 if (vf->offset + count > vf->length)
94 count = vf->length - vf->offset ;
96 memcpy (ptr, vf->data + vf->offset, count) ;
103 vfwrite (const void *ptr, sf_count_t count, void *user_data)
104 { VIO_DATA *vf = (VIO_DATA *) user_data ;
107 ** This will break badly for files over 2Gig in length, but
108 ** is sufficient for testing.
110 if (vf->offset >= SIGNED_SIZEOF (vf->data))
113 if (vf->offset + count > SIGNED_SIZEOF (vf->data))
114 count = sizeof (vf->data) - vf->offset ;
116 memcpy (vf->data + vf->offset, ptr, (size_t) count) ;
117 vf->offset += count ;
119 if (vf->offset > vf->length)
120 vf->length = vf->offset ;
126 vftell (void *user_data)
127 { VIO_DATA *vf = (VIO_DATA *) user_data ;
133 /*==============================================================================
137 gen_short_data (short * data, int len, int start)
140 for (k = 0 ; k < len ; k++)
141 data [k] = start + k ;
142 } /* gen_short_data */
146 check_short_data (short * data, int len, int start, int line)
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) ;
154 } /* gen_short_data */
156 /*------------------------------------------------------------------------------
160 vio_test (const char *fname, int format)
161 { static VIO_DATA vio_data ;
162 static short data [256] ;
168 print_test_name ("virtual i/o test", fname) ;
170 /* Set up pointers to the locally defined functions. */
171 vio.get_filelen = vfget_filelen ;
174 vio.write = vfwrite ;
177 /* Set virtual file offset and length to zero. */
178 vio_data.offset = 0 ;
179 vio_data.length = 0 ;
181 memset (&sfinfo, 0, sizeof (sfinfo)) ;
182 sfinfo.format = format ;
183 sfinfo.channels = 2 ;
184 sfinfo.samplerate = 44100 ;
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__) ;
189 puts (sf_strerror (NULL)) ;
193 if (vfget_filelen (&vio_data) < 0)
194 { printf ("\n\nLine %d : vfget_filelen returned negative length.\n\n", __LINE__) ;
198 gen_short_data (data, ARRAY_LEN (data), 0) ;
199 sf_write_short (file, data, ARRAY_LEN (data)) ;
201 gen_short_data (data, ARRAY_LEN (data), 1) ;
202 sf_write_short (file, data, ARRAY_LEN (data)) ;
204 gen_short_data (data, ARRAY_LEN (data), 2) ;
205 sf_write_short (file, data, ARRAY_LEN (data)) ;
210 memset (&sfinfo, 0, sizeof (sfinfo)) ;
212 vio_data.offset = 0 ;
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__) ;
217 puts (sf_strerror (NULL)) ;
219 dump_data_to_file (fname, vio_data.data, vio_data.length) ;
224 sf_read_short (file, data, ARRAY_LEN (data)) ;
225 check_short_data (data, ARRAY_LEN (data), 0, __LINE__) ;
227 sf_read_short (file, data, ARRAY_LEN (data)) ;
228 check_short_data (data, ARRAY_LEN (data), 1, __LINE__) ;
230 sf_read_short (file, data, ARRAY_LEN (data)) ;
231 check_short_data (data, ARRAY_LEN (data), 2, __LINE__) ;