/libs/libsndfile/tests/multi_file_test.c

https://github.com/tzuryby/mod_conference-admin · C · 239 lines · 160 code · 55 blank · 24 comment · 28 complexity · 1ee7e83310a6f05d6e01d866840bb6b0 MD5 · raw file

  1. /*
  2. ** Copyright (C) 1999-2009 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. #if HAVE_UNISTD_H
  22. #include <unistd.h>
  23. #endif
  24. #if (HAVE_DECL_S_IRGRP == 0)
  25. #include <sf_unistd.h>
  26. #endif
  27. #include <fcntl.h>
  28. #include <math.h>
  29. #include <string.h>
  30. #include <errno.h>
  31. #include <sys/stat.h>
  32. #include <sndfile.h>
  33. #include "utils.h"
  34. #define DATA_LENGTH (512)
  35. static void write_file_at_end (int fd, int filetype, int channels, int file_num) ;
  36. static void multi_file_test (const char *filename, int *formats, int format_count) ;
  37. static short data [DATA_LENGTH] ;
  38. static int wav_formats [] =
  39. { SF_FORMAT_WAV | SF_FORMAT_PCM_16,
  40. SF_FORMAT_WAV | SF_FORMAT_PCM_24,
  41. SF_FORMAT_WAV | SF_FORMAT_ULAW,
  42. SF_FORMAT_WAV | SF_FORMAT_ALAW,
  43. /* Lite remove start */
  44. SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM,
  45. SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM,
  46. /* Lite remove end */
  47. /*-SF_FORMAT_WAV | SF_FORMAT_GSM610 Doesn't work yet. -*/
  48. } ;
  49. static int aiff_formats [] =
  50. { SF_FORMAT_AIFF | SF_FORMAT_PCM_16,
  51. SF_FORMAT_AIFF | SF_FORMAT_PCM_24,
  52. SF_FORMAT_AIFF | SF_FORMAT_ULAW,
  53. SF_FORMAT_AIFF | SF_FORMAT_ALAW
  54. } ;
  55. static int au_formats [] =
  56. { SF_FORMAT_AU | SF_FORMAT_PCM_16,
  57. SF_FORMAT_AU | SF_FORMAT_PCM_24,
  58. SF_FORMAT_AU | SF_FORMAT_ULAW,
  59. SF_FORMAT_AU | SF_FORMAT_ALAW
  60. } ;
  61. static int verbose = SF_FALSE ;
  62. int
  63. main (int argc, char **argv)
  64. { int do_all = 0 ;
  65. int test_count = 0 ;
  66. if (argc == 3 && strcmp (argv [2], "-v") == 0)
  67. { verbose = SF_TRUE ;
  68. argc -- ;
  69. } ;
  70. if (argc != 2)
  71. { printf ("Usage : %s <test>\n", argv [0]) ;
  72. printf (" Where <test> is one of the following:\n") ;
  73. printf (" wav - test WAV file functions (little endian)\n") ;
  74. printf (" aiff - test AIFF file functions (big endian)\n") ;
  75. printf (" au - test AU file functions\n") ;
  76. #if 0
  77. printf (" svx - test 8SVX/16SV file functions\n") ;
  78. printf (" nist - test NIST Sphere file functions\n") ;
  79. printf (" ircam - test IRCAM file functions\n") ;
  80. printf (" voc - Create Voice file functions\n") ;
  81. printf (" w64 - Sonic Foundry's W64 file functions\n") ;
  82. #endif
  83. printf (" all - perform all tests\n") ;
  84. exit (1) ;
  85. } ;
  86. do_all = !strcmp (argv [1], "all") ;
  87. if (do_all || ! strcmp (argv [1], "wav"))
  88. { multi_file_test ("multi_wav.dat", wav_formats, ARRAY_LEN (wav_formats)) ;
  89. test_count++ ;
  90. } ;
  91. if (do_all || ! strcmp (argv [1], "aiff"))
  92. { multi_file_test ("multi_aiff.dat", aiff_formats, ARRAY_LEN (aiff_formats)) ;
  93. test_count++ ;
  94. } ;
  95. if (do_all || ! strcmp (argv [1], "au"))
  96. { multi_file_test ("multi_au.dat", au_formats, ARRAY_LEN (au_formats)) ;
  97. test_count++ ;
  98. } ;
  99. return 0 ;
  100. } /* main */
  101. /*======================================================================================
  102. */
  103. static void
  104. multi_file_test (const char *filename, int *formats, int format_count)
  105. { SNDFILE *sndfile ;
  106. SF_INFO sfinfo ;
  107. SF_EMBED_FILE_INFO embed_info ;
  108. sf_count_t filelen ;
  109. int fd, k, file_count = 0, open_perm ;
  110. print_test_name ("multi_file_test", filename) ;
  111. unlink (filename) ;
  112. open_perm = OS_IS_WIN32 ? 0 : S_IRUSR | S_IWUSR | S_IRGRP ;
  113. if ((fd = open (filename, O_RDWR | O_CREAT, open_perm)) < 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 = 0 ; embed_info.offset + embed_info.length < filelen ; )
  124. {
  125. file_count ++ ;
  126. if (verbose)
  127. { puts ("\n------------------------------------") ;
  128. printf ("This offset : %ld\n", SF_COUNT_TO_LONG (embed_info.offset + embed_info.length)) ;
  129. } ;
  130. if (lseek (fd, embed_info.offset + embed_info.length, SEEK_SET) < 0)
  131. { printf ("\n\nLine %d: lseek failed : %s\n", __LINE__, strerror (errno)) ;
  132. exit (1) ;
  133. } ;
  134. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  135. if ((sndfile = sf_open_fd (fd, SFM_READ, &sfinfo, SF_FALSE)) == NULL)
  136. { printf ("\n\nLine %d: sf_open_fd failed\n", __LINE__) ;
  137. printf ("Embedded file number : %d offset : %ld\n", file_count, SF_COUNT_TO_LONG (embed_info.offset)) ;
  138. puts (sf_strerror (sndfile)) ;
  139. dump_log_buffer (sndfile) ;
  140. exit (1) ;
  141. } ;
  142. sf_command (sndfile, SFC_GET_EMBED_FILE_INFO, &embed_info, sizeof (embed_info)) ;
  143. sf_close (sndfile) ;
  144. if (verbose)
  145. printf ("\nNext offset : %ld\nNext length : %ld\n", SF_COUNT_TO_LONG (embed_info.offset), SF_COUNT_TO_LONG (embed_info.length)) ;
  146. } ;
  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 */