12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731 |
- /*
- ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
- **
- ** This program is free software; you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation; either version 2 of the License, or
- ** (at your option) any later version.
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ** GNU General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program; if not, write to the Free Software
- ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- #include "sfconfig.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #if HAVE_UNISTD_H
- #include <unistd.h>
- #endif
- #include <math.h>
- #if (defined (WIN32) || defined (_WIN32))
- #include <fcntl.h>
- static int truncate (const char *filename, int ignored) ;
- #endif
- #include <sndfile.h>
- #include "utils.h"
- #include "generate.h"
- #define SAMPLE_RATE 11025
- #define DATA_LENGTH (1<<12)
- #define SILLY_WRITE_COUNT (234)
- static void pcm_test_char (const char *str, int format, int long_file_okz) ;
- static void pcm_test_short (const char *str, int format, int long_file_okz) ;
- static void pcm_test_24bit (const char *str, int format, int long_file_okz) ;
- static void pcm_test_int (const char *str, int format, int long_file_okz) ;
- static void pcm_test_float (const char *str, int format, int long_file_okz) ;
- static void pcm_test_double (const char *str, int format, int long_file_okz) ;
- static void empty_file_test (const char *filename, int format) ;
- typedef union
- { double d [DATA_LENGTH] ;
- float f [DATA_LENGTH] ;
- int i [DATA_LENGTH] ;
- short s [DATA_LENGTH] ;
- char c [DATA_LENGTH] ;
- } BUFFER ;
- static BUFFER orig_data ;
- static BUFFER test_data ;
- int
- main (int argc, char **argv)
- { int do_all = 0 ;
- int test_count = 0 ;
- count_open_files () ;
- if (argc != 2)
- { printf ("Usage : %s <test>\n", argv [0]) ;
- printf (" Where <test> is one of the following:\n") ;
- printf (" wav - test WAV file functions (little endian)\n") ;
- printf (" aiff - test AIFF file functions (big endian)\n") ;
- printf (" au - test AU file functions\n") ;
- printf (" avr - test AVR file functions\n") ;
- printf (" caf - test CAF file functions\n") ;
- printf (" raw - test RAW header-less PCM file functions\n") ;
- printf (" paf - test PAF file functions\n") ;
- printf (" svx - test 8SVX/16SV file functions\n") ;
- printf (" nist - test NIST Sphere file functions\n") ;
- printf (" ircam - test IRCAM file functions\n") ;
- printf (" voc - Create Voice file functions\n") ;
- printf (" w64 - Sonic Foundry's W64 file functions\n") ;
- printf (" flac - test FLAC file functions\n") ;
- printf (" mpc2k - test MPC 2000 file functions\n") ;
- printf (" rf64 - test RF64 file functions\n") ;
- printf (" all - perform all tests\n") ;
- exit (1) ;
- } ;
- do_all = !strcmp (argv [1], "all") ;
- if (do_all || ! strcmp (argv [1], "wav"))
- { pcm_test_char ("char.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_U8, SF_FALSE) ;
- pcm_test_short ("short.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_24bit ("24bit.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_24, SF_FALSE) ;
- pcm_test_int ("int.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_32, SF_FALSE) ;
- pcm_test_char ("char.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_U8, SF_FALSE) ;
- pcm_test_short ("short.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_24bit ("24bit.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_24, SF_FALSE) ;
- pcm_test_int ("int.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_32, SF_FALSE) ;
- pcm_test_24bit ("24bit.wavex" , SF_FORMAT_WAVEX | SF_FORMAT_PCM_24, SF_FALSE) ;
- pcm_test_int ("int.wavex" , SF_FORMAT_WAVEX | SF_FORMAT_PCM_32, SF_FALSE) ;
- /* Lite remove start */
- pcm_test_float ("float.wav" , SF_FORMAT_WAV | SF_FORMAT_FLOAT , SF_FALSE) ;
- pcm_test_double ("double.wav" , SF_FORMAT_WAV | SF_FORMAT_DOUBLE, SF_FALSE) ;
- pcm_test_float ("float.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_FLOAT , SF_FALSE) ;
- pcm_test_double ("double.rifx" , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_DOUBLE, SF_FALSE) ;
- pcm_test_float ("float.wavex" , SF_FORMAT_WAVEX | SF_FORMAT_FLOAT , SF_FALSE) ;
- pcm_test_double ("double.wavex" , SF_FORMAT_WAVEX | SF_FORMAT_DOUBLE, SF_FALSE) ;
- /* Lite remove end */
- empty_file_test ("empty_char.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8) ;
- empty_file_test ("empty_short.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
- empty_file_test ("empty_float.wav", SF_FORMAT_WAV | SF_FORMAT_FLOAT) ;
- test_count++ ;
- } ;
- if (do_all || ! strcmp (argv [1], "aiff"))
- { pcm_test_char ("char_u8.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_U8, SF_FALSE) ;
- pcm_test_char ("char_s8.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_S8, SF_FALSE) ;
- pcm_test_short ("short.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_24bit ("24bit.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_24, SF_FALSE) ;
- pcm_test_int ("int.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_32, SF_FALSE) ;
- pcm_test_short ("short_sowt.aifc" , SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_24bit ("24bit_sowt.aifc" , SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | SF_FORMAT_PCM_24, SF_FALSE) ;
- pcm_test_int ("int_sowt.aifc" , SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | SF_FORMAT_PCM_32, SF_FALSE) ;
- pcm_test_short ("short_twos.aifc" , SF_ENDIAN_BIG | SF_FORMAT_AIFF | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_24bit ("24bit_twos.aifc" , SF_ENDIAN_BIG | SF_FORMAT_AIFF | SF_FORMAT_PCM_24, SF_FALSE) ;
- pcm_test_int ("int_twos.aifc" , SF_ENDIAN_BIG | SF_FORMAT_AIFF | SF_FORMAT_PCM_32, SF_FALSE) ;
- /* Lite remove start */
- pcm_test_short ("dwvw16.aifc", SF_FORMAT_AIFF | SF_FORMAT_DWVW_16, SF_TRUE) ;
- pcm_test_24bit ("dwvw24.aifc", SF_FORMAT_AIFF | SF_FORMAT_DWVW_24, SF_TRUE) ;
- pcm_test_float ("float.aifc" , SF_FORMAT_AIFF | SF_FORMAT_FLOAT , SF_FALSE) ;
- pcm_test_double ("double.aifc" , SF_FORMAT_AIFF | SF_FORMAT_DOUBLE, SF_FALSE) ;
- /* Lite remove end */
- empty_file_test ("empty_char.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_U8) ;
- empty_file_test ("empty_short.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_16) ;
- empty_file_test ("empty_float.aiff", SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
- test_count++ ;
- } ;
- if (do_all || ! strcmp (argv [1], "au"))
- { pcm_test_char ("char.au" , SF_FORMAT_AU | SF_FORMAT_PCM_S8, SF_FALSE) ;
- pcm_test_short ("short.au" , SF_FORMAT_AU | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_24bit ("24bit.au" , SF_FORMAT_AU | SF_FORMAT_PCM_24, SF_FALSE) ;
- pcm_test_int ("int.au" , SF_FORMAT_AU | SF_FORMAT_PCM_32, SF_FALSE) ;
- /* Lite remove start */
- pcm_test_float ("float.au" , SF_FORMAT_AU | SF_FORMAT_FLOAT , SF_FALSE) ;
- pcm_test_double ("double.au", SF_FORMAT_AU | SF_FORMAT_DOUBLE, SF_FALSE) ;
- /* Lite remove end */
- pcm_test_char ("char_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_S8, SF_FALSE) ;
- pcm_test_short ("short_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_24bit ("24bit_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_24, SF_FALSE) ;
- pcm_test_int ("int_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_32, SF_FALSE) ;
- /* Lite remove start */
- pcm_test_float ("float_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT , SF_FALSE) ;
- pcm_test_double ("double_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE, SF_FALSE) ;
- /* Lite remove end */
- test_count++ ;
- } ;
- if (do_all || ! strcmp (argv [1], "caf"))
- { pcm_test_char ("char.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_S8, SF_FALSE) ;
- pcm_test_short ("short.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_24bit ("24bit.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_24, SF_FALSE) ;
- pcm_test_int ("int.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_32, SF_FALSE) ;
- /* Lite remove start */
- pcm_test_float ("float.caf" , SF_FORMAT_CAF | SF_FORMAT_FLOAT , SF_FALSE) ;
- pcm_test_double ("double.caf" , SF_FORMAT_CAF | SF_FORMAT_DOUBLE, SF_FALSE) ;
- /* Lite remove end */
- pcm_test_short ("short_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_24bit ("24bit_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_PCM_24, SF_FALSE) ;
- pcm_test_int ("int_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_PCM_32, SF_FALSE) ;
- /* Lite remove start */
- pcm_test_float ("float_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_FLOAT , SF_FALSE) ;
- pcm_test_double ("double_le.caf", SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_DOUBLE, SF_FALSE) ;
- /* Lite remove end */
- test_count++ ;
- } ;
- if (do_all || ! strcmp (argv [1], "raw"))
- { pcm_test_char ("char_s8.raw" , SF_FORMAT_RAW | SF_FORMAT_PCM_S8, SF_FALSE) ;
- pcm_test_char ("char_u8.raw" , SF_FORMAT_RAW | SF_FORMAT_PCM_U8, SF_FALSE) ;
- pcm_test_short ("short_le.raw" , SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_short ("short_be.raw" , SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_24bit ("24bit_le.raw" , SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_24, SF_FALSE) ;
- pcm_test_24bit ("24bit_be.raw" , SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_PCM_24, SF_FALSE) ;
- pcm_test_int ("int_le.raw" , SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_32, SF_FALSE) ;
- pcm_test_int ("int_be.raw" , SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_PCM_32, SF_FALSE) ;
- /* Lite remove start */
- pcm_test_float ("float_le.raw" , SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_FLOAT , SF_FALSE) ;
- pcm_test_float ("float_be.raw" , SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_FLOAT , SF_FALSE) ;
- pcm_test_double ("double_le.raw", SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_DOUBLE, SF_FALSE) ;
- pcm_test_double ("double_be.raw", SF_ENDIAN_BIG | SF_FORMAT_RAW | SF_FORMAT_DOUBLE, SF_FALSE) ;
- /* Lite remove end */
- test_count++ ;
- } ;
- /* Lite remove start */
- if (do_all || ! strcmp (argv [1], "paf"))
- { pcm_test_char ("char_le.paf", SF_ENDIAN_LITTLE | SF_FORMAT_PAF | SF_FORMAT_PCM_S8, SF_FALSE) ;
- pcm_test_char ("char_be.paf", SF_ENDIAN_BIG | SF_FORMAT_PAF | SF_FORMAT_PCM_S8, SF_FALSE) ;
- pcm_test_short ("short_le.paf", SF_ENDIAN_LITTLE | SF_FORMAT_PAF | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_short ("short_be.paf", SF_ENDIAN_BIG | SF_FORMAT_PAF | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_24bit ("24bit_le.paf", SF_ENDIAN_LITTLE | SF_FORMAT_PAF | SF_FORMAT_PCM_24, SF_TRUE) ;
- pcm_test_24bit ("24bit_be.paf", SF_ENDIAN_BIG | SF_FORMAT_PAF | SF_FORMAT_PCM_24, SF_TRUE) ;
- test_count++ ;
- } ;
- if (do_all || ! strcmp (argv [1], "svx"))
- { pcm_test_char ("char.svx" , SF_FORMAT_SVX | SF_FORMAT_PCM_S8, SF_FALSE) ;
- pcm_test_short ("short.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_16, SF_FALSE) ;
- empty_file_test ("empty_char.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_S8) ;
- empty_file_test ("empty_short.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_16) ;
- test_count++ ;
- } ;
- if (do_all || ! strcmp (argv [1], "nist"))
- { pcm_test_short ("short_le.nist", SF_ENDIAN_LITTLE | SF_FORMAT_NIST | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_short ("short_be.nist", SF_ENDIAN_BIG | SF_FORMAT_NIST | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_24bit ("24bit_le.nist", SF_ENDIAN_LITTLE | SF_FORMAT_NIST | SF_FORMAT_PCM_24, SF_FALSE) ;
- pcm_test_24bit ("24bit_be.nist", SF_ENDIAN_BIG | SF_FORMAT_NIST | SF_FORMAT_PCM_24, SF_FALSE) ;
- pcm_test_int ("int_le.nist" , SF_ENDIAN_LITTLE | SF_FORMAT_NIST | SF_FORMAT_PCM_32, SF_FALSE) ;
- pcm_test_int ("int_be.nist" , SF_ENDIAN_BIG | SF_FORMAT_NIST | SF_FORMAT_PCM_32, SF_FALSE) ;
- test_count++ ;
- } ;
- if (do_all || ! strcmp (argv [1], "ircam"))
- { pcm_test_short ("short_be.ircam" , SF_ENDIAN_BIG | SF_FORMAT_IRCAM | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_short ("short_le.ircam" , SF_ENDIAN_LITTLE | SF_FORMAT_IRCAM | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_int ("int_be.ircam" , SF_ENDIAN_BIG | SF_FORMAT_IRCAM | SF_FORMAT_PCM_32, SF_FALSE) ;
- pcm_test_int ("int_le.ircam" , SF_ENDIAN_LITTLE | SF_FORMAT_IRCAM | SF_FORMAT_PCM_32, SF_FALSE) ;
- pcm_test_float ("float_be.ircam" , SF_ENDIAN_BIG | SF_FORMAT_IRCAM | SF_FORMAT_FLOAT , SF_FALSE) ;
- pcm_test_float ("float_le.ircam" , SF_ENDIAN_LITTLE | SF_FORMAT_IRCAM | SF_FORMAT_FLOAT , SF_FALSE) ;
- test_count++ ;
- } ;
- if (do_all || ! strcmp (argv [1], "voc"))
- { pcm_test_char ("char.voc" , SF_FORMAT_VOC | SF_FORMAT_PCM_U8, SF_FALSE) ;
- pcm_test_short ("short.voc", SF_FORMAT_VOC | SF_FORMAT_PCM_16, SF_FALSE) ;
- test_count++ ;
- } ;
- if (do_all || ! strcmp (argv [1], "mat4"))
- { pcm_test_short ("short_be.mat4" , SF_ENDIAN_BIG | SF_FORMAT_MAT4 | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_short ("short_le.mat4" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT4 | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_int ("int_be.mat4" , SF_ENDIAN_BIG | SF_FORMAT_MAT4 | SF_FORMAT_PCM_32, SF_FALSE) ;
- pcm_test_int ("int_le.mat4" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT4 | SF_FORMAT_PCM_32, SF_FALSE) ;
- pcm_test_float ("float_be.mat4" , SF_ENDIAN_BIG | SF_FORMAT_MAT4 | SF_FORMAT_FLOAT , SF_FALSE) ;
- pcm_test_float ("float_le.mat4" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT4 | SF_FORMAT_FLOAT , SF_FALSE) ;
- pcm_test_double ("double_be.mat4" , SF_ENDIAN_BIG | SF_FORMAT_MAT4 | SF_FORMAT_DOUBLE, SF_FALSE) ;
- pcm_test_double ("double_le.mat4" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT4 | SF_FORMAT_DOUBLE, SF_FALSE) ;
- empty_file_test ("empty_short.mat4", SF_FORMAT_MAT4 | SF_FORMAT_PCM_16) ;
- empty_file_test ("empty_float.mat4", SF_FORMAT_MAT4 | SF_FORMAT_FLOAT) ;
- test_count++ ;
- } ;
- if (do_all || ! strcmp (argv [1], "mat5"))
- { pcm_test_char ("char_be.mat5" , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_PCM_U8, SF_FALSE) ;
- pcm_test_char ("char_le.mat5" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT5 | SF_FORMAT_PCM_U8, SF_FALSE) ;
- pcm_test_short ("short_be.mat5" , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_short ("short_le.mat5" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT5 | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_int ("int_be.mat5" , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_PCM_32, SF_FALSE) ;
- pcm_test_int ("int_le.mat5" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT5 | SF_FORMAT_PCM_32, SF_FALSE) ;
- pcm_test_float ("float_be.mat5" , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_FLOAT , SF_FALSE) ;
- pcm_test_float ("float_le.mat5" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT5 | SF_FORMAT_FLOAT , SF_FALSE) ;
- pcm_test_double ("double_be.mat5" , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_DOUBLE, SF_FALSE) ;
- pcm_test_double ("double_le.mat5" , SF_ENDIAN_LITTLE | SF_FORMAT_MAT5 | SF_FORMAT_DOUBLE, SF_FALSE) ;
- increment_open_file_count () ;
- empty_file_test ("empty_char.mat5", SF_FORMAT_MAT5 | SF_FORMAT_PCM_U8) ;
- empty_file_test ("empty_short.mat5", SF_FORMAT_MAT5 | SF_FORMAT_PCM_16) ;
- empty_file_test ("empty_float.mat5", SF_FORMAT_MAT5 | SF_FORMAT_FLOAT) ;
- test_count++ ;
- } ;
- if (do_all || ! strcmp (argv [1], "pvf"))
- { pcm_test_char ("char.pvf" , SF_FORMAT_PVF | SF_FORMAT_PCM_S8, SF_FALSE) ;
- pcm_test_short ("short.pvf", SF_FORMAT_PVF | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_int ("int.pvf" , SF_FORMAT_PVF | SF_FORMAT_PCM_32, SF_FALSE) ;
- test_count++ ;
- } ;
- if (do_all || ! strcmp (argv [1], "htk"))
- { pcm_test_short ("short.htk", SF_FORMAT_HTK | SF_FORMAT_PCM_16, SF_FALSE) ;
- test_count++ ;
- } ;
- if (do_all || ! strcmp (argv [1], "mpc2k"))
- { pcm_test_short ("short.mpc", SF_FORMAT_MPC2K | SF_FORMAT_PCM_16, SF_FALSE) ;
- test_count++ ;
- } ;
- if (do_all || ! strcmp (argv [1], "avr"))
- { pcm_test_char ("char_u8.avr" , SF_FORMAT_AVR | SF_FORMAT_PCM_U8, SF_FALSE) ;
- pcm_test_char ("char_s8.avr" , SF_FORMAT_AVR | SF_FORMAT_PCM_S8, SF_FALSE) ;
- pcm_test_short ("short.avr" , SF_FORMAT_AVR | SF_FORMAT_PCM_16, SF_FALSE) ;
- test_count++ ;
- } ;
- /* Lite remove end */
- if (do_all || ! strcmp (argv [1], "w64"))
- { pcm_test_char ("char.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_U8, SF_FALSE) ;
- pcm_test_short ("short.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_24bit ("24bit.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_24, SF_FALSE) ;
- pcm_test_int ("int.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_32, SF_FALSE) ;
- /* Lite remove start */
- pcm_test_float ("float.w64" , SF_FORMAT_W64 | SF_FORMAT_FLOAT , SF_FALSE) ;
- pcm_test_double ("double.w64" , SF_FORMAT_W64 | SF_FORMAT_DOUBLE, SF_FALSE) ;
- /* Lite remove end */
- empty_file_test ("empty_char.w64", SF_FORMAT_W64 | SF_FORMAT_PCM_U8) ;
- empty_file_test ("empty_short.w64", SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
- empty_file_test ("empty_float.w64", SF_FORMAT_W64 | SF_FORMAT_FLOAT) ;
- test_count++ ;
- } ;
- if (do_all || ! strcmp (argv [1], "sds"))
- { pcm_test_char ("char.sds" , SF_FORMAT_SDS | SF_FORMAT_PCM_S8, SF_FALSE) ;
- pcm_test_short ("short.sds" , SF_FORMAT_SDS | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_24bit ("24bit.sds" , SF_FORMAT_SDS | SF_FORMAT_PCM_24, SF_FALSE) ;
- empty_file_test ("empty_char.sds", SF_FORMAT_SDS | SF_FORMAT_PCM_S8) ;
- empty_file_test ("empty_short.sds", SF_FORMAT_SDS | SF_FORMAT_PCM_16) ;
- test_count++ ;
- } ;
- if (do_all || ! strcmp (argv [1], "sd2"))
- { pcm_test_char ("char.sd2" , SF_FORMAT_SD2 | SF_FORMAT_PCM_S8, SF_TRUE) ;
- pcm_test_short ("short.sd2" , SF_FORMAT_SD2 | SF_FORMAT_PCM_16, SF_TRUE) ;
- pcm_test_24bit ("24bit.sd2" , SF_FORMAT_SD2 | SF_FORMAT_PCM_24, SF_TRUE) ;
- test_count++ ;
- } ;
- if (do_all || ! strcmp (argv [1], "flac"))
- { if (HAVE_EXTERNAL_LIBS)
- { pcm_test_char ("char.flac" , SF_FORMAT_FLAC | SF_FORMAT_PCM_S8, SF_TRUE) ;
- pcm_test_short ("short.flac" , SF_FORMAT_FLAC | SF_FORMAT_PCM_16, SF_TRUE) ;
- pcm_test_24bit ("24bit.flac" , SF_FORMAT_FLAC | SF_FORMAT_PCM_24, SF_TRUE) ;
- }
- else
- puts (" No FLAC tests because FLAC support was not compiled in.") ;
- test_count++ ;
- } ;
- if (do_all || ! strcmp (argv [1], "rf64"))
- { pcm_test_char ("char.rf64" , SF_FORMAT_RF64 | SF_FORMAT_PCM_U8, SF_FALSE) ;
- pcm_test_short ("short.rf64" , SF_FORMAT_RF64 | SF_FORMAT_PCM_16, SF_FALSE) ;
- pcm_test_24bit ("24bit.rf64" , SF_FORMAT_RF64 | SF_FORMAT_PCM_24, SF_FALSE) ;
- pcm_test_int ("int.rf64" , SF_FORMAT_RF64 | SF_FORMAT_PCM_32, SF_FALSE) ;
- /* Lite remove start */
- pcm_test_float ("float.rf64" , SF_FORMAT_RF64 | SF_FORMAT_FLOAT , SF_FALSE) ;
- pcm_test_double ("double.rf64" , SF_FORMAT_RF64 | SF_FORMAT_DOUBLE, SF_FALSE) ;
- empty_file_test ("empty_char.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_U8) ;
- empty_file_test ("empty_short.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
- empty_file_test ("empty_float.rf64", SF_FORMAT_RF64 | SF_FORMAT_FLOAT) ;
- /* Lite remove end */
- test_count++ ;
- } ;
- if (test_count == 0)
- { printf ("Mono : ************************************\n") ;
- printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
- printf ("Mono : ************************************\n") ;
- return 1 ;
- } ;
- /* Only open file descriptors should be stdin, stdout and stderr. */
- check_open_file_count_or_die (__LINE__) ;
- return 0 ;
- } /* main */
- /*============================================================================================
- ** Helper functions and macros.
- */
- static void create_short_file (const char *filename) ;
- #define CHAR_ERROR(x,y) (abs ((x) - (y)) > 255)
- #define INT_ERROR(x,y) (((x) - (y)) != 0)
- #define TRIBYTE_ERROR(x,y) (abs ((x) - (y)) > 255)
- #define FLOAT_ERROR(x,y) (fabs ((x) - (y)) > 1e-5)
- #define CONVERT_DATA(k,len,new,orig) \
- { for ((k) = 0 ; (k) < (len) ; (k) ++) \
- (new) [k] = (orig) [k] ; \
- }
- /*======================================================================================
- */
- static void mono_char_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
- static void stereo_char_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
- static void mono_rdwr_char_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
- static void new_rdwr_char_test (const char *filename, int format, int allow_fd) ;
- static void multi_seek_test (const char * filename, int format) ;
- static void write_seek_extend_test (const char * filename, int format) ;
- static void
- pcm_test_char (const char *filename, int format, int long_file_ok)
- { SF_INFO sfinfo ;
- short *orig ;
- int k, allow_fd ;
- /* Sd2 files cannot be opened from an existing file descriptor. */
- allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
- print_test_name ("pcm_test_char", filename) ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 32000.0) ;
- orig = orig_data.s ;
- /* Make this a macro so gdb steps over it in one go. */
- CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
- /* Some test broken out here. */
- mono_char_test (filename, format, long_file_ok, allow_fd) ;
- /* Sub format DWVW does not allow seeking. */
- if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
- (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
- { unlink (filename) ;
- printf ("no seek : ok\n") ;
- return ;
- } ;
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
- mono_rdwr_char_test (filename, format, long_file_ok, allow_fd) ;
- /* If the format doesn't support stereo we're done. */
- sfinfo.channels = 2 ;
- if (sf_format_check (&sfinfo) == 0)
- { unlink (filename) ;
- puts ("no stereo : ok") ;
- return ;
- } ;
- stereo_char_test (filename, format, long_file_ok, allow_fd) ;
- /* New read/write test. Not sure if this is needed yet. */
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
- (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
- (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
- new_rdwr_char_test (filename, format, allow_fd) ;
- delete_file (format, filename) ;
- puts ("ok") ;
- return ;
- } /* pcm_test_char */
- static void
- mono_char_test (const char *filename, int format, int long_file_ok, int allow_fd)
- { SNDFILE *file ;
- SF_INFO sfinfo ;
- short *orig, *test ;
- sf_count_t count ;
- int k, items ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- orig = orig_data.s ;
- test = test_data.s ;
- items = DATA_LENGTH ;
- file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
- sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
- test_write_short_or_die (file, 0, orig, items, __LINE__) ;
- sf_write_sync (file) ;
- test_write_short_or_die (file, 0, orig, items, __LINE__) ;
- sf_write_sync (file) ;
- /* Add non-audio data after the audio. */
- sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
- sf_close (file) ;
- memset (test, 0, items * sizeof (short)) ;
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
- memset (&sfinfo, 0, sizeof (sfinfo)) ;
- file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.format != format)
- { printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
- exit (1) ;
- } ;
- if (sfinfo.frames < 2 * items)
- { 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) ;
- exit (1) ;
- } ;
- if (! long_file_ok && sfinfo.frames > 2 * items)
- { 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) ;
- exit (1) ;
- } ;
- if (sfinfo.channels != 1)
- { printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
- exit (1) ;
- } ;
- check_log_buffer_or_die (file, __LINE__) ;
- test_read_short_or_die (file, 0, test, items, __LINE__) ;
- for (k = 0 ; k < items ; k++)
- if (CHAR_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- oct_save_short (orig, test, items) ;
- exit (1) ;
- } ;
- /* Seek to start of file. */
- test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
- test_read_short_or_die (file, 0, test, 4, __LINE__) ;
- for (k = 0 ; k < 4 ; k++)
- if (CHAR_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
- (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
- { sf_close (file) ;
- unlink (filename) ;
- printf ("no seek : ") ;
- return ;
- } ;
- /* Seek to offset from start of file. */
- test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
- test_read_short_or_die (file, 0, test + 10, 4, __LINE__) ;
- for (k = 10 ; k < 14 ; k++)
- if (CHAR_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from current position. */
- test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
- test_read_short_or_die (file, 0, test + 20, 4, __LINE__) ;
- for (k = 20 ; k < 24 ; k++)
- if (CHAR_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from end of file. */
- test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
- test_read_short_or_die (file, 0, test + 10, 4, __LINE__) ;
- for (k = 10 ; k < 14 ; k++)
- if (CHAR_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
- exit (1) ;
- } ;
- /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
- test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
- count = 0 ;
- while (count < sfinfo.frames)
- count += sf_read_short (file, test, 311) ;
- /* Check that no error has occurred. */
- if (sf_error (file))
- { printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
- puts (sf_strerror (file)) ;
- exit (1) ;
- } ;
- /* Check that we haven't read beyond EOF. */
- if (count > sfinfo.frames)
- { printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
- exit (1) ;
- } ;
- test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
- sf_close (file) ;
- multi_seek_test (filename, format) ;
- write_seek_extend_test (filename, format) ;
- } /* mono_char_test */
- static void
- stereo_char_test (const char *filename, int format, int long_file_ok, int allow_fd)
- { SNDFILE *file ;
- SF_INFO sfinfo ;
- short *orig, *test ;
- int k, items, frames ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 2 ;
- sfinfo.format = format ;
- gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 32000.0) ;
- orig = orig_data.s ;
- test = test_data.s ;
- /* Make this a macro so gdb steps over it in one go. */
- CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
- items = DATA_LENGTH ;
- frames = items / sfinfo.channels ;
- file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
- sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
- test_writef_short_or_die (file, 0, orig, frames, __LINE__) ;
- sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
- sf_close (file) ;
- memset (test, 0, items * sizeof (short)) ;
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
- memset (&sfinfo, 0, sizeof (sfinfo)) ;
- file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.format != format)
- { printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
- __LINE__, format, sfinfo.format) ;
- exit (1) ;
- } ;
- if (sfinfo.frames < frames)
- { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
- __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
- exit (1) ;
- } ;
- if (! long_file_ok && sfinfo.frames > frames)
- { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
- __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
- exit (1) ;
- } ;
- if (sfinfo.channels != 2)
- { printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
- exit (1) ;
- } ;
- check_log_buffer_or_die (file, __LINE__) ;
- test_readf_short_or_die (file, 0, test, frames, __LINE__) ;
- for (k = 0 ; k < items ; k++)
- if (CHAR_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to start of file. */
- test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
- test_readf_short_or_die (file, 0, test, 2, __LINE__) ;
- for (k = 0 ; k < 4 ; k++)
- if (CHAR_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from start of file. */
- test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
- /* Check for errors here. */
- if (sf_error (file))
- { printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
- puts (sf_strerror (file)) ;
- exit (1) ;
- } ;
- if (sf_read_short (file, test, 1) > 0)
- { printf ("Line %d: Should return 0.\n", __LINE__) ;
- exit (1) ;
- } ;
- if (! sf_error (file))
- { printf ("Line %d: Should return an error.\n", __LINE__) ;
- exit (1) ;
- } ;
- /*-----------------------*/
- test_readf_short_or_die (file, 0, test + 10, 2, __LINE__) ;
- for (k = 20 ; k < 24 ; k++)
- if (CHAR_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from current position. */
- test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
- test_readf_short_or_die (file, 0, test + 20, 2, __LINE__) ;
- for (k = 40 ; k < 44 ; k++)
- if (CHAR_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from end of file. */
- test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
- test_readf_short_or_die (file, 0, test + 20, 2, __LINE__) ;
- for (k = 20 ; k < 24 ; k++)
- if (CHAR_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- sf_close (file) ;
- } /* stereo_char_test */
- static void
- mono_rdwr_char_test (const char *filename, int format, int long_file_ok, int allow_fd)
- { SNDFILE *file ;
- SF_INFO sfinfo ;
- short *orig, *test ;
- int k, pass ;
- orig = orig_data.s ;
- test = test_data.s ;
- sfinfo.samplerate = SAMPLE_RATE ;
- sfinfo.frames = DATA_LENGTH ;
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
- || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
- || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
- unlink (filename) ;
- else
- { /* Create a short file. */
- create_short_file (filename) ;
- /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
- ** If this returns a valif pointer sf_open() screwed up.
- */
- if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
- { printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
- exit (1) ;
- } ;
- /* Truncate the file to zero bytes. */
- if (truncate (filename, 0) < 0)
- { printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
- perror (NULL) ;
- exit (1) ;
- } ;
- } ;
- /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
- ** all the usual data required when opening the file in WRITE mode.
- */
- sfinfo.samplerate = SAMPLE_RATE ;
- sfinfo.frames = DATA_LENGTH ;
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
- /* Do 3 writes followed by reads. After each, check the data and the current
- ** read and write offsets.
- */
- for (pass = 1 ; pass <= 3 ; pass ++)
- { orig [20] = pass * 2 ;
- /* Write some data. */
- test_write_short_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
- test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
- /* Read what we just wrote. */
- test_read_short_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
- /* Check the data. */
- for (k = 0 ; k < DATA_LENGTH ; k++)
- if (CHAR_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d (pass %d) A : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
- oct_save_short (orig, test, DATA_LENGTH) ;
- exit (1) ;
- } ;
- test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
- } ; /* for (pass ...) */
- sf_close (file) ;
- /* Open the file again to check the data. */
- file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.format != format)
- { printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
- exit (1) ;
- } ;
- if (sfinfo.frames < 3 * DATA_LENGTH)
- { printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
- exit (1) ;
- }
- if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
- { 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 ) ;
- exit (1) ;
- } ;
- if (sfinfo.channels != 1)
- { printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
- exit (1) ;
- } ;
- if (! long_file_ok)
- test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
- else
- test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
- for (pass = 1 ; pass <= 3 ; pass ++)
- { orig [20] = pass * 2 ;
- test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
- /* Read what we just wrote. */
- test_read_short_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
- /* Check the data. */
- for (k = 0 ; k < DATA_LENGTH ; k++)
- if (CHAR_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d (pass %d) B : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
- oct_save_short (orig, test, DATA_LENGTH) ;
- exit (1) ;
- } ;
- } ; /* for (pass ...) */
- sf_close (file) ;
- } /* mono_rdwr_short_test */
- static void
- new_rdwr_char_test (const char *filename, int format, int allow_fd)
- { SNDFILE *wfile, *rwfile ;
- SF_INFO sfinfo ;
- short *orig, *test ;
- int items, frames ;
- orig = orig_data.s ;
- test = test_data.s ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 2 ;
- sfinfo.format = format ;
- items = DATA_LENGTH ;
- frames = items / sfinfo.channels ;
- wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
- sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
- test_writef_short_or_die (wfile, 1, orig, frames, __LINE__) ;
- sf_write_sync (wfile) ;
- test_writef_short_or_die (wfile, 2, orig, frames, __LINE__) ;
- sf_write_sync (wfile) ;
- rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.frames != 2 * frames)
- { 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) ;
- exit (1) ;
- } ;
- test_writef_short_or_die (wfile, 3, orig, frames, __LINE__) ;
- test_readf_short_or_die (rwfile, 1, test, frames, __LINE__) ;
- test_readf_short_or_die (rwfile, 2, test, frames, __LINE__) ;
- sf_close (wfile) ;
- sf_close (rwfile) ;
- } /* new_rdwr_char_test */
- /*======================================================================================
- */
- static void mono_short_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
- static void stereo_short_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
- static void mono_rdwr_short_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
- static void new_rdwr_short_test (const char *filename, int format, int allow_fd) ;
- static void multi_seek_test (const char * filename, int format) ;
- static void write_seek_extend_test (const char * filename, int format) ;
- static void
- pcm_test_short (const char *filename, int format, int long_file_ok)
- { SF_INFO sfinfo ;
- short *orig ;
- int k, allow_fd ;
- /* Sd2 files cannot be opened from an existing file descriptor. */
- allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
- print_test_name ("pcm_test_short", filename) ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 32000.0) ;
- orig = orig_data.s ;
- /* Make this a macro so gdb steps over it in one go. */
- CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
- /* Some test broken out here. */
- mono_short_test (filename, format, long_file_ok, allow_fd) ;
- /* Sub format DWVW does not allow seeking. */
- if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
- (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
- { unlink (filename) ;
- printf ("no seek : ok\n") ;
- return ;
- } ;
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
- mono_rdwr_short_test (filename, format, long_file_ok, allow_fd) ;
- /* If the format doesn't support stereo we're done. */
- sfinfo.channels = 2 ;
- if (sf_format_check (&sfinfo) == 0)
- { unlink (filename) ;
- puts ("no stereo : ok") ;
- return ;
- } ;
- stereo_short_test (filename, format, long_file_ok, allow_fd) ;
- /* New read/write test. Not sure if this is needed yet. */
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
- (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
- (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
- new_rdwr_short_test (filename, format, allow_fd) ;
- delete_file (format, filename) ;
- puts ("ok") ;
- return ;
- } /* pcm_test_short */
- static void
- mono_short_test (const char *filename, int format, int long_file_ok, int allow_fd)
- { SNDFILE *file ;
- SF_INFO sfinfo ;
- short *orig, *test ;
- sf_count_t count ;
- int k, items ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- orig = orig_data.s ;
- test = test_data.s ;
- items = DATA_LENGTH ;
- file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
- sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
- test_write_short_or_die (file, 0, orig, items, __LINE__) ;
- sf_write_sync (file) ;
- test_write_short_or_die (file, 0, orig, items, __LINE__) ;
- sf_write_sync (file) ;
- /* Add non-audio data after the audio. */
- sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
- sf_close (file) ;
- memset (test, 0, items * sizeof (short)) ;
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
- memset (&sfinfo, 0, sizeof (sfinfo)) ;
- file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.format != format)
- { printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
- exit (1) ;
- } ;
- if (sfinfo.frames < 2 * items)
- { 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) ;
- exit (1) ;
- } ;
- if (! long_file_ok && sfinfo.frames > 2 * items)
- { 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) ;
- exit (1) ;
- } ;
- if (sfinfo.channels != 1)
- { printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
- exit (1) ;
- } ;
- check_log_buffer_or_die (file, __LINE__) ;
- test_read_short_or_die (file, 0, test, items, __LINE__) ;
- for (k = 0 ; k < items ; k++)
- if (INT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- oct_save_short (orig, test, items) ;
- exit (1) ;
- } ;
- /* Seek to start of file. */
- test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
- test_read_short_or_die (file, 0, test, 4, __LINE__) ;
- for (k = 0 ; k < 4 ; k++)
- if (INT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
- (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
- { sf_close (file) ;
- unlink (filename) ;
- printf ("no seek : ") ;
- return ;
- } ;
- /* Seek to offset from start of file. */
- test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
- test_read_short_or_die (file, 0, test + 10, 4, __LINE__) ;
- for (k = 10 ; k < 14 ; k++)
- if (INT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from current position. */
- test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
- test_read_short_or_die (file, 0, test + 20, 4, __LINE__) ;
- for (k = 20 ; k < 24 ; k++)
- if (INT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from end of file. */
- test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
- test_read_short_or_die (file, 0, test + 10, 4, __LINE__) ;
- for (k = 10 ; k < 14 ; k++)
- if (INT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
- exit (1) ;
- } ;
- /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
- test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
- count = 0 ;
- while (count < sfinfo.frames)
- count += sf_read_short (file, test, 311) ;
- /* Check that no error has occurred. */
- if (sf_error (file))
- { printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
- puts (sf_strerror (file)) ;
- exit (1) ;
- } ;
- /* Check that we haven't read beyond EOF. */
- if (count > sfinfo.frames)
- { printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
- exit (1) ;
- } ;
- test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
- sf_close (file) ;
- multi_seek_test (filename, format) ;
- write_seek_extend_test (filename, format) ;
- } /* mono_short_test */
- static void
- stereo_short_test (const char *filename, int format, int long_file_ok, int allow_fd)
- { SNDFILE *file ;
- SF_INFO sfinfo ;
- short *orig, *test ;
- int k, items, frames ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 2 ;
- sfinfo.format = format ;
- gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 32000.0) ;
- orig = orig_data.s ;
- test = test_data.s ;
- /* Make this a macro so gdb steps over it in one go. */
- CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
- items = DATA_LENGTH ;
- frames = items / sfinfo.channels ;
- file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
- sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
- test_writef_short_or_die (file, 0, orig, frames, __LINE__) ;
- sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
- sf_close (file) ;
- memset (test, 0, items * sizeof (short)) ;
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
- memset (&sfinfo, 0, sizeof (sfinfo)) ;
- file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.format != format)
- { printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
- __LINE__, format, sfinfo.format) ;
- exit (1) ;
- } ;
- if (sfinfo.frames < frames)
- { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
- __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
- exit (1) ;
- } ;
- if (! long_file_ok && sfinfo.frames > frames)
- { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
- __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
- exit (1) ;
- } ;
- if (sfinfo.channels != 2)
- { printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
- exit (1) ;
- } ;
- check_log_buffer_or_die (file, __LINE__) ;
- test_readf_short_or_die (file, 0, test, frames, __LINE__) ;
- for (k = 0 ; k < items ; k++)
- if (INT_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to start of file. */
- test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
- test_readf_short_or_die (file, 0, test, 2, __LINE__) ;
- for (k = 0 ; k < 4 ; k++)
- if (INT_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from start of file. */
- test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
- /* Check for errors here. */
- if (sf_error (file))
- { printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
- puts (sf_strerror (file)) ;
- exit (1) ;
- } ;
- if (sf_read_short (file, test, 1) > 0)
- { printf ("Line %d: Should return 0.\n", __LINE__) ;
- exit (1) ;
- } ;
- if (! sf_error (file))
- { printf ("Line %d: Should return an error.\n", __LINE__) ;
- exit (1) ;
- } ;
- /*-----------------------*/
- test_readf_short_or_die (file, 0, test + 10, 2, __LINE__) ;
- for (k = 20 ; k < 24 ; k++)
- if (INT_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from current position. */
- test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
- test_readf_short_or_die (file, 0, test + 20, 2, __LINE__) ;
- for (k = 40 ; k < 44 ; k++)
- if (INT_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from end of file. */
- test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
- test_readf_short_or_die (file, 0, test + 20, 2, __LINE__) ;
- for (k = 20 ; k < 24 ; k++)
- if (INT_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- sf_close (file) ;
- } /* stereo_short_test */
- static void
- mono_rdwr_short_test (const char *filename, int format, int long_file_ok, int allow_fd)
- { SNDFILE *file ;
- SF_INFO sfinfo ;
- short *orig, *test ;
- int k, pass ;
- orig = orig_data.s ;
- test = test_data.s ;
- sfinfo.samplerate = SAMPLE_RATE ;
- sfinfo.frames = DATA_LENGTH ;
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
- || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
- || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
- unlink (filename) ;
- else
- { /* Create a short file. */
- create_short_file (filename) ;
- /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
- ** If this returns a valif pointer sf_open() screwed up.
- */
- if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
- { printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
- exit (1) ;
- } ;
- /* Truncate the file to zero bytes. */
- if (truncate (filename, 0) < 0)
- { printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
- perror (NULL) ;
- exit (1) ;
- } ;
- } ;
- /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
- ** all the usual data required when opening the file in WRITE mode.
- */
- sfinfo.samplerate = SAMPLE_RATE ;
- sfinfo.frames = DATA_LENGTH ;
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
- /* Do 3 writes followed by reads. After each, check the data and the current
- ** read and write offsets.
- */
- for (pass = 1 ; pass <= 3 ; pass ++)
- { orig [20] = pass * 2 ;
- /* Write some data. */
- test_write_short_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
- test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
- /* Read what we just wrote. */
- test_read_short_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
- /* Check the data. */
- for (k = 0 ; k < DATA_LENGTH ; k++)
- if (INT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d (pass %d) A : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
- oct_save_short (orig, test, DATA_LENGTH) ;
- exit (1) ;
- } ;
- test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
- } ; /* for (pass ...) */
- sf_close (file) ;
- /* Open the file again to check the data. */
- file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.format != format)
- { printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
- exit (1) ;
- } ;
- if (sfinfo.frames < 3 * DATA_LENGTH)
- { printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
- exit (1) ;
- }
- if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
- { 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 ) ;
- exit (1) ;
- } ;
- if (sfinfo.channels != 1)
- { printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
- exit (1) ;
- } ;
- if (! long_file_ok)
- test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
- else
- test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
- for (pass = 1 ; pass <= 3 ; pass ++)
- { orig [20] = pass * 2 ;
- test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
- /* Read what we just wrote. */
- test_read_short_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
- /* Check the data. */
- for (k = 0 ; k < DATA_LENGTH ; k++)
- if (INT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d (pass %d) B : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
- oct_save_short (orig, test, DATA_LENGTH) ;
- exit (1) ;
- } ;
- } ; /* for (pass ...) */
- sf_close (file) ;
- } /* mono_rdwr_short_test */
- static void
- new_rdwr_short_test (const char *filename, int format, int allow_fd)
- { SNDFILE *wfile, *rwfile ;
- SF_INFO sfinfo ;
- short *orig, *test ;
- int items, frames ;
- orig = orig_data.s ;
- test = test_data.s ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 2 ;
- sfinfo.format = format ;
- items = DATA_LENGTH ;
- frames = items / sfinfo.channels ;
- wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
- sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
- test_writef_short_or_die (wfile, 1, orig, frames, __LINE__) ;
- sf_write_sync (wfile) ;
- test_writef_short_or_die (wfile, 2, orig, frames, __LINE__) ;
- sf_write_sync (wfile) ;
- rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.frames != 2 * frames)
- { 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) ;
- exit (1) ;
- } ;
- test_writef_short_or_die (wfile, 3, orig, frames, __LINE__) ;
- test_readf_short_or_die (rwfile, 1, test, frames, __LINE__) ;
- test_readf_short_or_die (rwfile, 2, test, frames, __LINE__) ;
- sf_close (wfile) ;
- sf_close (rwfile) ;
- } /* new_rdwr_short_test */
- /*======================================================================================
- */
- static void mono_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
- static void stereo_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
- static void mono_rdwr_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
- static void new_rdwr_24bit_test (const char *filename, int format, int allow_fd) ;
- static void multi_seek_test (const char * filename, int format) ;
- static void write_seek_extend_test (const char * filename, int format) ;
- static void
- pcm_test_24bit (const char *filename, int format, int long_file_ok)
- { SF_INFO sfinfo ;
- int *orig ;
- int k, allow_fd ;
- /* Sd2 files cannot be opened from an existing file descriptor. */
- allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
- print_test_name ("pcm_test_24bit", filename) ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- gen_windowed_sine_double (orig_data.d, DATA_LENGTH, (1.0 * 0x7F000000)) ;
- orig = orig_data.i ;
- /* Make this a macro so gdb steps over it in one go. */
- CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
- /* Some test broken out here. */
- mono_24bit_test (filename, format, long_file_ok, allow_fd) ;
- /* Sub format DWVW does not allow seeking. */
- if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
- (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
- { unlink (filename) ;
- printf ("no seek : ok\n") ;
- return ;
- } ;
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
- mono_rdwr_24bit_test (filename, format, long_file_ok, allow_fd) ;
- /* If the format doesn't support stereo we're done. */
- sfinfo.channels = 2 ;
- if (sf_format_check (&sfinfo) == 0)
- { unlink (filename) ;
- puts ("no stereo : ok") ;
- return ;
- } ;
- stereo_24bit_test (filename, format, long_file_ok, allow_fd) ;
- /* New read/write test. Not sure if this is needed yet. */
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
- (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
- (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
- new_rdwr_24bit_test (filename, format, allow_fd) ;
- delete_file (format, filename) ;
- puts ("ok") ;
- return ;
- } /* pcm_test_24bit */
- static void
- mono_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd)
- { SNDFILE *file ;
- SF_INFO sfinfo ;
- int *orig, *test ;
- sf_count_t count ;
- int k, items ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- orig = orig_data.i ;
- test = test_data.i ;
- items = DATA_LENGTH ;
- file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
- sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
- test_write_int_or_die (file, 0, orig, items, __LINE__) ;
- sf_write_sync (file) ;
- test_write_int_or_die (file, 0, orig, items, __LINE__) ;
- sf_write_sync (file) ;
- /* Add non-audio data after the audio. */
- sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
- sf_close (file) ;
- memset (test, 0, items * sizeof (int)) ;
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
- memset (&sfinfo, 0, sizeof (sfinfo)) ;
- file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.format != format)
- { printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
- exit (1) ;
- } ;
- if (sfinfo.frames < 2 * items)
- { 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) ;
- exit (1) ;
- } ;
- if (! long_file_ok && sfinfo.frames > 2 * items)
- { 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) ;
- exit (1) ;
- } ;
- if (sfinfo.channels != 1)
- { printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
- exit (1) ;
- } ;
- check_log_buffer_or_die (file, __LINE__) ;
- test_read_int_or_die (file, 0, test, items, __LINE__) ;
- for (k = 0 ; k < items ; k++)
- if (TRIBYTE_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- oct_save_int (orig, test, items) ;
- exit (1) ;
- } ;
- /* Seek to start of file. */
- test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
- test_read_int_or_die (file, 0, test, 4, __LINE__) ;
- for (k = 0 ; k < 4 ; k++)
- if (TRIBYTE_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
- (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
- { sf_close (file) ;
- unlink (filename) ;
- printf ("no seek : ") ;
- return ;
- } ;
- /* Seek to offset from start of file. */
- test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
- test_read_int_or_die (file, 0, test + 10, 4, __LINE__) ;
- for (k = 10 ; k < 14 ; k++)
- if (TRIBYTE_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from current position. */
- test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
- test_read_int_or_die (file, 0, test + 20, 4, __LINE__) ;
- for (k = 20 ; k < 24 ; k++)
- if (TRIBYTE_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from end of file. */
- test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
- test_read_int_or_die (file, 0, test + 10, 4, __LINE__) ;
- for (k = 10 ; k < 14 ; k++)
- if (TRIBYTE_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
- exit (1) ;
- } ;
- /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
- test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
- count = 0 ;
- while (count < sfinfo.frames)
- count += sf_read_int (file, test, 311) ;
- /* Check that no error has occurred. */
- if (sf_error (file))
- { printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
- puts (sf_strerror (file)) ;
- exit (1) ;
- } ;
- /* Check that we haven't read beyond EOF. */
- if (count > sfinfo.frames)
- { printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
- exit (1) ;
- } ;
- test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
- sf_close (file) ;
- multi_seek_test (filename, format) ;
- write_seek_extend_test (filename, format) ;
- } /* mono_24bit_test */
- static void
- stereo_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd)
- { SNDFILE *file ;
- SF_INFO sfinfo ;
- int *orig, *test ;
- int k, items, frames ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 2 ;
- sfinfo.format = format ;
- gen_windowed_sine_double (orig_data.d, DATA_LENGTH, (1.0 * 0x7F000000)) ;
- orig = orig_data.i ;
- test = test_data.i ;
- /* Make this a macro so gdb steps over it in one go. */
- CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
- items = DATA_LENGTH ;
- frames = items / sfinfo.channels ;
- file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
- sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
- test_writef_int_or_die (file, 0, orig, frames, __LINE__) ;
- sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
- sf_close (file) ;
- memset (test, 0, items * sizeof (int)) ;
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
- memset (&sfinfo, 0, sizeof (sfinfo)) ;
- file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.format != format)
- { printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
- __LINE__, format, sfinfo.format) ;
- exit (1) ;
- } ;
- if (sfinfo.frames < frames)
- { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
- __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
- exit (1) ;
- } ;
- if (! long_file_ok && sfinfo.frames > frames)
- { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
- __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
- exit (1) ;
- } ;
- if (sfinfo.channels != 2)
- { printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
- exit (1) ;
- } ;
- check_log_buffer_or_die (file, __LINE__) ;
- test_readf_int_or_die (file, 0, test, frames, __LINE__) ;
- for (k = 0 ; k < items ; k++)
- if (TRIBYTE_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to start of file. */
- test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
- test_readf_int_or_die (file, 0, test, 2, __LINE__) ;
- for (k = 0 ; k < 4 ; k++)
- if (TRIBYTE_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from start of file. */
- test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
- /* Check for errors here. */
- if (sf_error (file))
- { printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
- puts (sf_strerror (file)) ;
- exit (1) ;
- } ;
- if (sf_read_int (file, test, 1) > 0)
- { printf ("Line %d: Should return 0.\n", __LINE__) ;
- exit (1) ;
- } ;
- if (! sf_error (file))
- { printf ("Line %d: Should return an error.\n", __LINE__) ;
- exit (1) ;
- } ;
- /*-----------------------*/
- test_readf_int_or_die (file, 0, test + 10, 2, __LINE__) ;
- for (k = 20 ; k < 24 ; k++)
- if (TRIBYTE_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from current position. */
- test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
- test_readf_int_or_die (file, 0, test + 20, 2, __LINE__) ;
- for (k = 40 ; k < 44 ; k++)
- if (TRIBYTE_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from end of file. */
- test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
- test_readf_int_or_die (file, 0, test + 20, 2, __LINE__) ;
- for (k = 20 ; k < 24 ; k++)
- if (TRIBYTE_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- sf_close (file) ;
- } /* stereo_24bit_test */
- static void
- mono_rdwr_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd)
- { SNDFILE *file ;
- SF_INFO sfinfo ;
- int *orig, *test ;
- int k, pass ;
- orig = orig_data.i ;
- test = test_data.i ;
- sfinfo.samplerate = SAMPLE_RATE ;
- sfinfo.frames = DATA_LENGTH ;
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
- || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
- || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
- unlink (filename) ;
- else
- { /* Create a short file. */
- create_short_file (filename) ;
- /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
- ** If this returns a valif pointer sf_open() screwed up.
- */
- if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
- { printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
- exit (1) ;
- } ;
- /* Truncate the file to zero bytes. */
- if (truncate (filename, 0) < 0)
- { printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
- perror (NULL) ;
- exit (1) ;
- } ;
- } ;
- /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
- ** all the usual data required when opening the file in WRITE mode.
- */
- sfinfo.samplerate = SAMPLE_RATE ;
- sfinfo.frames = DATA_LENGTH ;
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
- /* Do 3 writes followed by reads. After each, check the data and the current
- ** read and write offsets.
- */
- for (pass = 1 ; pass <= 3 ; pass ++)
- { orig [20] = pass * 2 ;
- /* Write some data. */
- test_write_int_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
- test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
- /* Read what we just wrote. */
- test_read_int_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
- /* Check the data. */
- for (k = 0 ; k < DATA_LENGTH ; k++)
- if (TRIBYTE_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d (pass %d) A : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
- oct_save_int (orig, test, DATA_LENGTH) ;
- exit (1) ;
- } ;
- test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
- } ; /* for (pass ...) */
- sf_close (file) ;
- /* Open the file again to check the data. */
- file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.format != format)
- { printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
- exit (1) ;
- } ;
- if (sfinfo.frames < 3 * DATA_LENGTH)
- { printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
- exit (1) ;
- }
- if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
- { 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 ) ;
- exit (1) ;
- } ;
- if (sfinfo.channels != 1)
- { printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
- exit (1) ;
- } ;
- if (! long_file_ok)
- test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
- else
- test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
- for (pass = 1 ; pass <= 3 ; pass ++)
- { orig [20] = pass * 2 ;
- test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
- /* Read what we just wrote. */
- test_read_int_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
- /* Check the data. */
- for (k = 0 ; k < DATA_LENGTH ; k++)
- if (TRIBYTE_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d (pass %d) B : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
- oct_save_int (orig, test, DATA_LENGTH) ;
- exit (1) ;
- } ;
- } ; /* for (pass ...) */
- sf_close (file) ;
- } /* mono_rdwr_int_test */
- static void
- new_rdwr_24bit_test (const char *filename, int format, int allow_fd)
- { SNDFILE *wfile, *rwfile ;
- SF_INFO sfinfo ;
- int *orig, *test ;
- int items, frames ;
- orig = orig_data.i ;
- test = test_data.i ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 2 ;
- sfinfo.format = format ;
- items = DATA_LENGTH ;
- frames = items / sfinfo.channels ;
- wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
- sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
- test_writef_int_or_die (wfile, 1, orig, frames, __LINE__) ;
- sf_write_sync (wfile) ;
- test_writef_int_or_die (wfile, 2, orig, frames, __LINE__) ;
- sf_write_sync (wfile) ;
- rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.frames != 2 * frames)
- { 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) ;
- exit (1) ;
- } ;
- test_writef_int_or_die (wfile, 3, orig, frames, __LINE__) ;
- test_readf_int_or_die (rwfile, 1, test, frames, __LINE__) ;
- test_readf_int_or_die (rwfile, 2, test, frames, __LINE__) ;
- sf_close (wfile) ;
- sf_close (rwfile) ;
- } /* new_rdwr_24bit_test */
- /*======================================================================================
- */
- static void mono_int_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
- static void stereo_int_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
- static void mono_rdwr_int_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
- static void new_rdwr_int_test (const char *filename, int format, int allow_fd) ;
- static void multi_seek_test (const char * filename, int format) ;
- static void write_seek_extend_test (const char * filename, int format) ;
- static void
- pcm_test_int (const char *filename, int format, int long_file_ok)
- { SF_INFO sfinfo ;
- int *orig ;
- int k, allow_fd ;
- /* Sd2 files cannot be opened from an existing file descriptor. */
- allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
- print_test_name ("pcm_test_int", filename) ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- gen_windowed_sine_double (orig_data.d, DATA_LENGTH, (1.0 * 0x7F000000)) ;
- orig = orig_data.i ;
- /* Make this a macro so gdb steps over it in one go. */
- CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
- /* Some test broken out here. */
- mono_int_test (filename, format, long_file_ok, allow_fd) ;
- /* Sub format DWVW does not allow seeking. */
- if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
- (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
- { unlink (filename) ;
- printf ("no seek : ok\n") ;
- return ;
- } ;
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
- mono_rdwr_int_test (filename, format, long_file_ok, allow_fd) ;
- /* If the format doesn't support stereo we're done. */
- sfinfo.channels = 2 ;
- if (sf_format_check (&sfinfo) == 0)
- { unlink (filename) ;
- puts ("no stereo : ok") ;
- return ;
- } ;
- stereo_int_test (filename, format, long_file_ok, allow_fd) ;
- /* New read/write test. Not sure if this is needed yet. */
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
- (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
- (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
- new_rdwr_int_test (filename, format, allow_fd) ;
- delete_file (format, filename) ;
- puts ("ok") ;
- return ;
- } /* pcm_test_int */
- static void
- mono_int_test (const char *filename, int format, int long_file_ok, int allow_fd)
- { SNDFILE *file ;
- SF_INFO sfinfo ;
- int *orig, *test ;
- sf_count_t count ;
- int k, items ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- orig = orig_data.i ;
- test = test_data.i ;
- items = DATA_LENGTH ;
- file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
- sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
- test_write_int_or_die (file, 0, orig, items, __LINE__) ;
- sf_write_sync (file) ;
- test_write_int_or_die (file, 0, orig, items, __LINE__) ;
- sf_write_sync (file) ;
- /* Add non-audio data after the audio. */
- sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
- sf_close (file) ;
- memset (test, 0, items * sizeof (int)) ;
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
- memset (&sfinfo, 0, sizeof (sfinfo)) ;
- file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.format != format)
- { printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
- exit (1) ;
- } ;
- if (sfinfo.frames < 2 * items)
- { 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) ;
- exit (1) ;
- } ;
- if (! long_file_ok && sfinfo.frames > 2 * items)
- { 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) ;
- exit (1) ;
- } ;
- if (sfinfo.channels != 1)
- { printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
- exit (1) ;
- } ;
- check_log_buffer_or_die (file, __LINE__) ;
- test_read_int_or_die (file, 0, test, items, __LINE__) ;
- for (k = 0 ; k < items ; k++)
- if (INT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- oct_save_int (orig, test, items) ;
- exit (1) ;
- } ;
- /* Seek to start of file. */
- test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
- test_read_int_or_die (file, 0, test, 4, __LINE__) ;
- for (k = 0 ; k < 4 ; k++)
- if (INT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
- (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
- { sf_close (file) ;
- unlink (filename) ;
- printf ("no seek : ") ;
- return ;
- } ;
- /* Seek to offset from start of file. */
- test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
- test_read_int_or_die (file, 0, test + 10, 4, __LINE__) ;
- for (k = 10 ; k < 14 ; k++)
- if (INT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from current position. */
- test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
- test_read_int_or_die (file, 0, test + 20, 4, __LINE__) ;
- for (k = 20 ; k < 24 ; k++)
- if (INT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from end of file. */
- test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
- test_read_int_or_die (file, 0, test + 10, 4, __LINE__) ;
- for (k = 10 ; k < 14 ; k++)
- if (INT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
- exit (1) ;
- } ;
- /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
- test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
- count = 0 ;
- while (count < sfinfo.frames)
- count += sf_read_int (file, test, 311) ;
- /* Check that no error has occurred. */
- if (sf_error (file))
- { printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
- puts (sf_strerror (file)) ;
- exit (1) ;
- } ;
- /* Check that we haven't read beyond EOF. */
- if (count > sfinfo.frames)
- { printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
- exit (1) ;
- } ;
- test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
- sf_close (file) ;
- multi_seek_test (filename, format) ;
- write_seek_extend_test (filename, format) ;
- } /* mono_int_test */
- static void
- stereo_int_test (const char *filename, int format, int long_file_ok, int allow_fd)
- { SNDFILE *file ;
- SF_INFO sfinfo ;
- int *orig, *test ;
- int k, items, frames ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 2 ;
- sfinfo.format = format ;
- gen_windowed_sine_double (orig_data.d, DATA_LENGTH, (1.0 * 0x7F000000)) ;
- orig = orig_data.i ;
- test = test_data.i ;
- /* Make this a macro so gdb steps over it in one go. */
- CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
- items = DATA_LENGTH ;
- frames = items / sfinfo.channels ;
- file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
- sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
- test_writef_int_or_die (file, 0, orig, frames, __LINE__) ;
- sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
- sf_close (file) ;
- memset (test, 0, items * sizeof (int)) ;
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
- memset (&sfinfo, 0, sizeof (sfinfo)) ;
- file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.format != format)
- { printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
- __LINE__, format, sfinfo.format) ;
- exit (1) ;
- } ;
- if (sfinfo.frames < frames)
- { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
- __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
- exit (1) ;
- } ;
- if (! long_file_ok && sfinfo.frames > frames)
- { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
- __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
- exit (1) ;
- } ;
- if (sfinfo.channels != 2)
- { printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
- exit (1) ;
- } ;
- check_log_buffer_or_die (file, __LINE__) ;
- test_readf_int_or_die (file, 0, test, frames, __LINE__) ;
- for (k = 0 ; k < items ; k++)
- if (INT_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to start of file. */
- test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
- test_readf_int_or_die (file, 0, test, 2, __LINE__) ;
- for (k = 0 ; k < 4 ; k++)
- if (INT_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from start of file. */
- test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
- /* Check for errors here. */
- if (sf_error (file))
- { printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
- puts (sf_strerror (file)) ;
- exit (1) ;
- } ;
- if (sf_read_int (file, test, 1) > 0)
- { printf ("Line %d: Should return 0.\n", __LINE__) ;
- exit (1) ;
- } ;
- if (! sf_error (file))
- { printf ("Line %d: Should return an error.\n", __LINE__) ;
- exit (1) ;
- } ;
- /*-----------------------*/
- test_readf_int_or_die (file, 0, test + 10, 2, __LINE__) ;
- for (k = 20 ; k < 24 ; k++)
- if (INT_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from current position. */
- test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
- test_readf_int_or_die (file, 0, test + 20, 2, __LINE__) ;
- for (k = 40 ; k < 44 ; k++)
- if (INT_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from end of file. */
- test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
- test_readf_int_or_die (file, 0, test + 20, 2, __LINE__) ;
- for (k = 20 ; k < 24 ; k++)
- if (INT_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- sf_close (file) ;
- } /* stereo_int_test */
- static void
- mono_rdwr_int_test (const char *filename, int format, int long_file_ok, int allow_fd)
- { SNDFILE *file ;
- SF_INFO sfinfo ;
- int *orig, *test ;
- int k, pass ;
- orig = orig_data.i ;
- test = test_data.i ;
- sfinfo.samplerate = SAMPLE_RATE ;
- sfinfo.frames = DATA_LENGTH ;
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
- || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
- || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
- unlink (filename) ;
- else
- { /* Create a short file. */
- create_short_file (filename) ;
- /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
- ** If this returns a valif pointer sf_open() screwed up.
- */
- if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
- { printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
- exit (1) ;
- } ;
- /* Truncate the file to zero bytes. */
- if (truncate (filename, 0) < 0)
- { printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
- perror (NULL) ;
- exit (1) ;
- } ;
- } ;
- /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
- ** all the usual data required when opening the file in WRITE mode.
- */
- sfinfo.samplerate = SAMPLE_RATE ;
- sfinfo.frames = DATA_LENGTH ;
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
- /* Do 3 writes followed by reads. After each, check the data and the current
- ** read and write offsets.
- */
- for (pass = 1 ; pass <= 3 ; pass ++)
- { orig [20] = pass * 2 ;
- /* Write some data. */
- test_write_int_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
- test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
- /* Read what we just wrote. */
- test_read_int_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
- /* Check the data. */
- for (k = 0 ; k < DATA_LENGTH ; k++)
- if (INT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d (pass %d) A : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
- oct_save_int (orig, test, DATA_LENGTH) ;
- exit (1) ;
- } ;
- test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
- } ; /* for (pass ...) */
- sf_close (file) ;
- /* Open the file again to check the data. */
- file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.format != format)
- { printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
- exit (1) ;
- } ;
- if (sfinfo.frames < 3 * DATA_LENGTH)
- { printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
- exit (1) ;
- }
- if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
- { 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 ) ;
- exit (1) ;
- } ;
- if (sfinfo.channels != 1)
- { printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
- exit (1) ;
- } ;
- if (! long_file_ok)
- test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
- else
- test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
- for (pass = 1 ; pass <= 3 ; pass ++)
- { orig [20] = pass * 2 ;
- test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
- /* Read what we just wrote. */
- test_read_int_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
- /* Check the data. */
- for (k = 0 ; k < DATA_LENGTH ; k++)
- if (INT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d (pass %d) B : Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
- oct_save_int (orig, test, DATA_LENGTH) ;
- exit (1) ;
- } ;
- } ; /* for (pass ...) */
- sf_close (file) ;
- } /* mono_rdwr_int_test */
- static void
- new_rdwr_int_test (const char *filename, int format, int allow_fd)
- { SNDFILE *wfile, *rwfile ;
- SF_INFO sfinfo ;
- int *orig, *test ;
- int items, frames ;
- orig = orig_data.i ;
- test = test_data.i ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 2 ;
- sfinfo.format = format ;
- items = DATA_LENGTH ;
- frames = items / sfinfo.channels ;
- wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
- sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
- test_writef_int_or_die (wfile, 1, orig, frames, __LINE__) ;
- sf_write_sync (wfile) ;
- test_writef_int_or_die (wfile, 2, orig, frames, __LINE__) ;
- sf_write_sync (wfile) ;
- rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.frames != 2 * frames)
- { 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) ;
- exit (1) ;
- } ;
- test_writef_int_or_die (wfile, 3, orig, frames, __LINE__) ;
- test_readf_int_or_die (rwfile, 1, test, frames, __LINE__) ;
- test_readf_int_or_die (rwfile, 2, test, frames, __LINE__) ;
- sf_close (wfile) ;
- sf_close (rwfile) ;
- } /* new_rdwr_int_test */
- /*======================================================================================
- */
- static void mono_float_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
- static void stereo_float_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
- static void mono_rdwr_float_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
- static void new_rdwr_float_test (const char *filename, int format, int allow_fd) ;
- static void multi_seek_test (const char * filename, int format) ;
- static void write_seek_extend_test (const char * filename, int format) ;
- static void
- pcm_test_float (const char *filename, int format, int long_file_ok)
- { SF_INFO sfinfo ;
- float *orig ;
- int k, allow_fd ;
- /* Sd2 files cannot be opened from an existing file descriptor. */
- allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
- print_test_name ("pcm_test_float", filename) ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 1.0) ;
- orig = orig_data.f ;
- /* Make this a macro so gdb steps over it in one go. */
- CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
- /* Some test broken out here. */
- mono_float_test (filename, format, long_file_ok, allow_fd) ;
- /* Sub format DWVW does not allow seeking. */
- if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
- (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
- { unlink (filename) ;
- printf ("no seek : ok\n") ;
- return ;
- } ;
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
- mono_rdwr_float_test (filename, format, long_file_ok, allow_fd) ;
- /* If the format doesn't support stereo we're done. */
- sfinfo.channels = 2 ;
- if (sf_format_check (&sfinfo) == 0)
- { unlink (filename) ;
- puts ("no stereo : ok") ;
- return ;
- } ;
- stereo_float_test (filename, format, long_file_ok, allow_fd) ;
- /* New read/write test. Not sure if this is needed yet. */
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
- (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
- (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
- new_rdwr_float_test (filename, format, allow_fd) ;
- delete_file (format, filename) ;
- puts ("ok") ;
- return ;
- } /* pcm_test_float */
- static void
- mono_float_test (const char *filename, int format, int long_file_ok, int allow_fd)
- { SNDFILE *file ;
- SF_INFO sfinfo ;
- float *orig, *test ;
- sf_count_t count ;
- int k, items ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- orig = orig_data.f ;
- test = test_data.f ;
- items = DATA_LENGTH ;
- file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
- sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
- test_write_float_or_die (file, 0, orig, items, __LINE__) ;
- sf_write_sync (file) ;
- test_write_float_or_die (file, 0, orig, items, __LINE__) ;
- sf_write_sync (file) ;
- /* Add non-audio data after the audio. */
- sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
- sf_close (file) ;
- memset (test, 0, items * sizeof (float)) ;
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
- memset (&sfinfo, 0, sizeof (sfinfo)) ;
- file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.format != format)
- { printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
- exit (1) ;
- } ;
- if (sfinfo.frames < 2 * items)
- { 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) ;
- exit (1) ;
- } ;
- if (! long_file_ok && sfinfo.frames > 2 * items)
- { 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) ;
- exit (1) ;
- } ;
- if (sfinfo.channels != 1)
- { printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
- exit (1) ;
- } ;
- check_log_buffer_or_die (file, __LINE__) ;
- test_read_float_or_die (file, 0, test, items, __LINE__) ;
- for (k = 0 ; k < items ; k++)
- if (FLOAT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
- oct_save_float (orig, test, items) ;
- exit (1) ;
- } ;
- /* Seek to start of file. */
- test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
- test_read_float_or_die (file, 0, test, 4, __LINE__) ;
- for (k = 0 ; k < 4 ; k++)
- if (FLOAT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
- (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
- { sf_close (file) ;
- unlink (filename) ;
- printf ("no seek : ") ;
- return ;
- } ;
- /* Seek to offset from start of file. */
- test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
- test_read_float_or_die (file, 0, test + 10, 4, __LINE__) ;
- for (k = 10 ; k < 14 ; k++)
- if (FLOAT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from current position. */
- test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
- test_read_float_or_die (file, 0, test + 20, 4, __LINE__) ;
- for (k = 20 ; k < 24 ; k++)
- if (FLOAT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from end of file. */
- test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
- test_read_float_or_die (file, 0, test + 10, 4, __LINE__) ;
- for (k = 10 ; k < 14 ; k++)
- if (FLOAT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
- exit (1) ;
- } ;
- /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
- test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
- count = 0 ;
- while (count < sfinfo.frames)
- count += sf_read_float (file, test, 311) ;
- /* Check that no error has occurred. */
- if (sf_error (file))
- { printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
- puts (sf_strerror (file)) ;
- exit (1) ;
- } ;
- /* Check that we haven't read beyond EOF. */
- if (count > sfinfo.frames)
- { printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
- exit (1) ;
- } ;
- test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
- sf_close (file) ;
- multi_seek_test (filename, format) ;
- write_seek_extend_test (filename, format) ;
- } /* mono_float_test */
- static void
- stereo_float_test (const char *filename, int format, int long_file_ok, int allow_fd)
- { SNDFILE *file ;
- SF_INFO sfinfo ;
- float *orig, *test ;
- int k, items, frames ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 2 ;
- sfinfo.format = format ;
- gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 1.0) ;
- orig = orig_data.f ;
- test = test_data.f ;
- /* Make this a macro so gdb steps over it in one go. */
- CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
- items = DATA_LENGTH ;
- frames = items / sfinfo.channels ;
- file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
- sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
- test_writef_float_or_die (file, 0, orig, frames, __LINE__) ;
- sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
- sf_close (file) ;
- memset (test, 0, items * sizeof (float)) ;
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
- memset (&sfinfo, 0, sizeof (sfinfo)) ;
- file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.format != format)
- { printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
- __LINE__, format, sfinfo.format) ;
- exit (1) ;
- } ;
- if (sfinfo.frames < frames)
- { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
- __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
- exit (1) ;
- } ;
- if (! long_file_ok && sfinfo.frames > frames)
- { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
- __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
- exit (1) ;
- } ;
- if (sfinfo.channels != 2)
- { printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
- exit (1) ;
- } ;
- check_log_buffer_or_die (file, __LINE__) ;
- test_readf_float_or_die (file, 0, test, frames, __LINE__) ;
- for (k = 0 ; k < items ; k++)
- if (FLOAT_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to start of file. */
- test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
- test_readf_float_or_die (file, 0, test, 2, __LINE__) ;
- for (k = 0 ; k < 4 ; k++)
- if (FLOAT_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from start of file. */
- test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
- /* Check for errors here. */
- if (sf_error (file))
- { printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
- puts (sf_strerror (file)) ;
- exit (1) ;
- } ;
- if (sf_read_float (file, test, 1) > 0)
- { printf ("Line %d: Should return 0.\n", __LINE__) ;
- exit (1) ;
- } ;
- if (! sf_error (file))
- { printf ("Line %d: Should return an error.\n", __LINE__) ;
- exit (1) ;
- } ;
- /*-----------------------*/
- test_readf_float_or_die (file, 0, test + 10, 2, __LINE__) ;
- for (k = 20 ; k < 24 ; k++)
- if (FLOAT_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from current position. */
- test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
- test_readf_float_or_die (file, 0, test + 20, 2, __LINE__) ;
- for (k = 40 ; k < 44 ; k++)
- if (FLOAT_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from end of file. */
- test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
- test_readf_float_or_die (file, 0, test + 20, 2, __LINE__) ;
- for (k = 20 ; k < 24 ; k++)
- if (FLOAT_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- sf_close (file) ;
- } /* stereo_float_test */
- static void
- mono_rdwr_float_test (const char *filename, int format, int long_file_ok, int allow_fd)
- { SNDFILE *file ;
- SF_INFO sfinfo ;
- float *orig, *test ;
- int k, pass ;
- orig = orig_data.f ;
- test = test_data.f ;
- sfinfo.samplerate = SAMPLE_RATE ;
- sfinfo.frames = DATA_LENGTH ;
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
- || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
- || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
- unlink (filename) ;
- else
- { /* Create a short file. */
- create_short_file (filename) ;
- /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
- ** If this returns a valif pointer sf_open() screwed up.
- */
- if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
- { printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
- exit (1) ;
- } ;
- /* Truncate the file to zero bytes. */
- if (truncate (filename, 0) < 0)
- { printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
- perror (NULL) ;
- exit (1) ;
- } ;
- } ;
- /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
- ** all the usual data required when opening the file in WRITE mode.
- */
- sfinfo.samplerate = SAMPLE_RATE ;
- sfinfo.frames = DATA_LENGTH ;
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
- /* Do 3 writes followed by reads. After each, check the data and the current
- ** read and write offsets.
- */
- for (pass = 1 ; pass <= 3 ; pass ++)
- { orig [20] = pass * 2 ;
- /* Write some data. */
- test_write_float_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
- test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
- /* Read what we just wrote. */
- test_read_float_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
- /* Check the data. */
- for (k = 0 ; k < DATA_LENGTH ; k++)
- if (FLOAT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d (pass %d) A : Error at sample %d (%g => %g).\n", __LINE__, pass, k, orig [k], test [k]) ;
- oct_save_float (orig, test, DATA_LENGTH) ;
- exit (1) ;
- } ;
- test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
- } ; /* for (pass ...) */
- sf_close (file) ;
- /* Open the file again to check the data. */
- file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.format != format)
- { printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
- exit (1) ;
- } ;
- if (sfinfo.frames < 3 * DATA_LENGTH)
- { printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
- exit (1) ;
- }
- if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
- { 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 ) ;
- exit (1) ;
- } ;
- if (sfinfo.channels != 1)
- { printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
- exit (1) ;
- } ;
- if (! long_file_ok)
- test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
- else
- test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
- for (pass = 1 ; pass <= 3 ; pass ++)
- { orig [20] = pass * 2 ;
- test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
- /* Read what we just wrote. */
- test_read_float_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
- /* Check the data. */
- for (k = 0 ; k < DATA_LENGTH ; k++)
- if (FLOAT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d (pass %d) B : Error at sample %d (%g => %g).\n", __LINE__, pass, k, orig [k], test [k]) ;
- oct_save_float (orig, test, DATA_LENGTH) ;
- exit (1) ;
- } ;
- } ; /* for (pass ...) */
- sf_close (file) ;
- } /* mono_rdwr_float_test */
- static void
- new_rdwr_float_test (const char *filename, int format, int allow_fd)
- { SNDFILE *wfile, *rwfile ;
- SF_INFO sfinfo ;
- float *orig, *test ;
- int items, frames ;
- orig = orig_data.f ;
- test = test_data.f ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 2 ;
- sfinfo.format = format ;
- items = DATA_LENGTH ;
- frames = items / sfinfo.channels ;
- wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
- sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
- test_writef_float_or_die (wfile, 1, orig, frames, __LINE__) ;
- sf_write_sync (wfile) ;
- test_writef_float_or_die (wfile, 2, orig, frames, __LINE__) ;
- sf_write_sync (wfile) ;
- rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.frames != 2 * frames)
- { 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) ;
- exit (1) ;
- } ;
- test_writef_float_or_die (wfile, 3, orig, frames, __LINE__) ;
- test_readf_float_or_die (rwfile, 1, test, frames, __LINE__) ;
- test_readf_float_or_die (rwfile, 2, test, frames, __LINE__) ;
- sf_close (wfile) ;
- sf_close (rwfile) ;
- } /* new_rdwr_float_test */
- /*======================================================================================
- */
- static void mono_double_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
- static void stereo_double_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
- static void mono_rdwr_double_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
- static void new_rdwr_double_test (const char *filename, int format, int allow_fd) ;
- static void multi_seek_test (const char * filename, int format) ;
- static void write_seek_extend_test (const char * filename, int format) ;
- static void
- pcm_test_double (const char *filename, int format, int long_file_ok)
- { SF_INFO sfinfo ;
- double *orig ;
- int k, allow_fd ;
- /* Sd2 files cannot be opened from an existing file descriptor. */
- allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
- print_test_name ("pcm_test_double", filename) ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 1.0) ;
- orig = orig_data.d ;
- /* Make this a macro so gdb steps over it in one go. */
- CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
- /* Some test broken out here. */
- mono_double_test (filename, format, long_file_ok, allow_fd) ;
- /* Sub format DWVW does not allow seeking. */
- if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
- (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
- { unlink (filename) ;
- printf ("no seek : ok\n") ;
- return ;
- } ;
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
- mono_rdwr_double_test (filename, format, long_file_ok, allow_fd) ;
- /* If the format doesn't support stereo we're done. */
- sfinfo.channels = 2 ;
- if (sf_format_check (&sfinfo) == 0)
- { unlink (filename) ;
- puts ("no stereo : ok") ;
- return ;
- } ;
- stereo_double_test (filename, format, long_file_ok, allow_fd) ;
- /* New read/write test. Not sure if this is needed yet. */
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
- (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
- (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
- new_rdwr_double_test (filename, format, allow_fd) ;
- delete_file (format, filename) ;
- puts ("ok") ;
- return ;
- } /* pcm_test_double */
- static void
- mono_double_test (const char *filename, int format, int long_file_ok, int allow_fd)
- { SNDFILE *file ;
- SF_INFO sfinfo ;
- double *orig, *test ;
- sf_count_t count ;
- int k, items ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- orig = orig_data.d ;
- test = test_data.d ;
- items = DATA_LENGTH ;
- file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
- sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
- test_write_double_or_die (file, 0, orig, items, __LINE__) ;
- sf_write_sync (file) ;
- test_write_double_or_die (file, 0, orig, items, __LINE__) ;
- sf_write_sync (file) ;
- /* Add non-audio data after the audio. */
- sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
- sf_close (file) ;
- memset (test, 0, items * sizeof (double)) ;
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
- memset (&sfinfo, 0, sizeof (sfinfo)) ;
- file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.format != format)
- { printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
- exit (1) ;
- } ;
- if (sfinfo.frames < 2 * items)
- { 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) ;
- exit (1) ;
- } ;
- if (! long_file_ok && sfinfo.frames > 2 * items)
- { 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) ;
- exit (1) ;
- } ;
- if (sfinfo.channels != 1)
- { printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
- exit (1) ;
- } ;
- check_log_buffer_or_die (file, __LINE__) ;
- test_read_double_or_die (file, 0, test, items, __LINE__) ;
- for (k = 0 ; k < items ; k++)
- if (FLOAT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
- oct_save_double (orig, test, items) ;
- exit (1) ;
- } ;
- /* Seek to start of file. */
- test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
- test_read_double_or_die (file, 0, test, 4, __LINE__) ;
- for (k = 0 ; k < 4 ; k++)
- if (FLOAT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
- (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
- { sf_close (file) ;
- unlink (filename) ;
- printf ("no seek : ") ;
- return ;
- } ;
- /* Seek to offset from start of file. */
- test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
- test_read_double_or_die (file, 0, test + 10, 4, __LINE__) ;
- for (k = 10 ; k < 14 ; k++)
- if (FLOAT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from current position. */
- test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
- test_read_double_or_die (file, 0, test + 20, 4, __LINE__) ;
- for (k = 20 ; k < 24 ; k++)
- if (FLOAT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from end of file. */
- test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
- test_read_double_or_die (file, 0, test + 10, 4, __LINE__) ;
- for (k = 10 ; k < 14 ; k++)
- if (FLOAT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
- exit (1) ;
- } ;
- /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
- test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
- count = 0 ;
- while (count < sfinfo.frames)
- count += sf_read_double (file, test, 311) ;
- /* Check that no error has occurred. */
- if (sf_error (file))
- { printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
- puts (sf_strerror (file)) ;
- exit (1) ;
- } ;
- /* Check that we haven't read beyond EOF. */
- if (count > sfinfo.frames)
- { printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
- exit (1) ;
- } ;
- test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
- sf_close (file) ;
- multi_seek_test (filename, format) ;
- write_seek_extend_test (filename, format) ;
- } /* mono_double_test */
- static void
- stereo_double_test (const char *filename, int format, int long_file_ok, int allow_fd)
- { SNDFILE *file ;
- SF_INFO sfinfo ;
- double *orig, *test ;
- int k, items, frames ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 2 ;
- sfinfo.format = format ;
- gen_windowed_sine_double (orig_data.d, DATA_LENGTH, 1.0) ;
- orig = orig_data.d ;
- test = test_data.d ;
- /* Make this a macro so gdb steps over it in one go. */
- CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
- items = DATA_LENGTH ;
- frames = items / sfinfo.channels ;
- file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
- sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
- test_writef_double_or_die (file, 0, orig, frames, __LINE__) ;
- sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
- sf_close (file) ;
- memset (test, 0, items * sizeof (double)) ;
- if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
- memset (&sfinfo, 0, sizeof (sfinfo)) ;
- file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.format != format)
- { printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
- __LINE__, format, sfinfo.format) ;
- exit (1) ;
- } ;
- if (sfinfo.frames < frames)
- { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
- __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
- exit (1) ;
- } ;
- if (! long_file_ok && sfinfo.frames > frames)
- { printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
- __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
- exit (1) ;
- } ;
- if (sfinfo.channels != 2)
- { printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
- exit (1) ;
- } ;
- check_log_buffer_or_die (file, __LINE__) ;
- test_readf_double_or_die (file, 0, test, frames, __LINE__) ;
- for (k = 0 ; k < items ; k++)
- if (FLOAT_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to start of file. */
- test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
- test_readf_double_or_die (file, 0, test, 2, __LINE__) ;
- for (k = 0 ; k < 4 ; k++)
- if (FLOAT_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from start of file. */
- test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
- /* Check for errors here. */
- if (sf_error (file))
- { printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
- puts (sf_strerror (file)) ;
- exit (1) ;
- } ;
- if (sf_read_double (file, test, 1) > 0)
- { printf ("Line %d: Should return 0.\n", __LINE__) ;
- exit (1) ;
- } ;
- if (! sf_error (file))
- { printf ("Line %d: Should return an error.\n", __LINE__) ;
- exit (1) ;
- } ;
- /*-----------------------*/
- test_readf_double_or_die (file, 0, test + 10, 2, __LINE__) ;
- for (k = 20 ; k < 24 ; k++)
- if (FLOAT_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from current position. */
- test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
- test_readf_double_or_die (file, 0, test + 20, 2, __LINE__) ;
- for (k = 40 ; k < 44 ; k++)
- if (FLOAT_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- /* Seek to offset from end of file. */
- test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
- test_readf_double_or_die (file, 0, test + 20, 2, __LINE__) ;
- for (k = 20 ; k < 24 ; k++)
- if (FLOAT_ERROR (test [k], orig [k]))
- { printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
- exit (1) ;
- } ;
- sf_close (file) ;
- } /* stereo_double_test */
- static void
- mono_rdwr_double_test (const char *filename, int format, int long_file_ok, int allow_fd)
- { SNDFILE *file ;
- SF_INFO sfinfo ;
- double *orig, *test ;
- int k, pass ;
- orig = orig_data.d ;
- test = test_data.d ;
- sfinfo.samplerate = SAMPLE_RATE ;
- sfinfo.frames = DATA_LENGTH ;
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
- || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
- || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
- unlink (filename) ;
- else
- { /* Create a short file. */
- create_short_file (filename) ;
- /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
- ** If this returns a valif pointer sf_open() screwed up.
- */
- if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
- { printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
- exit (1) ;
- } ;
- /* Truncate the file to zero bytes. */
- if (truncate (filename, 0) < 0)
- { printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
- perror (NULL) ;
- exit (1) ;
- } ;
- } ;
- /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
- ** all the usual data required when opening the file in WRITE mode.
- */
- sfinfo.samplerate = SAMPLE_RATE ;
- sfinfo.frames = DATA_LENGTH ;
- sfinfo.channels = 1 ;
- sfinfo.format = format ;
- file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
- /* Do 3 writes followed by reads. After each, check the data and the current
- ** read and write offsets.
- */
- for (pass = 1 ; pass <= 3 ; pass ++)
- { orig [20] = pass * 2 ;
- /* Write some data. */
- test_write_double_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
- test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
- /* Read what we just wrote. */
- test_read_double_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
- /* Check the data. */
- for (k = 0 ; k < DATA_LENGTH ; k++)
- if (FLOAT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d (pass %d) A : Error at sample %d (%g => %g).\n", __LINE__, pass, k, orig [k], test [k]) ;
- oct_save_double (orig, test, DATA_LENGTH) ;
- exit (1) ;
- } ;
- test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
- } ; /* for (pass ...) */
- sf_close (file) ;
- /* Open the file again to check the data. */
- file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.format != format)
- { printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
- exit (1) ;
- } ;
- if (sfinfo.frames < 3 * DATA_LENGTH)
- { printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
- exit (1) ;
- }
- if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
- { 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 ) ;
- exit (1) ;
- } ;
- if (sfinfo.channels != 1)
- { printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
- exit (1) ;
- } ;
- if (! long_file_ok)
- test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
- else
- test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
- for (pass = 1 ; pass <= 3 ; pass ++)
- { orig [20] = pass * 2 ;
- test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
- /* Read what we just wrote. */
- test_read_double_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
- /* Check the data. */
- for (k = 0 ; k < DATA_LENGTH ; k++)
- if (FLOAT_ERROR (orig [k], test [k]))
- { printf ("\n\nLine %d (pass %d) B : Error at sample %d (%g => %g).\n", __LINE__, pass, k, orig [k], test [k]) ;
- oct_save_double (orig, test, DATA_LENGTH) ;
- exit (1) ;
- } ;
- } ; /* for (pass ...) */
- sf_close (file) ;
- } /* mono_rdwr_double_test */
- static void
- new_rdwr_double_test (const char *filename, int format, int allow_fd)
- { SNDFILE *wfile, *rwfile ;
- SF_INFO sfinfo ;
- double *orig, *test ;
- int items, frames ;
- orig = orig_data.d ;
- test = test_data.d ;
- sfinfo.samplerate = 44100 ;
- sfinfo.frames = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
- sfinfo.channels = 2 ;
- sfinfo.format = format ;
- items = DATA_LENGTH ;
- frames = items / sfinfo.channels ;
- wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
- sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
- test_writef_double_or_die (wfile, 1, orig, frames, __LINE__) ;
- sf_write_sync (wfile) ;
- test_writef_double_or_die (wfile, 2, orig, frames, __LINE__) ;
- sf_write_sync (wfile) ;
- rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
- if (sfinfo.frames != 2 * frames)
- { 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) ;
- exit (1) ;
- } ;
- test_writef_double_or_die (wfile, 3, orig, frames, __LINE__) ;
- test_readf_double_or_die (rwfile, 1, test, frames, __LINE__) ;
- test_readf_double_or_die (rwfile, 2, test, frames, __LINE__) ;
- sf_close (wfile) ;
- sf_close (rwfile) ;
- } /* new_rdwr_double_test */
- /*----------------------------------------------------------------------------------------
- */
- static void
- empty_file_test (const char *filename, int format)
- { SNDFILE *file ;
- SF_INFO info ;
- int allow_fd ;
- /* Sd2 files cannot be opened from an existing file descriptor. */
- allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
- print_test_name ("empty_file_test", filename) ;
- unlink (filename) ;
- info.samplerate = 48000 ;
- info.channels = 2 ;
- info.format = format ;
- if (sf_format_check (&info) == SF_FALSE)
- { info.channels = 1 ;
- if (sf_format_check (&info) == SF_FALSE)
- { puts ("invalid file format") ;
- return ;
- } ;
- } ;
- /* Create an empty file. */
- file = test_open_file_or_die (filename, SFM_WRITE, &info, allow_fd, __LINE__) ;
- sf_close (file) ;
- /* Open for read and check the length. */
- file = test_open_file_or_die (filename, SFM_READ, &info, allow_fd, __LINE__) ;
- if (SF_COUNT_TO_LONG (info.frames) != 0)
- { printf ("\n\nError : frame count (%ld) should be zero.\n", SF_COUNT_TO_LONG (info.frames)) ;
- exit (1) ;
- } ;
- sf_close (file) ;
- /* Open for read/write and check the length. */
- file = test_open_file_or_die (filename, SFM_RDWR, &info, allow_fd, __LINE__) ;
- if (SF_COUNT_TO_LONG (info.frames) != 0)
- { printf ("\n\nError : frame count (%ld) should be zero.\n", SF_COUNT_TO_LONG (info.frames)) ;
- exit (1) ;
- } ;
- sf_close (file) ;
- /* Open for read and check the length. */
- file = test_open_file_or_die (filename, SFM_READ, &info, allow_fd, __LINE__) ;
- if (SF_COUNT_TO_LONG (info.frames) != 0)
- { printf ("\n\nError : frame count (%ld) should be zero.\n", SF_COUNT_TO_LONG (info.frames)) ;
- exit (1) ;
- } ;
- sf_close (file) ;
- check_open_file_count_or_die (__LINE__) ;
- unlink (filename) ;
- puts ("ok") ;
- return ;
- } /* empty_file_test */
- /*----------------------------------------------------------------------------------------
- */
- static void
- create_short_file (const char *filename)
- { FILE *file ;
- if (! (file = fopen (filename, "w")))
- { printf ("create_short_file : fopen (%s, \"w\") failed.", filename) ;
- fflush (stdout) ;
- perror (NULL) ;
- exit (1) ;
- } ;
- fprintf (file, "This is the file data.\n") ;
- fclose (file) ;
- } /* create_short_file */
- #if (defined (WIN32) || defined (__WIN32))
- /* Win32 does not have truncate (nor does it have the POSIX function ftruncate).
- ** Hack somethng up here to over come this. This function can only truncate to a
- ** length of zero.
- */
- static int
- truncate (const char *filename, int ignored)
- { int fd ;
- ignored = 0 ;
- if ((fd = open (filename, O_RDWR | O_TRUNC | O_BINARY)) < 0)
- return 0 ;
- close (fd) ;
- return 0 ;
- } /* truncate */
- #endif
- static void
- multi_seek_test (const char * filename, int format)
- { SNDFILE * file ;
- SF_INFO info ;
- sf_count_t pos ;
- int k ;
- /* This test doesn't work on the following. */
- switch (format & SF_FORMAT_TYPEMASK)
- { case SF_FORMAT_RAW :
- return ;
- default :
- break ;
- } ;
- memset (&info, 0, sizeof (info)) ;
- generate_file (filename, format, 88200) ;
- file = test_open_file_or_die (filename, SFM_READ, &info, SF_FALSE, __LINE__) ;
- for (k = 0 ; k < 10 ; k++)
- { pos = info.frames / (k + 2) ;
- test_seek_or_die (file, pos, SEEK_SET, pos, info.channels, __LINE__) ;
- } ;
- sf_close (file) ;
- } /* multi_seek_test */
- static void
- write_seek_extend_test (const char * filename, int format)
- { SNDFILE * file ;
- SF_INFO info ;
- short *orig, *test ;
- unsigned items, k ;
- /* This test doesn't work on the following. */
- switch (format & SF_FORMAT_TYPEMASK)
- { case SF_FORMAT_FLAC :
- case SF_FORMAT_HTK :
- case SF_FORMAT_PAF :
- case SF_FORMAT_SDS :
- case SF_FORMAT_SVX :
- return ;
- default :
- break ;
- } ;
- memset (&info, 0, sizeof (info)) ;
- info.samplerate = 48000 ;
- info.channels = 1 ;
- info.format = format ;
- items = 512 ;
- exit_if_true (items > ARRAY_LEN (orig_data.s), "Line %d : Bad assumption.\n", __LINE__) ;
- orig = orig_data.s ;
- test = test_data.s ;
- for (k = 0 ; k < ARRAY_LEN (orig_data.s) ; k++)
- orig [k] = 0x3fff ;
- file = test_open_file_or_die (filename, SFM_WRITE, &info, SF_FALSE, __LINE__) ;
- test_write_short_or_die (file, 0, orig, items, __LINE__) ;
- /* Extend the file using a seek. */
- test_seek_or_die (file, 2 * items, SEEK_SET, 2 * items, info.channels, __LINE__) ;
- test_writef_short_or_die (file, 0, orig, items, __LINE__) ;
- sf_close (file) ;
- file = test_open_file_or_die (filename, SFM_READ, &info, SF_FALSE, __LINE__) ;
- test_read_short_or_die (file, 0, test, 3 * items, __LINE__) ;
- sf_close (file) ;
- /* Can't do these formats due to scaling. */
- switch (format & SF_FORMAT_SUBMASK)
- { case SF_FORMAT_PCM_S8 :
- case SF_FORMAT_PCM_U8 :
- return ;
- default :
- break ;
- } ;
- for (k = 0 ; k < items ; k++)
- { exit_if_true (test [k] != 0x3fff, "Line %d : test [%d] == %d, should be 0x3fff.\n", __LINE__, k, test [k]) ;
- exit_if_true (test [items + k] != 0, "Line %d : test [%d] == %d, should be 0.\n", __LINE__, items + k, test [items + k]) ;
- 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]) ;
- } ;
- return ;
- } /* write_seek_extend_test */
|