fix_this.c 9.7 KB

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