2
0

fix_this.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. ** Copyright (C) 1999-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 <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <math.h>
  23. #include <inttypes.h>
  24. #include <sndfile.h>
  25. #include "utils.h"
  26. #define BUFFER_SIZE (1 << 14)
  27. #define SAMPLE_RATE (11025)
  28. #ifndef M_PI
  29. #define M_PI 3.14159265358979323846264338
  30. #endif
  31. static void lcomp_test_int (const char *str, const char *filename, int filetype, double margin) ;
  32. static int error_function (double data, double orig, double margin) ;
  33. static int decay_response (int k) ;
  34. static void gen_signal_double (double *data, double scale, int datalen) ;
  35. /* Force the start of these buffers to be double aligned. Sparc-solaris will
  36. ** choke if they are not.
  37. */
  38. typedef union
  39. { double d [BUFFER_SIZE + 1] ;
  40. int i [BUFFER_SIZE + 1] ;
  41. } BUFFER ;
  42. static BUFFER data_buffer ;
  43. static BUFFER orig_buffer ;
  44. int
  45. main (void)
  46. { const char *filename = "test.au" ;
  47. lcomp_test_int ("au_g721", filename, SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_G721_32, 0.06) ;
  48. return 0 ;
  49. } /* main */
  50. /*============================================================================================
  51. ** Here are the test functions.
  52. */
  53. static void
  54. lcomp_test_int (const char *str, const char *filename, int filetype, double margin)
  55. { SNDFILE *file ;
  56. SF_INFO sfinfo ;
  57. int k, m, *orig, *data ;
  58. sf_count_t datalen, seekpos ;
  59. int64_t sum_abs ;
  60. double scale ;
  61. printf ("\nThis is program is not part of the libsndfile test suite.\n\n") ;
  62. printf (" lcomp_test_int : %s ... ", str) ;
  63. fflush (stdout) ;
  64. datalen = BUFFER_SIZE ;
  65. scale = 1.0 * 0x10000 ;
  66. data = data_buffer.i ;
  67. orig = orig_buffer.i ;
  68. gen_signal_double (orig_buffer.d, 32000.0 * scale, datalen) ;
  69. for (k = 0 ; k < datalen ; k++)
  70. orig [k] = orig_buffer.d [k] ;
  71. sfinfo.samplerate = SAMPLE_RATE ;
  72. sfinfo.frames = 123456789 ; /* Ridiculous value. */
  73. sfinfo.channels = 1 ;
  74. sfinfo.format = filetype ;
  75. if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
  76. { printf ("sf_open_write failed with error : ") ;
  77. puts (sf_strerror (NULL)) ;
  78. exit (1) ;
  79. } ;
  80. if ((k = sf_writef_int (file, orig, datalen)) != datalen)
  81. { printf ("sf_writef_int failed with short write (%" PRId64 " => %d).\n", datalen, k) ;
  82. exit (1) ;
  83. } ;
  84. sf_close (file) ;
  85. memset (data, 0, datalen * sizeof (int)) ;
  86. if ((filetype & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
  87. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  88. if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
  89. { printf ("sf_open_read failed with error : ") ;
  90. puts (sf_strerror (NULL)) ;
  91. exit (1) ;
  92. } ;
  93. if ((sfinfo.format & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)) != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
  94. { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
  95. exit (1) ;
  96. } ;
  97. if (sfinfo.frames < datalen)
  98. { printf ("Too few.frames in file. (%" PRId64 " should be a little more than %" PRId64 ")\n", datalen, sfinfo.frames) ;
  99. exit (1) ;
  100. } ;
  101. if (sfinfo.frames > (datalen + datalen / 2))
  102. { printf ("Too many.frames in file. (%" PRId64 " should be a little more than %" PRId64 ")\n", datalen, sfinfo.frames) ;
  103. exit (1) ;
  104. } ;
  105. if (sfinfo.channels != 1)
  106. { printf ("Incorrect number of channels in file.\n") ;
  107. exit (1) ;
  108. } ;
  109. check_log_buffer_or_die (file, __LINE__) ;
  110. if ((k = sf_readf_int (file, data, datalen)) != datalen)
  111. { printf ("Line %d: short read (%d should be %" PRId64 ").\n", __LINE__, k, datalen) ;
  112. exit (1) ;
  113. } ;
  114. sum_abs = 0 ;
  115. for (k = 0 ; k < datalen ; k++)
  116. { if (error_function (data [k] / scale, orig [k] / scale, margin))
  117. { printf ("Line %d: Incorrect sample (#%d : %f should be %f).\n", __LINE__, k, data [k] / scale, orig [k] / scale) ;
  118. oct_save_int (orig, data, datalen) ;
  119. exit (1) ;
  120. } ;
  121. sum_abs += abs (data [k]) ;
  122. } ;
  123. if (sum_abs < 1.0)
  124. { printf ("Line %d: Signal is all zeros.\n", __LINE__) ;
  125. exit (1) ;
  126. } ;
  127. if ((k = sf_readf_int (file, data, datalen)) != sfinfo.frames - datalen)
  128. { printf ("Line %d: Incorrect read length (%" PRId64 " should be %d).\n", __LINE__, sfinfo.frames - datalen, k) ;
  129. exit (1) ;
  130. } ;
  131. /* This check is only for block based encoders which must append silence
  132. ** to the end of a file so as to fill out a block.
  133. */
  134. if ((sfinfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_MS_ADPCM)
  135. for (k = 0 ; k < sfinfo.frames - datalen ; k++)
  136. if (abs (data [k] / scale) > decay_response (k))
  137. { printf ("Line %d : Incorrect sample B (#%d : abs (%d) should be < %d).\n", __LINE__, k, data [k], decay_response (k)) ;
  138. exit (1) ;
  139. } ;
  140. if (! sfinfo.seekable)
  141. { printf ("ok\n") ;
  142. return ;
  143. } ;
  144. /* Now test sf_seek function. */
  145. if ((k = sf_seek (file, 0, SEEK_SET)) != 0)
  146. { printf ("Line %d: Seek to start of file failed (%d).\n", __LINE__, k) ;
  147. exit (1) ;
  148. } ;
  149. for (m = 0 ; m < 3 ; m++)
  150. { int n ;
  151. if ((k = sf_readf_int (file, data, 11)) != 11)
  152. { printf ("Line %d: Incorrect read length (11 => %d).\n", __LINE__, k) ;
  153. exit (1) ;
  154. } ;
  155. for (k = 0 ; k < 11 ; k++)
  156. if (error_function (data [k] / scale, orig [k + m * 11] / scale, margin))
  157. { printf ("Line %d: Incorrect sample (m = %d) (#%d : %d => %d).\n", __LINE__, m, k + m * 11, orig [k + m * 11], data [k]) ;
  158. for (n = 0 ; n < 1 ; n++)
  159. printf ("%d ", data [n]) ;
  160. printf ("\n") ;
  161. exit (1) ;
  162. } ;
  163. } ;
  164. seekpos = BUFFER_SIZE / 10 ;
  165. /* Check seek from start of file. */
  166. if ((k = sf_seek (file, seekpos, SEEK_SET)) != seekpos)
  167. { printf ("Seek to start of file + %" PRId64 " failed (%d).\n", seekpos, k) ;
  168. exit (1) ;
  169. } ;
  170. if ((k = sf_readf_int (file, data, 1)) != 1)
  171. { printf ("Line %d: sf_readf_int (file, data, 1) returned %d.\n", __LINE__, k) ;
  172. exit (1) ;
  173. } ;
  174. if (error_function ((double) data [0], (double) orig [seekpos], margin))
  175. { printf ("Line %d: sf_seek (SEEK_SET) followed by sf_readf_int failed (%d, %d).\n", __LINE__, orig [1], data [0]) ;
  176. exit (1) ;
  177. } ;
  178. if ((k = sf_seek (file, 0, SEEK_CUR)) != seekpos + 1)
  179. { printf ("Line %d: sf_seek (SEEK_CUR) with 0 offset failed (%d should be %" PRId64 ")\n", __LINE__, k, seekpos + 1) ;
  180. exit (1) ;
  181. } ;
  182. seekpos = sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ;
  183. k = sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ;
  184. sf_readf_int (file, data, 1) ;
  185. if (error_function ((double) data [0], (double) orig [seekpos], margin) || k != seekpos)
  186. { printf ("Line %d: sf_seek (forwards, SEEK_CUR) followed by sf_readf_int failed (%d, %d) (%d, %" PRId64 ").\n", __LINE__, data [0], orig [seekpos], k, seekpos + 1) ;
  187. exit (1) ;
  188. } ;
  189. seekpos = sf_seek (file, 0, SEEK_CUR) - 20 ;
  190. /* Check seek backward from current position. */
  191. k = sf_seek (file, -20, SEEK_CUR) ;
  192. sf_readf_int (file, data, 1) ;
  193. if (error_function ((double) data [0], (double) orig [seekpos], margin) || k != seekpos)
  194. { printf ("sf_seek (backwards, SEEK_CUR) followed by sf_readf_int failed (%d, %d) (%d, %" PRId64 ").\n", data [0], orig [seekpos], k, seekpos) ;
  195. exit (1) ;
  196. } ;
  197. /* Check that read past end of file returns number of items. */
  198. sf_seek (file, (int) sfinfo.frames, SEEK_SET) ;
  199. if ((k = sf_readf_int (file, data, datalen)) != 0)
  200. { printf ("Line %d: Return value from sf_readf_int past end of file incorrect (%d).\n", __LINE__, k) ;
  201. exit (1) ;
  202. } ;
  203. /* Check seek backward from end. */
  204. if ((k = sf_seek (file, 5 - (int) sfinfo.frames, SEEK_END)) != 5)
  205. { printf ("sf_seek (SEEK_END) returned %d instead of %d.\n", k, 5) ;
  206. exit (1) ;
  207. } ;
  208. sf_readf_int (file, data, 1) ;
  209. if (error_function (data [0] / scale, orig [5] / scale, margin))
  210. { printf ("Line %d: sf_seek (SEEK_END) followed by sf_readf_short failed (%d should be %d).\n", __LINE__, data [0], orig [5]) ;
  211. exit (1) ;
  212. } ;
  213. sf_close (file) ;
  214. printf ("ok\n") ;
  215. } /* lcomp_test_int */
  216. /*========================================================================================
  217. ** Auxiliary functions
  218. */
  219. #define SIGNAL_MAXVAL 30000.0
  220. #define DECAY_COUNT 800
  221. static int
  222. decay_response (int k)
  223. { if (k < 1)
  224. return (int) (1.2 * SIGNAL_MAXVAL) ;
  225. if (k > DECAY_COUNT)
  226. return 0 ;
  227. return (int) (1.2 * SIGNAL_MAXVAL * (DECAY_COUNT - k) / (1.0 * DECAY_COUNT)) ;
  228. } /* decay_response */
  229. static void
  230. gen_signal_double (double *data, double scale, int datalen)
  231. { int k, ramplen ;
  232. double amp = 0.0 ;
  233. ramplen = datalen / 18 ;
  234. for (k = 0 ; k < datalen ; k++)
  235. { if (k <= ramplen)
  236. amp = scale * k / ((double) ramplen) ;
  237. else if (k > datalen - ramplen)
  238. amp = scale * (datalen - k) / ((double) ramplen) ;
  239. data [k] = amp * (0.4 * sin (33.3 * 2.0 * M_PI * ((double) (k + 1)) / ((double) SAMPLE_RATE))
  240. + 0.3 * cos (201.1 * 2.0 * M_PI * ((double) (k + 1)) / ((double) SAMPLE_RATE))) ;
  241. } ;
  242. return ;
  243. } /* gen_signal_double */
  244. static int
  245. error_function (double data, double orig, double margin)
  246. { double error ;
  247. if (fabs (orig) <= 500.0)
  248. error = fabs (fabs (data) - fabs (orig)) / 2000.0 ;
  249. else if (fabs (orig) <= 1000.0)
  250. error = fabs (data - orig) / 3000.0 ;
  251. else
  252. error = fabs (data - orig) / fabs (orig) ;
  253. if (error > margin)
  254. { printf ("\n\n*******************\nError : %f\n", error) ;
  255. return 1 ;
  256. } ;
  257. return 0 ;
  258. } /* error_function */