dither_test.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. ** Copyright (C) 2003-2013 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 <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <math.h>
  23. #include <sndfile.h>
  24. #include "utils.h"
  25. #define BUFFER_LEN (1 << 16)
  26. #define LOG_BUFFER_SIZE 1024
  27. static void dither_test (const char *filename, int filetype) ;
  28. /* Force the start of this buffer to be double aligned. Sparc-solaris will
  29. ** choke if its not.
  30. */
  31. static short data_out [BUFFER_LEN] ;
  32. int
  33. main (int argc, char *argv [])
  34. { int do_all = 0 ;
  35. int test_count = 0 ;
  36. if (argc != 2)
  37. { printf ("Usage : %s <test>\n", argv [0]) ;
  38. printf (" Where <test> is one of the following:\n") ;
  39. printf (" wav - test WAV file peak chunk\n") ;
  40. printf (" aiff - test AIFF file PEAK chunk\n") ;
  41. printf (" all - perform all tests\n") ;
  42. exit (1) ;
  43. } ;
  44. do_all = ! strcmp (argv [1], "all") ;
  45. if (do_all || ! strcmp (argv [1], "wav"))
  46. { dither_test ("dither.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8) ;
  47. test_count++ ;
  48. } ;
  49. if (do_all || ! strcmp (argv [1], "aiff"))
  50. { dither_test ("dither.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_S8) ;
  51. test_count++ ;
  52. } ;
  53. if (do_all || ! strcmp (argv [1], "au"))
  54. { dither_test ("dither.au", SF_FORMAT_AU | SF_FORMAT_PCM_S8) ;
  55. test_count++ ;
  56. } ;
  57. if (do_all || ! strcmp (argv [1], "svx"))
  58. { dither_test ("dither.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_S8) ;
  59. test_count++ ;
  60. } ;
  61. if (do_all || ! strcmp (argv [1], "nist"))
  62. { dither_test ("dither.nist", SF_FORMAT_NIST | SF_FORMAT_PCM_S8) ;
  63. test_count++ ;
  64. } ;
  65. if (do_all || ! strcmp (argv [1], "paf"))
  66. { dither_test ("dither.paf", SF_FORMAT_PAF | SF_FORMAT_PCM_S8) ;
  67. test_count++ ;
  68. } ;
  69. if (do_all || ! strcmp (argv [1], "ircam"))
  70. { dither_test ("dither.ircam", SF_FORMAT_IRCAM | SF_FORMAT_PCM_S8) ;
  71. test_count++ ;
  72. } ;
  73. if (do_all || ! strcmp (argv [1], "voc"))
  74. { dither_test ("dither.voc", SF_FORMAT_VOC | SF_FORMAT_PCM_S8) ;
  75. test_count++ ;
  76. } ;
  77. if (do_all || ! strcmp (argv [1], "w64"))
  78. { dither_test ("dither.w64", SF_FORMAT_W64 | SF_FORMAT_PCM_S8) ;
  79. test_count++ ;
  80. } ;
  81. if (do_all || ! strcmp (argv [1], "mat4"))
  82. { dither_test ("dither.mat4", SF_FORMAT_MAT4 | SF_FORMAT_PCM_S8) ;
  83. test_count++ ;
  84. } ;
  85. if (do_all || ! strcmp (argv [1], "mat5"))
  86. { dither_test ("dither.mat5", SF_FORMAT_MAT5 | SF_FORMAT_PCM_S8) ;
  87. test_count++ ;
  88. } ;
  89. if (do_all || ! strcmp (argv [1], "pvf"))
  90. { dither_test ("dither.pvf", SF_FORMAT_PVF | SF_FORMAT_PCM_S8) ;
  91. test_count++ ;
  92. } ;
  93. if (test_count == 0)
  94. { printf ("Mono : ************************************\n") ;
  95. printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
  96. printf ("Mono : ************************************\n") ;
  97. return 1 ;
  98. } ;
  99. return 0 ;
  100. } /* main */
  101. /*============================================================================================
  102. ** Here are the test functions.
  103. */
  104. static void
  105. dither_test (const char *filename, int filetype)
  106. { SNDFILE *file ;
  107. SF_INFO sfinfo ;
  108. SF_DITHER_INFO dither ;
  109. print_test_name ("dither_test", filename) ;
  110. sfinfo.samplerate = 44100 ;
  111. sfinfo.format = filetype ;
  112. sfinfo.channels = 1 ;
  113. sfinfo.frames = 0 ;
  114. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  115. /* Check for old version of the dither API. */
  116. if (sf_command (file, SFC_SET_DITHER_ON_WRITE, NULL, SF_TRUE) == 0)
  117. { printf ("\n\nLine %d: Should have an error here but don't.\n\n", __LINE__) ;
  118. exit (1) ;
  119. } ;
  120. memset (&dither, 0, sizeof (dither)) ;
  121. dither.type = SFD_WHITE ;
  122. dither.level = 0 ;
  123. if (sf_command (file, SFC_SET_DITHER_ON_WRITE, &dither, sizeof (dither)) != 0)
  124. { printf ("\n\nLine %d: sf_command (SFC_SET_DITHER_ON_WRITE) returned error : %s\n\n",
  125. __LINE__, sf_strerror (file)) ;
  126. exit (1) ;
  127. } ;
  128. /* Write data to file. */
  129. test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ;
  130. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  131. sf_close (file) ;
  132. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  133. if (sfinfo.frames != BUFFER_LEN)
  134. { printf ("\n\nLine %d: Bad frame count %d (should be %d)\n\n", __LINE__, (int) sfinfo.frames, BUFFER_LEN) ;
  135. } ;
  136. sf_close (file) ;
  137. /*-unlink (filename) ;-*/
  138. puts ("ok") ;
  139. } /* dither_test */