channel_test.c 4.1 KB

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