stdin_test.c 5.0 KB

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