scale_clip_test.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853
  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. #if HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #endif
  25. #include <math.h>
  26. #include <sndfile.h>
  27. #include "utils.h"
  28. #ifndef M_PI
  29. #define M_PI 3.14159265358979323846264338
  30. #endif
  31. #define HALF_BUFFER_SIZE (1 << 12)
  32. #define BUFFER_SIZE (2 * HALF_BUFFER_SIZE)
  33. #define SINE_AMP 1.1
  34. #define MAX_ERROR 0.0202
  35. static void flt_scale_clip_test_16 (const char *filename, int filetype, float maxval) ;
  36. static void flt_scale_clip_test_24 (const char *filename, int filetype, float maxval) ;
  37. static void flt_scale_clip_test_32 (const char *filename, int filetype, float maxval) ;
  38. static void flt_scale_clip_test_08 (const char *filename, int filetype, float maxval) ;
  39. static void dbl_scale_clip_test_16 (const char *filename, int filetype, float maxval) ;
  40. static void dbl_scale_clip_test_24 (const char *filename, int filetype, float maxval) ;
  41. static void dbl_scale_clip_test_32 (const char *filename, int filetype, float maxval) ;
  42. static void dbl_scale_clip_test_08 (const char *filename, int filetype, float maxval) ;
  43. static void flt_short_clip_read_test (const char *filename, int filetype) ;
  44. static void flt_int_clip_read_test (const char *filename, int filetype) ;
  45. static void dbl_short_clip_read_test (const char *filename, int filetype) ;
  46. static void dbl_int_clip_read_test (const char *filename, int filetype) ;
  47. static void short_flt_scale_write_test (const char *filename, int filetype) ;
  48. static void short_dbl_scale_write_test (const char *filename, int filetype) ;
  49. static void int_flt_scale_write_test (const char *filename, int filetype) ;
  50. static void int_dbl_scale_write_test (const char *filename, int filetype) ;
  51. typedef union
  52. { double dbl [BUFFER_SIZE] ;
  53. float flt [BUFFER_SIZE] ;
  54. int i [BUFFER_SIZE] ;
  55. short s [BUFFER_SIZE] ;
  56. } BUFFER ;
  57. /* Data buffer. */
  58. static BUFFER buffer_out ;
  59. static BUFFER buffer_in ;
  60. int
  61. main (void)
  62. {
  63. flt_scale_clip_test_08 ("scale_clip_s8.au", SF_FORMAT_AU | SF_FORMAT_PCM_S8, 1.0 * 0x80) ;
  64. flt_scale_clip_test_08 ("scale_clip_u8.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8, 1.0 * 0x80) ;
  65. dbl_scale_clip_test_08 ("scale_clip_s8.au", SF_FORMAT_AU | SF_FORMAT_PCM_S8, 1.0 * 0x80) ;
  66. dbl_scale_clip_test_08 ("scale_clip_u8.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8, 1.0 * 0x80) ;
  67. /*
  68. ** Now use SF_FORMAT_AU where possible because it allows both
  69. ** big and little endian files.
  70. */
  71. flt_scale_clip_test_16 ("scale_clip_be16.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
  72. flt_scale_clip_test_16 ("scale_clip_le16.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
  73. flt_scale_clip_test_24 ("scale_clip_be24.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
  74. flt_scale_clip_test_24 ("scale_clip_le24.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
  75. flt_scale_clip_test_32 ("scale_clip_be32.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
  76. flt_scale_clip_test_32 ("scale_clip_le32.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
  77. dbl_scale_clip_test_16 ("scale_clip_be16.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
  78. dbl_scale_clip_test_16 ("scale_clip_le16.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
  79. dbl_scale_clip_test_24 ("scale_clip_be24.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
  80. dbl_scale_clip_test_24 ("scale_clip_le24.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
  81. dbl_scale_clip_test_32 ("scale_clip_be32.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
  82. dbl_scale_clip_test_32 ("scale_clip_le32.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
  83. flt_short_clip_read_test ("flt_short.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
  84. flt_int_clip_read_test ("flt_int.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
  85. dbl_short_clip_read_test ("dbl_short.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
  86. dbl_int_clip_read_test ("dbl_int.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
  87. short_flt_scale_write_test ("short_flt.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
  88. int_flt_scale_write_test ("int_flt.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
  89. short_dbl_scale_write_test ("short_dbl.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
  90. int_dbl_scale_write_test ("int_dbl.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
  91. return 0 ;
  92. } /* main */
  93. /*============================================================================================
  94. ** Here are the test functions.
  95. */
  96. static void
  97. flt_scale_clip_test_16 (const char *filename, int filetype, float maxval)
  98. { SNDFILE *file ;
  99. SF_INFO sfinfo ;
  100. int k ;
  101. float *data_out, *data_in ;
  102. double diff, clip_max_diff ;
  103. print_test_name ("flt_scale_clip_test_16", filename) ;
  104. data_out = buffer_out.flt ;
  105. data_in = buffer_in.flt ;
  106. for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
  107. { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
  108. data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
  109. } ;
  110. sfinfo.samplerate = 44100 ;
  111. sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
  112. sfinfo.channels = 1 ;
  113. sfinfo.format = filetype ;
  114. /*
  115. ** Write two versions of the data:
  116. ** normalized and clipped
  117. ** un-normalized and clipped.
  118. */
  119. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  120. sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
  121. test_write_float_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
  122. sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
  123. test_write_float_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
  124. sf_close (file) ;
  125. memset (&buffer_in, 0, sizeof (buffer_in)) ;
  126. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  127. sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
  128. if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
  129. { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
  130. exit (1) ;
  131. } ;
  132. if (sfinfo.frames != BUFFER_SIZE)
  133. { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
  134. exit (1) ;
  135. } ;
  136. if (sfinfo.channels != 1)
  137. { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
  138. exit (1) ;
  139. } ;
  140. check_log_buffer_or_die (file, __LINE__) ;
  141. test_read_float_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
  142. sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
  143. test_read_float_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
  144. sf_close (file) ;
  145. /* Check normalized version. */
  146. clip_max_diff = 0.0 ;
  147. for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
  148. { if (fabs (data_in [k]) > 1.0)
  149. { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
  150. exit (1) ;
  151. } ;
  152. if (data_out [k] * data_in [k] < 0.0)
  153. { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
  154. exit (1) ;
  155. } ;
  156. if (fabs (data_out [k]) > 1.0)
  157. continue ;
  158. diff = fabs (data_out [k] - data_in [k]) ;
  159. if (diff > clip_max_diff)
  160. clip_max_diff = diff ;
  161. } ;
  162. if (clip_max_diff < 1e-20)
  163. { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
  164. exit (1) ;
  165. } ;
  166. if (clip_max_diff > 1.0 / 0x8000)
  167. { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
  168. exit (1) ;
  169. } ;
  170. /* Check the un-normalized data. */
  171. clip_max_diff = 0.0 ;
  172. for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
  173. { if (fabs (data_in [k]) > maxval)
  174. { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
  175. exit (1) ;
  176. } ;
  177. if (data_out [k] * data_in [k] < 0.0)
  178. { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
  179. exit (1) ;
  180. } ;
  181. if (fabs (data_out [k]) > maxval)
  182. continue ;
  183. diff = fabs (data_out [k] - data_in [k]) ;
  184. if (diff > clip_max_diff)
  185. clip_max_diff = diff ;
  186. } ;
  187. if (clip_max_diff < 1e-20)
  188. { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
  189. exit (1) ;
  190. } ;
  191. if (clip_max_diff > 1.0)
  192. { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
  193. exit (1) ;
  194. } ;
  195. printf ("ok\n") ;
  196. unlink (filename) ;
  197. } /* flt_scale_clip_test_16 */
  198. static void
  199. flt_scale_clip_test_24 (const char *filename, int filetype, float maxval)
  200. { SNDFILE *file ;
  201. SF_INFO sfinfo ;
  202. int k ;
  203. float *data_out, *data_in ;
  204. double diff, clip_max_diff ;
  205. print_test_name ("flt_scale_clip_test_24", filename) ;
  206. data_out = buffer_out.flt ;
  207. data_in = buffer_in.flt ;
  208. for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
  209. { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
  210. data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
  211. } ;
  212. sfinfo.samplerate = 44100 ;
  213. sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
  214. sfinfo.channels = 1 ;
  215. sfinfo.format = filetype ;
  216. /*
  217. ** Write two versions of the data:
  218. ** normalized and clipped
  219. ** un-normalized and clipped.
  220. */
  221. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  222. sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
  223. test_write_float_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
  224. sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
  225. test_write_float_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
  226. sf_close (file) ;
  227. memset (&buffer_in, 0, sizeof (buffer_in)) ;
  228. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  229. sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
  230. if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
  231. { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
  232. exit (1) ;
  233. } ;
  234. if (sfinfo.frames != BUFFER_SIZE)
  235. { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
  236. exit (1) ;
  237. } ;
  238. if (sfinfo.channels != 1)
  239. { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
  240. exit (1) ;
  241. } ;
  242. check_log_buffer_or_die (file, __LINE__) ;
  243. test_read_float_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
  244. sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
  245. test_read_float_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
  246. sf_close (file) ;
  247. /* Check normalized version. */
  248. clip_max_diff = 0.0 ;
  249. for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
  250. { if (fabs (data_in [k]) > 1.0)
  251. { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
  252. exit (1) ;
  253. } ;
  254. if (data_out [k] * data_in [k] < 0.0)
  255. { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
  256. exit (1) ;
  257. } ;
  258. if (fabs (data_out [k]) > 1.0)
  259. continue ;
  260. diff = fabs (data_out [k] - data_in [k]) ;
  261. if (diff > clip_max_diff)
  262. clip_max_diff = diff ;
  263. } ;
  264. if (clip_max_diff < 1e-20)
  265. { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
  266. exit (1) ;
  267. } ;
  268. if (clip_max_diff > 1.0 / 0x800000)
  269. { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
  270. exit (1) ;
  271. } ;
  272. /* Check the un-normalized data. */
  273. clip_max_diff = 0.0 ;
  274. for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
  275. { if (fabs (data_in [k]) > maxval)
  276. { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
  277. exit (1) ;
  278. } ;
  279. if (data_out [k] * data_in [k] < 0.0)
  280. { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
  281. exit (1) ;
  282. } ;
  283. if (fabs (data_out [k]) > maxval)
  284. continue ;
  285. diff = fabs (data_out [k] - data_in [k]) ;
  286. if (diff > clip_max_diff)
  287. clip_max_diff = diff ;
  288. } ;
  289. if (clip_max_diff < 1e-20)
  290. { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
  291. exit (1) ;
  292. } ;
  293. if (clip_max_diff > 1.0)
  294. { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
  295. exit (1) ;
  296. } ;
  297. printf ("ok\n") ;
  298. unlink (filename) ;
  299. } /* flt_scale_clip_test_24 */
  300. static void
  301. flt_scale_clip_test_32 (const char *filename, int filetype, float maxval)
  302. { SNDFILE *file ;
  303. SF_INFO sfinfo ;
  304. int k ;
  305. float *data_out, *data_in ;
  306. double diff, clip_max_diff ;
  307. print_test_name ("flt_scale_clip_test_32", filename) ;
  308. data_out = buffer_out.flt ;
  309. data_in = buffer_in.flt ;
  310. for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
  311. { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
  312. data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
  313. } ;
  314. sfinfo.samplerate = 44100 ;
  315. sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
  316. sfinfo.channels = 1 ;
  317. sfinfo.format = filetype ;
  318. /*
  319. ** Write two versions of the data:
  320. ** normalized and clipped
  321. ** un-normalized and clipped.
  322. */
  323. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  324. sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
  325. test_write_float_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
  326. sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
  327. test_write_float_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
  328. sf_close (file) ;
  329. memset (&buffer_in, 0, sizeof (buffer_in)) ;
  330. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  331. sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
  332. if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
  333. { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
  334. exit (1) ;
  335. } ;
  336. if (sfinfo.frames != BUFFER_SIZE)
  337. { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
  338. exit (1) ;
  339. } ;
  340. if (sfinfo.channels != 1)
  341. { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
  342. exit (1) ;
  343. } ;
  344. check_log_buffer_or_die (file, __LINE__) ;
  345. test_read_float_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
  346. sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
  347. test_read_float_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
  348. sf_close (file) ;
  349. /* Check normalized version. */
  350. clip_max_diff = 0.0 ;
  351. for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
  352. { if (fabs (data_in [k]) > 1.0)
  353. { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
  354. exit (1) ;
  355. } ;
  356. if (data_out [k] * data_in [k] < 0.0)
  357. { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
  358. exit (1) ;
  359. } ;
  360. if (fabs (data_out [k]) > 1.0)
  361. continue ;
  362. diff = fabs (data_out [k] - data_in [k]) ;
  363. if (diff > clip_max_diff)
  364. clip_max_diff = diff ;
  365. } ;
  366. if (clip_max_diff < 1e-20)
  367. { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
  368. exit (1) ;
  369. } ;
  370. if (clip_max_diff > 1.0 / 0x80000000)
  371. { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
  372. exit (1) ;
  373. } ;
  374. /* Check the un-normalized data. */
  375. clip_max_diff = 0.0 ;
  376. for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
  377. { if (fabs (data_in [k]) > maxval)
  378. { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
  379. exit (1) ;
  380. } ;
  381. if (data_out [k] * data_in [k] < 0.0)
  382. { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
  383. exit (1) ;
  384. } ;
  385. if (fabs (data_out [k]) > maxval)
  386. continue ;
  387. diff = fabs (data_out [k] - data_in [k]) ;
  388. if (diff > clip_max_diff)
  389. clip_max_diff = diff ;
  390. } ;
  391. if (clip_max_diff < 1e-20)
  392. { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
  393. exit (1) ;
  394. } ;
  395. if (clip_max_diff > 1.0)
  396. { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
  397. exit (1) ;
  398. } ;
  399. printf ("ok\n") ;
  400. unlink (filename) ;
  401. } /* flt_scale_clip_test_32 */
  402. static void
  403. flt_scale_clip_test_08 (const char *filename, int filetype, float maxval)
  404. { SNDFILE *file ;
  405. SF_INFO sfinfo ;
  406. int k ;
  407. float *data_out, *data_in ;
  408. double diff, clip_max_diff ;
  409. print_test_name ("flt_scale_clip_test_08", filename) ;
  410. data_out = buffer_out.flt ;
  411. data_in = buffer_in.flt ;
  412. for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
  413. { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
  414. data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
  415. } ;
  416. sfinfo.samplerate = 44100 ;
  417. sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
  418. sfinfo.channels = 1 ;
  419. sfinfo.format = filetype ;
  420. /*
  421. ** Write two versions of the data:
  422. ** normalized and clipped
  423. ** un-normalized and clipped.
  424. */
  425. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  426. sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
  427. test_write_float_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
  428. sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
  429. test_write_float_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
  430. sf_close (file) ;
  431. memset (&buffer_in, 0, sizeof (buffer_in)) ;
  432. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  433. sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
  434. if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
  435. { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
  436. exit (1) ;
  437. } ;
  438. if (sfinfo.frames != BUFFER_SIZE)
  439. { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
  440. exit (1) ;
  441. } ;
  442. if (sfinfo.channels != 1)
  443. { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
  444. exit (1) ;
  445. } ;
  446. check_log_buffer_or_die (file, __LINE__) ;
  447. test_read_float_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
  448. sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
  449. test_read_float_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
  450. sf_close (file) ;
  451. /* Check normalized version. */
  452. clip_max_diff = 0.0 ;
  453. for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
  454. { if (fabs (data_in [k]) > 1.0)
  455. { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
  456. exit (1) ;
  457. } ;
  458. if (data_out [k] * data_in [k] < 0.0)
  459. { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
  460. exit (1) ;
  461. } ;
  462. if (fabs (data_out [k]) > 1.0)
  463. continue ;
  464. diff = fabs (data_out [k] - data_in [k]) ;
  465. if (diff > clip_max_diff)
  466. clip_max_diff = diff ;
  467. } ;
  468. if (clip_max_diff < 1e-20)
  469. { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
  470. exit (1) ;
  471. } ;
  472. if (clip_max_diff > 1.0 / 0x80)
  473. { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
  474. exit (1) ;
  475. } ;
  476. /* Check the un-normalized data. */
  477. clip_max_diff = 0.0 ;
  478. for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
  479. { if (fabs (data_in [k]) > maxval)
  480. { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
  481. exit (1) ;
  482. } ;
  483. if (data_out [k] * data_in [k] < 0.0)
  484. { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
  485. exit (1) ;
  486. } ;
  487. if (fabs (data_out [k]) > maxval)
  488. continue ;
  489. diff = fabs (data_out [k] - data_in [k]) ;
  490. if (diff > clip_max_diff)
  491. clip_max_diff = diff ;
  492. } ;
  493. if (clip_max_diff < 1e-20)
  494. { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
  495. exit (1) ;
  496. } ;
  497. if (clip_max_diff > 1.0)
  498. { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
  499. exit (1) ;
  500. } ;
  501. printf ("ok\n") ;
  502. unlink (filename) ;
  503. } /* flt_scale_clip_test_08 */
  504. static void
  505. dbl_scale_clip_test_16 (const char *filename, int filetype, float maxval)
  506. { SNDFILE *file ;
  507. SF_INFO sfinfo ;
  508. int k ;
  509. double *data_out, *data_in ;
  510. double diff, clip_max_diff ;
  511. print_test_name ("dbl_scale_clip_test_16", filename) ;
  512. data_out = buffer_out.dbl ;
  513. data_in = buffer_in.dbl ;
  514. for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
  515. { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
  516. data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
  517. } ;
  518. sfinfo.samplerate = 44100 ;
  519. sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
  520. sfinfo.channels = 1 ;
  521. sfinfo.format = filetype ;
  522. /*
  523. ** Write two versions of the data:
  524. ** normalized and clipped
  525. ** un-normalized and clipped.
  526. */
  527. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  528. sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
  529. test_write_double_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
  530. sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
  531. test_write_double_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
  532. sf_close (file) ;
  533. memset (&buffer_in, 0, sizeof (buffer_in)) ;
  534. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  535. sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
  536. if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
  537. { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
  538. exit (1) ;
  539. } ;
  540. if (sfinfo.frames != BUFFER_SIZE)
  541. { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
  542. exit (1) ;
  543. } ;
  544. if (sfinfo.channels != 1)
  545. { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
  546. exit (1) ;
  547. } ;
  548. check_log_buffer_or_die (file, __LINE__) ;
  549. test_read_double_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
  550. sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
  551. test_read_double_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
  552. sf_close (file) ;
  553. /* Check normalized version. */
  554. clip_max_diff = 0.0 ;
  555. for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
  556. { if (fabs (data_in [k]) > 1.0)
  557. { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
  558. exit (1) ;
  559. } ;
  560. if (data_out [k] * data_in [k] < 0.0)
  561. { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
  562. exit (1) ;
  563. } ;
  564. if (fabs (data_out [k]) > 1.0)
  565. continue ;
  566. diff = fabs (data_out [k] - data_in [k]) ;
  567. if (diff > clip_max_diff)
  568. clip_max_diff = diff ;
  569. } ;
  570. if (clip_max_diff < 1e-20)
  571. { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
  572. exit (1) ;
  573. } ;
  574. if (clip_max_diff > 1.0 / 0x8000)
  575. { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
  576. exit (1) ;
  577. } ;
  578. /* Check the un-normalized data. */
  579. clip_max_diff = 0.0 ;
  580. for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
  581. { if (fabs (data_in [k]) > maxval)
  582. { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
  583. exit (1) ;
  584. } ;
  585. if (data_out [k] * data_in [k] < 0.0)
  586. { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
  587. exit (1) ;
  588. } ;
  589. if (fabs (data_out [k]) > maxval)
  590. continue ;
  591. diff = fabs (data_out [k] - data_in [k]) ;
  592. if (diff > clip_max_diff)
  593. clip_max_diff = diff ;
  594. } ;
  595. if (clip_max_diff < 1e-20)
  596. { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
  597. exit (1) ;
  598. } ;
  599. if (clip_max_diff > 1.0)
  600. { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
  601. exit (1) ;
  602. } ;
  603. printf ("ok\n") ;
  604. unlink (filename) ;
  605. } /* dbl_scale_clip_test_16 */
  606. static void
  607. dbl_scale_clip_test_24 (const char *filename, int filetype, float maxval)
  608. { SNDFILE *file ;
  609. SF_INFO sfinfo ;
  610. int k ;
  611. double *data_out, *data_in ;
  612. double diff, clip_max_diff ;
  613. print_test_name ("dbl_scale_clip_test_24", filename) ;
  614. data_out = buffer_out.dbl ;
  615. data_in = buffer_in.dbl ;
  616. for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
  617. { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
  618. data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
  619. } ;
  620. sfinfo.samplerate = 44100 ;
  621. sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
  622. sfinfo.channels = 1 ;
  623. sfinfo.format = filetype ;
  624. /*
  625. ** Write two versions of the data:
  626. ** normalized and clipped
  627. ** un-normalized and clipped.
  628. */
  629. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  630. sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
  631. test_write_double_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
  632. sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
  633. test_write_double_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
  634. sf_close (file) ;
  635. memset (&buffer_in, 0, sizeof (buffer_in)) ;
  636. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  637. sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
  638. if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
  639. { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
  640. exit (1) ;
  641. } ;
  642. if (sfinfo.frames != BUFFER_SIZE)
  643. { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
  644. exit (1) ;
  645. } ;
  646. if (sfinfo.channels != 1)
  647. { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
  648. exit (1) ;
  649. } ;
  650. check_log_buffer_or_die (file, __LINE__) ;
  651. test_read_double_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
  652. sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
  653. test_read_double_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
  654. sf_close (file) ;
  655. /* Check normalized version. */
  656. clip_max_diff = 0.0 ;
  657. for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
  658. { if (fabs (data_in [k]) > 1.0)
  659. { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
  660. exit (1) ;
  661. } ;
  662. if (data_out [k] * data_in [k] < 0.0)
  663. { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
  664. exit (1) ;
  665. } ;
  666. if (fabs (data_out [k]) > 1.0)
  667. continue ;
  668. diff = fabs (data_out [k] - data_in [k]) ;
  669. if (diff > clip_max_diff)
  670. clip_max_diff = diff ;
  671. } ;
  672. if (clip_max_diff < 1e-20)
  673. { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
  674. exit (1) ;
  675. } ;
  676. if (clip_max_diff > 1.0 / 0x800000)
  677. { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
  678. exit (1) ;
  679. } ;
  680. /* Check the un-normalized data. */
  681. clip_max_diff = 0.0 ;
  682. for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
  683. { if (fabs (data_in [k]) > maxval)
  684. { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
  685. exit (1) ;
  686. } ;
  687. if (data_out [k] * data_in [k] < 0.0)
  688. { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
  689. exit (1) ;
  690. } ;
  691. if (fabs (data_out [k]) > maxval)
  692. continue ;
  693. diff = fabs (data_out [k] - data_in [k]) ;
  694. if (diff > clip_max_diff)
  695. clip_max_diff = diff ;
  696. } ;
  697. if (clip_max_diff < 1e-20)
  698. { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
  699. exit (1) ;
  700. } ;
  701. if (clip_max_diff > 1.0)
  702. { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
  703. exit (1) ;
  704. } ;
  705. printf ("ok\n") ;
  706. unlink (filename) ;
  707. } /* dbl_scale_clip_test_24 */
  708. static void
  709. dbl_scale_clip_test_32 (const char *filename, int filetype, float maxval)
  710. { SNDFILE *file ;
  711. SF_INFO sfinfo ;
  712. int k ;
  713. double *data_out, *data_in ;
  714. double diff, clip_max_diff ;
  715. print_test_name ("dbl_scale_clip_test_32", filename) ;
  716. data_out = buffer_out.dbl ;
  717. data_in = buffer_in.dbl ;
  718. for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
  719. { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
  720. data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
  721. } ;
  722. sfinfo.samplerate = 44100 ;
  723. sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
  724. sfinfo.channels = 1 ;
  725. sfinfo.format = filetype ;
  726. /*
  727. ** Write two versions of the data:
  728. ** normalized and clipped
  729. ** un-normalized and clipped.
  730. */
  731. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  732. sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
  733. test_write_double_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
  734. sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
  735. test_write_double_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
  736. sf_close (file) ;
  737. memset (&buffer_in, 0, sizeof (buffer_in)) ;
  738. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  739. sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
  740. if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
  741. { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
  742. exit (1) ;
  743. } ;
  744. if (sfinfo.frames != BUFFER_SIZE)
  745. { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
  746. exit (1) ;
  747. } ;
  748. if (sfinfo.channels != 1)
  749. { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
  750. exit (1) ;
  751. } ;
  752. check_log_buffer_or_die (file, __LINE__) ;
  753. test_read_double_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
  754. sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
  755. test_read_double_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
  756. sf_close (file) ;
  757. /* Check normalized version. */
  758. clip_max_diff = 0.0 ;
  759. for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
  760. { if (fabs (data_in [k]) > 1.0)
  761. { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
  762. exit (1) ;
  763. } ;
  764. if (data_out [k] * data_in [k] < 0.0)
  765. { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
  766. exit (1) ;
  767. } ;
  768. if (fabs (data_out [k]) > 1.0)
  769. continue ;
  770. diff = fabs (data_out [k] - data_in [k]) ;
  771. if (diff > clip_max_diff)
  772. clip_max_diff = diff ;
  773. } ;
  774. if (clip_max_diff < 1e-20)
  775. { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
  776. exit (1) ;
  777. } ;
  778. if (clip_max_diff > 1.0 / 0x80000000)
  779. { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
  780. exit (1) ;
  781. } ;
  782. /* Check the un-normalized data. */
  783. clip_max_diff = 0.0 ;
  784. for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
  785. { if (fabs (data_in [k]) > maxval)
  786. { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
  787. exit (1) ;
  788. } ;
  789. if (data_out [k] * data_in [k] < 0.0)
  790. { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
  791. exit (1) ;
  792. } ;
  793. if (fabs (data_out [k]) > maxval)
  794. continue ;
  795. diff = fabs (data_out [k] - data_in [k]) ;
  796. if (diff > clip_max_diff)
  797. clip_max_diff = diff ;
  798. } ;
  799. if (clip_max_diff < 1e-20)
  800. { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
  801. exit (1) ;
  802. } ;
  803. if (clip_max_diff > 1.0)
  804. { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
  805. exit (1) ;
  806. } ;
  807. printf ("ok\n") ;
  808. unlink (filename) ;
  809. } /* dbl_scale_clip_test_32 */
  810. static void
  811. dbl_scale_clip_test_08 (const char *filename, int filetype, float maxval)
  812. { SNDFILE *file ;
  813. SF_INFO sfinfo ;
  814. int k ;
  815. double *data_out, *data_in ;
  816. double diff, clip_max_diff ;
  817. print_test_name ("dbl_scale_clip_test_08", filename) ;
  818. data_out = buffer_out.dbl ;
  819. data_in = buffer_in.dbl ;
  820. for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
  821. { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
  822. data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
  823. } ;
  824. sfinfo.samplerate = 44100 ;
  825. sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
  826. sfinfo.channels = 1 ;
  827. sfinfo.format = filetype ;
  828. /*
  829. ** Write two versions of the data:
  830. ** normalized and clipped
  831. ** un-normalized and clipped.
  832. */
  833. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  834. sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
  835. test_write_double_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
  836. sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
  837. test_write_double_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
  838. sf_close (file) ;
  839. memset (&buffer_in, 0, sizeof (buffer_in)) ;
  840. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  841. sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
  842. if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
  843. { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
  844. exit (1) ;
  845. } ;
  846. if (sfinfo.frames != BUFFER_SIZE)
  847. { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
  848. exit (1) ;
  849. } ;
  850. if (sfinfo.channels != 1)
  851. { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
  852. exit (1) ;
  853. } ;
  854. check_log_buffer_or_die (file, __LINE__) ;
  855. test_read_double_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
  856. sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
  857. test_read_double_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
  858. sf_close (file) ;
  859. /* Check normalized version. */
  860. clip_max_diff = 0.0 ;
  861. for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
  862. { if (fabs (data_in [k]) > 1.0)
  863. { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
  864. exit (1) ;
  865. } ;
  866. if (data_out [k] * data_in [k] < 0.0)
  867. { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
  868. exit (1) ;
  869. } ;
  870. if (fabs (data_out [k]) > 1.0)
  871. continue ;
  872. diff = fabs (data_out [k] - data_in [k]) ;
  873. if (diff > clip_max_diff)
  874. clip_max_diff = diff ;
  875. } ;
  876. if (clip_max_diff < 1e-20)
  877. { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
  878. exit (1) ;
  879. } ;
  880. if (clip_max_diff > 1.0 / 0x80)
  881. { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
  882. exit (1) ;
  883. } ;
  884. /* Check the un-normalized data. */
  885. clip_max_diff = 0.0 ;
  886. for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
  887. { if (fabs (data_in [k]) > maxval)
  888. { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
  889. exit (1) ;
  890. } ;
  891. if (data_out [k] * data_in [k] < 0.0)
  892. { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
  893. exit (1) ;
  894. } ;
  895. if (fabs (data_out [k]) > maxval)
  896. continue ;
  897. diff = fabs (data_out [k] - data_in [k]) ;
  898. if (diff > clip_max_diff)
  899. clip_max_diff = diff ;
  900. } ;
  901. if (clip_max_diff < 1e-20)
  902. { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
  903. exit (1) ;
  904. } ;
  905. if (clip_max_diff > 1.0)
  906. { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
  907. exit (1) ;
  908. } ;
  909. printf ("ok\n") ;
  910. unlink (filename) ;
  911. } /* dbl_scale_clip_test_08 */
  912. /*==============================================================================
  913. */
  914. static void flt_short_clip_read_test (const char *filename, int filetype)
  915. { SNDFILE *file ;
  916. SF_INFO sfinfo ;
  917. float *data_out ;
  918. short *data_in, max_value ;
  919. int k ;
  920. print_test_name ("flt_short_clip_read_test", filename) ;
  921. data_out = buffer_out.flt ;
  922. data_in = buffer_in.s ;
  923. for (k = 0 ; k < BUFFER_SIZE ; k++)
  924. data_out [k] = 0.995 * sin (4 * M_PI * k / BUFFER_SIZE) ;
  925. data_out [BUFFER_SIZE / 8] = 1.0 ;
  926. data_out [3 * BUFFER_SIZE / 8] = -1.000000001 ;
  927. data_out [5 * BUFFER_SIZE / 8] = 1.0 ;
  928. data_out [7 * BUFFER_SIZE / 8] = -1.000000001 ;
  929. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  930. sfinfo.samplerate = 44100 ;
  931. sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
  932. sfinfo.channels = 1 ;
  933. sfinfo.format = filetype ;
  934. /* Save unclipped data to the file. */
  935. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  936. test_write_float_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
  937. sf_close (file) ;
  938. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  939. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  940. sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
  941. sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
  942. if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
  943. { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
  944. exit (1) ;
  945. } ;
  946. if (sfinfo.frames != BUFFER_SIZE)
  947. { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
  948. exit (1) ;
  949. } ;
  950. if (sfinfo.channels != 1)
  951. { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
  952. exit (1) ;
  953. } ;
  954. check_log_buffer_or_die (file, __LINE__) ;
  955. sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
  956. test_read_short_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
  957. /*-sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;-*/
  958. sf_close (file) ;
  959. /* Check the first half. */
  960. max_value = 0 ;
  961. for (k = 0 ; k < sfinfo.frames ; k++)
  962. { /* Check if data_out has different sign from data_in. */
  963. if ((data_out [k] < 0.0 && data_in [k] > 0) || (data_out [k] > 0.0 && data_in [k] < 0))
  964. { printf ("\n\nLine %d: Data wrap around at index %d/%d (%f -> %d).\n\n", __LINE__, k, BUFFER_SIZE, data_out [k], data_in [k]) ;
  965. exit (1) ;
  966. } ;
  967. max_value = (max_value > abs (data_in [k])) ? max_value : abs (data_in [k]) ;
  968. } ;
  969. unlink (filename) ;
  970. puts ("ok") ;
  971. } /* flt_short_clip_read_test */
  972. static void flt_int_clip_read_test (const char *filename, int filetype)
  973. { SNDFILE *file ;
  974. SF_INFO sfinfo ;
  975. float *data_out ;
  976. int *data_in, max_value ;
  977. int k ;
  978. print_test_name ("flt_int_clip_read_test", filename) ;
  979. data_out = buffer_out.flt ;
  980. data_in = buffer_in.i ;
  981. for (k = 0 ; k < BUFFER_SIZE ; k++)
  982. data_out [k] = 0.995 * sin (4 * M_PI * k / BUFFER_SIZE) ;
  983. data_out [BUFFER_SIZE / 8] = 1.0 ;
  984. data_out [3 * BUFFER_SIZE / 8] = -1.000000001 ;
  985. data_out [5 * BUFFER_SIZE / 8] = 1.0 ;
  986. data_out [7 * BUFFER_SIZE / 8] = -1.000000001 ;
  987. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  988. sfinfo.samplerate = 44100 ;
  989. sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
  990. sfinfo.channels = 1 ;
  991. sfinfo.format = filetype ;
  992. /* Save unclipped data to the file. */
  993. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  994. test_write_float_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
  995. sf_close (file) ;
  996. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  997. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  998. sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
  999. sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
  1000. if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
  1001. { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
  1002. exit (1) ;
  1003. } ;
  1004. if (sfinfo.frames != BUFFER_SIZE)
  1005. { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
  1006. exit (1) ;
  1007. } ;
  1008. if (sfinfo.channels != 1)
  1009. { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
  1010. exit (1) ;
  1011. } ;
  1012. check_log_buffer_or_die (file, __LINE__) ;
  1013. sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
  1014. test_read_int_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
  1015. /*-sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;-*/
  1016. sf_close (file) ;
  1017. /* Check the first half. */
  1018. max_value = 0 ;
  1019. for (k = 0 ; k < sfinfo.frames ; k++)
  1020. { /* Check if data_out has different sign from data_in. */
  1021. if ((data_out [k] < 0.0 && data_in [k] > 0) || (data_out [k] > 0.0 && data_in [k] < 0))
  1022. { printf ("\n\nLine %d: Data wrap around at index %d/%d (%f -> %d).\n\n", __LINE__, k, BUFFER_SIZE, data_out [k], data_in [k]) ;
  1023. exit (1) ;
  1024. } ;
  1025. max_value = (max_value > abs (data_in [k])) ? max_value : abs (data_in [k]) ;
  1026. } ;
  1027. unlink (filename) ;
  1028. puts ("ok") ;
  1029. } /* flt_int_clip_read_test */
  1030. static void dbl_short_clip_read_test (const char *filename, int filetype)
  1031. { SNDFILE *file ;
  1032. SF_INFO sfinfo ;
  1033. double *data_out ;
  1034. short *data_in, max_value ;
  1035. int k ;
  1036. print_test_name ("dbl_short_clip_read_test", filename) ;
  1037. data_out = buffer_out.dbl ;
  1038. data_in = buffer_in.s ;
  1039. for (k = 0 ; k < BUFFER_SIZE ; k++)
  1040. data_out [k] = 0.995 * sin (4 * M_PI * k / BUFFER_SIZE) ;
  1041. data_out [BUFFER_SIZE / 8] = 1.0 ;
  1042. data_out [3 * BUFFER_SIZE / 8] = -1.000000001 ;
  1043. data_out [5 * BUFFER_SIZE / 8] = 1.0 ;
  1044. data_out [7 * BUFFER_SIZE / 8] = -1.000000001 ;
  1045. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  1046. sfinfo.samplerate = 44100 ;
  1047. sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
  1048. sfinfo.channels = 1 ;
  1049. sfinfo.format = filetype ;
  1050. /* Save unclipped data to the file. */
  1051. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  1052. test_write_double_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
  1053. sf_close (file) ;
  1054. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  1055. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  1056. sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
  1057. sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
  1058. if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
  1059. { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
  1060. exit (1) ;
  1061. } ;
  1062. if (sfinfo.frames != BUFFER_SIZE)
  1063. { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
  1064. exit (1) ;
  1065. } ;
  1066. if (sfinfo.channels != 1)
  1067. { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
  1068. exit (1) ;
  1069. } ;
  1070. check_log_buffer_or_die (file, __LINE__) ;
  1071. sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
  1072. test_read_short_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
  1073. /*-sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;-*/
  1074. sf_close (file) ;
  1075. /* Check the first half. */
  1076. max_value = 0 ;
  1077. for (k = 0 ; k < sfinfo.frames ; k++)
  1078. { /* Check if data_out has different sign from data_in. */
  1079. if ((data_out [k] < 0.0 && data_in [k] > 0) || (data_out [k] > 0.0 && data_in [k] < 0))
  1080. { printf ("\n\nLine %d: Data wrap around at index %d/%d (%f -> %d).\n\n", __LINE__, k, BUFFER_SIZE, data_out [k], data_in [k]) ;
  1081. exit (1) ;
  1082. } ;
  1083. max_value = (max_value > abs (data_in [k])) ? max_value : abs (data_in [k]) ;
  1084. } ;
  1085. unlink (filename) ;
  1086. puts ("ok") ;
  1087. } /* dbl_short_clip_read_test */
  1088. static void dbl_int_clip_read_test (const char *filename, int filetype)
  1089. { SNDFILE *file ;
  1090. SF_INFO sfinfo ;
  1091. double *data_out ;
  1092. int *data_in, max_value ;
  1093. int k ;
  1094. print_test_name ("dbl_int_clip_read_test", filename) ;
  1095. data_out = buffer_out.dbl ;
  1096. data_in = buffer_in.i ;
  1097. for (k = 0 ; k < BUFFER_SIZE ; k++)
  1098. data_out [k] = 0.995 * sin (4 * M_PI * k / BUFFER_SIZE) ;
  1099. data_out [BUFFER_SIZE / 8] = 1.0 ;
  1100. data_out [3 * BUFFER_SIZE / 8] = -1.000000001 ;
  1101. data_out [5 * BUFFER_SIZE / 8] = 1.0 ;
  1102. data_out [7 * BUFFER_SIZE / 8] = -1.000000001 ;
  1103. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  1104. sfinfo.samplerate = 44100 ;
  1105. sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
  1106. sfinfo.channels = 1 ;
  1107. sfinfo.format = filetype ;
  1108. /* Save unclipped data to the file. */
  1109. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  1110. test_write_double_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
  1111. sf_close (file) ;
  1112. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  1113. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  1114. sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
  1115. sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
  1116. if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
  1117. { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
  1118. exit (1) ;
  1119. } ;
  1120. if (sfinfo.frames != BUFFER_SIZE)
  1121. { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
  1122. exit (1) ;
  1123. } ;
  1124. if (sfinfo.channels != 1)
  1125. { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
  1126. exit (1) ;
  1127. } ;
  1128. check_log_buffer_or_die (file, __LINE__) ;
  1129. sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
  1130. test_read_int_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
  1131. /*-sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;-*/
  1132. sf_close (file) ;
  1133. /* Check the first half. */
  1134. max_value = 0 ;
  1135. for (k = 0 ; k < sfinfo.frames ; k++)
  1136. { /* Check if data_out has different sign from data_in. */
  1137. if ((data_out [k] < 0.0 && data_in [k] > 0) || (data_out [k] > 0.0 && data_in [k] < 0))
  1138. { printf ("\n\nLine %d: Data wrap around at index %d/%d (%f -> %d).\n\n", __LINE__, k, BUFFER_SIZE, data_out [k], data_in [k]) ;
  1139. exit (1) ;
  1140. } ;
  1141. max_value = (max_value > abs (data_in [k])) ? max_value : abs (data_in [k]) ;
  1142. } ;
  1143. unlink (filename) ;
  1144. puts ("ok") ;
  1145. } /* dbl_int_clip_read_test */
  1146. /*==============================================================================
  1147. */
  1148. static void short_flt_scale_write_test (const char *filename, int filetype)
  1149. { SNDFILE *file ;
  1150. SF_INFO sfinfo ;
  1151. short *data_out ;
  1152. float *data_in, max_value ;
  1153. int k ;
  1154. print_test_name ("short_flt_clip_write_test", filename) ;
  1155. data_out = buffer_out.s ;
  1156. data_in = buffer_in.flt ;
  1157. for (k = 0 ; k < BUFFER_SIZE ; k++)
  1158. data_out [k] = lrintf (0x7FFFF * 0.995 * sin (4 * M_PI * k / BUFFER_SIZE)) ;
  1159. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  1160. sfinfo.samplerate = 44100 ;
  1161. sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
  1162. sfinfo.channels = 1 ;
  1163. sfinfo.format = filetype ;
  1164. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  1165. test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
  1166. sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_TRUE) ;
  1167. test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
  1168. sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_FALSE) ;
  1169. test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
  1170. sf_close (file) ;
  1171. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  1172. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  1173. sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
  1174. if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
  1175. { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
  1176. exit (1) ;
  1177. } ;
  1178. if (sfinfo.frames != 3 * BUFFER_SIZE)
  1179. { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, 3 * BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
  1180. exit (1) ;
  1181. } ;
  1182. if (sfinfo.channels != 1)
  1183. { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
  1184. exit (1) ;
  1185. } ;
  1186. check_log_buffer_or_die (file, __LINE__) ;
  1187. /* Check the first section. */
  1188. test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
  1189. max_value = 0.0 ;
  1190. for (k = 0 ; k < BUFFER_SIZE ; k++)
  1191. max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
  1192. if (max_value < 1000.0)
  1193. { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
  1194. exit (1) ;
  1195. } ;
  1196. /* Check the second section. */
  1197. test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
  1198. max_value = 0.0 ;
  1199. for (k = 0 ; k < BUFFER_SIZE ; k++)
  1200. max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
  1201. if (max_value > 1.0)
  1202. { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__, max_value) ;
  1203. exit (1) ;
  1204. } ;
  1205. /* Check the third section. */
  1206. test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
  1207. max_value = 0.0 ;
  1208. for (k = 0 ; k < BUFFER_SIZE ; k++)
  1209. max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
  1210. if (max_value < 1000.0)
  1211. { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
  1212. exit (1) ;
  1213. } ;
  1214. sf_close (file) ;
  1215. unlink (filename) ;
  1216. puts ("ok") ;
  1217. } /* short_flt_scale_write_test */
  1218. static void short_dbl_scale_write_test (const char *filename, int filetype)
  1219. { SNDFILE *file ;
  1220. SF_INFO sfinfo ;
  1221. short *data_out ;
  1222. double *data_in, max_value ;
  1223. int k ;
  1224. print_test_name ("short_dbl_clip_write_test", filename) ;
  1225. data_out = buffer_out.s ;
  1226. data_in = buffer_in.dbl ;
  1227. for (k = 0 ; k < BUFFER_SIZE ; k++)
  1228. data_out [k] = lrint (0x7FFFF * 0.995 * sin (4 * M_PI * k / BUFFER_SIZE)) ;
  1229. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  1230. sfinfo.samplerate = 44100 ;
  1231. sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
  1232. sfinfo.channels = 1 ;
  1233. sfinfo.format = filetype ;
  1234. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  1235. test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
  1236. sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_TRUE) ;
  1237. test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
  1238. sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_FALSE) ;
  1239. test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
  1240. sf_close (file) ;
  1241. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  1242. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  1243. sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
  1244. if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
  1245. { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
  1246. exit (1) ;
  1247. } ;
  1248. if (sfinfo.frames != 3 * BUFFER_SIZE)
  1249. { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, 3 * BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
  1250. exit (1) ;
  1251. } ;
  1252. if (sfinfo.channels != 1)
  1253. { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
  1254. exit (1) ;
  1255. } ;
  1256. check_log_buffer_or_die (file, __LINE__) ;
  1257. /* Check the first section. */
  1258. test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
  1259. max_value = 0.0 ;
  1260. for (k = 0 ; k < BUFFER_SIZE ; k++)
  1261. max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
  1262. if (max_value < 1000.0)
  1263. { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
  1264. exit (1) ;
  1265. } ;
  1266. /* Check the second section. */
  1267. test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
  1268. max_value = 0.0 ;
  1269. for (k = 0 ; k < BUFFER_SIZE ; k++)
  1270. max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
  1271. if (max_value > 1.0)
  1272. { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__, max_value) ;
  1273. exit (1) ;
  1274. } ;
  1275. /* Check the third section. */
  1276. test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
  1277. max_value = 0.0 ;
  1278. for (k = 0 ; k < BUFFER_SIZE ; k++)
  1279. max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
  1280. if (max_value < 1000.0)
  1281. { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
  1282. exit (1) ;
  1283. } ;
  1284. sf_close (file) ;
  1285. unlink (filename) ;
  1286. puts ("ok") ;
  1287. } /* short_dbl_scale_write_test */
  1288. static void int_flt_scale_write_test (const char *filename, int filetype)
  1289. { SNDFILE *file ;
  1290. SF_INFO sfinfo ;
  1291. int *data_out ;
  1292. float *data_in, max_value ;
  1293. int k ;
  1294. print_test_name ("int_flt_clip_write_test", filename) ;
  1295. data_out = buffer_out.i ;
  1296. data_in = buffer_in.flt ;
  1297. for (k = 0 ; k < BUFFER_SIZE ; k++)
  1298. data_out [k] = lrintf (0x7FFFFFFF * 0.995 * sin (4 * M_PI * k / BUFFER_SIZE)) ;
  1299. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  1300. sfinfo.samplerate = 44100 ;
  1301. sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
  1302. sfinfo.channels = 1 ;
  1303. sfinfo.format = filetype ;
  1304. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  1305. test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
  1306. sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_TRUE) ;
  1307. test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
  1308. sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_FALSE) ;
  1309. test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
  1310. sf_close (file) ;
  1311. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  1312. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  1313. sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
  1314. if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
  1315. { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
  1316. exit (1) ;
  1317. } ;
  1318. if (sfinfo.frames != 3 * BUFFER_SIZE)
  1319. { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, 3 * BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
  1320. exit (1) ;
  1321. } ;
  1322. if (sfinfo.channels != 1)
  1323. { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
  1324. exit (1) ;
  1325. } ;
  1326. check_log_buffer_or_die (file, __LINE__) ;
  1327. /* Check the first section. */
  1328. test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
  1329. max_value = 0.0 ;
  1330. for (k = 0 ; k < BUFFER_SIZE ; k++)
  1331. max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
  1332. if (max_value < 1000.0)
  1333. { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
  1334. exit (1) ;
  1335. } ;
  1336. /* Check the second section. */
  1337. test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
  1338. max_value = 0.0 ;
  1339. for (k = 0 ; k < BUFFER_SIZE ; k++)
  1340. max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
  1341. if (max_value > 1.0)
  1342. { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__, max_value) ;
  1343. exit (1) ;
  1344. } ;
  1345. /* Check the third section. */
  1346. test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
  1347. max_value = 0.0 ;
  1348. for (k = 0 ; k < BUFFER_SIZE ; k++)
  1349. max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
  1350. if (max_value < 1000.0)
  1351. { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
  1352. exit (1) ;
  1353. } ;
  1354. sf_close (file) ;
  1355. unlink (filename) ;
  1356. puts ("ok") ;
  1357. } /* int_flt_scale_write_test */
  1358. static void int_dbl_scale_write_test (const char *filename, int filetype)
  1359. { SNDFILE *file ;
  1360. SF_INFO sfinfo ;
  1361. int *data_out ;
  1362. double *data_in, max_value ;
  1363. int k ;
  1364. print_test_name ("int_dbl_clip_write_test", filename) ;
  1365. data_out = buffer_out.i ;
  1366. data_in = buffer_in.dbl ;
  1367. for (k = 0 ; k < BUFFER_SIZE ; k++)
  1368. data_out [k] = lrint (0x7FFFFFFF * 0.995 * sin (4 * M_PI * k / BUFFER_SIZE)) ;
  1369. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  1370. sfinfo.samplerate = 44100 ;
  1371. sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
  1372. sfinfo.channels = 1 ;
  1373. sfinfo.format = filetype ;
  1374. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  1375. test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
  1376. sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_TRUE) ;
  1377. test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
  1378. sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_FALSE) ;
  1379. test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
  1380. sf_close (file) ;
  1381. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  1382. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  1383. sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
  1384. if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
  1385. { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
  1386. exit (1) ;
  1387. } ;
  1388. if (sfinfo.frames != 3 * BUFFER_SIZE)
  1389. { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, 3 * BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
  1390. exit (1) ;
  1391. } ;
  1392. if (sfinfo.channels != 1)
  1393. { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
  1394. exit (1) ;
  1395. } ;
  1396. check_log_buffer_or_die (file, __LINE__) ;
  1397. /* Check the first section. */
  1398. test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
  1399. max_value = 0.0 ;
  1400. for (k = 0 ; k < BUFFER_SIZE ; k++)
  1401. max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
  1402. if (max_value < 1000.0)
  1403. { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
  1404. exit (1) ;
  1405. } ;
  1406. /* Check the second section. */
  1407. test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
  1408. max_value = 0.0 ;
  1409. for (k = 0 ; k < BUFFER_SIZE ; k++)
  1410. max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
  1411. if (max_value > 1.0)
  1412. { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__, max_value) ;
  1413. exit (1) ;
  1414. } ;
  1415. /* Check the third section. */
  1416. test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
  1417. max_value = 0.0 ;
  1418. for (k = 0 ; k < BUFFER_SIZE ; k++)
  1419. max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
  1420. if (max_value < 1000.0)
  1421. { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
  1422. exit (1) ;
  1423. } ;
  1424. sf_close (file) ;
  1425. unlink (filename) ;
  1426. puts ("ok") ;
  1427. } /* int_dbl_scale_write_test */