mp3-engine.js 367 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401
  1. /*
  2. mp3编码器用到的lamejs编码引擎,一般都用MP3格式,浏览器支持广泛,此引擎测试的也比较多,稳定
  3. https://github.com/xiangyuecn/Recorder
  4. 由此源码改动而来 (2023-09-25 大幅精简代码,移除了大量从未调用的函数、条件分支)(2019-11-03 精简代码)
  5. https://github.com/zhuker/lamejs/blob/bfb7f6c6d7877e0fe1ad9e72697a871676119a0e/lame.all.js
  6. */
  7. (function(factory){
  8. var browser=typeof window=="object" && !!window.document;
  9. var win=browser?window:Object; //非浏览器环境,Recorder挂载在Object下面
  10. var rec=win.Recorder;
  11. factory(rec);
  12. }(function(Recorder){ //需要在Worker中运行,不能使用Recorder里的方法,包括$T
  13. "use strict";
  14. function lamejs() {
  15. var Math_log10=function(s){//坚决不能用也不要报语言问题的错误
  16. return Math.log(s)/Math.log(10);
  17. };
  18. var abort=function(what){
  19. throw new Error("abort(" + what + ")")
  20. };
  21. function new_byte(count) {
  22. return new Int8Array(count);
  23. }
  24. function new_short(count) {
  25. return new Int16Array(count);
  26. }
  27. function new_int(count) {
  28. return new Int32Array(count);
  29. }
  30. function new_float(count) {
  31. return new Float32Array(count);
  32. }
  33. function new_double(count) {
  34. return new Float64Array(count);
  35. }
  36. function new_float_n(args) {
  37. if (args.length == 1) {
  38. return new_float(args[0]);
  39. }
  40. var sz = args[0];
  41. args = args.slice(1);
  42. var A = [];
  43. for (var i = 0; i < sz; i++) {
  44. A.push(new_float_n(args));
  45. }
  46. return A;
  47. }
  48. function new_int_n(args) {
  49. if (args.length == 1) {
  50. return new_int(args[0]);
  51. }
  52. var sz = args[0];
  53. args = args.slice(1);
  54. var A = [];
  55. for (var i = 0; i < sz; i++) {
  56. A.push(new_int_n(args));
  57. }
  58. return A;
  59. }
  60. function new_short_n(args) {
  61. if (args.length == 1) {
  62. return new_short(args[0]);
  63. }
  64. var sz = args[0];
  65. args = args.slice(1);
  66. var A = [];
  67. for (var i = 0; i < sz; i++) {
  68. A.push(new_short_n(args));
  69. }
  70. return A;
  71. }
  72. function new_array_n(args) {
  73. if (args.length == 1) {
  74. return new Array(args[0]);
  75. }
  76. var sz = args[0];
  77. args = args.slice(1);
  78. var A = [];
  79. for (var i = 0; i < sz; i++) {
  80. A.push(new_array_n(args));
  81. }
  82. return A;
  83. }
  84. var Arrays = {};
  85. Arrays.fill = function (a, fromIndex, toIndex, val) {
  86. if (arguments.length == 2) {
  87. for (var i = 0; i < a.length; i++) {
  88. a[i] = arguments[1];
  89. }
  90. } else {
  91. for (var i = fromIndex; i < toIndex; i++) {
  92. a[i] = val;
  93. }
  94. }
  95. };
  96. var System = {};
  97. System.arraycopy = function (src, srcPos, dest, destPos, length) {
  98. var srcEnd = srcPos + length;
  99. while (srcPos < srcEnd)
  100. dest[destPos++] = src[srcPos++];
  101. };
  102. var Util = {};
  103. Util.SQRT2 = 1.41421356237309504880;
  104. Util.FAST_LOG10 = function (x) {
  105. return Math_log10(x);
  106. };
  107. Util.FAST_LOG10_X = function (x, y) {
  108. return Math_log10(x) * y;
  109. };
  110. function ShortBlock(ordinal) {
  111. this.ordinal = ordinal;
  112. }
  113. /**
  114. * LAME may use them, even different block types for L/R.
  115. */
  116. ShortBlock.short_block_allowed = new ShortBlock(0);
  117. /**
  118. * LAME may use them, but always same block types in L/R.
  119. */
  120. ShortBlock.short_block_coupled = new ShortBlock(1);
  121. /**
  122. * LAME will not use short blocks, long blocks only.
  123. */
  124. ShortBlock.short_block_dispensed = new ShortBlock(2);
  125. /**
  126. * LAME will not use long blocks, short blocks only.
  127. */
  128. ShortBlock.short_block_forced = new ShortBlock(3);
  129. var Float = {};
  130. Float.MAX_VALUE = 3.4028235e+38;
  131. function VbrMode(ordinal) {
  132. this.ordinal = ordinal;
  133. }
  134. VbrMode.vbr_off = new VbrMode(0);
  135. VbrMode.vbr_mt = new VbrMode(1);
  136. VbrMode.vbr_rh = new VbrMode(2);
  137. VbrMode.vbr_abr = new VbrMode(3);
  138. VbrMode.vbr_mtrh = new VbrMode(4);
  139. VbrMode.vbr_default = VbrMode.vbr_mtrh;
  140. var assert = function (x) {
  141. abort(x);
  142. };
  143. var module_exports = {
  144. "System": System,
  145. "VbrMode": VbrMode,
  146. "Float": Float,
  147. "ShortBlock": ShortBlock,
  148. "Util": Util,
  149. "Arrays": Arrays,
  150. "new_array_n": new_array_n,
  151. "new_byte": new_byte,
  152. "new_double": new_double,
  153. "new_float": new_float,
  154. "new_float_n": new_float_n,
  155. "new_int": new_int,
  156. "new_int_n": new_int_n,
  157. "new_short": new_short,
  158. "new_short_n": new_short_n,
  159. "assert": assert
  160. };
  161. //package mp3;
  162. /* MPEG modes */
  163. function MPEGMode(ordinal) {
  164. var _ordinal = ordinal;
  165. this.ordinal = function () {
  166. return _ordinal;
  167. }
  168. }
  169. MPEGMode.STEREO = new MPEGMode(0);
  170. MPEGMode.JOINT_STEREO = new MPEGMode(1);
  171. MPEGMode.DUAL_CHANNEL = new MPEGMode(2);
  172. MPEGMode.MONO = new MPEGMode(3);
  173. MPEGMode.NOT_SET = new MPEGMode(4);
  174. function Version() {
  175. /**
  176. * URL for the LAME website.
  177. */
  178. //var LAME_URL = "http://www.mp3dev.org/";
  179. /**
  180. * Major version number.
  181. */
  182. var LAME_MAJOR_VERSION = 3;
  183. /**
  184. * Minor version number.
  185. */
  186. var LAME_MINOR_VERSION = 98;
  187. /**
  188. * Patch level.
  189. */
  190. var LAME_PATCH_VERSION = 4;
  191. //fix cc 精简
  192. /**
  193. * The short version of the LAME version string.
  194. *
  195. * @return short version of the LAME version string
  196. */
  197. this.getLameShortVersion = function () {
  198. // Adding date and time to version string makes it harder for output
  199. // validation
  200. return (LAME_MAJOR_VERSION + "." + LAME_MINOR_VERSION + "." + LAME_PATCH_VERSION);
  201. }
  202. //fix cc 精简
  203. }
  204. /*
  205. * MP3 huffman table selecting and bit counting
  206. *
  207. * Copyright (c) 1999-2005 Takehiro TOMINAGA
  208. * Copyright (c) 2002-2005 Gabriel Bouvigne
  209. *
  210. * This library is free software; you can redistribute it and/or
  211. * modify it under the terms of the GNU Lesser General Public
  212. * License as published by the Free Software Foundation; either
  213. * version 2 of the License, or (at your option) any later version.
  214. *
  215. * This library is distributed in the hope that it will be useful,
  216. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  217. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  218. * Library General Public License for more details.
  219. *
  220. * You should have received a copy of the GNU Lesser General Public
  221. * License along with this library; if not, write to the
  222. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  223. * Boston, MA 02111-1307, USA.
  224. */
  225. /* $Id: Takehiro.java,v 1.26 2011/05/24 20:48:06 kenchis Exp $ */
  226. //package mp3;
  227. //import java.util.Arrays;
  228. function Takehiro() {
  229. var qupvt = null;
  230. this.qupvt = null;
  231. this.setModules = function (_qupvt) {
  232. this.qupvt = _qupvt;
  233. qupvt = _qupvt;
  234. }
  235. function Bits(b) {
  236. this.bits = 0 | b;
  237. }
  238. var subdv_table = [[0, 0], /* 0 bands */
  239. [0, 0], /* 1 bands */
  240. [0, 0], /* 2 bands */
  241. [0, 0], /* 3 bands */
  242. [0, 0], /* 4 bands */
  243. [0, 1], /* 5 bands */
  244. [1, 1], /* 6 bands */
  245. [1, 1], /* 7 bands */
  246. [1, 2], /* 8 bands */
  247. [2, 2], /* 9 bands */
  248. [2, 3], /* 10 bands */
  249. [2, 3], /* 11 bands */
  250. [3, 4], /* 12 bands */
  251. [3, 4], /* 13 bands */
  252. [3, 4], /* 14 bands */
  253. [4, 5], /* 15 bands */
  254. [4, 5], /* 16 bands */
  255. [4, 6], /* 17 bands */
  256. [5, 6], /* 18 bands */
  257. [5, 6], /* 19 bands */
  258. [5, 7], /* 20 bands */
  259. [6, 7], /* 21 bands */
  260. [6, 7], /* 22 bands */
  261. ];
  262. /**
  263. * nonlinear quantization of xr More accurate formula than the ISO formula.
  264. * Takes into account the fact that we are quantizing xr . ix, but we want
  265. * ix^4/3 to be as close as possible to x^4/3. (taking the nearest int would
  266. * mean ix is as close as possible to xr, which is different.)
  267. *
  268. * From Segher Boessenkool <segher@eastsite.nl> 11/1999
  269. *
  270. * 09/2000: ASM code removed in favor of IEEE754 hack by Takehiro Tominaga.
  271. * If you need the ASM code, check CVS circa Aug 2000.
  272. *
  273. * 01/2004: Optimizations by Gabriel Bouvigne
  274. */
  275. function quantize_lines_xrpow_01(l, istep, xr, xrPos, ix, ixPos) {
  276. var compareval0 = (1.0 - 0.4054) / istep;
  277. l = l >> 1;
  278. while ((l--) != 0) {
  279. ix[ixPos++] = (compareval0 > xr[xrPos++]) ? 0 : 1;
  280. ix[ixPos++] = (compareval0 > xr[xrPos++]) ? 0 : 1;
  281. }
  282. }
  283. /**
  284. * XRPOW_FTOI is a macro to convert floats to ints.<BR>
  285. * if XRPOW_FTOI(x) = nearest_int(x), then QUANTFAC(x)=adj43asm[x]<BR>
  286. * ROUNDFAC= -0.0946<BR>
  287. *
  288. * if XRPOW_FTOI(x) = floor(x), then QUANTFAC(x)=asj43[x]<BR>
  289. * ROUNDFAC=0.4054<BR>
  290. *
  291. * Note: using floor() or 0| is extremely slow. On machines where the
  292. * TAKEHIRO_IEEE754_HACK code above does not work, it is worthwile to write
  293. * some ASM for XRPOW_FTOI().
  294. */
  295. function quantize_lines_xrpow(l, istep, xr, xrPos, ix, ixPos) {
  296. l = l >> 1;
  297. var remaining = l % 2;
  298. l = l >> 1;
  299. while (l-- != 0) {
  300. var x0, x1, x2, x3;
  301. var rx0, rx1, rx2, rx3;
  302. x0 = xr[xrPos++] * istep;
  303. x1 = xr[xrPos++] * istep;
  304. rx0 = 0 | x0;
  305. x2 = xr[xrPos++] * istep;
  306. rx1 = 0 | x1;
  307. x3 = xr[xrPos++] * istep;
  308. rx2 = 0 | x2;
  309. x0 += qupvt.adj43[rx0];
  310. rx3 = 0 | x3;
  311. x1 += qupvt.adj43[rx1];
  312. ix[ixPos++] = 0 | x0;
  313. x2 += qupvt.adj43[rx2];
  314. ix[ixPos++] = 0 | x1;
  315. x3 += qupvt.adj43[rx3];
  316. ix[ixPos++] = 0 | x2;
  317. ix[ixPos++] = 0 | x3;
  318. }
  319. if (remaining != 0) {
  320. var x0, x1;
  321. var rx0, rx1;
  322. x0 = xr[xrPos++] * istep;
  323. x1 = xr[xrPos++] * istep;
  324. rx0 = 0 | x0;
  325. rx1 = 0 | x1;
  326. x0 += qupvt.adj43[rx0];
  327. x1 += qupvt.adj43[rx1];
  328. ix[ixPos++] = 0 | x0;
  329. ix[ixPos++] = 0 | x1;
  330. }
  331. }
  332. /**
  333. * Quantization function This function will select which lines to quantize
  334. * and call the proper quantization function
  335. */
  336. function quantize_xrpow(xp, pi, istep, codInfo, prevNoise) {
  337. /* quantize on xr^(3/4) instead of xr */
  338. var sfb;
  339. var sfbmax;
  340. var j = 0;
  341. var prev_data_use;
  342. var accumulate = 0;
  343. var accumulate01 = 0;
  344. var xpPos = 0;
  345. var iData = pi;
  346. var iDataPos = 0;
  347. var acc_iData = iData;
  348. var acc_iDataPos = 0;
  349. var acc_xp = xp;
  350. var acc_xpPos = 0;
  351. /*
  352. * Reusing previously computed data does not seems to work if global
  353. * gain is changed. Finding why it behaves this way would allow to use a
  354. * cache of previously computed values (let's 10 cached values per sfb)
  355. * that would probably provide a noticeable speedup
  356. */
  357. prev_data_use = (prevNoise != null && (codInfo.global_gain == prevNoise.global_gain));
  358. if (codInfo.block_type == Encoder.SHORT_TYPE)
  359. sfbmax = 38;
  360. else
  361. sfbmax = 21;
  362. for (sfb = 0; sfb <= sfbmax; sfb++) {
  363. var step = -1;
  364. if (prev_data_use || codInfo.block_type == Encoder.NORM_TYPE) {
  365. step = codInfo.global_gain
  366. - ((codInfo.scalefac[sfb] + (codInfo.preflag != 0 ? qupvt.pretab[sfb]
  367. : 0)) << (codInfo.scalefac_scale + 1))
  368. - codInfo.subblock_gain[codInfo.window[sfb]] * 8;
  369. }
  370. if (prev_data_use && (prevNoise.step[sfb] == step)) {
  371. /*
  372. * do not recompute this part, but compute accumulated lines
  373. */
  374. if (accumulate != 0) {
  375. quantize_lines_xrpow(accumulate, istep, acc_xp, acc_xpPos,
  376. acc_iData, acc_iDataPos);
  377. accumulate = 0;
  378. }
  379. if (accumulate01 != 0) {
  380. abort();//fix cc 精简
  381. }
  382. } else { /* should compute this part */
  383. var l = codInfo.width[sfb];
  384. if ((j + codInfo.width[sfb]) > codInfo.max_nonzero_coeff) {
  385. /* do not compute upper zero part */
  386. var usefullsize;
  387. usefullsize = codInfo.max_nonzero_coeff - j + 1;
  388. Arrays.fill(pi, codInfo.max_nonzero_coeff, 576, 0);
  389. l = usefullsize;
  390. if (l < 0) {
  391. l = 0;
  392. }
  393. /* no need to compute higher sfb values */
  394. sfb = sfbmax + 1;
  395. }
  396. /* accumulate lines to quantize */
  397. if (0 == accumulate && 0 == accumulate01) {
  398. acc_iData = iData;
  399. acc_iDataPos = iDataPos;
  400. acc_xp = xp;
  401. acc_xpPos = xpPos;
  402. }
  403. if (prevNoise != null && prevNoise.sfb_count1 > 0
  404. && sfb >= prevNoise.sfb_count1
  405. && prevNoise.step[sfb] > 0
  406. && step >= prevNoise.step[sfb]) {
  407. if (accumulate != 0) {
  408. quantize_lines_xrpow(accumulate, istep, acc_xp,
  409. acc_xpPos, acc_iData, acc_iDataPos);
  410. accumulate = 0;
  411. acc_iData = iData;
  412. acc_iDataPos = iDataPos;
  413. acc_xp = xp;
  414. acc_xpPos = xpPos;
  415. }
  416. accumulate01 += l;
  417. } else {
  418. if (accumulate01 != 0) {
  419. quantize_lines_xrpow_01(accumulate01, istep, acc_xp,
  420. acc_xpPos, acc_iData, acc_iDataPos);
  421. accumulate01 = 0;
  422. acc_iData = iData;
  423. acc_iDataPos = iDataPos;
  424. acc_xp = xp;
  425. acc_xpPos = xpPos;
  426. }
  427. accumulate += l;
  428. }
  429. if (l <= 0) {
  430. /*
  431. * rh: 20040215 may happen due to "prev_data_use"
  432. * optimization
  433. */
  434. if (accumulate01 != 0) {
  435. abort();//fix cc 精简
  436. }
  437. if (accumulate != 0) {
  438. abort();//fix cc 精简
  439. }
  440. break;
  441. /* ends for-loop */
  442. }
  443. }
  444. if (sfb <= sfbmax) {
  445. iDataPos += codInfo.width[sfb];
  446. xpPos += codInfo.width[sfb];
  447. j += codInfo.width[sfb];
  448. }
  449. }
  450. if (accumulate != 0) { /* last data part */
  451. quantize_lines_xrpow(accumulate, istep, acc_xp, acc_xpPos,
  452. acc_iData, acc_iDataPos);
  453. accumulate = 0;
  454. }
  455. if (accumulate01 != 0) { /* last data part */
  456. abort();//fix cc 精简
  457. }
  458. }
  459. /**
  460. * ix_max
  461. */
  462. function ix_max(ix, ixPos, endPos) {
  463. var max1 = 0, max2 = 0;
  464. do {
  465. var x1 = ix[ixPos++];
  466. var x2 = ix[ixPos++];
  467. if (max1 < x1)
  468. max1 = x1;
  469. if (max2 < x2)
  470. max2 = x2;
  471. } while (ixPos < endPos);
  472. if (max1 < max2)
  473. max1 = max2;
  474. return max1;
  475. }
  476. function count_bit_ESC(ix, ixPos, end, t1, t2, s) {
  477. /* ESC-table is used */
  478. var linbits = Tables.ht[t1].xlen * 65536 + Tables.ht[t2].xlen;
  479. var sum = 0, sum2;
  480. do {
  481. var x = ix[ixPos++];
  482. var y = ix[ixPos++];
  483. if (x != 0) {
  484. if (x > 14) {
  485. x = 15;
  486. sum += linbits;
  487. }
  488. x *= 16;
  489. }
  490. if (y != 0) {
  491. if (y > 14) {
  492. y = 15;
  493. sum += linbits;
  494. }
  495. x += y;
  496. }
  497. sum += Tables.largetbl[x];
  498. } while (ixPos < end);
  499. sum2 = sum & 0xffff;
  500. sum >>= 16;
  501. if (sum > sum2) {
  502. sum = sum2;
  503. t1 = t2;
  504. }
  505. s.bits += sum;
  506. return t1;
  507. }
  508. function count_bit_noESC(ix, ixPos, end, s) {
  509. /* No ESC-words */
  510. var sum1 = 0;
  511. var hlen1 = Tables.ht[1].hlen;
  512. do {
  513. var x = ix[ixPos + 0] * 2 + ix[ixPos + 1];
  514. ixPos += 2;
  515. sum1 += hlen1[x];
  516. } while (ixPos < end);
  517. s.bits += sum1;
  518. return 1;
  519. }
  520. function count_bit_noESC_from2(ix, ixPos, end, t1, s) {
  521. /* No ESC-words */
  522. var sum = 0, sum2;
  523. var xlen = Tables.ht[t1].xlen;
  524. var hlen;
  525. if (t1 == 2)
  526. hlen = Tables.table23;
  527. else
  528. hlen = Tables.table56;
  529. do {
  530. var x = ix[ixPos + 0] * xlen + ix[ixPos + 1];
  531. ixPos += 2;
  532. sum += hlen[x];
  533. } while (ixPos < end);
  534. sum2 = sum & 0xffff;
  535. sum >>= 16;
  536. if (sum > sum2) {
  537. sum = sum2;
  538. t1++;
  539. }
  540. s.bits += sum;
  541. return t1;
  542. }
  543. function count_bit_noESC_from3(ix, ixPos, end, t1, s) {
  544. /* No ESC-words */
  545. var sum1 = 0;
  546. var sum2 = 0;
  547. var sum3 = 0;
  548. var xlen = Tables.ht[t1].xlen;
  549. var hlen1 = Tables.ht[t1].hlen;
  550. var hlen2 = Tables.ht[t1 + 1].hlen;
  551. var hlen3 = Tables.ht[t1 + 2].hlen;
  552. do {
  553. var x = ix[ixPos + 0] * xlen + ix[ixPos + 1];
  554. ixPos += 2;
  555. sum1 += hlen1[x];
  556. sum2 += hlen2[x];
  557. sum3 += hlen3[x];
  558. } while (ixPos < end);
  559. var t = t1;
  560. if (sum1 > sum2) {
  561. sum1 = sum2;
  562. t++;
  563. }
  564. if (sum1 > sum3) {
  565. sum1 = sum3;
  566. t = t1 + 2;
  567. }
  568. s.bits += sum1;
  569. return t;
  570. }
  571. /*************************************************************************/
  572. /* choose table */
  573. /*************************************************************************/
  574. var huf_tbl_noESC = [1, 2, 5, 7, 7, 10, 10, 13, 13,
  575. 13, 13, 13, 13, 13, 13];
  576. /**
  577. * Choose the Huffman table that will encode ix[begin..end] with the fewest
  578. * bits.
  579. *
  580. * Note: This code contains knowledge about the sizes and characteristics of
  581. * the Huffman tables as defined in the IS (Table B.7), and will not work
  582. * with any arbitrary tables.
  583. */
  584. function choose_table(ix, ixPos, endPos, s) {
  585. var max = ix_max(ix, ixPos, endPos);
  586. switch (max) {
  587. case 0:
  588. return max;
  589. case 1:
  590. return count_bit_noESC(ix, ixPos, endPos, s);
  591. case 2:
  592. case 3:
  593. return count_bit_noESC_from2(ix, ixPos, endPos,
  594. huf_tbl_noESC[max - 1], s);
  595. case 4:
  596. case 5:
  597. case 6:
  598. case 7:
  599. case 8:
  600. case 9:
  601. case 10:
  602. case 11:
  603. case 12:
  604. case 13:
  605. case 14:
  606. case 15:
  607. return count_bit_noESC_from3(ix, ixPos, endPos,
  608. huf_tbl_noESC[max - 1], s);
  609. default:
  610. /* try tables with linbits */
  611. if (max > QuantizePVT.IXMAX_VAL) {
  612. abort();//fix cc 精简
  613. }
  614. max -= 15;
  615. var choice2;
  616. for (choice2 = 24; choice2 < 32; choice2++) {
  617. if (Tables.ht[choice2].linmax >= max) {
  618. break;
  619. }
  620. }
  621. var choice;
  622. for (choice = choice2 - 8; choice < 24; choice++) {
  623. if (Tables.ht[choice].linmax >= max) {
  624. break;
  625. }
  626. }
  627. return count_bit_ESC(ix, ixPos, endPos, choice, choice2, s);
  628. }
  629. }
  630. /**
  631. * count_bit
  632. */
  633. this.noquant_count_bits = function (gfc, gi, prev_noise) {
  634. var ix = gi.l3_enc;
  635. var i = Math.min(576, ((gi.max_nonzero_coeff + 2) >> 1) << 1);
  636. if (prev_noise != null)
  637. prev_noise.sfb_count1 = 0;
  638. /* Determine count1 region */
  639. for (; i > 1; i -= 2)
  640. if ((ix[i - 1] | ix[i - 2]) != 0)
  641. break;
  642. gi.count1 = i;
  643. /* Determines the number of bits to encode the quadruples. */
  644. var a1 = 0;
  645. var a2 = 0;
  646. for (; i > 3; i -= 4) {
  647. var p;
  648. /* hack to check if all values <= 1 */
  649. //throw "TODO: HACK if ((((long) ix[i - 1] | (long) ix[i - 2] | (long) ix[i - 3] | (long) ix[i - 4]) & 0xffffffffL) > 1L "
  650. //if (true) {
  651. if (((ix[i - 1] | ix[i - 2] | ix[i - 3] | ix[i - 4]) & 0x7fffffff) > 1) {
  652. break;
  653. }
  654. p = ((ix[i - 4] * 2 + ix[i - 3]) * 2 + ix[i - 2]) * 2 + ix[i - 1];
  655. a1 += Tables.t32l[p];
  656. a2 += Tables.t33l[p];
  657. }
  658. var bits = a1;
  659. gi.count1table_select = 0;
  660. if (a1 > a2) {
  661. bits = a2;
  662. gi.count1table_select = 1;
  663. }
  664. gi.count1bits = bits;
  665. gi.big_values = i;
  666. if (i == 0)
  667. return bits;
  668. if (gi.block_type == Encoder.SHORT_TYPE) {
  669. a1 = 3 * gfc.scalefac_band.s[3];
  670. if (a1 > gi.big_values)
  671. a1 = gi.big_values;
  672. a2 = gi.big_values;
  673. } else if (gi.block_type == Encoder.NORM_TYPE) {
  674. /* bv_scf has 576 entries (0..575) */
  675. a1 = gi.region0_count = gfc.bv_scf[i - 2];
  676. a2 = gi.region1_count = gfc.bv_scf[i - 1];
  677. a2 = gfc.scalefac_band.l[a1 + a2 + 2];
  678. a1 = gfc.scalefac_band.l[a1 + 1];
  679. if (a2 < i) {
  680. var bi = new Bits(bits);
  681. gi.table_select[2] = choose_table(ix, a2, i, bi);
  682. bits = bi.bits;
  683. }
  684. } else {
  685. gi.region0_count = 7;
  686. /* gi.region1_count = SBPSY_l - 7 - 1; */
  687. gi.region1_count = Encoder.SBMAX_l - 1 - 7 - 1;
  688. a1 = gfc.scalefac_band.l[7 + 1];
  689. a2 = i;
  690. if (a1 > a2) {
  691. a1 = a2;
  692. }
  693. }
  694. /* have to allow for the case when bigvalues < region0 < region1 */
  695. /* (and region0, region1 are ignored) */
  696. a1 = Math.min(a1, i);
  697. a2 = Math.min(a2, i);
  698. /* Count the number of bits necessary to code the bigvalues region. */
  699. if (0 < a1) {
  700. var bi = new Bits(bits);
  701. gi.table_select[0] = choose_table(ix, 0, a1, bi);
  702. bits = bi.bits;
  703. }
  704. if (a1 < a2) {
  705. var bi = new Bits(bits);
  706. gi.table_select[1] = choose_table(ix, a1, a2, bi);
  707. bits = bi.bits;
  708. }
  709. if (gfc.use_best_huffman == 2) {
  710. abort();//fix cc 精简
  711. }
  712. if (prev_noise != null) {
  713. if (gi.block_type == Encoder.NORM_TYPE) {
  714. var sfb = 0;
  715. while (gfc.scalefac_band.l[sfb] < gi.big_values) {
  716. sfb++;
  717. }
  718. prev_noise.sfb_count1 = sfb;
  719. }
  720. }
  721. return bits;
  722. }
  723. this.count_bits = function (gfc, xr, gi, prev_noise) {
  724. var ix = gi.l3_enc;
  725. /* since quantize_xrpow uses table lookup, we need to check this first: */
  726. var w = (QuantizePVT.IXMAX_VAL) / qupvt.IPOW20(gi.global_gain);
  727. if (gi.xrpow_max > w)
  728. return QuantizePVT.LARGE_BITS;
  729. quantize_xrpow(xr, ix, qupvt.IPOW20(gi.global_gain), gi, prev_noise);
  730. if ((gfc.substep_shaping & 2) != 0) {
  731. abort();//fix cc 精简
  732. }
  733. return this.noquant_count_bits(gfc, gi, prev_noise);
  734. }
  735. /**
  736. * re-calculate the best scalefac_compress using scfsi the saved bits are
  737. * kept in the bit reservoir.
  738. */
  739. function recalc_divide_init(gfc, cod_info, ix, r01_bits, r01_div, r0_tbl, r1_tbl) {
  740. var bigv = cod_info.big_values;
  741. for (var r0 = 0; r0 <= 7 + 15; r0++) {
  742. r01_bits[r0] = QuantizePVT.LARGE_BITS;
  743. }
  744. for (var r0 = 0; r0 < 16; r0++) {
  745. var a1 = gfc.scalefac_band.l[r0 + 1];
  746. if (a1 >= bigv)
  747. break;
  748. var r0bits = 0;
  749. var bi = new Bits(r0bits);
  750. var r0t = choose_table(ix, 0, a1, bi);
  751. r0bits = bi.bits;
  752. for (var r1 = 0; r1 < 8; r1++) {
  753. var a2 = gfc.scalefac_band.l[r0 + r1 + 2];
  754. if (a2 >= bigv)
  755. break;
  756. var bits = r0bits;
  757. bi = new Bits(bits);
  758. var r1t = choose_table(ix, a1, a2, bi);
  759. bits = bi.bits;
  760. if (r01_bits[r0 + r1] > bits) {
  761. r01_bits[r0 + r1] = bits;
  762. r01_div[r0 + r1] = r0;
  763. r0_tbl[r0 + r1] = r0t;
  764. r1_tbl[r0 + r1] = r1t;
  765. }
  766. }
  767. }
  768. }
  769. function recalc_divide_sub(gfc, cod_info2, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl) {
  770. var bigv = cod_info2.big_values;
  771. for (var r2 = 2; r2 < Encoder.SBMAX_l + 1; r2++) {
  772. var a2 = gfc.scalefac_band.l[r2];
  773. if (a2 >= bigv)
  774. break;
  775. var bits = r01_bits[r2 - 2] + cod_info2.count1bits;
  776. if (gi.part2_3_length <= bits)
  777. break;
  778. var bi = new Bits(bits);
  779. var r2t = choose_table(ix, a2, bigv, bi);
  780. bits = bi.bits;
  781. if (gi.part2_3_length <= bits)
  782. continue;
  783. gi.assign(cod_info2);
  784. gi.part2_3_length = bits;
  785. gi.region0_count = r01_div[r2 - 2];
  786. gi.region1_count = r2 - 2 - r01_div[r2 - 2];
  787. gi.table_select[0] = r0_tbl[r2 - 2];
  788. gi.table_select[1] = r1_tbl[r2 - 2];
  789. gi.table_select[2] = r2t;
  790. }
  791. }
  792. this.best_huffman_divide = function (gfc, gi) {
  793. var cod_info2 = new GrInfo();
  794. var ix = gi.l3_enc;
  795. var r01_bits = new_int(7 + 15 + 1);
  796. var r01_div = new_int(7 + 15 + 1);
  797. var r0_tbl = new_int(7 + 15 + 1);
  798. var r1_tbl = new_int(7 + 15 + 1);
  799. /* SHORT BLOCK stuff fails for MPEG2 */
  800. if (gi.block_type == Encoder.SHORT_TYPE && gfc.mode_gr == 1)
  801. return;
  802. cod_info2.assign(gi);
  803. if (gi.block_type == Encoder.NORM_TYPE) {
  804. recalc_divide_init(gfc, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);
  805. recalc_divide_sub(gfc, cod_info2, gi, ix, r01_bits, r01_div,
  806. r0_tbl, r1_tbl);
  807. }
  808. var i = cod_info2.big_values;
  809. if (i == 0 || (ix[i - 2] | ix[i - 1]) > 1)
  810. return;
  811. i = gi.count1 + 2;
  812. if (i > 576)
  813. return;
  814. /* Determines the number of bits to encode the quadruples. */
  815. cod_info2.assign(gi);
  816. cod_info2.count1 = i;
  817. var a1 = 0;
  818. var a2 = 0;
  819. for (; i > cod_info2.big_values; i -= 4) {
  820. var p = ((ix[i - 4] * 2 + ix[i - 3]) * 2 + ix[i - 2]) * 2
  821. + ix[i - 1];
  822. a1 += Tables.t32l[p];
  823. a2 += Tables.t33l[p];
  824. }
  825. cod_info2.big_values = i;
  826. cod_info2.count1table_select = 0;
  827. if (a1 > a2) {
  828. a1 = a2;
  829. cod_info2.count1table_select = 1;
  830. }
  831. cod_info2.count1bits = a1;
  832. if (cod_info2.block_type == Encoder.NORM_TYPE)
  833. recalc_divide_sub(gfc, cod_info2, gi, ix, r01_bits, r01_div,
  834. r0_tbl, r1_tbl);
  835. else {
  836. /* Count the number of bits necessary to code the bigvalues region. */
  837. cod_info2.part2_3_length = a1;
  838. a1 = gfc.scalefac_band.l[7 + 1];
  839. if (a1 > i) {
  840. a1 = i;
  841. }
  842. if (a1 > 0) {
  843. var bi = new Bits(cod_info2.part2_3_length);
  844. cod_info2.table_select[0] = choose_table(ix, 0, a1, bi);
  845. cod_info2.part2_3_length = bi.bits;
  846. }
  847. if (i > a1) {
  848. var bi = new Bits(cod_info2.part2_3_length);
  849. cod_info2.table_select[1] = choose_table(ix, a1, i, bi);
  850. cod_info2.part2_3_length = bi.bits;
  851. }
  852. if (gi.part2_3_length > cod_info2.part2_3_length)
  853. gi.assign(cod_info2);
  854. }
  855. }
  856. var slen1_n = [1, 1, 1, 1, 8, 2, 2, 2, 4, 4, 4, 8, 8, 8, 16, 16];
  857. var slen2_n = [1, 2, 4, 8, 1, 2, 4, 8, 2, 4, 8, 2, 4, 8, 4, 8];
  858. var slen1_tab = [0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4];
  859. var slen2_tab = [0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3];
  860. Takehiro.slen1_tab = slen1_tab;
  861. Takehiro.slen2_tab = slen2_tab;
  862. function scfsi_calc(ch, l3_side) {
  863. var sfb;
  864. var gi = l3_side.tt[1][ch];
  865. var g0 = l3_side.tt[0][ch];
  866. for (var i = 0; i < Tables.scfsi_band.length - 1; i++) {
  867. for (sfb = Tables.scfsi_band[i]; sfb < Tables.scfsi_band[i + 1]; sfb++) {
  868. if (g0.scalefac[sfb] != gi.scalefac[sfb]
  869. && gi.scalefac[sfb] >= 0)
  870. break;
  871. }
  872. if (sfb == Tables.scfsi_band[i + 1]) {
  873. for (sfb = Tables.scfsi_band[i]; sfb < Tables.scfsi_band[i + 1]; sfb++) {
  874. gi.scalefac[sfb] = -1;
  875. }
  876. l3_side.scfsi[ch][i] = 1;
  877. }
  878. }
  879. var s1 = 0;
  880. var c1 = 0;
  881. for (sfb = 0; sfb < 11; sfb++) {
  882. if (gi.scalefac[sfb] == -1)
  883. continue;
  884. c1++;
  885. if (s1 < gi.scalefac[sfb])
  886. s1 = gi.scalefac[sfb];
  887. }
  888. var s2 = 0;
  889. var c2 = 0;
  890. for (; sfb < Encoder.SBPSY_l; sfb++) {
  891. if (gi.scalefac[sfb] == -1)
  892. continue;
  893. c2++;
  894. if (s2 < gi.scalefac[sfb])
  895. s2 = gi.scalefac[sfb];
  896. }
  897. for (var i = 0; i < 16; i++) {
  898. if (s1 < slen1_n[i] && s2 < slen2_n[i]) {
  899. var c = slen1_tab[i] * c1 + slen2_tab[i] * c2;
  900. if (gi.part2_length > c) {
  901. gi.part2_length = c;
  902. gi.scalefac_compress = i;
  903. }
  904. }
  905. }
  906. }
  907. /**
  908. * Find the optimal way to store the scalefactors. Only call this routine
  909. * after final scalefactors have been chosen and the channel/granule will
  910. * not be re-encoded.
  911. */
  912. this.best_scalefac_store = function (gfc, gr, ch, l3_side) {
  913. /* use scalefac_scale if we can */
  914. var gi = l3_side.tt[gr][ch];
  915. var sfb, i, j, l;
  916. var recalc = 0;
  917. /*
  918. * remove scalefacs from bands with ix=0. This idea comes from the AAC
  919. * ISO docs. added mt 3/00
  920. */
  921. /* check if l3_enc=0 */
  922. j = 0;
  923. for (sfb = 0; sfb < gi.sfbmax; sfb++) {
  924. var width = gi.width[sfb];
  925. j += width;
  926. for (l = -width; l < 0; l++) {
  927. if (gi.l3_enc[l + j] != 0)
  928. break;
  929. }
  930. if (l == 0)
  931. gi.scalefac[sfb] = recalc = -2;
  932. /* anything goes. */
  933. /*
  934. * only best_scalefac_store and calc_scfsi know--and only they
  935. * should know--about the magic number -2.
  936. */
  937. }
  938. if (0 == gi.scalefac_scale && 0 == gi.preflag) {
  939. var s = 0;
  940. for (sfb = 0; sfb < gi.sfbmax; sfb++)
  941. if (gi.scalefac[sfb] > 0)
  942. s |= gi.scalefac[sfb];
  943. if (0 == (s & 1) && s != 0) {
  944. for (sfb = 0; sfb < gi.sfbmax; sfb++)
  945. if (gi.scalefac[sfb] > 0)
  946. gi.scalefac[sfb] >>= 1;
  947. gi.scalefac_scale = recalc = 1;
  948. }
  949. }
  950. if (0 == gi.preflag && gi.block_type != Encoder.SHORT_TYPE
  951. && gfc.mode_gr == 2) {
  952. for (sfb = 11; sfb < Encoder.SBPSY_l; sfb++)
  953. if (gi.scalefac[sfb] < qupvt.pretab[sfb]
  954. && gi.scalefac[sfb] != -2)
  955. break;
  956. if (sfb == Encoder.SBPSY_l) {
  957. for (sfb = 11; sfb < Encoder.SBPSY_l; sfb++)
  958. if (gi.scalefac[sfb] > 0)
  959. gi.scalefac[sfb] -= qupvt.pretab[sfb];
  960. gi.preflag = recalc = 1;
  961. }
  962. }
  963. for (i = 0; i < 4; i++)
  964. l3_side.scfsi[ch][i] = 0;
  965. if (gfc.mode_gr == 2 && gr == 1
  966. && l3_side.tt[0][ch].block_type != Encoder.SHORT_TYPE
  967. && l3_side.tt[1][ch].block_type != Encoder.SHORT_TYPE) {
  968. scfsi_calc(ch, l3_side);
  969. recalc = 0;
  970. }
  971. for (sfb = 0; sfb < gi.sfbmax; sfb++) {
  972. if (gi.scalefac[sfb] == -2) {
  973. gi.scalefac[sfb] = 0;
  974. /* if anything goes, then 0 is a good choice */
  975. }
  976. }
  977. if (recalc != 0) {
  978. if (gfc.mode_gr == 2) {
  979. this.scale_bitcount(gi);
  980. } else {
  981. this.scale_bitcount_lsf(gfc, gi);
  982. }
  983. }
  984. }
  985. //fix cc 精简
  986. /**
  987. * number of bits used to encode scalefacs.
  988. *
  989. * 18*slen1_tab[i] + 18*slen2_tab[i]
  990. */
  991. var scale_short = [0, 18, 36, 54, 54, 36, 54, 72,
  992. 54, 72, 90, 72, 90, 108, 108, 126];
  993. /**
  994. * number of bits used to encode scalefacs.
  995. *
  996. * 17*slen1_tab[i] + 18*slen2_tab[i]
  997. */
  998. var scale_mixed = [0, 18, 36, 54, 51, 35, 53, 71,
  999. 52, 70, 88, 69, 87, 105, 104, 122];
  1000. /**
  1001. * number of bits used to encode scalefacs.
  1002. *
  1003. * 11*slen1_tab[i] + 10*slen2_tab[i]
  1004. */
  1005. var scale_long = [0, 10, 20, 30, 33, 21, 31, 41, 32, 42,
  1006. 52, 43, 53, 63, 64, 74];
  1007. /**
  1008. * Also calculates the number of bits necessary to code the scalefactors.
  1009. */
  1010. this.scale_bitcount = function (cod_info) {
  1011. var k, sfb, max_slen1 = 0, max_slen2 = 0;
  1012. /* maximum values */
  1013. var tab;
  1014. var scalefac = cod_info.scalefac;
  1015. if (cod_info.block_type == Encoder.SHORT_TYPE) {
  1016. tab = scale_short;
  1017. if (cod_info.mixed_block_flag != 0)
  1018. tab = scale_mixed;
  1019. } else { /* block_type == 1,2,or 3 */
  1020. tab = scale_long;
  1021. if (0 == cod_info.preflag) {
  1022. for (sfb = 11; sfb < Encoder.SBPSY_l; sfb++)
  1023. if (scalefac[sfb] < qupvt.pretab[sfb])
  1024. break;
  1025. if (sfb == Encoder.SBPSY_l) {
  1026. cod_info.preflag = 1;
  1027. for (sfb = 11; sfb < Encoder.SBPSY_l; sfb++)
  1028. scalefac[sfb] -= qupvt.pretab[sfb];
  1029. }
  1030. }
  1031. }
  1032. for (sfb = 0; sfb < cod_info.sfbdivide; sfb++)
  1033. if (max_slen1 < scalefac[sfb])
  1034. max_slen1 = scalefac[sfb];
  1035. for (; sfb < cod_info.sfbmax; sfb++)
  1036. if (max_slen2 < scalefac[sfb])
  1037. max_slen2 = scalefac[sfb];
  1038. /*
  1039. * from Takehiro TOMINAGA <tominaga@isoternet.org> 10/99 loop over *all*
  1040. * posible values of scalefac_compress to find the one which uses the
  1041. * smallest number of bits. ISO would stop at first valid index
  1042. */
  1043. cod_info.part2_length = QuantizePVT.LARGE_BITS;
  1044. for (k = 0; k < 16; k++) {
  1045. if (max_slen1 < slen1_n[k] && max_slen2 < slen2_n[k]
  1046. && cod_info.part2_length > tab[k]) {
  1047. cod_info.part2_length = tab[k];
  1048. cod_info.scalefac_compress = k;
  1049. }
  1050. }
  1051. return cod_info.part2_length == QuantizePVT.LARGE_BITS;
  1052. }
  1053. /**
  1054. * table of largest scalefactor values for MPEG2
  1055. */
  1056. var max_range_sfac_tab = [[15, 15, 7, 7],
  1057. [15, 15, 7, 0], [7, 3, 0, 0], [15, 31, 31, 0],
  1058. [7, 7, 7, 0], [3, 3, 0, 0]];
  1059. /**
  1060. * Also counts the number of bits to encode the scalefacs but for MPEG 2
  1061. * Lower sampling frequencies (24, 22.05 and 16 kHz.)
  1062. *
  1063. * This is reverse-engineered from section 2.4.3.2 of the MPEG2 IS,
  1064. * "Audio Decoding Layer III"
  1065. */
  1066. this.scale_bitcount_lsf = function (gfc, cod_info) {
  1067. var table_number, row_in_table, partition, nr_sfb, window;
  1068. var over;
  1069. var i, sfb;
  1070. var max_sfac = new_int(4);
  1071. //var partition_table;
  1072. var scalefac = cod_info.scalefac;
  1073. /*
  1074. * Set partition table. Note that should try to use table one, but do
  1075. * not yet...
  1076. */
  1077. if (cod_info.preflag != 0)
  1078. table_number = 2;
  1079. else
  1080. table_number = 0;
  1081. for (i = 0; i < 4; i++)
  1082. max_sfac[i] = 0;
  1083. if (cod_info.block_type == Encoder.SHORT_TYPE) {
  1084. row_in_table = 1;
  1085. var partition_table = qupvt.nr_of_sfb_block[table_number][row_in_table];
  1086. for (sfb = 0, partition = 0; partition < 4; partition++) {
  1087. nr_sfb = partition_table[partition] / 3;
  1088. for (i = 0; i < nr_sfb; i++, sfb++)
  1089. for (window = 0; window < 3; window++)
  1090. if (scalefac[sfb * 3 + window] > max_sfac[partition])
  1091. max_sfac[partition] = scalefac[sfb * 3 + window];
  1092. }
  1093. } else {
  1094. row_in_table = 0;
  1095. var partition_table = qupvt.nr_of_sfb_block[table_number][row_in_table];
  1096. for (sfb = 0, partition = 0; partition < 4; partition++) {
  1097. nr_sfb = partition_table[partition];
  1098. for (i = 0; i < nr_sfb; i++, sfb++)
  1099. if (scalefac[sfb] > max_sfac[partition])
  1100. max_sfac[partition] = scalefac[sfb];
  1101. }
  1102. }
  1103. for (over = false, partition = 0; partition < 4; partition++) {
  1104. if (max_sfac[partition] > max_range_sfac_tab[table_number][partition])
  1105. over = true;
  1106. }
  1107. if (!over) {
  1108. var slen1, slen2, slen3, slen4;
  1109. cod_info.sfb_partition_table = qupvt.nr_of_sfb_block[table_number][row_in_table];
  1110. for (partition = 0; partition < 4; partition++)
  1111. cod_info.slen[partition] = log2tab[max_sfac[partition]];
  1112. /* set scalefac_compress */
  1113. slen1 = cod_info.slen[0];
  1114. slen2 = cod_info.slen[1];
  1115. slen3 = cod_info.slen[2];
  1116. slen4 = cod_info.slen[3];
  1117. switch (table_number) {
  1118. case 0:
  1119. cod_info.scalefac_compress = (((slen1 * 5) + slen2) << 4)
  1120. + (slen3 << 2) + slen4;
  1121. break;
  1122. case 1:
  1123. cod_info.scalefac_compress = 400 + (((slen1 * 5) + slen2) << 2)
  1124. + slen3;
  1125. break;
  1126. case 2:
  1127. cod_info.scalefac_compress = 500 + (slen1 * 3) + slen2;
  1128. break;
  1129. default:
  1130. //fix cc 精简 print
  1131. break;
  1132. }
  1133. }
  1134. if (!over) {
  1135. cod_info.part2_length = 0;
  1136. for (partition = 0; partition < 4; partition++)
  1137. cod_info.part2_length += cod_info.slen[partition]
  1138. * cod_info.sfb_partition_table[partition];
  1139. }
  1140. return over;
  1141. }
  1142. /*
  1143. * Since no bands have been over-amplified, we can set scalefac_compress and
  1144. * slen[] for the formatter
  1145. */
  1146. var log2tab = [0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4,
  1147. 4, 4, 4, 4];
  1148. this.huffman_init = function (gfc) {
  1149. for (var i = 2; i <= 576; i += 2) {
  1150. var scfb_anz = 0, bv_index;
  1151. while (gfc.scalefac_band.l[++scfb_anz] < i)
  1152. ;
  1153. bv_index = subdv_table[scfb_anz][0]; // .region0_count
  1154. while (gfc.scalefac_band.l[bv_index + 1] > i)
  1155. bv_index--;
  1156. if (bv_index < 0) {
  1157. /*
  1158. * this is an indication that everything is going to be encoded
  1159. * as region0: bigvalues < region0 < region1 so lets set
  1160. * region0, region1 to some value larger than bigvalues
  1161. */
  1162. bv_index = subdv_table[scfb_anz][0]; // .region0_count
  1163. }
  1164. gfc.bv_scf[i - 2] = bv_index;
  1165. bv_index = subdv_table[scfb_anz][1]; // .region1_count
  1166. while (gfc.scalefac_band.l[bv_index + gfc.bv_scf[i - 2] + 2] > i)
  1167. bv_index--;
  1168. if (bv_index < 0) {
  1169. bv_index = subdv_table[scfb_anz][1]; // .region1_count
  1170. }
  1171. gfc.bv_scf[i - 1] = bv_index;
  1172. }
  1173. }
  1174. }
  1175. /*
  1176. * ReplayGainAnalysis - analyzes input samples and give the recommended dB change
  1177. * Copyright (C) 2001 David Robinson and Glen Sawyer
  1178. * Improvements and optimizations added by Frank Klemm, and by Marcel Muller
  1179. *
  1180. * This library is free software; you can redistribute it and/or
  1181. * modify it under the terms of the GNU Lesser General Public
  1182. * License as published by the Free Software Foundation; either
  1183. * version 2.1 of the License, or (at your option) any later version.
  1184. *
  1185. * This library is distributed in the hope that it will be useful,
  1186. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1187. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1188. * Lesser General Public License for more details.
  1189. *
  1190. * You should have received a copy of the GNU Lesser General Public
  1191. * License along with this library; if not, write to the Free Software
  1192. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  1193. *
  1194. * concept and filter values by David Robinson (David@Robinson.org)
  1195. * -- blame him if you think the idea is flawed
  1196. * original coding by Glen Sawyer (mp3gain@hotmail.com)
  1197. * -- blame him if you think this runs too slowly, or the coding is otherwise flawed
  1198. *
  1199. * lots of code improvements by Frank Klemm ( http://www.uni-jena.de/~pfk/mpp/ )
  1200. * -- credit him for all the _good_ programming ;)
  1201. *
  1202. *
  1203. * For an explanation of the concepts and the basic algorithms involved, go to:
  1204. * http://www.replaygain.org/
  1205. */
  1206. /*
  1207. * Here's the deal. Call
  1208. *
  1209. * InitGainAnalysis ( long samplefreq );
  1210. *
  1211. * to initialize everything. Call
  1212. *
  1213. * AnalyzeSamples ( var Float_t* left_samples,
  1214. * var Float_t* right_samples,
  1215. * size_t num_samples,
  1216. * int num_channels );
  1217. *
  1218. * as many times as you want, with as many or as few samples as you want.
  1219. * If mono, pass the sample buffer in through left_samples, leave
  1220. * right_samples NULL, and make sure num_channels = 1.
  1221. *
  1222. * GetTitleGain()
  1223. *
  1224. * will return the recommended dB level change for all samples analyzed
  1225. * SINCE THE LAST TIME you called GetTitleGain() OR InitGainAnalysis().
  1226. *
  1227. * GetAlbumGain()
  1228. *
  1229. * will return the recommended dB level change for all samples analyzed
  1230. * since InitGainAnalysis() was called and finalized with GetTitleGain().
  1231. *
  1232. * Pseudo-code to process an album:
  1233. *
  1234. * Float_t l_samples [4096];
  1235. * Float_t r_samples [4096];
  1236. * size_t num_samples;
  1237. * unsigned int num_songs;
  1238. * unsigned int i;
  1239. *
  1240. * InitGainAnalysis ( 44100 );
  1241. * for ( i = 1; i <= num_songs; i++ ) {
  1242. * while ( ( num_samples = getSongSamples ( song[i], left_samples, right_samples ) ) > 0 )
  1243. * AnalyzeSamples ( left_samples, right_samples, num_samples, 2 );
  1244. * fprintf ("Recommended dB change for song %2d: %+6.2 dB\n", i, GetTitleGain() );
  1245. * }
  1246. * fprintf ("Recommended dB change for whole album: %+6.2 dB\n", GetAlbumGain() );
  1247. */
  1248. /*
  1249. * So here's the main source of potential code confusion:
  1250. *
  1251. * The filters applied to the incoming samples are IIR filters,
  1252. * meaning they rely on up to <filter order> number of previous samples
  1253. * AND up to <filter order> number of previous filtered samples.
  1254. *
  1255. * I set up the AnalyzeSamples routine to minimize memory usage and interface
  1256. * complexity. The speed isn't compromised too much (I don't think), but the
  1257. * internal complexity is higher than it should be for such a relatively
  1258. * simple routine.
  1259. *
  1260. * Optimization/clarity suggestions are welcome.
  1261. */
  1262. /**
  1263. * Table entries per dB
  1264. */
  1265. GainAnalysis.STEPS_per_dB = 100.;
  1266. /**
  1267. * Table entries for 0...MAX_dB (normal max. values are 70...80 dB)
  1268. */
  1269. GainAnalysis.MAX_dB = 120.;
  1270. GainAnalysis.GAIN_NOT_ENOUGH_SAMPLES = -24601;
  1271. GainAnalysis.GAIN_ANALYSIS_ERROR = 0;
  1272. GainAnalysis.GAIN_ANALYSIS_OK = 1;
  1273. GainAnalysis.INIT_GAIN_ANALYSIS_ERROR = 0;
  1274. GainAnalysis.INIT_GAIN_ANALYSIS_OK = 1;
  1275. GainAnalysis.YULE_ORDER = 10;
  1276. GainAnalysis.MAX_ORDER = GainAnalysis.YULE_ORDER;
  1277. GainAnalysis.MAX_SAMP_FREQ = 48000;
  1278. GainAnalysis.RMS_WINDOW_TIME_NUMERATOR = 1;
  1279. GainAnalysis.RMS_WINDOW_TIME_DENOMINATOR = 20;
  1280. GainAnalysis.MAX_SAMPLES_PER_WINDOW = ((GainAnalysis.MAX_SAMP_FREQ * GainAnalysis.RMS_WINDOW_TIME_NUMERATOR) / GainAnalysis.RMS_WINDOW_TIME_DENOMINATOR + 1);
  1281. function GainAnalysis() {
  1282. //fix 精简
  1283. }
  1284. function Presets() {
  1285. //fix 精简
  1286. function ABRPresets(kbps, comp, compS,
  1287. joint, fix, shThreshold,
  1288. shThresholdS, bass, sc,
  1289. mask, lower, curve,
  1290. interCh, sfScale) {
  1291. this.quant_comp = comp;
  1292. this.quant_comp_s = compS;
  1293. this.safejoint = joint;
  1294. this.nsmsfix = fix;
  1295. this.st_lrm = shThreshold;
  1296. this.st_s = shThresholdS;
  1297. this.nsbass = bass;
  1298. this.scale = sc;
  1299. this.masking_adj = mask;
  1300. this.ath_lower = lower;
  1301. this.ath_curve = curve;
  1302. this.interch = interCh;
  1303. this.sfscale = sfScale;
  1304. }
  1305. var lame;
  1306. this.setModules = function (_lame) {
  1307. lame = _lame;
  1308. };
  1309. //fix cc 精简
  1310. function apply_vbr_preset(gfp, a, enforce) {
  1311. abort();//fix cc 精简
  1312. }
  1313. /**
  1314. * <PRE>
  1315. * Switch mappings for ABR mode
  1316. *
  1317. * kbps quant q_s safejoint nsmsfix st_lrm st_s ns-bass scale msk ath_lwr ath_curve interch , sfscale
  1318. * </PRE>
  1319. */
  1320. var abr_switch_map = [
  1321. new ABRPresets(8, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -30.0, 11, 0.0012, 1), /* 8, impossible to use in stereo */
  1322. new ABRPresets(16, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -25.0, 11, 0.0010, 1), /* 16 */
  1323. new ABRPresets(24, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -20.0, 11, 0.0010, 1), /* 24 */
  1324. new ABRPresets(32, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -15.0, 11, 0.0010, 1), /* 32 */
  1325. new ABRPresets(40, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -10.0, 11, 0.0009, 1), /* 40 */
  1326. new ABRPresets(48, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -10.0, 11, 0.0009, 1), /* 48 */
  1327. new ABRPresets(56, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -6.0, 11, 0.0008, 1), /* 56 */
  1328. new ABRPresets(64, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -2.0, 11, 0.0008, 1), /* 64 */
  1329. new ABRPresets(80, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, .0, 8, 0.0007, 1), /* 80 */
  1330. new ABRPresets(96, 9, 9, 0, 2.50, 6.60, 145, 0, 0.95, 0, 1.0, 5.5, 0.0006, 1), /* 96 */
  1331. new ABRPresets(112, 9, 9, 0, 2.25, 6.60, 145, 0, 0.95, 0, 2.0, 4.5, 0.0005, 1), /* 112 */
  1332. new ABRPresets(128, 9, 9, 0, 1.95, 6.40, 140, 0, 0.95, 0, 3.0, 4, 0.0002, 1), /* 128 */
  1333. new ABRPresets(160, 9, 9, 1, 1.79, 6.00, 135, 0, 0.95, -2, 5.0, 3.5, 0, 1), /* 160 */
  1334. new ABRPresets(192, 9, 9, 1, 1.49, 5.60, 125, 0, 0.97, -4, 7.0, 3, 0, 0), /* 192 */
  1335. new ABRPresets(224, 9, 9, 1, 1.25, 5.20, 125, 0, 0.98, -6, 9.0, 2, 0, 0), /* 224 */
  1336. new ABRPresets(256, 9, 9, 1, 0.97, 5.20, 125, 0, 1.00, -8, 10.0, 1, 0, 0), /* 256 */
  1337. new ABRPresets(320, 9, 9, 1, 0.90, 5.20, 125, 0, 1.00, -10, 12.0, 0, 0, 0) /* 320 */
  1338. ];
  1339. function apply_abr_preset(gfp, preset, enforce) {
  1340. /* Variables for the ABR stuff */
  1341. var actual_bitrate = preset;
  1342. var r = lame.nearestBitrateFullIndex(preset);
  1343. gfp.VBR = VbrMode.vbr_abr;
  1344. gfp.VBR_mean_bitrate_kbps = actual_bitrate;
  1345. gfp.VBR_mean_bitrate_kbps = Math.min(gfp.VBR_mean_bitrate_kbps, 320);
  1346. gfp.VBR_mean_bitrate_kbps = Math.max(gfp.VBR_mean_bitrate_kbps, 8);
  1347. gfp.brate = gfp.VBR_mean_bitrate_kbps;
  1348. if (gfp.VBR_mean_bitrate_kbps > 320) {
  1349. gfp.disable_reservoir = true;
  1350. }
  1351. /* parameters for which there is no proper set/get interface */
  1352. if (abr_switch_map[r].safejoint > 0)
  1353. gfp.exp_nspsytune = gfp.exp_nspsytune | 2;
  1354. /* safejoint */
  1355. if (abr_switch_map[r].sfscale > 0) {
  1356. gfp.internal_flags.noise_shaping = 2;
  1357. }
  1358. /* ns-bass tweaks */
  1359. if (Math.abs(abr_switch_map[r].nsbass) > 0) {
  1360. var k = (int)(abr_switch_map[r].nsbass * 4);
  1361. if (k < 0)
  1362. k += 64;
  1363. gfp.exp_nspsytune = gfp.exp_nspsytune | (k << 2);
  1364. }
  1365. if (enforce != 0)
  1366. gfp.quant_comp = abr_switch_map[r].quant_comp;
  1367. else if (!(Math.abs(gfp.quant_comp - -1) > 0))
  1368. gfp.quant_comp = abr_switch_map[r].quant_comp;
  1369. // SET_OPTION(quant_comp, abr_switch_map[r].quant_comp, -1);
  1370. if (enforce != 0)
  1371. gfp.quant_comp_short = abr_switch_map[r].quant_comp_s;
  1372. else if (!(Math.abs(gfp.quant_comp_short - -1) > 0))
  1373. gfp.quant_comp_short = abr_switch_map[r].quant_comp_s;
  1374. // SET_OPTION(quant_comp_short, abr_switch_map[r].quant_comp_s, -1);
  1375. if (enforce != 0)
  1376. gfp.msfix = abr_switch_map[r].nsmsfix;
  1377. else if (!(Math.abs(gfp.msfix - -1) > 0))
  1378. gfp.msfix = abr_switch_map[r].nsmsfix;
  1379. // SET_OPTION(msfix, abr_switch_map[r].nsmsfix, -1);
  1380. if (enforce != 0)
  1381. gfp.internal_flags.nsPsy.attackthre = abr_switch_map[r].st_lrm;
  1382. else if (!(Math.abs(gfp.internal_flags.nsPsy.attackthre - -1) > 0))
  1383. gfp.internal_flags.nsPsy.attackthre = abr_switch_map[r].st_lrm;
  1384. // SET_OPTION(short_threshold_lrm, abr_switch_map[r].st_lrm, -1);
  1385. if (enforce != 0)
  1386. gfp.internal_flags.nsPsy.attackthre_s = abr_switch_map[r].st_s;
  1387. else if (!(Math.abs(gfp.internal_flags.nsPsy.attackthre_s - -1) > 0))
  1388. gfp.internal_flags.nsPsy.attackthre_s = abr_switch_map[r].st_s;
  1389. // SET_OPTION(short_threshold_s, abr_switch_map[r].st_s, -1);
  1390. /*
  1391. * ABR seems to have big problems with clipping, especially at low
  1392. * bitrates
  1393. */
  1394. /*
  1395. * so we compensate for that here by using a scale value depending on
  1396. * bitrate
  1397. */
  1398. if (enforce != 0)
  1399. gfp.scale = abr_switch_map[r].scale;
  1400. else if (!(Math.abs(gfp.scale - -1) > 0))
  1401. gfp.scale = abr_switch_map[r].scale;
  1402. // SET_OPTION(scale, abr_switch_map[r].scale, -1);
  1403. if (enforce != 0)
  1404. gfp.maskingadjust = abr_switch_map[r].masking_adj;
  1405. else if (!(Math.abs(gfp.maskingadjust - 0) > 0))
  1406. gfp.maskingadjust = abr_switch_map[r].masking_adj;
  1407. // SET_OPTION(maskingadjust, abr_switch_map[r].masking_adj, 0);
  1408. if (abr_switch_map[r].masking_adj > 0) {
  1409. if (enforce != 0)
  1410. gfp.maskingadjust_short = (abr_switch_map[r].masking_adj * .9);
  1411. else if (!(Math.abs(gfp.maskingadjust_short - 0) > 0))
  1412. gfp.maskingadjust_short = (abr_switch_map[r].masking_adj * .9);
  1413. // SET_OPTION(maskingadjust_short, abr_switch_map[r].masking_adj *
  1414. // .9, 0);
  1415. } else {
  1416. if (enforce != 0)
  1417. gfp.maskingadjust_short = (abr_switch_map[r].masking_adj * 1.1);
  1418. else if (!(Math.abs(gfp.maskingadjust_short - 0) > 0))
  1419. gfp.maskingadjust_short = (abr_switch_map[r].masking_adj * 1.1);
  1420. // SET_OPTION(maskingadjust_short, abr_switch_map[r].masking_adj *
  1421. // 1.1, 0);
  1422. }
  1423. if (enforce != 0)
  1424. gfp.ATHlower = -abr_switch_map[r].ath_lower / 10.;
  1425. else if (!(Math.abs((-gfp.ATHlower * 10.) - 0) > 0))
  1426. gfp.ATHlower = -abr_switch_map[r].ath_lower / 10.;
  1427. // SET_OPTION(ATHlower, abr_switch_map[r].ath_lower, 0);
  1428. if (enforce != 0)
  1429. gfp.ATHcurve = abr_switch_map[r].ath_curve;
  1430. else if (!(Math.abs(gfp.ATHcurve - -1) > 0))
  1431. gfp.ATHcurve = abr_switch_map[r].ath_curve;
  1432. // SET_OPTION(ATHcurve, abr_switch_map[r].ath_curve, -1);
  1433. if (enforce != 0)
  1434. gfp.interChRatio = abr_switch_map[r].interch;
  1435. else if (!(Math.abs(gfp.interChRatio - -1) > 0))
  1436. gfp.interChRatio = abr_switch_map[r].interch;
  1437. // SET_OPTION(interChRatio, abr_switch_map[r].interch, -1);
  1438. return preset;
  1439. }
  1440. this.apply_preset = function(gfp, preset, enforce) {
  1441. /* translate legacy presets */
  1442. switch (preset) {
  1443. case Lame.R3MIX:
  1444. {
  1445. preset = Lame.V3;
  1446. gfp.VBR = VbrMode.vbr_mtrh;
  1447. break;
  1448. }
  1449. case Lame.MEDIUM:
  1450. {
  1451. preset = Lame.V4;
  1452. gfp.VBR = VbrMode.vbr_rh;
  1453. break;
  1454. }
  1455. case Lame.MEDIUM_FAST:
  1456. {
  1457. preset = Lame.V4;
  1458. gfp.VBR = VbrMode.vbr_mtrh;
  1459. break;
  1460. }
  1461. case Lame.STANDARD:
  1462. {
  1463. preset = Lame.V2;
  1464. gfp.VBR = VbrMode.vbr_rh;
  1465. break;
  1466. }
  1467. case Lame.STANDARD_FAST:
  1468. {
  1469. preset = Lame.V2;
  1470. gfp.VBR = VbrMode.vbr_mtrh;
  1471. break;
  1472. }
  1473. case Lame.EXTREME:
  1474. {
  1475. preset = Lame.V0;
  1476. gfp.VBR = VbrMode.vbr_rh;
  1477. break;
  1478. }
  1479. case Lame.EXTREME_FAST:
  1480. {
  1481. preset = Lame.V0;
  1482. gfp.VBR = VbrMode.vbr_mtrh;
  1483. break;
  1484. }
  1485. case Lame.INSANE:
  1486. {
  1487. preset = 320;
  1488. gfp.preset = preset;
  1489. apply_abr_preset(gfp, preset, enforce);
  1490. gfp.VBR = VbrMode.vbr_off;
  1491. return preset;
  1492. }
  1493. }
  1494. gfp.preset = preset;
  1495. {
  1496. switch (preset) {
  1497. case Lame.V9:
  1498. apply_vbr_preset(gfp, 9, enforce);
  1499. return preset;
  1500. case Lame.V8:
  1501. apply_vbr_preset(gfp, 8, enforce);
  1502. return preset;
  1503. case Lame.V7:
  1504. apply_vbr_preset(gfp, 7, enforce);
  1505. return preset;
  1506. case Lame.V6:
  1507. apply_vbr_preset(gfp, 6, enforce);
  1508. return preset;
  1509. case Lame.V5:
  1510. apply_vbr_preset(gfp, 5, enforce);
  1511. return preset;
  1512. case Lame.V4:
  1513. apply_vbr_preset(gfp, 4, enforce);
  1514. return preset;
  1515. case Lame.V3:
  1516. apply_vbr_preset(gfp, 3, enforce);
  1517. return preset;
  1518. case Lame.V2:
  1519. apply_vbr_preset(gfp, 2, enforce);
  1520. return preset;
  1521. case Lame.V1:
  1522. apply_vbr_preset(gfp, 1, enforce);
  1523. return preset;
  1524. case Lame.V0:
  1525. apply_vbr_preset(gfp, 0, enforce);
  1526. return preset;
  1527. default:
  1528. break;
  1529. }
  1530. }
  1531. if (8 <= preset && preset <= 320) {
  1532. return apply_abr_preset(gfp, preset, enforce);
  1533. }
  1534. /* no corresponding preset found */
  1535. gfp.preset = 0;
  1536. return preset;
  1537. }
  1538. // Rest from getset.c:
  1539. //fix cc 精简
  1540. }
  1541. /*
  1542. * bit reservoir source file
  1543. *
  1544. * Copyright (c) 1999-2000 Mark Taylor
  1545. *
  1546. * This library is free software; you can redistribute it and/or
  1547. * modify it under the terms of the GNU Lesser General Public
  1548. * License as published by the Free Software Foundation; either
  1549. * version 2 of the License, or (at your option) any later version.
  1550. *
  1551. * This library is distributed in the hope that it will be useful,
  1552. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1553. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1554. * Library General Public License for more details.
  1555. *
  1556. * You should have received a copy of the GNU Lesser General Public
  1557. * License along with this library; if not, write to the
  1558. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  1559. * Boston, MA 02111-1307, USA.
  1560. */
  1561. /* $Id: Reservoir.java,v 1.9 2011/05/24 20:48:06 kenchis Exp $ */
  1562. //package mp3;
  1563. /**
  1564. * ResvFrameBegin:<BR>
  1565. * Called (repeatedly) at the beginning of a frame. Updates the maximum size of
  1566. * the reservoir, and checks to make sure main_data_begin was set properly by
  1567. * the formatter<BR>
  1568. * Background information:
  1569. *
  1570. * This is the original text from the ISO standard. Because of sooo many bugs
  1571. * and irritations correcting comments are added in brackets []. A '^W' means
  1572. * you should remove the last word.
  1573. *
  1574. * <PRE>
  1575. * 1. The following rule can be used to calculate the maximum
  1576. * number of bits used for one granule [^W frame]:<BR>
  1577. * At the highest possible bitrate of Layer III (320 kbps
  1578. * per stereo signal [^W^W^W], 48 kHz) the frames must be of
  1579. * [^W^W^W are designed to have] constant length, i.e.
  1580. * one buffer [^W^W the frame] length is:<BR>
  1581. *
  1582. * 320 kbps * 1152/48 kHz = 7680 bit = 960 byte
  1583. *
  1584. * This value is used as the maximum buffer per channel [^W^W] at
  1585. * lower bitrates [than 320 kbps]. At 64 kbps mono or 128 kbps
  1586. * stereo the main granule length is 64 kbps * 576/48 kHz = 768 bit
  1587. * [per granule and channel] at 48 kHz sampling frequency.
  1588. * This means that there is a maximum deviation (short time buffer
  1589. * [= reservoir]) of 7680 - 2*2*768 = 4608 bits is allowed at 64 kbps.
  1590. * The actual deviation is equal to the number of bytes [with the
  1591. * meaning of octets] denoted by the main_data_end offset pointer.
  1592. * The actual maximum deviation is (2^9-1)*8 bit = 4088 bits
  1593. * [for MPEG-1 and (2^8-1)*8 bit for MPEG-2, both are hard limits].
  1594. * ... The xchange of buffer bits between the left and right channel
  1595. * is allowed without restrictions [exception: dual channel].
  1596. * Because of the [constructed] constraint on the buffer size
  1597. * main_data_end is always set to 0 in the case of bit_rate_index==14,
  1598. * i.e. data rate 320 kbps per stereo signal [^W^W^W]. In this case
  1599. * all data are allocated between adjacent header [^W sync] words
  1600. * [, i.e. there is no buffering at all].
  1601. * </PRE>
  1602. */
  1603. function Reservoir() {
  1604. var bs;
  1605. this.setModules = function(_bs) {
  1606. bs = _bs;
  1607. }
  1608. this.ResvFrameBegin = function(gfp, mean_bits) {
  1609. var gfc = gfp.internal_flags;
  1610. var maxmp3buf;
  1611. var l3_side = gfc.l3_side;
  1612. var frameLength = bs.getframebits(gfp);
  1613. mean_bits.bits = (frameLength - gfc.sideinfo_len * 8) / gfc.mode_gr;
  1614. /**
  1615. * <PRE>
  1616. * Meaning of the variables:
  1617. * resvLimit: (0, 8, ..., 8*255 (MPEG-2), 8*511 (MPEG-1))
  1618. * Number of bits can be stored in previous frame(s) due to
  1619. * counter size constaints
  1620. * maxmp3buf: ( ??? ... 8*1951 (MPEG-1 and 2), 8*2047 (MPEG-2.5))
  1621. * Number of bits allowed to encode one frame (you can take 8*511 bit
  1622. * from the bit reservoir and at most 8*1440 bit from the current
  1623. * frame (320 kbps, 32 kHz), so 8*1951 bit is the largest possible
  1624. * value for MPEG-1 and -2)
  1625. *
  1626. * maximum allowed granule/channel size times 4 = 8*2047 bits.,
  1627. * so this is the absolute maximum supported by the format.
  1628. *
  1629. *
  1630. * fullFrameBits: maximum number of bits available for encoding
  1631. * the current frame.
  1632. *
  1633. * mean_bits: target number of bits per granule.
  1634. *
  1635. * frameLength:
  1636. *
  1637. * gfc.ResvMax: maximum allowed reservoir
  1638. *
  1639. * gfc.ResvSize: current reservoir size
  1640. *
  1641. * l3_side.resvDrain_pre:
  1642. * ancillary data to be added to previous frame:
  1643. * (only usefull in VBR modes if it is possible to have
  1644. * maxmp3buf < fullFrameBits)). Currently disabled,
  1645. * see #define NEW_DRAIN
  1646. * 2010-02-13: RH now enabled, it seems to be needed for CBR too,
  1647. * as there exists one example, where the FhG decoder
  1648. * can't decode a -b320 CBR file anymore.
  1649. *
  1650. * l3_side.resvDrain_post:
  1651. * ancillary data to be added to this frame:
  1652. *
  1653. * </PRE>
  1654. */
  1655. /* main_data_begin has 9 bits in MPEG-1, 8 bits MPEG-2 */
  1656. var resvLimit = (8 * 256) * gfc.mode_gr - 8;
  1657. /*
  1658. * maximum allowed frame size. dont use more than this number of bits,
  1659. * even if the frame has the space for them:
  1660. */
  1661. if (gfp.brate > 320) {
  1662. abort();//fix cc 精简
  1663. } else {
  1664. /*
  1665. * all mp3 decoders should have enough buffer to handle this value:
  1666. * size of a 320kbps 32kHz frame
  1667. */
  1668. maxmp3buf = 8 * 1440;
  1669. /*
  1670. * Bouvigne suggests this more lax interpretation of the ISO doc
  1671. * instead of using 8*960.
  1672. */
  1673. if (gfp.strict_ISO) {
  1674. abort();//fix cc 精简
  1675. }
  1676. }
  1677. gfc.ResvMax = maxmp3buf - frameLength;
  1678. if (gfc.ResvMax > resvLimit)
  1679. gfc.ResvMax = resvLimit;
  1680. if (gfc.ResvMax < 0 || gfp.disable_reservoir)
  1681. gfc.ResvMax = 0;
  1682. var fullFrameBits = mean_bits.bits * gfc.mode_gr
  1683. + Math.min(gfc.ResvSize, gfc.ResvMax);
  1684. if (fullFrameBits > maxmp3buf)
  1685. fullFrameBits = maxmp3buf;
  1686. l3_side.resvDrain_pre = 0;
  1687. // frame analyzer code
  1688. if (gfc.pinfo != null) {
  1689. abort();//fix cc 精简
  1690. }
  1691. return fullFrameBits;
  1692. }
  1693. /**
  1694. * returns targ_bits: target number of bits to use for 1 granule<BR>
  1695. * extra_bits: amount extra available from reservoir<BR>
  1696. * Mark Taylor 4/99
  1697. */
  1698. this.ResvMaxBits = function(gfp, mean_bits, targ_bits, cbr) {
  1699. var gfc = gfp.internal_flags;
  1700. var add_bits;
  1701. var ResvSize = gfc.ResvSize, ResvMax = gfc.ResvMax;
  1702. /* compensate the saved bits used in the 1st granule */
  1703. if (cbr != 0)
  1704. ResvSize += mean_bits;
  1705. if ((gfc.substep_shaping & 1) != 0)
  1706. ResvMax *= 0.9;
  1707. targ_bits.bits = mean_bits;
  1708. /* extra bits if the reservoir is almost full */
  1709. if (ResvSize * 10 > ResvMax * 9) {
  1710. add_bits = ResvSize - (ResvMax * 9) / 10;
  1711. targ_bits.bits += add_bits;
  1712. gfc.substep_shaping |= 0x80;
  1713. } else {
  1714. add_bits = 0;
  1715. gfc.substep_shaping &= 0x7f;
  1716. /*
  1717. * build up reservoir. this builds the reservoir a little slower
  1718. * than FhG. It could simple be mean_bits/15, but this was rigged to
  1719. * always produce 100 (the old value) at 128kbs
  1720. */
  1721. if (!gfp.disable_reservoir && 0 == (gfc.substep_shaping & 1))
  1722. targ_bits.bits -= .1 * mean_bits;
  1723. }
  1724. /* amount from the reservoir we are allowed to use. ISO says 6/10 */
  1725. var extra_bits = (ResvSize < (gfc.ResvMax * 6) / 10 ? ResvSize
  1726. : (gfc.ResvMax * 6) / 10);
  1727. extra_bits -= add_bits;
  1728. if (extra_bits < 0)
  1729. extra_bits = 0;
  1730. return extra_bits;
  1731. }
  1732. /**
  1733. * Called after a granule's bit allocation. Readjusts the size of the
  1734. * reservoir to reflect the granule's usage.
  1735. */
  1736. this.ResvAdjust = function(gfc, gi) {
  1737. gfc.ResvSize -= gi.part2_3_length + gi.part2_length;
  1738. }
  1739. /**
  1740. * Called after all granules in a frame have been allocated. Makes sure that
  1741. * the reservoir size is within limits, possibly by adding stuffing bits.
  1742. */
  1743. this.ResvFrameEnd = function(gfc, mean_bits) {
  1744. var over_bits;
  1745. var l3_side = gfc.l3_side;
  1746. gfc.ResvSize += mean_bits * gfc.mode_gr;
  1747. var stuffingBits = 0;
  1748. l3_side.resvDrain_post = 0;
  1749. l3_side.resvDrain_pre = 0;
  1750. /* we must be byte aligned */
  1751. if ((over_bits = gfc.ResvSize % 8) != 0)
  1752. stuffingBits += over_bits;
  1753. over_bits = (gfc.ResvSize - stuffingBits) - gfc.ResvMax;
  1754. if (over_bits > 0) {
  1755. stuffingBits += over_bits;
  1756. }
  1757. /*
  1758. * NOTE: enabling the NEW_DRAIN code fixes some problems with FhG
  1759. * decoder shipped with MS Windows operating systems. Using this, it is
  1760. * even possible to use Gabriel's lax buffer consideration again, which
  1761. * assumes, any decoder should have a buffer large enough for a 320 kbps
  1762. * frame at 32 kHz sample rate.
  1763. *
  1764. * old drain code: lame -b320 BlackBird.wav --. does not play with
  1765. * GraphEdit.exe using FhG decoder V1.5 Build 50
  1766. *
  1767. * new drain code: lame -b320 BlackBird.wav --. plays fine with
  1768. * GraphEdit.exe using FhG decoder V1.5 Build 50
  1769. *
  1770. * Robert Hegemann, 2010-02-13.
  1771. */
  1772. /*
  1773. * drain as many bits as possible into previous frame ancillary data In
  1774. * particular, in VBR mode ResvMax may have changed, and we have to make
  1775. * sure main_data_begin does not create a reservoir bigger than ResvMax
  1776. * mt 4/00
  1777. */
  1778. {
  1779. var mdb_bytes = Math.min(l3_side.main_data_begin * 8, stuffingBits) / 8;
  1780. l3_side.resvDrain_pre += 8 * mdb_bytes;
  1781. stuffingBits -= 8 * mdb_bytes;
  1782. gfc.ResvSize -= 8 * mdb_bytes;
  1783. l3_side.main_data_begin -= mdb_bytes;
  1784. }
  1785. /* drain the rest into this frames ancillary data */
  1786. l3_side.resvDrain_post += stuffingBits;
  1787. gfc.ResvSize -= stuffingBits;
  1788. }
  1789. }
  1790. /**
  1791. * A Vbr header may be present in the ancillary data field of the first frame of
  1792. * an mp3 bitstream<BR>
  1793. * The Vbr header (optionally) contains
  1794. * <UL>
  1795. * <LI>frames total number of audio frames in the bitstream
  1796. * <LI>bytes total number of bytes in the bitstream
  1797. * <LI>toc table of contents
  1798. * </UL>
  1799. *
  1800. * toc (table of contents) gives seek points for random access.<BR>
  1801. * The ith entry determines the seek point for i-percent duration.<BR>
  1802. * seek point in bytes = (toc[i]/256.0) * total_bitstream_bytes<BR>
  1803. * e.g. half duration seek point = (toc[50]/256.0) * total_bitstream_bytes
  1804. */
  1805. VBRTag.NUMTOCENTRIES = 100;
  1806. VBRTag.MAXFRAMESIZE = 2880;
  1807. function VBRTag() {
  1808. var lame;
  1809. var bs;
  1810. var v;
  1811. this.setModules = function (_lame, _bs, _v) {
  1812. lame = _lame;
  1813. bs = _bs;
  1814. v = _v;
  1815. };
  1816. //fix 精简
  1817. /**
  1818. * Lookup table for fast CRC-16 computation. Uses the polynomial
  1819. * x^16+x^15+x^2+1
  1820. */
  1821. var crc16Lookup = [0x0000, 0xC0C1, 0xC181, 0x0140,
  1822. 0xC301, 0x03C0, 0x0280, 0xC241, 0xC601, 0x06C0, 0x0780, 0xC741,
  1823. 0x0500, 0xC5C1, 0xC481, 0x0440, 0xCC01, 0x0CC0, 0x0D80, 0xCD41,
  1824. 0x0F00, 0xCFC1, 0xCE81, 0x0E40, 0x0A00, 0xCAC1, 0xCB81, 0x0B40,
  1825. 0xC901, 0x09C0, 0x0880, 0xC841, 0xD801, 0x18C0, 0x1980, 0xD941,
  1826. 0x1B00, 0xDBC1, 0xDA81, 0x1A40, 0x1E00, 0xDEC1, 0xDF81, 0x1F40,
  1827. 0xDD01, 0x1DC0, 0x1C80, 0xDC41, 0x1400, 0xD4C1, 0xD581, 0x1540,
  1828. 0xD701, 0x17C0, 0x1680, 0xD641, 0xD201, 0x12C0, 0x1380, 0xD341,
  1829. 0x1100, 0xD1C1, 0xD081, 0x1040, 0xF001, 0x30C0, 0x3180, 0xF141,
  1830. 0x3300, 0xF3C1, 0xF281, 0x3240, 0x3600, 0xF6C1, 0xF781, 0x3740,
  1831. 0xF501, 0x35C0, 0x3480, 0xF441, 0x3C00, 0xFCC1, 0xFD81, 0x3D40,
  1832. 0xFF01, 0x3FC0, 0x3E80, 0xFE41, 0xFA01, 0x3AC0, 0x3B80, 0xFB41,
  1833. 0x3900, 0xF9C1, 0xF881, 0x3840, 0x2800, 0xE8C1, 0xE981, 0x2940,
  1834. 0xEB01, 0x2BC0, 0x2A80, 0xEA41, 0xEE01, 0x2EC0, 0x2F80, 0xEF41,
  1835. 0x2D00, 0xEDC1, 0xEC81, 0x2C40, 0xE401, 0x24C0, 0x2580, 0xE541,
  1836. 0x2700, 0xE7C1, 0xE681, 0x2640, 0x2200, 0xE2C1, 0xE381, 0x2340,
  1837. 0xE101, 0x21C0, 0x2080, 0xE041, 0xA001, 0x60C0, 0x6180, 0xA141,
  1838. 0x6300, 0xA3C1, 0xA281, 0x6240, 0x6600, 0xA6C1, 0xA781, 0x6740,
  1839. 0xA501, 0x65C0, 0x6480, 0xA441, 0x6C00, 0xACC1, 0xAD81, 0x6D40,
  1840. 0xAF01, 0x6FC0, 0x6E80, 0xAE41, 0xAA01, 0x6AC0, 0x6B80, 0xAB41,
  1841. 0x6900, 0xA9C1, 0xA881, 0x6840, 0x7800, 0xB8C1, 0xB981, 0x7940,
  1842. 0xBB01, 0x7BC0, 0x7A80, 0xBA41, 0xBE01, 0x7EC0, 0x7F80, 0xBF41,
  1843. 0x7D00, 0xBDC1, 0xBC81, 0x7C40, 0xB401, 0x74C0, 0x7580, 0xB541,
  1844. 0x7700, 0xB7C1, 0xB681, 0x7640, 0x7200, 0xB2C1, 0xB381, 0x7340,
  1845. 0xB101, 0x71C0, 0x7080, 0xB041, 0x5000, 0x90C1, 0x9181, 0x5140,
  1846. 0x9301, 0x53C0, 0x5280, 0x9241, 0x9601, 0x56C0, 0x5780, 0x9741,
  1847. 0x5500, 0x95C1, 0x9481, 0x5440, 0x9C01, 0x5CC0, 0x5D80, 0x9D41,
  1848. 0x5F00, 0x9FC1, 0x9E81, 0x5E40, 0x5A00, 0x9AC1, 0x9B81, 0x5B40,
  1849. 0x9901, 0x59C0, 0x5880, 0x9841, 0x8801, 0x48C0, 0x4980, 0x8941,
  1850. 0x4B00, 0x8BC1, 0x8A81, 0x4A40, 0x4E00, 0x8EC1, 0x8F81, 0x4F40,
  1851. 0x8D01, 0x4DC0, 0x4C80, 0x8C41, 0x4400, 0x84C1, 0x8581, 0x4540,
  1852. 0x8701, 0x47C0, 0x4680, 0x8641, 0x8201, 0x42C0, 0x4380, 0x8341,
  1853. 0x4100, 0x81C1, 0x8081, 0x4040];
  1854. //fix 精简
  1855. /**
  1856. * Fast CRC-16 computation (uses table crc16Lookup).
  1857. *
  1858. * @param value
  1859. * @param crc
  1860. * @return
  1861. */
  1862. function crcUpdateLookup(value, crc) {
  1863. var tmp = crc ^ value;
  1864. crc = (crc >> 8) ^ crc16Lookup[tmp & 0xff];
  1865. return crc;
  1866. }
  1867. this.updateMusicCRC = function (crc, buffer, bufferPos, size) {
  1868. for (var i = 0; i < size; ++i)
  1869. crc[0] = crcUpdateLookup(buffer[bufferPos + i], crc[0]);
  1870. }
  1871. //fix 精简
  1872. }
  1873. BitStream.EQ = function (a, b) {
  1874. return (Math.abs(a) > Math.abs(b)) ? (Math.abs((a) - (b)) <= (Math
  1875. .abs(a) * 1e-6))
  1876. : (Math.abs((a) - (b)) <= (Math.abs(b) * 1e-6));
  1877. };
  1878. BitStream.NEQ = function (a, b) {
  1879. return !BitStream.EQ(a, b);
  1880. };
  1881. function BitStream() {
  1882. var self = this;
  1883. var CRC16_POLYNOMIAL = 0x8005;
  1884. /*
  1885. * we work with ints, so when doing bit manipulation, we limit ourselves to
  1886. * MAX_LENGTH-2 just to be on the safe side
  1887. */
  1888. var MAX_LENGTH = 32;
  1889. //GainAnalysis ga;
  1890. //MPGLib mpg;
  1891. //Version ver;
  1892. //VBRTag vbr;
  1893. var ga = null;
  1894. var mpg = null;
  1895. var ver = null;
  1896. var vbr = null;
  1897. //public final void setModules(GainAnalysis ga, MPGLib mpg, Version ver,
  1898. // VBRTag vbr) {
  1899. this.setModules = function (_ga, _mpg, _ver, _vbr) {
  1900. ga = _ga;
  1901. mpg = _mpg;
  1902. ver = _ver;
  1903. vbr = _vbr;
  1904. };
  1905. /**
  1906. * Bit stream buffer.
  1907. */
  1908. //private byte[] buf;
  1909. var buf = null;
  1910. /**
  1911. * Bit counter of bit stream.
  1912. */
  1913. var totbit = 0;
  1914. /**
  1915. * Pointer to top byte in buffer.
  1916. */
  1917. var bufByteIdx = 0;
  1918. /**
  1919. * Pointer to top bit of top byte in buffer.
  1920. */
  1921. var bufBitIdx = 0;
  1922. /**
  1923. * compute bitsperframe and mean_bits for a layer III frame
  1924. */
  1925. this.getframebits = function (gfp) {
  1926. var gfc = gfp.internal_flags;
  1927. var bit_rate;
  1928. /* get bitrate in kbps [?] */
  1929. if (gfc.bitrate_index != 0)
  1930. bit_rate = Tables.bitrate_table[gfp.version][gfc.bitrate_index];
  1931. else
  1932. bit_rate = gfp.brate;
  1933. /* main encoding routine toggles padding on and off */
  1934. /* one Layer3 Slot consists of 8 bits */
  1935. var bytes = 0 | (gfp.version + 1) * 72000 * bit_rate / gfp.out_samplerate + gfc.padding;
  1936. return 8 * bytes;
  1937. };
  1938. function putheader_bits(gfc) {
  1939. System.arraycopy(gfc.header[gfc.w_ptr].buf, 0, buf, bufByteIdx, gfc.sideinfo_len);
  1940. bufByteIdx += gfc.sideinfo_len;
  1941. totbit += gfc.sideinfo_len * 8;
  1942. gfc.w_ptr = (gfc.w_ptr + 1) & (LameInternalFlags.MAX_HEADER_BUF - 1);
  1943. }
  1944. /**
  1945. * write j bits into the bit stream
  1946. */
  1947. function putbits2(gfc, val, j) {
  1948. while (j > 0) {
  1949. var k;
  1950. if (bufBitIdx == 0) {
  1951. bufBitIdx = 8;
  1952. bufByteIdx++;
  1953. if (gfc.header[gfc.w_ptr].write_timing == totbit) {
  1954. putheader_bits(gfc);
  1955. }
  1956. buf[bufByteIdx] = 0;
  1957. }
  1958. k = Math.min(j, bufBitIdx);
  1959. j -= k;
  1960. bufBitIdx -= k;
  1961. /* 32 too large on 32 bit machines */
  1962. buf[bufByteIdx] |= ((val >> j) << bufBitIdx);
  1963. totbit += k;
  1964. }
  1965. }
  1966. //fix cc 精简
  1967. /**
  1968. * Some combinations of bitrate, Fs, and stereo make it impossible to stuff
  1969. * out a frame using just main_data, due to the limited number of bits to
  1970. * indicate main_data_length. In these situations, we put stuffing bits into
  1971. * the ancillary data...
  1972. */
  1973. function drain_into_ancillary(gfp, remainingBits) {
  1974. var gfc = gfp.internal_flags;
  1975. var i;
  1976. if (remainingBits >= 8) {
  1977. putbits2(gfc, 0x4c, 8);
  1978. remainingBits -= 8;
  1979. }
  1980. if (remainingBits >= 8) {
  1981. putbits2(gfc, 0x41, 8);
  1982. remainingBits -= 8;
  1983. }
  1984. if (remainingBits >= 8) {
  1985. putbits2(gfc, 0x4d, 8);
  1986. remainingBits -= 8;
  1987. }
  1988. if (remainingBits >= 8) {
  1989. putbits2(gfc, 0x45, 8);
  1990. remainingBits -= 8;
  1991. }
  1992. if (remainingBits >= 32) {
  1993. var version = ver.getLameShortVersion();
  1994. if (remainingBits >= 32)
  1995. for (i = 0; i < version.length && remainingBits >= 8; ++i) {
  1996. remainingBits -= 8;
  1997. putbits2(gfc, version.charCodeAt(i), 8); //fix 错误的使用charAt
  1998. }
  1999. }
  2000. for (; remainingBits >= 1; remainingBits -= 1) {
  2001. putbits2(gfc, gfc.ancillary_flag, 1);
  2002. gfc.ancillary_flag ^= (!gfp.disable_reservoir ? 1 : 0);
  2003. }
  2004. }
  2005. /**
  2006. * write N bits into the header
  2007. */
  2008. function writeheader(gfc, val, j) {
  2009. var ptr = gfc.header[gfc.h_ptr].ptr;
  2010. while (j > 0) {
  2011. var k = Math.min(j, 8 - (ptr & 7));
  2012. j -= k;
  2013. /* >> 32 too large for 32 bit machines */
  2014. gfc.header[gfc.h_ptr].buf[ptr >> 3] |= ((val >> j)) << (8 - (ptr & 7) - k);
  2015. ptr += k;
  2016. }
  2017. gfc.header[gfc.h_ptr].ptr = ptr;
  2018. }
  2019. //fix cc 精简
  2020. function encodeSideInfo2(gfp, bitsPerFrame) {
  2021. var gfc = gfp.internal_flags;
  2022. var l3_side;
  2023. var gr, ch;
  2024. l3_side = gfc.l3_side;
  2025. gfc.header[gfc.h_ptr].ptr = 0;
  2026. Arrays.fill(gfc.header[gfc.h_ptr].buf, 0, gfc.sideinfo_len, 0);
  2027. if (gfp.out_samplerate < 16000)
  2028. writeheader(gfc, 0xffe, 12);
  2029. else
  2030. writeheader(gfc, 0xfff, 12);
  2031. writeheader(gfc, (gfp.version), 1);
  2032. writeheader(gfc, 4 - 3, 2);
  2033. writeheader(gfc, (!gfp.error_protection ? 1 : 0), 1);
  2034. writeheader(gfc, (gfc.bitrate_index), 4);
  2035. writeheader(gfc, (gfc.samplerate_index), 2);
  2036. writeheader(gfc, (gfc.padding), 1);
  2037. writeheader(gfc, (gfp.extension), 1);
  2038. writeheader(gfc, (gfp.mode.ordinal()), 2);
  2039. writeheader(gfc, (gfc.mode_ext), 2);
  2040. writeheader(gfc, (gfp.copyright), 1);
  2041. writeheader(gfc, (gfp.original), 1);
  2042. writeheader(gfc, (gfp.emphasis), 2);
  2043. if (gfp.error_protection) {
  2044. writeheader(gfc, 0, 16);
  2045. /* dummy */
  2046. }
  2047. if (gfp.version == 1) {
  2048. /* MPEG1 */
  2049. writeheader(gfc, (l3_side.main_data_begin), 9);
  2050. if (gfc.channels_out == 2)
  2051. writeheader(gfc, l3_side.private_bits, 3);
  2052. else
  2053. writeheader(gfc, l3_side.private_bits, 5);
  2054. for (ch = 0; ch < gfc.channels_out; ch++) {
  2055. var band;
  2056. for (band = 0; band < 4; band++) {
  2057. writeheader(gfc, l3_side.scfsi[ch][band], 1);
  2058. }
  2059. }
  2060. for (gr = 0; gr < 2; gr++) {
  2061. for (ch = 0; ch < gfc.channels_out; ch++) {
  2062. var gi = l3_side.tt[gr][ch];
  2063. writeheader(gfc, gi.part2_3_length + gi.part2_length, 12);
  2064. writeheader(gfc, gi.big_values / 2, 9);
  2065. writeheader(gfc, gi.global_gain, 8);
  2066. writeheader(gfc, gi.scalefac_compress, 4);
  2067. if (gi.block_type != Encoder.NORM_TYPE) {
  2068. writeheader(gfc, 1, 1);
  2069. /* window_switching_flag */
  2070. writeheader(gfc, gi.block_type, 2);
  2071. writeheader(gfc, gi.mixed_block_flag, 1);
  2072. if (gi.table_select[0] == 14)
  2073. gi.table_select[0] = 16;
  2074. writeheader(gfc, gi.table_select[0], 5);
  2075. if (gi.table_select[1] == 14)
  2076. gi.table_select[1] = 16;
  2077. writeheader(gfc, gi.table_select[1], 5);
  2078. writeheader(gfc, gi.subblock_gain[0], 3);
  2079. writeheader(gfc, gi.subblock_gain[1], 3);
  2080. writeheader(gfc, gi.subblock_gain[2], 3);
  2081. } else {
  2082. writeheader(gfc, 0, 1);
  2083. /* window_switching_flag */
  2084. if (gi.table_select[0] == 14)
  2085. gi.table_select[0] = 16;
  2086. writeheader(gfc, gi.table_select[0], 5);
  2087. if (gi.table_select[1] == 14)
  2088. gi.table_select[1] = 16;
  2089. writeheader(gfc, gi.table_select[1], 5);
  2090. if (gi.table_select[2] == 14)
  2091. gi.table_select[2] = 16;
  2092. writeheader(gfc, gi.table_select[2], 5);
  2093. writeheader(gfc, gi.region0_count, 4);
  2094. writeheader(gfc, gi.region1_count, 3);
  2095. }
  2096. writeheader(gfc, gi.preflag, 1);
  2097. writeheader(gfc, gi.scalefac_scale, 1);
  2098. writeheader(gfc, gi.count1table_select, 1);
  2099. }
  2100. }
  2101. } else {
  2102. /* MPEG2 */
  2103. writeheader(gfc, (l3_side.main_data_begin), 8);
  2104. writeheader(gfc, l3_side.private_bits, gfc.channels_out);
  2105. gr = 0;
  2106. for (ch = 0; ch < gfc.channels_out; ch++) {
  2107. var gi = l3_side.tt[gr][ch];
  2108. writeheader(gfc, gi.part2_3_length + gi.part2_length, 12);
  2109. writeheader(gfc, gi.big_values / 2, 9);
  2110. writeheader(gfc, gi.global_gain, 8);
  2111. writeheader(gfc, gi.scalefac_compress, 9);
  2112. if (gi.block_type != Encoder.NORM_TYPE) {
  2113. writeheader(gfc, 1, 1);
  2114. /* window_switching_flag */
  2115. writeheader(gfc, gi.block_type, 2);
  2116. writeheader(gfc, gi.mixed_block_flag, 1);
  2117. if (gi.table_select[0] == 14)
  2118. gi.table_select[0] = 16;
  2119. writeheader(gfc, gi.table_select[0], 5);
  2120. if (gi.table_select[1] == 14)
  2121. gi.table_select[1] = 16;
  2122. writeheader(gfc, gi.table_select[1], 5);
  2123. writeheader(gfc, gi.subblock_gain[0], 3);
  2124. writeheader(gfc, gi.subblock_gain[1], 3);
  2125. writeheader(gfc, gi.subblock_gain[2], 3);
  2126. } else {
  2127. writeheader(gfc, 0, 1);
  2128. /* window_switching_flag */
  2129. if (gi.table_select[0] == 14)
  2130. gi.table_select[0] = 16;
  2131. writeheader(gfc, gi.table_select[0], 5);
  2132. if (gi.table_select[1] == 14)
  2133. gi.table_select[1] = 16;
  2134. writeheader(gfc, gi.table_select[1], 5);
  2135. if (gi.table_select[2] == 14)
  2136. gi.table_select[2] = 16;
  2137. writeheader(gfc, gi.table_select[2], 5);
  2138. writeheader(gfc, gi.region0_count, 4);
  2139. writeheader(gfc, gi.region1_count, 3);
  2140. }
  2141. writeheader(gfc, gi.scalefac_scale, 1);
  2142. writeheader(gfc, gi.count1table_select, 1);
  2143. }
  2144. }
  2145. if (gfp.error_protection) {
  2146. abort();//fix cc 精简
  2147. }
  2148. {
  2149. var old = gfc.h_ptr;
  2150. gfc.h_ptr = (old + 1) & (LameInternalFlags.MAX_HEADER_BUF - 1);
  2151. gfc.header[gfc.h_ptr].write_timing = gfc.header[old].write_timing
  2152. + bitsPerFrame;
  2153. if (gfc.h_ptr == gfc.w_ptr) {
  2154. /* yikes! we are out of header buffer space */
  2155. //fix cc 精简 print
  2156. }
  2157. }
  2158. }
  2159. function huffman_coder_count1(gfc, gi) {
  2160. /* Write count1 area */
  2161. var h = Tables.ht[gi.count1table_select + 32];
  2162. var i, bits = 0;
  2163. var ix = gi.big_values;
  2164. var xr = gi.big_values;
  2165. for (i = (gi.count1 - gi.big_values) / 4; i > 0; --i) {
  2166. var huffbits = 0;
  2167. var p = 0, v;
  2168. v = gi.l3_enc[ix + 0];
  2169. if (v != 0) {
  2170. p += 8;
  2171. if (gi.xr[xr + 0] < 0)
  2172. huffbits++;
  2173. }
  2174. v = gi.l3_enc[ix + 1];
  2175. if (v != 0) {
  2176. p += 4;
  2177. huffbits *= 2;
  2178. if (gi.xr[xr + 1] < 0)
  2179. huffbits++;
  2180. }
  2181. v = gi.l3_enc[ix + 2];
  2182. if (v != 0) {
  2183. p += 2;
  2184. huffbits *= 2;
  2185. if (gi.xr[xr + 2] < 0)
  2186. huffbits++;
  2187. }
  2188. v = gi.l3_enc[ix + 3];
  2189. if (v != 0) {
  2190. p++;
  2191. huffbits *= 2;
  2192. if (gi.xr[xr + 3] < 0)
  2193. huffbits++;
  2194. }
  2195. ix += 4;
  2196. xr += 4;
  2197. putbits2(gfc, huffbits + h.table[p], h.hlen[p]);
  2198. bits += h.hlen[p];
  2199. }
  2200. return bits;
  2201. }
  2202. /**
  2203. * Implements the pseudocode of page 98 of the IS
  2204. */
  2205. function Huffmancode(gfc, tableindex, start, end, gi) {
  2206. var h = Tables.ht[tableindex];
  2207. var bits = 0;
  2208. if (0 == tableindex)
  2209. return bits;
  2210. for (var i = start; i < end; i += 2) {
  2211. var cbits = 0;
  2212. var xbits = 0;
  2213. var linbits = h.xlen;
  2214. var xlen = h.xlen;
  2215. var ext = 0;
  2216. var x1 = gi.l3_enc[i];
  2217. var x2 = gi.l3_enc[i + 1];
  2218. if (x1 != 0) {
  2219. if (gi.xr[i] < 0)
  2220. ext++;
  2221. cbits--;
  2222. }
  2223. if (tableindex > 15) {
  2224. /* use ESC-words */
  2225. if (x1 > 14) {
  2226. var linbits_x1 = x1 - 15;
  2227. ext |= linbits_x1 << 1;
  2228. xbits = linbits;
  2229. x1 = 15;
  2230. }
  2231. if (x2 > 14) {
  2232. var linbits_x2 = x2 - 15;
  2233. ext <<= linbits;
  2234. ext |= linbits_x2;
  2235. xbits += linbits;
  2236. x2 = 15;
  2237. }
  2238. xlen = 16;
  2239. }
  2240. if (x2 != 0) {
  2241. ext <<= 1;
  2242. if (gi.xr[i + 1] < 0)
  2243. ext++;
  2244. cbits--;
  2245. }
  2246. x1 = x1 * xlen + x2;
  2247. xbits -= cbits;
  2248. cbits += h.hlen[x1];
  2249. putbits2(gfc, h.table[x1], cbits);
  2250. putbits2(gfc, ext, xbits);
  2251. bits += cbits + xbits;
  2252. }
  2253. return bits;
  2254. }
  2255. /**
  2256. * Note the discussion of huffmancodebits() on pages 28 and 29 of the IS, as
  2257. * well as the definitions of the side information on pages 26 and 27.
  2258. */
  2259. function ShortHuffmancodebits(gfc, gi) {
  2260. var region1Start = 3 * gfc.scalefac_band.s[3];
  2261. if (region1Start > gi.big_values)
  2262. region1Start = gi.big_values;
  2263. /* short blocks do not have a region2 */
  2264. var bits = Huffmancode(gfc, gi.table_select[0], 0, region1Start, gi);
  2265. bits += Huffmancode(gfc, gi.table_select[1], region1Start,
  2266. gi.big_values, gi);
  2267. return bits;
  2268. }
  2269. function LongHuffmancodebits(gfc, gi) {
  2270. var bigvalues, bits;
  2271. var region1Start, region2Start;
  2272. bigvalues = gi.big_values;
  2273. var i = gi.region0_count + 1;
  2274. region1Start = gfc.scalefac_band.l[i];
  2275. i += gi.region1_count + 1;
  2276. region2Start = gfc.scalefac_band.l[i];
  2277. if (region1Start > bigvalues)
  2278. region1Start = bigvalues;
  2279. if (region2Start > bigvalues)
  2280. region2Start = bigvalues;
  2281. bits = Huffmancode(gfc, gi.table_select[0], 0, region1Start, gi);
  2282. bits += Huffmancode(gfc, gi.table_select[1], region1Start,
  2283. region2Start, gi);
  2284. bits += Huffmancode(gfc, gi.table_select[2], region2Start, bigvalues,
  2285. gi);
  2286. return bits;
  2287. }
  2288. function writeMainData(gfp) {
  2289. var gr, ch, sfb, data_bits, tot_bits = 0;
  2290. var gfc = gfp.internal_flags;
  2291. var l3_side = gfc.l3_side;
  2292. if (gfp.version == 1) {
  2293. /* MPEG 1 */
  2294. for (gr = 0; gr < 2; gr++) {
  2295. for (ch = 0; ch < gfc.channels_out; ch++) {
  2296. var gi = l3_side.tt[gr][ch];
  2297. var slen1 = Takehiro.slen1_tab[gi.scalefac_compress];
  2298. var slen2 = Takehiro.slen2_tab[gi.scalefac_compress];
  2299. data_bits = 0;
  2300. for (sfb = 0; sfb < gi.sfbdivide; sfb++) {
  2301. if (gi.scalefac[sfb] == -1)
  2302. continue;
  2303. /* scfsi is used */
  2304. putbits2(gfc, gi.scalefac[sfb], slen1);
  2305. data_bits += slen1;
  2306. }
  2307. for (; sfb < gi.sfbmax; sfb++) {
  2308. if (gi.scalefac[sfb] == -1)
  2309. continue;
  2310. /* scfsi is used */
  2311. putbits2(gfc, gi.scalefac[sfb], slen2);
  2312. data_bits += slen2;
  2313. }
  2314. if (gi.block_type == Encoder.SHORT_TYPE) {
  2315. data_bits += ShortHuffmancodebits(gfc, gi);
  2316. } else {
  2317. data_bits += LongHuffmancodebits(gfc, gi);
  2318. }
  2319. data_bits += huffman_coder_count1(gfc, gi);
  2320. /* does bitcount in quantize.c agree with actual bit count? */
  2321. tot_bits += data_bits;
  2322. }
  2323. /* for ch */
  2324. }
  2325. /* for gr */
  2326. } else {
  2327. /* MPEG 2 */
  2328. gr = 0;
  2329. for (ch = 0; ch < gfc.channels_out; ch++) {
  2330. var gi = l3_side.tt[gr][ch];
  2331. var i, sfb_partition, scale_bits = 0;
  2332. data_bits = 0;
  2333. sfb = 0;
  2334. sfb_partition = 0;
  2335. if (gi.block_type == Encoder.SHORT_TYPE) {
  2336. for (; sfb_partition < 4; sfb_partition++) {
  2337. var sfbs = gi.sfb_partition_table[sfb_partition] / 3;
  2338. var slen = gi.slen[sfb_partition];
  2339. for (i = 0; i < sfbs; i++, sfb++) {
  2340. putbits2(gfc,
  2341. Math.max(gi.scalefac[sfb * 3 + 0], 0), slen);
  2342. putbits2(gfc,
  2343. Math.max(gi.scalefac[sfb * 3 + 1], 0), slen);
  2344. putbits2(gfc,
  2345. Math.max(gi.scalefac[sfb * 3 + 2], 0), slen);
  2346. scale_bits += 3 * slen;
  2347. }
  2348. }
  2349. data_bits += ShortHuffmancodebits(gfc, gi);
  2350. } else {
  2351. for (; sfb_partition < 4; sfb_partition++) {
  2352. var sfbs = gi.sfb_partition_table[sfb_partition];
  2353. var slen = gi.slen[sfb_partition];
  2354. for (i = 0; i < sfbs; i++, sfb++) {
  2355. putbits2(gfc, Math.max(gi.scalefac[sfb], 0), slen);
  2356. scale_bits += slen;
  2357. }
  2358. }
  2359. data_bits += LongHuffmancodebits(gfc, gi);
  2360. }
  2361. data_bits += huffman_coder_count1(gfc, gi);
  2362. /* does bitcount in quantize.c agree with actual bit count? */
  2363. tot_bits += scale_bits + data_bits;
  2364. }
  2365. /* for ch */
  2366. }
  2367. /* for gf */
  2368. return tot_bits;
  2369. }
  2370. /* main_data */
  2371. function TotalBytes() {
  2372. this.total = 0;
  2373. }
  2374. /*
  2375. * compute the number of bits required to flush all mp3 frames currently in
  2376. * the buffer. This should be the same as the reservoir size. Only call this
  2377. * routine between frames - i.e. only after all headers and data have been
  2378. * added to the buffer by format_bitstream().
  2379. *
  2380. * Also compute total_bits_output = size of mp3 buffer (including frame
  2381. * headers which may not have yet been send to the mp3 buffer) + number of
  2382. * bits needed to flush all mp3 frames.
  2383. *
  2384. * total_bytes_output is the size of the mp3 output buffer if
  2385. * lame_encode_flush_nogap() was called right now.
  2386. */
  2387. function compute_flushbits(gfp, total_bytes_output) {
  2388. var gfc = gfp.internal_flags;
  2389. var flushbits, remaining_headers;
  2390. var bitsPerFrame;
  2391. var last_ptr, first_ptr;
  2392. first_ptr = gfc.w_ptr;
  2393. /* first header to add to bitstream */
  2394. last_ptr = gfc.h_ptr - 1;
  2395. /* last header to add to bitstream */
  2396. if (last_ptr == -1)
  2397. last_ptr = LameInternalFlags.MAX_HEADER_BUF - 1;
  2398. /* add this many bits to bitstream so we can flush all headers */
  2399. flushbits = gfc.header[last_ptr].write_timing - totbit;
  2400. total_bytes_output.total = flushbits;
  2401. if (flushbits >= 0) {
  2402. abort();//fix cc 精简
  2403. }
  2404. /*
  2405. * finally, add some bits so that the last frame is complete these bits
  2406. * are not necessary to decode the last frame, but some decoders will
  2407. * ignore last frame if these bits are missing
  2408. */
  2409. bitsPerFrame = self.getframebits(gfp);
  2410. flushbits += bitsPerFrame;
  2411. total_bytes_output.total += bitsPerFrame;
  2412. /* round up: */
  2413. if ((total_bytes_output.total % 8) != 0)
  2414. total_bytes_output.total = 1 + (total_bytes_output.total / 8);
  2415. else
  2416. total_bytes_output.total = (total_bytes_output.total / 8);
  2417. total_bytes_output.total += bufByteIdx + 1;
  2418. if (flushbits < 0) {
  2419. //fix cc 精简 print
  2420. }
  2421. return flushbits;
  2422. }
  2423. this.flush_bitstream = function (gfp) {
  2424. var gfc = gfp.internal_flags;
  2425. var l3_side;
  2426. var flushbits;
  2427. var last_ptr = gfc.h_ptr - 1;
  2428. /* last header to add to bitstream */
  2429. if (last_ptr == -1)
  2430. last_ptr = LameInternalFlags.MAX_HEADER_BUF - 1;
  2431. l3_side = gfc.l3_side;
  2432. if ((flushbits = compute_flushbits(gfp, new TotalBytes())) < 0)
  2433. return;
  2434. drain_into_ancillary(gfp, flushbits);
  2435. /* check that the 100% of the last frame has been written to bitstream */
  2436. /*
  2437. * we have padded out all frames with ancillary data, which is the same
  2438. * as filling the bitreservoir with ancillary data, so :
  2439. */
  2440. gfc.ResvSize = 0;
  2441. l3_side.main_data_begin = 0;
  2442. /* save the ReplayGain value */
  2443. if (gfc.findReplayGain) {
  2444. abort();//fix cc 精简
  2445. }
  2446. /* find the gain and scale change required for no clipping */
  2447. if (gfc.findPeakSample) {
  2448. abort();//fix cc 精简
  2449. }
  2450. };
  2451. //fix cc 精简
  2452. /**
  2453. * This is called after a frame of audio has been quantized and coded. It
  2454. * will write the encoded audio to the bitstream. Note that from a layer3
  2455. * encoder's perspective the bit stream is primarily a series of main_data()
  2456. * blocks, with header and side information inserted at the proper locations
  2457. * to maintain framing. (See Figure A.7 in the IS).
  2458. */
  2459. this.format_bitstream = function (gfp) {
  2460. var gfc = gfp.internal_flags;
  2461. var l3_side;
  2462. l3_side = gfc.l3_side;
  2463. var bitsPerFrame = this.getframebits(gfp);
  2464. drain_into_ancillary(gfp, l3_side.resvDrain_pre);
  2465. encodeSideInfo2(gfp, bitsPerFrame);
  2466. var bits = 8 * gfc.sideinfo_len;
  2467. bits += writeMainData(gfp);
  2468. drain_into_ancillary(gfp, l3_side.resvDrain_post);
  2469. bits += l3_side.resvDrain_post;
  2470. l3_side.main_data_begin += (bitsPerFrame - bits) / 8;
  2471. /*
  2472. * compare number of bits needed to clear all buffered mp3 frames with
  2473. * what we think the resvsize is:
  2474. */
  2475. if (compute_flushbits(gfp, new TotalBytes()) != gfc.ResvSize) {
  2476. //fix cc 精简 print
  2477. }
  2478. /*
  2479. * compare main_data_begin for the next frame with what we think the
  2480. * resvsize is:
  2481. */
  2482. if ((l3_side.main_data_begin * 8) != gfc.ResvSize) {
  2483. //fix cc 精简 print
  2484. gfc.ResvSize = l3_side.main_data_begin * 8;
  2485. }
  2486. //;
  2487. if (totbit > 1000000000) { //不可精简
  2488. /*
  2489. * to avoid totbit overflow, (at 8h encoding at 128kbs) lets reset
  2490. * bit counter
  2491. */
  2492. var i;
  2493. for (i = 0; i < LameInternalFlags.MAX_HEADER_BUF; ++i)
  2494. gfc.header[i].write_timing -= totbit;
  2495. totbit = 0;
  2496. }
  2497. return 0;
  2498. };
  2499. /**
  2500. * <PRE>
  2501. * copy data out of the internal MP3 bit buffer into a user supplied
  2502. * unsigned char buffer.
  2503. *
  2504. * mp3data=0 indicates data in buffer is an id3tags and VBR tags
  2505. * mp3data=1 data is real mp3 frame data.
  2506. * </PRE>
  2507. */
  2508. this.copy_buffer = function (gfc, buffer, bufferPos, size, mp3data) {
  2509. var minimum = bufByteIdx + 1;
  2510. if (minimum <= 0)
  2511. return 0;
  2512. if (size != 0 && minimum > size) {
  2513. /* buffer is too small */
  2514. return -1;
  2515. }
  2516. System.arraycopy(buf, 0, buffer, bufferPos, minimum);
  2517. bufByteIdx = -1;
  2518. bufBitIdx = 0;
  2519. if (mp3data != 0) {
  2520. var crc = new_int(1);
  2521. crc[0] = gfc.nMusicCRC;
  2522. vbr.updateMusicCRC(crc, buffer, bufferPos, minimum);
  2523. gfc.nMusicCRC = crc[0];
  2524. /**
  2525. * sum number of bytes belonging to the mp3 stream this info will be
  2526. * written into the Xing/LAME header for seeking
  2527. */
  2528. if (minimum > 0) {
  2529. gfc.VBR_seek_table.nBytesWritten += minimum;
  2530. }
  2531. if (gfc.decode_on_the_fly) { /* decode the frame */
  2532. abort();//fix cc 精简
  2533. }
  2534. /* if (gfc.decode_on_the_fly) */
  2535. }
  2536. /* if (mp3data) */
  2537. return minimum;
  2538. };
  2539. this.init_bit_stream_w = function (gfc) {
  2540. buf = new_byte(Lame.LAME_MAXMP3BUFFER);
  2541. gfc.h_ptr = gfc.w_ptr = 0;
  2542. gfc.header[gfc.h_ptr].write_timing = 0;
  2543. bufByteIdx = -1;
  2544. bufBitIdx = 0;
  2545. totbit = 0;
  2546. };
  2547. // From machine.h
  2548. }
  2549. function HuffCodeTab(len, max, tab, hl) {
  2550. this.xlen = len;
  2551. this.linmax = max;
  2552. this.table = tab;
  2553. this.hlen = hl;
  2554. }
  2555. var Tables = {};
  2556. Tables.t1HB = [
  2557. 1, 1,
  2558. 1, 0
  2559. ];
  2560. Tables.t2HB = [
  2561. 1, 2, 1,
  2562. 3, 1, 1,
  2563. 3, 2, 0
  2564. ];
  2565. Tables.t3HB = [
  2566. 3, 2, 1,
  2567. 1, 1, 1,
  2568. 3, 2, 0
  2569. ];
  2570. Tables.t5HB = [
  2571. 1, 2, 6, 5,
  2572. 3, 1, 4, 4,
  2573. 7, 5, 7, 1,
  2574. 6, 1, 1, 0
  2575. ];
  2576. Tables.t6HB = [
  2577. 7, 3, 5, 1,
  2578. 6, 2, 3, 2,
  2579. 5, 4, 4, 1,
  2580. 3, 3, 2, 0
  2581. ];
  2582. Tables.t7HB = [
  2583. 1, 2, 10, 19, 16, 10,
  2584. 3, 3, 7, 10, 5, 3,
  2585. 11, 4, 13, 17, 8, 4,
  2586. 12, 11, 18, 15, 11, 2,
  2587. 7, 6, 9, 14, 3, 1,
  2588. 6, 4, 5, 3, 2, 0
  2589. ];
  2590. Tables.t8HB = [
  2591. 3, 4, 6, 18, 12, 5,
  2592. 5, 1, 2, 16, 9, 3,
  2593. 7, 3, 5, 14, 7, 3,
  2594. 19, 17, 15, 13, 10, 4,
  2595. 13, 5, 8, 11, 5, 1,
  2596. 12, 4, 4, 1, 1, 0
  2597. ];
  2598. Tables.t9HB = [
  2599. 7, 5, 9, 14, 15, 7,
  2600. 6, 4, 5, 5, 6, 7,
  2601. 7, 6, 8, 8, 8, 5,
  2602. 15, 6, 9, 10, 5, 1,
  2603. 11, 7, 9, 6, 4, 1,
  2604. 14, 4, 6, 2, 6, 0
  2605. ];
  2606. Tables.t10HB = [
  2607. 1, 2, 10, 23, 35, 30, 12, 17,
  2608. 3, 3, 8, 12, 18, 21, 12, 7,
  2609. 11, 9, 15, 21, 32, 40, 19, 6,
  2610. 14, 13, 22, 34, 46, 23, 18, 7,
  2611. 20, 19, 33, 47, 27, 22, 9, 3,
  2612. 31, 22, 41, 26, 21, 20, 5, 3,
  2613. 14, 13, 10, 11, 16, 6, 5, 1,
  2614. 9, 8, 7, 8, 4, 4, 2, 0
  2615. ];
  2616. Tables.t11HB = [
  2617. 3, 4, 10, 24, 34, 33, 21, 15,
  2618. 5, 3, 4, 10, 32, 17, 11, 10,
  2619. 11, 7, 13, 18, 30, 31, 20, 5,
  2620. 25, 11, 19, 59, 27, 18, 12, 5,
  2621. 35, 33, 31, 58, 30, 16, 7, 5,
  2622. 28, 26, 32, 19, 17, 15, 8, 14,
  2623. 14, 12, 9, 13, 14, 9, 4, 1,
  2624. 11, 4, 6, 6, 6, 3, 2, 0
  2625. ];
  2626. Tables.t12HB = [
  2627. 9, 6, 16, 33, 41, 39, 38, 26,
  2628. 7, 5, 6, 9, 23, 16, 26, 11,
  2629. 17, 7, 11, 14, 21, 30, 10, 7,
  2630. 17, 10, 15, 12, 18, 28, 14, 5,
  2631. 32, 13, 22, 19, 18, 16, 9, 5,
  2632. 40, 17, 31, 29, 17, 13, 4, 2,
  2633. 27, 12, 11, 15, 10, 7, 4, 1,
  2634. 27, 12, 8, 12, 6, 3, 1, 0
  2635. ];
  2636. Tables.t13HB = [
  2637. 1, 5, 14, 21, 34, 51, 46, 71, 42, 52, 68, 52, 67, 44, 43, 19,
  2638. 3, 4, 12, 19, 31, 26, 44, 33, 31, 24, 32, 24, 31, 35, 22, 14,
  2639. 15, 13, 23, 36, 59, 49, 77, 65, 29, 40, 30, 40, 27, 33, 42, 16,
  2640. 22, 20, 37, 61, 56, 79, 73, 64, 43, 76, 56, 37, 26, 31, 25, 14,
  2641. 35, 16, 60, 57, 97, 75, 114, 91, 54, 73, 55, 41, 48, 53, 23, 24,
  2642. 58, 27, 50, 96, 76, 70, 93, 84, 77, 58, 79, 29, 74, 49, 41, 17,
  2643. 47, 45, 78, 74, 115, 94, 90, 79, 69, 83, 71, 50, 59, 38, 36, 15,
  2644. 72, 34, 56, 95, 92, 85, 91, 90, 86, 73, 77, 65, 51, 44, 43, 42,
  2645. 43, 20, 30, 44, 55, 78, 72, 87, 78, 61, 46, 54, 37, 30, 20, 16,
  2646. 53, 25, 41, 37, 44, 59, 54, 81, 66, 76, 57, 54, 37, 18, 39, 11,
  2647. 35, 33, 31, 57, 42, 82, 72, 80, 47, 58, 55, 21, 22, 26, 38, 22,
  2648. 53, 25, 23, 38, 70, 60, 51, 36, 55, 26, 34, 23, 27, 14, 9, 7,
  2649. 34, 32, 28, 39, 49, 75, 30, 52, 48, 40, 52, 28, 18, 17, 9, 5,
  2650. 45, 21, 34, 64, 56, 50, 49, 45, 31, 19, 12, 15, 10, 7, 6, 3,
  2651. 48, 23, 20, 39, 36, 35, 53, 21, 16, 23, 13, 10, 6, 1, 4, 2,
  2652. 16, 15, 17, 27, 25, 20, 29, 11, 17, 12, 16, 8, 1, 1, 0, 1
  2653. ];
  2654. Tables.t15HB = [
  2655. 7, 12, 18, 53, 47, 76, 124, 108, 89, 123, 108, 119, 107, 81, 122, 63,
  2656. 13, 5, 16, 27, 46, 36, 61, 51, 42, 70, 52, 83, 65, 41, 59, 36,
  2657. 19, 17, 15, 24, 41, 34, 59, 48, 40, 64, 50, 78, 62, 80, 56, 33,
  2658. 29, 28, 25, 43, 39, 63, 55, 93, 76, 59, 93, 72, 54, 75, 50, 29,
  2659. 52, 22, 42, 40, 67, 57, 95, 79, 72, 57, 89, 69, 49, 66, 46, 27,
  2660. 77, 37, 35, 66, 58, 52, 91, 74, 62, 48, 79, 63, 90, 62, 40, 38,
  2661. 125, 32, 60, 56, 50, 92, 78, 65, 55, 87, 71, 51, 73, 51, 70, 30,
  2662. 109, 53, 49, 94, 88, 75, 66, 122, 91, 73, 56, 42, 64, 44, 21, 25,
  2663. 90, 43, 41, 77, 73, 63, 56, 92, 77, 66, 47, 67, 48, 53, 36, 20,
  2664. 71, 34, 67, 60, 58, 49, 88, 76, 67, 106, 71, 54, 38, 39, 23, 15,
  2665. 109, 53, 51, 47, 90, 82, 58, 57, 48, 72, 57, 41, 23, 27, 62, 9,
  2666. 86, 42, 40, 37, 70, 64, 52, 43, 70, 55, 42, 25, 29, 18, 11, 11,
  2667. 118, 68, 30, 55, 50, 46, 74, 65, 49, 39, 24, 16, 22, 13, 14, 7,
  2668. 91, 44, 39, 38, 34, 63, 52, 45, 31, 52, 28, 19, 14, 8, 9, 3,
  2669. 123, 60, 58, 53, 47, 43, 32, 22, 37, 24, 17, 12, 15, 10, 2, 1,
  2670. 71, 37, 34, 30, 28, 20, 17, 26, 21, 16, 10, 6, 8, 6, 2, 0
  2671. ];
  2672. Tables.t16HB = [
  2673. 1, 5, 14, 44, 74, 63, 110, 93, 172, 149, 138, 242, 225, 195, 376, 17,
  2674. 3, 4, 12, 20, 35, 62, 53, 47, 83, 75, 68, 119, 201, 107, 207, 9,
  2675. 15, 13, 23, 38, 67, 58, 103, 90, 161, 72, 127, 117, 110, 209, 206, 16,
  2676. 45, 21, 39, 69, 64, 114, 99, 87, 158, 140, 252, 212, 199, 387, 365, 26,
  2677. 75, 36, 68, 65, 115, 101, 179, 164, 155, 264, 246, 226, 395, 382, 362, 9,
  2678. 66, 30, 59, 56, 102, 185, 173, 265, 142, 253, 232, 400, 388, 378, 445, 16,
  2679. 111, 54, 52, 100, 184, 178, 160, 133, 257, 244, 228, 217, 385, 366, 715, 10,
  2680. 98, 48, 91, 88, 165, 157, 148, 261, 248, 407, 397, 372, 380, 889, 884, 8,
  2681. 85, 84, 81, 159, 156, 143, 260, 249, 427, 401, 392, 383, 727, 713, 708, 7,
  2682. 154, 76, 73, 141, 131, 256, 245, 426, 406, 394, 384, 735, 359, 710, 352, 11,
  2683. 139, 129, 67, 125, 247, 233, 229, 219, 393, 743, 737, 720, 885, 882, 439, 4,
  2684. 243, 120, 118, 115, 227, 223, 396, 746, 742, 736, 721, 712, 706, 223, 436, 6,
  2685. 202, 224, 222, 218, 216, 389, 386, 381, 364, 888, 443, 707, 440, 437, 1728, 4,
  2686. 747, 211, 210, 208, 370, 379, 734, 723, 714, 1735, 883, 877, 876, 3459, 865, 2,
  2687. 377, 369, 102, 187, 726, 722, 358, 711, 709, 866, 1734, 871, 3458, 870, 434, 0,
  2688. 12, 10, 7, 11, 10, 17, 11, 9, 13, 12, 10, 7, 5, 3, 1, 3
  2689. ];
  2690. Tables.t24HB = [
  2691. 15, 13, 46, 80, 146, 262, 248, 434, 426, 669, 653, 649, 621, 517, 1032, 88,
  2692. 14, 12, 21, 38, 71, 130, 122, 216, 209, 198, 327, 345, 319, 297, 279, 42,
  2693. 47, 22, 41, 74, 68, 128, 120, 221, 207, 194, 182, 340, 315, 295, 541, 18,
  2694. 81, 39, 75, 70, 134, 125, 116, 220, 204, 190, 178, 325, 311, 293, 271, 16,
  2695. 147, 72, 69, 135, 127, 118, 112, 210, 200, 188, 352, 323, 306, 285, 540, 14,
  2696. 263, 66, 129, 126, 119, 114, 214, 202, 192, 180, 341, 317, 301, 281, 262, 12,
  2697. 249, 123, 121, 117, 113, 215, 206, 195, 185, 347, 330, 308, 291, 272, 520, 10,
  2698. 435, 115, 111, 109, 211, 203, 196, 187, 353, 332, 313, 298, 283, 531, 381, 17,
  2699. 427, 212, 208, 205, 201, 193, 186, 177, 169, 320, 303, 286, 268, 514, 377, 16,
  2700. 335, 199, 197, 191, 189, 181, 174, 333, 321, 305, 289, 275, 521, 379, 371, 11,
  2701. 668, 184, 183, 179, 175, 344, 331, 314, 304, 290, 277, 530, 383, 373, 366, 10,
  2702. 652, 346, 171, 168, 164, 318, 309, 299, 287, 276, 263, 513, 375, 368, 362, 6,
  2703. 648, 322, 316, 312, 307, 302, 292, 284, 269, 261, 512, 376, 370, 364, 359, 4,
  2704. 620, 300, 296, 294, 288, 282, 273, 266, 515, 380, 374, 369, 365, 361, 357, 2,
  2705. 1033, 280, 278, 274, 267, 264, 259, 382, 378, 372, 367, 363, 360, 358, 356, 0,
  2706. 43, 20, 19, 17, 15, 13, 11, 9, 7, 6, 4, 7, 5, 3, 1, 3
  2707. ];
  2708. Tables.t32HB = [
  2709. 1 << 0, 5 << 1, 4 << 1, 5 << 2, 6 << 1, 5 << 2, 4 << 2, 4 << 3,
  2710. 7 << 1, 3 << 2, 6 << 2, 0 << 3, 7 << 2, 2 << 3, 3 << 3, 1 << 4
  2711. ];
  2712. Tables.t33HB = [
  2713. 15 << 0, 14 << 1, 13 << 1, 12 << 2, 11 << 1, 10 << 2, 9 << 2, 8 << 3,
  2714. 7 << 1, 6 << 2, 5 << 2, 4 << 3, 3 << 2, 2 << 3, 1 << 3, 0 << 4
  2715. ];
  2716. Tables.t1l = [
  2717. 1, 4,
  2718. 3, 5
  2719. ];
  2720. Tables.t2l = [
  2721. 1, 4, 7,
  2722. 4, 5, 7,
  2723. 6, 7, 8
  2724. ];
  2725. Tables.t3l = [
  2726. 2, 3, 7,
  2727. 4, 4, 7,
  2728. 6, 7, 8
  2729. ];
  2730. Tables.t5l = [
  2731. 1, 4, 7, 8,
  2732. 4, 5, 8, 9,
  2733. 7, 8, 9, 10,
  2734. 8, 8, 9, 10
  2735. ];
  2736. Tables.t6l = [
  2737. 3, 4, 6, 8,
  2738. 4, 4, 6, 7,
  2739. 5, 6, 7, 8,
  2740. 7, 7, 8, 9
  2741. ];
  2742. Tables.t7l = [
  2743. 1, 4, 7, 9, 9, 10,
  2744. 4, 6, 8, 9, 9, 10,
  2745. 7, 7, 9, 10, 10, 11,
  2746. 8, 9, 10, 11, 11, 11,
  2747. 8, 9, 10, 11, 11, 12,
  2748. 9, 10, 11, 12, 12, 12
  2749. ];
  2750. Tables.t8l = [
  2751. 2, 4, 7, 9, 9, 10,
  2752. 4, 4, 6, 10, 10, 10,
  2753. 7, 6, 8, 10, 10, 11,
  2754. 9, 10, 10, 11, 11, 12,
  2755. 9, 9, 10, 11, 12, 12,
  2756. 10, 10, 11, 11, 13, 13
  2757. ];
  2758. Tables.t9l = [
  2759. 3, 4, 6, 7, 9, 10,
  2760. 4, 5, 6, 7, 8, 10,
  2761. 5, 6, 7, 8, 9, 10,
  2762. 7, 7, 8, 9, 9, 10,
  2763. 8, 8, 9, 9, 10, 11,
  2764. 9, 9, 10, 10, 11, 11
  2765. ];
  2766. Tables.t10l = [
  2767. 1, 4, 7, 9, 10, 10, 10, 11,
  2768. 4, 6, 8, 9, 10, 11, 10, 10,
  2769. 7, 8, 9, 10, 11, 12, 11, 11,
  2770. 8, 9, 10, 11, 12, 12, 11, 12,
  2771. 9, 10, 11, 12, 12, 12, 12, 12,
  2772. 10, 11, 12, 12, 13, 13, 12, 13,
  2773. 9, 10, 11, 12, 12, 12, 13, 13,
  2774. 10, 10, 11, 12, 12, 13, 13, 13
  2775. ];
  2776. Tables.t11l = [
  2777. 2, 4, 6, 8, 9, 10, 9, 10,
  2778. 4, 5, 6, 8, 10, 10, 9, 10,
  2779. 6, 7, 8, 9, 10, 11, 10, 10,
  2780. 8, 8, 9, 11, 10, 12, 10, 11,
  2781. 9, 10, 10, 11, 11, 12, 11, 12,
  2782. 9, 10, 11, 12, 12, 13, 12, 13,
  2783. 9, 9, 9, 10, 11, 12, 12, 12,
  2784. 9, 9, 10, 11, 12, 12, 12, 12
  2785. ];
  2786. Tables.t12l = [
  2787. 4, 4, 6, 8, 9, 10, 10, 10,
  2788. 4, 5, 6, 7, 9, 9, 10, 10,
  2789. 6, 6, 7, 8, 9, 10, 9, 10,
  2790. 7, 7, 8, 8, 9, 10, 10, 10,
  2791. 8, 8, 9, 9, 10, 10, 10, 11,
  2792. 9, 9, 10, 10, 10, 11, 10, 11,
  2793. 9, 9, 9, 10, 10, 11, 11, 12,
  2794. 10, 10, 10, 11, 11, 11, 11, 12
  2795. ];
  2796. Tables.t13l = [
  2797. 1, 5, 7, 8, 9, 10, 10, 11, 10, 11, 12, 12, 13, 13, 14, 14,
  2798. 4, 6, 8, 9, 10, 10, 11, 11, 11, 11, 12, 12, 13, 14, 14, 14,
  2799. 7, 8, 9, 10, 11, 11, 12, 12, 11, 12, 12, 13, 13, 14, 15, 15,
  2800. 8, 9, 10, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 15, 15,
  2801. 9, 9, 11, 11, 12, 12, 13, 13, 12, 13, 13, 14, 14, 15, 15, 16,
  2802. 10, 10, 11, 12, 12, 12, 13, 13, 13, 13, 14, 13, 15, 15, 16, 16,
  2803. 10, 11, 12, 12, 13, 13, 13, 13, 13, 14, 14, 14, 15, 15, 16, 16,
  2804. 11, 11, 12, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 16, 18, 18,
  2805. 10, 10, 11, 12, 12, 13, 13, 14, 14, 14, 14, 15, 15, 16, 17, 17,
  2806. 11, 11, 12, 12, 13, 13, 13, 15, 14, 15, 15, 16, 16, 16, 18, 17,
  2807. 11, 12, 12, 13, 13, 14, 14, 15, 14, 15, 16, 15, 16, 17, 18, 19,
  2808. 12, 12, 12, 13, 14, 14, 14, 14, 15, 15, 15, 16, 17, 17, 17, 18,
  2809. 12, 13, 13, 14, 14, 15, 14, 15, 16, 16, 17, 17, 17, 18, 18, 18,
  2810. 13, 13, 14, 15, 15, 15, 16, 16, 16, 16, 16, 17, 18, 17, 18, 18,
  2811. 14, 14, 14, 15, 15, 15, 17, 16, 16, 19, 17, 17, 17, 19, 18, 18,
  2812. 13, 14, 15, 16, 16, 16, 17, 16, 17, 17, 18, 18, 21, 20, 21, 18
  2813. ];
  2814. Tables.t15l = [
  2815. 3, 5, 6, 8, 8, 9, 10, 10, 10, 11, 11, 12, 12, 12, 13, 14,
  2816. 5, 5, 7, 8, 9, 9, 10, 10, 10, 11, 11, 12, 12, 12, 13, 13,
  2817. 6, 7, 7, 8, 9, 9, 10, 10, 10, 11, 11, 12, 12, 13, 13, 13,
  2818. 7, 8, 8, 9, 9, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13,
  2819. 8, 8, 9, 9, 10, 10, 11, 11, 11, 11, 12, 12, 12, 13, 13, 13,
  2820. 9, 9, 9, 10, 10, 10, 11, 11, 11, 11, 12, 12, 13, 13, 13, 14,
  2821. 10, 9, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 13, 13, 14, 14,
  2822. 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 14,
  2823. 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 14, 14, 14,
  2824. 10, 10, 11, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14,
  2825. 11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 14, 15, 14,
  2826. 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15,
  2827. 12, 12, 11, 12, 12, 12, 13, 13, 13, 13, 13, 13, 14, 14, 15, 15,
  2828. 12, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15,
  2829. 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 14, 15,
  2830. 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15, 15, 15
  2831. ];
  2832. Tables.t16_5l = [
  2833. 1, 5, 7, 9, 10, 10, 11, 11, 12, 12, 12, 13, 13, 13, 14, 11,
  2834. 4, 6, 8, 9, 10, 11, 11, 11, 12, 12, 12, 13, 14, 13, 14, 11,
  2835. 7, 8, 9, 10, 11, 11, 12, 12, 13, 12, 13, 13, 13, 14, 14, 12,
  2836. 9, 9, 10, 11, 11, 12, 12, 12, 13, 13, 14, 14, 14, 15, 15, 13,
  2837. 10, 10, 11, 11, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 12,
  2838. 10, 10, 11, 11, 12, 13, 13, 14, 13, 14, 14, 15, 15, 15, 16, 13,
  2839. 11, 11, 11, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 16, 13,
  2840. 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 15, 15, 17, 17, 13,
  2841. 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 15, 15, 16, 16, 16, 13,
  2842. 12, 12, 12, 13, 13, 14, 14, 15, 15, 15, 15, 16, 15, 16, 15, 14,
  2843. 12, 13, 12, 13, 14, 14, 14, 14, 15, 16, 16, 16, 17, 17, 16, 13,
  2844. 13, 13, 13, 13, 14, 14, 15, 16, 16, 16, 16, 16, 16, 15, 16, 14,
  2845. 13, 14, 14, 14, 14, 15, 15, 15, 15, 17, 16, 16, 16, 16, 18, 14,
  2846. 15, 14, 14, 14, 15, 15, 16, 16, 16, 18, 17, 17, 17, 19, 17, 14,
  2847. 14, 15, 13, 14, 16, 16, 15, 16, 16, 17, 18, 17, 19, 17, 16, 14,
  2848. 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 12
  2849. ];
  2850. Tables.t16l = [
  2851. 1, 5, 7, 9, 10, 10, 11, 11, 12, 12, 12, 13, 13, 13, 14, 10,
  2852. 4, 6, 8, 9, 10, 11, 11, 11, 12, 12, 12, 13, 14, 13, 14, 10,
  2853. 7, 8, 9, 10, 11, 11, 12, 12, 13, 12, 13, 13, 13, 14, 14, 11,
  2854. 9, 9, 10, 11, 11, 12, 12, 12, 13, 13, 14, 14, 14, 15, 15, 12,
  2855. 10, 10, 11, 11, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 11,
  2856. 10, 10, 11, 11, 12, 13, 13, 14, 13, 14, 14, 15, 15, 15, 16, 12,
  2857. 11, 11, 11, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 16, 12,
  2858. 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 15, 15, 17, 17, 12,
  2859. 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 15, 15, 16, 16, 16, 12,
  2860. 12, 12, 12, 13, 13, 14, 14, 15, 15, 15, 15, 16, 15, 16, 15, 13,
  2861. 12, 13, 12, 13, 14, 14, 14, 14, 15, 16, 16, 16, 17, 17, 16, 12,
  2862. 13, 13, 13, 13, 14, 14, 15, 16, 16, 16, 16, 16, 16, 15, 16, 13,
  2863. 13, 14, 14, 14, 14, 15, 15, 15, 15, 17, 16, 16, 16, 16, 18, 13,
  2864. 15, 14, 14, 14, 15, 15, 16, 16, 16, 18, 17, 17, 17, 19, 17, 13,
  2865. 14, 15, 13, 14, 16, 16, 15, 16, 16, 17, 18, 17, 19, 17, 16, 13,
  2866. 10, 10, 10, 11, 11, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 10
  2867. ];
  2868. Tables.t24l = [
  2869. 4, 5, 7, 8, 9, 10, 10, 11, 11, 12, 12, 12, 12, 12, 13, 10,
  2870. 5, 6, 7, 8, 9, 10, 10, 11, 11, 11, 12, 12, 12, 12, 12, 10,
  2871. 7, 7, 8, 9, 9, 10, 10, 11, 11, 11, 11, 12, 12, 12, 13, 9,
  2872. 8, 8, 9, 9, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 9,
  2873. 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 12, 12, 12, 12, 13, 9,
  2874. 10, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12, 9,
  2875. 10, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 9,
  2876. 11, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 13, 10,
  2877. 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 10,
  2878. 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 10,
  2879. 12, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 10,
  2880. 12, 12, 11, 11, 11, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 10,
  2881. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 10,
  2882. 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 10,
  2883. 13, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 10,
  2884. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 6
  2885. ];
  2886. Tables.t32l = [
  2887. 1 + 0, 4 + 1, 4 + 1, 5 + 2, 4 + 1, 6 + 2, 5 + 2, 6 + 3,
  2888. 4 + 1, 5 + 2, 5 + 2, 6 + 3, 5 + 2, 6 + 3, 6 + 3, 6 + 4
  2889. ];
  2890. Tables.t33l = [
  2891. 4 + 0, 4 + 1, 4 + 1, 4 + 2, 4 + 1, 4 + 2, 4 + 2, 4 + 3,
  2892. 4 + 1, 4 + 2, 4 + 2, 4 + 3, 4 + 2, 4 + 3, 4 + 3, 4 + 4
  2893. ];
  2894. Tables.ht = [
  2895. /* xlen, linmax, table, hlen */
  2896. new HuffCodeTab(0, 0, null, null),
  2897. new HuffCodeTab(2, 0, Tables.t1HB, Tables.t1l),
  2898. new HuffCodeTab(3, 0, Tables.t2HB, Tables.t2l),
  2899. new HuffCodeTab(3, 0, Tables.t3HB, Tables.t3l),
  2900. new HuffCodeTab(0, 0, null, null), /* Apparently not used */
  2901. new HuffCodeTab(4, 0, Tables.t5HB, Tables.t5l),
  2902. new HuffCodeTab(4, 0, Tables.t6HB, Tables.t6l),
  2903. new HuffCodeTab(6, 0, Tables.t7HB, Tables.t7l),
  2904. new HuffCodeTab(6, 0, Tables.t8HB, Tables.t8l),
  2905. new HuffCodeTab(6, 0, Tables.t9HB, Tables.t9l),
  2906. new HuffCodeTab(8, 0, Tables.t10HB, Tables.t10l),
  2907. new HuffCodeTab(8, 0, Tables.t11HB, Tables.t11l),
  2908. new HuffCodeTab(8, 0, Tables.t12HB, Tables.t12l),
  2909. new HuffCodeTab(16, 0, Tables.t13HB, Tables.t13l),
  2910. new HuffCodeTab(0, 0, null, Tables.t16_5l), /* Apparently not used */
  2911. new HuffCodeTab(16, 0, Tables.t15HB, Tables.t15l),
  2912. new HuffCodeTab(1, 1, Tables.t16HB, Tables.t16l),
  2913. new HuffCodeTab(2, 3, Tables.t16HB, Tables.t16l),
  2914. new HuffCodeTab(3, 7, Tables.t16HB, Tables.t16l),
  2915. new HuffCodeTab(4, 15, Tables.t16HB, Tables.t16l),
  2916. new HuffCodeTab(6, 63, Tables.t16HB, Tables.t16l),
  2917. new HuffCodeTab(8, 255, Tables.t16HB, Tables.t16l),
  2918. new HuffCodeTab(10, 1023, Tables.t16HB, Tables.t16l),
  2919. new HuffCodeTab(13, 8191, Tables.t16HB, Tables.t16l),
  2920. new HuffCodeTab(4, 15, Tables.t24HB, Tables.t24l),
  2921. new HuffCodeTab(5, 31, Tables.t24HB, Tables.t24l),
  2922. new HuffCodeTab(6, 63, Tables.t24HB, Tables.t24l),
  2923. new HuffCodeTab(7, 127, Tables.t24HB, Tables.t24l),
  2924. new HuffCodeTab(8, 255, Tables.t24HB, Tables.t24l),
  2925. new HuffCodeTab(9, 511, Tables.t24HB, Tables.t24l),
  2926. new HuffCodeTab(11, 2047, Tables.t24HB, Tables.t24l),
  2927. new HuffCodeTab(13, 8191, Tables.t24HB, Tables.t24l),
  2928. new HuffCodeTab(0, 0, Tables.t32HB, Tables.t32l),
  2929. new HuffCodeTab(0, 0, Tables.t33HB, Tables.t33l),
  2930. ];
  2931. /**
  2932. * <CODE>
  2933. * for (i = 0; i < 16*16; i++) [
  2934. * largetbl[i] = ((ht[16].hlen[i]) << 16) + ht[24].hlen[i];
  2935. * ]
  2936. * </CODE>
  2937. *
  2938. */
  2939. Tables.largetbl = [
  2940. 0x010004, 0x050005, 0x070007, 0x090008, 0x0a0009, 0x0a000a, 0x0b000a, 0x0b000b,
  2941. 0x0c000b, 0x0c000c, 0x0c000c, 0x0d000c, 0x0d000c, 0x0d000c, 0x0e000d, 0x0a000a,
  2942. 0x040005, 0x060006, 0x080007, 0x090008, 0x0a0009, 0x0b000a, 0x0b000a, 0x0b000b,
  2943. 0x0c000b, 0x0c000b, 0x0c000c, 0x0d000c, 0x0e000c, 0x0d000c, 0x0e000c, 0x0a000a,
  2944. 0x070007, 0x080007, 0x090008, 0x0a0009, 0x0b0009, 0x0b000a, 0x0c000a, 0x0c000b,
  2945. 0x0d000b, 0x0c000b, 0x0d000b, 0x0d000c, 0x0d000c, 0x0e000c, 0x0e000d, 0x0b0009,
  2946. 0x090008, 0x090008, 0x0a0009, 0x0b0009, 0x0b000a, 0x0c000a, 0x0c000a, 0x0c000b,
  2947. 0x0d000b, 0x0d000b, 0x0e000b, 0x0e000c, 0x0e000c, 0x0f000c, 0x0f000c, 0x0c0009,
  2948. 0x0a0009, 0x0a0009, 0x0b0009, 0x0b000a, 0x0c000a, 0x0c000a, 0x0d000a, 0x0d000b,
  2949. 0x0d000b, 0x0e000b, 0x0e000c, 0x0e000c, 0x0f000c, 0x0f000c, 0x0f000d, 0x0b0009,
  2950. 0x0a000a, 0x0a0009, 0x0b000a, 0x0b000a, 0x0c000a, 0x0d000a, 0x0d000b, 0x0e000b,
  2951. 0x0d000b, 0x0e000b, 0x0e000c, 0x0f000c, 0x0f000c, 0x0f000c, 0x10000c, 0x0c0009,
  2952. 0x0b000a, 0x0b000a, 0x0b000a, 0x0c000a, 0x0d000a, 0x0d000b, 0x0d000b, 0x0d000b,
  2953. 0x0e000b, 0x0e000c, 0x0e000c, 0x0e000c, 0x0f000c, 0x0f000c, 0x10000d, 0x0c0009,
  2954. 0x0b000b, 0x0b000a, 0x0c000a, 0x0c000a, 0x0d000b, 0x0d000b, 0x0d000b, 0x0e000b,
  2955. 0x0e000c, 0x0f000c, 0x0f000c, 0x0f000c, 0x0f000c, 0x11000d, 0x11000d, 0x0c000a,
  2956. 0x0b000b, 0x0c000b, 0x0c000b, 0x0d000b, 0x0d000b, 0x0d000b, 0x0e000b, 0x0e000b,
  2957. 0x0f000b, 0x0f000c, 0x0f000c, 0x0f000c, 0x10000c, 0x10000d, 0x10000d, 0x0c000a,
  2958. 0x0c000b, 0x0c000b, 0x0c000b, 0x0d000b, 0x0d000b, 0x0e000b, 0x0e000b, 0x0f000c,
  2959. 0x0f000c, 0x0f000c, 0x0f000c, 0x10000c, 0x0f000d, 0x10000d, 0x0f000d, 0x0d000a,
  2960. 0x0c000c, 0x0d000b, 0x0c000b, 0x0d000b, 0x0e000b, 0x0e000c, 0x0e000c, 0x0e000c,
  2961. 0x0f000c, 0x10000c, 0x10000c, 0x10000d, 0x11000d, 0x11000d, 0x10000d, 0x0c000a,
  2962. 0x0d000c, 0x0d000c, 0x0d000b, 0x0d000b, 0x0e000b, 0x0e000c, 0x0f000c, 0x10000c,
  2963. 0x10000c, 0x10000c, 0x10000c, 0x10000d, 0x10000d, 0x0f000d, 0x10000d, 0x0d000a,
  2964. 0x0d000c, 0x0e000c, 0x0e000c, 0x0e000c, 0x0e000c, 0x0f000c, 0x0f000c, 0x0f000c,
  2965. 0x0f000c, 0x11000c, 0x10000d, 0x10000d, 0x10000d, 0x10000d, 0x12000d, 0x0d000a,
  2966. 0x0f000c, 0x0e000c, 0x0e000c, 0x0e000c, 0x0f000c, 0x0f000c, 0x10000c, 0x10000c,
  2967. 0x10000d, 0x12000d, 0x11000d, 0x11000d, 0x11000d, 0x13000d, 0x11000d, 0x0d000a,
  2968. 0x0e000d, 0x0f000c, 0x0d000c, 0x0e000c, 0x10000c, 0x10000c, 0x0f000c, 0x10000d,
  2969. 0x10000d, 0x11000d, 0x12000d, 0x11000d, 0x13000d, 0x11000d, 0x10000d, 0x0d000a,
  2970. 0x0a0009, 0x0a0009, 0x0a0009, 0x0b0009, 0x0b0009, 0x0c0009, 0x0c0009, 0x0c0009,
  2971. 0x0d0009, 0x0d0009, 0x0d0009, 0x0d000a, 0x0d000a, 0x0d000a, 0x0d000a, 0x0a0006
  2972. ];
  2973. /**
  2974. * <CODE>
  2975. * for (i = 0; i < 3*3; i++) [
  2976. * table23[i] = ((ht[2].hlen[i]) << 16) + ht[3].hlen[i];
  2977. * ]
  2978. * </CODE>
  2979. *
  2980. */
  2981. Tables.table23 = [
  2982. 0x010002, 0x040003, 0x070007,
  2983. 0x040004, 0x050004, 0x070007,
  2984. 0x060006, 0x070007, 0x080008
  2985. ];
  2986. /**
  2987. * <CODE>
  2988. * for (i = 0; i < 4*4; i++) [
  2989. * table56[i] = ((ht[5].hlen[i]) << 16) + ht[6].hlen[i];
  2990. * ]
  2991. * </CODE>
  2992. *
  2993. */
  2994. Tables.table56 = [
  2995. 0x010003, 0x040004, 0x070006, 0x080008, 0x040004, 0x050004, 0x080006, 0x090007,
  2996. 0x070005, 0x080006, 0x090007, 0x0a0008, 0x080007, 0x080007, 0x090008, 0x0a0009
  2997. ];
  2998. Tables.bitrate_table = [
  2999. [0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, -1], /* MPEG 2 */
  3000. [0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, -1], /* MPEG 1 */
  3001. [0, 8, 16, 24, 32, 40, 48, 56, 64, -1, -1, -1, -1, -1, -1, -1], /* MPEG 2.5 */
  3002. ];
  3003. /**
  3004. * MPEG 2, MPEG 1, MPEG 2.5.
  3005. */
  3006. Tables.samplerate_table = [
  3007. [22050, 24000, 16000, -1],
  3008. [44100, 48000, 32000, -1],
  3009. [11025, 12000, 8000, -1],
  3010. ];
  3011. /**
  3012. * This is the scfsi_band table from 2.4.2.7 of the IS.
  3013. */
  3014. Tables.scfsi_band = [0, 6, 11, 16, 21];
  3015. function MeanBits(meanBits) {
  3016. this.bits = meanBits;
  3017. }
  3018. //package mp3;
  3019. function CalcNoiseResult() {
  3020. /**
  3021. * sum of quantization noise > masking
  3022. */
  3023. this.over_noise = 0.;
  3024. /**
  3025. * sum of all quantization noise
  3026. */
  3027. this.tot_noise = 0.;
  3028. /**
  3029. * max quantization noise
  3030. */
  3031. this.max_noise = 0.;
  3032. /**
  3033. * number of quantization noise > masking
  3034. */
  3035. this.over_count = 0;
  3036. /**
  3037. * SSD-like cost of distorted bands
  3038. */
  3039. this.over_SSD = 0;
  3040. this.bits = 0;
  3041. }
  3042. function VBRQuantize() {
  3043. var qupvt;
  3044. var tak;
  3045. this.setModules = function (_qupvt, _tk) {
  3046. qupvt = _qupvt;
  3047. tak = _tk;
  3048. }
  3049. //TODO
  3050. }
  3051. /**
  3052. * ATH related stuff, if something new ATH related has to be added, please plug
  3053. * it here into the ATH.
  3054. */
  3055. function ATH() {
  3056. /**
  3057. * Method for the auto adjustment.
  3058. */
  3059. this.useAdjust = 0;
  3060. /**
  3061. * factor for tuning the (sample power) point below which adaptive threshold
  3062. * of hearing adjustment occurs
  3063. */
  3064. this.aaSensitivityP = 0.;
  3065. /**
  3066. * Lowering based on peak volume, 1 = no lowering.
  3067. */
  3068. this.adjust = 0.;
  3069. /**
  3070. * Limit for dynamic ATH adjust.
  3071. */
  3072. this.adjustLimit = 0.;
  3073. /**
  3074. * Determined to lower x dB each second.
  3075. */
  3076. this.decay = 0.;
  3077. /**
  3078. * Lowest ATH value.
  3079. */
  3080. this.floor = 0.;
  3081. /**
  3082. * ATH for sfbs in long blocks.
  3083. */
  3084. this.l = new_float(Encoder.SBMAX_l);
  3085. /**
  3086. * ATH for sfbs in short blocks.
  3087. */
  3088. this.s = new_float(Encoder.SBMAX_s);
  3089. /**
  3090. * ATH for partitioned sfb21 in long blocks.
  3091. */
  3092. this.psfb21 = new_float(Encoder.PSFB21);
  3093. /**
  3094. * ATH for partitioned sfb12 in short blocks.
  3095. */
  3096. this.psfb12 = new_float(Encoder.PSFB12);
  3097. /**
  3098. * ATH for long block convolution bands.
  3099. */
  3100. this.cb_l = new_float(Encoder.CBANDS);
  3101. /**
  3102. * ATH for short block convolution bands.
  3103. */
  3104. this.cb_s = new_float(Encoder.CBANDS);
  3105. /**
  3106. * Equal loudness weights (based on ATH).
  3107. */
  3108. this.eql_w = new_float(Encoder.BLKSIZE / 2);
  3109. }
  3110. function LameGlobalFlags() {
  3111. this.class_id = 0;
  3112. /* input description */
  3113. /**
  3114. * number of samples. default=-1
  3115. */
  3116. this.num_samples = 0;
  3117. /**
  3118. * input number of channels. default=2
  3119. */
  3120. this.num_channels = 0;
  3121. /**
  3122. * input_samp_rate in Hz. default=44.1 kHz
  3123. */
  3124. this.in_samplerate = 0;
  3125. /**
  3126. * output_samp_rate. default: LAME picks best value at least not used for
  3127. * MP3 decoding: Remember 44.1 kHz MP3s and AC97
  3128. */
  3129. this.out_samplerate = 0;
  3130. /**
  3131. * scale input by this amount before encoding at least not used for MP3
  3132. * decoding
  3133. */
  3134. this.scale = 0.;
  3135. /**
  3136. * scale input of channel 0 (left) by this amount before encoding
  3137. */
  3138. this.scale_left = 0.;
  3139. /**
  3140. * scale input of channel 1 (right) by this amount before encoding
  3141. */
  3142. this.scale_right = 0.;
  3143. /* general control params */
  3144. /**
  3145. * collect data for a MP3 frame analyzer?
  3146. */
  3147. this.analysis = false;
  3148. /**
  3149. * add Xing VBR tag?
  3150. */
  3151. this.bWriteVbrTag = false;
  3152. /**
  3153. * use lame/mpglib to convert mp3 to wav
  3154. */
  3155. this.decode_only = false;
  3156. /**
  3157. * quality setting 0=best, 9=worst default=5
  3158. */
  3159. this.quality = 0;
  3160. /**
  3161. * see enum default = LAME picks best value
  3162. */
  3163. this.mode = MPEGMode.STEREO;
  3164. /**
  3165. * force M/S mode. requires mode=1
  3166. */
  3167. this.force_ms = false;
  3168. /**
  3169. * use free format? default=0
  3170. */
  3171. this.free_format = false;
  3172. /**
  3173. * find the RG value? default=0
  3174. */
  3175. this.findReplayGain = false;
  3176. /**
  3177. * decode on the fly? default=0
  3178. */
  3179. this.decode_on_the_fly = false;
  3180. /**
  3181. * 1 (default) writes ID3 tags, 0 not
  3182. */
  3183. this.write_id3tag_automatic = false;
  3184. /*
  3185. * set either brate>0 or compression_ratio>0, LAME will compute the value of
  3186. * the variable not set. Default is compression_ratio = 11.025
  3187. */
  3188. /**
  3189. * bitrate
  3190. */
  3191. this.brate = 0;
  3192. /**
  3193. * sizeof(wav file)/sizeof(mp3 file)
  3194. */
  3195. this.compression_ratio = 0.;
  3196. /* frame params */
  3197. /**
  3198. * mark as copyright. default=0
  3199. */
  3200. this.copyright = 0;
  3201. /**
  3202. * mark as original. default=1
  3203. */
  3204. this.original = 0;
  3205. /**
  3206. * the MP3 'private extension' bit. Meaningless
  3207. */
  3208. this.extension = 0;
  3209. /**
  3210. * Input PCM is emphased PCM (for instance from one of the rarely emphased
  3211. * CDs), it is STRONGLY not recommended to use this, because psycho does not
  3212. * take it into account, and last but not least many decoders don't care
  3213. * about these bits
  3214. */
  3215. this.emphasis = 0;
  3216. /**
  3217. * use 2 bytes per frame for a CRC checksum. default=0
  3218. */
  3219. this.error_protection = 0;
  3220. /**
  3221. * enforce ISO spec as much as possible
  3222. */
  3223. this.strict_ISO = false;
  3224. /**
  3225. * use bit reservoir?
  3226. */
  3227. this.disable_reservoir = false;
  3228. /* quantization/noise shaping */
  3229. this.quant_comp = 0;
  3230. this.quant_comp_short = 0;
  3231. this.experimentalY = false;
  3232. this.experimentalZ = 0;
  3233. this.exp_nspsytune = 0;
  3234. this.preset = 0;
  3235. /* VBR control */
  3236. this.VBR = null;
  3237. /**
  3238. * Range [0,...,1[
  3239. */
  3240. this.VBR_q_frac = 0.;
  3241. /**
  3242. * Range [0,...,9]
  3243. */
  3244. this.VBR_q = 0;
  3245. this.VBR_mean_bitrate_kbps = 0;
  3246. this.VBR_min_bitrate_kbps = 0;
  3247. this.VBR_max_bitrate_kbps = 0;
  3248. /**
  3249. * strictly enforce VBR_min_bitrate normaly, it will be violated for analog
  3250. * silence
  3251. */
  3252. this.VBR_hard_min = 0;
  3253. /* resampling and filtering */
  3254. /**
  3255. * freq in Hz. 0=lame choses. -1=no filter
  3256. */
  3257. this.lowpassfreq = 0;
  3258. /**
  3259. * freq in Hz. 0=lame choses. -1=no filter
  3260. */
  3261. this.highpassfreq = 0;
  3262. /**
  3263. * freq width of filter, in Hz (default=15%)
  3264. */
  3265. this.lowpasswidth = 0;
  3266. /**
  3267. * freq width of filter, in Hz (default=15%)
  3268. */
  3269. this.highpasswidth = 0;
  3270. /*
  3271. * psycho acoustics and other arguments which you should not change unless
  3272. * you know what you are doing
  3273. */
  3274. this.maskingadjust = 0.;
  3275. this.maskingadjust_short = 0.;
  3276. /**
  3277. * only use ATH
  3278. */
  3279. this.ATHonly = false;
  3280. /**
  3281. * only use ATH for short blocks
  3282. */
  3283. this.ATHshort = false;
  3284. /**
  3285. * disable ATH
  3286. */
  3287. this.noATH = false;
  3288. /**
  3289. * select ATH formula
  3290. */
  3291. this.ATHtype = 0;
  3292. /**
  3293. * change ATH formula 4 shape
  3294. */
  3295. this.ATHcurve = 0.;
  3296. /**
  3297. * lower ATH by this many db
  3298. */
  3299. this.ATHlower = 0.;
  3300. /**
  3301. * select ATH auto-adjust scheme
  3302. */
  3303. this.athaa_type = 0;
  3304. /**
  3305. * select ATH auto-adjust loudness calc
  3306. */
  3307. this.athaa_loudapprox = 0;
  3308. /**
  3309. * dB, tune active region of auto-level
  3310. */
  3311. this.athaa_sensitivity = 0.;
  3312. this.short_blocks = null;
  3313. /**
  3314. * use temporal masking effect
  3315. */
  3316. this.useTemporal = false;
  3317. this.interChRatio = 0.;
  3318. /**
  3319. * Naoki's adjustment of Mid/Side maskings
  3320. */
  3321. this.msfix = 0.;
  3322. /**
  3323. * 0 off, 1 on
  3324. */
  3325. this.tune = false;
  3326. /**
  3327. * used to pass values for debugging and stuff
  3328. */
  3329. this.tune_value_a = 0.;
  3330. /************************************************************************/
  3331. /* internal variables, do not set... */
  3332. /* provided because they may be of use to calling application */
  3333. /************************************************************************/
  3334. /**
  3335. * 0=MPEG-2/2.5 1=MPEG-1
  3336. */
  3337. this.version = 0;
  3338. this.encoder_delay = 0;
  3339. /**
  3340. * number of samples of padding appended to input
  3341. */
  3342. this.encoder_padding = 0;
  3343. this.framesize = 0;
  3344. /**
  3345. * number of frames encoded
  3346. */
  3347. this.frameNum = 0;
  3348. /**
  3349. * is this struct owned by calling program or lame?
  3350. */
  3351. this.lame_allocated_gfp = 0;
  3352. /**************************************************************************/
  3353. /* more internal variables are stored in this structure: */
  3354. /**************************************************************************/
  3355. this.internal_flags = null;
  3356. }
  3357. function CBRNewIterationLoop(_quantize) {
  3358. var quantize = _quantize;
  3359. this.quantize = quantize;
  3360. this.iteration_loop = function(gfp, pe, ms_ener_ratio, ratio) {
  3361. var gfc = gfp.internal_flags;
  3362. var l3_xmin = new_float(L3Side.SFBMAX);
  3363. var xrpow = new_float(576);
  3364. var targ_bits = new_int(2);
  3365. var mean_bits = 0, max_bits;
  3366. var l3_side = gfc.l3_side;
  3367. var mb = new MeanBits(mean_bits);
  3368. this.quantize.rv.ResvFrameBegin(gfp, mb);
  3369. mean_bits = mb.bits;
  3370. /* quantize! */
  3371. for (var gr = 0; gr < gfc.mode_gr; gr++) {
  3372. /*
  3373. * calculate needed bits
  3374. */
  3375. max_bits = this.quantize.qupvt.on_pe(gfp, pe, targ_bits, mean_bits,
  3376. gr, gr);
  3377. if (gfc.mode_ext == Encoder.MPG_MD_MS_LR) {
  3378. abort();//fix cc 精简
  3379. }
  3380. for (var ch = 0; ch < gfc.channels_out; ch++) {
  3381. var adjust, masking_lower_db;
  3382. var cod_info = l3_side.tt[gr][ch];
  3383. if (cod_info.block_type != Encoder.SHORT_TYPE) {
  3384. // NORM, START or STOP type
  3385. adjust = 0;
  3386. masking_lower_db = gfc.PSY.mask_adjust - adjust;
  3387. } else {
  3388. adjust = 0;
  3389. masking_lower_db = gfc.PSY.mask_adjust_short - adjust;
  3390. }
  3391. gfc.masking_lower = Math.pow(10.0,
  3392. masking_lower_db * 0.1);
  3393. /*
  3394. * init_outer_loop sets up cod_info, scalefac and xrpow
  3395. */
  3396. this.quantize.init_outer_loop(gfc, cod_info);
  3397. if (this.quantize.init_xrpow(gfc, cod_info, xrpow)) {
  3398. /*
  3399. * xr contains energy we will have to encode calculate the
  3400. * masking abilities find some good quantization in
  3401. * outer_loop
  3402. */
  3403. this.quantize.qupvt.calc_xmin(gfp, ratio[gr][ch], cod_info,
  3404. l3_xmin);
  3405. this.quantize.outer_loop(gfp, cod_info, l3_xmin, xrpow, ch,
  3406. targ_bits[ch]);
  3407. }
  3408. this.quantize.iteration_finish_one(gfc, gr, ch);
  3409. } /* for ch */
  3410. } /* for gr */
  3411. this.quantize.rv.ResvFrameEnd(gfc, mean_bits);
  3412. }
  3413. }
  3414. function ReplayGain() {
  3415. //fix 精简
  3416. }
  3417. //package mp3;
  3418. /**
  3419. * Layer III side information.
  3420. *
  3421. * @author Ken
  3422. *
  3423. */
  3424. function ScaleFac(arrL, arrS, arr21, arr12) {
  3425. this.l = new_int(1 + Encoder.SBMAX_l);
  3426. this.s = new_int(1 + Encoder.SBMAX_s);
  3427. this.psfb21 = new_int(1 + Encoder.PSFB21);
  3428. this.psfb12 = new_int(1 + Encoder.PSFB12);
  3429. var l = this.l;
  3430. var s = this.s;
  3431. if (arguments.length == 4) {
  3432. //public ScaleFac(final int[] arrL, final int[] arrS, final int[] arr21,
  3433. // final int[] arr12) {
  3434. this.arrL = arguments[0];
  3435. this.arrS = arguments[1];
  3436. this.arr21 = arguments[2];
  3437. this.arr12 = arguments[3];
  3438. System.arraycopy(this.arrL, 0, l, 0, Math.min(this.arrL.length, this.l.length));
  3439. System.arraycopy(this.arrS, 0, s, 0, Math.min(this.arrS.length, this.s.length));
  3440. System.arraycopy(this.arr21, 0, this.psfb21, 0, Math.min(this.arr21.length, this.psfb21.length));
  3441. System.arraycopy(this.arr12, 0, this.psfb12, 0, Math.min(this.arr12.length, this.psfb12.length));
  3442. }
  3443. }
  3444. /*
  3445. * quantize_pvt source file
  3446. *
  3447. * Copyright (c) 1999-2002 Takehiro Tominaga
  3448. * Copyright (c) 2000-2002 Robert Hegemann
  3449. * Copyright (c) 2001 Naoki Shibata
  3450. * Copyright (c) 2002-2005 Gabriel Bouvigne
  3451. *
  3452. * This library is free software; you can redistribute it and/or
  3453. * modify it under the terms of the GNU Lesser General Public
  3454. * License as published by the Free Software Foundation; either
  3455. * version 2 of the License, or (at your option) any later version.
  3456. *
  3457. * This library is distributed in the hope that it will be useful,
  3458. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3459. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  3460. * Library General Public License for more details.
  3461. *
  3462. * You should have received a copy of the GNU Lesser General Public
  3463. * License along with this library; if not, write to the
  3464. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  3465. * Boston, MA 02111-1307, USA.
  3466. */
  3467. /* $Id: QuantizePVT.java,v 1.24 2011/05/24 20:48:06 kenchis Exp $ */
  3468. QuantizePVT.Q_MAX = (256 + 1);
  3469. QuantizePVT.Q_MAX2 = 116;
  3470. QuantizePVT.LARGE_BITS = 100000;
  3471. QuantizePVT.IXMAX_VAL = 8206;
  3472. function QuantizePVT() {
  3473. var tak = null;
  3474. var rv = null;
  3475. var psy = null;
  3476. this.setModules = function (_tk, _rv, _psy) {
  3477. tak = _tk;
  3478. rv = _rv;
  3479. psy = _psy;
  3480. };
  3481. function POW20(x) {
  3482. return pow20[x + QuantizePVT.Q_MAX2];
  3483. }
  3484. this.IPOW20 = function (x) {
  3485. return ipow20[x];
  3486. }
  3487. /**
  3488. * smallest such that 1.0+DBL_EPSILON != 1.0
  3489. */
  3490. var DBL_EPSILON = 2.2204460492503131e-016;
  3491. /**
  3492. * ix always <= 8191+15. see count_bits()
  3493. */
  3494. var IXMAX_VAL = QuantizePVT.IXMAX_VAL;
  3495. var PRECALC_SIZE = (IXMAX_VAL + 2);
  3496. var Q_MAX = QuantizePVT.Q_MAX;
  3497. /**
  3498. * <CODE>
  3499. * minimum possible number of
  3500. * -cod_info.global_gain + ((scalefac[] + (cod_info.preflag ? pretab[sfb] : 0))
  3501. * << (cod_info.scalefac_scale + 1)) + cod_info.subblock_gain[cod_info.window[sfb]] * 8;
  3502. *
  3503. * for long block, 0+((15+3)<<2) = 18*4 = 72
  3504. * for short block, 0+(15<<2)+7*8 = 15*4+56 = 116
  3505. * </CODE>
  3506. */
  3507. var Q_MAX2 = QuantizePVT.Q_MAX2;
  3508. var LARGE_BITS = QuantizePVT.LARGE_BITS;
  3509. /**
  3510. * Assuming dynamic range=96dB, this value should be 92
  3511. */
  3512. var NSATHSCALE = 100;
  3513. /**
  3514. * The following table is used to implement the scalefactor partitioning for
  3515. * MPEG2 as described in section 2.4.3.2 of the IS. The indexing corresponds
  3516. * to the way the tables are presented in the IS:
  3517. *
  3518. * [table_number][row_in_table][column of nr_of_sfb]
  3519. */
  3520. this.nr_of_sfb_block = [
  3521. [[6, 5, 5, 5], [9, 9, 9, 9], [6, 9, 9, 9]],
  3522. [[6, 5, 7, 3], [9, 9, 12, 6], [6, 9, 12, 6]],
  3523. [[11, 10, 0, 0], [18, 18, 0, 0], [15, 18, 0, 0]],
  3524. [[7, 7, 7, 0], [12, 12, 12, 0], [6, 15, 12, 0]],
  3525. [[6, 6, 6, 3], [12, 9, 9, 6], [6, 12, 9, 6]],
  3526. [[8, 8, 5, 0], [15, 12, 9, 0], [6, 18, 9, 0]]];
  3527. /**
  3528. * Table B.6: layer3 preemphasis
  3529. */
  3530. var pretab = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
  3531. 2, 2, 3, 3, 3, 2, 0];
  3532. this.pretab = pretab;
  3533. /**
  3534. * Here are MPEG1 Table B.8 and MPEG2 Table B.1 -- Layer III scalefactor
  3535. * bands. <BR>
  3536. * Index into this using a method such as:<BR>
  3537. * idx = fr_ps.header.sampling_frequency + (fr_ps.header.version * 3)
  3538. */
  3539. this.sfBandIndex = [
  3540. // Table B.2.b: 22.05 kHz
  3541. new ScaleFac([0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464,
  3542. 522, 576],
  3543. [0, 4, 8, 12, 18, 24, 32, 42, 56, 74, 100, 132, 174, 192]
  3544. , [0, 0, 0, 0, 0, 0, 0] // sfb21 pseudo sub bands
  3545. , [0, 0, 0, 0, 0, 0, 0] // sfb12 pseudo sub bands
  3546. ),
  3547. /* Table B.2.c: 24 kHz */ /* docs: 332. mpg123(broken): 330 */
  3548. new ScaleFac([0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 114, 136, 162, 194, 232, 278, 332, 394, 464,
  3549. 540, 576],
  3550. [0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 136, 180, 192]
  3551. , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */
  3552. , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */
  3553. ),
  3554. /* Table B.2.a: 16 kHz */
  3555. new ScaleFac([0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464,
  3556. 522, 576],
  3557. [0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 134, 174, 192]
  3558. , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */
  3559. , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */
  3560. ),
  3561. /* Table B.8.b: 44.1 kHz */
  3562. new ScaleFac([0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 52, 62, 74, 90, 110, 134, 162, 196, 238, 288, 342, 418,
  3563. 576],
  3564. [0, 4, 8, 12, 16, 22, 30, 40, 52, 66, 84, 106, 136, 192]
  3565. , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */
  3566. , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */
  3567. ),
  3568. /* Table B.8.c: 48 kHz */
  3569. new ScaleFac([0, 4, 8, 12, 16, 20, 24, 30, 36, 42, 50, 60, 72, 88, 106, 128, 156, 190, 230, 276, 330, 384,
  3570. 576],
  3571. [0, 4, 8, 12, 16, 22, 28, 38, 50, 64, 80, 100, 126, 192]
  3572. , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */
  3573. , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */
  3574. ),
  3575. /* Table B.8.a: 32 kHz */
  3576. new ScaleFac([0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 54, 66, 82, 102, 126, 156, 194, 240, 296, 364, 448, 550,
  3577. 576],
  3578. [0, 4, 8, 12, 16, 22, 30, 42, 58, 78, 104, 138, 180, 192]
  3579. , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */
  3580. , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */
  3581. ),
  3582. /* MPEG-2.5 11.025 kHz */
  3583. new ScaleFac([0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464,
  3584. 522, 576],
  3585. [0 / 3, 12 / 3, 24 / 3, 36 / 3, 54 / 3, 78 / 3, 108 / 3, 144 / 3, 186 / 3, 240 / 3, 312 / 3,
  3586. 402 / 3, 522 / 3, 576 / 3]
  3587. , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */
  3588. , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */
  3589. ),
  3590. /* MPEG-2.5 12 kHz */
  3591. new ScaleFac([0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464,
  3592. 522, 576],
  3593. [0 / 3, 12 / 3, 24 / 3, 36 / 3, 54 / 3, 78 / 3, 108 / 3, 144 / 3, 186 / 3, 240 / 3, 312 / 3,
  3594. 402 / 3, 522 / 3, 576 / 3]
  3595. , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */
  3596. , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */
  3597. ),
  3598. /* MPEG-2.5 8 kHz */
  3599. new ScaleFac([0, 12, 24, 36, 48, 60, 72, 88, 108, 132, 160, 192, 232, 280, 336, 400, 476, 566, 568, 570,
  3600. 572, 574, 576],
  3601. [0 / 3, 24 / 3, 48 / 3, 72 / 3, 108 / 3, 156 / 3, 216 / 3, 288 / 3, 372 / 3, 480 / 3, 486 / 3,
  3602. 492 / 3, 498 / 3, 576 / 3]
  3603. , [0, 0, 0, 0, 0, 0, 0] /* sfb21 pseudo sub bands */
  3604. , [0, 0, 0, 0, 0, 0, 0] /* sfb12 pseudo sub bands */
  3605. )
  3606. ];
  3607. var pow20 = new_float(Q_MAX + Q_MAX2 + 1);
  3608. var ipow20 = new_float(Q_MAX);
  3609. var pow43 = new_float(PRECALC_SIZE);
  3610. var adj43 = new_float(PRECALC_SIZE);
  3611. this.adj43 = adj43;
  3612. /**
  3613. * <PRE>
  3614. * compute the ATH for each scalefactor band cd range: 0..96db
  3615. *
  3616. * Input: 3.3kHz signal 32767 amplitude (3.3kHz is where ATH is smallest =
  3617. * -5db) longblocks: sfb=12 en0/bw=-11db max_en0 = 1.3db shortblocks: sfb=5
  3618. * -9db 0db
  3619. *
  3620. * Input: 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 (repeated) longblocks: amp=1
  3621. * sfb=12 en0/bw=-103 db max_en0 = -92db amp=32767 sfb=12 -12 db -1.4db
  3622. *
  3623. * Input: 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 (repeated) shortblocks: amp=1
  3624. * sfb=5 en0/bw= -99 -86 amp=32767 sfb=5 -9 db 4db
  3625. *
  3626. *
  3627. * MAX energy of largest wave at 3.3kHz = 1db AVE energy of largest wave at
  3628. * 3.3kHz = -11db Let's take AVE: -11db = maximum signal in sfb=12. Dynamic
  3629. * range of CD: 96db. Therefor energy of smallest audible wave in sfb=12 =
  3630. * -11 - 96 = -107db = ATH at 3.3kHz.
  3631. *
  3632. * ATH formula for this wave: -5db. To adjust to LAME scaling, we need ATH =
  3633. * ATH_formula - 103 (db) ATH = ATH * 2.5e-10 (ener)
  3634. * </PRE>
  3635. */
  3636. function ATHmdct(gfp, f) {
  3637. var ath = psy.ATHformula(f, gfp);
  3638. ath -= NSATHSCALE;
  3639. /* modify the MDCT scaling for the ATH and convert to energy */
  3640. ath = Math.pow(10.0, ath / 10.0 + gfp.ATHlower);
  3641. return ath;
  3642. }
  3643. function compute_ath(gfp) {
  3644. var ATH_l = gfp.internal_flags.ATH.l;
  3645. var ATH_psfb21 = gfp.internal_flags.ATH.psfb21;
  3646. var ATH_s = gfp.internal_flags.ATH.s;
  3647. var ATH_psfb12 = gfp.internal_flags.ATH.psfb12;
  3648. var gfc = gfp.internal_flags;
  3649. var samp_freq = gfp.out_samplerate;
  3650. for (var sfb = 0; sfb < Encoder.SBMAX_l; sfb++) {
  3651. var start = gfc.scalefac_band.l[sfb];
  3652. var end = gfc.scalefac_band.l[sfb + 1];
  3653. ATH_l[sfb] = Float.MAX_VALUE;
  3654. for (var i = start; i < end; i++) {
  3655. var freq = i * samp_freq / (2 * 576);
  3656. var ATH_f = ATHmdct(gfp, freq);
  3657. /* freq in kHz */
  3658. ATH_l[sfb] = Math.min(ATH_l[sfb], ATH_f);
  3659. }
  3660. }
  3661. for (var sfb = 0; sfb < Encoder.PSFB21; sfb++) {
  3662. var start = gfc.scalefac_band.psfb21[sfb];
  3663. var end = gfc.scalefac_band.psfb21[sfb + 1];
  3664. ATH_psfb21[sfb] = Float.MAX_VALUE;
  3665. for (var i = start; i < end; i++) {
  3666. var freq = i * samp_freq / (2 * 576);
  3667. var ATH_f = ATHmdct(gfp, freq);
  3668. /* freq in kHz */
  3669. ATH_psfb21[sfb] = Math.min(ATH_psfb21[sfb], ATH_f);
  3670. }
  3671. }
  3672. for (var sfb = 0; sfb < Encoder.SBMAX_s; sfb++) {
  3673. var start = gfc.scalefac_band.s[sfb];
  3674. var end = gfc.scalefac_band.s[sfb + 1];
  3675. ATH_s[sfb] = Float.MAX_VALUE;
  3676. for (var i = start; i < end; i++) {
  3677. var freq = i * samp_freq / (2 * 192);
  3678. var ATH_f = ATHmdct(gfp, freq);
  3679. /* freq in kHz */
  3680. ATH_s[sfb] = Math.min(ATH_s[sfb], ATH_f);
  3681. }
  3682. ATH_s[sfb] *= (gfc.scalefac_band.s[sfb + 1] - gfc.scalefac_band.s[sfb]);
  3683. }
  3684. for (var sfb = 0; sfb < Encoder.PSFB12; sfb++) {
  3685. var start = gfc.scalefac_band.psfb12[sfb];
  3686. var end = gfc.scalefac_band.psfb12[sfb + 1];
  3687. ATH_psfb12[sfb] = Float.MAX_VALUE;
  3688. for (var i = start; i < end; i++) {
  3689. var freq = i * samp_freq / (2 * 192);
  3690. var ATH_f = ATHmdct(gfp, freq);
  3691. /* freq in kHz */
  3692. ATH_psfb12[sfb] = Math.min(ATH_psfb12[sfb], ATH_f);
  3693. }
  3694. /* not sure about the following */
  3695. ATH_psfb12[sfb] *= (gfc.scalefac_band.s[13] - gfc.scalefac_band.s[12]);
  3696. }
  3697. /*
  3698. * no-ATH mode: reduce ATH to -200 dB
  3699. */
  3700. if (gfp.noATH) {
  3701. abort();//fix cc 精简
  3702. }
  3703. /*
  3704. * work in progress, don't rely on it too much
  3705. */
  3706. gfc.ATH.floor = 10. * Math_log10(ATHmdct(gfp, -1.));
  3707. }
  3708. /**
  3709. * initialization for iteration_loop
  3710. */
  3711. this.iteration_init = function (gfp) {
  3712. var gfc = gfp.internal_flags;
  3713. var l3_side = gfc.l3_side;
  3714. var i;
  3715. if (gfc.iteration_init_init == 0) {
  3716. gfc.iteration_init_init = 1;
  3717. l3_side.main_data_begin = 0;
  3718. compute_ath(gfp);
  3719. pow43[0] = 0.0;
  3720. for (i = 1; i < PRECALC_SIZE; i++)
  3721. pow43[i] = Math.pow(i, 4.0 / 3.0);
  3722. for (i = 0; i < PRECALC_SIZE - 1; i++)
  3723. adj43[i] = ((i + 1) - Math.pow(
  3724. 0.5 * (pow43[i] + pow43[i + 1]), 0.75));
  3725. adj43[i] = 0.5;
  3726. for (i = 0; i < Q_MAX; i++)
  3727. ipow20[i] = Math.pow(2.0, (i - 210) * -0.1875);
  3728. for (i = 0; i <= Q_MAX + Q_MAX2; i++)
  3729. pow20[i] = Math.pow(2.0, (i - 210 - Q_MAX2) * 0.25);
  3730. tak.huffman_init(gfc);
  3731. {
  3732. var bass, alto, treble, sfb21;
  3733. i = (gfp.exp_nspsytune >> 2) & 63;
  3734. if (i >= 32)
  3735. i -= 64;
  3736. bass = Math.pow(10, i / 4.0 / 10.0);
  3737. i = (gfp.exp_nspsytune >> 8) & 63;
  3738. if (i >= 32)
  3739. i -= 64;
  3740. alto = Math.pow(10, i / 4.0 / 10.0);
  3741. i = (gfp.exp_nspsytune >> 14) & 63;
  3742. if (i >= 32)
  3743. i -= 64;
  3744. treble = Math.pow(10, i / 4.0 / 10.0);
  3745. /*
  3746. * to be compatible with Naoki's original code, the next 6 bits
  3747. * define only the amount of changing treble for sfb21
  3748. */
  3749. i = (gfp.exp_nspsytune >> 20) & 63;
  3750. if (i >= 32)
  3751. i -= 64;
  3752. sfb21 = treble * Math.pow(10, i / 4.0 / 10.0);
  3753. for (i = 0; i < Encoder.SBMAX_l; i++) {
  3754. var f;
  3755. if (i <= 6)
  3756. f = bass;
  3757. else if (i <= 13)
  3758. f = alto;
  3759. else if (i <= 20)
  3760. f = treble;
  3761. else
  3762. f = sfb21;
  3763. gfc.nsPsy.longfact[i] = f;
  3764. }
  3765. for (i = 0; i < Encoder.SBMAX_s; i++) {
  3766. var f;
  3767. if (i <= 5)
  3768. f = bass;
  3769. else if (i <= 10)
  3770. f = alto;
  3771. else if (i <= 11)
  3772. f = treble;
  3773. else
  3774. f = sfb21;
  3775. gfc.nsPsy.shortfact[i] = f;
  3776. }
  3777. }
  3778. }
  3779. }
  3780. /**
  3781. * allocate bits among 2 channels based on PE<BR>
  3782. * mt 6/99<BR>
  3783. * bugfixes rh 8/01: often allocated more than the allowed 4095 bits
  3784. */
  3785. this.on_pe = function (gfp, pe,
  3786. targ_bits, mean_bits, gr, cbr) {
  3787. var gfc = gfp.internal_flags;
  3788. var tbits = 0, bits;
  3789. var add_bits = new_int(2);
  3790. var ch;
  3791. /* allocate targ_bits for granule */
  3792. var mb = new MeanBits(tbits);
  3793. var extra_bits = rv.ResvMaxBits(gfp, mean_bits, mb, cbr);
  3794. tbits = mb.bits;
  3795. /* maximum allowed bits for this granule */
  3796. var max_bits = tbits + extra_bits;
  3797. if (max_bits > LameInternalFlags.MAX_BITS_PER_GRANULE) {
  3798. // hard limit per granule
  3799. max_bits = LameInternalFlags.MAX_BITS_PER_GRANULE;
  3800. }
  3801. for (bits = 0, ch = 0; ch < gfc.channels_out; ++ch) {
  3802. /******************************************************************
  3803. * allocate bits for each channel
  3804. ******************************************************************/
  3805. targ_bits[ch] = Math.min(LameInternalFlags.MAX_BITS_PER_CHANNEL,
  3806. tbits / gfc.channels_out);
  3807. add_bits[ch] = 0 | (targ_bits[ch] * pe[gr][ch] / 700.0 - targ_bits[ch]);
  3808. /* at most increase bits by 1.5*average */
  3809. if (add_bits[ch] > mean_bits * 3 / 4)
  3810. add_bits[ch] = mean_bits * 3 / 4;
  3811. if (add_bits[ch] < 0)
  3812. add_bits[ch] = 0;
  3813. if (add_bits[ch] + targ_bits[ch] > LameInternalFlags.MAX_BITS_PER_CHANNEL)
  3814. add_bits[ch] = Math.max(0,
  3815. LameInternalFlags.MAX_BITS_PER_CHANNEL - targ_bits[ch]);
  3816. bits += add_bits[ch];
  3817. }
  3818. if (bits > extra_bits) {
  3819. for (ch = 0; ch < gfc.channels_out; ++ch) {
  3820. add_bits[ch] = extra_bits * add_bits[ch] / bits;
  3821. }
  3822. }
  3823. for (ch = 0; ch < gfc.channels_out; ++ch) {
  3824. targ_bits[ch] += add_bits[ch];
  3825. extra_bits -= add_bits[ch];
  3826. }
  3827. for (bits = 0, ch = 0; ch < gfc.channels_out; ++ch) {
  3828. bits += targ_bits[ch];
  3829. }
  3830. if (bits > LameInternalFlags.MAX_BITS_PER_GRANULE) {
  3831. abort();//fix cc 精简
  3832. }
  3833. return max_bits;
  3834. }
  3835. //fix cc 精简
  3836. /**
  3837. * Robert Hegemann 2001-04-27:
  3838. * this adjusts the ATH, keeping the original noise floor
  3839. * affects the higher frequencies more than the lower ones
  3840. */
  3841. this.athAdjust = function (a, x, athFloor) {
  3842. /*
  3843. * work in progress
  3844. */
  3845. var o = 90.30873362;
  3846. var p = 94.82444863;
  3847. var u = Util.FAST_LOG10_X(x, 10.0);
  3848. var v = a * a;
  3849. var w = 0.0;
  3850. u -= athFloor;
  3851. /* undo scaling */
  3852. if (v > 1E-20)
  3853. w = 1. + Util.FAST_LOG10_X(v, 10.0 / o);
  3854. if (w < 0)
  3855. w = 0.;
  3856. u *= w;
  3857. u += athFloor + o - p;
  3858. /* redo scaling */
  3859. return Math.pow(10., 0.1 * u);
  3860. };
  3861. /**
  3862. * Calculate the allowed distortion for each scalefactor band, as determined
  3863. * by the psychoacoustic model. xmin(sb) = ratio(sb) * en(sb) / bw(sb)
  3864. *
  3865. * returns number of sfb's with energy > ATH
  3866. */
  3867. this.calc_xmin = function (gfp, ratio, cod_info, pxmin) {
  3868. var pxminPos = 0;
  3869. var gfc = gfp.internal_flags;
  3870. var gsfb, j = 0, ath_over = 0;
  3871. var ATH = gfc.ATH;
  3872. var xr = cod_info.xr;
  3873. var enable_athaa_fix = (gfp.VBR == VbrMode.vbr_mtrh) ? 1 : 0;
  3874. var masking_lower = gfc.masking_lower;
  3875. if (gfp.VBR == VbrMode.vbr_mtrh || gfp.VBR == VbrMode.vbr_mt) {
  3876. /* was already done in PSY-Model */
  3877. masking_lower = 1.0;
  3878. }
  3879. for (gsfb = 0; gsfb < cod_info.psy_lmax; gsfb++) {
  3880. var en0, xmin;
  3881. var rh1, rh2;
  3882. var width, l;
  3883. if (gfp.VBR == VbrMode.vbr_rh || gfp.VBR == VbrMode.vbr_mtrh)
  3884. xmin = athAdjust(ATH.adjust, ATH.l[gsfb], ATH.floor);
  3885. else
  3886. xmin = ATH.adjust * ATH.l[gsfb];
  3887. width = cod_info.width[gsfb];
  3888. rh1 = xmin / width;
  3889. rh2 = DBL_EPSILON;
  3890. l = width >> 1;
  3891. en0 = 0.0;
  3892. do {
  3893. var xa, xb;
  3894. xa = xr[j] * xr[j];
  3895. en0 += xa;
  3896. rh2 += (xa < rh1) ? xa : rh1;
  3897. j++;
  3898. xb = xr[j] * xr[j];
  3899. en0 += xb;
  3900. rh2 += (xb < rh1) ? xb : rh1;
  3901. j++;
  3902. } while (--l > 0);
  3903. if (en0 > xmin)
  3904. ath_over++;
  3905. if (gsfb == Encoder.SBPSY_l) {
  3906. abort();//fix cc 精简
  3907. }
  3908. if (enable_athaa_fix != 0) {
  3909. xmin = rh2;
  3910. }
  3911. if (!gfp.ATHonly) {
  3912. var e = ratio.en.l[gsfb];
  3913. if (e > 0.0) {
  3914. var x;
  3915. x = en0 * ratio.thm.l[gsfb] * masking_lower / e;
  3916. if (enable_athaa_fix != 0)
  3917. x *= gfc.nsPsy.longfact[gsfb];
  3918. if (xmin < x)
  3919. xmin = x;
  3920. }
  3921. }
  3922. if (enable_athaa_fix != 0)
  3923. pxmin[pxminPos++] = xmin;
  3924. else
  3925. pxmin[pxminPos++] = xmin * gfc.nsPsy.longfact[gsfb];
  3926. }
  3927. /* end of long block loop */
  3928. /* use this function to determine the highest non-zero coeff */
  3929. var max_nonzero = 575;
  3930. if (cod_info.block_type != Encoder.SHORT_TYPE) {
  3931. // NORM, START or STOP type, but not SHORT
  3932. var k = 576;
  3933. while (k-- != 0 && BitStream.EQ(xr[k], 0)) {
  3934. max_nonzero = k;
  3935. }
  3936. }
  3937. cod_info.max_nonzero_coeff = max_nonzero;
  3938. for (var sfb = cod_info.sfb_smin; gsfb < cod_info.psymax; sfb++, gsfb += 3) {
  3939. var width, b;
  3940. var tmpATH;
  3941. if (gfp.VBR == VbrMode.vbr_rh || gfp.VBR == VbrMode.vbr_mtrh)
  3942. tmpATH = athAdjust(ATH.adjust, ATH.s[sfb], ATH.floor);
  3943. else
  3944. tmpATH = ATH.adjust * ATH.s[sfb];
  3945. width = cod_info.width[gsfb];
  3946. for (b = 0; b < 3; b++) {
  3947. var en0 = 0.0, xmin;
  3948. var rh1, rh2;
  3949. var l = width >> 1;
  3950. rh1 = tmpATH / width;
  3951. rh2 = DBL_EPSILON;
  3952. do {
  3953. var xa, xb;
  3954. xa = xr[j] * xr[j];
  3955. en0 += xa;
  3956. rh2 += (xa < rh1) ? xa : rh1;
  3957. j++;
  3958. xb = xr[j] * xr[j];
  3959. en0 += xb;
  3960. rh2 += (xb < rh1) ? xb : rh1;
  3961. j++;
  3962. } while (--l > 0);
  3963. if (en0 > tmpATH)
  3964. ath_over++;
  3965. if (sfb == Encoder.SBPSY_s) {
  3966. abort();//fix cc 精简
  3967. }
  3968. if (enable_athaa_fix != 0)
  3969. xmin = rh2;
  3970. else
  3971. xmin = tmpATH;
  3972. if (!gfp.ATHonly && !gfp.ATHshort) {
  3973. var e = ratio.en.s[sfb][b];
  3974. if (e > 0.0) {
  3975. var x;
  3976. x = en0 * ratio.thm.s[sfb][b] * masking_lower / e;
  3977. if (enable_athaa_fix != 0)
  3978. x *= gfc.nsPsy.shortfact[sfb];
  3979. if (xmin < x)
  3980. xmin = x;
  3981. }
  3982. }
  3983. if (enable_athaa_fix != 0)
  3984. pxmin[pxminPos++] = xmin;
  3985. else
  3986. pxmin[pxminPos++] = xmin * gfc.nsPsy.shortfact[sfb];
  3987. }
  3988. /* b */
  3989. if (gfp.useTemporal) {
  3990. if (pxmin[pxminPos - 3] > pxmin[pxminPos - 3 + 1])
  3991. pxmin[pxminPos - 3 + 1] += (pxmin[pxminPos - 3] - pxmin[pxminPos - 3 + 1])
  3992. * gfc.decay;
  3993. if (pxmin[pxminPos - 3 + 1] > pxmin[pxminPos - 3 + 2])
  3994. pxmin[pxminPos - 3 + 2] += (pxmin[pxminPos - 3 + 1] - pxmin[pxminPos - 3 + 2])
  3995. * gfc.decay;
  3996. }
  3997. }
  3998. /* end of short block sfb loop */
  3999. return ath_over;
  4000. };
  4001. function StartLine(j) {
  4002. this.s = j;
  4003. }
  4004. this.calc_noise_core = function (cod_info, startline, l, step) {
  4005. var noise = 0;
  4006. var j = startline.s;
  4007. var ix = cod_info.l3_enc;
  4008. if (j > cod_info.count1) {
  4009. while ((l--) != 0) {
  4010. var temp;
  4011. temp = cod_info.xr[j];
  4012. j++;
  4013. noise += temp * temp;
  4014. temp = cod_info.xr[j];
  4015. j++;
  4016. noise += temp * temp;
  4017. }
  4018. } else if (j > cod_info.big_values) {
  4019. var ix01 = new_float(2);
  4020. ix01[0] = 0;
  4021. ix01[1] = step;
  4022. while ((l--) != 0) {
  4023. var temp;
  4024. temp = Math.abs(cod_info.xr[j]) - ix01[ix[j]];
  4025. j++;
  4026. noise += temp * temp;
  4027. temp = Math.abs(cod_info.xr[j]) - ix01[ix[j]];
  4028. j++;
  4029. noise += temp * temp;
  4030. }
  4031. } else {
  4032. while ((l--) != 0) {
  4033. var temp;
  4034. temp = Math.abs(cod_info.xr[j]) - pow43[ix[j]] * step;
  4035. j++;
  4036. noise += temp * temp;
  4037. temp = Math.abs(cod_info.xr[j]) - pow43[ix[j]] * step;
  4038. j++;
  4039. noise += temp * temp;
  4040. }
  4041. }
  4042. startline.s = j;
  4043. return noise;
  4044. }
  4045. /**
  4046. * <PRE>
  4047. * -oo dB => -1.00
  4048. * - 6 dB => -0.97
  4049. * - 3 dB => -0.80
  4050. * - 2 dB => -0.64
  4051. * - 1 dB => -0.38
  4052. * 0 dB => 0.00
  4053. * + 1 dB => +0.49
  4054. * + 2 dB => +1.06
  4055. * + 3 dB => +1.68
  4056. * + 6 dB => +3.69
  4057. * +10 dB => +6.45
  4058. * </PRE>
  4059. */
  4060. this.calc_noise = function (cod_info, l3_xmin, distort, res, prev_noise) {
  4061. var distortPos = 0;
  4062. var l3_xminPos = 0;
  4063. var sfb, l, over = 0;
  4064. var over_noise_db = 0;
  4065. /* 0 dB relative to masking */
  4066. var tot_noise_db = 0;
  4067. /* -200 dB relative to masking */
  4068. var max_noise = -20.0;
  4069. var j = 0;
  4070. var scalefac = cod_info.scalefac;
  4071. var scalefacPos = 0;
  4072. res.over_SSD = 0;
  4073. for (sfb = 0; sfb < cod_info.psymax; sfb++) {
  4074. var s = cod_info.global_gain
  4075. - (((scalefac[scalefacPos++]) + (cod_info.preflag != 0 ? pretab[sfb]
  4076. : 0)) << (cod_info.scalefac_scale + 1))
  4077. - cod_info.subblock_gain[cod_info.window[sfb]] * 8;
  4078. var noise = 0.0;
  4079. if (prev_noise != null && (prev_noise.step[sfb] == s)) {
  4080. /* use previously computed values */
  4081. noise = prev_noise.noise[sfb];
  4082. j += cod_info.width[sfb];
  4083. distort[distortPos++] = noise / l3_xmin[l3_xminPos++];
  4084. noise = prev_noise.noise_log[sfb];
  4085. } else {
  4086. var step = POW20(s);
  4087. l = cod_info.width[sfb] >> 1;
  4088. if ((j + cod_info.width[sfb]) > cod_info.max_nonzero_coeff) {
  4089. var usefullsize;
  4090. usefullsize = cod_info.max_nonzero_coeff - j + 1;
  4091. if (usefullsize > 0)
  4092. l = usefullsize >> 1;
  4093. else
  4094. l = 0;
  4095. }
  4096. var sl = new StartLine(j);
  4097. noise = this.calc_noise_core(cod_info, sl, l, step);
  4098. j = sl.s;
  4099. if (prev_noise != null) {
  4100. /* save noise values */
  4101. prev_noise.step[sfb] = s;
  4102. prev_noise.noise[sfb] = noise;
  4103. }
  4104. noise = distort[distortPos++] = noise / l3_xmin[l3_xminPos++];
  4105. /* multiplying here is adding in dB, but can overflow */
  4106. noise = Util.FAST_LOG10(Math.max(noise, 1E-20));
  4107. if (prev_noise != null) {
  4108. /* save noise values */
  4109. prev_noise.noise_log[sfb] = noise;
  4110. }
  4111. }
  4112. if (prev_noise != null) {
  4113. /* save noise values */
  4114. prev_noise.global_gain = cod_info.global_gain;
  4115. }
  4116. tot_noise_db += noise;
  4117. if (noise > 0.0) {
  4118. var tmp;
  4119. tmp = Math.max(0 | (noise * 10 + .5), 1);
  4120. res.over_SSD += tmp * tmp;
  4121. over++;
  4122. /* multiplying here is adding in dB -but can overflow */
  4123. /* over_noise *= noise; */
  4124. over_noise_db += noise;
  4125. }
  4126. max_noise = Math.max(max_noise, noise);
  4127. }
  4128. res.over_count = over;
  4129. res.tot_noise = tot_noise_db;
  4130. res.over_noise = over_noise_db;
  4131. res.max_noise = max_noise;
  4132. return over;
  4133. }
  4134. //fix cc 精简
  4135. }
  4136. function CalcNoiseData() {
  4137. this.global_gain = 0;
  4138. this.sfb_count1 = 0;
  4139. this.step = new_int(39);
  4140. this.noise = new_float(39);
  4141. this.noise_log = new_float(39);
  4142. }
  4143. //package mp3;
  4144. function GrInfo() {
  4145. //float xr[] = new float[576];
  4146. this.xr = new_float(576);
  4147. //int l3_enc[] = new int[576];
  4148. this.l3_enc = new_int(576);
  4149. //int scalefac[] = new int[L3Side.SFBMAX];
  4150. this.scalefac = new_int(L3Side.SFBMAX);
  4151. this.xrpow_max = 0.;
  4152. this.part2_3_length = 0;
  4153. this.big_values = 0;
  4154. this.count1 = 0;
  4155. this.global_gain = 0;
  4156. this.scalefac_compress = 0;
  4157. this.block_type = 0;
  4158. this.mixed_block_flag = 0;
  4159. this.table_select = new_int(3);
  4160. this.subblock_gain = new_int(3 + 1);
  4161. this.region0_count = 0;
  4162. this.region1_count = 0;
  4163. this.preflag = 0;
  4164. this.scalefac_scale = 0;
  4165. this.count1table_select = 0;
  4166. this.part2_length = 0;
  4167. this.sfb_lmax = 0;
  4168. this.sfb_smin = 0;
  4169. this.psy_lmax = 0;
  4170. this.sfbmax = 0;
  4171. this.psymax = 0;
  4172. this.sfbdivide = 0;
  4173. this.width = new_int(L3Side.SFBMAX);
  4174. this.window = new_int(L3Side.SFBMAX);
  4175. this.count1bits = 0;
  4176. /**
  4177. * added for LSF
  4178. */
  4179. this.sfb_partition_table = null;
  4180. this.slen = new_int(4);
  4181. this.max_nonzero_coeff = 0;
  4182. var self = this;
  4183. function clone_int(array) {
  4184. return new Int32Array(array);
  4185. }
  4186. function clone_float(array) {
  4187. return new Float32Array(array);
  4188. }
  4189. this.assign = function (other) {
  4190. self.xr = clone_float(other.xr); //.slice(0); //clone();
  4191. self.l3_enc = clone_int(other.l3_enc); //.slice(0); //clone();
  4192. self.scalefac = clone_int(other.scalefac);//.slice(0); //clone();
  4193. self.xrpow_max = other.xrpow_max;
  4194. self.part2_3_length = other.part2_3_length;
  4195. self.big_values = other.big_values;
  4196. self.count1 = other.count1;
  4197. self.global_gain = other.global_gain;
  4198. self.scalefac_compress = other.scalefac_compress;
  4199. self.block_type = other.block_type;
  4200. self.mixed_block_flag = other.mixed_block_flag;
  4201. self.table_select = clone_int(other.table_select);//.slice(0); //clone();
  4202. self.subblock_gain = clone_int(other.subblock_gain); //.slice(0); //.clone();
  4203. self.region0_count = other.region0_count;
  4204. self.region1_count = other.region1_count;
  4205. self.preflag = other.preflag;
  4206. self.scalefac_scale = other.scalefac_scale;
  4207. self.count1table_select = other.count1table_select;
  4208. self.part2_length = other.part2_length;
  4209. self.sfb_lmax = other.sfb_lmax;
  4210. self.sfb_smin = other.sfb_smin;
  4211. self.psy_lmax = other.psy_lmax;
  4212. self.sfbmax = other.sfbmax;
  4213. self.psymax = other.psymax;
  4214. self.sfbdivide = other.sfbdivide;
  4215. self.width = clone_int(other.width); //.slice(0); //.clone();
  4216. self.window = clone_int(other.window); //.slice(0); //.clone();
  4217. self.count1bits = other.count1bits;
  4218. self.sfb_partition_table = other.sfb_partition_table.slice(0); //.clone();
  4219. self.slen = clone_int(other.slen); //.slice(0); //.clone();
  4220. self.max_nonzero_coeff = other.max_nonzero_coeff;
  4221. }
  4222. }
  4223. var L3Side = {};
  4224. /**
  4225. * max scalefactor band, max(SBMAX_l, SBMAX_s*3, (SBMAX_s-3)*3+8)
  4226. */
  4227. L3Side.SFBMAX = (Encoder.SBMAX_s * 3);
  4228. /*
  4229. * MP3 quantization
  4230. *
  4231. * Copyright (c) 1999-2000 Mark Taylor
  4232. * Copyright (c) 1999-2003 Takehiro Tominaga
  4233. * Copyright (c) 2000-2007 Robert Hegemann
  4234. * Copyright (c) 2001-2005 Gabriel Bouvigne
  4235. *
  4236. * This library is free software; you can redistribute it and/or
  4237. * modify it under the terms of the GNU Lesser General Public
  4238. * License as published by the Free Software Foundation; either
  4239. * version 2 of the License, or (at your option) any later version.
  4240. *
  4241. * This library is distributed in the hope that it will be useful,
  4242. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4243. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  4244. * Library General Public License for more details.
  4245. *
  4246. * You should have received a copy of the GNU Lesser General Public
  4247. * License along with this library; if not, write to the
  4248. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  4249. * Boston, MA 02111-1307, USA.
  4250. */
  4251. /* $Id: Quantize.java,v 1.24 2011/05/24 20:48:06 kenchis Exp $ */
  4252. //package mp3;
  4253. //import java.util.Arrays;
  4254. function Quantize() {
  4255. var bs;
  4256. this.rv = null;
  4257. var rv;
  4258. this.qupvt = null;
  4259. var qupvt;
  4260. var vbr = new VBRQuantize();
  4261. var tk;
  4262. this.setModules = function (_bs, _rv, _qupvt, _tk) {
  4263. bs = _bs;
  4264. rv = _rv;
  4265. this.rv = _rv;
  4266. qupvt = _qupvt;
  4267. this.qupvt = _qupvt;
  4268. tk = _tk;
  4269. vbr.setModules(qupvt, tk);
  4270. }
  4271. //fix cc 精简
  4272. /**
  4273. * mt 6/99
  4274. *
  4275. * initializes cod_info, scalefac and xrpow
  4276. *
  4277. * returns 0 if all energies in xr are zero, else 1
  4278. */
  4279. function init_xrpow_core(cod_info, xrpow, upper, sum) {
  4280. sum = 0;
  4281. for (var i = 0; i <= upper; ++i) {
  4282. var tmp = Math.abs(cod_info.xr[i]);
  4283. sum += tmp;
  4284. xrpow[i] = Math.sqrt(tmp * Math.sqrt(tmp));
  4285. if (xrpow[i] > cod_info.xrpow_max)
  4286. cod_info.xrpow_max = xrpow[i];
  4287. }
  4288. return sum;
  4289. }
  4290. this.init_xrpow = function (gfc, cod_info, xrpow) {
  4291. var sum = 0;
  4292. var upper = 0 | cod_info.max_nonzero_coeff;
  4293. cod_info.xrpow_max = 0;
  4294. /*
  4295. * check if there is some energy we have to quantize and calculate xrpow
  4296. * matching our fresh scalefactors
  4297. */
  4298. Arrays.fill(xrpow, upper, 576, 0);
  4299. sum = init_xrpow_core(cod_info, xrpow, upper, sum);
  4300. /*
  4301. * return 1 if we have something to quantize, else 0
  4302. */
  4303. if (sum > 1E-20) {
  4304. var j = 0;
  4305. if ((gfc.substep_shaping & 2) != 0)
  4306. j = 1;
  4307. for (var i = 0; i < cod_info.psymax; i++)
  4308. gfc.pseudohalf[i] = j;
  4309. return true;
  4310. }
  4311. Arrays.fill(cod_info.l3_enc, 0, 576, 0);
  4312. return false;
  4313. }
  4314. /**
  4315. * Gabriel Bouvigne feb/apr 2003<BR>
  4316. * Analog silence detection in partitionned sfb21 or sfb12 for short blocks
  4317. *
  4318. * From top to bottom of sfb, changes to 0 coeffs which are below ath. It
  4319. * stops on the first coeff higher than ath.
  4320. */
  4321. function psfb21_analogsilence(gfc, cod_info) {
  4322. var ath = gfc.ATH;
  4323. var xr = cod_info.xr;
  4324. if (cod_info.block_type != Encoder.SHORT_TYPE) {
  4325. /* NORM, START or STOP type, but not SHORT blocks */
  4326. var stop = false;
  4327. for (var gsfb = Encoder.PSFB21 - 1; gsfb >= 0 && !stop; gsfb--) {
  4328. var start = gfc.scalefac_band.psfb21[gsfb];
  4329. var end = gfc.scalefac_band.psfb21[gsfb + 1];
  4330. var ath21 = qupvt.athAdjust(ath.adjust, ath.psfb21[gsfb],
  4331. ath.floor);
  4332. if (gfc.nsPsy.longfact[21] > 1e-12)
  4333. ath21 *= gfc.nsPsy.longfact[21];
  4334. for (var j = end - 1; j >= start; j--) {
  4335. if (Math.abs(xr[j]) < ath21)
  4336. xr[j] = 0;
  4337. else {
  4338. stop = true;
  4339. break;
  4340. }
  4341. }
  4342. }
  4343. } else {
  4344. /* note: short blocks coeffs are reordered */
  4345. for (var block = 0; block < 3; block++) {
  4346. var stop = false;
  4347. for (var gsfb = Encoder.PSFB12 - 1; gsfb >= 0 && !stop; gsfb--) {
  4348. var start = gfc.scalefac_band.s[12]
  4349. * 3
  4350. + (gfc.scalefac_band.s[13] - gfc.scalefac_band.s[12])
  4351. * block
  4352. + (gfc.scalefac_band.psfb12[gsfb] - gfc.scalefac_band.psfb12[0]);
  4353. var end = start
  4354. + (gfc.scalefac_band.psfb12[gsfb + 1] - gfc.scalefac_band.psfb12[gsfb]);
  4355. var ath12 = qupvt.athAdjust(ath.adjust, ath.psfb12[gsfb],
  4356. ath.floor);
  4357. if (gfc.nsPsy.shortfact[12] > 1e-12)
  4358. ath12 *= gfc.nsPsy.shortfact[12];
  4359. for (var j = end - 1; j >= start; j--) {
  4360. if (Math.abs(xr[j]) < ath12)
  4361. xr[j] = 0;
  4362. else {
  4363. stop = true;
  4364. break;
  4365. }
  4366. }
  4367. }
  4368. }
  4369. }
  4370. }
  4371. this.init_outer_loop = function (gfc, cod_info) {
  4372. /*
  4373. * initialize fresh cod_info
  4374. */
  4375. cod_info.part2_3_length = 0;
  4376. cod_info.big_values = 0;
  4377. cod_info.count1 = 0;
  4378. cod_info.global_gain = 210;
  4379. cod_info.scalefac_compress = 0;
  4380. /* mixed_block_flag, block_type was set in psymodel.c */
  4381. cod_info.table_select[0] = 0;
  4382. cod_info.table_select[1] = 0;
  4383. cod_info.table_select[2] = 0;
  4384. cod_info.subblock_gain[0] = 0;
  4385. cod_info.subblock_gain[1] = 0;
  4386. cod_info.subblock_gain[2] = 0;
  4387. cod_info.subblock_gain[3] = 0;
  4388. /* this one is always 0 */
  4389. cod_info.region0_count = 0;
  4390. cod_info.region1_count = 0;
  4391. cod_info.preflag = 0;
  4392. cod_info.scalefac_scale = 0;
  4393. cod_info.count1table_select = 0;
  4394. cod_info.part2_length = 0;
  4395. cod_info.sfb_lmax = Encoder.SBPSY_l;
  4396. cod_info.sfb_smin = Encoder.SBPSY_s;
  4397. cod_info.psy_lmax = gfc.sfb21_extra ? Encoder.SBMAX_l : Encoder.SBPSY_l;
  4398. cod_info.psymax = cod_info.psy_lmax;
  4399. cod_info.sfbmax = cod_info.sfb_lmax;
  4400. cod_info.sfbdivide = 11;
  4401. for (var sfb = 0; sfb < Encoder.SBMAX_l; sfb++) {
  4402. cod_info.width[sfb] = gfc.scalefac_band.l[sfb + 1]
  4403. - gfc.scalefac_band.l[sfb];
  4404. /* which is always 0. */
  4405. cod_info.window[sfb] = 3;
  4406. }
  4407. if (cod_info.block_type == Encoder.SHORT_TYPE) {
  4408. var ixwork = new_float(576);
  4409. cod_info.sfb_smin = 0;
  4410. cod_info.sfb_lmax = 0;
  4411. if (cod_info.mixed_block_flag != 0) {
  4412. abort();//fix cc 精简
  4413. }
  4414. cod_info.psymax = cod_info.sfb_lmax
  4415. + 3
  4416. * ((gfc.sfb21_extra ? Encoder.SBMAX_s : Encoder.SBPSY_s) - cod_info.sfb_smin);
  4417. cod_info.sfbmax = cod_info.sfb_lmax + 3
  4418. * (Encoder.SBPSY_s - cod_info.sfb_smin);
  4419. cod_info.sfbdivide = cod_info.sfbmax - 18;
  4420. cod_info.psy_lmax = cod_info.sfb_lmax;
  4421. /* re-order the short blocks, for more efficient encoding below */
  4422. /* By Takehiro TOMINAGA */
  4423. /*
  4424. * Within each scalefactor band, data is given for successive time
  4425. * windows, beginning with window 0 and ending with window 2. Within
  4426. * each window, the quantized values are then arranged in order of
  4427. * increasing frequency...
  4428. */
  4429. var ix = gfc.scalefac_band.l[cod_info.sfb_lmax];
  4430. System.arraycopy(cod_info.xr, 0, ixwork, 0, 576);
  4431. for (var sfb = cod_info.sfb_smin; sfb < Encoder.SBMAX_s; sfb++) {
  4432. var start = gfc.scalefac_band.s[sfb];
  4433. var end = gfc.scalefac_band.s[sfb + 1];
  4434. for (var window = 0; window < 3; window++) {
  4435. for (var l = start; l < end; l++) {
  4436. cod_info.xr[ix++] = ixwork[3 * l + window];
  4437. }
  4438. }
  4439. }
  4440. var j = cod_info.sfb_lmax;
  4441. for (var sfb = cod_info.sfb_smin; sfb < Encoder.SBMAX_s; sfb++) {
  4442. cod_info.width[j] = cod_info.width[j + 1] = cod_info.width[j + 2] = gfc.scalefac_band.s[sfb + 1]
  4443. - gfc.scalefac_band.s[sfb];
  4444. cod_info.window[j] = 0;
  4445. cod_info.window[j + 1] = 1;
  4446. cod_info.window[j + 2] = 2;
  4447. j += 3;
  4448. }
  4449. }
  4450. cod_info.count1bits = 0;
  4451. cod_info.sfb_partition_table = qupvt.nr_of_sfb_block[0][0];
  4452. cod_info.slen[0] = 0;
  4453. cod_info.slen[1] = 0;
  4454. cod_info.slen[2] = 0;
  4455. cod_info.slen[3] = 0;
  4456. cod_info.max_nonzero_coeff = 575;
  4457. /*
  4458. * fresh scalefactors are all zero
  4459. */
  4460. Arrays.fill(cod_info.scalefac, 0);
  4461. psfb21_analogsilence(gfc, cod_info);
  4462. };
  4463. function BinSearchDirection(ordinal) {
  4464. this.ordinal = ordinal;
  4465. }
  4466. BinSearchDirection.BINSEARCH_NONE = new BinSearchDirection(0);
  4467. BinSearchDirection.BINSEARCH_UP = new BinSearchDirection(1);
  4468. BinSearchDirection.BINSEARCH_DOWN = new BinSearchDirection(2);
  4469. /**
  4470. * author/date??
  4471. *
  4472. * binary step size search used by outer_loop to get a quantizer step size
  4473. * to start with
  4474. */
  4475. function bin_search_StepSize(gfc, cod_info, desired_rate, ch, xrpow) {
  4476. var nBits;
  4477. var CurrentStep = gfc.CurrentStep[ch];
  4478. var flagGoneOver = false;
  4479. var start = gfc.OldValue[ch];
  4480. var Direction = BinSearchDirection.BINSEARCH_NONE;
  4481. cod_info.global_gain = start;
  4482. desired_rate -= cod_info.part2_length;
  4483. for (; ;) {
  4484. var step;
  4485. nBits = tk.count_bits(gfc, xrpow, cod_info, null);
  4486. if (CurrentStep == 1 || nBits == desired_rate)
  4487. break;
  4488. /* nothing to adjust anymore */
  4489. if (nBits > desired_rate) {
  4490. /* increase Quantize_StepSize */
  4491. if (Direction == BinSearchDirection.BINSEARCH_DOWN)
  4492. flagGoneOver = true;
  4493. if (flagGoneOver)
  4494. CurrentStep /= 2;
  4495. Direction = BinSearchDirection.BINSEARCH_UP;
  4496. step = CurrentStep;
  4497. } else {
  4498. /* decrease Quantize_StepSize */
  4499. if (Direction == BinSearchDirection.BINSEARCH_UP)
  4500. flagGoneOver = true;
  4501. if (flagGoneOver)
  4502. CurrentStep /= 2;
  4503. Direction = BinSearchDirection.BINSEARCH_DOWN;
  4504. step = -CurrentStep;
  4505. }
  4506. cod_info.global_gain += step;
  4507. if (cod_info.global_gain < 0) {
  4508. abort();//fix cc 精简
  4509. }
  4510. if (cod_info.global_gain > 255) {
  4511. abort();//fix cc 精简
  4512. }
  4513. }
  4514. while (nBits > desired_rate && cod_info.global_gain < 255) {
  4515. cod_info.global_gain++;
  4516. nBits = tk.count_bits(gfc, xrpow, cod_info, null);
  4517. }
  4518. gfc.CurrentStep[ch] = (start - cod_info.global_gain >= 4) ? 4 : 2;
  4519. gfc.OldValue[ch] = cod_info.global_gain;
  4520. cod_info.part2_3_length = nBits;
  4521. return nBits;
  4522. }
  4523. //fix cc 精简
  4524. /**
  4525. * author/date??
  4526. *
  4527. * Function: Returns zero if there is a scalefac which has not been
  4528. * amplified. Otherwise it returns one.
  4529. */
  4530. function loop_break(cod_info) {
  4531. for (var sfb = 0; sfb < cod_info.sfbmax; sfb++)
  4532. if (cod_info.scalefac[sfb]
  4533. + cod_info.subblock_gain[cod_info.window[sfb]] == 0)
  4534. return false;
  4535. return true;
  4536. }
  4537. //fix cc 精简
  4538. function quant_compare(quant_comp, best, calc, gi, distort) {
  4539. /**
  4540. * noise is given in decibels (dB) relative to masking thesholds.<BR>
  4541. *
  4542. * over_noise: ??? (the previous comment is fully wrong)<BR>
  4543. * tot_noise: ??? (the previous comment is fully wrong)<BR>
  4544. * max_noise: max quantization noise
  4545. */
  4546. var better;
  4547. switch (quant_comp) {
  4548. default:
  4549. case 9:
  4550. {
  4551. if (best.over_count > 0) {
  4552. /* there are distorted sfb */
  4553. better = calc.over_SSD <= best.over_SSD;
  4554. if (calc.over_SSD == best.over_SSD)
  4555. better = calc.bits < best.bits;
  4556. } else {
  4557. /* no distorted sfb */
  4558. better = ((calc.max_noise < 0) && ((calc.max_noise * 10 + calc.bits) <= (best.max_noise * 10 + best.bits)));
  4559. }
  4560. break;
  4561. }
  4562. case 0:
  4563. better = calc.over_count < best.over_count
  4564. || (calc.over_count == best.over_count && calc.over_noise < best.over_noise)
  4565. || (calc.over_count == best.over_count
  4566. && BitStream.EQ(calc.over_noise, best.over_noise) && calc.tot_noise < best.tot_noise);
  4567. break;
  4568. case 8:
  4569. abort();//fix cc 精简
  4570. //$FALL-THROUGH$
  4571. case 1:
  4572. better = calc.max_noise < best.max_noise;
  4573. break;
  4574. case 2:
  4575. better = calc.tot_noise < best.tot_noise;
  4576. break;
  4577. case 3:
  4578. better = (calc.tot_noise < best.tot_noise)
  4579. && (calc.max_noise < best.max_noise);
  4580. break;
  4581. case 4:
  4582. better = (calc.max_noise <= 0.0 && best.max_noise > 0.2)
  4583. || (calc.max_noise <= 0.0 && best.max_noise < 0.0
  4584. && best.max_noise > calc.max_noise - 0.2 && calc.tot_noise < best.tot_noise)
  4585. || (calc.max_noise <= 0.0 && best.max_noise > 0.0
  4586. && best.max_noise > calc.max_noise - 0.2 && calc.tot_noise < best.tot_noise
  4587. + best.over_noise)
  4588. || (calc.max_noise > 0.0 && best.max_noise > -0.05
  4589. && best.max_noise > calc.max_noise - 0.1 && calc.tot_noise
  4590. + calc.over_noise < best.tot_noise
  4591. + best.over_noise)
  4592. || (calc.max_noise > 0.0 && best.max_noise > -0.1
  4593. && best.max_noise > calc.max_noise - 0.15 && calc.tot_noise
  4594. + calc.over_noise + calc.over_noise < best.tot_noise
  4595. + best.over_noise + best.over_noise);
  4596. break;
  4597. case 5:
  4598. better = calc.over_noise < best.over_noise
  4599. || (BitStream.EQ(calc.over_noise, best.over_noise) && calc.tot_noise < best.tot_noise);
  4600. break;
  4601. case 6:
  4602. better = calc.over_noise < best.over_noise
  4603. || (BitStream.EQ(calc.over_noise, best.over_noise) && (calc.max_noise < best.max_noise || (BitStream
  4604. .EQ(calc.max_noise, best.max_noise) && calc.tot_noise <= best.tot_noise)));
  4605. break;
  4606. case 7:
  4607. better = calc.over_count < best.over_count
  4608. || calc.over_noise < best.over_noise;
  4609. break;
  4610. }
  4611. if (best.over_count == 0) {
  4612. /*
  4613. * If no distorted bands, only use this quantization if it is
  4614. * better, and if it uses less bits. Unfortunately, part2_3_length
  4615. * is sometimes a poor estimator of the final size at low bitrates.
  4616. */
  4617. better = better && calc.bits < best.bits;
  4618. }
  4619. return better;
  4620. }
  4621. /**
  4622. * author/date??
  4623. *
  4624. * <PRE>
  4625. * Amplify the scalefactor bands that violate the masking threshold.
  4626. * See ISO 11172-3 Section C.1.5.4.3.5
  4627. *
  4628. * distort[] = noise/masking
  4629. * distort[] > 1 ==> noise is not masked
  4630. * distort[] < 1 ==> noise is masked
  4631. * max_dist = maximum value of distort[]
  4632. *
  4633. * Three algorithms:
  4634. * noise_shaping_amp
  4635. * 0 Amplify all bands with distort[]>1.
  4636. *
  4637. * 1 Amplify all bands with distort[] >= max_dist^(.5);
  4638. * ( 50% in the db scale)
  4639. *
  4640. * 2 Amplify first band with distort[] >= max_dist;
  4641. *
  4642. *
  4643. * For algorithms 0 and 1, if max_dist < 1, then amplify all bands
  4644. * with distort[] >= .95*max_dist. This is to make sure we always
  4645. * amplify at least one band.
  4646. * </PRE>
  4647. */
  4648. function amp_scalefac_bands(gfp, cod_info, distort, xrpow, bRefine) {
  4649. var gfc = gfp.internal_flags;
  4650. var ifqstep34;
  4651. if (cod_info.scalefac_scale == 0) {
  4652. ifqstep34 = 1.29683955465100964055;
  4653. /* 2**(.75*.5) */
  4654. } else {
  4655. ifqstep34 = 1.68179283050742922612;
  4656. /* 2**(.75*1) */
  4657. }
  4658. /* compute maximum value of distort[] */
  4659. var trigger = 0;
  4660. for (var sfb = 0; sfb < cod_info.sfbmax; sfb++) {
  4661. if (trigger < distort[sfb])
  4662. trigger = distort[sfb];
  4663. }
  4664. var noise_shaping_amp = gfc.noise_shaping_amp;
  4665. if (noise_shaping_amp == 3) {
  4666. abort();//fix cc 精简
  4667. }
  4668. switch (noise_shaping_amp) {
  4669. case 2:
  4670. /* amplify exactly 1 band */
  4671. break;
  4672. case 1:
  4673. /* amplify bands within 50% of max (on db scale) */
  4674. if (trigger > 1.0)
  4675. trigger = Math.pow(trigger, .5);
  4676. else
  4677. trigger *= .95;
  4678. break;
  4679. case 0:
  4680. default:
  4681. /* ISO algorithm. amplify all bands with distort>1 */
  4682. if (trigger > 1.0)
  4683. trigger = 1.0;
  4684. else
  4685. trigger *= .95;
  4686. break;
  4687. }
  4688. var j = 0;
  4689. for (var sfb = 0; sfb < cod_info.sfbmax; sfb++) {
  4690. var width = cod_info.width[sfb];
  4691. var l;
  4692. j += width;
  4693. if (distort[sfb] < trigger)
  4694. continue;
  4695. if ((gfc.substep_shaping & 2) != 0) {
  4696. abort();//fix cc 精简
  4697. }
  4698. cod_info.scalefac[sfb]++;
  4699. for (l = -width; l < 0; l++) {
  4700. xrpow[j + l] *= ifqstep34;
  4701. if (xrpow[j + l] > cod_info.xrpow_max)
  4702. cod_info.xrpow_max = xrpow[j + l];
  4703. }
  4704. if (gfc.noise_shaping_amp == 2)
  4705. return;
  4706. }
  4707. }
  4708. /**
  4709. * Takehiro Tominaga 2000-xx-xx
  4710. *
  4711. * turns on scalefac scale and adjusts scalefactors
  4712. */
  4713. function inc_scalefac_scale(cod_info, xrpow) {
  4714. var ifqstep34 = 1.29683955465100964055;
  4715. var j = 0;
  4716. for (var sfb = 0; sfb < cod_info.sfbmax; sfb++) {
  4717. var width = cod_info.width[sfb];
  4718. var s = cod_info.scalefac[sfb];
  4719. if (cod_info.preflag != 0)
  4720. s += qupvt.pretab[sfb];
  4721. j += width;
  4722. if ((s & 1) != 0) {
  4723. s++;
  4724. for (var l = -width; l < 0; l++) {
  4725. xrpow[j + l] *= ifqstep34;
  4726. if (xrpow[j + l] > cod_info.xrpow_max)
  4727. cod_info.xrpow_max = xrpow[j + l];
  4728. }
  4729. }
  4730. cod_info.scalefac[sfb] = s >> 1;
  4731. }
  4732. cod_info.preflag = 0;
  4733. cod_info.scalefac_scale = 1;
  4734. }
  4735. /**
  4736. * Takehiro Tominaga 2000-xx-xx
  4737. *
  4738. * increases the subblock gain and adjusts scalefactors
  4739. */
  4740. function inc_subblock_gain(gfc, cod_info, xrpow) {
  4741. var sfb;
  4742. var scalefac = cod_info.scalefac;
  4743. /* subbloc_gain can't do anything in the long block region */
  4744. for (sfb = 0; sfb < cod_info.sfb_lmax; sfb++) {
  4745. if (scalefac[sfb] >= 16)
  4746. return true;
  4747. }
  4748. for (var window = 0; window < 3; window++) {
  4749. var s1 = 0;
  4750. var s2 = 0;
  4751. for (sfb = cod_info.sfb_lmax + window; sfb < cod_info.sfbdivide; sfb += 3) {
  4752. if (s1 < scalefac[sfb])
  4753. s1 = scalefac[sfb];
  4754. }
  4755. for (; sfb < cod_info.sfbmax; sfb += 3) {
  4756. if (s2 < scalefac[sfb])
  4757. s2 = scalefac[sfb];
  4758. }
  4759. if (s1 < 16 && s2 < 8)
  4760. continue;
  4761. if (cod_info.subblock_gain[window] >= 7)
  4762. return true;
  4763. /*
  4764. * even though there is no scalefactor for sfb12 subblock gain
  4765. * affects upper frequencies too, that's why we have to go up to
  4766. * SBMAX_s
  4767. */
  4768. cod_info.subblock_gain[window]++;
  4769. var j = gfc.scalefac_band.l[cod_info.sfb_lmax];
  4770. for (sfb = cod_info.sfb_lmax + window; sfb < cod_info.sfbmax; sfb += 3) {
  4771. var amp;
  4772. var width = cod_info.width[sfb];
  4773. var s = scalefac[sfb];
  4774. s = s - (4 >> cod_info.scalefac_scale);
  4775. if (s >= 0) {
  4776. scalefac[sfb] = s;
  4777. j += width * 3;
  4778. continue;
  4779. }
  4780. scalefac[sfb] = 0;
  4781. {
  4782. var gain = 210 + (s << (cod_info.scalefac_scale + 1));
  4783. amp = qupvt.IPOW20(gain);
  4784. }
  4785. j += width * (window + 1);
  4786. for (var l = -width; l < 0; l++) {
  4787. xrpow[j + l] *= amp;
  4788. if (xrpow[j + l] > cod_info.xrpow_max)
  4789. cod_info.xrpow_max = xrpow[j + l];
  4790. }
  4791. j += width * (3 - window - 1);
  4792. }
  4793. {
  4794. var amp = qupvt.IPOW20(202);
  4795. j += cod_info.width[sfb] * (window + 1);
  4796. for (var l = -cod_info.width[sfb]; l < 0; l++) {
  4797. xrpow[j + l] *= amp;
  4798. if (xrpow[j + l] > cod_info.xrpow_max)
  4799. cod_info.xrpow_max = xrpow[j + l];
  4800. }
  4801. }
  4802. }
  4803. return false;
  4804. }
  4805. /**
  4806. * <PRE>
  4807. * Takehiro Tominaga /date??
  4808. * Robert Hegemann 2000-09-06: made a function of it
  4809. *
  4810. * amplifies scalefactor bands,
  4811. * - if all are already amplified returns 0
  4812. * - if some bands are amplified too much:
  4813. * * try to increase scalefac_scale
  4814. * * if already scalefac_scale was set
  4815. * try on short blocks to increase subblock gain
  4816. * </PRE>
  4817. */
  4818. function balance_noise(gfp, cod_info, distort, xrpow, bRefine) {
  4819. var gfc = gfp.internal_flags;
  4820. amp_scalefac_bands(gfp, cod_info, distort, xrpow, bRefine);
  4821. /*
  4822. * check to make sure we have not amplified too much loop_break returns
  4823. * 0 if there is an unamplified scalefac scale_bitcount returns 0 if no
  4824. * scalefactors are too large
  4825. */
  4826. var status = loop_break(cod_info);
  4827. if (status)
  4828. return false;
  4829. /* all bands amplified */
  4830. /*
  4831. * not all scalefactors have been amplified. so these scalefacs are
  4832. * possibly valid. encode them:
  4833. */
  4834. if (gfc.mode_gr == 2)
  4835. status = tk.scale_bitcount(cod_info);
  4836. else
  4837. status = tk.scale_bitcount_lsf(gfc, cod_info);
  4838. if (!status)
  4839. return true;
  4840. /* amplified some bands not exceeding limits */
  4841. /*
  4842. * some scalefactors are too large. lets try setting scalefac_scale=1
  4843. */
  4844. if (gfc.noise_shaping > 1) {
  4845. Arrays.fill(gfc.pseudohalf, 0);
  4846. if (0 == cod_info.scalefac_scale) {
  4847. inc_scalefac_scale(cod_info, xrpow);
  4848. status = false;
  4849. } else {
  4850. if (cod_info.block_type == Encoder.SHORT_TYPE
  4851. && gfc.subblock_gain > 0) {
  4852. status = (inc_subblock_gain(gfc, cod_info, xrpow) || loop_break(cod_info));
  4853. }
  4854. }
  4855. }
  4856. if (!status) {
  4857. if (gfc.mode_gr == 2)
  4858. status = tk.scale_bitcount(cod_info);
  4859. else
  4860. status = tk.scale_bitcount_lsf(gfc, cod_info);
  4861. }
  4862. return !status;
  4863. }
  4864. /**
  4865. * <PRE>
  4866. * Function: The outer iteration loop controls the masking conditions
  4867. * of all scalefactorbands. It computes the best scalefac and
  4868. * global gain. This module calls the inner iteration loop
  4869. *
  4870. * mt 5/99 completely rewritten to allow for bit reservoir control,
  4871. * mid/side channels with L/R or mid/side masking thresholds,
  4872. * and chooses best quantization instead of last quantization when
  4873. * no distortion free quantization can be found.
  4874. *
  4875. * added VBR support mt 5/99
  4876. *
  4877. * some code shuffle rh 9/00
  4878. * </PRE>
  4879. *
  4880. * @param l3_xmin
  4881. * allowed distortion
  4882. * @param xrpow
  4883. * coloured magnitudes of spectral
  4884. * @param targ_bits
  4885. * maximum allowed bits
  4886. */
  4887. this.outer_loop = function (gfp, cod_info, l3_xmin, xrpow, ch, targ_bits) {
  4888. var gfc = gfp.internal_flags;
  4889. var cod_info_w = new GrInfo();
  4890. var save_xrpow = new_float(576);
  4891. var distort = new_float(L3Side.SFBMAX);
  4892. var best_noise_info = new CalcNoiseResult();
  4893. var better;
  4894. var prev_noise = new CalcNoiseData();
  4895. var best_part2_3_length = 9999999;
  4896. var bEndOfSearch = false;
  4897. var bRefine = false;
  4898. var best_ggain_pass1 = 0;
  4899. bin_search_StepSize(gfc, cod_info, targ_bits, ch, xrpow);
  4900. if (0 == gfc.noise_shaping)
  4901. /* fast mode, no noise shaping, we are ready */
  4902. return 100;
  4903. /* default noise_info.over_count */
  4904. /* compute the distortion in this quantization */
  4905. /* coefficients and thresholds both l/r (or both mid/side) */
  4906. qupvt.calc_noise(cod_info, l3_xmin, distort, best_noise_info,
  4907. prev_noise);
  4908. best_noise_info.bits = cod_info.part2_3_length;
  4909. cod_info_w.assign(cod_info);
  4910. var age = 0;
  4911. System.arraycopy(xrpow, 0, save_xrpow, 0, 576);
  4912. while (!bEndOfSearch) {
  4913. /* BEGIN MAIN LOOP */
  4914. do {
  4915. var noise_info = new CalcNoiseResult();
  4916. var search_limit;
  4917. var maxggain = 255;
  4918. /*
  4919. * When quantization with no distorted bands is found, allow up
  4920. * to X new unsuccesful tries in serial. This gives us more
  4921. * possibilities for different quant_compare modes. Much more
  4922. * than 3 makes not a big difference, it is only slower.
  4923. */
  4924. if ((gfc.substep_shaping & 2) != 0) {
  4925. search_limit = 20;
  4926. } else {
  4927. search_limit = 3;
  4928. }
  4929. /*
  4930. * Check if the last scalefactor band is distorted. in VBR mode
  4931. * we can't get rid of the distortion, so quit now and VBR mode
  4932. * will try again with more bits. (makes a 10% speed increase,
  4933. * the files I tested were binary identical, 2000/05/20 Robert
  4934. * Hegemann) distort[] > 1 means noise > allowed noise
  4935. */
  4936. if (gfc.sfb21_extra) {
  4937. abort();//fix cc 精简
  4938. }
  4939. /* try a new scalefactor conbination on cod_info_w */
  4940. if (!balance_noise(gfp, cod_info_w, distort, xrpow, bRefine))
  4941. break;
  4942. if (cod_info_w.scalefac_scale != 0)
  4943. maxggain = 254;
  4944. /*
  4945. * inner_loop starts with the initial quantization step computed
  4946. * above and slowly increases until the bits < huff_bits. Thus
  4947. * it is important not to start with too large of an inital
  4948. * quantization step. Too small is ok, but inner_loop will take
  4949. * longer
  4950. */
  4951. var huff_bits = targ_bits - cod_info_w.part2_length;
  4952. if (huff_bits <= 0)
  4953. break;
  4954. /*
  4955. * increase quantizer stepsize until needed bits are below
  4956. * maximum
  4957. */
  4958. while ((cod_info_w.part2_3_length = tk.count_bits(gfc, xrpow,
  4959. cod_info_w, prev_noise)) > huff_bits
  4960. && cod_info_w.global_gain <= maxggain)
  4961. cod_info_w.global_gain++;
  4962. if (cod_info_w.global_gain > maxggain)
  4963. break;
  4964. if (best_noise_info.over_count == 0) {
  4965. while ((cod_info_w.part2_3_length = tk.count_bits(gfc,
  4966. xrpow, cod_info_w, prev_noise)) > best_part2_3_length
  4967. && cod_info_w.global_gain <= maxggain)
  4968. cod_info_w.global_gain++;
  4969. if (cod_info_w.global_gain > maxggain)
  4970. break;
  4971. }
  4972. /* compute the distortion in this quantization */
  4973. qupvt.calc_noise(cod_info_w, l3_xmin, distort, noise_info,
  4974. prev_noise);
  4975. noise_info.bits = cod_info_w.part2_3_length;
  4976. /*
  4977. * check if this quantization is better than our saved
  4978. * quantization
  4979. */
  4980. if (cod_info.block_type != Encoder.SHORT_TYPE) {
  4981. // NORM, START or STOP type
  4982. better = gfp.quant_comp;
  4983. } else
  4984. better = gfp.quant_comp_short;
  4985. better = quant_compare(better, best_noise_info, noise_info,
  4986. cod_info_w, distort) ? 1 : 0;
  4987. /* save data so we can restore this quantization later */
  4988. if (better != 0) {
  4989. best_part2_3_length = cod_info.part2_3_length;
  4990. best_noise_info = noise_info;
  4991. cod_info.assign(cod_info_w);
  4992. age = 0;
  4993. /* save data so we can restore this quantization later */
  4994. /* store for later reuse */
  4995. System.arraycopy(xrpow, 0, save_xrpow, 0, 576);
  4996. } else {
  4997. /* early stop? */
  4998. if (gfc.full_outer_loop == 0) {
  4999. if (++age > search_limit
  5000. && best_noise_info.over_count == 0)
  5001. break;
  5002. if ((gfc.noise_shaping_amp == 3) && bRefine && age > 30)
  5003. break;
  5004. if ((gfc.noise_shaping_amp == 3)
  5005. && bRefine
  5006. && (cod_info_w.global_gain - best_ggain_pass1) > 15)
  5007. break;
  5008. }
  5009. }
  5010. } while ((cod_info_w.global_gain + cod_info_w.scalefac_scale) < 255);
  5011. if (gfc.noise_shaping_amp == 3) {
  5012. abort();//fix cc 精简
  5013. } else {
  5014. bEndOfSearch = true;
  5015. }
  5016. }
  5017. /*
  5018. * finish up
  5019. */
  5020. if (gfp.VBR == VbrMode.vbr_rh || gfp.VBR == VbrMode.vbr_mtrh)
  5021. /* restore for reuse on next try */
  5022. System.arraycopy(save_xrpow, 0, xrpow, 0, 576);
  5023. /*
  5024. * do the 'substep shaping'
  5025. */
  5026. else if ((gfc.substep_shaping & 1) != 0)
  5027. abort();//fix cc 精简
  5028. return best_noise_info.over_count;
  5029. }
  5030. /**
  5031. * Robert Hegemann 2000-09-06
  5032. *
  5033. * update reservoir status after FINAL quantization/bitrate
  5034. */
  5035. this.iteration_finish_one = function (gfc, gr, ch) {
  5036. var l3_side = gfc.l3_side;
  5037. var cod_info = l3_side.tt[gr][ch];
  5038. /*
  5039. * try some better scalefac storage
  5040. */
  5041. tk.best_scalefac_store(gfc, gr, ch, l3_side);
  5042. /*
  5043. * best huffman_divide may save some bits too
  5044. */
  5045. if (gfc.use_best_huffman == 1)
  5046. tk.best_huffman_divide(gfc, cod_info);
  5047. /*
  5048. * update reservoir status after FINAL quantization/bitrate
  5049. */
  5050. rv.ResvAdjust(gfc, cod_info);
  5051. };
  5052. //fix cc 精简
  5053. }
  5054. /*
  5055. * MP3 window subband -> subband filtering -> mdct routine
  5056. *
  5057. * Copyright (c) 1999-2000 Takehiro Tominaga
  5058. *
  5059. *
  5060. * This library is free software; you can redistribute it and/or
  5061. * modify it under the terms of the GNU Lesser General Public
  5062. * License as published by the Free Software Foundation; either
  5063. * version 2 of the License, or (at your option) any later version.
  5064. *
  5065. * This library is distributed in the hope that it will be useful,
  5066. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5067. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  5068. * Library General Public License for more details.
  5069. *
  5070. * You should have received a copy of the GNU Library General Public
  5071. * License along with this library; if not, write to the
  5072. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  5073. * Boston, MA 02111-1307, USA.
  5074. */
  5075. /*
  5076. * Special Thanks to Patrick De Smet for your advices.
  5077. */
  5078. /* $Id: NewMDCT.java,v 1.11 2011/05/24 20:48:06 kenchis Exp $ */
  5079. //package mp3;
  5080. //import java.util.Arrays;
  5081. function NewMDCT() {
  5082. var enwindow = [
  5083. -4.77e-07 * 0.740951125354959 / 2.384e-06,
  5084. 1.03951e-04 * 0.740951125354959 / 2.384e-06,
  5085. 9.53674e-04 * 0.740951125354959 / 2.384e-06,
  5086. 2.841473e-03 * 0.740951125354959 / 2.384e-06,
  5087. 3.5758972e-02 * 0.740951125354959 / 2.384e-06,
  5088. 3.401756e-03 * 0.740951125354959 / 2.384e-06,
  5089. 9.83715e-04 * 0.740951125354959 / 2.384e-06,
  5090. 9.9182e-05 * 0.740951125354959 / 2.384e-06, /* 15 */
  5091. 1.2398e-05 * 0.740951125354959 / 2.384e-06,
  5092. 1.91212e-04 * 0.740951125354959 / 2.384e-06,
  5093. 2.283096e-03 * 0.740951125354959 / 2.384e-06,
  5094. 1.6994476e-02 * 0.740951125354959 / 2.384e-06,
  5095. -1.8756866e-02 * 0.740951125354959 / 2.384e-06,
  5096. -2.630711e-03 * 0.740951125354959 / 2.384e-06,
  5097. -2.47478e-04 * 0.740951125354959 / 2.384e-06,
  5098. -1.4782e-05 * 0.740951125354959 / 2.384e-06,
  5099. 9.063471690191471e-01, 1.960342806591213e-01,
  5100. -4.77e-07 * 0.773010453362737 / 2.384e-06,
  5101. 1.05858e-04 * 0.773010453362737 / 2.384e-06,
  5102. 9.30786e-04 * 0.773010453362737 / 2.384e-06,
  5103. 2.521515e-03 * 0.773010453362737 / 2.384e-06,
  5104. 3.5694122e-02 * 0.773010453362737 / 2.384e-06,
  5105. 3.643036e-03 * 0.773010453362737 / 2.384e-06,
  5106. 9.91821e-04 * 0.773010453362737 / 2.384e-06,
  5107. 9.6321e-05 * 0.773010453362737 / 2.384e-06, /* 14 */
  5108. 1.1444e-05 * 0.773010453362737 / 2.384e-06,
  5109. 1.65462e-04 * 0.773010453362737 / 2.384e-06,
  5110. 2.110004e-03 * 0.773010453362737 / 2.384e-06,
  5111. 1.6112804e-02 * 0.773010453362737 / 2.384e-06,
  5112. -1.9634247e-02 * 0.773010453362737 / 2.384e-06,
  5113. -2.803326e-03 * 0.773010453362737 / 2.384e-06,
  5114. -2.77042e-04 * 0.773010453362737 / 2.384e-06,
  5115. -1.6689e-05 * 0.773010453362737 / 2.384e-06,
  5116. 8.206787908286602e-01, 3.901806440322567e-01,
  5117. -4.77e-07 * 0.803207531480645 / 2.384e-06,
  5118. 1.07288e-04 * 0.803207531480645 / 2.384e-06,
  5119. 9.02653e-04 * 0.803207531480645 / 2.384e-06,
  5120. 2.174854e-03 * 0.803207531480645 / 2.384e-06,
  5121. 3.5586357e-02 * 0.803207531480645 / 2.384e-06,
  5122. 3.858566e-03 * 0.803207531480645 / 2.384e-06,
  5123. 9.95159e-04 * 0.803207531480645 / 2.384e-06,
  5124. 9.3460e-05 * 0.803207531480645 / 2.384e-06, /* 13 */
  5125. 1.0014e-05 * 0.803207531480645 / 2.384e-06,
  5126. 1.40190e-04 * 0.803207531480645 / 2.384e-06,
  5127. 1.937389e-03 * 0.803207531480645 / 2.384e-06,
  5128. 1.5233517e-02 * 0.803207531480645 / 2.384e-06,
  5129. -2.0506859e-02 * 0.803207531480645 / 2.384e-06,
  5130. -2.974033e-03 * 0.803207531480645 / 2.384e-06,
  5131. -3.07560e-04 * 0.803207531480645 / 2.384e-06,
  5132. -1.8120e-05 * 0.803207531480645 / 2.384e-06,
  5133. 7.416505462720353e-01, 5.805693545089249e-01,
  5134. -4.77e-07 * 0.831469612302545 / 2.384e-06,
  5135. 1.08242e-04 * 0.831469612302545 / 2.384e-06,
  5136. 8.68797e-04 * 0.831469612302545 / 2.384e-06,
  5137. 1.800537e-03 * 0.831469612302545 / 2.384e-06,
  5138. 3.5435200e-02 * 0.831469612302545 / 2.384e-06,
  5139. 4.049301e-03 * 0.831469612302545 / 2.384e-06,
  5140. 9.94205e-04 * 0.831469612302545 / 2.384e-06,
  5141. 9.0599e-05 * 0.831469612302545 / 2.384e-06, /* 12 */
  5142. 9.060e-06 * 0.831469612302545 / 2.384e-06,
  5143. 1.16348e-04 * 0.831469612302545 / 2.384e-06,
  5144. 1.766682e-03 * 0.831469612302545 / 2.384e-06,
  5145. 1.4358521e-02 * 0.831469612302545 / 2.384e-06,
  5146. -2.1372318e-02 * 0.831469612302545 / 2.384e-06,
  5147. -3.14188e-03 * 0.831469612302545 / 2.384e-06,
  5148. -3.39031e-04 * 0.831469612302545 / 2.384e-06,
  5149. -1.9550e-05 * 0.831469612302545 / 2.384e-06,
  5150. 6.681786379192989e-01, 7.653668647301797e-01,
  5151. -4.77e-07 * 0.857728610000272 / 2.384e-06,
  5152. 1.08719e-04 * 0.857728610000272 / 2.384e-06,
  5153. 8.29220e-04 * 0.857728610000272 / 2.384e-06,
  5154. 1.399517e-03 * 0.857728610000272 / 2.384e-06,
  5155. 3.5242081e-02 * 0.857728610000272 / 2.384e-06,
  5156. 4.215240e-03 * 0.857728610000272 / 2.384e-06,
  5157. 9.89437e-04 * 0.857728610000272 / 2.384e-06,
  5158. 8.7261e-05 * 0.857728610000272 / 2.384e-06, /* 11 */
  5159. 8.106e-06 * 0.857728610000272 / 2.384e-06,
  5160. 9.3937e-05 * 0.857728610000272 / 2.384e-06,
  5161. 1.597881e-03 * 0.857728610000272 / 2.384e-06,
  5162. 1.3489246e-02 * 0.857728610000272 / 2.384e-06,
  5163. -2.2228718e-02 * 0.857728610000272 / 2.384e-06,
  5164. -3.306866e-03 * 0.857728610000272 / 2.384e-06,
  5165. -3.71456e-04 * 0.857728610000272 / 2.384e-06,
  5166. -2.1458e-05 * 0.857728610000272 / 2.384e-06,
  5167. 5.993769336819237e-01, 9.427934736519954e-01,
  5168. -4.77e-07 * 0.881921264348355 / 2.384e-06,
  5169. 1.08719e-04 * 0.881921264348355 / 2.384e-06,
  5170. 7.8392e-04 * 0.881921264348355 / 2.384e-06,
  5171. 9.71317e-04 * 0.881921264348355 / 2.384e-06,
  5172. 3.5007000e-02 * 0.881921264348355 / 2.384e-06,
  5173. 4.357815e-03 * 0.881921264348355 / 2.384e-06,
  5174. 9.80854e-04 * 0.881921264348355 / 2.384e-06,
  5175. 8.3923e-05 * 0.881921264348355 / 2.384e-06, /* 10 */
  5176. 7.629e-06 * 0.881921264348355 / 2.384e-06,
  5177. 7.2956e-05 * 0.881921264348355 / 2.384e-06,
  5178. 1.432419e-03 * 0.881921264348355 / 2.384e-06,
  5179. 1.2627602e-02 * 0.881921264348355 / 2.384e-06,
  5180. -2.3074150e-02 * 0.881921264348355 / 2.384e-06,
  5181. -3.467083e-03 * 0.881921264348355 / 2.384e-06,
  5182. -4.04358e-04 * 0.881921264348355 / 2.384e-06,
  5183. -2.3365e-05 * 0.881921264348355 / 2.384e-06,
  5184. 5.345111359507916e-01, 1.111140466039205e+00,
  5185. -9.54e-07 * 0.903989293123443 / 2.384e-06,
  5186. 1.08242e-04 * 0.903989293123443 / 2.384e-06,
  5187. 7.31945e-04 * 0.903989293123443 / 2.384e-06,
  5188. 5.15938e-04 * 0.903989293123443 / 2.384e-06,
  5189. 3.4730434e-02 * 0.903989293123443 / 2.384e-06,
  5190. 4.477024e-03 * 0.903989293123443 / 2.384e-06,
  5191. 9.68933e-04 * 0.903989293123443 / 2.384e-06,
  5192. 8.0585e-05 * 0.903989293123443 / 2.384e-06, /* 9 */
  5193. 6.676e-06 * 0.903989293123443 / 2.384e-06,
  5194. 5.2929e-05 * 0.903989293123443 / 2.384e-06,
  5195. 1.269817e-03 * 0.903989293123443 / 2.384e-06,
  5196. 1.1775017e-02 * 0.903989293123443 / 2.384e-06,
  5197. -2.3907185e-02 * 0.903989293123443 / 2.384e-06,
  5198. -3.622532e-03 * 0.903989293123443 / 2.384e-06,
  5199. -4.38213e-04 * 0.903989293123443 / 2.384e-06,
  5200. -2.5272e-05 * 0.903989293123443 / 2.384e-06,
  5201. 4.729647758913199e-01, 1.268786568327291e+00,
  5202. -9.54e-07 * 0.92387953251128675613 / 2.384e-06,
  5203. 1.06812e-04 * 0.92387953251128675613 / 2.384e-06,
  5204. 6.74248e-04 * 0.92387953251128675613 / 2.384e-06,
  5205. 3.3379e-05 * 0.92387953251128675613 / 2.384e-06,
  5206. 3.4412861e-02 * 0.92387953251128675613 / 2.384e-06,
  5207. 4.573822e-03 * 0.92387953251128675613 / 2.384e-06,
  5208. 9.54151e-04 * 0.92387953251128675613 / 2.384e-06,
  5209. 7.6771e-05 * 0.92387953251128675613 / 2.384e-06,
  5210. 6.199e-06 * 0.92387953251128675613 / 2.384e-06,
  5211. 3.4332e-05 * 0.92387953251128675613 / 2.384e-06,
  5212. 1.111031e-03 * 0.92387953251128675613 / 2.384e-06,
  5213. 1.0933399e-02 * 0.92387953251128675613 / 2.384e-06,
  5214. -2.4725437e-02 * 0.92387953251128675613 / 2.384e-06,
  5215. -3.771782e-03 * 0.92387953251128675613 / 2.384e-06,
  5216. -4.72546e-04 * 0.92387953251128675613 / 2.384e-06,
  5217. -2.7657e-05 * 0.92387953251128675613 / 2.384e-06,
  5218. 4.1421356237309504879e-01, /* tan(PI/8) */
  5219. 1.414213562373095e+00,
  5220. -9.54e-07 * 0.941544065183021 / 2.384e-06,
  5221. 1.05381e-04 * 0.941544065183021 / 2.384e-06,
  5222. 6.10352e-04 * 0.941544065183021 / 2.384e-06,
  5223. -4.75883e-04 * 0.941544065183021 / 2.384e-06,
  5224. 3.4055710e-02 * 0.941544065183021 / 2.384e-06,
  5225. 4.649162e-03 * 0.941544065183021 / 2.384e-06,
  5226. 9.35555e-04 * 0.941544065183021 / 2.384e-06,
  5227. 7.3433e-05 * 0.941544065183021 / 2.384e-06, /* 7 */
  5228. 5.245e-06 * 0.941544065183021 / 2.384e-06,
  5229. 1.7166e-05 * 0.941544065183021 / 2.384e-06,
  5230. 9.56535e-04 * 0.941544065183021 / 2.384e-06,
  5231. 1.0103703e-02 * 0.941544065183021 / 2.384e-06,
  5232. -2.5527000e-02 * 0.941544065183021 / 2.384e-06,
  5233. -3.914356e-03 * 0.941544065183021 / 2.384e-06,
  5234. -5.07355e-04 * 0.941544065183021 / 2.384e-06,
  5235. -3.0041e-05 * 0.941544065183021 / 2.384e-06,
  5236. 3.578057213145241e-01, 1.546020906725474e+00,
  5237. -9.54e-07 * 0.956940335732209 / 2.384e-06,
  5238. 1.02520e-04 * 0.956940335732209 / 2.384e-06,
  5239. 5.39303e-04 * 0.956940335732209 / 2.384e-06,
  5240. -1.011848e-03 * 0.956940335732209 / 2.384e-06,
  5241. 3.3659935e-02 * 0.956940335732209 / 2.384e-06,
  5242. 4.703045e-03 * 0.956940335732209 / 2.384e-06,
  5243. 9.15051e-04 * 0.956940335732209 / 2.384e-06,
  5244. 7.0095e-05 * 0.956940335732209 / 2.384e-06, /* 6 */
  5245. 4.768e-06 * 0.956940335732209 / 2.384e-06,
  5246. 9.54e-07 * 0.956940335732209 / 2.384e-06,
  5247. 8.06808e-04 * 0.956940335732209 / 2.384e-06,
  5248. 9.287834e-03 * 0.956940335732209 / 2.384e-06,
  5249. -2.6310921e-02 * 0.956940335732209 / 2.384e-06,
  5250. -4.048824e-03 * 0.956940335732209 / 2.384e-06,
  5251. -5.42164e-04 * 0.956940335732209 / 2.384e-06,
  5252. -3.2425e-05 * 0.956940335732209 / 2.384e-06,
  5253. 3.033466836073424e-01, 1.662939224605090e+00,
  5254. -1.431e-06 * 0.970031253194544 / 2.384e-06,
  5255. 9.9182e-05 * 0.970031253194544 / 2.384e-06,
  5256. 4.62532e-04 * 0.970031253194544 / 2.384e-06,
  5257. -1.573563e-03 * 0.970031253194544 / 2.384e-06,
  5258. 3.3225536e-02 * 0.970031253194544 / 2.384e-06,
  5259. 4.737377e-03 * 0.970031253194544 / 2.384e-06,
  5260. 8.91685e-04 * 0.970031253194544 / 2.384e-06,
  5261. 6.6280e-05 * 0.970031253194544 / 2.384e-06, /* 5 */
  5262. 4.292e-06 * 0.970031253194544 / 2.384e-06,
  5263. -1.3828e-05 * 0.970031253194544 / 2.384e-06,
  5264. 6.61850e-04 * 0.970031253194544 / 2.384e-06,
  5265. 8.487225e-03 * 0.970031253194544 / 2.384e-06,
  5266. -2.7073860e-02 * 0.970031253194544 / 2.384e-06,
  5267. -4.174709e-03 * 0.970031253194544 / 2.384e-06,
  5268. -5.76973e-04 * 0.970031253194544 / 2.384e-06,
  5269. -3.4809e-05 * 0.970031253194544 / 2.384e-06,
  5270. 2.504869601913055e-01, 1.763842528696710e+00,
  5271. -1.431e-06 * 0.98078528040323 / 2.384e-06,
  5272. 9.5367e-05 * 0.98078528040323 / 2.384e-06,
  5273. 3.78609e-04 * 0.98078528040323 / 2.384e-06,
  5274. -2.161503e-03 * 0.98078528040323 / 2.384e-06,
  5275. 3.2754898e-02 * 0.98078528040323 / 2.384e-06,
  5276. 4.752159e-03 * 0.98078528040323 / 2.384e-06,
  5277. 8.66413e-04 * 0.98078528040323 / 2.384e-06,
  5278. 6.2943e-05 * 0.98078528040323 / 2.384e-06, /* 4 */
  5279. 3.815e-06 * 0.98078528040323 / 2.384e-06,
  5280. -2.718e-05 * 0.98078528040323 / 2.384e-06,
  5281. 5.22137e-04 * 0.98078528040323 / 2.384e-06,
  5282. 7.703304e-03 * 0.98078528040323 / 2.384e-06,
  5283. -2.7815342e-02 * 0.98078528040323 / 2.384e-06,
  5284. -4.290581e-03 * 0.98078528040323 / 2.384e-06,
  5285. -6.11782e-04 * 0.98078528040323 / 2.384e-06,
  5286. -3.7670e-05 * 0.98078528040323 / 2.384e-06,
  5287. 1.989123673796580e-01, 1.847759065022573e+00,
  5288. -1.907e-06 * 0.989176509964781 / 2.384e-06,
  5289. 9.0122e-05 * 0.989176509964781 / 2.384e-06,
  5290. 2.88486e-04 * 0.989176509964781 / 2.384e-06,
  5291. -2.774239e-03 * 0.989176509964781 / 2.384e-06,
  5292. 3.2248020e-02 * 0.989176509964781 / 2.384e-06,
  5293. 4.748821e-03 * 0.989176509964781 / 2.384e-06,
  5294. 8.38757e-04 * 0.989176509964781 / 2.384e-06,
  5295. 5.9605e-05 * 0.989176509964781 / 2.384e-06, /* 3 */
  5296. 3.338e-06 * 0.989176509964781 / 2.384e-06,
  5297. -3.9577e-05 * 0.989176509964781 / 2.384e-06,
  5298. 3.88145e-04 * 0.989176509964781 / 2.384e-06,
  5299. 6.937027e-03 * 0.989176509964781 / 2.384e-06,
  5300. -2.8532982e-02 * 0.989176509964781 / 2.384e-06,
  5301. -4.395962e-03 * 0.989176509964781 / 2.384e-06,
  5302. -6.46591e-04 * 0.989176509964781 / 2.384e-06,
  5303. -4.0531e-05 * 0.989176509964781 / 2.384e-06,
  5304. 1.483359875383474e-01, 1.913880671464418e+00,
  5305. -1.907e-06 * 0.995184726672197 / 2.384e-06,
  5306. 8.4400e-05 * 0.995184726672197 / 2.384e-06,
  5307. 1.91689e-04 * 0.995184726672197 / 2.384e-06,
  5308. -3.411293e-03 * 0.995184726672197 / 2.384e-06,
  5309. 3.1706810e-02 * 0.995184726672197 / 2.384e-06,
  5310. 4.728317e-03 * 0.995184726672197 / 2.384e-06,
  5311. 8.09669e-04 * 0.995184726672197 / 2.384e-06,
  5312. 5.579e-05 * 0.995184726672197 / 2.384e-06,
  5313. 3.338e-06 * 0.995184726672197 / 2.384e-06,
  5314. -5.0545e-05 * 0.995184726672197 / 2.384e-06,
  5315. 2.59876e-04 * 0.995184726672197 / 2.384e-06,
  5316. 6.189346e-03 * 0.995184726672197 / 2.384e-06,
  5317. -2.9224873e-02 * 0.995184726672197 / 2.384e-06,
  5318. -4.489899e-03 * 0.995184726672197 / 2.384e-06,
  5319. -6.80923e-04 * 0.995184726672197 / 2.384e-06,
  5320. -4.3392e-05 * 0.995184726672197 / 2.384e-06,
  5321. 9.849140335716425e-02, 1.961570560806461e+00,
  5322. -2.384e-06 * 0.998795456205172 / 2.384e-06,
  5323. 7.7724e-05 * 0.998795456205172 / 2.384e-06,
  5324. 8.8215e-05 * 0.998795456205172 / 2.384e-06,
  5325. -4.072189e-03 * 0.998795456205172 / 2.384e-06,
  5326. 3.1132698e-02 * 0.998795456205172 / 2.384e-06,
  5327. 4.691124e-03 * 0.998795456205172 / 2.384e-06,
  5328. 7.79152e-04 * 0.998795456205172 / 2.384e-06,
  5329. 5.2929e-05 * 0.998795456205172 / 2.384e-06,
  5330. 2.861e-06 * 0.998795456205172 / 2.384e-06,
  5331. -6.0558e-05 * 0.998795456205172 / 2.384e-06,
  5332. 1.37329e-04 * 0.998795456205172 / 2.384e-06,
  5333. 5.462170e-03 * 0.998795456205172 / 2.384e-06,
  5334. -2.9890060e-02 * 0.998795456205172 / 2.384e-06,
  5335. -4.570484e-03 * 0.998795456205172 / 2.384e-06,
  5336. -7.14302e-04 * 0.998795456205172 / 2.384e-06,
  5337. -4.6253e-05 * 0.998795456205172 / 2.384e-06,
  5338. 4.912684976946725e-02, 1.990369453344394e+00,
  5339. 3.5780907e-02 * Util.SQRT2 * 0.5 / 2.384e-06,
  5340. 1.7876148e-02 * Util.SQRT2 * 0.5 / 2.384e-06,
  5341. 3.134727e-03 * Util.SQRT2 * 0.5 / 2.384e-06,
  5342. 2.457142e-03 * Util.SQRT2 * 0.5 / 2.384e-06,
  5343. 9.71317e-04 * Util.SQRT2 * 0.5 / 2.384e-06,
  5344. 2.18868e-04 * Util.SQRT2 * 0.5 / 2.384e-06,
  5345. 1.01566e-04 * Util.SQRT2 * 0.5 / 2.384e-06,
  5346. 1.3828e-05 * Util.SQRT2 * 0.5 / 2.384e-06,
  5347. 3.0526638e-02 / 2.384e-06, 4.638195e-03 / 2.384e-06,
  5348. 7.47204e-04 / 2.384e-06, 4.9591e-05 / 2.384e-06,
  5349. 4.756451e-03 / 2.384e-06, 2.1458e-05 / 2.384e-06,
  5350. -6.9618e-05 / 2.384e-06, /* 2.384e-06/2.384e-06 */
  5351. ];
  5352. var NS = 12;
  5353. var NL = 36;
  5354. var win = [
  5355. [
  5356. 2.382191739347913e-13,
  5357. 6.423305872147834e-13,
  5358. 9.400849094049688e-13,
  5359. 1.122435026096556e-12,
  5360. 1.183840321267481e-12,
  5361. 1.122435026096556e-12,
  5362. 9.400849094049690e-13,
  5363. 6.423305872147839e-13,
  5364. 2.382191739347918e-13,
  5365. 5.456116108943412e-12,
  5366. 4.878985199565852e-12,
  5367. 4.240448995017367e-12,
  5368. 3.559909094758252e-12,
  5369. 2.858043359288075e-12,
  5370. 2.156177623817898e-12,
  5371. 1.475637723558783e-12,
  5372. 8.371015190102974e-13,
  5373. 2.599706096327376e-13,
  5374. -5.456116108943412e-12,
  5375. -4.878985199565852e-12,
  5376. -4.240448995017367e-12,
  5377. -3.559909094758252e-12,
  5378. -2.858043359288076e-12,
  5379. -2.156177623817898e-12,
  5380. -1.475637723558783e-12,
  5381. -8.371015190102975e-13,
  5382. -2.599706096327376e-13,
  5383. -2.382191739347923e-13,
  5384. -6.423305872147843e-13,
  5385. -9.400849094049696e-13,
  5386. -1.122435026096556e-12,
  5387. -1.183840321267481e-12,
  5388. -1.122435026096556e-12,
  5389. -9.400849094049694e-13,
  5390. -6.423305872147840e-13,
  5391. -2.382191739347918e-13,
  5392. ],
  5393. [
  5394. 2.382191739347913e-13,
  5395. 6.423305872147834e-13,
  5396. 9.400849094049688e-13,
  5397. 1.122435026096556e-12,
  5398. 1.183840321267481e-12,
  5399. 1.122435026096556e-12,
  5400. 9.400849094049688e-13,
  5401. 6.423305872147841e-13,
  5402. 2.382191739347918e-13,
  5403. 5.456116108943413e-12,
  5404. 4.878985199565852e-12,
  5405. 4.240448995017367e-12,
  5406. 3.559909094758253e-12,
  5407. 2.858043359288075e-12,
  5408. 2.156177623817898e-12,
  5409. 1.475637723558782e-12,
  5410. 8.371015190102975e-13,
  5411. 2.599706096327376e-13,
  5412. -5.461314069809755e-12,
  5413. -4.921085770524055e-12,
  5414. -4.343405037091838e-12,
  5415. -3.732668368707687e-12,
  5416. -3.093523840190885e-12,
  5417. -2.430835727329465e-12,
  5418. -1.734679010007751e-12,
  5419. -9.748253656609281e-13,
  5420. -2.797435120168326e-13,
  5421. 0.000000000000000e+00,
  5422. 0.000000000000000e+00,
  5423. 0.000000000000000e+00,
  5424. 0.000000000000000e+00,
  5425. 0.000000000000000e+00,
  5426. 0.000000000000000e+00,
  5427. -2.283748241799531e-13,
  5428. -4.037858874020686e-13,
  5429. -2.146547464825323e-13,
  5430. ],
  5431. [
  5432. 1.316524975873958e-01, /* win[SHORT_TYPE] */
  5433. 4.142135623730950e-01,
  5434. 7.673269879789602e-01,
  5435. 1.091308501069271e+00, /* tantab_l */
  5436. 1.303225372841206e+00,
  5437. 1.569685577117490e+00,
  5438. 1.920982126971166e+00,
  5439. 2.414213562373094e+00,
  5440. 3.171594802363212e+00,
  5441. 4.510708503662055e+00,
  5442. 7.595754112725146e+00,
  5443. 2.290376554843115e+01,
  5444. 0.98480775301220802032, /* cx */
  5445. 0.64278760968653936292,
  5446. 0.34202014332566882393,
  5447. 0.93969262078590842791,
  5448. -0.17364817766693030343,
  5449. -0.76604444311897790243,
  5450. 0.86602540378443870761,
  5451. 0.500000000000000e+00,
  5452. -5.144957554275265e-01, /* ca */
  5453. -4.717319685649723e-01,
  5454. -3.133774542039019e-01,
  5455. -1.819131996109812e-01,
  5456. -9.457419252642064e-02,
  5457. -4.096558288530405e-02,
  5458. -1.419856857247115e-02,
  5459. -3.699974673760037e-03,
  5460. 8.574929257125442e-01, /* cs */
  5461. 8.817419973177052e-01,
  5462. 9.496286491027329e-01,
  5463. 9.833145924917901e-01,
  5464. 9.955178160675857e-01,
  5465. 9.991605581781475e-01,
  5466. 9.998991952444470e-01,
  5467. 9.999931550702802e-01,
  5468. ],
  5469. [
  5470. 0.000000000000000e+00,
  5471. 0.000000000000000e+00,
  5472. 0.000000000000000e+00,
  5473. 0.000000000000000e+00,
  5474. 0.000000000000000e+00,
  5475. 0.000000000000000e+00,
  5476. 2.283748241799531e-13,
  5477. 4.037858874020686e-13,
  5478. 2.146547464825323e-13,
  5479. 5.461314069809755e-12,
  5480. 4.921085770524055e-12,
  5481. 4.343405037091838e-12,
  5482. 3.732668368707687e-12,
  5483. 3.093523840190885e-12,
  5484. 2.430835727329466e-12,
  5485. 1.734679010007751e-12,
  5486. 9.748253656609281e-13,
  5487. 2.797435120168326e-13,
  5488. -5.456116108943413e-12,
  5489. -4.878985199565852e-12,
  5490. -4.240448995017367e-12,
  5491. -3.559909094758253e-12,
  5492. -2.858043359288075e-12,
  5493. -2.156177623817898e-12,
  5494. -1.475637723558782e-12,
  5495. -8.371015190102975e-13,
  5496. -2.599706096327376e-13,
  5497. -2.382191739347913e-13,
  5498. -6.423305872147834e-13,
  5499. -9.400849094049688e-13,
  5500. -1.122435026096556e-12,
  5501. -1.183840321267481e-12,
  5502. -1.122435026096556e-12,
  5503. -9.400849094049688e-13,
  5504. -6.423305872147841e-13,
  5505. -2.382191739347918e-13,
  5506. ]
  5507. ];
  5508. var tantab_l = win[Encoder.SHORT_TYPE];
  5509. var cx = win[Encoder.SHORT_TYPE];
  5510. var ca = win[Encoder.SHORT_TYPE];
  5511. var cs = win[Encoder.SHORT_TYPE];
  5512. /**
  5513. * new IDCT routine written by Takehiro TOMINAGA
  5514. *
  5515. * PURPOSE: Overlapping window on PCM samples<BR>
  5516. *
  5517. * SEMANTICS:<BR>
  5518. * 32 16-bit pcm samples are scaled to fractional 2's complement and
  5519. * concatenated to the end of the window buffer #x#. The updated window
  5520. * buffer #x# is then windowed by the analysis window #c# to produce the
  5521. * windowed sample #z#
  5522. */
  5523. var order = [
  5524. 0, 1, 16, 17, 8, 9, 24, 25, 4, 5, 20, 21, 12, 13, 28, 29,
  5525. 2, 3, 18, 19, 10, 11, 26, 27, 6, 7, 22, 23, 14, 15, 30, 31
  5526. ];
  5527. /**
  5528. * returns sum_j=0^31 a[j]*cos(PI*j*(k+1/2)/32), 0<=k<32
  5529. */
  5530. function window_subband(x1, x1Pos, a) {
  5531. var wp = 10;
  5532. var x2 = x1Pos + 238 - 14 - 286;
  5533. for (var i = -15; i < 0; i++) {
  5534. var w, s, t;
  5535. w = enwindow[wp + -10];
  5536. s = x1[x2 + -224] * w;
  5537. t = x1[x1Pos + 224] * w;
  5538. w = enwindow[wp + -9];
  5539. s += x1[x2 + -160] * w;
  5540. t += x1[x1Pos + 160] * w;
  5541. w = enwindow[wp + -8];
  5542. s += x1[x2 + -96] * w;
  5543. t += x1[x1Pos + 96] * w;
  5544. w = enwindow[wp + -7];
  5545. s += x1[x2 + -32] * w;
  5546. t += x1[x1Pos + 32] * w;
  5547. w = enwindow[wp + -6];
  5548. s += x1[x2 + 32] * w;
  5549. t += x1[x1Pos + -32] * w;
  5550. w = enwindow[wp + -5];
  5551. s += x1[x2 + 96] * w;
  5552. t += x1[x1Pos + -96] * w;
  5553. w = enwindow[wp + -4];
  5554. s += x1[x2 + 160] * w;
  5555. t += x1[x1Pos + -160] * w;
  5556. w = enwindow[wp + -3];
  5557. s += x1[x2 + 224] * w;
  5558. t += x1[x1Pos + -224] * w;
  5559. w = enwindow[wp + -2];
  5560. s += x1[x1Pos + -256] * w;
  5561. t -= x1[x2 + 256] * w;
  5562. w = enwindow[wp + -1];
  5563. s += x1[x1Pos + -192] * w;
  5564. t -= x1[x2 + 192] * w;
  5565. w = enwindow[wp + 0];
  5566. s += x1[x1Pos + -128] * w;
  5567. t -= x1[x2 + 128] * w;
  5568. w = enwindow[wp + 1];
  5569. s += x1[x1Pos + -64] * w;
  5570. t -= x1[x2 + 64] * w;
  5571. w = enwindow[wp + 2];
  5572. s += x1[x1Pos + 0] * w;
  5573. t -= x1[x2 + 0] * w;
  5574. w = enwindow[wp + 3];
  5575. s += x1[x1Pos + 64] * w;
  5576. t -= x1[x2 + -64] * w;
  5577. w = enwindow[wp + 4];
  5578. s += x1[x1Pos + 128] * w;
  5579. t -= x1[x2 + -128] * w;
  5580. w = enwindow[wp + 5];
  5581. s += x1[x1Pos + 192] * w;
  5582. t -= x1[x2 + -192] * w;
  5583. /*
  5584. * this multiplyer could be removed, but it needs more 256 FLOAT
  5585. * data. thinking about the data cache performance, I think we
  5586. * should not use such a huge table. tt 2000/Oct/25
  5587. */
  5588. s *= enwindow[wp + 6];
  5589. w = t - s;
  5590. a[30 + i * 2] = t + s;
  5591. a[31 + i * 2] = enwindow[wp + 7] * w;
  5592. wp += 18;
  5593. x1Pos--;
  5594. x2++;
  5595. }
  5596. {
  5597. var s, t, u, v;
  5598. t = x1[x1Pos + -16] * enwindow[wp + -10];
  5599. s = x1[x1Pos + -32] * enwindow[wp + -2];
  5600. t += (x1[x1Pos + -48] - x1[x1Pos + 16]) * enwindow[wp + -9];
  5601. s += x1[x1Pos + -96] * enwindow[wp + -1];
  5602. t += (x1[x1Pos + -80] + x1[x1Pos + 48]) * enwindow[wp + -8];
  5603. s += x1[x1Pos + -160] * enwindow[wp + 0];
  5604. t += (x1[x1Pos + -112] - x1[x1Pos + 80]) * enwindow[wp + -7];
  5605. s += x1[x1Pos + -224] * enwindow[wp + 1];
  5606. t += (x1[x1Pos + -144] + x1[x1Pos + 112]) * enwindow[wp + -6];
  5607. s -= x1[x1Pos + 32] * enwindow[wp + 2];
  5608. t += (x1[x1Pos + -176] - x1[x1Pos + 144]) * enwindow[wp + -5];
  5609. s -= x1[x1Pos + 96] * enwindow[wp + 3];
  5610. t += (x1[x1Pos + -208] + x1[x1Pos + 176]) * enwindow[wp + -4];
  5611. s -= x1[x1Pos + 160] * enwindow[wp + 4];
  5612. t += (x1[x1Pos + -240] - x1[x1Pos + 208]) * enwindow[wp + -3];
  5613. s -= x1[x1Pos + 224];
  5614. u = s - t;
  5615. v = s + t;
  5616. t = a[14];
  5617. s = a[15] - t;
  5618. a[31] = v + t; /* A0 */
  5619. a[30] = u + s; /* A1 */
  5620. a[15] = u - s; /* A2 */
  5621. a[14] = v - t; /* A3 */
  5622. }
  5623. {
  5624. var xr;
  5625. xr = a[28] - a[0];
  5626. a[0] += a[28];
  5627. a[28] = xr * enwindow[wp + -2 * 18 + 7];
  5628. xr = a[29] - a[1];
  5629. a[1] += a[29];
  5630. a[29] = xr * enwindow[wp + -2 * 18 + 7];
  5631. xr = a[26] - a[2];
  5632. a[2] += a[26];
  5633. a[26] = xr * enwindow[wp + -4 * 18 + 7];
  5634. xr = a[27] - a[3];
  5635. a[3] += a[27];
  5636. a[27] = xr * enwindow[wp + -4 * 18 + 7];
  5637. xr = a[24] - a[4];
  5638. a[4] += a[24];
  5639. a[24] = xr * enwindow[wp + -6 * 18 + 7];
  5640. xr = a[25] - a[5];
  5641. a[5] += a[25];
  5642. a[25] = xr * enwindow[wp + -6 * 18 + 7];
  5643. xr = a[22] - a[6];
  5644. a[6] += a[22];
  5645. a[22] = xr * Util.SQRT2;
  5646. xr = a[23] - a[7];
  5647. a[7] += a[23];
  5648. a[23] = xr * Util.SQRT2 - a[7];
  5649. a[7] -= a[6];
  5650. a[22] -= a[7];
  5651. a[23] -= a[22];
  5652. xr = a[6];
  5653. a[6] = a[31] - xr;
  5654. a[31] = a[31] + xr;
  5655. xr = a[7];
  5656. a[7] = a[30] - xr;
  5657. a[30] = a[30] + xr;
  5658. xr = a[22];
  5659. a[22] = a[15] - xr;
  5660. a[15] = a[15] + xr;
  5661. xr = a[23];
  5662. a[23] = a[14] - xr;
  5663. a[14] = a[14] + xr;
  5664. xr = a[20] - a[8];
  5665. a[8] += a[20];
  5666. a[20] = xr * enwindow[wp + -10 * 18 + 7];
  5667. xr = a[21] - a[9];
  5668. a[9] += a[21];
  5669. a[21] = xr * enwindow[wp + -10 * 18 + 7];
  5670. xr = a[18] - a[10];
  5671. a[10] += a[18];
  5672. a[18] = xr * enwindow[wp + -12 * 18 + 7];
  5673. xr = a[19] - a[11];
  5674. a[11] += a[19];
  5675. a[19] = xr * enwindow[wp + -12 * 18 + 7];
  5676. xr = a[16] - a[12];
  5677. a[12] += a[16];
  5678. a[16] = xr * enwindow[wp + -14 * 18 + 7];
  5679. xr = a[17] - a[13];
  5680. a[13] += a[17];
  5681. a[17] = xr * enwindow[wp + -14 * 18 + 7];
  5682. xr = -a[20] + a[24];
  5683. a[20] += a[24];
  5684. a[24] = xr * enwindow[wp + -12 * 18 + 7];
  5685. xr = -a[21] + a[25];
  5686. a[21] += a[25];
  5687. a[25] = xr * enwindow[wp + -12 * 18 + 7];
  5688. xr = a[4] - a[8];
  5689. a[4] += a[8];
  5690. a[8] = xr * enwindow[wp + -12 * 18 + 7];
  5691. xr = a[5] - a[9];
  5692. a[5] += a[9];
  5693. a[9] = xr * enwindow[wp + -12 * 18 + 7];
  5694. xr = a[0] - a[12];
  5695. a[0] += a[12];
  5696. a[12] = xr * enwindow[wp + -4 * 18 + 7];
  5697. xr = a[1] - a[13];
  5698. a[1] += a[13];
  5699. a[13] = xr * enwindow[wp + -4 * 18 + 7];
  5700. xr = a[16] - a[28];
  5701. a[16] += a[28];
  5702. a[28] = xr * enwindow[wp + -4 * 18 + 7];
  5703. xr = -a[17] + a[29];
  5704. a[17] += a[29];
  5705. a[29] = xr * enwindow[wp + -4 * 18 + 7];
  5706. xr = Util.SQRT2 * (a[2] - a[10]);
  5707. a[2] += a[10];
  5708. a[10] = xr;
  5709. xr = Util.SQRT2 * (a[3] - a[11]);
  5710. a[3] += a[11];
  5711. a[11] = xr;
  5712. xr = Util.SQRT2 * (-a[18] + a[26]);
  5713. a[18] += a[26];
  5714. a[26] = xr - a[18];
  5715. xr = Util.SQRT2 * (-a[19] + a[27]);
  5716. a[19] += a[27];
  5717. a[27] = xr - a[19];
  5718. xr = a[2];
  5719. a[19] -= a[3];
  5720. a[3] -= xr;
  5721. a[2] = a[31] - xr;
  5722. a[31] += xr;
  5723. xr = a[3];
  5724. a[11] -= a[19];
  5725. a[18] -= xr;
  5726. a[3] = a[30] - xr;
  5727. a[30] += xr;
  5728. xr = a[18];
  5729. a[27] -= a[11];
  5730. a[19] -= xr;
  5731. a[18] = a[15] - xr;
  5732. a[15] += xr;
  5733. xr = a[19];
  5734. a[10] -= xr;
  5735. a[19] = a[14] - xr;
  5736. a[14] += xr;
  5737. xr = a[10];
  5738. a[11] -= xr;
  5739. a[10] = a[23] - xr;
  5740. a[23] += xr;
  5741. xr = a[11];
  5742. a[26] -= xr;
  5743. a[11] = a[22] - xr;
  5744. a[22] += xr;
  5745. xr = a[26];
  5746. a[27] -= xr;
  5747. a[26] = a[7] - xr;
  5748. a[7] += xr;
  5749. xr = a[27];
  5750. a[27] = a[6] - xr;
  5751. a[6] += xr;
  5752. xr = Util.SQRT2 * (a[0] - a[4]);
  5753. a[0] += a[4];
  5754. a[4] = xr;
  5755. xr = Util.SQRT2 * (a[1] - a[5]);
  5756. a[1] += a[5];
  5757. a[5] = xr;
  5758. xr = Util.SQRT2 * (a[16] - a[20]);
  5759. a[16] += a[20];
  5760. a[20] = xr;
  5761. xr = Util.SQRT2 * (a[17] - a[21]);
  5762. a[17] += a[21];
  5763. a[21] = xr;
  5764. xr = -Util.SQRT2 * (a[8] - a[12]);
  5765. a[8] += a[12];
  5766. a[12] = xr - a[8];
  5767. xr = -Util.SQRT2 * (a[9] - a[13]);
  5768. a[9] += a[13];
  5769. a[13] = xr - a[9];
  5770. xr = -Util.SQRT2 * (a[25] - a[29]);
  5771. a[25] += a[29];
  5772. a[29] = xr - a[25];
  5773. xr = -Util.SQRT2 * (a[24] + a[28]);
  5774. a[24] -= a[28];
  5775. a[28] = xr - a[24];
  5776. xr = a[24] - a[16];
  5777. a[24] = xr;
  5778. xr = a[20] - xr;
  5779. a[20] = xr;
  5780. xr = a[28] - xr;
  5781. a[28] = xr;
  5782. xr = a[25] - a[17];
  5783. a[25] = xr;
  5784. xr = a[21] - xr;
  5785. a[21] = xr;
  5786. xr = a[29] - xr;
  5787. a[29] = xr;
  5788. xr = a[17] - a[1];
  5789. a[17] = xr;
  5790. xr = a[9] - xr;
  5791. a[9] = xr;
  5792. xr = a[25] - xr;
  5793. a[25] = xr;
  5794. xr = a[5] - xr;
  5795. a[5] = xr;
  5796. xr = a[21] - xr;
  5797. a[21] = xr;
  5798. xr = a[13] - xr;
  5799. a[13] = xr;
  5800. xr = a[29] - xr;
  5801. a[29] = xr;
  5802. xr = a[1] - a[0];
  5803. a[1] = xr;
  5804. xr = a[16] - xr;
  5805. a[16] = xr;
  5806. xr = a[17] - xr;
  5807. a[17] = xr;
  5808. xr = a[8] - xr;
  5809. a[8] = xr;
  5810. xr = a[9] - xr;
  5811. a[9] = xr;
  5812. xr = a[24] - xr;
  5813. a[24] = xr;
  5814. xr = a[25] - xr;
  5815. a[25] = xr;
  5816. xr = a[4] - xr;
  5817. a[4] = xr;
  5818. xr = a[5] - xr;
  5819. a[5] = xr;
  5820. xr = a[20] - xr;
  5821. a[20] = xr;
  5822. xr = a[21] - xr;
  5823. a[21] = xr;
  5824. xr = a[12] - xr;
  5825. a[12] = xr;
  5826. xr = a[13] - xr;
  5827. a[13] = xr;
  5828. xr = a[28] - xr;
  5829. a[28] = xr;
  5830. xr = a[29] - xr;
  5831. a[29] = xr;
  5832. xr = a[0];
  5833. a[0] += a[31];
  5834. a[31] -= xr;
  5835. xr = a[1];
  5836. a[1] += a[30];
  5837. a[30] -= xr;
  5838. xr = a[16];
  5839. a[16] += a[15];
  5840. a[15] -= xr;
  5841. xr = a[17];
  5842. a[17] += a[14];
  5843. a[14] -= xr;
  5844. xr = a[8];
  5845. a[8] += a[23];
  5846. a[23] -= xr;
  5847. xr = a[9];
  5848. a[9] += a[22];
  5849. a[22] -= xr;
  5850. xr = a[24];
  5851. a[24] += a[7];
  5852. a[7] -= xr;
  5853. xr = a[25];
  5854. a[25] += a[6];
  5855. a[6] -= xr;
  5856. xr = a[4];
  5857. a[4] += a[27];
  5858. a[27] -= xr;
  5859. xr = a[5];
  5860. a[5] += a[26];
  5861. a[26] -= xr;
  5862. xr = a[20];
  5863. a[20] += a[11];
  5864. a[11] -= xr;
  5865. xr = a[21];
  5866. a[21] += a[10];
  5867. a[10] -= xr;
  5868. xr = a[12];
  5869. a[12] += a[19];
  5870. a[19] -= xr;
  5871. xr = a[13];
  5872. a[13] += a[18];
  5873. a[18] -= xr;
  5874. xr = a[28];
  5875. a[28] += a[3];
  5876. a[3] -= xr;
  5877. xr = a[29];
  5878. a[29] += a[2];
  5879. a[2] -= xr;
  5880. }
  5881. }
  5882. /**
  5883. * Function: Calculation of the MDCT In the case of long blocks (type 0,1,3)
  5884. * there are 36 coefficents in the time domain and 18 in the frequency
  5885. * domain.<BR>
  5886. * In the case of short blocks (type 2) there are 3 transformations with
  5887. * short length. This leads to 12 coefficents in the time and 6 in the
  5888. * frequency domain. In this case the results are stored side by side in the
  5889. * vector out[].
  5890. *
  5891. * New layer3
  5892. */
  5893. function mdct_short(inout, inoutPos) {
  5894. for (var l = 0; l < 3; l++) {
  5895. var tc0, tc1, tc2, ts0, ts1, ts2;
  5896. ts0 = inout[inoutPos + 2 * 3] * win[Encoder.SHORT_TYPE][0]
  5897. - inout[inoutPos + 5 * 3];
  5898. tc0 = inout[inoutPos + 0 * 3] * win[Encoder.SHORT_TYPE][2]
  5899. - inout[inoutPos + 3 * 3];
  5900. tc1 = ts0 + tc0;
  5901. tc2 = ts0 - tc0;
  5902. ts0 = inout[inoutPos + 5 * 3] * win[Encoder.SHORT_TYPE][0]
  5903. + inout[inoutPos + 2 * 3];
  5904. tc0 = inout[inoutPos + 3 * 3] * win[Encoder.SHORT_TYPE][2]
  5905. + inout[inoutPos + 0 * 3];
  5906. ts1 = ts0 + tc0;
  5907. ts2 = -ts0 + tc0;
  5908. tc0 = (inout[inoutPos + 1 * 3] * win[Encoder.SHORT_TYPE][1] - inout[inoutPos + 4 * 3]) * 2.069978111953089e-11;
  5909. /*
  5910. * tritab_s [ 1 ]
  5911. */
  5912. ts0 = (inout[inoutPos + 4 * 3] * win[Encoder.SHORT_TYPE][1] + inout[inoutPos + 1 * 3]) * 2.069978111953089e-11;
  5913. /*
  5914. * tritab_s [ 1 ]
  5915. */
  5916. inout[inoutPos + 3 * 0] = tc1 * 1.907525191737280e-11 + tc0;
  5917. /*
  5918. * tritab_s[ 2 ]
  5919. */
  5920. inout[inoutPos + 3 * 5] = -ts1 * 1.907525191737280e-11 + ts0;
  5921. /*
  5922. * tritab_s[0 ]
  5923. */
  5924. tc2 = tc2 * 0.86602540378443870761 * 1.907525191737281e-11;
  5925. /*
  5926. * tritab_s[ 2]
  5927. */
  5928. ts1 = ts1 * 0.5 * 1.907525191737281e-11 + ts0;
  5929. inout[inoutPos + 3 * 1] = tc2 - ts1;
  5930. inout[inoutPos + 3 * 2] = tc2 + ts1;
  5931. tc1 = tc1 * 0.5 * 1.907525191737281e-11 - tc0;
  5932. ts2 = ts2 * 0.86602540378443870761 * 1.907525191737281e-11;
  5933. /*
  5934. * tritab_s[ 0]
  5935. */
  5936. inout[inoutPos + 3 * 3] = tc1 + ts2;
  5937. inout[inoutPos + 3 * 4] = tc1 - ts2;
  5938. inoutPos++;
  5939. }
  5940. }
  5941. function mdct_long(out, outPos, _in) {
  5942. var ct, st;
  5943. {
  5944. var tc1, tc2, tc3, tc4, ts5, ts6, ts7, ts8;
  5945. /* 1,2, 5,6, 9,10, 13,14, 17 */
  5946. tc1 = _in[17] - _in[9];
  5947. tc3 = _in[15] - _in[11];
  5948. tc4 = _in[14] - _in[12];
  5949. ts5 = _in[0] + _in[8];
  5950. ts6 = _in[1] + _in[7];
  5951. ts7 = _in[2] + _in[6];
  5952. ts8 = _in[3] + _in[5];
  5953. out[outPos + 17] = (ts5 + ts7 - ts8) - (ts6 - _in[4]);
  5954. st = (ts5 + ts7 - ts8) * cx[12 + 7] + (ts6 - _in[4]);
  5955. ct = (tc1 - tc3 - tc4) * cx[12 + 6];
  5956. out[outPos + 5] = ct + st;
  5957. out[outPos + 6] = ct - st;
  5958. tc2 = (_in[16] - _in[10]) * cx[12 + 6];
  5959. ts6 = ts6 * cx[12 + 7] + _in[4];
  5960. ct = tc1 * cx[12 + 0] + tc2 + tc3 * cx[12 + 1] + tc4 * cx[12 + 2];
  5961. st = -ts5 * cx[12 + 4] + ts6 - ts7 * cx[12 + 5] + ts8 * cx[12 + 3];
  5962. out[outPos + 1] = ct + st;
  5963. out[outPos + 2] = ct - st;
  5964. ct = tc1 * cx[12 + 1] - tc2 - tc3 * cx[12 + 2] + tc4 * cx[12 + 0];
  5965. st = -ts5 * cx[12 + 5] + ts6 - ts7 * cx[12 + 3] + ts8 * cx[12 + 4];
  5966. out[outPos + 9] = ct + st;
  5967. out[outPos + 10] = ct - st;
  5968. ct = tc1 * cx[12 + 2] - tc2 + tc3 * cx[12 + 0] - tc4 * cx[12 + 1];
  5969. st = ts5 * cx[12 + 3] - ts6 + ts7 * cx[12 + 4] - ts8 * cx[12 + 5];
  5970. out[outPos + 13] = ct + st;
  5971. out[outPos + 14] = ct - st;
  5972. }
  5973. {
  5974. var ts1, ts2, ts3, ts4, tc5, tc6, tc7, tc8;
  5975. ts1 = _in[8] - _in[0];
  5976. ts3 = _in[6] - _in[2];
  5977. ts4 = _in[5] - _in[3];
  5978. tc5 = _in[17] + _in[9];
  5979. tc6 = _in[16] + _in[10];
  5980. tc7 = _in[15] + _in[11];
  5981. tc8 = _in[14] + _in[12];
  5982. out[outPos + 0] = (tc5 + tc7 + tc8) + (tc6 + _in[13]);
  5983. ct = (tc5 + tc7 + tc8) * cx[12 + 7] - (tc6 + _in[13]);
  5984. st = (ts1 - ts3 + ts4) * cx[12 + 6];
  5985. out[outPos + 11] = ct + st;
  5986. out[outPos + 12] = ct - st;
  5987. ts2 = (_in[7] - _in[1]) * cx[12 + 6];
  5988. tc6 = _in[13] - tc6 * cx[12 + 7];
  5989. ct = tc5 * cx[12 + 3] - tc6 + tc7 * cx[12 + 4] + tc8 * cx[12 + 5];
  5990. st = ts1 * cx[12 + 2] + ts2 + ts3 * cx[12 + 0] + ts4 * cx[12 + 1];
  5991. out[outPos + 3] = ct + st;
  5992. out[outPos + 4] = ct - st;
  5993. ct = -tc5 * cx[12 + 5] + tc6 - tc7 * cx[12 + 3] - tc8 * cx[12 + 4];
  5994. st = ts1 * cx[12 + 1] + ts2 - ts3 * cx[12 + 2] - ts4 * cx[12 + 0];
  5995. out[outPos + 7] = ct + st;
  5996. out[outPos + 8] = ct - st;
  5997. ct = -tc5 * cx[12 + 4] + tc6 - tc7 * cx[12 + 5] - tc8 * cx[12 + 3];
  5998. st = ts1 * cx[12 + 0] - ts2 + ts3 * cx[12 + 1] - ts4 * cx[12 + 2];
  5999. out[outPos + 15] = ct + st;
  6000. out[outPos + 16] = ct - st;
  6001. }
  6002. }
  6003. this.mdct_sub48 = function(gfc, w0, w1) {
  6004. var wk = w0;
  6005. var wkPos = 286;
  6006. /* thinking cache performance, ch->gr loop is better than gr->ch loop */
  6007. for (var ch = 0; ch < gfc.channels_out; ch++) {
  6008. for (var gr = 0; gr < gfc.mode_gr; gr++) {
  6009. var band;
  6010. var gi = (gfc.l3_side.tt[gr][ch]);
  6011. var mdct_enc = gi.xr;
  6012. var mdct_encPos = 0;
  6013. var samp = gfc.sb_sample[ch][1 - gr];
  6014. var sampPos = 0;
  6015. for (var k = 0; k < 18 / 2; k++) {
  6016. window_subband(wk, wkPos, samp[sampPos]);
  6017. window_subband(wk, wkPos + 32, samp[sampPos + 1]);
  6018. sampPos += 2;
  6019. wkPos += 64;
  6020. /*
  6021. * Compensate for inversion in the analysis filter
  6022. */
  6023. for (band = 1; band < 32; band += 2) {
  6024. samp[sampPos - 1][band] *= -1;
  6025. }
  6026. }
  6027. /*
  6028. * Perform imdct of 18 previous subband samples + 18 current
  6029. * subband samples
  6030. */
  6031. for (band = 0; band < 32; band++, mdct_encPos += 18) {
  6032. var type = gi.block_type;
  6033. var band0 = gfc.sb_sample[ch][gr];
  6034. var band1 = gfc.sb_sample[ch][1 - gr];
  6035. if (gi.mixed_block_flag != 0 && band < 2)
  6036. type = 0;
  6037. if (gfc.amp_filter[band] < 1e-12) {
  6038. Arrays.fill(mdct_enc, mdct_encPos + 0,
  6039. mdct_encPos + 18, 0);
  6040. } else {
  6041. if (gfc.amp_filter[band] < 1.0) {
  6042. abort();//fix cc 精简
  6043. }
  6044. if (type == Encoder.SHORT_TYPE) {
  6045. for (var k = -NS / 4; k < 0; k++) {
  6046. var w = win[Encoder.SHORT_TYPE][k + 3];
  6047. mdct_enc[mdct_encPos + k * 3 + 9] = band0[9 + k][order[band]]
  6048. * w - band0[8 - k][order[band]];
  6049. mdct_enc[mdct_encPos + k * 3 + 18] = band0[14 - k][order[band]]
  6050. * w + band0[15 + k][order[band]];
  6051. mdct_enc[mdct_encPos + k * 3 + 10] = band0[15 + k][order[band]]
  6052. * w - band0[14 - k][order[band]];
  6053. mdct_enc[mdct_encPos + k * 3 + 19] = band1[2 - k][order[band]]
  6054. * w + band1[3 + k][order[band]];
  6055. mdct_enc[mdct_encPos + k * 3 + 11] = band1[3 + k][order[band]]
  6056. * w - band1[2 - k][order[band]];
  6057. mdct_enc[mdct_encPos + k * 3 + 20] = band1[8 - k][order[band]]
  6058. * w + band1[9 + k][order[band]];
  6059. }
  6060. mdct_short(mdct_enc, mdct_encPos);
  6061. } else {
  6062. var work = new_float(18);
  6063. for (var k = -NL / 4; k < 0; k++) {
  6064. var a, b;
  6065. a = win[type][k + 27]
  6066. * band1[k + 9][order[band]]
  6067. + win[type][k + 36]
  6068. * band1[8 - k][order[band]];
  6069. b = win[type][k + 9]
  6070. * band0[k + 9][order[band]]
  6071. - win[type][k + 18]
  6072. * band0[8 - k][order[band]];
  6073. work[k + 9] = a - b * tantab_l[3 + k + 9];
  6074. work[k + 18] = a * tantab_l[3 + k + 9] + b;
  6075. }
  6076. mdct_long(mdct_enc, mdct_encPos, work);
  6077. }
  6078. }
  6079. /*
  6080. * Perform aliasing reduction butterfly
  6081. */
  6082. if (type != Encoder.SHORT_TYPE && band != 0) {
  6083. for (var k = 7; k >= 0; --k) {
  6084. var bu, bd;
  6085. bu = mdct_enc[mdct_encPos + k] * ca[20 + k]
  6086. + mdct_enc[mdct_encPos + -1 - k]
  6087. * cs[28 + k];
  6088. bd = mdct_enc[mdct_encPos + k] * cs[28 + k]
  6089. - mdct_enc[mdct_encPos + -1 - k]
  6090. * ca[20 + k];
  6091. mdct_enc[mdct_encPos + -1 - k] = bu;
  6092. mdct_enc[mdct_encPos + k] = bd;
  6093. }
  6094. }
  6095. }
  6096. }
  6097. wk = w1;
  6098. wkPos = 286;
  6099. if (gfc.mode_gr == 1) {
  6100. for (var i = 0; i < 18; i++) {
  6101. System.arraycopy(gfc.sb_sample[ch][1][i], 0,
  6102. gfc.sb_sample[ch][0][i], 0, 32);
  6103. }
  6104. }
  6105. }
  6106. }
  6107. }
  6108. //package mp3;
  6109. function III_psy_ratio() {
  6110. this.thm = new III_psy_xmin();
  6111. this.en = new III_psy_xmin();
  6112. }
  6113. /**
  6114. * ENCDELAY The encoder delay.
  6115. *
  6116. * Minimum allowed is MDCTDELAY (see below)
  6117. *
  6118. * The first 96 samples will be attenuated, so using a value less than 96
  6119. * will result in corrupt data for the first 96-ENCDELAY samples.
  6120. *
  6121. * suggested: 576 set to 1160 to sync with FhG.
  6122. */
  6123. Encoder.ENCDELAY = 576;
  6124. /**
  6125. * make sure there is at least one complete frame after the last frame
  6126. * containing real data
  6127. *
  6128. * Using a value of 288 would be sufficient for a a very sophisticated
  6129. * decoder that can decode granule-by-granule instead of frame by frame. But
  6130. * lets not assume this, and assume the decoder will not decode frame N
  6131. * unless it also has data for frame N+1
  6132. */
  6133. Encoder.POSTDELAY = 1152;
  6134. /**
  6135. * delay of the MDCT used in mdct.c original ISO routines had a delay of
  6136. * 528! Takehiro's routines:
  6137. */
  6138. Encoder.MDCTDELAY = 48;
  6139. Encoder.FFTOFFSET = (224 + Encoder.MDCTDELAY);
  6140. /**
  6141. * Most decoders, including the one we use, have a delay of 528 samples.
  6142. */
  6143. Encoder.DECDELAY = 528;
  6144. /**
  6145. * number of subbands
  6146. */
  6147. Encoder.SBLIMIT = 32;
  6148. /**
  6149. * parition bands bands
  6150. */
  6151. Encoder.CBANDS = 64;
  6152. /**
  6153. * number of critical bands/scale factor bands where masking is computed
  6154. */
  6155. Encoder.SBPSY_l = 21;
  6156. Encoder.SBPSY_s = 12;
  6157. /**
  6158. * total number of scalefactor bands encoded
  6159. */
  6160. Encoder.SBMAX_l = 22;
  6161. Encoder.SBMAX_s = 13;
  6162. Encoder.PSFB21 = 6;
  6163. Encoder.PSFB12 = 6;
  6164. /**
  6165. * FFT sizes
  6166. */
  6167. Encoder.BLKSIZE = 1024;
  6168. Encoder.HBLKSIZE = (Encoder.BLKSIZE / 2 + 1);
  6169. Encoder.BLKSIZE_s = 256;
  6170. Encoder.HBLKSIZE_s = (Encoder.BLKSIZE_s / 2 + 1);
  6171. Encoder.NORM_TYPE = 0;
  6172. Encoder.START_TYPE = 1;
  6173. Encoder.SHORT_TYPE = 2;
  6174. Encoder.STOP_TYPE = 3;
  6175. /**
  6176. * <PRE>
  6177. * Mode Extention:
  6178. * When we are in stereo mode, there are 4 possible methods to store these
  6179. * two channels. The stereo modes -m? are using a subset of them.
  6180. *
  6181. * -ms: MPG_MD_LR_LR
  6182. * -mj: MPG_MD_LR_LR and MPG_MD_MS_LR
  6183. * -mf: MPG_MD_MS_LR
  6184. * -mi: all
  6185. * </PRE>
  6186. */
  6187. Encoder.MPG_MD_LR_LR = 0;
  6188. Encoder.MPG_MD_LR_I = 1;
  6189. Encoder.MPG_MD_MS_LR = 2;
  6190. Encoder.MPG_MD_MS_I = 3;
  6191. Encoder.fircoef = [-0.0207887 * 5, -0.0378413 * 5,
  6192. -0.0432472 * 5, -0.031183 * 5, 7.79609e-18 * 5, 0.0467745 * 5,
  6193. 0.10091 * 5, 0.151365 * 5, 0.187098 * 5];
  6194. function Encoder() {
  6195. var FFTOFFSET = Encoder.FFTOFFSET;
  6196. var MPG_MD_MS_LR = Encoder.MPG_MD_MS_LR;
  6197. //BitStream bs;
  6198. //PsyModel psy;
  6199. //VBRTag vbr;
  6200. //QuantizePVT qupvt;
  6201. var bs = null;
  6202. this.psy = null;
  6203. var psy = null;
  6204. var vbr = null;
  6205. var qupvt = null;
  6206. //public final void setModules(BitStream bs, PsyModel psy, QuantizePVT qupvt,
  6207. // VBRTag vbr) {
  6208. this.setModules = function (_bs, _psy, _qupvt, _vbr) {
  6209. bs = _bs;
  6210. this.psy = _psy;
  6211. psy = _psy;
  6212. vbr = _vbr;
  6213. qupvt = _qupvt;
  6214. };
  6215. var newMDCT = new NewMDCT();
  6216. /***********************************************************************
  6217. *
  6218. * encoder and decoder delays
  6219. *
  6220. ***********************************************************************/
  6221. /**
  6222. * <PRE>
  6223. * layer III enc->dec delay: 1056 (1057?) (observed)
  6224. * layer II enc->dec delay: 480 (481?) (observed)
  6225. *
  6226. * polyphase 256-16 (dec or enc) = 240
  6227. * mdct 256+32 (9*32) (dec or enc) = 288
  6228. * total: 512+16
  6229. *
  6230. * My guess is that delay of polyphase filterbank is actualy 240.5
  6231. * (there are technical reasons for this, see postings in mp3encoder).
  6232. * So total Encode+Decode delay = ENCDELAY + 528 + 1
  6233. * </PRE>
  6234. */
  6235. /**
  6236. * auto-adjust of ATH, useful for low volume Gabriel Bouvigne 3 feb 2001
  6237. *
  6238. * modifies some values in gfp.internal_flags.ATH (gfc.ATH)
  6239. */
  6240. //private void adjust_ATH(final LameInternalFlags gfc) {
  6241. function adjust_ATH(gfc) {
  6242. var gr2_max, max_pow;
  6243. if (gfc.ATH.useAdjust == 0) {
  6244. gfc.ATH.adjust = 1.0;
  6245. /* no adjustment */
  6246. return;
  6247. }
  6248. /* jd - 2001 mar 12, 27, jun 30 */
  6249. /* loudness based on equal loudness curve; */
  6250. /* use granule with maximum combined loudness */
  6251. max_pow = gfc.loudness_sq[0][0];
  6252. gr2_max = gfc.loudness_sq[1][0];
  6253. if (gfc.channels_out == 2) {
  6254. abort();//fix cc 精简
  6255. } else {
  6256. max_pow += max_pow;
  6257. gr2_max += gr2_max;
  6258. }
  6259. if (gfc.mode_gr == 2) {
  6260. max_pow = Math.max(max_pow, gr2_max);
  6261. }
  6262. max_pow *= 0.5;
  6263. /* max_pow approaches 1.0 for full band noise */
  6264. /* jd - 2001 mar 31, jun 30 */
  6265. /* user tuning of ATH adjustment region */
  6266. max_pow *= gfc.ATH.aaSensitivityP;
  6267. /*
  6268. * adjust ATH depending on range of maximum value
  6269. */
  6270. /* jd - 2001 feb27, mar12,20, jun30, jul22 */
  6271. /* continuous curves based on approximation */
  6272. /* to GB's original values. */
  6273. /* For an increase in approximate loudness, */
  6274. /* set ATH adjust to adjust_limit immediately */
  6275. /* after a delay of one frame. */
  6276. /* For a loudness decrease, reduce ATH adjust */
  6277. /* towards adjust_limit gradually. */
  6278. /* max_pow is a loudness squared or a power. */
  6279. if (max_pow > 0.03125) { /* ((1 - 0.000625)/ 31.98) from curve below */
  6280. if (gfc.ATH.adjust >= 1.0) {
  6281. gfc.ATH.adjust = 1.0;
  6282. } else {
  6283. /* preceding frame has lower ATH adjust; */
  6284. /* ascend only to the preceding adjust_limit */
  6285. /* in case there is leading low volume */
  6286. if (gfc.ATH.adjust < gfc.ATH.adjustLimit) {
  6287. gfc.ATH.adjust = gfc.ATH.adjustLimit;
  6288. }
  6289. }
  6290. gfc.ATH.adjustLimit = 1.0;
  6291. } else { /* adjustment curve */
  6292. /* about 32 dB maximum adjust (0.000625) */
  6293. var adj_lim_new = 31.98 * max_pow + 0.000625;
  6294. if (gfc.ATH.adjust >= adj_lim_new) { /* descend gradually */
  6295. gfc.ATH.adjust *= adj_lim_new * 0.075 + 0.925;
  6296. if (gfc.ATH.adjust < adj_lim_new) { /* stop descent */
  6297. gfc.ATH.adjust = adj_lim_new;
  6298. }
  6299. } else { /* ascend */
  6300. if (gfc.ATH.adjustLimit >= adj_lim_new) {
  6301. gfc.ATH.adjust = adj_lim_new;
  6302. } else {
  6303. /* preceding frame has lower ATH adjust; */
  6304. /* ascend only to the preceding adjust_limit */
  6305. if (gfc.ATH.adjust < gfc.ATH.adjustLimit) {
  6306. gfc.ATH.adjust = gfc.ATH.adjustLimit;
  6307. }
  6308. }
  6309. }
  6310. gfc.ATH.adjustLimit = adj_lim_new;
  6311. }
  6312. }
  6313. /**
  6314. * <PRE>
  6315. * some simple statistics
  6316. *
  6317. * bitrate index 0: free bitrate . not allowed in VBR mode
  6318. * : bitrates, kbps depending on MPEG version
  6319. * bitrate index 15: forbidden
  6320. *
  6321. * mode_ext:
  6322. * 0: LR
  6323. * 1: LR-i
  6324. * 2: MS
  6325. * 3: MS-i
  6326. * </PRE>
  6327. */
  6328. function updateStats(gfc) {
  6329. var gr, ch;
  6330. /* count bitrate indices */
  6331. gfc.bitrate_stereoMode_Hist[gfc.bitrate_index][4]++;
  6332. gfc.bitrate_stereoMode_Hist[15][4]++;
  6333. /* count 'em for every mode extension in case of 2 channel encoding */
  6334. if (gfc.channels_out == 2) {
  6335. abort();//fix cc 精简
  6336. }
  6337. for (gr = 0; gr < gfc.mode_gr; ++gr) {
  6338. for (ch = 0; ch < gfc.channels_out; ++ch) {
  6339. var bt = gfc.l3_side.tt[gr][ch].block_type | 0;
  6340. if (gfc.l3_side.tt[gr][ch].mixed_block_flag != 0)
  6341. bt = 4;
  6342. gfc.bitrate_blockType_Hist[gfc.bitrate_index][bt]++;
  6343. gfc.bitrate_blockType_Hist[gfc.bitrate_index][5]++;
  6344. gfc.bitrate_blockType_Hist[15][bt]++;
  6345. gfc.bitrate_blockType_Hist[15][5]++;
  6346. }
  6347. }
  6348. }
  6349. function lame_encode_frame_init(gfp, inbuf) {
  6350. var gfc = gfp.internal_flags;
  6351. var ch, gr;
  6352. if (gfc.lame_encode_frame_init == 0) {
  6353. /* prime the MDCT/polyphase filterbank with a short block */
  6354. var i, j;
  6355. var primebuff0 = new_float(286 + 1152 + 576);
  6356. var primebuff1 = new_float(286 + 1152 + 576);
  6357. gfc.lame_encode_frame_init = 1;
  6358. for (i = 0, j = 0; i < 286 + 576 * (1 + gfc.mode_gr); ++i) {
  6359. if (i < 576 * gfc.mode_gr) {
  6360. primebuff0[i] = 0;
  6361. if (gfc.channels_out == 2)
  6362. primebuff1[i] = 0;
  6363. } else {
  6364. primebuff0[i] = inbuf[0][j];
  6365. if (gfc.channels_out == 2)
  6366. primebuff1[i] = inbuf[1][j];
  6367. ++j;
  6368. }
  6369. }
  6370. /* polyphase filtering / mdct */
  6371. for (gr = 0; gr < gfc.mode_gr; gr++) {
  6372. for (ch = 0; ch < gfc.channels_out; ch++) {
  6373. gfc.l3_side.tt[gr][ch].block_type = Encoder.SHORT_TYPE;
  6374. }
  6375. }
  6376. newMDCT.mdct_sub48(gfc, primebuff0, primebuff1);
  6377. /* check FFT will not use a negative starting offset */
  6378. /* check if we have enough data for FFT */
  6379. /* check if we have enough data for polyphase filterbank */
  6380. }
  6381. }
  6382. /**
  6383. * <PRE>
  6384. * encodeframe() Layer 3
  6385. *
  6386. * encode a single frame
  6387. *
  6388. *
  6389. * lame_encode_frame()
  6390. *
  6391. *
  6392. * gr 0 gr 1
  6393. * inbuf: |--------------|--------------|--------------|
  6394. *
  6395. *
  6396. * Polyphase (18 windows, each shifted 32)
  6397. * gr 0:
  6398. * window1 <----512---.
  6399. * window18 <----512---.
  6400. *
  6401. * gr 1:
  6402. * window1 <----512---.
  6403. * window18 <----512---.
  6404. *
  6405. *
  6406. *
  6407. * MDCT output: |--------------|--------------|--------------|
  6408. *
  6409. * FFT's <---------1024---------.
  6410. * <---------1024-------.
  6411. *
  6412. *
  6413. *
  6414. * inbuf = buffer of PCM data size=MP3 framesize
  6415. * encoder acts on inbuf[ch][0], but output is delayed by MDCTDELAY
  6416. * so the MDCT coefficints are from inbuf[ch][-MDCTDELAY]
  6417. *
  6418. * psy-model FFT has a 1 granule delay, so we feed it data for the
  6419. * next granule.
  6420. * FFT is centered over granule: 224+576+224
  6421. * So FFT starts at: 576-224-MDCTDELAY
  6422. *
  6423. * MPEG2: FFT ends at: BLKSIZE+576-224-MDCTDELAY (1328)
  6424. * MPEG1: FFT ends at: BLKSIZE+2*576-224-MDCTDELAY (1904)
  6425. *
  6426. * MPEG2: polyphase first window: [0..511]
  6427. * 18th window: [544..1055] (1056)
  6428. * MPEG1: 36th window: [1120..1631] (1632)
  6429. * data needed: 512+framesize-32
  6430. *
  6431. * A close look newmdct.c shows that the polyphase filterbank
  6432. * only uses data from [0..510] for each window. Perhaps because the window
  6433. * used by the filterbank is zero for the last point, so Takehiro's
  6434. * code doesn't bother to compute with it.
  6435. *
  6436. * FFT starts at 576-224-MDCTDELAY (304) = 576-FFTOFFSET
  6437. *
  6438. * </PRE>
  6439. */
  6440. this.lame_encode_mp3_frame = function (gfp, inbuf_l, inbuf_r, mp3buf, mp3bufPos, mp3buf_size) {
  6441. var mp3count;
  6442. var masking_LR = new_array_n([2, 2]);
  6443. /*
  6444. * LR masking &
  6445. * energy
  6446. */
  6447. masking_LR[0][0] = new III_psy_ratio();
  6448. masking_LR[0][1] = new III_psy_ratio();
  6449. masking_LR[1][0] = new III_psy_ratio();
  6450. masking_LR[1][1] = new III_psy_ratio();
  6451. var masking_MS = new_array_n([2, 2]);
  6452. /* MS masking & energy */
  6453. masking_MS[0][0] = new III_psy_ratio();
  6454. masking_MS[0][1] = new III_psy_ratio();
  6455. masking_MS[1][0] = new III_psy_ratio();
  6456. masking_MS[1][1] = new III_psy_ratio();
  6457. //III_psy_ratio masking[][];
  6458. var masking;
  6459. /* pointer to selected maskings */
  6460. var inbuf = [null, null];
  6461. var gfc = gfp.internal_flags;
  6462. var tot_ener = new_float_n([2, 4]);
  6463. var ms_ener_ratio = [.5, .5];
  6464. var pe = [[0., 0.], [0., 0.]];
  6465. var pe_MS = [[0., 0.], [0., 0.]];
  6466. //float[][] pe_use;
  6467. var pe_use;
  6468. var ch, gr;
  6469. inbuf[0] = inbuf_l;
  6470. inbuf[1] = inbuf_r;
  6471. if (gfc.lame_encode_frame_init == 0) {
  6472. /* first run? */
  6473. lame_encode_frame_init(gfp, inbuf);
  6474. }
  6475. /********************** padding *****************************/
  6476. /**
  6477. * <PRE>
  6478. * padding method as described in
  6479. * "MPEG-Layer3 / Bitstream Syntax and Decoding"
  6480. * by Martin Sieler, Ralph Sperschneider
  6481. *
  6482. * note: there is no padding for the very first frame
  6483. *
  6484. * Robert Hegemann 2000-06-22
  6485. * </PRE>
  6486. */
  6487. gfc.padding = 0;
  6488. if ((gfc.slot_lag -= gfc.frac_SpF) < 0) {//不可精简
  6489. gfc.slot_lag += gfp.out_samplerate;
  6490. gfc.padding = 1;
  6491. }
  6492. /****************************************
  6493. * Stage 1: psychoacoustic model *
  6494. ****************************************/
  6495. if (gfc.psymodel != 0) {
  6496. /*
  6497. * psychoacoustic model psy model has a 1 granule (576) delay that
  6498. * we must compensate for (mt 6/99).
  6499. */
  6500. var ret;
  6501. var bufp = [null, null];
  6502. /* address of beginning of left & right granule */
  6503. var bufpPos = 0;
  6504. /* address of beginning of left & right granule */
  6505. var blocktype = new_int(2);
  6506. for (gr = 0; gr < gfc.mode_gr; gr++) {
  6507. for (ch = 0; ch < gfc.channels_out; ch++) {
  6508. bufp[ch] = inbuf[ch];
  6509. bufpPos = 576 + gr * 576 - Encoder.FFTOFFSET;
  6510. }
  6511. if (gfp.VBR == VbrMode.vbr_mtrh || gfp.VBR == VbrMode.vbr_mt) {
  6512. abort();//fix cc 精简
  6513. } else {
  6514. ret = psy.L3psycho_anal_ns(gfp, bufp, bufpPos, gr,
  6515. masking_LR, masking_MS, pe[gr], pe_MS[gr],
  6516. tot_ener[gr], blocktype);
  6517. }
  6518. if (ret != 0)
  6519. return -4;
  6520. if (gfp.mode == MPEGMode.JOINT_STEREO) {
  6521. abort();//fix cc 精简 stereo
  6522. }
  6523. /* block type flags */
  6524. for (ch = 0; ch < gfc.channels_out; ch++) {
  6525. var cod_info = gfc.l3_side.tt[gr][ch];
  6526. cod_info.block_type = blocktype[ch];
  6527. cod_info.mixed_block_flag = 0;
  6528. }
  6529. }
  6530. } else {
  6531. abort();//fix cc 精简
  6532. }
  6533. /* auto-adjust of ATH, useful for low volume */
  6534. adjust_ATH(gfc);
  6535. /****************************************
  6536. * Stage 2: MDCT *
  6537. ****************************************/
  6538. /* polyphase filtering / mdct */
  6539. newMDCT.mdct_sub48(gfc, inbuf[0], inbuf[1]);
  6540. /****************************************
  6541. * Stage 3: MS/LR decision *
  6542. ****************************************/
  6543. /* Here will be selected MS or LR coding of the 2 stereo channels */
  6544. gfc.mode_ext = Encoder.MPG_MD_LR_LR;
  6545. if (gfp.force_ms) {
  6546. gfc.mode_ext = Encoder.MPG_MD_MS_LR;
  6547. } else if (gfp.mode == MPEGMode.JOINT_STEREO) {
  6548. abort();//fix cc 精简 stereo
  6549. }
  6550. /* bit and noise allocation */
  6551. if (gfc.mode_ext == MPG_MD_MS_LR) {
  6552. masking = masking_MS;
  6553. /* use MS masking */
  6554. pe_use = pe_MS;
  6555. } else {
  6556. masking = masking_LR;
  6557. /* use LR masking */
  6558. pe_use = pe;
  6559. }
  6560. /* copy data for MP3 frame analyzer */
  6561. if (gfp.analysis && gfc.pinfo != null) {
  6562. abort();//fix cc 精简
  6563. }
  6564. /****************************************
  6565. * Stage 4: quantization loop *
  6566. ****************************************/
  6567. if (gfp.VBR == VbrMode.vbr_off || gfp.VBR == VbrMode.vbr_abr) {
  6568. var i;
  6569. var f;
  6570. for (i = 0; i < 18; i++)
  6571. gfc.nsPsy.pefirbuf[i] = gfc.nsPsy.pefirbuf[i + 1];
  6572. f = 0.0;
  6573. for (gr = 0; gr < gfc.mode_gr; gr++)
  6574. for (ch = 0; ch < gfc.channels_out; ch++)
  6575. f += pe_use[gr][ch];
  6576. gfc.nsPsy.pefirbuf[18] = f;
  6577. f = gfc.nsPsy.pefirbuf[9];
  6578. for (i = 0; i < 9; i++)
  6579. f += (gfc.nsPsy.pefirbuf[i] + gfc.nsPsy.pefirbuf[18 - i])
  6580. * Encoder.fircoef[i];
  6581. f = (670 * 5 * gfc.mode_gr * gfc.channels_out) / f;
  6582. for (gr = 0; gr < gfc.mode_gr; gr++) {
  6583. for (ch = 0; ch < gfc.channels_out; ch++) {
  6584. pe_use[gr][ch] *= f;
  6585. }
  6586. }
  6587. }
  6588. gfc.iteration_loop.iteration_loop(gfp, pe_use, ms_ener_ratio, masking);
  6589. /****************************************
  6590. * Stage 5: bitstream formatting *
  6591. ****************************************/
  6592. /* write the frame to the bitstream */
  6593. bs.format_bitstream(gfp);
  6594. /* copy mp3 bit buffer into array */
  6595. mp3count = bs.copy_buffer(gfc, mp3buf, mp3bufPos, mp3buf_size, 1);
  6596. if (gfp.bWriteVbrTag)
  6597. vbr.addVbrFrame(gfp);
  6598. if (gfp.analysis && gfc.pinfo != null) {
  6599. abort();//fix cc 精简
  6600. }
  6601. updateStats(gfc);
  6602. return mp3count;
  6603. }
  6604. }
  6605. //package mp3;
  6606. function VBRSeekInfo() {
  6607. /**
  6608. * What we have seen so far.
  6609. */
  6610. this.sum = 0;
  6611. /**
  6612. * How many frames we have seen in this chunk.
  6613. */
  6614. this.seen = 0;
  6615. /**
  6616. * How many frames we want to collect into one chunk.
  6617. */
  6618. this.want = 0;
  6619. /**
  6620. * Actual position in our bag.
  6621. */
  6622. this.pos = 0;
  6623. /**
  6624. * Size of our bag.
  6625. */
  6626. this.size = 0;
  6627. /**
  6628. * Pointer to our bag.
  6629. */
  6630. this.bag = null;
  6631. this.nVbrNumFrames = 0;
  6632. this.nBytesWritten = 0;
  6633. /* VBR tag data */
  6634. this.TotalFrameSize = 0;
  6635. }
  6636. function IIISideInfo() {
  6637. this.tt = [[null, null], [null, null]];
  6638. this.main_data_begin = 0;
  6639. this.private_bits = 0;
  6640. this.resvDrain_pre = 0;
  6641. this.resvDrain_post = 0;
  6642. this.scfsi = [new_int(4), new_int(4)];
  6643. for (var gr = 0; gr < 2; gr++) {
  6644. for (var ch = 0; ch < 2; ch++) {
  6645. this.tt[gr][ch] = new GrInfo();
  6646. }
  6647. }
  6648. }
  6649. function III_psy_xmin() {
  6650. this.l = new_float(Encoder.SBMAX_l);
  6651. this.s = new_float_n([Encoder.SBMAX_s, 3]);
  6652. var self = this;
  6653. this.assign = function (iii_psy_xmin) {
  6654. System.arraycopy(iii_psy_xmin.l, 0, self.l, 0, Encoder.SBMAX_l);
  6655. for (var i = 0; i < Encoder.SBMAX_s; i++) {
  6656. for (var j = 0; j < 3; j++) {
  6657. self.s[i][j] = iii_psy_xmin.s[i][j];
  6658. }
  6659. }
  6660. }
  6661. }
  6662. //package mp3;
  6663. /**
  6664. * Variables used for --nspsytune
  6665. *
  6666. * @author Ken
  6667. *
  6668. */
  6669. function NsPsy() {
  6670. this.last_en_subshort = new_float_n([4, 9]);
  6671. this.lastAttacks = new_int(4);
  6672. this.pefirbuf = new_float(19);
  6673. this.longfact = new_float(Encoder.SBMAX_l);
  6674. this.shortfact = new_float(Encoder.SBMAX_s);
  6675. /**
  6676. * short block tuning
  6677. */
  6678. this.attackthre = 0.;
  6679. this.attackthre_s = 0.;
  6680. }
  6681. LameInternalFlags.MFSIZE = (3 * 1152 + Encoder.ENCDELAY - Encoder.MDCTDELAY);
  6682. LameInternalFlags.MAX_HEADER_BUF = 256;
  6683. LameInternalFlags.MAX_BITS_PER_CHANNEL = 4095;
  6684. LameInternalFlags.MAX_BITS_PER_GRANULE = 7680;
  6685. LameInternalFlags.BPC = 320;
  6686. function LameInternalFlags() {
  6687. var MAX_HEADER_LEN = 40;
  6688. /********************************************************************
  6689. * internal variables NOT set by calling program, and should not be *
  6690. * modified by the calling program *
  6691. ********************************************************************/
  6692. /**
  6693. * Some remarks to the Class_ID field: The Class ID is an Identifier for a
  6694. * pointer to this struct. It is very unlikely that a pointer to
  6695. * lame_global_flags has the same 32 bits in it's structure (large and other
  6696. * special properties, for instance prime).
  6697. *
  6698. * To test that the structure is right and initialized, use: if ( gfc .
  6699. * Class_ID == LAME_ID ) ... Other remark: If you set a flag to 0 for uninit
  6700. * data and 1 for init data, the right test should be "if (flag == 1)" and
  6701. * NOT "if (flag)". Unintended modification of this element will be
  6702. * otherwise misinterpreted as an init.
  6703. */
  6704. this.Class_ID = 0;
  6705. this.lame_encode_frame_init = 0;
  6706. this.iteration_init_init = 0;
  6707. this.fill_buffer_resample_init = 0;
  6708. //public float mfbuf[][] = new float[2][MFSIZE];
  6709. this.mfbuf = new_float_n([2, LameInternalFlags.MFSIZE]);
  6710. /**
  6711. * granules per frame
  6712. */
  6713. this.mode_gr = 0;
  6714. /**
  6715. * number of channels in the input data stream (PCM or decoded PCM)
  6716. */
  6717. this.channels_in = 0;
  6718. /**
  6719. * number of channels in the output data stream (not used for decoding)
  6720. */
  6721. this.channels_out = 0;
  6722. /**
  6723. * input_samp_rate/output_samp_rate
  6724. */
  6725. //public double resample_ratio;
  6726. this.resample_ratio = 0.;
  6727. this.mf_samples_to_encode = 0;
  6728. this.mf_size = 0;
  6729. /**
  6730. * min bitrate index
  6731. */
  6732. this.VBR_min_bitrate = 0;
  6733. /**
  6734. * max bitrate index
  6735. */
  6736. this.VBR_max_bitrate = 0;
  6737. this.bitrate_index = 0;
  6738. this.samplerate_index = 0;
  6739. this.mode_ext = 0;
  6740. /* lowpass and highpass filter control */
  6741. /**
  6742. * normalized frequency bounds of passband
  6743. */
  6744. this.lowpass1 = 0.;
  6745. this.lowpass2 = 0.;
  6746. /**
  6747. * normalized frequency bounds of passband
  6748. */
  6749. this.highpass1 = 0.;
  6750. this.highpass2 = 0.;
  6751. /**
  6752. * 0 = none 1 = ISO AAC model 2 = allow scalefac_select=1
  6753. */
  6754. this.noise_shaping = 0;
  6755. /**
  6756. * 0 = ISO model: amplify all distorted bands<BR>
  6757. * 1 = amplify within 50% of max (on db scale)<BR>
  6758. * 2 = amplify only most distorted band<BR>
  6759. * 3 = method 1 and refine with method 2<BR>
  6760. */
  6761. this.noise_shaping_amp = 0;
  6762. /**
  6763. * 0 = no substep<BR>
  6764. * 1 = use substep shaping at last step(VBR only)<BR>
  6765. * (not implemented yet)<BR>
  6766. * 2 = use substep inside loop<BR>
  6767. * 3 = use substep inside loop and last step<BR>
  6768. */
  6769. this.substep_shaping = 0;
  6770. /**
  6771. * 1 = gpsycho. 0 = none
  6772. */
  6773. this.psymodel = 0;
  6774. /**
  6775. * 0 = stop at over=0, all scalefacs amplified or<BR>
  6776. * a scalefac has reached max value<BR>
  6777. * 1 = stop when all scalefacs amplified or a scalefac has reached max value<BR>
  6778. * 2 = stop when all scalefacs amplified
  6779. */
  6780. this.noise_shaping_stop = 0;
  6781. /**
  6782. * 0 = no, 1 = yes
  6783. */
  6784. this.subblock_gain = 0;
  6785. /**
  6786. * 0 = no. 1=outside loop 2=inside loop(slow)
  6787. */
  6788. this.use_best_huffman = 0;
  6789. /**
  6790. * 0 = stop early after 0 distortion found. 1 = full search
  6791. */
  6792. this.full_outer_loop = 0;
  6793. //public IIISideInfo l3_side = new IIISideInfo();
  6794. this.l3_side = new IIISideInfo();
  6795. this.ms_ratio = new_float(2);
  6796. /* used for padding */
  6797. /**
  6798. * padding for the current frame?
  6799. */
  6800. this.padding = 0;
  6801. this.frac_SpF = 0;
  6802. this.slot_lag = 0;
  6803. /**
  6804. * optional ID3 tags
  6805. */
  6806. //public ID3TagSpec tag_spec;
  6807. this.tag_spec = null;
  6808. this.nMusicCRC = 0;
  6809. /* variables used by Quantize */
  6810. //public int OldValue[] = new int[2];
  6811. this.OldValue = new_int(2);
  6812. //public int CurrentStep[] = new int[2];
  6813. this.CurrentStep = new_int(2);
  6814. this.masking_lower = 0.;
  6815. //public int bv_scf[] = new int[576];
  6816. this.bv_scf = new_int(576);
  6817. //public int pseudohalf[] = new int[L3Side.SFBMAX];
  6818. this.pseudohalf = new_int(L3Side.SFBMAX);
  6819. /**
  6820. * will be set in lame_init_params
  6821. */
  6822. this.sfb21_extra = false;
  6823. /* BPC = maximum number of filter convolution windows to precompute */
  6824. //public float[][] inbuf_old = new float[2][];
  6825. this.inbuf_old = new Array(2);
  6826. //public float[][] blackfilt = new float[2 * BPC + 1][];
  6827. this.blackfilt = new Array(2 * LameInternalFlags.BPC + 1);
  6828. //public double itime[] = new double[2];
  6829. this.itime = new_double(2);
  6830. this.sideinfo_len = 0;
  6831. /* variables for newmdct.c */
  6832. //public float sb_sample[][][][] = new float[2][2][18][Encoder.SBLIMIT];
  6833. this.sb_sample = new_float_n([2, 2, 18, Encoder.SBLIMIT]);
  6834. this.amp_filter = new_float(32);
  6835. /* variables for BitStream */
  6836. /**
  6837. * <PRE>
  6838. * mpeg1: buffer=511 bytes smallest frame: 96-38(sideinfo)=58
  6839. * max number of frames in reservoir: 8
  6840. * mpeg2: buffer=255 bytes. smallest frame: 24-23bytes=1
  6841. * with VBR, if you are encoding all silence, it is possible to
  6842. * have 8kbs/24khz frames with 1byte of data each, which means we need
  6843. * to buffer up to 255 headers!
  6844. * </PRE>
  6845. */
  6846. /**
  6847. * also, max_header_buf has to be a power of two
  6848. */
  6849. /**
  6850. * max size of header is 38
  6851. */
  6852. function Header() {
  6853. this.write_timing = 0;
  6854. this.ptr = 0;
  6855. //public byte buf[] = new byte[MAX_HEADER_LEN];
  6856. this.buf = new_byte(MAX_HEADER_LEN);
  6857. }
  6858. this.header = new Array(LameInternalFlags.MAX_HEADER_BUF);
  6859. this.h_ptr = 0;
  6860. this.w_ptr = 0;
  6861. this.ancillary_flag = 0;
  6862. /* variables for Reservoir */
  6863. /**
  6864. * in bits
  6865. */
  6866. this.ResvSize = 0;
  6867. /**
  6868. * in bits
  6869. */
  6870. this.ResvMax = 0;
  6871. //public ScaleFac scalefac_band = new ScaleFac();
  6872. this.scalefac_band = new ScaleFac();
  6873. /* daa from PsyModel */
  6874. /* The static variables "r", "phi_sav", "new", "old" and "oldest" have */
  6875. /* to be remembered for the unpredictability measure. For "r" and */
  6876. /* "phi_sav", the first index from the left is the channel select and */
  6877. /* the second index is the "age" of the data. */
  6878. this.minval_l = new_float(Encoder.CBANDS);
  6879. this.minval_s = new_float(Encoder.CBANDS);
  6880. this.nb_1 = new_float_n([4, Encoder.CBANDS]);
  6881. this.nb_2 = new_float_n([4, Encoder.CBANDS]);
  6882. this.nb_s1 = new_float_n([4, Encoder.CBANDS]);
  6883. this.nb_s2 = new_float_n([4, Encoder.CBANDS]);
  6884. this.s3_ss = null;
  6885. this.s3_ll = null;
  6886. this.decay = 0.;
  6887. //public III_psy_xmin[] thm = new III_psy_xmin[4];
  6888. //public III_psy_xmin[] en = new III_psy_xmin[4];
  6889. this.thm = new Array(4);
  6890. this.en = new Array(4);
  6891. /**
  6892. * fft and energy calculation
  6893. */
  6894. this.tot_ener = new_float(4);
  6895. /* loudness calculation (for adaptive threshold of hearing) */
  6896. /**
  6897. * loudness^2 approx. per granule and channel
  6898. */
  6899. this.loudness_sq = new_float_n([2, 2]);
  6900. /**
  6901. * account for granule delay of L3psycho_anal
  6902. */
  6903. this.loudness_sq_save = new_float(2);
  6904. /**
  6905. * Scale Factor Bands
  6906. */
  6907. this.mld_l = new_float(Encoder.SBMAX_l);
  6908. this.mld_s = new_float(Encoder.SBMAX_s);
  6909. this.bm_l = new_int(Encoder.SBMAX_l);
  6910. this.bo_l = new_int(Encoder.SBMAX_l);
  6911. this.bm_s = new_int(Encoder.SBMAX_s);
  6912. this.bo_s = new_int(Encoder.SBMAX_s);
  6913. this.npart_l = 0;
  6914. this.npart_s = 0;
  6915. this.s3ind = new_int_n([Encoder.CBANDS, 2]);
  6916. this.s3ind_s = new_int_n([Encoder.CBANDS, 2]);
  6917. this.numlines_s = new_int(Encoder.CBANDS);
  6918. this.numlines_l = new_int(Encoder.CBANDS);
  6919. this.rnumlines_l = new_float(Encoder.CBANDS);
  6920. this.mld_cb_l = new_float(Encoder.CBANDS);
  6921. this.mld_cb_s = new_float(Encoder.CBANDS);
  6922. this.numlines_s_num1 = 0;
  6923. this.numlines_l_num1 = 0;
  6924. /* ratios */
  6925. this.pe = new_float(4);
  6926. this.ms_ratio_s_old = 0.;
  6927. this.ms_ratio_l_old = 0.;
  6928. this.ms_ener_ratio_old = 0.;
  6929. /**
  6930. * block type
  6931. */
  6932. this.blocktype_old = new_int(2);
  6933. /**
  6934. * variables used for --nspsytune
  6935. */
  6936. this.nsPsy = new NsPsy();
  6937. /**
  6938. * used for Xing VBR header
  6939. */
  6940. this.VBR_seek_table = new VBRSeekInfo();
  6941. /**
  6942. * all ATH related stuff
  6943. */
  6944. //public ATH ATH;
  6945. this.ATH = null;
  6946. this.PSY = null;
  6947. this.nogap_total = 0;
  6948. this.nogap_current = 0;
  6949. /* ReplayGain */
  6950. this.decode_on_the_fly = true;
  6951. this.findReplayGain = true;
  6952. this.findPeakSample = true;
  6953. this.PeakSample = 0.;
  6954. this.RadioGain = 0;
  6955. this.AudiophileGain = 0;
  6956. //public ReplayGain rgdata;
  6957. this.rgdata = null;
  6958. /**
  6959. * gain change required for preventing clipping
  6960. */
  6961. this.noclipGainChange = 0;
  6962. /**
  6963. * user-specified scale factor required for preventing clipping
  6964. */
  6965. this.noclipScale = 0.;
  6966. /* simple statistics */
  6967. this.bitrate_stereoMode_Hist = new_int_n([16, 4 + 1]);
  6968. /**
  6969. * norm/start/short/stop/mixed(short)/sum
  6970. */
  6971. this.bitrate_blockType_Hist = new_int_n([16, 4 + 1 + 1]);
  6972. //public PlottingData pinfo;
  6973. //public MPGLib.mpstr_tag hip;
  6974. this.pinfo = null;
  6975. this.hip = null;
  6976. this.in_buffer_nsamples = 0;
  6977. //public float[] in_buffer_0;
  6978. //public float[] in_buffer_1;
  6979. this.in_buffer_0 = null;
  6980. this.in_buffer_1 = null;
  6981. //public IIterationLoop iteration_loop;
  6982. this.iteration_loop = null;
  6983. for (var i = 0; i < this.en.length; i++) {
  6984. this.en[i] = new III_psy_xmin();
  6985. }
  6986. for (var i = 0; i < this.thm.length; i++) {
  6987. this.thm[i] = new III_psy_xmin();
  6988. }
  6989. for (var i = 0; i < this.header.length; i++) {
  6990. this.header[i] = new Header();
  6991. }
  6992. }
  6993. function FFT() {
  6994. var window = new_float(Encoder.BLKSIZE);
  6995. var window_s = new_float(Encoder.BLKSIZE_s / 2);
  6996. var costab = [
  6997. 9.238795325112867e-01, 3.826834323650898e-01,
  6998. 9.951847266721969e-01, 9.801714032956060e-02,
  6999. 9.996988186962042e-01, 2.454122852291229e-02,
  7000. 9.999811752826011e-01, 6.135884649154475e-03
  7001. ];
  7002. function fht(fz, fzPos, n) {
  7003. var tri = 0;
  7004. var k4;
  7005. var fi;
  7006. var gi;
  7007. n <<= 1;
  7008. /* to get BLKSIZE, because of 3DNow! ASM routine */
  7009. var fn = fzPos + n;
  7010. k4 = 4;
  7011. do {
  7012. var s1, c1;
  7013. var i, k1, k2, k3, kx;
  7014. kx = k4 >> 1;
  7015. k1 = k4;
  7016. k2 = k4 << 1;
  7017. k3 = k2 + k1;
  7018. k4 = k2 << 1;
  7019. fi = fzPos;
  7020. gi = fi + kx;
  7021. do {
  7022. var f0, f1, f2, f3;
  7023. f1 = fz[fi + 0] - fz[fi + k1];
  7024. f0 = fz[fi + 0] + fz[fi + k1];
  7025. f3 = fz[fi + k2] - fz[fi + k3];
  7026. f2 = fz[fi + k2] + fz[fi + k3];
  7027. fz[fi + k2] = f0 - f2;
  7028. fz[fi + 0] = f0 + f2;
  7029. fz[fi + k3] = f1 - f3;
  7030. fz[fi + k1] = f1 + f3;
  7031. f1 = fz[gi + 0] - fz[gi + k1];
  7032. f0 = fz[gi + 0] + fz[gi + k1];
  7033. f3 = (Util.SQRT2 * fz[gi + k3]);
  7034. f2 = (Util.SQRT2 * fz[gi + k2]);
  7035. fz[gi + k2] = f0 - f2;
  7036. fz[gi + 0] = f0 + f2;
  7037. fz[gi + k3] = f1 - f3;
  7038. fz[gi + k1] = f1 + f3;
  7039. gi += k4;
  7040. fi += k4;
  7041. } while (fi < fn);
  7042. c1 = costab[tri + 0];
  7043. s1 = costab[tri + 1];
  7044. for (i = 1; i < kx; i++) {
  7045. var c2, s2;
  7046. c2 = 1 - (2 * s1) * s1;
  7047. s2 = (2 * s1) * c1;
  7048. fi = fzPos + i;
  7049. gi = fzPos + k1 - i;
  7050. do {
  7051. var a, b, g0, f0, f1, g1, f2, g2, f3, g3;
  7052. b = s2 * fz[fi + k1] - c2 * fz[gi + k1];
  7053. a = c2 * fz[fi + k1] + s2 * fz[gi + k1];
  7054. f1 = fz[fi + 0] - a;
  7055. f0 = fz[fi + 0] + a;
  7056. g1 = fz[gi + 0] - b;
  7057. g0 = fz[gi + 0] + b;
  7058. b = s2 * fz[fi + k3] - c2 * fz[gi + k3];
  7059. a = c2 * fz[fi + k3] + s2 * fz[gi + k3];
  7060. f3 = fz[fi + k2] - a;
  7061. f2 = fz[fi + k2] + a;
  7062. g3 = fz[gi + k2] - b;
  7063. g2 = fz[gi + k2] + b;
  7064. b = s1 * f2 - c1 * g3;
  7065. a = c1 * f2 + s1 * g3;
  7066. fz[fi + k2] = f0 - a;
  7067. fz[fi + 0] = f0 + a;
  7068. fz[gi + k3] = g1 - b;
  7069. fz[gi + k1] = g1 + b;
  7070. b = c1 * g2 - s1 * f3;
  7071. a = s1 * g2 + c1 * f3;
  7072. fz[gi + k2] = g0 - a;
  7073. fz[gi + 0] = g0 + a;
  7074. fz[fi + k3] = f1 - b;
  7075. fz[fi + k1] = f1 + b;
  7076. gi += k4;
  7077. fi += k4;
  7078. } while (fi < fn);
  7079. c2 = c1;
  7080. c1 = c2 * costab[tri + 0] - s1 * costab[tri + 1];
  7081. s1 = c2 * costab[tri + 1] + s1 * costab[tri + 0];
  7082. }
  7083. tri += 2;
  7084. } while (k4 < n);
  7085. }
  7086. var rv_tbl = [0x00, 0x80, 0x40,
  7087. 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10,
  7088. 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70,
  7089. 0xf0, 0x08, 0x88, 0x48, 0xc8, 0x28,
  7090. 0xa8, 0x68, 0xe8, 0x18, 0x98, 0x58,
  7091. 0xd8, 0x38, 0xb8, 0x78, 0xf8, 0x04,
  7092. 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64,
  7093. 0xe4, 0x14, 0x94, 0x54, 0xd4, 0x34,
  7094. 0xb4, 0x74, 0xf4, 0x0c, 0x8c, 0x4c,
  7095. 0xcc, 0x2c, 0xac, 0x6c, 0xec, 0x1c,
  7096. 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c,
  7097. 0xfc, 0x02, 0x82, 0x42, 0xc2, 0x22,
  7098. 0xa2, 0x62, 0xe2, 0x12, 0x92, 0x52,
  7099. 0xd2, 0x32, 0xb2, 0x72, 0xf2, 0x0a,
  7100. 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a,
  7101. 0xea, 0x1a, 0x9a, 0x5a, 0xda, 0x3a,
  7102. 0xba, 0x7a, 0xfa, 0x06, 0x86, 0x46,
  7103. 0xc6, 0x26, 0xa6, 0x66, 0xe6, 0x16,
  7104. 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76,
  7105. 0xf6, 0x0e, 0x8e, 0x4e, 0xce, 0x2e,
  7106. 0xae, 0x6e, 0xee, 0x1e, 0x9e, 0x5e,
  7107. 0xde, 0x3e, 0xbe, 0x7e, 0xfe];
  7108. this.fft_short = function (gfc, x_real, chn, buffer, bufPos) {
  7109. for (var b = 0; b < 3; b++) {
  7110. var x = Encoder.BLKSIZE_s / 2;
  7111. var k = 0xffff & ((576 / 3) * (b + 1));
  7112. var j = Encoder.BLKSIZE_s / 8 - 1;
  7113. do {
  7114. var f0, f1, f2, f3, w;
  7115. var i = rv_tbl[j << 2] & 0xff;
  7116. f0 = window_s[i] * buffer[chn][bufPos + i + k];
  7117. w = window_s[0x7f - i] * buffer[chn][bufPos + i + k + 0x80];
  7118. f1 = f0 - w;
  7119. f0 = f0 + w;
  7120. f2 = window_s[i + 0x40] * buffer[chn][bufPos + i + k + 0x40];
  7121. w = window_s[0x3f - i] * buffer[chn][bufPos + i + k + 0xc0];
  7122. f3 = f2 - w;
  7123. f2 = f2 + w;
  7124. x -= 4;
  7125. x_real[b][x + 0] = f0 + f2;
  7126. x_real[b][x + 2] = f0 - f2;
  7127. x_real[b][x + 1] = f1 + f3;
  7128. x_real[b][x + 3] = f1 - f3;
  7129. f0 = window_s[i + 0x01] * buffer[chn][bufPos + i + k + 0x01];
  7130. w = window_s[0x7e - i] * buffer[chn][bufPos + i + k + 0x81];
  7131. f1 = f0 - w;
  7132. f0 = f0 + w;
  7133. f2 = window_s[i + 0x41] * buffer[chn][bufPos + i + k + 0x41];
  7134. w = window_s[0x3e - i] * buffer[chn][bufPos + i + k + 0xc1];
  7135. f3 = f2 - w;
  7136. f2 = f2 + w;
  7137. x_real[b][x + Encoder.BLKSIZE_s / 2 + 0] = f0 + f2;
  7138. x_real[b][x + Encoder.BLKSIZE_s / 2 + 2] = f0 - f2;
  7139. x_real[b][x + Encoder.BLKSIZE_s / 2 + 1] = f1 + f3;
  7140. x_real[b][x + Encoder.BLKSIZE_s / 2 + 3] = f1 - f3;
  7141. } while (--j >= 0);
  7142. fht(x_real[b], x, Encoder.BLKSIZE_s / 2);
  7143. /* BLKSIZE_s/2 because of 3DNow! ASM routine */
  7144. /* BLKSIZE/2 because of 3DNow! ASM routine */
  7145. }
  7146. }
  7147. this.fft_long = function (gfc, y, chn, buffer, bufPos) {
  7148. var jj = Encoder.BLKSIZE / 8 - 1;
  7149. var x = Encoder.BLKSIZE / 2;
  7150. do {
  7151. var f0, f1, f2, f3, w;
  7152. var i = rv_tbl[jj] & 0xff;
  7153. f0 = window[i] * buffer[chn][bufPos + i];
  7154. w = window[i + 0x200] * buffer[chn][bufPos + i + 0x200];
  7155. f1 = f0 - w;
  7156. f0 = f0 + w;
  7157. f2 = window[i + 0x100] * buffer[chn][bufPos + i + 0x100];
  7158. w = window[i + 0x300] * buffer[chn][bufPos + i + 0x300];
  7159. f3 = f2 - w;
  7160. f2 = f2 + w;
  7161. x -= 4;
  7162. y[x + 0] = f0 + f2;
  7163. y[x + 2] = f0 - f2;
  7164. y[x + 1] = f1 + f3;
  7165. y[x + 3] = f1 - f3;
  7166. f0 = window[i + 0x001] * buffer[chn][bufPos + i + 0x001];
  7167. w = window[i + 0x201] * buffer[chn][bufPos + i + 0x201];
  7168. f1 = f0 - w;
  7169. f0 = f0 + w;
  7170. f2 = window[i + 0x101] * buffer[chn][bufPos + i + 0x101];
  7171. w = window[i + 0x301] * buffer[chn][bufPos + i + 0x301];
  7172. f3 = f2 - w;
  7173. f2 = f2 + w;
  7174. y[x + Encoder.BLKSIZE / 2 + 0] = f0 + f2;
  7175. y[x + Encoder.BLKSIZE / 2 + 2] = f0 - f2;
  7176. y[x + Encoder.BLKSIZE / 2 + 1] = f1 + f3;
  7177. y[x + Encoder.BLKSIZE / 2 + 3] = f1 - f3;
  7178. } while (--jj >= 0);
  7179. fht(y, x, Encoder.BLKSIZE / 2);
  7180. /* BLKSIZE/2 because of 3DNow! ASM routine */
  7181. }
  7182. this.init_fft = function (gfc) {
  7183. /* The type of window used here will make no real difference, but */
  7184. /*
  7185. * in the interest of merging nspsytune stuff - switch to blackman
  7186. * window
  7187. */
  7188. for (var i = 0; i < Encoder.BLKSIZE; i++)
  7189. /* blackman window */
  7190. window[i] = (0.42 - 0.5 * Math.cos(2 * Math.PI * (i + .5)
  7191. / Encoder.BLKSIZE) + 0.08 * Math.cos(4 * Math.PI * (i + .5)
  7192. / Encoder.BLKSIZE));
  7193. for (var i = 0; i < Encoder.BLKSIZE_s / 2; i++)
  7194. window_s[i] = (0.5 * (1.0 - Math.cos(2.0 * Math.PI
  7195. * (i + 0.5) / Encoder.BLKSIZE_s)));
  7196. }
  7197. }
  7198. /*
  7199. * psymodel.c
  7200. *
  7201. * Copyright (c) 1999-2000 Mark Taylor
  7202. * Copyright (c) 2001-2002 Naoki Shibata
  7203. * Copyright (c) 2000-2003 Takehiro Tominaga
  7204. * Copyright (c) 2000-2008 Robert Hegemann
  7205. * Copyright (c) 2000-2005 Gabriel Bouvigne
  7206. * Copyright (c) 2000-2005 Alexander Leidinger
  7207. *
  7208. * This library is free software; you can redistribute it and/or
  7209. * modify it under the terms of the GNU Lesser General Public
  7210. * License as published by the Free Software Foundation; either
  7211. * version 2 of the License, or (at your option) any later version.
  7212. *
  7213. * This library is distributed in the hope that it will be useful,
  7214. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7215. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  7216. * Library General Public License for more details.
  7217. *
  7218. * You should have received a copy of the GNU Lesser General Public
  7219. * License along with this library; if not, write to the
  7220. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  7221. * Boston, MA 02111-1307, USA.
  7222. */
  7223. /* $Id: PsyModel.java,v 1.27 2011/05/24 20:48:06 kenchis Exp $ */
  7224. /*
  7225. PSYCHO ACOUSTICS
  7226. This routine computes the psycho acoustics, delayed by one granule.
  7227. Input: buffer of PCM data (1024 samples).
  7228. This window should be centered over the 576 sample granule window.
  7229. The routine will compute the psycho acoustics for
  7230. this granule, but return the psycho acoustics computed
  7231. for the *previous* granule. This is because the block
  7232. type of the previous granule can only be determined
  7233. after we have computed the psycho acoustics for the following
  7234. granule.
  7235. Output: maskings and energies for each scalefactor band.
  7236. block type, PE, and some correlation measures.
  7237. The PE is used by CBR modes to determine if extra bits
  7238. from the bit reservoir should be used. The correlation
  7239. measures are used to determine mid/side or regular stereo.
  7240. */
  7241. /*
  7242. Notation:
  7243. barks: a non-linear frequency scale. Mapping from frequency to
  7244. barks is given by freq2bark()
  7245. scalefactor bands: The spectrum (frequencies) are broken into
  7246. SBMAX "scalefactor bands". Thes bands
  7247. are determined by the MPEG ISO spec. In
  7248. the noise shaping/quantization code, we allocate
  7249. bits among the partition bands to achieve the
  7250. best possible quality
  7251. partition bands: The spectrum is also broken into about
  7252. 64 "partition bands". Each partition
  7253. band is about .34 barks wide. There are about 2-5
  7254. partition bands for each scalefactor band.
  7255. LAME computes all psycho acoustic information for each partition
  7256. band. Then at the end of the computations, this information
  7257. is mapped to scalefactor bands. The energy in each scalefactor
  7258. band is taken as the sum of the energy in all partition bands
  7259. which overlap the scalefactor band. The maskings can be computed
  7260. in the same way (and thus represent the average masking in that band)
  7261. or by taking the minmum value multiplied by the number of
  7262. partition bands used (which represents a minimum masking in that band).
  7263. */
  7264. /*
  7265. The general outline is as follows:
  7266. 1. compute the energy in each partition band
  7267. 2. compute the tonality in each partition band
  7268. 3. compute the strength of each partion band "masker"
  7269. 4. compute the masking (via the spreading function applied to each masker)
  7270. 5. Modifications for mid/side masking.
  7271. Each partition band is considiered a "masker". The strength
  7272. of the i'th masker in band j is given by:
  7273. s3(bark(i)-bark(j))*strength(i)
  7274. The strength of the masker is a function of the energy and tonality.
  7275. The more tonal, the less masking. LAME uses a simple linear formula
  7276. (controlled by NMT and TMN) which says the strength is given by the
  7277. energy divided by a linear function of the tonality.
  7278. */
  7279. /*
  7280. s3() is the "spreading function". It is given by a formula
  7281. determined via listening tests.
  7282. The total masking in the j'th partition band is the sum over
  7283. all maskings i. It is thus given by the convolution of
  7284. the strength with s3(), the "spreading function."
  7285. masking(j) = sum_over_i s3(i-j)*strength(i) = s3 o strength
  7286. where "o" = convolution operator. s3 is given by a formula determined
  7287. via listening tests. It is normalized so that s3 o 1 = 1.
  7288. Note: instead of a simple convolution, LAME also has the
  7289. option of using "additive masking"
  7290. The most critical part is step 2, computing the tonality of each
  7291. partition band. LAME has two tonality estimators. The first
  7292. is based on the ISO spec, and measures how predictiable the
  7293. signal is over time. The more predictable, the more tonal.
  7294. The second measure is based on looking at the spectrum of
  7295. a single granule. The more peaky the spectrum, the more
  7296. tonal. By most indications, the latter approach is better.
  7297. Finally, in step 5, the maskings for the mid and side
  7298. channel are possibly increased. Under certain circumstances,
  7299. noise in the mid & side channels is assumed to also
  7300. be masked by strong maskers in the L or R channels.
  7301. Other data computed by the psy-model:
  7302. ms_ratio side-channel / mid-channel masking ratio (for previous granule)
  7303. ms_ratio_next side-channel / mid-channel masking ratio for this granule
  7304. percep_entropy[2] L and R values (prev granule) of PE - A measure of how
  7305. much pre-echo is in the previous granule
  7306. percep_entropy_MS[2] mid and side channel values (prev granule) of percep_entropy
  7307. energy[4] L,R,M,S energy in each channel, prev granule
  7308. blocktype_d[2] block type to use for previous granule
  7309. */
  7310. //package mp3;
  7311. //import java.util.Arrays;
  7312. function PsyModel() {
  7313. var fft = new FFT();
  7314. var LOG10 = 2.30258509299404568402;
  7315. var rpelev = 2;
  7316. var rpelev2 = 16;
  7317. var rpelev_s = 2;
  7318. var rpelev2_s = 16;
  7319. /* size of each partition band, in barks: */
  7320. var DELBARK = .34;
  7321. /* tuned for output level (sensitive to energy scale) */
  7322. var VO_SCALE = (1. / (14752 * 14752) / (Encoder.BLKSIZE / 2));
  7323. var temporalmask_sustain_sec = 0.01;
  7324. var NS_PREECHO_ATT0 = 0.8;
  7325. var NS_PREECHO_ATT1 = 0.6;
  7326. var NS_PREECHO_ATT2 = 0.3;
  7327. var NS_MSFIX = 3.5;
  7328. var NSATTACKTHRE = 4.4;
  7329. var NSATTACKTHRE_S = 25;
  7330. var NSFIRLEN = 21;
  7331. /* size of each partition band, in barks: */
  7332. var LN_TO_LOG10 = 0.2302585093;
  7333. /*fix cc 精简 func NON_LINEAR_SCALE_ENERGY(x) {
  7334. return x;
  7335. }*/
  7336. /**
  7337. * <PRE>
  7338. * L3psycho_anal. Compute psycho acoustics.
  7339. *
  7340. * Data returned to the calling program must be delayed by one
  7341. * granule.
  7342. *
  7343. * This is done in two places.
  7344. * If we do not need to know the blocktype, the copying
  7345. * can be done here at the top of the program: we copy the data for
  7346. * the last granule (computed during the last call) before it is
  7347. * overwritten with the new data. It looks like this:
  7348. *
  7349. * 0. static psymodel_data
  7350. * 1. calling_program_data = psymodel_data
  7351. * 2. compute psymodel_data
  7352. *
  7353. * For data which needs to know the blocktype, the copying must be
  7354. * done at the end of this loop, and the old values must be saved:
  7355. *
  7356. * 0. static psymodel_data_old
  7357. * 1. compute psymodel_data
  7358. * 2. compute possible block type of this granule
  7359. * 3. compute final block type of previous granule based on #2.
  7360. * 4. calling_program_data = psymodel_data_old
  7361. * 5. psymodel_data_old = psymodel_data
  7362. * psycho_loudness_approx
  7363. * jd - 2001 mar 12
  7364. * in: energy - BLKSIZE/2 elements of frequency magnitudes ^ 2
  7365. * gfp - uses out_samplerate, ATHtype (also needed for ATHformula)
  7366. * returns: loudness^2 approximation, a positive value roughly tuned for a value
  7367. * of 1.0 for signals near clipping.
  7368. * notes: When calibrated, feeding this function binary white noise at sample
  7369. * values +32767 or -32768 should return values that approach 3.
  7370. * ATHformula is used to approximate an equal loudness curve.
  7371. * future: Data indicates that the shape of the equal loudness curve varies
  7372. * with intensity. This function might be improved by using an equal
  7373. * loudness curve shaped for typical playback levels (instead of the
  7374. * ATH, that is shaped for the threshold). A flexible realization might
  7375. * simply bend the existing ATH curve to achieve the desired shape.
  7376. * However, the potential gain may not be enough to justify an effort.
  7377. * </PRE>
  7378. */
  7379. function psycho_loudness_approx(energy, gfc) {
  7380. var loudness_power = 0.0;
  7381. /* apply weights to power in freq. bands */
  7382. for (var i = 0; i < Encoder.BLKSIZE / 2; ++i)
  7383. loudness_power += energy[i] * gfc.ATH.eql_w[i];
  7384. loudness_power *= VO_SCALE;
  7385. return loudness_power;
  7386. }
  7387. function compute_ffts(gfp, fftenergy, fftenergy_s, wsamp_l, wsamp_lPos, wsamp_s, wsamp_sPos, gr_out, chn, buffer, bufPos) {
  7388. var gfc = gfp.internal_flags;
  7389. if (chn < 2) {
  7390. fft.fft_long(gfc, wsamp_l[wsamp_lPos], chn, buffer, bufPos);
  7391. fft.fft_short(gfc, wsamp_s[wsamp_sPos], chn, buffer, bufPos);
  7392. }
  7393. /* FFT data for mid and side channel is derived from L & R */
  7394. else if (chn == 2) {
  7395. abort();//fix cc 精简
  7396. }
  7397. /*********************************************************************
  7398. * compute energies
  7399. *********************************************************************/
  7400. fftenergy[0] = /*fix NON_LINEAR_SCALE_ENERGY*/(wsamp_l[wsamp_lPos + 0][0]);
  7401. fftenergy[0] *= fftenergy[0];
  7402. for (var j = Encoder.BLKSIZE / 2 - 1; j >= 0; --j) {
  7403. var re = (wsamp_l[wsamp_lPos + 0])[Encoder.BLKSIZE / 2 - j];
  7404. var im = (wsamp_l[wsamp_lPos + 0])[Encoder.BLKSIZE / 2 + j];
  7405. fftenergy[Encoder.BLKSIZE / 2 - j] = /*fix NON_LINEAR_SCALE_ENERGY*/((re
  7406. * re + im * im) * 0.5);
  7407. }
  7408. for (var b = 2; b >= 0; --b) {
  7409. fftenergy_s[b][0] = (wsamp_s[wsamp_sPos + 0])[b][0];
  7410. fftenergy_s[b][0] *= fftenergy_s[b][0];
  7411. for (var j = Encoder.BLKSIZE_s / 2 - 1; j >= 0; --j) {
  7412. var re = (wsamp_s[wsamp_sPos + 0])[b][Encoder.BLKSIZE_s
  7413. / 2 - j];
  7414. var im = (wsamp_s[wsamp_sPos + 0])[b][Encoder.BLKSIZE_s
  7415. / 2 + j];
  7416. fftenergy_s[b][Encoder.BLKSIZE_s / 2 - j] = /*fix NON_LINEAR_SCALE_ENERGY*/((re
  7417. * re + im * im) * 0.5);
  7418. }
  7419. }
  7420. /* total energy */
  7421. {
  7422. var totalenergy = 0.0;
  7423. for (var j = 11; j < Encoder.HBLKSIZE; j++)
  7424. totalenergy += fftenergy[j];
  7425. gfc.tot_ener[chn] = totalenergy;
  7426. }
  7427. if (gfp.analysis) {
  7428. abort();//fix cc 精简
  7429. }
  7430. /*********************************************************************
  7431. * compute loudness approximation (used for ATH auto-level adjustment)
  7432. *********************************************************************/
  7433. if (gfp.athaa_loudapprox == 2 && chn < 2) {
  7434. // no loudness for mid/side ch
  7435. gfc.loudness_sq[gr_out][chn] = gfc.loudness_sq_save[chn];
  7436. gfc.loudness_sq_save[chn] = psycho_loudness_approx(fftenergy, gfc);
  7437. }
  7438. }
  7439. /* mask_add optimization */
  7440. /* init the limit values used to avoid computing log in mask_add when it is not necessary */
  7441. /**
  7442. * <PRE>
  7443. * For example, with i = 10*log10(m2/m1)/10*16 (= log10(m2/m1)*16)
  7444. *
  7445. * abs(i)>8 is equivalent (as i is an integer) to
  7446. * abs(i)>=9
  7447. * i>=9 || i<=-9
  7448. * equivalent to (as i is the biggest integer smaller than log10(m2/m1)*16
  7449. * or the smallest integer bigger than log10(m2/m1)*16 depending on the sign of log10(m2/m1)*16)
  7450. * log10(m2/m1)>=9/16 || log10(m2/m1)<=-9/16
  7451. * exp10 is strictly increasing thus this is equivalent to
  7452. * m2/m1 >= 10^(9/16) || m2/m1<=10^(-9/16) which are comparisons to constants
  7453. * </PRE>
  7454. */
  7455. /**
  7456. * as in if(i>8)
  7457. */
  7458. var I1LIMIT = 8;
  7459. /**
  7460. * as in if(i>24) . changed 23
  7461. */
  7462. var I2LIMIT = 23;
  7463. /**
  7464. * as in if(m<15)
  7465. */
  7466. var MLIMIT = 15;
  7467. var ma_max_i1;
  7468. var ma_max_i2;
  7469. var ma_max_m;
  7470. /**
  7471. * This is the masking table:<BR>
  7472. * According to tonality, values are going from 0dB (TMN) to 9.3dB (NMT).<BR>
  7473. * After additive masking computation, 8dB are added, so final values are
  7474. * going from 8dB to 17.3dB
  7475. *
  7476. * pow(10, -0.0..-0.6)
  7477. */
  7478. var tab = [1.0, 0.79433, 0.63096, 0.63096,
  7479. 0.63096, 0.63096, 0.63096, 0.25119, 0.11749];
  7480. function init_mask_add_max_values() {
  7481. ma_max_i1 = Math.pow(10, (I1LIMIT + 1) / 16.0);
  7482. ma_max_i2 = Math.pow(10, (I2LIMIT + 1) / 16.0);
  7483. ma_max_m = Math.pow(10, (MLIMIT) / 10.0);
  7484. }
  7485. var table1 = [3.3246 * 3.3246,
  7486. 3.23837 * 3.23837, 3.15437 * 3.15437, 3.00412 * 3.00412,
  7487. 2.86103 * 2.86103, 2.65407 * 2.65407, 2.46209 * 2.46209,
  7488. 2.284 * 2.284, 2.11879 * 2.11879, 1.96552 * 1.96552,
  7489. 1.82335 * 1.82335, 1.69146 * 1.69146, 1.56911 * 1.56911,
  7490. 1.46658 * 1.46658, 1.37074 * 1.37074, 1.31036 * 1.31036,
  7491. 1.25264 * 1.25264, 1.20648 * 1.20648, 1.16203 * 1.16203,
  7492. 1.12765 * 1.12765, 1.09428 * 1.09428, 1.0659 * 1.0659,
  7493. 1.03826 * 1.03826, 1.01895 * 1.01895, 1];
  7494. var table2 = [1.33352 * 1.33352,
  7495. 1.35879 * 1.35879, 1.38454 * 1.38454, 1.39497 * 1.39497,
  7496. 1.40548 * 1.40548, 1.3537 * 1.3537, 1.30382 * 1.30382,
  7497. 1.22321 * 1.22321, 1.14758 * 1.14758, 1];
  7498. var table3 = [2.35364 * 2.35364,
  7499. 2.29259 * 2.29259, 2.23313 * 2.23313, 2.12675 * 2.12675,
  7500. 2.02545 * 2.02545, 1.87894 * 1.87894, 1.74303 * 1.74303,
  7501. 1.61695 * 1.61695, 1.49999 * 1.49999, 1.39148 * 1.39148,
  7502. 1.29083 * 1.29083, 1.19746 * 1.19746, 1.11084 * 1.11084,
  7503. 1.03826 * 1.03826];
  7504. /**
  7505. * addition of simultaneous masking Naoki Shibata 2000/7
  7506. */
  7507. function mask_add(m1, m2, kk, b, gfc, shortblock) {
  7508. var ratio;
  7509. if (m2 > m1) {
  7510. if (m2 < (m1 * ma_max_i2))
  7511. ratio = m2 / m1;
  7512. else
  7513. return (m1 + m2);
  7514. } else {
  7515. if (m1 >= (m2 * ma_max_i2))
  7516. return (m1 + m2);
  7517. ratio = m1 / m2;
  7518. }
  7519. /* Should always be true, just checking */
  7520. m1 += m2;
  7521. //if (((long)(b + 3) & 0xffffffff) <= 3 + 3) {
  7522. if ((b + 3) <= 3 + 3) {
  7523. /* approximately, 1 bark = 3 partitions */
  7524. /* 65% of the cases */
  7525. /* originally 'if(i > 8)' */
  7526. if (ratio >= ma_max_i1) {
  7527. /* 43% of the total */
  7528. return m1;
  7529. }
  7530. /* 22% of the total */
  7531. var i = 0 | (Util.FAST_LOG10_X(ratio, 16.0));
  7532. return m1 * table2[i];
  7533. }
  7534. /**
  7535. * <PRE>
  7536. * m<15 equ log10((m1+m2)/gfc.ATH.cb[k])<1.5
  7537. * equ (m1+m2)/gfc.ATH.cb[k]<10^1.5
  7538. * equ (m1+m2)<10^1.5 * gfc.ATH.cb[k]
  7539. * </PRE>
  7540. */
  7541. var i = 0 | Util.FAST_LOG10_X(ratio, 16.0);
  7542. if (shortblock != 0) {
  7543. m2 = gfc.ATH.cb_s[kk] * gfc.ATH.adjust;
  7544. } else {
  7545. m2 = gfc.ATH.cb_l[kk] * gfc.ATH.adjust;
  7546. }
  7547. if (m1 < ma_max_m * m2) {
  7548. /* 3% of the total */
  7549. /* Originally if (m > 0) { */
  7550. if (m1 > m2) {
  7551. var f, r;
  7552. f = 1.0;
  7553. if (i <= 13)
  7554. f = table3[i];
  7555. r = Util.FAST_LOG10_X(m1 / m2, 10.0 / 15.0);
  7556. return m1 * ((table1[i] - f) * r + f);
  7557. }
  7558. if (i > 13)
  7559. return m1;
  7560. return m1 * table3[i];
  7561. }
  7562. /* 10% of total */
  7563. return m1 * table1[i];
  7564. }
  7565. //fix cc 精简
  7566. /**
  7567. * short block threshold calculation (part 2)
  7568. *
  7569. * partition band bo_s[sfb] is at the transition from scalefactor band sfb
  7570. * to the next one sfb+1; enn and thmm have to be split between them
  7571. */
  7572. function convert_partition2scalefac_s(gfc, eb, thr, chn, sblock) {
  7573. var sb, b;
  7574. var enn = 0.0;
  7575. var thmm = 0.0;
  7576. for (sb = b = 0; sb < Encoder.SBMAX_s; ++b, ++sb) {
  7577. var bo_s_sb = gfc.bo_s[sb];
  7578. var npart_s = gfc.npart_s;
  7579. var b_lim = bo_s_sb < npart_s ? bo_s_sb : npart_s;
  7580. while (b < b_lim) {
  7581. // iff failed, it may indicate some index error elsewhere
  7582. enn += eb[b];
  7583. thmm += thr[b];
  7584. b++;
  7585. }
  7586. gfc.en[chn].s[sb][sblock] = enn;
  7587. gfc.thm[chn].s[sb][sblock] = thmm;
  7588. if (b >= npart_s) {
  7589. ++sb;
  7590. break;
  7591. }
  7592. // iff failed, it may indicate some index error elsewhere
  7593. {
  7594. /* at transition sfb . sfb+1 */
  7595. var w_curr = gfc.PSY.bo_s_weight[sb];
  7596. var w_next = 1.0 - w_curr;
  7597. enn = w_curr * eb[b];
  7598. thmm = w_curr * thr[b];
  7599. gfc.en[chn].s[sb][sblock] += enn;
  7600. gfc.thm[chn].s[sb][sblock] += thmm;
  7601. enn = w_next * eb[b];
  7602. thmm = w_next * thr[b];
  7603. }
  7604. }
  7605. /* zero initialize the rest */
  7606. for (; sb < Encoder.SBMAX_s; ++sb) {
  7607. gfc.en[chn].s[sb][sblock] = 0;
  7608. gfc.thm[chn].s[sb][sblock] = 0;
  7609. }
  7610. }
  7611. /**
  7612. * longblock threshold calculation (part 2)
  7613. */
  7614. function convert_partition2scalefac_l(gfc, eb, thr, chn) {
  7615. var sb, b;
  7616. var enn = 0.0;
  7617. var thmm = 0.0;
  7618. for (sb = b = 0; sb < Encoder.SBMAX_l; ++b, ++sb) {
  7619. var bo_l_sb = gfc.bo_l[sb];
  7620. var npart_l = gfc.npart_l;
  7621. var b_lim = bo_l_sb < npart_l ? bo_l_sb : npart_l;
  7622. while (b < b_lim) {
  7623. // iff failed, it may indicate some index error elsewhere
  7624. enn += eb[b];
  7625. thmm += thr[b];
  7626. b++;
  7627. }
  7628. gfc.en[chn].l[sb] = enn;
  7629. gfc.thm[chn].l[sb] = thmm;
  7630. if (b >= npart_l) {
  7631. ++sb;
  7632. break;
  7633. }
  7634. {
  7635. /* at transition sfb . sfb+1 */
  7636. var w_curr = gfc.PSY.bo_l_weight[sb];
  7637. var w_next = 1.0 - w_curr;
  7638. enn = w_curr * eb[b];
  7639. thmm = w_curr * thr[b];
  7640. gfc.en[chn].l[sb] += enn;
  7641. gfc.thm[chn].l[sb] += thmm;
  7642. enn = w_next * eb[b];
  7643. thmm = w_next * thr[b];
  7644. }
  7645. }
  7646. /* zero initialize the rest */
  7647. for (; sb < Encoder.SBMAX_l; ++sb) {
  7648. gfc.en[chn].l[sb] = 0;
  7649. gfc.thm[chn].l[sb] = 0;
  7650. }
  7651. }
  7652. function compute_masking_s(gfp, fftenergy_s, eb, thr, chn, sblock) {
  7653. var gfc = gfp.internal_flags;
  7654. var j, b;
  7655. for (b = j = 0; b < gfc.npart_s; ++b) {
  7656. var ebb = 0, m = 0;
  7657. var n = gfc.numlines_s[b];
  7658. for (var i = 0; i < n; ++i, ++j) {
  7659. var el = fftenergy_s[sblock][j];
  7660. ebb += el;
  7661. if (m < el)
  7662. m = el;
  7663. }
  7664. eb[b] = ebb;
  7665. }
  7666. for (j = b = 0; b < gfc.npart_s; b++) {
  7667. var kk = gfc.s3ind_s[b][0];
  7668. var ecb = gfc.s3_ss[j++] * eb[kk];
  7669. ++kk;
  7670. while (kk <= gfc.s3ind_s[b][1]) {
  7671. ecb += gfc.s3_ss[j] * eb[kk];
  7672. ++j;
  7673. ++kk;
  7674. }
  7675. { /* limit calculated threshold by previous granule */
  7676. var x = rpelev_s * gfc.nb_s1[chn][b];
  7677. thr[b] = Math.min(ecb, x);
  7678. }
  7679. if (gfc.blocktype_old[chn & 1] == Encoder.SHORT_TYPE) {
  7680. /* limit calculated threshold by even older granule */
  7681. var x = rpelev2_s * gfc.nb_s2[chn][b];
  7682. var y = thr[b];
  7683. thr[b] = Math.min(x, y);
  7684. }
  7685. gfc.nb_s2[chn][b] = gfc.nb_s1[chn][b];
  7686. gfc.nb_s1[chn][b] = ecb;
  7687. }
  7688. for (; b <= Encoder.CBANDS; ++b) {
  7689. eb[b] = 0;
  7690. thr[b] = 0;
  7691. }
  7692. }
  7693. function block_type_set(gfp, uselongblock, blocktype_d, blocktype) {
  7694. var gfc = gfp.internal_flags;
  7695. if (gfp.short_blocks == ShortBlock.short_block_coupled
  7696. /* force both channels to use the same block type */
  7697. /* this is necessary if the frame is to be encoded in ms_stereo. */
  7698. /* But even without ms_stereo, FhG does this */
  7699. && !(uselongblock[0] != 0 && uselongblock[1] != 0))
  7700. uselongblock[0] = uselongblock[1] = 0;
  7701. /*
  7702. * update the blocktype of the previous granule, since it depends on
  7703. * what happend in this granule
  7704. */
  7705. for (var chn = 0; chn < gfc.channels_out; chn++) {
  7706. blocktype[chn] = Encoder.NORM_TYPE;
  7707. /* disable short blocks */
  7708. if (gfp.short_blocks == ShortBlock.short_block_dispensed)
  7709. uselongblock[chn] = 1;
  7710. if (gfp.short_blocks == ShortBlock.short_block_forced)
  7711. uselongblock[chn] = 0;
  7712. if (uselongblock[chn] != 0) {
  7713. /* no attack : use long blocks */
  7714. if (gfc.blocktype_old[chn] == Encoder.SHORT_TYPE)
  7715. blocktype[chn] = Encoder.STOP_TYPE;
  7716. } else {
  7717. /* attack : use short blocks */
  7718. blocktype[chn] = Encoder.SHORT_TYPE;
  7719. if (gfc.blocktype_old[chn] == Encoder.NORM_TYPE) {
  7720. gfc.blocktype_old[chn] = Encoder.START_TYPE;
  7721. }
  7722. if (gfc.blocktype_old[chn] == Encoder.STOP_TYPE)
  7723. gfc.blocktype_old[chn] = Encoder.SHORT_TYPE;
  7724. }
  7725. blocktype_d[chn] = gfc.blocktype_old[chn];
  7726. // value returned to calling program
  7727. gfc.blocktype_old[chn] = blocktype[chn];
  7728. // save for next call to l3psy_anal
  7729. }
  7730. }
  7731. function NS_INTERP(x, y, r) {
  7732. /* was pow((x),(r))*pow((y),1-(r)) */
  7733. if (r >= 1.0) {
  7734. /* 99.7% of the time */
  7735. return x;
  7736. }
  7737. if (r <= 0.0)
  7738. return y;
  7739. if (y > 0.0) {
  7740. /* rest of the time */
  7741. return (Math.pow(x / y, r) * y);
  7742. }
  7743. /* never happens */
  7744. return 0.0;
  7745. }
  7746. /**
  7747. * these values are tuned only for 44.1kHz...
  7748. */
  7749. var regcoef_s = [11.8, 13.6, 17.2, 32, 46.5,
  7750. 51.3, 57.5, 67.1, 71.5, 84.6, 97.6, 130,
  7751. /* 255.8 */
  7752. ];
  7753. function pecalc_s(mr, masking_lower) {
  7754. var pe_s = 1236.28 / 4;
  7755. for (var sb = 0; sb < Encoder.SBMAX_s - 1; sb++) {
  7756. for (var sblock = 0; sblock < 3; sblock++) {
  7757. var thm = mr.thm.s[sb][sblock];
  7758. if (thm > 0.0) {
  7759. var x = thm * masking_lower;
  7760. var en = mr.en.s[sb][sblock];
  7761. if (en > x) {
  7762. if (en > x * 1e10) {
  7763. pe_s += regcoef_s[sb] * (10.0 * LOG10);
  7764. } else {
  7765. pe_s += regcoef_s[sb] * Util.FAST_LOG10(en / x);
  7766. }
  7767. }
  7768. }
  7769. }
  7770. }
  7771. return pe_s;
  7772. }
  7773. /**
  7774. * these values are tuned only for 44.1kHz...
  7775. */
  7776. var regcoef_l = [6.8, 5.8, 5.8, 6.4, 6.5, 9.9,
  7777. 12.1, 14.4, 15, 18.9, 21.6, 26.9, 34.2, 40.2, 46.8, 56.5,
  7778. 60.7, 73.9, 85.7, 93.4, 126.1,
  7779. /* 241.3 */
  7780. ];
  7781. function pecalc_l(mr, masking_lower) {
  7782. var pe_l = 1124.23 / 4;
  7783. for (var sb = 0; sb < Encoder.SBMAX_l - 1; sb++) {
  7784. var thm = mr.thm.l[sb];
  7785. if (thm > 0.0) {
  7786. var x = thm * masking_lower;
  7787. var en = mr.en.l[sb];
  7788. if (en > x) {
  7789. if (en > x * 1e10) {
  7790. pe_l += regcoef_l[sb] * (10.0 * LOG10);
  7791. } else {
  7792. pe_l += regcoef_l[sb] * Util.FAST_LOG10(en / x);
  7793. }
  7794. }
  7795. }
  7796. }
  7797. return pe_l;
  7798. }
  7799. function calc_energy(gfc, fftenergy, eb, max, avg) {
  7800. var b, j;
  7801. for (b = j = 0; b < gfc.npart_l; ++b) {
  7802. var ebb = 0, m = 0;
  7803. var i;
  7804. for (i = 0; i < gfc.numlines_l[b]; ++i, ++j) {
  7805. var el = fftenergy[j];
  7806. ebb += el;
  7807. if (m < el)
  7808. m = el;
  7809. }
  7810. eb[b] = ebb;
  7811. max[b] = m;
  7812. avg[b] = ebb * gfc.rnumlines_l[b];
  7813. }
  7814. }
  7815. function calc_mask_index_l(gfc, max, avg, mask_idx) {
  7816. var last_tab_entry = tab.length - 1;
  7817. var b = 0;
  7818. var a = avg[b] + avg[b + 1];
  7819. if (a > 0.0) {
  7820. var m = max[b];
  7821. if (m < max[b + 1])
  7822. m = max[b + 1];
  7823. a = 20.0 * (m * 2.0 - a)
  7824. / (a * (gfc.numlines_l[b] + gfc.numlines_l[b + 1] - 1));
  7825. var k = 0 | a;
  7826. if (k > last_tab_entry)
  7827. k = last_tab_entry;
  7828. mask_idx[b] = k;
  7829. } else {
  7830. mask_idx[b] = 0;
  7831. }
  7832. for (b = 1; b < gfc.npart_l - 1; b++) {
  7833. a = avg[b - 1] + avg[b] + avg[b + 1];
  7834. if (a > 0.0) {
  7835. var m = max[b - 1];
  7836. if (m < max[b])
  7837. m = max[b];
  7838. if (m < max[b + 1])
  7839. m = max[b + 1];
  7840. a = 20.0
  7841. * (m * 3.0 - a)
  7842. / (a * (gfc.numlines_l[b - 1] + gfc.numlines_l[b]
  7843. + gfc.numlines_l[b + 1] - 1));
  7844. var k = 0 | a;
  7845. if (k > last_tab_entry)
  7846. k = last_tab_entry;
  7847. mask_idx[b] = k;
  7848. } else {
  7849. mask_idx[b] = 0;
  7850. }
  7851. }
  7852. a = avg[b - 1] + avg[b];
  7853. if (a > 0.0) {
  7854. var m = max[b - 1];
  7855. if (m < max[b])
  7856. m = max[b];
  7857. a = 20.0 * (m * 2.0 - a)
  7858. / (a * (gfc.numlines_l[b - 1] + gfc.numlines_l[b] - 1));
  7859. var k = 0 | a;
  7860. if (k > last_tab_entry)
  7861. k = last_tab_entry;
  7862. mask_idx[b] = k;
  7863. } else {
  7864. mask_idx[b] = 0;
  7865. }
  7866. }
  7867. var fircoef = [
  7868. -8.65163e-18 * 2, -0.00851586 * 2, -6.74764e-18 * 2, 0.0209036 * 2,
  7869. -3.36639e-17 * 2, -0.0438162 * 2, -1.54175e-17 * 2, 0.0931738 * 2,
  7870. -5.52212e-17 * 2, -0.313819 * 2
  7871. ];
  7872. this.L3psycho_anal_ns = function (gfp, buffer, bufPos, gr_out, masking_ratio, masking_MS_ratio, percep_entropy, percep_MS_entropy, energy, blocktype_d) {
  7873. /*
  7874. * to get a good cache performance, one has to think about the sequence,
  7875. * in which the variables are used.
  7876. */
  7877. var gfc = gfp.internal_flags;
  7878. /* fft and energy calculation */
  7879. var wsamp_L = new_float_n([2, Encoder.BLKSIZE]);
  7880. var wsamp_S = new_float_n([2, 3, Encoder.BLKSIZE_s]);
  7881. /* convolution */
  7882. var eb_l = new_float(Encoder.CBANDS + 1);
  7883. var eb_s = new_float(Encoder.CBANDS + 1);
  7884. var thr = new_float(Encoder.CBANDS + 2);
  7885. /* block type */
  7886. var blocktype = new_int(2), uselongblock = new_int(2);
  7887. /* usual variables like loop indices, etc.. */
  7888. var numchn, chn;
  7889. var b, i, j, k;
  7890. var sb, sblock;
  7891. /* variables used for --nspsytune */
  7892. var ns_hpfsmpl = new_float_n([2, 576]);
  7893. var pcfact;
  7894. var mask_idx_l = new_int(Encoder.CBANDS + 2), mask_idx_s = new_int(Encoder.CBANDS + 2);
  7895. Arrays.fill(mask_idx_s, 0);
  7896. numchn = gfc.channels_out;
  7897. /* chn=2 and 3 = Mid and Side channels */
  7898. if (gfp.mode == MPEGMode.JOINT_STEREO)
  7899. numchn = 4;
  7900. if (gfp.VBR == VbrMode.vbr_off)
  7901. pcfact = gfc.ResvMax == 0 ? 0 : ( gfc.ResvSize)
  7902. / gfc.ResvMax * 0.5;
  7903. else if (gfp.VBR == VbrMode.vbr_rh || gfp.VBR == VbrMode.vbr_mtrh
  7904. || gfp.VBR == VbrMode.vbr_mt) {
  7905. pcfact = 0.6;
  7906. } else
  7907. pcfact = 1.0;
  7908. /**********************************************************************
  7909. * Apply HPF of fs/4 to the input signal. This is used for attack
  7910. * detection / handling.
  7911. **********************************************************************/
  7912. /* Don't copy the input buffer into a temporary buffer */
  7913. /* unroll the loop 2 times */
  7914. for (chn = 0; chn < gfc.channels_out; chn++) {
  7915. /* apply high pass filter of fs/4 */
  7916. var firbuf = buffer[chn];
  7917. var firbufPos = bufPos + 576 - 350 - NSFIRLEN + 192;
  7918. for (i = 0; i < 576; i++) {
  7919. var sum1, sum2;
  7920. sum1 = firbuf[firbufPos + i + 10];
  7921. sum2 = 0.0;
  7922. for (j = 0; j < ((NSFIRLEN - 1) / 2) - 1; j += 2) {
  7923. sum1 += fircoef[j]
  7924. * (firbuf[firbufPos + i + j] + firbuf[firbufPos + i
  7925. + NSFIRLEN - j]);
  7926. sum2 += fircoef[j + 1]
  7927. * (firbuf[firbufPos + i + j + 1] + firbuf[firbufPos
  7928. + i + NSFIRLEN - j - 1]);
  7929. }
  7930. ns_hpfsmpl[chn][i] = sum1 + sum2;
  7931. }
  7932. masking_ratio[gr_out][chn].en.assign(gfc.en[chn]);
  7933. masking_ratio[gr_out][chn].thm.assign(gfc.thm[chn]);
  7934. if (numchn > 2) {
  7935. abort();//fix cc 精简
  7936. }
  7937. }
  7938. for (chn = 0; chn < numchn; chn++) {
  7939. var wsamp_l;
  7940. var wsamp_s;
  7941. var en_subshort = new_float(12);
  7942. var en_short = [0, 0, 0, 0];
  7943. var attack_intensity = new_float(12);
  7944. var ns_uselongblock = 1;
  7945. var attackThreshold;
  7946. var max = new_float(Encoder.CBANDS), avg = new_float(Encoder.CBANDS);
  7947. var ns_attacks = [0, 0, 0, 0];
  7948. var fftenergy = new_float(Encoder.HBLKSIZE);
  7949. var fftenergy_s = new_float_n([3, Encoder.HBLKSIZE_s]);
  7950. /*
  7951. * rh 20040301: the following loops do access one off the limits so
  7952. * I increase the array dimensions by one and initialize the
  7953. * accessed values to zero
  7954. */
  7955. /***************************************************************
  7956. * determine the block type (window type)
  7957. ***************************************************************/
  7958. /* calculate energies of each sub-shortblocks */
  7959. for (i = 0; i < 3; i++) {
  7960. en_subshort[i] = gfc.nsPsy.last_en_subshort[chn][i + 6];
  7961. attack_intensity[i] = en_subshort[i]
  7962. / gfc.nsPsy.last_en_subshort[chn][i + 4];
  7963. en_short[0] += en_subshort[i];
  7964. }
  7965. if (chn == 2) {
  7966. abort();//fix cc 精简
  7967. }
  7968. {
  7969. var pf = ns_hpfsmpl[chn & 1];
  7970. var pfPos = 0;
  7971. for (i = 0; i < 9; i++) {
  7972. var pfe = pfPos + 576 / 9;
  7973. var p = 1.;
  7974. for (; pfPos < pfe; pfPos++)
  7975. if (p < Math.abs(pf[pfPos]))
  7976. p = Math.abs(pf[pfPos]);
  7977. gfc.nsPsy.last_en_subshort[chn][i] = en_subshort[i + 3] = p;
  7978. en_short[1 + i / 3] += p;
  7979. if (p > en_subshort[i + 3 - 2]) {
  7980. p = p / en_subshort[i + 3 - 2];
  7981. } else if (en_subshort[i + 3 - 2] > p * 10.0) {
  7982. p = en_subshort[i + 3 - 2] / (p * 10.0);
  7983. } else
  7984. p = 0.0;
  7985. attack_intensity[i + 3] = p;
  7986. }
  7987. }
  7988. if (gfp.analysis) {
  7989. abort();//fix cc 精简
  7990. }
  7991. /* compare energies between sub-shortblocks */
  7992. attackThreshold = (chn == 3) ? gfc.nsPsy.attackthre_s
  7993. : gfc.nsPsy.attackthre;
  7994. for (i = 0; i < 12; i++)
  7995. if (0 == ns_attacks[i / 3]
  7996. && attack_intensity[i] > attackThreshold)
  7997. ns_attacks[i / 3] = (i % 3) + 1;
  7998. /*
  7999. * should have energy change between short blocks, in order to avoid
  8000. * periodic signals
  8001. */
  8002. for (i = 1; i < 4; i++) {
  8003. var ratio;
  8004. if (en_short[i - 1] > en_short[i]) {
  8005. ratio = en_short[i - 1] / en_short[i];
  8006. } else {
  8007. ratio = en_short[i] / en_short[i - 1];
  8008. }
  8009. if (ratio < 1.7) {
  8010. ns_attacks[i] = 0;
  8011. if (i == 1)
  8012. ns_attacks[0] = 0;
  8013. }
  8014. }
  8015. if (ns_attacks[0] != 0 && gfc.nsPsy.lastAttacks[chn] != 0)
  8016. ns_attacks[0] = 0;
  8017. if (gfc.nsPsy.lastAttacks[chn] == 3
  8018. || (ns_attacks[0] + ns_attacks[1] + ns_attacks[2] + ns_attacks[3]) != 0) {
  8019. ns_uselongblock = 0;
  8020. if (ns_attacks[1] != 0 && ns_attacks[0] != 0)
  8021. ns_attacks[1] = 0;
  8022. if (ns_attacks[2] != 0 && ns_attacks[1] != 0)
  8023. ns_attacks[2] = 0;
  8024. if (ns_attacks[3] != 0 && ns_attacks[2] != 0)
  8025. ns_attacks[3] = 0;
  8026. }
  8027. if (chn < 2) {
  8028. uselongblock[chn] = ns_uselongblock;
  8029. } else {
  8030. abort();//fix cc 精简
  8031. }
  8032. /*
  8033. * there is a one granule delay. Copy maskings computed last call
  8034. * into masking_ratio to return to calling program.
  8035. */
  8036. energy[chn] = gfc.tot_ener[chn];
  8037. /*********************************************************************
  8038. * compute FFTs
  8039. *********************************************************************/
  8040. wsamp_s = wsamp_S;
  8041. wsamp_l = wsamp_L;
  8042. compute_ffts(gfp, fftenergy, fftenergy_s, wsamp_l, (chn & 1),
  8043. wsamp_s, (chn & 1), gr_out, chn, buffer, bufPos);
  8044. /*********************************************************************
  8045. * Calculate the energy and the tonality of each partition.
  8046. *********************************************************************/
  8047. calc_energy(gfc, fftenergy, eb_l, max, avg);
  8048. calc_mask_index_l(gfc, max, avg, mask_idx_l);
  8049. /* compute masking thresholds for short blocks */
  8050. for (sblock = 0; sblock < 3; sblock++) {
  8051. var enn, thmm;
  8052. compute_masking_s(gfp, fftenergy_s, eb_s, thr, chn, sblock);
  8053. convert_partition2scalefac_s(gfc, eb_s, thr, chn, sblock);
  8054. /**** short block pre-echo control ****/
  8055. for (sb = 0; sb < Encoder.SBMAX_s; sb++) {
  8056. thmm = gfc.thm[chn].s[sb][sblock];
  8057. thmm *= NS_PREECHO_ATT0;
  8058. if (ns_attacks[sblock] >= 2 || ns_attacks[sblock + 1] == 1) {
  8059. var idx = (sblock != 0) ? sblock - 1 : 2;
  8060. var p = NS_INTERP(gfc.thm[chn].s[sb][idx], thmm,
  8061. NS_PREECHO_ATT1 * pcfact);
  8062. thmm = Math.min(thmm, p);
  8063. }
  8064. if (ns_attacks[sblock] == 1) {
  8065. var idx = (sblock != 0) ? sblock - 1 : 2;
  8066. var p = NS_INTERP(gfc.thm[chn].s[sb][idx], thmm,
  8067. NS_PREECHO_ATT2 * pcfact);
  8068. thmm = Math.min(thmm, p);
  8069. } else if ((sblock != 0 && ns_attacks[sblock - 1] == 3)
  8070. || (sblock == 0 && gfc.nsPsy.lastAttacks[chn] == 3)) {
  8071. var idx = (sblock != 2) ? sblock + 1 : 0;
  8072. var p = NS_INTERP(gfc.thm[chn].s[sb][idx], thmm,
  8073. NS_PREECHO_ATT2 * pcfact);
  8074. thmm = Math.min(thmm, p);
  8075. }
  8076. /* pulse like signal detection for fatboy.wav and so on */
  8077. enn = en_subshort[sblock * 3 + 3]
  8078. + en_subshort[sblock * 3 + 4]
  8079. + en_subshort[sblock * 3 + 5];
  8080. if (en_subshort[sblock * 3 + 5] * 6 < enn) {
  8081. thmm *= 0.5;
  8082. if (en_subshort[sblock * 3 + 4] * 6 < enn)
  8083. thmm *= 0.5;
  8084. }
  8085. gfc.thm[chn].s[sb][sblock] = thmm;
  8086. }
  8087. }
  8088. gfc.nsPsy.lastAttacks[chn] = ns_attacks[2];
  8089. /*********************************************************************
  8090. * convolve the partitioned energy and unpredictability with the
  8091. * spreading function, s3_l[b][k]
  8092. ********************************************************************/
  8093. k = 0;
  8094. {
  8095. for (b = 0; b < gfc.npart_l; b++) {
  8096. /*
  8097. * convolve the partitioned energy with the spreading
  8098. * function
  8099. */
  8100. var kk = gfc.s3ind[b][0];
  8101. var eb2 = eb_l[kk] * tab[mask_idx_l[kk]];
  8102. var ecb = gfc.s3_ll[k++] * eb2;
  8103. while (++kk <= gfc.s3ind[b][1]) {
  8104. eb2 = eb_l[kk] * tab[mask_idx_l[kk]];
  8105. ecb = mask_add(ecb, gfc.s3_ll[k++] * eb2, kk, kk - b,
  8106. gfc, 0);
  8107. }
  8108. ecb *= 0.158489319246111;
  8109. /* pow(10,-0.8) */
  8110. /**** long block pre-echo control ****/
  8111. /**
  8112. * <PRE>
  8113. * dont use long block pre-echo control if previous granule was
  8114. * a short block. This is to avoid the situation:
  8115. * frame0: quiet (very low masking)
  8116. * frame1: surge (triggers short blocks)
  8117. * frame2: regular frame. looks like pre-echo when compared to
  8118. * frame0, but all pre-echo was in frame1.
  8119. * </PRE>
  8120. */
  8121. /*
  8122. * chn=0,1 L and R channels
  8123. *
  8124. * chn=2,3 S and M channels.
  8125. */
  8126. if (gfc.blocktype_old[chn & 1] == Encoder.SHORT_TYPE)
  8127. thr[b] = ecb;
  8128. else
  8129. thr[b] = NS_INTERP(
  8130. Math.min(ecb, Math.min(rpelev
  8131. * gfc.nb_1[chn][b], rpelev2
  8132. * gfc.nb_2[chn][b])), ecb, pcfact);
  8133. gfc.nb_2[chn][b] = gfc.nb_1[chn][b];
  8134. gfc.nb_1[chn][b] = ecb;
  8135. }
  8136. }
  8137. for (; b <= Encoder.CBANDS; ++b) {
  8138. eb_l[b] = 0;
  8139. thr[b] = 0;
  8140. }
  8141. /* compute masking thresholds for long blocks */
  8142. convert_partition2scalefac_l(gfc, eb_l, thr, chn);
  8143. }
  8144. /* end loop over chn */
  8145. if (gfp.mode == MPEGMode.STEREO || gfp.mode == MPEGMode.JOINT_STEREO) {
  8146. abort();//fix cc 精简 stereo
  8147. }
  8148. if (gfp.mode == MPEGMode.JOINT_STEREO) {
  8149. abort();//fix cc 精简 stereo
  8150. }
  8151. /***************************************************************
  8152. * determine final block type
  8153. ***************************************************************/
  8154. block_type_set(gfp, uselongblock, blocktype_d, blocktype);
  8155. /*********************************************************************
  8156. * compute the value of PE to return ... no delay and advance
  8157. *********************************************************************/
  8158. for (chn = 0; chn < numchn; chn++) {
  8159. var ppe;
  8160. var ppePos = 0;
  8161. var type;
  8162. var mr;
  8163. if (chn > 1) {
  8164. abort();//fix cc 精简
  8165. } else {
  8166. ppe = percep_entropy;
  8167. ppePos = 0;
  8168. type = blocktype_d[chn];
  8169. mr = masking_ratio[gr_out][chn];
  8170. }
  8171. if (type == Encoder.SHORT_TYPE)
  8172. ppe[ppePos + chn] = pecalc_s(mr, gfc.masking_lower);
  8173. else
  8174. ppe[ppePos + chn] = pecalc_l(mr, gfc.masking_lower);
  8175. if (gfp.analysis)
  8176. gfc.pinfo.pe[gr_out][chn] = ppe[ppePos + chn];
  8177. }
  8178. return 0;
  8179. }
  8180. //fix cc 精简
  8181. /**
  8182. * The spreading function. Values returned in units of energy
  8183. */
  8184. function s3_func(bark) {
  8185. var tempx, x, tempy, temp;
  8186. tempx = bark;
  8187. if (tempx >= 0)
  8188. tempx *= 3;
  8189. else
  8190. tempx *= 1.5;
  8191. if (tempx >= 0.5 && tempx <= 2.5) {
  8192. temp = tempx - 0.5;
  8193. x = 8.0 * (temp * temp - 2.0 * temp);
  8194. } else
  8195. x = 0.0;
  8196. tempx += 0.474;
  8197. tempy = 15.811389 + 7.5 * tempx - 17.5
  8198. * Math.sqrt(1.0 + tempx * tempx);
  8199. if (tempy <= -60.0)
  8200. return 0.0;
  8201. tempx = Math.exp((x + tempy) * LN_TO_LOG10);
  8202. /**
  8203. * <PRE>
  8204. * Normalization. The spreading function should be normalized so that:
  8205. * +inf
  8206. * /
  8207. * | s3 [ bark ] d(bark) = 1
  8208. * /
  8209. * -inf
  8210. * </PRE>
  8211. */
  8212. tempx /= .6609193;
  8213. return tempx;
  8214. }
  8215. /**
  8216. * see for example "Zwicker: Psychoakustik, 1982; ISBN 3-540-11401-7
  8217. */
  8218. function freq2bark(freq) {
  8219. /* input: freq in hz output: barks */
  8220. if (freq < 0)
  8221. freq = 0;
  8222. freq = freq * 0.001;
  8223. return 13.0 * Math.atan(.76 * freq) + 3.5
  8224. * Math.atan(freq * freq / (7.5 * 7.5));
  8225. }
  8226. function init_numline(numlines, bo, bm, bval, bval_width, mld, bo_w, sfreq, blksize, scalepos, deltafreq, sbmax) {
  8227. var b_frq = new_float(Encoder.CBANDS + 1);
  8228. var sample_freq_frac = sfreq / (sbmax > 15 ? 2 * 576 : 2 * 192);
  8229. var partition = new_int(Encoder.HBLKSIZE);
  8230. var i;
  8231. sfreq /= blksize;
  8232. var j = 0;
  8233. var ni = 0;
  8234. /* compute numlines, the number of spectral lines in each partition band */
  8235. /* each partition band should be about DELBARK wide. */
  8236. for (i = 0; i < Encoder.CBANDS; i++) {
  8237. var bark1;
  8238. var j2;
  8239. bark1 = freq2bark(sfreq * j);
  8240. b_frq[i] = sfreq * j;
  8241. for (j2 = j; freq2bark(sfreq * j2) - bark1 < DELBARK
  8242. && j2 <= blksize / 2; j2++)
  8243. ;
  8244. numlines[i] = j2 - j;
  8245. ni = i + 1;
  8246. while (j < j2) {
  8247. partition[j++] = i;
  8248. }
  8249. if (j > blksize / 2) {
  8250. j = blksize / 2;
  8251. ++i;
  8252. break;
  8253. }
  8254. }
  8255. b_frq[i] = sfreq * j;
  8256. for (var sfb = 0; sfb < sbmax; sfb++) {
  8257. var i1, i2, start, end;
  8258. var arg;
  8259. start = scalepos[sfb];
  8260. end = scalepos[sfb + 1];
  8261. i1 = 0 | Math.floor(.5 + deltafreq * (start - .5));
  8262. if (i1 < 0)
  8263. i1 = 0;
  8264. i2 = 0 | Math.floor(.5 + deltafreq * (end - .5));
  8265. if (i2 > blksize / 2)
  8266. i2 = blksize / 2;
  8267. bm[sfb] = (partition[i1] + partition[i2]) / 2;
  8268. bo[sfb] = partition[i2];
  8269. var f_tmp = sample_freq_frac * end;
  8270. /*
  8271. * calculate how much of this band belongs to current scalefactor
  8272. * band
  8273. */
  8274. bo_w[sfb] = (f_tmp - b_frq[bo[sfb]])
  8275. / (b_frq[bo[sfb] + 1] - b_frq[bo[sfb]]);
  8276. if (bo_w[sfb] < 0) {
  8277. bo_w[sfb] = 0;
  8278. } else {
  8279. if (bo_w[sfb] > 1) {
  8280. bo_w[sfb] = 1;
  8281. }
  8282. }
  8283. /* setup stereo demasking thresholds */
  8284. /* formula reverse enginerred from plot in paper */
  8285. arg = freq2bark(sfreq * scalepos[sfb] * deltafreq);
  8286. arg = ( Math.min(arg, 15.5) / 15.5);
  8287. mld[sfb] = Math.pow(10.0,
  8288. 1.25 * (1 - Math.cos(Math.PI * arg)) - 2.5);
  8289. }
  8290. /* compute bark values of each critical band */
  8291. j = 0;
  8292. for (var k = 0; k < ni; k++) {
  8293. var w = numlines[k];
  8294. var bark1, bark2;
  8295. bark1 = freq2bark(sfreq * (j));
  8296. bark2 = freq2bark(sfreq * (j + w - 1));
  8297. bval[k] = .5 * (bark1 + bark2);
  8298. bark1 = freq2bark(sfreq * (j - .5));
  8299. bark2 = freq2bark(sfreq * (j + w - .5));
  8300. bval_width[k] = bark2 - bark1;
  8301. j += w;
  8302. }
  8303. return ni;
  8304. }
  8305. function init_s3_values(s3ind, npart, bval, bval_width, norm, use_old_s3) {
  8306. var s3 = new_float_n([Encoder.CBANDS, Encoder.CBANDS]);
  8307. /*
  8308. * The s3 array is not linear in the bark scale.
  8309. *
  8310. * bval[x] should be used to get the bark value.
  8311. */
  8312. var j;
  8313. var numberOfNoneZero = 0;
  8314. /**
  8315. * <PRE>
  8316. * s[i][j], the value of the spreading function,
  8317. * centered at band j (masker), for band i (maskee)
  8318. *
  8319. * i.e.: sum over j to spread into signal barkval=i
  8320. * NOTE: i and j are used opposite as in the ISO docs
  8321. * </PRE>
  8322. */
  8323. if (use_old_s3) {
  8324. for (var i = 0; i < npart; i++) {
  8325. for (j = 0; j < npart; j++) {
  8326. var v = s3_func(bval[i] - bval[j]) * bval_width[j];
  8327. s3[i][j] = v * norm[i];
  8328. }
  8329. }
  8330. } else {
  8331. abort();//fix cc 精简
  8332. }
  8333. for (var i = 0; i < npart; i++) {
  8334. for (j = 0; j < npart; j++) {
  8335. if (s3[i][j] > 0.0)
  8336. break;
  8337. }
  8338. s3ind[i][0] = j;
  8339. for (j = npart - 1; j > 0; j--) {
  8340. if (s3[i][j] > 0.0)
  8341. break;
  8342. }
  8343. s3ind[i][1] = j;
  8344. numberOfNoneZero += (s3ind[i][1] - s3ind[i][0] + 1);
  8345. }
  8346. var p = new_float(numberOfNoneZero);
  8347. var k = 0;
  8348. for (var i = 0; i < npart; i++)
  8349. for (j = s3ind[i][0]; j <= s3ind[i][1]; j++)
  8350. p[k++] = s3[i][j];
  8351. return p;
  8352. }
  8353. function stereo_demask(f) {
  8354. /* setup stereo demasking thresholds */
  8355. /* formula reverse enginerred from plot in paper */
  8356. var arg = freq2bark(f);
  8357. arg = (Math.min(arg, 15.5) / 15.5);
  8358. return Math.pow(10.0,
  8359. 1.25 * (1 - Math.cos(Math.PI * arg)) - 2.5);
  8360. }
  8361. /**
  8362. * NOTE: the bitrate reduction from the inter-channel masking effect is low
  8363. * compared to the chance of getting annyoing artefacts. L3psycho_anal_vbr
  8364. * does not use this feature. (Robert 071216)
  8365. */
  8366. this.psymodel_init = function (gfp) {
  8367. var gfc = gfp.internal_flags;
  8368. var i;
  8369. var useOldS3 = true;
  8370. var bvl_a = 13, bvl_b = 24;
  8371. var snr_l_a = 0, snr_l_b = 0;
  8372. var snr_s_a = -8.25, snr_s_b = -4.5;
  8373. var bval = new_float(Encoder.CBANDS);
  8374. var bval_width = new_float(Encoder.CBANDS);
  8375. var norm = new_float(Encoder.CBANDS);
  8376. var sfreq = gfp.out_samplerate;
  8377. switch (gfp.experimentalZ) {
  8378. default:
  8379. case 0:
  8380. useOldS3 = true;
  8381. break;
  8382. case 1:
  8383. useOldS3 = (gfp.VBR == VbrMode.vbr_mtrh || gfp.VBR == VbrMode.vbr_mt) ? false
  8384. : true;
  8385. break;
  8386. case 2:
  8387. useOldS3 = false;
  8388. break;
  8389. case 3:
  8390. bvl_a = 8;
  8391. snr_l_a = -1.75;
  8392. snr_l_b = -0.0125;
  8393. snr_s_a = -8.25;
  8394. snr_s_b = -2.25;
  8395. break;
  8396. }
  8397. gfc.ms_ener_ratio_old = .25;
  8398. gfc.blocktype_old[0] = gfc.blocktype_old[1] = Encoder.NORM_TYPE;
  8399. // the vbr header is long blocks
  8400. for (i = 0; i < 4; ++i) {
  8401. for (var j = 0; j < Encoder.CBANDS; ++j) {
  8402. gfc.nb_1[i][j] = 1e20;
  8403. gfc.nb_2[i][j] = 1e20;
  8404. gfc.nb_s1[i][j] = gfc.nb_s2[i][j] = 1.0;
  8405. }
  8406. for (var sb = 0; sb < Encoder.SBMAX_l; sb++) {
  8407. gfc.en[i].l[sb] = 1e20;
  8408. gfc.thm[i].l[sb] = 1e20;
  8409. }
  8410. for (var j = 0; j < 3; ++j) {
  8411. for (var sb = 0; sb < Encoder.SBMAX_s; sb++) {
  8412. gfc.en[i].s[sb][j] = 1e20;
  8413. gfc.thm[i].s[sb][j] = 1e20;
  8414. }
  8415. gfc.nsPsy.lastAttacks[i] = 0;
  8416. }
  8417. for (var j = 0; j < 9; j++)
  8418. gfc.nsPsy.last_en_subshort[i][j] = 10.;
  8419. }
  8420. /* init. for loudness approx. -jd 2001 mar 27 */
  8421. gfc.loudness_sq_save[0] = gfc.loudness_sq_save[1] = 0.0;
  8422. /*************************************************************************
  8423. * now compute the psychoacoustic model specific constants
  8424. ************************************************************************/
  8425. /* compute numlines, bo, bm, bval, bval_width, mld */
  8426. gfc.npart_l = init_numline(gfc.numlines_l, gfc.bo_l, gfc.bm_l, bval,
  8427. bval_width, gfc.mld_l, gfc.PSY.bo_l_weight, sfreq,
  8428. Encoder.BLKSIZE, gfc.scalefac_band.l, Encoder.BLKSIZE
  8429. / (2.0 * 576), Encoder.SBMAX_l);
  8430. /* compute the spreading function */
  8431. for (i = 0; i < gfc.npart_l; i++) {
  8432. var snr = snr_l_a;
  8433. if (bval[i] >= bvl_a) {
  8434. snr = snr_l_b * (bval[i] - bvl_a) / (bvl_b - bvl_a) + snr_l_a
  8435. * (bvl_b - bval[i]) / (bvl_b - bvl_a);
  8436. }
  8437. norm[i] = Math.pow(10.0, snr / 10.0);
  8438. if (gfc.numlines_l[i] > 0) {
  8439. gfc.rnumlines_l[i] = 1.0 / gfc.numlines_l[i];
  8440. } else {
  8441. gfc.rnumlines_l[i] = 0;
  8442. }
  8443. }
  8444. gfc.s3_ll = init_s3_values(gfc.s3ind, gfc.npart_l, bval, bval_width,
  8445. norm, useOldS3);
  8446. /* compute long block specific values, ATH and MINVAL */
  8447. var j = 0;
  8448. for (i = 0; i < gfc.npart_l; i++) {
  8449. var x;
  8450. /* ATH */
  8451. x = Float.MAX_VALUE;
  8452. for (var k = 0; k < gfc.numlines_l[i]; k++, j++) {
  8453. var freq = sfreq * j / (1000.0 * Encoder.BLKSIZE);
  8454. var level;
  8455. /*
  8456. * ATH below 100 Hz constant, not further climbing
  8457. */
  8458. level = this.ATHformula(freq * 1000, gfp) - 20;
  8459. // scale to FFT units; returned value is in dB
  8460. level = Math.pow(10., 0.1 * level);
  8461. // convert from dB . energy
  8462. level *= gfc.numlines_l[i];
  8463. if (x > level)
  8464. x = level;
  8465. }
  8466. gfc.ATH.cb_l[i] = x;
  8467. /*
  8468. * MINVAL. For low freq, the strength of the masking is limited by
  8469. * minval this is an ISO MPEG1 thing, dont know if it is really
  8470. * needed
  8471. */
  8472. /*
  8473. * FIXME: it does work to reduce low-freq problems in S53-Wind-Sax
  8474. * and lead-voice samples, but introduces some 3 kbps bit bloat too.
  8475. * TODO: Further refinement of the shape of this hack.
  8476. */
  8477. x = -20 + bval[i] * 20 / 10;
  8478. if (x > 6) {
  8479. x = 100;
  8480. }
  8481. if (x < -15) {
  8482. x = -15;
  8483. }
  8484. x -= 8.;
  8485. gfc.minval_l[i] = (Math.pow(10.0, x / 10.) * gfc.numlines_l[i]);
  8486. }
  8487. /************************************************************************
  8488. * do the same things for short blocks
  8489. ************************************************************************/
  8490. gfc.npart_s = init_numline(gfc.numlines_s, gfc.bo_s, gfc.bm_s, bval,
  8491. bval_width, gfc.mld_s, gfc.PSY.bo_s_weight, sfreq,
  8492. Encoder.BLKSIZE_s, gfc.scalefac_band.s, Encoder.BLKSIZE_s
  8493. / (2.0 * 192), Encoder.SBMAX_s);
  8494. /* SNR formula. short block is normalized by SNR. is it still right ? */
  8495. j = 0;
  8496. for (i = 0; i < gfc.npart_s; i++) {
  8497. var x;
  8498. var snr = snr_s_a;
  8499. if (bval[i] >= bvl_a) {
  8500. snr = snr_s_b * (bval[i] - bvl_a) / (bvl_b - bvl_a) + snr_s_a
  8501. * (bvl_b - bval[i]) / (bvl_b - bvl_a);
  8502. }
  8503. norm[i] = Math.pow(10.0, snr / 10.0);
  8504. /* ATH */
  8505. x = Float.MAX_VALUE;
  8506. for (var k = 0; k < gfc.numlines_s[i]; k++, j++) {
  8507. var freq = sfreq * j / (1000.0 * Encoder.BLKSIZE_s);
  8508. var level;
  8509. /* freq = Min(.1,freq); */
  8510. /*
  8511. * ATH below 100 Hz constant, not
  8512. * further climbing
  8513. */
  8514. level = this.ATHformula(freq * 1000, gfp) - 20;
  8515. // scale to FFT units; returned value is in dB
  8516. level = Math.pow(10., 0.1 * level);
  8517. // convert from dB . energy
  8518. level *= gfc.numlines_s[i];
  8519. if (x > level)
  8520. x = level;
  8521. }
  8522. gfc.ATH.cb_s[i] = x;
  8523. /*
  8524. * MINVAL. For low freq, the strength of the masking is limited by
  8525. * minval this is an ISO MPEG1 thing, dont know if it is really
  8526. * needed
  8527. */
  8528. x = (-7.0 + bval[i] * 7.0 / 12.0);
  8529. if (bval[i] > 12) {
  8530. x *= 1 + Math.log(1 + x) * 3.1;
  8531. }
  8532. if (bval[i] < 12) {
  8533. x *= 1 + Math.log(1 - x) * 2.3;
  8534. }
  8535. if (x < -15) {
  8536. x = -15;
  8537. }
  8538. x -= 8;
  8539. gfc.minval_s[i] = Math.pow(10.0, x / 10)
  8540. * gfc.numlines_s[i];
  8541. }
  8542. gfc.s3_ss = init_s3_values(gfc.s3ind_s, gfc.npart_s, bval, bval_width,
  8543. norm, useOldS3);
  8544. init_mask_add_max_values();
  8545. fft.init_fft(gfc);
  8546. /* setup temporal masking */
  8547. gfc.decay = Math.exp(-1.0 * LOG10
  8548. / (temporalmask_sustain_sec * sfreq / 192.0));
  8549. {
  8550. var msfix;
  8551. msfix = NS_MSFIX;
  8552. if ((gfp.exp_nspsytune & 2) != 0)
  8553. msfix = 1.0;
  8554. if (Math.abs(gfp.msfix) > 0.0)
  8555. msfix = gfp.msfix;
  8556. gfp.msfix = msfix;
  8557. /*
  8558. * spread only from npart_l bands. Normally, we use the spreading
  8559. * function to convolve from npart_l down to npart_l bands
  8560. */
  8561. for (var b = 0; b < gfc.npart_l; b++)
  8562. if (gfc.s3ind[b][1] > gfc.npart_l - 1)
  8563. gfc.s3ind[b][1] = gfc.npart_l - 1;
  8564. }
  8565. /*
  8566. * prepare for ATH auto adjustment: we want to decrease the ATH by 12 dB
  8567. * per second
  8568. */
  8569. var frame_duration = (576. * gfc.mode_gr / sfreq);
  8570. gfc.ATH.decay = Math.pow(10., -12. / 10. * frame_duration);
  8571. gfc.ATH.adjust = 0.01;
  8572. /* minimum, for leading low loudness */
  8573. gfc.ATH.adjustLimit = 1.0;
  8574. /* on lead, allow adjust up to maximum */
  8575. if (gfp.ATHtype != -1) {
  8576. /* compute equal loudness weights (eql_w) */
  8577. var freq;
  8578. var freq_inc = gfp.out_samplerate
  8579. / (Encoder.BLKSIZE);
  8580. var eql_balance = 0.0;
  8581. freq = 0.0;
  8582. for (i = 0; i < Encoder.BLKSIZE / 2; ++i) {
  8583. /* convert ATH dB to relative power (not dB) */
  8584. /* to determine eql_w */
  8585. freq += freq_inc;
  8586. gfc.ATH.eql_w[i] = 1. / Math.pow(10, this.ATHformula(freq, gfp) / 10);
  8587. eql_balance += gfc.ATH.eql_w[i];
  8588. }
  8589. eql_balance = 1.0 / eql_balance;
  8590. for (i = Encoder.BLKSIZE / 2; --i >= 0;) { /* scale weights */
  8591. gfc.ATH.eql_w[i] *= eql_balance;
  8592. }
  8593. }
  8594. {
  8595. for (var b = j = 0; b < gfc.npart_s; ++b) {
  8596. for (i = 0; i < gfc.numlines_s[b]; ++i) {
  8597. ++j;
  8598. }
  8599. }
  8600. for (var b = j = 0; b < gfc.npart_l; ++b) {
  8601. for (i = 0; i < gfc.numlines_l[b]; ++i) {
  8602. ++j;
  8603. }
  8604. }
  8605. }
  8606. j = 0;
  8607. for (i = 0; i < gfc.npart_l; i++) {
  8608. var freq = sfreq * (j + gfc.numlines_l[i] / 2) / (1.0 * Encoder.BLKSIZE);
  8609. gfc.mld_cb_l[i] = stereo_demask(freq);
  8610. j += gfc.numlines_l[i];
  8611. }
  8612. for (; i < Encoder.CBANDS; ++i) {
  8613. gfc.mld_cb_l[i] = 1;
  8614. }
  8615. j = 0;
  8616. for (i = 0; i < gfc.npart_s; i++) {
  8617. var freq = sfreq * (j + gfc.numlines_s[i] / 2) / (1.0 * Encoder.BLKSIZE_s);
  8618. gfc.mld_cb_s[i] = stereo_demask(freq);
  8619. j += gfc.numlines_s[i];
  8620. }
  8621. for (; i < Encoder.CBANDS; ++i) {
  8622. gfc.mld_cb_s[i] = 1;
  8623. }
  8624. return 0;
  8625. }
  8626. /**
  8627. * Those ATH formulas are returning their minimum value for input = -1
  8628. */
  8629. function ATHformula_GB(f, value) {
  8630. /**
  8631. * <PRE>
  8632. * from Painter & Spanias
  8633. * modified by Gabriel Bouvigne to better fit the reality
  8634. * ath = 3.640 * pow(f,-0.8)
  8635. * - 6.800 * exp(-0.6*pow(f-3.4,2.0))
  8636. * + 6.000 * exp(-0.15*pow(f-8.7,2.0))
  8637. * + 0.6* 0.001 * pow(f,4.0);
  8638. *
  8639. *
  8640. * In the past LAME was using the Painter &Spanias formula.
  8641. * But we had some recurrent problems with HF content.
  8642. * We measured real ATH values, and found the older formula
  8643. * to be inaccurate in the higher part. So we made this new
  8644. * formula and this solved most of HF problematic test cases.
  8645. * The tradeoff is that in VBR mode it increases a lot the
  8646. * bitrate.
  8647. * </PRE>
  8648. */
  8649. /*
  8650. * This curve can be adjusted according to the VBR scale: it adjusts
  8651. * from something close to Painter & Spanias on V9 up to Bouvigne's
  8652. * formula for V0. This way the VBR bitrate is more balanced according
  8653. * to the -V value.
  8654. */
  8655. // the following Hack allows to ask for the lowest value
  8656. if (f < -.3)
  8657. f = 3410;
  8658. // convert to khz
  8659. f /= 1000;
  8660. f = Math.max(0.1, f);
  8661. var ath = 3.640 * Math.pow(f, -0.8) - 6.800
  8662. * Math.exp(-0.6 * Math.pow(f - 3.4, 2.0)) + 6.000
  8663. * Math.exp(-0.15 * Math.pow(f - 8.7, 2.0))
  8664. + (0.6 + 0.04 * value) * 0.001 * Math.pow(f, 4.0);
  8665. return ath;
  8666. }
  8667. this.ATHformula = function (f, gfp) {
  8668. var ath;
  8669. switch (gfp.ATHtype) {
  8670. case 0:
  8671. ath = ATHformula_GB(f, 9);
  8672. break;
  8673. case 1:
  8674. // over sensitive, should probably be removed
  8675. ath = ATHformula_GB(f, -1);
  8676. break;
  8677. case 2:
  8678. ath = ATHformula_GB(f, 0);
  8679. break;
  8680. case 3:
  8681. // modification of GB formula by Roel
  8682. ath = ATHformula_GB(f, 1) + 6;
  8683. break;
  8684. case 4:
  8685. ath = ATHformula_GB(f, gfp.ATHcurve);
  8686. break;
  8687. default:
  8688. ath = ATHformula_GB(f, 0);
  8689. break;
  8690. }
  8691. return ath;
  8692. }
  8693. }
  8694. function Lame() {
  8695. var self = this;
  8696. var LAME_MAXALBUMART = (128 * 1024);
  8697. Lame.V9 = 410;
  8698. Lame.V8 = 420;
  8699. Lame.V7 = 430;
  8700. Lame.V6 = 440;
  8701. Lame.V5 = 450;
  8702. Lame.V4 = 460;
  8703. Lame.V3 = 470;
  8704. Lame.V2 = 480;
  8705. Lame.V1 = 490;
  8706. Lame.V0 = 500;
  8707. /* still there for compatibility */
  8708. Lame.R3MIX = 1000;
  8709. Lame.STANDARD = 1001;
  8710. Lame.EXTREME = 1002;
  8711. Lame.INSANE = 1003;
  8712. Lame.STANDARD_FAST = 1004;
  8713. Lame.EXTREME_FAST = 1005;
  8714. Lame.MEDIUM = 1006;
  8715. Lame.MEDIUM_FAST = 1007;
  8716. /**
  8717. * maximum size of mp3buffer needed if you encode at most 1152 samples for
  8718. * each call to lame_encode_buffer. see lame_encode_buffer() below
  8719. * (LAME_MAXMP3BUFFER is now obsolete)
  8720. */
  8721. var LAME_MAXMP3BUFFER = (16384 + LAME_MAXALBUMART);
  8722. Lame.LAME_MAXMP3BUFFER = LAME_MAXMP3BUFFER;
  8723. var ga;
  8724. var bs;
  8725. var p;
  8726. var qupvt;
  8727. var qu;
  8728. var psy = new PsyModel();
  8729. var vbr;
  8730. var ver;
  8731. var id3;
  8732. var mpglib;
  8733. this.enc = new Encoder();
  8734. this.setModules = function (_ga, _bs, _p, _qupvt, _qu, _vbr, _ver, _id3, _mpglib) {
  8735. ga = _ga;
  8736. bs = _bs;
  8737. p = _p;
  8738. qupvt = _qupvt;
  8739. qu = _qu;
  8740. vbr = _vbr;
  8741. ver = _ver;
  8742. id3 = _id3;
  8743. mpglib = _mpglib;
  8744. this.enc.setModules(bs, psy, qupvt, vbr);
  8745. }
  8746. /**
  8747. * PSY Model related stuff
  8748. */
  8749. function PSY() {
  8750. /**
  8751. * The dbQ stuff.
  8752. */
  8753. this.mask_adjust = 0.;
  8754. /**
  8755. * The dbQ stuff.
  8756. */
  8757. this.mask_adjust_short = 0.;
  8758. /* at transition from one scalefactor band to next */
  8759. /**
  8760. * Band weight long scalefactor bands.
  8761. */
  8762. this.bo_l_weight = new_float(Encoder.SBMAX_l);
  8763. /**
  8764. * Band weight short scalefactor bands.
  8765. */
  8766. this.bo_s_weight = new_float(Encoder.SBMAX_s);
  8767. }
  8768. function LowPassHighPass() {
  8769. this.lowerlimit = 0.;
  8770. }
  8771. function BandPass(bitrate, lPass) {
  8772. this.lowpass = lPass;
  8773. }
  8774. var LAME_ID = 0xFFF88E3B;
  8775. function lame_init_old(gfp) {
  8776. var gfc;
  8777. gfp.class_id = LAME_ID;
  8778. gfc = gfp.internal_flags = new LameInternalFlags();
  8779. /* Global flags. set defaults here for non-zero values */
  8780. /* see lame.h for description */
  8781. /*
  8782. * set integer values to -1 to mean that LAME will compute the best
  8783. * value, UNLESS the calling program as set it (and the value is no
  8784. * longer -1)
  8785. */
  8786. gfp.mode = MPEGMode.NOT_SET;
  8787. gfp.original = 1;
  8788. gfp.in_samplerate = 44100;
  8789. gfp.num_channels = 2;
  8790. gfp.num_samples = -1;
  8791. gfp.bWriteVbrTag = true;
  8792. gfp.quality = -1;
  8793. gfp.short_blocks = null;
  8794. gfc.subblock_gain = -1;
  8795. gfp.lowpassfreq = 0;
  8796. gfp.highpassfreq = 0;
  8797. gfp.lowpasswidth = -1;
  8798. gfp.highpasswidth = -1;
  8799. gfp.VBR = VbrMode.vbr_off;
  8800. gfp.VBR_q = 4;
  8801. gfp.ATHcurve = -1;
  8802. gfp.VBR_mean_bitrate_kbps = 128;
  8803. gfp.VBR_min_bitrate_kbps = 0;
  8804. gfp.VBR_max_bitrate_kbps = 0;
  8805. gfp.VBR_hard_min = 0;
  8806. gfc.VBR_min_bitrate = 1;
  8807. /* not 0 ????? */
  8808. gfc.VBR_max_bitrate = 13;
  8809. /* not 14 ????? */
  8810. gfp.quant_comp = -1;
  8811. gfp.quant_comp_short = -1;
  8812. gfp.msfix = -1;
  8813. gfc.resample_ratio = 1;
  8814. gfc.OldValue[0] = 180;
  8815. gfc.OldValue[1] = 180;
  8816. gfc.CurrentStep[0] = 4;
  8817. gfc.CurrentStep[1] = 4;
  8818. gfc.masking_lower = 1;
  8819. gfc.nsPsy.attackthre = -1;
  8820. gfc.nsPsy.attackthre_s = -1;
  8821. gfp.scale = -1;
  8822. gfp.athaa_type = -1;
  8823. gfp.ATHtype = -1;
  8824. /* default = -1 = set in lame_init_params */
  8825. gfp.athaa_loudapprox = -1;
  8826. /* 1 = flat loudness approx. (total energy) */
  8827. /* 2 = equal loudness curve */
  8828. gfp.athaa_sensitivity = 0.0;
  8829. /* no offset */
  8830. gfp.useTemporal = null;
  8831. gfp.interChRatio = -1;
  8832. /*
  8833. * The reason for int mf_samples_to_encode = ENCDELAY + POSTDELAY;
  8834. * ENCDELAY = internal encoder delay. And then we have to add
  8835. * POSTDELAY=288 because of the 50% MDCT overlap. A 576 MDCT granule
  8836. * decodes to 1152 samples. To synthesize the 576 samples centered under
  8837. * this granule we need the previous granule for the first 288 samples
  8838. * (no problem), and the next granule for the next 288 samples (not
  8839. * possible if this is last granule). So we need to pad with 288 samples
  8840. * to make sure we can encode the 576 samples we are interested in.
  8841. */
  8842. gfc.mf_samples_to_encode = Encoder.ENCDELAY + Encoder.POSTDELAY;
  8843. gfp.encoder_padding = 0;
  8844. gfc.mf_size = Encoder.ENCDELAY - Encoder.MDCTDELAY;
  8845. /*
  8846. * we pad input with this many 0's
  8847. */
  8848. gfp.findReplayGain = false;
  8849. gfp.decode_on_the_fly = false;
  8850. gfc.decode_on_the_fly = false;
  8851. gfc.findReplayGain = false;
  8852. gfc.findPeakSample = false;
  8853. gfc.RadioGain = 0;
  8854. gfc.AudiophileGain = 0;
  8855. gfc.noclipGainChange = 0;
  8856. gfc.noclipScale = -1.0;
  8857. gfp.preset = 0;
  8858. gfp.write_id3tag_automatic = true;
  8859. return 0;
  8860. }
  8861. this.lame_init = function () {
  8862. var gfp = new LameGlobalFlags();
  8863. var ret = lame_init_old(gfp);
  8864. if (ret != 0) {
  8865. return null;
  8866. }
  8867. gfp.lame_allocated_gfp = 1;
  8868. return gfp;
  8869. }
  8870. function filter_coef(x) {
  8871. if (x > 1.0)
  8872. return 0.0;
  8873. if (x <= 0.0)
  8874. return 1.0;
  8875. return Math.cos(Math.PI / 2 * x);
  8876. }
  8877. this.nearestBitrateFullIndex = function (bitrate) {
  8878. /* borrowed from DM abr presets */
  8879. var full_bitrate_table = [8, 16, 24, 32, 40, 48, 56, 64, 80,
  8880. 96, 112, 128, 160, 192, 224, 256, 320];
  8881. var lower_range = 0, lower_range_kbps = 0, upper_range = 0, upper_range_kbps = 0;
  8882. /* We assume specified bitrate will be 320kbps */
  8883. upper_range_kbps = full_bitrate_table[16];
  8884. upper_range = 16;
  8885. lower_range_kbps = full_bitrate_table[16];
  8886. lower_range = 16;
  8887. /*
  8888. * Determine which significant bitrates the value specified falls
  8889. * between, if loop ends without breaking then we were correct above
  8890. * that the value was 320
  8891. */
  8892. for (var b = 0; b < 16; b++) {
  8893. if ((Math.max(bitrate, full_bitrate_table[b + 1])) != bitrate) {
  8894. upper_range_kbps = full_bitrate_table[b + 1];
  8895. upper_range = b + 1;
  8896. lower_range_kbps = full_bitrate_table[b];
  8897. lower_range = (b);
  8898. break;
  8899. /* We found upper range */
  8900. }
  8901. }
  8902. /* Determine which range the value specified is closer to */
  8903. if ((upper_range_kbps - bitrate) > (bitrate - lower_range_kbps)) {
  8904. return lower_range;
  8905. }
  8906. return upper_range;
  8907. }
  8908. //fix cc 精简
  8909. /**
  8910. * convert samp freq in Hz to index
  8911. */
  8912. function SmpFrqIndex(sample_freq, gpf) {
  8913. switch (sample_freq) {
  8914. case 44100:
  8915. gpf.version = 1;
  8916. return 0;
  8917. case 48000:
  8918. gpf.version = 1;
  8919. return 1;
  8920. case 32000:
  8921. gpf.version = 1;
  8922. return 2;
  8923. case 22050:
  8924. gpf.version = 0;
  8925. return 0;
  8926. case 24000:
  8927. gpf.version = 0;
  8928. return 1;
  8929. case 16000:
  8930. gpf.version = 0;
  8931. return 2;
  8932. case 11025:
  8933. gpf.version = 0;
  8934. return 0;
  8935. case 12000:
  8936. gpf.version = 0;
  8937. return 1;
  8938. case 8000:
  8939. gpf.version = 0;
  8940. return 2;
  8941. default:
  8942. gpf.version = 0;
  8943. return -1;
  8944. }
  8945. }
  8946. /**
  8947. * @param bRate
  8948. * legal rates from 8 to 320
  8949. */
  8950. function FindNearestBitrate(bRate, version, samplerate) {
  8951. /* MPEG-1 or MPEG-2 LSF */
  8952. if (samplerate < 16000)
  8953. version = 2;
  8954. var bitrate = Tables.bitrate_table[version][1];
  8955. for (var i = 2; i <= 14; i++) {
  8956. if (Tables.bitrate_table[version][i] > 0) {
  8957. if (Math.abs(Tables.bitrate_table[version][i] - bRate) < Math
  8958. .abs(bitrate - bRate))
  8959. bitrate = Tables.bitrate_table[version][i];
  8960. }
  8961. }
  8962. return bitrate;
  8963. }
  8964. /**
  8965. * @param bRate
  8966. * legal rates from 32 to 448 kbps
  8967. * @param version
  8968. * MPEG-1 or MPEG-2/2.5 LSF
  8969. */
  8970. function BitrateIndex(bRate, version, samplerate) {
  8971. /* convert bitrate in kbps to index */
  8972. if (samplerate < 16000)
  8973. version = 2;
  8974. for (var i = 0; i <= 14; i++) {
  8975. if (Tables.bitrate_table[version][i] > 0) {
  8976. if (Tables.bitrate_table[version][i] == bRate) {
  8977. return i;
  8978. }
  8979. }
  8980. }
  8981. return -1;
  8982. }
  8983. function optimum_bandwidth(lh, bitrate) {
  8984. /**
  8985. * <PRE>
  8986. * Input:
  8987. * bitrate total bitrate in kbps
  8988. *
  8989. * Output:
  8990. * lowerlimit: best lowpass frequency limit for input filter in Hz
  8991. * upperlimit: best highpass frequency limit for input filter in Hz
  8992. * </PRE>
  8993. */
  8994. var freq_map = [new BandPass(8, 2000),
  8995. new BandPass(16, 3700), new BandPass(24, 3900),
  8996. new BandPass(32, 5500), new BandPass(40, 7000),
  8997. new BandPass(48, 7500), new BandPass(56, 10000),
  8998. new BandPass(64, 11000), new BandPass(80, 13500),
  8999. new BandPass(96, 15100), new BandPass(112, 15600),
  9000. new BandPass(128, 17000), new BandPass(160, 17500),
  9001. new BandPass(192, 18600), new BandPass(224, 19400),
  9002. new BandPass(256, 19700), new BandPass(320, 20500)];
  9003. var table_index = self.nearestBitrateFullIndex(bitrate);
  9004. lh.lowerlimit = freq_map[table_index].lowpass;
  9005. }
  9006. function lame_init_params_ppflt(gfp) {
  9007. var gfc = gfp.internal_flags;
  9008. /***************************************************************/
  9009. /* compute info needed for polyphase filter (filter type==0, default) */
  9010. /***************************************************************/
  9011. var lowpass_band = 32;
  9012. var highpass_band = -1;
  9013. if (gfc.lowpass1 > 0) {
  9014. var minband = 999;
  9015. for (var band = 0; band <= 31; band++) {
  9016. var freq = (band / 31.0);
  9017. /* this band and above will be zeroed: */
  9018. if (freq >= gfc.lowpass2) {
  9019. lowpass_band = Math.min(lowpass_band, band);
  9020. }
  9021. if (gfc.lowpass1 < freq && freq < gfc.lowpass2) {
  9022. minband = Math.min(minband, band);
  9023. }
  9024. }
  9025. /*
  9026. * compute the *actual* transition band implemented by the polyphase
  9027. * filter
  9028. */
  9029. if (minband == 999) {
  9030. gfc.lowpass1 = (lowpass_band - .75) / 31.0;
  9031. } else {
  9032. gfc.lowpass1 = (minband - .75) / 31.0;
  9033. }
  9034. gfc.lowpass2 = lowpass_band / 31.0;
  9035. }
  9036. /*
  9037. * make sure highpass filter is within 90% of what the effective
  9038. * highpass frequency will be
  9039. */
  9040. if (gfc.highpass2 > 0) {
  9041. abort();//fix cc 精简
  9042. }
  9043. if (gfc.highpass2 > 0) {
  9044. abort();//fix cc 精简
  9045. }
  9046. for (var band = 0; band < 32; band++) {
  9047. var fc1, fc2;
  9048. var freq = band / 31.0;
  9049. if (gfc.highpass2 > gfc.highpass1) {
  9050. abort();//fix cc 精简
  9051. } else {
  9052. fc1 = 1.0;
  9053. }
  9054. if (gfc.lowpass2 > gfc.lowpass1) {
  9055. fc2 = filter_coef((freq - gfc.lowpass1)
  9056. / (gfc.lowpass2 - gfc.lowpass1 + 1e-20));
  9057. } else {
  9058. fc2 = 1.0;
  9059. }
  9060. gfc.amp_filter[band] = (fc1 * fc2);
  9061. }
  9062. }
  9063. function lame_init_qval(gfp) {
  9064. var gfc = gfp.internal_flags;
  9065. switch (gfp.quality) {
  9066. default:
  9067. case 9: /* no psymodel, no noise shaping */
  9068. gfc.psymodel = 0;
  9069. gfc.noise_shaping = 0;
  9070. gfc.noise_shaping_amp = 0;
  9071. gfc.noise_shaping_stop = 0;
  9072. gfc.use_best_huffman = 0;
  9073. gfc.full_outer_loop = 0;
  9074. break;
  9075. case 8:
  9076. gfp.quality = 7;
  9077. //$FALL-THROUGH$
  9078. case 7:
  9079. /*
  9080. * use psymodel (for short block and m/s switching), but no noise
  9081. * shapping
  9082. */
  9083. gfc.psymodel = 1;
  9084. gfc.noise_shaping = 0;
  9085. gfc.noise_shaping_amp = 0;
  9086. gfc.noise_shaping_stop = 0;
  9087. gfc.use_best_huffman = 0;
  9088. gfc.full_outer_loop = 0;
  9089. break;
  9090. case 6:
  9091. gfc.psymodel = 1;
  9092. if (gfc.noise_shaping == 0)
  9093. gfc.noise_shaping = 1;
  9094. gfc.noise_shaping_amp = 0;
  9095. gfc.noise_shaping_stop = 0;
  9096. if (gfc.subblock_gain == -1)
  9097. gfc.subblock_gain = 1;
  9098. gfc.use_best_huffman = 0;
  9099. gfc.full_outer_loop = 0;
  9100. break;
  9101. case 5:
  9102. gfc.psymodel = 1;
  9103. if (gfc.noise_shaping == 0)
  9104. gfc.noise_shaping = 1;
  9105. gfc.noise_shaping_amp = 0;
  9106. gfc.noise_shaping_stop = 0;
  9107. if (gfc.subblock_gain == -1)
  9108. gfc.subblock_gain = 1;
  9109. gfc.use_best_huffman = 0;
  9110. gfc.full_outer_loop = 0;
  9111. break;
  9112. case 4:
  9113. gfc.psymodel = 1;
  9114. if (gfc.noise_shaping == 0)
  9115. gfc.noise_shaping = 1;
  9116. gfc.noise_shaping_amp = 0;
  9117. gfc.noise_shaping_stop = 0;
  9118. if (gfc.subblock_gain == -1)
  9119. gfc.subblock_gain = 1;
  9120. gfc.use_best_huffman = 1;
  9121. gfc.full_outer_loop = 0;
  9122. break;
  9123. case 3:
  9124. gfc.psymodel = 1;
  9125. if (gfc.noise_shaping == 0)
  9126. gfc.noise_shaping = 1;
  9127. gfc.noise_shaping_amp = 1;
  9128. gfc.noise_shaping_stop = 1;
  9129. if (gfc.subblock_gain == -1)
  9130. gfc.subblock_gain = 1;
  9131. gfc.use_best_huffman = 1;
  9132. gfc.full_outer_loop = 0;
  9133. break;
  9134. case 2:
  9135. gfc.psymodel = 1;
  9136. if (gfc.noise_shaping == 0)
  9137. gfc.noise_shaping = 1;
  9138. if (gfc.substep_shaping == 0)
  9139. gfc.substep_shaping = 2;
  9140. gfc.noise_shaping_amp = 1;
  9141. gfc.noise_shaping_stop = 1;
  9142. if (gfc.subblock_gain == -1)
  9143. gfc.subblock_gain = 1;
  9144. gfc.use_best_huffman = 1;
  9145. /* inner loop */
  9146. gfc.full_outer_loop = 0;
  9147. break;
  9148. case 1:
  9149. gfc.psymodel = 1;
  9150. if (gfc.noise_shaping == 0)
  9151. gfc.noise_shaping = 1;
  9152. if (gfc.substep_shaping == 0)
  9153. gfc.substep_shaping = 2;
  9154. gfc.noise_shaping_amp = 2;
  9155. gfc.noise_shaping_stop = 1;
  9156. if (gfc.subblock_gain == -1)
  9157. gfc.subblock_gain = 1;
  9158. gfc.use_best_huffman = 1;
  9159. gfc.full_outer_loop = 0;
  9160. break;
  9161. case 0:
  9162. gfc.psymodel = 1;
  9163. if (gfc.noise_shaping == 0)
  9164. gfc.noise_shaping = 1;
  9165. if (gfc.substep_shaping == 0)
  9166. gfc.substep_shaping = 2;
  9167. gfc.noise_shaping_amp = 2;
  9168. gfc.noise_shaping_stop = 1;
  9169. if (gfc.subblock_gain == -1)
  9170. gfc.subblock_gain = 1;
  9171. gfc.use_best_huffman = 1;
  9172. /*
  9173. * type 2 disabled because of it slowness, in favor of full outer
  9174. * loop search
  9175. */
  9176. gfc.full_outer_loop = 0;
  9177. /*
  9178. * full outer loop search disabled because of audible distortions it
  9179. * may generate rh 060629
  9180. */
  9181. break;
  9182. }
  9183. }
  9184. function lame_init_bitstream(gfp) {
  9185. var gfc = gfp.internal_flags;
  9186. gfp.frameNum = 0;
  9187. if (gfp.write_id3tag_automatic) {
  9188. id3.id3tag_write_v2(gfp);
  9189. }
  9190. /* initialize histogram data optionally used by frontend */
  9191. gfc.bitrate_stereoMode_Hist = new_int_n([16, 4 + 1]);
  9192. gfc.bitrate_blockType_Hist = new_int_n([16, 4 + 1 + 1]);
  9193. gfc.PeakSample = 0.0;
  9194. /* Write initial VBR Header to bitstream and init VBR data */
  9195. if (gfp.bWriteVbrTag)
  9196. vbr.InitVbrTag(gfp);
  9197. }
  9198. /********************************************************************
  9199. * initialize internal params based on data in gf (globalflags struct filled
  9200. * in by calling program)
  9201. *
  9202. * OUTLINE:
  9203. *
  9204. * We first have some complex code to determine bitrate, output samplerate
  9205. * and mode. It is complicated by the fact that we allow the user to set
  9206. * some or all of these parameters, and need to determine best possible
  9207. * values for the rest of them:
  9208. *
  9209. * 1. set some CPU related flags 2. check if we are mono.mono, stereo.mono
  9210. * or stereo.stereo 3. compute bitrate and output samplerate: user may have
  9211. * set compression ratio user may have set a bitrate user may have set a
  9212. * output samplerate 4. set some options which depend on output samplerate
  9213. * 5. compute the actual compression ratio 6. set mode based on compression
  9214. * ratio
  9215. *
  9216. * The remaining code is much simpler - it just sets options based on the
  9217. * mode & compression ratio:
  9218. *
  9219. * set allow_diff_short based on mode select lowpass filter based on
  9220. * compression ratio & mode set the bitrate index, and min/max bitrates for
  9221. * VBR modes disable VBR tag if it is not appropriate initialize the
  9222. * bitstream initialize scalefac_band data set sideinfo_len (based on
  9223. * channels, CRC, out_samplerate) write an id3v2 tag into the bitstream
  9224. * write VBR tag into the bitstream set mpeg1/2 flag estimate the number of
  9225. * frames (based on a lot of data)
  9226. *
  9227. * now we set more flags: nspsytune: see code VBR modes see code CBR/ABR see
  9228. * code
  9229. *
  9230. * Finally, we set the algorithm flags based on the gfp.quality value
  9231. * lame_init_qval(gfp);
  9232. *
  9233. ********************************************************************/
  9234. this.lame_init_params = function (gfp) {
  9235. var gfc = gfp.internal_flags;
  9236. gfc.Class_ID = 0;
  9237. if (gfc.ATH == null)
  9238. gfc.ATH = new ATH();
  9239. if (gfc.PSY == null)
  9240. gfc.PSY = new PSY();
  9241. if (gfc.rgdata == null)
  9242. gfc.rgdata = new ReplayGain();
  9243. gfc.channels_in = gfp.num_channels;
  9244. if (gfc.channels_in == 1)
  9245. gfp.mode = MPEGMode.MONO;
  9246. gfc.channels_out = (gfp.mode == MPEGMode.MONO) ? 1 : 2;
  9247. gfc.mode_ext = Encoder.MPG_MD_MS_LR;
  9248. if (gfp.mode == MPEGMode.MONO)
  9249. gfp.force_ms = false;
  9250. /*
  9251. * don't allow forced mid/side stereo for mono output
  9252. */
  9253. if (gfp.VBR == VbrMode.vbr_off && gfp.VBR_mean_bitrate_kbps != 128
  9254. && gfp.brate == 0)
  9255. gfp.brate = gfp.VBR_mean_bitrate_kbps;
  9256. if (gfp.VBR == VbrMode.vbr_off || gfp.VBR == VbrMode.vbr_mtrh
  9257. || gfp.VBR == VbrMode.vbr_mt) {
  9258. /* these modes can handle free format condition */
  9259. } else {
  9260. gfp.free_format = false;
  9261. /* mode can't be mixed with free format */
  9262. }
  9263. if (gfp.VBR == VbrMode.vbr_off && gfp.brate == 0) {
  9264. abort();//fix cc 精简
  9265. }
  9266. /* find bitrate if user specify a compression ratio */
  9267. if (gfp.VBR == VbrMode.vbr_off && gfp.compression_ratio > 0) {
  9268. abort();//fix cc 精简
  9269. }
  9270. if (gfp.out_samplerate != 0) {
  9271. if (gfp.out_samplerate < 16000) {
  9272. gfp.VBR_mean_bitrate_kbps = Math.max(gfp.VBR_mean_bitrate_kbps,
  9273. 8);
  9274. gfp.VBR_mean_bitrate_kbps = Math.min(gfp.VBR_mean_bitrate_kbps,
  9275. 64);
  9276. } else if (gfp.out_samplerate < 32000) {
  9277. gfp.VBR_mean_bitrate_kbps = Math.max(gfp.VBR_mean_bitrate_kbps,
  9278. 8);
  9279. gfp.VBR_mean_bitrate_kbps = Math.min(gfp.VBR_mean_bitrate_kbps,
  9280. 160);
  9281. } else {
  9282. gfp.VBR_mean_bitrate_kbps = Math.max(gfp.VBR_mean_bitrate_kbps,
  9283. 32);
  9284. gfp.VBR_mean_bitrate_kbps = Math.min(gfp.VBR_mean_bitrate_kbps,
  9285. 320);
  9286. }
  9287. }
  9288. /****************************************************************/
  9289. /* if a filter has not been enabled, see if we should add one: */
  9290. /****************************************************************/
  9291. if (gfp.lowpassfreq == 0) {
  9292. var lowpass = 16000.;
  9293. switch (gfp.VBR) {
  9294. case VbrMode.vbr_off:
  9295. {
  9296. var lh = new LowPassHighPass();
  9297. optimum_bandwidth(lh, gfp.brate);
  9298. lowpass = lh.lowerlimit;
  9299. break;
  9300. }
  9301. case VbrMode.vbr_abr:
  9302. {
  9303. var lh = new LowPassHighPass();
  9304. optimum_bandwidth(lh, gfp.VBR_mean_bitrate_kbps);
  9305. lowpass = lh.lowerlimit;
  9306. break;
  9307. }
  9308. case VbrMode.vbr_rh:
  9309. {
  9310. abort();//fix cc 精简
  9311. }
  9312. default:
  9313. {
  9314. abort();//fix cc 精简
  9315. }
  9316. }
  9317. if (gfp.mode == MPEGMode.MONO
  9318. && (gfp.VBR == VbrMode.vbr_off || gfp.VBR == VbrMode.vbr_abr))
  9319. lowpass *= 1.5;
  9320. gfp.lowpassfreq = lowpass | 0;
  9321. }
  9322. if (gfp.out_samplerate == 0) {
  9323. abort();//fix cc 精简
  9324. }
  9325. gfp.lowpassfreq = Math.min(20500, gfp.lowpassfreq);
  9326. gfp.lowpassfreq = Math.min(gfp.out_samplerate / 2, gfp.lowpassfreq);
  9327. if (gfp.VBR == VbrMode.vbr_off) {
  9328. gfp.compression_ratio = gfp.out_samplerate * 16 * gfc.channels_out
  9329. / (1.e3 * gfp.brate);
  9330. }
  9331. if (gfp.VBR == VbrMode.vbr_abr) {
  9332. abort();//fix cc 精简
  9333. }
  9334. /*
  9335. * do not compute ReplayGain values and do not find the peak sample if
  9336. * we can't store them
  9337. */
  9338. if (!gfp.bWriteVbrTag) {
  9339. gfp.findReplayGain = false;
  9340. gfp.decode_on_the_fly = false;
  9341. gfc.findPeakSample = false;
  9342. }
  9343. gfc.findReplayGain = gfp.findReplayGain;
  9344. gfc.decode_on_the_fly = gfp.decode_on_the_fly;
  9345. if (gfc.decode_on_the_fly)
  9346. gfc.findPeakSample = true;
  9347. if (gfc.findReplayGain) {
  9348. abort();//fix cc 精简
  9349. }
  9350. if (gfc.decode_on_the_fly && !gfp.decode_only) {
  9351. abort();//fix cc 精简
  9352. }
  9353. gfc.mode_gr = gfp.out_samplerate <= 24000 ? 1 : 2;
  9354. /*
  9355. * Number of granules per frame
  9356. */
  9357. gfp.framesize = 576 * gfc.mode_gr;
  9358. gfp.encoder_delay = Encoder.ENCDELAY;
  9359. gfc.resample_ratio = gfp.in_samplerate / gfp.out_samplerate;
  9360. /**
  9361. * <PRE>
  9362. * sample freq bitrate compression ratio
  9363. * [kHz] [kbps/channel] for 16 bit input
  9364. * 44.1 56 12.6
  9365. * 44.1 64 11.025
  9366. * 44.1 80 8.82
  9367. * 22.05 24 14.7
  9368. * 22.05 32 11.025
  9369. * 22.05 40 8.82
  9370. * 16 16 16.0
  9371. * 16 24 10.667
  9372. * </PRE>
  9373. */
  9374. /**
  9375. * <PRE>
  9376. * For VBR, take a guess at the compression_ratio.
  9377. * For example:
  9378. *
  9379. * VBR_q compression like
  9380. * - 4.4 320 kbps/44 kHz
  9381. * 0...1 5.5 256 kbps/44 kHz
  9382. * 2 7.3 192 kbps/44 kHz
  9383. * 4 8.8 160 kbps/44 kHz
  9384. * 6 11 128 kbps/44 kHz
  9385. * 9 14.7 96 kbps
  9386. *
  9387. * for lower bitrates, downsample with --resample
  9388. * </PRE>
  9389. */
  9390. switch (gfp.VBR) {
  9391. case VbrMode.vbr_mt:
  9392. case VbrMode.vbr_rh:
  9393. case VbrMode.vbr_mtrh:
  9394. {
  9395. /* numbers are a bit strange, but they determine the lowpass value */
  9396. var cmp = [5.7, 6.5, 7.3, 8.2, 10, 11.9, 13, 14,
  9397. 15, 16.5];
  9398. gfp.compression_ratio = cmp[gfp.VBR_q];
  9399. }
  9400. break;
  9401. case VbrMode.vbr_abr:
  9402. gfp.compression_ratio = gfp.out_samplerate * 16 * gfc.channels_out
  9403. / (1.e3 * gfp.VBR_mean_bitrate_kbps);
  9404. break;
  9405. default:
  9406. gfp.compression_ratio = gfp.out_samplerate * 16 * gfc.channels_out
  9407. / (1.e3 * gfp.brate);
  9408. break;
  9409. }
  9410. /*
  9411. * mode = -1 (not set by user) or mode = MONO (because of only 1 input
  9412. * channel). If mode has not been set, then select J-STEREO
  9413. */
  9414. if (gfp.mode == MPEGMode.NOT_SET) {
  9415. gfp.mode = MPEGMode.JOINT_STEREO;
  9416. }
  9417. /* apply user driven high pass filter */
  9418. if (gfp.highpassfreq > 0) {
  9419. abort();//fix cc 精简
  9420. } else {
  9421. gfc.highpass1 = 0;
  9422. gfc.highpass2 = 0;
  9423. }
  9424. /* apply user driven low pass filter */
  9425. if (gfp.lowpassfreq > 0) {
  9426. gfc.lowpass2 = 2. * gfp.lowpassfreq;
  9427. if (gfp.lowpasswidth >= 0) {
  9428. abort();//fix cc 精简
  9429. } else { /* 0% below on default */
  9430. gfc.lowpass1 = (1 - 0.00) * 2. * gfp.lowpassfreq;
  9431. }
  9432. gfc.lowpass1 /= gfp.out_samplerate;
  9433. gfc.lowpass2 /= gfp.out_samplerate;
  9434. } else {
  9435. abort();//fix cc 精简
  9436. }
  9437. /**********************************************************************/
  9438. /* compute info needed for polyphase filter (filter type==0, default) */
  9439. /**********************************************************************/
  9440. lame_init_params_ppflt(gfp);
  9441. /*******************************************************
  9442. * samplerate and bitrate index
  9443. *******************************************************/
  9444. gfc.samplerate_index = SmpFrqIndex(gfp.out_samplerate, gfp);
  9445. if (gfc.samplerate_index < 0) {
  9446. abort();//fix cc 精简
  9447. }
  9448. if (gfp.VBR == VbrMode.vbr_off) {
  9449. if (gfp.free_format) {
  9450. gfc.bitrate_index = 0;
  9451. } else {
  9452. gfp.brate = FindNearestBitrate(gfp.brate, gfp.version,
  9453. gfp.out_samplerate);
  9454. gfc.bitrate_index = BitrateIndex(gfp.brate, gfp.version,
  9455. gfp.out_samplerate);
  9456. if (gfc.bitrate_index <= 0) {
  9457. abort();//fix cc 精简
  9458. }
  9459. }
  9460. } else {
  9461. gfc.bitrate_index = 1;
  9462. }
  9463. /* for CBR, we will write an "info" tag. */
  9464. if (gfp.analysis)
  9465. gfp.bWriteVbrTag = false;
  9466. /* some file options not allowed if output is: not specified or stdout */
  9467. if (gfc.pinfo != null)
  9468. gfp.bWriteVbrTag = false;
  9469. /* disable Xing VBR tag */
  9470. bs.init_bit_stream_w(gfc);
  9471. var j = gfc.samplerate_index + (3 * gfp.version) + 6
  9472. * (gfp.out_samplerate < 16000 ? 1 : 0);
  9473. for (var i = 0; i < Encoder.SBMAX_l + 1; i++)
  9474. gfc.scalefac_band.l[i] = qupvt.sfBandIndex[j].l[i];
  9475. for (var i = 0; i < Encoder.PSFB21 + 1; i++) {
  9476. var size = (gfc.scalefac_band.l[22] - gfc.scalefac_band.l[21])
  9477. / Encoder.PSFB21;
  9478. var start = gfc.scalefac_band.l[21] + i * size;
  9479. gfc.scalefac_band.psfb21[i] = start;
  9480. }
  9481. gfc.scalefac_band.psfb21[Encoder.PSFB21] = 576;
  9482. for (var i = 0; i < Encoder.SBMAX_s + 1; i++)
  9483. gfc.scalefac_band.s[i] = qupvt.sfBandIndex[j].s[i];
  9484. for (var i = 0; i < Encoder.PSFB12 + 1; i++) {
  9485. var size = (gfc.scalefac_band.s[13] - gfc.scalefac_band.s[12])
  9486. / Encoder.PSFB12;
  9487. var start = gfc.scalefac_band.s[12] + i * size;
  9488. gfc.scalefac_band.psfb12[i] = start;
  9489. }
  9490. gfc.scalefac_band.psfb12[Encoder.PSFB12] = 192;
  9491. /* determine the mean bitrate for main data */
  9492. if (gfp.version == 1) /* MPEG 1 */
  9493. gfc.sideinfo_len = (gfc.channels_out == 1) ? 4 + 17 : 4 + 32;
  9494. else
  9495. /* MPEG 2 */
  9496. gfc.sideinfo_len = (gfc.channels_out == 1) ? 4 + 9 : 4 + 17;
  9497. if (gfp.error_protection)
  9498. gfc.sideinfo_len += 2;
  9499. lame_init_bitstream(gfp);
  9500. gfc.Class_ID = LAME_ID;
  9501. {
  9502. var k;
  9503. for (k = 0; k < 19; k++)
  9504. gfc.nsPsy.pefirbuf[k] = 700 * gfc.mode_gr * gfc.channels_out;
  9505. if (gfp.ATHtype == -1)
  9506. gfp.ATHtype = 4;
  9507. }
  9508. switch (gfp.VBR) {
  9509. case VbrMode.vbr_mt:
  9510. gfp.VBR = VbrMode.vbr_mtrh;
  9511. //$FALL-THROUGH$
  9512. case VbrMode.vbr_mtrh:
  9513. {
  9514. if (gfp.useTemporal == null) {
  9515. gfp.useTemporal = false;
  9516. /* off by default for this VBR mode */
  9517. }
  9518. p.apply_preset(gfp, 500 - (gfp.VBR_q * 10), 0);
  9519. /**
  9520. * <PRE>
  9521. * The newer VBR code supports only a limited
  9522. * subset of quality levels:
  9523. * 9-5=5 are the same, uses x^3/4 quantization
  9524. * 4-0=0 are the same 5 plus best huffman divide code
  9525. * </PRE>
  9526. */
  9527. if (gfp.quality < 0)
  9528. gfp.quality = LAME_DEFAULT_QUALITY;
  9529. if (gfp.quality < 5)
  9530. gfp.quality = 0;
  9531. if (gfp.quality > 5)
  9532. gfp.quality = 5;
  9533. gfc.PSY.mask_adjust = gfp.maskingadjust;
  9534. gfc.PSY.mask_adjust_short = gfp.maskingadjust_short;
  9535. /*
  9536. * sfb21 extra only with MPEG-1 at higher sampling rates
  9537. */
  9538. if (gfp.experimentalY)
  9539. gfc.sfb21_extra = false;
  9540. else
  9541. gfc.sfb21_extra = (gfp.out_samplerate > 44000);
  9542. gfc.iteration_loop = new VBRNewIterationLoop(qu);
  9543. break;
  9544. }
  9545. case VbrMode.vbr_rh:
  9546. {
  9547. p.apply_preset(gfp, 500 - (gfp.VBR_q * 10), 0);
  9548. gfc.PSY.mask_adjust = gfp.maskingadjust;
  9549. gfc.PSY.mask_adjust_short = gfp.maskingadjust_short;
  9550. /*
  9551. * sfb21 extra only with MPEG-1 at higher sampling rates
  9552. */
  9553. if (gfp.experimentalY)
  9554. gfc.sfb21_extra = false;
  9555. else
  9556. gfc.sfb21_extra = (gfp.out_samplerate > 44000);
  9557. /*
  9558. * VBR needs at least the output of GPSYCHO, so we have to garantee
  9559. * that by setting a minimum quality level, actually level 6 does
  9560. * it. down to level 6
  9561. */
  9562. if (gfp.quality > 6)
  9563. gfp.quality = 6;
  9564. if (gfp.quality < 0)
  9565. gfp.quality = LAME_DEFAULT_QUALITY;
  9566. gfc.iteration_loop = new VBROldIterationLoop(qu);
  9567. break;
  9568. }
  9569. default: /* cbr/abr */
  9570. {
  9571. var vbrmode;
  9572. /*
  9573. * no sfb21 extra with CBR code
  9574. */
  9575. gfc.sfb21_extra = false;
  9576. if (gfp.quality < 0)
  9577. gfp.quality = LAME_DEFAULT_QUALITY;
  9578. vbrmode = gfp.VBR;
  9579. if (vbrmode == VbrMode.vbr_off)
  9580. gfp.VBR_mean_bitrate_kbps = gfp.brate;
  9581. /* second, set parameters depending on bitrate */
  9582. p.apply_preset(gfp, gfp.VBR_mean_bitrate_kbps, 0);
  9583. gfp.VBR = vbrmode;
  9584. gfc.PSY.mask_adjust = gfp.maskingadjust;
  9585. gfc.PSY.mask_adjust_short = gfp.maskingadjust_short;
  9586. if (vbrmode == VbrMode.vbr_off) {
  9587. gfc.iteration_loop = new CBRNewIterationLoop(qu);
  9588. } else {
  9589. abort();//fix cc 精简
  9590. }
  9591. break;
  9592. }
  9593. }
  9594. /* initialize default values common for all modes */
  9595. if (gfp.VBR != VbrMode.vbr_off) { /* choose a min/max bitrate for VBR */
  9596. abort();//fix cc 精简
  9597. }
  9598. /* just another daily changing developer switch */
  9599. if (gfp.tune) {
  9600. abort();//fix cc 精简
  9601. }
  9602. /* initialize internal qval settings */
  9603. lame_init_qval(gfp);
  9604. /*
  9605. * automatic ATH adjustment on
  9606. */
  9607. if (gfp.athaa_type < 0)
  9608. gfc.ATH.useAdjust = 3;
  9609. else
  9610. gfc.ATH.useAdjust = gfp.athaa_type;
  9611. /* initialize internal adaptive ATH settings -jd */
  9612. gfc.ATH.aaSensitivityP = Math.pow(10.0, gfp.athaa_sensitivity
  9613. / -10.0);
  9614. if (gfp.short_blocks == null) {
  9615. gfp.short_blocks = ShortBlock.short_block_allowed;
  9616. }
  9617. /*
  9618. * Note Jan/2003: Many hardware decoders cannot handle short blocks in
  9619. * regular stereo mode unless they are coupled (same type in both
  9620. * channels) it is a rare event (1 frame per min. or so) that LAME would
  9621. * use uncoupled short blocks, so lets turn them off until we decide how
  9622. * to handle this. No other encoders allow uncoupled short blocks, even
  9623. * though it is in the standard.
  9624. */
  9625. /*
  9626. * rh 20040217: coupling makes no sense for mono and dual-mono streams
  9627. */
  9628. if (gfp.short_blocks == ShortBlock.short_block_allowed
  9629. && (gfp.mode == MPEGMode.JOINT_STEREO || gfp.mode == MPEGMode.STEREO)) {
  9630. gfp.short_blocks = ShortBlock.short_block_coupled;
  9631. }
  9632. if (gfp.quant_comp < 0)
  9633. gfp.quant_comp = 1;
  9634. if (gfp.quant_comp_short < 0)
  9635. gfp.quant_comp_short = 0;
  9636. if (gfp.msfix < 0)
  9637. gfp.msfix = 0;
  9638. /* select psychoacoustic model */
  9639. gfp.exp_nspsytune = gfp.exp_nspsytune | 1;
  9640. if (gfp.internal_flags.nsPsy.attackthre < 0)
  9641. gfp.internal_flags.nsPsy.attackthre = PsyModel.NSATTACKTHRE;
  9642. if (gfp.internal_flags.nsPsy.attackthre_s < 0)
  9643. gfp.internal_flags.nsPsy.attackthre_s = PsyModel.NSATTACKTHRE_S;
  9644. if (gfp.scale < 0)
  9645. gfp.scale = 1;
  9646. if (gfp.ATHtype < 0)
  9647. gfp.ATHtype = 4;
  9648. if (gfp.ATHcurve < 0)
  9649. gfp.ATHcurve = 4;
  9650. if (gfp.athaa_loudapprox < 0)
  9651. gfp.athaa_loudapprox = 2;
  9652. if (gfp.interChRatio < 0)
  9653. gfp.interChRatio = 0;
  9654. if (gfp.useTemporal == null)
  9655. gfp.useTemporal = true;
  9656. /* on by default */
  9657. /*
  9658. * padding method as described in
  9659. * "MPEG-Layer3 / Bitstream Syntax and Decoding" by Martin Sieler, Ralph
  9660. * Sperschneider
  9661. *
  9662. * note: there is no padding for the very first frame
  9663. *
  9664. * Robert Hegemann 2000-06-22
  9665. */
  9666. gfc.slot_lag = gfc.frac_SpF = 0;
  9667. if (gfp.VBR == VbrMode.vbr_off)
  9668. gfc.slot_lag = gfc.frac_SpF = (((gfp.version + 1) * 72000 * gfp.brate) % gfp.out_samplerate) | 0;
  9669. qupvt.iteration_init(gfp);
  9670. psy.psymodel_init(gfp);
  9671. return 0;
  9672. }
  9673. function update_inbuffer_size(gfc, nsamples) {
  9674. if (gfc.in_buffer_0 == null || gfc.in_buffer_nsamples < nsamples) {
  9675. gfc.in_buffer_0 = new_float(nsamples);
  9676. gfc.in_buffer_1 = new_float(nsamples);
  9677. gfc.in_buffer_nsamples = nsamples;
  9678. }
  9679. }
  9680. this.lame_encode_flush = function (gfp, mp3buffer, mp3bufferPos, mp3buffer_size) {
  9681. var gfc = gfp.internal_flags;
  9682. var buffer = new_short_n([2, 1152]);
  9683. var imp3 = 0, mp3count, mp3buffer_size_remaining;
  9684. /*
  9685. * we always add POSTDELAY=288 padding to make sure granule with real
  9686. * data can be complety decoded (because of 50% overlap with next
  9687. * granule
  9688. */
  9689. var end_padding;
  9690. var frames_left;
  9691. var samples_to_encode = gfc.mf_samples_to_encode - Encoder.POSTDELAY;
  9692. var mf_needed = calcNeeded(gfp);
  9693. /* Was flush already called? */
  9694. if (gfc.mf_samples_to_encode < 1) {
  9695. return 0;
  9696. }
  9697. mp3count = 0;
  9698. if (gfp.in_samplerate != gfp.out_samplerate) {
  9699. abort();//fix cc 精简
  9700. }
  9701. end_padding = gfp.framesize - (samples_to_encode % gfp.framesize);
  9702. if (end_padding < 576)
  9703. end_padding += gfp.framesize;
  9704. gfp.encoder_padding = end_padding;
  9705. frames_left = (samples_to_encode + end_padding) / gfp.framesize;
  9706. /*
  9707. * send in a frame of 0 padding until all internal sample buffers are
  9708. * flushed
  9709. */
  9710. while (frames_left > 0 && imp3 >= 0) {
  9711. var bunch = mf_needed - gfc.mf_size;
  9712. var frame_num = gfp.frameNum;
  9713. bunch *= gfp.in_samplerate;
  9714. bunch /= gfp.out_samplerate;
  9715. if (bunch > 1152)
  9716. bunch = 1152;
  9717. if (bunch < 1)
  9718. bunch = 1;
  9719. mp3buffer_size_remaining = mp3buffer_size - mp3count;
  9720. /* if user specifed buffer size = 0, dont check size */
  9721. if (mp3buffer_size == 0)
  9722. mp3buffer_size_remaining = 0;
  9723. imp3 = this.lame_encode_buffer(gfp, buffer[0], buffer[1], bunch,
  9724. mp3buffer, mp3bufferPos, mp3buffer_size_remaining);
  9725. mp3bufferPos += imp3;
  9726. mp3count += imp3;
  9727. frames_left -= (frame_num != gfp.frameNum) ? 1 : 0;
  9728. }
  9729. /*
  9730. * Set gfc.mf_samples_to_encode to 0, so we may detect and break loops
  9731. * calling it more than once in a row.
  9732. */
  9733. gfc.mf_samples_to_encode = 0;
  9734. if (imp3 < 0) {
  9735. /* some type of fatal error */
  9736. return imp3;
  9737. }
  9738. mp3buffer_size_remaining = mp3buffer_size - mp3count;
  9739. /* if user specifed buffer size = 0, dont check size */
  9740. if (mp3buffer_size == 0)
  9741. mp3buffer_size_remaining = 0;
  9742. /* mp3 related stuff. bit buffer might still contain some mp3 data */
  9743. bs.flush_bitstream(gfp);
  9744. imp3 = bs.copy_buffer(gfc, mp3buffer, mp3bufferPos,
  9745. mp3buffer_size_remaining, 1);
  9746. if (imp3 < 0) {
  9747. /* some type of fatal error */
  9748. return imp3;
  9749. }
  9750. mp3bufferPos += imp3;
  9751. mp3count += imp3;
  9752. mp3buffer_size_remaining = mp3buffer_size - mp3count;
  9753. /* if user specifed buffer size = 0, dont check size */
  9754. if (mp3buffer_size == 0)
  9755. mp3buffer_size_remaining = 0;
  9756. if (gfp.write_id3tag_automatic) {
  9757. abort();//fix cc 精简
  9758. }
  9759. return mp3count;
  9760. };
  9761. this.lame_encode_buffer = function (gfp, buffer_l, buffer_r, nsamples, mp3buf, mp3bufPos, mp3buf_size) {
  9762. var gfc = gfp.internal_flags;
  9763. var in_buffer = [null, null];
  9764. if (gfc.Class_ID != LAME_ID)
  9765. return -3;
  9766. if (nsamples == 0)
  9767. return 0;
  9768. update_inbuffer_size(gfc, nsamples);
  9769. in_buffer[0] = gfc.in_buffer_0;
  9770. in_buffer[1] = gfc.in_buffer_1;
  9771. /* make a copy of input buffer, changing type to sample_t */
  9772. for (var i = 0; i < nsamples; i++) {
  9773. in_buffer[0][i] = buffer_l[i];
  9774. if (gfc.channels_in > 1)
  9775. in_buffer[1][i] = buffer_r[i];
  9776. }
  9777. return lame_encode_buffer_sample(gfp, in_buffer[0], in_buffer[1],
  9778. nsamples, mp3buf, mp3bufPos, mp3buf_size);
  9779. }
  9780. function calcNeeded(gfp) {
  9781. var mf_needed = Encoder.BLKSIZE + gfp.framesize - Encoder.FFTOFFSET;
  9782. /*
  9783. * amount needed for FFT
  9784. */
  9785. mf_needed = Math.max(mf_needed, 512 + gfp.framesize - 32);
  9786. return mf_needed;
  9787. }
  9788. function lame_encode_buffer_sample(gfp, buffer_l, buffer_r, nsamples, mp3buf, mp3bufPos, mp3buf_size) {
  9789. var gfc = gfp.internal_flags;
  9790. var mp3size = 0, ret, i, ch, mf_needed;
  9791. var mp3out;
  9792. var mfbuf = [null, null];
  9793. var in_buffer = [null, null];
  9794. if (gfc.Class_ID != LAME_ID)
  9795. return -3;
  9796. if (nsamples == 0)
  9797. return 0;
  9798. /* copy out any tags that may have been written into bitstream */
  9799. mp3out = bs.copy_buffer(gfc, mp3buf, mp3bufPos, mp3buf_size, 0);
  9800. if (mp3out < 0)
  9801. return mp3out;
  9802. /* not enough buffer space */
  9803. mp3bufPos += mp3out;
  9804. mp3size += mp3out;
  9805. in_buffer[0] = buffer_l;
  9806. in_buffer[1] = buffer_r;
  9807. /* Apply user defined re-scaling */
  9808. /* user selected scaling of the samples */
  9809. if (BitStream.NEQ(gfp.scale, 0) && BitStream.NEQ(gfp.scale, 1.0)) {
  9810. for (i = 0; i < nsamples; ++i) {
  9811. in_buffer[0][i] *= gfp.scale;
  9812. if (gfc.channels_out == 2)
  9813. in_buffer[1][i] *= gfp.scale;
  9814. }
  9815. }
  9816. /* user selected scaling of the channel 0 (left) samples */
  9817. if (BitStream.NEQ(gfp.scale_left, 0)
  9818. && BitStream.NEQ(gfp.scale_left, 1.0)) {
  9819. for (i = 0; i < nsamples; ++i) {
  9820. in_buffer[0][i] *= gfp.scale_left;
  9821. }
  9822. }
  9823. /* user selected scaling of the channel 1 (right) samples */
  9824. if (BitStream.NEQ(gfp.scale_right, 0)
  9825. && BitStream.NEQ(gfp.scale_right, 1.0)) {
  9826. for (i = 0; i < nsamples; ++i) {
  9827. in_buffer[1][i] *= gfp.scale_right;
  9828. }
  9829. }
  9830. /* Downsample to Mono if 2 channels in and 1 channel out */
  9831. if (gfp.num_channels == 2 && gfc.channels_out == 1) {
  9832. abort();//fix cc 精简 stereo
  9833. }
  9834. mf_needed = calcNeeded(gfp);
  9835. mfbuf[0] = gfc.mfbuf[0];
  9836. mfbuf[1] = gfc.mfbuf[1];
  9837. var in_bufferPos = 0;
  9838. while (nsamples > 0) {
  9839. var in_buffer_ptr = [null, null];
  9840. var n_in = 0;
  9841. /* number of input samples processed with fill_buffer */
  9842. var n_out = 0;
  9843. /* number of samples output with fill_buffer */
  9844. /* n_in <> n_out if we are resampling */
  9845. in_buffer_ptr[0] = in_buffer[0];
  9846. in_buffer_ptr[1] = in_buffer[1];
  9847. /* copy in new samples into mfbuf, with resampling */
  9848. var inOut = new InOut();
  9849. fill_buffer(gfp, mfbuf, in_buffer_ptr, in_bufferPos, nsamples,
  9850. inOut);
  9851. n_in = inOut.n_in;
  9852. n_out = inOut.n_out;
  9853. /* compute ReplayGain of resampled input if requested */
  9854. if (gfc.findReplayGain && !gfc.decode_on_the_fly)
  9855. if (ga.AnalyzeSamples(gfc.rgdata, mfbuf[0], gfc.mf_size,
  9856. mfbuf[1], gfc.mf_size, n_out, gfc.channels_out) == GainAnalysis.GAIN_ANALYSIS_ERROR)
  9857. return -6;
  9858. /* update in_buffer counters */
  9859. nsamples -= n_in;
  9860. in_bufferPos += n_in;
  9861. if (gfc.channels_out == 2)
  9862. ;// in_bufferPos += n_in;
  9863. /* update mfbuf[] counters */
  9864. gfc.mf_size += n_out;
  9865. /*
  9866. * lame_encode_flush may have set gfc.mf_sample_to_encode to 0 so we
  9867. * have to reinitialize it here when that happened.
  9868. */
  9869. if (gfc.mf_samples_to_encode < 1) {
  9870. abort();//fix cc 精简
  9871. }
  9872. gfc.mf_samples_to_encode += n_out;
  9873. if (gfc.mf_size >= mf_needed) {
  9874. /* encode the frame. */
  9875. /* mp3buf = pointer to current location in buffer */
  9876. /* mp3buf_size = size of original mp3 output buffer */
  9877. /* = 0 if we should not worry about the */
  9878. /* buffer size because calling program is */
  9879. /* to lazy to compute it */
  9880. /* mp3size = size of data written to buffer so far */
  9881. /* mp3buf_size-mp3size = amount of space avalable */
  9882. var buf_size = mp3buf_size - mp3size;
  9883. if (mp3buf_size == 0)
  9884. buf_size = 0;
  9885. ret = lame_encode_frame(gfp, mfbuf[0], mfbuf[1], mp3buf,
  9886. mp3bufPos, buf_size);
  9887. if (ret < 0)
  9888. return ret;
  9889. mp3bufPos += ret;
  9890. mp3size += ret;
  9891. /* shift out old samples */
  9892. gfc.mf_size -= gfp.framesize;
  9893. gfc.mf_samples_to_encode -= gfp.framesize;
  9894. for (ch = 0; ch < gfc.channels_out; ch++)
  9895. for (i = 0; i < gfc.mf_size; i++)
  9896. mfbuf[ch][i] = mfbuf[ch][i + gfp.framesize];
  9897. }
  9898. }
  9899. return mp3size;
  9900. }
  9901. function lame_encode_frame(gfp, inbuf_l, inbuf_r, mp3buf, mp3bufPos, mp3buf_size) {
  9902. var ret = self.enc.lame_encode_mp3_frame(gfp, inbuf_l, inbuf_r, mp3buf,
  9903. mp3bufPos, mp3buf_size);
  9904. gfp.frameNum++;
  9905. return ret;
  9906. }
  9907. function InOut() {
  9908. this.n_in = 0;
  9909. this.n_out = 0;
  9910. }
  9911. //fix cc 精简
  9912. function fill_buffer(gfp, mfbuf, in_buffer, in_bufferPos, nsamples, io) {
  9913. var gfc = gfp.internal_flags;
  9914. /* copy in new samples into mfbuf, with resampling if necessary */
  9915. if ((gfc.resample_ratio < .9999) || (gfc.resample_ratio > 1.0001)) {
  9916. abort();//fix cc 精简
  9917. } else {
  9918. io.n_out = Math.min(gfp.framesize, nsamples);
  9919. io.n_in = io.n_out;
  9920. for (var i = 0; i < io.n_out; ++i) {
  9921. mfbuf[0][gfc.mf_size + i] = in_buffer[0][in_bufferPos + i];
  9922. if (gfc.channels_out == 2)
  9923. mfbuf[1][gfc.mf_size + i] = in_buffer[1][in_bufferPos + i];
  9924. }
  9925. }
  9926. }
  9927. }
  9928. function GetAudio() {
  9929. var parse;
  9930. var mpg;
  9931. this.setModules = function (parse2, mpg2) {
  9932. parse = parse2;
  9933. mpg = mpg2;
  9934. }
  9935. }
  9936. function Parse() {
  9937. var ver;
  9938. var id3;
  9939. var pre;
  9940. this.setModules = function (ver2, id32, pre2) {
  9941. ver = ver2;
  9942. id3 = id32;
  9943. pre = pre2;
  9944. }
  9945. }
  9946. function MPGLib() {
  9947. }
  9948. function ID3Tag() {
  9949. var bits;
  9950. var ver;
  9951. this.setModules = function (_bits, _ver) {
  9952. bits = _bits;
  9953. ver = _ver;
  9954. }
  9955. }
  9956. function Mp3Encoder(channels, samplerate, kbps) {
  9957. if (channels!=1) { //精简后的代码不支持双声道
  9958. abort("fix cc: only supports mono")
  9959. }
  9960. var lame = new Lame();
  9961. var gaud = new GetAudio();
  9962. var ga = new GainAnalysis();
  9963. var bs = new BitStream();
  9964. var p = new Presets();
  9965. var qupvt = new QuantizePVT();
  9966. var qu = new Quantize();
  9967. var vbr = new VBRTag();
  9968. var ver = new Version();
  9969. var id3 = new ID3Tag();
  9970. var rv = new Reservoir();
  9971. var tak = new Takehiro();
  9972. var parse = new Parse();
  9973. var mpg = new MPGLib();
  9974. lame.setModules(ga, bs, p, qupvt, qu, vbr, ver, id3, mpg);
  9975. bs.setModules(ga, mpg, ver, vbr);
  9976. id3.setModules(bs, ver);
  9977. p.setModules(lame);
  9978. qu.setModules(bs, rv, qupvt, tak);
  9979. qupvt.setModules(tak, rv, lame.enc.psy);
  9980. rv.setModules(bs);
  9981. tak.setModules(qupvt);
  9982. vbr.setModules(lame, bs, ver);
  9983. gaud.setModules(parse, mpg);
  9984. parse.setModules(ver, id3, p);
  9985. var gfp = lame.lame_init();
  9986. gfp.num_channels = channels;
  9987. gfp.in_samplerate = samplerate;
  9988. gfp.out_samplerate = samplerate;//fix by xiangyuecn 2018-12-6 01:48:12 64kbps以下可能无声音,手动控制输出码率
  9989. gfp.brate = kbps;
  9990. gfp.mode = MPEGMode.STEREO;
  9991. gfp.quality = 3;
  9992. gfp.bWriteVbrTag = false;
  9993. gfp.disable_reservoir = true;
  9994. gfp.write_id3tag_automatic = false;
  9995. var retcode = lame.lame_init_params(gfp);
  9996. var maxSamples = 1152;
  9997. var mp3buf_size = 0 | (1.25 * maxSamples + 7200);
  9998. var mp3buf = new_byte(mp3buf_size);
  9999. this.encodeBuffer = function (left, right) {
  10000. if (channels == 1) {
  10001. right = left;
  10002. }
  10003. if (left.length > maxSamples) {
  10004. maxSamples = left.length;
  10005. mp3buf_size = 0 | (1.25 * maxSamples + 7200);
  10006. mp3buf = new_byte(mp3buf_size);
  10007. }
  10008. var _sz = lame.lame_encode_buffer(gfp, left, right, left.length, mp3buf, 0, mp3buf_size);
  10009. return new Int8Array(mp3buf.subarray(0, _sz));
  10010. };
  10011. this.flush = function () {
  10012. var _sz = lame.lame_encode_flush(gfp, mp3buf, 0, mp3buf_size);
  10013. return new Int8Array(mp3buf.subarray(0, _sz));
  10014. };
  10015. }
  10016. //fix 精简
  10017. L3Side.SFBMAX = (Encoder.SBMAX_s * 3);
  10018. //testFullLength();
  10019. lamejs.Mp3Encoder = Mp3Encoder;
  10020. }
  10021. //fs=require('fs');
  10022. lamejs();
  10023. Recorder.lamejs=lamejs;
  10024. }));