floating_point_test.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /*
  2. ** Copyright (C) 1999-2011 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 <string.h>
  22. #include <math.h>
  23. #if HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26. #include <sndfile.h>
  27. #include "dft_cmp.h"
  28. #include "utils.h"
  29. #define SAMPLE_RATE 16000
  30. static void float_scaled_test (const char *filename, int allow_exit, int replace_float, int filetype, double target_snr) ;
  31. static void double_scaled_test (const char *filename, int allow_exit, int replace_float, int filetype, double target_snr) ;
  32. static void float_short_little_test (const char * filename) ;
  33. static void float_short_big_test (const char * filename) ;
  34. static void float_int_little_test (const char * filename) ;
  35. static void float_int_big_test (const char * filename) ;
  36. static void double_short_little_test (const char * filename) ;
  37. static void double_short_big_test (const char * filename) ;
  38. static void double_int_little_test (const char * filename) ;
  39. static void double_int_big_test (const char * filename) ;
  40. static double double_data [DFT_DATA_LENGTH] ;
  41. static double double_test [DFT_DATA_LENGTH] ;
  42. static float float_data [DFT_DATA_LENGTH] ;
  43. static float float_test [DFT_DATA_LENGTH] ;
  44. static double double_data [DFT_DATA_LENGTH] ;
  45. static short short_data [DFT_DATA_LENGTH] ;
  46. static int int_data [DFT_DATA_LENGTH] ;
  47. int
  48. main (int argc, char *argv [])
  49. { int allow_exit = 1 ;
  50. if (argc == 2 && ! strstr (argv [1], "no-exit"))
  51. allow_exit = 0 ;
  52. #if ((HAVE_LRINTF == 0) && (HAVE_LRINT_REPLACEMENT == 0))
  53. puts ("*** Cannot run this test on this platform because it lacks lrintf().") ;
  54. exit (0) ;
  55. #endif
  56. /* Float tests. */
  57. float_scaled_test ("float.raw", allow_exit, SF_FALSE, SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_FLOAT, -163.0) ;
  58. /* Test both signed and unsigned 8 bit files. */
  59. float_scaled_test ("pcm_s8.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_PCM_S8, -39.0) ;
  60. float_scaled_test ("pcm_u8.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_PCM_U8, -39.0) ;
  61. float_scaled_test ("pcm_16.raw", allow_exit, SF_FALSE, SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_PCM_16, -87.0) ;
  62. float_scaled_test ("pcm_24.raw", allow_exit, SF_FALSE, SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_24, -138.0) ;
  63. float_scaled_test ("pcm_32.raw", allow_exit, SF_FALSE, SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_PCM_32, -163.0) ;
  64. float_scaled_test ("ulaw.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_ULAW, -50.0) ;
  65. float_scaled_test ("alaw.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_ALAW, -49.0) ;
  66. float_scaled_test ("ima_adpcm.wav", allow_exit, SF_FALSE, SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM, -47.0) ;
  67. float_scaled_test ("ms_adpcm.wav" , allow_exit, SF_FALSE, SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM, -40.0) ;
  68. float_scaled_test ("gsm610.raw" , allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_GSM610, -33.0) ;
  69. float_scaled_test ("g721_32.au", allow_exit, SF_FALSE, SF_FORMAT_AU | SF_FORMAT_G721_32, -34.0) ;
  70. float_scaled_test ("g723_24.au", allow_exit, SF_FALSE, SF_FORMAT_AU | SF_FORMAT_G723_24, -34.0) ;
  71. float_scaled_test ("g723_40.au", allow_exit, SF_FALSE, SF_FORMAT_AU | SF_FORMAT_G723_40, -40.0) ;
  72. /* PAF files do not use the same encoding method for 24 bit PCM data as other file
  73. ** formats so we need to explicitly test it here.
  74. */
  75. float_scaled_test ("le_paf_24.paf", allow_exit, SF_FALSE, SF_ENDIAN_LITTLE | SF_FORMAT_PAF | SF_FORMAT_PCM_24, -149.0) ;
  76. float_scaled_test ("be_paf_24.paf", allow_exit, SF_FALSE, SF_ENDIAN_BIG | SF_FORMAT_PAF | SF_FORMAT_PCM_24, -149.0) ;
  77. float_scaled_test ("dwvw_12.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_DWVW_12, -64.0) ;
  78. float_scaled_test ("dwvw_16.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_DWVW_16, -92.0) ;
  79. float_scaled_test ("dwvw_24.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_DWVW_24, -151.0) ;
  80. float_scaled_test ("adpcm.vox", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM, -40.0) ;
  81. float_scaled_test ("dpcm_16.xi", allow_exit, SF_FALSE, SF_FORMAT_XI | SF_FORMAT_DPCM_16, -90.0) ;
  82. float_scaled_test ("dpcm_8.xi" , allow_exit, SF_FALSE, SF_FORMAT_XI | SF_FORMAT_DPCM_8 , -41.0) ;
  83. float_scaled_test ("pcm_s8.sds", allow_exit, SF_FALSE, SF_FORMAT_SDS | SF_FORMAT_PCM_S8, -90.0) ;
  84. float_scaled_test ("pcm_16.sds", allow_exit, SF_FALSE, SF_FORMAT_SDS | SF_FORMAT_PCM_16, -140.0) ;
  85. float_scaled_test ("pcm_24.sds", allow_exit, SF_FALSE, SF_FORMAT_SDS | SF_FORMAT_PCM_24, -170.0) ;
  86. #if HAVE_EXTERNAL_LIBS
  87. float_scaled_test ("flac_8.flac", allow_exit, SF_FALSE, SF_FORMAT_FLAC | SF_FORMAT_PCM_S8, -39.0) ;
  88. float_scaled_test ("flac_16.flac", allow_exit, SF_FALSE, SF_FORMAT_FLAC | SF_FORMAT_PCM_16, -87.0) ;
  89. float_scaled_test ("flac_24.flac", allow_exit, SF_FALSE, SF_FORMAT_FLAC | SF_FORMAT_PCM_24, -138.0) ;
  90. float_scaled_test ("vorbis.oga", allow_exit, SF_FALSE, SF_FORMAT_OGG | SF_FORMAT_VORBIS, -31.0) ;
  91. #endif
  92. float_scaled_test ("replace_float.raw", allow_exit, SF_TRUE, SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_FLOAT, -163.0) ;
  93. /*==============================================================================
  94. ** Double tests.
  95. */
  96. double_scaled_test ("double.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_DOUBLE, -300.0) ;
  97. /* Test both signed (AIFF) and unsigned (WAV) 8 bit files. */
  98. double_scaled_test ("pcm_s8.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_PCM_S8, -39.0) ;
  99. double_scaled_test ("pcm_u8.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_PCM_U8, -39.0) ;
  100. double_scaled_test ("pcm_16.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_PCM_16, -87.0) ;
  101. double_scaled_test ("pcm_24.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_PCM_24, -135.0) ;
  102. double_scaled_test ("pcm_32.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_PCM_32, -184.0) ;
  103. double_scaled_test ("ulaw.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_ULAW, -50.0) ;
  104. double_scaled_test ("alaw.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_ALAW, -49.0) ;
  105. double_scaled_test ("ima_adpcm.wav", allow_exit, SF_FALSE, SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM, -47.0) ;
  106. double_scaled_test ("ms_adpcm.wav" , allow_exit, SF_FALSE, SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM, -40.0) ;
  107. double_scaled_test ("gsm610.raw" , allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_GSM610, -33.0) ;
  108. double_scaled_test ("g721_32.au", allow_exit, SF_FALSE, SF_FORMAT_AU | SF_FORMAT_G721_32, -34.0) ;
  109. double_scaled_test ("g723_24.au", allow_exit, SF_FALSE, SF_FORMAT_AU | SF_FORMAT_G723_24, -34.0) ;
  110. double_scaled_test ("g723_40.au", allow_exit, SF_FALSE, SF_FORMAT_AU | SF_FORMAT_G723_40, -40.0) ;
  111. /* 24 bit PCM PAF files tested here. */
  112. double_scaled_test ("be_paf_24.paf", allow_exit, SF_FALSE, SF_ENDIAN_BIG | SF_FORMAT_PAF | SF_FORMAT_PCM_24, -151.0) ;
  113. double_scaled_test ("le_paf_24.paf", allow_exit, SF_FALSE, SF_ENDIAN_LITTLE | SF_FORMAT_PAF | SF_FORMAT_PCM_24, -151.0) ;
  114. double_scaled_test ("dwvw_12.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_DWVW_12, -64.0) ;
  115. double_scaled_test ("dwvw_16.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_DWVW_16, -92.0) ;
  116. double_scaled_test ("dwvw_24.raw", allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_DWVW_24, -151.0) ;
  117. double_scaled_test ("adpcm.vox" , allow_exit, SF_FALSE, SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM, -40.0) ;
  118. double_scaled_test ("dpcm_16.xi", allow_exit, SF_FALSE, SF_FORMAT_XI | SF_FORMAT_DPCM_16, -90.0) ;
  119. double_scaled_test ("dpcm_8.xi" , allow_exit, SF_FALSE, SF_FORMAT_XI | SF_FORMAT_DPCM_8 , -42.0) ;
  120. double_scaled_test ("pcm_s8.sds", allow_exit, SF_FALSE, SF_FORMAT_SDS | SF_FORMAT_PCM_S8, -90.0) ;
  121. double_scaled_test ("pcm_16.sds", allow_exit, SF_FALSE, SF_FORMAT_SDS | SF_FORMAT_PCM_16, -140.0) ;
  122. double_scaled_test ("pcm_24.sds", allow_exit, SF_FALSE, SF_FORMAT_SDS | SF_FORMAT_PCM_24, -180.0) ;
  123. #if HAVE_EXTERNAL_LIBS
  124. double_scaled_test ("flac_8.flac", allow_exit, SF_FALSE, SF_FORMAT_FLAC | SF_FORMAT_PCM_S8, -39.0) ;
  125. double_scaled_test ("flac_16.flac", allow_exit, SF_FALSE, SF_FORMAT_FLAC | SF_FORMAT_PCM_16, -87.0) ;
  126. double_scaled_test ("flac_24.flac", allow_exit, SF_FALSE, SF_FORMAT_FLAC | SF_FORMAT_PCM_24, -138.0) ;
  127. double_scaled_test ("vorbis.oga", allow_exit, SF_FALSE, SF_FORMAT_OGG | SF_FORMAT_VORBIS, -29.0) ;
  128. #endif
  129. double_scaled_test ("replace_double.raw", allow_exit, SF_TRUE, SF_FORMAT_RAW | SF_FORMAT_DOUBLE, -300.0) ;
  130. putchar ('\n') ;
  131. /* Float int tests. */
  132. float_short_little_test ("float_short_little.au") ;
  133. float_short_big_test ("float_short_big.au") ;
  134. float_int_little_test ("float_int_little.au") ;
  135. float_int_big_test ("float_int_big.au") ;
  136. double_short_little_test ("double_short_little.au") ;
  137. double_short_big_test ("double_short_big.au") ;
  138. double_int_little_test ("double_int_little.au") ;
  139. double_int_big_test ("double_int_big.au") ;
  140. return 0 ;
  141. } /* main */
  142. /*============================================================================================
  143. * Here are the test functions.
  144. */
  145. static void
  146. float_scaled_test (const char *filename, int allow_exit, int replace_float, int filetype, double target_snr)
  147. { SNDFILE *file ;
  148. SF_INFO sfinfo ;
  149. double snr ;
  150. print_test_name ("float_scaled_test", filename) ;
  151. gen_windowed_sine_float (float_data, DFT_DATA_LENGTH, 1.0) ;
  152. sfinfo.samplerate = SAMPLE_RATE ;
  153. sfinfo.frames = DFT_DATA_LENGTH ;
  154. sfinfo.channels = 1 ;
  155. sfinfo.format = filetype ;
  156. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  157. sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ;
  158. test_write_float_or_die (file, 0, float_data, DFT_DATA_LENGTH, __LINE__) ;
  159. sf_close (file) ;
  160. memset (float_test, 0, sizeof (float_test)) ;
  161. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  162. sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ;
  163. exit_if_true (sfinfo.format != filetype, "\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
  164. exit_if_true (sfinfo.frames < DFT_DATA_LENGTH, "\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ;
  165. exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
  166. check_log_buffer_or_die (file, __LINE__) ;
  167. test_read_float_or_die (file, 0, float_test, DFT_DATA_LENGTH, __LINE__) ;
  168. sf_close (file) ;
  169. snr = dft_cmp_float (__LINE__, float_data, float_test, DFT_DATA_LENGTH, target_snr, allow_exit) ;
  170. exit_if_true (snr > target_snr, "% 6.1fdB SNR\n\n Error : should be better than % 6.1fdB\n\n", snr, target_snr) ;
  171. printf ("% 6.1fdB SNR ... ok\n", snr) ;
  172. unlink (filename) ;
  173. return ;
  174. } /* float_scaled_test */
  175. static void
  176. double_scaled_test (const char *filename, int allow_exit, int replace_float, int filetype, double target_snr)
  177. { SNDFILE *file ;
  178. SF_INFO sfinfo ;
  179. double snr ;
  180. print_test_name ("double_scaled_test", filename) ;
  181. gen_windowed_sine_double (double_data, DFT_DATA_LENGTH, 0.95) ;
  182. sfinfo.samplerate = SAMPLE_RATE ;
  183. sfinfo.frames = DFT_DATA_LENGTH ;
  184. sfinfo.channels = 1 ;
  185. sfinfo.format = filetype ;
  186. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  187. sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ;
  188. test_write_double_or_die (file, 0, double_data, DFT_DATA_LENGTH, __LINE__) ;
  189. sf_close (file) ;
  190. memset (double_test, 0, sizeof (double_test)) ;
  191. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  192. sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ;
  193. exit_if_true (sfinfo.format != filetype, "\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
  194. exit_if_true (sfinfo.frames < DFT_DATA_LENGTH, "\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ;
  195. exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
  196. check_log_buffer_or_die (file, __LINE__) ;
  197. test_read_double_or_die (file, 0, double_test, DFT_DATA_LENGTH, __LINE__) ;
  198. sf_close (file) ;
  199. snr = dft_cmp_double (__LINE__, double_data, double_test, DFT_DATA_LENGTH, target_snr, allow_exit) ;
  200. exit_if_true (snr > target_snr, "% 6.1fdB SNR\n\n Error : should be better than % 6.1fdB\n\n", snr, target_snr) ;
  201. printf ("% 6.1fdB SNR ... ok\n", snr) ;
  202. unlink (filename) ;
  203. return ;
  204. } /* double_scaled_test */
  205. /*==============================================================================
  206. */
  207. static void
  208. float_short_little_test (const char * filename)
  209. { SNDFILE *file ;
  210. SF_INFO sfinfo ;
  211. unsigned k, max ;
  212. print_test_name ("float_short_little_test", filename) ;
  213. gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 0.98) ;
  214. sfinfo.samplerate = SAMPLE_RATE ;
  215. sfinfo.frames = ARRAY_LEN (short_data) ;
  216. sfinfo.channels = 1 ;
  217. sfinfo.format = SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT ;
  218. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  219. test_write_float_or_die (file, 0, float_data, ARRAY_LEN (float_data), __LINE__) ;
  220. sf_close (file) ;
  221. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  222. if (sfinfo.frames != ARRAY_LEN (float_data))
  223. { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ;
  224. exit (1) ;
  225. } ;
  226. if (sfinfo.channels != 1)
  227. { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
  228. exit (1) ;
  229. } ;
  230. sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
  231. test_read_short_or_die (file, 0, short_data, ARRAY_LEN (short_data), __LINE__) ;
  232. sf_close (file) ;
  233. max = 0 ;
  234. for (k = 0 ; k < ARRAY_LEN (short_data) ; k++)
  235. if ((unsigned) abs (short_data [k]) > max)
  236. max = abs (short_data [k]) ;
  237. if (1.0 * abs (max - 0x7FFF) / 0x7FFF > 0.01)
  238. { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFF) ;
  239. exit (1) ;
  240. } ;
  241. unlink (filename) ;
  242. puts ("ok") ;
  243. } /* float_short_little_test */
  244. static void
  245. float_short_big_test (const char * filename)
  246. { SNDFILE *file ;
  247. SF_INFO sfinfo ;
  248. unsigned k, max ;
  249. print_test_name ("float_short_big_test", filename) ;
  250. gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 0.98) ;
  251. sfinfo.samplerate = SAMPLE_RATE ;
  252. sfinfo.frames = ARRAY_LEN (short_data) ;
  253. sfinfo.channels = 1 ;
  254. sfinfo.format = SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_FLOAT ;
  255. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  256. test_write_float_or_die (file, 0, float_data, ARRAY_LEN (float_data), __LINE__) ;
  257. sf_close (file) ;
  258. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  259. if (sfinfo.frames != ARRAY_LEN (float_data))
  260. { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ;
  261. exit (1) ;
  262. } ;
  263. if (sfinfo.channels != 1)
  264. { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
  265. exit (1) ;
  266. } ;
  267. sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
  268. test_read_short_or_die (file, 0, short_data, ARRAY_LEN (short_data), __LINE__) ;
  269. sf_close (file) ;
  270. max = 0 ;
  271. for (k = 0 ; k < ARRAY_LEN (short_data) ; k++)
  272. if ((unsigned) abs (short_data [k]) > max)
  273. max = abs (short_data [k]) ;
  274. if (1.0 * abs (max - 0x7FFF) / 0x7FFF > 0.01)
  275. { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFF) ;
  276. exit (1) ;
  277. } ;
  278. unlink (filename) ;
  279. puts ("ok") ;
  280. } /* float_short_big_test */
  281. static void
  282. float_int_little_test (const char * filename)
  283. { SNDFILE *file ;
  284. SF_INFO sfinfo ;
  285. unsigned k, max ;
  286. print_test_name ("float_int_little_test", filename) ;
  287. gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 0.98) ;
  288. sfinfo.samplerate = SAMPLE_RATE ;
  289. sfinfo.frames = ARRAY_LEN (int_data) ;
  290. sfinfo.channels = 1 ;
  291. sfinfo.format = SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT ;
  292. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  293. test_write_float_or_die (file, 0, float_data, ARRAY_LEN (float_data), __LINE__) ;
  294. sf_close (file) ;
  295. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  296. if (sfinfo.frames != ARRAY_LEN (float_data))
  297. { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ;
  298. exit (1) ;
  299. } ;
  300. if (sfinfo.channels != 1)
  301. { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
  302. exit (1) ;
  303. } ;
  304. sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
  305. test_read_int_or_die (file, 0, int_data, ARRAY_LEN (int_data), __LINE__) ;
  306. sf_close (file) ;
  307. max = 0 ;
  308. for (k = 0 ; k < ARRAY_LEN (int_data) ; k++)
  309. if ((unsigned) abs (int_data [k]) > max)
  310. max = abs (int_data [k]) ;
  311. if (1.0 * abs (max - 0x7FFFFFFF) / 0x7FFFFFFF > 0.01)
  312. { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFFFFFF) ;
  313. exit (1) ;
  314. } ;
  315. unlink (filename) ;
  316. puts ("ok") ;
  317. } /* float_int_little_test */
  318. static void
  319. float_int_big_test (const char * filename)
  320. { SNDFILE *file ;
  321. SF_INFO sfinfo ;
  322. unsigned k, max ;
  323. print_test_name ("float_int_big_test", filename) ;
  324. gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 0.98) ;
  325. sfinfo.samplerate = SAMPLE_RATE ;
  326. sfinfo.frames = ARRAY_LEN (int_data) ;
  327. sfinfo.channels = 1 ;
  328. sfinfo.format = SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_FLOAT ;
  329. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  330. test_write_float_or_die (file, 0, float_data, ARRAY_LEN (float_data), __LINE__) ;
  331. sf_close (file) ;
  332. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  333. if (sfinfo.frames != ARRAY_LEN (float_data))
  334. { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ;
  335. exit (1) ;
  336. } ;
  337. if (sfinfo.channels != 1)
  338. { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
  339. exit (1) ;
  340. } ;
  341. sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
  342. test_read_int_or_die (file, 0, int_data, ARRAY_LEN (int_data), __LINE__) ;
  343. sf_close (file) ;
  344. max = 0 ;
  345. for (k = 0 ; k < ARRAY_LEN (int_data) ; k++)
  346. if ((unsigned) abs (int_data [k]) > max)
  347. max = abs (int_data [k]) ;
  348. if (1.0 * abs (max - 0x7FFFFFFF) / 0x7FFFFFFF > 0.01)
  349. { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFFFFFF) ;
  350. exit (1) ;
  351. } ;
  352. unlink (filename) ;
  353. puts ("ok") ;
  354. } /* float_int_big_test */
  355. static void
  356. double_short_little_test (const char * filename)
  357. { SNDFILE *file ;
  358. SF_INFO sfinfo ;
  359. unsigned k, max ;
  360. print_test_name ("double_short_little_test", filename) ;
  361. gen_windowed_sine_double (double_data, ARRAY_LEN (double_data), 0.98) ;
  362. sfinfo.samplerate = SAMPLE_RATE ;
  363. sfinfo.frames = ARRAY_LEN (short_data) ;
  364. sfinfo.channels = 1 ;
  365. sfinfo.format = SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE ;
  366. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  367. test_write_double_or_die (file, 0, double_data, ARRAY_LEN (double_data), __LINE__) ;
  368. sf_close (file) ;
  369. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  370. if (sfinfo.frames != ARRAY_LEN (double_data))
  371. { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ;
  372. exit (1) ;
  373. } ;
  374. if (sfinfo.channels != 1)
  375. { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
  376. exit (1) ;
  377. } ;
  378. sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
  379. test_read_short_or_die (file, 0, short_data, ARRAY_LEN (short_data), __LINE__) ;
  380. sf_close (file) ;
  381. max = 0 ;
  382. for (k = 0 ; k < ARRAY_LEN (short_data) ; k++)
  383. if ((unsigned) abs (short_data [k]) > max)
  384. max = abs (short_data [k]) ;
  385. if (1.0 * abs (max - 0x7FFF) / 0x7FFF > 0.01)
  386. { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFF) ;
  387. exit (1) ;
  388. } ;
  389. unlink (filename) ;
  390. puts ("ok") ;
  391. } /* double_short_little_test */
  392. static void
  393. double_short_big_test (const char * filename)
  394. { SNDFILE *file ;
  395. SF_INFO sfinfo ;
  396. unsigned k, max ;
  397. print_test_name ("double_short_big_test", filename) ;
  398. gen_windowed_sine_double (double_data, ARRAY_LEN (double_data), 0.98) ;
  399. sfinfo.samplerate = SAMPLE_RATE ;
  400. sfinfo.frames = ARRAY_LEN (short_data) ;
  401. sfinfo.channels = 1 ;
  402. sfinfo.format = SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_DOUBLE ;
  403. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  404. test_write_double_or_die (file, 0, double_data, ARRAY_LEN (double_data), __LINE__) ;
  405. sf_close (file) ;
  406. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  407. if (sfinfo.frames != ARRAY_LEN (double_data))
  408. { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ;
  409. exit (1) ;
  410. } ;
  411. if (sfinfo.channels != 1)
  412. { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
  413. exit (1) ;
  414. } ;
  415. sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
  416. test_read_short_or_die (file, 0, short_data, ARRAY_LEN (short_data), __LINE__) ;
  417. sf_close (file) ;
  418. max = 0 ;
  419. for (k = 0 ; k < ARRAY_LEN (short_data) ; k++)
  420. if ((unsigned) abs (short_data [k]) > max)
  421. max = abs (short_data [k]) ;
  422. if (1.0 * abs (max - 0x7FFF) / 0x7FFF > 0.01)
  423. { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFF) ;
  424. exit (1) ;
  425. } ;
  426. unlink (filename) ;
  427. puts ("ok") ;
  428. } /* double_short_big_test */
  429. static void
  430. double_int_little_test (const char * filename)
  431. { SNDFILE *file ;
  432. SF_INFO sfinfo ;
  433. unsigned k, max ;
  434. print_test_name ("double_int_little_test", filename) ;
  435. gen_windowed_sine_double (double_data, ARRAY_LEN (double_data), 0.98) ;
  436. sfinfo.samplerate = SAMPLE_RATE ;
  437. sfinfo.frames = ARRAY_LEN (int_data) ;
  438. sfinfo.channels = 1 ;
  439. sfinfo.format = SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE ;
  440. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  441. test_write_double_or_die (file, 0, double_data, ARRAY_LEN (double_data), __LINE__) ;
  442. sf_close (file) ;
  443. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  444. if (sfinfo.frames != ARRAY_LEN (double_data))
  445. { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ;
  446. exit (1) ;
  447. } ;
  448. if (sfinfo.channels != 1)
  449. { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
  450. exit (1) ;
  451. } ;
  452. sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
  453. test_read_int_or_die (file, 0, int_data, ARRAY_LEN (int_data), __LINE__) ;
  454. sf_close (file) ;
  455. max = 0 ;
  456. for (k = 0 ; k < ARRAY_LEN (int_data) ; k++)
  457. if ((unsigned) abs (int_data [k]) > max)
  458. max = abs (int_data [k]) ;
  459. if (1.0 * abs (max - 0x7FFFFFFF) / 0x7FFFFFFF > 0.01)
  460. { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFFFFFF) ;
  461. exit (1) ;
  462. } ;
  463. unlink (filename) ;
  464. puts ("ok") ;
  465. } /* double_int_little_test */
  466. static void
  467. double_int_big_test (const char * filename)
  468. { SNDFILE *file ;
  469. SF_INFO sfinfo ;
  470. unsigned k, max ;
  471. print_test_name ("double_int_big_test", filename) ;
  472. gen_windowed_sine_double (double_data, ARRAY_LEN (double_data), 0.98) ;
  473. sfinfo.samplerate = SAMPLE_RATE ;
  474. sfinfo.frames = ARRAY_LEN (int_data) ;
  475. sfinfo.channels = 1 ;
  476. sfinfo.format = SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_DOUBLE ;
  477. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  478. test_write_double_or_die (file, 0, double_data, ARRAY_LEN (double_data), __LINE__) ;
  479. sf_close (file) ;
  480. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  481. if (sfinfo.frames != ARRAY_LEN (double_data))
  482. { printf ("\n\nLine %d: Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), DFT_DATA_LENGTH) ;
  483. exit (1) ;
  484. } ;
  485. if (sfinfo.channels != 1)
  486. { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
  487. exit (1) ;
  488. } ;
  489. sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
  490. test_read_int_or_die (file, 0, int_data, ARRAY_LEN (int_data), __LINE__) ;
  491. sf_close (file) ;
  492. max = 0 ;
  493. for (k = 0 ; k < ARRAY_LEN (int_data) ; k++)
  494. if ((unsigned) abs (int_data [k]) > max)
  495. max = abs (int_data [k]) ;
  496. if (1.0 * abs (max - 0x7FFFFFFF) / 0x7FFFFFFF > 0.01)
  497. { printf ("\n\nLine %d: Bad maximum (%d should be %d).\n\n", __LINE__, max, 0x7FFFFFFF) ;
  498. exit (1) ;
  499. } ;
  500. unlink (filename) ;
  501. puts ("ok") ;
  502. } /* double_int_big_test */