write_read_test.tpl 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. [+ AutoGen5 template c +]
  2. /*
  3. ** Copyright (C) 1999-2017 Erik de Castro Lopo <[email protected]>
  4. **
  5. ** This program is free software; you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation; either version 2 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program; if not, write to the Free Software
  17. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #include "sfconfig.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <math.h>
  24. #include <inttypes.h>
  25. #if HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #else
  28. #include "sf_unistd.h"
  29. #endif
  30. #include <sndfile.h>
  31. #include "utils.h"
  32. #include "generate.h"
  33. #define SAMPLE_RATE 11025
  34. #define DATA_LENGTH (1 << 12)
  35. #define SILLY_WRITE_COUNT (234)
  36. static const char WRT_TEST_PREFIX[] = "wrt" ;
  37. [+ FOR data_type
  38. +]static void pcm_test_[+ (get "type_name") +] (const char *str, int format, int long_file_ok) ;
  39. [+ ENDFOR data_type
  40. +]
  41. static void empty_file_test (const char *filename, int format) ;
  42. typedef union
  43. { double d [DATA_LENGTH] ;
  44. float f [DATA_LENGTH] ;
  45. int i [DATA_LENGTH] ;
  46. short s [DATA_LENGTH] ;
  47. char c [DATA_LENGTH] ;
  48. } BUFFER ;
  49. static BUFFER orig_data ;
  50. static BUFFER test_data ;
  51. int
  52. main (int argc, char **argv)
  53. { int do_all = 0 ;
  54. int test_count = 0 ;
  55. count_open_files () ;
  56. if (argc != 2)
  57. { printf ("Usage : %s <test>\n", argv [0]) ;
  58. printf (" Where <test> is one of the following:\n") ;
  59. printf (" wav - test WAV file functions (little endian)\n") ;
  60. printf (" aiff - test AIFF file functions (big endian)\n") ;
  61. printf (" au - test AU file functions\n") ;
  62. printf (" avr - test AVR file functions\n") ;
  63. printf (" caf - test CAF file functions\n") ;
  64. printf (" raw - test RAW header-less PCM file functions\n") ;
  65. printf (" paf - test PAF file functions\n") ;
  66. printf (" svx - test 8SVX/16SV file functions\n") ;
  67. printf (" nist - test NIST Sphere file functions\n") ;
  68. printf (" ircam - test IRCAM file functions\n") ;
  69. printf (" voc - Create Voice file functions\n") ;
  70. printf (" w64 - Sonic Foundry's W64 file functions\n") ;
  71. printf (" flac - test FLAC file functions\n") ;
  72. printf (" mpc2k - test MPC 2000 file functions\n") ;
  73. printf (" rf64 - test RF64 file functions\n") ;
  74. printf (" all - perform all tests\n") ;
  75. exit (1) ;
  76. } ;
  77. do_all = !strcmp (argv [1], "all") ;
  78. if (do_all || ! strcmp (argv [1], "wav"))
  79. { pcm_test_char ("char.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_U8, SF_FALSE) ;
  80. pcm_test_short ("short.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_16, SF_FALSE) ;
  81. pcm_test_24bit ("24bit.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_24, SF_FALSE) ;
  82. pcm_test_int ("int.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_32, SF_FALSE) ;
  83. pcm_test_char ("char.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_U8, SF_FALSE) ;
  84. pcm_test_short ("short.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_16, SF_FALSE) ;
  85. pcm_test_24bit ("24bit.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_24, SF_FALSE) ;
  86. pcm_test_int ("int.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_32, SF_FALSE) ;
  87. pcm_test_24bit ("24bit.wavex" , SF_FORMAT_WAVEX | SF_FORMAT_PCM_24, SF_FALSE) ;
  88. pcm_test_int ("int.wavex" , SF_FORMAT_WAVEX | SF_FORMAT_PCM_32, SF_FALSE) ;
  89. /* Lite remove start */
  90. pcm_test_float ("float.wav" , SF_FORMAT_WAV | SF_FORMAT_FLOAT , SF_FALSE) ;
  91. pcm_test_double ("double.wav" , SF_FORMAT_WAV | SF_FORMAT_DOUBLE, SF_FALSE) ;
  92. pcm_test_float ("float.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_FLOAT , SF_FALSE) ;
  93. pcm_test_double ("double.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_DOUBLE, SF_FALSE) ;
  94. pcm_test_float ("float.wavex" , SF_FORMAT_WAVEX | SF_FORMAT_FLOAT , SF_FALSE) ;
  95. pcm_test_double ("double.wavex" , SF_FORMAT_WAVEX | SF_FORMAT_DOUBLE, SF_FALSE) ;
  96. /* Lite remove end */
  97. empty_file_test ("empty_char.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8) ;
  98. empty_file_test ("empty_short.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
  99. empty_file_test ("empty_float.wav", SF_FORMAT_WAV | SF_FORMAT_FLOAT) ;
  100. test_count++ ;
  101. } ;
  102. if (do_all || ! strcmp (argv [1], "aiff"))
  103. { pcm_test_char ("char_u8.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_U8, SF_FALSE) ;
  104. pcm_test_char ("char_s8.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_S8, SF_FALSE) ;
  105. pcm_test_short ("short.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_16, SF_FALSE) ;
  106. pcm_test_24bit ("24bit.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_24, SF_FALSE) ;
  107. pcm_test_int ("int.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_32, SF_FALSE) ;
  108. pcm_test_short ("short_sowt.aifc" , SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | SF_FORMAT_PCM_16, SF_FALSE) ;
  109. pcm_test_24bit ("24bit_sowt.aifc" , SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | SF_FORMAT_PCM_24, SF_FALSE) ;
  110. pcm_test_int ("int_sowt.aifc" , SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | SF_FORMAT_PCM_32, SF_FALSE) ;
  111. pcm_test_short ("short_twos.aifc" , SF_ENDIAN_BIG | SF_FORMAT_AIFF | SF_FORMAT_PCM_16, SF_FALSE) ;
  112. pcm_test_24bit ("24bit_twos.aifc" , SF_ENDIAN_BIG | SF_FORMAT_AIFF | SF_FORMAT_PCM_24, SF_FALSE) ;
  113. pcm_test_int ("int_twos.aifc" , SF_ENDIAN_BIG | SF_FORMAT_AIFF | SF_FORMAT_PCM_32, SF_FALSE) ;
  114. /* Lite remove start */
  115. pcm_test_short ("dwvw16.aifc", SF_FORMAT_AIFF | SF_FORMAT_DWVW_16, SF_TRUE) ;
  116. pcm_test_24bit ("dwvw24.aifc", SF_FORMAT_AIFF | SF_FORMAT_DWVW_24, SF_TRUE) ;
  117. pcm_test_float ("float.aifc" , SF_FORMAT_AIFF | SF_FORMAT_FLOAT , SF_FALSE) ;
  118. pcm_test_double ("double.aifc" , SF_FORMAT_AIFF | SF_FORMAT_DOUBLE, SF_FALSE) ;
  119. /* Lite remove end */
  120. empty_file_test ("empty_char.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_U8) ;
  121. empty_file_test ("empty_short.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_16) ;
  122. empty_file_test ("empty_float.aiff", SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
  123. test_count++ ;
  124. } ;
  125. if (do_all || ! strcmp (argv [1], "au"))
  126. { pcm_test_char ("char.au" , SF_FORMAT_AU | SF_FORMAT_PCM_S8, SF_FALSE) ;
  127. pcm_test_short ("short.au" , SF_FORMAT_AU | SF_FORMAT_PCM_16, SF_FALSE) ;
  128. pcm_test_24bit ("24bit.au" , SF_FORMAT_AU | SF_FORMAT_PCM_24, SF_FALSE) ;
  129. pcm_test_int ("int.au" , SF_FORMAT_AU | SF_FORMAT_PCM_32, SF_FALSE) ;
  130. /* Lite remove start */
  131. pcm_test_float ("float.au" , SF_FORMAT_AU | SF_FORMAT_FLOAT , SF_FALSE) ;
  132. pcm_test_double ("double.au", SF_FORMAT_AU | SF_FORMAT_DOUBLE, SF_FALSE) ;
  133. /* Lite remove end */
  134. pcm_test_char ("char_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_S8, SF_FALSE) ;
  135. pcm_test_short ("short_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_16, SF_FALSE) ;
  136. pcm_test_24bit ("24bit_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_24, SF_FALSE) ;
  137. pcm_test_int ("int_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_32, SF_FALSE) ;
  138. /* Lite remove start */
  139. pcm_test_float ("float_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT , SF_FALSE) ;
  140. pcm_test_double ("double_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE, SF_FALSE) ;
  141. /* Lite remove end */
  142. test_count++ ;
  143. } ;
  144. if (do_all || ! strcmp (argv [1], "caf"))
  145. { pcm_test_char ("char.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_S8, SF_FALSE) ;
  146. pcm_test_short ("short.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_16, SF_FALSE) ;
  147. pcm_test_24bit ("24bit.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_24, SF_FALSE) ;
  148. pcm_test_int ("int.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_32, SF_FALSE) ;
  149. /* Lite remove start */
  150. pcm_test_float ("float.caf" , SF_FORMAT_CAF | SF_FORMAT_FLOAT , SF_FALSE) ;
  151. pcm_test_double ("double.caf" , SF_FORMAT_CAF | SF_FORMAT_DOUBLE, SF_FALSE) ;
  152. /* Lite remove end */
  153. pcm_test_short ("short_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_PCM_16, SF_FALSE) ;
  154. pcm_test_24bit ("24bit_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_PCM_24, SF_FALSE) ;
  155. pcm_test_int ("int_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_PCM_32, SF_FALSE) ;
  156. /* Lite remove start */
  157. pcm_test_float ("float_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_FLOAT , SF_FALSE) ;
  158. pcm_test_double ("double_le.caf", SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_DOUBLE, SF_FALSE) ;
  159. pcm_test_short ("alac16.caf" , SF_FORMAT_CAF | SF_FORMAT_ALAC_16, SF_FALSE) ;
  160. pcm_test_20bit ("alac20.caf" , SF_FORMAT_CAF | SF_FORMAT_ALAC_20, SF_FALSE) ;
  161. pcm_test_24bit ("alac24.caf" , SF_FORMAT_CAF | SF_FORMAT_ALAC_24, SF_FALSE) ;
  162. pcm_test_int ("alac32.caf" , SF_FORMAT_CAF | SF_FORMAT_ALAC_32, SF_FALSE) ;
  163. /* Lite remove end */
  164. test_count++ ;
  165. } ;
  166. if (do_all || ! strcmp (argv [1], "raw"))
  167. { pcm_test_char ("char_s8.raw" , SF_FORMAT_RAW | SF_FORMAT_PCM_S8, SF_FALSE) ;
  168. pcm_test_char ("char_u8.raw" , SF_FORMAT_RAW | SF_FORMAT_PCM_U8, SF_FALSE) ;
  169. pcm_test_short ("short_le.raw" , SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_16, SF_FALSE) ;
  170. pcm_test_short ("short_be.raw" , SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_PCM_16, SF_FALSE) ;
  171. pcm_test_24bit ("24bit_le.raw" , SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_24, SF_FALSE) ;
  172. pcm_test_24bit ("24bit_be.raw" , SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_PCM_24, SF_FALSE) ;
  173. pcm_test_int ("int_le.raw" , SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_32, SF_FALSE) ;
  174. pcm_test_int ("int_be.raw" , SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_PCM_32, SF_FALSE) ;
  175. /* Lite remove start */
  176. pcm_test_float ("float_le.raw" , SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_FLOAT , SF_FALSE) ;
  177. pcm_test_float ("float_be.raw" , SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_FLOAT , SF_FALSE) ;
  178. pcm_test_double ("double_le.raw", SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_DOUBLE, SF_FALSE) ;
  179. pcm_test_double ("double_be.raw", SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_DOUBLE, SF_FALSE) ;
  180. /* Lite remove end */
  181. test_count++ ;
  182. } ;
  183. /* Lite remove start */
  184. if (do_all || ! strcmp (argv [1], "paf"))
  185. { pcm_test_char ("char_le.paf", SF_ENDIAN_LITTLE | SF_FORMAT_PAF | SF_FORMAT_PCM_S8, SF_FALSE) ;
  186. pcm_test_char ("char_be.paf", SF_ENDIAN_BIG | SF_FORMAT_PAF | SF_FORMAT_PCM_S8, SF_FALSE) ;
  187. pcm_test_short ("short_le.paf", SF_ENDIAN_LITTLE | SF_FORMAT_PAF | SF_FORMAT_PCM_16, SF_FALSE) ;
  188. pcm_test_short ("short_be.paf", SF_ENDIAN_BIG | SF_FORMAT_PAF | SF_FORMAT_PCM_16, SF_FALSE) ;
  189. pcm_test_24bit ("24bit_le.paf", SF_ENDIAN_LITTLE | SF_FORMAT_PAF | SF_FORMAT_PCM_24, SF_TRUE) ;
  190. pcm_test_24bit ("24bit_be.paf", SF_ENDIAN_BIG | SF_FORMAT_PAF | SF_FORMAT_PCM_24, SF_TRUE) ;
  191. test_count++ ;
  192. } ;
  193. if (do_all || ! strcmp (argv [1], "svx"))
  194. { pcm_test_char ("char.svx" , SF_FORMAT_SVX | SF_FORMAT_PCM_S8, SF_FALSE) ;
  195. pcm_test_short ("short.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_16, SF_FALSE) ;
  196. empty_file_test ("empty_char.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_S8) ;
  197. empty_file_test ("empty_short.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_16) ;
  198. test_count++ ;
  199. } ;
  200. if (do_all || ! strcmp (argv [1], "nist"))
  201. { pcm_test_short ("short_le.nist", SF_ENDIAN_LITTLE | SF_FORMAT_NIST | SF_FORMAT_PCM_16, SF_FALSE) ;
  202. pcm_test_short ("short_be.nist", SF_ENDIAN_BIG | SF_FORMAT_NIST | SF_FORMAT_PCM_16, SF_FALSE) ;
  203. pcm_test_24bit ("24bit_le.nist", SF_ENDIAN_LITTLE | SF_FORMAT_NIST | SF_FORMAT_PCM_24, SF_FALSE) ;
  204. pcm_test_24bit ("24bit_be.nist", SF_ENDIAN_BIG | SF_FORMAT_NIST | SF_FORMAT_PCM_24, SF_FALSE) ;
  205. pcm_test_int ("int_le.nist" , SF_ENDIAN_LITTLE | SF_FORMAT_NIST | SF_FORMAT_PCM_32, SF_FALSE) ;
  206. pcm_test_int ("int_be.nist" , SF_ENDIAN_BIG | SF_FORMAT_NIST | SF_FORMAT_PCM_32, SF_FALSE) ;
  207. test_count++ ;
  208. } ;
  209. if (do_all || ! strcmp (argv [1], "ircam"))
  210. { pcm_test_short ("short_be.ircam" , SF_ENDIAN_BIG | SF_FORMAT_IRCAM | SF_FORMAT_PCM_16, SF_FALSE) ;
  211. pcm_test_short ("short_le.ircam" , SF_ENDIAN_LITTLE | SF_FORMAT_IRCAM | SF_FORMAT_PCM_16, SF_FALSE) ;
  212. pcm_test_int ("int_be.ircam" , SF_ENDIAN_BIG | SF_FORMAT_IRCAM | SF_FORMAT_PCM_32, SF_FALSE) ;
  213. pcm_test_int ("int_le.ircam" , SF_ENDIAN_LITTLE | SF_FORMAT_IRCAM | SF_FORMAT_PCM_32, SF_FALSE) ;
  214. pcm_test_float ("float_be.ircam" , SF_ENDIAN_BIG | SF_FORMAT_IRCAM | SF_FORMAT_FLOAT , SF_FALSE) ;
  215. pcm_test_float ("float_le.ircam" , SF_ENDIAN_LITTLE | SF_FORMAT_IRCAM | SF_FORMAT_FLOAT , SF_FALSE) ;
  216. test_count++ ;
  217. } ;
  218. if (do_all || ! strcmp (argv [1], "voc"))
  219. { pcm_test_char ("char.voc" , SF_FORMAT_VOC | SF_FORMAT_PCM_U8, SF_FALSE) ;
  220. pcm_test_short ("short.voc", SF_FORMAT_VOC | SF_FORMAT_PCM_16, SF_FALSE) ;
  221. test_count++ ;
  222. } ;
  223. if (do_all || ! strcmp (argv [1], "mat4"))
  224. { pcm_test_short ("short_be.mat4" , SF_ENDIAN_BIG | SF_FORMAT_MAT4 | SF_FORMAT_PCM_16, SF_FALSE) ;
  225. pcm_test_short ("short_le.mat4" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT4 | SF_FORMAT_PCM_16, SF_FALSE) ;
  226. pcm_test_int ("int_be.mat4" , SF_ENDIAN_BIG | SF_FORMAT_MAT4 | SF_FORMAT_PCM_32, SF_FALSE) ;
  227. pcm_test_int ("int_le.mat4" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT4 | SF_FORMAT_PCM_32, SF_FALSE) ;
  228. pcm_test_float ("float_be.mat4" , SF_ENDIAN_BIG | SF_FORMAT_MAT4 | SF_FORMAT_FLOAT , SF_FALSE) ;
  229. pcm_test_float ("float_le.mat4" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT4 | SF_FORMAT_FLOAT , SF_FALSE) ;
  230. pcm_test_double ("double_be.mat4" , SF_ENDIAN_BIG | SF_FORMAT_MAT4 | SF_FORMAT_DOUBLE, SF_FALSE) ;
  231. pcm_test_double ("double_le.mat4" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT4 | SF_FORMAT_DOUBLE, SF_FALSE) ;
  232. empty_file_test ("empty_short.mat4", SF_FORMAT_MAT4 | SF_FORMAT_PCM_16) ;
  233. empty_file_test ("empty_float.mat4", SF_FORMAT_MAT4 | SF_FORMAT_FLOAT) ;
  234. test_count++ ;
  235. } ;
  236. if (do_all || ! strcmp (argv [1], "mat5"))
  237. { pcm_test_char ("char_be.mat5" , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_PCM_U8, SF_FALSE) ;
  238. pcm_test_char ("char_le.mat5" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT5 | SF_FORMAT_PCM_U8, SF_FALSE) ;
  239. pcm_test_short ("short_be.mat5" , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_PCM_16, SF_FALSE) ;
  240. pcm_test_short ("short_le.mat5" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT5 | SF_FORMAT_PCM_16, SF_FALSE) ;
  241. pcm_test_int ("int_be.mat5" , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_PCM_32, SF_FALSE) ;
  242. pcm_test_int ("int_le.mat5" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT5 | SF_FORMAT_PCM_32, SF_FALSE) ;
  243. pcm_test_float ("float_be.mat5" , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_FLOAT , SF_FALSE) ;
  244. pcm_test_float ("float_le.mat5" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT5 | SF_FORMAT_FLOAT , SF_FALSE) ;
  245. pcm_test_double ("double_be.mat5" , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_DOUBLE, SF_FALSE) ;
  246. pcm_test_double ("double_le.mat5" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT5 | SF_FORMAT_DOUBLE, SF_FALSE) ;
  247. increment_open_file_count () ;
  248. empty_file_test ("empty_char.mat5", SF_FORMAT_MAT5 | SF_FORMAT_PCM_U8) ;
  249. empty_file_test ("empty_short.mat5", SF_FORMAT_MAT5 | SF_FORMAT_PCM_16) ;
  250. empty_file_test ("empty_float.mat5", SF_FORMAT_MAT5 | SF_FORMAT_FLOAT) ;
  251. test_count++ ;
  252. } ;
  253. if (do_all || ! strcmp (argv [1], "pvf"))
  254. { pcm_test_char ("char.pvf" , SF_FORMAT_PVF | SF_FORMAT_PCM_S8, SF_FALSE) ;
  255. pcm_test_short ("short.pvf", SF_FORMAT_PVF | SF_FORMAT_PCM_16, SF_FALSE) ;
  256. pcm_test_int ("int.pvf" , SF_FORMAT_PVF | SF_FORMAT_PCM_32, SF_FALSE) ;
  257. test_count++ ;
  258. } ;
  259. if (do_all || ! strcmp (argv [1], "htk"))
  260. { pcm_test_short ("short.htk", SF_FORMAT_HTK | SF_FORMAT_PCM_16, SF_FALSE) ;
  261. test_count++ ;
  262. } ;
  263. if (do_all || ! strcmp (argv [1], "mpc2k"))
  264. { pcm_test_short ("short.mpc", SF_FORMAT_MPC2K | SF_FORMAT_PCM_16, SF_FALSE) ;
  265. test_count++ ;
  266. } ;
  267. if (do_all || ! strcmp (argv [1], "avr"))
  268. { pcm_test_char ("char_u8.avr" , SF_FORMAT_AVR | SF_FORMAT_PCM_U8, SF_FALSE) ;
  269. pcm_test_char ("char_s8.avr" , SF_FORMAT_AVR | SF_FORMAT_PCM_S8, SF_FALSE) ;
  270. pcm_test_short ("short.avr" , SF_FORMAT_AVR | SF_FORMAT_PCM_16, SF_FALSE) ;
  271. test_count++ ;
  272. } ;
  273. /* Lite remove end */
  274. if (do_all || ! strcmp (argv [1], "w64"))
  275. { pcm_test_char ("char.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_U8, SF_FALSE) ;
  276. pcm_test_short ("short.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_16, SF_FALSE) ;
  277. pcm_test_24bit ("24bit.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_24, SF_FALSE) ;
  278. pcm_test_int ("int.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_32, SF_FALSE) ;
  279. /* Lite remove start */
  280. pcm_test_float ("float.w64" , SF_FORMAT_W64 | SF_FORMAT_FLOAT , SF_FALSE) ;
  281. pcm_test_double ("double.w64" , SF_FORMAT_W64 | SF_FORMAT_DOUBLE, SF_FALSE) ;
  282. /* Lite remove end */
  283. empty_file_test ("empty_char.w64", SF_FORMAT_W64 | SF_FORMAT_PCM_U8) ;
  284. empty_file_test ("empty_short.w64", SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
  285. empty_file_test ("empty_float.w64", SF_FORMAT_W64 | SF_FORMAT_FLOAT) ;
  286. test_count++ ;
  287. } ;
  288. if (do_all || ! strcmp (argv [1], "sds"))
  289. { pcm_test_char ("char.sds" , SF_FORMAT_SDS | SF_FORMAT_PCM_S8, SF_FALSE) ;
  290. pcm_test_short ("short.sds" , SF_FORMAT_SDS | SF_FORMAT_PCM_16, SF_FALSE) ;
  291. pcm_test_24bit ("24bit.sds" , SF_FORMAT_SDS | SF_FORMAT_PCM_24, SF_FALSE) ;
  292. empty_file_test ("empty_char.sds", SF_FORMAT_SDS | SF_FORMAT_PCM_S8) ;
  293. empty_file_test ("empty_short.sds", SF_FORMAT_SDS | SF_FORMAT_PCM_16) ;
  294. test_count++ ;
  295. } ;
  296. if (do_all || ! strcmp (argv [1], "sd2"))
  297. { pcm_test_char ("char.sd2" , SF_FORMAT_SD2 | SF_FORMAT_PCM_S8, SF_TRUE) ;
  298. pcm_test_short ("short.sd2" , SF_FORMAT_SD2 | SF_FORMAT_PCM_16, SF_TRUE) ;
  299. pcm_test_24bit ("24bit.sd2" , SF_FORMAT_SD2 | SF_FORMAT_PCM_24, SF_TRUE) ;
  300. pcm_test_int ("32bit.sd2" , SF_FORMAT_SD2 | SF_FORMAT_PCM_32, SF_TRUE) ;
  301. test_count++ ;
  302. } ;
  303. if (do_all || ! strcmp (argv [1], "flac"))
  304. { if (HAVE_EXTERNAL_XIPH_LIBS)
  305. { pcm_test_char ("char.flac" , SF_FORMAT_FLAC | SF_FORMAT_PCM_S8, SF_TRUE) ;
  306. pcm_test_short ("short.flac" , SF_FORMAT_FLAC | SF_FORMAT_PCM_16, SF_TRUE) ;
  307. pcm_test_24bit ("24bit.flac" , SF_FORMAT_FLAC | SF_FORMAT_PCM_24, SF_TRUE) ;
  308. }
  309. else
  310. puts (" No FLAC tests because FLAC support was not compiled in.") ;
  311. test_count++ ;
  312. } ;
  313. if (do_all || ! strcmp (argv [1], "rf64"))
  314. { pcm_test_char ("char.rf64" , SF_FORMAT_RF64 | SF_FORMAT_PCM_U8, SF_FALSE) ;
  315. pcm_test_short ("short.rf64" , SF_FORMAT_RF64 | SF_FORMAT_PCM_16, SF_FALSE) ;
  316. pcm_test_24bit ("24bit.rf64" , SF_FORMAT_RF64 | SF_FORMAT_PCM_24, SF_FALSE) ;
  317. pcm_test_int ("int.rf64" , SF_FORMAT_RF64 | SF_FORMAT_PCM_32, SF_FALSE) ;
  318. /* Lite remove start */
  319. pcm_test_float ("float.rf64" , SF_FORMAT_RF64 | SF_FORMAT_FLOAT , SF_FALSE) ;
  320. pcm_test_double ("double.rf64" , SF_FORMAT_RF64 | SF_FORMAT_DOUBLE, SF_FALSE) ;
  321. empty_file_test ("empty_char.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_U8) ;
  322. empty_file_test ("empty_short.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
  323. empty_file_test ("empty_float.rf64", SF_FORMAT_RF64 | SF_FORMAT_FLOAT) ;
  324. /* Lite remove end */
  325. test_count++ ;
  326. } ;
  327. if (test_count == 0)
  328. { printf ("Mono : ************************************\n") ;
  329. printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
  330. printf ("Mono : ************************************\n") ;
  331. return 1 ;
  332. } ;
  333. /* Only open file descriptors should be stdin, stdout and stderr. */
  334. check_open_file_count_or_die (__LINE__) ;
  335. return 0 ;
  336. } /* main */
  337. /*============================================================================================
  338. ** Helper functions and macros.
  339. */
  340. static void create_short_file (const char *filename) ;
  341. #define CHAR_ERROR(x, y) (abs ((x) - (y)) > 255)
  342. #define INT_ERROR(x, y) (((x) - (y)) != 0)
  343. #define BIT_20_ERROR(x, y) (abs ((x) - (y)) > 4095)
  344. #define TRIBYTE_ERROR(x, y) (abs ((x) - (y)) > 255)
  345. #define FLOAT_ERROR(x, y) (fabs ((x) - (y)) > 1e-5)
  346. #define CONVERT_DATA(k, len, new, orig) \
  347. { for ((k) = 0 ; (k) < (len) ; (k) ++) \
  348. (new) [k] = (orig) [k] ; \
  349. }
  350. [+ FOR data_type
  351. +]
  352. /*======================================================================================
  353. */
  354. static void mono_[+ (get "type_name") +]_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
  355. static void stereo_[+ (get "type_name") +]_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
  356. static void mono_rdwr_[+ (get "type_name") +]_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
  357. static void new_rdwr_[+ (get "type_name") +]_test (const char *filename, int format, int allow_fd) ;
  358. static void multi_seek_test (const char * filename, int format) ;
  359. static void write_seek_extend_test (const char * filename, int format) ;
  360. static void
  361. pcm_test_[+ (get "type_name") +] (const char *filename, int format, int long_file_ok)
  362. { SF_INFO sfinfo ;
  363. [+ (get "data_type") +] *orig ;
  364. int k, allow_fd ;
  365. /* Sd2 files cannot be opened from an existing file descriptor. */
  366. allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
  367. get_unique_test_name (&filename, WRT_TEST_PREFIX) ;
  368. print_test_name ("pcm_test_[+ (get "type_name") +]", filename) ;
  369. sfinfo.samplerate = 44100 ;
  370. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  371. sfinfo.channels = 1 ;
  372. sfinfo.format = format ;
  373. test_sf_format_or_die (&sfinfo, __LINE__) ;
  374. gen_windowed_sine_double (orig_data.d, DATA_LENGTH, [+ (get "max_val") +]) ;
  375. orig = orig_data.[+ (get "data_field") +] ;
  376. /* Make this a macro so gdb steps over it in one go. */
  377. CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
  378. /* Some test broken out here. */
  379. mono_[+ (get "type_name") +]_test (filename, format, long_file_ok, allow_fd) ;
  380. /* Sub format DWVW does not allow seeking. */
  381. if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
  382. (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
  383. { unlink (filename) ;
  384. printf ("no seek : ok\n") ;
  385. return ;
  386. } ;
  387. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC
  388. && (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_16
  389. && (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_20
  390. && (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_24
  391. && (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_32
  392. )
  393. mono_rdwr_[+ (get "type_name") +]_test (filename, format, long_file_ok, allow_fd) ;
  394. /* If the format doesn't support stereo we're done. */
  395. sfinfo.channels = 2 ;
  396. if (sf_format_check (&sfinfo) == 0)
  397. { unlink (filename) ;
  398. puts ("no stereo : ok") ;
  399. return ;
  400. } ;
  401. stereo_[+ (get "type_name") +]_test (filename, format, long_file_ok, allow_fd) ;
  402. /* New read/write test. Not sure if this is needed yet. */
  403. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF
  404. && (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC
  405. && (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC
  406. && (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_16
  407. && (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_20
  408. && (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_24
  409. && (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_32
  410. )
  411. new_rdwr_[+ (get "type_name") +]_test (filename, format, allow_fd) ;
  412. delete_file (format, filename) ;
  413. puts ("ok") ;
  414. return ;
  415. } /* pcm_test_[+ (get "type_name") +] */
  416. static void
  417. mono_[+ (get "type_name") +]_test (const char *filename, int format, int long_file_ok, int allow_fd)
  418. { SNDFILE *file ;
  419. SF_INFO sfinfo ;
  420. [+ (get "data_type") +] *orig, *test ;
  421. sf_count_t count ;
  422. int k, items, total ;
  423. sfinfo.samplerate = 44100 ;
  424. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  425. sfinfo.channels = 1 ;
  426. sfinfo.format = format ;
  427. orig = orig_data.[+ (get "data_field") +] ;
  428. test = test_data.[+ (get "data_field") +] ;
  429. items = DATA_LENGTH ;
  430. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
  431. if (sfinfo.frames || sfinfo.sections || sfinfo.seekable)
  432. { printf ("\n\nLine %d : Weird SF_INFO fields.\n", __LINE__) ;
  433. exit (1) ;
  434. } ;
  435. sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
  436. test_write_[+ (get "data_type") +]_or_die (file, 0, orig, items, __LINE__) ;
  437. sf_write_sync (file) ;
  438. test_write_[+ (get "data_type") +]_or_die (file, 0, orig, items, __LINE__) ;
  439. sf_write_sync (file) ;
  440. /* Add non-audio data after the audio. */
  441. sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
  442. sf_close (file) ;
  443. memset (test, 0, items * sizeof ([+ (get "data_type") +])) ;
  444. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
  445. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  446. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
  447. if (sfinfo.format != format)
  448. { printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
  449. exit (1) ;
  450. } ;
  451. if (sfinfo.frames < 2 * items)
  452. { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, items) ;
  453. exit (1) ;
  454. } ;
  455. if (! long_file_ok && sfinfo.frames > 2 * items)
  456. { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, items) ;
  457. exit (1) ;
  458. } ;
  459. if (sfinfo.channels != 1)
  460. { printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
  461. exit (1) ;
  462. } ;
  463. if (sfinfo.seekable != 1)
  464. { printf ("\n\nLine %d : File should be seekable.\n", __LINE__) ;
  465. exit (1) ;
  466. } ;
  467. check_log_buffer_or_die (file, __LINE__) ;
  468. test_read_[+ (get "data_type") +]_or_die (file, 0, test, items, __LINE__) ;
  469. for (k = 0 ; k < items ; k++)
  470. if ([+ (get "error_func") +] (orig [k], test [k]))
  471. { printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ;
  472. oct_save_[+ (get "data_type") +] (orig, test, items) ;
  473. exit (1) ;
  474. } ;
  475. /* Test multiple short reads. */
  476. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  477. total = 0 ;
  478. for (k = 1 ; k <= 32 ; k++)
  479. { int ik ;
  480. test_read_[+ (get "data_type") +]_or_die (file, 0, test + total, k, __LINE__) ;
  481. total += k ;
  482. for (ik = 0 ; ik < total ; ik++)
  483. if ([+ (get "error_func") +] (orig [ik], test [ik]))
  484. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, ik, orig [ik], test [ik]) ;
  485. exit (1) ;
  486. } ;
  487. } ;
  488. /* Seek to start of file. */
  489. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  490. test_read_[+ (get "data_type") +]_or_die (file, 0, test, 4, __LINE__) ;
  491. for (k = 0 ; k < 4 ; k++)
  492. if ([+ (get "error_func") +] (orig [k], test [k]))
  493. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ;
  494. exit (1) ;
  495. } ;
  496. /* For some codecs we can't go past here. */
  497. if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
  498. (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
  499. { sf_close (file) ;
  500. unlink (filename) ;
  501. printf ("no seek : ") ;
  502. return ;
  503. } ;
  504. /* Seek to offset from start of file. */
  505. test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
  506. test_read_[+ (get "data_type") +]_or_die (file, 0, test + 10, 4, __LINE__) ;
  507. for (k = 10 ; k < 14 ; k++)
  508. if ([+ (get "error_func") +] (orig [k], test [k]))
  509. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, test [k], orig [k]) ;
  510. exit (1) ;
  511. } ;
  512. /* Seek to offset from current position. */
  513. test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
  514. test_read_[+ (get "data_type") +]_or_die (file, 0, test + 20, 4, __LINE__) ;
  515. for (k = 20 ; k < 24 ; k++)
  516. if ([+ (get "error_func") +] (orig [k], test [k]))
  517. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, test [k], orig [k]) ;
  518. exit (1) ;
  519. } ;
  520. /* Seek to offset from end of file. */
  521. test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
  522. test_read_[+ (get "data_type") +]_or_die (file, 0, test + 10, 4, __LINE__) ;
  523. for (k = 10 ; k < 14 ; k++)
  524. if ([+ (get "error_func") +] (orig [k], test [k]))
  525. { printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, test [k], orig [k]) ;
  526. exit (1) ;
  527. } ;
  528. /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
  529. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  530. count = 0 ;
  531. while (count < sfinfo.frames)
  532. count += sf_read_[+ (get "data_type") +] (file, test, 311) ;
  533. /* Check that no error has occurred. */
  534. if (sf_error (file))
  535. { printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
  536. puts (sf_strerror (file)) ;
  537. exit (1) ;
  538. } ;
  539. /* Check that we haven't read beyond EOF. */
  540. if (count > sfinfo.frames)
  541. { printf ("\n\nLines %d : read past end of file (%" PRId64 " should be %" PRId64 ")\n", __LINE__, count, sfinfo.frames) ;
  542. exit (1) ;
  543. } ;
  544. test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
  545. sf_close (file) ;
  546. multi_seek_test (filename, format) ;
  547. write_seek_extend_test (filename, format) ;
  548. } /* mono_[+ (get "type_name") +]_test */
  549. static void
  550. stereo_[+ (get "type_name") +]_test (const char *filename, int format, int long_file_ok, int allow_fd)
  551. { SNDFILE *file ;
  552. SF_INFO sfinfo ;
  553. [+ (get "data_type") +] *orig, *test ;
  554. int k, items, frames ;
  555. sfinfo.samplerate = 44100 ;
  556. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  557. sfinfo.channels = 2 ;
  558. sfinfo.format = format ;
  559. gen_windowed_sine_double (orig_data.d, DATA_LENGTH, [+ (get "max_val") +]) ;
  560. orig = orig_data.[+ (get "data_field") +] ;
  561. test = test_data.[+ (get "data_field") +] ;
  562. /* Make this a macro so gdb steps over it in one go. */
  563. CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
  564. items = DATA_LENGTH ;
  565. frames = items / sfinfo.channels ;
  566. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
  567. sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
  568. test_writef_[+ (get "data_type") +]_or_die (file, 0, orig, frames, __LINE__) ;
  569. sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
  570. sf_close (file) ;
  571. memset (test, 0, items * sizeof ([+ (get "data_type") +])) ;
  572. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
  573. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  574. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
  575. if (sfinfo.format != format)
  576. { printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
  577. __LINE__, format, sfinfo.format) ;
  578. exit (1) ;
  579. } ;
  580. if (sfinfo.frames < frames)
  581. { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n",
  582. __LINE__, sfinfo.frames, frames) ;
  583. exit (1) ;
  584. } ;
  585. if (! long_file_ok && sfinfo.frames > frames)
  586. { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%" PRId64 " should be %d)\n",
  587. __LINE__, sfinfo.frames, frames) ;
  588. exit (1) ;
  589. } ;
  590. if (sfinfo.channels != 2)
  591. { printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
  592. exit (1) ;
  593. } ;
  594. check_log_buffer_or_die (file, __LINE__) ;
  595. test_readf_[+ (get "data_type") +]_or_die (file, 0, test, frames, __LINE__) ;
  596. for (k = 0 ; k < items ; k++)
  597. if ([+ (get "error_func") +] (test [k], orig [k]))
  598. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ;
  599. exit (1) ;
  600. } ;
  601. /* Seek to start of file. */
  602. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  603. test_readf_[+ (get "data_type") +]_or_die (file, 0, test, 2, __LINE__) ;
  604. for (k = 0 ; k < 4 ; k++)
  605. if ([+ (get "error_func") +] (test [k], orig [k]))
  606. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ;
  607. exit (1) ;
  608. } ;
  609. /* Seek to offset from start of file. */
  610. test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
  611. /* Check for errors here. */
  612. if (sf_error (file))
  613. { printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
  614. puts (sf_strerror (file)) ;
  615. exit (1) ;
  616. } ;
  617. if (sf_read_[+ (get "data_type") +] (file, test, 1) > 0)
  618. { printf ("Line %d: Should return 0.\n", __LINE__) ;
  619. exit (1) ;
  620. } ;
  621. if (! sf_error (file))
  622. { printf ("Line %d: Should return an error.\n", __LINE__) ;
  623. exit (1) ;
  624. } ;
  625. /*-----------------------*/
  626. test_readf_[+ (get "data_type") +]_or_die (file, 0, test + 10, 2, __LINE__) ;
  627. for (k = 20 ; k < 24 ; k++)
  628. if ([+ (get "error_func") +] (test [k], orig [k]))
  629. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ;
  630. exit (1) ;
  631. } ;
  632. /* Seek to offset from current position. */
  633. test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
  634. test_readf_[+ (get "data_type") +]_or_die (file, 0, test + 20, 2, __LINE__) ;
  635. for (k = 40 ; k < 44 ; k++)
  636. if ([+ (get "error_func") +] (test [k], orig [k]))
  637. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ;
  638. exit (1) ;
  639. } ;
  640. /* Seek to offset from end of file. */
  641. test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
  642. test_readf_[+ (get "data_type") +]_or_die (file, 0, test + 20, 2, __LINE__) ;
  643. for (k = 20 ; k < 24 ; k++)
  644. if ([+ (get "error_func") +] (test [k], orig [k]))
  645. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ;
  646. exit (1) ;
  647. } ;
  648. sf_close (file) ;
  649. } /* stereo_[+ (get "type_name") +]_test */
  650. static void
  651. mono_rdwr_[+ (get "type_name") +]_test (const char *filename, int format, int long_file_ok, int allow_fd)
  652. { SNDFILE *file ;
  653. SF_INFO sfinfo ;
  654. [+ (get "data_type") +] *orig, *test ;
  655. int k, pass ;
  656. switch (format & SF_FORMAT_SUBMASK)
  657. { case SF_FORMAT_ALAC_16 :
  658. case SF_FORMAT_ALAC_20 :
  659. case SF_FORMAT_ALAC_24 :
  660. case SF_FORMAT_ALAC_32 :
  661. allow_fd = 0 ;
  662. break ;
  663. default :
  664. break ;
  665. } ;
  666. orig = orig_data.[+ (get "data_field") +] ;
  667. test = test_data.[+ (get "data_field") +] ;
  668. sfinfo.samplerate = SAMPLE_RATE ;
  669. sfinfo.frames = DATA_LENGTH ;
  670. sfinfo.channels = 1 ;
  671. sfinfo.format = format ;
  672. if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
  673. || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
  674. || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
  675. unlink (filename) ;
  676. else
  677. { /* Create a short file. */
  678. create_short_file (filename) ;
  679. /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
  680. ** If this returns a valif pointer sf_open() screwed up.
  681. */
  682. if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
  683. { printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
  684. exit (1) ;
  685. } ;
  686. /* Truncate the file to zero bytes. */
  687. if (truncate_file_to_zero (filename) < 0)
  688. { printf ("\n\nLine %d: truncate_file_to_zero (%s) failed", __LINE__, filename) ;
  689. perror (NULL) ;
  690. exit (1) ;
  691. } ;
  692. } ;
  693. /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
  694. ** all the usual data required when opening the file in WRITE mode.
  695. */
  696. sfinfo.samplerate = SAMPLE_RATE ;
  697. sfinfo.frames = DATA_LENGTH ;
  698. sfinfo.channels = 1 ;
  699. sfinfo.format = format ;
  700. file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
  701. /* Do 3 writes followed by reads. After each, check the data and the current
  702. ** read and write offsets.
  703. */
  704. for (pass = 1 ; pass <= 3 ; pass ++)
  705. { orig [20] = pass * 2 ;
  706. /* Write some data. */
  707. test_write_[+ (get "data_type") +]_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
  708. test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
  709. /* Read what we just wrote. */
  710. test_read_[+ (get "data_type") +]_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
  711. /* Check the data. */
  712. for (k = 0 ; k < DATA_LENGTH ; k++)
  713. if ([+ (get "error_func") +] (orig [k], test [k]))
  714. { printf ("\n\nLine %d (pass %d) A : Error at sample %d ([+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, pass, k, orig [k], test [k]) ;
  715. oct_save_[+ (get "data_type") +] (orig, test, DATA_LENGTH) ;
  716. exit (1) ;
  717. } ;
  718. test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
  719. } ; /* for (pass ...) */
  720. sf_close (file) ;
  721. /* Open the file again to check the data. */
  722. file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
  723. if (sfinfo.format != format)
  724. { printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
  725. exit (1) ;
  726. } ;
  727. if (sfinfo.frames < 3 * DATA_LENGTH)
  728. { printf ("\n\nLine %d : Not enough frames in file. (%" PRId64 " < %d)\n", __LINE__, sfinfo.frames, 3 * DATA_LENGTH) ;
  729. exit (1) ;
  730. }
  731. if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
  732. { printf ("\n\nLine %d : Incorrect number of frames in file. (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, 3 * DATA_LENGTH) ;
  733. exit (1) ;
  734. } ;
  735. if (sfinfo.channels != 1)
  736. { printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
  737. exit (1) ;
  738. } ;
  739. if (! long_file_ok)
  740. test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
  741. else
  742. test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
  743. for (pass = 1 ; pass <= 3 ; pass ++)
  744. { orig [20] = pass * 2 ;
  745. test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
  746. /* Read what we just wrote. */
  747. test_read_[+ (get "data_type") +]_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
  748. /* Check the data. */
  749. for (k = 0 ; k < DATA_LENGTH ; k++)
  750. if ([+ (get "error_func") +] (orig [k], test [k]))
  751. { printf ("\n\nLine %d (pass %d) B : Error at sample %d ([+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, pass, k, orig [k], test [k]) ;
  752. oct_save_[+ (get "data_type") +] (orig, test, DATA_LENGTH) ;
  753. exit (1) ;
  754. } ;
  755. } ; /* for (pass ...) */
  756. sf_close (file) ;
  757. } /* mono_rdwr_[+ (get "data_type") +]_test */
  758. static void
  759. new_rdwr_[+ (get "type_name") +]_test (const char *filename, int format, int allow_fd)
  760. { SNDFILE *wfile, *rwfile ;
  761. SF_INFO sfinfo ;
  762. [+ (get "data_type") +] *orig, *test ;
  763. int items, frames ;
  764. orig = orig_data.[+ (get "data_field") +] ;
  765. test = test_data.[+ (get "data_field") +] ;
  766. sfinfo.samplerate = 44100 ;
  767. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  768. sfinfo.channels = 2 ;
  769. sfinfo.format = format ;
  770. items = DATA_LENGTH ;
  771. frames = items / sfinfo.channels ;
  772. wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
  773. sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
  774. test_writef_[+ (get "data_type") +]_or_die (wfile, 1, orig, frames, __LINE__) ;
  775. sf_write_sync (wfile) ;
  776. test_writef_[+ (get "data_type") +]_or_die (wfile, 2, orig, frames, __LINE__) ;
  777. sf_write_sync (wfile) ;
  778. rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
  779. if (sfinfo.frames != 2 * frames)
  780. { printf ("\n\nLine %d : incorrect number of frames in file (%" PRId64 " should be %d)\n\n", __LINE__, sfinfo.frames, 2 * frames) ;
  781. exit (1) ;
  782. } ;
  783. test_writef_[+ (get "data_type") +]_or_die (wfile, 3, orig, frames, __LINE__) ;
  784. test_readf_[+ (get "data_type") +]_or_die (rwfile, 1, test, frames, __LINE__) ;
  785. test_readf_[+ (get "data_type") +]_or_die (rwfile, 2, test, frames, __LINE__) ;
  786. sf_close (wfile) ;
  787. sf_close (rwfile) ;
  788. } /* new_rdwr_[+ (get "type_name") +]_test */
  789. [+ ENDFOR data_type +]
  790. /*----------------------------------------------------------------------------------------
  791. */
  792. static void
  793. empty_file_test (const char *filename, int format)
  794. { SNDFILE *file ;
  795. SF_INFO info ;
  796. int allow_fd ;
  797. /* Sd2 files cannot be opened from an existing file descriptor. */
  798. allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
  799. get_unique_test_name (&filename, WRT_TEST_PREFIX) ;
  800. print_test_name ("empty_file_test", filename) ;
  801. unlink (filename) ;
  802. info.samplerate = 48000 ;
  803. info.channels = 2 ;
  804. info.format = format ;
  805. info.frames = 0 ;
  806. if (sf_format_check (&info) == SF_FALSE)
  807. { info.channels = 1 ;
  808. if (sf_format_check (&info) == SF_FALSE)
  809. { puts ("invalid file format") ;
  810. return ;
  811. } ;
  812. } ;
  813. /* Create an empty file. */
  814. file = test_open_file_or_die (filename, SFM_WRITE, &info, allow_fd, __LINE__) ;
  815. sf_close (file) ;
  816. /* Open for read and check the length. */
  817. file = test_open_file_or_die (filename, SFM_READ, &info, allow_fd, __LINE__) ;
  818. if (info.frames != 0)
  819. { printf ("\n\nError : frame count (%" PRId64 ") should be zero.\n", info.frames) ;
  820. exit (1) ;
  821. } ;
  822. sf_close (file) ;
  823. /* Open for read/write and check the length. */
  824. file = test_open_file_or_die (filename, SFM_RDWR, &info, allow_fd, __LINE__) ;
  825. if (info.frames != 0)
  826. { printf ("\n\nError : frame count (%" PRId64 ") should be zero.\n", info.frames) ;
  827. exit (1) ;
  828. } ;
  829. sf_close (file) ;
  830. /* Open for read and check the length. */
  831. file = test_open_file_or_die (filename, SFM_READ, &info, allow_fd, __LINE__) ;
  832. if (info.frames != 0)
  833. { printf ("\n\nError : frame count (%" PRId64 ") should be zero.\n", info.frames) ;
  834. exit (1) ;
  835. } ;
  836. sf_close (file) ;
  837. check_open_file_count_or_die (__LINE__) ;
  838. unlink (filename) ;
  839. puts ("ok") ;
  840. return ;
  841. } /* empty_file_test */
  842. /*----------------------------------------------------------------------------------------
  843. */
  844. static void
  845. create_short_file (const char *filename)
  846. { FILE *file ;
  847. if (! (file = fopen (filename, "w")))
  848. { printf ("create_short_file : fopen (%s, \"w\") failed.", filename) ;
  849. fflush (stdout) ;
  850. perror (NULL) ;
  851. exit (1) ;
  852. } ;
  853. fprintf (file, "This is the file data.\n") ;
  854. fclose (file) ;
  855. } /* create_short_file */
  856. static void
  857. multi_seek_test (const char * filename, int format)
  858. { SNDFILE * file ;
  859. SF_INFO info ;
  860. sf_count_t pos ;
  861. int k ;
  862. /* This test doesn't work on the following. */
  863. switch (format & SF_FORMAT_TYPEMASK)
  864. { case SF_FORMAT_RAW :
  865. return ;
  866. default :
  867. break ;
  868. } ;
  869. memset (&info, 0, sizeof (info)) ;
  870. generate_file (filename, format, 88200) ;
  871. file = test_open_file_or_die (filename, SFM_READ, &info, SF_FALSE, __LINE__) ;
  872. for (k = 0 ; k < 10 ; k++)
  873. { pos = info.frames / (k + 2) ;
  874. test_seek_or_die (file, pos, SEEK_SET, pos, info.channels, __LINE__) ;
  875. } ;
  876. sf_close (file) ;
  877. } /* multi_seek_test */
  878. static void
  879. write_seek_extend_test (const char * filename, int format)
  880. { SNDFILE * file ;
  881. SF_INFO info ;
  882. short *orig, *test ;
  883. unsigned items, k ;
  884. /* This test doesn't work on the following container formats. */
  885. switch (format & SF_FORMAT_TYPEMASK)
  886. { case SF_FORMAT_FLAC :
  887. case SF_FORMAT_HTK :
  888. case SF_FORMAT_PAF :
  889. case SF_FORMAT_SDS :
  890. case SF_FORMAT_SVX :
  891. return ;
  892. default :
  893. break ;
  894. } ;
  895. /* This test doesn't work on the following codec formats. */
  896. switch (format & SF_FORMAT_SUBMASK)
  897. { case SF_FORMAT_ALAC_16 :
  898. case SF_FORMAT_ALAC_20 :
  899. case SF_FORMAT_ALAC_24 :
  900. case SF_FORMAT_ALAC_32 :
  901. return ;
  902. default :
  903. break ;
  904. } ;
  905. memset (&info, 0, sizeof (info)) ;
  906. info.samplerate = 48000 ;
  907. info.channels = 1 ;
  908. info.format = format ;
  909. items = 512 ;
  910. exit_if_true (items > ARRAY_LEN (orig_data.s), "Line %d : Bad assumption.\n", __LINE__) ;
  911. orig = orig_data.s ;
  912. test = test_data.s ;
  913. for (k = 0 ; k < ARRAY_LEN (orig_data.s) ; k++)
  914. orig [k] = 0x3fff ;
  915. file = test_open_file_or_die (filename, SFM_WRITE, &info, SF_FALSE, __LINE__) ;
  916. test_write_short_or_die (file, 0, orig, items, __LINE__) ;
  917. /* Extend the file using a seek. */
  918. test_seek_or_die (file, 2 * items, SEEK_SET, 2 * items, info.channels, __LINE__) ;
  919. test_writef_short_or_die (file, 0, orig, items, __LINE__) ;
  920. sf_close (file) ;
  921. file = test_open_file_or_die (filename, SFM_READ, &info, SF_FALSE, __LINE__) ;
  922. test_read_short_or_die (file, 0, test, 3 * items, __LINE__) ;
  923. sf_close (file) ;
  924. if (info.frames < 3 * items)
  925. { printf ("\n\nLine %d : Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n", __LINE__, info.frames, 3 * items) ;
  926. exit (1) ;
  927. } ;
  928. /* Can't do these formats due to scaling. */
  929. switch (format & SF_FORMAT_SUBMASK)
  930. { case SF_FORMAT_PCM_S8 :
  931. case SF_FORMAT_PCM_U8 :
  932. return ;
  933. default :
  934. break ;
  935. } ;
  936. for (k = 0 ; k < items ; k++)
  937. { exit_if_true (test [k] != 0x3fff, "Line %d : test [%d] == %d, should be 0x3fff.\n", __LINE__, k, test [k]) ;
  938. exit_if_true (test [items + k] != 0, "Line %d : test [%d] == %d, should be 0.\n", __LINE__, items + k, test [items + k]) ;
  939. exit_if_true (test [2 * items + k] != 0x3fff, "Line %d : test [%d] == %d, should be 0x3fff.\n", __LINE__, 2 * items + k, test [2 * items + k]) ;
  940. } ;
  941. return ;
  942. } /* write_seek_extend_test */