1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
/*
** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "sfconfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <math.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sndfile.h>
#include "utils.h"
static void vio_test (const char *fname, int format) ;
int
main (void)
{
vio_test ("vio_pcm16.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
vio_test ("vio_pcm24.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_24) ;
vio_test ("vio_float.au", SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
vio_test ("vio_pcm24.paf", SF_FORMAT_PAF | SF_FORMAT_PCM_24) ;
return 0 ;
} /* main */
/*==============================================================================
*/
typedef struct
{ sf_count_t offset, length ;
unsigned char data [16 * 1024] ;
} VIO_DATA ;
static sf_count_t
vfget_filelen (void *user_data)
{ VIO_DATA *vf = (VIO_DATA *) user_data ;
return vf->length ;
} /* vfget_filelen */
static sf_count_t
vfseek (sf_count_t offset, int whence, void *user_data)
{ VIO_DATA *vf = (VIO_DATA *) user_data ;
switch (whence)
{ case SEEK_SET :
vf->offset = offset ;
break ;
case SEEK_CUR :
vf->offset = vf->offset + offset ;
break ;
case SEEK_END :
vf->offset = vf->length + offset ;
break ;
default :
break ;
} ;
return vf->offset ;
} /* vfseek */
static sf_count_t
vfread (void *ptr, sf_count_t count, void *user_data)
{ VIO_DATA *vf = (VIO_DATA *) user_data ;
/*
** This will brack badly for files over 2Gig in length, but
** is sufficient for testing.
*/
if (vf->offset + count > vf->length)
count = vf->length - vf->offset ;
memcpy (ptr, vf->data + vf->offset, count) ;
vf->offset += count ;
return count ;
} /* vfread */
static sf_count_t
vfwrite (const void *ptr, sf_count_t count, void *user_data)
{ VIO_DATA *vf = (VIO_DATA *) user_data ;
/*
** This will break badly for files over 2Gig in length, but
** is sufficient for testing.
*/
if (vf->offset >= SIGNED_SIZEOF (vf->data))
return 0 ;
if (vf->offset + count > SIGNED_SIZEOF (vf->data))
count = sizeof (vf->data) - vf->offset ;
memcpy (vf->data + vf->offset, ptr, (size_t) count) ;
vf->offset += count ;
if (vf->offset > vf->length)
vf->length = vf->offset ;
return count ;
} /* vfwrite */
static sf_count_t
vftell (void *user_data)
{ VIO_DATA *vf = (VIO_DATA *) user_data ;
return vf->offset ;
} /* vftell */
/*==============================================================================
*/
static void
gen_short_data (short * data, int len, int start)
{ int k ;
for (k = 0 ; k < len ; k++)
data [k] = start + k ;
} /* gen_short_data */
static void
check_short_data (short * data, int len, int start, int line)
{ int k ;
for (k = 0 ; k < len ; k++)
if (data [k] != start + k)
{ printf ("\n\nLine %d : data [%d] = %d (should be %d).\n\n", line, k, data [k], start + k) ;
exit (1) ;
} ;
} /* gen_short_data */
/*------------------------------------------------------------------------------
*/
static void
vio_test (const char *fname, int format)
{ static VIO_DATA vio_data ;
static short data [256] ;
SF_VIRTUAL_IO vio ;
SNDFILE * file ;
SF_INFO sfinfo ;
print_test_name ("virtual i/o test", fname) ;
/* Set up pointers to the locally defined functions. */
vio.get_filelen = vfget_filelen ;
vio.seek = vfseek ;
vio.read = vfread ;
vio.write = vfwrite ;
vio.tell = vftell ;
/* Set virtual file offset and length to zero. */
vio_data.offset = 0 ;
vio_data.length = 0 ;
memset (&sfinfo, 0, sizeof (sfinfo)) ;
sfinfo.format = format ;
sfinfo.channels = 2 ;
sfinfo.samplerate = 44100 ;
if ((file = sf_open_virtual (&vio, SFM_WRITE, &sfinfo, &vio_data)) == NULL)
{ printf ("\n\nLine %d : sf_open_write failed with error : ", __LINE__) ;
fflush (stdout) ;
puts (sf_strerror (NULL)) ;
exit (1) ;
} ;
if (vfget_filelen (&vio_data) < 0)
{ printf ("\n\nLine %d : vfget_filelen returned negative length.\n\n", __LINE__) ;
exit (1) ;
} ;
gen_short_data (data, ARRAY_LEN (data), 0) ;
sf_write_short (file, data, ARRAY_LEN (data)) ;
gen_short_data (data, ARRAY_LEN (data), 1) ;
sf_write_short (file, data, ARRAY_LEN (data)) ;
gen_short_data (data, ARRAY_LEN (data), 2) ;
sf_write_short (file, data, ARRAY_LEN (data)) ;
sf_close (file) ;
/* Now test read. */
memset (&sfinfo, 0, sizeof (sfinfo)) ;
vio_data.offset = 0 ;
if ((file = sf_open_virtual (&vio, SFM_READ, &sfinfo, &vio_data)) == NULL)
{ printf ("\n\nLine %d : sf_open_write failed with error : ", __LINE__) ;
fflush (stdout) ;
puts (sf_strerror (NULL)) ;
dump_data_to_file (fname, vio_data.data, vio_data.length) ;
exit (1) ;
} ;
sf_read_short (file, data, ARRAY_LEN (data)) ;
check_short_data (data, ARRAY_LEN (data), 0, __LINE__) ;
sf_read_short (file, data, ARRAY_LEN (data)) ;
check_short_data (data, ARRAY_LEN (data), 1, __LINE__) ;
sf_read_short (file, data, ARRAY_LEN (data)) ;
check_short_data (data, ARRAY_LEN (data), 2, __LINE__) ;
sf_close (file) ;
puts ("ok") ;
} /* vio_test */
|