2
0

virtual_io_test.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #include "sfconfig.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <fcntl.h>
  22. #include <math.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <sys/stat.h>
  26. #include <sndfile.h>
  27. #include "utils.h"
  28. static void vio_test (const char *fname, int format) ;
  29. int
  30. main (void)
  31. {
  32. vio_test ("vio_pcm16.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
  33. vio_test ("vio_pcm24.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_24) ;
  34. vio_test ("vio_float.au", SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
  35. vio_test ("vio_pcm24.paf", SF_FORMAT_PAF | SF_FORMAT_PCM_24) ;
  36. return 0 ;
  37. } /* main */
  38. /*==============================================================================
  39. */
  40. typedef struct
  41. { sf_count_t offset, length ;
  42. unsigned char data [16 * 1024] ;
  43. } VIO_DATA ;
  44. static sf_count_t
  45. vfget_filelen (void *user_data)
  46. { VIO_DATA *vf = (VIO_DATA *) user_data ;
  47. return vf->length ;
  48. } /* vfget_filelen */
  49. static sf_count_t
  50. vfseek (sf_count_t offset, int whence, void *user_data)
  51. { VIO_DATA *vf = (VIO_DATA *) user_data ;
  52. switch (whence)
  53. { case SEEK_SET :
  54. vf->offset = offset ;
  55. break ;
  56. case SEEK_CUR :
  57. vf->offset = vf->offset + offset ;
  58. break ;
  59. case SEEK_END :
  60. vf->offset = vf->length + offset ;
  61. break ;
  62. default :
  63. break ;
  64. } ;
  65. return vf->offset ;
  66. } /* vfseek */
  67. static sf_count_t
  68. vfread (void *ptr, sf_count_t count, void *user_data)
  69. { VIO_DATA *vf = (VIO_DATA *) user_data ;
  70. /*
  71. ** This will brack badly for files over 2Gig in length, but
  72. ** is sufficient for testing.
  73. */
  74. if (vf->offset + count > vf->length)
  75. count = vf->length - vf->offset ;
  76. memcpy (ptr, vf->data + vf->offset, count) ;
  77. vf->offset += count ;
  78. return count ;
  79. } /* vfread */
  80. static sf_count_t
  81. vfwrite (const void *ptr, sf_count_t count, void *user_data)
  82. { VIO_DATA *vf = (VIO_DATA *) user_data ;
  83. /*
  84. ** This will break badly for files over 2Gig in length, but
  85. ** is sufficient for testing.
  86. */
  87. if (vf->offset >= SIGNED_SIZEOF (vf->data))
  88. return 0 ;
  89. if (vf->offset + count > SIGNED_SIZEOF (vf->data))
  90. count = sizeof (vf->data) - vf->offset ;
  91. memcpy (vf->data + vf->offset, ptr, (size_t) count) ;
  92. vf->offset += count ;
  93. if (vf->offset > vf->length)
  94. vf->length = vf->offset ;
  95. return count ;
  96. } /* vfwrite */
  97. static sf_count_t
  98. vftell (void *user_data)
  99. { VIO_DATA *vf = (VIO_DATA *) user_data ;
  100. return vf->offset ;
  101. } /* vftell */
  102. /*==============================================================================
  103. */
  104. static void
  105. gen_short_data (short * data, int len, int start)
  106. { int k ;
  107. for (k = 0 ; k < len ; k++)
  108. data [k] = start + k ;
  109. } /* gen_short_data */
  110. static void
  111. check_short_data (short * data, int len, int start, int line)
  112. { int k ;
  113. for (k = 0 ; k < len ; k++)
  114. if (data [k] != start + k)
  115. { printf ("\n\nLine %d : data [%d] = %d (should be %d).\n\n", line, k, data [k], start + k) ;
  116. exit (1) ;
  117. } ;
  118. } /* gen_short_data */
  119. /*------------------------------------------------------------------------------
  120. */
  121. static void
  122. vio_test (const char *fname, int format)
  123. { static VIO_DATA vio_data ;
  124. static short data [256] ;
  125. SF_VIRTUAL_IO vio ;
  126. SNDFILE * file ;
  127. SF_INFO sfinfo ;
  128. print_test_name ("virtual i/o test", fname) ;
  129. /* Set up pointers to the locally defined functions. */
  130. vio.get_filelen = vfget_filelen ;
  131. vio.seek = vfseek ;
  132. vio.read = vfread ;
  133. vio.write = vfwrite ;
  134. vio.tell = vftell ;
  135. /* Set virtual file offset and length to zero. */
  136. vio_data.offset = 0 ;
  137. vio_data.length = 0 ;
  138. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  139. sfinfo.format = format ;
  140. sfinfo.channels = 2 ;
  141. sfinfo.samplerate = 44100 ;
  142. if ((file = sf_open_virtual (&vio, SFM_WRITE, &sfinfo, &vio_data)) == NULL)
  143. { printf ("\n\nLine %d : sf_open_write failed with error : ", __LINE__) ;
  144. fflush (stdout) ;
  145. puts (sf_strerror (NULL)) ;
  146. exit (1) ;
  147. } ;
  148. if (vfget_filelen (&vio_data) < 0)
  149. { printf ("\n\nLine %d : vfget_filelen returned negative length.\n\n", __LINE__) ;
  150. exit (1) ;
  151. } ;
  152. gen_short_data (data, ARRAY_LEN (data), 0) ;
  153. sf_write_short (file, data, ARRAY_LEN (data)) ;
  154. gen_short_data (data, ARRAY_LEN (data), 1) ;
  155. sf_write_short (file, data, ARRAY_LEN (data)) ;
  156. gen_short_data (data, ARRAY_LEN (data), 2) ;
  157. sf_write_short (file, data, ARRAY_LEN (data)) ;
  158. sf_close (file) ;
  159. /* Now test read. */
  160. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  161. vio_data.offset = 0 ;
  162. if ((file = sf_open_virtual (&vio, SFM_READ, &sfinfo, &vio_data)) == NULL)
  163. { printf ("\n\nLine %d : sf_open_write failed with error : ", __LINE__) ;
  164. fflush (stdout) ;
  165. puts (sf_strerror (NULL)) ;
  166. dump_data_to_file (fname, vio_data.data, vio_data.length) ;
  167. exit (1) ;
  168. } ;
  169. sf_read_short (file, data, ARRAY_LEN (data)) ;
  170. check_short_data (data, ARRAY_LEN (data), 0, __LINE__) ;
  171. sf_read_short (file, data, ARRAY_LEN (data)) ;
  172. check_short_data (data, ARRAY_LEN (data), 1, __LINE__) ;
  173. sf_read_short (file, data, ARRAY_LEN (data)) ;
  174. check_short_data (data, ARRAY_LEN (data), 2, __LINE__) ;
  175. sf_close (file) ;
  176. puts ("ok") ;
  177. } /* vio_test */