multi_file_test.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. ** Copyright (C) 1999-2012 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 <string.h>
  22. #include <math.h>
  23. #include <inttypes.h>
  24. #if HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. #if (HAVE_DECL_S_IRGRP == 0)
  28. #include <sf_unistd.h>
  29. #endif
  30. #include <fcntl.h>
  31. #include <errno.h>
  32. #include <sys/stat.h>
  33. #include <sndfile.h>
  34. #include "utils.h"
  35. #define DATA_LENGTH (512)
  36. static void write_file_at_end (int fd, int filetype, int channels, int file_num) ;
  37. static void multi_file_test (const char *filename, int *formats, int format_count) ;
  38. static short data [DATA_LENGTH] ;
  39. static int wav_formats [] =
  40. { SF_FORMAT_WAV | SF_FORMAT_PCM_16,
  41. SF_FORMAT_WAV | SF_FORMAT_PCM_24,
  42. SF_FORMAT_WAV | SF_FORMAT_ULAW,
  43. SF_FORMAT_WAV | SF_FORMAT_ALAW,
  44. /* Lite remove start */
  45. SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM,
  46. SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM,
  47. /* Lite remove end */
  48. /*-SF_FORMAT_WAV | SF_FORMAT_GSM610 Doesn't work yet. -*/
  49. } ;
  50. static int aiff_formats [] =
  51. { SF_FORMAT_AIFF | SF_FORMAT_PCM_16,
  52. SF_FORMAT_AIFF | SF_FORMAT_PCM_24,
  53. SF_FORMAT_AIFF | SF_FORMAT_ULAW,
  54. SF_FORMAT_AIFF | SF_FORMAT_ALAW
  55. } ;
  56. static int au_formats [] =
  57. { SF_FORMAT_AU | SF_FORMAT_PCM_16,
  58. SF_FORMAT_AU | SF_FORMAT_PCM_24,
  59. SF_FORMAT_AU | SF_FORMAT_ULAW,
  60. SF_FORMAT_AU | SF_FORMAT_ALAW
  61. } ;
  62. static int verbose = SF_FALSE ;
  63. int
  64. main (int argc, char **argv)
  65. { int do_all = 0 ;
  66. int test_count = 0 ;
  67. if (argc == 3 && strcmp (argv [2], "-v") == 0)
  68. { verbose = SF_TRUE ;
  69. argc -- ;
  70. } ;
  71. if (argc != 2)
  72. { printf ("Usage : %s <test>\n", argv [0]) ;
  73. printf (" Where <test> is one of the following:\n") ;
  74. printf (" wav - test WAV file functions (little endian)\n") ;
  75. printf (" aiff - test AIFF file functions (big endian)\n") ;
  76. printf (" au - test AU file functions\n") ;
  77. #if 0
  78. printf (" svx - test 8SVX/16SV file functions\n") ;
  79. printf (" nist - test NIST Sphere file functions\n") ;
  80. printf (" ircam - test IRCAM file functions\n") ;
  81. printf (" voc - Create Voice file functions\n") ;
  82. printf (" w64 - Sonic Foundry's W64 file functions\n") ;
  83. #endif
  84. printf (" all - perform all tests\n") ;
  85. exit (1) ;
  86. } ;
  87. do_all = !strcmp (argv [1], "all") ;
  88. if (do_all || ! strcmp (argv [1], "wav"))
  89. { multi_file_test ("multi_wav.dat", wav_formats, ARRAY_LEN (wav_formats)) ;
  90. test_count++ ;
  91. } ;
  92. if (do_all || ! strcmp (argv [1], "aiff"))
  93. { multi_file_test ("multi_aiff.dat", aiff_formats, ARRAY_LEN (aiff_formats)) ;
  94. test_count++ ;
  95. } ;
  96. if (do_all || ! strcmp (argv [1], "au"))
  97. { multi_file_test ("multi_au.dat", au_formats, ARRAY_LEN (au_formats)) ;
  98. test_count++ ;
  99. } ;
  100. return 0 ;
  101. } /* main */
  102. /*======================================================================================
  103. */
  104. static void
  105. multi_file_test (const char *filename, int *formats, int format_count)
  106. { SNDFILE *sndfile ;
  107. SF_INFO sfinfo ;
  108. SF_EMBED_FILE_INFO embed_info ;
  109. sf_count_t filelen ;
  110. int fd, k, file_count = 0 ;
  111. print_test_name ("multi_file_test", filename) ;
  112. unlink (filename) ;
  113. if ((fd = open (filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0)
  114. { printf ("\n\nLine %d: open failed : %s\n", __LINE__, strerror (errno)) ;
  115. exit (1) ;
  116. } ;
  117. k = write (fd, "1234", 4) ;
  118. for (k = 0 ; k < format_count ; k++)
  119. write_file_at_end (fd, formats [k], 2, k) ;
  120. filelen = file_length_fd (fd) ;
  121. embed_info.offset = 4 ;
  122. embed_info.length = 0 ;
  123. for (file_count = 1 ; embed_info.offset + embed_info.length < filelen ; file_count ++)
  124. {
  125. if (verbose)
  126. { puts ("\n------------------------------------") ;
  127. printf ("This offset : %" PRId64 "\n", embed_info.offset + embed_info.length) ;
  128. } ;
  129. if (lseek (fd, embed_info.offset + embed_info.length, SEEK_SET) < 0)
  130. { printf ("\n\nLine %d: lseek failed : %s\n", __LINE__, strerror (errno)) ;
  131. exit (1) ;
  132. } ;
  133. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  134. if ((sndfile = sf_open_fd (fd, SFM_READ, &sfinfo, SF_FALSE)) == NULL)
  135. { printf ("\n\nLine %d: sf_open_fd failed\n", __LINE__) ;
  136. printf ("Embedded file number : %d offset : %" PRId64 "\n", file_count, embed_info.offset) ;
  137. puts (sf_strerror (sndfile)) ;
  138. dump_log_buffer (sndfile) ;
  139. exit (1) ;
  140. } ;
  141. sf_command (sndfile, SFC_GET_EMBED_FILE_INFO, &embed_info, sizeof (embed_info)) ;
  142. sf_close (sndfile) ;
  143. if (verbose)
  144. printf ("\nNext offset : %" PRId64 "\nNext length : %" PRId64 "\n", embed_info.offset, embed_info.length) ;
  145. } ;
  146. file_count -- ;
  147. if (file_count != format_count)
  148. { printf ("\n\nLine %d: file count (%d) not equal to %d.\n\n", __LINE__, file_count, format_count) ;
  149. printf ("Embedded file number : %d\n", file_count) ;
  150. exit (1) ;
  151. } ;
  152. close (fd) ;
  153. unlink (filename) ;
  154. printf ("ok\n") ;
  155. return ;
  156. } /* multi_file_test */
  157. /*======================================================================================
  158. */
  159. static void
  160. write_file_at_end (int fd, int filetype, int channels, int file_num)
  161. { SNDFILE *sndfile ;
  162. SF_INFO sfinfo ;
  163. int frames, k ;
  164. lseek (fd, 0, SEEK_END) ;
  165. for (k = 0 ; k < DATA_LENGTH ; k++)
  166. data [k] = k ;
  167. frames = DATA_LENGTH / channels ;
  168. sfinfo.format = filetype ;
  169. sfinfo.channels = channels ;
  170. sfinfo.samplerate = 44100 ;
  171. if ((sndfile = sf_open_fd (fd, SFM_WRITE, &sfinfo, SF_FALSE)) == NULL)
  172. { printf ("\n\nLine %d: sf_open_fd failed\n", __LINE__) ;
  173. printf ("Embedded file number : %d\n", file_num) ;
  174. puts (sf_strerror (sndfile)) ;
  175. dump_log_buffer (sndfile) ;
  176. exit (1) ;
  177. } ;
  178. if (sf_writef_short (sndfile, data, frames) != frames)
  179. { printf ("\n\nLine %d: short write\n", __LINE__) ;
  180. printf ("Embedded file number : %d\n", file_num) ;
  181. exit (1) ;
  182. } ;
  183. sf_close (sndfile) ;
  184. } /* write_file_at_end */