stdin_test.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. ** Copyright (C) 2001-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. #if HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26. #include <sndfile.h>
  27. #include "utils.h"
  28. #define BUFFER_LEN (1 << 16)
  29. static void stdin_test (int typemajor, int count) ;
  30. int
  31. main (int argc, char *argv [])
  32. { int do_all = 0, test_count = 0 ;
  33. if (BUFFER_LEN < PIPE_TEST_LEN)
  34. { fprintf (stderr, "Error : BUFFER_LEN < PIPE_TEST_LEN.\n\n") ;
  35. exit (1) ;
  36. } ;
  37. if (argc != 2)
  38. { fprintf (stderr, "This program cannot be run by itself. It needs\n") ;
  39. fprintf (stderr, "to be run from the stdio_test program.\n") ;
  40. exit (1) ;
  41. } ;
  42. do_all = ! strcmp (argv [1], "all") ;
  43. if (do_all || ! strcmp (argv [1], "raw"))
  44. { stdin_test (SF_FORMAT_RAW, PIPE_TEST_LEN) ;
  45. test_count++ ;
  46. } ;
  47. if (do_all || ! strcmp (argv [1], "wav"))
  48. { stdin_test (SF_FORMAT_WAV, PIPE_TEST_LEN) ;
  49. test_count++ ;
  50. } ;
  51. if (do_all || ! strcmp (argv [1], "aiff"))
  52. { stdin_test (SF_FORMAT_AIFF, PIPE_TEST_LEN) ;
  53. test_count++ ;
  54. } ;
  55. if (do_all || ! strcmp (argv [1], "au"))
  56. { stdin_test (SF_FORMAT_AU, PIPE_TEST_LEN) ;
  57. test_count++ ;
  58. } ;
  59. if (do_all || ! strcmp (argv [1], "paf"))
  60. { stdin_test (SF_FORMAT_PAF, PIPE_TEST_LEN) ;
  61. test_count++ ;
  62. } ;
  63. if (do_all || ! strcmp (argv [1], "svx"))
  64. { stdin_test (SF_FORMAT_SVX, PIPE_TEST_LEN) ;
  65. test_count++ ;
  66. } ;
  67. if (do_all || ! strcmp (argv [1], "nist"))
  68. { stdin_test (SF_FORMAT_NIST, PIPE_TEST_LEN) ;
  69. test_count++ ;
  70. } ;
  71. if (do_all || ! strcmp (argv [1], "ircam"))
  72. { stdin_test (SF_FORMAT_IRCAM, PIPE_TEST_LEN) ;
  73. test_count++ ;
  74. } ;
  75. if (do_all || ! strcmp (argv [1], "voc"))
  76. { stdin_test (SF_FORMAT_VOC, PIPE_TEST_LEN) ;
  77. test_count++ ;
  78. } ;
  79. if (do_all || ! strcmp (argv [1], "w64"))
  80. { stdin_test (SF_FORMAT_W64, PIPE_TEST_LEN) ;
  81. test_count++ ;
  82. } ;
  83. if (do_all || ! strcmp (argv [1], "mat4"))
  84. { stdin_test (SF_FORMAT_MAT4, PIPE_TEST_LEN) ;
  85. test_count++ ;
  86. } ;
  87. if (do_all || ! strcmp (argv [1], "mat5"))
  88. { stdin_test (SF_FORMAT_MAT5, PIPE_TEST_LEN) ;
  89. test_count++ ;
  90. } ;
  91. if (do_all || ! strcmp (argv [1], "pvf"))
  92. { stdin_test (SF_FORMAT_PVF, PIPE_TEST_LEN) ;
  93. test_count++ ;
  94. } ;
  95. if (do_all || ! strcmp (argv [1], "htk"))
  96. { stdin_test (SF_FORMAT_HTK, PIPE_TEST_LEN) ;
  97. test_count++ ;
  98. } ;
  99. if (test_count == 0)
  100. { fprintf (stderr, "\n*****************************************\n") ;
  101. fprintf (stderr, "* stdin_test : No '%s' test defined.\n", argv [1]) ;
  102. fprintf (stderr, "*****************************************\n") ;
  103. return 1 ;
  104. } ;
  105. return 0 ;
  106. } /* main */
  107. static void
  108. stdin_test (int typemajor, int count)
  109. { static short data [BUFFER_LEN] ;
  110. SNDFILE *file ;
  111. SF_INFO sfinfo ;
  112. int k, total, err ;
  113. if (typemajor == SF_FORMAT_RAW)
  114. { sfinfo.samplerate = 44100 ;
  115. sfinfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16 ;
  116. sfinfo.channels = 1 ;
  117. sfinfo.frames = 0 ;
  118. }
  119. else
  120. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  121. if ((file = sf_open_fd (STDIN_FILENO, SFM_READ, &sfinfo, SF_TRUE)) == NULL)
  122. { fprintf (stderr, "sf_open_fd failed with error : ") ;
  123. puts (sf_strerror (NULL)) ;
  124. dump_log_buffer (NULL) ;
  125. exit (1) ;
  126. } ;
  127. err = sf_error (file) ;
  128. if (err != SF_ERR_NO_ERROR)
  129. { printf ("Line %d : unexpected error : %s\n", __LINE__, sf_error_number (err)) ;
  130. exit (1) ;
  131. } ;
  132. if ((sfinfo.format & SF_FORMAT_TYPEMASK) != typemajor)
  133. { fprintf (stderr, "\n\nError : File type doesn't match.\n") ;
  134. exit (1) ;
  135. } ;
  136. if (sfinfo.samplerate != 44100)
  137. { fprintf (stderr, "\n\nError : Sample rate (%d) should be 44100\n", sfinfo.samplerate) ;
  138. exit (1) ;
  139. } ;
  140. if (sfinfo.channels != 1)
  141. { fprintf (stderr, "\n\nError : Channels (%d) should be 1\n", sfinfo.channels) ;
  142. exit (1) ;
  143. } ;
  144. if (sfinfo.frames < count)
  145. { fprintf (stderr, "\n\nError : Sample count (%ld) should be %d\n", (long) sfinfo.frames, count) ;
  146. exit (1) ;
  147. } ;
  148. total = 0 ;
  149. while ((k = sf_read_short (file, data + total, BUFFER_LEN - total)) > 0)
  150. total += k ;
  151. if (total != count)
  152. { fprintf (stderr, "\n\nError : Expected %d frames, read %d.\n", count, total) ;
  153. exit (1) ;
  154. } ;
  155. for (k = 0 ; k < total ; k++)
  156. if (data [k] != PIPE_INDEX (k))
  157. { printf ("\n\nError : data [%d] == %d, should have been %d.\n\n", k, data [k], k) ;
  158. exit (1) ;
  159. } ;
  160. sf_close (file) ;
  161. return ;
  162. } /* stdin_test */