psow.c 352 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336
  1. /*
  2. * Copyright (c) 1983-2023 Trevor Wishart and Composers Desktop Project Ltd
  3. * http://www.trevorwishart.co.uk
  4. * http://www.composersdesktop.com
  5. *
  6. This file is part of the CDP System.
  7. The CDP System is free software; you can redistribute it
  8. and/or modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The CDP System is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the CDP System; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA
  19. *
  20. */
  21. /*
  22. * INPUT MUST BE MONO
  23. * & use PTOBRK to create the pitcb brkpoint file with no-SIG markers
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <structures.h>
  28. #include <tkglobals.h>
  29. #include <pnames.h>
  30. #include <filetype.h>
  31. #include <processno.h>
  32. #include <modeno.h>
  33. #include <logic.h>
  34. #include <globcon.h>
  35. #include <cdpmain.h>
  36. #include <math.h>
  37. #include <mixxcon.h>
  38. #include <osbind.h>
  39. #include <standalone.h>
  40. #include <ctype.h>
  41. #include <sfsys.h>
  42. #include <string.h>
  43. #include <srates.h>
  44. //#ifdef unix
  45. #define round(x) lround((x))
  46. //#endif
  47. #ifndef HUGE
  48. #define HUGE 3.40282347e+38F
  49. #endif
  50. char errstr[2400];
  51. int anal_infiles = 1;
  52. int sloom = 0;
  53. int sloombatch = 0;
  54. const char* cdp_version = "7.1.0";
  55. #define WINDIV (6.0)
  56. #define BUMZEROCNT (20) /* No of zeros that can occur WITHIN a genuine signal */
  57. #define SMOOTHWIN (40) /* Max no samples to smooth if glitch at end */
  58. #define PITCHERROR (1.5)
  59. #define ALMOST_OCT (1.9)
  60. /* windows are three times smaller than the pitch-cycle */
  61. //CDP LIB REPLACEMENTS
  62. static int check_psow_param_validity_and_consistency(dataptr dz);
  63. static int setup_psow_application(dataptr dz);
  64. static int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz);
  65. static int parse_infile_and_check_type(char **cmdline,dataptr dz);
  66. static int setup_psow_param_ranges_and_defaults(dataptr dz);
  67. static int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz);
  68. static int parse_infile_and_check_type(char **cmdline,dataptr dz);
  69. static int setup_and_init_input_param_activity(dataptr dz,int tipc);
  70. static int setup_input_param_defaultval_stores(int tipc,aplptr ap);
  71. static int establish_application(dataptr dz);
  72. static int initialise_vflags(dataptr dz);
  73. static int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz);
  74. static int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz);
  75. static int mark_parameter_types(dataptr dz,aplptr ap);
  76. static int assign_file_data_storage(int infilecnt,dataptr dz);
  77. static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
  78. static int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz);
  79. static int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt);
  80. static int establish_maxmode(dataptr dz);
  81. static int get_the_mode_from_cmdline(char *str,dataptr dz);
  82. static int find_max_nopitch_stretch(dataptr dz);
  83. static int create_psow_sndbufs(int minbuf,dataptr dz);
  84. static int psoa_weird(dataptr dz);
  85. static int extract_pitch_dependent_env_from_sndfile(int minwsize,int k,int *maxwsize,dataptr dz);
  86. static float getmaxsampr(int startsamp, int sampcnt,float *buffer);
  87. static int get_min_wsize(int *minwsize,int k,dataptr dz);
  88. static int find_min_energy_downward_zero_crossing_point
  89. (int *n,int *trofpnt,int trofpntcnt,double *scanarray,int *cutcnt,int *cut,int kk,int cutstart,dataptr dz);
  90. static int read_validpitch_wsize_in_samps_from_brktable(double thistime,int kk,dataptr dz);
  91. static int next_zero_cross(int here,dataptr dz);
  92. static int previous_zero_cross(int here,int last,dataptr dz);
  93. static int get_envelope_troughs(int *trofpnt,int *trofpntcnt,int envcnt,dataptr dz);
  94. static int eliminate_too_short_events(int *zcnt,int *final_pos,int *sigcnt,int opos,int last,float *obuf,int chans,dataptr dz);
  95. static int smooth_bad_events(int *zcnt,int *final_pos,int *sig_cnt,int *stsmoothed,int opos,int last,float *buf,int chans,dataptr dz);
  96. static int smooth_bad_grains(int seglen,int bufno, dataptr dz);
  97. static int triangulate_env(int *here,int *there,int ideal_place,float *buf);
  98. static int count_zerocrossings(int here,int there,float *buf);
  99. static int mark_cut(int *cutcnt,int *cut,int localpeakcnt,double *startarray,int here,int there,
  100. int startsamp,int first_downcross,double starttime,int msg,dataptr dz);
  101. static int find_the_local_peaks(int *here,int *there,float *buf,int *n,int trofpntcnt,int *trofpnt,
  102. int *startsamp,int *endsamp,int losamp, int *cut, int cutcnt, double *localpeak, double *scanarray,
  103. int *localpeakcnt,int *first_downcross,int kk,dataptr dz);
  104. static int smooth_cuts(int *cut,int *cutcnt,int kk,int cutstart,dataptr dz);
  105. static int auto_correlate(int start,int *at,int end,int realend,int minlen,double pitchseg,int kk,dataptr dz);
  106. static double autocorrelate(int n,int m,float *buf);
  107. static int next_down_zcross(int here,int hibound,float *buf);
  108. static int last_down_zcross(int here,int lobound,float *buf);
  109. static void keep_filename_tail_only(dataptr dz);
  110. static void create_next_filename(char *outfilename,int n);
  111. static int psoa_interp(dataptr dz);
  112. static int is_valid_grain(float *buf,int filelen);
  113. static int chop_zero_signal_areas_into_compatible_units(int *cutcnt,int *cuttime,int cutstart,int kk,dataptr dz);
  114. static int get_time_nearest_true_pitch(double time,double *pitch,int kk,dataptr dz);
  115. static int do_pitchsync_grain_interp(int start0,int end0,int start1,int end1,int *opos,dataptr dz);
  116. static int count_wavesets(int *cnt,int *maxsize,int start,int end,float *buf);
  117. static int get_waveset_end(int start,int end,int *thisend,float *buf);
  118. static int find_repcnt(int startlen,int endlen,int samplen,dataptr dz);
  119. static double tremolo(int seglen,double tremfrq,double tremdepth,dataptr dz);
  120. static int vibrato(int seglen,double vibfrq,double vibdep, dataptr dz);
  121. static int zerofof(int start,int end,float *buf);
  122. static int fof_stretch(int n,double time,int here_in_buf,int there_in_buf,float *ibuf,float *obuf,
  123. int *opos,double *maxlevel,double gain,dataptr dz);
  124. //static int interp_maxfofs(int k,int here_in_buf, int there_in_buf,float *ibuf,float *obuf,int *cuttime,
  125. // int startsamp,double gain,int *opos,int *n,double maxlevel,int cutcnt,int *seglen,dataptr dz);
  126. static double linear_splint(double *fofenv,int *fofloc,int envcnt,double time,int *lo);
  127. static int superimpose_fofs_on_synthtones(double *fofenv,int *fofloc,double *sintab,int here_in_buf,int there_in_buf,
  128. float *ibuf,float *obuf,int *opos,int samptime,int envcnt,dataptr dz);
  129. #define SINTABLEN (4096.0)
  130. #define ROOT2 (1.4142136)
  131. #define PSOW_POW (.25)
  132. static int read_synth_data(char *filename,dataptr dz);
  133. static int read_time_varying_synth_data(char *filename,dataptr dz);
  134. static int get_data_from_tvary_infile(char *filename,dataptr dz);
  135. static int check_synth_data(int wordcnt_in_line,dataptr dz);
  136. static int getmaxlinelen(int *maxcnt,FILE *fp);;
  137. static int allocate_osc_frq_amp_arrays(int fltcnt,dataptr dz);
  138. static int check_seq_and_range_of_oscillator_data(double *fbrk,int wordcnt_in_line,dataptr dz);
  139. static int allocate_tvarying_osc_arrays(dataptr dz);
  140. static int put_tvarying_osc_data_in_arrays(double *fbrk,dataptr dz);
  141. static int initialise_psowsynth_internal_params(dataptr dz);
  142. static int newoscval(int samptime,dataptr dz);
  143. static int handle_the_special_data(int *cmdlinecnt,char ***cmdline,dataptr dz);
  144. static int setup_the_special_data_ranges(int mode,int srate,double duration,double nyquist,int wlength,int channels,aplptr ap);
  145. static int create_normalising_envelope(dataptr dz);
  146. static void getfofenv(float *ibuf,double *fofenv,int *fofloc,int maxseglen,int here_in_buf,int there_in_buf,int *envcnt);
  147. static int superimpose_fofs_on_input(double *fofenv,int *fofloc,int here_in_buf,int there_in_buf,
  148. float *ibuf,float *ibuf2,float *obuf,int *opos,int startsamp,
  149. int *envtoptime,double *envval,double *envincr,int envcnt,dataptr dz);
  150. static void spacecalc(double position,double *leftgain,double *rightgain);
  151. static void insert_edge_cuts(int *cuttime,int *cutcnt,int *cutstart,dataptr dz);
  152. static float *do_weight(int *cnt0,int *cnt1,float *ibuf,float *ibuf3,float *readbuf,dataptr dz);
  153. static int read_reinforcement_data(char *filename,dataptr dz);
  154. static int harmonic_is_duplicated(int j,int m,int *hno);
  155. static void sort_harmonics_to_ascending_order(dataptr dz);
  156. static int read_inharmonics_data(char *filename,dataptr dz);
  157. static int convert_pse_semit_to_octratio(dataptr dz);
  158. /**************************************** MAIN *********************************************/
  159. int main(int argc,char *argv[])
  160. {
  161. int exit_status;
  162. dataptr dz = NULL;
  163. char **cmdline;
  164. int cmdlinecnt;
  165. int minbuf, n;
  166. //aplptr ap;
  167. int is_launched = FALSE;
  168. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  169. fprintf(stdout,"%s\n",cdp_version);
  170. fflush(stdout);
  171. return 0;
  172. }
  173. /* CHECK FOR SOUNDLOOM */
  174. if((sloom = sound_loom_in_use(&argc,&argv)) > 1) {
  175. sloom = 0;
  176. sloombatch = 1;
  177. }
  178. if(sflinit("cdp")){
  179. sfperror("cdp: initialisation\n");
  180. return(FAILED);
  181. }
  182. /* SET UP THE PRINCIPLE DATASTRUCTURE */
  183. if((exit_status = establish_datastructure(&dz))<0) { // CDP LIB
  184. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  185. return(FAILED);
  186. }
  187. if(!sloom) {
  188. if(argc == 1) {
  189. usage1();
  190. return(FAILED);
  191. } else if(argc == 2) {
  192. usage2(argv[1]);
  193. return(FAILED);
  194. }
  195. }
  196. if(!sloom) {
  197. if((exit_status = make_initial_cmdline_check(&argc,&argv))<0) { // CDP LIB
  198. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  199. return(FAILED);
  200. }
  201. cmdline = argv;
  202. cmdlinecnt = argc;
  203. if((get_the_process_no(argv[0],dz))<0)
  204. return(FAILED);
  205. cmdline++;
  206. cmdlinecnt--;
  207. if((exit_status = establish_maxmode(dz))<0)
  208. return(FAILED);
  209. if(dz->maxmode > 0) {
  210. if((exit_status = get_the_mode_from_cmdline(cmdline[0],dz))<0) {
  211. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  212. return(exit_status);
  213. }
  214. cmdline++;
  215. cmdlinecnt--;
  216. }
  217. // setup_particular_application =
  218. if((exit_status = setup_psow_application(dz))<0) {
  219. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  220. return(FAILED);
  221. }
  222. if((exit_status = count_and_allocate_for_infiles(cmdlinecnt,cmdline,dz))<0) { // CDP LIB
  223. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  224. return(FAILED);
  225. }
  226. } else {
  227. //parse_TK_data() =
  228. if((exit_status = parse_sloom_data(argc,argv,&cmdline,&cmdlinecnt,dz))<0) {
  229. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  230. return(exit_status);
  231. }
  232. }
  233. //ap = dz->application;
  234. // parse_infile_and_hone_type() =
  235. if((exit_status = parse_infile_and_check_type(cmdline,dz))<0) {
  236. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  237. return(FAILED);
  238. }
  239. // setup_param_ranges_and_defaults() =
  240. if((exit_status = setup_psow_param_ranges_and_defaults(dz))<0) {
  241. exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  242. return(FAILED);
  243. }
  244. // open_first_infile CDP LIB
  245. if((exit_status = open_first_infile(cmdline[0],dz))<0) {
  246. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  247. return(FAILED);
  248. }
  249. cmdlinecnt--;
  250. cmdline++;
  251. // handle_extra_infiles() ........
  252. if(dz->process == PSOW_INTERP || dz->process == PSOW_IMPOSE || dz->process == PSOW_INTERLEAVE || dz->process == PSOW_REPLACE) {
  253. if((exit_status = handle_other_infile(1,cmdline[0],dz))<0)
  254. return(exit_status);
  255. cmdline++;
  256. cmdlinecnt--;
  257. }
  258. // handle_outfile() =
  259. if(sloom || (dz->process != PSOW_LOCATE)) {
  260. if((exit_status = handle_the_outfile(&cmdlinecnt,&cmdline,dz))<0) {
  261. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  262. return(FAILED);
  263. }
  264. }
  265. // handle_formants() redundant
  266. // handle_formant_quiksearch() redundant
  267. // handle_special_data() =
  268. if(dz->process == PSOW_SYNTH || dz->process == PSOW_REINF) {
  269. if((exit_status = handle_the_special_data(&cmdlinecnt,&cmdline,dz))<0) {
  270. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  271. return(FAILED);
  272. }
  273. }
  274. if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB
  275. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  276. return(FAILED);
  277. }
  278. // check_param_validity_and_consistency....
  279. if((exit_status = check_psow_param_validity_and_consistency(dz))<0) {
  280. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  281. return(FAILED);
  282. }
  283. is_launched = TRUE;
  284. switch(dz->process) {
  285. case(PSOW_STRETCH): dz->bufcnt = 4; break;
  286. case(PSOW_DUPL): dz->bufcnt = 4; break;
  287. case(PSOW_DEL): dz->bufcnt = 3; break;
  288. case(PSOW_STRFILL): dz->bufcnt = 5; break;
  289. case(PSOW_FREEZE): dz->bufcnt = 3; break;
  290. case(PSOW_CHOP): dz->bufcnt = 3; break;
  291. case(PSOW_INTERP): dz->bufcnt = 5; break;
  292. case(PSOW_FEATURES):dz->bufcnt = 4; break;
  293. case(PSOW_SYNTH): dz->bufcnt = 5; break;
  294. case(PSOW_IMPOSE): dz->bufcnt = 6; break;
  295. case(PSOW_SPLIT): dz->bufcnt = 4; break;
  296. case(PSOW_SPACE): dz->bufcnt = 6; break;
  297. case(PSOW_INTERLEAVE): dz->bufcnt = 6; break;
  298. case(PSOW_REPLACE): dz->bufcnt = 4; break;
  299. case(PSOW_EXTEND):
  300. case(PSOW_EXTEND2): dz->bufcnt = 5; break;
  301. case(PSOW_LOCATE): dz->bufcnt = 2; break;
  302. case(PSOW_CUT): dz->bufcnt = 4; break;
  303. case(PSOW_REINF): dz->bufcnt = 5; break;
  304. }
  305. if((dz->sampbuf = (float **)malloc(sizeof(float *) * (dz->bufcnt+1)))==NULL) {
  306. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffers.\n");
  307. return(MEMORY_ERROR);
  308. }
  309. if((dz->sbufptr = (float **)malloc(sizeof(float *) * dz->bufcnt))==NULL) {
  310. sprintf(errstr,"INSUFFICIENT MEMORY establishing sample buffer pointers.\n");
  311. return(MEMORY_ERROR);
  312. }
  313. for(n = 0;n <dz->bufcnt; n++)
  314. dz->sampbuf[n] = dz->sbufptr[n] = (float *)0;
  315. dz->sampbuf[n] = (float *)0;
  316. if(dz->process == PSOW_INTERP)
  317. minbuf = dz->infile->srate; /* 1 second */
  318. else
  319. minbuf = find_max_nopitch_stretch(dz);
  320. if((exit_status = create_psow_sndbufs(minbuf,dz))<0) {
  321. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  322. return(FAILED);
  323. }
  324. //param_preprocess() redundant
  325. //spec_process_file =
  326. if(dz->process == PSOW_INTERP) {
  327. if((exit_status = psoa_interp(dz))<0) {
  328. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  329. return(FAILED);
  330. }
  331. } else if((exit_status = psoa_weird(dz))<0) {
  332. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  333. return(FAILED);
  334. }
  335. if((exit_status = complete_output(dz))<0) { // CDP LIB
  336. print_messages_and_close_sndfiles(exit_status,is_launched,dz);
  337. return(FAILED);
  338. }
  339. exit_status = print_messages_and_close_sndfiles(FINISHED,is_launched,dz); // CDP LIB
  340. free(dz);
  341. return(SUCCEEDED);
  342. }
  343. /**************************************************/
  344. /* GENERAL FUNCTIONS, REPLACING CDP LIB FUNCTIONS */
  345. /**************************************************/
  346. /****************************** SET_PARAM_DATA *********************************/
  347. int set_param_data(aplptr ap, int special_data,int maxparamcnt,int paramcnt,char *paramlist)
  348. {
  349. ap->special_data = (char)special_data;
  350. ap->param_cnt = (char)paramcnt;
  351. ap->max_param_cnt = (char)maxparamcnt;
  352. if(ap->max_param_cnt>0) {
  353. if((ap->param_list = (char *)malloc((size_t)(ap->max_param_cnt+1)))==NULL) {
  354. sprintf(errstr,"INSUFFICIENT MEMORY: for param_list\n");
  355. return(MEMORY_ERROR);
  356. }
  357. strcpy(ap->param_list,paramlist);
  358. }
  359. return(FINISHED);
  360. }
  361. /****************************** SET_VFLGS *********************************/
  362. int set_vflgs
  363. (aplptr ap,char *optflags,int optcnt,char *optlist,char *varflags,int vflagcnt, int vparamcnt,char *varlist)
  364. {
  365. ap->option_cnt = (char) optcnt; /*RWD added cast */
  366. if(optcnt) {
  367. if((ap->option_list = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  368. sprintf(errstr,"INSUFFICIENT MEMORY: for option_list\n");
  369. return(MEMORY_ERROR);
  370. }
  371. strcpy(ap->option_list,optlist);
  372. if((ap->option_flags = (char *)malloc((size_t)(optcnt+1)))==NULL) {
  373. sprintf(errstr,"INSUFFICIENT MEMORY: for option_flags\n");
  374. return(MEMORY_ERROR);
  375. }
  376. strcpy(ap->option_flags,optflags);
  377. }
  378. ap->vflag_cnt = (char) vflagcnt;
  379. ap->variant_param_cnt = (char) vparamcnt;
  380. if(vflagcnt) {
  381. if((ap->variant_list = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  382. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_list\n");
  383. return(MEMORY_ERROR);
  384. }
  385. strcpy(ap->variant_list,varlist);
  386. if((ap->variant_flags = (char *)malloc((size_t)(vflagcnt+1)))==NULL) {
  387. sprintf(errstr,"INSUFFICIENT MEMORY: for variant_flags\n");
  388. return(MEMORY_ERROR);
  389. }
  390. strcpy(ap->variant_flags,varflags);
  391. }
  392. return(FINISHED);
  393. }
  394. /***************************** APPLICATION_INIT **************************/
  395. int application_init(dataptr dz)
  396. {
  397. int exit_status;
  398. int storage_cnt;
  399. int tipc, brkcnt;
  400. aplptr ap = dz->application;
  401. if(ap->vflag_cnt>0)
  402. initialise_vflags(dz);
  403. tipc = ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt;
  404. ap->total_input_param_cnt = (char)tipc;
  405. if(tipc>0) {
  406. if((exit_status = setup_input_param_range_stores(tipc,ap))<0)
  407. return(exit_status);
  408. if((exit_status = setup_input_param_defaultval_stores(tipc,ap))<0)
  409. return(exit_status);
  410. if((exit_status = setup_and_init_input_param_activity(dz,tipc))<0)
  411. return(exit_status);
  412. }
  413. brkcnt = tipc;
  414. //THERE ARE NO INPUTFILE brktables USED IN THIS PROCESS
  415. if(brkcnt>0) {
  416. if((exit_status = setup_and_init_input_brktable_constants(dz,brkcnt))<0)
  417. return(exit_status);
  418. }
  419. if((storage_cnt = tipc + ap->internal_param_cnt)>0) {
  420. if((exit_status = setup_parameter_storage_and_constants(storage_cnt,dz))<0)
  421. return(exit_status);
  422. if((exit_status = initialise_is_int_and_no_brk_constants(storage_cnt,dz))<0)
  423. return(exit_status);
  424. }
  425. if((exit_status = mark_parameter_types(dz,ap))<0)
  426. return(exit_status);
  427. // establish_infile_constants() replaced by
  428. switch(dz->process) {
  429. case(PSOW_INTERLEAVE):
  430. case(PSOW_REPLACE):
  431. dz->infilecnt = 2;
  432. break;
  433. default:
  434. dz->infilecnt = 1;
  435. break;
  436. }
  437. if((exit_status = setup_internal_arrays_and_array_pointers(dz))<0)
  438. return(exit_status);
  439. //establish_bufptrs_and_extra_buffers():
  440. return(FINISHED);
  441. }
  442. /********************** SETUP_PARAMETER_STORAGE_AND_CONSTANTS ********************/
  443. /* RWD mallo changed to calloc; helps debug verison run as release! */
  444. int setup_parameter_storage_and_constants(int storage_cnt,dataptr dz)
  445. {
  446. if((dz->param = (double *)calloc(storage_cnt, sizeof(double)))==NULL) {
  447. sprintf(errstr,"setup_parameter_storage_and_constants(): 1\n");
  448. return(MEMORY_ERROR);
  449. }
  450. if((dz->iparam = (int *)calloc(storage_cnt, sizeof(int) ))==NULL) {
  451. sprintf(errstr,"setup_parameter_storage_and_constants(): 2\n");
  452. return(MEMORY_ERROR);
  453. }
  454. if((dz->is_int = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  455. sprintf(errstr,"setup_parameter_storage_and_constants(): 3\n");
  456. return(MEMORY_ERROR);
  457. }
  458. if((dz->no_brk = (char *)calloc(storage_cnt, sizeof(char)))==NULL) {
  459. sprintf(errstr,"setup_parameter_storage_and_constants(): 5\n");
  460. return(MEMORY_ERROR);
  461. }
  462. return(FINISHED);
  463. }
  464. /************** INITIALISE_IS_INT_AND_NO_BRK_CONSTANTS *****************/
  465. int initialise_is_int_and_no_brk_constants(int storage_cnt,dataptr dz)
  466. {
  467. int n;
  468. for(n=0;n<storage_cnt;n++) {
  469. dz->is_int[n] = (char)0;
  470. dz->no_brk[n] = (char)0;
  471. }
  472. return(FINISHED);
  473. }
  474. /***************************** MARK_PARAMETER_TYPES **************************/
  475. int mark_parameter_types(dataptr dz,aplptr ap)
  476. {
  477. int n, m; /* PARAMS */
  478. for(n=0;n<ap->max_param_cnt;n++) {
  479. switch(ap->param_list[n]) {
  480. case('0'): break; /* dz->is_active[n] = 0 is default */
  481. case('i'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1;dz->no_brk[n] = (char)1; break;
  482. case('I'): dz->is_active[n] = (char)1; dz->is_int[n] = (char)1; break;
  483. case('d'): dz->is_active[n] = (char)1; dz->no_brk[n] = (char)1; break;
  484. case('D'): dz->is_active[n] = (char)1; /* normal case: double val or brkpnt file */ break;
  485. default:
  486. sprintf(errstr,"Programming error: invalid parameter type in mark_parameter_types()\n");
  487. return(PROGRAM_ERROR);
  488. }
  489. } /* OPTIONS */
  490. for(n=0,m=ap->max_param_cnt;n<ap->option_cnt;n++,m++) {
  491. switch(ap->option_list[n]) {
  492. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  493. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  494. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  495. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  496. default:
  497. sprintf(errstr,"Programming error: invalid option type in mark_parameter_types()\n");
  498. return(PROGRAM_ERROR);
  499. }
  500. } /* VARIANTS */
  501. for(n=0,m=ap->max_param_cnt + ap->option_cnt;n < ap->variant_param_cnt; n++, m++) {
  502. switch(ap->variant_list[n]) {
  503. case('0'): break;
  504. case('i'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  505. case('I'): dz->is_active[m] = (char)1; dz->is_int[m] = (char)1; break;
  506. case('d'): dz->is_active[m] = (char)1; dz->no_brk[m] = (char)1; break;
  507. case('D'): dz->is_active[m] = (char)1; /* normal case: double val or brkpnt file */ break;
  508. default:
  509. sprintf(errstr,"Programming error: invalid variant type in mark_parameter_types()\n");
  510. return(PROGRAM_ERROR);
  511. }
  512. } /* INTERNAL */
  513. for(n=0,
  514. m=ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt; n<ap->internal_param_cnt; n++,m++) {
  515. switch(ap->internal_param_list[n]) {
  516. case('0'): break; /* dummy variables: variables not used: but important for internal paream numbering!! */
  517. case('i'): dz->is_int[m] = (char)1; dz->no_brk[m] = (char)1; break;
  518. case('d'): dz->no_brk[m] = (char)1; break;
  519. default:
  520. sprintf(errstr,"Programming error: invalid internal param type in mark_parameter_types()\n");
  521. return(PROGRAM_ERROR);
  522. }
  523. }
  524. return(FINISHED);
  525. }
  526. /************************ HANDLE_THE_OUTFILE *********************/
  527. int handle_the_outfile(int *cmdlinecnt,char ***cmdline,dataptr dz)
  528. {
  529. int exit_status;
  530. char *filename = (*cmdline)[0];
  531. if(filename[0]=='-' && filename[1]=='f') {
  532. dz->floatsam_output = 1;
  533. dz->true_outfile_stype = SAMP_FLOAT;
  534. filename+= 2;
  535. }
  536. if(!sloom) {
  537. if(file_has_invalid_startchar(filename) || value_is_numeric(filename)) {
  538. sprintf(errstr,"Outfile name %s has invalid start character(s) or looks too much like a number.\n",filename);
  539. return(DATA_ERROR);
  540. }
  541. }
  542. strcpy(dz->outfilename,filename);
  543. if(dz->process != PSOW_LOCATE) {
  544. if(dz->process == PSOW_SPACE)
  545. dz->infile->channels = 2;
  546. if((exit_status = create_sized_outfile(filename,dz))<0)
  547. return(exit_status);
  548. if(dz->process == PSOW_SPACE)
  549. dz->infile->channels = 1;
  550. if(dz->process == PSOW_CHOP) {
  551. dz->all_words = 0;
  552. if((exit_status = store_filename(filename,dz))<0)
  553. return(exit_status);
  554. }
  555. }
  556. (*cmdline)++;
  557. (*cmdlinecnt)--;
  558. return(FINISHED);
  559. }
  560. /***************************** ESTABLISH_APPLICATION **************************/
  561. int establish_application(dataptr dz)
  562. {
  563. aplptr ap;
  564. if((dz->application = (aplptr)malloc(sizeof (struct applic)))==NULL) {
  565. sprintf(errstr,"establish_application()\n");
  566. return(MEMORY_ERROR);
  567. }
  568. ap = dz->application;
  569. memset((char *)ap,0,sizeof(struct applic));
  570. return(FINISHED);
  571. }
  572. /************************* INITIALISE_VFLAGS *************************/
  573. int initialise_vflags(dataptr dz)
  574. {
  575. int n;
  576. if((dz->vflag = (char *)malloc(dz->application->vflag_cnt * sizeof(char)))==NULL) {
  577. sprintf(errstr,"INSUFFICIENT MEMORY: vflag store,\n");
  578. return(MEMORY_ERROR);
  579. }
  580. for(n=0;n<dz->application->vflag_cnt;n++)
  581. dz->vflag[n] = FALSE;
  582. return FINISHED;
  583. }
  584. /************************* SETUP_INPUT_PARAM_DEFAULTVALS *************************/
  585. int setup_input_param_defaultval_stores(int tipc,aplptr ap)
  586. {
  587. int n;
  588. if((ap->default_val = (double *)malloc(tipc * sizeof(double)))==NULL) {
  589. sprintf(errstr,"INSUFFICIENT MEMORY for application default values store\n");
  590. return(MEMORY_ERROR);
  591. }
  592. for(n=0;n<tipc;n++)
  593. ap->default_val[n] = 0.0;
  594. return(FINISHED);
  595. }
  596. /***************************** SETUP_AND_INIT_INPUT_PARAM_ACTIVITY **************************/
  597. int setup_and_init_input_param_activity(dataptr dz,int tipc)
  598. {
  599. int n;
  600. if((dz->is_active = (char *)malloc((size_t)tipc))==NULL) {
  601. sprintf(errstr,"setup_and_init_input_param_activity()\n");
  602. return(MEMORY_ERROR);
  603. }
  604. for(n=0;n<tipc;n++)
  605. dz->is_active[n] = (char)0;
  606. return(FINISHED);
  607. }
  608. /************************* SETUP_PSOW_APPLICATION *******************/
  609. int setup_psow_application(dataptr dz)
  610. {
  611. int exit_status;
  612. aplptr ap;
  613. if((exit_status = establish_application(dz))<0) // GLOBAL
  614. return(FAILED);
  615. ap = dz->application;
  616. // SEE parstruct FOR EXPLANATION of next 2 functions
  617. switch(dz->process) {
  618. case(PSOW_STRETCH):
  619. if((exit_status = set_param_data(ap,0 ,3,3,"DDi" ))<0)
  620. return(FAILED);
  621. if((exit_status = set_vflgs(ap,"",0,"","",0,0,""))<0)
  622. return(FAILED);
  623. break;
  624. case(PSOW_DUPL):
  625. if((exit_status = set_param_data(ap,0 ,3,3,"DIi" ))<0)
  626. return(FAILED);
  627. if((exit_status = set_vflgs(ap,"",0,"","",0,0,""))<0)
  628. return(FAILED);
  629. break;
  630. case(PSOW_DEL):
  631. if((exit_status = set_param_data(ap,0 ,3,3,"DIi" ))<0)
  632. return(FAILED);
  633. if((exit_status = set_vflgs(ap,"",0,"","",0,0,""))<0)
  634. return(FAILED);
  635. break;
  636. case(PSOW_STRFILL):
  637. if((exit_status = set_param_data(ap,0 ,4,4,"DDiD" ))<0)
  638. return(FAILED);
  639. if((exit_status = set_vflgs(ap,"",0,"","",0,0,""))<0)
  640. return(FAILED);
  641. break;
  642. case(PSOW_FREEZE):
  643. if((exit_status = set_param_data(ap,0 ,8,8,"DddiDDDd" ))<0)
  644. return(FAILED);
  645. if((exit_status = set_vflgs(ap,"",0,"","",0,0,""))<0)
  646. return(FAILED);
  647. break;
  648. case(PSOW_CHOP):
  649. if((exit_status = set_param_data(ap,0 ,2,2,"DD" ))<0)
  650. return(FAILED);
  651. if((exit_status = set_vflgs(ap,"",0,"","",0,0,""))<0)
  652. return(FAILED);
  653. break;
  654. case(PSOW_INTERP):
  655. if((exit_status = set_param_data(ap,0 ,7,7,"dddDDDD"))<0)
  656. return(FAILED);
  657. if((exit_status = set_vflgs(ap,"",0,"","",0,0,""))<0)
  658. return(FAILED);
  659. break;
  660. case(PSOW_FEATURES):
  661. if((exit_status = set_param_data(ap,0 ,11,11,"DiDDDDDdIDI"))<0)
  662. return(FAILED);
  663. if((exit_status = set_vflgs(ap,"",0,"","a",1,0,"0"))<0)
  664. return(FAILED);
  665. break;
  666. case(PSOW_SYNTH):
  667. switch(dz->mode) {
  668. case(0):
  669. case(1):
  670. exit_status = set_param_data(ap,SYNTHBANK ,2,2, "DD");
  671. break;
  672. case(2):
  673. case(3):
  674. exit_status = set_param_data(ap,TIMEVARYING_SYNTHBANK ,2,2, "DD");
  675. break;
  676. case(4):
  677. exit_status = set_param_data(ap,0 ,2,2, "DD");
  678. break;
  679. }
  680. if(exit_status < 0)
  681. return(FAILED);
  682. if((exit_status = set_vflgs(ap,"",0,"","",0,0,"0"))<0)
  683. return(FAILED);
  684. break;
  685. case(PSOW_IMPOSE):
  686. if((exit_status = set_param_data(ap,0 ,4,4,"DDdd"))<0)
  687. return(FAILED);
  688. if((exit_status = set_vflgs(ap,"",0,"","",0,0,"0"))<0)
  689. return(FAILED);
  690. break;
  691. case(PSOW_SPLIT):
  692. if((exit_status = set_param_data(ap,0 ,4,4,"Didd"))<0)
  693. return(FAILED);
  694. if((exit_status = set_vflgs(ap,"",0,"","",0,0,"0"))<0)
  695. return(FAILED);
  696. break;
  697. case(PSOW_SPACE):
  698. if((exit_status = set_param_data(ap,0 ,5,5,"DiDDD"))<0)
  699. return(FAILED);
  700. if((exit_status = set_vflgs(ap,"",0,"","",0,0,"0"))<0)
  701. return(FAILED);
  702. break;
  703. case(PSOW_INTERLEAVE):
  704. if((exit_status = set_param_data(ap,0 ,6,6,"DDiDDD"))<0)
  705. return(FAILED);
  706. if((exit_status = set_vflgs(ap,"",0,"","",0,0,"0"))<0)
  707. return(FAILED);
  708. break;
  709. case(PSOW_REPLACE):
  710. if((exit_status = set_param_data(ap,0 ,3,3,"DDi"))<0)
  711. return(FAILED);
  712. if((exit_status = set_vflgs(ap,"",0,"","",0,0,"0"))<0)
  713. return(FAILED);
  714. break;
  715. case(PSOW_EXTEND):
  716. if((exit_status = set_param_data(ap,0 ,8,8,"DddiDDDD"))<0)
  717. return(FAILED);
  718. if((exit_status = set_vflgs(ap,"",0,"","s",1,0,"0"))<0)
  719. return(FAILED);
  720. break;
  721. case(PSOW_EXTEND2):
  722. if((exit_status = set_param_data(ap,0 ,6,6,"iidDDi"))<0)
  723. return(FAILED);
  724. if((exit_status = set_vflgs(ap,"",0,"","",0,0,"0"))<0)
  725. return(FAILED);
  726. break;
  727. case(PSOW_LOCATE):
  728. if((exit_status = set_param_data(ap,0 ,2,2,"Dd"))<0)
  729. return(FAILED);
  730. if((exit_status = set_vflgs(ap,"",0,"","",0,0,"0"))<0)
  731. return(FAILED);
  732. break;
  733. case(PSOW_CUT):
  734. if((exit_status = set_param_data(ap,0 ,2,2,"Dd"))<0)
  735. return(FAILED);
  736. if((exit_status = set_vflgs(ap,"",0,"","",0,0,"0"))<0)
  737. return(FAILED);
  738. break;
  739. case(PSOW_REINF):
  740. switch(dz->mode) {
  741. case(0):
  742. if((exit_status = set_param_data(ap,PSOW_REINFORCEMENT ,1,1,"D"))<0)
  743. return(FAILED);
  744. if((exit_status = set_vflgs(ap,"",0,"","ds",2,1,"d0"))<0)
  745. return(FAILED);
  746. break;
  747. case(1):
  748. if((exit_status = set_param_data(ap,PSOW_INHARMONICS ,1,1,"D"))<0)
  749. return(FAILED);
  750. if((exit_status = set_vflgs(ap,"",0,"","w",1,1,"d"))<0)
  751. return(FAILED);
  752. break;
  753. }
  754. break;
  755. }
  756. // set_legal_infile_structure -->
  757. dz->has_otherfile = FALSE;
  758. // assign_process_logic -->
  759. switch(dz->process) {
  760. case(PSOW_INTERP):
  761. case(PSOW_IMPOSE):
  762. case(PSOW_INTERLEAVE):
  763. case(PSOW_REPLACE):
  764. dz->input_data_type = TWO_SNDFILES;
  765. break;
  766. default:
  767. dz->input_data_type = SNDFILES_ONLY;
  768. break;
  769. }
  770. if(dz->process == PSOW_LOCATE) {
  771. dz->process_type = OTHER_PROCESS;
  772. dz->outfiletype = NO_OUTPUTFILE;
  773. } else {
  774. dz->process_type = UNEQUAL_SNDFILE;
  775. dz->outfiletype = SNDFILE_OUT;
  776. }
  777. return application_init(dz); //GLOBAL
  778. }
  779. /******************************** SETUP_AND_INIT_INPUT_BRKTABLE_CONSTANTS ********************************/
  780. int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt)
  781. {
  782. int n;
  783. if((dz->brk = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  784. sprintf(errstr,"setup_and_init_input_brktable_constants(): 1\n");
  785. return(MEMORY_ERROR);
  786. }
  787. if((dz->brkptr = (double **)malloc(brkcnt * sizeof(double *)))==NULL) {
  788. sprintf(errstr,"setup_and_init_input_brktable_constants(): 6\n");
  789. return(MEMORY_ERROR);
  790. }
  791. if((dz->brksize = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  792. sprintf(errstr,"setup_and_init_input_brktable_constants(): 2\n");
  793. return(MEMORY_ERROR);
  794. }
  795. if((dz->firstval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  796. sprintf(errstr,"setup_and_init_input_brktable_constants(): 3\n");
  797. return(MEMORY_ERROR);
  798. }
  799. if((dz->lastind = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  800. sprintf(errstr,"setup_and_init_input_brktable_constants(): 4\n");
  801. return(MEMORY_ERROR);
  802. }
  803. if((dz->lastval = (double *)malloc(brkcnt * sizeof(double)))==NULL) {
  804. sprintf(errstr,"setup_and_init_input_brktable_constants(): 5\n");
  805. return(MEMORY_ERROR);
  806. }
  807. if((dz->brkinit = (int *)malloc(brkcnt * sizeof(int)))==NULL) {
  808. sprintf(errstr,"setup_and_init_input_brktable_constants(): 7\n");
  809. return(MEMORY_ERROR);
  810. }
  811. for(n=0;n<brkcnt;n++) {
  812. dz->brk[n] = NULL;
  813. dz->brkptr[n] = NULL;
  814. dz->brkinit[n] = 0;
  815. dz->brksize[n] = 0;
  816. }
  817. return(FINISHED);
  818. }
  819. /************************* PARSE_INFILE_AND_CHECK_TYPE *******************/
  820. int parse_infile_and_check_type(char **cmdline,dataptr dz)
  821. {
  822. int exit_status;
  823. infileptr infile_info;
  824. if(!sloom) {
  825. if((infile_info = (infileptr)malloc(sizeof(struct filedata)))==NULL) {
  826. sprintf(errstr,"INSUFFICIENT MEMORY for infile structure to test file data.");
  827. return(MEMORY_ERROR);
  828. } else if((exit_status = cdparse(cmdline[0],infile_info))<0) {
  829. sprintf(errstr,"Failed to parse input file %s\n",cmdline[0]);
  830. return(PROGRAM_ERROR);
  831. } else if(infile_info->filetype != SNDFILE) {
  832. sprintf(errstr,"File %s is not of correct type\n",cmdline[0]);
  833. return(DATA_ERROR);
  834. } else if(infile_info->channels != 1) {
  835. sprintf(errstr,"File %s is not of correct type (must be mono)\n",cmdline[0]);
  836. return(DATA_ERROR);
  837. } else if((exit_status = copy_parse_info_to_main_structure(infile_info,dz))<0) {
  838. sprintf(errstr,"Failed to copy file parsing information\n");
  839. return(PROGRAM_ERROR);
  840. }
  841. free(infile_info);
  842. }
  843. return(FINISHED);
  844. }
  845. /************************* SETUP_PSOW_PARAM_RANGES_AND_DEFAULTS *******************/
  846. int setup_psow_param_ranges_and_defaults(dataptr dz)
  847. {
  848. int exit_status;
  849. aplptr ap = dz->application;
  850. // set_param_ranges()
  851. ap->total_input_param_cnt = (char)(ap->max_param_cnt + ap->option_cnt + ap->variant_param_cnt);
  852. // NB total_input_param_cnt is > 0 !!!s
  853. if((exit_status = setup_input_param_range_stores(ap->total_input_param_cnt,ap))<0)
  854. return(FAILED);
  855. // get_param_ranges()
  856. ap->lo[0] = -2.0;
  857. ap->hi[0] = dz->nyquist;
  858. ap->default_val[0] = 440.0;
  859. switch(dz->process) {
  860. case(PSOW_STRETCH):
  861. ap->lo[1] = 0.1;
  862. ap->hi[1] = 10.00;
  863. ap->default_val[1] = 1.0;
  864. ap->lo[2] = 1;
  865. ap->hi[2] = 256;
  866. ap->default_val[2] = 1;
  867. dz->maxmode = 0;
  868. break;
  869. case(PSOW_DUPL):
  870. ap->lo[1] = 2;
  871. ap->hi[1] = 256;
  872. ap->default_val[1] = 2;
  873. ap->lo[2] = 1;
  874. ap->hi[2] = 256;
  875. ap->default_val[2] = 1;
  876. dz->maxmode = 0;
  877. break;
  878. case(PSOW_DEL):
  879. ap->lo[1] = 2;
  880. ap->hi[1] = 20;
  881. ap->default_val[1] = 2;
  882. ap->lo[2] = 1;
  883. ap->hi[2] = 256;
  884. ap->default_val[2] = 1;
  885. dz->maxmode = 0;
  886. break;
  887. case(PSOW_STRFILL):
  888. ap->lo[1] = 1.0;
  889. ap->hi[1] = 10.00;
  890. ap->default_val[1] = 1.0;
  891. ap->lo[2] = 1;
  892. ap->hi[2] = 32767;
  893. ap->default_val[2] = 1;
  894. ap->lo[3] = -24;
  895. ap->hi[3] = 24;
  896. ap->default_val[3] = 0;
  897. dz->maxmode = 0;
  898. break;
  899. case(PSOW_FREEZE):
  900. ap->lo[PS_TIME] = 0.0;
  901. ap->hi[PS_TIME] = dz->duration;
  902. ap->default_val[PS_TIME] = dz->duration/2.0;
  903. ap->lo[PS_DUR] = 0.0;
  904. ap->hi[PS_DUR] = 32767.0;
  905. ap->default_val[PS_DUR] = 1.0;
  906. ap->lo[PS_SEGS] = 1;
  907. ap->hi[PS_SEGS] = 256;
  908. ap->default_val[PS_SEGS] = 1;
  909. ap->lo[PS_DENS] = .001;
  910. ap->hi[PS_DENS] = 100;
  911. ap->default_val[PS_DENS] = 1.0;
  912. ap->lo[PS_TRNS] = .125;
  913. ap->hi[PS_TRNS] = 8.0;
  914. ap->default_val[PS_TRNS] = 1.0;
  915. ap->lo[PS_RAND] = 0.0;
  916. ap->hi[PS_RAND] = 1.0;
  917. ap->default_val[PS_RAND] = 0.0;
  918. ap->lo[PS_GAIN] = 0.0;
  919. ap->hi[PS_GAIN] = 1.0;
  920. ap->default_val[PS_GAIN] = 1.0;
  921. dz->maxmode = 0;
  922. break;
  923. case(PSOW_CHOP):
  924. ap->lo[1] = 1.0;
  925. ap->hi[1] = dz->insams[0];
  926. ap->default_val[1] = 1.0;
  927. dz->maxmode = 0;
  928. break;
  929. case(PSOW_INTERP):
  930. ap->lo[PS_SDUR] = 0.0;
  931. ap->hi[PS_SDUR] = 32767.0;
  932. ap->default_val[PS_SDUR] = 1.0;
  933. ap->lo[PS_IDUR] = 0.0;
  934. ap->hi[PS_IDUR] = 32767.0;
  935. ap->default_val[PS_IDUR] = 1.0;
  936. ap->lo[PS_EDUR] = 0.0;
  937. ap->hi[PS_EDUR] = 32767.0;
  938. ap->default_val[PS_EDUR] = 1.0;
  939. ap->lo[PS_VIBFRQ] = 0.0;
  940. ap->hi[PS_VIBFRQ] = 20;
  941. ap->default_val[PS_VIBFRQ] = 6.5;
  942. ap->lo[PS_VIBDEPTH] = 0.0;
  943. ap->hi[PS_VIBDEPTH] = 3.0;
  944. ap->default_val[PS_VIBDEPTH] = 0.0;
  945. ap->lo[PS_TREMFRQ] = 0.0;
  946. ap->hi[PS_TREMFRQ] = 30.0;
  947. ap->default_val[PS_TREMFRQ] = 8.34712;
  948. ap->lo[PS_TREMDEPTH] = 0;
  949. ap->hi[PS_TREMDEPTH] = 10.0;
  950. ap->default_val[PS_TREMDEPTH] = 0.2;
  951. dz->maxmode = 0;
  952. break;
  953. case(PSOW_FEATURES):
  954. ap->lo[PSF_SEGS] = 1;
  955. ap->hi[PSF_SEGS] = 256;
  956. ap->default_val[PSF_SEGS] = 1;
  957. ap->lo[PSF_TRNS] = -48;
  958. ap->hi[PSF_TRNS] = 96;
  959. ap->default_val[PSF_TRNS] = 0;
  960. ap->lo[PS_VIBFRQ] = 0.0;
  961. ap->hi[PS_VIBFRQ] = 20;
  962. ap->default_val[PS_VIBFRQ] = 6.5;
  963. ap->lo[PS_VIBDEPTH] = 0.0;
  964. ap->hi[PS_VIBDEPTH] = 3.0;
  965. ap->default_val[PS_VIBDEPTH] = 0.0;
  966. ap->lo[PSF_SPEC] = -24;
  967. ap->hi[PSF_SPEC] = 24;
  968. ap->default_val[PSF_SPEC] = 0;
  969. ap->lo[PSF_RAND] = 0.0;
  970. ap->hi[PSF_RAND] = 1.0;
  971. ap->default_val[PSF_RAND] = 0.0;
  972. ap->lo[PSF_GAIN] = 0.0;
  973. ap->hi[PSF_GAIN] = 1.0;
  974. ap->default_val[PSF_GAIN] = 1.0;
  975. ap->lo[PSF_SUB] = 0;
  976. ap->hi[PSF_SUB] = 8;
  977. ap->default_val[PSF_SUB] = 0;
  978. ap->lo[PSF_SUBGAIN] = 0;
  979. ap->hi[PSF_SUBGAIN] = 1;
  980. ap->default_val[PSF_SUBGAIN] = 0.5;
  981. ap->lo[PSF_FOFSTR] = 1.0;
  982. ap->hi[PSF_FOFSTR] = 4096;
  983. ap->default_val[PSF_FOFSTR] = 1.0;
  984. dz->maxmode = 2;
  985. break;
  986. case(PSOW_SYNTH):
  987. ap->lo[PS_DEPTH] = 0.0; /* depth */
  988. ap->hi[PS_DEPTH] = 1.0;
  989. ap->default_val[PS_DEPTH] = 1.0;
  990. dz->maxmode = 5;
  991. break;
  992. case(PSOW_IMPOSE):
  993. ap->lo[PS_DEPTH] = 0.0; /* depth */
  994. ap->hi[PS_DEPTH] = 1.0;
  995. ap->default_val[PS_DEPTH] = 1.0;
  996. ap->lo[PS_WSIZE] = 1.0; /* windowsize (ms) */
  997. ap->hi[PS_WSIZE] = 200.0;
  998. ap->default_val[PS_WSIZE] = 50.0;
  999. ap->lo[PS_GATE] = -96.0; /* gate (db) */
  1000. ap->hi[PS_GATE] = 0.0;
  1001. ap->default_val[PS_GATE] = -60.0;
  1002. dz->maxmode = 0;
  1003. break;
  1004. case(PSOW_SPLIT):
  1005. ap->lo[PS_SUBNO] = 3.0; /* suharm no */
  1006. ap->hi[PS_SUBNO] = 8.0;
  1007. ap->default_val[PS_SUBNO] = 3.0;
  1008. ap->lo[PS_UTRNS] = 0.0; /* up transpos (semitones) */
  1009. ap->hi[PS_UTRNS] = 48.0;
  1010. ap->default_val[PS_UTRNS] = 0.0;
  1011. ap->lo[PS_ATTEN] = 0.0; /* relative level */
  1012. ap->hi[PS_ATTEN] = 8.0;
  1013. ap->default_val[PS_ATTEN] = 1.0;
  1014. dz->maxmode = 0;
  1015. break;
  1016. case(PSOW_SPACE):
  1017. ap->lo[PS_SUBNO] = 2.0; /* suharm no */
  1018. ap->hi[PS_SUBNO] = 5.0;
  1019. ap->default_val[PS_SUBNO] = 2.0;
  1020. ap->lo[PS_SEPAR] = -1.0; /* relative level */
  1021. ap->hi[PS_SEPAR] = 1.0;
  1022. ap->default_val[PS_SEPAR] = 0.0;
  1023. ap->lo[PS_RELEV] = 0.001; /* LR relative level */
  1024. ap->hi[PS_RELEV] = 1000.0;
  1025. ap->default_val[PS_RELEV] = 1.0;
  1026. ap->lo[PS_RELV2] = 0.0; /* HI frq suppression */
  1027. ap->hi[PS_RELV2] = 1.0;
  1028. ap->default_val[PS_RELV2] = 0.0;
  1029. dz->maxmode = 0;
  1030. break;
  1031. case(PSOW_INTERLEAVE):
  1032. ap->lo[1] = -2.0; /* pitchdata for file 2 */
  1033. ap->hi[1] = dz->nyquist;
  1034. ap->default_val[1] = 440.0;
  1035. ap->lo[PS_GCNT] = 1.0; /* fofs per bunch */
  1036. ap->hi[PS_GCNT] = 16.0;
  1037. ap->default_val[PS_GCNT] = 1.0;
  1038. ap->lo[PS_BIAS] = -1; /* bias to pitch of one or other */
  1039. ap->hi[PS_BIAS] = 1;
  1040. ap->default_val[PS_BIAS] = 0;
  1041. ap->lo[PS_RELV2] = 0.0001; /* relative level */
  1042. ap->hi[PS_RELV2] = 10000.0;
  1043. ap->default_val[PS_RELV2] = 1.0;
  1044. ap->lo[PS_WEIGHT] = 0.0625; /* weighting to one sound or other */
  1045. ap->hi[PS_WEIGHT] = 16.0;
  1046. ap->default_val[PS_WEIGHT] = 1.0;
  1047. dz->maxmode = 0;
  1048. break;
  1049. case(PSOW_REPLACE):
  1050. ap->lo[1] = -2.0; /* pitchdata for file 2 */
  1051. ap->hi[1] = dz->nyquist;
  1052. ap->default_val[1] = 449.0;
  1053. ap->lo[2] = 1; /* fofs per bunch */
  1054. ap->hi[2] = 16;
  1055. ap->default_val[2] = 1.0;
  1056. dz->maxmode = 0;
  1057. break;
  1058. case(PSOW_EXTEND):
  1059. ap->default_val[0] = 440.0; /* pitch */
  1060. ap->lo[PS_TIME] = 0.0; /* grabtime */
  1061. ap->hi[PS_TIME] = dz->duration;
  1062. ap->default_val[PS_TIME] = 0.0; /* grabtime */
  1063. ap->lo[PS_DUR] = dz->duration; /* TOTAL duration of output */
  1064. ap->hi[PS_DUR] = 32767.0;
  1065. ap->default_val[PS_DUR] = dz->duration + 1.0;
  1066. ap->lo[PS_SEGS] = 1;
  1067. ap->hi[PS_SEGS] = 256;
  1068. ap->default_val[PS_SEGS] = 1;
  1069. ap->lo[PSE_VFRQ] = 0.0;
  1070. ap->hi[PSE_VFRQ] = 20;
  1071. ap->default_val[PSE_VFRQ] = 6.5;
  1072. ap->lo[PSE_VDEP] = 0.0;
  1073. ap->hi[PSE_VDEP] = 3.0;
  1074. ap->default_val[PSE_VDEP] = 0.0;
  1075. ap->lo[PSE_TRNS] = -48.0;
  1076. ap->hi[PSE_TRNS] = 24.0;
  1077. ap->default_val[PSE_TRNS] = 0.0;
  1078. ap->lo[PSE_GAIN] = 0.0;
  1079. ap->hi[PSE_GAIN] = 10.0;
  1080. ap->default_val[PSE_GAIN] = 1.0;
  1081. dz->maxmode = 0;
  1082. break;
  1083. case(PSOW_EXTEND2):
  1084. ap->lo[0] = 0;
  1085. ap->hi[0] = dz->duration;
  1086. ap->default_val[0] = 0.0;
  1087. ap->lo[1] = 0;
  1088. ap->hi[1] = dz->duration;
  1089. ap->default_val[1] = 0.0;
  1090. ap->lo[PS_DUR] = dz->duration; /* TOTAL duration of output */
  1091. ap->hi[PS_DUR] = 32767.0;
  1092. ap->default_val[PS_DUR] = dz->duration + 1.0;
  1093. ap->lo[PS2_VFRQ] = 0.0;
  1094. ap->hi[PS2_VFRQ] = 20;
  1095. ap->default_val[PS2_VFRQ] = 6.5;
  1096. ap->lo[PS2_VDEP] = 0.0;
  1097. ap->hi[PS2_VDEP] = 3.0;
  1098. ap->default_val[PS2_VDEP] = 0.0;
  1099. ap->lo[PS2_NUJ] = -24.0;
  1100. ap->hi[PS2_NUJ] = 24.0;
  1101. ap->default_val[PS2_NUJ] = 0;
  1102. dz->maxmode = 0;
  1103. break;
  1104. case(PSOW_LOCATE):
  1105. ap->lo[1] = 0.0; /* time */
  1106. ap->hi[1] = dz->duration;
  1107. ap->default_val[1] = 0.0;
  1108. dz->maxmode = 0;
  1109. break;
  1110. case(PSOW_CUT):
  1111. ap->lo[1] = 0.0; /* time */
  1112. ap->hi[1] = dz->duration;
  1113. ap->default_val[1] = 0.0;
  1114. dz->maxmode = 2;
  1115. break;
  1116. case(PSOW_REINF):
  1117. dz->maxmode = 2;
  1118. if(dz->mode == 0) {
  1119. ap->lo[ISTR] = 0;
  1120. ap->hi[ISTR] = 1000;
  1121. ap->default_val[ISTR] = 0;
  1122. } else {
  1123. ap->lo[ISTR] = 1.0;
  1124. ap->hi[ISTR] = 256.0;
  1125. ap->default_val[ISTR] = 4.0;
  1126. }
  1127. break;
  1128. }
  1129. if(!sloom)
  1130. put_default_vals_in_all_params(dz);
  1131. return(FINISHED);
  1132. }
  1133. /********************************* PARSE_SLOOM_DATA *********************************/
  1134. int parse_sloom_data(int argc,char *argv[],char ***cmdline,int *cmdlinecnt,dataptr dz)
  1135. {
  1136. int exit_status;
  1137. int cnt = 1, infilecnt;
  1138. int filesize, insams, inbrksize;
  1139. double dummy;
  1140. int true_cnt = 0;
  1141. //aplptr ap;
  1142. while(cnt<=PRE_CMDLINE_DATACNT) {
  1143. if(cnt > argc) {
  1144. sprintf(errstr,"Insufficient data sent from TK\n");
  1145. return(DATA_ERROR);
  1146. }
  1147. switch(cnt) {
  1148. case(1):
  1149. if(sscanf(argv[cnt],"%d",&dz->process)!=1) {
  1150. sprintf(errstr,"Cannot read process no. sent from TK\n");
  1151. return(DATA_ERROR);
  1152. }
  1153. break;
  1154. case(2):
  1155. if(sscanf(argv[cnt],"%d",&dz->mode)!=1) {
  1156. sprintf(errstr,"Cannot read mode no. sent from TK\n");
  1157. return(DATA_ERROR);
  1158. }
  1159. if(dz->mode > 0)
  1160. dz->mode--;
  1161. //setup_particular_application() =
  1162. if((exit_status = setup_psow_application(dz))<0)
  1163. return(exit_status);
  1164. //ap = dz->application;
  1165. break;
  1166. case(3):
  1167. if(sscanf(argv[cnt],"%d",&infilecnt)!=1) {
  1168. sprintf(errstr,"Cannot read infilecnt sent from TK\n");
  1169. return(DATA_ERROR);
  1170. }
  1171. if(infilecnt < 1) {
  1172. true_cnt = cnt + 1;
  1173. cnt = PRE_CMDLINE_DATACNT; /* force exit from loop after assign_file_data_storage */
  1174. }
  1175. if((exit_status = assign_file_data_storage(infilecnt,dz))<0)
  1176. return(exit_status);
  1177. break;
  1178. case(INPUT_FILETYPE+4):
  1179. if(sscanf(argv[cnt],"%d",&dz->infile->filetype)!=1) {
  1180. sprintf(errstr,"Cannot read filetype sent from TK (%s)\n",argv[cnt]);
  1181. return(DATA_ERROR);
  1182. }
  1183. break;
  1184. case(INPUT_FILESIZE+4):
  1185. if(sscanf(argv[cnt],"%d",&filesize)!=1) {
  1186. sprintf(errstr,"Cannot read infilesize sent from TK\n");
  1187. return(DATA_ERROR);
  1188. }
  1189. dz->insams[0] = filesize;
  1190. break;
  1191. case(INPUT_INSAMS+4):
  1192. if(sscanf(argv[cnt],"%d",&insams)!=1) {
  1193. sprintf(errstr,"Cannot read insams sent from TK\n");
  1194. return(DATA_ERROR);
  1195. }
  1196. dz->insams[0] = insams;
  1197. break;
  1198. case(INPUT_SRATE+4):
  1199. if(sscanf(argv[cnt],"%d",&dz->infile->srate)!=1) {
  1200. sprintf(errstr,"Cannot read srate sent from TK\n");
  1201. return(DATA_ERROR);
  1202. }
  1203. break;
  1204. case(INPUT_CHANNELS+4):
  1205. if(sscanf(argv[cnt],"%d",&dz->infile->channels)!=1) {
  1206. sprintf(errstr,"Cannot read channels sent from TK\n");
  1207. return(DATA_ERROR);
  1208. }
  1209. break;
  1210. case(INPUT_STYPE+4):
  1211. if(sscanf(argv[cnt],"%d",&dz->infile->stype)!=1) {
  1212. sprintf(errstr,"Cannot read stype sent from TK\n");
  1213. return(DATA_ERROR);
  1214. }
  1215. break;
  1216. case(INPUT_ORIGSTYPE+4):
  1217. if(sscanf(argv[cnt],"%d",&dz->infile->origstype)!=1) {
  1218. sprintf(errstr,"Cannot read origstype sent from TK\n");
  1219. return(DATA_ERROR);
  1220. }
  1221. break;
  1222. case(INPUT_ORIGRATE+4):
  1223. if(sscanf(argv[cnt],"%d",&dz->infile->origrate)!=1) {
  1224. sprintf(errstr,"Cannot read origrate sent from TK\n");
  1225. return(DATA_ERROR);
  1226. }
  1227. break;
  1228. case(INPUT_MLEN+4):
  1229. if(sscanf(argv[cnt],"%d",&dz->infile->Mlen)!=1) {
  1230. sprintf(errstr,"Cannot read Mlen sent from TK\n");
  1231. return(DATA_ERROR);
  1232. }
  1233. break;
  1234. case(INPUT_DFAC+4):
  1235. if(sscanf(argv[cnt],"%d",&dz->infile->Dfac)!=1) {
  1236. sprintf(errstr,"Cannot read Dfac sent from TK\n");
  1237. return(DATA_ERROR);
  1238. }
  1239. break;
  1240. case(INPUT_ORIGCHANS+4):
  1241. if(sscanf(argv[cnt],"%d",&dz->infile->origchans)!=1) {
  1242. sprintf(errstr,"Cannot read origchans sent from TK\n");
  1243. return(DATA_ERROR);
  1244. }
  1245. break;
  1246. case(INPUT_SPECENVCNT+4):
  1247. if(sscanf(argv[cnt],"%d",&dz->infile->specenvcnt)!=1) {
  1248. sprintf(errstr,"Cannot read specenvcnt sent from TK\n");
  1249. return(DATA_ERROR);
  1250. }
  1251. dz->specenvcnt = dz->infile->specenvcnt;
  1252. break;
  1253. case(INPUT_WANTED+4):
  1254. if(sscanf(argv[cnt],"%d",&dz->wanted)!=1) {
  1255. sprintf(errstr,"Cannot read wanted sent from TK\n");
  1256. return(DATA_ERROR);
  1257. }
  1258. break;
  1259. case(INPUT_WLENGTH+4):
  1260. if(sscanf(argv[cnt],"%d",&dz->wlength)!=1) {
  1261. sprintf(errstr,"Cannot read wlength sent from TK\n");
  1262. return(DATA_ERROR);
  1263. }
  1264. break;
  1265. case(INPUT_OUT_CHANS+4):
  1266. if(sscanf(argv[cnt],"%d",&dz->out_chans)!=1) {
  1267. sprintf(errstr,"Cannot read out_chans sent from TK\n");
  1268. return(DATA_ERROR);
  1269. }
  1270. break;
  1271. /* RWD these chanegs to samps - tk will have to deal with that! */
  1272. case(INPUT_DESCRIPTOR_BYTES+4):
  1273. if(sscanf(argv[cnt],"%d",&dz->descriptor_samps)!=1) {
  1274. sprintf(errstr,"Cannot read descriptor_samps sent from TK\n");
  1275. return(DATA_ERROR);
  1276. }
  1277. break;
  1278. case(INPUT_IS_TRANSPOS+4):
  1279. if(sscanf(argv[cnt],"%d",&dz->is_transpos)!=1) {
  1280. sprintf(errstr,"Cannot read is_transpos sent from TK\n");
  1281. return(DATA_ERROR);
  1282. }
  1283. break;
  1284. case(INPUT_COULD_BE_TRANSPOS+4):
  1285. if(sscanf(argv[cnt],"%d",&dz->could_be_transpos)!=1) {
  1286. sprintf(errstr,"Cannot read could_be_transpos sent from TK\n");
  1287. return(DATA_ERROR);
  1288. }
  1289. break;
  1290. case(INPUT_COULD_BE_PITCH+4):
  1291. if(sscanf(argv[cnt],"%d",&dz->could_be_pitch)!=1) {
  1292. sprintf(errstr,"Cannot read could_be_pitch sent from TK\n");
  1293. return(DATA_ERROR);
  1294. }
  1295. break;
  1296. case(INPUT_DIFFERENT_SRATES+4):
  1297. if(sscanf(argv[cnt],"%d",&dz->different_srates)!=1) {
  1298. sprintf(errstr,"Cannot read different_srates sent from TK\n");
  1299. return(DATA_ERROR);
  1300. }
  1301. break;
  1302. case(INPUT_DUPLICATE_SNDS+4):
  1303. if(sscanf(argv[cnt],"%d",&dz->duplicate_snds)!=1) {
  1304. sprintf(errstr,"Cannot read duplicate_snds sent from TK\n");
  1305. return(DATA_ERROR);
  1306. }
  1307. break;
  1308. case(INPUT_BRKSIZE+4):
  1309. if(sscanf(argv[cnt],"%d",&inbrksize)!=1) {
  1310. sprintf(errstr,"Cannot read brksize sent from TK\n");
  1311. return(DATA_ERROR);
  1312. }
  1313. if(inbrksize > 0) {
  1314. switch(dz->input_data_type) {
  1315. case(WORDLIST_ONLY):
  1316. break;
  1317. case(PITCH_AND_PITCH):
  1318. case(PITCH_AND_TRANSPOS):
  1319. case(TRANSPOS_AND_TRANSPOS):
  1320. dz->tempsize = inbrksize;
  1321. break;
  1322. case(BRKFILES_ONLY):
  1323. case(UNRANGED_BRKFILE_ONLY):
  1324. case(DB_BRKFILES_ONLY):
  1325. case(ALL_FILES):
  1326. case(ANY_NUMBER_OF_ANY_FILES):
  1327. if(dz->extrabrkno < 0) {
  1328. sprintf(errstr,"Storage location number for brktable not established by CDP.\n");
  1329. return(DATA_ERROR);
  1330. }
  1331. if(dz->brksize == NULL) {
  1332. sprintf(errstr,"CDP has not established storage space for input brktable.\n");
  1333. return(PROGRAM_ERROR);
  1334. }
  1335. dz->brksize[dz->extrabrkno] = inbrksize;
  1336. break;
  1337. default:
  1338. sprintf(errstr,"TK sent brktablesize > 0 for input_data_type [%d] not using brktables.\n",
  1339. dz->input_data_type);
  1340. return(PROGRAM_ERROR);
  1341. }
  1342. break;
  1343. }
  1344. break;
  1345. case(INPUT_NUMSIZE+4):
  1346. if(sscanf(argv[cnt],"%d",&dz->numsize)!=1) {
  1347. sprintf(errstr,"Cannot read numsize sent from TK\n");
  1348. return(DATA_ERROR);
  1349. }
  1350. break;
  1351. case(INPUT_LINECNT+4):
  1352. if(sscanf(argv[cnt],"%d",&dz->linecnt)!=1) {
  1353. sprintf(errstr,"Cannot read linecnt sent from TK\n");
  1354. return(DATA_ERROR);
  1355. }
  1356. break;
  1357. case(INPUT_ALL_WORDS+4):
  1358. if(sscanf(argv[cnt],"%d",&dz->all_words)!=1) {
  1359. sprintf(errstr,"Cannot read all_words sent from TK\n");
  1360. return(DATA_ERROR);
  1361. }
  1362. break;
  1363. case(INPUT_ARATE+4):
  1364. if(sscanf(argv[cnt],"%f",&dz->infile->arate)!=1) {
  1365. sprintf(errstr,"Cannot read arate sent from TK\n");
  1366. return(DATA_ERROR);
  1367. }
  1368. break;
  1369. case(INPUT_FRAMETIME+4):
  1370. if(sscanf(argv[cnt],"%lf",&dummy)!=1) {
  1371. sprintf(errstr,"Cannot read frametime sent from TK\n");
  1372. return(DATA_ERROR);
  1373. }
  1374. dz->frametime = (float)dummy;
  1375. break;
  1376. case(INPUT_WINDOW_SIZE+4):
  1377. if(sscanf(argv[cnt],"%f",&dz->infile->window_size)!=1) {
  1378. sprintf(errstr,"Cannot read window_size sent from TK\n");
  1379. return(DATA_ERROR);
  1380. }
  1381. break;
  1382. case(INPUT_NYQUIST+4):
  1383. if(sscanf(argv[cnt],"%lf",&dz->nyquist)!=1) {
  1384. sprintf(errstr,"Cannot read nyquist sent from TK\n");
  1385. return(DATA_ERROR);
  1386. }
  1387. break;
  1388. case(INPUT_DURATION+4):
  1389. if(sscanf(argv[cnt],"%lf",&dz->duration)!=1) {
  1390. sprintf(errstr,"Cannot read duration sent from TK\n");
  1391. return(DATA_ERROR);
  1392. }
  1393. break;
  1394. case(INPUT_MINBRK+4):
  1395. if(sscanf(argv[cnt],"%lf",&dz->minbrk)!=1) {
  1396. sprintf(errstr,"Cannot read minbrk sent from TK\n");
  1397. return(DATA_ERROR);
  1398. }
  1399. break;
  1400. case(INPUT_MAXBRK+4):
  1401. if(sscanf(argv[cnt],"%lf",&dz->maxbrk)!=1) {
  1402. sprintf(errstr,"Cannot read maxbrk sent from TK\n");
  1403. return(DATA_ERROR);
  1404. }
  1405. break;
  1406. case(INPUT_MINNUM+4):
  1407. if(sscanf(argv[cnt],"%lf",&dz->minnum)!=1) {
  1408. sprintf(errstr,"Cannot read minnum sent from TK\n");
  1409. return(DATA_ERROR);
  1410. }
  1411. break;
  1412. case(INPUT_MAXNUM+4):
  1413. if(sscanf(argv[cnt],"%lf",&dz->maxnum)!=1) {
  1414. sprintf(errstr,"Cannot read maxnum sent from TK\n");
  1415. return(DATA_ERROR);
  1416. }
  1417. break;
  1418. default:
  1419. sprintf(errstr,"case switch item missing: parse_sloom_data()\n");
  1420. return(PROGRAM_ERROR);
  1421. }
  1422. cnt++;
  1423. }
  1424. if(cnt!=PRE_CMDLINE_DATACNT+1) {
  1425. sprintf(errstr,"Insufficient pre-cmdline params sent from TK\n");
  1426. return(DATA_ERROR);
  1427. }
  1428. if(true_cnt)
  1429. cnt = true_cnt;
  1430. *cmdlinecnt = 0;
  1431. while(cnt < argc) {
  1432. if((exit_status = get_tk_cmdline_word(cmdlinecnt,cmdline,argv[cnt]))<0)
  1433. return(exit_status);
  1434. cnt++;
  1435. }
  1436. return(FINISHED);
  1437. }
  1438. /********************************* GET_TK_CMDLINE_WORD *********************************/
  1439. int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q)
  1440. {
  1441. if(*cmdlinecnt==0) {
  1442. if((*cmdline = (char **)malloc(sizeof(char *)))==NULL) {
  1443. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  1444. return(MEMORY_ERROR);
  1445. }
  1446. } else {
  1447. if((*cmdline = (char **)realloc(*cmdline,((*cmdlinecnt)+1) * sizeof(char *)))==NULL) {
  1448. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline array.\n");
  1449. return(MEMORY_ERROR);
  1450. }
  1451. }
  1452. if(((*cmdline)[*cmdlinecnt] = (char *)malloc((strlen(q) + 1) * sizeof(char)))==NULL) {
  1453. sprintf(errstr,"INSUFFICIENT MEMORY for TK cmdline item %d.\n",(*cmdlinecnt)+1);
  1454. return(MEMORY_ERROR);
  1455. }
  1456. strcpy((*cmdline)[*cmdlinecnt],q);
  1457. (*cmdlinecnt)++;
  1458. return(FINISHED);
  1459. }
  1460. /****************************** ASSIGN_FILE_DATA_STORAGE *********************************/
  1461. int assign_file_data_storage(int infilecnt,dataptr dz)
  1462. {
  1463. int exit_status;
  1464. int no_sndfile_system_files = FALSE;
  1465. dz->infilecnt = infilecnt;
  1466. if((exit_status = allocate_filespace(dz))<0)
  1467. return(exit_status);
  1468. if(no_sndfile_system_files)
  1469. dz->infilecnt = 0;
  1470. return(FINISHED);
  1471. }
  1472. /****************************** SET_LEGAL_INTERNALPARAM_STRUCTURE *********************************/
  1473. int set_legal_internalparam_structure(int process,int mode,aplptr ap)
  1474. {
  1475. int exit_status = FINISHED;
  1476. if(process == PSOW_SYNTH) {
  1477. switch(mode) {
  1478. case(0):
  1479. case(1):
  1480. exit_status = set_internalparam_data("00iiiiii",ap); break;
  1481. case(2):
  1482. case(3):
  1483. exit_status = set_internalparam_data("iiiiii",ap); break;
  1484. case(4):
  1485. break;
  1486. }
  1487. }
  1488. return(exit_status);
  1489. }
  1490. /************************* SETUP_INTERNAL_ARRAYS_AND_ARRAY_POINTERS *******************/
  1491. int setup_internal_arrays_and_array_pointers(dataptr dz)
  1492. {
  1493. int n;
  1494. dz->array_cnt = 0;
  1495. dz->larray_cnt = 0;
  1496. switch(dz->process) {
  1497. case(PSOW_SYNTH):
  1498. dz->array_cnt = 5;
  1499. dz->larray_cnt = 1;
  1500. break;
  1501. case(PSOW_REINF):
  1502. if(dz->mode == 0) {
  1503. dz->array_cnt = 1;
  1504. dz->larray_cnt = 1;
  1505. } else {
  1506. dz->array_cnt = 2;
  1507. }
  1508. break;
  1509. }
  1510. if(dz->array_cnt) {
  1511. if((dz->parray = (double **)malloc(dz->array_cnt * sizeof(double *)))==NULL) {
  1512. sprintf(errstr,"INSUFFICIENT MEMORY for internal double arrays.\n");
  1513. return(MEMORY_ERROR);
  1514. }
  1515. for(n=0;n<dz->array_cnt;n++)
  1516. dz->parray[n] = NULL;
  1517. }
  1518. if(dz->larray_cnt) {
  1519. if((dz->lparray = (int **)malloc(dz->larray_cnt * sizeof(int *)))==NULL) {
  1520. sprintf(errstr,"INSUFFICIENT MEMORY for internal long arrays.\n");
  1521. return(MEMORY_ERROR);
  1522. }
  1523. for(n=0;n<dz->larray_cnt;n++)
  1524. dz->lparray[n] = NULL;
  1525. }
  1526. return(FINISHED);
  1527. }
  1528. /************************* READ_SPECIAL_DATA *******************/
  1529. int read_special_data(char *str,dataptr dz)
  1530. {
  1531. aplptr ap = dz->application;
  1532. switch(ap->special_data) {
  1533. case(SYNTHBANK): return read_synth_data(str,dz);
  1534. case(TIMEVARYING_SYNTHBANK): return read_time_varying_synth_data(str,dz);
  1535. case(PSOW_REINFORCEMENT): return read_reinforcement_data(str,dz);
  1536. case(PSOW_INHARMONICS): return read_inharmonics_data(str,dz);
  1537. }
  1538. return(FINISHED);
  1539. }
  1540. /************************ HANDLE_THE_SPECIAL_DATA *********************/
  1541. int handle_the_special_data(int *cmdlinecnt,char ***cmdline,dataptr dz)
  1542. {
  1543. int exit_status;
  1544. aplptr ap = dz->application;
  1545. if(ap->special_data) {
  1546. if(!sloom) {
  1547. if(*cmdlinecnt <= 0) {
  1548. sprintf(errstr,"Insufficient parameters on command line.\n");
  1549. return(USAGE_ONLY);
  1550. }
  1551. }
  1552. if((exit_status = setup_the_special_data_ranges
  1553. (dz->mode,dz->infile->srate,dz->duration,dz->nyquist,dz->wlength,dz->infile->channels,ap))<0)
  1554. return(exit_status);
  1555. if((exit_status = read_special_data((*cmdline)[0],dz))<0)
  1556. return(exit_status);
  1557. (*cmdline)++;
  1558. (*cmdlinecnt)--;
  1559. }
  1560. return(FINISHED);
  1561. }
  1562. /************************ SETUP_SPECIAL_DATA_RANGES *********************/
  1563. int setup_the_special_data_ranges(int mode,int srate,double duration,double nyquist,int wlength,int channels,aplptr ap)
  1564. {
  1565. switch(ap->special_data) {
  1566. case(SYNTHBANK):
  1567. case(TIMEVARYING_SYNTHBANK):
  1568. ap->special_range = TRUE;
  1569. switch(mode) {
  1570. case(0):
  1571. case(2):
  1572. ap->min_special = 0.1;
  1573. ap->max_special = nyquist/2.0;
  1574. break;
  1575. case(1):
  1576. case(3):
  1577. ap->min_special = unchecked_hztomidi(0.1);
  1578. ap->max_special = MIDIMAX;
  1579. break;
  1580. }
  1581. ap->default_special = 440.0;
  1582. ap->other_special_range = TRUE;
  1583. ap->min_special2 = 0.0;
  1584. ap->max_special2 = 1.0;
  1585. break;
  1586. case(PSOW_REINFORCEMENT):
  1587. ap->min_special = 2.0;
  1588. ap->max_special = 256.0;
  1589. ap->min_special2 = FLTERR;
  1590. ap->max_special2 = 16.0;
  1591. break;
  1592. case(PSOW_INHARMONICS):
  1593. ap->min_special = 1.0;
  1594. ap->max_special = 256.0;
  1595. ap->min_special2 = FLTERR;
  1596. ap->max_special2 = 16.0;
  1597. break;
  1598. default:
  1599. sprintf(errstr,"Unknown special_data type: setup_the_special_data_ranges()\n");
  1600. return(PROGRAM_ERROR);
  1601. }
  1602. return(FINISHED);
  1603. }
  1604. /**************************** CHECK_PSOW_PARAM_VALIDITY_AND_CONSISTENCY *****************************/
  1605. int check_psow_param_validity_and_consistency(dataptr dz)
  1606. {
  1607. int exit_status;
  1608. int n;
  1609. double time, time2;
  1610. if(dz->process != PSOW_INTERP && dz->process != PSOW_EXTEND2) {
  1611. if(dz->brksize[0] == 0) {
  1612. sprintf(errstr,"PITCH PARAMETER MUST BE IN A BREAKPOINT FILE.\n");
  1613. return(DATA_ERROR);
  1614. }
  1615. n = (dz->brksize[0] - 1) * 2;
  1616. time = dz->brk[0][n];
  1617. time2 = (double)dz->insams[0]/(double)dz->infile->srate;
  1618. if(fabs(time - time2) > .05) {
  1619. sprintf(errstr,"PITCH DATA FILE DOES NOT CORRESPOND IN LENGTH (%lf) TO SOUND FILE (%lf).\n",time,time2);
  1620. return(DATA_ERROR);
  1621. }
  1622. }
  1623. switch(dz->process) {
  1624. case(PSOW_FREEZE):
  1625. if(!dz->brksize[PS_GAIN] && (dz->param[PS_GAIN] <= 0.0)) {
  1626. sprintf(errstr,"ZERO output gain specified.\n");
  1627. return(DATA_ERROR);
  1628. }
  1629. /* fall thro */
  1630. case(PSOW_EXTEND):
  1631. if((exit_status = read_value_from_brktable(dz->param[1],0,dz))<0)
  1632. return(exit_status);
  1633. if(flteq(dz->param[0],NOT_SOUND)) {
  1634. sprintf(errstr,"There is no significant signal at time %lf in the file.\n",dz->param[1]);
  1635. return(DATA_ERROR);
  1636. } else if(flteq(dz->param[0],NOT_PITCH)) {
  1637. fprintf(stdout,"WARNING: At time %lf there is no known pitch.\n",dz->param[1]);
  1638. fflush(stdout);
  1639. }
  1640. if((exit_status = convert_pse_semit_to_octratio(dz))<0)
  1641. return(exit_status);
  1642. break;
  1643. case(PSOW_EXTEND2):
  1644. if(dz->param[1] <= dz->param[0]) {
  1645. sprintf(errstr,"Start and End times for grain are incopatible.\n");
  1646. return(DATA_ERROR);
  1647. }
  1648. dz->iparam[0] = (int)round(dz->param[0] * dz->infile->srate);
  1649. dz->iparam[1] = (int)round(dz->param[1] * dz->infile->srate);
  1650. break;
  1651. case(PSOW_SYNTH):
  1652. switch(dz->mode) {
  1653. case(2):
  1654. case(3):
  1655. if((exit_status = allocate_tvarying_osc_arrays(dz))<0)
  1656. return(exit_status);
  1657. if((exit_status = put_tvarying_osc_data_in_arrays(dz->parray[PS_FBRK],dz))<0)
  1658. return(exit_status);
  1659. if((exit_status = initialise_psowsynth_internal_params(dz))<0)
  1660. return(exit_status);
  1661. break;
  1662. }
  1663. break;
  1664. case(PSOW_IMPOSE):
  1665. dz->param[PS_GATE] = dbtogain(dz->param[PS_GATE]);
  1666. break;
  1667. case(PSOW_SPLIT):
  1668. dz->param[PS_UTRNS] = pow(2.0,(dz->param[PS_UTRNS]/SEMITONES_PER_OCTAVE));
  1669. break;
  1670. case(PSOW_REINF):
  1671. if(dz->mode==0)
  1672. dz->iparam[ISTR] = (int)round(dz->param[ISTR] * MS_TO_SECS * (double)dz->infile->srate) * dz->infile->channels;
  1673. break;
  1674. }
  1675. return FINISHED;
  1676. }
  1677. /*************************** CREATE_SNDBUFS **************************/
  1678. int create_psow_sndbufs(int minbuf,dataptr dz)
  1679. {
  1680. int n;
  1681. size_t bigbufsize;
  1682. if(dz->sbufptr == 0 || dz->sampbuf==0) {
  1683. sprintf(errstr,"buffer pointers not allocated: create_sndbufs()\n");
  1684. return(PROGRAM_ERROR);
  1685. }
  1686. bigbufsize = (size_t) Malloc(-1);
  1687. bigbufsize = max(bigbufsize,minbuf * sizeof(float) * dz->bufcnt);
  1688. bigbufsize /= dz->bufcnt;
  1689. if(bigbufsize <=0)
  1690. bigbufsize = F_SECSIZE * sizeof(float); /* RWD keep ths for now */
  1691. dz->buflen = (int)(bigbufsize / sizeof(float));
  1692. /*RWD also cover n-channels usage */
  1693. bigbufsize = (size_t)(dz->buflen * sizeof(float));
  1694. if((dz->bigbuf = (float *)malloc(bigbufsize * dz->bufcnt)) == NULL) {
  1695. sprintf(errstr,"INSUFFICIENT MEMORY to create sound buffers.\n");
  1696. return(PROGRAM_ERROR);
  1697. }
  1698. if(dz->process==PSOW_SPACE) {
  1699. dz->sampbuf[0] = dz->bigbuf;
  1700. dz->sampbuf[1] = dz->bigbuf + dz->buflen;
  1701. dz->sampbuf[2] = dz->bigbuf + (2 * dz->buflen);
  1702. dz->sampbuf[3] = dz->bigbuf + (4 * dz->buflen);
  1703. } else {
  1704. for(n=0;n<dz->bufcnt;n++)
  1705. dz->sbufptr[n] = dz->sampbuf[n] = dz->bigbuf + (dz->buflen * n);
  1706. dz->sampbuf[n] = dz->bigbuf + (dz->buflen * n);
  1707. }
  1708. return(FINISHED);
  1709. }
  1710. /********************************************************************************************/
  1711. int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  1712. {
  1713. if (!strcmp(prog_identifier_from_cmdline,"stretch")) dz->process = PSOW_STRETCH;
  1714. else if (!strcmp(prog_identifier_from_cmdline,"dupl")) dz->process = PSOW_DUPL;
  1715. else if (!strcmp(prog_identifier_from_cmdline,"delete")) dz->process = PSOW_DEL;
  1716. else if (!strcmp(prog_identifier_from_cmdline,"strtrans")) dz->process = PSOW_STRFILL;
  1717. else if (!strcmp(prog_identifier_from_cmdline,"grab")) dz->process = PSOW_FREEZE;
  1718. else if (!strcmp(prog_identifier_from_cmdline,"chop")) dz->process = PSOW_CHOP;
  1719. else if (!strcmp(prog_identifier_from_cmdline,"interp")) dz->process = PSOW_INTERP;
  1720. else if (!strcmp(prog_identifier_from_cmdline,"features")) dz->process = PSOW_FEATURES;
  1721. else if (!strcmp(prog_identifier_from_cmdline,"synth")) dz->process = PSOW_SYNTH;
  1722. else if (!strcmp(prog_identifier_from_cmdline,"impose")) dz->process = PSOW_IMPOSE;
  1723. else if (!strcmp(prog_identifier_from_cmdline,"split")) dz->process = PSOW_SPLIT;
  1724. else if (!strcmp(prog_identifier_from_cmdline,"space")) dz->process = PSOW_SPACE;
  1725. else if (!strcmp(prog_identifier_from_cmdline,"interleave")) dz->process = PSOW_INTERLEAVE;
  1726. else if (!strcmp(prog_identifier_from_cmdline,"replace")) dz->process = PSOW_REPLACE;
  1727. else if (!strcmp(prog_identifier_from_cmdline,"sustain")) dz->process = PSOW_EXTEND;
  1728. else if (!strcmp(prog_identifier_from_cmdline,"sustain2")) dz->process = PSOW_EXTEND2;
  1729. else if (!strcmp(prog_identifier_from_cmdline,"locate")) dz->process = PSOW_LOCATE;
  1730. else if (!strcmp(prog_identifier_from_cmdline,"cutatgrain")) dz->process = PSOW_CUT;
  1731. else if (!strcmp(prog_identifier_from_cmdline,"reinforce")) dz->process = PSOW_REINF;
  1732. else {
  1733. sprintf(errstr,"Unknown program identification string '%s'\n",prog_identifier_from_cmdline);
  1734. return(USAGE_ONLY);
  1735. }
  1736. return(FINISHED);
  1737. }
  1738. /******************************** USAGE1 ********************************/
  1739. int usage1(void)
  1740. {
  1741. fprintf(stderr,
  1742. "\nOPERATIONS ON PITCH-SYNCHRONOUS GRAINS (FOFS)\n\n"
  1743. "USAGE: psow NAME (mode) infile(s) outfile parameters: \n"
  1744. "\n"
  1745. "where NAME can be any one of\n"
  1746. "\n"
  1747. "stretch dupl delete strtrans features grab sustain sustain2\n"
  1748. "chop interp synth impose split space interleave replace\n"
  1749. "locate cutatgrain reinforce\n\n"
  1750. "Type 'psow stretch' for more info on psow stretch..ETC.\n");
  1751. return(USAGE_ONLY);
  1752. }
  1753. /******************************** USAGE2 ********************************/
  1754. int usage2(char *str)
  1755. {
  1756. if(!strcmp(str,"stretch")) {
  1757. fprintf(stderr,
  1758. "USAGE:\n"
  1759. "psow stretch infile outfile pitch-brkpnt-data timestretch segcnt\n"
  1760. "\n"
  1761. "Timestretch/transpose a sound by repositioning the pitch-syncd grains.\n"
  1762. "The grains themselves are not time-stretched.\n"
  1763. "\n"
  1764. "PITCH-BRKPNT-DATA a breakpoint file with time and frq information.\n"
  1765. " File may contain zeros (indicating moments of no-signal)\n"
  1766. " but NOT pitch-zeros (indicating moments of no-pitch).\n"
  1767. " It must contain SOME significant frequency information.\n"
  1768. "TIMESTRETCH Proportion by which sound is stretched (or shrunk).\n"
  1769. "SEGCNT no of grains in a chunk retained as-is,\n"
  1770. " while gaps between segments are stretched.\n"
  1771. "\n");
  1772. } else if(!strcmp(str,"dupl")) {
  1773. fprintf(stdout,
  1774. "USAGE:\n"
  1775. "psow dupl infile outfile pitch-brkpnt-data repeat-cnt segcnt\n"
  1776. "\n"
  1777. "Timestretch/transpose a sound by duplicating the pitch-syncd grains.\n"
  1778. "\n"
  1779. "PITCH-BRKPNT-DATA a breakpoint file with time and frq information.\n"
  1780. " File may contain zeros (indicating moments of no-signal)\n"
  1781. " but NOT pitch-zeros (indicating moments of no-pitch).\n"
  1782. " It must contain SOME significant frequency information.\n"
  1783. "REPEAT-CNT Number of repetitions of each chunk.\n"
  1784. "SEGCNT no of grains in a chunk .\n"
  1785. "\n");
  1786. } else if(!strcmp(str,"delete")) {
  1787. fprintf(stdout,
  1788. "USAGE:\n"
  1789. "psow del infile outfile pitch-brkpnt-data propkeep segcnt\n"
  1790. "\n"
  1791. "Time shrink sound by deleting proportion of pitch-syncd grains.\n"
  1792. "\n"
  1793. "PITCH-BRKPNT-DATA a breakpoint file with time and frq information.\n"
  1794. " File may contain zeros (indicating moments of no-signal)\n"
  1795. " but NOT pitch-zeros (indicating moments of no-pitch).\n"
  1796. " It must contain SOME significant frequency information.\n"
  1797. "PROPKEEP Proportion of chunks to keep.\n"
  1798. " '2' keeps 1 in 2: '7' Keeps 1 in 7: etc.\n"
  1799. "SEGCNT no of grains in a chunk.\n"
  1800. "\n");
  1801. } else if(!strcmp(str,"strtrans")) {
  1802. fprintf(stdout,
  1803. "USAGE:\n"
  1804. "psow strtrans infile outfile pitch-brkpnt-data timestretch segcnt trans\n"
  1805. "\n"
  1806. "Timestretch/transpose a sound by repositioning the pitch-syncd grains,\n"
  1807. "and overlapping them.\n"
  1808. "\n"
  1809. "PITCH-BRKPNT-DATA a breakpoint file with time and frq information.\n"
  1810. " File may contain zeros (indicating moments of no-signal)\n"
  1811. " but NOT pitch-zeros (indicating moments of no-pitch).\n"
  1812. " It must contain SOME significant frequency information.\n"
  1813. "TIMESTRETCH Proportion by which sound is stretched (or shrunk).\n"
  1814. "SEGCNT no of grains in a chunk retained as-is,\n"
  1815. " while gaps between segments are stretched.\n"
  1816. "TRANS Transposition in semitones, corresponds to overlap between\n"
  1817. " succesive segments.\n"
  1818. " NB This parameter interacts with 'Timestretch' parameter\n"
  1819. " in unpredicable ways.\n"
  1820. "\n");
  1821. } else if(!strcmp(str,"grab")) {
  1822. fprintf(stdout,
  1823. "USAGE:\n"
  1824. "psow grab infil outfil pitch-brkpnt-data time dur segcnt spectrans density rand gain\n"
  1825. "\n"
  1826. "Grab a pitch-syncd grain from a file, and use it to create a new sound\n"
  1827. "\n"
  1828. "PITCH-BRKPNT-DATA a breakpoint file with time and frq information.\n"
  1829. " File may contain zeros (indicating moments of no-signal)\n"
  1830. " but NOT pitch-zeros (indicating moments of no-pitch).\n"
  1831. " It must contain SOME significant frequency information.\n"
  1832. "TIME Time at which to grab grain(s).\n"
  1833. "DUR Output file duration. Dur ZERO grabs SINGLE grain(chunk).\n"
  1834. "SEGCNT no of grains in a chunk.\n"
  1835. "DENSITY Rate at which the chunks in the outfile succeed one another.\n"
  1836. " 1: grains follow on, 2: grains overlap by 2, 3: by 3 etc\n"
  1837. " 0.5: grains separated by equivalent silence etc.\n"
  1838. " overlap by 2 can transpose up by 8va, without changing spectrum.\n"
  1839. "SPECTRANS Transposition of spectrum (not fundamental) in semitones.\n"
  1840. "RAND Randomisation of position of grainchunks in output.\n"
  1841. " Randomisation introduces noisiness into output sound.\n"
  1842. "GAIN Overall gain: may need to reduce this if density > 1 in output.\n"
  1843. "\n");
  1844. } else if(!strcmp(str,"chop")) {
  1845. fprintf(stdout,
  1846. "USAGE:\n"
  1847. "psow chop infile outfile pitch-brkpnt-data time-grain-pairs\n"
  1848. "\n"
  1849. "Chop sound into sections between specified grain(chunks)\n"
  1850. "\n"
  1851. "PITCH-BRKPNT-DATA a breakpoint file with time and frq information.\n"
  1852. " File may contain zeros (indicating moments of no-signal)\n"
  1853. " but NOT pitch-zeros (indicating moments of no-pitch).\n"
  1854. " It must contain SOME significant frequency information.\n"
  1855. "TIME-GRAIN-PAIRS File contains pairs of values: time graincnt.\n"
  1856. " 'Time' is time of grain where file is cut.\n"
  1857. " File is always cut at start-of-grain boundary.\n"
  1858. " Next segment begins AFTER the specified grainchunk.\n"
  1859. " 'Graincnt' is no. of grains in chunk, at specified time,\n"
  1860. " before the next cut-section starts.\n"
  1861. "\n"
  1862. "This process might be used in conjunction with 'psow grab' or 'interp',\n"
  1863. "Grabbing a grain at a particular time in the sound, extending it,\n"
  1864. "then reinserting it into the original sound by using the chopped components.\n"
  1865. "\n");
  1866. } else if(!strcmp(str,"interp")) {
  1867. fprintf(stdout,
  1868. "USAGE: psow interp infile1 infile2 outfile startdur interpdur enddur\n"
  1869. " vibfrq vibdepth tremfrq tremdepth\n"
  1870. "\n"
  1871. "Interpolate between 2 pitch-synchronised grains, to produce new sound.\n"
  1872. "\n"
  1873. "STARTDUR Duration to sustain initial grain.\n"
  1874. "INTERPDUR Duration of interpolation.\n"
  1875. "ENDDUR Duration to sustain final grain.\n"
  1876. "VIBFRQ Frequency of any added vibrato.\n"
  1877. "VIBDEPTH Depth (semitones) of any added vibrato.\n"
  1878. "TREMFRQ Frequency of any added tremolo.\n"
  1879. "TREMDEPTH Depth of any added tremolo.\n"
  1880. "\n"
  1881. "Process assumes your files each contain a single pitchsync grain.\n"
  1882. "obtained using 'psow grab' with output duration 0.0\n"
  1883. "\n");
  1884. } else if(!strcmp(str,"features")) {
  1885. fprintf(stdout,
  1886. "USAGE: psow features 1-2 infile1 outfile pitch-brkpnt-data segcnt\n"
  1887. " trans vibfrq vibdepth spectrans hoarseness attenuation\n"
  1888. " subharmno subharmamp fof-stretching [-a]\n"
  1889. "Impose new features on vocal-type sound, preserving or modifying FOF-grains.\n"
  1890. "\n"
  1891. "PITCH-BRKPNT-DATA a breakpoint file with time and frq information.\n"
  1892. " File may contain zeros (indicating moments of no-signal)\n"
  1893. " but NOT pitch-zeros (indicating moments of no-pitch).\n"
  1894. " It must contain SOME significant frequency information.\n"
  1895. "SEGCNT No of grains in a chunk retained as-is.\n"
  1896. "TRANS Pitch transposition (semitones). Works differently in 2 modes.\n"
  1897. " 1) Transpose accompanied by timewarp (pitch up, snd shorter).\n"
  1898. " 2) Transposed pitch accompanied by additional lower pitch.\n"
  1899. "VIBFRQ Frequency of any added vibrato.\n"
  1900. "VIBDEPTH Depth (semitones) of any added vibrato.\n"
  1901. "SPECTRANS Transposition of spectrum (not fundamental) in semitones.\n"
  1902. "HOARSENESS Degree of hoarseness of voice (Range 0 to 1).\n"
  1903. "ATTENUATION Attenuation (maybe necessary when 'fof-stretching') (Range 0-1)\n"
  1904. "SUBHARMNO Amount by which fundamental divided, 0 or 1 gives NO subharm.\n"
  1905. "SUBHARMAMP Level of any subharmonic introduced.\n"
  1906. "FOF-STRETCHING Time-extension of FOFs: does NOT stretch sound (Range 1 to 512)\n"
  1907. "-a Alternative algorithm for fof_stretch.\n");
  1908. } else if(!strcmp(str,"synth")) {
  1909. fprintf(stdout,
  1910. "USAGE: psow synth 1-5 infile1 outfile [oscdatafile] pitch-brkpnt-data depth\n"
  1911. "\n"
  1912. "Impose vocal FOFs on a stream of synthesized sound.\n"
  1913. "\n"
  1914. "OSCDATAFILE (Amplitude values range is 0.0 to 1.0)\n"
  1915. " Mode 1: Each line has pair of values for frequency and amplitude.\n"
  1916. " Mode 2: Each line has pair of values for midipitch and amplitude.\n"
  1917. " Mode 3: Each line has frq and amp data in the 'filter varibank' format.\n"
  1918. " Mode 4: Each line has midipitch & amp data in 'filter varibank' format.\n"
  1919. " Mode 5: No 'oscdatafile' ... synthetic source is noise.\n"
  1920. "PITCH-BRKPNT-DATA a breakpoint file with time and frq information.\n"
  1921. " File may contain zeros (indicating moments of no-signal)\n"
  1922. " but NOT pitch-zeros (indicating moments of no-pitch).\n"
  1923. " It must contain SOME significant frequency information.\n"
  1924. "DEPTH Depth of application of FOFS to the synthesized sound.\n"
  1925. "\n");
  1926. } else if(!strcmp(str,"impose")) {
  1927. fprintf(stdout,
  1928. "USAGE: psow synth infile1 infile2 outfile pitch-brkpnt-data depth wsize gate\n"
  1929. "\n"
  1930. "Impose vocal FOFs in 1st sound onto the 2nd sound.\n"
  1931. "\n"
  1932. "PITCH-BRKPNT-DATA a breakpoint file with time and frq information.\n"
  1933. " File may contain zeros (indicating moments of no-signal)\n"
  1934. " but NOT pitch-zeros (indicating moments of no-pitch).\n"
  1935. " It must contain SOME significant frequency information.\n"
  1936. "DEPTH Depth of application of FOFS to the 2nd sound.\n"
  1937. "WSIZE Windowsize (mS) to envelope track 2nd sound (for normalisation).\n"
  1938. "GATE Level (decibels) in 2nd sound at which it's assumed to be zero.\n"
  1939. "\n");
  1940. } else if(!strcmp(str,"split")) {
  1941. fprintf(stdout,
  1942. "USAGE: psow split infile1 outfile pitch-brkpnt-data subharmno uptrans balance\n"
  1943. "\n"
  1944. "Split vocal FOFs into subharmonic and upwardly transposed pitch.\n"
  1945. "\n"
  1946. "PITCH-BRKPNT-DATA a breakpoint file with time and frq information.\n"
  1947. " File may contain zeros (indicating moments of no-signal)\n"
  1948. " but NOT pitch-zeros (indicating moments of no-pitch).\n"
  1949. " It must contain SOME significant frequency information.\n"
  1950. "SUBHARMNO Subharmonic number (divides frequency of source) (Range 3-8).\n"
  1951. "UPTRANS Upward transposition in semitones (Range 0 - 48).\n"
  1952. "BALANCE Level of up-transposed components relative to subharms (0-8).\n"
  1953. "\n");
  1954. } else if(!strcmp(str,"space")) {
  1955. fprintf(stdout,
  1956. "USAGE: psow space infile1 outfile\n"
  1957. " pitch-brkpnt-data subno separation balance hisuppress\n"
  1958. "\n"
  1959. "Split alternating FOFs between different spatial locations.\n"
  1960. "\n"
  1961. "PITCH-BRKPNT-DATA a breakpoint file with time and frq information.\n"
  1962. " File may contain zeros (indicating moments of no-signal)\n"
  1963. " but NOT pitch-zeros (indicating moments of no-pitch).\n"
  1964. " It must contain SOME significant frequency information.\n"
  1965. "SUBNO Subharmonic number (divides frequency of source) (Range 2-5).\n"
  1966. "SEPARATION spatial separation of alternate FOFs (range -1 to 1).\n"
  1967. " 0: no separation, all output is stereo-centred.\n"
  1968. " 1: alternate FOFS to widest spread, starting with far right position.\n"
  1969. " -1: alternate FOFS to widest spread, starting with far left position.\n"
  1970. " intermediate vals give intermediate degrees of spatial separation.\n"
  1971. "BALANCE of left:right components (0-8).\n"
  1972. " 1.0: leftward & rightward levels equal.\n"
  1973. " >1: leftward signal divided by balance. (Bias to right)\n"
  1974. " <1: rightward signal multiplied by balance. (Bias to left)\n"
  1975. "HISUPPRESS Suppression of high-frequency components (0-1).\n"
  1976. "\n");
  1977. } else if(!strcmp(str,"interleave")) {
  1978. fprintf(stdout,
  1979. "USAGE: psow interleave\n"
  1980. " infile1 infile2 outfile pbrk1 pbrk2 grplen bias bal weight\n"
  1981. "\n"
  1982. "Interleave FOFs from two different files.\n"
  1983. "\n"
  1984. "PBRK1 a breakpoint file with time & frq info about infile1.\n"
  1985. " File may contain zeros (indicating moments of no-signal)\n"
  1986. " but NOT pitch-zeros (indicating moments of no-pitch).\n"
  1987. " It must contain SOME significant frequency information.\n"
  1988. "PBRK2 Similar file for infile 2.\n"
  1989. "GRPLEN number of FOFS in each manipulated segment.\n"
  1990. "BIAS Is outpitch biased to one or other of infiles ?.\n"
  1991. " 0: no bias. 1: biased to 1st. -1: biased to 2nd.\n"
  1992. " intermediate vals give intermediate degrees of bias.\n"
  1993. "BAL level balance of components of 2 input files in output.\n"
  1994. " 1.0: equally loud. >1: 1st louder. <1: 2nd louder.\n"
  1995. "WEIGTH relative number of components of 2 input files in output.\n"
  1996. " 1.0: equal. >1: more of 1st. <1: more of 2nd.\n"
  1997. "\n");
  1998. } else if(!strcmp(str,"replace")) {
  1999. fprintf(stdout,
  2000. "USAGE: psow replace infile1 infile2 outfile pbrk1 pbrk2 grpcnt\n"
  2001. "\n"
  2002. "Combine fofs of first sound with pitch of 2nd.\n"
  2003. "\n"
  2004. "PBRK1 a breakpoint file with time & frq info about infile1.\n"
  2005. " File may contain zeros (indicating moments of no-signal)\n"
  2006. " but NOT pitch-zeros (indicating moments of no-pitch).\n"
  2007. " It must contain SOME significant frequency information.\n"
  2008. "PBRK2 Similar file for infile 2.\n"
  2009. "GRPCNT Number of FOFS in a chunk.\n"
  2010. "\n"
  2011. "This process assumes that BOTH the input files have pitched-grains\n"
  2012. "\n");
  2013. } else if(!strcmp(str,"sustain")) {
  2014. fprintf(stdout,
  2015. "USAGE:\n"
  2016. "psow sustain infil outfil pch-brkdata time dur segcnt\n"
  2017. " vibfrq vibdepth transpos gain [-s]\n"
  2018. "\n"
  2019. "Freeze and Sustain a sound on a specified pitch-syncd grain.\n"
  2020. "\n"
  2021. "PCH-BRKDATA a breakpoint file with time and frq information.\n"
  2022. " File may contain zeros (indicating moments of no-signal)\n"
  2023. " but NOT pitch-zeros (indicating moments of no-pitch).\n"
  2024. " It must contain SOME significant frequency information.\n"
  2025. "TIME Time at which to freeze grain(s).\n"
  2026. "DUR Output file duration. (Greater than input file's duration).\n"
  2027. "SEGCNT no of grains in a chunk.\n"
  2028. "VIBFRQ Frequency of any added vibrato (added only to expanded grain).\n"
  2029. "VIBDEPTH Depth (semitones) of any added vibrato.\n"
  2030. "TRANSPOS Transposition (semitones) of grain.\n"
  2031. " Time zero in the brkpnt file for transposition parameter (ONLY)\n"
  2032. " = start of expanded grain, & NOT (necessarily) start of sound.\n"
  2033. "GAIN loudness contour of the entire output.\n"
  2034. "-s Smooth the grabbed fofs\n"
  2035. "\n");
  2036. } else if(!strcmp(str,"sustain2")) {
  2037. fprintf(stdout,
  2038. "USAGE:\n"
  2039. "psow sustain2 infil outfil start end dur vibfrq vibdepth nudge\n"
  2040. "\n"
  2041. "Freeze and Sustain a sound on a specified grain.\n"
  2042. "\n"
  2043. "START Time at which to cut grain.\n"
  2044. "END Time OF end of grain.\n"
  2045. "DUR Output file duration. (Greater than input file's duration).\n"
  2046. "VIBFRQ Frequency of any added vibrato.\n"
  2047. "VIBDEPTH Depth (semitones) of any added vibrato.\n"
  2048. "NUDGE Move selected grain position by 'nudge' zerocrossings\n"
  2049. "\n");
  2050. } else if(!strcmp(str,"locate")) {
  2051. fprintf(stdout,
  2052. "USAGE: psow locate infil pitch-brkpnt-data time\n"
  2053. "\n"
  2054. "Gives exact time of grain-start nearest to specified time.\n"
  2055. "\n"
  2056. "PITCH-BRKPNT-DATA a breakpoint file with time and frq information.\n"
  2057. " File may contain zeros (indicating moments of no-signal)\n"
  2058. " but NOT pitch-zeros (indicating moments of no-pitch).\n"
  2059. " It must contain SOME significant frequency information.\n"
  2060. "TIME Time at which find grain.\n"
  2061. "\n");
  2062. } else if(!strcmp(str,"cutatgrain")) {
  2063. fprintf(stdout,
  2064. "USAGE:\n"
  2065. "psow cutatgrain 1-2 infil outfil pitch-brkpnt-data time\n"
  2066. "\n"
  2067. "Cuts the file at the specified time.\n"
  2068. "Mode 1: Retains file BEFORE (exact) specified grain time.\n"
  2069. "Mode 2: Retains file AT AND AFTER (exact) specified grain.\n"
  2070. "\n"
  2071. "PITCH-BRKPNT-DATA a breakpoint file with time and frq information.\n"
  2072. " File may contain zeros (indicating moments of no-signal)\n"
  2073. " but NOT pitch-zeros (indicating moments of no-pitch).\n"
  2074. " It must contain SOME significant frequency information.\n"
  2075. "TIME time at which to cut the file.\n"
  2076. "\n");
  2077. } else if(!strcmp(str,"reinforce")) {
  2078. fprintf(stdout,
  2079. "USAGE:\n"
  2080. "psow reinforce 1 infil outfil reinforcement-data pitch-brkpnt-data [-ddelay] [-s]\n"
  2081. "psow reinforce 2 infil outfil reinforcement-data pitch-brkpnt-data [-wweight]\n"
  2082. "\n"
  2083. "MODE 1: Reinforce the harmonic content of the sound.\n"
  2084. "MODE 2: Reinforce sound with inharmonic partials.\n"
  2085. "\n"
  2086. "REINFORCEMENT-DATA File of pairs of values, which represent\n"
  2087. " a harmonic number (2-256) + level relative to src (>0.0 - 16.0).\n"
  2088. " In MODE 2, the 'harmonics' may be fractional (range >1 - 256).\n"
  2089. "\n"
  2090. "PITCH-BRKPNT-DATA a breakpoint file with time and frq information.\n"
  2091. " File may contain zeros (indicating moments of no-signal)\n"
  2092. " but NOT pitch-zeros (indicating moments of no-pitch).\n"
  2093. " It must contain SOME significant frequency information.\n"
  2094. "\n"
  2095. "-s FOFs generated for higher harmonics which coincide with\n"
  2096. " FOFs of lower harmonics, are omitted.\n"
  2097. "delay mS delay of onset of the added harmonics.\n"
  2098. "weight Sustain inharmonic components. Higher weight, longer sustain.\n"
  2099. " A very high weight may cause buffers to overflow. Default 4.0\n"
  2100. "\n");
  2101. } else
  2102. fprintf(stdout,"Unknown option '%s'\n",str);
  2103. return(USAGE_ONLY);
  2104. }
  2105. int usage3(char *str1,char *str2)
  2106. {
  2107. fprintf(stderr,"Insufficient parameters on command line.\n");
  2108. return(USAGE_ONLY);
  2109. }
  2110. /************************* redundant functions: to ensure libs compile OK *******************/
  2111. int assign_process_logic(dataptr dz)
  2112. {
  2113. return(FINISHED);
  2114. }
  2115. void set_legal_infile_structure(dataptr dz)
  2116. {}
  2117. int establish_bufptrs_and_extra_buffers(dataptr dz)
  2118. {
  2119. return(FINISHED);
  2120. }
  2121. int inner_loop
  2122. (int *peakscore,int *descnt,int *in_start_portion,int *least,int *pitchcnt,int windows_in_buf,dataptr dz)
  2123. {
  2124. return(FINISHED);
  2125. }
  2126. int get_process_no(char *prog_identifier_from_cmdline,dataptr dz)
  2127. {
  2128. return(FINISHED);
  2129. }
  2130. /**********************************************************/
  2131. /* UBER-FUNCTION THAT EXTRACTS FOFS, AND CALLS PROCESSES */
  2132. /**********************************************************/
  2133. /************************* PSOA_WEIRD *******************************/
  2134. int psoa_weird(dataptr dz)
  2135. {
  2136. int exit_status, iswarned = 0, one_grain = 0, display_time;
  2137. /* double maxenv = 10000.0;*/
  2138. int n, m, k, j, cutcnt, opos, ipos, writesize, seglen=0, next_opos, advance, repets;
  2139. int *trofpnt=NULL, *cuttime=NULL;
  2140. double *scanarray, time, dhere, dstep, thisplace, transinc, sum, diff, valdiff, maxamp, rand, gain=0.0, transpos, thisval;
  2141. double maxlevel, spectranspos, envincr, envval, average_seglen, valleft, valright, spacepos=0.0, suppression, dur, timestep;
  2142. int trofpntcnt = 0, cnt, startpos, orig_startpos=0, minwsize2, envcnt2=0;
  2143. float *ibuf=NULL, *ibuf2=NULL, *ibuf3=NULL, *ibuf4=NULL, *obuf=NULL, *obuf2=NULL, *ovflw=NULL, *xbuf=NULL, val;
  2144. int envcnt, minwsize, maxwsize = 0, here, here_in_buf=0, there_in_buf=0, lastwrite, startsamp, startsamp2, endsamp, overflow;
  2145. int xbufpos, maxwritesize, bufstart, outseglen, transtep, startopos, endopos, startz, endz, vibshift;
  2146. int bigarray, lastwritend, ssampsread2=0, limit, envtoptime, total_seglen, segcnt, cutstart = 0, cnt0, cnt1;
  2147. int namelen, spaceswitch, done_freeze, got;
  2148. char *outfilename;
  2149. double *fofenv, *sintab, srate = (double)dz->infile->srate, *amp, atten, *ihno=NULL, maxsamp, normaliser, ingraintime;
  2150. int *fofloc, segstep, bakstep;
  2151. int maxseglen, seglen0, seglen1, startcut=0, endcut=0, offset, nextcut, lastcut, startbuf = 0, last_sampsread=0;
  2152. int zcnt = 0, final_pos = 0, sig_cnt = 0, zcntb = 0, final_posb = 0, sig_cntb = 0, *hno=NULL;
  2153. int grainlen /*= there_in_buf - here_in_buf*/;
  2154. double sinstep, thissin;
  2155. int stsmoothed[2];
  2156. switch(dz->process) {
  2157. case(PSOW_FEATURES):
  2158. case(PSOW_SYNTH):
  2159. case(PSOW_SPLIT):
  2160. case(PSOW_SPACE):
  2161. case(PSOW_REPLACE):
  2162. case(PSOW_EXTEND):
  2163. case(PSOW_EXTEND2):
  2164. case(PSOW_CUT):
  2165. case(PSOW_REINF):
  2166. ibuf = dz->sampbuf[0];
  2167. ibuf2 = dz->sampbuf[1];
  2168. obuf = dz->sampbuf[2];
  2169. obuf2 = dz->sampbuf[3];
  2170. break;
  2171. case(PSOW_LOCATE):
  2172. ibuf = dz->sampbuf[0];
  2173. ibuf2 = dz->sampbuf[1];
  2174. break;
  2175. case(PSOW_IMPOSE):
  2176. case(PSOW_INTERLEAVE):
  2177. ibuf = dz->sampbuf[0];
  2178. ibuf2 = dz->sampbuf[1];
  2179. ibuf3 = dz->sampbuf[2];
  2180. ibuf4 = dz->sampbuf[3];
  2181. obuf = dz->sampbuf[4];
  2182. obuf2 = dz->sampbuf[5];
  2183. break;
  2184. default:
  2185. ibuf = dz->sampbuf[0];
  2186. obuf = dz->sampbuf[1];
  2187. obuf2 = dz->sampbuf[2];
  2188. break;
  2189. }
  2190. if(dz->process==PSOW_EXTEND2) {
  2191. offset = max(0,dz->iparam[0] - dz->buflen/2);
  2192. if(offset > 0){
  2193. if((sndseekEx(dz->ifd[0],offset,0)<0)){
  2194. sprintf(errstr,"sndseek() failed\n");
  2195. return SYSTEM_ERROR;
  2196. }
  2197. }
  2198. if((exit_status = read_samps(dz->sampbuf[0],dz))<0)
  2199. return exit_status;
  2200. here = dz->iparam[0] - offset;
  2201. if(dz->iparam[0] == 0) {
  2202. here = 0;
  2203. if(dz->iparam[PS2_NUJ] < 0) {
  2204. sprintf(errstr,"Nudge incompatible with given start position");
  2205. return(DATA_ERROR);
  2206. }
  2207. } else {
  2208. got = 0;
  2209. nextcut = next_down_zcross(here,dz->buflen,dz->sampbuf[0]);
  2210. lastcut = last_down_zcross(here,0,dz->sampbuf[0]);
  2211. if(lastcut < 0) {
  2212. if(offset == 0)
  2213. lastcut = 0;
  2214. }
  2215. if(nextcut < 0) {
  2216. here = lastcut;
  2217. got = 1;
  2218. }
  2219. if(lastcut == 0) {
  2220. if(nextcut >= 0) {
  2221. if(nextcut - here > here)
  2222. here = nextcut;
  2223. else
  2224. here = 0;
  2225. }
  2226. got = 1;
  2227. } else if(lastcut < 0) {
  2228. here = nextcut;
  2229. got = 1;
  2230. }
  2231. if(here < 0) {
  2232. sprintf(errstr,"Failed to find zero-cross point near time specified\n");
  2233. return(DATA_ERROR);
  2234. }
  2235. if(!got) {
  2236. if(nextcut - here < here -lastcut)
  2237. here = nextcut;
  2238. else
  2239. here = lastcut;
  2240. }
  2241. k = dz->iparam[PS2_NUJ];
  2242. while(k > 0) {
  2243. here = next_down_zcross(here,dz->buflen,dz->sampbuf[0]);
  2244. if(here < 0) {
  2245. sprintf(errstr,"Nudged zero-cross not found in buffer.\n");
  2246. return(DATA_ERROR);
  2247. }
  2248. k--;
  2249. }
  2250. while(k < 0) {
  2251. here = last_down_zcross(here,0,dz->sampbuf[0]);
  2252. if(here < 0) {
  2253. sprintf(errstr,"Nudged zero-cross not found in buffer.\n");
  2254. return(DATA_ERROR);
  2255. }
  2256. k++;
  2257. }
  2258. }
  2259. startcut = here + offset;
  2260. if(dz->iparam[1] >= dz->insams[0]) {
  2261. here = dz->insams[0];
  2262. if(dz->iparam[PS2_NUJ] > 0) {
  2263. sprintf(errstr,"Nudge incompatible with given end position");
  2264. return(DATA_ERROR);
  2265. }
  2266. } else {
  2267. got = 0;
  2268. here = dz->iparam[1] - offset;
  2269. nextcut = next_down_zcross(here,dz->buflen,dz->sampbuf[0]);
  2270. lastcut = last_down_zcross(here,0,dz->sampbuf[0]);
  2271. if(nextcut < 0) {
  2272. here = lastcut;
  2273. got = 1;
  2274. } else if(lastcut < 0) {
  2275. here = nextcut;
  2276. got = 1;
  2277. }
  2278. if(here < 0) {
  2279. sprintf(errstr,"Failed to find zero-cross point near time specified\n");
  2280. return(DATA_ERROR);
  2281. }
  2282. if(!got) {
  2283. if(nextcut - here < here -lastcut) {
  2284. here = nextcut;
  2285. } else {
  2286. here = lastcut;
  2287. }
  2288. }
  2289. k = dz->iparam[PS2_NUJ];
  2290. while(k > 0) {
  2291. here = next_down_zcross(here,dz->buflen,dz->sampbuf[0]);
  2292. if(here < 0) {
  2293. sprintf(errstr,"Nudged zero-cross not found in buffer.\n");
  2294. return(DATA_ERROR);
  2295. }
  2296. k--;
  2297. }
  2298. while(k < 0) {
  2299. here = last_down_zcross(here,0,dz->sampbuf[0]);
  2300. if(here < 0) {
  2301. sprintf(errstr,"Nudged zero-cross not found in buffer.\n");
  2302. return(DATA_ERROR);
  2303. }
  2304. k++;
  2305. }
  2306. }
  2307. endcut = here + offset;
  2308. if((seglen = endcut-startcut) <= 0) {
  2309. sprintf(errstr,"Times too close to find a grain.\n");
  2310. return(DATA_ERROR);
  2311. }
  2312. memset((char *)dz->sampbuf[0],0,dz->buflen * sizeof(float));
  2313. if((sndseekEx(dz->ifd[0],0,0)<0)){
  2314. sprintf(errstr,"sndseek() failed\n");
  2315. return SYSTEM_ERROR;
  2316. }
  2317. dz->total_samps_read = 0;
  2318. dz->samps_left = dz->insams[0];
  2319. } else {
  2320. if((exit_status = get_min_wsize(&minwsize,0,dz)) < 0)
  2321. return exit_status;
  2322. if(((envcnt = dz->insams[0]/minwsize) * minwsize)!=dz->insams[0])
  2323. envcnt++;
  2324. if((dz->env=(float *)malloc(((envcnt+20) * 2) * sizeof(float)))==NULL) {
  2325. sprintf(errstr,"INSUFFICIENT MEMORY for envelope array.\n");
  2326. return(MEMORY_ERROR);
  2327. }
  2328. bigarray = envcnt+20;
  2329. if(dz->process == PSOW_INTERLEAVE || dz->process == PSOW_REPLACE) {
  2330. if((exit_status = get_min_wsize(&minwsize2,1,dz)) < 0)
  2331. return exit_status;
  2332. if(((envcnt2 = dz->insams[1]/minwsize2) * minwsize2)!=dz->insams[1])
  2333. envcnt2++;
  2334. bigarray += envcnt2 + 20;
  2335. }
  2336. memset(dz->env,0,(envcnt+20) * 2 * sizeof(float)); /* PRESET env VALS TO ZERO */
  2337. /* PART ENVELOPE vals CAN THEN BE STORED, where env-extract overlaps a buffer boundary */
  2338. fprintf(stdout,"INFO: Extracting pitch-related envelope from file.\n");
  2339. fflush(stdout);
  2340. if((exit_status = extract_pitch_dependent_env_from_sndfile(minwsize,0,&maxwsize,dz))<0)
  2341. return(exit_status);
  2342. envcnt = dz->envend - dz->env;
  2343. // NB EACH ENVELOPE-ITEM CONSISTS OF TWO VALS (time & env-val).
  2344. // REVISED HERE TAKING THIS INTO ACCOUNT
  2345. fprintf(stdout,"INFO: Locating envelope peaks.\n");
  2346. fflush(stdout);
  2347. if((trofpnt = (int *)malloc(envcnt/2 * sizeof(int)))==NULL) {
  2348. sprintf(errstr,"INSUFFICIENT MEMORY TO ANALYSE ENVELOPE.\n");
  2349. return(MEMORY_ERROR);
  2350. }
  2351. if((cuttime = (int *)malloc(bigarray/2 * sizeof(int)))==NULL) {
  2352. sprintf(errstr,"INSUFFICIENT MEMORY TO ANALYSE ENVELOPE.\n");
  2353. return(MEMORY_ERROR);
  2354. }
  2355. if((exit_status = get_envelope_troughs(trofpnt,&trofpntcnt,envcnt,dz))<0)
  2356. return(exit_status);
  2357. if((scanarray = (double *)malloc((maxwsize + 20) * sizeof(double)))==NULL) {
  2358. sprintf(errstr,"Insufficient memory for array to scan for zero-crossings.\n");
  2359. return(MEMORY_ERROR);
  2360. }
  2361. if((sndseekEx(dz->ifd[0],0,0)<0)){
  2362. sprintf(errstr,"sndseek() failed\n");
  2363. return SYSTEM_ERROR;
  2364. }
  2365. dz->total_samps_read = 0;
  2366. dz->samps_left = dz->insams[0];
  2367. if(sloom)
  2368. display_virtual_time(dz->total_samps_read,dz);
  2369. fprintf(stdout,"INFO: Calculating cut points.\n");
  2370. fflush(stdout);
  2371. if((exit_status = read_samps(ibuf,dz))<0)
  2372. return(exit_status);
  2373. k = 0;
  2374. cutcnt = 0;
  2375. if(dz->env[trofpnt[0]] <= 0.0) /* if first trof is at zero, skip that one */
  2376. k++;
  2377. for(n = k;n<trofpntcnt;n++) { /* find places to cut the source */
  2378. if((exit_status = find_min_energy_downward_zero_crossing_point(&n,trofpnt,trofpntcnt,scanarray,&cutcnt,cuttime,0,cutstart,dz)) < 0)
  2379. return(exit_status);
  2380. cutcnt++;
  2381. }
  2382. if((exit_status = smooth_cuts(cuttime,&cutcnt,0,cutstart,dz))<0)
  2383. return(exit_status);
  2384. #ifdef PSOWTEST
  2385. for(n = 0;n < cutcnt;n++) {
  2386. fprintf(stdout,"INFO: cuttime[%d] = %d\n",n,cuttime[n]);
  2387. fflush(stdout);
  2388. }
  2389. #endif
  2390. if((exit_status = chop_zero_signal_areas_into_compatible_units(&cutcnt,cuttime,cutstart,0,dz)) < 0)
  2391. return(exit_status);
  2392. if(dz->process == PSOW_INTERLEAVE || dz->process == PSOW_REPLACE) {
  2393. free(dz->env);
  2394. if((dz->env=(float *)malloc(((envcnt2+20) * 2) * sizeof(float)))==NULL) {
  2395. sprintf(errstr,"INSUFFICIENT MEMORY for 2nd envelope array.\n");
  2396. return(MEMORY_ERROR);
  2397. }
  2398. memset(dz->env,0,(envcnt2+20) * 2 * sizeof(float)); /* PRESET env VALS TO ZERO */
  2399. /* PART ENVELOPE vals CAN THEN BE STORED, where env-extract overlaps a buffer boundary */
  2400. fprintf(stdout,"INFO: Extracting pitch-related envelope from 2nd file.\n");
  2401. fflush(stdout);
  2402. maxwsize = 0;
  2403. if((exit_status = extract_pitch_dependent_env_from_sndfile(minwsize2,1,&maxwsize,dz))<0)
  2404. return(exit_status);
  2405. envcnt2 = dz->envend - dz->env;
  2406. // NB EACH ENVELOPE-ITEM CONSISTS OF TWO VALS (time & env-val).
  2407. // REVISED HERE TAKING THIS INTO ACCOUNT
  2408. fprintf(stdout,"INFO: Locating envelope peaks for 2nd file.\n");
  2409. fflush(stdout);
  2410. free(trofpnt);
  2411. if((trofpnt = (int *)malloc(envcnt2/2 * sizeof(int)))==NULL) {
  2412. sprintf(errstr,"INSUFFICIENT MEMORY TO ANALYSE ENVELOPE OF 2ND FILE.\n");
  2413. return(MEMORY_ERROR);
  2414. }
  2415. trofpntcnt = 0;
  2416. if((exit_status = get_envelope_troughs(trofpnt,&trofpntcnt,envcnt2,dz))<0)
  2417. return(exit_status);
  2418. free(scanarray);
  2419. if((scanarray = (double *)malloc((maxwsize + 20) * sizeof(double)))==NULL) {
  2420. sprintf(errstr,"Insufficient memory for array to scan for zero-crossings in 2nd file.\n");
  2421. return(MEMORY_ERROR);
  2422. }
  2423. if((sndseekEx(dz->ifd[1],0,0)<0)){
  2424. sprintf(errstr,"sndseek() failed\n");
  2425. return SYSTEM_ERROR;
  2426. }
  2427. dz->total_samps_read = 0;
  2428. dz->samps_left = dz->insams[1];
  2429. if(sloom)
  2430. display_virtual_time(dz->total_samps_read,dz);
  2431. fprintf(stdout,"INFO: Calculating cut points in 2nd file.\n");
  2432. fflush(stdout);
  2433. if((dz->ssampsread = fgetfbufEx(dz->sampbuf[0], dz->buflen,dz->ifd[1],0)) < 0) {
  2434. sprintf(errstr,"Can't read samples from input soundfile 2\n");
  2435. return(SYSTEM_ERROR);
  2436. }
  2437. dz->total_samps_read += dz->ssampsread;
  2438. k = 0;
  2439. cutstart = cutcnt;
  2440. if(dz->env[trofpnt[0]] <= 0.0) /* if first trof is at zero, skip that one */
  2441. k++;
  2442. for(n = k;n<trofpntcnt;n++) { /* find places to cut the source */
  2443. if((exit_status = find_min_energy_downward_zero_crossing_point(&n,trofpnt,trofpntcnt,scanarray,&cutcnt,cuttime,1,cutstart,dz)) < 0)
  2444. return(exit_status);
  2445. cutcnt++;
  2446. }
  2447. if((exit_status = smooth_cuts(cuttime,&cutcnt,1,cutstart,dz))<0)
  2448. return(exit_status);
  2449. if((exit_status = chop_zero_signal_areas_into_compatible_units(&cutcnt,cuttime,cutstart,1,dz)) < 0)
  2450. return(exit_status);
  2451. }
  2452. }
  2453. opos = 0;
  2454. switch(dz->process) {
  2455. case(PSOW_FREEZE):
  2456. j = (int)round(dz->param[1] * srate); /* Time at which to look for frozen segment */
  2457. for(n = 0; n < cutcnt;n++) {
  2458. if(cuttime[n] >= j) {
  2459. if(cuttime[n] == j)
  2460. n++;
  2461. break;
  2462. }
  2463. }
  2464. n--;
  2465. startsamp = cuttime[n];
  2466. j = dz->iparam[PS_SEGS]; /* no. of segs to use */
  2467. if((k = n+j) > cutcnt) {
  2468. sprintf(errstr,"Insufficient segments after time %f secs\n",dz->param[1]);
  2469. return(GOAL_FAILED);
  2470. }
  2471. if(k == cutcnt) {
  2472. endsamp = dz->insams[0];
  2473. if(k-1 > n)
  2474. fprintf(stdout,"INFO: grabbing grain-segments %d at time %lf to end of file\n",n,(double)startsamp/srate);
  2475. else
  2476. fprintf(stdout,"INFO: grabbing grain-segment %d at time %lf to end of file\n",n,(double)startsamp/srate);
  2477. fflush(stdout);
  2478. } else {
  2479. endsamp = cuttime[k];
  2480. if(k-1 > n)
  2481. fprintf(stdout,"INFO: grabbing grain-segments %d at time %lf to %d\n",n,(double)startsamp/srate,k-1);
  2482. else
  2483. fprintf(stdout,"INFO: grabbing grain-segment %d at time %lf\n",n,(double)startsamp/srate);
  2484. fflush(stdout);
  2485. }
  2486. seglen = endsamp - startsamp;
  2487. if(seglen > dz->buflen) {
  2488. sprintf(errstr,"Specified chunk is too large to fit in buffer\n");
  2489. return(PROGRAM_ERROR);
  2490. }
  2491. if((sndseekEx(dz->ifd[0],startsamp,0)<0)){
  2492. sprintf(errstr,"sndseek() failed\n");
  2493. return SYSTEM_ERROR;
  2494. }
  2495. if((exit_status = read_samps(ibuf,dz))<0)
  2496. return(exit_status);
  2497. for(n=0;n<seglen;n++)
  2498. ibuf[n] = (float)(ibuf[n] * dz->param[PS_GAIN]); /* adjust gain of input */
  2499. maxwritesize = (int)round(dz->param[PS_DUR] * srate);
  2500. if(maxwritesize == 0) {
  2501. one_grain = 1;
  2502. maxwritesize = 1;
  2503. }
  2504. writesize = 0;
  2505. here_in_buf = 0;
  2506. maxamp = 0.0;
  2507. while(writesize < maxwritesize) {
  2508. time = (double)writesize/srate;
  2509. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  2510. return(exit_status);
  2511. if(!flteq(dz->param[PS_TRNS],1.0)) {
  2512. thisplace = 0.0;
  2513. transinc = dz->param[PS_TRNS];
  2514. obuf[opos++] = ibuf[0];
  2515. while(thisplace < (double)seglen) {
  2516. thisplace += transinc;
  2517. j = (int)floor(thisplace);
  2518. k = j+1;
  2519. diff = thisplace - (double)j;
  2520. valdiff = ibuf[k] - ibuf[j];
  2521. valdiff *= diff;
  2522. val = (float)(ibuf[j] + valdiff);
  2523. if((k >= seglen) && (val < 0.0)) /* Seg ends just before seg crosses zero downwards. */
  2524. val = 0.0; /* If interpolating from final samp to first samp after, */
  2525. /* once interpolated value crosses zero, break */
  2526. obuf[opos++] += val;
  2527. }
  2528. } else {
  2529. for(n=0;n<seglen;n++)
  2530. obuf[opos++] += ibuf[n];
  2531. }
  2532. if(one_grain)
  2533. break;
  2534. dstep = (double)seglen/dz->param[PS_DENS];
  2535. if(dz->param[PS_RAND] > 0.0) {
  2536. rand = drand48() - 0.5; /* rand number between -0.5 & 0.5 */
  2537. rand *= dz->param[PS_RAND]; /* reduced according to degree of randomness */
  2538. rand *= dstep;
  2539. dstep += rand;
  2540. }
  2541. if((j = (int)round(dstep)) <= 0) {
  2542. if(!iswarned) {
  2543. fprintf(stdout,"WARNING: Density too high at %lf secs: using max density %d\n",time,seglen);
  2544. fflush(stdout);
  2545. iswarned = 1;
  2546. }
  2547. j = 1;
  2548. }
  2549. here_in_buf += j;
  2550. writesize += j;
  2551. if(here_in_buf > dz->buflen * 2) {
  2552. sprintf(errstr,"Output buffer overflowed.\n");
  2553. return(PROGRAM_ERROR);
  2554. }
  2555. if(here_in_buf + dz->total_samps_written >= maxwritesize)
  2556. break;
  2557. if(here_in_buf >= dz->buflen) {
  2558. for(n=0;n<dz->buflen;n++)
  2559. maxamp = max(fabs(obuf[n]),maxamp);
  2560. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  2561. return(exit_status);
  2562. memset((char*)obuf,0,dz->buflen * sizeof(float));
  2563. memcpy((char*)obuf,(char*)obuf2,(opos - dz->buflen) * sizeof(float));
  2564. memset((char*)obuf2,0,dz->buflen * sizeof(float));
  2565. here_in_buf -= dz->buflen;
  2566. }
  2567. opos = here_in_buf;
  2568. }
  2569. if(opos > 0) {
  2570. if((exit_status = write_samps(obuf,opos,dz))<0)
  2571. return(exit_status);
  2572. }
  2573. if(maxamp > 1.0) {
  2574. fprintf(stdout,"ERROR: Output overloaded: set gain (at least) to %.2lf\n",1.0/maxamp);
  2575. fflush(stdout);
  2576. }
  2577. return FINISHED;
  2578. case(PSOW_CHOP):
  2579. /* CHECK INPUT DATA, NOW SEGS ARE LOCATED */
  2580. if((dz->brksize[1] + 1) > 999) { /* Check for too many segments */
  2581. sprintf(errstr,"Too many segments to cut (%d)\n",dz->brksize[1]);
  2582. return(GOAL_FAILED);
  2583. }
  2584. for(n=0,m=0;n<dz->brksize[1];n++,m+=2) { /* Check input data */
  2585. here = (int)round(dz->brk[1][m] * srate);
  2586. seglen = (int)round(dz->brk[1][m+1]);
  2587. for(j=0;j<cutcnt;j++) {
  2588. if(cuttime[j] >= here) {
  2589. if(cuttime[j] == here)
  2590. j++;
  2591. break;
  2592. }
  2593. }
  2594. j--;
  2595. if(j == 0) /* Time before first segend (before first cut) */
  2596. k = 0; /* Chop at end of first seg (a chop before this does nothing) */
  2597. else if(j == cutcnt) /* Time in last seg (after last cut) */
  2598. k = cutcnt - 1; /* Chop before last seg */
  2599. else
  2600. k = j; /* Time within segement after cut j: chop out this segment */
  2601. j = k;
  2602. k += seglen;
  2603. if(k > cutcnt) {
  2604. sprintf(errstr,"Insufficient segments (%d asked for) after time %lf\n",seglen,dz->brk[1][m]);
  2605. return(GOAL_FAILED);
  2606. }
  2607. if(seglen > 1)
  2608. fprintf(stdout,"INFO: cutting around grain-segments %d at time %lf to %d\n",j,dz->brk[1][m],k-1);
  2609. else
  2610. fprintf(stdout,"INFO: cutting around grain-segment %d at time %lf\n",j,dz->brk[1][m]);
  2611. }
  2612. /* COPY FILENAME, FOR GENERIC USE FOR ALL OUTFILES */
  2613. if(!sloom)
  2614. keep_filename_tail_only(dz);
  2615. namelen = strlen(dz->wordstor[0]);
  2616. if((outfilename = (char *)malloc((namelen + 5) * sizeof(char)))==NULL) {
  2617. sprintf(errstr,"INSUFFICIENT MEMORY for outfilename.\n");
  2618. return(MEMORY_ERROR);
  2619. }
  2620. strcpy(outfilename,dz->wordstor[0]);
  2621. /* START READING INFLE */
  2622. dz->total_samps_read = 0;
  2623. dz->samps_left = dz->insams[0];
  2624. if((sndseekEx(dz->ifd[0],0,0)<0)){
  2625. sprintf(errstr,"sndseek() failed\n");
  2626. return SYSTEM_ERROR;
  2627. }
  2628. if((exit_status = read_samps(ibuf,dz))<0)
  2629. return(exit_status);
  2630. startsamp = 0;
  2631. bufstart = 0;
  2632. there_in_buf = 0;
  2633. /* CUT SEGMENTS FROM THE INFILE */
  2634. for(n=0,m=0;n<dz->brksize[1];n++,m+=2) { /* Read cutpoints */
  2635. memset((char *)obuf,0,dz->buflen * sizeof(float));
  2636. opos = 0;
  2637. here = (int)round(dz->brk[1][m] * dz->infile->srate);
  2638. seglen = (int)round(dz->brk[1][m+1]);
  2639. for(j=0;j<cutcnt;j++) {
  2640. if(cuttime[j] >= here) {
  2641. if(cuttime[j] == here)
  2642. j++;
  2643. break;
  2644. }
  2645. }
  2646. j--;
  2647. if(j == 0)
  2648. k = -1;
  2649. else if(j == cutcnt)
  2650. k = cutcnt - 1;
  2651. else
  2652. k = j;
  2653. here = cuttime[k];
  2654. while(here > dz->total_samps_read) { /* If not enough samps in buf to reach "here" */
  2655. for(j=there_in_buf;j < dz->ssampsread;j++) { /* Write rest of samps in buffer ETC. */
  2656. obuf[opos++] = ibuf[j];
  2657. if(opos >= dz->ssampsread) {
  2658. if((exit_status = write_samps(obuf,opos,dz))<0)
  2659. return(exit_status);
  2660. opos = 0;
  2661. }
  2662. }
  2663. if((exit_status = read_samps(ibuf,dz))<0)
  2664. return(exit_status);
  2665. there_in_buf = 0;
  2666. bufstart += dz->buflen;
  2667. }
  2668. here_in_buf = here - bufstart;
  2669. for(j=there_in_buf;j < here_in_buf;j++) { /* Write samps between last start and here */
  2670. obuf[opos++] = ibuf[j];
  2671. if(opos >= dz->ssampsread) {
  2672. if((exit_status = write_samps(obuf,opos,dz))<0)
  2673. return(exit_status);
  2674. opos = 0;
  2675. }
  2676. }
  2677. if(opos > 0) {
  2678. if((exit_status = write_samps(obuf,opos,dz))<0)
  2679. return(exit_status);
  2680. }
  2681. /* WRITE HEADER AND CLOSE FILE */
  2682. if((exit_status = headwrite(dz->ofd,dz))<0) {
  2683. free(outfilename);
  2684. return(exit_status);
  2685. }
  2686. if((exit_status = reset_peak_finder(dz))<0)
  2687. return(exit_status);
  2688. if(sndcloseEx(dz->ofd) < 0) {
  2689. fprintf(stdout,"WARNING: Can't close output soundfile %s\n",outfilename);
  2690. fflush(stdout);
  2691. }
  2692. dz->ofd = -1;
  2693. /* CREATE NEXT OUTFILE */
  2694. strcpy(outfilename,dz->wordstor[0]);
  2695. create_next_filename(outfilename,(int)n);
  2696. if((exit_status = create_sized_outfile(outfilename,dz))<0) {
  2697. sprintf(errstr,"Failed to make segment %d and beyond\n",n+1);
  2698. return(exit_status);
  2699. }
  2700. /* SKIP OVER THE GRAIN(S) */
  2701. startsamp = here + seglen;
  2702. while(startsamp > dz->total_samps_read) {
  2703. if((exit_status = read_samps(ibuf,dz))<0)
  2704. return(exit_status);
  2705. bufstart += dz->buflen;
  2706. }
  2707. there_in_buf = startsamp - bufstart;
  2708. }
  2709. /* WRITE SEGMENT REMAINING AT FILE END */
  2710. memset((char *)obuf,0,dz->buflen * sizeof(float));
  2711. opos = 0;
  2712. while(dz->ssampsread > 0) {
  2713. for(j = there_in_buf; j < dz->ssampsread; j++) {
  2714. obuf[opos++] = ibuf[j];
  2715. if(opos >= dz->buflen) {
  2716. if((exit_status = write_samps(obuf,opos,dz))<0)
  2717. return(exit_status);
  2718. opos = 0;
  2719. }
  2720. }
  2721. if((exit_status = read_samps(ibuf,dz))<0)
  2722. return(exit_status);
  2723. there_in_buf = 0;
  2724. }
  2725. if(opos > 0) {
  2726. if((exit_status = write_samps(obuf,opos,dz))<0)
  2727. return(exit_status);
  2728. }
  2729. return FINISHED;
  2730. }
  2731. here = 0;
  2732. dz->total_samps_read = 0;
  2733. dz->samps_left = dz->insams[0];
  2734. if((sndseekEx(dz->ifd[0],0,0)<0)){
  2735. sprintf(errstr,"sndseek() failed\n");
  2736. return SYSTEM_ERROR;
  2737. }
  2738. memset((char*)ibuf,0,dz->buflen * sizeof(float));
  2739. if((exit_status = read_samps(ibuf,dz))<0)
  2740. return(exit_status);
  2741. if(dz->process != PSOW_LOCATE) {
  2742. fprintf(stdout,"INFO: Creating the outfile.\n");
  2743. fflush(stdout);
  2744. }
  2745. switch(dz->process) {
  2746. case(PSOW_FEATURES):
  2747. memset((char*)ibuf2,0,dz->buflen * sizeof(float));
  2748. if((exit_status = read_samps(ibuf2,dz))<0)
  2749. return(exit_status);
  2750. j = dz->iparam[1];
  2751. break;
  2752. case(PSOW_SYNTH):
  2753. case(PSOW_IMPOSE):
  2754. case(PSOW_SPLIT):
  2755. case(PSOW_SPACE):
  2756. memset((char*)ibuf2,0,dz->buflen * sizeof(float));
  2757. if((exit_status = read_samps(ibuf2,dz))<0)
  2758. return(exit_status);
  2759. j = 1;
  2760. break;
  2761. case(PSOW_REPLACE):
  2762. memset((char*)ibuf2,0,dz->buflen * sizeof(float));
  2763. if((exit_status = read_samps(ibuf2,dz))<0)
  2764. return(exit_status);
  2765. j = dz->iparam[2];
  2766. break;
  2767. case(PSOW_EXTEND2):
  2768. last_sampsread = dz->ssampsread;
  2769. /* fall thro */
  2770. case(PSOW_EXTEND):
  2771. memset((char*)ibuf2,0,dz->buflen * sizeof(float));
  2772. if((exit_status = read_samps(ibuf2,dz))<0)
  2773. return(exit_status);
  2774. j = dz->iparam[3];
  2775. break;
  2776. case(PSOW_INTERLEAVE):
  2777. memset((char*)ibuf2,0,dz->buflen * sizeof(float));
  2778. if((exit_status = read_samps(ibuf2,dz))<0)
  2779. return(exit_status);
  2780. if((exit_status = read_samps(ibuf2,dz))<0)
  2781. return(exit_status);
  2782. if((sndseekEx(dz->ifd[1],0,0)<0)){
  2783. sprintf(errstr,"sndseek() failed\n");
  2784. return SYSTEM_ERROR;
  2785. }
  2786. memset((char*)ibuf3,0,dz->buflen * sizeof(float));
  2787. if((ssampsread2 = fgetfbufEx(ibuf3, dz->buflen,dz->ifd[1],0)) < 0) {
  2788. sprintf(errstr,"Can't read samples from input soundfile 2\n");
  2789. return(SYSTEM_ERROR);
  2790. }
  2791. memset((char*)ibuf4,0,dz->buflen * sizeof(float));
  2792. if((ssampsread2 = fgetfbufEx(ibuf4, dz->buflen,dz->ifd[1],0)) < 0) {
  2793. sprintf(errstr,"Can't read samples from input soundfile 2\n");
  2794. return(SYSTEM_ERROR);
  2795. }
  2796. j = dz->iparam[2];
  2797. break;
  2798. case(PSOW_REINF):
  2799. memset((char*)ibuf2,0,dz->buflen * sizeof(float));
  2800. if((exit_status = read_samps(ibuf2,dz))<0)
  2801. return(exit_status);
  2802. j = 1;
  2803. break;
  2804. default:
  2805. j = dz->iparam[2];
  2806. break;
  2807. }
  2808. if(dz->process != PSOW_EXTEND2) {
  2809. if(j >= cutcnt) {
  2810. sprintf(errstr,"Insufficient Grains in source (%d) to create blocksize requested (%d).\n",cutcnt,j);
  2811. return(DATA_ERROR);
  2812. }
  2813. }
  2814. lastwrite = 0;
  2815. startsamp = 0;
  2816. stsmoothed[0] = 0;
  2817. stsmoothed[1] = 0;
  2818. if(dz->process != PSOW_LOCATE) {
  2819. memset((char*)obuf,0,dz->buflen * sizeof(float));
  2820. memset((char*)obuf2,0,dz->buflen * sizeof(float));
  2821. }
  2822. switch(dz->process) {
  2823. case(PSOW_STRETCH):
  2824. maxsamp = 0.0;
  2825. ovflw = dz->sampbuf[3];
  2826. memset((char*)ovflw,0,dz->buflen * sizeof(float));
  2827. for(n = j-1; n < cutcnt;n+=j) {
  2828. here_in_buf = here - startsamp;
  2829. there_in_buf = cuttime[n] - startsamp;
  2830. if(dz->brksize[1]) {
  2831. time = here/srate;
  2832. if((exit_status = read_value_from_brktable(time,1,dz))<0)
  2833. return(exit_status);
  2834. }
  2835. opos = (int)round(here * dz->param[1]); /* absolute output position */
  2836. opos -= lastwrite; /* output position relative to buf start */
  2837. if((overflow = opos - (dz->buflen * 2))>0) {
  2838. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,dz->buflen,0,obuf,1,dz)) < 0)
  2839. return(exit_status);
  2840. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,dz->buflen,0,obuf,1,dz)) < 0)
  2841. return(exit_status);
  2842. for(m = 0;m < dz->buflen;m++)
  2843. maxsamp = max(maxsamp,fabs(obuf[m]));
  2844. lastwrite += dz->buflen;
  2845. memcpy((char*)obuf,(char*)obuf2,dz->buflen * sizeof(float));
  2846. memset((char*)obuf2,0,dz->buflen * sizeof(float));
  2847. memcpy((char*)obuf2,(char*)ovflw,overflow * sizeof(float));
  2848. memset((char*)ovflw,0,dz->buflen * sizeof(float));
  2849. opos -= dz->buflen;
  2850. }
  2851. if((opos + (there_in_buf - here_in_buf)) >= dz->buflen * 3) {
  2852. sprintf(errstr,"OVERFLOWED OUTPUT BUFFER.\n");
  2853. return(MEMORY_ERROR);
  2854. }
  2855. while(there_in_buf > dz->buflen) {
  2856. there_in_buf = dz->buflen;
  2857. for(k = here_in_buf; k < there_in_buf;k++)
  2858. obuf[opos++] += ibuf[k];
  2859. if((exit_status = read_samps(ibuf,dz))<0)
  2860. return(exit_status);
  2861. startsamp += dz->buflen;
  2862. here_in_buf = 0;
  2863. there_in_buf = cuttime[n] - startsamp;
  2864. }
  2865. for(k = here_in_buf; k < there_in_buf;k++)
  2866. obuf[opos++] += ibuf[k];
  2867. here = cuttime[n];
  2868. }
  2869. if(opos > 0) {
  2870. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,opos,1,obuf,1,dz)) < 0)
  2871. return(exit_status);
  2872. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,opos,1,obuf,1,dz)) < 0)
  2873. return(exit_status);
  2874. for(m = 0;m < opos;m++)
  2875. maxsamp = max(maxsamp,fabs(obuf[m]));
  2876. }
  2877. if(maxsamp > 1.0)
  2878. normaliser = 1.0/maxsamp;
  2879. else
  2880. normaliser = 1.0;
  2881. memset((char*)obuf,0,dz->buflen * sizeof(float));
  2882. memset((char*)obuf2,0,dz->buflen * sizeof(float));
  2883. if((sndseekEx(dz->ifd[0],0,0)<0)){
  2884. sprintf(errstr,"sndseek() failed\n");
  2885. return SYSTEM_ERROR;
  2886. }
  2887. here = 0;
  2888. ovflw = dz->sampbuf[3];
  2889. memset((char*)ovflw,0,dz->buflen * sizeof(float));
  2890. for(n = j-1; n < cutcnt;n+=j) {
  2891. here_in_buf = here - startsamp;
  2892. there_in_buf = cuttime[n] - startsamp;
  2893. if(dz->brksize[1]) {
  2894. time = here/srate;
  2895. if((exit_status = read_value_from_brktable(time,1,dz))<0)
  2896. return(exit_status);
  2897. }
  2898. opos = (int)round(here * dz->param[1]); /* absolute output position */
  2899. opos -= lastwrite; /* output position relative to buf start */
  2900. if((overflow = opos - (dz->buflen * 2))>0) {
  2901. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,dz->buflen,0,obuf,1,dz)) < 0)
  2902. return(exit_status);
  2903. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,dz->buflen,0,obuf,1,dz)) < 0)
  2904. return(exit_status);
  2905. for(m = 0;m < dz->buflen;m++)
  2906. obuf[m] = (float)(obuf[m] * normaliser);
  2907. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  2908. return(exit_status);
  2909. lastwrite += dz->buflen;
  2910. memcpy((char*)obuf,(char*)obuf2,dz->buflen * sizeof(float));
  2911. memset((char*)obuf2,0,dz->buflen * sizeof(float));
  2912. memcpy((char*)obuf2,(char*)ovflw,overflow * sizeof(float));
  2913. memset((char*)ovflw,0,dz->buflen * sizeof(float));
  2914. opos -= dz->buflen;
  2915. }
  2916. if((opos + (there_in_buf - here_in_buf)) >= dz->buflen * 3) {
  2917. sprintf(errstr,"OVERFLOWED OUTPUT BUFFER.\n");
  2918. return(MEMORY_ERROR);
  2919. }
  2920. while(there_in_buf > dz->buflen) {
  2921. there_in_buf = dz->buflen;
  2922. for(k = here_in_buf; k < there_in_buf;k++)
  2923. obuf[opos++] += ibuf[k];
  2924. if((exit_status = read_samps(ibuf,dz))<0)
  2925. return(exit_status);
  2926. startsamp += dz->buflen;
  2927. here_in_buf = 0;
  2928. there_in_buf = cuttime[n] - startsamp;
  2929. }
  2930. for(k = here_in_buf; k < there_in_buf;k++)
  2931. obuf[opos++] += ibuf[k];
  2932. here = cuttime[n];
  2933. }
  2934. if(opos > 0) {
  2935. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,opos,1,obuf,1,dz)) < 0)
  2936. return(exit_status);
  2937. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,opos,1,obuf,1,dz)) < 0)
  2938. return(exit_status);
  2939. for(m = 0;m < opos;m++)
  2940. obuf[m] = (float)(obuf[m] * normaliser);
  2941. if((exit_status = write_samps(obuf,opos,dz))<0)
  2942. return(exit_status);
  2943. }
  2944. return(FINISHED);
  2945. case(PSOW_DUPL):
  2946. maxwritesize = 0;
  2947. for(n = j-1; n < cutcnt;n+=j) {
  2948. here_in_buf = here - startsamp;
  2949. there_in_buf = cuttime[n] - startsamp;
  2950. writesize = there_in_buf - here_in_buf;
  2951. if(writesize > maxwritesize)
  2952. maxwritesize = writesize;
  2953. here = cuttime[n];
  2954. }
  2955. here = 0;
  2956. if(maxwritesize > dz->buflen) {
  2957. free(dz->sampbuf[3]);
  2958. if((dz->sampbuf[3] = (float *)((maxwritesize + 2) * sizeof(float)))==NULL) {
  2959. sprintf(errstr,"INSUFFICIENT MEMORY TO STORE GRAIN-BLOCKS\n");
  2960. return(MEMORY_ERROR);
  2961. }
  2962. }
  2963. xbuf = dz->sampbuf[3];
  2964. for(n = j-1; n < cutcnt;n+=j) {
  2965. if(dz->brksize[1]) {
  2966. time = here/srate;
  2967. if((exit_status = read_value_from_brktable(time,1,dz))<0)
  2968. return(exit_status);
  2969. }
  2970. here_in_buf = here - startsamp;
  2971. there_in_buf = cuttime[n] - startsamp;
  2972. writesize = there_in_buf - here_in_buf;
  2973. xbufpos = 0;
  2974. while(there_in_buf > dz->buflen) {
  2975. there_in_buf = dz->buflen;
  2976. for(k = here_in_buf; k < there_in_buf;k++)
  2977. xbuf[xbufpos++] = ibuf[k];
  2978. if((exit_status = read_samps(ibuf,dz))<0)
  2979. return(exit_status);
  2980. startsamp += dz->buflen;
  2981. here_in_buf = 0;
  2982. there_in_buf = cuttime[n] - startsamp;
  2983. }
  2984. for(k = here_in_buf; k < there_in_buf;k++)
  2985. xbuf[xbufpos++] = ibuf[k];
  2986. for(m=0;m<dz->iparam[1];m++) {
  2987. for(k = 0;k < xbufpos;k++) {
  2988. obuf[opos++] = xbuf[k];
  2989. if(opos >= dz->buflen * 2) {
  2990. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,dz->buflen,0,obuf,1,dz)) < 0)
  2991. return(exit_status);
  2992. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,dz->buflen,0,obuf,1,dz)) < 0)
  2993. return(exit_status);
  2994. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  2995. return(exit_status);
  2996. memcpy((char *)obuf,(char *)obuf2,dz->buflen * sizeof(float));
  2997. memset((char *)obuf2,0,dz->buflen * sizeof(float));
  2998. opos = 0;
  2999. }
  3000. }
  3001. }
  3002. here = cuttime[n];
  3003. }
  3004. break;
  3005. case(PSOW_DEL):
  3006. for(n = j; n < cutcnt;n+=j) {
  3007. here_in_buf = here - startsamp;
  3008. there_in_buf = cuttime[n] - startsamp;
  3009. while(there_in_buf > dz->buflen) {
  3010. there_in_buf = dz->buflen;
  3011. for(k = here_in_buf; k < there_in_buf;k++) {
  3012. obuf[opos++] = ibuf[k];
  3013. if(opos >= dz->buflen * 2) {
  3014. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,dz->buflen,0,obuf,1,dz)) < 0)
  3015. return(exit_status);
  3016. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,dz->buflen,0,obuf,1,dz)) < 0)
  3017. return(exit_status);
  3018. if((exit_status = write_samps(obuf,dz->buflen ,dz))<0)
  3019. return(exit_status);
  3020. memcpy((char *)obuf,(char *)obuf2,dz->buflen * sizeof(float));
  3021. opos = 0;
  3022. }
  3023. }
  3024. if((exit_status = read_samps(ibuf,dz))<0)
  3025. return(exit_status);
  3026. startsamp += dz->buflen;
  3027. here_in_buf = 0;
  3028. there_in_buf = cuttime[n] - startsamp;
  3029. }
  3030. for(k = here_in_buf; k < there_in_buf;k++) {
  3031. obuf[opos++] = ibuf[k];
  3032. if(opos >= dz->buflen * 2) {
  3033. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,dz->buflen,0,obuf,1,dz)) < 0)
  3034. return(exit_status);
  3035. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,dz->buflen,0,obuf,1,dz)) < 0)
  3036. return(exit_status);
  3037. if((exit_status = write_samps(obuf,dz->buflen ,dz))<0)
  3038. return(exit_status);
  3039. memcpy((char *)obuf,(char *)obuf2,dz->buflen * sizeof(float));
  3040. opos = 0;
  3041. }
  3042. }
  3043. if(dz->brksize[1]) {
  3044. time = here/srate;
  3045. if((exit_status = read_value_from_brktable(time,1,dz))<0)
  3046. return(exit_status);
  3047. }
  3048. n += (dz->iparam[1] - 1) * j; /* SKIP GRAINS */
  3049. here = cuttime[n];
  3050. }
  3051. break;
  3052. case(PSOW_STRFILL):
  3053. if(!dz->brksize[3])
  3054. dz->param[3] /= SEMITONES_PER_OCTAVE;
  3055. ovflw = dz->sampbuf[3];
  3056. xbuf = dz->sampbuf[4];
  3057. memset((char*)ovflw,0,dz->buflen * sizeof(float));
  3058. memset((char*)xbuf,0,dz->buflen * sizeof(float));
  3059. for(n = j-1; n < cutcnt;n+=j) {
  3060. here_in_buf = here - startsamp;
  3061. there_in_buf = cuttime[n] - startsamp;
  3062. if((seglen = there_in_buf - here_in_buf) > dz->buflen) {
  3063. sprintf(errstr,"Too long grain encountered at %f secs\n",here/srate);
  3064. return(GOAL_FAILED);
  3065. }
  3066. if(dz->brksize[1]) {
  3067. time = here/srate;
  3068. if((exit_status = read_value_from_brktable(time,1,dz))<0)
  3069. return(exit_status);
  3070. }
  3071. opos = (int)round(here * dz->param[1]); /* absolute output position */
  3072. opos -= lastwrite; /* output position relative to buf start */
  3073. if((overflow = opos - (dz->buflen * 2))>0) {
  3074. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,dz->buflen,0,obuf,1,dz)) < 0)
  3075. return(exit_status);
  3076. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,dz->buflen,0,obuf,1,dz)) < 0)
  3077. return(exit_status);
  3078. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  3079. return(exit_status);
  3080. lastwrite += dz->buflen;
  3081. memcpy((char*)obuf,(char*)obuf2,dz->buflen * sizeof(float));
  3082. memset((char*)obuf2,0,dz->buflen * sizeof(float));
  3083. memcpy((char*)obuf2,(char*)ovflw,overflow * sizeof(float));
  3084. memset((char*)ovflw,0,dz->buflen * sizeof(float));
  3085. opos -= dz->buflen;
  3086. }
  3087. if((opos + seglen) >= dz->buflen * 3) {
  3088. sprintf(errstr,"OVERFLOWED OUTPUT BUFFER.\n");
  3089. return(MEMORY_ERROR);
  3090. }
  3091. /* Fill the temporary buffer with the input segment */
  3092. xbufpos = 0;
  3093. while(there_in_buf > dz->buflen) {
  3094. there_in_buf = dz->buflen;
  3095. for(k = here_in_buf; k < there_in_buf;k++)
  3096. xbuf[xbufpos++] = ibuf[k];
  3097. if((exit_status = read_samps(ibuf,dz))<0)
  3098. return(exit_status);
  3099. startsamp += dz->buflen;
  3100. here_in_buf = 0;
  3101. there_in_buf = cuttime[n] - startsamp;
  3102. }
  3103. for(k = here_in_buf; k < there_in_buf;k++)
  3104. xbuf[xbufpos++] = ibuf[k];
  3105. if((exit_status = smooth_bad_grains(xbufpos,4,dz))<0)
  3106. return(exit_status);
  3107. /* FIND THE TIME WHERE NEXT SEGMENT WILL START */
  3108. time = cuttime[n]/srate;
  3109. if(dz->brksize[1]) {
  3110. if((exit_status = read_value_from_brktable(time,1,dz))<0)
  3111. return(exit_status);
  3112. }
  3113. if(dz->brksize[3]) {
  3114. if((exit_status = read_value_from_brktable(time,3,dz))<0)
  3115. return(exit_status);
  3116. dz->param[3] /= SEMITONES_PER_OCTAVE;
  3117. }
  3118. next_opos = (int)round(cuttime[n] * dz->param[1]); /* absolute output next-position */
  3119. next_opos -= lastwrite;
  3120. advance = next_opos - opos; /* gap between start of this seg and start of next */
  3121. transinc = pow(2.0,(double)dz->param[3]);
  3122. dstep = (double)seglen/transinc;
  3123. sum = 0.0;
  3124. repets = 0;
  3125. while(sum < (double)advance) {
  3126. sum += dstep;
  3127. repets++;
  3128. }
  3129. /* FIND HOW MANY SEGMENTS NEEDED TO FILL THE GAP, WITH (at least) SLIGHT OVERLAP OF SEGS */
  3130. thisplace = (double)opos;
  3131. for(k = 0; k < repets;k++) {
  3132. opos = round(thisplace);
  3133. /* ADD SOME RAND but don't go below zero !!! */
  3134. if((overflow = opos - (dz->buflen * 2))>0) {
  3135. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,dz->buflen,0,obuf,1,dz)) < 0)
  3136. return(exit_status);
  3137. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,dz->buflen,0,obuf,1,dz)) < 0)
  3138. return(exit_status);
  3139. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  3140. return(exit_status);
  3141. lastwrite += dz->buflen;
  3142. memcpy((char*)obuf,(char*)obuf2,dz->buflen * sizeof(float));
  3143. memset((char*)obuf2,0,dz->buflen * sizeof(float));
  3144. memcpy((char*)obuf2,(char*)ovflw,overflow * sizeof(float));
  3145. memset((char*)ovflw,0,dz->buflen * sizeof(float));
  3146. opos -= dz->buflen;
  3147. thisplace -= (double)dz->buflen;
  3148. }
  3149. for(m=0;m<seglen;m++)
  3150. obuf[opos++] += xbuf[m];
  3151. thisplace += dstep;
  3152. }
  3153. here = cuttime[n];
  3154. }
  3155. break;
  3156. case(PSOW_FEATURES):
  3157. lastwritend = 0;
  3158. maxlevel = 0.0;
  3159. cnt = 0;
  3160. for(n = j-1; n < cutcnt;n+=j) {
  3161. gain = dz->param[PSF_GAIN];
  3162. transpos = 0.0;
  3163. if(n<j) {
  3164. seglen = cuttime[n];
  3165. here = 0;
  3166. } else
  3167. seglen = cuttime[n] - cuttime[n-j];
  3168. startopos = opos + dz->total_samps_written;
  3169. here_in_buf = here - startsamp;
  3170. there_in_buf = cuttime[n] - startsamp;
  3171. time = startopos/srate;
  3172. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  3173. return(exit_status);
  3174. if(dz->iparam[PSF_SUB] > 1) {
  3175. if(cnt % dz->iparam[PSF_SUB] != 0)
  3176. gain *= 1.0 - dz->param[PSF_SUBGAIN];
  3177. }
  3178. cnt++;
  3179. if(!flteq(dz->param[PSF_TRNS],0.0))
  3180. transpos = pow(2.0,(dz->param[PSF_TRNS]/SEMITONES_PER_OCTAVE));
  3181. if((overflow = opos - dz->buflen)>0) {
  3182. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,dz->buflen,0,obuf,1,dz)) < 0)
  3183. return(exit_status);
  3184. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,dz->buflen,0,obuf,1,dz)) < 0)
  3185. return(exit_status);
  3186. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  3187. return(exit_status);
  3188. lastwritend -= dz->buflen;
  3189. lastwrite += dz->buflen;
  3190. memset((char*)obuf,0,dz->buflen * sizeof(float));
  3191. if(lastwritend > 0)
  3192. memcpy((char*)obuf,(char*)obuf2,lastwritend * sizeof(float));
  3193. memset((char*)obuf2,0,dz->buflen * sizeof(float));
  3194. opos -= dz->buflen;
  3195. }
  3196. if((opos + (there_in_buf - here_in_buf)) >= dz->buflen * 2) {
  3197. sprintf(errstr,"OVERFLOWED OUTPUT BUFFER.\n");
  3198. return(MEMORY_ERROR);
  3199. }
  3200. if(here_in_buf >= dz->buflen * 2) {
  3201. sprintf(errstr,"OVERFLOWED INPUT BUFFER.\n");
  3202. return(MEMORY_ERROR);
  3203. }
  3204. while(here_in_buf > dz->buflen) {
  3205. memset((char *)ibuf,0,dz->buflen * sizeof(float));
  3206. if(dz->ssampsread > 0) {
  3207. memcpy((char *)ibuf,(char *)ibuf2,dz->buflen * sizeof(float));
  3208. memset((char *)ibuf2,0,dz->buflen * sizeof(float));
  3209. if((exit_status = read_samps(ibuf2,dz))<0)
  3210. return(exit_status);
  3211. }
  3212. startsamp += dz->buflen;
  3213. here_in_buf -= dz->buflen;
  3214. there_in_buf = cuttime[n] - startsamp;
  3215. }
  3216. if(zerofof(here_in_buf,there_in_buf,ibuf)) {
  3217. for(k = here_in_buf; k < there_in_buf;k++)
  3218. opos++;
  3219. } else {
  3220. if(dz->iparam[PSF_FOFSTR] > 1) {
  3221. // INTERPOLATE MAX FOFS ... expt: doesn't really work -- too many glitches so far
  3222. // if((exit_status = interp_maxfofs(k,here_in_buf,there_in_buf,ibuf,obuf,cuttime,
  3223. // startsamp,gain,&opos,&n,maxlevel,cutcnt,&seglen,dz))<0)
  3224. // return(exit_status);
  3225. if((exit_status = fof_stretch(n,time,here_in_buf,there_in_buf,ibuf,obuf,&opos,&maxlevel,gain,dz))<0)
  3226. return(exit_status);
  3227. } else if(!flteq(dz->param[PSF_SPEC],0.0)) {
  3228. dhere = here_in_buf;
  3229. spectranspos = pow(2.0,(dz->param[PSF_SPEC]/SEMITONES_PER_OCTAVE));
  3230. while(dhere < there_in_buf) {
  3231. k = (int)floor(dhere);
  3232. diff = dhere - (double)k;
  3233. valdiff = ibuf[k+1] - ibuf[k];
  3234. valdiff *= diff;
  3235. sum = ibuf[k] + valdiff;
  3236. sum *= gain;
  3237. obuf[opos] = (float)(obuf[opos] + sum);
  3238. maxlevel = max(obuf[opos],maxlevel);
  3239. opos++;
  3240. dhere += spectranspos;
  3241. }
  3242. if(opos > lastwritend)
  3243. lastwritend = opos;
  3244. } else {
  3245. for(k = here_in_buf; k < there_in_buf;k++) {
  3246. sum = ibuf[k] * gain;
  3247. obuf[opos] = (float)(obuf[opos] + sum);
  3248. maxlevel = max(obuf[opos],maxlevel);
  3249. opos++;
  3250. }
  3251. }
  3252. }
  3253. if(opos > lastwritend)
  3254. lastwritend = opos;
  3255. endopos = startopos + seglen;
  3256. opos = endopos - dz->total_samps_written;
  3257. if(!flteq(transpos,0.0)) {
  3258. outseglen = (int)round((double)seglen/transpos);
  3259. transtep = outseglen - seglen;
  3260. seglen = outseglen;
  3261. opos += transtep;
  3262. if(dz->mode != 0) {
  3263. if(opos + dz->total_samps_written < cuttime[n]) /* i.e. dupl the last FOF until we catch up with */
  3264. n--; /* FOFs in orig */
  3265. }
  3266. }
  3267. here = cuttime[n];
  3268. if(dz->param[PS_VIBDEPTH] > 0.0)
  3269. opos += vibrato(seglen,dz->param[PS_VIBFRQ],dz->param[PS_VIBDEPTH],dz);
  3270. if(dz->param[PSF_RAND] > 0.0) {
  3271. rand = drand48() - 0.5; /* rand number between -0.5 & 0.5 */
  3272. rand *= dz->param[PSF_RAND]; /* reduced according to degree of randomness */
  3273. rand *= (double)seglen;
  3274. opos += (int)round(rand);
  3275. }
  3276. }
  3277. if(lastwritend > 0) {
  3278. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,lastwritend,1,obuf,1,dz)) < 0)
  3279. return(exit_status);
  3280. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,lastwritend,1,obuf,1,dz)) < 0)
  3281. return(exit_status);
  3282. if((exit_status = write_samps(obuf,lastwritend,dz))<0)
  3283. return(exit_status);
  3284. }
  3285. if(maxlevel > 1.0) {
  3286. fprintf(stdout,"WARNING: Signal distorted: maxlevel = %lf\n",maxlevel);
  3287. fflush(stdout);
  3288. }
  3289. return FINISHED;
  3290. case(PSOW_SYNTH):
  3291. lastwritend = 0;
  3292. maxseglen = cuttime[0];
  3293. for(n=1;n<cutcnt;n++)
  3294. maxseglen = max(maxseglen,cuttime[n] - cuttime[n-1]);
  3295. maxseglen = max(maxseglen,dz->insams[0] - cuttime[cutcnt-1]);
  3296. if((fofenv = (double *)malloc(maxseglen * sizeof(double)))==NULL) {
  3297. sprintf(errstr,"Insufficient memory for FOF envelope vals store\n");
  3298. return(MEMORY_ERROR);
  3299. }
  3300. if((fofloc = (int *)malloc(maxseglen * sizeof(int)))==NULL) {
  3301. sprintf(errstr,"Insufficient memory for FOF envelope times store\n");
  3302. return(MEMORY_ERROR);
  3303. }
  3304. if((sintab = (double *)malloc((unsigned int)round(SINTABLEN+1) * sizeof(double)))==NULL) {
  3305. sprintf(errstr,"Insufficient memory for SINE table\n");
  3306. return(MEMORY_ERROR);
  3307. }
  3308. dstep = TWOPI/SINTABLEN;
  3309. for(n=0;n<SINTABLEN;n++)
  3310. sintab[n] = sin((double)n * dstep);
  3311. sintab[n] = 0.0; /* wraparound */
  3312. for(n = 1; n <= cutcnt;n++) {
  3313. if(sloom) {
  3314. display_time = round(((double)n/(double)cutcnt) * PBAR_LENGTH);
  3315. fprintf(stdout,"TIME: %d\n",display_time);
  3316. fflush(stdout);
  3317. }
  3318. if(n<1) {
  3319. seglen = cuttime[n];
  3320. here = 0;
  3321. } else if(n==cutcnt)
  3322. seglen = dz->insams[0] - cuttime[n];
  3323. else
  3324. seglen = cuttime[n] - cuttime[n-1];
  3325. startopos = opos + dz->total_samps_written;
  3326. here_in_buf = here - startsamp;
  3327. there_in_buf = cuttime[n] - startsamp;
  3328. time = startopos/srate;
  3329. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  3330. return(exit_status);
  3331. if((overflow = opos - dz->buflen)>0) {
  3332. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,dz->buflen,0,obuf,1,dz)) < 0)
  3333. return(exit_status);
  3334. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,dz->buflen,0,obuf,1,dz)) < 0)
  3335. return(exit_status);
  3336. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  3337. return(exit_status);
  3338. lastwritend -= dz->buflen;
  3339. lastwrite += dz->buflen;
  3340. memset((char*)obuf,0,dz->buflen * sizeof(float));
  3341. if(lastwritend > 0)
  3342. memcpy((char*)obuf,(char*)obuf2,lastwritend * sizeof(float));
  3343. memset((char*)obuf2,0,dz->buflen * sizeof(float));
  3344. opos -= dz->buflen;
  3345. }
  3346. if((opos + (there_in_buf - here_in_buf)) >= dz->buflen * 2) {
  3347. sprintf(errstr,"OVERFLOWED OUTPUT BUFFER.\n");
  3348. return(MEMORY_ERROR);
  3349. }
  3350. if(here_in_buf >= dz->buflen * 2) {
  3351. sprintf(errstr,"OVERFLOWED INPUT BUFFER.\n");
  3352. return(MEMORY_ERROR);
  3353. }
  3354. while(here_in_buf > dz->buflen) {
  3355. memset((char *)ibuf,0,dz->buflen * sizeof(float));
  3356. if(dz->ssampsread > 0) {
  3357. memcpy((char *)ibuf,(char *)ibuf2,dz->buflen * sizeof(float));
  3358. memset((char *)ibuf2,0,dz->buflen * sizeof(float));
  3359. if((exit_status = read_samps(ibuf2,dz))<0)
  3360. return(exit_status);
  3361. }
  3362. startsamp += dz->buflen;
  3363. here_in_buf -= dz->buflen;
  3364. there_in_buf = cuttime[n] - startsamp;
  3365. }
  3366. if(zerofof(here_in_buf,there_in_buf,ibuf)) {
  3367. for(k = here_in_buf; k < there_in_buf;k++)
  3368. opos++;
  3369. } else {
  3370. getfofenv(ibuf,fofenv,fofloc,maxseglen,here_in_buf,there_in_buf,&envcnt);
  3371. if((exit_status = superimpose_fofs_on_synthtones(fofenv,fofloc,sintab,
  3372. here_in_buf,there_in_buf,ibuf,obuf,&opos,cuttime[n-1],envcnt,dz))<0)
  3373. return(exit_status);
  3374. }
  3375. if(opos > lastwritend)
  3376. lastwritend = opos;
  3377. endopos = startopos + seglen;
  3378. opos = endopos - dz->total_samps_written;
  3379. here = cuttime[n];
  3380. }
  3381. if(lastwritend > 0) {
  3382. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,lastwritend,1,obuf,1,dz)) < 0)
  3383. return(exit_status);
  3384. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,lastwritend,1,obuf,1,dz)) < 0)
  3385. return(exit_status);
  3386. if((exit_status = write_samps(obuf,lastwritend,dz))<0)
  3387. return(exit_status);
  3388. }
  3389. return FINISHED;
  3390. case(PSOW_IMPOSE):
  3391. limit = dz->insams[1];
  3392. if((exit_status = create_normalising_envelope(dz))<0)
  3393. return(exit_status);
  3394. envtoptime = 2;
  3395. envincr = dz->env[3] - dz->env[1];
  3396. envincr /= dz->env[2] - dz->env[0];
  3397. envincr /= srate;
  3398. envval = dz->env[1];
  3399. if((sndseekEx(dz->ifd[1],0,0)<0)){
  3400. sprintf(errstr,"sndseek() failed\n");
  3401. return SYSTEM_ERROR;
  3402. }
  3403. memset((char*)ibuf3,0,dz->buflen * sizeof(float));
  3404. if((ssampsread2 = fgetfbufEx(ibuf3, dz->buflen,dz->ifd[1],0)) < 0) {
  3405. sprintf(errstr,"Can't read samples from 2nd input soundfile.\n");
  3406. return(SYSTEM_ERROR);
  3407. }
  3408. memset((char*)ibuf4,0,dz->buflen * sizeof(float));
  3409. if((ssampsread2 = fgetfbufEx(ibuf4, dz->buflen,dz->ifd[1],0)) < 0) {
  3410. sprintf(errstr,"Can't read samples from 2nd input soundfile.\n");
  3411. return(SYSTEM_ERROR);
  3412. }
  3413. lastwritend = 0;
  3414. maxseglen = cuttime[0];
  3415. for(n=1;n<cutcnt;n++)
  3416. maxseglen = max(maxseglen,cuttime[n] - cuttime[n-1]);
  3417. maxseglen = max(maxseglen,dz->insams[0] - cuttime[cutcnt-1]);
  3418. if((fofenv = (double *)malloc(maxseglen * sizeof(double)))==NULL) {
  3419. sprintf(errstr,"Insufficient memory for FOF envelope vals store\n");
  3420. return(MEMORY_ERROR);
  3421. }
  3422. if((fofloc = (int *)malloc(maxseglen * sizeof(int)))==NULL) {
  3423. sprintf(errstr,"Insufficient memory for FOF envelope times store\n");
  3424. return(MEMORY_ERROR);
  3425. }
  3426. for(n = 1; n <= cutcnt;n++) {
  3427. if(cuttime[n] > limit)
  3428. break;
  3429. if(sloom) {
  3430. display_time = round(((double)n/(double)cutcnt) * PBAR_LENGTH);
  3431. fprintf(stdout,"TIME: %d\n",display_time);
  3432. fflush(stdout);
  3433. }
  3434. if(n<1) {
  3435. seglen = cuttime[n];
  3436. here = 0;
  3437. } else if(n==cutcnt)
  3438. seglen = dz->insams[0] - cuttime[n];
  3439. else
  3440. seglen = cuttime[n] - cuttime[n-1];
  3441. startopos = opos + dz->total_samps_written;
  3442. here_in_buf = here - startsamp;
  3443. there_in_buf = cuttime[n] - startsamp;
  3444. time = startopos/srate;
  3445. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  3446. return(exit_status);
  3447. if((overflow = opos - dz->buflen)>0) {
  3448. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,dz->buflen,0,obuf,1,dz)) < 0)
  3449. return(exit_status);
  3450. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,dz->buflen,0,obuf,1,dz)) < 0)
  3451. return(exit_status);
  3452. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  3453. return(exit_status);
  3454. lastwritend -= dz->buflen;
  3455. lastwrite += dz->buflen;
  3456. memset((char*)obuf,0,dz->buflen * sizeof(float));
  3457. if(lastwritend > 0)
  3458. memcpy((char*)obuf,(char*)obuf2,lastwritend * sizeof(float));
  3459. memset((char*)obuf2,0,dz->buflen * sizeof(float));
  3460. opos -= dz->buflen;
  3461. }
  3462. if((opos + (there_in_buf - here_in_buf)) >= dz->buflen * 2) {
  3463. sprintf(errstr,"OVERFLOWED OUTPUT BUFFER.\n");
  3464. return(MEMORY_ERROR);
  3465. }
  3466. if(here_in_buf >= dz->buflen * 2) {
  3467. sprintf(errstr,"OVERFLOWED INPUT BUFFER.\n");
  3468. return(MEMORY_ERROR);
  3469. }
  3470. while(here_in_buf > dz->buflen) {
  3471. memset((char *)ibuf,0,dz->buflen * sizeof(float));
  3472. memset((char *)ibuf3,0,dz->buflen * sizeof(float));
  3473. if(dz->ssampsread > 0) {
  3474. memcpy((char *)ibuf,(char *)ibuf2,dz->buflen * sizeof(float));
  3475. memset((char *)ibuf2,0,dz->buflen * sizeof(float));
  3476. if((exit_status = read_samps(ibuf2,dz))<0)
  3477. return(exit_status);
  3478. }
  3479. if(ssampsread2 > 0) {
  3480. memcpy((char *)ibuf3,(char *)ibuf4,dz->buflen * sizeof(float));
  3481. memset((char *)ibuf4,0,dz->buflen * sizeof(float));
  3482. if((ssampsread2 = fgetfbufEx(ibuf4,dz->buflen,dz->ifd[1],0)) < 0) {
  3483. sprintf(errstr,"Can't read samples from 2nd input soundfile.\n");
  3484. return(SYSTEM_ERROR);
  3485. }
  3486. }
  3487. startsamp += dz->buflen;
  3488. here_in_buf -= dz->buflen;
  3489. there_in_buf = cuttime[n] - startsamp;
  3490. }
  3491. if(zerofof(here_in_buf,there_in_buf,ibuf)) {
  3492. for(k = here_in_buf; k < there_in_buf;k++)
  3493. opos++;
  3494. } else {
  3495. getfofenv(ibuf,fofenv,fofloc,maxseglen,here_in_buf,there_in_buf,&envcnt);
  3496. if((exit_status = superimpose_fofs_on_input(fofenv,fofloc,
  3497. here_in_buf,there_in_buf,ibuf,ibuf3,obuf,&opos,startsamp,&envtoptime,&envval,&envincr,envcnt,dz))<0)
  3498. return(exit_status);
  3499. }
  3500. if(opos > lastwritend)
  3501. lastwritend = opos;
  3502. endopos = startopos + seglen;
  3503. opos = endopos - dz->total_samps_written;
  3504. here = cuttime[n];
  3505. }
  3506. if(lastwritend > 0) {
  3507. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,lastwritend,1,obuf,1,dz)) < 0)
  3508. return(exit_status);
  3509. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,lastwritend,1,obuf,1,dz)) < 0)
  3510. return(exit_status);
  3511. if((exit_status = write_samps(obuf,lastwritend,dz))<0)
  3512. return(exit_status);
  3513. }
  3514. if(dz->total_samps_written <= 0) {
  3515. sprintf(errstr,"No output written: 2nd sound is too short ??\n");
  3516. return(GOAL_FAILED);
  3517. }
  3518. return FINISHED;
  3519. case(PSOW_SPLIT):
  3520. lastwritend = 0;
  3521. ipos = 0;
  3522. opos = 0;
  3523. startsamp = 0;
  3524. for(n = dz->iparam[PS_SUBNO]-1; n <= cutcnt;n+=dz->iparam[PS_SUBNO]) {
  3525. if((overflow = opos - dz->buflen)>0) { /* write outbuf */
  3526. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,dz->buflen,0,obuf,1,dz)) < 0)
  3527. return(exit_status);
  3528. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,dz->buflen,0,obuf,1,dz)) < 0)
  3529. return(exit_status);
  3530. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  3531. return(exit_status);
  3532. lastwritend -= dz->buflen;
  3533. lastwrite += dz->buflen;
  3534. memset((char*)obuf,0,dz->buflen * sizeof(float));
  3535. if(lastwritend > 0)
  3536. memcpy((char*)obuf,(char*)obuf2,lastwritend * sizeof(float));
  3537. memset((char*)obuf2,0,dz->buflen * sizeof(float));
  3538. opos -= dz->buflen;
  3539. }
  3540. m = n - dz->iparam[PS_SUBNO];
  3541. if(m < 0)
  3542. total_seglen = cuttime[n];
  3543. else
  3544. total_seglen = cuttime[n] - cuttime[m];
  3545. if((opos + total_seglen) >= dz->buflen * 2) {
  3546. sprintf(errstr,"OVERFLOWED OUTPUT BUFFER.\n");
  3547. return(MEMORY_ERROR);
  3548. }
  3549. if((m > 0) && (cuttime[m] - startsamp > dz->buflen)) { /* refill inbuf */
  3550. memset((char *)ibuf,0,dz->buflen * sizeof(float));
  3551. if(dz->ssampsread > 0) {
  3552. memcpy((char *)ibuf,(char *)ibuf2,dz->buflen * sizeof(float));
  3553. memset((char *)ibuf2,0,dz->buflen * sizeof(float));
  3554. if((exit_status = read_samps(ibuf2,dz))<0)
  3555. return(exit_status);
  3556. }
  3557. startsamp += dz->buflen;
  3558. ipos -= dz->buflen;
  3559. there_in_buf = cuttime[n] - startsamp;
  3560. }
  3561. if(ipos + total_seglen >= dz->buflen * 2) {
  3562. sprintf(errstr,"OVERFLOWED INPUT BUFFER.\n");
  3563. return(MEMORY_ERROR);
  3564. }
  3565. segcnt = 0;
  3566. average_seglen = 0.0;
  3567. for(m = n - dz->iparam[PS_SUBNO];m < n;m++) {
  3568. if(m < 0)
  3569. startz = 0;
  3570. else
  3571. startz = cuttime[m] - startsamp;
  3572. endz = cuttime[m+1] - startsamp;
  3573. if(!zerofof(startz,endz,ibuf)) {
  3574. average_seglen += (double)(endz - startz);
  3575. segcnt++;
  3576. }
  3577. }
  3578. if(segcnt > 0) {
  3579. average_seglen = (double)average_seglen/(double)segcnt;
  3580. transtep = (int)round(average_seglen/dz->param[PS_UTRNS]);
  3581. cnt = 0;
  3582. orig_startpos = opos;
  3583. startpos = opos;
  3584. for(m = n - dz->iparam[PS_SUBNO];m < n;m++,cnt++) {
  3585. opos = startpos;
  3586. if(m < 0) {
  3587. here_in_buf = 0;
  3588. there_in_buf = cuttime[0];
  3589. } else {
  3590. here_in_buf = cuttime[m] - startsamp;
  3591. there_in_buf = cuttime[m+1] - startsamp;
  3592. }
  3593. if(!zerofof(here_in_buf,there_in_buf,ibuf)) {
  3594. if(dz->param[PS_ATTEN] <= 1.0) {
  3595. if(cnt > 0)
  3596. gain = dz->param[PS_ATTEN]; /* attenuate uptransposed FOFs */
  3597. else
  3598. gain = 1.0;
  3599. } else {
  3600. if(cnt == 0)
  3601. gain /= dz->param[PS_ATTEN]; /* attenuate subharmonics */
  3602. else
  3603. gain = 1.0;
  3604. }
  3605. for(k = here_in_buf;k<there_in_buf;k++) {
  3606. thisval = ibuf[k] * gain;
  3607. obuf[opos] = (float)(obuf[opos] + thisval);
  3608. opos++;
  3609. }
  3610. lastwritend = max(opos,lastwritend);
  3611. }
  3612. startpos += transtep; /* uptranspose FOFs */
  3613. }
  3614. }
  3615. opos = orig_startpos + total_seglen; /* retain pitch of next subharm */
  3616. ipos += total_seglen;
  3617. }
  3618. if(lastwritend > 0) {
  3619. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,lastwritend,1,obuf,1,dz)) < 0)
  3620. return(exit_status);
  3621. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,lastwritend,1,obuf,1,dz)) < 0)
  3622. return(exit_status);
  3623. if((exit_status = write_samps(obuf,lastwritend,dz))<0)
  3624. return(exit_status);
  3625. }
  3626. if(dz->total_samps_written <= 0) {
  3627. sprintf(errstr,"No output written: 2nd sound is too short ??\n");
  3628. return(GOAL_FAILED);
  3629. }
  3630. return FINISHED;
  3631. case(PSOW_SPACE):
  3632. lastwritend = 0;
  3633. opos = 0;
  3634. startsamp = 0;
  3635. spaceswitch = 0;
  3636. if(cuttime[cutcnt-1] != dz->insams[0]) /* force segment at end */
  3637. cuttime[cutcnt++] = dz->insams[0]; /* force segment at start */
  3638. for(n=cutcnt-1;n>=0;n--)
  3639. cuttime[n+1] = cuttime[n];
  3640. cuttime[0] = 0;
  3641. cutcnt++;
  3642. memset((char*)obuf,0,dz->buflen * 2 * sizeof(float));
  3643. memset((char*)obuf2,0,dz->buflen * 2 * sizeof(float));
  3644. for(n = 1; n < cutcnt;n++) {
  3645. if((here_in_buf = cuttime[n-1] - startsamp) >= dz->buflen) {
  3646. memset((char *)ibuf,0,dz->buflen * sizeof(float));
  3647. if(dz->ssampsread > 0) {
  3648. memcpy((char *)ibuf,(char *)ibuf2,dz->buflen * sizeof(float));
  3649. memset((char *)ibuf2,0,dz->buflen * sizeof(float));
  3650. if((exit_status = read_samps(ibuf2,dz))<0)
  3651. return(exit_status);
  3652. }
  3653. startsamp += dz->buflen;
  3654. here_in_buf -= dz->buflen;
  3655. }
  3656. there_in_buf = cuttime[n] - startsamp;
  3657. if((overflow = opos - (dz->buflen*2))>0) { /* write outbuf */
  3658. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,dz->buflen*2,0,obuf,2,dz)) < 0)
  3659. return(exit_status);
  3660. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,dz->buflen*2,0,obuf,2,dz)) < 0)
  3661. return(exit_status);
  3662. if((exit_status = write_samps(obuf,dz->buflen*2,dz))<0)
  3663. return(exit_status);
  3664. lastwritend -= dz->buflen*2;
  3665. lastwrite += dz->buflen*2;
  3666. memset((char*)obuf,0,dz->buflen * 2 * sizeof(float));
  3667. if(lastwritend > 0)
  3668. memcpy((char*)obuf,(char*)obuf2,lastwritend * sizeof(float));
  3669. memset((char*)obuf2,0,dz->buflen * 2 * sizeof(float));
  3670. opos -= dz->buflen * 2;
  3671. }
  3672. time = (double)cuttime[n-1]/srate;
  3673. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  3674. return(exit_status);
  3675. switch(spaceswitch) {
  3676. case(0):
  3677. if(dz->param[PS_SEPAR] >= 0.0) /* even events move rightwards */
  3678. spacepos = dz->param[PS_SEPAR];
  3679. else /* even events move leftwards */
  3680. spacepos = -(dz->param[PS_SEPAR]);
  3681. break;
  3682. case(1):
  3683. if(dz->param[PS_SEPAR] >= 0.0) /* odd events move leftwards */
  3684. spacepos = -(dz->param[PS_SEPAR]);
  3685. else /* odd events move rightwards */
  3686. spacepos = dz->param[PS_SEPAR];
  3687. break;
  3688. case(2):
  3689. spacepos = 0.0;
  3690. break;
  3691. case(3):
  3692. if(dz->param[PS_SEPAR] >= 0.0) /* even events move rightwards */
  3693. spacepos = dz->param[PS_SEPAR]/2.0;
  3694. else /* even events move leftwards */
  3695. spacepos = -(dz->param[PS_SEPAR]/2.0);
  3696. break;
  3697. case(4):
  3698. if(dz->param[PS_SEPAR] >= 0.0) /* odd events move leftwards */
  3699. spacepos = -(dz->param[PS_SEPAR]/2.0);
  3700. else /* odd events move rightwards */
  3701. spacepos = dz->param[PS_SEPAR]/2.0;
  3702. break;
  3703. }
  3704. spacecalc(spacepos,&valleft,&valright);
  3705. suppression = (dz->param[PS_RELV2] * (double)spaceswitch/(double)(dz->iparam[PS_SUBNO] - 1));
  3706. suppression = pow(suppression,PSOW_POW);
  3707. valleft *= 1.0 - suppression;
  3708. valright *= 1.0 - suppression;
  3709. spaceswitch++;
  3710. spaceswitch %= dz->iparam[PS_SUBNO];
  3711. if(dz->param[PS_RELEV] < 1.0) /* de-emphasize rightward sig */
  3712. valright *= dz->param[PS_RELEV];
  3713. else if(dz->param[PS_RELEV] > 0.0) /* de-emphasize leftward sig */
  3714. valleft /= dz->param[PS_RELEV];
  3715. for(k=here_in_buf;k<there_in_buf;k++) {
  3716. obuf[opos++] = (float)(ibuf[k] * valleft);
  3717. obuf[opos++] = (float)(ibuf[k] * valright);
  3718. }
  3719. }
  3720. lastwritend = opos;
  3721. if(lastwritend > 0) {
  3722. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,lastwritend,1,obuf,2,dz)) < 0)
  3723. return(exit_status);
  3724. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,lastwritend,1,obuf,2,dz)) < 0)
  3725. return(exit_status);
  3726. if((exit_status = write_samps(obuf,lastwritend,dz))<0)
  3727. return(exit_status);
  3728. }
  3729. return FINISHED;
  3730. case(PSOW_INTERLEAVE):
  3731. insert_edge_cuts(cuttime,&cutcnt,&cutstart,dz);
  3732. limit = min(cutstart,cutcnt - cutstart); /* work to end of smallest infile, counted in FOFs */
  3733. lastwritend = 0;
  3734. opos = 0;
  3735. startsamp = 0;
  3736. startsamp2 = 0;
  3737. cnt0 = 0;
  3738. cnt1 = 0;
  3739. for(n = j-1, m = cutstart+j-1; n < limit;n+=j,m+=j) {
  3740. startopos = opos;
  3741. gain = 1.0;
  3742. if(n == j-1) {
  3743. seglen0 = cuttime[n];
  3744. seglen1 = cuttime[m];
  3745. time = 0.0;
  3746. xbuf = ibuf;
  3747. } else {
  3748. seglen0 = cuttime[n] - cuttime[n-j];
  3749. seglen1 = cuttime[m] - cuttime[m-j];
  3750. time = (double)cuttime[n-j]/srate;
  3751. xbuf = do_weight(&cnt0,&cnt1,ibuf,ibuf3,xbuf,dz);
  3752. }
  3753. diff = seglen1 - seglen0;
  3754. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  3755. return(exit_status);
  3756. if(xbuf == ibuf) { /* FILE 1 */
  3757. if(n == j-1)
  3758. here_in_buf = 0;
  3759. else
  3760. here_in_buf = cuttime[n-j] - startsamp;
  3761. while(here_in_buf >= dz->buflen) {
  3762. memset((char *)ibuf,0,dz->buflen * sizeof(float));
  3763. if(dz->ssampsread > 0) {
  3764. memcpy((char *)ibuf,(char *)ibuf2,dz->buflen * sizeof(float));
  3765. memset((char *)ibuf2,0,dz->buflen * sizeof(float));
  3766. if((exit_status = read_samps(ibuf2,dz))<0)
  3767. return(exit_status);
  3768. }
  3769. startsamp += dz->buflen;
  3770. here_in_buf -= dz->buflen;
  3771. }
  3772. there_in_buf = cuttime[n] - startsamp;
  3773. if(dz->param[PS_RELV2] < 1.0) /* 1st sound de-emphasized */
  3774. gain = dz->param[PS_RELV2];
  3775. if(dz->param[PS_BIAS] > 0.0)
  3776. advance = seglen0; /* keep length of seg from snd 1 */
  3777. else /* bias towards length of 2nd snd's seg */
  3778. advance = seglen0 - (int)round(diff * dz->param[PS_BIAS]);
  3779. } else {
  3780. if(m == cutstart+j-1)
  3781. here_in_buf = 0;
  3782. else
  3783. here_in_buf = cuttime[m-j] - startsamp2;
  3784. while(here_in_buf >= dz->buflen) {
  3785. memset((char *)ibuf3,0,dz->buflen * sizeof(float));
  3786. if(ssampsread2 > 0) {
  3787. memcpy((char *)ibuf3,(char *)ibuf4,dz->buflen * sizeof(float));
  3788. memset((char *)ibuf4,0,dz->buflen * sizeof(float));
  3789. if((ssampsread2 = fgetfbufEx(ibuf4,dz->buflen,dz->ifd[1],0)) < 0) {
  3790. sprintf(errstr,"Can't read samples from 2nd input soundfile.\n");
  3791. return(SYSTEM_ERROR);
  3792. }
  3793. }
  3794. startsamp2 += dz->buflen;
  3795. here_in_buf -= dz->buflen;
  3796. }
  3797. there_in_buf = cuttime[m] - startsamp2;
  3798. if(dz->param[PS_RELV2] > 1.0) /* 2nd sound de-emphasized */
  3799. gain = 1.0/dz->param[PS_RELV2];
  3800. if(dz->param[PS_BIAS] < 0.0)
  3801. advance = seglen1; /* keep length of seg from snd 2 */
  3802. else /* bias towards length of 1st snd's seg */
  3803. advance = seglen1 - (int)round(diff * dz->param[PS_BIAS]);
  3804. /* diff is reversed from situation with sound 1 (+ becomes -) */
  3805. /* but PS_BIAS is -ve .... so these negs cancel */
  3806. }
  3807. if((overflow = opos - (dz->buflen))>0) { /* write outbuf */
  3808. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,dz->buflen,0,obuf,1,dz)) < 0)
  3809. return(exit_status);
  3810. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,dz->buflen,0,obuf,1,dz)) < 0)
  3811. return(exit_status);
  3812. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  3813. return(exit_status);
  3814. lastwritend -= dz->buflen;
  3815. lastwrite += dz->buflen;
  3816. memset((char*)obuf,0,dz->buflen * sizeof(float));
  3817. if(lastwritend > 0)
  3818. memcpy((char*)obuf,(char*)obuf2,lastwritend * sizeof(float));
  3819. memset((char*)obuf2,0,dz->buflen * sizeof(float));
  3820. opos -= dz->buflen;
  3821. }
  3822. for(k=here_in_buf;k<there_in_buf;k++) {
  3823. thisval = xbuf[k] * gain;
  3824. obuf[opos] = (float)(obuf[opos] + thisval);
  3825. opos++;
  3826. }
  3827. lastwritend = opos;
  3828. opos = startopos + advance;
  3829. }
  3830. if(lastwritend > 0) {
  3831. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,lastwritend,1,obuf,1,dz)) < 0)
  3832. return(exit_status);
  3833. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,lastwritend,1,obuf,1,dz)) < 0)
  3834. return(exit_status);
  3835. if((exit_status = write_samps(obuf,lastwritend,dz))<0)
  3836. return(exit_status);
  3837. }
  3838. return FINISHED;
  3839. case(PSOW_REPLACE):
  3840. lastwritend = 0;
  3841. opos = 0;
  3842. startsamp = 0;
  3843. limit = min(cutstart,cutcnt - cutstart); /* work to end of smallest infile, counted in FOFs */
  3844. for(n = j-1, m = cutstart+j-1; n < limit;n+=j,m+=j) {
  3845. if(n == j-1) {
  3846. seglen = cuttime[m];
  3847. here_in_buf = 0;
  3848. } else {
  3849. seglen = cuttime[m] - cuttime[m-j];
  3850. here_in_buf = cuttime[n-j] - startsamp;
  3851. }
  3852. if(here_in_buf >= dz->buflen) {
  3853. memset((char *)ibuf,0,dz->buflen * sizeof(float));
  3854. if(dz->ssampsread > 0) {
  3855. memcpy((char *)ibuf,(char *)ibuf2,dz->buflen * sizeof(float));
  3856. memset((char *)ibuf2,0,dz->buflen * sizeof(float));
  3857. if((exit_status = read_samps(ibuf2,dz))<0)
  3858. return(exit_status);
  3859. }
  3860. startsamp += dz->buflen;
  3861. here_in_buf -= dz->buflen;
  3862. }
  3863. there_in_buf = cuttime[n] - startsamp;
  3864. if((overflow = opos - (dz->buflen*2))>0) { /* write outbuf */
  3865. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,dz->buflen,0,obuf,1,dz)) < 0)
  3866. return(exit_status);
  3867. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,dz->buflen,0,obuf,1,dz)) < 0)
  3868. return(exit_status);
  3869. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  3870. return(exit_status);
  3871. lastwritend -= dz->buflen;
  3872. lastwrite += dz->buflen;
  3873. memset((char*)obuf,0,dz->buflen * sizeof(float));
  3874. if(lastwritend > 0)
  3875. memcpy((char*)obuf,(char*)obuf2,lastwritend * sizeof(float));
  3876. memset((char*)obuf2,0,dz->buflen * sizeof(float));
  3877. opos -= dz->buflen;
  3878. }
  3879. startopos = opos;
  3880. for(k=here_in_buf;k<there_in_buf;k++) {
  3881. obuf[opos] = (float)(obuf[opos] + ibuf[k]);
  3882. opos++;
  3883. }
  3884. lastwritend = opos;
  3885. opos = startopos + seglen;
  3886. }
  3887. if(lastwritend > 0) {
  3888. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,lastwritend,1,obuf,1,dz)) < 0)
  3889. return(exit_status);
  3890. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,lastwritend,1,obuf,1,dz)) < 0)
  3891. return(exit_status);
  3892. if((exit_status = write_samps(obuf,lastwritend,dz))<0)
  3893. return(exit_status);
  3894. }
  3895. return FINISHED;
  3896. case(PSOW_EXTEND):
  3897. xbuf = dz->sampbuf[4];
  3898. lastwritend = 0;
  3899. opos = 0;
  3900. if(cuttime[cutcnt-1] != dz->insams[0]) /* force segment at end */
  3901. cuttime[cutcnt++] = dz->insams[0];
  3902. for(n=cutcnt-1;n>=0;n--)
  3903. cuttime[n+1] = cuttime[n];
  3904. cuttime[0] = 0; /* force segment at start */
  3905. cutcnt++;
  3906. startsamp = (int)round(dz->param[1] * srate); /* Time at which to look for frozen segment */
  3907. for(n = j; n < cutcnt;n+=j) {
  3908. if(cuttime[n] >= startsamp) {
  3909. if(cuttime[n] == startsamp)
  3910. n+=j;
  3911. break;
  3912. }
  3913. }
  3914. n-= j;
  3915. if(n+j >= cutcnt) {
  3916. sprintf(errstr,"Segment cut too close to end to include %d grains.\n",j);
  3917. return(GOAL_FAILED);
  3918. }
  3919. seglen = cuttime[n+j] - cuttime[n];
  3920. dur = (dz->param[PS_DUR] - dz->duration) * srate;
  3921. repets = (int)round(dur/(double)seglen);
  3922. if(repets <= 1) {
  3923. sprintf(errstr,"File will not be extended,with this value of Output File Duration.\n");
  3924. return(GOAL_FAILED);
  3925. }
  3926. startz = n;
  3927. startsamp = 0;
  3928. done_freeze = 0;
  3929. for(n = j; n < cutcnt;n+=j) {
  3930. seglen = cuttime[n] - cuttime[n-j];
  3931. startopos = opos + dz->total_samps_written;
  3932. here_in_buf = cuttime[n-j] - startsamp;
  3933. there_in_buf = cuttime[n] - startsamp;
  3934. time = (double)startopos/srate;
  3935. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  3936. return(exit_status);
  3937. if((overflow = opos - dz->buflen)>0) {
  3938. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,dz->buflen,0,obuf,1,dz)) < 0)
  3939. return(exit_status);
  3940. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,dz->buflen,0,obuf,1,dz)) < 0)
  3941. return(exit_status);
  3942. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  3943. return(exit_status);
  3944. lastwritend -= dz->buflen;
  3945. lastwrite += dz->buflen;
  3946. memset((char*)obuf,0,dz->buflen * sizeof(float));
  3947. if(lastwritend > 0)
  3948. memcpy((char*)obuf,(char*)obuf2,lastwritend * sizeof(float));
  3949. memset((char*)obuf2,0,dz->buflen * sizeof(float));
  3950. opos -= dz->buflen;
  3951. }
  3952. if((opos + (there_in_buf - here_in_buf)) >= dz->buflen * 2) {
  3953. sprintf(errstr,"OVERFLOWED OUTPUT BUFFER.\n");
  3954. return(MEMORY_ERROR);
  3955. }
  3956. if(here_in_buf >= dz->buflen * 2) {
  3957. sprintf(errstr,"OVERFLOWED INPUT BUFFER.\n");
  3958. return(MEMORY_ERROR);
  3959. }
  3960. while(here_in_buf > dz->buflen) {
  3961. memset((char *)ibuf,0,dz->buflen * sizeof(float));
  3962. if(dz->ssampsread > 0) {
  3963. memcpy((char *)ibuf,(char *)ibuf2,dz->buflen * sizeof(float));
  3964. memset((char *)ibuf2,0,dz->buflen * sizeof(float));
  3965. if((exit_status = read_samps(ibuf2,dz))<0)
  3966. return(exit_status);
  3967. }
  3968. startsamp += dz->buflen;
  3969. here_in_buf -= dz->buflen;
  3970. there_in_buf -= dz->buflen;
  3971. }
  3972. if((n < startz) || done_freeze) {
  3973. if(zerofof(here_in_buf,there_in_buf,ibuf)) {
  3974. for(k = here_in_buf; k < there_in_buf;k++)
  3975. opos++;
  3976. } else if (dz->brksize[PSE_GAIN]) {
  3977. for(k = here_in_buf; k < there_in_buf;k++) {
  3978. startopos = opos + dz->total_samps_written;
  3979. time = (double)startopos/srate;
  3980. if((exit_status = read_value_from_brktable(time,PSE_GAIN,dz))<0)
  3981. return(exit_status);
  3982. obuf[opos] = (float)(obuf[opos] + (ibuf[k] * dz->param[PSE_GAIN]));
  3983. opos++;
  3984. }
  3985. } else {
  3986. for(k = here_in_buf; k < there_in_buf;k++) {
  3987. obuf[opos] = (float)(obuf[opos] + ibuf[k]);
  3988. opos++;
  3989. }
  3990. }
  3991. if(opos > lastwritend)
  3992. lastwritend = opos;
  3993. if(dz->param[PSE_VDEP] > 0.0) {
  3994. vibshift = vibrato(seglen,dz->param[PSE_VFRQ],dz->param[PSE_VDEP],dz);
  3995. opos += vibshift;
  3996. }
  3997. } else {
  3998. ingraintime = 0; /* Start measuring time in brkpnt for grain frq transpositions */
  3999. grainlen = there_in_buf - here_in_buf;
  4000. if(dz->vflag[0]) {
  4001. sinstep = PI/(double)grainlen;
  4002. thissin = 0.0;
  4003. for(k=here_in_buf,m=0;k<there_in_buf;k++,m++) {
  4004. xbuf[m] = (float)(sin(thissin) * ibuf[k]);
  4005. thissin += sinstep;
  4006. }
  4007. } else {
  4008. for(k=here_in_buf,m=0;k<there_in_buf;k++,m++)
  4009. xbuf[m] = ibuf[k];
  4010. }
  4011. limit = (int)round((dz->param[1] + dz->param[2] - dz->duration) * (double)dz->infile->srate); // End of freeze section
  4012. for(k = 0;k<repets;k++) {
  4013. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  4014. return(exit_status);
  4015. if(dz->brksize[PSE_TRNS] > 0) {
  4016. if((exit_status = read_value_from_brktable(ingraintime,PSE_TRNS,dz))<0)
  4017. return(exit_status);
  4018. }
  4019. segstep = (int)floor((double)seglen/dz->param[PSE_TRNS]); /* segstep corresponds to wavelen of transposed seg */
  4020. bakstep = seglen - segstep; /* This results in next seg being written 'bakstep' earlier */
  4021. opos -= bakstep;
  4022. timestep = (double)segstep/(double)srate;
  4023. ingraintime += timestep;
  4024. time += timestep;
  4025. if((overflow = opos - dz->buflen)>0) {
  4026. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,dz->buflen,0,obuf,1,dz)) < 0)
  4027. return(exit_status);
  4028. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,dz->buflen,0,obuf,1,dz)) < 0)
  4029. return(exit_status);
  4030. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  4031. return(exit_status);
  4032. lastwritend -= dz->buflen;
  4033. lastwrite += dz->buflen;
  4034. memset((char*)obuf,0,dz->buflen * sizeof(float));
  4035. if(lastwritend > 0)
  4036. memcpy((char*)obuf,(char*)obuf2,lastwritend * sizeof(float));
  4037. memset((char*)obuf2,0,dz->buflen * sizeof(float));
  4038. opos -= dz->buflen;
  4039. }
  4040. if(dz->total_samps_written + opos > limit) {
  4041. done_freeze = 1;
  4042. break;
  4043. }
  4044. if (dz->brksize[PSE_GAIN]) {
  4045. for(m=0;m<seglen;m++) {
  4046. startopos = opos + dz->total_samps_written;
  4047. time = (double)startopos/srate;
  4048. if((exit_status = read_value_from_brktable(time,PSE_GAIN,dz))<0)
  4049. return(exit_status);
  4050. obuf[opos] = (float)(obuf[opos] + (xbuf[m] * dz->param[PSE_GAIN]));
  4051. opos++;
  4052. }
  4053. } else {
  4054. for(m=0;m<seglen;m++) {
  4055. obuf[opos] = (float)(obuf[opos] + xbuf[m]);
  4056. opos++;
  4057. }
  4058. }
  4059. if(opos > lastwritend)
  4060. lastwritend = opos;
  4061. if(dz->param[PSE_VDEP] > 0.0) { /* vibrato rate relates to seg-advance (segstep) (???) */
  4062. vibshift = vibrato(segstep,dz->param[PSE_VFRQ],dz->param[PSE_VDEP],dz);
  4063. opos += vibshift;
  4064. }
  4065. }
  4066. done_freeze = 1;
  4067. }
  4068. }
  4069. if(lastwritend > 0) {
  4070. // if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,lastwritend,1,obuf,1,dz)) < 0)
  4071. // return(exit_status);
  4072. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,lastwritend,1,obuf,1,dz)) < 0)
  4073. return(exit_status);
  4074. if((exit_status = write_samps(obuf,lastwritend,dz))<0)
  4075. return(exit_status);
  4076. }
  4077. return FINISHED;
  4078. case(PSOW_EXTEND2):
  4079. xbuf = dz->sampbuf[4];
  4080. lastwritend = 0;
  4081. opos = 0;
  4082. dur = (dz->param[PS_DUR] - dz->duration) * srate;
  4083. repets = (int)round(dur/(double)seglen);
  4084. if(repets <= 1) {
  4085. sprintf(errstr,"File will not be extended,with this value of Output File Duration.\n");
  4086. return(GOAL_FAILED);
  4087. }
  4088. startbuf = 0;
  4089. while(dz->total_samps_read < startcut) {
  4090. if((exit_status = write_samps(ibuf,dz->buflen,dz))<0)
  4091. return(exit_status);
  4092. startbuf += dz->buflen;
  4093. memcpy((char *)ibuf,(char *)ibuf2,dz->buflen * sizeof(float));
  4094. memset((char *)ibuf2,0,dz->buflen * sizeof(float));
  4095. last_sampsread = dz->ssampsread;
  4096. if((exit_status = read_samps(ibuf2,dz))<0)
  4097. return(exit_status);
  4098. }
  4099. if(startcut - startbuf > dz->buflen) {
  4100. if((exit_status = write_samps(ibuf,dz->buflen,dz))<0)
  4101. return(exit_status);
  4102. startbuf += dz->buflen;
  4103. memcpy((char *)ibuf,(char *)ibuf2,dz->buflen * sizeof(float));
  4104. memset((char *)ibuf2,0,dz->buflen * sizeof(float));
  4105. last_sampsread = dz->ssampsread;
  4106. if((exit_status = read_samps(ibuf2,dz))<0)
  4107. return(exit_status);
  4108. startbuf += dz->buflen;
  4109. }
  4110. here_in_buf = startcut - startbuf;
  4111. there_in_buf = endcut - startbuf;
  4112. for(n=0;n<here_in_buf;n++)
  4113. obuf[opos++] = ibuf[n];
  4114. lastwritend = opos;
  4115. for(k=here_in_buf,m=0;k<there_in_buf;k++,m++)
  4116. xbuf[m] = ibuf[k];
  4117. for(k = 0;k<repets;k++) {
  4118. if((overflow = opos - dz->buflen)>0) {
  4119. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  4120. return(exit_status);
  4121. lastwritend -= dz->buflen;
  4122. memset((char*)obuf,0,dz->buflen * sizeof(float));
  4123. if(lastwritend > 0)
  4124. memcpy((char*)obuf,(char*)obuf2,lastwritend * sizeof(float));
  4125. memset((char*)obuf2,0,dz->buflen * sizeof(float));
  4126. opos -= dz->buflen;
  4127. }
  4128. startopos = opos;
  4129. for(m=0;m<seglen;m++) {
  4130. obuf[opos] = (float)(obuf[opos] + xbuf[m]);
  4131. opos++;
  4132. }
  4133. if(opos > lastwritend)
  4134. lastwritend = opos;
  4135. time = (double)(startopos + dz->total_samps_written)/srate;
  4136. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  4137. return(exit_status);
  4138. if(dz->param[PS2_VDEP] > 0.0)
  4139. opos += vibrato(seglen,dz->param[PS2_VFRQ],dz->param[PS2_VDEP],dz);
  4140. }
  4141. for(n=there_in_buf;n<last_sampsread;n++) {
  4142. obuf[opos++] = ibuf[n];
  4143. lastwritend = opos;
  4144. if((overflow = opos - dz->buflen)>0) {
  4145. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  4146. return(exit_status);
  4147. lastwritend -= dz->buflen;
  4148. memset((char*)obuf,0,dz->buflen * sizeof(float));
  4149. if(lastwritend > 0)
  4150. memcpy((char*)obuf,(char*)obuf2,lastwritend * sizeof(float));
  4151. memset((char*)obuf2,0,dz->buflen * sizeof(float));
  4152. opos -= dz->buflen;
  4153. }
  4154. }
  4155. while(dz->ssampsread > 0) {
  4156. memcpy((char *)ibuf,(char *)ibuf2,dz->buflen * sizeof(float));
  4157. memset((char *)ibuf2,0,dz->buflen * sizeof(float));
  4158. last_sampsread = dz->ssampsread;
  4159. if((exit_status = read_samps(ibuf2,dz))<0)
  4160. return(exit_status);
  4161. for(n=0;n<last_sampsread;n++) {
  4162. obuf[opos++] = ibuf[n];
  4163. lastwritend = opos;
  4164. if((overflow = opos - dz->buflen)>0) {
  4165. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  4166. return(exit_status);
  4167. lastwritend -= dz->buflen;
  4168. memset((char*)obuf,0,dz->buflen * sizeof(float));
  4169. if(lastwritend > 0)
  4170. memcpy((char*)obuf,(char*)obuf2,lastwritend * sizeof(float));
  4171. memset((char*)obuf2,0,dz->buflen * sizeof(float));
  4172. opos -= dz->buflen;
  4173. }
  4174. }
  4175. }
  4176. if(lastwritend > 0) {
  4177. if((exit_status = write_samps(obuf,lastwritend,dz))<0)
  4178. return(exit_status);
  4179. }
  4180. return FINISHED;
  4181. case(PSOW_LOCATE):
  4182. startsamp = (int)round(dz->param[1] * srate);
  4183. for(n=0;n<cutcnt;n++) {
  4184. if(cuttime[n] >= startsamp) {
  4185. diff = startsamp - cuttime[n];
  4186. if(n == cutcnt-1)
  4187. endsamp = dz->insams[0];
  4188. else
  4189. endsamp = cuttime[n+1];
  4190. if(endsamp - startsamp < diff)
  4191. n++;
  4192. if(n >= cutcnt) {
  4193. sprintf(errstr,"Time is too close to end of file.\n");
  4194. return(GOAL_FAILED);
  4195. }
  4196. time = (double)cuttime[n]/srate;
  4197. fprintf(stdout,"INFO: TIME %lf IS NEAREST GRAIN START %lf\n",dz->param[1],time);
  4198. break;
  4199. }
  4200. }
  4201. fflush(stdout);
  4202. return FINISHED;
  4203. case(PSOW_CUT):
  4204. startsamp = (int)round(dz->param[1] * srate);
  4205. time = -1.0;
  4206. for(n=0;n<cutcnt;n++) {
  4207. if(cuttime[n] >= startsamp) {
  4208. diff = startsamp - cuttime[n];
  4209. if(n == cutcnt-1)
  4210. endsamp = dz->insams[0];
  4211. else
  4212. endsamp = cuttime[n+1];
  4213. if(endsamp - startsamp < diff)
  4214. n++;
  4215. if(n >= cutcnt) {
  4216. sprintf(errstr,"Time is too close to end of file.\n");
  4217. return(GOAL_FAILED);
  4218. }
  4219. time = (double)cuttime[n]/srate;
  4220. fprintf(stdout,"INFO: TIME %lf IS NEAREST GRAIN START %lf\n",dz->param[1],time);
  4221. break;
  4222. }
  4223. }
  4224. if(time < 0.0) {
  4225. sprintf(errstr,"Cut Time is not within the input sound.");
  4226. return(GOAL_FAILED);
  4227. }
  4228. fflush(stdout);
  4229. opos = 0;
  4230. startsamp = (int)round(time * srate);
  4231. if(startsamp <= 0) {
  4232. sprintf(errstr,"Cut is at very start of file. No change to source sound");
  4233. return(GOAL_FAILED);
  4234. }
  4235. if(startsamp >= dz->insams[0]) {
  4236. fprintf(stdout,"WARNING: Grain number beyond end of file.\n");
  4237. fflush(stdout);
  4238. startsamp = cutcnt-1;
  4239. }
  4240. switch(dz->mode) {
  4241. case(0):
  4242. while(startsamp >= dz->buflen) {
  4243. if((exit_status = write_samps(ibuf,dz->buflen,dz))<0)
  4244. return(exit_status);
  4245. if((exit_status = read_samps(ibuf,dz))<0)
  4246. return(exit_status);
  4247. startsamp -= dz->buflen;
  4248. }
  4249. if(startsamp > 0) {
  4250. if((exit_status = write_samps(ibuf,startsamp,dz))<0)
  4251. return(exit_status);
  4252. }
  4253. break;
  4254. case(1):
  4255. while(startsamp >= dz->buflen) {
  4256. if((exit_status = read_samps(ibuf,dz))<0)
  4257. return(exit_status);
  4258. startsamp -= dz->buflen;
  4259. }
  4260. while(dz->ssampsread > 0) {
  4261. for(n=startsamp;n<dz->ssampsread;n++)
  4262. obuf[opos++] = ibuf[n];
  4263. if((exit_status = read_samps(ibuf,dz))<0)
  4264. return(exit_status);
  4265. if(opos >= dz->buflen) {
  4266. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  4267. return(exit_status);
  4268. memset((char*)obuf,0,dz->buflen * sizeof(float));
  4269. memcpy((char*)obuf,(char*)obuf2,dz->buflen * sizeof(float));
  4270. opos -= dz->buflen;
  4271. }
  4272. startsamp = 0;
  4273. }
  4274. break;
  4275. }
  4276. if(opos > 0) {
  4277. if((exit_status = write_samps(obuf,opos,dz))<0)
  4278. return(exit_status);
  4279. }
  4280. return FINISHED;
  4281. case(PSOW_REINF):
  4282. maxamp = 0.0;
  4283. ovflw = dz->sampbuf[4];
  4284. if(dz->mode == 0) {
  4285. hno = dz->lparray[0];
  4286. amp = dz->parray[0];
  4287. } else {
  4288. ihno = dz->parray[0];
  4289. amp = dz->parray[1];
  4290. }
  4291. lastwritend = 0;
  4292. memset((char*)ovflw,0,dz->buflen * sizeof(float));
  4293. if(dz->mode == 0 && dz->vflag[0])
  4294. sort_harmonics_to_ascending_order(dz);
  4295. fprintf(stdout,"INFO: Assessing final level\n");
  4296. fflush(stdout);
  4297. for(n = 0; n < cutcnt;n++) {
  4298. here_in_buf = here - startsamp;
  4299. there_in_buf = cuttime[n] - startsamp;
  4300. seglen = there_in_buf - here_in_buf;
  4301. opos = here; /* absolute output position */
  4302. opos -= lastwrite; /* output position relative to buf start */
  4303. if((overflow = opos - (dz->buflen * 2))>0) {
  4304. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,dz->buflen,0,obuf,1,dz)) < 0)
  4305. return(exit_status);
  4306. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,dz->buflen,0,obuf,1,dz)) < 0)
  4307. return(exit_status);
  4308. for(j=0;j<dz->buflen;j++)
  4309. maxamp = max(fabs(obuf[j]),maxamp);
  4310. lastwrite += dz->buflen;
  4311. display_virtual_time(lastwrite,dz);
  4312. memcpy((char*)obuf,(char*)obuf2,dz->buflen * sizeof(float));
  4313. memset((char*)obuf2,0,dz->buflen * sizeof(float));
  4314. memcpy((char*)obuf2,(char*)ovflw,overflow * sizeof(float));
  4315. memset((char*)ovflw,0,dz->buflen * sizeof(float));
  4316. lastwritend -= dz->buflen;
  4317. opos -= dz->buflen;
  4318. }
  4319. if(dz->mode == 0) {
  4320. if((opos + (seglen * 2) + (dz->iparam[ISTR] * dz->itemcnt)) >= dz->buflen * 3) {
  4321. sprintf(errstr,"OVERFLOWED OUTPUT BUFFER.\n");
  4322. return(MEMORY_ERROR);
  4323. }
  4324. } else {
  4325. if((opos + (seglen * (1 + dz->param[ISTR]))) >= dz->buflen * 3) {
  4326. sprintf(errstr,"OVERFLOWED OUTPUT BUFFER.\n");
  4327. return(MEMORY_ERROR);
  4328. }
  4329. }
  4330. startopos = opos;
  4331. display_virtual_time(here,dz);
  4332. for(k = here_in_buf; k < there_in_buf;k++)
  4333. obuf[opos++] += ibuf[k];
  4334. if(opos > lastwritend)
  4335. lastwritend = opos;
  4336. if(dz->mode == 0) { /* HARMONIC */
  4337. for(m=0;m<dz->itemcnt;m++) {
  4338. dstep = (double)seglen/(double)hno[m];
  4339. gain = amp[m];
  4340. dhere = (double)startopos + ((m+1) * dz->iparam[ISTR]);
  4341. j = 0;
  4342. while(j < hno[m]) {
  4343. opos = (int)round(dhere);
  4344. if(dz->mode == 0 && dz->vflag[0]) {
  4345. if(j==0 || ((m > 0) && harmonic_is_duplicated(j,m,hno))) {
  4346. dhere += dstep;
  4347. j++;
  4348. continue;
  4349. }
  4350. }
  4351. for(k = here_in_buf; k < there_in_buf;k++)
  4352. obuf[opos++] += (float)(ibuf[k] * gain);
  4353. if(opos > lastwritend)
  4354. lastwritend = opos;
  4355. dhere += dstep;
  4356. j++;
  4357. }
  4358. }
  4359. } else { /* INHARMONIC */
  4360. for(m=0;m<dz->itemcnt;m++) {
  4361. dstep = (double)seglen/ihno[m];
  4362. gain = amp[m];
  4363. dhere = (double)startopos;
  4364. j = 0;
  4365. while((double)j < (ihno[m] * dz->param[ISTR])) {
  4366. opos = (int)round(dhere);
  4367. if(j > 0) {
  4368. for(k = here_in_buf; k < there_in_buf;k++)
  4369. obuf[opos++] += (float)(ibuf[k] * gain);
  4370. if(opos > lastwritend)
  4371. lastwritend = opos;
  4372. }
  4373. dhere += dstep;
  4374. j++;
  4375. }
  4376. }
  4377. }
  4378. here = cuttime[n];
  4379. if(there_in_buf >= dz->buflen) {
  4380. memcpy((char*)ibuf,(char*)ibuf2,dz->buflen * sizeof(float));
  4381. memset((char*)ibuf2,0,dz->buflen * sizeof(float));
  4382. if((exit_status = read_samps(ibuf2,dz))<0)
  4383. return(exit_status);
  4384. startsamp += dz->buflen;
  4385. }
  4386. }
  4387. opos = lastwritend;
  4388. if(opos > 0) {
  4389. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,opos,1,obuf,1,dz)) < 0)
  4390. return(exit_status);
  4391. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,opos,1,obuf,1,dz)) < 0)
  4392. return(exit_status);
  4393. display_virtual_time(dz->insams[0],dz);
  4394. for(j=0;j<opos;j++)
  4395. maxamp = max(fabs(obuf[j]),maxamp);
  4396. }
  4397. if(maxamp > .999)
  4398. atten = .999/maxamp;
  4399. else
  4400. atten = 1.0;
  4401. if((sndseekEx(dz->ifd[0],0,0)<0)){
  4402. sprintf(errstr,"sndseek() failed\n");
  4403. return SYSTEM_ERROR;
  4404. }
  4405. dz->total_samps_read = 0;
  4406. dz->samps_left = dz->insams[0];
  4407. here = 0;
  4408. lastwritend = 0;
  4409. memset((char*)ovflw,0,dz->buflen * sizeof(float));
  4410. memset((char*)obuf,0,dz->buflen * 2 * sizeof(float));
  4411. memset((char*)ibuf,0,dz->buflen * 2 * sizeof(float));
  4412. if((exit_status = read_samps(ibuf,dz))<0)
  4413. return(exit_status);
  4414. if((exit_status = read_samps(ibuf2,dz))<0)
  4415. return(exit_status);
  4416. display_virtual_time(0,dz);
  4417. fprintf(stdout,"INFO: Creating the output.\n");
  4418. fflush(stdout);
  4419. for(n = 0; n < cutcnt;n++) {
  4420. here_in_buf = here - startsamp;
  4421. there_in_buf = cuttime[n] - startsamp;
  4422. seglen = there_in_buf - here_in_buf;
  4423. opos = here; /* absolute output position */
  4424. opos -= lastwrite; /* output position relative to buf start */
  4425. if((overflow = opos - (dz->buflen * 2))>0) {
  4426. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,dz->buflen,0,obuf,1,dz)) < 0)
  4427. return(exit_status);
  4428. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,dz->buflen,0,obuf,1,dz)) < 0)
  4429. return(exit_status);
  4430. for(j=0;j<dz->buflen;j++)
  4431. obuf[j] = (float)(obuf[j] * atten);
  4432. if((exit_status = write_samps(obuf,dz->buflen,dz))<0)
  4433. return(exit_status);
  4434. lastwrite += dz->buflen;
  4435. memcpy((char*)obuf,(char*)obuf2,dz->buflen * sizeof(float));
  4436. memset((char*)obuf2,0,dz->buflen * sizeof(float));
  4437. memcpy((char*)obuf2,(char*)ovflw,overflow * sizeof(float));
  4438. memset((char*)ovflw,0,dz->buflen * sizeof(float));
  4439. lastwritend -= dz->buflen;
  4440. opos -= dz->buflen;
  4441. }
  4442. if((opos + (seglen * 2)) >= dz->buflen * 3) {
  4443. sprintf(errstr,"OVERFLOWED OUTPUT BUFFER.\n");
  4444. return(MEMORY_ERROR);
  4445. }
  4446. startopos = opos;
  4447. display_virtual_time(here,dz);
  4448. for(k = here_in_buf; k < there_in_buf;k++)
  4449. obuf[opos++] += ibuf[k];
  4450. if(opos > lastwritend)
  4451. lastwritend = opos;
  4452. if(dz->mode == 0) { /* HARMONIC */
  4453. for(m=0;m<dz->itemcnt;m++) {
  4454. dstep = (double)seglen/(double)hno[m];
  4455. gain = amp[m];
  4456. dhere = (double)startopos + ((m+1) * dz->iparam[ISTR]);
  4457. j = 0;
  4458. while(j < hno[m]) {
  4459. opos = (int)round(dhere);
  4460. if(dz->mode == 0 && dz->vflag[0]) {
  4461. if(j==0 || ((m > 0) && harmonic_is_duplicated(j,m,hno))) {
  4462. dhere += dstep;
  4463. j++;
  4464. continue;
  4465. }
  4466. }
  4467. for(k = here_in_buf; k < there_in_buf;k++)
  4468. obuf[opos++] += (float)(ibuf[k] * gain);
  4469. if(opos > lastwritend)
  4470. lastwritend = opos;
  4471. dhere += dstep;
  4472. j++;
  4473. }
  4474. }
  4475. } else { /* INHARMONIC */
  4476. for(m=0;m<dz->itemcnt;m++) {
  4477. dstep = (double)seglen/ihno[m];
  4478. gain = amp[m];
  4479. dhere = (double)startopos;
  4480. j = 0;
  4481. while((double)j < (ihno[m] * dz->param[ISTR])) {
  4482. opos = (int)round(dhere);
  4483. if(j > 0) {
  4484. for(k = here_in_buf; k < there_in_buf;k++)
  4485. obuf[opos++] += (float)(ibuf[k] * gain);
  4486. if(opos > lastwritend)
  4487. lastwritend = opos;
  4488. }
  4489. dhere += dstep;
  4490. j++;
  4491. }
  4492. }
  4493. }
  4494. here = cuttime[n];
  4495. if(there_in_buf >= dz->buflen) {
  4496. memcpy((char*)ibuf,(char*)ibuf2,dz->buflen * sizeof(float));
  4497. memset((char*)ibuf2,0,dz->buflen * sizeof(float));
  4498. if((exit_status = read_samps(ibuf2,dz))<0)
  4499. return(exit_status);
  4500. startsamp += dz->buflen;
  4501. }
  4502. }
  4503. opos = lastwritend;
  4504. if(opos > 0) {
  4505. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,opos,1,obuf,1,dz)) < 0)
  4506. return(exit_status);
  4507. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,opos,1,obuf,1,dz)) < 0)
  4508. return(exit_status);
  4509. for(j=0;j<opos;j++)
  4510. obuf[j] = (float)(obuf[j] * atten);
  4511. if((exit_status = write_samps(obuf,opos,dz))<0)
  4512. return(exit_status);
  4513. }
  4514. return FINISHED;
  4515. }
  4516. if(opos > 0) {
  4517. if((exit_status = eliminate_too_short_events(&zcnt,&final_pos,&sig_cnt,opos,1,obuf,1,dz)) < 0)
  4518. return(exit_status);
  4519. if((exit_status = smooth_bad_events(&zcntb,&final_posb,&sig_cntb,stsmoothed,opos,1,obuf,1,dz)) < 0)
  4520. return(exit_status);
  4521. if((exit_status = write_samps(obuf,opos,dz))<0)
  4522. return(exit_status);
  4523. }
  4524. return FINISHED;
  4525. }
  4526. /************************* EXTRACT_PITCH_DEPENDENT_ENV_FROM_SNDFILE *******************************/
  4527. int extract_pitch_dependent_env_from_sndfile(int minwsize,int k,int *maxwsize,dataptr dz)
  4528. {
  4529. int exit_status;
  4530. double tconvertor = 1.0/(double)dz->infile->srate;
  4531. double fconvertor = (double)dz->infile->srate/WINDIV;
  4532. int start_samp = 0, envwindow_sampsize, big_envwindow_sampsize, here = 0, start_buf = 0;
  4533. float *env = dz->env;
  4534. double time = 0.0;
  4535. if((dz->ssampsread = fgetfbufEx(dz->sampbuf[0], dz->buflen,dz->ifd[k],0)) < 0) {
  4536. sprintf(errstr,"Can't read samples from input soundfile %d\n",k+1);
  4537. return(SYSTEM_ERROR);
  4538. }
  4539. if(dz->ssampsread < dz->insams[k]) {
  4540. if((dz->ssampsread = fgetfbufEx(dz->sampbuf[1], dz->buflen,dz->ifd[k],0)) < 0) {
  4541. sprintf(errstr,"Can't read samples from input soundfile %d\n",k+1);
  4542. return(SYSTEM_ERROR);
  4543. }
  4544. }
  4545. if((exit_status = read_value_from_brktable(time,k,dz))<0)
  4546. return(exit_status);
  4547. if(dz->param[k] < 0.0) /* no-pitch */
  4548. envwindow_sampsize = minwsize;
  4549. else
  4550. envwindow_sampsize = (int)round(fconvertor/dz->param[k]);
  4551. while(here < dz->insams[k]) {
  4552. if(here - start_buf >= dz->buflen) {
  4553. memset((char *)dz->sampbuf[0],0,dz->buflen * sizeof(float));
  4554. memcpy((char *)dz->sampbuf[0],(char *)dz->sampbuf[1],dz->buflen * sizeof(float));
  4555. start_buf += dz->buflen;
  4556. if(dz->total_samps_read < dz->insams[k]) {
  4557. if((dz->ssampsread = fgetfbufEx(dz->sampbuf[1], dz->buflen,dz->ifd[k],0)) < 0) {
  4558. sprintf(errstr,"Can't read samples from input soundfile %d\n",k+1);
  4559. return(SYSTEM_ERROR);
  4560. }
  4561. }
  4562. }
  4563. big_envwindow_sampsize = envwindow_sampsize * 2;
  4564. *env++ = (float)time;
  4565. if(here + big_envwindow_sampsize >= dz->insams[k]) {
  4566. here -= envwindow_sampsize;
  4567. big_envwindow_sampsize = dz->insams[k] - here;
  4568. *env++ = getmaxsampr(start_samp,big_envwindow_sampsize,dz->sampbuf[0]);
  4569. break;
  4570. }
  4571. start_samp = here - start_buf;
  4572. *env++ = getmaxsampr(start_samp,big_envwindow_sampsize,dz->sampbuf[0]);
  4573. here += envwindow_sampsize;
  4574. time = (float)(here * tconvertor);
  4575. if((exit_status = read_value_from_brktable(time,k,dz))<0)
  4576. return(exit_status);
  4577. if(dz->param[k] < 0.0) /* no-pitch */
  4578. envwindow_sampsize = minwsize;
  4579. else
  4580. envwindow_sampsize = (int)round(fconvertor/dz->param[k]);
  4581. if(envwindow_sampsize > *maxwsize)
  4582. *maxwsize = envwindow_sampsize;
  4583. }
  4584. *maxwsize *= 2; /* using overlapping windows */
  4585. dz->envend = env;
  4586. memset((char *)dz->sampbuf[0],0,dz->buflen * sizeof(float));
  4587. memset((char *)dz->sampbuf[1],0,dz->buflen * sizeof(float));
  4588. return FINISHED;
  4589. }
  4590. /*************************** GETMAXSAMPR ******************************/
  4591. float getmaxsampr(int startsamp, int sampcnt,float *buffer)
  4592. {
  4593. int i, endsamp = startsamp + sampcnt;
  4594. float thisval, thismaxsamp = -1;
  4595. for(i = startsamp; i<endsamp; i++) {
  4596. if((thisval = (float)fabs(buffer[i]))>thismaxsamp)
  4597. thismaxsamp = thisval;
  4598. }
  4599. return(thismaxsamp);
  4600. }
  4601. /************************** GET_MIN_WSIZE **************************/
  4602. int get_min_wsize(int *minwsize, int k, dataptr dz) {
  4603. int n,m;
  4604. int wsize;
  4605. double fconvertor = (double)dz->infile->srate/WINDIV, frq;
  4606. *minwsize = dz->insams[k];
  4607. for(n=0,m=1;n<dz->brksize[k];n++,m+=2) {
  4608. frq = dz->brk[k][m];
  4609. if(frq > 0.0) {
  4610. wsize = (int)round(fconvertor/frq);
  4611. if(wsize < *minwsize)
  4612. *minwsize = wsize;
  4613. }
  4614. }
  4615. if(*minwsize >= dz->insams[k])
  4616. return(DATA_ERROR);
  4617. return FINISHED;
  4618. }
  4619. /************************** FIND_MIN_ENERGY_DOWNWARD_ZERO_CROSSING_POINT **************************/
  4620. int find_min_energy_downward_zero_crossing_point(int *n,int *trofpnt,int trofpntcnt,
  4621. double *scanarray,int *cutcnt,int *cut,int kk,int cutstart,dataptr dz)
  4622. {
  4623. int exit_status;
  4624. int check;
  4625. float starttime, val, *buf = dz->sampbuf[0];
  4626. int wsize, thissamp, hisamp, losamp, j, zc_cnt, at;
  4627. int endsamp, startsamp, seglen;
  4628. int newpos, diff, losampinbuf, hisampinbuf, here, there, localpeakcnt;
  4629. int k, first_downcross=0, mindiff, goalseglen, newseglen, thiscut;
  4630. double official_pitchlen;
  4631. int pitchseg_lolimit, pitchseg_hilimit;
  4632. int done;
  4633. int pitchwindow, ideal_place, last_startsamp, thiscutinbuf, lastcut, lastcutinbuf;
  4634. int seg1, seg2;
  4635. double rat;
  4636. endsamp = dz->total_samps_read;
  4637. startsamp = endsamp - dz->ssampsread;
  4638. j = trofpnt[*n];
  4639. starttime = dz->env[j];
  4640. wsize = read_validpitch_wsize_in_samps_from_brktable(starttime,kk,dz);
  4641. thissamp = (int)round(starttime * (double)dz->infile->srate);
  4642. losamp = thissamp;
  4643. if((exit_status = read_value_from_brktable(starttime,kk,dz))<0) /* get pitch at start of window */
  4644. return(exit_status);
  4645. if(losamp >= dz->insams[kk])
  4646. return(FINISHED);
  4647. while(losamp >= endsamp) {
  4648. if((dz->ssampsread = fgetfbufEx(dz->sampbuf[0], dz->buflen,dz->ifd[kk],0)) < 0) {
  4649. sprintf(errstr,"Can't read samples from input soundfile %d\n",kk+1);
  4650. return(SYSTEM_ERROR);
  4651. }
  4652. dz->total_samps_read += dz->ssampsread;
  4653. dz->samps_left -= dz->ssampsread;
  4654. endsamp = dz->total_samps_read;
  4655. startsamp = endsamp - dz->ssampsread;
  4656. }
  4657. hisamp = min(dz->insams[kk],thissamp + wsize); /* get end-of-search area from wtime & half-wsize */
  4658. if(hisamp >= dz->insams[kk])
  4659. hisamp = dz->insams[kk];
  4660. if(hisamp > endsamp) {
  4661. newpos = (losamp/SECSIZE) * SECSIZE; /* if end-of--search beyond end of current buf */
  4662. if((sndseekEx(dz->ifd[kk],newpos,0)<0)){ /* adjust buffer to contain whole search area */
  4663. sprintf(errstr,"sndseek() failed\n");
  4664. return SYSTEM_ERROR;
  4665. }
  4666. diff = dz->total_samps_read - newpos;
  4667. dz->total_samps_read -= diff;
  4668. dz->samps_left += diff;
  4669. if((dz->ssampsread = fgetfbufEx(dz->sampbuf[0], dz->buflen,dz->ifd[kk],0)) < 0) {
  4670. sprintf(errstr,"Can't read samples from input soundfile %d\n",kk+1);
  4671. return(SYSTEM_ERROR);
  4672. }
  4673. dz->total_samps_read += dz->ssampsread;
  4674. dz->samps_left -= dz->ssampsread;
  4675. endsamp = dz->total_samps_read;
  4676. startsamp = endsamp - dz->ssampsread;
  4677. }
  4678. losampinbuf = losamp - startsamp; /* get search ends relative to buffer */
  4679. hisampinbuf = hisamp - startsamp;
  4680. if(dz->param[kk] > 0) {
  4681. pitchwindow = (int)round((double)dz->infile->srate/dz->param[kk]);
  4682. ideal_place = cut[(*cutcnt) - 1] + pitchwindow;
  4683. ideal_place -= startsamp;
  4684. } else {
  4685. ideal_place = -1;
  4686. }
  4687. here = losampinbuf;
  4688. there = hisampinbuf;
  4689. zc_cnt = count_zerocrossings(losampinbuf,hisampinbuf,buf);
  4690. while(zc_cnt > 5) /* Divide segment into 4 quarters, making 3 overlapping areas, while any area has >= 5 zcrossings */
  4691. zc_cnt = triangulate_env(&here,&there,ideal_place,buf); /* and find area with least energy */
  4692. losamp = here;
  4693. //NEW
  4694. losamp = here + startsamp;
  4695. if((exit_status = find_the_local_peaks(&here,&there,buf,n,trofpntcnt,trofpnt,
  4696. &startsamp,&endsamp,losamp,cut,*cutcnt,scanarray,scanarray,&localpeakcnt,&first_downcross,kk,dz) )<0)
  4697. return(exit_status);
  4698. if(exit_status == FINISHED)
  4699. return(exit_status);
  4700. if((exit_status = mark_cut(cutcnt,cut,localpeakcnt,scanarray,here,there,startsamp,first_downcross,starttime,1,dz))<0)
  4701. return(exit_status);
  4702. if((*cutcnt)-1 < cutstart) /* FIND LENGTH OF SEGMENT GENERATED */
  4703. seglen = cut[*cutcnt];
  4704. else
  4705. seglen = cut[*cutcnt] - cut[(*cutcnt)-1];
  4706. if(seglen == 0) { /* ELIMINATE ZERO-LENGTH SEGMENTS (CUTS AT SAME TIME) */
  4707. (*cutcnt)--;
  4708. } else if(*cutcnt > cutstart && dz->param[kk] > 0) {
  4709. /* CHECK THE LENGTH OF THE WINDOW AGAINST WHAT WE'D PREDICT FROM THE PITCH VALUE */
  4710. check = 1;
  4711. while(check) {
  4712. official_pitchlen = (double)dz->infile->srate/dz->param[kk];
  4713. pitchseg_lolimit = (int)round(official_pitchlen/PITCHERROR);
  4714. pitchseg_hilimit = (int)round(official_pitchlen * PITCHERROR);
  4715. /* IF SEGLEN IS ACCEPTABLE, BREAK */
  4716. if(seglen > pitchseg_lolimit && seglen < pitchseg_hilimit) {
  4717. break;
  4718. }
  4719. if(seglen < pitchseg_lolimit) { /* TOO SHORT ITEM, DELETE FROM CUTLIST */
  4720. (*cutcnt)--;
  4721. break;
  4722. }
  4723. /* TOO LONG ITEM */
  4724. /* BRUTE FORCE METHOD, CUT AT ANY DOWN ZEROCROSS NEAR THE RIGHT TIME */
  4725. /* TRY TO MAKE A CUT HALF-WAY THROUGH THE SEGMENT */
  4726. at = 0;
  4727. mindiff = seglen;
  4728. goalseglen = seglen/2; /* set the goal segement length to half the current seglen */
  4729. /* Geteach zero-crossing in this segment */
  4730. last_startsamp = -1;
  4731. thiscutinbuf = cut[*cutcnt] - startsamp;
  4732. lastcut = cut[(*cutcnt) - 1];
  4733. lastcutinbuf = lastcut - startsamp;
  4734. if(lastcutinbuf < 0) {
  4735. last_startsamp = startsamp;
  4736. if((sndseekEx(dz->ifd[kk],lastcut,0)<0)){
  4737. sprintf(errstr,"sndseek() failed\n");
  4738. return SYSTEM_ERROR;
  4739. }
  4740. dz->total_samps_read = lastcut;
  4741. if((dz->ssampsread = fgetfbufEx(dz->sampbuf[0], dz->buflen,dz->ifd[kk],0)) < 0) {
  4742. sprintf(errstr,"Can't read samples from input soundfile %d\n",kk+1);
  4743. return(SYSTEM_ERROR);
  4744. }
  4745. dz->total_samps_read += dz->ssampsread;
  4746. dz->samps_left = dz->insams[kk] - dz->total_samps_read;
  4747. startsamp = dz->total_samps_read - dz->ssampsread;
  4748. thiscutinbuf -= lastcutinbuf; /* lastcutinbuf is -ve */
  4749. lastcutinbuf = 0;
  4750. }
  4751. done = 0;
  4752. k = lastcutinbuf;
  4753. val = buf[++k];
  4754. while(k < thiscutinbuf) {
  4755. if(val > 0.0) {
  4756. while(val >= 0.0) {
  4757. k++;
  4758. if(k >= thiscutinbuf) {
  4759. done = 1;
  4760. break;
  4761. }
  4762. val = buf[k];
  4763. }
  4764. }
  4765. if(done)
  4766. break;
  4767. if(val < 0.0) {
  4768. newseglen = k - lastcutinbuf;
  4769. diff = newseglen - goalseglen;
  4770. if(abs(diff) < mindiff) { /* if a segment cut from here is closer to the goallength, mark it */
  4771. mindiff = abs(diff);
  4772. at = k + startsamp;
  4773. } else if(diff > 0) { /* if not closer, and already above goal length, finished */
  4774. break;
  4775. }
  4776. while(val < 0.0) {
  4777. k++;
  4778. if(k >= thiscutinbuf) {
  4779. done = 1;
  4780. break;
  4781. }
  4782. val = buf[k];
  4783. }
  4784. }
  4785. if(done)
  4786. break;
  4787. if(val == 0.0) {
  4788. while(val == 0.0) {
  4789. k++;
  4790. if(k >= thiscutinbuf)
  4791. break;
  4792. val = buf[k];
  4793. }
  4794. }
  4795. }
  4796. if(last_startsamp >= 0) { /* rejiggle bufs */
  4797. if((sndseekEx(dz->ifd[kk],startsamp,0)<0)){
  4798. sprintf(errstr,"sndseek() failed\n");
  4799. return SYSTEM_ERROR;
  4800. }
  4801. dz->total_samps_read = startsamp;
  4802. if((dz->ssampsread = fgetfbufEx(dz->sampbuf[0], dz->buflen,dz->ifd[kk],0)) < 0) {
  4803. sprintf(errstr,"Can't read samples from input soundfile %d\n",kk+1);
  4804. return(SYSTEM_ERROR);
  4805. }
  4806. dz->total_samps_read += dz->ssampsread;
  4807. dz->samps_left = dz->insams[kk] - dz->total_samps_read;
  4808. }
  4809. if(at) { /* if appropriate cutpoint found, mark it */
  4810. seg1 = (at - startsamp) - lastcutinbuf;
  4811. seg2 = thiscutinbuf - (at - startsamp);
  4812. rat = (double)seg1/(double)seg2;
  4813. if(rat < 2.0 && rat > .5) { /* if new cut is reasonable */
  4814. thiscut = cut[*cutcnt]; /* insert new cuts */
  4815. cut[*cutcnt] = at;
  4816. (*cutcnt)++;
  4817. cut[*cutcnt] = thiscut;
  4818. }
  4819. }
  4820. break;
  4821. }
  4822. }
  4823. return FINISHED;
  4824. }
  4825. /**************************** READ_VALIDPITCH_WSIZE_IN_SAMPS_FROM_BRKTABLE *****************************/
  4826. int read_validpitch_wsize_in_samps_from_brktable(double thistime,int kk,dataptr dz)
  4827. {
  4828. double dnval = 0.0, upval = 0.0, val, time;
  4829. double diff, mindiff = HUGE;
  4830. double dntimediff = -1.0, uptimediff = -1.0;
  4831. int n, m, wsize, here = 0;
  4832. for(n = 0,m = 0;n < dz->brksize[kk];n++,m+=2) {
  4833. time = dz->brk[kk][m];
  4834. if((diff = fabs(time - thistime)) < mindiff) {
  4835. mindiff = diff;
  4836. here = m;
  4837. }
  4838. n++;
  4839. }
  4840. time = dz->brk[kk][here];
  4841. val = dz->brk[kk][here+1];
  4842. if(val <= 0.0) { /* no pitch */
  4843. m = here;
  4844. m -= 2;
  4845. while(m >= 0) {
  4846. if((dnval = dz->brk[kk][m+1]) > 0.0) {
  4847. dntimediff = time - dz->brk[kk][m];
  4848. break;
  4849. }
  4850. m -= 2;
  4851. }
  4852. m = here;
  4853. m += 2;
  4854. while(m < (dz->brksize[kk] * 2)) {
  4855. if((upval = dz->brk[kk][m+1]) > 0.0) {
  4856. uptimediff = dz->brk[kk][m] - time;
  4857. break;
  4858. }
  4859. m += 2;
  4860. }
  4861. if(dntimediff < 0.0)
  4862. val = upval;
  4863. else if(uptimediff < 0.0)
  4864. val = dnval;
  4865. else if(uptimediff < dntimediff)
  4866. val = upval;
  4867. else
  4868. val = dnval;
  4869. }
  4870. wsize = (int)round((double)dz->infile->srate/val);
  4871. return wsize;
  4872. }
  4873. /**************************** NEXT_ZERO_CROSS *****************************/
  4874. int next_zero_cross(int here,dataptr dz)
  4875. {
  4876. float *buf = dz->sampbuf[0];
  4877. float val = buf[here];
  4878. while((val = buf[here]) >= 0.0)
  4879. here++;
  4880. return here;
  4881. }
  4882. /**************************** PREVIOUS_ZERO_CROSS *****************************/
  4883. int previous_zero_cross(int here,int firstzero,dataptr dz)
  4884. {
  4885. float *buf = dz->sampbuf[0];
  4886. float val;
  4887. while((val = buf[here]) < 0.0) {
  4888. here--;
  4889. if(here <= firstzero)
  4890. return firstzero;
  4891. }
  4892. here++;
  4893. return here;
  4894. }
  4895. /*************************** FIND_MAX_NOPITCH_STRETCH **************************/
  4896. int find_max_nopitch_stretch(dataptr dz)
  4897. {
  4898. int n, m, seccnt;
  4899. double start = -1.0, end = -1.0, maxblank = 0.0, thisblank;
  4900. for(n=0,m=1;n<dz->brksize[0];n++,m+=2) {
  4901. if(start >= 0.0) {
  4902. if(dz->brk[0][m] < 0.0)
  4903. end = dz->brk[0][m-1];
  4904. else {
  4905. if(end > 0.0) {
  4906. thisblank = end - start;
  4907. if(thisblank > maxblank)
  4908. maxblank = thisblank;
  4909. }
  4910. start = -1.0;
  4911. end = -1.0;
  4912. }
  4913. } else if(dz->brk[0][m] < 0.0)
  4914. start = dz->brk[0][m-1];
  4915. }
  4916. n = (int)ceil((maxblank + 200) * (double)dz->infile->srate);
  4917. seccnt = n/SECSIZE;
  4918. if(seccnt * SECSIZE < n)
  4919. seccnt++;
  4920. n = seccnt * SECSIZE;
  4921. return n;
  4922. }
  4923. /******************************** GET_ENVELOPE_TROUGHS ********************************/
  4924. int get_envelope_troughs(int *trofpnt,int *trofpntcnt,int envcnt,dataptr dz)
  4925. {
  4926. int n;
  4927. int lasttrofpntcnt = 0, realtrofpntcnt = 0, reallasttrofpntcnt = 0;
  4928. float lastenval;
  4929. int zerotrof;
  4930. trofpnt[0] = 0;
  4931. n = 1; /* time-val pairs : vals are odd numbers */
  4932. lastenval = dz->env[n];
  4933. if(lastenval <= 0.0) {
  4934. trofpnt[0] = 0; /* mark start of zero trough */
  4935. while(dz->env[n] <= 0.0) {
  4936. n+= 2;
  4937. if(n >= envcnt) {
  4938. sprintf(errstr,"NO PEAKS FOUND IN ENVELOPE\n");
  4939. return(GOAL_FAILED);
  4940. }
  4941. }
  4942. n -= 2;
  4943. if(n > 1) { /* if zero trough persists, also mark its end */
  4944. trofpnt[1] = n-1; /* times are even numbers : n is odd */
  4945. lastenval = dz->env[n];
  4946. *trofpntcnt = 1;
  4947. lasttrofpntcnt = 1;
  4948. }
  4949. n += 2;
  4950. } else {
  4951. n += 2;
  4952. while(n < envcnt) { /* GET FIRST ENVELOPE TROUGH */
  4953. if(dz->env[n] > lastenval) {
  4954. trofpnt[0] = 0;
  4955. lastenval = dz->env[n];
  4956. n+=2;
  4957. break;
  4958. } else if (dz->env[n] < lastenval) {
  4959. trofpnt[0] = n-1; /* times are even numbers : n is odd */
  4960. lastenval = dz->env[n];
  4961. n+=2;
  4962. break;
  4963. }
  4964. lastenval = dz->env[n];
  4965. n+=2;
  4966. }
  4967. }
  4968. if(n >= envcnt) {
  4969. sprintf(errstr,"NO PEAKS FOUND IN ENVELOPE\n");
  4970. return(GOAL_FAILED);
  4971. }
  4972. zerotrof = 0;
  4973. while(n < envcnt) { /* GET ENVELOPE TROUGHS */
  4974. if(dz->env[n] > lastenval) {
  4975. if(zerotrof) { /* mark and count end of a zero section */
  4976. *trofpntcnt = lasttrofpntcnt + 1;
  4977. trofpnt[*trofpntcnt] = n-3; /* times are even numbers : n is odd; previous time is a zeroval */
  4978. lasttrofpntcnt = *trofpntcnt; /* count end-of-a-zero-section: but don't count as a real (new) trof */
  4979. zerotrof = 0;
  4980. }
  4981. *trofpntcnt = lasttrofpntcnt + 1;
  4982. realtrofpntcnt = reallasttrofpntcnt + 1;
  4983. } else if (dz->env[n] < lastenval) { /* can't be in a zero block, as zero is min val */
  4984. trofpnt[*trofpntcnt] = n-1; /* times are even numbers : n is odd */
  4985. lasttrofpntcnt = *trofpntcnt;
  4986. reallasttrofpntcnt = realtrofpntcnt;
  4987. } else if(dz->env[n] <= 0.0) { /* dz->env[n] == previous value SO zero value is continued */
  4988. zerotrof = 1;
  4989. }
  4990. lastenval = dz->env[n];
  4991. n+=2;
  4992. }
  4993. if(realtrofpntcnt < 2) {
  4994. sprintf(errstr,"NO SIGNIFICANT PEAKS FOUND IN ENVELOPE\n");
  4995. return(GOAL_FAILED);
  4996. }
  4997. return(FINISHED);
  4998. }
  4999. /******************************** CHOP_ZERO_SIGNAL_AREAS_INTO_COMPATIBLE_UNITS ********************************/
  5000. int chop_zero_signal_areas_into_compatible_units(int *cutcnt,int *cuttime,int cutstart,int kk,dataptr dz)
  5001. {
  5002. int exit_status, iszero;
  5003. int n, m, k, seglen, zeromax, zerostart=0, maxzerostart, maxzeroend = 0, thispitch, bufstart;
  5004. int zerolen, segcnt;
  5005. double time, dseglen, incr;
  5006. float *buf = dz->sampbuf[0];
  5007. time = 0.0;
  5008. for(n=cutstart+1;n < *cutcnt;n++) {
  5009. time = (double)cuttime[n-1]/(double)dz->infile->srate;
  5010. if((exit_status = read_value_from_brktable(time,kk,dz))<0)
  5011. return(exit_status);
  5012. if(dz->param[kk] < 0.0) {
  5013. if((exit_status = get_time_nearest_true_pitch(time,&(dz->param[kk]),kk,dz))<0)
  5014. return(exit_status);
  5015. }
  5016. thispitch = (int)round((double)dz->infile->srate/dz->param[kk]);
  5017. seglen = cuttime[n] - cuttime[n-1];
  5018. if(seglen > thispitch * 2) {
  5019. iszero = 0;
  5020. zeromax = 0;
  5021. if((sndseekEx(dz->ifd[kk],cuttime[n-1],0)<0)){
  5022. sprintf(errstr,"sndseek() failed\n");
  5023. return SYSTEM_ERROR;
  5024. }
  5025. if((dz->ssampsread = fgetfbufEx(dz->sampbuf[0], dz->buflen,dz->ifd[kk],0)) < 0) {
  5026. sprintf(errstr,"Can't read samples from input soundfile %d\n",kk+1);
  5027. return(SYSTEM_ERROR);
  5028. }
  5029. dz->total_samps_read += dz->ssampsread;
  5030. dz->samps_left -= dz->ssampsread;
  5031. bufstart = cuttime[n-1];
  5032. maxzerostart = -1;
  5033. for(m = 0;m < seglen; m++) {
  5034. if(buf[m] == 0.0) {
  5035. if(!iszero) {
  5036. zerostart = m;
  5037. iszero = 1;
  5038. }
  5039. } else {
  5040. if(iszero) {
  5041. if((m - zerostart) > zeromax) {
  5042. zeromax = m - zerostart;
  5043. maxzerostart = zerostart;
  5044. maxzeroend = m;
  5045. }
  5046. iszero = 0;
  5047. }
  5048. }
  5049. }
  5050. if(maxzerostart >= 0) {
  5051. while(buf[maxzeroend] >= 0.0)
  5052. maxzeroend++;
  5053. if(maxzeroend > seglen || maxzeroend > dz->ssampsread) /* can't find appropriate down_zcross within segment */
  5054. continue;
  5055. if(maxzerostart <= thispitch)
  5056. maxzerostart = thispitch;
  5057. if(maxzeroend < ALMOST_OCT * thispitch) /* insufficient zeros to divide the seg */
  5058. continue;
  5059. for(k = *cutcnt;k >= n;k--) /* insert first extra point */
  5060. cuttime[k+1] = cuttime[k];
  5061. cuttime[n] = maxzerostart + bufstart;
  5062. (*cutcnt)++;
  5063. n++;
  5064. zerolen = maxzeroend - maxzerostart; /* if enough space for further cuts, make them */
  5065. if((segcnt = (int)round((double)zerolen/(double)thispitch)) > 0) {
  5066. dseglen = (double)zerolen/(double)segcnt;
  5067. incr = dseglen;
  5068. zerostart = maxzerostart;
  5069. maxzerostart = zerostart + (int)floor(dseglen);
  5070. while(maxzerostart < (maxzeroend - 1)) { /* SAFETY */
  5071. for(k = *cutcnt;k >= n;k--)
  5072. cuttime[k+1] = cuttime[k];
  5073. cuttime[n] = maxzerostart + bufstart;
  5074. (*cutcnt)++;
  5075. n++;
  5076. dseglen += incr;
  5077. maxzerostart = zerostart + (int)floor(dseglen);
  5078. }
  5079. }
  5080. }
  5081. }
  5082. }
  5083. return(FINISHED);
  5084. }
  5085. /******************************** GET_TIME_NEAREST_TRUE_PITCH ********************************/
  5086. int get_time_nearest_true_pitch(double time,double *pitch,int kk,dataptr dz)
  5087. {
  5088. int n, no_up = 0, no_dn = 0, start;
  5089. double uptime=0.0, dntime, uppitch=0.0, dnpitch;
  5090. double *thisbrk = dz->brk[kk];
  5091. for(n=0;n<dz->brksize[kk] * 2;n+=2) {
  5092. if(thisbrk[n] >= time)
  5093. break;
  5094. }
  5095. if(n >= dz->brksize[kk] * 2) {
  5096. n--;
  5097. while(thisbrk[n] < 0.0)
  5098. n -= 2;
  5099. if(n<0) {
  5100. sprintf(errstr,"Pitch not found in pitch breakpoint file %d.\n",kk+1);
  5101. return(DATA_ERROR);
  5102. }
  5103. *pitch = thisbrk[n];
  5104. return FINISHED;
  5105. }
  5106. n++;
  5107. start = n;
  5108. while(thisbrk[n] < 0.0) {
  5109. n+= 2;
  5110. if(n >= dz->brksize[kk] * 2) {
  5111. no_up = 1;
  5112. break;
  5113. }
  5114. }
  5115. if(!no_up) {
  5116. uppitch = thisbrk[n];
  5117. uptime = thisbrk[n-1];
  5118. }
  5119. n = start;
  5120. while(thisbrk[n] < 0.0) {
  5121. n-= 2;
  5122. if(n < 0) {
  5123. no_dn = 1;
  5124. break;
  5125. }
  5126. }
  5127. if(no_up && no_dn) {
  5128. sprintf(errstr,"Pitch not found in pitch breakpoint file %d.\n",kk+1);
  5129. return(DATA_ERROR);
  5130. }
  5131. if(no_dn)
  5132. *pitch = uppitch;
  5133. else {
  5134. dnpitch = thisbrk[n];
  5135. dntime = thisbrk[n-1];
  5136. if(no_up)
  5137. *pitch = dnpitch;
  5138. else if((uptime - time) < (time - dntime))
  5139. *pitch = uppitch;
  5140. else
  5141. *pitch = dnpitch;
  5142. }
  5143. return FINISHED;
  5144. }
  5145. /******************************** SMOOTH_BAD_GRAINS ********************************/
  5146. int smooth_bad_grains(int seglen,int bufno, dataptr dz)
  5147. {
  5148. int dosmooth;
  5149. int k, startend;
  5150. float *buf = dz->sampbuf[bufno];
  5151. double incr, gain, diff;
  5152. int smoothlen = SMOOTHWIN;
  5153. if(seglen < SMOOTHWIN * 2)
  5154. smoothlen = seglen/2;
  5155. dosmooth = 0;
  5156. diff = fabs(buf[1] - buf[0]);
  5157. for(k = 0;k < smoothlen - 1;k++) {
  5158. /* look for sudden gradient changes relative to size of sample */
  5159. if((diff > 0.001) && (fabs(buf[k]) < (diff * 4.0))) {
  5160. dosmooth = 1;
  5161. break;
  5162. }
  5163. diff = fabs(buf[k+1] - buf[k]);
  5164. }
  5165. if(dosmooth) {
  5166. gain = 1.0;
  5167. incr = 1.0/(double)smoothlen;
  5168. for(k = smoothlen - 1;k >= 0;k--) {
  5169. gain -= incr;
  5170. buf[k] = (float)(buf[k] * gain);
  5171. }
  5172. }
  5173. startend = seglen-smoothlen;
  5174. dosmooth = 0;
  5175. diff = fabs(buf[startend] - buf[startend + 1]);
  5176. for(k = startend;k < seglen-2;k++) {
  5177. /* look for sudden gradient changes relative to size of sample */
  5178. if((diff > 0.001) && (fabs(buf[k+1]) < (diff * 4.0))) {
  5179. dosmooth = 1;
  5180. break;
  5181. }
  5182. diff = fabs(buf[k] - buf[k+1]);
  5183. }
  5184. if(dosmooth) {
  5185. gain = 1.0;
  5186. incr = 1.0/(double)smoothlen;
  5187. for(k = startend;k < seglen;k++) {
  5188. gain -= incr;
  5189. buf[k] = (float)(buf[k] * gain);
  5190. }
  5191. }
  5192. return FINISHED;
  5193. }
  5194. /******************************** COUNT_ZEROCROSSINGS ********************************/
  5195. int count_zerocrossings(int here,int there,float *buf)
  5196. {
  5197. int done = 0, isup = 0;
  5198. int zc_cnt = 0;
  5199. float val = buf[here];
  5200. while(here < there) {
  5201. while(flteq(val,0.0)) { /* only accessed if segment starts with zeros */
  5202. here++;
  5203. if(here >= there) {
  5204. done = 1;
  5205. break;
  5206. }
  5207. val = buf[here];
  5208. }
  5209. if(done)
  5210. break;
  5211. while(val >= 0.0) {
  5212. if(isup != 1) {
  5213. zc_cnt++;
  5214. isup = 1;
  5215. }
  5216. here++;
  5217. if(here >= there) {
  5218. done = 1;
  5219. break;
  5220. }
  5221. val = buf[here];
  5222. }
  5223. if(done)
  5224. break;
  5225. while(val <= 0.0) {
  5226. if(isup != -1) {
  5227. zc_cnt++;
  5228. isup = -1;
  5229. }
  5230. here++;
  5231. if(here >= there) {
  5232. done = 1;
  5233. break;
  5234. }
  5235. val = buf[here];
  5236. }
  5237. }
  5238. return zc_cnt;
  5239. }
  5240. /******************************** TRIANGULATE_ENV ********************************
  5241. *
  5242. * Segment divided into 4 quarters, to define 3 overlapping areas...
  5243. *
  5244. * 1st quarter 2nd quarter 3rd quarter 4th quarter
  5245. * ___________ ___________ ___________ ___________
  5246. * | || || || |
  5247. *
  5248. * |______Env0______________||_____Env2_______________|
  5249. *
  5250. * |_____Env1_______________|
  5251. *
  5252. * Find area with minimum energy.
  5253. */
  5254. int triangulate_env(int *here,int *there,int ideal_place,float *buf)
  5255. {
  5256. int localhere = *here, localthere = *there;
  5257. int seglen, this_zc_cnt[3], max_zc_cnt;
  5258. int quartlen, quart[5], n, m;
  5259. int oversize, use_1st_quart = 0, use_last_quart = 0;
  5260. double qsum[4], esum[3];
  5261. seglen = localthere - localhere;
  5262. quartlen = seglen/4;
  5263. oversize = seglen - (quartlen * 4);
  5264. if(*here > ideal_place)
  5265. use_1st_quart = 1;
  5266. if(*there < ideal_place)
  5267. use_last_quart =1;
  5268. if(oversize) { /* func only called if >=5 zero crossings: so >= 9 samples: so 'there' never <= 'here' */
  5269. localhere++; /* truncate examined area by 1 sample */
  5270. if(oversize > 1)
  5271. localthere--; /* truncate examined area by 1 more sample */
  5272. if(oversize > 2)
  5273. localhere++; /* truncate examined area by 1 more sample */
  5274. }
  5275. quart[0] = localhere; /* divide segment into 4 equal parts */
  5276. for(n = 0; n < 4; n++)
  5277. quart[n+1] = quart[n] + quartlen;
  5278. if(use_1st_quart)
  5279. max_zc_cnt = count_zerocrossings(quart[0],quart[2],buf);
  5280. else if(use_last_quart)
  5281. max_zc_cnt = count_zerocrossings(quart[2],quart[4],buf);
  5282. else {
  5283. max_zc_cnt = -1;
  5284. for(n = 0; n < 3; n++) { /* Find zcross count in each of 3 overalpping Envs */
  5285. this_zc_cnt[n] = count_zerocrossings(quart[n],quart[n+2],buf);
  5286. if(this_zc_cnt[n] > max_zc_cnt)
  5287. max_zc_cnt = this_zc_cnt[n];
  5288. }
  5289. }
  5290. if(max_zc_cnt < 5) /* If none of the Env-segs has >= 5 zcrossings, return withot doing anything */
  5291. return max_zc_cnt; /* causing calling loop to finish */
  5292. if(use_1st_quart) {
  5293. *here = quart[0];
  5294. *there = quart[2];
  5295. return max_zc_cnt;
  5296. } else if(use_last_quart) {
  5297. *here = quart[2];
  5298. *there = quart[4];
  5299. return max_zc_cnt;
  5300. }
  5301. for(n = 0; n < 4; n++) { /* Sum the abs-samples in each Quarter */
  5302. qsum[n] = 0.0;
  5303. for(m = quart[n];m <quart[n+1];m++)
  5304. qsum[n] += fabs(buf[m]);
  5305. }
  5306. for(n = 0; n < 3; n++) /* Sum the abs-samples in each Env */
  5307. esum[n] = qsum[n] + qsum[n+1];
  5308. if((flteq(esum[0],esum[1])) && (flteq(esum[0],esum[2]))) { /* esums are all equal */
  5309. if(this_zc_cnt[1] >= 5) { /* return middle seg limits, unless too few zcrossings */
  5310. *here = quart[1];
  5311. *there = quart[3];
  5312. return this_zc_cnt[1];
  5313. } else if(this_zc_cnt[0] >= 5) { /* else return start seg limits, unless too few zcrossings */
  5314. *here = quart[0];
  5315. *there = quart[2];
  5316. return this_zc_cnt[0];
  5317. } else { /* else return end seg limits */
  5318. *here = quart[2];
  5319. *there = quart[4];
  5320. return this_zc_cnt[2];
  5321. }
  5322. } else if((esum[0] <= esum[1]) && (esum[0] <= esum[2])) { /* esum[0] in Env0 has minimum energy */
  5323. if(this_zc_cnt[0] >= 5) { /* If Env0 has > 5 zcrossings */
  5324. *here = quart[0]; /* define this as the new working segment in which to find min energy */
  5325. *there = quart[2]; /* Else return without doing anything */
  5326. } /* (Calling process then drops out & uses segment we had at start of this func) */
  5327. return this_zc_cnt[0];
  5328. } else if((esum[1] <= esum[0]) && (esum[1] <= esum[2])) { /* esum[1] in Env1 has minimum energy */
  5329. if(this_zc_cnt[1] >= 5) {
  5330. *here = quart[1];
  5331. *there = quart[3];
  5332. }
  5333. return this_zc_cnt[1];
  5334. } /* else */ /* esum[2] in Env2 has minimum energy */
  5335. if(this_zc_cnt[2] >= 5) {
  5336. *here = quart[2];
  5337. *there = quart[4];
  5338. }
  5339. return this_zc_cnt[2];
  5340. }
  5341. /******************************** MARK_CUT ********************************/
  5342. int mark_cut(int *cutcnt,int *cut,int localpeakcnt,double *startarray,int here,int there,
  5343. int startsamp,int first_downcross,double starttime,int msg,dataptr dz)
  5344. {
  5345. double localpeak1, localpeak2, localpeak3, maxlocalpeak, minenergy, energy;
  5346. int localpeakat1, localpeakat2 , at = 0, k, m, thissampinbuf;
  5347. int up;
  5348. double *localpeak = startarray;
  5349. switch(localpeakcnt) {
  5350. case(0):
  5351. if(msg)
  5352. sprintf(errstr,"FAILED TO LOCATE CUT POINT NEAR %lf secs: NO LOCAL PEAK FOUND: IMPOSSIBLE!!\n",starttime);
  5353. return(PROGRAM_ERROR);
  5354. case(1): /* SINGLE PEAK */
  5355. if(*localpeak > 0.0) /* +ve peak, downward-cross is at end */
  5356. cut[*cutcnt] = there + startsamp; /* return location of zero crossing after peak */
  5357. else
  5358. cut[*cutcnt] = here + startsamp; /* -ve peak, zerocross at start, return location zerocros before peak */
  5359. break;
  5360. case(2): /* TWO PEAKS */
  5361. if(*localpeak > 0.0) { /* FIRST PEAK IS ABOVE ZERO: find downcross after this */
  5362. here = (int)round(*(localpeak+1)); /* location of 1st local peak */
  5363. here -= startsamp; /* location in buf */
  5364. here = next_zero_cross(here,dz); /* find following down-zerocross */
  5365. cut[*cutcnt] = here + startsamp; /* return its absolute location */
  5366. } else { /* FIRST PEAK IS BELOW ZERO */
  5367. if(fabs(*localpeak) < *(localpeak+2)) /* initial peak is lower energy */
  5368. cut[*cutcnt] = here + startsamp; /* return absolute-position of its start */
  5369. cut[*cutcnt] = there + startsamp; /*final peak is lower energy, return absolute-pos of its end */
  5370. }
  5371. break;
  5372. case(3):
  5373. localpeak1 = *localpeak++; /* THREE PEAKS */
  5374. localpeakat1 = (int)round(*localpeak++); /* Find location of (abs) max of the 3 */
  5375. maxlocalpeak = *localpeak;
  5376. at = 1;
  5377. localpeak2 = *localpeak++;
  5378. localpeakat2 = (int)round(*localpeak++);
  5379. if(localpeak2 > maxlocalpeak) {
  5380. maxlocalpeak = localpeak2;
  5381. at = 2;
  5382. }
  5383. localpeak3 = *localpeak++;
  5384. if(localpeak3 > maxlocalpeak) {
  5385. maxlocalpeak = localpeak3;
  5386. at = 3;
  5387. }
  5388. up = 0; /* Is first peak +ve or -ve */
  5389. if(localpeak1 > 0.0)
  5390. up = 1;
  5391. switch(at) { /* Select on basis of where max peak is */
  5392. case(1): /* 1st peak is max */
  5393. if(up) {
  5394. cut[*cutcnt] = there + startsamp; /* +ve cycle, return end of last peak */
  5395. } else {
  5396. here = localpeakat2 - startsamp;
  5397. here = next_zero_cross(here,dz); /* -ve cycle, return zero after peak2 */
  5398. cut[*cutcnt] = here + startsamp;
  5399. }
  5400. break;
  5401. case(2): /* 2nd peak is max */
  5402. if(localpeak1 > localpeak3) { /* 3rd peak is minimum */
  5403. if(up) {
  5404. cut[*cutcnt] = there + startsamp; /* +ve cycle, return end of peak3 */
  5405. } else {
  5406. here = localpeakat2 - startsamp;
  5407. here = next_zero_cross(here,dz); /* -ve cycle, return start of peak3 */
  5408. cut[*cutcnt] = here + startsamp;
  5409. }
  5410. } else { /* 1st peak is minimum */
  5411. if(up) {
  5412. here = localpeakat1 - startsamp;
  5413. here = next_zero_cross(here,dz); /* +ve cycle, return end of peak1 */
  5414. cut[*cutcnt] = here + startsamp;
  5415. } else {
  5416. cut[*cutcnt] = here + startsamp; /* -ve cycle, return start of peak1 */
  5417. }
  5418. }
  5419. break;
  5420. case(3): /* 3rd peak is max */
  5421. if(up) {
  5422. here = localpeakat1 - startsamp;
  5423. here = next_zero_cross(here,dz); /* +ve cycle, return end of peak1 */
  5424. cut[*cutcnt] = here + startsamp;
  5425. } else {
  5426. cut[*cutcnt] = here + startsamp; /* -ve cycle, return start of peak1 */
  5427. }
  5428. break;
  5429. }
  5430. break;
  5431. default: /* MORE THAN 3 PEAKS */
  5432. localpeak = startarray;
  5433. minenergy = HUGE; /* find minimum energy of each PAIR of peaks */
  5434. for(k = 0,m = 2; m < localpeakcnt * 2; k+=2,m+=2) {
  5435. energy = fabs(localpeak[k]) + fabs(localpeak[m]);
  5436. if(energy < minenergy) {
  5437. minenergy = energy;
  5438. at = k;
  5439. }
  5440. }
  5441. thissampinbuf = (int)round(localpeak[at+1]);
  5442. thissampinbuf -= startsamp;
  5443. if(localpeak[at] > 0.0) { /* if min energy is at a pair that starts +ve */
  5444. thissampinbuf = next_zero_cross(thissampinbuf,dz);
  5445. cut[*cutcnt] = thissampinbuf + startsamp; /* return end of that pair's 1st peak */
  5446. } else {
  5447. thissampinbuf = previous_zero_cross(thissampinbuf,first_downcross,dz);
  5448. cut[*cutcnt] = thissampinbuf + startsamp; /* else return start of that pair's 1st peak */
  5449. }
  5450. }
  5451. return(FINISHED);
  5452. }
  5453. /******************************** FIND_THE_LOCAL_PEAKS ********************************/
  5454. int find_the_local_peaks(int *here,int *there,float *buf,int *n,int trofpntcnt,int *trofpnt,
  5455. int *startsamp,int *endsamp,int losamp, int *cut, int cutcnt, double *localpeak, double *scanarray,
  5456. int *localpeakcnt,int *first_downcross,int kk,dataptr dz)
  5457. {
  5458. int notfound = 0, finished = 0, bufinc = 0;
  5459. int maxat=0;
  5460. float maxval=0.0, val;
  5461. int wsize, hisamp,losampinbuf,hisampinbuf;
  5462. int orighere, origthere;
  5463. wsize = *there - *here;
  5464. while(!finished) {
  5465. hisamp = min(dz->insams[kk],losamp + wsize); /* get end-of-search area from wtime & half-wsize */
  5466. if(hisamp >= dz->insams[kk])
  5467. hisamp = dz->insams[kk];
  5468. losampinbuf = losamp - *startsamp; /* get search ends relative to buffer */
  5469. hisampinbuf = hisamp - *startsamp;
  5470. if(hisampinbuf >= dz->buflen) {
  5471. if((sndseekEx(dz->ifd[kk],losamp,0)<0)){
  5472. sprintf(errstr,"sndseek() failed\n");
  5473. return SYSTEM_ERROR;
  5474. }
  5475. dz->total_samps_read -= dz->ssampsread;
  5476. dz->total_samps_read += losamp;
  5477. dz->samps_left = dz->insams[kk] - dz->total_samps_read;
  5478. if((dz->ssampsread = fgetfbufEx(dz->sampbuf[0], dz->buflen,dz->ifd[kk],0)) < 0) {
  5479. sprintf(errstr,"Can't read samples from input soundfile %d\n",kk+1);
  5480. return(SYSTEM_ERROR);
  5481. }
  5482. dz->total_samps_read += dz->ssampsread;
  5483. dz->samps_left -= dz->ssampsread;
  5484. *endsamp = dz->total_samps_read;
  5485. *startsamp = *endsamp - dz->ssampsread;
  5486. losampinbuf = losamp - *startsamp; /* get search ends relative to buffer */
  5487. hisampinbuf = hisamp - *startsamp;
  5488. bufinc = 0;
  5489. }
  5490. *here = losampinbuf;
  5491. /* IGNORE EDGES OF WINDOW before the first, and after the last, zero crossing */
  5492. /* MOVE START SEARCH TO FIRST ZERO CROSSING */
  5493. if(!bufinc) {
  5494. val = buf[*here];
  5495. *first_downcross = -1;
  5496. if(val >= 0.0) {
  5497. while(val >= 0.0) {
  5498. (*here)++;
  5499. if(*here >= hisampinbuf) {
  5500. notfound = 1; /* no zero-cross found */
  5501. break;
  5502. }
  5503. val = buf[*here];
  5504. }
  5505. if(notfound) {
  5506. wsize += 20; /* no zero-cross found, return to loop head */
  5507. bufinc = 1;
  5508. continue; /* and move end of search window */
  5509. }
  5510. // VAL is just after zero cross downwards
  5511. *first_downcross = *here; /* mark 1st down-zerocross in search area */
  5512. } else if (val <= 0.0) {
  5513. while(val <= 0.0) {
  5514. (*here)++;
  5515. if(*here >= hisampinbuf) {
  5516. notfound = 1; /* no zero-cross found */
  5517. break;
  5518. }
  5519. val = buf[*here];
  5520. }
  5521. if(notfound) {
  5522. wsize += 20; /* no zero-cross found, return to loop head */
  5523. bufinc = 1;
  5524. continue;
  5525. }
  5526. // VAL is just after zero cross upwards
  5527. }
  5528. }
  5529. /* MOVE END OF SEARCH TO LAST ZERO CROSSING */
  5530. *there = hisampinbuf - 1;
  5531. val = buf[*there];
  5532. if(val >= 0.0) {
  5533. //below = 0;
  5534. while(val >= 0.0) {
  5535. (*there)--;
  5536. if(*there < losampinbuf) {
  5537. break;
  5538. }
  5539. val = buf[*there];
  5540. }
  5541. } else if(val <= 0.0) {
  5542. //below = 1;
  5543. while(val <= 0.0) {
  5544. (*there)--;
  5545. if(*there < losampinbuf) {
  5546. break;
  5547. }
  5548. val = buf[*there];
  5549. }
  5550. }
  5551. if(*there <= *here) { /* There is ONLY ONE zero-crossing found */
  5552. if(buf[*here] >= 0.0) { /* This is an upward-crossing zero, NOT a downward crossing zero */
  5553. notfound = 1; /* no DOWN-zero-cross found, return to loop head */
  5554. wsize += 20;
  5555. bufinc = 1;
  5556. continue; /* and move end of search window */
  5557. }
  5558. cut[cutcnt] = *here + *startsamp; /* return the single downward-crossing zero */
  5559. *first_downcross = *here;
  5560. return FINISHED;
  5561. } /* ELSE more than one zero-cross found */
  5562. /* therefore at least one intervening (+ or -) peak exists */
  5563. (*there)++; /* move end search to the value AFTER the last zero-crossing */
  5564. finished = 1; /* break from search loop */
  5565. }
  5566. orighere = *here;
  5567. origthere = *there;
  5568. /* STORE PEAKS BETWEEN ZERO-CROSSINGS */
  5569. val = buf[*here];
  5570. while(*here < *there) {
  5571. if(val >= 0.0) {
  5572. maxval = val;
  5573. maxat = *here;
  5574. while(val >= 0.0) {
  5575. (*here)++;
  5576. if(*here >= *there)
  5577. break;
  5578. val = buf[*here];
  5579. if(val > maxval) {
  5580. maxval = val;
  5581. maxat = *here;
  5582. }
  5583. }
  5584. } else if(val <= 0.0) {
  5585. maxval = val;
  5586. maxat = *here;
  5587. while(val <= 0.0) {
  5588. (*here)++;
  5589. if(*here >= *there)
  5590. break;
  5591. val = buf[*here];
  5592. if(val < maxval) {
  5593. maxval = val;
  5594. maxat = *here;
  5595. }
  5596. }
  5597. }
  5598. if((maxval > 0.0) && (*first_downcross < 0))
  5599. *first_downcross = *here;
  5600. *localpeak++ = (double)maxval;
  5601. *localpeak++ = (double)(maxat + *startsamp);
  5602. }
  5603. *localpeakcnt = (localpeak - scanarray)/2;
  5604. if(*localpeakcnt == 1) { /* If only 1 peak, the cut points are at its start or end */
  5605. *here = orighere;
  5606. *there = origthere;
  5607. }
  5608. return CONTINUE;
  5609. }
  5610. /***************************** SMOOTH_CUTS *****************************/
  5611. int smooth_cuts(int *cut,int *cutcnt,int kk,int cutstart,dataptr dz)
  5612. {
  5613. int exit_status;
  5614. int n, k, seg0, seg1, seg2, minseg, end, start, *thiscut, srchlen, minlen;
  5615. int pitchseglim, pitchseglo=0, realend, lastcutval;
  5616. double pitchseg, lastpitchseg=0.0, time, big_seg, this_seg, last_seg, big_int, this_int, last_int;
  5617. if((sndseekEx(dz->ifd[kk],0,0) < 0)){
  5618. sprintf(errstr,"sndseek() failed\n");
  5619. return SYSTEM_ERROR;
  5620. }
  5621. dz->samps_left = dz->insams[kk];
  5622. dz->total_samps_read = 0;
  5623. if((dz->ssampsread = fgetfbufEx(dz->sampbuf[0], dz->buflen,dz->ifd[kk],0)) < 0) {
  5624. sprintf(errstr,"Can't read samples from input soundfile %d\n",kk+1);
  5625. return(SYSTEM_ERROR);
  5626. }
  5627. dz->total_samps_read += dz->ssampsread;
  5628. dz->samps_left -= dz->ssampsread;
  5629. for(n=3 + cutstart;n<*cutcnt;n++) {
  5630. lastcutval = 0;
  5631. end = cut[n];
  5632. realend = end;
  5633. time = (double)cut[n-3]/(double)dz->infile->srate;
  5634. if((exit_status = read_value_from_brktable(time,kk,dz))<0)
  5635. return(exit_status); /* Get pitch at start of cut segment */
  5636. if(dz->param[kk] > 0.0) { /* if it's a true pitch ... */
  5637. pitchseg = (double)dz->infile->srate/dz->param[kk]; /* Find corresponding segment length */
  5638. pitchseglim = (int)round(pitchseg * ALMOST_OCT); /* Go to almost an 8va up */
  5639. pitchseglo = (int)round(pitchseg/ALMOST_OCT); /* Go to almost an 8va down */
  5640. pitchseglim += cut[n-3]; /* Prevent autocorrelation-search going beyond 8va */
  5641. if(end > pitchseglim) {
  5642. end = pitchseglim; /* Typically, end of search is ~2* expected pitchseg len */
  5643. lastcutval = cut[n-2]; /* & we save origval in case new val is to be INTERPOLATED. */
  5644. } /* But if 3segs is smaller, set endofsearch to endof 3segs */
  5645. } else
  5646. pitchseg = -1.0;
  5647. start = cut[n-3];
  5648. thiscut = &(cut[n-2]);
  5649. seg0 = cut[n-2] - cut[n-3];
  5650. seg1 = cut[n-1] - cut[n-2];
  5651. seg2 = cut[n] - cut[n-1];
  5652. minseg = min(seg0,seg1);
  5653. minseg = min(minseg,seg2);
  5654. srchlen = cut[n] - cut[n-3];
  5655. if((minlen = minseg/2) <= 0) /* if previous segs tiny, e.g. just 1 seg, this val gets 0 */
  5656. minlen = min(srchlen/6,pitchseglo); /* use either ~8va below pitch, or half the average of 3seglen */
  5657. if((exit_status = auto_correlate(start,thiscut,end,realend,minlen,pitchseg,kk,dz))<0)
  5658. return(exit_status);
  5659. if(cut[n-2] >= cut[n-1]) { /* If next cut is now too near to new cut ... */
  5660. for(k = n;k<*cutcnt;k++) /* e.g. 0 20 30 40 --> 0 20 40 40 .... omit duplicate */
  5661. cut[k-1] = cut[k]; /* OR 0 20 30 40 --> 0 20 50 40 .... omit skipped val */
  5662. (*cutcnt)--;
  5663. n--;
  5664. } else if(lastcutval > 0) { /* If new cut will fit between previous, and original value here */
  5665. /* e.g. originally 20 [200] 220 240 --> 20 40 220 240 (200 stored as lastcutval) */
  5666. if(lastcutval - cut[n-2] > pitchseglo) {
  5667. for(k = *cutcnt;k>=n-1;k--) /* then reinsert orig val .... */
  5668. cut[k+1] = cut[k]; /* e.g. 20 40 220 240 --> 20 40 200 220 240 */
  5669. cut[n-1] = lastcutval;
  5670. (*cutcnt)++;
  5671. }
  5672. } /* Eliminate segs that are 8va up (twice too short) */
  5673. if((n > cutstart+3) && (lastpitchseg > 0.0) && (pitchseg > 0.0)) {
  5674. pitchseg = (pitchseg + lastpitchseg)/2.0;
  5675. this_seg = (double)(cut[n-2] - cut[n-3]);
  5676. if((big_seg = (double)(cut[n-2] - cut[n-4])) > pitchseg)
  5677. big_int = big_seg/pitchseg;
  5678. else
  5679. big_int = pitchseg/big_seg;
  5680. if(this_seg > pitchseg)
  5681. this_int = this_seg/pitchseg;
  5682. else
  5683. this_int = pitchseg/this_seg;
  5684. if((last_seg = (double)(cut[n-3] - cut[n-4])) > pitchseg)
  5685. last_int = last_seg/pitchseg;
  5686. else
  5687. last_int = pitchseg/last_seg;
  5688. if((big_int < this_int) && (big_int < last_int)) {
  5689. for(k = n-2;k < *cutcnt;k++)
  5690. cut[k-1] = cut[k];
  5691. (*cutcnt)--;
  5692. n--;
  5693. }
  5694. }
  5695. lastpitchseg = pitchseg;
  5696. }
  5697. return FINISHED;
  5698. }
  5699. /******************************** AUTO_CORRELATE ********************************
  5700. * *at
  5701. * (cut to search realend
  5702. * start adjust) end of 3segs
  5703. * ___________ ______ _____________i___________
  5704. * | | | i |
  5705. * i
  5706. *
  5707. *
  5708. *
  5709. * start = start of the segment.
  5710. * end = end of the search area for correlation between segs.
  5711. * *at = starts out as the original cut position, and may be changed by this process.
  5712. * realend = end of the group of 3 segs: if no cutsite found before 'end', extend search towards 'realend'
  5713. * (cutsites are always at downward-crossing zeros)
  5714. * minlen = minimum length of adjacent stretches of sound to compare
  5715. * pitchseg = length of seg corresponding to pitch (if any) at this place.
  5716. * if the new cutpoint is further from the pitchseg val than the original cut (*at)
  5717. * the search can be restricted to a smaller range then defiend by 'end'.
  5718. */
  5719. int auto_correlate(int start,int *at,int end,int realend,int minlen,double pitchseg,int kk,dataptr dz)
  5720. {
  5721. float *buf = dz->sampbuf[0];
  5722. int startbuf = 0, startinbuf, atinbuf, orig_atinbuf, maxinbuf, n, m, oldlen, newlen;
  5723. int newlen2=0, atinbuf2, orig_maxinbuf;
  5724. double max_auto, max_auto2, thisauto, time;
  5725. int finished, unpitched = 0, extended = 0;
  5726. if(end > dz->total_samps_read) {
  5727. if((sndseekEx(dz->ifd[kk],start,0)<0)){
  5728. sprintf(errstr,"sndseek() failed\n");
  5729. return SYSTEM_ERROR;
  5730. }
  5731. dz->total_samps_read = start;
  5732. dz->samps_left = dz->insams[kk] - start;
  5733. if((dz->ssampsread = fgetfbufEx(dz->sampbuf[0], dz->buflen,dz->ifd[kk],0)) < 0) {
  5734. sprintf(errstr,"Can't read samples from input soundfile %d\n",kk+1);
  5735. return(SYSTEM_ERROR);
  5736. }
  5737. dz->total_samps_read += dz->ssampsread;
  5738. dz->samps_left -= dz->ssampsread;
  5739. }
  5740. //NEW
  5741. startbuf = dz->total_samps_read - dz->ssampsread;
  5742. startinbuf = start - startbuf;
  5743. atinbuf = *at - startbuf;
  5744. maxinbuf = end - startbuf;
  5745. orig_atinbuf = atinbuf;
  5746. orig_maxinbuf = maxinbuf;
  5747. finished = 0;
  5748. // oldlen = *at - startinbuf;
  5749. oldlen = atinbuf - startinbuf;
  5750. do {
  5751. while(!finished) {
  5752. atinbuf2 = -1;
  5753. n = startinbuf;
  5754. m = startinbuf + minlen;
  5755. m = next_down_zcross(m,maxinbuf,buf); /* starting with a segment which ends at first down_zcross after minlen */
  5756. if(m > maxinbuf || m < 0)
  5757. break;
  5758. max_auto = autocorrelate(n,m,buf); /* autocorrelate two minsize segments */
  5759. max_auto2 = max_auto;
  5760. atinbuf = m;
  5761. for(;;) { /* gradually expand size to next (etc) down_zcross, & autocorrelate */
  5762. if((m = next_down_zcross(m,maxinbuf,buf)) < 0)
  5763. break;
  5764. if((thisauto = autocorrelate(n,m,buf)) > max_auto) {
  5765. max_auto = thisauto;
  5766. atinbuf = m;
  5767. } else if(thisauto > max_auto2) {
  5768. max_auto2 = thisauto;
  5769. atinbuf2 = m; /* Save best and 2nd best lengths (tested by correlation) */
  5770. }
  5771. }
  5772. if(pitchseg < 0.0) { /* If an unpitched seg, use closest pitch to set arbitrary 'ideal' seg size */
  5773. unpitched = 1;
  5774. time = (double)(*at)/(double)dz->infile->srate;
  5775. pitchseg = (double)read_validpitch_wsize_in_samps_from_brktable(time,kk,dz);
  5776. }
  5777. newlen = atinbuf - startinbuf;
  5778. if(atinbuf2 >= 0)
  5779. newlen2 = atinbuf2 - startinbuf;
  5780. *at = atinbuf + startbuf; /* Compare best-correlated cuts, with existing cut */
  5781. if(fabs((double)newlen - pitchseg) <= fabs((double)oldlen - pitchseg)) {
  5782. if(atinbuf2 >= 0) { /* If 2nd best correlation point exists & is closer to pitch than best, use it */
  5783. if(fabs((double)newlen2 - pitchseg) < fabs((double)newlen - pitchseg))
  5784. *at = atinbuf2 + startbuf;
  5785. } /* Else, If best new value is closer to pitch than original: use it */
  5786. return FINISHED;
  5787. /* If best cut(s) are worse than existing cut, */
  5788. } else if(extended) { /* if already EXTENDED the search, no point in looking at smaller range */
  5789. *at = orig_atinbuf + startbuf; /* Reset, and break from inner ('while') loop */
  5790. break;
  5791. } else { /* Otherwise, set end of search to the down_zcross PRIOR to the one found */
  5792. maxinbuf = last_down_zcross(maxinbuf,startinbuf,buf);
  5793. if(maxinbuf <= 0) { /* If can't get any smaller, break from inner 'while' loop */
  5794. *at = orig_atinbuf + startbuf;
  5795. break;
  5796. } /* Otherwise continue search in 'while' loop */
  5797. }
  5798. if(unpitched)
  5799. pitchseg = -1.0;
  5800. } /* If inner loop search has failed */
  5801. if(end < realend) { /* if the search end is less than the realend of the 3segments, EXTEND the search */
  5802. maxinbuf = next_down_zcross(orig_maxinbuf,dz->buflen,buf);
  5803. if(maxinbuf < 0)
  5804. break;
  5805. end = maxinbuf + startbuf;
  5806. orig_maxinbuf = maxinbuf;
  5807. extended = 1;
  5808. }
  5809. } while(end < realend);
  5810. return FINISHED;
  5811. }
  5812. /******************************** AUTOCORRELATE ********************************/
  5813. double autocorrelate(int n,int m,float *buf)
  5814. {
  5815. int j, k;
  5816. double sum = 0.0;
  5817. for(j = n,k = m;j<m;j++,k++)
  5818. sum += (buf[j] * buf[k]);
  5819. sum = sum/(double)(m-n);
  5820. return sum;
  5821. }
  5822. /******************************** NEXT_DOWN_ZCROSS ********************************/
  5823. int next_down_zcross(int here,int hibound,float *buf)
  5824. {
  5825. double val = buf[here];
  5826. while(val <= 0.0) {
  5827. here++;
  5828. if(here >= hibound)
  5829. return -1;
  5830. val = buf[here];
  5831. }
  5832. while(val > 0.0) {
  5833. here++;
  5834. if(here >= hibound)
  5835. return -1;
  5836. val = buf[here];
  5837. }
  5838. return here;
  5839. }
  5840. /******************************** LAST_DOWN_ZCROSS ********************************/
  5841. int last_down_zcross(int here,int lobound,float *buf)
  5842. {
  5843. double val = buf[here];
  5844. while(val <= 0.0) {
  5845. here--;
  5846. if(here < lobound)
  5847. return -1;
  5848. val = buf[here];
  5849. }
  5850. while(val >= 0.0) {
  5851. here--;
  5852. if(here < lobound)
  5853. return -1;
  5854. val = buf[here];
  5855. }
  5856. while(val < 0.0) {
  5857. here--;
  5858. if(here < lobound)
  5859. return -1;
  5860. val = buf[here];
  5861. }
  5862. here++;
  5863. return here;
  5864. }
  5865. /************************************/
  5866. /* CLEAN UP THE OUTPUT IF NECESSARY */
  5867. /************************************/
  5868. /******************************** ELIMINATE_TOO_SHORT_EVENTS ********************************/
  5869. int eliminate_too_short_events(int *zcnt,int *final_pos,int *sigcnt,int opos,int last,float *obuf,int chans,dataptr dz)
  5870. {
  5871. int too_long_zeros = 0, badsig_possible = 0, finished;
  5872. int n, k, sigstart=0, sigend=0;
  5873. int zeros = *zcnt, sigs = *sigcnt, bumzeros;
  5874. int tooshortsig = dz->infile->srate/50;
  5875. int toolongsil = dz->infile->srate/25;
  5876. if(zeros <= BUMZEROCNT * chans) {
  5877. bumzeros = zeros; /* bumzeros are zeros within signals */
  5878. zeros = 0;
  5879. } else {
  5880. bumzeros = 0;
  5881. }
  5882. if(*final_pos > 0)
  5883. *final_pos -= dz->buflen;
  5884. for(n=*final_pos; n < opos; n++) {
  5885. if(flteq(obuf[n],0.0)) {
  5886. if(zeros == 0) {
  5887. bumzeros++;
  5888. if(bumzeros <= BUMZEROCNT * chans) {
  5889. continue;
  5890. } else {
  5891. zeros = BUMZEROCNT * chans;
  5892. bumzeros = 0;
  5893. }
  5894. }
  5895. if(sigs) {
  5896. if((sigs < tooshortsig * chans) && too_long_zeros) {
  5897. sigend = n;
  5898. badsig_possible = 1;
  5899. too_long_zeros = 0;
  5900. } else {
  5901. badsig_possible = 0;
  5902. }
  5903. sigs = 0;
  5904. }
  5905. zeros++;
  5906. if(!too_long_zeros) {
  5907. if(zeros >= toolongsil * chans) {
  5908. if(badsig_possible) {
  5909. for(k = sigstart; k < sigend;k++)
  5910. obuf[k] = 0;
  5911. badsig_possible = 0;
  5912. }
  5913. too_long_zeros = 1;
  5914. }
  5915. }
  5916. } else {
  5917. if(bumzeros) {
  5918. sigs += bumzeros;
  5919. bumzeros = 0;
  5920. }
  5921. if(!sigs) {
  5922. bumzeros = 0;
  5923. badsig_possible = 0;
  5924. sigstart = n;
  5925. sigs = 0;
  5926. zeros = 0;
  5927. }
  5928. sigs++;
  5929. }
  5930. }
  5931. if(last) { /* if end of input file : Eliminate any too short item near end */
  5932. if(badsig_possible) {
  5933. for(k = sigstart; k < sigend;k++)
  5934. obuf[k] = 0;
  5935. }
  5936. return FINISHED;
  5937. }
  5938. /* PROCESS START OF SIGNAL IN CONTINUATION-BUFFER, in case badsig_possible overlaps bufend */
  5939. if(badsig_possible) { /* if bad signal possibility, must be in stretch of zeros */
  5940. while(flteq(obuf[n],0.0)) {
  5941. zeros++;
  5942. if(zeros >= toolongsil * chans) { /* if post-zeros too long, zero bad sig, and break */
  5943. for(k = sigstart; k < sigend;k++)
  5944. obuf[k] = 0;
  5945. break;
  5946. }
  5947. n++; /* else, once signal becomes non-zero, breaks from while */
  5948. }
  5949. *zcnt = zeros; /* store final zero count, for next pass */
  5950. *final_pos = n;
  5951. *sigcnt = sigs;
  5952. return FINISHED;
  5953. }
  5954. finished = 0;
  5955. while(!finished) {
  5956. if(flteq(obuf[n],0.0)) {
  5957. if(sigs) {
  5958. if((sigs < tooshortsig * chans) && too_long_zeros) {
  5959. sigend = n;
  5960. badsig_possible = 1;
  5961. too_long_zeros = 0;
  5962. } else {
  5963. finished = 1; /* No bad signal here after bufend, exit */
  5964. }
  5965. sigs = 0;
  5966. }
  5967. zeros++;
  5968. if(!too_long_zeros) {
  5969. if(zeros >= toolongsil * chans) {
  5970. if(badsig_possible) {
  5971. for(k = sigstart; k < sigend;k++)
  5972. obuf[k] = 0; /* Eliminate bad sig, and exit */
  5973. }
  5974. finished = 1; /* Finished checking for badsig at bufend, exit */
  5975. }
  5976. }
  5977. } else {
  5978. if(!sigs) {
  5979. finished = 1; /* start of new sig section: */
  5980. } /* any previous badsigs eliminated in zeros block, so exit */
  5981. sigs++;
  5982. if(sigs >= tooshortsig * chans) /* too much sig to be bad sig: exit */
  5983. finished = 1;
  5984. }
  5985. n++;
  5986. }
  5987. //NEW
  5988. *zcnt = zeros; /* store final zero count, for next pass */
  5989. *final_pos = n;
  5990. *sigcnt = sigs;
  5991. return FINISHED;
  5992. }
  5993. /******************************** SMOOTH_BAD_EVENTS ********************************/
  5994. int smooth_bad_events(int *zcnt,int *final_pos,int *sig_cnt,int *stsmoothed,int opos,int last,float *buf,int chans,dataptr dz)
  5995. {
  5996. int finished, dosmooth, start_smoothed, q;
  5997. int n=0, k, fadelen, checklen, start, end;
  5998. int zeros, sigs, bumzeros, startzeros, startbumzeros, startsigs;
  5999. double incr, gain, diff;
  6000. int new_sig_total = 0, new_z_total = 0;
  6001. zeros = *zcnt;
  6002. if(zeros <= BUMZEROCNT * chans) {
  6003. bumzeros = zeros/chans; /* bumzeros are zeros within signals */
  6004. zeros = 0;
  6005. } else {
  6006. bumzeros = 0;
  6007. zeros /= chans;
  6008. }
  6009. startzeros = zeros;
  6010. startbumzeros = bumzeros;
  6011. startsigs = *sig_cnt/chans;
  6012. if(*final_pos > 0)
  6013. *final_pos -= dz->buflen;
  6014. for(q=0;q<chans;q++) {
  6015. zeros = startzeros;
  6016. bumzeros = startbumzeros;
  6017. sigs = startsigs;
  6018. start_smoothed = stsmoothed[q];
  6019. for(n=q + *final_pos; n < opos; n+=chans) {
  6020. if(flteq(buf[n],0.0)) {
  6021. if(zeros == 0) {
  6022. bumzeros++;
  6023. if(bumzeros <= BUMZEROCNT) {
  6024. continue;
  6025. } else {
  6026. zeros = BUMZEROCNT;
  6027. bumzeros = 0;
  6028. }
  6029. }
  6030. if(sigs) {
  6031. checklen = min(sigs,SMOOTHWIN);
  6032. end = n - (BUMZEROCNT * chans);
  6033. start = end - (checklen * chans);
  6034. dosmooth = 0;
  6035. diff = fabs(buf[start] - buf[start+chans]);
  6036. for(k = start+chans;k < end;k+=chans) {
  6037. /* look for sudden gradient changes relative to size of sample */
  6038. if((diff > 0.001) && (fabs(buf[k]) < (diff * 4.0))) {
  6039. dosmooth = 1;
  6040. break;
  6041. }
  6042. diff = fabs(buf[k+chans] - buf[k]);
  6043. }
  6044. if(dosmooth) {
  6045. fadelen = min(sigs,SMOOTHWIN);
  6046. start = end - (fadelen * chans);
  6047. gain = 1.0;
  6048. incr = 1.0/(double)fadelen;
  6049. for(k = start+chans;k < end;k+=chans) {
  6050. gain -= incr;
  6051. buf[k] = (float)(buf[k] * gain);
  6052. }
  6053. }
  6054. }
  6055. sigs = 0;
  6056. start_smoothed = 0;
  6057. zeros++;
  6058. } else {
  6059. if(bumzeros) {
  6060. sigs += bumzeros;
  6061. bumzeros = 0;
  6062. }
  6063. if(!sigs) {
  6064. bumzeros = 0;
  6065. sigs = 0;
  6066. zeros = 0;
  6067. }
  6068. sigs++;
  6069. if(!start_smoothed && (sigs > SMOOTHWIN)) {
  6070. dosmooth = 0;
  6071. start = n - (SMOOTHWIN * chans);
  6072. diff = fabs(buf[start] - buf[start+chans]);
  6073. for(k = start+chans;k < n;k+=chans) {
  6074. if((diff > 0.001) && (fabs(buf[k-chans]) < (diff * 4.0))) {
  6075. dosmooth = 1;
  6076. break;
  6077. }
  6078. diff = fabs(buf[k+chans] - buf[k]);
  6079. }
  6080. if(dosmooth) {
  6081. fadelen = min(sigs,SMOOTHWIN);
  6082. start = n - (SMOOTHWIN * chans);
  6083. gain = 1.0;
  6084. incr = 1.0/(double)fadelen;
  6085. for(k = n-chans;k >= start;k-=chans) {
  6086. gain -= incr;
  6087. buf[k] = (float)(buf[k] * gain);
  6088. }
  6089. }
  6090. start_smoothed = 1;
  6091. }
  6092. }
  6093. }
  6094. /* PROCESS START OF SIGNAL IN CONTINUATION-BUFFER, in case badsig_possible overlaps bufend */
  6095. if(!last) {
  6096. finished = 0;
  6097. while(!finished) {
  6098. if(flteq(buf[n],0.0)) {
  6099. if(zeros == 0) {
  6100. bumzeros++;
  6101. if(bumzeros <= BUMZEROCNT) {
  6102. continue;
  6103. } else {
  6104. zeros = BUMZEROCNT;
  6105. bumzeros = 0;
  6106. }
  6107. }
  6108. checklen = min(sigs,SMOOTHWIN);
  6109. end = n - (BUMZEROCNT * chans);
  6110. start = end - (checklen * chans);
  6111. dosmooth = 0;
  6112. diff = fabs(buf[start] - buf[start+chans]);
  6113. for(k = start+chans;k < end;k+=chans) {
  6114. /* if the samp-to-samp step > 0.001 (-60dB) */
  6115. /* If the step is more than 4 times the sizeof the sample, complain */
  6116. if((diff > 0.001) && (fabs(buf[k]) < (diff * 4.0))) {
  6117. dosmooth = 1;
  6118. break;
  6119. }
  6120. diff = fabs(buf[k+chans] - buf[k]);
  6121. }
  6122. if(dosmooth) {
  6123. fadelen = min(sigs,SMOOTHWIN);
  6124. start = end - (fadelen * chans);
  6125. gain = 1.0;
  6126. incr = 1.0/(double)fadelen;
  6127. for(k = start+chans;k < end;k+=chans) {
  6128. gain -= incr;
  6129. buf[k] = (float)(buf[k] * gain);
  6130. }
  6131. }
  6132. finished = 1;
  6133. } else {
  6134. if(bumzeros) {
  6135. sigs += bumzeros;
  6136. bumzeros = 0;
  6137. }
  6138. if(!sigs) {
  6139. bumzeros = 0;
  6140. sigs = 0;
  6141. zeros = 0;
  6142. }
  6143. sigs++;
  6144. if(!start_smoothed && (sigs > SMOOTHWIN)) {
  6145. dosmooth = 0;
  6146. start = n - (SMOOTHWIN * chans);
  6147. diff = fabs(buf[start] - buf[start+chans]);
  6148. for(k = start+chans;k < n;k+=chans) {
  6149. if((diff > 0.001) && (fabs(buf[k-chans]) < (diff * 4.0))) {
  6150. dosmooth = 1;
  6151. break;
  6152. }
  6153. diff = fabs(buf[k+chans] - buf[k]);
  6154. }
  6155. if(dosmooth) {
  6156. start = n - (SMOOTHWIN * chans);
  6157. gain = 1.0;
  6158. incr = 1.0/(double)SMOOTHWIN;
  6159. for(k = n-chans;k >= start;k-=chans) {
  6160. gain -= incr;
  6161. buf[k] = (float)(buf[k] * gain);
  6162. }
  6163. }
  6164. start_smoothed = 1;
  6165. }
  6166. if(n > (dz->buflen + SMOOTHWIN) * chans) /* The signal tail all falls in next buffer */
  6167. finished = 1;
  6168. }
  6169. n+=chans;
  6170. }
  6171. }
  6172. new_sig_total += sigs;
  6173. new_z_total += zeros;
  6174. stsmoothed[q] = start_smoothed;
  6175. }
  6176. *sig_cnt = new_sig_total;
  6177. *zcnt = new_z_total;
  6178. *final_pos = n;
  6179. return FINISHED;
  6180. }
  6181. /********************************/
  6182. /* PSOW_CHOP */
  6183. /********************************/
  6184. /******************************** KEEP_FILENAME_TAIL_ONLY ********************************/
  6185. void keep_filename_tail_only(dataptr dz)
  6186. {
  6187. int namelen = strlen(dz->wordstor[0]);
  6188. char *q = dz->wordstor[0];
  6189. char *r = dz->wordstor[0] + namelen;
  6190. char *p = r - 1;
  6191. while((*p != '\\') && (*p != '/') && (*p != ':')) {
  6192. p-- ;
  6193. if(p < dz->wordstor[0])
  6194. break;
  6195. }
  6196. if(p > dz->wordstor[0]) {
  6197. p++;
  6198. while(p <= r)
  6199. *q++ = *p++;
  6200. }
  6201. }
  6202. /******************************** CREATE_NEXT_FILENAME ********************************/
  6203. void create_next_filename(char *outfilename,int n)
  6204. {
  6205. n++;
  6206. if(!sloom) {
  6207. if(n<10)
  6208. insert_new_chars_at_filename_end(outfilename,"_00");
  6209. else if(n<100)
  6210. insert_new_chars_at_filename_end(outfilename,"_0");
  6211. else
  6212. insert_new_chars_at_filename_end(outfilename,"_");
  6213. insert_new_number_at_filename_end(outfilename,n,0);
  6214. } else
  6215. insert_new_number_at_filename_end(outfilename,n,1);
  6216. }
  6217. /********************************/
  6218. /* PSOA_INTERP */
  6219. /********************************/
  6220. /******************************** PSOA_INTERP ********************************/
  6221. int psoa_interp(dataptr dz)
  6222. {
  6223. int exit_status;
  6224. int start0, end0, start1, end1, n, m, seglen0, seglen1, repets, sampcnt;
  6225. float *ibuf0 = dz->sampbuf[0];
  6226. float *ibuf1 = dz->sampbuf[1];
  6227. float *obuf0 = dz->sampbuf[2];
  6228. float *obuf1 = dz->sampbuf[3];
  6229. float *ovflw = dz->sampbuf[4];
  6230. int opos = 0;
  6231. double gain, time;
  6232. initrand48();
  6233. memset((char *)obuf0,0,dz->buflen * sizeof(float));
  6234. memset((char *)obuf1,0,dz->buflen * sizeof(float));
  6235. memset((char *)ovflw,0,dz->buflen * sizeof(float));
  6236. /* GET 1ST GRAIN */
  6237. if((exit_status = read_samps(ibuf0,dz))<0)
  6238. return exit_status;
  6239. start0 = 0;
  6240. end0 = dz->insams[0];
  6241. if(!is_valid_grain(ibuf0,dz->insams[0])) {
  6242. sprintf(errstr,"File 1 is not a valid pitch-sync grain file.\n");
  6243. return(DATA_ERROR);
  6244. }
  6245. seglen0 = end0 - start0;
  6246. /* IN CASES EXPANDING A FIRST GRAIN, Do the expansion */
  6247. sampcnt = (int)round(dz->param[PS_SDUR] * (double)dz->infile->srate);
  6248. if(sampcnt == 0)
  6249. repets = 1;
  6250. else
  6251. repets = (int)round((double)sampcnt/(double)seglen0);
  6252. for(n=0;n<repets;n++) {
  6253. time = (double)(dz->total_samps_written + opos)/(double)dz->infile->srate;
  6254. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  6255. return(exit_status);
  6256. if(dz->param[PS_TREMDEPTH] < 0.01)
  6257. dz->param[PS_TREMDEPTH] = 0.0;
  6258. if(dz->param[PS_TREMDEPTH] > 0.0)
  6259. gain = tremolo(end0 - start0,dz->param[PS_TREMFRQ],dz->param[PS_TREMDEPTH],dz);
  6260. else
  6261. gain = 1.0;
  6262. for(m=start0;m < end0;m++) {
  6263. obuf0[opos++] = (float)(ibuf0[m] * gain);
  6264. if(opos >= dz->buflen * 2) {
  6265. if((exit_status = write_samps(obuf0,dz->buflen,dz))<0)
  6266. return(exit_status);
  6267. memcpy((char *)obuf0,(char *)obuf1,dz->buflen * sizeof(float));
  6268. memset((char *)obuf1,0,dz->buflen * sizeof(float));
  6269. opos = dz->buflen;
  6270. }
  6271. }
  6272. if(dz->param[PS_VIBDEPTH] > 0.0)
  6273. opos += vibrato(seglen0,dz->param[PS_VIBFRQ],dz->param[PS_VIBDEPTH],dz);
  6274. }
  6275. /* FIND 2ND GRAIN */
  6276. if((dz->ssampsread = fgetfbufEx(ibuf1, dz->buflen,dz->ifd[1],0)) < 0) {
  6277. sprintf(errstr,"Can't read samples from input soundfile 2\n");
  6278. return(SYSTEM_ERROR);
  6279. }
  6280. start1 = 0;
  6281. end1 = dz->insams[1];
  6282. if(!is_valid_grain(ibuf1,dz->insams[1])) {
  6283. sprintf(errstr,"File 2 is not a valid pitch-sync grain file.\n");
  6284. return(DATA_ERROR);
  6285. }
  6286. seglen1 = end1 - start1;
  6287. if((exit_status = do_pitchsync_grain_interp(start0,end0,start1,end1,&opos,dz))<0)
  6288. return(exit_status);
  6289. /* IN CASES EXPANDING A FINAL GRAIN, Expand it */
  6290. sampcnt = (int)round(dz->param[PS_EDUR] * (double)dz->infile->srate);
  6291. if(sampcnt == 0)
  6292. repets = 1;
  6293. else
  6294. repets = (int)round((double)sampcnt/(double)seglen1);
  6295. for(n=0;n<repets;n++) {
  6296. time = (double)(dz->total_samps_written + opos)/(double)dz->infile->srate;
  6297. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  6298. return(exit_status);
  6299. if(dz->param[PS_TREMDEPTH] < 0.01)
  6300. dz->param[PS_TREMDEPTH] = 0.0;
  6301. if(dz->param[PS_TREMDEPTH] > 0.0)
  6302. gain = tremolo(end0 - start0,dz->param[PS_TREMFRQ],dz->param[PS_TREMDEPTH],dz);
  6303. else
  6304. gain = 1.0;
  6305. for(m=start1;m < end1;m++) {
  6306. obuf0[opos] = (float)(obuf0[opos] + (ibuf1[m] * gain));
  6307. opos++;
  6308. if(opos >= dz->buflen * 2) {
  6309. if((exit_status = write_samps(obuf0,dz->buflen,dz))<0)
  6310. return(exit_status);
  6311. memcpy((char *)obuf0,(char *)obuf1,dz->buflen * sizeof(float));
  6312. memset((char *)obuf1,0,dz->buflen * sizeof(float));
  6313. memcpy((char *)obuf1,(char *)ovflw,dz->buflen * sizeof(float));
  6314. memset((char *)ovflw,0,dz->buflen * sizeof(float));
  6315. opos = dz->buflen;
  6316. }
  6317. }
  6318. if(dz->param[PS_VIBDEPTH] > 0.0) {
  6319. if(n != repets-1)
  6320. opos += vibrato(seglen1,dz->param[PS_VIBFRQ],dz->param[PS_VIBDEPTH],dz);
  6321. }
  6322. }
  6323. /* WRITE REMAINDER OF OUTPUT FILE */
  6324. if(opos > 0) {
  6325. if((exit_status = write_samps(obuf0,opos,dz))<0)
  6326. return(exit_status);
  6327. }
  6328. return FINISHED;
  6329. }
  6330. /******************************** IS_VALID_GRAIN ********************************/
  6331. int is_valid_grain(float *buf,int filelen)
  6332. {
  6333. int n = 0, finished = 0;
  6334. if(buf[0] > 0)
  6335. return 0;
  6336. while(!finished) {
  6337. while(buf[n] <= 0) {
  6338. if(n >= filelen)
  6339. return 0;
  6340. n++;
  6341. }
  6342. while(buf[n] >= 0) {
  6343. if(n >= filelen) {
  6344. finished = 1;
  6345. break;
  6346. }
  6347. n++;
  6348. }
  6349. }
  6350. if(n != filelen) {
  6351. return 0;
  6352. }
  6353. return 1;
  6354. }
  6355. /******************************** DO_PITCHSYNC_GRAIN_INTERP ********************************/
  6356. int do_pitchsync_grain_interp(int start0,int end0,int start1,int end1,int *opos,dataptr dz)
  6357. {
  6358. int exit_status;
  6359. float *ibuf0 = dz->sampbuf[0];
  6360. float *ibuf1 = dz->sampbuf[1];
  6361. float *obuf0 = dz->sampbuf[2];
  6362. float *obuf1 = dz->sampbuf[3];
  6363. float *ovflw = dz->sampbuf[4];
  6364. double ratio, dlen, outgrain_position_incr, diff, valdiff, step0, step1, dhere0, dhere1, val0, val1, val;
  6365. float **array0, **array1;
  6366. int cnt0 = 0, cnt1 = 0, maxsize0 = 0, maxsize1 = 0, maxcnt, n, m, j, baselen0, baselen1;
  6367. int seglen0, seglen1, thisstart0, thisstart1, thisend0, thisend1, repcnt, wavcnt, change, thislen;
  6368. int here0, here1, startopos;
  6369. int *len0, *len1;
  6370. int wavchange, thiswavlen;
  6371. double wavset_len_incr_step, wavset_len_incr, wavset_interp_ratio, wavset_interp_ratio_step, gain, time;
  6372. seglen0 = end0 - start0;
  6373. seglen1 = end1 - start1;
  6374. /* COUNT NO OF WAVESETS IN EACH GRAIN, AND FIND MAXSIZE */
  6375. if((exit_status = count_wavesets(&cnt0,&maxsize0,start0,end0,ibuf0))<0)
  6376. return(exit_status);
  6377. if((exit_status = count_wavesets(&cnt1,&maxsize1,start1,end1,ibuf1))<0)
  6378. return(exit_status);
  6379. //maxsize = max(maxsize0,maxsize1);
  6380. maxcnt = max(cnt0,cnt1);
  6381. /* ESTABLISH ARRAYS TO STORE EACH WAVESET, and each waveset's length */
  6382. if((array0 = (float **)malloc(maxcnt * sizeof(float *)))==NULL) {
  6383. sprintf(errstr,"INSUFFICIENT MEMORY for 1st interpolation buffer array.\n");
  6384. return(MEMORY_ERROR);
  6385. }
  6386. if((array1 = (float **)malloc(maxcnt * sizeof(float *)))==NULL) {
  6387. sprintf(errstr,"INSUFFICIENT MEMORY for 2nd interpolation buffer array.\n");
  6388. return(MEMORY_ERROR);
  6389. }
  6390. if((len0 = (int *)malloc(maxcnt * sizeof(int)))==NULL) {
  6391. sprintf(errstr,"INSUFFICIENT MEMORY for 1st interpolation buffer array.\n");
  6392. return(MEMORY_ERROR);
  6393. }
  6394. if((len1 = (int *)malloc(maxcnt * sizeof(int)))==NULL) {
  6395. sprintf(errstr,"INSUFFICIENT MEMORY for 1st interpolation buffer array.\n");
  6396. return(MEMORY_ERROR);
  6397. }
  6398. baselen0 = 0;
  6399. baselen1 = 0;
  6400. /* GET SIZES OF WAVESET STORES */
  6401. if(cnt0 > cnt1) { /* If more wavesets in 1st, store zeroed arrays for corresponding wavesets of 2nd, */
  6402. thisstart0 = start0; /* calculating length by using ratio of sizes of wavesets in the 1st. */
  6403. thisstart1 = start1;
  6404. for(n=0;n<cnt0;n++) {
  6405. if((exit_status = get_waveset_end(thisstart0,end0,&thisend0,ibuf0))<0)
  6406. return(exit_status);
  6407. len0[n] = thisend0 - thisstart0;
  6408. thisstart0 = thisend0;
  6409. if(n >= cnt1) {
  6410. if(baselen0==0) {
  6411. baselen0 = len0[n-1];
  6412. baselen1 = len1[n-1];
  6413. }
  6414. ratio = (double)len0[n]/(double)baselen0;
  6415. len1[n] = (int)round((double)baselen1 * ratio);
  6416. } else {
  6417. if((exit_status = get_waveset_end(thisstart1,end1,&thisend1,ibuf1))<0)
  6418. return(exit_status);
  6419. len1[n] = thisend1 - thisstart1;
  6420. thisstart1 = thisend1;
  6421. }
  6422. }
  6423. } else if(cnt1 > cnt0) { /* If more wavesets in 2nd, store zeroed arrays for corresponding wavesets of 1st, */
  6424. thisstart0 = start0; /* calculating length by using ratio of sizes of wavesets in the 2nd. */
  6425. thisstart1 = start1;
  6426. for(n=0;n<cnt1;n++) {
  6427. if((exit_status = get_waveset_end(thisstart1,end1,&thisend1,ibuf1))<0)
  6428. return(exit_status);
  6429. len1[n] = thisend1 - thisstart1;
  6430. thisstart1 = thisend1;
  6431. if(n >= cnt0) {
  6432. if(baselen0==0) {
  6433. baselen0 = len0[n-1];
  6434. baselen1 = len1[n-1];
  6435. }
  6436. ratio = (double)len1[n]/(double)baselen1;
  6437. len0[n] = (int)round((double)baselen0 * ratio);
  6438. } else {
  6439. if((exit_status = get_waveset_end(thisstart0,end0,&thisend0,ibuf0))<0)
  6440. return(exit_status);
  6441. len0[n] = thisend0 - thisstart0;
  6442. thisstart0 = thisend0;
  6443. }
  6444. }
  6445. } else { /* Simple case, equal no of wavesets : store them */
  6446. thisstart0 = start0; /* calculating length by using ratio of sizes of wavesets in the 2nd. */
  6447. thisstart1 = start1;
  6448. for(n=0;n<cnt1;n++) {
  6449. if((exit_status = get_waveset_end(thisstart0,end0,&thisend0,ibuf0))<0)
  6450. return(exit_status);
  6451. len0[n] = thisend0 - thisstart0;
  6452. thisstart0 = thisend0;
  6453. if((exit_status = get_waveset_end(thisstart1,end1,&thisend1,ibuf1))<0)
  6454. return(exit_status);
  6455. len1[n] = thisend1 - thisstart1;
  6456. thisstart1 = thisend1;
  6457. }
  6458. }
  6459. for(n=0;n<maxcnt;n++) {
  6460. if((array0[n] = (float *)malloc((len0[n] + 1) * sizeof(float)))==NULL) {
  6461. sprintf(errstr,"INSUFFICIENT MEMORY for grain1 interpolation buffer %d.\n",n+1);
  6462. return(MEMORY_ERROR);
  6463. }
  6464. if((array1[n] = (float *)malloc((len1[n] + 1) * sizeof(float)))==NULL) {
  6465. sprintf(errstr,"INSUFFICIENT MEMORY for grain2 interpolation buffer %d.\n",n+1);
  6466. return(MEMORY_ERROR);
  6467. }
  6468. }
  6469. /* STORE THE INITIAL WAVESETS */
  6470. if(cnt0 > cnt1) { /* If more wavesets in 1st, store zeroed arrays for corresponding wavesets of 2nd, */
  6471. thisstart0 = start0; /* calculating length by using ratio of sizes of wavesets in the 1st. */
  6472. thisstart1 = start1;
  6473. for(n=0;n<cnt0;n++) {
  6474. if((exit_status = get_waveset_end(thisstart0,end0,&thisend0,ibuf0))<0)
  6475. return(exit_status);
  6476. memcpy((char *)array0[n],(char *)(ibuf0 + thisstart0),len0[n] * sizeof(float));
  6477. thisstart0 = thisend0;
  6478. if(n >= cnt1) {
  6479. memset((char *)array1[n],0,len1[n] * sizeof(float));
  6480. } else {
  6481. if((exit_status = get_waveset_end(thisstart1,end1,&thisend1,ibuf1))<0)
  6482. return(exit_status);
  6483. memcpy((char *)array1[n],(char *)(ibuf1 + thisstart1),len1[n] * sizeof(float));
  6484. thisstart1 = thisend1;
  6485. }
  6486. array0[n][len0[n]] = 0.0f; /* wraparound points */
  6487. array1[n][len1[n]] = 0.0f; /* wraparound points */
  6488. }
  6489. } else if(cnt1 > cnt0) { /* If more wavesets in 2nd, store zeroed arrays for corresponding wavesets of 1st, */
  6490. thisstart0 = start0; /* calculating length by using ratio of sizes of wavesets in the 2nd. */
  6491. thisstart1 = start1;
  6492. for(n=0;n<cnt1;n++) {
  6493. if((exit_status = get_waveset_end(thisstart1,end1,&thisend1,ibuf1))<0)
  6494. return(exit_status);
  6495. memcpy((char *)array1[n],(char *)(ibuf1 + thisstart1),len1[n] * sizeof(float));
  6496. thisstart1 = thisend1;
  6497. if(n >= cnt0) {
  6498. memset((char *)array0[n],0,len0[n] * sizeof(float));
  6499. } else {
  6500. if((exit_status = get_waveset_end(thisstart0,end0,&thisend0,ibuf0))<0)
  6501. return(exit_status);
  6502. memcpy((char *)array0[n],(char *)(ibuf0 + thisstart0),len0[n] * sizeof(float));
  6503. thisstart0 = thisend0;
  6504. }
  6505. array0[n][len0[n]] = 0.0f; /* wraparound points */
  6506. array1[n][len1[n]] = 0.0f; /* wraparound points */
  6507. }
  6508. } else { /* Simple case, equal no of wavesets : store them */
  6509. thisstart0 = start0; /* calculating length by using ratio of sizes of wavesets in the 2nd. */
  6510. thisstart1 = start1;
  6511. for(n=0;n<cnt1;n++) {
  6512. if((exit_status = get_waveset_end(thisstart0,end0,&thisend0,ibuf0))<0)
  6513. return(exit_status);
  6514. memcpy((char *)array0[n],(char *)(ibuf0 + thisstart0),len0[n] * sizeof(float));
  6515. thisstart0 = thisend0;
  6516. if((exit_status = get_waveset_end(thisstart1,end1,&thisend1,ibuf1))<0)
  6517. return(exit_status);
  6518. memcpy((char *)array1[n],(char *)(ibuf1 + thisstart1),len1[n] * sizeof(float));
  6519. thisstart1 = thisend1;
  6520. array0[n][len0[n]] = 0.0f; /* wraparound points */
  6521. array1[n][len1[n]] = 0.0f; /* wraparound points */
  6522. }
  6523. }
  6524. repcnt = find_repcnt(seglen0,seglen1,0,dz);
  6525. wavcnt = max(cnt0,cnt1);
  6526. dlen = seglen0;
  6527. change = (seglen1 - seglen0);
  6528. outgrain_position_incr = (double)change/(double)repcnt;
  6529. wavset_interp_ratio_step = 1.0/(double)repcnt;
  6530. wavset_interp_ratio = wavset_interp_ratio_step;
  6531. startopos = *opos;
  6532. for(n=0;n<repcnt;n++) {
  6533. *opos = startopos;
  6534. thislen = (int)round(dlen);
  6535. time = (double)(dz->total_samps_written + *opos)/(double)dz->infile->srate;
  6536. if((exit_status = read_values_from_all_existing_brktables(time,dz))<0)
  6537. return(exit_status);
  6538. if(dz->param[PS_TREMDEPTH] < 0.01)
  6539. dz->param[PS_TREMDEPTH] = 0.0;
  6540. if(dz->param[PS_TREMDEPTH] > 0.0)
  6541. gain = tremolo(end0 - start0,dz->param[PS_TREMFRQ],dz->param[PS_TREMDEPTH],dz);
  6542. else
  6543. gain = 1.0;
  6544. for(m=0;m<wavcnt;m++) {
  6545. here0 = 0;
  6546. here1 = 0;
  6547. dhere0 = (double)here0;
  6548. dhere1 = (double)here1;
  6549. wavchange = len1[m] - len0[m];
  6550. wavset_len_incr_step = (double)wavchange/(double)repcnt;
  6551. wavset_len_incr = wavset_len_incr_step * (double)n;
  6552. thiswavlen = (int)round((double)len0[m] + wavset_len_incr);
  6553. step0 = (double)len0[m]/(double)thiswavlen;
  6554. step1 = (double)len1[m]/(double)thiswavlen;
  6555. val0 = array0[m][here0] * (1.0 - wavset_interp_ratio);
  6556. val1 = array1[m][here1] * wavset_interp_ratio;
  6557. val = val0 + val1;
  6558. val *= gain;
  6559. obuf0[*opos] = (float)(obuf0[*opos] + val);
  6560. (*opos)++;
  6561. for(j=1;j<thiswavlen;j++) {
  6562. dhere0 += step0; /* get value from 1st waveset, by interp Nn stored waveset */
  6563. here0 = (int)floor(dhere0); /* as outlen may be different to stored length */
  6564. diff = dhere0 - (double)here0;
  6565. valdiff= array0[m][here0 + 1] - array0[m][here0];
  6566. val0 = (double)array0[m][here0];
  6567. val0 += (diff * valdiff);
  6568. val0 *= (1.0 - wavset_interp_ratio); /* Get proportion of the value approp to TIME in interp */
  6569. dhere1 += step1; /* Simil with 2nd waveset */
  6570. here1 = (int)floor(dhere1);
  6571. diff = dhere1 - (double)here1;
  6572. valdiff= array1[m][here1 + 1] - array1[m][here1];
  6573. val1 = (double)array1[m][here1];
  6574. val1 += (diff * valdiff);
  6575. val1 *= wavset_interp_ratio;
  6576. val = val0 + val1;
  6577. val *= gain;
  6578. obuf0[*opos] = (float)(obuf0[*opos] + val);
  6579. (*opos)++;
  6580. }
  6581. }
  6582. dlen += outgrain_position_incr;
  6583. wavset_interp_ratio += wavset_interp_ratio_step;
  6584. startopos += thislen;
  6585. if(dz->param[PS_VIBDEPTH] > 0.0)
  6586. startopos += vibrato(thislen,dz->param[PS_VIBFRQ],dz->param[PS_VIBDEPTH],dz);
  6587. if(startopos >= dz->buflen * 2) {
  6588. if((exit_status = write_samps(obuf0,dz->buflen,dz))<0)
  6589. return(exit_status);
  6590. memcpy((char *)obuf0,(char *)obuf1,dz->buflen * sizeof(float));
  6591. memset((char *)obuf1,0,dz->buflen * sizeof(float));
  6592. memcpy((char *)obuf1,(char *)ovflw,dz->buflen * sizeof(float));
  6593. memset((char *)ovflw,0,dz->buflen * sizeof(float));
  6594. startopos -= dz->buflen;
  6595. }
  6596. }
  6597. *opos = startopos;
  6598. return(FINISHED);
  6599. }
  6600. /******************************** COUNT_WAVESETS ********************************/
  6601. int count_wavesets(int *cnt,int *maxsize,int start,int end,float *buf)
  6602. {
  6603. int thisstart, n, size;
  6604. thisstart = start;
  6605. for(n=start;n<end;n++) {
  6606. while(buf[n] < 0) {
  6607. n++;
  6608. if(n >= end) {
  6609. sprintf(errstr,"Anomalous segment end.\n");
  6610. return(DATA_ERROR);
  6611. }
  6612. }
  6613. while(buf[n] >= 0) {
  6614. n++;
  6615. if(n >= end)
  6616. break;
  6617. }
  6618. (*cnt)++;
  6619. size = n - thisstart;
  6620. *maxsize = max(*maxsize,size);
  6621. thisstart = n;
  6622. }
  6623. return(FINISHED);
  6624. }
  6625. /******************************** GET_WAVESET_END ********************************/
  6626. int get_waveset_end(int start,int end,int *thisend,float *buf)
  6627. {
  6628. int n;
  6629. for(n=start;n<end;n++) {
  6630. while(buf[n] < 0) {
  6631. n++;
  6632. if(n >= end) {
  6633. sprintf(errstr,"Anomalous segment end.\n");
  6634. return(DATA_ERROR);
  6635. }
  6636. }
  6637. while(buf[n] >= 0) {
  6638. n++;
  6639. if(n >= end) {
  6640. break;
  6641. }
  6642. }
  6643. *thisend = n;
  6644. break;
  6645. }
  6646. return(FINISHED);
  6647. }
  6648. /******************************** FIND_REPCNT ********************************/
  6649. int find_repcnt(int startlen,int endlen,int samplen,dataptr dz)
  6650. {
  6651. int maxlen, minlen, repcnt, diff, sum, n, thisrep=0;
  6652. double incr, dlen;
  6653. int thisgap, thatgap;
  6654. if(samplen == 0)
  6655. samplen = (int)round(dz->param[1] * (double)dz->infile->srate);
  6656. thatgap = samplen * 2;
  6657. maxlen = max(startlen,endlen); /* get the largest segment */
  6658. minlen = min(startlen,endlen); /* get the largest segment */
  6659. repcnt = (int)round((double)samplen/(double)maxlen); /* divide whole length by this to get min no of repetitions */
  6660. diff = maxlen - minlen;
  6661. for(;;) {
  6662. dlen = (double)minlen; /* the unrounded no of samps counted */
  6663. incr = (double)diff/repcnt; /* the size increment from grain to grain */
  6664. sum = 0;
  6665. for(n=0;n<repcnt;n++) { /* add grains, incrementing their lengths as we go */
  6666. sum += (int)round(dlen); /* and find total length */
  6667. incr = (double)diff/repcnt;
  6668. dlen += incr;
  6669. }
  6670. if(sum == samplen) { /* If we hit target length break */
  6671. thisrep = repcnt;
  6672. break;
  6673. } else if(sum > samplen) { /* if TOO BIG */
  6674. thisgap = sum - samplen; /* find difference from target */
  6675. if(thisgap >= thatgap) { /* if bigger than or equal to last diff, we've found best fit */
  6676. break;
  6677. }
  6678. thatgap = thisgap; /* if smaller than last diff, remember diff */
  6679. thisrep = repcnt; /* and remember this no of repeptitions */
  6680. repcnt--; /* decrement the repetition count */
  6681. } else { /* sum < samplen */ /* if TOO SMALL */
  6682. thisgap = samplen - sum; /* find difference from target */
  6683. if(thisgap >= thatgap) { /* if bigger than or equal to last diff, we've found best fit */
  6684. break;
  6685. }
  6686. thatgap = thisgap; /* if smaller than last diff, remember diff */
  6687. thisrep = repcnt; /* and remember this no of repeptitions */
  6688. repcnt++; /* increment the repetition count */
  6689. }
  6690. }
  6691. return thisrep;
  6692. }
  6693. /******************************** VIBRATO ********************************/
  6694. int vibrato(int seglen,double vibfrq,double vibdep,dataptr dz)
  6695. {
  6696. double shift, advance;
  6697. static int val = 0;
  6698. static double pos = 0;
  6699. int zz, len;
  6700. len = (int)round((double)dz->infile->srate/vibfrq); // Effective sample length of vibrato cycle
  6701. if(val == 0)
  6702. advance = 0.0;
  6703. else
  6704. advance = (double)seglen/(double)len; // Fractional stop through vibrato table
  6705. advance *= TWOPI;
  6706. pos += advance;
  6707. pos = fmod(pos,TWOPI); // Position in vibrato cycle
  6708. shift = pow(2.0,vibdep/SEMITONES_PER_OCTAVE) - 1.0; // Frequency shift corresponfing to position in vibrato cycle
  6709. zz = (int)round(sin(pos) * shift * seglen); // Shift in position of FOF to correspond to vibrato
  6710. val += seglen;
  6711. return zz;
  6712. }
  6713. /******************************** TREMOLO ********************************/
  6714. double tremolo(int seglen,double tremfrq,double tremdepth,dataptr dz)
  6715. {
  6716. double pos, gain;
  6717. static int val = 0;
  6718. int len;
  6719. len = (int)round((double)dz->infile->srate/(double)tremfrq);
  6720. pos = (double)val/(double)len;
  6721. pos *= TWOPI;
  6722. pos = fmod(pos,TWOPI);
  6723. gain = (cos(pos) - 1.0) * 0.5; /* range 0 to -1 */
  6724. gain *= tremdepth; /* range 0 to -tremdepth */
  6725. gain = 1.0 - gain; /* range 1 to 1-tremdepth */
  6726. val += seglen;
  6727. if(tremdepth > 1.0)
  6728. gain /= tremdepth;
  6729. return gain;
  6730. }
  6731. /*****************/
  6732. /* PSOW_FEATURES */
  6733. /*****************/
  6734. /******************************** ZEROFOF ********************************/
  6735. int zerofof(int start,int end,float *buf)
  6736. {
  6737. int n;
  6738. float val;
  6739. for(n = start; n < end; n++) {
  6740. val = buf[n];
  6741. if(val > 0.0)
  6742. return 0;
  6743. if(val < 0.0)
  6744. return 0;
  6745. }
  6746. return 1;
  6747. }
  6748. /******************************** FOF_STRETCH ********************************/
  6749. int fof_stretch(int n,double time,int here_in_buf,int there_in_buf,float *ibuf,float *obuf,
  6750. int *opos,double *maxlevel,double gain,dataptr dz)
  6751. {
  6752. int maxloc, maxwavstart, maxwavend, repets, k;
  6753. double maxamp, val;
  6754. if((dz->iparam[PSF_FOFSTR] > 256) && (n % 100 == 0)) {
  6755. fprintf(stdout,"INFO: Input time %lf\n",time);
  6756. fflush(stdout);
  6757. }
  6758. maxamp = fabs(ibuf[here_in_buf]);
  6759. maxloc = here_in_buf;
  6760. for(k = here_in_buf; k < there_in_buf;k++) {
  6761. if(fabs(ibuf[k]) > maxamp) {
  6762. maxamp = fabs(ibuf[k]);
  6763. maxloc = k;
  6764. }
  6765. }
  6766. k = here_in_buf;
  6767. maxwavstart = k;
  6768. while((maxwavend = next_down_zcross(k,there_in_buf,ibuf))<there_in_buf) {
  6769. if(maxwavend < 0)
  6770. maxwavend = there_in_buf;
  6771. if(maxwavend > maxloc)
  6772. break;
  6773. if(dz->vflag[0] == 1)
  6774. maxwavstart = k;
  6775. k = maxwavend;
  6776. }
  6777. for(k = here_in_buf;k < maxwavstart;k++) {
  6778. val = ibuf[k] * gain;
  6779. obuf[*opos] = (float)(obuf[*opos] + val);
  6780. *maxlevel = max(obuf[*opos],*maxlevel);
  6781. (*opos)++;
  6782. }
  6783. for(repets = 0; repets < dz->iparam[PSF_FOFSTR]; repets++) {
  6784. for(k = maxwavstart;k < maxwavend;k++) {
  6785. val = ibuf[k] * gain;
  6786. obuf[*opos] = (float)(obuf[*opos] + val);
  6787. *maxlevel = max(obuf[*opos],*maxlevel);
  6788. (*opos)++;
  6789. if(*opos >= dz->buflen * 2) {
  6790. sprintf(errstr,"OVERFLOWED OUTPUT BUFFER.\n");
  6791. return(MEMORY_ERROR);
  6792. }
  6793. }
  6794. }
  6795. for(k = maxwavend;k < there_in_buf;k++) {
  6796. val = ibuf[k] * gain;
  6797. obuf[*opos] = (float)(obuf[*opos] + val);
  6798. *maxlevel = max(obuf[*opos],*maxlevel);
  6799. (*opos)++;
  6800. if(*opos >= dz->buflen * 2) {
  6801. sprintf(errstr,"OVERFLOWED OUTPUT BUFFER.\n");
  6802. return(MEMORY_ERROR);
  6803. }
  6804. }
  6805. return(FINISHED);
  6806. }
  6807. /*******************************************/
  6808. /* SUPERIMPOSE FOFS ON SYNTHTONES OR INPUT */
  6809. /*******************************************/
  6810. /******************************** GETFOFENV ********************************/
  6811. void getfofenv(float *ibuf,double *fofenv,int *fofloc,int maxseglen,int here_in_buf,int there_in_buf,int *envcnt)
  6812. {
  6813. double maxamp;
  6814. int maxloc, n;
  6815. //seglen = there_in_buf - here_in_buf;
  6816. memset((char *)fofenv,0,maxseglen * sizeof(double));
  6817. memset((char *)fofloc,0,maxseglen * sizeof(int));
  6818. maxloc = here_in_buf;
  6819. maxamp = 0.0;
  6820. *envcnt = 0;
  6821. for(n = here_in_buf; n < there_in_buf;n++) {
  6822. maxamp = 0.0;
  6823. while(ibuf[n] <= 0.0) { /* find absmax in each half-cycle */
  6824. if(fabs(ibuf[n]) > maxamp) {
  6825. maxamp = fabs(ibuf[n]);
  6826. maxloc = n;
  6827. }
  6828. n++;
  6829. }
  6830. fofenv[*envcnt] = maxamp; /* and store as points on envelope curve */
  6831. fofloc[*envcnt] = maxloc - here_in_buf;
  6832. (*envcnt)++;
  6833. maxamp = 0.0;
  6834. maxloc = n;
  6835. while(ibuf[n] >= 0.0) {
  6836. if(ibuf[n] > maxamp) {
  6837. maxamp = ibuf[n];
  6838. maxloc = n;
  6839. }
  6840. n++;
  6841. }
  6842. fofenv[*envcnt] = maxamp;
  6843. fofloc[*envcnt] = maxloc - here_in_buf;
  6844. (*envcnt)++;
  6845. }
  6846. /* Add edge zeros */
  6847. for(n=(*envcnt)-1;n >=0;n--) {
  6848. fofenv[n+1] = fofenv[n];
  6849. fofloc[n+1] = fofloc[n];
  6850. }
  6851. fofenv[0] = 0.0;
  6852. fofloc[0] = 0;
  6853. (*envcnt)++;
  6854. if(!flteq(fofenv[(*envcnt)-1],0.0)) {
  6855. fofenv[*envcnt] = 0.0;
  6856. fofloc[*envcnt] = there_in_buf - here_in_buf;
  6857. (*envcnt)++;
  6858. }
  6859. }
  6860. /******************************** LINEAR_SPLINT ********************************/
  6861. double linear_splint(double *fofenv,int *fofloc,int envcnt,double time,int *lo)
  6862. {
  6863. int hi;
  6864. double val, timediff, valdiff, timestep;
  6865. if((double)fofloc[*lo] <= time) {
  6866. while((double)fofloc[*lo] <= time)
  6867. (*lo)++;
  6868. (*lo)--;
  6869. }
  6870. hi = (*lo) + 1;
  6871. timediff = time - (double)fofloc[*lo];
  6872. timestep = (double)(fofloc[hi] - fofloc[*lo]);
  6873. valdiff = fofenv[hi] - fofenv[*lo];
  6874. val = fofenv[*lo];
  6875. val += (valdiff * timediff)/timestep;
  6876. return max(val,0.0);
  6877. }
  6878. /******************************** SUPERIMPOSE_FOFS_ON_SYNTHTONES ********************************/
  6879. int superimpose_fofs_on_synthtones(double *fofenv,int *fofloc,double *sintab,int here_in_buf,int there_in_buf,
  6880. float *ibuf,float *obuf,int *opos,int samptime,int envcnt,dataptr dz)
  6881. {
  6882. int exit_status;
  6883. int seglen, n, m, tabpos, lo;
  6884. double maxamp, timediff, valdiff, normaliser, fofval;
  6885. double wavtabincr, dtabpos;
  6886. double vallo, valhi, val;
  6887. float *sbuf = dz->sampbuf[4];
  6888. double *frq = dz->parray[PS_OSCFRQ], *amp = dz->parray[PS_OSCAMP];
  6889. seglen = there_in_buf - here_in_buf;
  6890. memset((char *)sbuf,0,dz->buflen * sizeof(float)); /* clear synthesis buffer */
  6891. switch(dz->mode) {
  6892. case(2): /* TIMEVARYING OSCILLATOR BANK */
  6893. case(3):
  6894. if((exit_status = newoscval(samptime,dz))<0)
  6895. return(exit_status);
  6896. /* fall thro */
  6897. case(0): /* FIXED OSCILLATOR BANK */
  6898. case(1):
  6899. for(n=0;n < dz->iparam[PS_SCNT];n++) { /* Generate multi-syn synthed waveform */
  6900. wavtabincr = (SINTABLEN * frq[n])/(double)dz->infile->srate;
  6901. dtabpos = 0;
  6902. tabpos = 0;
  6903. for(m=0;m<=seglen;m++) {
  6904. tabpos = (int)floor(dtabpos);
  6905. timediff = dtabpos - (double)tabpos;
  6906. tabpos %= (int)SINTABLEN;
  6907. vallo = sintab[tabpos];
  6908. valhi = sintab[tabpos+1];
  6909. valdiff = valhi - vallo;
  6910. val = vallo + (valdiff * timediff);
  6911. val *= amp[n]; /* use amplitude of each component */
  6912. sbuf[m] = (float)(sbuf[m] + val);
  6913. dtabpos += wavtabincr;
  6914. }
  6915. }
  6916. break;
  6917. case(4): /* NOISE */
  6918. for(n=0;n<=seglen;n++)
  6919. sbuf[n] = (float)((drand48() * 2.0) - 1.0);
  6920. break;
  6921. }
  6922. maxamp = 0.0;
  6923. for(n=0;n<seglen;n++) /* Find synthetic sound's maxamp */
  6924. maxamp = max(maxamp,fabs(sbuf[n]));
  6925. normaliser = 1.0/maxamp;
  6926. for(n=0;n<seglen;n++) /* Normalise synthetic sound */
  6927. sbuf[n] = (float)(sbuf[n] * normaliser);
  6928. lo = 0;
  6929. for(n=0;n<seglen;n++) {
  6930. val = sbuf[n];
  6931. fofval = linear_splint(fofenv,fofloc,envcnt,(double)n,&lo);
  6932. if(dz->param[PS_DEPTH] < 1.0) {
  6933. vallo = val * (1.0 - dz->param[PS_DEPTH]);
  6934. valhi = val * dz->param[PS_DEPTH]; /* if fof isn't depth of whole sound */
  6935. valhi *= fofval;
  6936. val = vallo + valhi;
  6937. } else
  6938. val *= fofval;
  6939. obuf[*opos] = (float)val;
  6940. (*opos)++;
  6941. }
  6942. return(FINISHED);
  6943. }
  6944. /******************************* NEWOSCVAL ****************************/
  6945. int newoscval(int samptime,dataptr dz)
  6946. {
  6947. int thistime, lasttime, nowdiff, timediff;
  6948. double rratio;
  6949. int n,m,k;
  6950. double thisval, lastval, timeratio;
  6951. int total_frqcnt = dz->iparam[PS_SCNT] * dz->iparam[PS_TIMESLOTS];
  6952. k = dz->iparam[PS_TIMES_CNT];
  6953. thistime = dz->lparray[PS_SAMPTIME][k];
  6954. while(thistime < samptime) {
  6955. dz->iparam[PS_TIMES_CNT]++;
  6956. dz->iparam[PS_FRQ_INDEX] += dz->iparam[PS_SCNT];
  6957. if(dz->iparam[PS_TIMES_CNT]>dz->iparam[PS_TIMESLOTS]) {
  6958. sprintf(errstr,"Ran off end of oscillator data: newoscval()\n");
  6959. return(PROGRAM_ERROR);
  6960. }
  6961. k = dz->iparam[PS_TIMES_CNT];
  6962. thistime = dz->lparray[PS_SAMPTIME][k];
  6963. }
  6964. lasttime = dz->lparray[PS_SAMPTIME][k-1];
  6965. if(dz->iparam[PS_FRQ_INDEX] >= total_frqcnt) {
  6966. sprintf(errstr,"Ran out of frq, amp values too soon: newoscval()\n");
  6967. return(PROGRAM_ERROR);
  6968. }
  6969. timediff = thistime - lasttime;
  6970. nowdiff = samptime - lasttime;
  6971. timeratio = (double)nowdiff/(double)timediff;
  6972. for(n=0, m= dz->iparam[PS_FRQ_INDEX];n<dz->iparam[PS_SCNT];n++,m++) {
  6973. /* FREQUENCY */
  6974. thisval = dz->parray[PS_INFRQ][m];
  6975. lastval = dz->parray[PS_INFRQ][m - dz->iparam[PS_SCNT]];
  6976. if(flteq(lastval,thisval))
  6977. dz->parray[PS_OSCFRQ][n] = thisval;
  6978. else {
  6979. rratio = (thisval/lastval);
  6980. dz->parray[PS_OSCFRQ][n] = lastval * pow(rratio,timeratio);
  6981. }
  6982. /* AMPLITUDE */
  6983. thisval = dz->parray[PS_INAMP][m];
  6984. lastval = dz->parray[PS_INAMP][m - dz->iparam[PS_SCNT]];
  6985. if(flteq(thisval,lastval))
  6986. dz->parray[PS_OSCAMP][n] = thisval;
  6987. else {
  6988. rratio = (thisval/lastval);
  6989. dz->parray[PS_OSCAMP][n] = lastval * pow(rratio,timeratio);
  6990. }
  6991. }
  6992. return(FINISHED);
  6993. }
  6994. /******************************** SUPERIMPOSE_FOFS_ON_INPUT ********************************/
  6995. int superimpose_fofs_on_input(double *fofenv,int *fofloc,int here_in_buf,int there_in_buf,float *ibuf,float *ibuf3,float *obuf,
  6996. int *opos,int startsamp,int *envtoptime,double *envval,double *envincr,int envcnt,dataptr dz)
  6997. {
  6998. int changenv = 0;
  6999. int seglen, pos_in_inbuf, pos_in_seg,lo = 0;
  7000. double fofval, time;
  7001. double vallo, valhi, val, srate = dz->infile->srate;
  7002. double endtime = (double)(startsamp + there_in_buf)/srate;
  7003. if(endtime > dz->env[*envtoptime])
  7004. changenv = 1;
  7005. seglen = there_in_buf - here_in_buf;
  7006. for(pos_in_seg=0,pos_in_inbuf=here_in_buf;pos_in_seg<=seglen;pos_in_seg++,pos_in_inbuf++) {
  7007. if(changenv && (time = (double)(startsamp + pos_in_inbuf)/srate) >= dz->env[*envtoptime]) {
  7008. *envval = dz->env[(*envtoptime)+1];
  7009. *envtoptime += 2;
  7010. *envincr = dz->env[(*envtoptime)+1] - dz->env[(*envtoptime)-1];
  7011. *envincr /= dz->env[*envtoptime] - dz->env[(*envtoptime)-2];
  7012. *envincr /= srate;
  7013. } else
  7014. *envval += *envincr;
  7015. fofval = linear_splint(fofenv,fofloc,envcnt,(double)pos_in_seg,&lo);
  7016. val = ibuf3[pos_in_inbuf] * (*envval);
  7017. if(dz->param[PS_DEPTH] < 1.0) {
  7018. vallo = val * (1.0 - dz->param[PS_DEPTH]);
  7019. valhi = val * dz->param[PS_DEPTH]; /* if fof isn't depth of whole sound */
  7020. valhi *= fofval;
  7021. val = vallo + valhi;
  7022. } else
  7023. val *= fofval;
  7024. obuf[*opos] = (float)val;
  7025. (*opos)++;
  7026. }
  7027. return(FINISHED);
  7028. }
  7029. /**************************** CREATE_NORMALISING_ENVELOPE **********************/
  7030. int create_normalising_envelope(dataptr dz)
  7031. {
  7032. int wsize, bigarray, j, n, k, startsamp;
  7033. double maxval = 0.0, time, srate = (double)dz->infile->srate;
  7034. int donebuf;
  7035. float *ibuf = dz->sampbuf[2];
  7036. wsize = (int)floor(dz->param[PS_WSIZE] * MS_TO_SECS * srate);
  7037. bigarray = (dz->insams[1]/wsize) + 20;
  7038. free(dz->env);
  7039. if((dz->env=(float *)malloc(bigarray * 2 * sizeof(float)))==NULL) {
  7040. sprintf(errstr,"INSUFFICIENT MEMORY for envelope array.\n");
  7041. return(MEMORY_ERROR);
  7042. }
  7043. startsamp = 0;
  7044. k = 0;
  7045. while((dz->ssampsread = fgetfbufEx(ibuf, dz->buflen,dz->ifd[1],0)) > 0) {
  7046. if(dz->ssampsread < 0) {
  7047. sprintf(errstr,"Can't read samples from 2nd input soundfile.\n");
  7048. return(SYSTEM_ERROR);
  7049. }
  7050. donebuf = 0;
  7051. j = 0;
  7052. while(j < dz->ssampsread) {
  7053. for(n=0;n<wsize;n++) {
  7054. maxval = max(fabs(ibuf[j++]),maxval);
  7055. if(j >= dz->ssampsread) {
  7056. donebuf = 1;
  7057. break;
  7058. }
  7059. }
  7060. if(donebuf)
  7061. break;
  7062. time = (double)(startsamp + j)/srate;
  7063. dz->env[k++] = (float)time;
  7064. dz->env[k++] = (float)maxval;
  7065. maxval = 0.0;
  7066. }
  7067. startsamp += dz->ssampsread;
  7068. }
  7069. dz->env[k++] = (float)((double)dz->insams[1]/srate);
  7070. dz->env[k++] = 0.0f;
  7071. if(k < 4) {
  7072. sprintf(errstr,"Failed to find envelope of 2nd soundfile: File too short ??\n");
  7073. return(GOAL_FAILED);
  7074. }
  7075. for(n=1;n<k;n+=2) {
  7076. if(dz->env[n] < dz->param[PS_GATE]) /* eliminate zeros */
  7077. dz->env[n] = 0.0f;
  7078. else /* force normalisation curve */
  7079. dz->env[n] = (float)(1.0/dz->env[n]);
  7080. }
  7081. return(FINISHED);
  7082. }
  7083. /*****************************************/
  7084. /* PSOW_SYNTH: DEAL WITH SYNTHESIS DATA */
  7085. /*****************************************/
  7086. /************************** READ_SYNTH_DATA ********************************/
  7087. int read_synth_data(char *filename,dataptr dz)
  7088. {
  7089. int exit_status;
  7090. int n = 0, valcnt;
  7091. char temp[200], *p, *thisword;
  7092. double *frq, *amp;
  7093. int is_frq = TRUE;
  7094. if((dz->fp = fopen(filename,"r"))==NULL) {
  7095. sprintf(errstr,"Cannot open datafile %s\n",filename);
  7096. return(DATA_ERROR);
  7097. }
  7098. while(fgets(temp,200,dz->fp)!=NULL) {
  7099. p = temp;
  7100. if(is_an_empty_line_or_a_comment(p))
  7101. continue;
  7102. n++;
  7103. }
  7104. if(n==0) {
  7105. sprintf(errstr,"No data in file %s\n",filename);
  7106. return(DATA_ERROR);
  7107. }
  7108. dz->iparam[PS_SCNT] = n;
  7109. if((exit_status = allocate_osc_frq_amp_arrays(dz->iparam[PS_SCNT],dz))<0)
  7110. return(exit_status);
  7111. frq = dz->parray[PS_OSCFRQ];
  7112. amp = dz->parray[PS_OSCAMP];
  7113. if(fseek(dz->fp,0,0)<0) {
  7114. sprintf(errstr,"fseek() failed in read_synth_data()\n");
  7115. return(SYSTEM_ERROR);
  7116. }
  7117. n = 0;
  7118. while(fgets(temp,200,dz->fp)!=NULL) {
  7119. p = temp;
  7120. if(is_an_empty_line_or_a_comment(temp))
  7121. continue;
  7122. if(n >= dz->iparam[PS_SCNT]) {
  7123. sprintf(errstr,"Accounting problem reading Frq & Amp: read_synth_data()\n");
  7124. return(PROGRAM_ERROR);
  7125. }
  7126. valcnt = 0;
  7127. while(get_word_from_string(&p,&thisword)) {
  7128. if(valcnt>=2) {
  7129. sprintf(errstr,"Too many values on line %d: file %s\n",n+1,filename);
  7130. return(DATA_ERROR);
  7131. }
  7132. if(is_frq) {
  7133. if(sscanf(thisword,"%lf",&(frq[n]))!=1) {
  7134. sprintf(errstr,"Problem reading Frq data: line %d: file %s\n",n+1,filename);
  7135. return(DATA_ERROR);
  7136. }
  7137. if(frq[n]<dz->application->min_special || frq[n]>dz->application->max_special) {
  7138. sprintf(errstr,"frq (%.3lf) on line %d out of range (%.1lf to %.1lf):file %s\n",
  7139. frq[n],n+1,dz->application->min_special,dz->application->max_special,filename);
  7140. return(DATA_ERROR);
  7141. }
  7142. if(dz->mode==1 || dz->mode==3)
  7143. frq[n] = miditohz(frq[n]);
  7144. } else {
  7145. if((exit_status = get_level(thisword,&(amp[n])))<0)
  7146. return(exit_status);
  7147. if(amp[n]<0.0 || amp[n]>1.0) {
  7148. sprintf(errstr,"amp (%lf) out of range (0 to 1) on line %d: file %s\n",amp[n],n+1,filename);
  7149. return(DATA_ERROR);
  7150. }
  7151. }
  7152. is_frq = !is_frq;
  7153. valcnt++;
  7154. }
  7155. if(valcnt<2) {
  7156. sprintf(errstr,"Not enough values on line %d: file %s\n",n+1,filename);
  7157. return(DATA_ERROR);
  7158. }
  7159. n++;
  7160. }
  7161. if(fclose(dz->fp)<0) {
  7162. fprintf(stdout,"WARNING: Failed to close input textfile %s.\n",filename);
  7163. fflush(stdout);
  7164. }
  7165. return(FINISHED);
  7166. }
  7167. /************************** READ_TIME_VARYING_SYNTH_DATA ********************************/
  7168. int read_time_varying_synth_data(char *filename,dataptr dz)
  7169. {
  7170. int exit_status;
  7171. // int cnt = 0;
  7172. if((exit_status = get_data_from_tvary_infile(filename,dz))<0)
  7173. return(exit_status);
  7174. if((exit_status = check_synth_data(dz->iparam[PS_ENTRYCNT],dz))<0)
  7175. return(exit_status);
  7176. return(FINISHED);
  7177. }
  7178. /**************************** GET_DATA_FROM_TVARY_INFILE *******************************/
  7179. int get_data_from_tvary_infile(char *filename,dataptr dz)
  7180. {
  7181. int exit_status;
  7182. char *temp, *p, *thisword;
  7183. int maxlinelen, frqcnt;
  7184. int total_wordcnt = 0;
  7185. int columns_in_this_row, columns_in_row = 0, number_of_rows = 0;
  7186. int n, m, k, old_wordcnt;
  7187. double filedur = (double)dz->insams[0]/(double)dz->infile->srate;
  7188. double far_time = filedur + 1.0;
  7189. double val;
  7190. if((dz->fp = fopen(filename,"r"))==NULL) {
  7191. sprintf(errstr,"Cannot open datafile %s\n",filename);
  7192. return(DATA_ERROR);
  7193. }
  7194. if((exit_status = getmaxlinelen(&maxlinelen,dz->fp))<0)
  7195. return(exit_status);
  7196. if((fseek(dz->fp,0,0))<0) {
  7197. sprintf(errstr,"Seek failed in get_data_from_tvary_infile()\n");
  7198. return(SYSTEM_ERROR);
  7199. }
  7200. if((temp = (char *)malloc((maxlinelen+2) * sizeof(char)))==NULL) {
  7201. sprintf(errstr,"INSUFFICIENT MEMORY for temporary line storage.\n");
  7202. return(MEMORY_ERROR);
  7203. }
  7204. while(fgets(temp,maxlinelen,dz->fp)!=NULL) {
  7205. columns_in_this_row = 0;
  7206. if(is_an_empty_line_or_a_comment(temp))
  7207. continue;
  7208. p = temp;
  7209. while(get_word_from_string(&p,&thisword)) {
  7210. if((exit_status = get_level(thisword,&val))<0) { /* reads vals or dB vals */
  7211. free(temp);
  7212. return(exit_status);
  7213. }
  7214. if((total_wordcnt==0 && ((dz->parray[PS_FBRK] = (double *)malloc(sizeof(double)))==NULL))
  7215. || (dz->parray[PS_FBRK] = (double *)realloc(dz->parray[PS_FBRK],(total_wordcnt+1) * sizeof(double)))==NULL) {
  7216. free(temp);
  7217. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate oscillator brktable.\n");
  7218. return(MEMORY_ERROR);
  7219. }
  7220. dz->parray[PS_FBRK][total_wordcnt] = val;
  7221. columns_in_this_row++;
  7222. total_wordcnt++;
  7223. }
  7224. if(number_of_rows==0) {
  7225. if((columns_in_row = columns_in_this_row)<3) {
  7226. sprintf(errstr,"Insufficient oscillator data in row 1 of file %s.\n",filename);
  7227. free(temp);
  7228. return(DATA_ERROR);
  7229. } else if (ODD(columns_in_row - 1)) {
  7230. sprintf(errstr,"Frq and Amp data not paired correctly (or no Time) in row 1 of file %s.\n",filename);
  7231. free(temp);
  7232. return(DATA_ERROR);
  7233. }
  7234. } else if(columns_in_this_row!=columns_in_row) {
  7235. free(temp);
  7236. if(columns_in_this_row < columns_in_row)
  7237. sprintf(errstr,"Not enough entries in row %d of file %s\n",number_of_rows+1,filename);
  7238. else
  7239. sprintf(errstr,"Too many entries in row %d of file %s\n",number_of_rows+1,filename);
  7240. return(DATA_ERROR);
  7241. }
  7242. number_of_rows++;
  7243. }
  7244. dz->iparam[PS_WORDCNT] = total_wordcnt;
  7245. free(temp);
  7246. if(columns_in_row<3) {
  7247. sprintf(errstr,"Insufficient data in each row, to define oscillator.\n");
  7248. return(DATA_ERROR);
  7249. }
  7250. dz->iparam[PS_ENTRYCNT] = columns_in_row;
  7251. frqcnt = columns_in_row - 1;
  7252. if(ODD(frqcnt)) {
  7253. sprintf(errstr,"amplitude and freq data not correctly paired in rows.\n");
  7254. return(DATA_ERROR);
  7255. }
  7256. dz->iparam[PS_SCNT] = frqcnt/2;
  7257. dz->iparam[PS_TIMESLOTS] = number_of_rows;
  7258. if(fclose(dz->fp)<0) {
  7259. fprintf(stdout,"WARNING: Failed to close input textfile %s.\n",filename);
  7260. fflush(stdout);
  7261. }
  7262. if(dz->parray[PS_FBRK][0] !=0.0) {
  7263. if(flteq(dz->parray[PS_FBRK][0],0.0))
  7264. dz->parray[PS_FBRK][0] = 0.0;
  7265. else {
  7266. dz->iparam[PS_TIMESLOTS]++;
  7267. old_wordcnt = dz->iparam[PS_WORDCNT];
  7268. k = old_wordcnt-1;
  7269. dz->iparam[PS_WORDCNT] += dz->iparam[PS_ENTRYCNT];
  7270. m = dz->iparam[PS_WORDCNT] - 1;
  7271. if((dz->parray[PS_FBRK] = (double *)realloc(dz->parray[PS_FBRK],dz->iparam[PS_WORDCNT] * sizeof(double)))==NULL) {
  7272. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate oscillator brktable.\n");
  7273. return(MEMORY_ERROR);
  7274. }
  7275. for(n=0;n<old_wordcnt;n++,m--,k--)
  7276. dz->parray[PS_FBRK][m] = dz->parray[PS_FBRK][k];
  7277. dz->parray[PS_FBRK][0] = 0.0;
  7278. total_wordcnt = dz->iparam[PS_WORDCNT];
  7279. }
  7280. }
  7281. if(dz->parray[PS_FBRK][total_wordcnt - dz->iparam[PS_ENTRYCNT]] < far_time) {
  7282. dz->iparam[PS_TIMESLOTS]++;
  7283. m = dz->iparam[PS_WORDCNT];
  7284. k = m - dz->iparam[PS_ENTRYCNT];
  7285. dz->iparam[PS_WORDCNT] += dz->iparam[PS_ENTRYCNT];
  7286. if((dz->parray[PS_FBRK] = (double *)realloc(dz->parray[PS_FBRK],dz->iparam[PS_WORDCNT] * sizeof(double)))==NULL) {
  7287. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate oscillator brktable.\n");
  7288. return(MEMORY_ERROR);
  7289. }
  7290. dz->parray[PS_FBRK][m] = far_time;
  7291. m++;
  7292. k++;
  7293. for(n=1;n<dz->iparam[PS_ENTRYCNT];n++,m++,k++)
  7294. dz->parray[PS_FBRK][m] = dz->parray[PS_FBRK][k];
  7295. }
  7296. /* NEW CODE */
  7297. return(FINISHED);
  7298. }
  7299. /**************************** CHECK_SYNTH_DATA *******************************/
  7300. int check_synth_data(int wordcnt_in_line,dataptr dz)
  7301. {
  7302. int exit_status;
  7303. int n, lastosc, new_total_wordcnt;
  7304. double endtime;
  7305. int total_wordcnt = dz->iparam[PS_WORDCNT];
  7306. if(dz->parray[PS_FBRK][0] < 0.0) {
  7307. sprintf(errstr,"Negative time value (%lf) on line 1.\n",dz->parray[PS_FBRK][0]);
  7308. return(DATA_ERROR);
  7309. }
  7310. if(flteq(dz->parray[PS_FBRK][0],0.0))
  7311. dz->parray[PS_FBRK][0] = 0.0; /* FORCE AN OSCILLATOR SETTING AT TIME ZERO */
  7312. else {
  7313. if((dz->parray[PS_FBRK] =
  7314. (double *)realloc(dz->parray[PS_FBRK],(total_wordcnt+wordcnt_in_line) * sizeof(double)))==NULL) {
  7315. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate oscillator brktable.\n");
  7316. return(MEMORY_ERROR);
  7317. }
  7318. for(n=total_wordcnt-1; n>=0; n--)
  7319. dz->parray[PS_FBRK][n + wordcnt_in_line] = dz->parray[PS_FBRK][n];
  7320. total_wordcnt += wordcnt_in_line;
  7321. dz->parray[PS_FBRK][0] = 0.0;
  7322. dz->iparam[PS_TIMESLOTS]++;
  7323. }
  7324. dz->iparam[PS_WORDCNT] = total_wordcnt;
  7325. if((exit_status = check_seq_and_range_of_oscillator_data(dz->parray[PS_FBRK],wordcnt_in_line,dz))<0)
  7326. return(exit_status);
  7327. /* FORCE AN OSCILLATOR SETTING AT (BEYOND) END OF FILE */
  7328. lastosc = total_wordcnt - wordcnt_in_line;
  7329. endtime = (double)dz->insams[0]/(double)dz->infile->srate;
  7330. if(dz->parray[PS_FBRK][lastosc] <= endtime) {
  7331. if((dz->parray[PS_FBRK] =
  7332. (double *)realloc(dz->parray[PS_FBRK],(total_wordcnt + wordcnt_in_line) * sizeof(double)))==NULL) {
  7333. sprintf(errstr,"INSUFFICIENT MEMORY to reallocate oscillator brktable.\n");
  7334. return(MEMORY_ERROR);
  7335. }
  7336. new_total_wordcnt = total_wordcnt + wordcnt_in_line;
  7337. for(n=total_wordcnt;n<new_total_wordcnt;n++)
  7338. dz->parray[PS_FBRK][n] = dz->parray[PS_FBRK][n - wordcnt_in_line];
  7339. dz->parray[PS_FBRK][total_wordcnt] = endtime + 1.0;
  7340. total_wordcnt = new_total_wordcnt;
  7341. dz->iparam[PS_TIMESLOTS]++;
  7342. }
  7343. if(dz->iparam[PS_TIMESLOTS]<2) {
  7344. sprintf(errstr,"Error in timeslot logic: check_synth_data()\n");
  7345. return(PROGRAM_ERROR);
  7346. }
  7347. dz->iparam[PS_WORDCNT] = total_wordcnt;
  7348. return(FINISHED);
  7349. }
  7350. /**************************** GETMAXLINELEN *******************************/
  7351. int getmaxlinelen(int *maxcnt,FILE *fp)
  7352. {
  7353. int thiscnt = 0;
  7354. char c;
  7355. *maxcnt = 0;
  7356. while((c= (char)fgetc(fp))!=EOF) {
  7357. if(c=='\n' || c == ENDOFSTR) {
  7358. *maxcnt = max(*maxcnt,thiscnt);
  7359. thiscnt = 0;
  7360. } else
  7361. thiscnt++;
  7362. }
  7363. *maxcnt = (int)max(*maxcnt,thiscnt);
  7364. *maxcnt += 4; /* NEWLINE, ENDOFSTR and safety!! */
  7365. return(FINISHED);
  7366. }
  7367. /**************************** ALLOCATE_OSC_FRQ_AMP_ARRAYS *******************************/
  7368. int allocate_osc_frq_amp_arrays(int fltcnt,dataptr dz)
  7369. {
  7370. /*RWD 9:2001 must have empty arrays */
  7371. if((dz->parray[PS_OSCFRQ] = (double *)calloc(fltcnt * sizeof(double),sizeof(char)))==NULL
  7372. || (dz->parray[PS_OSCAMP] = (double *)calloc(fltcnt * sizeof(double),sizeof(char)))==NULL) {
  7373. sprintf(errstr,"INSUFFICIENT MEMORY for oscilattor amp and frq arrays.\n");
  7374. return(MEMORY_ERROR);
  7375. }
  7376. return(FINISHED);
  7377. }
  7378. /**************************** CHECK_SEQ_AND_RANGE_OF_OSCILLATOR_DATA *******************************/
  7379. int check_seq_and_range_of_oscillator_data(double *fbrk,int wordcnt_in_line,dataptr dz)
  7380. {
  7381. double lasttime = 0.0;
  7382. int n, m, lineno;
  7383. for(n=1;n<dz->iparam[PS_WORDCNT];n++) {
  7384. m = n%wordcnt_in_line;
  7385. lineno = (n/wordcnt_in_line)+1; /* original line-no : ignoring comments */
  7386. if(m==0) {
  7387. if(fbrk[n] <= lasttime) {
  7388. sprintf(errstr,"Time is out of sequence on line %d\n",lineno);
  7389. return(DATA_ERROR);
  7390. }
  7391. lasttime = fbrk[n];
  7392. } else if(ODD(m)) {
  7393. if(fbrk[n]<dz->application->min_special || fbrk[n]>dz->application->max_special) {
  7394. sprintf(errstr,"frq_or_midi value [%.3lf] out of range (%.1f - %.1f) on line %d\n",
  7395. fbrk[n],dz->application->min_special,dz->application->max_special,lineno);
  7396. return(DATA_ERROR);
  7397. }
  7398. if(dz->mode==1 || dz->mode==3)
  7399. fbrk[n] = miditohz(fbrk[n]);
  7400. } else
  7401. fbrk[n] = max(fbrk[n],0.0);
  7402. }
  7403. return(FINISHED);
  7404. }
  7405. /**************************** ALLOCATE_TVARYING_OSC_ARRAYS *******************************/
  7406. int allocate_tvarying_osc_arrays(dataptr dz)
  7407. {
  7408. if((dz->lparray[PS_SAMPTIME] = (int *)calloc(dz->iparam[PS_TIMESLOTS] * sizeof(int),sizeof(char)))==NULL
  7409. || (dz->parray[PS_INFRQ] = (double *)calloc(dz->iparam[PS_SCNT] * dz->iparam[PS_TIMESLOTS] * sizeof(double),sizeof(char)))==NULL
  7410. || (dz->parray[PS_INAMP] = (double *)calloc(dz->iparam[PS_SCNT] * dz->iparam[PS_TIMESLOTS] * sizeof(double),sizeof(char)))==NULL) {
  7411. sprintf(errstr,"INSUFFICIENT MEMORY for oscillator coefficients.\n");
  7412. return(MEMORY_ERROR);
  7413. }
  7414. return(FINISHED);
  7415. }
  7416. /**************************** PUT_TVARYING_OSC_DATA_IN_ARRAYS *******************************/
  7417. int put_tvarying_osc_data_in_arrays(double *fbrk,dataptr dz)
  7418. {
  7419. int timescnt = 0, freqcnt = 0, ampcnt = 0, n, m;
  7420. int total_frq_cnt = dz->iparam[PS_SCNT] * dz->iparam[PS_TIMESLOTS];
  7421. int entrycnt = dz->iparam[PS_ENTRYCNT];
  7422. int wordcnt = dz->iparam[PS_WORDCNT];
  7423. if(dz->parray[PS_INFRQ]==NULL) {
  7424. sprintf(errstr,"PS_INFRQ array not established: put_tvarying_osc_data_in_arrays()\n");
  7425. return(PROGRAM_ERROR);
  7426. }
  7427. if(dz->parray[PS_INAMP]==NULL) {
  7428. sprintf(errstr,"PS_INAMP array not established: put_tvarying_osc_data_in_arrays()\n");
  7429. return(PROGRAM_ERROR);
  7430. }
  7431. if(dz->lparray[PS_SAMPTIME]==NULL) {
  7432. sprintf(errstr,"PS_SAMPTIME array not established: put_tvarying_osc_data_in_arrays()\n");
  7433. return(PROGRAM_ERROR);
  7434. }
  7435. for(n=0;n<wordcnt;n++) {
  7436. m = n%entrycnt;
  7437. if(m==0)
  7438. dz->lparray[PS_SAMPTIME][timescnt++] = round(fbrk[n] * dz->infile->srate);
  7439. else if(ODD(m))
  7440. dz->parray[PS_INFRQ][freqcnt++] = fbrk[n];
  7441. else
  7442. dz->parray[PS_INAMP][ampcnt++] = fbrk[n];
  7443. }
  7444. if(freqcnt != total_frq_cnt || ampcnt != freqcnt || timescnt != dz->iparam[PS_TIMESLOTS]) {
  7445. sprintf(errstr,"Oscillator data accounting problem: put_tvarying_osc_data_in_arrays()\n");
  7446. return(PROGRAM_ERROR);
  7447. }
  7448. return(FINISHED);
  7449. }
  7450. /**************************** INITIALISE_PSOWSYNTH_INTERNAL_PARAMS **********************/
  7451. int initialise_psowsynth_internal_params(dataptr dz)
  7452. {
  7453. int exit_status;
  7454. int n;
  7455. if((exit_status = allocate_osc_frq_amp_arrays(dz->iparam[PS_SCNT],dz))<0)
  7456. return(exit_status);
  7457. for(n = 0;n<dz->iparam[PS_SCNT];n++) {
  7458. dz->parray[PS_OSCFRQ][n] = dz->parray[PS_INFRQ][n];
  7459. dz->parray[PS_OSCAMP][n] = dz->parray[PS_INAMP][n];
  7460. }
  7461. dz->iparam[PS_FRQ_INDEX] = dz->iparam[PS_SCNT];
  7462. dz->iparam[PS_TIMES_CNT] = 1;
  7463. return(FINISHED);
  7464. }
  7465. /**************/
  7466. /* PSOW_SPACE */
  7467. /**************/
  7468. /******************************** SPACECALC ********************************/
  7469. void spacecalc(double position,double *leftgain,double *rightgain)
  7470. {
  7471. double temp;
  7472. double relpos;
  7473. double reldist;
  7474. if(position < 0)
  7475. relpos = -position;
  7476. else
  7477. relpos = position;
  7478. temp = 1.0 + (relpos * relpos);
  7479. reldist = ROOT2 / sqrt(temp);
  7480. temp = (position + 1.0) / 2.0;
  7481. *rightgain = temp * reldist;
  7482. *leftgain = (1.0 - temp ) * reldist;
  7483. }
  7484. /*******************/
  7485. /* PSOW_INTERLEAVE */
  7486. /*******************/
  7487. /******************************** INSERT_EDGE_CUTS ********************************/
  7488. void insert_edge_cuts(int *cuttime,int *cutcnt,int *cutstart,dataptr dz)
  7489. {
  7490. int n;
  7491. if(cuttime[(*cutstart)-1] != dz->insams[0]) { /* force segment end of 1st file */
  7492. for(n=(*cutcnt)-1;n>=*cutstart;n--)
  7493. cuttime[n+1] = cuttime[n];
  7494. (*cutcnt)++;
  7495. cuttime[*cutstart] = 0;
  7496. (*cutstart)++;
  7497. }
  7498. for(n=(*cutcnt)-1;n>=0;n--) /* force zero at start of 1st file */
  7499. cuttime[n+1] = cuttime[n];
  7500. cuttime[0] = 0;
  7501. (*cutcnt)++;
  7502. (*cutstart)++;
  7503. if(cuttime[(*cutcnt)-1] != dz->insams[1]) { /* force segment end of 2ND file */
  7504. cuttime[(*cutcnt)++] = dz->insams[1];
  7505. (*cutcnt)++;
  7506. }
  7507. for(n=(*cutcnt)-1;n>=(*cutstart);n--) /* force zero at start of 2ND file */
  7508. cuttime[n+1] = cuttime[n];
  7509. (*cutcnt)++;
  7510. cuttime[*cutstart] = 0;
  7511. }
  7512. /******************************** DO_WEIGHT ********************************/
  7513. float *do_weight(int *cnt0,int *cnt1,float *ibuf,float *ibuf3,float *readbuf,dataptr dz)
  7514. {
  7515. int weight;
  7516. if(dz->param[PS_WEIGHT] > 1.0) {
  7517. weight = (int)round(dz->param[PS_WEIGHT]);
  7518. if(weight > *cnt0) {
  7519. readbuf = ibuf;
  7520. (*cnt0)++;
  7521. *cnt1 = 0;
  7522. } else {
  7523. readbuf = ibuf3;
  7524. (*cnt1)++;
  7525. *cnt0 = 0;
  7526. }
  7527. } else if(dz->param[PS_WEIGHT] < 1.0) {
  7528. weight = (int)round(1.0/dz->param[PS_WEIGHT]);
  7529. if(weight > *cnt1) {
  7530. readbuf = ibuf3;
  7531. (*cnt1)++;
  7532. *cnt0 = 0;
  7533. } else {
  7534. readbuf = ibuf;
  7535. (*cnt0)++;
  7536. *cnt1 = 0;
  7537. }
  7538. } else if(readbuf == ibuf) {
  7539. readbuf = ibuf3;
  7540. *cnt0 = 0;
  7541. *cnt1 = 1;
  7542. } else {
  7543. readbuf = ibuf;
  7544. *cnt0 = 1;
  7545. *cnt1 = 0;
  7546. }
  7547. return readbuf;
  7548. }
  7549. #ifdef PSOWTEST
  7550. tester(int wherelo,int wherehi,int opos,int here,int n,int j,int *cuttime,int srate)
  7551. {
  7552. if((opos >= wherelo) && (opos <= wherehi)) {
  7553. fprintf(stdout,"INFO: opos %d FROM cut[%d] to cut[%d] at %d && %d len = %d time = %lf\n",
  7554. opos,n-j,n,here,cuttime[n],cuttime[n]-here,(double)cuttime[n]/(double)srate);
  7555. fflush(stdout);
  7556. }
  7557. }
  7558. #endif
  7559. /******************************** INTERP_MAXFOFS ********************************/
  7560. #ifdef NOTDEF
  7561. int interp_maxfofs(int k,int here_in_buf, int there_in_buf,float *ibuf,float *obuf,int *cuttime,
  7562. int startsamp,double gain,int *opos,int *n,double maxlevel,int cutcnt,int *seglen,dataptr dz)
  7563. {
  7564. double maxamp, sum;
  7565. int maxloc, maxwav0start, maxwav0end, maxwav1start, maxwav1end, z, kk, thisseglen;
  7566. int thislen, thatlen, lendiff, totlen, repcnt, i0, i1;
  7567. double lenstep, dlen, dstep0, dstep1, di0, di1;
  7568. double tdiff0, tdiff1, val0, upval, valdiff0, val1, valdiff1;
  7569. int lastfofzero = 0, thisfofzero = 0;
  7570. maxamp = 0.0;
  7571. maxloc = k;
  7572. for(k = here_in_buf; k < there_in_buf;k++) {
  7573. if(fabs(ibuf[k]) > maxamp) {
  7574. maxamp = fabs(ibuf[k]);
  7575. maxloc = k;
  7576. }
  7577. }
  7578. thisseglen = *seglen;
  7579. if(maxamp == 0.0)
  7580. lastfofzero = 1;
  7581. if(!lastfofzero) {
  7582. k = here_in_buf;
  7583. maxwav0start = k;
  7584. while((maxwav0end = next_down_zcross(k,there_in_buf,ibuf))<there_in_buf) {
  7585. if(maxwav0end < 0)
  7586. maxwav0end = there_in_buf;
  7587. if(maxwav0end > maxloc)
  7588. break;
  7589. maxwav0start = k;
  7590. k = maxwav0end;
  7591. }
  7592. for(k = here_in_buf;k < maxwav0start;k++) {
  7593. sum = ibuf[k] * gain;
  7594. obuf[*opos] = (float)(obuf[*opos] + sum);
  7595. maxlevel = max(obuf[*opos],maxlevel);
  7596. (*opos)++;
  7597. }
  7598. }
  7599. z = dz->iparam[PSF_FOFSTR] - 1;
  7600. thisfofzero = 0;
  7601. while(z > 0) {
  7602. here_in_buf = there_in_buf;
  7603. (*n)++;
  7604. if(*n >= cutcnt) {
  7605. there_in_buf = dz->insams[0];
  7606. z = 0;
  7607. } else
  7608. there_in_buf = cuttime[*n] - startsamp;
  7609. thisseglen = there_in_buf - here_in_buf;
  7610. *seglen += thisseglen;
  7611. if(zerofof(here_in_buf,there_in_buf,ibuf))
  7612. thisfofzero = 1;
  7613. if(!thisfofzero) {
  7614. maxamp = 0.0;
  7615. maxloc = here_in_buf;
  7616. for(k = here_in_buf; k < there_in_buf;k++) {
  7617. if(fabs(ibuf[k]) > maxamp) {
  7618. maxamp = fabs(ibuf[k]);
  7619. maxloc = k;
  7620. }
  7621. }
  7622. k = here_in_buf;
  7623. maxwav1start = k;
  7624. while((maxwav1end = next_down_zcross(k,there_in_buf,ibuf))<there_in_buf) {
  7625. if(maxwav1end < 0)
  7626. maxwav1end = there_in_buf;
  7627. if(maxwav1end > maxloc)
  7628. break;
  7629. maxwav1start = k;
  7630. k = maxwav1end;
  7631. }
  7632. }
  7633. if(!lastfofzero && !thisfofzero) {
  7634. kk = maxwav0start;
  7635. thislen = maxwav0end - maxwav0start;
  7636. thatlen = maxwav1end - maxwav1start;
  7637. lendiff = thatlen - thislen;
  7638. totlen = maxwav1start - maxwav0start;
  7639. repcnt = find_repcnt(thislen,thatlen,totlen,dz);
  7640. lenstep = (double)lendiff/(double)repcnt;
  7641. dlen = (double)thislen;
  7642. dstep0 = 1.0;
  7643. dstep1 = (double)thislen/(double)thatlen;
  7644. for(kk=0;kk<repcnt;kk++) {
  7645. di0 = (double)maxwav0start;
  7646. di1 = (double)maxwav1start;
  7647. for(k = 0;k < thislen;k++) {
  7648. i0 = (int)floor(di0);
  7649. i1 = (int)floor(di1);
  7650. tdiff0 = di0 - (double)i0;
  7651. tdiff1 = di1 - (double)i1;
  7652. val0 = (double)ibuf[i0];
  7653. upval = ibuf[i0+1];
  7654. if(i0+1 > maxwav0end-1) {
  7655. if(upval < 0.0)
  7656. upval = 0.0;
  7657. }
  7658. valdiff0 = upval - val0;
  7659. val0 += valdiff0 * tdiff0;
  7660. val1 = (double)ibuf[i1];
  7661. upval = ibuf[i1+1];
  7662. if(i1+1 > maxwav1end-1) {
  7663. if(upval < 0.0)
  7664. upval = 0.0;
  7665. }
  7666. valdiff1 = upval - val1;
  7667. val1 += valdiff1 * tdiff1;
  7668. sum = (val0 + val1)/2.0;
  7669. sum *= gain;
  7670. obuf[*opos] = (float)(obuf[*opos] + sum);
  7671. maxlevel = max(obuf[*opos],maxlevel);
  7672. (*opos)++;
  7673. if(*opos >= dz->buflen * 2) {
  7674. sprintf(errstr,"OVERFLOWED OUTPUT BUFFER.\n");
  7675. return(MEMORY_ERROR);
  7676. }
  7677. di0 += dstep0;
  7678. di1 += dstep1;
  7679. }
  7680. dlen += lenstep;
  7681. thislen = (int)round(dlen);
  7682. dstep0 = (double)(maxwav0end-maxwav0start)/(double)thislen;
  7683. dstep1 = (double)thatlen/(double)thislen;
  7684. }
  7685. maxwav0start = maxwav1start;
  7686. maxwav0end = maxwav1end;
  7687. } else if(lastfofzero && thisfofzero) {
  7688. *opos += thisseglen;
  7689. if(*opos >= dz->buflen * 2) {
  7690. sprintf(errstr,"OVERFLOWED OUTPUT BUFFER.\n");
  7691. return(MEMORY_ERROR);
  7692. }
  7693. } else if(lastfofzero) {
  7694. for(k = here_in_buf;k < there_in_buf;k++) {
  7695. sum = ibuf[k] * gain;
  7696. obuf[*opos] = (float)(obuf[*opos] + sum);
  7697. maxlevel = max(obuf[*opos],maxlevel);
  7698. (*opos)++;
  7699. if(*opos >= dz->buflen * 2) {
  7700. sprintf(errstr,"OVERFLOWED OUTPUT BUFFER.\n");
  7701. return(MEMORY_ERROR);
  7702. }
  7703. }
  7704. } else if(thisfofzero) {
  7705. for(k = maxwav0start;k < there_in_buf;k++) {
  7706. sum = ibuf[k] * gain;
  7707. obuf[*opos] = (float)(obuf[*opos] + sum);
  7708. maxlevel = max(obuf[*opos],maxlevel);
  7709. (*opos)++;
  7710. if(*opos >= dz->buflen * 2) {
  7711. sprintf(errstr,"OVERFLOWED OUTPUT BUFFER.\n");
  7712. return(MEMORY_ERROR);
  7713. }
  7714. }
  7715. }
  7716. lastfofzero = thisfofzero;
  7717. z--;
  7718. }
  7719. if(!thisfofzero && !lastfofzero) {
  7720. for(k = maxwav1end;k < there_in_buf;k++) {
  7721. sum = ibuf[k] * gain;
  7722. obuf[*opos] = (float)(obuf[*opos] + sum);
  7723. maxlevel = max(obuf[*opos],maxlevel);
  7724. (*opos)++;
  7725. if(*opos >= dz->buflen * 2) {
  7726. sprintf(errstr,"OVERFLOWED OUTPUT BUFFER.\n");
  7727. return(MEMORY_ERROR);
  7728. }
  7729. }
  7730. }
  7731. return(FINISHED);
  7732. }
  7733. #endif
  7734. /****************************** ESTABLISH_MAXMODE *********************************/
  7735. int establish_maxmode(dataptr dz) {
  7736. switch(dz->process) {
  7737. case(PSOW_STRETCH):
  7738. case(PSOW_DUPL):
  7739. case(PSOW_DEL):
  7740. case(PSOW_STRFILL):
  7741. case(PSOW_FREEZE):
  7742. case(PSOW_CHOP):
  7743. case(PSOW_INTERP):
  7744. case(PSOW_IMPOSE):
  7745. case(PSOW_SPLIT):
  7746. case(PSOW_SPACE):
  7747. case(PSOW_INTERLEAVE):
  7748. case(PSOW_REPLACE):
  7749. case(PSOW_EXTEND):
  7750. case(PSOW_LOCATE):
  7751. case(PSOW_EXTEND2):
  7752. dz->maxmode = 0;
  7753. break;
  7754. case(PSOW_FEATURES):
  7755. dz->maxmode = 2;
  7756. break;
  7757. case(PSOW_SYNTH):
  7758. dz->maxmode = 5;
  7759. break;
  7760. case(PSOW_REINF):
  7761. case(PSOW_CUT):
  7762. dz->maxmode = 2;
  7763. break;
  7764. default:
  7765. sprintf(errstr,"FAILED TO FIND MAX MODE.\n");
  7766. return(PROGRAM_ERROR);
  7767. }
  7768. return(FINISHED);
  7769. }
  7770. /****************************** GET_MODE *********************************/
  7771. int get_the_mode_from_cmdline(char *str,dataptr dz)
  7772. {
  7773. if(sscanf(str,"%d",&dz->mode)!=1) {
  7774. sprintf(errstr,"Cannot read mode of program.\n");
  7775. return(USAGE_ONLY);
  7776. }
  7777. if(dz->mode <= 0 || dz->mode > dz->maxmode) {
  7778. sprintf(errstr,"Program mode value [%d] is out of range [1 - %d].\n",dz->mode,dz->maxmode);
  7779. return(USAGE_ONLY);
  7780. }
  7781. dz->mode--; /* CHANGE TO INTERNAL REPRESENTATION OF MODE NO */
  7782. return(FINISHED);
  7783. }
  7784. /**************************** READ_REINFORCEMENT_DATA *******************************/
  7785. int read_reinforcement_data(char *filename,dataptr dz)
  7786. {
  7787. int n = 0, m, valcnt;
  7788. char temp[200], *p, *thisword;
  7789. double *amp;
  7790. int *hno;
  7791. int is_hno = TRUE;
  7792. if((dz->fp = fopen(filename,"r"))==NULL) {
  7793. sprintf(errstr,"Cannot open datafile %s\n",filename);
  7794. return(DATA_ERROR);
  7795. }
  7796. while(fgets(temp,200,dz->fp)!=NULL) {
  7797. p = temp;
  7798. if(is_an_empty_line_or_a_comment(p))
  7799. continue;
  7800. n++;
  7801. }
  7802. if(n==0) {
  7803. sprintf(errstr,"No data in file %s\n",filename);
  7804. return(DATA_ERROR);
  7805. }
  7806. dz->itemcnt = n;
  7807. if ((dz->lparray[0] = (int *)malloc(dz->itemcnt * sizeof(int)))==NULL) {
  7808. sprintf(errstr,"Insufficient memory to store reinforcement harmonics data\n");
  7809. return(MEMORY_ERROR);
  7810. }
  7811. if ((dz->parray[0] = (double *)malloc(dz->itemcnt * sizeof(double)))==NULL) {
  7812. sprintf(errstr,"Insufficient memory to store reinforcement level data\n");
  7813. return(MEMORY_ERROR);
  7814. }
  7815. hno = dz->lparray[0];
  7816. amp = dz->parray[0];
  7817. if(fseek(dz->fp,0,0)<0) {
  7818. sprintf(errstr,"fseek() failed in read_reinforcement_data()\n");
  7819. return(SYSTEM_ERROR);
  7820. }
  7821. n = 0;
  7822. while(fgets(temp,200,dz->fp)!=NULL) {
  7823. p = temp;
  7824. if(is_an_empty_line_or_a_comment(temp))
  7825. continue;
  7826. if(n >= dz->itemcnt) {
  7827. sprintf(errstr,"Accounting problem reading reinforcement_data\n");
  7828. return(PROGRAM_ERROR);
  7829. }
  7830. valcnt = 0;
  7831. while(get_word_from_string(&p,&thisword)) {
  7832. if(valcnt>=2) {
  7833. sprintf(errstr,"Too many values on line %d: file %s\n",n+1,filename);
  7834. return(DATA_ERROR);
  7835. }
  7836. if(is_hno) {
  7837. if(sscanf(thisword,"%d",&(hno[n]))!=1) {
  7838. sprintf(errstr,"Problem reading Harmonic number data: line %d: file %s\n",n+1,filename);
  7839. return(DATA_ERROR);
  7840. }
  7841. if(hno[n]<dz->application->min_special || hno[n]>dz->application->max_special) {
  7842. sprintf(errstr,"harmonic number (%d) on line %d out of range (%d to %d):file %s\n",
  7843. hno[n],n+1,(int)round(dz->application->min_special),(int)round(dz->application->max_special),filename);
  7844. return(DATA_ERROR);
  7845. }
  7846. if(n > 0) {
  7847. for(m=0;m<n;m++) {
  7848. if(hno[m] == hno[n]) {
  7849. sprintf(errstr,"Harmonic number %d is duplicated in file %s\n",hno[m],filename);
  7850. return(DATA_ERROR);
  7851. }
  7852. }
  7853. }
  7854. } else {
  7855. if(sscanf(thisword,"%lf",&(amp[n]))!=1) {
  7856. sprintf(errstr,"Problem reading Gain for harmonic %d: line %d: file %s\n",hno[n],n+1,filename);
  7857. return(DATA_ERROR);
  7858. }
  7859. if(amp[n]<dz->application->min_special2 || amp[n]>dz->application->max_special2) {
  7860. sprintf(errstr,"amp (%lf) out of range (%lf to %.3lf) on line %d: file %s\n",
  7861. amp[n],dz->application->min_special2,dz->application->max_special2,n+1,filename);
  7862. return(DATA_ERROR);
  7863. }
  7864. }
  7865. is_hno = !is_hno;
  7866. valcnt++;
  7867. }
  7868. if(valcnt<2) {
  7869. sprintf(errstr,"Not enough values on line %d: file %s\n",n+1,filename);
  7870. return(DATA_ERROR);
  7871. }
  7872. n++;
  7873. }
  7874. if(fclose(dz->fp)<0) {
  7875. fprintf(stdout,"WARNING: Failed to close input textfile %s.\n",filename);
  7876. fflush(stdout);
  7877. }
  7878. return(FINISHED);
  7879. }
  7880. /**************************** HARMONIC_IS_DUPLICATED *******************************/
  7881. int harmonic_is_duplicated(int j,int m,int *hno)
  7882. {
  7883. int cnt = 0, zz;
  7884. while(cnt < m) {
  7885. zz = hno[m]/hno[cnt];
  7886. if(zz * hno[cnt] == hno[m]) { /* harmonic is integer multiple of a previous harmonic */
  7887. if(j % zz == 0)
  7888. return 1;
  7889. }
  7890. cnt++;
  7891. }
  7892. return 0;
  7893. }
  7894. /**************************** SORT_HARMONICS_TO_ASCENDING_ORDER *******************************/
  7895. void sort_harmonics_to_ascending_order(dataptr dz) {
  7896. int *hno, temphno;
  7897. double *amp, tempamp;
  7898. int n, m, subcnt;
  7899. if(dz->itemcnt == 1)
  7900. return;
  7901. hno = dz->lparray[0];
  7902. amp = dz->parray[0];
  7903. subcnt = dz->itemcnt - 1;
  7904. n = 0;
  7905. while(n < subcnt) {
  7906. m = n + 1;
  7907. while(m < dz->itemcnt) {
  7908. if(hno[m] < hno[n]) {
  7909. temphno = hno[n];
  7910. tempamp = amp[n];
  7911. hno[n] = hno[m];
  7912. amp[n] = amp[m];
  7913. hno[m] = temphno;
  7914. amp[m] = tempamp;
  7915. }
  7916. m++;
  7917. }
  7918. n++;
  7919. }
  7920. }
  7921. /**************************** READ_INHARMONICS_DATA *******************************/
  7922. int read_inharmonics_data(char *filename,dataptr dz)
  7923. {
  7924. int n = 0, m, valcnt;
  7925. char temp[200], *p, *thisword;
  7926. double *amp, *ihno;
  7927. int is_hno = TRUE;
  7928. if((dz->fp = fopen(filename,"r"))==NULL) {
  7929. sprintf(errstr,"Cannot open datafile %s\n",filename);
  7930. return(DATA_ERROR);
  7931. }
  7932. while(fgets(temp,200,dz->fp)!=NULL) {
  7933. p = temp;
  7934. if(is_an_empty_line_or_a_comment(p))
  7935. continue;
  7936. n++;
  7937. }
  7938. if(n==0) {
  7939. sprintf(errstr,"No data in file %s\n",filename);
  7940. return(DATA_ERROR);
  7941. }
  7942. dz->itemcnt = n;
  7943. if ((dz->parray[0] = (double *)malloc(dz->itemcnt * sizeof(double)))==NULL) {
  7944. sprintf(errstr,"Insufficient memory to store reinforcement harmonics data\n");
  7945. return(MEMORY_ERROR);
  7946. }
  7947. if ((dz->parray[1] = (double *)malloc(dz->itemcnt * sizeof(double)))==NULL) {
  7948. sprintf(errstr,"Insufficient memory to store reinforcement level data\n");
  7949. return(MEMORY_ERROR);
  7950. }
  7951. ihno = dz->parray[0];
  7952. amp = dz->parray[1];
  7953. if(fseek(dz->fp,0,0)<0) {
  7954. sprintf(errstr,"fseek() failed in read_reinforcement_data()\n");
  7955. return(SYSTEM_ERROR);
  7956. }
  7957. n = 0;
  7958. while(fgets(temp,200,dz->fp)!=NULL) {
  7959. p = temp;
  7960. if(is_an_empty_line_or_a_comment(temp))
  7961. continue;
  7962. if(n >= dz->itemcnt) {
  7963. sprintf(errstr,"Accounting problem reading reinforcement_data\n");
  7964. return(PROGRAM_ERROR);
  7965. }
  7966. valcnt = 0;
  7967. while(get_word_from_string(&p,&thisword)) {
  7968. if(valcnt>=2) {
  7969. sprintf(errstr,"Too many values on line %d: file %s\n",n+1,filename);
  7970. return(DATA_ERROR);
  7971. }
  7972. if(is_hno) {
  7973. if(sscanf(thisword,"%lf",&(ihno[n]))!=1) {
  7974. sprintf(errstr,"Problem reading Harmonic number data: line %d: file %s\n",n+1,filename);
  7975. return(DATA_ERROR);
  7976. }
  7977. if(ihno[n]<=dz->application->min_special || ihno[n]>dz->application->max_special) {
  7978. sprintf(errstr,"harmonic number (%lf) on line %d out of range (>%lf to %lf):file %s\n",
  7979. ihno[n],n+1,dz->application->min_special,dz->application->max_special,filename);
  7980. return(DATA_ERROR);
  7981. }
  7982. if(n > 0) {
  7983. for(m=0;m<n;m++) {
  7984. if(flteq(ihno[m],ihno[n])) {
  7985. sprintf(errstr,"Harmonic number %lf is duplicated in file %s\n",ihno[m],filename);
  7986. return(DATA_ERROR);
  7987. }
  7988. }
  7989. }
  7990. } else {
  7991. if(sscanf(thisword,"%lf",&(amp[n]))!=1) {
  7992. sprintf(errstr,"Problem reading Gain for harmonic %lf: line %d: file %s\n",ihno[n],n+1,filename);
  7993. return(DATA_ERROR);
  7994. }
  7995. if(amp[n]<dz->application->min_special2 || amp[n]>dz->application->max_special2) {
  7996. sprintf(errstr,"amp (%lf) out of range (%lf to %.3lf) on line %d: file %s\n",
  7997. amp[n],dz->application->min_special2,dz->application->max_special2,n+1,filename);
  7998. return(DATA_ERROR);
  7999. }
  8000. }
  8001. is_hno = !is_hno;
  8002. valcnt++;
  8003. }
  8004. if(valcnt<2) {
  8005. sprintf(errstr,"Not enough values on line %d: file %s\n",n+1,filename);
  8006. return(DATA_ERROR);
  8007. }
  8008. n++;
  8009. }
  8010. if(fclose(dz->fp)<0) {
  8011. fprintf(stdout,"WARNING: Failed to close input textfile %s.\n",filename);
  8012. fflush(stdout);
  8013. }
  8014. return(FINISHED);
  8015. }
  8016. /********************* CONVERT_PSE_SEMIT_TO_OCTRATIO *********************/
  8017. int convert_pse_semit_to_octratio(dataptr dz)
  8018. {
  8019. int n, m;
  8020. if(dz->brksize[PSE_TRNS] > 0) {
  8021. for(n=0,m = 1;n < dz->brksize[PSE_TRNS];n++,m+=2)
  8022. dz->brk[PSE_TRNS][m] = pow(2.0,(dz->brk[PSE_TRNS][m]/SEMITONES_PER_OCTAVE));
  8023. } else
  8024. dz->param[PSE_TRNS] = pow(2.0,(dz->param[PSE_TRNS]/SEMITONES_PER_OCTAVE));
  8025. return FINISHED;
  8026. }