write_read_test.tpl 44 KB

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