write_read_test.c 123 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731
  1. /*
  2. ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
  3. **
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU General Public License as published by
  6. ** the Free Software Foundation; either version 2 of the License, or
  7. ** (at your option) any later version.
  8. **
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ** GNU General Public License for more details.
  13. **
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include "sfconfig.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #if HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #endif
  25. #include <math.h>
  26. #if (defined (WIN32) || defined (_WIN32))
  27. #include <fcntl.h>
  28. static int truncate (const char *filename, int ignored) ;
  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 void pcm_test_char (const char *str, int format, int long_file_okz) ;
  37. static void pcm_test_short (const char *str, int format, int long_file_okz) ;
  38. static void pcm_test_24bit (const char *str, int format, int long_file_okz) ;
  39. static void pcm_test_int (const char *str, int format, int long_file_okz) ;
  40. static void pcm_test_float (const char *str, int format, int long_file_okz) ;
  41. static void pcm_test_double (const char *str, int format, int long_file_okz) ;
  42. static void empty_file_test (const char *filename, int format) ;
  43. typedef union
  44. { double d [DATA_LENGTH] ;
  45. float f [DATA_LENGTH] ;
  46. int i [DATA_LENGTH] ;
  47. short s [DATA_LENGTH] ;
  48. char c [DATA_LENGTH] ;
  49. } BUFFER ;
  50. static BUFFER orig_data ;
  51. static BUFFER test_data ;
  52. int
  53. main (int argc, char **argv)
  54. { int do_all = 0 ;
  55. int test_count = 0 ;
  56. count_open_files () ;
  57. if (argc != 2)
  58. { printf ("Usage : %s <test>\n", argv [0]) ;
  59. printf (" Where <test> is one of the following:\n") ;
  60. printf (" wav - test WAV file functions (little endian)\n") ;
  61. printf (" aiff - test AIFF file functions (big endian)\n") ;
  62. printf (" au - test AU file functions\n") ;
  63. printf (" avr - test AVR file functions\n") ;
  64. printf (" caf - test CAF file functions\n") ;
  65. printf (" raw - test RAW header-less PCM file functions\n") ;
  66. printf (" paf - test PAF file functions\n") ;
  67. printf (" svx - test 8SVX/16SV file functions\n") ;
  68. printf (" nist - test NIST Sphere file functions\n") ;
  69. printf (" ircam - test IRCAM file functions\n") ;
  70. printf (" voc - Create Voice file functions\n") ;
  71. printf (" w64 - Sonic Foundry's W64 file functions\n") ;
  72. printf (" flac - test FLAC file functions\n") ;
  73. printf (" mpc2k - test MPC 2000 file functions\n") ;
  74. printf (" rf64 - test RF64 file functions\n") ;
  75. printf (" all - perform all tests\n") ;
  76. exit (1) ;
  77. } ;
  78. do_all = !strcmp (argv [1], "all") ;
  79. if (do_all || ! strcmp (argv [1], "wav"))
  80. { pcm_test_char ("char.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_U8, SF_FALSE) ;
  81. pcm_test_short ("short.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_16, SF_FALSE) ;
  82. pcm_test_24bit ("24bit.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_24, SF_FALSE) ;
  83. pcm_test_int ("int.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_32, SF_FALSE) ;
  84. pcm_test_char ("char.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_U8, SF_FALSE) ;
  85. pcm_test_short ("short.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_16, SF_FALSE) ;
  86. pcm_test_24bit ("24bit.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_24, SF_FALSE) ;
  87. pcm_test_int ("int.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_32, SF_FALSE) ;
  88. pcm_test_24bit ("24bit.wavex" , SF_FORMAT_WAVEX | SF_FORMAT_PCM_24, SF_FALSE) ;
  89. pcm_test_int ("int.wavex" , SF_FORMAT_WAVEX | SF_FORMAT_PCM_32, SF_FALSE) ;
  90. /* Lite remove start */
  91. pcm_test_float ("float.wav" , SF_FORMAT_WAV | SF_FORMAT_FLOAT , SF_FALSE) ;
  92. pcm_test_double ("double.wav" , SF_FORMAT_WAV | SF_FORMAT_DOUBLE, SF_FALSE) ;
  93. pcm_test_float ("float.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_FLOAT , SF_FALSE) ;
  94. pcm_test_double ("double.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_DOUBLE, SF_FALSE) ;
  95. pcm_test_float ("float.wavex" , SF_FORMAT_WAVEX | SF_FORMAT_FLOAT , SF_FALSE) ;
  96. pcm_test_double ("double.wavex" , SF_FORMAT_WAVEX | SF_FORMAT_DOUBLE, SF_FALSE) ;
  97. /* Lite remove end */
  98. empty_file_test ("empty_char.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8) ;
  99. empty_file_test ("empty_short.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
  100. empty_file_test ("empty_float.wav", SF_FORMAT_WAV | SF_FORMAT_FLOAT) ;
  101. test_count++ ;
  102. } ;
  103. if (do_all || ! strcmp (argv [1], "aiff"))
  104. { pcm_test_char ("char_u8.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_U8, SF_FALSE) ;
  105. pcm_test_char ("char_s8.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_S8, SF_FALSE) ;
  106. pcm_test_short ("short.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_16, SF_FALSE) ;
  107. pcm_test_24bit ("24bit.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_24, SF_FALSE) ;
  108. pcm_test_int ("int.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_32, SF_FALSE) ;
  109. pcm_test_short ("short_sowt.aifc" , SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | SF_FORMAT_PCM_16, SF_FALSE) ;
  110. pcm_test_24bit ("24bit_sowt.aifc" , SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | SF_FORMAT_PCM_24, SF_FALSE) ;
  111. pcm_test_int ("int_sowt.aifc" , SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | SF_FORMAT_PCM_32, SF_FALSE) ;
  112. pcm_test_short ("short_twos.aifc" , SF_ENDIAN_BIG | SF_FORMAT_AIFF | SF_FORMAT_PCM_16, SF_FALSE) ;
  113. pcm_test_24bit ("24bit_twos.aifc" , SF_ENDIAN_BIG | SF_FORMAT_AIFF | SF_FORMAT_PCM_24, SF_FALSE) ;
  114. pcm_test_int ("int_twos.aifc" , SF_ENDIAN_BIG | SF_FORMAT_AIFF | SF_FORMAT_PCM_32, SF_FALSE) ;
  115. /* Lite remove start */
  116. pcm_test_short ("dwvw16.aifc", SF_FORMAT_AIFF | SF_FORMAT_DWVW_16, SF_TRUE) ;
  117. pcm_test_24bit ("dwvw24.aifc", SF_FORMAT_AIFF | SF_FORMAT_DWVW_24, SF_TRUE) ;
  118. pcm_test_float ("float.aifc" , SF_FORMAT_AIFF | SF_FORMAT_FLOAT , SF_FALSE) ;
  119. pcm_test_double ("double.aifc" , SF_FORMAT_AIFF | SF_FORMAT_DOUBLE, SF_FALSE) ;
  120. /* Lite remove end */
  121. empty_file_test ("empty_char.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_U8) ;
  122. empty_file_test ("empty_short.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_16) ;
  123. empty_file_test ("empty_float.aiff", SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
  124. test_count++ ;
  125. } ;
  126. if (do_all || ! strcmp (argv [1], "au"))
  127. { pcm_test_char ("char.au" , SF_FORMAT_AU | SF_FORMAT_PCM_S8, SF_FALSE) ;
  128. pcm_test_short ("short.au" , SF_FORMAT_AU | SF_FORMAT_PCM_16, SF_FALSE) ;
  129. pcm_test_24bit ("24bit.au" , SF_FORMAT_AU | SF_FORMAT_PCM_24, SF_FALSE) ;
  130. pcm_test_int ("int.au" , SF_FORMAT_AU | SF_FORMAT_PCM_32, SF_FALSE) ;
  131. /* Lite remove start */
  132. pcm_test_float ("float.au" , SF_FORMAT_AU | SF_FORMAT_FLOAT , SF_FALSE) ;
  133. pcm_test_double ("double.au", SF_FORMAT_AU | SF_FORMAT_DOUBLE, SF_FALSE) ;
  134. /* Lite remove end */
  135. pcm_test_char ("char_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_S8, SF_FALSE) ;
  136. pcm_test_short ("short_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_16, SF_FALSE) ;
  137. pcm_test_24bit ("24bit_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_24, SF_FALSE) ;
  138. pcm_test_int ("int_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_32, SF_FALSE) ;
  139. /* Lite remove start */
  140. pcm_test_float ("float_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT , SF_FALSE) ;
  141. pcm_test_double ("double_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE, SF_FALSE) ;
  142. /* Lite remove end */
  143. test_count++ ;
  144. } ;
  145. if (do_all || ! strcmp (argv [1], "caf"))
  146. { pcm_test_char ("char.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_S8, SF_FALSE) ;
  147. pcm_test_short ("short.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_16, SF_FALSE) ;
  148. pcm_test_24bit ("24bit.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_24, SF_FALSE) ;
  149. pcm_test_int ("int.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_32, SF_FALSE) ;
  150. /* Lite remove start */
  151. pcm_test_float ("float.caf" , SF_FORMAT_CAF | SF_FORMAT_FLOAT , SF_FALSE) ;
  152. pcm_test_double ("double.caf" , SF_FORMAT_CAF | SF_FORMAT_DOUBLE, SF_FALSE) ;
  153. /* Lite remove end */
  154. pcm_test_short ("short_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_PCM_16, SF_FALSE) ;
  155. pcm_test_24bit ("24bit_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_PCM_24, SF_FALSE) ;
  156. pcm_test_int ("int_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_PCM_32, SF_FALSE) ;
  157. /* Lite remove start */
  158. pcm_test_float ("float_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_FLOAT , SF_FALSE) ;
  159. pcm_test_double ("double_le.caf", SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_DOUBLE, 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. test_count++ ;
  298. } ;
  299. if (do_all || ! strcmp (argv [1], "flac"))
  300. { if (HAVE_EXTERNAL_LIBS)
  301. { pcm_test_char ("char.flac" , SF_FORMAT_FLAC | SF_FORMAT_PCM_S8, SF_TRUE) ;
  302. pcm_test_short ("short.flac" , SF_FORMAT_FLAC | SF_FORMAT_PCM_16, SF_TRUE) ;
  303. pcm_test_24bit ("24bit.flac" , SF_FORMAT_FLAC | SF_FORMAT_PCM_24, SF_TRUE) ;
  304. }
  305. else
  306. puts (" No FLAC tests because FLAC support was not compiled in.") ;
  307. test_count++ ;
  308. } ;
  309. if (do_all || ! strcmp (argv [1], "rf64"))
  310. { pcm_test_char ("char.rf64" , SF_FORMAT_RF64 | SF_FORMAT_PCM_U8, SF_FALSE) ;
  311. pcm_test_short ("short.rf64" , SF_FORMAT_RF64 | SF_FORMAT_PCM_16, SF_FALSE) ;
  312. pcm_test_24bit ("24bit.rf64" , SF_FORMAT_RF64 | SF_FORMAT_PCM_24, SF_FALSE) ;
  313. pcm_test_int ("int.rf64" , SF_FORMAT_RF64 | SF_FORMAT_PCM_32, SF_FALSE) ;
  314. /* Lite remove start */
  315. pcm_test_float ("float.rf64" , SF_FORMAT_RF64 | SF_FORMAT_FLOAT , SF_FALSE) ;
  316. pcm_test_double ("double.rf64" , SF_FORMAT_RF64 | SF_FORMAT_DOUBLE, SF_FALSE) ;
  317. empty_file_test ("empty_char.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_U8) ;
  318. empty_file_test ("empty_short.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
  319. empty_file_test ("empty_float.rf64", SF_FORMAT_RF64 | SF_FORMAT_FLOAT) ;
  320. /* Lite remove end */
  321. test_count++ ;
  322. } ;
  323. if (test_count == 0)
  324. { printf ("Mono : ************************************\n") ;
  325. printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
  326. printf ("Mono : ************************************\n") ;
  327. return 1 ;
  328. } ;
  329. /* Only open file descriptors should be stdin, stdout and stderr. */
  330. check_open_file_count_or_die (__LINE__) ;
  331. return 0 ;
  332. } /* main */
  333. /*============================================================================================
  334. ** Helper functions and macros.
  335. */
  336. static void create_short_file (const char *filename) ;
  337. #define CHAR_ERROR(x,y) (abs ((x) - (y)) > 255)
  338. #define INT_ERROR(x,y) (((x) - (y)) != 0)
  339. #define TRIBYTE_ERROR(x,y) (abs ((x) - (y)) > 255)
  340. #define FLOAT_ERROR(x,y) (fabs ((x) - (y)) > 1e-5)
  341. #define CONVERT_DATA(k,len,new,orig) \
  342. { for ((k) = 0 ; (k) < (len) ; (k) ++) \
  343. (new) [k] = (orig) [k] ; \
  344. }
  345. /*======================================================================================
  346. */
  347. static void mono_char_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
  348. static void stereo_char_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
  349. static void mono_rdwr_char_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
  350. static void new_rdwr_char_test (const char *filename, int format, int allow_fd) ;
  351. static void multi_seek_test (const char * filename, int format) ;
  352. static void write_seek_extend_test (const char * filename, int format) ;
  353. static void
  354. pcm_test_char (const char *filename, int format, int long_file_ok)
  355. { SF_INFO sfinfo ;
  356. short *orig ;
  357. int k, allow_fd ;
  358. /* Sd2 files cannot be opened from an existing file descriptor. */
  359. allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
  360. print_test_name ("pcm_test_char", filename) ;
  361. sfinfo.samplerate = 44100 ;
  362. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  363. sfinfo.channels = 1 ;
  364. sfinfo.format = format ;
  365. gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 32000.0) ;
  366. orig = orig_data.s ;
  367. /* Make this a macro so gdb steps over it in one go. */
  368. CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
  369. /* Some test broken out here. */
  370. mono_char_test (filename, format, long_file_ok, allow_fd) ;
  371. /* Sub format DWVW does not allow seeking. */
  372. if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
  373. (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
  374. { unlink (filename) ;
  375. printf ("no seek : ok\n") ;
  376. return ;
  377. } ;
  378. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
  379. mono_rdwr_char_test (filename, format, long_file_ok, allow_fd) ;
  380. /* If the format doesn't support stereo we're done. */
  381. sfinfo.channels = 2 ;
  382. if (sf_format_check (&sfinfo) == 0)
  383. { unlink (filename) ;
  384. puts ("no stereo : ok") ;
  385. return ;
  386. } ;
  387. stereo_char_test (filename, format, long_file_ok, allow_fd) ;
  388. /* New read/write test. Not sure if this is needed yet. */
  389. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
  390. (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
  391. (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
  392. new_rdwr_char_test (filename, format, allow_fd) ;
  393. delete_file (format, filename) ;
  394. puts ("ok") ;
  395. return ;
  396. } /* pcm_test_char */
  397. static void
  398. mono_char_test (const char *filename, int format, int long_file_ok, int allow_fd)
  399. { SNDFILE *file ;
  400. SF_INFO sfinfo ;
  401. short *orig, *test ;
  402. sf_count_t count ;
  403. int k, items ;
  404. sfinfo.samplerate = 44100 ;
  405. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  406. sfinfo.channels = 1 ;
  407. sfinfo.format = format ;
  408. orig = orig_data.s ;
  409. test = test_data.s ;
  410. items = DATA_LENGTH ;
  411. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
  412. sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
  413. test_write_short_or_die (file, 0, orig, items, __LINE__) ;
  414. sf_write_sync (file) ;
  415. test_write_short_or_die (file, 0, orig, items, __LINE__) ;
  416. sf_write_sync (file) ;
  417. /* Add non-audio data after the audio. */
  418. sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
  419. sf_close (file) ;
  420. memset (test, 0, items * sizeof (short)) ;
  421. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
  422. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  423. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
  424. if (sfinfo.format != format)
  425. { printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
  426. exit (1) ;
  427. } ;
  428. if (sfinfo.frames < 2 * items)
  429. { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
  430. exit (1) ;
  431. } ;
  432. if (! long_file_ok && sfinfo.frames > 2 * items)
  433. { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
  434. exit (1) ;
  435. } ;
  436. if (sfinfo.channels != 1)
  437. { printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
  438. exit (1) ;
  439. } ;
  440. check_log_buffer_or_die (file, __LINE__) ;
  441. test_read_short_or_die (file, 0, test, items, __LINE__) ;
  442. for (k = 0 ; k < items ; k++)
  443. if (CHAR_ERROR (orig [k], test [k]))
  444. { printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  445. oct_save_short (orig, test, items) ;
  446. exit (1) ;
  447. } ;
  448. /* Seek to start of file. */
  449. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  450. test_read_short_or_die (file, 0, test, 4, __LINE__) ;
  451. for (k = 0 ; k < 4 ; k++)
  452. if (CHAR_ERROR (orig [k], test [k]))
  453. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  454. exit (1) ;
  455. } ;
  456. if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
  457. (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
  458. { sf_close (file) ;
  459. unlink (filename) ;
  460. printf ("no seek : ") ;
  461. return ;
  462. } ;
  463. /* Seek to offset from start of file. */
  464. test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
  465. test_read_short_or_die (file, 0, test + 10, 4, __LINE__) ;
  466. for (k = 10 ; k < 14 ; k++)
  467. if (CHAR_ERROR (orig [k], test [k]))
  468. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
  469. exit (1) ;
  470. } ;
  471. /* Seek to offset from current position. */
  472. test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
  473. test_read_short_or_die (file, 0, test + 20, 4, __LINE__) ;
  474. for (k = 20 ; k < 24 ; k++)
  475. if (CHAR_ERROR (orig [k], test [k]))
  476. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
  477. exit (1) ;
  478. } ;
  479. /* Seek to offset from end of file. */
  480. test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
  481. test_read_short_or_die (file, 0, test + 10, 4, __LINE__) ;
  482. for (k = 10 ; k < 14 ; k++)
  483. if (CHAR_ERROR (orig [k], test [k]))
  484. { printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
  485. exit (1) ;
  486. } ;
  487. /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
  488. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  489. count = 0 ;
  490. while (count < sfinfo.frames)
  491. count += sf_read_short (file, test, 311) ;
  492. /* Check that no error has occurred. */
  493. if (sf_error (file))
  494. { printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
  495. puts (sf_strerror (file)) ;
  496. exit (1) ;
  497. } ;
  498. /* Check that we haven't read beyond EOF. */
  499. if (count > sfinfo.frames)
  500. { printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
  501. exit (1) ;
  502. } ;
  503. test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
  504. sf_close (file) ;
  505. multi_seek_test (filename, format) ;
  506. write_seek_extend_test (filename, format) ;
  507. } /* mono_char_test */
  508. static void
  509. stereo_char_test (const char *filename, int format, int long_file_ok, int allow_fd)
  510. { SNDFILE *file ;
  511. SF_INFO sfinfo ;
  512. short *orig, *test ;
  513. int k, items, frames ;
  514. sfinfo.samplerate = 44100 ;
  515. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  516. sfinfo.channels = 2 ;
  517. sfinfo.format = format ;
  518. gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 32000.0) ;
  519. orig = orig_data.s ;
  520. test = test_data.s ;
  521. /* Make this a macro so gdb steps over it in one go. */
  522. CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
  523. items = DATA_LENGTH ;
  524. frames = items / sfinfo.channels ;
  525. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
  526. sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
  527. test_writef_short_or_die (file, 0, orig, frames, __LINE__) ;
  528. sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
  529. sf_close (file) ;
  530. memset (test, 0, items * sizeof (short)) ;
  531. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
  532. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  533. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
  534. if (sfinfo.format != format)
  535. { printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
  536. __LINE__, format, sfinfo.format) ;
  537. exit (1) ;
  538. } ;
  539. if (sfinfo.frames < frames)
  540. { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
  541. __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
  542. exit (1) ;
  543. } ;
  544. if (! long_file_ok && sfinfo.frames > frames)
  545. { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
  546. __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
  547. exit (1) ;
  548. } ;
  549. if (sfinfo.channels != 2)
  550. { printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
  551. exit (1) ;
  552. } ;
  553. check_log_buffer_or_die (file, __LINE__) ;
  554. test_readf_short_or_die (file, 0, test, frames, __LINE__) ;
  555. for (k = 0 ; k < items ; k++)
  556. if (CHAR_ERROR (test [k], orig [k]))
  557. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  558. exit (1) ;
  559. } ;
  560. /* Seek to start of file. */
  561. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  562. test_readf_short_or_die (file, 0, test, 2, __LINE__) ;
  563. for (k = 0 ; k < 4 ; k++)
  564. if (CHAR_ERROR (test [k], orig [k]))
  565. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  566. exit (1) ;
  567. } ;
  568. /* Seek to offset from start of file. */
  569. test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
  570. /* Check for errors here. */
  571. if (sf_error (file))
  572. { printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
  573. puts (sf_strerror (file)) ;
  574. exit (1) ;
  575. } ;
  576. if (sf_read_short (file, test, 1) > 0)
  577. { printf ("Line %d: Should return 0.\n", __LINE__) ;
  578. exit (1) ;
  579. } ;
  580. if (! sf_error (file))
  581. { printf ("Line %d: Should return an error.\n", __LINE__) ;
  582. exit (1) ;
  583. } ;
  584. /*-----------------------*/
  585. test_readf_short_or_die (file, 0, test + 10, 2, __LINE__) ;
  586. for (k = 20 ; k < 24 ; k++)
  587. if (CHAR_ERROR (test [k], orig [k]))
  588. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  589. exit (1) ;
  590. } ;
  591. /* Seek to offset from current position. */
  592. test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
  593. test_readf_short_or_die (file, 0, test + 20, 2, __LINE__) ;
  594. for (k = 40 ; k < 44 ; k++)
  595. if (CHAR_ERROR (test [k], orig [k]))
  596. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  597. exit (1) ;
  598. } ;
  599. /* Seek to offset from end of file. */
  600. test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
  601. test_readf_short_or_die (file, 0, test + 20, 2, __LINE__) ;
  602. for (k = 20 ; k < 24 ; k++)
  603. if (CHAR_ERROR (test [k], orig [k]))
  604. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  605. exit (1) ;
  606. } ;
  607. sf_close (file) ;
  608. } /* stereo_char_test */
  609. static void
  610. mono_rdwr_char_test (const char *filename, int format, int long_file_ok, int allow_fd)
  611. { SNDFILE *file ;
  612. SF_INFO sfinfo ;
  613. short *orig, *test ;
  614. int k, pass ;
  615. orig = orig_data.s ;
  616. test = test_data.s ;
  617. sfinfo.samplerate = SAMPLE_RATE ;
  618. sfinfo.frames = DATA_LENGTH ;
  619. sfinfo.channels = 1 ;
  620. sfinfo.format = format ;
  621. if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
  622. || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
  623. || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
  624. unlink (filename) ;
  625. else
  626. { /* Create a short file. */
  627. create_short_file (filename) ;
  628. /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
  629. ** If this returns a valif pointer sf_open() screwed up.
  630. */
  631. if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
  632. { printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
  633. exit (1) ;
  634. } ;
  635. /* Truncate the file to zero bytes. */
  636. if (truncate (filename, 0) < 0)
  637. { printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
  638. perror (NULL) ;
  639. exit (1) ;
  640. } ;
  641. } ;
  642. /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
  643. ** all the usual data required when opening the file in WRITE mode.
  644. */
  645. sfinfo.samplerate = SAMPLE_RATE ;
  646. sfinfo.frames = DATA_LENGTH ;
  647. sfinfo.channels = 1 ;
  648. sfinfo.format = format ;
  649. file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
  650. /* Do 3 writes followed by reads. After each, check the data and the current
  651. ** read and write offsets.
  652. */
  653. for (pass = 1 ; pass <= 3 ; pass ++)
  654. { orig [20] = pass * 2 ;
  655. /* Write some data. */
  656. test_write_short_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
  657. test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
  658. /* Read what we just wrote. */
  659. test_read_short_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
  660. /* Check the data. */
  661. for (k = 0 ; k < DATA_LENGTH ; k++)
  662. if (CHAR_ERROR (orig [k], test [k]))
  663. { printf ("\n\nLine %d (pass %d) A : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
  664. oct_save_short (orig, test, DATA_LENGTH) ;
  665. exit (1) ;
  666. } ;
  667. test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
  668. } ; /* for (pass ...) */
  669. sf_close (file) ;
  670. /* Open the file again to check the data. */
  671. file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
  672. if (sfinfo.format != format)
  673. { printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
  674. exit (1) ;
  675. } ;
  676. if (sfinfo.frames < 3 * DATA_LENGTH)
  677. { printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
  678. exit (1) ;
  679. }
  680. if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
  681. { printf ("\n\nLine %d : Incorrect number of frames in file. (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
  682. exit (1) ;
  683. } ;
  684. if (sfinfo.channels != 1)
  685. { printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
  686. exit (1) ;
  687. } ;
  688. if (! long_file_ok)
  689. test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
  690. else
  691. test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
  692. for (pass = 1 ; pass <= 3 ; pass ++)
  693. { orig [20] = pass * 2 ;
  694. test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
  695. /* Read what we just wrote. */
  696. test_read_short_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
  697. /* Check the data. */
  698. for (k = 0 ; k < DATA_LENGTH ; k++)
  699. if (CHAR_ERROR (orig [k], test [k]))
  700. { printf ("\n\nLine %d (pass %d) B : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
  701. oct_save_short (orig, test, DATA_LENGTH) ;
  702. exit (1) ;
  703. } ;
  704. } ; /* for (pass ...) */
  705. sf_close (file) ;
  706. } /* mono_rdwr_short_test */
  707. static void
  708. new_rdwr_char_test (const char *filename, int format, int allow_fd)
  709. { SNDFILE *wfile, *rwfile ;
  710. SF_INFO sfinfo ;
  711. short *orig, *test ;
  712. int items, frames ;
  713. orig = orig_data.s ;
  714. test = test_data.s ;
  715. sfinfo.samplerate = 44100 ;
  716. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  717. sfinfo.channels = 2 ;
  718. sfinfo.format = format ;
  719. items = DATA_LENGTH ;
  720. frames = items / sfinfo.channels ;
  721. wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
  722. sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
  723. test_writef_short_or_die (wfile, 1, orig, frames, __LINE__) ;
  724. sf_write_sync (wfile) ;
  725. test_writef_short_or_die (wfile, 2, orig, frames, __LINE__) ;
  726. sf_write_sync (wfile) ;
  727. rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
  728. if (sfinfo.frames != 2 * frames)
  729. { printf ("\n\nLine %d : incorrect number of frames in file (%ld should be %d)\n\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 2 * frames) ;
  730. exit (1) ;
  731. } ;
  732. test_writef_short_or_die (wfile, 3, orig, frames, __LINE__) ;
  733. test_readf_short_or_die (rwfile, 1, test, frames, __LINE__) ;
  734. test_readf_short_or_die (rwfile, 2, test, frames, __LINE__) ;
  735. sf_close (wfile) ;
  736. sf_close (rwfile) ;
  737. } /* new_rdwr_char_test */
  738. /*======================================================================================
  739. */
  740. static void mono_short_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
  741. static void stereo_short_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
  742. static void mono_rdwr_short_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
  743. static void new_rdwr_short_test (const char *filename, int format, int allow_fd) ;
  744. static void multi_seek_test (const char * filename, int format) ;
  745. static void write_seek_extend_test (const char * filename, int format) ;
  746. static void
  747. pcm_test_short (const char *filename, int format, int long_file_ok)
  748. { SF_INFO sfinfo ;
  749. short *orig ;
  750. int k, allow_fd ;
  751. /* Sd2 files cannot be opened from an existing file descriptor. */
  752. allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
  753. print_test_name ("pcm_test_short", filename) ;
  754. sfinfo.samplerate = 44100 ;
  755. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  756. sfinfo.channels = 1 ;
  757. sfinfo.format = format ;
  758. gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 32000.0) ;
  759. orig = orig_data.s ;
  760. /* Make this a macro so gdb steps over it in one go. */
  761. CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
  762. /* Some test broken out here. */
  763. mono_short_test (filename, format, long_file_ok, allow_fd) ;
  764. /* Sub format DWVW does not allow seeking. */
  765. if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
  766. (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
  767. { unlink (filename) ;
  768. printf ("no seek : ok\n") ;
  769. return ;
  770. } ;
  771. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
  772. mono_rdwr_short_test (filename, format, long_file_ok, allow_fd) ;
  773. /* If the format doesn't support stereo we're done. */
  774. sfinfo.channels = 2 ;
  775. if (sf_format_check (&sfinfo) == 0)
  776. { unlink (filename) ;
  777. puts ("no stereo : ok") ;
  778. return ;
  779. } ;
  780. stereo_short_test (filename, format, long_file_ok, allow_fd) ;
  781. /* New read/write test. Not sure if this is needed yet. */
  782. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
  783. (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
  784. (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
  785. new_rdwr_short_test (filename, format, allow_fd) ;
  786. delete_file (format, filename) ;
  787. puts ("ok") ;
  788. return ;
  789. } /* pcm_test_short */
  790. static void
  791. mono_short_test (const char *filename, int format, int long_file_ok, int allow_fd)
  792. { SNDFILE *file ;
  793. SF_INFO sfinfo ;
  794. short *orig, *test ;
  795. sf_count_t count ;
  796. int k, items ;
  797. sfinfo.samplerate = 44100 ;
  798. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  799. sfinfo.channels = 1 ;
  800. sfinfo.format = format ;
  801. orig = orig_data.s ;
  802. test = test_data.s ;
  803. items = DATA_LENGTH ;
  804. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
  805. sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
  806. test_write_short_or_die (file, 0, orig, items, __LINE__) ;
  807. sf_write_sync (file) ;
  808. test_write_short_or_die (file, 0, orig, items, __LINE__) ;
  809. sf_write_sync (file) ;
  810. /* Add non-audio data after the audio. */
  811. sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
  812. sf_close (file) ;
  813. memset (test, 0, items * sizeof (short)) ;
  814. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
  815. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  816. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
  817. if (sfinfo.format != format)
  818. { printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
  819. exit (1) ;
  820. } ;
  821. if (sfinfo.frames < 2 * items)
  822. { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
  823. exit (1) ;
  824. } ;
  825. if (! long_file_ok && sfinfo.frames > 2 * items)
  826. { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
  827. exit (1) ;
  828. } ;
  829. if (sfinfo.channels != 1)
  830. { printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
  831. exit (1) ;
  832. } ;
  833. check_log_buffer_or_die (file, __LINE__) ;
  834. test_read_short_or_die (file, 0, test, items, __LINE__) ;
  835. for (k = 0 ; k < items ; k++)
  836. if (INT_ERROR (orig [k], test [k]))
  837. { printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  838. oct_save_short (orig, test, items) ;
  839. exit (1) ;
  840. } ;
  841. /* Seek to start of file. */
  842. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  843. test_read_short_or_die (file, 0, test, 4, __LINE__) ;
  844. for (k = 0 ; k < 4 ; k++)
  845. if (INT_ERROR (orig [k], test [k]))
  846. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  847. exit (1) ;
  848. } ;
  849. if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
  850. (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
  851. { sf_close (file) ;
  852. unlink (filename) ;
  853. printf ("no seek : ") ;
  854. return ;
  855. } ;
  856. /* Seek to offset from start of file. */
  857. test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
  858. test_read_short_or_die (file, 0, test + 10, 4, __LINE__) ;
  859. for (k = 10 ; k < 14 ; k++)
  860. if (INT_ERROR (orig [k], test [k]))
  861. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
  862. exit (1) ;
  863. } ;
  864. /* Seek to offset from current position. */
  865. test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
  866. test_read_short_or_die (file, 0, test + 20, 4, __LINE__) ;
  867. for (k = 20 ; k < 24 ; k++)
  868. if (INT_ERROR (orig [k], test [k]))
  869. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
  870. exit (1) ;
  871. } ;
  872. /* Seek to offset from end of file. */
  873. test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
  874. test_read_short_or_die (file, 0, test + 10, 4, __LINE__) ;
  875. for (k = 10 ; k < 14 ; k++)
  876. if (INT_ERROR (orig [k], test [k]))
  877. { printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
  878. exit (1) ;
  879. } ;
  880. /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
  881. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  882. count = 0 ;
  883. while (count < sfinfo.frames)
  884. count += sf_read_short (file, test, 311) ;
  885. /* Check that no error has occurred. */
  886. if (sf_error (file))
  887. { printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
  888. puts (sf_strerror (file)) ;
  889. exit (1) ;
  890. } ;
  891. /* Check that we haven't read beyond EOF. */
  892. if (count > sfinfo.frames)
  893. { printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
  894. exit (1) ;
  895. } ;
  896. test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
  897. sf_close (file) ;
  898. multi_seek_test (filename, format) ;
  899. write_seek_extend_test (filename, format) ;
  900. } /* mono_short_test */
  901. static void
  902. stereo_short_test (const char *filename, int format, int long_file_ok, int allow_fd)
  903. { SNDFILE *file ;
  904. SF_INFO sfinfo ;
  905. short *orig, *test ;
  906. int k, items, frames ;
  907. sfinfo.samplerate = 44100 ;
  908. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  909. sfinfo.channels = 2 ;
  910. sfinfo.format = format ;
  911. gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 32000.0) ;
  912. orig = orig_data.s ;
  913. test = test_data.s ;
  914. /* Make this a macro so gdb steps over it in one go. */
  915. CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
  916. items = DATA_LENGTH ;
  917. frames = items / sfinfo.channels ;
  918. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
  919. sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
  920. test_writef_short_or_die (file, 0, orig, frames, __LINE__) ;
  921. sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
  922. sf_close (file) ;
  923. memset (test, 0, items * sizeof (short)) ;
  924. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
  925. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  926. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
  927. if (sfinfo.format != format)
  928. { printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
  929. __LINE__, format, sfinfo.format) ;
  930. exit (1) ;
  931. } ;
  932. if (sfinfo.frames < frames)
  933. { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
  934. __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
  935. exit (1) ;
  936. } ;
  937. if (! long_file_ok && sfinfo.frames > frames)
  938. { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
  939. __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
  940. exit (1) ;
  941. } ;
  942. if (sfinfo.channels != 2)
  943. { printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
  944. exit (1) ;
  945. } ;
  946. check_log_buffer_or_die (file, __LINE__) ;
  947. test_readf_short_or_die (file, 0, test, frames, __LINE__) ;
  948. for (k = 0 ; k < items ; k++)
  949. if (INT_ERROR (test [k], orig [k]))
  950. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  951. exit (1) ;
  952. } ;
  953. /* Seek to start of file. */
  954. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  955. test_readf_short_or_die (file, 0, test, 2, __LINE__) ;
  956. for (k = 0 ; k < 4 ; k++)
  957. if (INT_ERROR (test [k], orig [k]))
  958. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  959. exit (1) ;
  960. } ;
  961. /* Seek to offset from start of file. */
  962. test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
  963. /* Check for errors here. */
  964. if (sf_error (file))
  965. { printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
  966. puts (sf_strerror (file)) ;
  967. exit (1) ;
  968. } ;
  969. if (sf_read_short (file, test, 1) > 0)
  970. { printf ("Line %d: Should return 0.\n", __LINE__) ;
  971. exit (1) ;
  972. } ;
  973. if (! sf_error (file))
  974. { printf ("Line %d: Should return an error.\n", __LINE__) ;
  975. exit (1) ;
  976. } ;
  977. /*-----------------------*/
  978. test_readf_short_or_die (file, 0, test + 10, 2, __LINE__) ;
  979. for (k = 20 ; k < 24 ; k++)
  980. if (INT_ERROR (test [k], orig [k]))
  981. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  982. exit (1) ;
  983. } ;
  984. /* Seek to offset from current position. */
  985. test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
  986. test_readf_short_or_die (file, 0, test + 20, 2, __LINE__) ;
  987. for (k = 40 ; k < 44 ; k++)
  988. if (INT_ERROR (test [k], orig [k]))
  989. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  990. exit (1) ;
  991. } ;
  992. /* Seek to offset from end of file. */
  993. test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
  994. test_readf_short_or_die (file, 0, test + 20, 2, __LINE__) ;
  995. for (k = 20 ; k < 24 ; k++)
  996. if (INT_ERROR (test [k], orig [k]))
  997. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  998. exit (1) ;
  999. } ;
  1000. sf_close (file) ;
  1001. } /* stereo_short_test */
  1002. static void
  1003. mono_rdwr_short_test (const char *filename, int format, int long_file_ok, int allow_fd)
  1004. { SNDFILE *file ;
  1005. SF_INFO sfinfo ;
  1006. short *orig, *test ;
  1007. int k, pass ;
  1008. orig = orig_data.s ;
  1009. test = test_data.s ;
  1010. sfinfo.samplerate = SAMPLE_RATE ;
  1011. sfinfo.frames = DATA_LENGTH ;
  1012. sfinfo.channels = 1 ;
  1013. sfinfo.format = format ;
  1014. if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
  1015. || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
  1016. || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
  1017. unlink (filename) ;
  1018. else
  1019. { /* Create a short file. */
  1020. create_short_file (filename) ;
  1021. /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
  1022. ** If this returns a valif pointer sf_open() screwed up.
  1023. */
  1024. if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
  1025. { printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
  1026. exit (1) ;
  1027. } ;
  1028. /* Truncate the file to zero bytes. */
  1029. if (truncate (filename, 0) < 0)
  1030. { printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
  1031. perror (NULL) ;
  1032. exit (1) ;
  1033. } ;
  1034. } ;
  1035. /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
  1036. ** all the usual data required when opening the file in WRITE mode.
  1037. */
  1038. sfinfo.samplerate = SAMPLE_RATE ;
  1039. sfinfo.frames = DATA_LENGTH ;
  1040. sfinfo.channels = 1 ;
  1041. sfinfo.format = format ;
  1042. file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
  1043. /* Do 3 writes followed by reads. After each, check the data and the current
  1044. ** read and write offsets.
  1045. */
  1046. for (pass = 1 ; pass <= 3 ; pass ++)
  1047. { orig [20] = pass * 2 ;
  1048. /* Write some data. */
  1049. test_write_short_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
  1050. test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
  1051. /* Read what we just wrote. */
  1052. test_read_short_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
  1053. /* Check the data. */
  1054. for (k = 0 ; k < DATA_LENGTH ; k++)
  1055. if (INT_ERROR (orig [k], test [k]))
  1056. { printf ("\n\nLine %d (pass %d) A : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
  1057. oct_save_short (orig, test, DATA_LENGTH) ;
  1058. exit (1) ;
  1059. } ;
  1060. test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
  1061. } ; /* for (pass ...) */
  1062. sf_close (file) ;
  1063. /* Open the file again to check the data. */
  1064. file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
  1065. if (sfinfo.format != format)
  1066. { printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
  1067. exit (1) ;
  1068. } ;
  1069. if (sfinfo.frames < 3 * DATA_LENGTH)
  1070. { printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
  1071. exit (1) ;
  1072. }
  1073. if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
  1074. { printf ("\n\nLine %d : Incorrect number of frames in file. (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
  1075. exit (1) ;
  1076. } ;
  1077. if (sfinfo.channels != 1)
  1078. { printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
  1079. exit (1) ;
  1080. } ;
  1081. if (! long_file_ok)
  1082. test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
  1083. else
  1084. test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
  1085. for (pass = 1 ; pass <= 3 ; pass ++)
  1086. { orig [20] = pass * 2 ;
  1087. test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
  1088. /* Read what we just wrote. */
  1089. test_read_short_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
  1090. /* Check the data. */
  1091. for (k = 0 ; k < DATA_LENGTH ; k++)
  1092. if (INT_ERROR (orig [k], test [k]))
  1093. { printf ("\n\nLine %d (pass %d) B : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
  1094. oct_save_short (orig, test, DATA_LENGTH) ;
  1095. exit (1) ;
  1096. } ;
  1097. } ; /* for (pass ...) */
  1098. sf_close (file) ;
  1099. } /* mono_rdwr_short_test */
  1100. static void
  1101. new_rdwr_short_test (const char *filename, int format, int allow_fd)
  1102. { SNDFILE *wfile, *rwfile ;
  1103. SF_INFO sfinfo ;
  1104. short *orig, *test ;
  1105. int items, frames ;
  1106. orig = orig_data.s ;
  1107. test = test_data.s ;
  1108. sfinfo.samplerate = 44100 ;
  1109. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  1110. sfinfo.channels = 2 ;
  1111. sfinfo.format = format ;
  1112. items = DATA_LENGTH ;
  1113. frames = items / sfinfo.channels ;
  1114. wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
  1115. sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
  1116. test_writef_short_or_die (wfile, 1, orig, frames, __LINE__) ;
  1117. sf_write_sync (wfile) ;
  1118. test_writef_short_or_die (wfile, 2, orig, frames, __LINE__) ;
  1119. sf_write_sync (wfile) ;
  1120. rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
  1121. if (sfinfo.frames != 2 * frames)
  1122. { printf ("\n\nLine %d : incorrect number of frames in file (%ld should be %d)\n\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 2 * frames) ;
  1123. exit (1) ;
  1124. } ;
  1125. test_writef_short_or_die (wfile, 3, orig, frames, __LINE__) ;
  1126. test_readf_short_or_die (rwfile, 1, test, frames, __LINE__) ;
  1127. test_readf_short_or_die (rwfile, 2, test, frames, __LINE__) ;
  1128. sf_close (wfile) ;
  1129. sf_close (rwfile) ;
  1130. } /* new_rdwr_short_test */
  1131. /*======================================================================================
  1132. */
  1133. static void mono_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
  1134. static void stereo_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
  1135. static void mono_rdwr_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
  1136. static void new_rdwr_24bit_test (const char *filename, int format, int allow_fd) ;
  1137. static void multi_seek_test (const char * filename, int format) ;
  1138. static void write_seek_extend_test (const char * filename, int format) ;
  1139. static void
  1140. pcm_test_24bit (const char *filename, int format, int long_file_ok)
  1141. { SF_INFO sfinfo ;
  1142. int *orig ;
  1143. int k, allow_fd ;
  1144. /* Sd2 files cannot be opened from an existing file descriptor. */
  1145. allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
  1146. print_test_name ("pcm_test_24bit", filename) ;
  1147. sfinfo.samplerate = 44100 ;
  1148. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  1149. sfinfo.channels = 1 ;
  1150. sfinfo.format = format ;
  1151. gen_windowed_sine_double (orig_data.d, DATA_LENGTH, (1.0 * 0x7F000000)) ;
  1152. orig = orig_data.i ;
  1153. /* Make this a macro so gdb steps over it in one go. */
  1154. CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
  1155. /* Some test broken out here. */
  1156. mono_24bit_test (filename, format, long_file_ok, allow_fd) ;
  1157. /* Sub format DWVW does not allow seeking. */
  1158. if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
  1159. (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
  1160. { unlink (filename) ;
  1161. printf ("no seek : ok\n") ;
  1162. return ;
  1163. } ;
  1164. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
  1165. mono_rdwr_24bit_test (filename, format, long_file_ok, allow_fd) ;
  1166. /* If the format doesn't support stereo we're done. */
  1167. sfinfo.channels = 2 ;
  1168. if (sf_format_check (&sfinfo) == 0)
  1169. { unlink (filename) ;
  1170. puts ("no stereo : ok") ;
  1171. return ;
  1172. } ;
  1173. stereo_24bit_test (filename, format, long_file_ok, allow_fd) ;
  1174. /* New read/write test. Not sure if this is needed yet. */
  1175. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
  1176. (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
  1177. (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
  1178. new_rdwr_24bit_test (filename, format, allow_fd) ;
  1179. delete_file (format, filename) ;
  1180. puts ("ok") ;
  1181. return ;
  1182. } /* pcm_test_24bit */
  1183. static void
  1184. mono_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd)
  1185. { SNDFILE *file ;
  1186. SF_INFO sfinfo ;
  1187. int *orig, *test ;
  1188. sf_count_t count ;
  1189. int k, items ;
  1190. sfinfo.samplerate = 44100 ;
  1191. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  1192. sfinfo.channels = 1 ;
  1193. sfinfo.format = format ;
  1194. orig = orig_data.i ;
  1195. test = test_data.i ;
  1196. items = DATA_LENGTH ;
  1197. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
  1198. sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
  1199. test_write_int_or_die (file, 0, orig, items, __LINE__) ;
  1200. sf_write_sync (file) ;
  1201. test_write_int_or_die (file, 0, orig, items, __LINE__) ;
  1202. sf_write_sync (file) ;
  1203. /* Add non-audio data after the audio. */
  1204. sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
  1205. sf_close (file) ;
  1206. memset (test, 0, items * sizeof (int)) ;
  1207. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
  1208. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  1209. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
  1210. if (sfinfo.format != format)
  1211. { printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
  1212. exit (1) ;
  1213. } ;
  1214. if (sfinfo.frames < 2 * items)
  1215. { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
  1216. exit (1) ;
  1217. } ;
  1218. if (! long_file_ok && sfinfo.frames > 2 * items)
  1219. { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
  1220. exit (1) ;
  1221. } ;
  1222. if (sfinfo.channels != 1)
  1223. { printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
  1224. exit (1) ;
  1225. } ;
  1226. check_log_buffer_or_die (file, __LINE__) ;
  1227. test_read_int_or_die (file, 0, test, items, __LINE__) ;
  1228. for (k = 0 ; k < items ; k++)
  1229. if (TRIBYTE_ERROR (orig [k], test [k]))
  1230. { printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  1231. oct_save_int (orig, test, items) ;
  1232. exit (1) ;
  1233. } ;
  1234. /* Seek to start of file. */
  1235. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  1236. test_read_int_or_die (file, 0, test, 4, __LINE__) ;
  1237. for (k = 0 ; k < 4 ; k++)
  1238. if (TRIBYTE_ERROR (orig [k], test [k]))
  1239. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  1240. exit (1) ;
  1241. } ;
  1242. if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
  1243. (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
  1244. { sf_close (file) ;
  1245. unlink (filename) ;
  1246. printf ("no seek : ") ;
  1247. return ;
  1248. } ;
  1249. /* Seek to offset from start of file. */
  1250. test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
  1251. test_read_int_or_die (file, 0, test + 10, 4, __LINE__) ;
  1252. for (k = 10 ; k < 14 ; k++)
  1253. if (TRIBYTE_ERROR (orig [k], test [k]))
  1254. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
  1255. exit (1) ;
  1256. } ;
  1257. /* Seek to offset from current position. */
  1258. test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
  1259. test_read_int_or_die (file, 0, test + 20, 4, __LINE__) ;
  1260. for (k = 20 ; k < 24 ; k++)
  1261. if (TRIBYTE_ERROR (orig [k], test [k]))
  1262. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
  1263. exit (1) ;
  1264. } ;
  1265. /* Seek to offset from end of file. */
  1266. test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
  1267. test_read_int_or_die (file, 0, test + 10, 4, __LINE__) ;
  1268. for (k = 10 ; k < 14 ; k++)
  1269. if (TRIBYTE_ERROR (orig [k], test [k]))
  1270. { printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
  1271. exit (1) ;
  1272. } ;
  1273. /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
  1274. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  1275. count = 0 ;
  1276. while (count < sfinfo.frames)
  1277. count += sf_read_int (file, test, 311) ;
  1278. /* Check that no error has occurred. */
  1279. if (sf_error (file))
  1280. { printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
  1281. puts (sf_strerror (file)) ;
  1282. exit (1) ;
  1283. } ;
  1284. /* Check that we haven't read beyond EOF. */
  1285. if (count > sfinfo.frames)
  1286. { printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
  1287. exit (1) ;
  1288. } ;
  1289. test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
  1290. sf_close (file) ;
  1291. multi_seek_test (filename, format) ;
  1292. write_seek_extend_test (filename, format) ;
  1293. } /* mono_24bit_test */
  1294. static void
  1295. stereo_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd)
  1296. { SNDFILE *file ;
  1297. SF_INFO sfinfo ;
  1298. int *orig, *test ;
  1299. int k, items, frames ;
  1300. sfinfo.samplerate = 44100 ;
  1301. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  1302. sfinfo.channels = 2 ;
  1303. sfinfo.format = format ;
  1304. gen_windowed_sine_double (orig_data.d, DATA_LENGTH, (1.0 * 0x7F000000)) ;
  1305. orig = orig_data.i ;
  1306. test = test_data.i ;
  1307. /* Make this a macro so gdb steps over it in one go. */
  1308. CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
  1309. items = DATA_LENGTH ;
  1310. frames = items / sfinfo.channels ;
  1311. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
  1312. sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
  1313. test_writef_int_or_die (file, 0, orig, frames, __LINE__) ;
  1314. sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
  1315. sf_close (file) ;
  1316. memset (test, 0, items * sizeof (int)) ;
  1317. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
  1318. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  1319. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
  1320. if (sfinfo.format != format)
  1321. { printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
  1322. __LINE__, format, sfinfo.format) ;
  1323. exit (1) ;
  1324. } ;
  1325. if (sfinfo.frames < frames)
  1326. { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
  1327. __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
  1328. exit (1) ;
  1329. } ;
  1330. if (! long_file_ok && sfinfo.frames > frames)
  1331. { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
  1332. __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
  1333. exit (1) ;
  1334. } ;
  1335. if (sfinfo.channels != 2)
  1336. { printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
  1337. exit (1) ;
  1338. } ;
  1339. check_log_buffer_or_die (file, __LINE__) ;
  1340. test_readf_int_or_die (file, 0, test, frames, __LINE__) ;
  1341. for (k = 0 ; k < items ; k++)
  1342. if (TRIBYTE_ERROR (test [k], orig [k]))
  1343. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  1344. exit (1) ;
  1345. } ;
  1346. /* Seek to start of file. */
  1347. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  1348. test_readf_int_or_die (file, 0, test, 2, __LINE__) ;
  1349. for (k = 0 ; k < 4 ; k++)
  1350. if (TRIBYTE_ERROR (test [k], orig [k]))
  1351. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  1352. exit (1) ;
  1353. } ;
  1354. /* Seek to offset from start of file. */
  1355. test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
  1356. /* Check for errors here. */
  1357. if (sf_error (file))
  1358. { printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
  1359. puts (sf_strerror (file)) ;
  1360. exit (1) ;
  1361. } ;
  1362. if (sf_read_int (file, test, 1) > 0)
  1363. { printf ("Line %d: Should return 0.\n", __LINE__) ;
  1364. exit (1) ;
  1365. } ;
  1366. if (! sf_error (file))
  1367. { printf ("Line %d: Should return an error.\n", __LINE__) ;
  1368. exit (1) ;
  1369. } ;
  1370. /*-----------------------*/
  1371. test_readf_int_or_die (file, 0, test + 10, 2, __LINE__) ;
  1372. for (k = 20 ; k < 24 ; k++)
  1373. if (TRIBYTE_ERROR (test [k], orig [k]))
  1374. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  1375. exit (1) ;
  1376. } ;
  1377. /* Seek to offset from current position. */
  1378. test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
  1379. test_readf_int_or_die (file, 0, test + 20, 2, __LINE__) ;
  1380. for (k = 40 ; k < 44 ; k++)
  1381. if (TRIBYTE_ERROR (test [k], orig [k]))
  1382. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  1383. exit (1) ;
  1384. } ;
  1385. /* Seek to offset from end of file. */
  1386. test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
  1387. test_readf_int_or_die (file, 0, test + 20, 2, __LINE__) ;
  1388. for (k = 20 ; k < 24 ; k++)
  1389. if (TRIBYTE_ERROR (test [k], orig [k]))
  1390. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  1391. exit (1) ;
  1392. } ;
  1393. sf_close (file) ;
  1394. } /* stereo_24bit_test */
  1395. static void
  1396. mono_rdwr_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd)
  1397. { SNDFILE *file ;
  1398. SF_INFO sfinfo ;
  1399. int *orig, *test ;
  1400. int k, pass ;
  1401. orig = orig_data.i ;
  1402. test = test_data.i ;
  1403. sfinfo.samplerate = SAMPLE_RATE ;
  1404. sfinfo.frames = DATA_LENGTH ;
  1405. sfinfo.channels = 1 ;
  1406. sfinfo.format = format ;
  1407. if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
  1408. || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
  1409. || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
  1410. unlink (filename) ;
  1411. else
  1412. { /* Create a short file. */
  1413. create_short_file (filename) ;
  1414. /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
  1415. ** If this returns a valif pointer sf_open() screwed up.
  1416. */
  1417. if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
  1418. { printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
  1419. exit (1) ;
  1420. } ;
  1421. /* Truncate the file to zero bytes. */
  1422. if (truncate (filename, 0) < 0)
  1423. { printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
  1424. perror (NULL) ;
  1425. exit (1) ;
  1426. } ;
  1427. } ;
  1428. /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
  1429. ** all the usual data required when opening the file in WRITE mode.
  1430. */
  1431. sfinfo.samplerate = SAMPLE_RATE ;
  1432. sfinfo.frames = DATA_LENGTH ;
  1433. sfinfo.channels = 1 ;
  1434. sfinfo.format = format ;
  1435. file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
  1436. /* Do 3 writes followed by reads. After each, check the data and the current
  1437. ** read and write offsets.
  1438. */
  1439. for (pass = 1 ; pass <= 3 ; pass ++)
  1440. { orig [20] = pass * 2 ;
  1441. /* Write some data. */
  1442. test_write_int_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
  1443. test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
  1444. /* Read what we just wrote. */
  1445. test_read_int_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
  1446. /* Check the data. */
  1447. for (k = 0 ; k < DATA_LENGTH ; k++)
  1448. if (TRIBYTE_ERROR (orig [k], test [k]))
  1449. { printf ("\n\nLine %d (pass %d) A : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
  1450. oct_save_int (orig, test, DATA_LENGTH) ;
  1451. exit (1) ;
  1452. } ;
  1453. test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
  1454. } ; /* for (pass ...) */
  1455. sf_close (file) ;
  1456. /* Open the file again to check the data. */
  1457. file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
  1458. if (sfinfo.format != format)
  1459. { printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
  1460. exit (1) ;
  1461. } ;
  1462. if (sfinfo.frames < 3 * DATA_LENGTH)
  1463. { printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
  1464. exit (1) ;
  1465. }
  1466. if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
  1467. { printf ("\n\nLine %d : Incorrect number of frames in file. (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
  1468. exit (1) ;
  1469. } ;
  1470. if (sfinfo.channels != 1)
  1471. { printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
  1472. exit (1) ;
  1473. } ;
  1474. if (! long_file_ok)
  1475. test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
  1476. else
  1477. test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
  1478. for (pass = 1 ; pass <= 3 ; pass ++)
  1479. { orig [20] = pass * 2 ;
  1480. test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
  1481. /* Read what we just wrote. */
  1482. test_read_int_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
  1483. /* Check the data. */
  1484. for (k = 0 ; k < DATA_LENGTH ; k++)
  1485. if (TRIBYTE_ERROR (orig [k], test [k]))
  1486. { printf ("\n\nLine %d (pass %d) B : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
  1487. oct_save_int (orig, test, DATA_LENGTH) ;
  1488. exit (1) ;
  1489. } ;
  1490. } ; /* for (pass ...) */
  1491. sf_close (file) ;
  1492. } /* mono_rdwr_int_test */
  1493. static void
  1494. new_rdwr_24bit_test (const char *filename, int format, int allow_fd)
  1495. { SNDFILE *wfile, *rwfile ;
  1496. SF_INFO sfinfo ;
  1497. int *orig, *test ;
  1498. int items, frames ;
  1499. orig = orig_data.i ;
  1500. test = test_data.i ;
  1501. sfinfo.samplerate = 44100 ;
  1502. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  1503. sfinfo.channels = 2 ;
  1504. sfinfo.format = format ;
  1505. items = DATA_LENGTH ;
  1506. frames = items / sfinfo.channels ;
  1507. wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
  1508. sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
  1509. test_writef_int_or_die (wfile, 1, orig, frames, __LINE__) ;
  1510. sf_write_sync (wfile) ;
  1511. test_writef_int_or_die (wfile, 2, orig, frames, __LINE__) ;
  1512. sf_write_sync (wfile) ;
  1513. rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
  1514. if (sfinfo.frames != 2 * frames)
  1515. { printf ("\n\nLine %d : incorrect number of frames in file (%ld should be %d)\n\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 2 * frames) ;
  1516. exit (1) ;
  1517. } ;
  1518. test_writef_int_or_die (wfile, 3, orig, frames, __LINE__) ;
  1519. test_readf_int_or_die (rwfile, 1, test, frames, __LINE__) ;
  1520. test_readf_int_or_die (rwfile, 2, test, frames, __LINE__) ;
  1521. sf_close (wfile) ;
  1522. sf_close (rwfile) ;
  1523. } /* new_rdwr_24bit_test */
  1524. /*======================================================================================
  1525. */
  1526. static void mono_int_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
  1527. static void stereo_int_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
  1528. static void mono_rdwr_int_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
  1529. static void new_rdwr_int_test (const char *filename, int format, int allow_fd) ;
  1530. static void multi_seek_test (const char * filename, int format) ;
  1531. static void write_seek_extend_test (const char * filename, int format) ;
  1532. static void
  1533. pcm_test_int (const char *filename, int format, int long_file_ok)
  1534. { SF_INFO sfinfo ;
  1535. int *orig ;
  1536. int k, allow_fd ;
  1537. /* Sd2 files cannot be opened from an existing file descriptor. */
  1538. allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
  1539. print_test_name ("pcm_test_int", filename) ;
  1540. sfinfo.samplerate = 44100 ;
  1541. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  1542. sfinfo.channels = 1 ;
  1543. sfinfo.format = format ;
  1544. gen_windowed_sine_double (orig_data.d, DATA_LENGTH, (1.0 * 0x7F000000)) ;
  1545. orig = orig_data.i ;
  1546. /* Make this a macro so gdb steps over it in one go. */
  1547. CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
  1548. /* Some test broken out here. */
  1549. mono_int_test (filename, format, long_file_ok, allow_fd) ;
  1550. /* Sub format DWVW does not allow seeking. */
  1551. if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
  1552. (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
  1553. { unlink (filename) ;
  1554. printf ("no seek : ok\n") ;
  1555. return ;
  1556. } ;
  1557. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
  1558. mono_rdwr_int_test (filename, format, long_file_ok, allow_fd) ;
  1559. /* If the format doesn't support stereo we're done. */
  1560. sfinfo.channels = 2 ;
  1561. if (sf_format_check (&sfinfo) == 0)
  1562. { unlink (filename) ;
  1563. puts ("no stereo : ok") ;
  1564. return ;
  1565. } ;
  1566. stereo_int_test (filename, format, long_file_ok, allow_fd) ;
  1567. /* New read/write test. Not sure if this is needed yet. */
  1568. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
  1569. (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
  1570. (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
  1571. new_rdwr_int_test (filename, format, allow_fd) ;
  1572. delete_file (format, filename) ;
  1573. puts ("ok") ;
  1574. return ;
  1575. } /* pcm_test_int */
  1576. static void
  1577. mono_int_test (const char *filename, int format, int long_file_ok, int allow_fd)
  1578. { SNDFILE *file ;
  1579. SF_INFO sfinfo ;
  1580. int *orig, *test ;
  1581. sf_count_t count ;
  1582. int k, items ;
  1583. sfinfo.samplerate = 44100 ;
  1584. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  1585. sfinfo.channels = 1 ;
  1586. sfinfo.format = format ;
  1587. orig = orig_data.i ;
  1588. test = test_data.i ;
  1589. items = DATA_LENGTH ;
  1590. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
  1591. sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
  1592. test_write_int_or_die (file, 0, orig, items, __LINE__) ;
  1593. sf_write_sync (file) ;
  1594. test_write_int_or_die (file, 0, orig, items, __LINE__) ;
  1595. sf_write_sync (file) ;
  1596. /* Add non-audio data after the audio. */
  1597. sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
  1598. sf_close (file) ;
  1599. memset (test, 0, items * sizeof (int)) ;
  1600. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
  1601. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  1602. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
  1603. if (sfinfo.format != format)
  1604. { printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
  1605. exit (1) ;
  1606. } ;
  1607. if (sfinfo.frames < 2 * items)
  1608. { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
  1609. exit (1) ;
  1610. } ;
  1611. if (! long_file_ok && sfinfo.frames > 2 * items)
  1612. { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
  1613. exit (1) ;
  1614. } ;
  1615. if (sfinfo.channels != 1)
  1616. { printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
  1617. exit (1) ;
  1618. } ;
  1619. check_log_buffer_or_die (file, __LINE__) ;
  1620. test_read_int_or_die (file, 0, test, items, __LINE__) ;
  1621. for (k = 0 ; k < items ; k++)
  1622. if (INT_ERROR (orig [k], test [k]))
  1623. { printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  1624. oct_save_int (orig, test, items) ;
  1625. exit (1) ;
  1626. } ;
  1627. /* Seek to start of file. */
  1628. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  1629. test_read_int_or_die (file, 0, test, 4, __LINE__) ;
  1630. for (k = 0 ; k < 4 ; k++)
  1631. if (INT_ERROR (orig [k], test [k]))
  1632. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  1633. exit (1) ;
  1634. } ;
  1635. if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
  1636. (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
  1637. { sf_close (file) ;
  1638. unlink (filename) ;
  1639. printf ("no seek : ") ;
  1640. return ;
  1641. } ;
  1642. /* Seek to offset from start of file. */
  1643. test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
  1644. test_read_int_or_die (file, 0, test + 10, 4, __LINE__) ;
  1645. for (k = 10 ; k < 14 ; k++)
  1646. if (INT_ERROR (orig [k], test [k]))
  1647. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
  1648. exit (1) ;
  1649. } ;
  1650. /* Seek to offset from current position. */
  1651. test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
  1652. test_read_int_or_die (file, 0, test + 20, 4, __LINE__) ;
  1653. for (k = 20 ; k < 24 ; k++)
  1654. if (INT_ERROR (orig [k], test [k]))
  1655. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
  1656. exit (1) ;
  1657. } ;
  1658. /* Seek to offset from end of file. */
  1659. test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
  1660. test_read_int_or_die (file, 0, test + 10, 4, __LINE__) ;
  1661. for (k = 10 ; k < 14 ; k++)
  1662. if (INT_ERROR (orig [k], test [k]))
  1663. { printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
  1664. exit (1) ;
  1665. } ;
  1666. /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
  1667. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  1668. count = 0 ;
  1669. while (count < sfinfo.frames)
  1670. count += sf_read_int (file, test, 311) ;
  1671. /* Check that no error has occurred. */
  1672. if (sf_error (file))
  1673. { printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
  1674. puts (sf_strerror (file)) ;
  1675. exit (1) ;
  1676. } ;
  1677. /* Check that we haven't read beyond EOF. */
  1678. if (count > sfinfo.frames)
  1679. { printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
  1680. exit (1) ;
  1681. } ;
  1682. test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
  1683. sf_close (file) ;
  1684. multi_seek_test (filename, format) ;
  1685. write_seek_extend_test (filename, format) ;
  1686. } /* mono_int_test */
  1687. static void
  1688. stereo_int_test (const char *filename, int format, int long_file_ok, int allow_fd)
  1689. { SNDFILE *file ;
  1690. SF_INFO sfinfo ;
  1691. int *orig, *test ;
  1692. int k, items, frames ;
  1693. sfinfo.samplerate = 44100 ;
  1694. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  1695. sfinfo.channels = 2 ;
  1696. sfinfo.format = format ;
  1697. gen_windowed_sine_double (orig_data.d, DATA_LENGTH, (1.0 * 0x7F000000)) ;
  1698. orig = orig_data.i ;
  1699. test = test_data.i ;
  1700. /* Make this a macro so gdb steps over it in one go. */
  1701. CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
  1702. items = DATA_LENGTH ;
  1703. frames = items / sfinfo.channels ;
  1704. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
  1705. sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
  1706. test_writef_int_or_die (file, 0, orig, frames, __LINE__) ;
  1707. sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
  1708. sf_close (file) ;
  1709. memset (test, 0, items * sizeof (int)) ;
  1710. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
  1711. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  1712. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
  1713. if (sfinfo.format != format)
  1714. { printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
  1715. __LINE__, format, sfinfo.format) ;
  1716. exit (1) ;
  1717. } ;
  1718. if (sfinfo.frames < frames)
  1719. { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
  1720. __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
  1721. exit (1) ;
  1722. } ;
  1723. if (! long_file_ok && sfinfo.frames > frames)
  1724. { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
  1725. __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
  1726. exit (1) ;
  1727. } ;
  1728. if (sfinfo.channels != 2)
  1729. { printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
  1730. exit (1) ;
  1731. } ;
  1732. check_log_buffer_or_die (file, __LINE__) ;
  1733. test_readf_int_or_die (file, 0, test, frames, __LINE__) ;
  1734. for (k = 0 ; k < items ; k++)
  1735. if (INT_ERROR (test [k], orig [k]))
  1736. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  1737. exit (1) ;
  1738. } ;
  1739. /* Seek to start of file. */
  1740. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  1741. test_readf_int_or_die (file, 0, test, 2, __LINE__) ;
  1742. for (k = 0 ; k < 4 ; k++)
  1743. if (INT_ERROR (test [k], orig [k]))
  1744. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  1745. exit (1) ;
  1746. } ;
  1747. /* Seek to offset from start of file. */
  1748. test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
  1749. /* Check for errors here. */
  1750. if (sf_error (file))
  1751. { printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
  1752. puts (sf_strerror (file)) ;
  1753. exit (1) ;
  1754. } ;
  1755. if (sf_read_int (file, test, 1) > 0)
  1756. { printf ("Line %d: Should return 0.\n", __LINE__) ;
  1757. exit (1) ;
  1758. } ;
  1759. if (! sf_error (file))
  1760. { printf ("Line %d: Should return an error.\n", __LINE__) ;
  1761. exit (1) ;
  1762. } ;
  1763. /*-----------------------*/
  1764. test_readf_int_or_die (file, 0, test + 10, 2, __LINE__) ;
  1765. for (k = 20 ; k < 24 ; k++)
  1766. if (INT_ERROR (test [k], orig [k]))
  1767. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  1768. exit (1) ;
  1769. } ;
  1770. /* Seek to offset from current position. */
  1771. test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
  1772. test_readf_int_or_die (file, 0, test + 20, 2, __LINE__) ;
  1773. for (k = 40 ; k < 44 ; k++)
  1774. if (INT_ERROR (test [k], orig [k]))
  1775. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  1776. exit (1) ;
  1777. } ;
  1778. /* Seek to offset from end of file. */
  1779. test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
  1780. test_readf_int_or_die (file, 0, test + 20, 2, __LINE__) ;
  1781. for (k = 20 ; k < 24 ; k++)
  1782. if (INT_ERROR (test [k], orig [k]))
  1783. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
  1784. exit (1) ;
  1785. } ;
  1786. sf_close (file) ;
  1787. } /* stereo_int_test */
  1788. static void
  1789. mono_rdwr_int_test (const char *filename, int format, int long_file_ok, int allow_fd)
  1790. { SNDFILE *file ;
  1791. SF_INFO sfinfo ;
  1792. int *orig, *test ;
  1793. int k, pass ;
  1794. orig = orig_data.i ;
  1795. test = test_data.i ;
  1796. sfinfo.samplerate = SAMPLE_RATE ;
  1797. sfinfo.frames = DATA_LENGTH ;
  1798. sfinfo.channels = 1 ;
  1799. sfinfo.format = format ;
  1800. if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
  1801. || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
  1802. || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
  1803. unlink (filename) ;
  1804. else
  1805. { /* Create a short file. */
  1806. create_short_file (filename) ;
  1807. /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
  1808. ** If this returns a valif pointer sf_open() screwed up.
  1809. */
  1810. if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
  1811. { printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
  1812. exit (1) ;
  1813. } ;
  1814. /* Truncate the file to zero bytes. */
  1815. if (truncate (filename, 0) < 0)
  1816. { printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
  1817. perror (NULL) ;
  1818. exit (1) ;
  1819. } ;
  1820. } ;
  1821. /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
  1822. ** all the usual data required when opening the file in WRITE mode.
  1823. */
  1824. sfinfo.samplerate = SAMPLE_RATE ;
  1825. sfinfo.frames = DATA_LENGTH ;
  1826. sfinfo.channels = 1 ;
  1827. sfinfo.format = format ;
  1828. file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
  1829. /* Do 3 writes followed by reads. After each, check the data and the current
  1830. ** read and write offsets.
  1831. */
  1832. for (pass = 1 ; pass <= 3 ; pass ++)
  1833. { orig [20] = pass * 2 ;
  1834. /* Write some data. */
  1835. test_write_int_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
  1836. test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
  1837. /* Read what we just wrote. */
  1838. test_read_int_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
  1839. /* Check the data. */
  1840. for (k = 0 ; k < DATA_LENGTH ; k++)
  1841. if (INT_ERROR (orig [k], test [k]))
  1842. { printf ("\n\nLine %d (pass %d) A : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
  1843. oct_save_int (orig, test, DATA_LENGTH) ;
  1844. exit (1) ;
  1845. } ;
  1846. test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
  1847. } ; /* for (pass ...) */
  1848. sf_close (file) ;
  1849. /* Open the file again to check the data. */
  1850. file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
  1851. if (sfinfo.format != format)
  1852. { printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
  1853. exit (1) ;
  1854. } ;
  1855. if (sfinfo.frames < 3 * DATA_LENGTH)
  1856. { printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
  1857. exit (1) ;
  1858. }
  1859. if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
  1860. { printf ("\n\nLine %d : Incorrect number of frames in file. (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
  1861. exit (1) ;
  1862. } ;
  1863. if (sfinfo.channels != 1)
  1864. { printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
  1865. exit (1) ;
  1866. } ;
  1867. if (! long_file_ok)
  1868. test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
  1869. else
  1870. test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
  1871. for (pass = 1 ; pass <= 3 ; pass ++)
  1872. { orig [20] = pass * 2 ;
  1873. test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
  1874. /* Read what we just wrote. */
  1875. test_read_int_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
  1876. /* Check the data. */
  1877. for (k = 0 ; k < DATA_LENGTH ; k++)
  1878. if (INT_ERROR (orig [k], test [k]))
  1879. { printf ("\n\nLine %d (pass %d) B : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
  1880. oct_save_int (orig, test, DATA_LENGTH) ;
  1881. exit (1) ;
  1882. } ;
  1883. } ; /* for (pass ...) */
  1884. sf_close (file) ;
  1885. } /* mono_rdwr_int_test */
  1886. static void
  1887. new_rdwr_int_test (const char *filename, int format, int allow_fd)
  1888. { SNDFILE *wfile, *rwfile ;
  1889. SF_INFO sfinfo ;
  1890. int *orig, *test ;
  1891. int items, frames ;
  1892. orig = orig_data.i ;
  1893. test = test_data.i ;
  1894. sfinfo.samplerate = 44100 ;
  1895. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  1896. sfinfo.channels = 2 ;
  1897. sfinfo.format = format ;
  1898. items = DATA_LENGTH ;
  1899. frames = items / sfinfo.channels ;
  1900. wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
  1901. sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
  1902. test_writef_int_or_die (wfile, 1, orig, frames, __LINE__) ;
  1903. sf_write_sync (wfile) ;
  1904. test_writef_int_or_die (wfile, 2, orig, frames, __LINE__) ;
  1905. sf_write_sync (wfile) ;
  1906. rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
  1907. if (sfinfo.frames != 2 * frames)
  1908. { printf ("\n\nLine %d : incorrect number of frames in file (%ld should be %d)\n\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 2 * frames) ;
  1909. exit (1) ;
  1910. } ;
  1911. test_writef_int_or_die (wfile, 3, orig, frames, __LINE__) ;
  1912. test_readf_int_or_die (rwfile, 1, test, frames, __LINE__) ;
  1913. test_readf_int_or_die (rwfile, 2, test, frames, __LINE__) ;
  1914. sf_close (wfile) ;
  1915. sf_close (rwfile) ;
  1916. } /* new_rdwr_int_test */
  1917. /*======================================================================================
  1918. */
  1919. static void mono_float_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
  1920. static void stereo_float_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
  1921. static void mono_rdwr_float_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
  1922. static void new_rdwr_float_test (const char *filename, int format, int allow_fd) ;
  1923. static void multi_seek_test (const char * filename, int format) ;
  1924. static void write_seek_extend_test (const char * filename, int format) ;
  1925. static void
  1926. pcm_test_float (const char *filename, int format, int long_file_ok)
  1927. { SF_INFO sfinfo ;
  1928. float *orig ;
  1929. int k, allow_fd ;
  1930. /* Sd2 files cannot be opened from an existing file descriptor. */
  1931. allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
  1932. print_test_name ("pcm_test_float", filename) ;
  1933. sfinfo.samplerate = 44100 ;
  1934. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  1935. sfinfo.channels = 1 ;
  1936. sfinfo.format = format ;
  1937. gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 1.0) ;
  1938. orig = orig_data.f ;
  1939. /* Make this a macro so gdb steps over it in one go. */
  1940. CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
  1941. /* Some test broken out here. */
  1942. mono_float_test (filename, format, long_file_ok, allow_fd) ;
  1943. /* Sub format DWVW does not allow seeking. */
  1944. if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
  1945. (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
  1946. { unlink (filename) ;
  1947. printf ("no seek : ok\n") ;
  1948. return ;
  1949. } ;
  1950. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
  1951. mono_rdwr_float_test (filename, format, long_file_ok, allow_fd) ;
  1952. /* If the format doesn't support stereo we're done. */
  1953. sfinfo.channels = 2 ;
  1954. if (sf_format_check (&sfinfo) == 0)
  1955. { unlink (filename) ;
  1956. puts ("no stereo : ok") ;
  1957. return ;
  1958. } ;
  1959. stereo_float_test (filename, format, long_file_ok, allow_fd) ;
  1960. /* New read/write test. Not sure if this is needed yet. */
  1961. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
  1962. (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
  1963. (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
  1964. new_rdwr_float_test (filename, format, allow_fd) ;
  1965. delete_file (format, filename) ;
  1966. puts ("ok") ;
  1967. return ;
  1968. } /* pcm_test_float */
  1969. static void
  1970. mono_float_test (const char *filename, int format, int long_file_ok, int allow_fd)
  1971. { SNDFILE *file ;
  1972. SF_INFO sfinfo ;
  1973. float *orig, *test ;
  1974. sf_count_t count ;
  1975. int k, items ;
  1976. sfinfo.samplerate = 44100 ;
  1977. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  1978. sfinfo.channels = 1 ;
  1979. sfinfo.format = format ;
  1980. orig = orig_data.f ;
  1981. test = test_data.f ;
  1982. items = DATA_LENGTH ;
  1983. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
  1984. sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
  1985. test_write_float_or_die (file, 0, orig, items, __LINE__) ;
  1986. sf_write_sync (file) ;
  1987. test_write_float_or_die (file, 0, orig, items, __LINE__) ;
  1988. sf_write_sync (file) ;
  1989. /* Add non-audio data after the audio. */
  1990. sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
  1991. sf_close (file) ;
  1992. memset (test, 0, items * sizeof (float)) ;
  1993. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
  1994. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  1995. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
  1996. if (sfinfo.format != format)
  1997. { printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
  1998. exit (1) ;
  1999. } ;
  2000. if (sfinfo.frames < 2 * items)
  2001. { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
  2002. exit (1) ;
  2003. } ;
  2004. if (! long_file_ok && sfinfo.frames > 2 * items)
  2005. { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
  2006. exit (1) ;
  2007. } ;
  2008. if (sfinfo.channels != 1)
  2009. { printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
  2010. exit (1) ;
  2011. } ;
  2012. check_log_buffer_or_die (file, __LINE__) ;
  2013. test_read_float_or_die (file, 0, test, items, __LINE__) ;
  2014. for (k = 0 ; k < items ; k++)
  2015. if (FLOAT_ERROR (orig [k], test [k]))
  2016. { printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
  2017. oct_save_float (orig, test, items) ;
  2018. exit (1) ;
  2019. } ;
  2020. /* Seek to start of file. */
  2021. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  2022. test_read_float_or_die (file, 0, test, 4, __LINE__) ;
  2023. for (k = 0 ; k < 4 ; k++)
  2024. if (FLOAT_ERROR (orig [k], test [k]))
  2025. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
  2026. exit (1) ;
  2027. } ;
  2028. if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
  2029. (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
  2030. { sf_close (file) ;
  2031. unlink (filename) ;
  2032. printf ("no seek : ") ;
  2033. return ;
  2034. } ;
  2035. /* Seek to offset from start of file. */
  2036. test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
  2037. test_read_float_or_die (file, 0, test + 10, 4, __LINE__) ;
  2038. for (k = 10 ; k < 14 ; k++)
  2039. if (FLOAT_ERROR (orig [k], test [k]))
  2040. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
  2041. exit (1) ;
  2042. } ;
  2043. /* Seek to offset from current position. */
  2044. test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
  2045. test_read_float_or_die (file, 0, test + 20, 4, __LINE__) ;
  2046. for (k = 20 ; k < 24 ; k++)
  2047. if (FLOAT_ERROR (orig [k], test [k]))
  2048. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
  2049. exit (1) ;
  2050. } ;
  2051. /* Seek to offset from end of file. */
  2052. test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
  2053. test_read_float_or_die (file, 0, test + 10, 4, __LINE__) ;
  2054. for (k = 10 ; k < 14 ; k++)
  2055. if (FLOAT_ERROR (orig [k], test [k]))
  2056. { printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
  2057. exit (1) ;
  2058. } ;
  2059. /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
  2060. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  2061. count = 0 ;
  2062. while (count < sfinfo.frames)
  2063. count += sf_read_float (file, test, 311) ;
  2064. /* Check that no error has occurred. */
  2065. if (sf_error (file))
  2066. { printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
  2067. puts (sf_strerror (file)) ;
  2068. exit (1) ;
  2069. } ;
  2070. /* Check that we haven't read beyond EOF. */
  2071. if (count > sfinfo.frames)
  2072. { printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
  2073. exit (1) ;
  2074. } ;
  2075. test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
  2076. sf_close (file) ;
  2077. multi_seek_test (filename, format) ;
  2078. write_seek_extend_test (filename, format) ;
  2079. } /* mono_float_test */
  2080. static void
  2081. stereo_float_test (const char *filename, int format, int long_file_ok, int allow_fd)
  2082. { SNDFILE *file ;
  2083. SF_INFO sfinfo ;
  2084. float *orig, *test ;
  2085. int k, items, frames ;
  2086. sfinfo.samplerate = 44100 ;
  2087. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  2088. sfinfo.channels = 2 ;
  2089. sfinfo.format = format ;
  2090. gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 1.0) ;
  2091. orig = orig_data.f ;
  2092. test = test_data.f ;
  2093. /* Make this a macro so gdb steps over it in one go. */
  2094. CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
  2095. items = DATA_LENGTH ;
  2096. frames = items / sfinfo.channels ;
  2097. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
  2098. sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
  2099. test_writef_float_or_die (file, 0, orig, frames, __LINE__) ;
  2100. sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
  2101. sf_close (file) ;
  2102. memset (test, 0, items * sizeof (float)) ;
  2103. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
  2104. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  2105. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
  2106. if (sfinfo.format != format)
  2107. { printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
  2108. __LINE__, format, sfinfo.format) ;
  2109. exit (1) ;
  2110. } ;
  2111. if (sfinfo.frames < frames)
  2112. { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
  2113. __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
  2114. exit (1) ;
  2115. } ;
  2116. if (! long_file_ok && sfinfo.frames > frames)
  2117. { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
  2118. __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
  2119. exit (1) ;
  2120. } ;
  2121. if (sfinfo.channels != 2)
  2122. { printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
  2123. exit (1) ;
  2124. } ;
  2125. check_log_buffer_or_die (file, __LINE__) ;
  2126. test_readf_float_or_die (file, 0, test, frames, __LINE__) ;
  2127. for (k = 0 ; k < items ; k++)
  2128. if (FLOAT_ERROR (test [k], orig [k]))
  2129. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
  2130. exit (1) ;
  2131. } ;
  2132. /* Seek to start of file. */
  2133. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  2134. test_readf_float_or_die (file, 0, test, 2, __LINE__) ;
  2135. for (k = 0 ; k < 4 ; k++)
  2136. if (FLOAT_ERROR (test [k], orig [k]))
  2137. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
  2138. exit (1) ;
  2139. } ;
  2140. /* Seek to offset from start of file. */
  2141. test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
  2142. /* Check for errors here. */
  2143. if (sf_error (file))
  2144. { printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
  2145. puts (sf_strerror (file)) ;
  2146. exit (1) ;
  2147. } ;
  2148. if (sf_read_float (file, test, 1) > 0)
  2149. { printf ("Line %d: Should return 0.\n", __LINE__) ;
  2150. exit (1) ;
  2151. } ;
  2152. if (! sf_error (file))
  2153. { printf ("Line %d: Should return an error.\n", __LINE__) ;
  2154. exit (1) ;
  2155. } ;
  2156. /*-----------------------*/
  2157. test_readf_float_or_die (file, 0, test + 10, 2, __LINE__) ;
  2158. for (k = 20 ; k < 24 ; k++)
  2159. if (FLOAT_ERROR (test [k], orig [k]))
  2160. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
  2161. exit (1) ;
  2162. } ;
  2163. /* Seek to offset from current position. */
  2164. test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
  2165. test_readf_float_or_die (file, 0, test + 20, 2, __LINE__) ;
  2166. for (k = 40 ; k < 44 ; k++)
  2167. if (FLOAT_ERROR (test [k], orig [k]))
  2168. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
  2169. exit (1) ;
  2170. } ;
  2171. /* Seek to offset from end of file. */
  2172. test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
  2173. test_readf_float_or_die (file, 0, test + 20, 2, __LINE__) ;
  2174. for (k = 20 ; k < 24 ; k++)
  2175. if (FLOAT_ERROR (test [k], orig [k]))
  2176. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
  2177. exit (1) ;
  2178. } ;
  2179. sf_close (file) ;
  2180. } /* stereo_float_test */
  2181. static void
  2182. mono_rdwr_float_test (const char *filename, int format, int long_file_ok, int allow_fd)
  2183. { SNDFILE *file ;
  2184. SF_INFO sfinfo ;
  2185. float *orig, *test ;
  2186. int k, pass ;
  2187. orig = orig_data.f ;
  2188. test = test_data.f ;
  2189. sfinfo.samplerate = SAMPLE_RATE ;
  2190. sfinfo.frames = DATA_LENGTH ;
  2191. sfinfo.channels = 1 ;
  2192. sfinfo.format = format ;
  2193. if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
  2194. || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
  2195. || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
  2196. unlink (filename) ;
  2197. else
  2198. { /* Create a short file. */
  2199. create_short_file (filename) ;
  2200. /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
  2201. ** If this returns a valif pointer sf_open() screwed up.
  2202. */
  2203. if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
  2204. { printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
  2205. exit (1) ;
  2206. } ;
  2207. /* Truncate the file to zero bytes. */
  2208. if (truncate (filename, 0) < 0)
  2209. { printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
  2210. perror (NULL) ;
  2211. exit (1) ;
  2212. } ;
  2213. } ;
  2214. /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
  2215. ** all the usual data required when opening the file in WRITE mode.
  2216. */
  2217. sfinfo.samplerate = SAMPLE_RATE ;
  2218. sfinfo.frames = DATA_LENGTH ;
  2219. sfinfo.channels = 1 ;
  2220. sfinfo.format = format ;
  2221. file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
  2222. /* Do 3 writes followed by reads. After each, check the data and the current
  2223. ** read and write offsets.
  2224. */
  2225. for (pass = 1 ; pass <= 3 ; pass ++)
  2226. { orig [20] = pass * 2 ;
  2227. /* Write some data. */
  2228. test_write_float_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
  2229. test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
  2230. /* Read what we just wrote. */
  2231. test_read_float_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
  2232. /* Check the data. */
  2233. for (k = 0 ; k < DATA_LENGTH ; k++)
  2234. if (FLOAT_ERROR (orig [k], test [k]))
  2235. { printf ("\n\nLine %d (pass %d) A : Error at sample %d (%g => %g).\n", __LINE__, pass, k, orig [k], test [k]) ;
  2236. oct_save_float (orig, test, DATA_LENGTH) ;
  2237. exit (1) ;
  2238. } ;
  2239. test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
  2240. } ; /* for (pass ...) */
  2241. sf_close (file) ;
  2242. /* Open the file again to check the data. */
  2243. file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
  2244. if (sfinfo.format != format)
  2245. { printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
  2246. exit (1) ;
  2247. } ;
  2248. if (sfinfo.frames < 3 * DATA_LENGTH)
  2249. { printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
  2250. exit (1) ;
  2251. }
  2252. if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
  2253. { printf ("\n\nLine %d : Incorrect number of frames in file. (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
  2254. exit (1) ;
  2255. } ;
  2256. if (sfinfo.channels != 1)
  2257. { printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
  2258. exit (1) ;
  2259. } ;
  2260. if (! long_file_ok)
  2261. test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
  2262. else
  2263. test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
  2264. for (pass = 1 ; pass <= 3 ; pass ++)
  2265. { orig [20] = pass * 2 ;
  2266. test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
  2267. /* Read what we just wrote. */
  2268. test_read_float_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
  2269. /* Check the data. */
  2270. for (k = 0 ; k < DATA_LENGTH ; k++)
  2271. if (FLOAT_ERROR (orig [k], test [k]))
  2272. { printf ("\n\nLine %d (pass %d) B : Error at sample %d (%g => %g).\n", __LINE__, pass, k, orig [k], test [k]) ;
  2273. oct_save_float (orig, test, DATA_LENGTH) ;
  2274. exit (1) ;
  2275. } ;
  2276. } ; /* for (pass ...) */
  2277. sf_close (file) ;
  2278. } /* mono_rdwr_float_test */
  2279. static void
  2280. new_rdwr_float_test (const char *filename, int format, int allow_fd)
  2281. { SNDFILE *wfile, *rwfile ;
  2282. SF_INFO sfinfo ;
  2283. float *orig, *test ;
  2284. int items, frames ;
  2285. orig = orig_data.f ;
  2286. test = test_data.f ;
  2287. sfinfo.samplerate = 44100 ;
  2288. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  2289. sfinfo.channels = 2 ;
  2290. sfinfo.format = format ;
  2291. items = DATA_LENGTH ;
  2292. frames = items / sfinfo.channels ;
  2293. wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
  2294. sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
  2295. test_writef_float_or_die (wfile, 1, orig, frames, __LINE__) ;
  2296. sf_write_sync (wfile) ;
  2297. test_writef_float_or_die (wfile, 2, orig, frames, __LINE__) ;
  2298. sf_write_sync (wfile) ;
  2299. rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
  2300. if (sfinfo.frames != 2 * frames)
  2301. { printf ("\n\nLine %d : incorrect number of frames in file (%ld should be %d)\n\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 2 * frames) ;
  2302. exit (1) ;
  2303. } ;
  2304. test_writef_float_or_die (wfile, 3, orig, frames, __LINE__) ;
  2305. test_readf_float_or_die (rwfile, 1, test, frames, __LINE__) ;
  2306. test_readf_float_or_die (rwfile, 2, test, frames, __LINE__) ;
  2307. sf_close (wfile) ;
  2308. sf_close (rwfile) ;
  2309. } /* new_rdwr_float_test */
  2310. /*======================================================================================
  2311. */
  2312. static void mono_double_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
  2313. static void stereo_double_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
  2314. static void mono_rdwr_double_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
  2315. static void new_rdwr_double_test (const char *filename, int format, int allow_fd) ;
  2316. static void multi_seek_test (const char * filename, int format) ;
  2317. static void write_seek_extend_test (const char * filename, int format) ;
  2318. static void
  2319. pcm_test_double (const char *filename, int format, int long_file_ok)
  2320. { SF_INFO sfinfo ;
  2321. double *orig ;
  2322. int k, allow_fd ;
  2323. /* Sd2 files cannot be opened from an existing file descriptor. */
  2324. allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
  2325. print_test_name ("pcm_test_double", filename) ;
  2326. sfinfo.samplerate = 44100 ;
  2327. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  2328. sfinfo.channels = 1 ;
  2329. sfinfo.format = format ;
  2330. gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 1.0) ;
  2331. orig = orig_data.d ;
  2332. /* Make this a macro so gdb steps over it in one go. */
  2333. CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
  2334. /* Some test broken out here. */
  2335. mono_double_test (filename, format, long_file_ok, allow_fd) ;
  2336. /* Sub format DWVW does not allow seeking. */
  2337. if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
  2338. (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
  2339. { unlink (filename) ;
  2340. printf ("no seek : ok\n") ;
  2341. return ;
  2342. } ;
  2343. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
  2344. mono_rdwr_double_test (filename, format, long_file_ok, allow_fd) ;
  2345. /* If the format doesn't support stereo we're done. */
  2346. sfinfo.channels = 2 ;
  2347. if (sf_format_check (&sfinfo) == 0)
  2348. { unlink (filename) ;
  2349. puts ("no stereo : ok") ;
  2350. return ;
  2351. } ;
  2352. stereo_double_test (filename, format, long_file_ok, allow_fd) ;
  2353. /* New read/write test. Not sure if this is needed yet. */
  2354. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
  2355. (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
  2356. (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
  2357. new_rdwr_double_test (filename, format, allow_fd) ;
  2358. delete_file (format, filename) ;
  2359. puts ("ok") ;
  2360. return ;
  2361. } /* pcm_test_double */
  2362. static void
  2363. mono_double_test (const char *filename, int format, int long_file_ok, int allow_fd)
  2364. { SNDFILE *file ;
  2365. SF_INFO sfinfo ;
  2366. double *orig, *test ;
  2367. sf_count_t count ;
  2368. int k, items ;
  2369. sfinfo.samplerate = 44100 ;
  2370. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  2371. sfinfo.channels = 1 ;
  2372. sfinfo.format = format ;
  2373. orig = orig_data.d ;
  2374. test = test_data.d ;
  2375. items = DATA_LENGTH ;
  2376. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
  2377. sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
  2378. test_write_double_or_die (file, 0, orig, items, __LINE__) ;
  2379. sf_write_sync (file) ;
  2380. test_write_double_or_die (file, 0, orig, items, __LINE__) ;
  2381. sf_write_sync (file) ;
  2382. /* Add non-audio data after the audio. */
  2383. sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
  2384. sf_close (file) ;
  2385. memset (test, 0, items * sizeof (double)) ;
  2386. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
  2387. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  2388. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
  2389. if (sfinfo.format != format)
  2390. { printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
  2391. exit (1) ;
  2392. } ;
  2393. if (sfinfo.frames < 2 * items)
  2394. { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
  2395. exit (1) ;
  2396. } ;
  2397. if (! long_file_ok && sfinfo.frames > 2 * items)
  2398. { printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
  2399. exit (1) ;
  2400. } ;
  2401. if (sfinfo.channels != 1)
  2402. { printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
  2403. exit (1) ;
  2404. } ;
  2405. check_log_buffer_or_die (file, __LINE__) ;
  2406. test_read_double_or_die (file, 0, test, items, __LINE__) ;
  2407. for (k = 0 ; k < items ; k++)
  2408. if (FLOAT_ERROR (orig [k], test [k]))
  2409. { printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
  2410. oct_save_double (orig, test, items) ;
  2411. exit (1) ;
  2412. } ;
  2413. /* Seek to start of file. */
  2414. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  2415. test_read_double_or_die (file, 0, test, 4, __LINE__) ;
  2416. for (k = 0 ; k < 4 ; k++)
  2417. if (FLOAT_ERROR (orig [k], test [k]))
  2418. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
  2419. exit (1) ;
  2420. } ;
  2421. if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
  2422. (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
  2423. { sf_close (file) ;
  2424. unlink (filename) ;
  2425. printf ("no seek : ") ;
  2426. return ;
  2427. } ;
  2428. /* Seek to offset from start of file. */
  2429. test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
  2430. test_read_double_or_die (file, 0, test + 10, 4, __LINE__) ;
  2431. for (k = 10 ; k < 14 ; k++)
  2432. if (FLOAT_ERROR (orig [k], test [k]))
  2433. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
  2434. exit (1) ;
  2435. } ;
  2436. /* Seek to offset from current position. */
  2437. test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
  2438. test_read_double_or_die (file, 0, test + 20, 4, __LINE__) ;
  2439. for (k = 20 ; k < 24 ; k++)
  2440. if (FLOAT_ERROR (orig [k], test [k]))
  2441. { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
  2442. exit (1) ;
  2443. } ;
  2444. /* Seek to offset from end of file. */
  2445. test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
  2446. test_read_double_or_die (file, 0, test + 10, 4, __LINE__) ;
  2447. for (k = 10 ; k < 14 ; k++)
  2448. if (FLOAT_ERROR (orig [k], test [k]))
  2449. { printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
  2450. exit (1) ;
  2451. } ;
  2452. /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
  2453. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  2454. count = 0 ;
  2455. while (count < sfinfo.frames)
  2456. count += sf_read_double (file, test, 311) ;
  2457. /* Check that no error has occurred. */
  2458. if (sf_error (file))
  2459. { printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
  2460. puts (sf_strerror (file)) ;
  2461. exit (1) ;
  2462. } ;
  2463. /* Check that we haven't read beyond EOF. */
  2464. if (count > sfinfo.frames)
  2465. { printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
  2466. exit (1) ;
  2467. } ;
  2468. test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
  2469. sf_close (file) ;
  2470. multi_seek_test (filename, format) ;
  2471. write_seek_extend_test (filename, format) ;
  2472. } /* mono_double_test */
  2473. static void
  2474. stereo_double_test (const char *filename, int format, int long_file_ok, int allow_fd)
  2475. { SNDFILE *file ;
  2476. SF_INFO sfinfo ;
  2477. double *orig, *test ;
  2478. int k, items, frames ;
  2479. sfinfo.samplerate = 44100 ;
  2480. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  2481. sfinfo.channels = 2 ;
  2482. sfinfo.format = format ;
  2483. gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 1.0) ;
  2484. orig = orig_data.d ;
  2485. test = test_data.d ;
  2486. /* Make this a macro so gdb steps over it in one go. */
  2487. CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
  2488. items = DATA_LENGTH ;
  2489. frames = items / sfinfo.channels ;
  2490. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
  2491. sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
  2492. test_writef_double_or_die (file, 0, orig, frames, __LINE__) ;
  2493. sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
  2494. sf_close (file) ;
  2495. memset (test, 0, items * sizeof (double)) ;
  2496. if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
  2497. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  2498. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
  2499. if (sfinfo.format != format)
  2500. { printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
  2501. __LINE__, format, sfinfo.format) ;
  2502. exit (1) ;
  2503. } ;
  2504. if (sfinfo.frames < frames)
  2505. { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
  2506. __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
  2507. exit (1) ;
  2508. } ;
  2509. if (! long_file_ok && sfinfo.frames > frames)
  2510. { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
  2511. __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
  2512. exit (1) ;
  2513. } ;
  2514. if (sfinfo.channels != 2)
  2515. { printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
  2516. exit (1) ;
  2517. } ;
  2518. check_log_buffer_or_die (file, __LINE__) ;
  2519. test_readf_double_or_die (file, 0, test, frames, __LINE__) ;
  2520. for (k = 0 ; k < items ; k++)
  2521. if (FLOAT_ERROR (test [k], orig [k]))
  2522. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
  2523. exit (1) ;
  2524. } ;
  2525. /* Seek to start of file. */
  2526. test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  2527. test_readf_double_or_die (file, 0, test, 2, __LINE__) ;
  2528. for (k = 0 ; k < 4 ; k++)
  2529. if (FLOAT_ERROR (test [k], orig [k]))
  2530. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
  2531. exit (1) ;
  2532. } ;
  2533. /* Seek to offset from start of file. */
  2534. test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
  2535. /* Check for errors here. */
  2536. if (sf_error (file))
  2537. { printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
  2538. puts (sf_strerror (file)) ;
  2539. exit (1) ;
  2540. } ;
  2541. if (sf_read_double (file, test, 1) > 0)
  2542. { printf ("Line %d: Should return 0.\n", __LINE__) ;
  2543. exit (1) ;
  2544. } ;
  2545. if (! sf_error (file))
  2546. { printf ("Line %d: Should return an error.\n", __LINE__) ;
  2547. exit (1) ;
  2548. } ;
  2549. /*-----------------------*/
  2550. test_readf_double_or_die (file, 0, test + 10, 2, __LINE__) ;
  2551. for (k = 20 ; k < 24 ; k++)
  2552. if (FLOAT_ERROR (test [k], orig [k]))
  2553. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
  2554. exit (1) ;
  2555. } ;
  2556. /* Seek to offset from current position. */
  2557. test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
  2558. test_readf_double_or_die (file, 0, test + 20, 2, __LINE__) ;
  2559. for (k = 40 ; k < 44 ; k++)
  2560. if (FLOAT_ERROR (test [k], orig [k]))
  2561. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
  2562. exit (1) ;
  2563. } ;
  2564. /* Seek to offset from end of file. */
  2565. test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
  2566. test_readf_double_or_die (file, 0, test + 20, 2, __LINE__) ;
  2567. for (k = 20 ; k < 24 ; k++)
  2568. if (FLOAT_ERROR (test [k], orig [k]))
  2569. { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
  2570. exit (1) ;
  2571. } ;
  2572. sf_close (file) ;
  2573. } /* stereo_double_test */
  2574. static void
  2575. mono_rdwr_double_test (const char *filename, int format, int long_file_ok, int allow_fd)
  2576. { SNDFILE *file ;
  2577. SF_INFO sfinfo ;
  2578. double *orig, *test ;
  2579. int k, pass ;
  2580. orig = orig_data.d ;
  2581. test = test_data.d ;
  2582. sfinfo.samplerate = SAMPLE_RATE ;
  2583. sfinfo.frames = DATA_LENGTH ;
  2584. sfinfo.channels = 1 ;
  2585. sfinfo.format = format ;
  2586. if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
  2587. || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
  2588. || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
  2589. unlink (filename) ;
  2590. else
  2591. { /* Create a short file. */
  2592. create_short_file (filename) ;
  2593. /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
  2594. ** If this returns a valif pointer sf_open() screwed up.
  2595. */
  2596. if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
  2597. { printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
  2598. exit (1) ;
  2599. } ;
  2600. /* Truncate the file to zero bytes. */
  2601. if (truncate (filename, 0) < 0)
  2602. { printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
  2603. perror (NULL) ;
  2604. exit (1) ;
  2605. } ;
  2606. } ;
  2607. /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
  2608. ** all the usual data required when opening the file in WRITE mode.
  2609. */
  2610. sfinfo.samplerate = SAMPLE_RATE ;
  2611. sfinfo.frames = DATA_LENGTH ;
  2612. sfinfo.channels = 1 ;
  2613. sfinfo.format = format ;
  2614. file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
  2615. /* Do 3 writes followed by reads. After each, check the data and the current
  2616. ** read and write offsets.
  2617. */
  2618. for (pass = 1 ; pass <= 3 ; pass ++)
  2619. { orig [20] = pass * 2 ;
  2620. /* Write some data. */
  2621. test_write_double_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
  2622. test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
  2623. /* Read what we just wrote. */
  2624. test_read_double_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
  2625. /* Check the data. */
  2626. for (k = 0 ; k < DATA_LENGTH ; k++)
  2627. if (FLOAT_ERROR (orig [k], test [k]))
  2628. { printf ("\n\nLine %d (pass %d) A : Error at sample %d (%g => %g).\n", __LINE__, pass, k, orig [k], test [k]) ;
  2629. oct_save_double (orig, test, DATA_LENGTH) ;
  2630. exit (1) ;
  2631. } ;
  2632. test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
  2633. } ; /* for (pass ...) */
  2634. sf_close (file) ;
  2635. /* Open the file again to check the data. */
  2636. file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
  2637. if (sfinfo.format != format)
  2638. { printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
  2639. exit (1) ;
  2640. } ;
  2641. if (sfinfo.frames < 3 * DATA_LENGTH)
  2642. { printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
  2643. exit (1) ;
  2644. }
  2645. if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
  2646. { printf ("\n\nLine %d : Incorrect number of frames in file. (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
  2647. exit (1) ;
  2648. } ;
  2649. if (sfinfo.channels != 1)
  2650. { printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
  2651. exit (1) ;
  2652. } ;
  2653. if (! long_file_ok)
  2654. test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
  2655. else
  2656. test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
  2657. for (pass = 1 ; pass <= 3 ; pass ++)
  2658. { orig [20] = pass * 2 ;
  2659. test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
  2660. /* Read what we just wrote. */
  2661. test_read_double_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
  2662. /* Check the data. */
  2663. for (k = 0 ; k < DATA_LENGTH ; k++)
  2664. if (FLOAT_ERROR (orig [k], test [k]))
  2665. { printf ("\n\nLine %d (pass %d) B : Error at sample %d (%g => %g).\n", __LINE__, pass, k, orig [k], test [k]) ;
  2666. oct_save_double (orig, test, DATA_LENGTH) ;
  2667. exit (1) ;
  2668. } ;
  2669. } ; /* for (pass ...) */
  2670. sf_close (file) ;
  2671. } /* mono_rdwr_double_test */
  2672. static void
  2673. new_rdwr_double_test (const char *filename, int format, int allow_fd)
  2674. { SNDFILE *wfile, *rwfile ;
  2675. SF_INFO sfinfo ;
  2676. double *orig, *test ;
  2677. int items, frames ;
  2678. orig = orig_data.d ;
  2679. test = test_data.d ;
  2680. sfinfo.samplerate = 44100 ;
  2681. sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
  2682. sfinfo.channels = 2 ;
  2683. sfinfo.format = format ;
  2684. items = DATA_LENGTH ;
  2685. frames = items / sfinfo.channels ;
  2686. wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
  2687. sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
  2688. test_writef_double_or_die (wfile, 1, orig, frames, __LINE__) ;
  2689. sf_write_sync (wfile) ;
  2690. test_writef_double_or_die (wfile, 2, orig, frames, __LINE__) ;
  2691. sf_write_sync (wfile) ;
  2692. rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
  2693. if (sfinfo.frames != 2 * frames)
  2694. { printf ("\n\nLine %d : incorrect number of frames in file (%ld should be %d)\n\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 2 * frames) ;
  2695. exit (1) ;
  2696. } ;
  2697. test_writef_double_or_die (wfile, 3, orig, frames, __LINE__) ;
  2698. test_readf_double_or_die (rwfile, 1, test, frames, __LINE__) ;
  2699. test_readf_double_or_die (rwfile, 2, test, frames, __LINE__) ;
  2700. sf_close (wfile) ;
  2701. sf_close (rwfile) ;
  2702. } /* new_rdwr_double_test */
  2703. /*----------------------------------------------------------------------------------------
  2704. */
  2705. static void
  2706. empty_file_test (const char *filename, int format)
  2707. { SNDFILE *file ;
  2708. SF_INFO info ;
  2709. int allow_fd ;
  2710. /* Sd2 files cannot be opened from an existing file descriptor. */
  2711. allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
  2712. print_test_name ("empty_file_test", filename) ;
  2713. unlink (filename) ;
  2714. info.samplerate = 48000 ;
  2715. info.channels = 2 ;
  2716. info.format = format ;
  2717. if (sf_format_check (&info) == SF_FALSE)
  2718. { info.channels = 1 ;
  2719. if (sf_format_check (&info) == SF_FALSE)
  2720. { puts ("invalid file format") ;
  2721. return ;
  2722. } ;
  2723. } ;
  2724. /* Create an empty file. */
  2725. file = test_open_file_or_die (filename, SFM_WRITE, &info, allow_fd, __LINE__) ;
  2726. sf_close (file) ;
  2727. /* Open for read and check the length. */
  2728. file = test_open_file_or_die (filename, SFM_READ, &info, allow_fd, __LINE__) ;
  2729. if (SF_COUNT_TO_LONG (info.frames) != 0)
  2730. { printf ("\n\nError : frame count (%ld) should be zero.\n", SF_COUNT_TO_LONG (info.frames)) ;
  2731. exit (1) ;
  2732. } ;
  2733. sf_close (file) ;
  2734. /* Open for read/write and check the length. */
  2735. file = test_open_file_or_die (filename, SFM_RDWR, &info, allow_fd, __LINE__) ;
  2736. if (SF_COUNT_TO_LONG (info.frames) != 0)
  2737. { printf ("\n\nError : frame count (%ld) should be zero.\n", SF_COUNT_TO_LONG (info.frames)) ;
  2738. exit (1) ;
  2739. } ;
  2740. sf_close (file) ;
  2741. /* Open for read and check the length. */
  2742. file = test_open_file_or_die (filename, SFM_READ, &info, allow_fd, __LINE__) ;
  2743. if (SF_COUNT_TO_LONG (info.frames) != 0)
  2744. { printf ("\n\nError : frame count (%ld) should be zero.\n", SF_COUNT_TO_LONG (info.frames)) ;
  2745. exit (1) ;
  2746. } ;
  2747. sf_close (file) ;
  2748. check_open_file_count_or_die (__LINE__) ;
  2749. unlink (filename) ;
  2750. puts ("ok") ;
  2751. return ;
  2752. } /* empty_file_test */
  2753. /*----------------------------------------------------------------------------------------
  2754. */
  2755. static void
  2756. create_short_file (const char *filename)
  2757. { FILE *file ;
  2758. if (! (file = fopen (filename, "w")))
  2759. { printf ("create_short_file : fopen (%s, \"w\") failed.", filename) ;
  2760. fflush (stdout) ;
  2761. perror (NULL) ;
  2762. exit (1) ;
  2763. } ;
  2764. fprintf (file, "This is the file data.\n") ;
  2765. fclose (file) ;
  2766. } /* create_short_file */
  2767. #if (defined (WIN32) || defined (__WIN32))
  2768. /* Win32 does not have truncate (nor does it have the POSIX function ftruncate).
  2769. ** Hack somethng up here to over come this. This function can only truncate to a
  2770. ** length of zero.
  2771. */
  2772. static int
  2773. truncate (const char *filename, int ignored)
  2774. { int fd ;
  2775. ignored = 0 ;
  2776. if ((fd = open (filename, O_RDWR | O_TRUNC | O_BINARY)) < 0)
  2777. return 0 ;
  2778. close (fd) ;
  2779. return 0 ;
  2780. } /* truncate */
  2781. #endif
  2782. static void
  2783. multi_seek_test (const char * filename, int format)
  2784. { SNDFILE * file ;
  2785. SF_INFO info ;
  2786. sf_count_t pos ;
  2787. int k ;
  2788. /* This test doesn't work on the following. */
  2789. switch (format & SF_FORMAT_TYPEMASK)
  2790. { case SF_FORMAT_RAW :
  2791. return ;
  2792. default :
  2793. break ;
  2794. } ;
  2795. memset (&info, 0, sizeof (info)) ;
  2796. generate_file (filename, format, 88200) ;
  2797. file = test_open_file_or_die (filename, SFM_READ, &info, SF_FALSE, __LINE__) ;
  2798. for (k = 0 ; k < 10 ; k++)
  2799. { pos = info.frames / (k + 2) ;
  2800. test_seek_or_die (file, pos, SEEK_SET, pos, info.channels, __LINE__) ;
  2801. } ;
  2802. sf_close (file) ;
  2803. } /* multi_seek_test */
  2804. static void
  2805. write_seek_extend_test (const char * filename, int format)
  2806. { SNDFILE * file ;
  2807. SF_INFO info ;
  2808. short *orig, *test ;
  2809. unsigned items, k ;
  2810. /* This test doesn't work on the following. */
  2811. switch (format & SF_FORMAT_TYPEMASK)
  2812. { case SF_FORMAT_FLAC :
  2813. case SF_FORMAT_HTK :
  2814. case SF_FORMAT_PAF :
  2815. case SF_FORMAT_SDS :
  2816. case SF_FORMAT_SVX :
  2817. return ;
  2818. default :
  2819. break ;
  2820. } ;
  2821. memset (&info, 0, sizeof (info)) ;
  2822. info.samplerate = 48000 ;
  2823. info.channels = 1 ;
  2824. info.format = format ;
  2825. items = 512 ;
  2826. exit_if_true (items > ARRAY_LEN (orig_data.s), "Line %d : Bad assumption.\n", __LINE__) ;
  2827. orig = orig_data.s ;
  2828. test = test_data.s ;
  2829. for (k = 0 ; k < ARRAY_LEN (orig_data.s) ; k++)
  2830. orig [k] = 0x3fff ;
  2831. file = test_open_file_or_die (filename, SFM_WRITE, &info, SF_FALSE, __LINE__) ;
  2832. test_write_short_or_die (file, 0, orig, items, __LINE__) ;
  2833. /* Extend the file using a seek. */
  2834. test_seek_or_die (file, 2 * items, SEEK_SET, 2 * items, info.channels, __LINE__) ;
  2835. test_writef_short_or_die (file, 0, orig, items, __LINE__) ;
  2836. sf_close (file) ;
  2837. file = test_open_file_or_die (filename, SFM_READ, &info, SF_FALSE, __LINE__) ;
  2838. test_read_short_or_die (file, 0, test, 3 * items, __LINE__) ;
  2839. sf_close (file) ;
  2840. /* Can't do these formats due to scaling. */
  2841. switch (format & SF_FORMAT_SUBMASK)
  2842. { case SF_FORMAT_PCM_S8 :
  2843. case SF_FORMAT_PCM_U8 :
  2844. return ;
  2845. default :
  2846. break ;
  2847. } ;
  2848. for (k = 0 ; k < items ; k++)
  2849. { exit_if_true (test [k] != 0x3fff, "Line %d : test [%d] == %d, should be 0x3fff.\n", __LINE__, k, test [k]) ;
  2850. exit_if_true (test [items + k] != 0, "Line %d : test [%d] == %d, should be 0.\n", __LINE__, items + k, test [items + k]) ;
  2851. 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]) ;
  2852. } ;
  2853. return ;
  2854. } /* write_seek_extend_test */