stdout_test.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. ** Copyright (C) 2001-2014 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. static void stdout_test (int typemajor, int count) ;
  31. int
  32. main (int argc, char *argv [])
  33. { int do_all, test_count = 0 ;
  34. if (argc != 2)
  35. { fprintf (stderr, "This program cannot be run by itself. It needs\n") ;
  36. fprintf (stderr, "to be run from the stdio_test program.\n") ;
  37. exit (1) ;
  38. } ;
  39. do_all = ! strcmp (argv [1], "all") ;
  40. if (do_all || ! strcmp (argv [1], "raw"))
  41. { stdout_test (SF_FORMAT_RAW, PIPE_TEST_LEN) ;
  42. test_count ++ ;
  43. } ;
  44. if (do_all || ! strcmp (argv [1], "wav"))
  45. { stdout_test (SF_FORMAT_WAV, PIPE_TEST_LEN) ;
  46. test_count ++ ;
  47. } ;
  48. if (do_all || ! strcmp (argv [1], "aiff"))
  49. { stdout_test (SF_FORMAT_AIFF, PIPE_TEST_LEN) ;
  50. test_count ++ ;
  51. } ;
  52. if (do_all || ! strcmp (argv [1], "au"))
  53. { stdout_test (SF_FORMAT_AU, PIPE_TEST_LEN) ;
  54. test_count ++ ;
  55. } ;
  56. if (do_all || ! strcmp (argv [1], "paf"))
  57. { stdout_test (SF_FORMAT_PAF, PIPE_TEST_LEN) ;
  58. test_count ++ ;
  59. } ;
  60. if (do_all || ! strcmp (argv [1], "svx"))
  61. { stdout_test (SF_FORMAT_SVX, PIPE_TEST_LEN) ;
  62. test_count ++ ;
  63. } ;
  64. if (do_all || ! strcmp (argv [1], "nist"))
  65. { stdout_test (SF_FORMAT_NIST, PIPE_TEST_LEN) ;
  66. test_count ++ ;
  67. } ;
  68. if (do_all || ! strcmp (argv [1], "ircam"))
  69. { stdout_test (SF_FORMAT_IRCAM, PIPE_TEST_LEN) ;
  70. test_count ++ ;
  71. } ;
  72. if (do_all || ! strcmp (argv [1], "voc"))
  73. { stdout_test (SF_FORMAT_VOC, PIPE_TEST_LEN) ;
  74. test_count ++ ;
  75. } ;
  76. if (do_all || ! strcmp (argv [1], "w64"))
  77. { stdout_test (SF_FORMAT_W64, PIPE_TEST_LEN) ;
  78. test_count ++ ;
  79. } ;
  80. if (do_all || ! strcmp (argv [1], "mat4"))
  81. { stdout_test (SF_FORMAT_MAT4, PIPE_TEST_LEN) ;
  82. test_count ++ ;
  83. } ;
  84. if (do_all || ! strcmp (argv [1], "mat5"))
  85. { stdout_test (SF_FORMAT_MAT5, PIPE_TEST_LEN) ;
  86. test_count ++ ;
  87. } ;
  88. if (do_all || ! strcmp (argv [1], "pvf"))
  89. { stdout_test (SF_FORMAT_PVF, PIPE_TEST_LEN) ;
  90. test_count ++ ;
  91. } ;
  92. if (test_count == 0)
  93. { fprintf (stderr, "\n******************************************\n") ;
  94. fprintf (stderr, "* stdout_test : No '%s' test defined.\n", argv [1]) ;
  95. fprintf (stderr, "******************************************\n") ;
  96. return 1 ;
  97. } ;
  98. return 0 ;
  99. } /* main */
  100. static void
  101. stdout_test (int typemajor, int count)
  102. { static short data [PIPE_TEST_LEN] ;
  103. SNDFILE *file ;
  104. SF_INFO sfinfo ;
  105. int k, total, this_write ;
  106. sfinfo.samplerate = 44100 ;
  107. sfinfo.format = (typemajor | SF_FORMAT_PCM_16) ;
  108. sfinfo.channels = 1 ;
  109. sfinfo.frames = 0 ;
  110. /* Create some random data. */
  111. for (k = 0 ; k < PIPE_TEST_LEN ; k++)
  112. data [k] = PIPE_INDEX (k) ;
  113. if ((file = sf_open ("-", SFM_WRITE, &sfinfo)) == NULL)
  114. { fprintf (stderr, "%s % d: sf_open_write failed with error : %s\n",
  115. __func__, __LINE__, sf_strerror (NULL)) ;
  116. exit (1) ;
  117. } ;
  118. if (sfinfo.frames != 0)
  119. { fprintf (stderr, "%s % d: Frames is %d (should be 0).\n",
  120. __func__, __LINE__, (int) sfinfo.frames) ;
  121. exit (1) ;
  122. } ;
  123. total = 0 ;
  124. while (total < count)
  125. { this_write = (count - total > 1024) ? 1024 : count - total ;
  126. if ((k = (int) sf_write_short (file, data + total, this_write)) != this_write)
  127. { fprintf (stderr, "sf_write_short # %d failed with short write (%d -> %d)\n", count, this_write, k) ;
  128. exit (1) ;
  129. } ;
  130. total += k ;
  131. } ;
  132. sf_close (file) ;
  133. return ;
  134. } /* stdout_test */