dwvw_test.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. ** Copyright (C) 2002-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. #define BUFFER_SIZE (10000)
  31. #ifndef M_PI
  32. #define M_PI 3.14159265358979323846264338
  33. #endif
  34. static void dwvw_test (const char *filename, int format, int bit_width) ;
  35. int
  36. main (void)
  37. {
  38. dwvw_test ("dwvw12.raw", SF_FORMAT_RAW | SF_FORMAT_DWVW_12, 12) ;
  39. dwvw_test ("dwvw16.raw", SF_FORMAT_RAW | SF_FORMAT_DWVW_16, 16) ;
  40. dwvw_test ("dwvw24.raw", SF_FORMAT_RAW | SF_FORMAT_DWVW_24, 24) ;
  41. return 0 ;
  42. } /* main */
  43. static void
  44. dwvw_test (const char *filename, int format, int bit_width)
  45. { static int write_buf [BUFFER_SIZE] ;
  46. static int read_buf [BUFFER_SIZE] ;
  47. SNDFILE *file ;
  48. SF_INFO sfinfo ;
  49. double value ;
  50. int k, bit_mask ;
  51. srand (123456) ;
  52. /* Only want to grab the top bit_width bits. */
  53. bit_mask = arith_shift_left (-1, 32 - bit_width) ;
  54. print_test_name ("dwvw_test", filename) ;
  55. sf_info_setup (&sfinfo, format, 44100, 1) ;
  56. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  57. /* Generate random.frames. */
  58. for (k = 0 ; k < BUFFER_SIZE / 2 ; k++)
  59. { value = 0x7FFFFFFF * sin (123.0 / sfinfo.samplerate * 2 * k * M_PI) ;
  60. write_buf [k] = bit_mask & lrint (value) ;
  61. } ;
  62. for ( ; k < BUFFER_SIZE ; k++)
  63. write_buf [k] = bit_mask & (arith_shift_left (rand (), 11) ^ (rand () >> 11)) ;
  64. sf_write_int (file, write_buf, BUFFER_SIZE) ;
  65. sf_close (file) ;
  66. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  67. if ((k = (int) sf_read_int (file, read_buf, BUFFER_SIZE)) != BUFFER_SIZE)
  68. { printf ("Error (line %d) : Only read %d/%d.frames.\n", __LINE__, k, BUFFER_SIZE) ;
  69. exit (1) ;
  70. }
  71. for (k = 0 ; k < BUFFER_SIZE ; k++)
  72. { if (read_buf [k] != write_buf [k])
  73. { printf ("Error (line %d) : %d != %d at position %d/%d\n", __LINE__,
  74. write_buf [k] >> (32 - bit_width), read_buf [k] >> (32 - bit_width),
  75. k, BUFFER_SIZE) ;
  76. oct_save_int (write_buf, read_buf, BUFFER_SIZE) ;
  77. exit (1) ;
  78. } ;
  79. } ;
  80. sf_close (file) ;
  81. unlink (filename) ;
  82. printf ("ok\n") ;
  83. return ;
  84. } /* dwvw_test */