channel_test.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. ** Copyright (C) 2001-2015 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 <stdint.h>
  22. #include <inttypes.h>
  23. #include <string.h>
  24. #include <time.h>
  25. #if HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #else
  28. #include "sf_unistd.h"
  29. #endif
  30. #include <math.h>
  31. #include <sndfile.h>
  32. #include "utils.h"
  33. #define BUFFER_LEN (1 << 10)
  34. #define LOG_BUFFER_SIZE 1024
  35. static void channel_test (void) ;
  36. static double max_diff (const float *a, const float *b, unsigned int len, unsigned int * position) ;
  37. int
  38. main (void) // int argc, char *argv [])
  39. { channel_test () ;
  40. return 0 ;
  41. } /* main */
  42. /*============================================================================================
  43. ** Here are the test functions.
  44. */
  45. static void
  46. channel_test (void)
  47. { static float float_data [1024] ;
  48. static float read_float [1024] ;
  49. static int read_int [1024] ;
  50. static short read_short [1024] ;
  51. unsigned int ch, k, position = 0 ;
  52. gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 0.9) ;
  53. for (ch = 1 ; ch <= 8 ; ch++)
  54. { SNDFILE *file ;
  55. SF_INFO wsfinfo, rsfinfo ;
  56. sf_count_t wframes = ARRAY_LEN (float_data) / ch ;
  57. double maxdiff ;
  58. char filename [256] ;
  59. snprintf (filename, sizeof (filename), "chan_%d.wav", ch) ;
  60. print_test_name (__func__, filename) ;
  61. sf_info_setup (&wsfinfo, SF_FORMAT_WAV | SF_FORMAT_FLOAT, 48000, ch) ;
  62. sf_info_clear (&rsfinfo) ;
  63. /* Write the test file. */
  64. file = test_open_file_or_die (filename, SFM_WRITE, &wsfinfo, SF_FALSE, __LINE__) ;
  65. test_writef_float_or_die (file, 0, float_data, wframes, __LINE__) ;
  66. sf_close (file) ;
  67. /* Read it as float and test. */
  68. file = test_open_file_or_die (filename, SFM_READ, &rsfinfo, SF_FALSE, __LINE__) ;
  69. exit_if_true (rsfinfo.frames == 0,
  70. "\n\nLine %d : Frames in file %" PRId64 ".\n\n", __LINE__, rsfinfo.frames) ;
  71. exit_if_true (wframes != rsfinfo.frames,
  72. "\n\nLine %d : Wrote %" PRId64 ", read %" PRId64 " frames.\n\n", __LINE__, wframes, rsfinfo.frames) ;
  73. sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
  74. test_readf_float_or_die (file, 0, read_float, rsfinfo.frames, __LINE__) ;
  75. compare_float_or_die (float_data, read_float, ch * rsfinfo.frames, __LINE__) ;
  76. /* Read it as short and test. */
  77. test_seek_or_die (file, 0, SEEK_SET, 0, ch, __LINE__) ;
  78. test_readf_short_or_die (file, 0, read_short, rsfinfo.frames, __LINE__) ;
  79. for (k = 0 ; k < ARRAY_LEN (read_float) ; k++)
  80. read_float [k] = read_short [k] * (0.9 / 0x8000) ;
  81. maxdiff = max_diff (float_data, read_float, ch * rsfinfo.frames, &position) ;
  82. exit_if_true (maxdiff > 0.5, "\n\nLine %d : Max diff is %f at index %u\n\n", __LINE__, maxdiff, position) ;
  83. /* Read it as int and test. */
  84. test_seek_or_die (file, 0, SEEK_SET, 0, ch, __LINE__) ;
  85. test_readf_int_or_die (file, 0, read_int, rsfinfo.frames, __LINE__) ;
  86. for (k = 0 ; k < ARRAY_LEN (read_float) ; k++)
  87. read_float [k] = read_int [k] * (0.9 / 0x80000000) ;
  88. maxdiff = max_diff (float_data, read_float, ch * rsfinfo.frames, &position) ;
  89. exit_if_true (maxdiff > 0.5, "\n\nLine %d : Max diff is %f at index %u\n\n", __LINE__, maxdiff, position) ;
  90. sf_close (file) ;
  91. unlink (filename) ;
  92. printf ("ok\n") ;
  93. } ;
  94. return ;
  95. } /* channel_test */
  96. static double
  97. max_diff (const float *a, const float *b, unsigned int len, unsigned int * position)
  98. { double mdiff = 0.0, diff ;
  99. unsigned int k ;
  100. for (k = 0 ; k < len ; k++)
  101. { diff = fabs (a [k] - b [k]) ;
  102. if (diff > mdiff)
  103. { mdiff = diff ;
  104. *position = k ;
  105. } ;
  106. } ;
  107. return mdiff ;
  108. } /* max_diff */