|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824 |
- (provide 'snd-generators.scm)
- (if (provided? 'snd)
- (require snd-ws.scm)
- (require sndlib-ws.scm))
-
- ;;; it is dangerous to use a method within a generator's definition of that method --
- ;;; if the gen is used as the environment in with-let, the embedded call
- ;;; becomes a recursive call on that method. You either need to check the type
- ;;; of the method argument, or use #_method to override the name lookup, or use
- ;;; the explicit call style: (((gen 'embedded-gen) 'shared-method) ...)
-
- ;;; if gen has embedded gen, mus-copy needs a special copy method (see adjustable-oscil)
- ;;; a problem with a special copy method: if you change the generator, remember to change its copy method!
- ;;; also, I think (inlet e) is a way to copy e without accidentally invoking any 'copy method in e
-
-
- (define nearly-zero 1.0e-10) ; 1.0e-14 in clm.c, but that is trouble here (noddcos)
- (define two-pi (* 2.0 pi))
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; nssb (see nxycos) -- wouldn't a more consistent name be nxycos? but it already exists -- perhaps delete nssb?
-
- (defgenerator (nssb
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (ratio 1.0) (n 1) (angle 0.0) fm)
-
-
- (define nssb
-
- (let ((documentation "(make-nssb frequency (ratio 1.0) (n 1)) creates an nssb generator,
- similar to nxysin. (nssb gen (fm 0.0)) returns n sinusoids from frequency spaced by frequency * ratio."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((cx angle)
- (mx (* cx ratio))
- (den (sin (* 0.5 mx))))
- (set! angle (+ angle fm frequency))
- (if (< (abs den) nearly-zero)
- -1.0
- (/ (- (* (sin cx)
- (sin (* mx (/ (+ n 1) 2)))
- (sin (/ (* n mx) 2)))
- (* (cos cx)
- 0.5 (+ den (sin (* mx (+ n 0.5))))))
- (* (+ n 1) den))))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nssb 1000.0 0.1 3)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (nssb gen)))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nssb 1000.0 0.1 3))
- (vib (make-oscil 5.0))
- (ampf (make-env '(0 0 1 1 2 1 3 0) :length 20000 :scaler 1.0)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (* (env ampf)
- (nssb gen (* (hz->radians 100.0)
- (oscil vib))))))))
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; G&R first col rows 1&2
-
- (define (find-nxysin-max n ratio)
-
- (define (find-mid-max n lo hi)
- (define (ns x n)
- (let* ((a2 (/ x 2))
- (den (sin a2)))
- (if (= den 0.0)
- 0.0
- (/ (* (sin (* n a2)) (sin (* (+ 1 n) a2))) den))))
- (let ((mid (/ (+ lo hi) 2))
- (ylo (ns lo n))
- (yhi (ns hi n)))
- (if (< (abs (- ylo yhi)) nearly-zero) ; was e-100 but that hangs if not using doubles
- (ns mid n)
- (find-mid-max n (if (> ylo yhi)
- (values lo mid)
- (values mid hi))))))
-
- (define (find-nodds-mid-max n lo hi)
- (define (nodds x n)
- (let ((den (sin x))
- (num (sin (* n x))))
- (if (= den 0.0)
- 0.0
- (/ (* num num) den))))
- (let ((mid (/ (+ lo hi) 2))
- (ylo (nodds lo n))
- (yhi (nodds hi n)))
- (if (< (abs (- ylo yhi)) nearly-zero)
- (nodds mid n)
- (find-nodds-mid-max n (if (> ylo yhi)
- (values lo mid)
- (values mid hi))))))
-
- (if (= ratio 1)
- (find-mid-max n 0.0 (/ pi (+ n .5)))
- (if (= ratio 2)
- (find-nodds-mid-max n 0.0 (/ pi (+ (* 2 n) 0.5)))
- n)))
-
-
- (defgenerator (nxysin
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'norm) (/ 1.0 (find-nxysin-max (g 'n) (g 'ratio))))
- g))
- (frequency *clm-default-frequency*) (ratio 1.0) (n 1) (angle 0.0) fm
- (norm 1.0))
-
-
- (define nxysin
-
- (let ((documentation "(make-nxysin frequency (ratio 1.0) (n 1)) creates an nxysin
- generator. (nxysin gen (fm 0.0)) returns n sines from frequency spaced by frequency * ratio."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((x angle)
- (y (* x ratio))
- (den (sin (* y 0.5))))
- (set! angle (+ angle fm frequency))
- (if (< (abs den) nearly-zero)
- 0.0
- (/ (* (sin (+ x (* 0.5 (- n 1) y)))
- (sin (* 0.5 n y))
- norm)
- den)))))))
-
- ;;; if x (angle) is constant (an initial-phase offset for a sum of sines,
- ;;; the peak amp is nsin-max(n) + abs(sin(initial-phase))*(1 - nsin-max(n))
- ;;; that is, it varys sinusoidally from a sum-of-sines .7245 to a sum-of-cosines 1
- ;;; since we're treating "x" as the carrier (it's not a constant phase offset in this case)
- ;;; the output varies as x does, so we have a maxamp of n? There are special cases
- ;;; for low n and low integer ratio:
-
- ;;; ratio (4): (40): (400):
- ;;; 1: 3.23 29.34 290.1
- ;;; 2: 2.9404 28.97 289.7
- ;;; 3: 3.85 38.6 346.8
- ;;; 1.123: n
- ;;; .5: 3.55 30.0 290
-
- ;;; a ratio of 1 gives a sum of equal amplitude sines, so we could use nsin-max?
- ;;; 2 odd harmonics -- use noddsin?
- ;;; else use n (not so great for ratio: 3, but not way off)
- ;;; worst case right now is probably ratio .5
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nxysin 300 1/3 3)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (nxysin gen)))))
-
- ;;; here's the varying initial-phase case:
-
- (with-sound (:clipped #f)
- (let ((x 0.0)
- (ix (/ pi 1000))
- (n 100))
- (do ((i 0 (+ i 1)))
- ((= i 1000))
- (let ((pk 0.0)
- (phi x)
- (y 0.0)
- (iy (/ (* 2 pi) 10000)))
- (set! x (+ x ix))
- (do ((k 0 (+ k 1)))
- ((= k 10000))
- ;; x = phi
- (let ((den (sin (/ y 2))))
- (if (not (= den 0.0))
- (let ((sum (abs (/ (* (sin (+ phi (* y (/ (- n 1) 2)))) (sin (/ (* n y) 2))) den))))
- (if (> sum pk)
- (set! pk sum)))))
- (set! y (+ y iy)))
- (outa i pk)))))
- |#
-
-
- (defgenerator (nxycos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (ratio 1.0) (n 1) (angle 0.0) fm)
-
-
- (define nxycos
-
- (let ((documentation "(make-nxycos frequency (ratio 1.0) (n 1)) creates an nxycos generator. (nxycos gen (fm 0.0))
- returns n cosines from frequency spaced by frequency * ratio."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((x angle)
- (y (* x ratio))
- (den (sin (* y 0.5))))
- (set! angle (+ angle fm frequency))
- (if (< (abs den) nearly-zero)
- 1.0
- (/ (* (cos (+ x (* 0.5 (- n 1) y)))
- (sin (* 0.5 n y)))
- (* n den)))))))) ; n=normalization
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nxycos 300 1/3 3)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (* .5 (nxycos gen))))))
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; G&R first col rows 3 4
-
- (defgenerator (nxy1cos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (ratio 1.0) (n 1) (angle 0.0) fm)
-
-
- (define nxy1cos
-
- (let ((documentation "(make-nxy1cos frequency (ratio 1.0) (n 1)) creates an nxy1cos
- generator. (nxy1cos gen (fm 0.0)) returns 2n cosines from frequency spaced by frequency * ratio with every other cosine multiplied by -1."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((x angle)
- (y (* x ratio))
- (den (cos (* y 0.5))))
- (set! angle (+ angle fm frequency))
- (if (< (abs den) nearly-zero)
- -1.0
- (max -1.0
- (min 1.0
- (/ (* (sin (* n y))
- (sin (+ x (* (- n 0.5) y))))
- (* 2 n den))))))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nxy1cos 300 1/3 3)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (nxy1cos gen)))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nxy1cos 300 1/3 3))
- (gen1 (make-nxycos 300 1/3 6)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (* 0.4 (+ (nxycos gen1 0.0) (nxy1cos gen)))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nxy1cos (radians->hz (* .01 pi)) 1.0 3)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (nxy1cos gen)))))
- |#
-
-
- (defgenerator (nxy1sin
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (ratio 1.0) (n 1) (angle 0.0) fm)
-
-
- (define nxy1sin
-
- (let ((documentation "(make-nxy1sin frequency (ratio 1.0) (n 1)) creates an nxy1sin generator. (nxy1sin gen (fm 0.0))
- returns n sines from frequency spaced by frequency * ratio with every other sine multiplied by -1."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((x angle)
- (y (* x ratio))
- (den (cos (* y 0.5))))
- (set! angle (+ angle fm frequency))
- (/ (* (sin (+ x (* 0.5 (- n 1) (+ y pi))))
- (sin (* 0.5 n (+ y pi))))
- (* n den)))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nxy1sin 300 1/3 3)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (nxy1sin gen)))))
- |#
-
- ;;; we can get the sinusoidally varying maxamp by using e.g. (make-nxy1sin 1 1000 3)
- ;;; the peak starts at ca .72 and goes to 1 etc
- ;;; the peak is just offset from pi (either way)
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; n odd sinusoids: noddsin, noddcos, noddssb
-
- ;;; sndclm.html (G&R) first col 5th row (sum of odd sines)
-
- (define (find-noddsin-max n)
- (let find-mid-max ((n n)
- (lo 0.0000)
- (hi (/ pi (+ (* 2 n) 0.5))))
- (define (nodds x n)
- (let ((den (sin x))
- (num (sin (* n x))))
- (if (= den 0.0)
- 0.0000
- (/ (* num num) den))))
- (let ((mid (/ (+ lo hi) 2))
- (ylo (nodds lo n))
- (yhi (nodds hi n)))
- (if (< (abs (- ylo yhi)) 1e-09)
- (nodds mid n)
- (find-mid-max n (if (> ylo yhi)
- (values lo mid)
- (values mid hi)))))))
-
- (define noddsin-maxes (make-float-vector 100))
-
- (defgenerator (noddsin
- :make-wrapper (lambda (g)
- (set! (g 'n) (max (g 'n) 1))
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (if (not (and (< (g 'n) 100)
- (> (noddsin-maxes (g 'n)) 0.0)))
- (set! (noddsin-maxes (g 'n)) (find-noddsin-max (g 'n))))
- (set! (g 'norm) (/ 1.0 (noddsin-maxes (g 'n))))
- g))
- (frequency *clm-default-frequency*) (n 1) (angle 0.0) (norm 1.0) fm)
-
-
- (define noddsin
-
- (let ((documentation "(make-noddsin frequency (n 1)) creates an noddsin generator. (noddsin gen (fm 0.0))
- returns n odd-numbered sines spaced by frequency."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((snx (sin (* n angle)))
- (den (sin angle)))
- (set! angle (+ angle fm frequency))
- (if (< (abs den) nearly-zero)
- 0.0
- (/ (* norm snx snx) den)))))))
-
- ;;; max is at about: 3*pi/(8*n) -- essentially half of the nsin peak
- ;;; and we end up with the same max amp as nsin!!
- ;;; :(/ (* 8 (sin (* pi 3/8)) (sin (* pi 3/8))) (* 3 pi))
- ;;; 7.245186202974229185687564326622851596478E-1
-
-
- #|
- ;;; clarinety
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-noddsin 300 :n 3))
- (ampf (make-env '(0 0 1 1 2 1 3 0) :length 40000 :scaler .5)))
- (do ((i 0 (+ i 1)))
- ((= i 40000))
- (outa i (* (env ampf) (noddsin gen))))))
- |#
-
-
-
- (defgenerator (noddcos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (n 1) (angle 0.0) fm)
-
-
- (define noddcos
-
- (let ((documentation "(make-noddcos frequency (n 1)) creates an noddcos generator. (noddcos gen (fm 0.0))
- returns n odd-numbered cosines spaced by frequency."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((cx angle)
- (den (* 2 n (sin angle)))) ; "n" here is normalization
- (set! angle (+ angle fm frequency))
- (if (< (abs den) nearly-zero)
- (let ((fang (modulo cx (* 2 pi))))
- ;; hopefully this almost never happens...
- (if (or (< fang 0.001)
- (< (abs (- fang (* 2 pi))) 0.001))
- 1.0
- -1.0))
- (/ (sin (* 2 n cx)) den)))))))
-
- ;;; (Gradshteyn and Ryzhik 1.342)
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-noddcos 100 :n 10)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (* .5 (noddcos gen))))))
- |#
-
-
-
- (defgenerator (noddssb
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (ratio 1.0) (n 1) (angle 0.0) fm)
-
-
- (define noddssb
-
- (let ((documentation "(make-noddssb frequency (ratio 1.0) (n 1)) creates an noddssb generator. (noddssb gen (fm 0.0))
- returns n sinusoids from frequency spaced by 2 * ratio * frequency."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((cx angle)
- (mx (* cx ratio)))
- (let ((x (- cx mx))
- (sinnx (sin (* n mx)))
- (den (* n (sin mx)))) ; "n" is normalization
- (set! angle (+ angle fm frequency))
- (if (< (abs den) nearly-zero)
- (if (< (modulo mx (* 2 pi)) .1)
- -1.0
- 1.0)
- (- (* (sin x)
- (/ (* sinnx sinnx) den))
- (* (cos x)
- (/ (sin (* 2 n mx))
- (* 2 den)))))))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-noddssb 1000.0 0.1 5)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (* .5 (noddssb gen))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-noddssb 1000.0 0.1 5))
- (vib (make-oscil 5.0)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (* .5 (noddssb gen (* (hz->radians 100.0) (oscil vib))))))))
- |#
-
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; various kernels: ncos2 = ncos squared (Fejer), ncos4 = ncos2 squared (Jackson), npcos = Poussin kernel
-
- (defgenerator (ncos2
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (n 1) (angle 0.0) fm)
-
-
- (define ncos2
-
- (let ((documentation "(make-ncos2 frequency (n 1)) creates an ncos2 (Fejer kernel) generator. (ncos2 gen (fm 0.0))
- returns n sinusoids spaced by frequency scaled by (n-k)/(n+1)"))
-
- ;; from "Trigonometric Series" Zygmund p88 with changes suggested by Katznelson "Introduction to Harmonic Analysis" p12, and
- ;; scaling by an extra factor of 1/n+1 to make sure we always peak at 1.0 (I assume callers in this context are interested
- ;; in the pulse-train aspect and want easily predictable peak amp). Harmonics go as (n-i)/n+1.
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((x angle)
- (den (sin (* 0.5 x))))
- (set! angle (+ angle fm frequency))
- (if (< (abs den) nearly-zero)
- 1.0
- (let ((val (/ (sin (* 0.5 (+ n 1) x))
- (* (+ n 1) den))))
- (* val val))))))))
-
- ;;; can't use two oscils here because the angles have to line up perfectly
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-ncos2 100.0 :n 10)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (* .5 (ncos2 gen))))))
- |#
-
-
-
- (define make-ncos4 make-ncos2)
-
- ;; Katznelson p16
-
- (define ncos4
-
- (let ((documentation "(make-ncos4 frequency (n 1)) creates an ncos4 (Jackson kernel) generator. (ncos4 gen (fm 0.0))
- returns n sinusoids spaced by frequency scaled by ((n-k)/(n+1))^2"))
-
- (lambda* (gen (fm 0.0))
- (let ((val (ncos2 gen fm)))
- (* val val))))) ; we already normalized this to 1.0
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-ncos4 100.0 :n 10)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (* .5 (ncos4 gen))))))
- |#
-
-
-
- (defgenerator (npcos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (n 1) (angle 0.0) fm)
-
-
- (define npcos
-
- (let ((documentation "(make-npcos frequency (n 1)) creates an npcos (Poussin kernel) generator. (npcos gen (fm 0.0))
- returns n*2+1 sinusoids spaced by frequency with amplitudes in a sort of tent shape."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((result (let ((den (sin (* 0.5 angle))))
- (if (< (abs den) nearly-zero)
- 1.0
- (let ((result1 (let ((val (let ((n1 (+ n 1)))
- (/ (sin (* 0.5 n1 angle))
- (* n1 den)))))
- (* val val)))
- (result2 (let ((val (let ((p2n2 (+ (* 2 n) 2)))
- (/ (sin (* 0.5 p2n2 angle))
- (* p2n2 den)))))
- (* val val))))
- (- (* 2 result2) result1))))))
- (set! angle (+ angle fm frequency))
- result)))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-npcos 100.0 :n 10)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (* .5 (npcos gen))))))
- |#
-
-
- #|
-
- ;;; ncos5 and nsin5 are minor variants of nsin and ncos -- the last component is at half amplitude
-
- (defgenerator (ncos5
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (n 1) (angle 0.0) fm)
-
-
- (define ncos5
-
- (let ((documentation "(make-ncos5 frequency (n 1)) creates an ncos5 generator. (ncos5 gen (fm 0.0))
- returns n cosines spaced by frequency. All are equal amplitude except the first and last at half amp."))
-
- ;; from "Chebyshev Polynomials", Mason and Handscomb, p87
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((x angle)
- (den (tan (* 0.5 x))))
- (set! angle (+ angle fm frequency))
- (if (< (abs den) nearly-zero)
- 1.0
- (/ (- (/ (sin (* n x))
- (* 2 den))
- 0.5)
- (- n 0.5))))))))
-
-
- (with-sound (:clipped #f :statistics #t)
- (let ((gen (make-ncos5 100.0 :n 10)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (* .5 (ncos5 gen))))))
-
-
- (define (find-nsin5-max n)
-
- (define (find-mid-max n lo hi)
- (define (ns x n)
- (let* ((den (tan (* 0.5 x))))
- (if (< (abs den) nearly-zero)
- 0.0
- (/ (- 1.0 (cos (* n x)))
- den))))
- (let ((mid (/ (+ lo hi) 2)))
- (let ((ylo (ns lo n))
- (yhi (ns hi n)))
- (if (< (abs (- ylo yhi)) 1e-9)
- (ns mid n)
- (if (> ylo yhi)
- (find-mid-max n lo mid)
- (find-mid-max n mid hi))))))
-
- (find-mid-max n 0.0 (/ pi (+ n .5))))
-
-
- (defgenerator (nsin5
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'n) (max 2 (g 'n)))
- (set! (g 'norm) (find-nsin5-max (g 'n)))
- g))
- (frequency *clm-default-frequency*) (n 2) (angle 0.0) (norm 1.0) fm)
-
-
- (define nsin5
-
- (let ((documentation "(make-nsin5 frequency (n 1)) creates an nsin5 generator. (nsin5 gen (fm 0.0))
- returns n sines spaced by frequency. All are equal amplitude except last at half amp."))
-
- ;; from "Chebyshev Polynomials", Mason and Handscomb, p100
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((x angle)
- (den (tan (* 0.5 x))))
- (set! angle (+ angle fm frequency))
- (if (< (abs den) nearly-zero)
- 0.0
- (/ (- 1.0 (cos (* n x)))
- (* den norm))))))))
-
-
- (define (find-nsin-max n)
-
- (define (find-mid-max n lo hi)
- (define (ns x n)
- (let* ((a2 (/ x 2))
- (den (sin a2)))
- (if (= den 0.0)
- 0.0
- (/ (* (sin (* n a2)) (sin (* (+ 1 n) a2))) den))))
- (let ((mid (/ (+ lo hi) 2)))
- (let ((ylo (ns lo n))
- (yhi (ns hi n)))
- (if (< (abs (- ylo yhi)) 1e-14)
- (ns mid n) ; rationalize (/ mid pi) for location
- (if (> ylo yhi)
- (find-mid-max n lo mid)
- (find-mid-max n mid hi))))))
-
- (find-mid-max n 0.0 (/ pi (+ n .5))))
-
-
- (with-sound (:clipped #f :statistics #t)
- (let ((gen (make-nsin5 100.0 :n 10)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (nsin5 gen)))))
-
- (let ((norms (list 1.0 0.0)))
- (do ((i 2 (+ i 1)))
- ((= i 40))
- (let* ((res (with-sound (:clipped #f)
- (let ((gen (make-nsin5 100.0 :n i)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (nsin5 gen))))))
- (snd (find-sound res)))
- (format () ";~D: ~A" i (maxamp snd 0))
- (set! norms (cons (maxamp snd 0) norms))))
- (reverse norms))
-
- ;;; from the same book p 110 is atan(x)/x, if x=cos we get:
-
- (with-sound (:clipped #f :statistics #t)
- (let* ((x 0.0)
- (freq (hz->radians 100.0)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (/ (- (/ (atan (cos x))
- (cos x))
- (* 0.5 1.76275))
- -0.1187))
- (set! x (+ x freq)))))
-
- (let ((sum 0.0))
- (do ((s 1 (+ s 2)))
- ((>= s 100))
- (set! sum (+ sum (* 4 (/ (expt (- (sqrt 2.0) 1.0) (+ (* 2 s) 1))
- (+ (* 2 s) 1))))))
- sum) ; ~ 0.096
-
- ;;; the evens cancel, each of the odds gets through once
- |#
-
-
-
-
- (define generator-max-r 0.999999)
- (define generator-min-r -0.999999)
- (define (generator-clamp-r r)
- (min generator-max-r (max generator-min-r r)))
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; n sinusoids scaled by r: nrsin, nrcos, nrssb
-
- #|
- (define nrsin-methods
- (list
- (cons 'mus-frequency
- (dilambda
- (lambda (g) (mus-frequency (g 'gen)))
- (lambda (g val) (set! (mus-frequency (g 'gen)) val))))
- (cons 'mus-scaler
- (dilambda
- (lambda (g) (mus-scaler (g 'gen)))
- (lambda (g val) (set! (mus-scaler (g 'gen)) val))))))
-
- (defgenerator (nrsin
- :make-wrapper (lambda (g)
- (set! (g 'r) (generator-clamp-r (g 'r)))
- (set! (g 'gen) (make-nrxysin (g 'frequency) 1.0 (g 'n) (g 'r)))
- g)
- :methods nrsin-methods)
- (frequency *clm-default-frequency*) (n 1) (r 0.5)
- (gen #f))
- |#
-
- (define make-nrsin make-nrxysin)
- (define nrsin nrxysin)
- (define nrsin? nrxysin?)
-
- ;; "(make-nrsin frequency (n 1) (r 0.5)) creates an nrsin generator.\n\
- ;; (nrsin gen (fm 0.0)) returns n sines spaced by frequency with amplitudes scaled by r^k."
-
-
- (define (nrcos-set-scaler g val)
- (set! (g 'r) (min 0.999999 (max -0.999999 val)))
- (with-let g
- (let ((absr (abs r)))
- (set! rr (* r r))
- (set! r1 (+ 1.0 rr))
- (set! norm (- (/ (- (expt absr n) 1) (- absr 1)) 1.0))
- (set! trouble (or (= n 1)
- (< absr 1.0e-12)))))
- val)
-
- (define nrcos-methods
- (list
- (cons 'mus-order
- (dilambda
- (lambda (g) (- (g 'n) 1))
- (lambda (g val)
- (set! (g 'n) (+ 1 val))
- (set! (g 'e1) (expt (g 'r) (g 'n)))
- (set! (g 'e2) (expt (g 'r) (+ (g 'n) 1)))
- (set! (g 'norm) (- (/ (- (expt (abs (g 'r)) (g 'n)) 1) (- (abs (g 'r)) 1)) 1.0))
- (set! (g 'trouble) (or (= (g 'n) 1) (< (abs (g 'r)) nearly-zero)))
- val)))
- (cons 'mus-frequency
- (dilambda
- (lambda (g) (radians->hz (g 'frequency)))
- (lambda (g val) (set! (g 'frequency) (hz->radians val)))))
- (cons 'mus-scaler
- (dilambda
- (lambda (g) (g 'r))
- nrcos-set-scaler))))
-
- (defgenerator (nrcos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'n) (+ 1 (g 'n)))
- (set! (g 'r) (generator-clamp-r (g 'r)))
- (set! (g 'rr) (* (g 'r) (g 'r)))
- (set! (g 'r1) (+ 1.0 (g 'rr)))
- (set! (g 'e1) (expt (g 'r) (g 'n)))
- (set! (g 'e2) (expt (g 'r) (+ (g 'n) 1)))
- (set! (g 'norm) (- (/ (- (expt (abs (g 'r)) (g 'n)) 1) (- (abs (g 'r)) 1)) 1.0)) ; n+1??
- (set! (g 'trouble) (or (= (g 'n) 1) (< (abs (g 'r)) nearly-zero)))
- g)
- :methods nrcos-methods)
- (frequency *clm-default-frequency*) (n 1) (r 0.5) (angle 0.0) fm rr r1 e1 e2 norm trouble)
-
-
- (define nrcos
-
- (let ((documentation "(make-nrcos frequency (n 1) (r 0.5)) creates an nrcos generator. (nrcos gen (fm 0.0))
- returns n cosines spaced by frequency with amplitudes scaled by r^k."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x angle)
- (rcos (* r (cos angle))))
- (set! angle (+ angle fm frequency))
- (if trouble
- 0.0
- (/ (- (+ rcos (* e2 (cos (* (- n 1) x))))
- (* e1 (cos (* n x))) rr)
- (* norm (+ r1 (* -2.0 rcos))))))))))
-
- ;; it's faster to use polywave here and nrcos->polywave for the partials list (animals.scm) if n is not enormous
-
- ;;; formula changed to start at k=1 and n increased so we get 1 to n
- ;;; here is the preoptimization form:
- #|
- (with-let gen
- (let ((x angle))
- (set! angle (+ angle fm frequency))
- (if (or (= n 1)
- (< (abs r) nearly-zero))
- 0.0
- (let ((norm (- (/ (- (expt (abs r) n) 1) (- (abs r) 1)) 1.0))) ; n+1??
- (/ (+ (- (* r (cos x))
- (* (expt r n) (cos (* n x))) (* r r))
- (* (expt r (+ n 1)) (cos (* (- n 1) x))))
- (* norm (+ 1.0 (* -2.0 r (cos x)) (* r r))))))))
- |#
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nrcos 400.0 :n 5 :r 0.5)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (* .5 (nrcos gen))))))
-
- (with-sound (:clipped #f :statistics #t :play #t :scaled-to .1)
- (let ((gen (make-nrcos 1200.0 :n 3 :r 0.99))
- (mod (make-oscil 400.0)) ; multi-carrier fm
- (index 0.01))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (outa i (nrcos gen (* index (oscil mod)))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nrcos 2000.0 :n 3 :r 0.5))
- (mod (make-oscil 400.0)) ; multi-carrier fm
- (index 0.02))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (outa i (* .5 (nrcos gen (* index (oscil mod))))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nrcos 2000.0 :n 3 :r 0.5))
- (mod (make-oscil 400.0))
- (index (make-env '(0 0 1 .1) :length 30000))) ; or '(0 .4 1 0)
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (outa i (* .5 (nrcos gen (* (env index) (oscil mod))))))))
- |#
-
- (definstrument (lutish beg dur freq amp)
- (let ((res1 (max 1 (round (/ 1000.0 (max 1.0 (min 1000.0 freq))))))
- (maxind (max .01 (min .3 (/ (- (log freq) 3.5) 8.0)))))
- (let ((gen (make-nrcos (* freq res1) :n (max 1 (- res1 2))))
- (mod (make-oscil freq))
- (start (seconds->samples beg))
- (stop (seconds->samples (+ beg dur)))
- (index (make-env (list 0 maxind 1 (* maxind .25) (max dur 2.0) 0.0) :duration dur))
- (amplitude (make-env (list 0 0 .01 1 .2 1 .5 .5 1 .25 (max dur 2.0) 0.0) :duration dur :scaler amp)))
- (do ((i start (+ i 1)))
- ((= i stop))
- (let ((ind (env index)))
- (set! (gen 'r) ind)
- (outa i (* (env amplitude)
- (nrcos gen (* ind (oscil mod))))))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (lutish 0 1 440 .1))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (do ((i 0 (+ i 1)))
- ((= i 10))
- (lutish (* i .1) 2 (* 100 (+ i 1)) .05)))
- |#
-
-
-
- ;;; G&R second col first and second rows
-
- (defgenerator (nrssb
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'r) (generator-clamp-r (g 'r)))
- (set! (g 'r) (max (g 'r) 0.0))
- (set! (g 'rn) (- (expt (g 'r) (g 'n))))
- (set! (g 'rn1) (expt (g 'r) (+ (g 'n) 1)))
- (set! (g 'norm) (/ (- (g 'rn) 1) (- (g 'r) 1)))
- g))
- (frequency *clm-default-frequency*) (ratio 1.0) (n 1) (r 0.5) (angle 0.0) fm interp rn rn1 norm)
-
-
- (define nrssb
-
- (let ((documentation "(make-nrssb frequency (ratio 1.0) (n 1) (r 0.5)) creates an nrssb generator. (nrssb gen (fm 0.0))
- returns n sinusoids from frequency spaced by frequency * ratio with amplitudes scaled by r^k."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((cx angle)
- (mx (* cx ratio)))
- (let ((nmx (* n mx))
- (n1mx (* (- n 1) mx))
- (den (* norm (+ 1.0 (* -2.0 r (cos mx)) (* r r)))))
- (set! angle (+ angle fm frequency))
- (/ (- (* (sin cx)
- (+ (* r (sin mx))
- (* rn (sin nmx))
- (* rn1 (sin n1mx))))
- (* (cos cx)
- (+ 1.0
- (* -1.0 r (cos mx))
- (* rn (cos nmx))
- (* rn1 (cos n1mx)))))
- den)))))))
-
-
- (define nrssb-interp
-
- (let ((documentation "(make-nrssb frequency (ratio 1.0) (n 1) (r 0.5)) creates an nrssb generator for use with
- nrssb-interp. (nrssb-interp gen fm interp) returns n sinusoids from frequency spaced by frequency * ratio with amplitudes
- scaled by r^k. The 'interp' argument determines whether the sidebands are above (1.0) or below (-1.0) frequency."))
-
- (lambda (gen fm interp)
- (let-set! gen 'fm fm)
- (let-set! gen 'interp interp)
- (with-let gen
- (let* ((cx angle)
- (mx (* cx ratio)))
- (let ((nmx (* n mx))
- (n1mx (* (- n 1) mx))
- (den (* norm (+ 1.0 (* -2.0 r (cos mx)) (* r r)))))
- (set! angle (+ angle fm frequency))
- (/ (- (* interp
- (sin cx)
- (+ (* r (sin mx))
- (* rn (sin nmx))
- (* rn1 (sin n1mx))))
- (* (cos cx)
- (+ 1.0
- (* -1.0 r (cos mx))
- (* rn (cos nmx))
- (* rn1 (cos n1mx)))))
- den)))))))
-
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nrssb 1000 0.1 5 0.5)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (nrssb gen)))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nrssb 1000 0.1 5 0.5))
- (vib (make-oscil 5)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (nrssb gen (* (hz->radians 100) (oscil vib)))))))
- |#
-
- (definstrument (oboish beg dur freq amp aenv)
- (let ((res1 (max 1 (round (/ 1400.0 (max 1.0 (min 1400.0 freq))))))
- (mod1 (make-oscil 5.0))
- (res2 (max 1 (round (/ 2400.0 (max 1.0 (min 2400.0 freq))))))
- (gen3 (make-oscil freq))
- (start (seconds->samples beg))
- (amplitude (make-env aenv :duration dur :base 4 :scaler amp))
- (skenv (make-env (list 0.0 0.0 1 1 2.0 (mus-random 1.0) 3.0 0.0 (max 4.0 (* dur 20.0)) 0.0)
- :duration dur :scaler (hz->radians (random (* freq .05)))))
- (relamp (+ .85 (random .1)))
- (avib (make-rand-interp 5 .2))
- (hfreq (hz->radians freq))
- (h3freq (hz->radians (* .003 freq)))
- (scl (/ 0.05 amp)))
- (let ((gen (make-nrssb (* freq res1) (/ res1) :n res1 :r 0.75))
- (gen2 (make-oscil (* freq res2)))
- (stop (+ start (seconds->samples dur))))
- (do ((i start (+ i 1)))
- ((= i stop))
- (let ((result (let* ((vol (* (+ .8 (rand-interp avib))
- (env amplitude)))
- (vola (* scl vol))
- (vib (+ (* h3freq (oscil mod1))
- (env skenv))))
- (* vol
- (+ (* (- relamp vola)
- (nrssb-interp gen (* res1 vib) -1.0))
- (* (- (+ 1.0 vola) relamp)
- (oscil gen2 (+ (* vib res2)
- (* hfreq (oscil gen3 vib))))))))))
- (outa i result)
- (if *reverb* (outa i (* .01 result) *reverb*)))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (oboish 0 1 300 .1 '(0 0 1 1 2 0)))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (do ((i 0 (+ i 1)))
- ((= i 10))
- (oboish (* i .3) .4 (+ 100 (* 50 i)) .05 '(0 0 1 1 2 1 3 0))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((rats (vector 1 256/243 9/8 32/27 81/64 4/3 1024/729 3/2 128/81 27/16 16/9 243/128 2))
- (mode (vector 0 0 2 4 11 11 5 6 7 9 2 12 0)))
- (do ((i 0 (+ i 1)))
- ((= i 20))
- (oboish (/ (random 32) 8)
- (/ (+ 3 (random 8)) 8)
- (* 16.351 16 (rats (mode (random 12))))
- (+ .25 (random .25))
- (let* ((pt1 (random 1.0))
- (pt2 (+ pt1 (random 1.0)))
- (pt3 (+ pt2 (random 1.0))))
- (list 0 0 pt1 1 pt2 .5 pt3 0))))))
-
- ;;; .85 .15 (* 2 freq) 300, 2400 + 0.5*vib
- |#
-
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; n sinusoids scaled by k: nkssb
-
-
- ;;; G&R first col ksinkx cases
-
- (define nkssb-methods
- (list
- (cons 'mus-order
- (dilambda
- (lambda (g) (- (g 'n) 1))
- (lambda (g val)
- (set! (g 'n) (+ 1 val))
- (set! (g 'norm) (/ (* 0.5 val (- val 1))))))))) ; nominal n is off by 1
-
- (defgenerator (nkssb
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'n) (+ 1 (g 'n))) ; sum goes 1 to n-1
- (set! (g 'norm) (/ (* 0.5 (g 'n) (- (g 'n) 1))))
- g)
- :methods nkssb-methods)
- (frequency *clm-default-frequency*) (ratio 1.0) (n 1) (angle 0.0) fm interp norm)
-
-
- (define nkssb
-
- (let ((documentation "(make-nkssb frequency (ratio 1.0) (n 1)) creates an nkssb generator. (nkssb gen (fm 0.0))
- returns n sinusoids from frequency spaced by frequency * ratio with amplitude k."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x (* angle ratio)))
- (let ((cxx (- angle x))
- (sx2 (sin (* 0.5 x)))
- (nx (* n x))
- (nx2 (* 0.5 (- (* 2 n) 1) x)))
- (let ((sx22 (* 2 sx2))
- (sxsx (* 4 sx2 sx2)))
- (set! angle (+ angle fm frequency))
- (if (< (abs sx2) 1.0e-8)
- -1.0
- (let ((s1 (- (/ (sin nx) sxsx)
- (/ (* n (cos nx2)) sx22)))
- (c1 (- (/ (* n (sin nx2)) sx22)
- (/ (- 1.0 (cos nx)) sxsx))))
- (* (- (* s1 (sin cxx))
- (* c1 (cos cxx)))
- norm))))))))))
-
-
- (define nkssb-interp
-
- (let ((documentation " (make-nkssb-interp frequency (ratio 1.0) (n 1)) creates an nkssb generator for
- nkssb-interp. (nkssb-interp gen fm interp) returns n sinusoids from frequency spaced by frequency * ratio
- with amplitude k. The 'interp' argument determines whether the sidebands are above (1.0) or below (-1.0) frequency."))
-
- (lambda (gen fm interp)
- (let-set! gen 'fm fm)
- (let-set! gen 'interp interp)
- (with-let gen
- (let ((x (* angle ratio)))
- (let ((cxx (- angle x))
- (sx2 (sin (* 0.5 x))))
- (let ((sx22 (* 2 sx2))
- (sxsx (* 4 sx2 sx2))
- (nx (* n x))
- (nx2 (* 0.5 (- (* 2 n) 1) x)))
- (set! angle (+ angle fm frequency))
- (if (< (abs sx2) 1.0e-8)
- 1.0
- (let ((s1 (- (/ (sin nx) sxsx)
- (/ (* n (cos nx2)) sx22)))
- (c1 (- (/ (* n (sin nx2)) sx22)
- (/ (- 1.0 (cos nx)) sxsx))))
- (* (- (* c1 (cos cxx))
- (* interp (sin cxx) s1))
- norm)))))))))) ; peak seems to be solid right through the interpolation
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nkssb 1000.0 0.1 5)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (nkssb gen)))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nkssb 1000.0 0.1 5))
- (vib (make-oscil 5.0))
- (vibamp (hz->radians 50.0)))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (outa i (nkssb gen (* vibamp (oscil vib)))))))
- |#
-
- (definstrument (nkssber beg dur freq mfreq n vibfreq amp)
- (let ((start (seconds->samples beg))
- (stop (seconds->samples (+ beg dur)))
- (gen (make-nkssb freq (/ mfreq freq) n))
- (move (make-env '(0 1 1 -1) :duration dur))
- (vib (make-polywave vibfreq (list 1 (hz->radians (* (/ freq mfreq) 5.0))) mus-chebyshev-second-kind))
- (ampf (make-env '(0 0 1 1 5 1 6 0) :scaler amp :duration dur)))
- (do ((i start (+ i 1)))
- ((= i stop))
- (outa i (* (env ampf)
- (nkssb-interp gen
- (polywave vib)
- (env move))) ; interp env
- ))))
-
- #|
- (with-sound (:play #t)
- (nkssber 0 1 1000 100 5 5 0.5)
- (nkssber 1 2 600 100 4 1 0.5)
- (nkssber 3 2 1000 540 3 3 0.5)
- (nkssber 5 4 300 120 2 0.25 0.5)
- (nkssber 9 1 30 4 40 0.5 0.5)
- (nkssber 10 1 20 6 80 0.5 0.5))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nkssb 1000.0 0.1 5))
- (move (make-env '(0 1 1 -1) :length 30000))
- (vib (make-oscil 5.0))
- (vibamp (hz->radians 50.0)))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (outa i (* 0.5 (nkssb-interp gen
- (* vibamp (oscil vib))
- (env move))) ; interp env
- ))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nkssb 600.0 1/6 4))
- (vib (make-oscil 1.0))
- (vibamp (hz->radians 30.0)))
- (do ((i 0 (+ i 1)))
- ((= i 100000))
- (let ((intrp (oscil vib)))
- (outa i (* 0.5 (nkssb-interp gen
- (* vibamp intrp)
- intrp)))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nkssb 1000.0 (/ 540 1000) 3))
- (vib (make-oscil 3.0)) ; 0.3 or 125 + 0.25 and 2 -> circling sound
- (vibamp (hz->radians (* (/ 1000 540) 5.0))))
- (do ((i 0 (+ i 1)))
- ((= i 100000))
- (let ((intrp (oscil vib)))
- (outa i (* 0.5 (nkssb-interp gen
- (* vibamp intrp)
- intrp)))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nkssb 300.0 (/ 120 300) 2))
- (vib (make-oscil 0.25))
- (vibamp (hz->radians (* (/ 300 120) 5.0))))
- (do ((i 0 (+ i 1)))
- ((= i 300000))
- (let ((intrp (oscil vib)))
- (outa i (* 0.5 (nkssb-interp gen
- (* vibamp intrp)
- intrp)))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nkssb 30.0 (/ 4 30) 40))
- (vib (make-oscil 0.5))
- (vibamp (hz->radians (* (/ 30 4) 5.0))))
- (do ((i 0 (+ i 1)))
- ((= i 300000))
- (let ((intrp (oscil vib)))
- (outa i (* 0.5 (nkssb-interp gen
- (* vibamp intrp)
- intrp)))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nkssb 20.0 (/ 6 20) 80)) ; 120 8 80 (100), 6 400
-
- (vib (make-oscil 0.5))
- (vibamp (hz->radians (* (/ 20 6) 5.0))))
- (do ((i 0 (+ i 1)))
- ((= i 300000))
- (let ((intrp (oscil vib)))
- (outa i (* 0.5 (nkssb-interp gen
- (* vibamp intrp)
- intrp)))))))
- |#
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; n cos scaled by sin(k*pi/(n+1))/sin(pi/(n+1))
- ;;; "Biased Trigonometric Polynomials", Montgomery and Vorhauer
- ;;; American Math Monthly vol 114 no 9 Nov 2007
-
- (defgenerator (nsincos
- :make-wrapper (lambda (g)
- (let ((n (g 'n)))
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'n2) (/ (+ n 1) 2))
- (set! (g 'cosn) (cos (/ pi (+ n 1))))
- (do ((k 1 (+ k 1)))
- ((> k n))
- (set! (g 'norm) (+ (g 'norm)
- (/ (sin (/ (* k pi) (+ n 1)))
- (sin (/ pi (+ n 1)))))))
- g)))
- (frequency *clm-default-frequency*) (n 1)
- (angle 0.0) (n2 1.0) (cosn 1.0) (norm 0.0) fm)
-
-
- (define nsincos
-
- (let ((documentation "(make-nsincos frequency (n 1)) creates an nsincos generator. (nsincos gen (fm 0.0))
- returns n cosines spaced by frequency with amplitude sin(k*pi/(n+1))/sin(pi/(n+1))"))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((x angle)
- (num (cos (* n2 x))))
- (set! angle (+ angle fm frequency))
- (/ (* num num)
- (* norm (- (cos x) cosn))))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #f)
- (let ((gen (make-nsincos 100.0 3)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (nsincos gen)))))
- |#
-
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; Ramanujan, "On certain Arithmetical Functions"
-
- (defgenerator (n1cos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (n 1) (angle 0.0) fm)
-
- (define* (n1cos gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((x angle)
- (tn (tan (* 0.5 x))))
- (set! angle (+ angle fm frequency))
- (if (< (abs tn) 1.0e-6)
- 1.0
- (/ (- 1.0 (cos (* n x)))
- (* tn tn
- n n 2)))))) ; normalization -- this still has the very large DC term
-
- #|
- (with-sound (:clipped #f)
- (let ((gen (make-n1cos 100.0 10)))
- (do ((i 0 (+ i 1)))
- ((= i 44100))
- (outa i (n1cos gen)))))
- |#
-
-
-
- #|
- ;;; --------------------------------------------------------------------------------
-
- ;;; not sure the next two are interesting -- 2 more kernels
-
- ;;; Dimitrov and Merlo
-
- (defgenerator (npos1cos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (n 1) (angle 0.0) fm)
-
-
- (define npos1cos
-
- (let ((documentation "(make-npos1cos frequency (n 1)) creates an npos1cos generator. (npos1cos gen (fm 0.0))
- returns n cosines spaced by frequency."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((x angle)
- (num (- (* (+ n 2) (sin (/ (* n x) 2)))
- (* n (sin (/ (* (+ n 2) x) 2)))))
- (sx (sin (/ x 2)))
- (den (* 4 n (+ n 1) (+ n 2) sx sx sx sx)))
- (set! angle (+ angle fm frequency))
- (if (< (abs den) nearly-zero)
- 0.0
- (/ (* 3 num num)
- den)))))))
-
- ;;; needs normalization and no DC. side amps seem close
-
-
- (with-sound (:clipped #f :statistics #t :play #f)
- (let ((gen (make-npos1cos 100.0 3)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (npos1cos gen)))))
-
-
- (defgenerator (npos3cos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (n 1) (angle 0.0) fm)
-
-
- (define npos3cos
-
- (let ((documentation "(make-npos3cos frequency (n 1)) creates an npos3cos generator. (npos3cos gen (fm 0.0))
- returns n cosines spaced by frequency."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((x angle)
- (sx (sin (/ x 2)))
- (den (* (+ (* 4 n) 2) sx sx)))
- (set! angle (+ angle fm frequency))
- (if (< (abs den) nearly-zero)
- (* 1.0 n)
- (/ (- 2 (cos (* n x)) (cos (* (+ n 1) x)))
- den)))))))
-
- ;;; needs normalization and no DC, peak at den=0 not right. side amps seem close
-
- (with-sound (:clipped #f :statistics #t :play #f)
- (let ((gen (make-npos3cos 100.0 3)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (npos3cos gen)))))
- |#
-
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; inf sinusoids scaled by r: rcos, rssb
-
- (define rcos-methods
- (list
- (cons 'mus-frequency
- (dilambda
- (lambda (g) (mus-frequency (g 'osc)))
- (lambda (g val) (set! (mus-frequency (g 'osc)) val))))
-
- (cons 'mus-scaler
- (dilambda
- (lambda (g) (g 'r))
- (lambda (g val)
- (set! (g 'r) (generator-clamp-r val))
- (set! (g 'rr) (* (g 'r) (g 'r)))
- (set! (g 'rr+1) (+ 1.0 (g 'rr)))
- (set! (g 'rr-1) (- 1.0 (g 'rr)))
- (set! (g 'r2) (* 2.0 (g 'r)))
- (let ((absr (abs (g 'r))))
- (set! (g 'norm) (if (< absr nearly-zero) 0.0 (/ (- 1.0 absr) (* 2.0 absr)))))
- val)))
-
- (cons 'mus-phase
- (dilambda
- (lambda (g) (mus-phase (g 'osc)))
- (lambda (g val) (set! (mus-phase (g 'osc)) val))))))
-
- (defgenerator (rcos
- :make-wrapper (lambda (g)
- (set! (g 'osc) (make-oscil (g 'frequency) (* 0.5 pi)))
- (set! (g 'r) (generator-clamp-r (g 'r)))
- (set! (g 'rr) (* (g 'r) (g 'r)))
- (set! (g 'rr+1) (+ 1.0 (g 'rr)))
- (set! (g 'rr-1) (- 1.0 (g 'rr)))
- (set! (g 'r2) (* 2.0 (g 'r)))
- (let ((absr (abs (g 'r))))
- (set! (g 'norm) (if (< absr nearly-zero) 0.0 (/ (- 1.0 absr) (* 2.0 absr)))))
- g)
- :methods rcos-methods)
- (frequency *clm-default-frequency*) (r 0.5) fm
- (osc #f) rr norm rr+1 rr-1 r2)
-
- (define rcos
-
- (let ((documentation "(make-rcos frequency (r 0.5)) creates an rcos generator. (rcos gen (fm 0.0))
- returns many cosines spaced by frequency with amplitude r^k."))
-
- ;; from Andrews, Askey, Roy "Special Functions" 5.1.16, p243. r^k cos sum
- ;; a variant of the G&R second col 4th row
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (* (- (/ rr-1 (- rr+1 (* r2 (oscil osc fm)))) 1.0) norm)))))
-
- #|
- (with-let gen
- (let ((absr (abs r))
- (rr (* r r)))
- (if (< absr nearly-zero)
- 0.0 ; 1.0 from the formula, but we're subtracting out DC
- (* (- (/ (- 1.0 rr)
- (- (+ 1.0 rr)
- (* 2.0 r (oscil osc fm))))
- 1.0)
- (/ (- 1.0 absr) (* 2.0 absr))))))) ; normalization
- |#
-
- #|
- ;;; G&R form:
- (define* (rcos gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((absr (abs r))
- (rcosx (* r (oscil osc fm))))
- (* (- (/ (- 1.0 rcosx)
- (+ 1.0
- (* r r)
- (* -2.0 rcosx)))
- 1.0)
- (/ (- 1.0 absr) absr))))) ; normalization
- |#
-
- ;;; if r>0 we get the spike at multiples of 2pi, since the k*pi case is flipping -1 1 -1 etc
- ;;; if r<0, we get the spike at multiples of (2k-1)pi since the r sign now counteracts the cos k*pi sign
- ;;; so the peak amp is the same in the two cases, so the normalization has to use abs(r)!
- ;;; but in the k*pi case we tend to miss k*pi (whereas we never miss 0 since we start there),
- ;;; so the actual maxamp may be less than 1.0
-
-
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-rcos 100.0 :r 0.5)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (rcos gen)))))
- |#
-
- ;; this uses rkoddssb below
-
- (definstrument (stringy beg dur freq amp)
- (let ((n (floor (/ *clm-srate* (* 3 freq)))))
- (let ((start (seconds->samples beg))
- (stop (seconds->samples (+ beg dur)))
- (r (expt .001 (/ n))))
- (let ((carrier (make-rcos freq (* .5 r)))
- (clang (make-rkoddssb (* freq 2) (/ 1.618 2) r))
- (ampf (make-env '(0 0 1 1 2 .5 4 .25 10 0) :scaler amp :duration dur))
- (clangf (make-env '(0 0 .1 1 .2 .1 .3 0) :scaler (* amp .5) :duration .1))
- (rf (make-env '(0 1 1 0) :scaler (* 0.5 r) :duration dur))
- (crf (make-env '(0 1 1 0) :scaler r :duration .1)))
- (let ((set-clang-scaler (procedure-setter (clang 'mus-scaler))))
- (do ((i start (+ i 1)))
- ((= i stop))
- (set-clang-scaler clang (env crf)) ;(set! (mus-scaler clang) (env crf))
- (set! (carrier 'r) (env rf))
- (outa i (+ (* (env clangf)
- (rkoddssb clang 0.0))
- (* (env ampf)
- (rcos carrier 0.0))))))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (stringy 0 1 1000 .5))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (do ((i 0 (+ i 1)))
- ((= i 10))
- (stringy (* i .3) .3 (+ 200 (* 100 i)) .5)))
- |#
-
-
- (define rssb-methods
- (list
- (cons 'mus-scaler
- (dilambda
- (lambda (g) (g 'r))
- (lambda (g val) (set! (g 'r) (generator-clamp-r val)))))))
-
- (defgenerator (rssb
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'r) (generator-clamp-r (g 'r)))
- g)
-
- :methods rssb-methods)
- (frequency *clm-default-frequency*) (ratio 1.0) (r 0.5) (angle 0.0) fm interp)
-
-
- (define rssb
-
- (let ((documentation "(make-rssb frequency (ratio 1.0) (r 0.5)) creates an rssb generator. (rssb gen (fm 0.0))
- returns many cosines from frequency spaced by frequency * ratio with amplitude r^k."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((angle1 angle)
- (angle2 (* angle1 ratio)))
- (let ((carsin (sin angle1))
- (canrcos (cos angle1))
- (den (+ 1.0 (* r r) (* -2.0 r (cos angle2))))
- (sumsin (* r (sin angle2)))
- (sumcos (- 1.0 (* r (cos angle2)))))
- (set! angle (+ angle1 fm frequency))
- (/ (- (* carsin sumsin)
- (* canrcos sumcos))
- (* 2 den))))))))
-
-
- (define rssb-interp
-
- (let ((documentation "(make-rssb frequency (ratio 1.0) (r 0.5)) creates an rssb generator for
- rssb-interp. (rssb-interp gen fm interp) returns many cosines from frequency spaced by frequency * ratio
- with amplitude r^k. The 'interp' argument determines whether the sidebands are above (1.0) or below (-1.0) frequency."))
-
- (lambda (gen fm interp)
- (let-set! gen 'fm fm)
- (let-set! gen 'interp interp)
- (with-let gen
- (let* ((angle1 angle)
- (angle2 (* angle1 ratio)))
- (let ((carsin (sin angle1))
- (canrcos (cos angle1))
- (den (+ 1.0 (* r r) (* -2.0 r (cos angle2))))
- (sumsin (* r (sin angle2)))
- (sumcos (- 1.0 (* r (cos angle2)))))
- (set! angle (+ angle1 fm frequency))
- (/ (- (* carsin sumsin)
- (* interp canrcos sumcos))
- (* 2 den))))))))
-
-
- (definstrument (bump beg dur freq amp f0 f1 f2)
- (let ((start (seconds->samples beg))
- (stop (seconds->samples (+ beg dur)))
- (res0 (round (/ f0 freq)))
- (res1 (round (/ f1 freq)))
- (res2 (round (/ f2 freq))))
- (let ((gen1 (make-rssb (* res0 freq) (/ res0) .4))
- (gen2 (make-rssb (* res1 freq) (/ res1) .5))
- (gen3 (make-rssb (* res2 freq) (/ res2) .6))
- (ampf (make-env '(0 0 .1 1 2 .5 3 .1 4 1 5 .4 6 .1 80 0) :scaler amp :base 32 :duration dur)) ; or 50 at end
- ;; or '(0 0 .1 1 2 .5 3 .1 4 .3 5 .1 40 0)
- (pervib (make-triangle-wave 5.0 (hz->radians 3.0)))
- (ranvib (make-rand-interp 12.0 (hz->radians 2.0))))
- (do ((i start (+ i 1)))
- ((= i stop))
- (let ((vib (+ (rand-interp ranvib)
- (triangle-wave pervib))))
- (outa i (* (env ampf)
- (+ (* .85 (rssb-interp gen1 (* res0 vib) -1))
- (* .1 (rssb-interp gen2 (* res1 vib) 0))
- (* .05 (rssb-interp gen3 (* res2 vib) 1))))))))))
-
- #|
- (with-sound (:play #t)
- (do ((k 0 (+ k 1)))
- ((= k 10))
- (bump (* 0.4 k) 1 (* 16.3 (expt 2.0 (+ 3 (/ k 12)))) .5 520 1190 2390))
- (do ((k 0 (+ k 1)))
- ((= k 10))
- (let* ((freq (* 16.3 (expt 2.0 (+ 3 (/ k 12)))))
- (scl (sqrt (/ freq 120))))
- (bump (+ 4 (* 0.4 k)) 1 freq .5 (* scl 520) (* scl 1190) (* scl 2390)))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (do ((k 0 (+ k 1)))
- ((= k 10))
- (let* ((freq (* 16.3 (expt 2.0 (+ 3 (/ k 12))))) ; if oct=5 (and env end at 100), sort of hammered string effect
- (f0 520) ; "uh"
- (f1 1190)
- (f2 2390)
- ;; "ah" is good: 730 1090 2440
- ;; it might be smoother to scale the formant freqs by (sqrt (/ freq 120)) or even (expt (/ freq 120) 0.3)
- (res0 (round (/ f0 freq)))
- (res1 (round (/ f1 freq)))
- (res2 (round (/ f2 freq)))
- (gen1 (make-rssb (* res0 freq) (/ res0) .4))
- (gen2 (make-rssb (* res1 freq) (/ res1) .5))
- (gen3 (make-rssb (* res2 freq) (/ res2) .6))
- (ampf (make-env '(0 0 .1 1 2 .5 3 .1 4 1 5 .4 6 .1 80 0) :scaler .5 :base 32 :length 60000)) ; or 50 at end
- ;; or '(0 0 .1 1 2 .5 3 .1 4 .3 5 .1 40 0)
- (pervib (make-triangle-wave 5.0 (hz->radians 3.0)))
- (ranvib (make-rand-interp 12.0 (hz->radians 2.0))))
- (do ((i 0 (+ i 1)))
- ((= i 60000))
- (let ((vib (+ (rand-interp ranvib)
- (triangle-wave pervib))))
- (outa (+ i (* k 30000)) (* (env ampf)
- (+ (* .85 (rssb-interp gen1 (* res0 vib) -1))
- (* .1 (rssb-interp gen2 (* res1 vib) 0))
- (* .05 (rssb-interp gen3 (* res2 vib) 1))))))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (do ((k 0 (+ k 1)))
- ((= k 10))
- (let* ((freq (* 16.3 (expt 2.0 (+ 3 (/ k 12))))) ; froggy if oct=1 or 2 and "ah" (env end at 10 = cycling) ("er" is good too at oct=2)
- (scl (sqrt (/ freq 120)))
- (f0 (* scl 520)) ; "uh"
- (f1 (* scl 1190))
- (f2 (* scl 2390))
- ;; "ah" is good: 730 1090 2440
- (res0 (floor (/ f0 freq)))
- (res1 (floor (/ f1 freq)))
- (res2 (floor (/ f2 freq)))
- (gen1 (make-rk!ssb (* res0 freq) (/ res0) 2.4))
- (gen2 (make-rssb (* res1 freq) (/ res1) .5))
- (gen3 (make-rssb (* res2 freq) (/ res2) .6))
- (ampf (make-env '(0 0 .1 1 2 .5 3 .1 4 .3 5 .4 6 .1 40 0) :scaler .5 :base 32 :length 60000)) ; or 50 at end
- ;; or '(0 0 .1 1 2 .5 3 .1 4 .3 5 .1 40 0)
- (pervib (make-triangle-wave 5.0 (hz->radians 3.0)))
- (ranvib (make-rand-interp 12.0 (hz->radians 2.0))))
- (do ((i 0 (+ i 1)))
- ((= i 60000))
- (let ((vib (+ (rand-interp ranvib)
- (triangle-wave pervib))))
- (outa (+ i (* k 30000)) (* (env ampf)
- (+ (* .85 (rk!ssb gen1 (* res0 vib)))
- (* .1 (rssb-interp gen2 (* res1 vib) 0))
- (* .05 (rssb-interp gen3 (* res2 vib) 1))))))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (do ((k 0 (+ k 1)))
- ((= k 10))
- (let* ((freq (* 16.3 (expt 2.0 (+ 3 (/ k 12)))))
- (scl (sqrt (/ freq 120)))
- (f0 (* scl 490)) ; "uh"
- (f1 (* scl 1350))
- (f2 (* scl 2440))
- ;; "ah" is good: 730 1090 2440
- (res0 (floor (/ f0 freq)))
- (res1 (floor (/ f1 freq)))
- (res2 (floor (/ f2 freq)))
- (gen1 (make-rk!ssb (* res0 freq) (/ res0) 2))
- (gen2 (make-rk!ssb (* res1 freq) (/ res1) 3))
- (gen3 (make-rk!ssb (* res2 freq) (/ res2) 3))
- (ampf (make-env '(0 0 .1 1 2 .5 3 .1 4 .3 5 .4 6 .1 40 0) :scaler .5 :base 32 :length 30000))
- (pervib (make-triangle-wave 5.0 (hz->radians 3.0)))
- (ranvib (make-rand-interp 12.0 (hz->radians 2.0))))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (let ((vib (+ (rand-interp ranvib)
- (triangle-wave pervib))))
- (outa (+ i (* k 30000)) (* (env ampf)
- (+ (* .85 (rk!ssb gen1 (* res0 vib)))
- (* .1 (rk!ssb gen2 (* res1 vib)))
- (* .05 (rk!ssb gen3 (* res2 vib)))))))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-rssb 2000.0 (/ 103.0 2000) 0.5)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (rssb gen)))))
- |#
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; rxysin
- ;;;
- ;;; similar to rssb: (JO first)
-
- (define rxysin-methods
- (list
- (cons 'mus-scaler
- (dilambda
- (lambda (g) (g 'r))
- (lambda (g val)
- (set! (g 'r) (generator-clamp-r val))
- (set! (g 'r2) (* -2.0 (g 'r)))
- (set! (g 'rr) (+ 1.0 (* (g 'r) (g 'r)))))))))
-
- (defgenerator (rxysin
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'r) (generator-clamp-r (g 'r)))
- (set! (g 'r2) (* -2.0 (g 'r)))
- (set! (g 'rr) (+ 1.0 (* (g 'r) (g 'r))))
- g)
- :methods rxysin-methods)
- (frequency *clm-default-frequency*) (ratio 1.0) (r 0.5) (angle 0.0) fm rr r2)
-
-
- (define rxysin
-
- (let ((documentation "(make-rxysin frequency (ratio 1.0) (r 0.5)) creates an rxysin generator (similar to rssb). (rxysin gen (fm 0.0))
- returns many sines from frequency spaced by frequency * ratio with amplitude r^k."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((x angle)
- (y (* x ratio)))
- (set! angle (+ angle fm frequency))
- (/ (- (sin x)
- (* r (sin (- x y))))
- (+ rr (* r2 (cos y)))))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t :scaled-to .5)
- (let ((gen (make-rxysin 1000 0.1 0.5)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (rxysin gen)))))
- |#
-
-
- (define rxycos-methods
- (list
- (cons 'mus-scaler
- (dilambda
- (lambda (g) (g 'r))
- (lambda (g val)
- (set! (g 'r) (generator-clamp-r val))
- (set! (g 'r2) (* -2.0 (g 'r)))
- (set! (g 'rr) (+ 1.0 (* (g 'r) (g 'r))))
- (set! (g 'norm) (- 1.0 (abs (g 'r)))))))))
-
- (defgenerator (rxycos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'r) (generator-clamp-r (g 'r)))
- (set! (g 'r2) (* -2.0 (g 'r)))
- (set! (g 'rr) (+ 1.0 (* (g 'r) (g 'r))))
- (set! (g 'norm) (- 1.0 (abs (g 'r)))) ; abs for negative r
- g)
- :methods rxycos-methods)
- (frequency *clm-default-frequency*) (ratio 1.0) (r 0.5) (angle 0.0) fm norm rr r2)
-
-
- (define rxycos
-
- (let ((documentation "(make-rxycos frequency (ratio 1.0) (r 0.5)) creates an rxycos generator. (rxycos gen (fm 0.0))
- returns many cosines from frequency spaced by frequency * ratio with amplitude r^k."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((x angle)
- (y (* x ratio)))
- (set! angle (+ angle fm frequency))
- (* (/ (- (cos x)
- (* r (cos (- x y))))
- (+ rr (* r2 (cos y))))
- norm))))))
-
- #|
- (with-sound (:clipped #f :statistics #t)
- (let ((gen (make-rxycos 1000 0.1 0.5)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (rxycos gen)))))
- |#
-
-
- (define* (clamp-rxycos-r gen (fm 0.0))
- ;; in this case we need to track ratio, as well as r, since the
- ;; highest frequency goes as x+ky (y=ratio*x); we want the value of k when
- ;; we reach srate/3, then solve for the corresponding r.
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((maxr (expt cutoff (/ (floor (- (/ two-pi (* 3 ratio (+ fm frequency))) (/ ratio)))))))
- (if (>= r 0.0)
- (min r maxr)
- (max r (- maxr))))))
-
- (define safe-rxycos-methods
- (list
- (cons 'mus-scaler
- (dilambda
- (lambda (g) (g 'r))
- (lambda (g val)
- (set! (g 'r) val)
- (set! (g 'r) (clamp-rxycos-r g 0.0)))))
- (cons 'mus-frequency
- (dilambda
- (lambda (g) (radians->hz (g 'frequency)))
- (lambda (g val)
- (set! (g 'frequency) (hz->radians val))
- (set! (g 'r) (clamp-rxycos-r g 0.0))
- val)))
- (cons 'mus-offset ; ratio accessor in defgenerator
- (dilambda
- (lambda (g) (g 'ratio))
- (lambda (g val)
- (set! (g 'ratio) val)
- (set! (g 'r) (clamp-rxycos-r g 0.0))
- val)))))
-
- (defgenerator (safe-rxycos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'r) (clamp-rxycos-r g 0.0))
- g)
- :methods safe-rxycos-methods)
- (frequency *clm-default-frequency*) (ratio 1.0) (r 0.5) (angle 0.0) (cutoff 0.001) fm)
-
-
- (define safe-rxycos
-
- (let ((documentation "(make-safe-rxycos frequency (ratio 1.0) (r 0.5)) creates a safe-rxycos generator. (safe-rxycos gen (fm 0.0))
- returns many cosines from frequency spaced by frequency * ratio with amplitude r^k where 'r' is restricted to a safe value."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x angle)
- (y (* angle ratio)))
- (set! angle (+ angle fm frequency))
-
- (if (not (= fm 0.0)) ;(set! r (clamp-rxycos-r (curlet) fm))
- (let ((maxr (expt cutoff (/ (floor (- (/ two-pi (* 3 ratio (+ fm frequency))) (/ ratio)))))))
- (set! r (if (>= r 0.0) (min r maxr) (max r (- maxr))))))
-
- (* (/ (- (cos x)
- (* r (cos (- x y))))
- (+ 1.0
- (* -2.0 r (cos y))
- (* r r)))
- (- 1.0 (abs r)))))))) ; norm, abs for negative r
-
- #|
- (with-sound (:clipped #f :statistics #t)
- (let ((gen (make-safe-rxycos 1000 0.1 0.99)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (safe-rxycos gen)))))
- |#
-
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; inf cosines scaled by e^-r (special case of rcos): ercos, erssb
-
- ;;; sndclm.html G&R second col last row (with normalization)
-
- (define ercos-methods
- (list
- (cons 'mus-frequency
- (dilambda
- (lambda (g) (mus-frequency (g 'osc)))
- (lambda (g val) (set! (mus-frequency (g 'osc)) val))))
- (cons 'mus-phase
- (dilambda
- (lambda (g) (mus-phase (g 'osc)))
- (lambda (g val) (set! (mus-phase (g 'osc)) val))))))
-
- (defgenerator (ercos
- :make-wrapper (lambda (g)
- (if (<= (g 'r) 0.0) (set! (g 'r) 0.00001))
- (set! (g 'cosh-t) (cosh (g 'r)))
- (set! (g 'osc) (make-polywave (g 'frequency) (list 0 (g 'cosh-t) 1 -1.0) mus-chebyshev-second-kind))
- (let ((exp-t (exp (- (g 'r)))))
- (set! (g 'offset) (/ (- 1.0 exp-t) (* 2.0 exp-t)))
- (set! (g 'scaler) (* (sinh (g 'r)) (g 'offset))))
- g)
- :methods ercos-methods)
- (frequency *clm-default-frequency*) (r 1.0) fm
- (osc #f) scaler offset cosh-t)
-
-
- (define ercos
-
- (let ((documentation "(make-ercos frequency (r 0.5)) creates an ercos generator (a special case of rcos). (ercos gen (fm 0.0))
- returns many cosines from frequency with amplitude e^(-kr)."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (- (/ scaler (polywave osc fm)) offset)))))
-
- #|
- (with-let gen
- (- (/ scaler
- (- cosh-t (oscil osc fm)))
- offset)))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-ercos 100 :r 1.0)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (ercos gen)))))
- |#
-
- (definstrument (ercoser beg dur freq amp r)
- (let ((start (seconds->samples beg))
- (stop (seconds->samples (+ beg dur)))
- (gen (make-ercos freq :r r))
- (t-env (make-env '(0 .1 1 2) :duration dur)))
- (with-let
- (varlet gen
- (cons 'start start) (cons 'stop stop) (cons 'amp amp) (cons 't-env t-env) (cons 'gen gen))
- (do ((i start (+ i 1)))
- ((= i stop))
- (set! r (env t-env))
- (set! cosh-t (cosh r))
- (set! ((mus-data osc) 0) cosh-t)
- (let ((exp-t (exp (- r))))
- (set! offset (/ (- 1.0 exp-t) (* 2.0 exp-t)))
- (set! scaler (* (sinh r) offset)))
- (outa i (* amp (ercos gen)))))))
-
- #|
- ;;; same, but slightly slower
- (definstrument (ercoser beg dur freq amp r)
- (let ((start (seconds->samples beg))
- (stop (seconds->samples (+ beg dur)))
- (gen (make-ercos freq :r r))
- (t-env (make-env '(0 .1 1 2) :duration dur)))
- (do ((i start (+ i 1)))
- ((= i stop))
- (let ((r (env t-env)))
- (set! (gen 'r) r)
- (set! (gen 'cosh-t) (cosh r))
- (set! ((mus-data (gen 'osc)) 0) (gen 'cosh-t))
- (let ((exp-t (exp (- r))))
- (set! (gen 'offset) (/ (- 1.0 exp-t) (* 2.0 exp-t)))
- (set! (gen 'scaler) (* (sinh r) (gen 'offset))))
- (outa i (* amp (ercos gen)))))))
- |#
-
- #|
- ;; change "t" during note -- smoothly changing sum-of-cosines spectra (damped "lute-stop" effect)
- (with-sound (:play #t)
- (ercoser 0 1 100 .5 0.1))
- |#
-
-
- (defgenerator (erssb
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (ratio 1.0) (r 0.5) (angle 0.0) fm)
-
-
- (define erssb
-
- (let ((documentation "(make-erssb frequency (ratio 1.0) (r 0.5)) creates an erssb generator (a special case of rssb). (erssb gen (fm 0.0))
- returns many sinusoids from frequency spaced by frequency * ratio with amplitude e^(-kr)."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((cx angle)
- (mx (* cx ratio)))
- (let ((cxx (- cx mx))
- (ccmx (- (cosh r) (cos mx))))
- (set! angle (+ angle fm frequency))
- (if (< (abs ccmx) nearly-zero)
- 1.0
- (/ (- (* (cos cxx)
- (- (/ (sinh r) ccmx)
- 1.0))
- (* (sin cxx)
- (/ (sin mx) ccmx)))
- (* 2.0 (- (/ 1.0 (- 1.0 (exp (- r)))) 1.0)))))))))) ; normalization
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-erssb 1000.0 0.1 1.0)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (erssb gen)))))
- |#
-
-
-
-
- #|
- ;;; --------------------------------------------------------------------------------
- ;;; removed 8-May-08 -- not useful or different from (for example) rk!cos
-
- ;;; inf sinusoids scaled by r^2: r2cos, r2sin, r2ssb
-
- ;;; Jolley second col second row (first row is cos tweak of this)
-
- (defgenerator (r2sin
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (if (>= (* (g 'r) (g 'r)) 1.0)
- (set! (g 'r) 0.9999999))
- g))
- (frequency *clm-default-frequency*) (r 0.5) (angle 0.0) fm)
-
-
- (define r2sin
-
- (let ((documentation "(make-r2sin frequency (r 0.5)) creates an r2sin generator. (r2sin gen (fm 0.0))
- returns many even-numbered sines from frequency with amplitude r^(2k)/(2k)!."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((x angle))
- (set! angle (+ angle fm frequency))
- (* (sinh (* r (cos x)))
- (sin (* r (sin x)))))))))
-
-
- ;;; even harmonics, but we can't push the upper partials past the (2k)! range, so not very flexible
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-r2sin 100.0 :r 0.5)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (r2sin gen)))))
-
-
-
- (defgenerator (r2cos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (if (>= (* (g 'r) (g 'r)) 1.0)
- (set! (g 'r) 0.9999999))
- g))
- (frequency *clm-default-frequency*) (r 0.5) (angle 0.0) fm)
-
-
- (define r2cos
-
- (let ((documentation "(make-r2cos frequency (r 0.5)) creates an r2cos generator. (r2cos gen (fm 0.0))
- returns many even-numbered cosines from frequency with amplitude r^(2k)/(2k)!."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((x angle))
- (set! angle (+ angle fm frequency))
- (/ (- (* (cosh (* r (cos x)))
- (cos (* r (sin x))))
- 1.0) ; omit DC
- (- (cosh r) 1.0))))))) ; normalize
-
- ;;; odd harmonics, but we can't push the upper partials past the (2k)! range, so not very flexible
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-r2cos 100.0 :r 0.5)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (r2cos gen)))))
-
-
-
- (defgenerator (r2ssb
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (ratio 1.0) (r 0.5) (angle 0.0) fm)
-
-
- (define r2ssb
-
- (let ((documentation "(make-r2ssb frequency (ratio 1.0) (r 0.5)) creates an r2ssb generator. (r2ssb gen (fm 0.0))
- returns many even-numbered sinusoids from frequency spaced by frequency * ratio, if that makes any sense, with amplitude r^(2k)/(2k)!."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((cx angle)
- (mx (* cx ratio))
- (a r)
- (asinx (* a (sin mx)))
- (acosx (* a (cos mx))))
- (set! angle (+ angle fm frequency))
- (/ (- (* (cos cx)
- (cosh acosx)
- (cos asinx))
- (* (sin cx)
- (sinh acosx)
- (sin asinx)))
- (cosh a))))))) ; normalization
-
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-r2ssb 1000.0 0.1 0.5)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (r2ssb gen)))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-r2ssb 1000.0 0.1 0.5))
- (vib (make-oscil 5)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (r2ssb gen (* (hz->radians 100.0) (oscil vib)))))))
- |#
-
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; inf odd cosines scaled by e^-r: eoddcos
-
- ;;; Jolley first col second row
- ;;; heads toward a square wave as "r" -> 0.0 (odd harmonics, 1/k amp)
-
- ;;; this is the cos side of rkoddssb with r=e^-a
-
- (define eoddcos-methods
- (list
- (cons 'mus-frequency
- (dilambda
- (lambda (g) (mus-frequency (g 'osc)))
- (lambda (g val) (set! (mus-frequency (g 'osc)) val))))
- (cons 'mus-phase
- (dilambda
- (lambda (g) (mus-phase (g 'osc)))
- (lambda (g val) (set! (mus-phase (g 'osc)) val))))))
-
- (defgenerator (eoddcos
- :make-wrapper (lambda (g)
- (set! (g 'osc) (make-oscil (g 'frequency) (* 0.5 pi)))
- g)
- :methods eoddcos-methods)
- (frequency *clm-default-frequency*) (r 1.0) fm
- (osc #f))
-
-
- (define eoddcos
-
- (let ((documentation "(make-eoddcos frequency (r 0.5)) creates an eoddcos generator. (eoddcos gen (fm 0.0))
- returns many cosines from spaced by frequency with amplitude e^(-r)."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((sinha (sinh r)))
- (if (zero? sinha)
- 0.0 ; just a guess
- (/ (atan (oscil osc fm) sinha)
- (atan 1.0 sinha)))))))) ; normalization
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-eoddcos 400.0 :r 1.0)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (eoddcos gen)))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-eoddcos 400.0 :r 0.0))
- (a-env (make-env '(0 0 1 1) :length 10000)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (set! (gen 'r) (env a-env))
- (outa i (eoddcos gen)))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen1 (make-eoddcos 400.0 :r 0.0))
- (gen2 (make-oscil 400.0))
- (a-env (make-env '(0 0 1 1) :length 10000)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (set! (gen 'r1) (env a-env))
- (outa i (* .5 (eoddcos gen1 (* .1 (oscil gen2))))))))
- |#
-
-
- #|
- ;;; --------------------------------------------------------------------------------
- ;;; removed 6-May-08
-
- ;;; inf odd cosines scaled by complicated mess: koddcos
-
- ;;; Jolley first col 5th row
-
- (define make-koddcos make-oscil)
-
- (define koddcos
-
- (let ((documentation "(make-koddcos frequency) creates a koddcos generator. (koddcos gen (fm 0.0))
- returns many cosines from spaced by frequency with amplitude too messy to write down, and the output looks wrong anyway."))
-
- (lambda* (gen (fm 0.0))
- (let ((arg (* 2.0 (oscil gen fm))))
- (if (>= arg 0.0)
- (/ (acos (- 1.0 arg)) pi)
- (/ (acos (+ 1.0 arg)) (- pi)))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-koddcos 400.0)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (* .3 (koddcos gen))))))
-
- ;;; as printed in J, this is not usable -- 1-2sin can be 3 so acos will be complex -- looks like we're missing: x < pi
- ;;; we get odd harmonics but wrong amps
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; inf cosines scaled by r^k/k: rkcos, rksin, rkssb
-
- ;;; G&R second col 6th row, also J 536
- ;;; r^k/k -- this sums to ln(1/(1-x)) if x<1 (J 118)
-
- (define rkcos-methods
- (list
- (cons 'mus-frequency
- (dilambda
- (lambda (g) (mus-frequency (g 'osc)))
- (lambda (g val) (set! (mus-frequency (g 'osc)) val))))
- (cons 'mus-scaler
- (dilambda
- (lambda (g) (g 'r))
- (lambda (g val) (set! (g 'r) (generator-clamp-r val)))))
- (cons 'mus-phase
- (dilambda
- (lambda (g) (mus-phase (g 'osc)))
- (lambda (g val) (set! (mus-phase (g 'osc)) val))))))
-
- (defgenerator (rkcos
- :make-wrapper (lambda (g)
- (set! (g 'osc) (make-oscil (g 'frequency) (* 0.5 pi)))
- (set! (g 'r) (generator-clamp-r (g 'r))) ; or clip at 0.0?
- (set! (g 'norm) (log (- 1.0 (abs (g 'r)))))
- g)
- :methods rkcos-methods)
- (frequency *clm-default-frequency*) (r 0.5) norm fm
- (osc #f))
-
- ;;; not very flexible, and very similar to others in the r^k mold
-
-
- (define rkcos
-
- (let ((documentation "(make-rkcos frequency (r 0.5)) creates an rkcos generator. (rkcos gen (fm 0.0))
- returns many cosines from spaced by frequency with amplitude (r^k)/k."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((cs (oscil osc fm)))
- (/ (* 0.5 (log (+ 1.0 (* -2.0 r cs) (* r r))))
- norm))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-rkcos 440.0 :r 0.5)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (rkcos gen)))))
- |#
-
-
- (define rksin-methods
- (list
- (cons 'mus-scaler
- (dilambda
- (lambda (g) (g 'r))
- (lambda (g val) (set! (g 'r) (generator-clamp-r val)))))))
-
- (defgenerator (rksin
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g)
- :methods rksin-methods)
- (frequency *clm-default-frequency*) (r 0.5) (angle 0.0) fm)
-
- ;;; normalization based on 0 of derivative of atan arg (for max) at cos x = r,
- ;;; so we get a maxamp here of (atan (/ (* r (sin (acos r))) (- 1.0 (* r r))))
-
- (define rksin
-
- (let ((documentation "(make-rksin frequency (r 0.5)) creates an rksin generator. (rksin gen (fm 0.0))
- returns many sines from spaced by frequency with amplitude (r^k)/k."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x angle))
- (set! angle (+ angle fm frequency))
- (/ (atan (* r (sin x))
- (- 1.0 (* r (cos x))))
- (atan (* r (sin (acos r))) ; normalization
- (- 1.0 (* r r)))))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-rksin 100.0 :r 0.5)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (rksin gen)))))
- |#
-
-
-
- (define rkssb-methods
- (list
- (cons 'mus-scaler
- (dilambda
- (lambda (g) (g 'r))
- (lambda (g val) (set! (g 'r) (generator-clamp-r val)))))))
-
- (defgenerator (rkssb
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g)
- :methods rkssb-methods)
- (frequency *clm-default-frequency*) (ratio 1.0) (r 0.5) (angle 0.0) fm)
-
-
- (define rkssb
-
- (let ((documentation "(make-rkssb frequency (ratio 1.0) (r 0.5)) creates an rkssb generator. (rkssb gen (fm 0.0))
- returns many sinusoids from frequency from spaced by frequency * ratio with amplitude (r^k)/k."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((cx angle)
- (mx (* cx ratio)))
- (let ((cxx (* (- 1.0 ratio) cx))
- (rcosmx (* r (cos mx))))
- (set! angle (+ angle fm frequency))
- (/ (- (* (cos cxx)
- -0.5 (log (+ 1.0 (* -2.0 rcosmx) (* r r))))
- (* (sin cxx)
- (atan (* r (sin mx))
- (- 1.0 rcosmx))))
- (- (log (- 1.0 (abs r))))))))))) ; normalization
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t :scaled-to .5)
- (let ((gen (make-rkssb 1000.0 0.5 :r 0.75)) ; (make-rkssb 300.0 3.0 :r 0.5)
- (ampf (make-env '(0 0 1 1 2 1 3 0) :length 20000)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (* (env ampf)
- (rkssb gen))))))
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; inf cosines scaled by r^k/k!: rk!cos, rk!ssb
-
- ;;; G&R second col third from last (simplified)
-
- (define rk!cos-methods
- (list
- (cons 'mus-phase
- (dilambda
- (lambda (g) (g 'angle))
- (lambda (g val) (set! (g 'angle) val))))))
-
- (defgenerator (rk!cos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'norm) (/ 1.0 (- (exp (abs r)) 1.0)))
- g)
- :methods rk!cos-methods)
- (frequency *clm-default-frequency*) (r 0.5) (angle 0.0) fm norm)
-
-
- (define rk!cos
-
- (let ((documentation "(make-rk!cos frequency (r 0.5)) creates an rk!cos generator. (rk!cos gen (fm 0.0))
- returns many cosines spaced by frequency with amplitude (r^k)/k!."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x angle))
- (set! angle (+ angle fm frequency))
- (* (- (* (exp (* r (cos x)))
- (cos (* r (sin x))))
- 1.0) ; omit DC
- norm))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-rk!cos 440.0 :r 0.5)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (* .5 (rk!cos gen))))))
- |#
-
- ;;; the k! denominator dominates, so r * ratio = formant center approximately; (n!)^(1/n)
- ;;; so freq=100, r=30, the center of the spectrum is around 3kHz:
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t :scaled-to .5)
- (let ((gen (make-rk!cos 100.0 :r 40.0))
- (r 40.0)
- (incr (/ -40.0 100000)))
- (do ((i 0 (+ i 1)))
- ((= i 100000))
- (set! (gen 'r) r)
- (set! r (+ r incr))
- (outa i (rk!cos gen)))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-rk!cos 300.0 :r 10.0))
- (ampf (make-env '(0 0 .1 1 .2 1 3 .5 5 .25 10 0) :scaler .5 :length 10000))
- (r 10.0)
- (incr (/ -10.0 10000)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (set! (gen 'r) r)
- (set! r (+ r incr))
- (outa i (* (env ampf) (rk!cos gen))))))
-
- (with-sound (:clipped #f :statistics #t :play #t :scaled-to .5)
- (let ((gen (make-rk!cos 1000.0 :r 8.0))
- (frqf (make-env '(0 1 1 0) :base 32 :scaler (hz->radians 1000) :length 10000)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (rk!cos gen (env frqf))))))
-
- (with-sound (:clipped #f :statistics #t :play #t :scaled-to .5)
- (let ((gen (make-rk!cos 3000.0 :r 1.0)) (ampf (make-env '(0 0 1 1 10 1 11 0) :length 10000))
- (frqf (make-env '(0 1 1 0 2 .25 3 0) :base 3 :scaler (hz->radians 2000) :length 10000)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (* (env ampf) (rk!cos gen (env frqf)))))))
-
- (with-sound (:play #t :scaled-to .5)
- (do ((k 0 (+ k 1)))
- ((= k 6))
- (let ((gen (make-rk!cos 3000.0 :r 0.6)) (ampf (make-env '(0 0 1 1 2 1 3 0) :length 3000))
- (frqf (make-env '(0 0 1 1) :base .1 :scaler (hz->radians 2000) :length 3000))) ; '(0 .5 1 1 2 0 3 0) '(0 1 1 0 2 1 6 -1)
- (do ((i 0 (+ i 1)))
- ((= i 3000))
- (outa (+ i (* k 4000))
- (* (env ampf)
- (rk!cos gen (env frqf))))))))
-
- (with-sound (:clipped #f :statistics #t :play #t :scaled-to .5)
- (do ((k 0 (+ k 1)))
- ((= k 6))
- (let ((gen (make-rk!cos 1000.0 :r 1.0)) (ampf (make-env '(0 0 1 1 2 1 3 0) :length 3000))
- (frqf (make-env '(0 .9 1 1 2 -1) :base .1 :scaler (hz->radians 500) :length 3000)))
- (do ((i 0 (+ i 1)))
- ((= i 3000))
- (outa (+ i (* k 10000)) (* (env ampf) (rk!cos gen (env frqf))))))))
-
- (with-sound (:clipped #f :statistics #t :play #t :scaled-to .5)
- (do ((k 0 (+ k 1)))
- ((= k 6))
- (let ((gen (make-rk!cos 500.0 :r 1.5)) (ampf (make-env '(0 0 1 1 2 1 3 0) :length 3000))
- (frqf (make-env '(0 1 1 1 2 -1) :base .5 :scaler (hz->radians 400) :length 3000)))
- (do ((i 0 (+ i 1)))
- ((= i 3000))
- (outa (+ i (* k 10000)) (* (env ampf) (rk!cos gen (env frqf))))))))
- |#
-
-
- (defgenerator (rk!ssb
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (ratio 1.0) (r 1.0) (angle 0.0) fm)
-
-
- (define rk!ssb
-
- (let ((documentation "(make-rk!ssb frequency (ratio 1.0) (r 0.5)) creates an rk!ssb generator. (rk!ssb gen (fm 0.0))
- returns many sinusoids from frequency spaced by frequency * ratio with amplitude (r^k)/k!."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((cx angle)
- (mx (* cx ratio)))
- (let ((ercosmx (exp (* r (cos mx))))
- (rsinmx (* r (sin mx))))
- (set! angle (+ angle fm frequency))
- (/ (- (* (cos cx) ercosmx (cos rsinmx))
- (* (sin cx) ercosmx (sin rsinmx)))
- (exp (abs r))))))))) ; normalization (keeping DC term here to get "carrier")
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t :scaled-to .5)
- (let ((gen (make-rk!ssb 1000.0 0.1 :r 0.5)) ; (make-rk!ssb 200.0 3.0 :r 2)
- (ampf (make-env '(0 0 1 1 2 1 3 0) :length 20000)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (* (env ampf) (rk!ssb gen))))))
-
- ; (make-rk!ssb 0.0 120.0 :r 15) gives a widely separated wave-train of pulses
- ; so (make-rk!ssb 0.0 40.0 :r 70) is insecty (:r 100)
- ; (make-rk!ssb 0.0 10.0 :r 100) -- some bird? (make-rk!ssb 0.0 15.0 :r 300)
- ; (make-rk!ssb 1000.0 25.0 :r 10) (make-rk!ssb 3000.0 25.0 :r 100) -- another bird (5000)
- |#
-
- (definstrument (bouncy beg dur freq amp (bounce-freq 5) (bounce-amp 20))
- (let ((len (seconds->samples dur))
- (start (seconds->samples beg)))
- (let ((gen (make-rk!ssb (* freq 4) 1/4 :r 1.0))
- (gen1 (make-oscil bounce-freq))
- (bouncef (make-env '(0 1 1 0) :base 32 :scaler bounce-amp :duration 1.0))
- (rf (make-env (list 0 0 1 1 (max 2.0 dur) 0) :base 32 :scaler 3 :duration dur))
- (ampf (make-env (list 0 0 .01 1 .03 1 1 .15 (max 2 dur) 0.0) :base 32 :scaler amp :duration dur))
- (stop (+ start len))
- (fv (make-float-vector len)))
- (do ((i 0 (+ i 1)))
- ((= i len))
- (float-vector-set! fv i (+ (env rf) (abs (* (env bouncef) (oscil gen1))))))
- (do ((i start (+ i 1))
- (j 0 (+ j 1)))
- ((= i stop))
- (set! (gen 'r) (float-vector-ref fv j))
- (outa i (* (env ampf)
- (rk!ssb gen)))))))
-
- #|
- (with-sound (:statistics #t :play #t :clipped #f)
- (bouncy 0 2 300 .5 5 10))
-
- (with-sound (:statistics #t :play #t :clipped #f)
- (bouncy 0 2 200 .5 3 2))
- |#
-
-
-
- #|
- ;;; --------------------------------------------------------------------------------
- ;;; rxyk!cos
- ;;; moved to clm.c 18-Apr-13)
-
- (defgenerator (rxyk!sin
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (ratio 1.0) (r 0.5) (angle 0.0) fm)
-
-
- (define rxyk!sin
-
- (let ((documentation "(make-rxyk!sin frequency (ratio 1.0) (r 0.5)) creates an rxyk!sin generator. (rxyk!sin gen (fm 0.0))
- returns many sines from frequency spaced by frequency * ratio with amplitude r^k/k!."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((x angle)
- (y (* x ratio)))
- (set! angle (+ angle fm frequency))
- (/ (* (exp (* r (cos y)))
- (sin (+ x (* r (sin y))))) ; was cos by mistake (18-Apr-13)
- (exp (abs r))))))))
-
-
- (with-sound (:clipped #f :statistics #t :play #t :scaled-to .5)
- (let ((gen (make-rxyk!sin 1000 0.1 0.5)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (rxyk!sin gen)))))
-
-
-
- (defgenerator (rxyk!cos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'ar) (/ 1.0 (exp (abs (g 'r)))))
- g))
- (frequency *clm-default-frequency*) (ratio 1.0) (r 0.5) (angle 0.0) fm ar)
-
-
- (define rxyk!cos
-
- (let ((documentation "(make-rxyk!cos frequency (ratio 1.0) (r 0.5)) creates an rxyk!cos generator. (rxyk!cos gen (fm 0.0))
- returns many cosines from frequency spaced by frequency * ratio with amplitude r^k/k!."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((x angle)
- (y (* x ratio)))
- (set! angle (+ angle fm frequency))
- (* (exp (* r (cos y)))
- (cos (+ x (* r (sin y))))
- ar))))))
-
-
- (with-sound (:clipped #f :statistics #t :play #t :scaled-to .5)
- (let ((gen (make-rxyk!cos 1000 0.1 0.5)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (rxyk!cos gen)))))
- |#
-
-
- (definstrument (brassy beg dur freq amp ampf freqf gliss)
- (let ((pitch-time .05)
- (amp-time .1))
- (let ((gen (make-rxyk!cos freq :r 0.0))
- (start (seconds->samples beg))
- (end (seconds->samples (+ beg dur)))
- (amp-env (make-env ampf :duration dur :scaler amp))
- (pitch-env (make-env freqf :scaler (/ gliss freq) :duration dur))
- (slant (make-moving-average (seconds->samples pitch-time)))
- (vib (make-polywave 5 (list 1 (hz->radians 4.0)) mus-chebyshev-second-kind))
- (harmfrq 0.0)
- (harmonic 0)
- (dist 0.0))
- (set! (mus-increment slant) (* (hz->radians freq) (mus-increment slant)))
- (do ((i start (+ i 1)))
- ((= i end))
- (set! harmfrq (env pitch-env))
- (set! harmonic (floor harmfrq))
- (set! dist (abs (- harmfrq harmonic)))
- (set! (mus-scaler gen) (* 20.0 (min amp-time dist (- 1.0 dist))))
- (outa i (* (env amp-env)
- (rxyk!cos gen (+ (moving-average slant harmonic)
- (polywave vib)))))))))
- #|
- (with-sound (:statistics #t :play #t)
- (brassy 0 4 50 .05 '(0 0 1 1 10 1 11 0) '(0 1 1 0) 1000))
- |#
-
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; inf cosines scaled by complicated mess: r2k!cos
-
- ;;; from Askey "Ramanujan and Hypergeometric Series" in Berndt and Rankin "Ramanujan: Essays and Surveys" p283
- ;;;
- ;;; this gives a sum of cosines of decreasing amp where the "k" parameter determines
- ;;; the "index" (in FM nomenclature) -- higher k = more cosines
-
- (define r2k!cos-methods
- (list
- (cons 'mus-frequency
- (dilambda
- (lambda (g) (mus-frequency (g 'osc)))
- (lambda (g val) (set! (mus-frequency (g 'osc)) val))))
- (cons 'mus-phase
- (dilambda
- (lambda (g) (mus-phase (g 'osc)))
- (lambda (g val) (set! (mus-phase (g 'osc)) val))))
- (cons 'mus-copy copy)))
-
- (defgenerator (r2k!cos
- :make-wrapper (lambda (g)
- (set! (g 'rr1) (+ 1.0 (* (g 'r) (g 'r))))
- (set! (g 'r2) (* 2.0 (abs (g 'r))))
- (set! (g 'norm) (expt (- (g 'rr1) (g 'r2)) (g 'k)))
- (set! (g 'osc) (make-polywave (g 'frequency) (list 0 (g 'rr1) 1 (- (g 'r2))) mus-chebyshev-second-kind))
- (set! (g 'k) (- (g 'k)))
- g)
- :methods r2k!cos-methods)
- (frequency *clm-default-frequency*) (r 0.5) (k 0.0) rr1 r2 norm fm
- (osc #f))
-
-
- (define r2k!cos
-
- (let ((documentation "(make-2rk!cos frequency (r 0.5) (k 0.0)) creates an r2k!cos generator. (r2k!cos gen (fm 0.0))
- returns many cosines spaced by frequency with amplitude too messy to write down."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (* (expt (polywave osc fm) k) norm)))))
-
- #|
- ;;; old form
- (with-let gen
- (let ((rr1 (+ 1.0 (* r r)))
- (r2 (* 2 (abs r)))) ; abs for negative r
- (* (expt (- rr1
- (* r2 (oscil osc fm)))
- (- k))
- (expt (- rr1 r2) k))))) ; amplitude normalization
- |#
-
- ;;; there is still noticable DC offset if r != 0.5 -- could precompute it and subtract (and there's lots of DC anyway)
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-r2k!cos 440.0 :r 0.5 :k 3.0)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (r2k!cos gen)))))
-
- (with-sound (:clipped #f :statistics #t :play #t :scaled-to .5)
- (let ((gen (make-r2k!cos 440.0 :r 0.5 :k 3.0))
- (indf (make-env '(0 1 1 0 10 0) :length 80000 :scaler 10.0 :offset 1)))
- (do ((i 0 (+ i 1)))
- ((= i 80000))
- (set! (gen 'k) (env indf))
- (outa i (r2k!cos gen)))))
- |#
-
- (definstrument (pianoy beg dur freq amp)
- (let ((gen (make-r2k!cos freq :r 0.5 :k 3.0))
- (ampf (make-env (list 0 0 .01 1 .03 1 1 .15 (max 2 dur) 0.0) :base 32 :scaler amp :duration dur))
- (start (seconds->samples beg))
- (stop (seconds->samples (+ beg dur))))
- (do ((i start (+ i 1)))
- ((= i stop))
- (outa i (* (env ampf)
- (r2k!cos gen))))))
-
- ;;; (with-sound (:statistics #t :play #t :clipped #f) (pianoy 0 3 100 .5))
- ;;; this can be combined with bouncy-like changes to get an evolving sound
-
- (definstrument (pianoy1 beg dur freq amp (bounce-freq 5) (bounce-amp 20))
- (let ((len (seconds->samples dur))
- (start (seconds->samples beg)))
- (let ((gen (make-r2k!cos freq :r 0.5 :k 3.0))
- (gen1 (make-oscil bounce-freq))
- (bouncef (make-env '(0 1 1 0) :base 32 :scaler bounce-amp :duration 1.0))
- (rf (make-env (list 0 0 1 1 (max 2.0 dur) 0) :base 32 :scaler .1 :offset .25 :duration dur))
- (ampf (make-env (list 0 0 .01 1 .03 1 1 .15 (max 2 dur) 0.0) :base 32 :scaler amp :duration dur))
- (stop (+ start len))
- (fv (make-float-vector len)))
- (do ((i 0 (+ i 1)))
- ((= i len))
- (float-vector-set! fv i (+ (env rf) (abs (* (env bouncef) (oscil gen1))))))
-
- (do ((i start (+ i 1))
- (j 0 (+ j 1)))
- ((= i stop))
- (set! (gen 'r) (float-vector-ref fv j))
- (outa i (* (env ampf)
- (r2k!cos gen)))))))
-
- #|
- (with-sound (:statistics #t :play #t :clipped #f)
- (pianoy1 0 4 200 .5 1 .1))
- |#
-
- (definstrument (pianoy2 beg dur freq amp)
- (let ((gen (make-r2k!cos freq :r 0.5 :k 3.0))
- (ampf (make-env (list 0 0 .01 1 .03 1 1 .15 (max 2 dur) 0.0) :base 32 :scaler amp :duration dur))
- (knock (make-fmssb 10.0 20.0 :index 1.0))
- (kmpf (make-env '(0 0 1 1 3 1 100 0) :base 3 :scaler .05 :length 30000))
- (indf (make-env '(0 1 1 0) :length 30000 :base 3 :scaler 10))
- (start (seconds->samples beg))
- (stop (seconds->samples (+ beg dur))))
- (do ((i start (+ i 1)))
- ((= i stop))
- (set! (knock 'index) (env indf))
- (outa i (+ (* (env ampf)
- (r2k!cos gen))
- (* (env kmpf)
- (fmssb knock 0.0)))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (pianoy2 0 1 100 .5))
- |#
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; inf sines scaled by 1/2^k: k2sin
-
- ;;; Jolley first col first row
-
- ;;; not flexible -- very similar to several others
-
- (defgenerator (k2sin
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (angle 0.0) fm)
-
-
- (define k2sin
-
- (let ((documentation "(make-k2sin frequency) creates a k2sin generator. (k2sin gen (fm 0.0))
- returns many sines spaced by frequency with amplitude 1/(2^k)."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x angle))
- (set! angle (+ angle fm frequency))
- (/ (* 3.0 (sin x)) ; 3 rather than 4 for normalization
- (- 5.0 (* 4.0 (cos x)))))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-k2sin 440.0)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (k2sin gen)))))
- |#
-
-
-
- ;;; using the second Sansone formula, we get the sum of cos case by using a=-5b/4 or 3/(4cosx-5)
-
- (defgenerator (k2cos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (angle 0.0) fm)
-
-
- (define k2cos
-
- (let ((documentation "(make-k2cos frequency) creates a k2cos generator. (k2cos gen (fm 0.0))
- returns many cosines spaced by frequency with amplitude 1/(2^k)."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x angle))
- (set! angle (+ angle fm frequency))
- (* 0.5 (- (/ 3.0
- (- 5.0 (* 4.0 (cos x))))
- 1.0)))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t :scaled-to .5)
- (let ((gen (make-k2cos 440.0)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (k2cos gen)))))
- |#
-
-
-
- (defgenerator (k2ssb
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (ratio 1.0) (angle 0.0) fm)
-
-
- (define k2ssb
-
- (let ((documentation "(make-k2ssb frequency (ratio 1.0)) creates a k2ssb generator. (k2ssb gen (fm 0.0))
- returns many sinusoids from frequency spaced by frequency * ratio with amplitude 1/(2^k)."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((cx angle)
- (mx (* cx ratio)))
- (set! angle (+ angle fm frequency))
- (/ (- (* 3 (cos cx))
- (* (sin cx) 4.0 (sin mx)))
- (* 3.0 (- 5.0 (* 4.0 (cos mx))))))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-k2ssb 1000.0 0.1)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (* .5 (k2ssb gen))))))
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
-
-
- ;;; this was inspired by Andrews, Askey, Roy "Special Functions" p396, but there's an error somewhere...
- ;;; it produces sum r^k sin(2k-1)x
- ;;; (not normalized)
-
- (define dblsum-methods
- (list
- (cons 'mus-frequency
- (dilambda
- (lambda (g) (radians->hz (* 0.5 (g 'frequency))))
- (lambda (g val) (set! (g 'frequency) (hz->radians (* 2 val))) val)))))
-
- (defgenerator (dblsum
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (* 2 (g 'frequency))))
- g)
- :methods dblsum-methods)
- (frequency *clm-default-frequency*) (r 0.5) (angle 0.0) fm)
-
-
- (define dblsum
-
- (let ((documentation "(make-dblsum frequency (r 0.5)) creates a dblsum generator. (dblsum gen (fm 0.0))
- returns many sines from frequency spaced by frequency * (2k -1) with amplitude r^k (this is buggy)."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x angle))
- (set! angle (+ angle fm frequency))
- (/ (* (+ 1 r) (sin (* 0.5 x)))
- (* (- 1 r) (+ 1.0 (* -2.0 r (cos x)) (* r r)))))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-dblsum 100 0.5)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (* .25 (dblsum gen))))))
- |#
-
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; inf odd sinusoids scaled by r^odd-k/odd-k: rkoddssb
-
- ;;; G&R second col rows 7&8 (odd r^k/k)
-
- (define rkoddssb-methods
- (list
- (cons 'mus-scaler
- (dilambda
- (lambda (g) (g 'r))
- (lambda (g val)
- (set! (g 'r) (generator-clamp-r val))
- (set! (g 'rr1) (+ 1.0 (* (g 'r) (g 'r))))
- (set! (g 'norm) (/ 1.0 (- (log (+ 1.0 (g 'r))) (log (- 1.0 (g 'r)))))))))))
-
- (defgenerator (rkoddssb
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'r) (generator-clamp-r (g 'r)))
- (set! (g 'rr1) (+ 1.0 (* (g 'r) (g 'r))))
- (set! (g 'norm) (/ 1.0 (- (log (+ 1.0 (g 'r))) (log (- 1.0 (g 'r))))))
- g)
- :methods rkoddssb-methods)
- (frequency *clm-default-frequency*) (ratio 1.0) (r 0.5) (angle 0.0) fm rr1 norm)
-
-
- (define rkoddssb
-
- (let ((documentation "(make-rkoddssb frequency (ratio 1.0) (r 0.5)) creates an rkoddssb generator. (rkoddssb gen (fm 0.0))
- returns many sinusoids from frequency spaced by frequency * 2 * ratio with amplitude (r^(2k-1))/(2k-1)."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((cx angle)
- (mx (* cx ratio)))
- (let ((cxx (- cx mx))
- (cmx (* 2.0 r (cos mx))))
- (set! angle (+ angle fm frequency))
- (* (- (* (cos cxx)
- 0.5
- (log (/ (+ rr1 cmx) (- rr1 cmx))))
- (* (sin cxx)
- (atan (* 2.0 r (sin mx))
- (- 1.0 (* r r)))))
- norm)))))))
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-rkoddssb 1000.0 0.1 0.5)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (* .5 (rkoddssb gen))))))
- |#
-
- (definstrument (glassy beg dur freq amp)
- (let ((r (expt .001 (/ (floor (/ *clm-srate* (* 3 freq)))))))
- (let ((start (seconds->samples beg))
- (stop (seconds->samples (+ beg dur)))
- (clang (make-rkoddssb (* freq 2) (/ 1.618 2) r))
- (clangf (make-env (list 0 0 .01 1 .1 1 .2 .4 (max .3 dur) 0) :scaler amp :duration dur))
- (crf (make-env '(0 1 1 0) :scaler r :duration dur)))
- (do ((i start (+ i 1)))
- ((= i stop))
- (set! (clang 'r) (env crf))
- (outa i (* (env clangf)
- (rkoddssb clang 0.0)))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (glassy 0 .1 1000 .5))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (do ((i 0 (+ i 1)))
- ((= i 10))
- (glassy (* i .3) .1 (+ 400 (* 100 i)) .5)))
-
- (with-sound (:statistics #t :play #t :scaled-to .5)
- (let ((gen (make-rkoddssb 5000.0 0.1 0.95))
- (ampf (make-env '(0 0 9 1 10 0) :base 32 :length 10000))
- (noi (make-rand 10000 .1)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (* (env ampf) (sin (rkoddssb gen (rand noi))))))))
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; inf sinusoids scaled by kr^k: krksin
-
- ;;; Zygmund first
- ;;; this looks interesting, but how to normalize? sum of sines is bad enough, kr^k -> r/(1-r)^2 if x^2<1 (since n=inf)
- ;;; for low n, we could use the Tn roots stuff (clm.c)
- ;;; the formula must be assuming r<1.0 -- if greater than 1 it's acting like r2k! above
-
- (defgenerator (krksin
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (r 0.5) (angle 0.0) fm)
-
-
- (define krksin
-
- (let ((documentation "(make-krksin frequency (r 0.5)) creates a krksin generator. (krksin gen (fm 0.0))
- returns many sines spaced by frequency with amplitude kr^k."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x angle)
- (r1 (- 1.0 r)))
- (let ((r3 (if (> r .9) r1 1.0)) ; not right yet...
- (den (+ 1.0 (* -2.0 r (cos x)) (* r r))))
- (set! angle (+ angle fm frequency))
- (/ (* r1 r1 r3 (sin x))
- (* den den))))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-krksin 440.0 0.5)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (krksin gen)))))
-
- (with-sound (:clipped #f :statistics #t :scaled-to .5 :play #t)
- (let ((gen (make-krksin 6.0 0.965))) ; 60 .6 also
- (do ((i 0 (+ i 1)))
- ((= i 100000))
- (outa i (krksin gen)))))
-
- (do ((i 0 (+ i 1)))
- ((= i 10))
- (let ((mx (maxamp (with-sound (:clipped #f :output (make-float-vector 10000))
- (let ((gen (make-krksin 20.0 (* i 0.1))))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (krksin gen))))))))
- (format () ";~A: ~A" (* 0.1 i) mx)))
-
- ;;; relation between 1/(1-x)^2 and peak amp:
- (with-sound (:clipped #f)
- (do ((i 0 (+ i 1))
- (r 0.0 (+ r .01)))
- ((= i 100))
- (let ((val (/ 1.0 (expt (- 1 r) 2))))
- (let ((pk 0.0))
- (let ((gen (make-krksin 1.0 r)))
- (do ((k 0 (+ k 1)))
- ((= k 100000))
- (let ((x (abs (krksin gen))))
- (if (> x pk) (set! pk x)))))
- (outa i (/ pk val))))))
-
- ;;; r 0: 1.0 (sin(x) in this case)
- ;;; else min den is (1-2r+r^2) so peak should be around (/ (expt (+ 1 (* - 2 r) (* r r)) 2))
- ;;; but at that point sin(x)->0 as x
-
- |#
-
-
-
- #|
- ;;; --------------------------------------------------------------------------------
-
- ;;; absolute value of oscil: abssin
-
- ;;; Zygmund second -- not actually very useful, but shows cos 2nx of abs
-
- (define abssin-methods
- (list
- (cons 'mus-frequency
- (dilambda
- (lambda (g) (mus-frequency (g 'osc)))
- (lambda (g val) (set! (mus-frequency (g 'osc)) val))))
- (cons 'mus-phase
- (dilambda
- (lambda (g) (mus-phase (g 'osc)))
- (lambda (g val) (set! (mus-phase (g 'osc)) val))))))
-
- (defgenerator (abssin
- :make-wrapper (lambda (g)
- (set! (g 'osc) (make-oscil (g 'frequency)))
- g)
- :methods abssin-methods)
- (frequency *clm-default-frequency*) fm
- (osc #f))
-
-
- (define abssin
-
- (let ((documentation "(make-abssin frequency) creates an abssin generator. (abssin gen (fm 0.0)) returns (abs oscil)."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (/ (- (abs (oscil osc fm))
- (/ 2.0 pi))
- (/ 2.0 pi)))))) ; original went from 0 to 1.0, subtract 2/pi, and we get peak at -2/pi
-
- ;; DC: sin^2 x = 1/2 - cos 2x,
- ;; so every term in the sum adds 1/(2(4k^2-1)) -> 1/4 (J 397 or 373)
- ;; so DC is 2/pi = 0.6366
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-abssin 440.0)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (abssin gen)))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((vib (make-abssin 100.0)) ; spacing will be 200, if FM you get index-proportional amount as constant offset
- (gen (make-oscil 1000.0))
- (ampf (make-env '(0 0 1 1 2 1 3 0) :scaler .5 :length 20000)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i
- (* (env ampf)
- (oscil gen 0.0 (* 3 (abssin vib 0.0))))))))
-
- ;;; pitch is 2*freq, 200 1, 400 .203, 600 .087, 800 .049, 1000 .031, 1200 .021
- ;;; 1 .2 .086 .048 .030 .021 -- (/ 3.0 (- (* 4 (* 6 6)) 1))
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; inf cosines, scaled by (-a+sqrt(a^2-b^2))^n/b^n: abcos
-
- ;;; from Sansone, p182, assumptions: a not 0, b not 0, b/a real, abs(b/a)<1 (b less than a)
-
- (defgenerator (abcos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'ab) (sqrt (- (* (g 'a) (g 'a)) (* (g 'b) (g 'b)))))
- (set! (g 'norm) (/ 0.5 (- (/ 1.0 (- 1.0 (/ (abs (- (g 'ab) (g 'a))) (g 'b)))) 1.0)))
- ;; i.e. 1/(1-r) -1 because we start at k=1, r=the complicated a/b business
- g))
- (frequency *clm-default-frequency*) (a 0.5) (b 0.25) (angle 0.0) ab norm fm)
-
-
- (define abcos
-
- (let ((documentation "(make-abcos frequency (a 0.5) (b 0.25)) creates an abcos generator. (abcos gen (fm 0.0))
- returns many cosines spaced by frequency with amplitude (-a+sqrt(a^2-b^2))^k/b^k."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x angle))
- (set! angle (+ angle fm frequency))
- (* norm (- (/ ab (+ a (* b (cos x)))) 1.0)))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-abcos 100.0 0.5 0.25)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (abcos gen)))))
- |#
-
-
-
- (defgenerator (absin
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'ab) (sqrt (- (* (g 'a) (g 'a)) (* (g 'b) (g 'b)))))
- g))
- (frequency *clm-default-frequency*) (a 0.5) (b 0.25) (angle 0.0) ab fm)
-
-
- (define absin
-
- (let ((documentation "(make-absin frequency (a 0.5) (b 0.25)) creates an absin generator. (absin gen (fm 0.0))
- returns many sines spaced by frequency with amplitude (-a+sqrt(a^2-b^2))^k/b^k."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x angle))
- (set! angle (+ angle fm frequency))
- (/ (* ab (sin x) )
- (+ a (* b (cos x)))))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-absin 100.0 0.5 0.25)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (* .5 (absin gen))))))
- |#
-
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; inf cosines scaled by 1/(r^2+k^2): r2k2cos
-
- ;;; J second col third row
-
- (defgenerator (r2k2cos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (r 1.0) (angle 0.0) fm)
-
-
- (define (r2k2cos-norm a)
- ;; J 124
- (- (/ (* pi (cosh (* pi a)))
- (* 2 a (sinh (* pi a))))
- (/ 1.0 (* 2 a a))))
-
- (define r2k2cos
-
- (let ((documentation "(make-r2k2cos frequency (r 1.0)) creates an r2k2cos generator. (r2k2cos gen (fm 0.0))
- returns many cosines spaced by frequency with amplitude 1/(r^2+k^2)."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x angle))
- (if (> x (* 2 pi))
- (set! x (modulo x (* 2 pi))))
- (set! angle (+ x fm frequency))
- (/ (- (* pi (/ (cosh (* r (- pi x)))
- (sinh (* r pi))))
- (/ r))
- (* 2 r (r2k2cos-norm r))))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-r2k2cos 100.0 1.0))) ; 400 .25 -- this isn't very flexible
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (* .5 (r2k2cos gen))))))
- |#
-
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; coskx/k = -ln(2sin(x/2)) or 1/2ln(1/(2-2cosx))
- ;;; sinkx/k = (pi-x)/2 both 0..2pi
- ;;; similarly -1^k : x/2 and ln(2cos(x/2)) (p44..46)
- ;;; 2k-1: pi/x and 1/2ln cot (x/2) 0..2pi and 0..pi
- ;;; but all of these are unbounded, and discontinuous
-
- ;;; --------------------------------------------------------------------------------
-
- #|
- ;;; from Stilson/Smith apparently -- was named "Discrete Summation Formula" which doesn't convey anything to me
- ;;; Alexander Kritov suggests time-varying "a" is good (this is a translation of his code)
-
- (defgenerator (blsaw
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (n 1) (r 0.5) (angle 0.0) fm)
-
-
- (define blsaw
- (let ((documentation "(make-blsaw frequency (n 1) (r 0.5)) creates a blsaw generator. (blsaw gen (fm 0.0)) returns a band-limited sawtooth wave."))
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((a r)
- (N n)
- (x angle)
- (incr frequency)
- (den (+ 1.0 (* -2.0 a (cos x)) (* a a))))
- (set! angle (+ angle fm incr))
- (if (< (abs den) nearly-zero)
- 0.0
- (let* ((s1 (* (expt a (- N 1.0)) (sin (+ (* (- N 1.0) x) incr))))
- (s2 (* (expt a N) (sin (+ (* N x) incr))))
- (s3 (* a (sin (+ x incr)))))
- (/ (+ (sin incr)
- (- s3)
- (- s2)
- s1)
- den))))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-blsaw 440.0 :r 0.5 :n 3)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (blsaw gen)))))
- |#
-
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; asymmetric fm gens
-
- (defgenerator (asyfm
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (ratio 1.0) (r 1.0) (index 1.0) (phase 0.0) fm)
-
-
- (define asyfm-J
- (let ((documentation "(asyfm-J gen fm) is the same as the CLM asymmetric-fm generator (index=1.0), set r != 1.0 to get the asymmetric spectra"))
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((result (let ((r1 (/ r))
- (one (if (or (> r 1.0)
- (< -1.0 r 0.0))
- -1.0 1.0))
- (modphase (* ratio phase)))
- (* (exp (* 0.5 index (- r r1) (+ one (cos modphase))))
- (cos (+ phase (* 0.5 index (+ r r1) (sin modphase)))))))) ; use cos, not sin, to get predictable amp
- (set! phase (+ phase fm frequency))
- result)))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-asyfm 2000.0 :ratio .1)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (* .5 (asyfm-J gen))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-asyfm 2000.0 :ratio .1 :index 1))
- (r-env (make-env '(0 -4 1 -1) :length 20000)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (set! (gen 'r) (env r-env))
- (outa i (asyfm-J gen)))))
-
- (define (val index r)
- (let ((sum 0.0))
- (do ((i -20 (+ i 1)))
- ((= i 21))
- (set! sum (+ sum (* (expt r i) (bes-jn i index)))))
- (let ((norm (exp (* 0.5 index (- r (/ r))))))
- (list sum norm))))
-
- (for-each
- (lambda (index)
- (for-each
- (lambda (r)
- (let ((peak (maxamp (with-sound (:clipped #f :output (make-float-vector 1000))
- (let ((gen (make-asymmetric-fm 2000.0 :ratio .1 :r r)))
- (do ((i 0 (+ i 1)))
- ((= i 1000))
- (outa i (asymmetric-fm gen index))))))))
- (if (> (abs (- peak 1.0)) .1)
- (format () ";asymmetric-fm peak: ~A, index: ~A, r: ~A" peak index r))))
- (list -10.0 -1.5 -0.5 0.5 1.0 1.5 10.0)))
- (list 1.0 3.0 10.0))
- |#
-
- (define asyfm-I
- (let ((documentation "(asyfm-I gen fm) is the I0 case of the asymmetric-fm generator"))
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((result (let ((r1 (/ r))
- (modphase (* ratio phase)))
- (* (exp (* 0.5 index (+ r r1) (- (cos modphase) 1.0)))
- (cos (+ phase (* 0.5 index (- r r1) (sin modphase))))))))
- (set! phase (+ phase fm frequency))
- result)))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-asyfm 2000.0 :ratio .1)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (* .5 (asyfm-I gen))))))
- |#
-
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; bess (returns bes-jn, like oscil returns sin) normalized to peak at 1.0
- ;;; frequency here is the frequency in Hz of the damped sinusoid part of the bessel function
-
- (define bessel-peaks (vector 1.000 0.582 0.487 0.435 0.400 0.375 0.355 0.338 0.325 0.313 0.303 0.294 0.286 0.279 0.273 0.267 0.262 0.257 0.252 0.248))
-
- (defgenerator (bess
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'norm) (if (>= (g 'n) (length bessel-peaks))
- (/ 0.67 (expt (g 'n) 1/3))
- ;; this formula comes from V P Krainov, "Selected Mathetical Methods in Theoretical Physics"
- (bessel-peaks (g 'n))))
- g))
- (frequency *clm-default-frequency*) (n 0) (angle 0.0) (norm 1.0) fm)
-
-
- (define bess
- (let ((documentation "(make-bess frequency (n 0)) creates a bessel function (Jn) generator. (bess gen (fm 0.0)) returns Jn."))
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((result (/ (bes-jn n angle) norm)))
- (set! angle (+ angle frequency fm))
- result)))))
-
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-bess 100.0 :n 0)))
- (do ((i 0 (+ i 1)))
- ((= i 1000))
- (outa i (bess gen)))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen1 (make-bess 400.0 :n 1))
- (gen2 (make-bess 400.0 :n 1))
- (vol (make-env '(0 0 1 1 9 1 10 0) :scaler 2.0 :length 20000)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (bess gen1 (* (env vol) (bess gen2 0.0)))))))
-
- ;;; max amps:
- (do ((i 1 (+ i 1)))
- ((= i 100))
- (let ((mx 0.0))
- (do ((k 0.0 (+ k .001)))
- ((> k 200))
- (let ((val (bes-jn i k)))
- (if (> (abs val) mx)
- (set! mx (abs val)))))
- (format () ";~A" (+ mx .001))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen1 (make-bess 400.0 :n 1))
- (gen2 (make-oscil 400.0))
- (vol (make-env '(0 1 1 0) :scaler 1.0 :length 20000)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (bess gen1 (* (env vol) (oscil gen2 0.0)))))))
-
- ;;; also gen2 800, env scl 0.2
- |#
-
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; Watson "Bessel Functions" p358 127 128 (J0(k sqrt(r^2+a^2- 2ar cos x)) = sum em Jm(ka)Jm(kr) cos mx
- ;;; em here is "Neumann's factor" (p22) = 1 if m=0, 2 otherwise
-
- (defgenerator (jjcos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (r 0.5) (a 1.0) (k 1.0) (angle 0.0) fm)
-
-
- (define jjcos
-
- (let ((documentation "(make-jjcos frequency (r 0.5) (a 1.0) (k 1)) creates a jjcos generator. (jjcos gen (fm 0.0))
- returns a sum of cosines scaled by a product of Bessel functions."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x angle)
- (dc (* (bes-j0 (* k a)) (bes-j0 (* k r)))))
- (let ((norm (- (bes-j0 (* k (sqrt (+ (* a a) (* r r) (* -2 a r))))) dc)))
-
- ;; this norm only works if the a/r/k values all small enough that the initial J0 bump dominates
- ;; if they're large (k=10 for example), later maxes come into play.
- ;; we need a formula for a sum of JJ's
- ;;
- ;; the resultant spectra are similar to FM (we can get sharper bumps, or low-passed bumps, etc)
-
- (set! angle (+ angle fm frequency))
- (/ (- (bes-j0 (* k (sqrt (+ (* r r)
- (* a a)
- (* a -2.0 r (cos x))))))
- dc) ; get rid of DC component
- norm)))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-jjcos 100.0 :a 1.0 :r 1.0 :k 1)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (* .5 (jjcos gen))))))
-
- ;;; example:
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-jjcos 100.0 :a 2.0 :r 1.0 :k 1)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (jjcos gen)))))
-
- :(* (bes-jn 1 1) (bes-jn 1 2))
- 0.253788089467046
- :(* (bes-jn 2 1) (bes-jn 2 2))
- 0.0405418594904987
- :(* (bes-jn 3 1) (bes-jn 3 2))
- 0.00252256243314325
- :(* (bes-jn 4 1) (bes-jn 4 2))
- 8.41951242883886e-5
- which matches perfectly
-
- set k=10
- :(* (bes-jn 1 10) (bes-jn 1 20))
- 0.00290541944296873
- :(* (bes-jn 2 10) (bes-jn 2 20))
- -0.0408277687368493
- :(* (bes-jn 3 10) (bes-jn 3 20))
- -0.00577380202685643
- :(* (bes-jn 4 10) (bes-jn 4 20))
- -0.0286956880041051
- :(* (bes-jn 5 10) (bes-jn 5 20))
- -0.0353830269096024
- :(* (bes-jn 6 10) (bes-jn 6 20))
- 7.96480491715688e-4
- :(* (bes-jn 7 10) (bes-jn 7 20))
- -0.0399227881572529
- :(* (bes-jn 8 10) (bes-jn 8 20))
- -0.0234795438775677
- :(* (bes-jn 9 10) (bes-jn 9 20))
- 0.0365188087949483
- :(* (bes-jn 10 10) (bes-jn 10 20))
- 0.0386925399194178
- :(* (bes-jn 11 10) (bes-jn 11 20))
- 0.00755397504265978
- :(* (bes-jn 12 10) (bes-jn 12 20))
- -0.00754046620160803
- :(* (bes-jn 13 10) (bes-jn 13 20))
- -0.00591450759566936
- :(* (bes-jn 14 10) (bes-jn 14 20))
- -0.00175050411436045
- :(* (bes-jn 15 10) (bes-jn 15 20))
- -3.66078549147997e-6
-
- which again matches
-
- (define* (jjsin gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x angle))
- (set! angle (+ angle fm frequency))
- (* (sin x)
- (bes-j0 (* k (sqrt (+ (* r r)
- (* a a)
- (* a (* -2.0 r (cos x)))))))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-jjcos 100.0 :a 1.0 :r 1.0 :k 1)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (jjsin gen)))))
-
- (define* (jjesin gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x angle))
- (set! angle (+ angle fm frequency))
- (* (exp (* r (- (cos x) 1.0))) ; -1 for norm , but there's huge DC offset
- (bes-j0 (* r (sin x)))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-jjcos 100.0 :a 1.0 :r 1.0 :k 1)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (jjesin gen)))))
-
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; check J0(zsinx) formula
- ;;; main difference from FM: index is divided by 2, J value is squared, else just like cos(sin)
-
- (defgenerator (j0evencos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (index 1.0) (angle 0.0) fm)
-
-
- (define j0evencos
-
- (let ((documentation "(make-j0evencos frequency (index 1.0)) creates a j0evencos generator. (j0evencos gen (fm 0.0))
- returns a sum of cosines scaled Jk^2(index/2)."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x angle)
- (dc (let ((j0 (bes-j0 (* 0.5 index))))
- (* j0 j0))))
- (set! angle (+ angle fm frequency))
- (if (= dc 1.0)
- 1.0
- (/ (- (bes-j0 (* index (sin x)))
- dc) ; get rid of DC component
- (- 1.0 dc)))))))) ; normalize
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-j0evencos 100.0 1.0)))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (outa i (* .5 (j0evencos gen))))))
-
- index 10 (so 10/2 is the bes-jn arg):
-
- (let ((base (* (bes-jn 4 5.0) (bes-jn 4 5.0)))) ; max (fft norms -> 1.0)
- (do ((i 1 (+ i 1)))
- ((= i 11))
- (format () ";~A: ~A ~A" i (* (bes-jn i 5.0) (bes-jn i 5.0)) (/ (* (bes-jn i 5.0) (bes-jn i 5.0)) base))))
- ;1: 0.107308091385168 0.701072497819036
- ;2: 0.00216831005396058 0.0141661502497507
- ;3: 0.133101826831083 0.86958987897572
- ;4: 0.153062759870046 1.0
- ;5: 0.0681943848279407 0.445532178342005
- ;6: 0.0171737701015899 0.112200839160164
- ;7: 0.00284904116112987 0.0186135488707298
- ;8: 3.38752000110201e-4 0.00221315753353599
- ;9: 3.04735259399795e-5 1.99091705688911e-4
- ;10: 2.15444461145164e-6 1.4075563600714e-5
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-j0evencos 100.0 0.0))
- (indf (make-env '(0 0 1 20) :length 30000)))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (set! (gen 'index) (env indf))
- (outa i (* 0.5 (j0evencos gen))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-j0evencos 100.0 0.0))
- (indf (make-env '(0 0 1 20) :length 30000))
- (carrier (make-oscil 2000.0)))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (set! (gen 'index) (env indf))
- (outa i (* 0.5 (oscil carrier) (j0evencos gen))))))
-
- ;;; why no "carrier"? I subtracted DC out above -- to make this look right, I need to use the bes(sin) without any fixup.
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-j0evencos 100.0 0.0))
- (indf (make-env '(0 20 1 0) :length 30000))
- (carrier (make-oscil 2000.0)))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (set! (gen 'index) (env indf))
- (outa i (* 0.5 (j0evencos gen (oscil carrier)))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-j0evencos 100.0 0.0)) ; also 20 800, 20 200 (less index mvt), or 200 50
- (indf (make-env '(0 10 1 0) :length 30000))
- (carrier (make-oscil 2000.0)))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (set! (gen 'index) (env indf))
- (outa i (* 0.5 (j0evencos gen (* .1 (oscil carrier))))))))
-
- (define (j0even beg dur freq amp mc-ratio index)
- (let* ((gen (make-j0evencos (* mc-ratio freq) 0.0))
- (indf (make-env '(0 10 1 0) :duration dur))
- (carrier (make-oscil freq))
- (start (seconds->samples beg))
- (end (+ start (seconds->samples dur))))
- (do ((i start (+ i 1)))
- ((= i end))
- (set! (gen 'index) (env indf))
- (outa i (* 0.5 (j0evencos gen (* index (oscil carrier))))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (do ((i 0 (+ i 1)))
- ((= i 10))
- (j0even i 1.0 2000.0 0.5 (+ .1 (* .05 i)) 0.1)))
-
- (define* (jfm beg dur freq amp mc-ratio index (index-env '(0 1 1 1 2 0)))
- (let* ((start (seconds->samples beg))
- (end (+ start (seconds->samples dur)))
- (md (make-j0evencos (* freq mc-ratio)))
- (cr (make-oscil 2000))
- (vib (make-oscil 5))
- (vibamp (hz->radians (* freq .01)))
- (ampf (make-env '(0 0 1 1 20 1 21 0) :scaler amp :duration dur))
- (indf (make-env index-env :scaler index :duration dur)))
- (do ((i start (+ i 1)))
- ((= i end))
- (let ((vb (* vibamp (oscil vib))))
- (set! (md 'index) (env indf))
- (outa i (* (env ampf)
- (oscil cr vb)
- (j0evencos md (* vb mc-ratio))))))))
-
- (with-sound ("test1.snd" :play #t) (jfm 0 3.0 400.0 0.5 .5 4.0 '(0 1 1 2 2 .5)))
- |#
-
-
- ;;; --------------------------------------------------------------------------------
-
- (defgenerator (j2cos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'n) (max (g 'n) 1))
- g))
- (frequency *clm-default-frequency*) (r 0.5) (n 1) (angle 0.0) fm)
-
-
- (define j2cos
-
- (let ((documentation "(make-j2cos frequency (r 0.5) (n 1)) creates a j2cos generator. (j2cos gen (fm 0.0))
- returns a sum of cosines scaled in a very complicated way."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((rsinx2 (* 2.0 r (sin (* 0.5 angle)))))
- (set! angle (+ angle fm frequency))
- (if (< (abs rsinx2) nearly-zero)
- 1.0
- (/ (bes-jn n rsinx2)
- rsinx2)))))))
-
- ;;; this goes berserk if n=0, needs normalization, dc omission, doc/test
- ;;; if n=1, sample 0 = 1, the rest are in the .5 range!
- ;;; maybe j2cos isn't all that useful...
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-j2cos 100.0 :r 1.0 :n 0)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (* .5 (j2cos gen))))))
- |#
-
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- (defgenerator (jpcos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (if (= (g 'r) (g 'a))
- (begin
- (snd-warning (format #f ";jpcos r and a can't be equal (~A)" (g 'r)))
- (set! (g 'r) (+ (g 'a) .01))))
- g))
- (frequency *clm-default-frequency*) (r 0.5) (a 0.0) (k 1.0) (angle 0.0) fm)
-
-
- (define jpcos
-
- (let ((documentation "(make-jpcos frequency (r 0.5) (a 0.0) (k 1)) creates a jpcos generator. (jpcos gen (fm 0.0))
- returns a sum of cosines scaled in a very complicated way."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- ;; (dc (/ (* (sin (* k a)) (sin (* k r))) (* k a r)))
- ;; from P0(x)=1, J[1/2](x)=sqrt(2/(pi x))sin(x), omitting original 1/pi
- ;; G&R 914 (8.464), 974 (8.912), but it's missing some remaining (small) component
- ;; also omitting the original divide by (* pi (sqrt arg)) -- it's just an amplitude scaler
- ;; and in this context, we get -1..1 peak amps from the sin anyway.
- (let ((arg (+ (* r r)
- (* a a)
- (* a -2.0 r (cos angle)))))
- (set! angle (+ angle fm frequency))
- (if (< (abs arg) nearly-zero) ; r = a, darn it! This will produce a spike, but at least it's not a NaN
- 1.0
- (sin (* k (sqrt arg)))))))))
-
- #|
- (with-sound (:clipped #f :statistics #t)
- (let ((gen (make-jpcos 100.0 :a 1.0 :r 0.5 :k 1)))
- (do ((i 0 (+ i 1)))
- ((= i 210000))
- (outa i (jpcos gen)))))
-
- (with-sound (:clipped #f :statistics #t)
- (let* ((gen (make-jpcos 400.0 :a 1.0 :r 0.5 :k 10))
- (dur 1.0)
- (samps (seconds->samples dur))
- (ampf (make-env '(0 0 1 1 10 1 11 0) :duration dur :scaler 0.5))
- (indf (make-env '(0 0 1 1) :duration dur :scaler 1.0)))
- (do ((i 0 (+ i 1)))
- ((= i samps))
- (set! (gen 'r) (env indf))
- (outa i (* (env ampf)
- (jpcos gen))))))
-
- ;;; -.725, 1/.275
- (with-sound (:clipped #f :scaled-to .5)
- (let* ((gen (make-oscil 100.0)))
- (do ((i 0 (+ i 1)))
- ((= i 44100))
- (outa i (sqrt (+ 1.0 (oscil gen)))))))
-
- (with-sound (:clipped #f :scaled-to .5)
- (let* ((gen (make-oscil 100.0))
- (indf (make-env '(0 .1 1 .9) :length 44100)))
- (do ((i 0 (+ i 1)))
- ((= i 44100))
- (let ((ind (env indf)))
- (outa i (sqrt (+ (* 1.0 1.0) (* ind ind) (* -2 1.0 ind (oscil gen)))))))))
-
- ;;; rkcos r=.4 or so (.6?), so rkcos+indf is mostly equivalent? (k=scaler in both)
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-rkcos 440.0 :r 0.6))
- (gen1 (make-oscil 440.0))
- (indf (make-env '(0 .1 1 .8) :length 50000)))
- (do ((i 0 (+ i 1)))
- ((= i 50000))
- (set! (gen 'r) (env indf))
- (outa i (oscil gen1 (* (gen 'r) (rkcos gen)))))))
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- (defgenerator (jncos :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'ra) (+ (* (g 'a) (g 'a)) (* (g 'r) (g 'r))))
- g))
- (frequency *clm-default-frequency*) (r 0.5) (a 1.0) (n 0) (angle 0.0) ra fm)
-
-
- (define jncos
-
- (let ((documentation "(make-jncos frequency (r 0.5) (a 1.0) (n 0)) creates a jncos generator. (jncos gen (fm 0.0))
- returns a sum of cosines scaled in a very complicated way."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((arg (sqrt (+ ra (* a -2.0 r (cos angle))))))
- (set! angle (+ angle fm frequency))
- (if (< arg nearly-zero)
- 1.0
- (/ (bes-jn n arg)
- (expt arg n))))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-jncos 100.0 :a 0.5 :r 1.0 :n 0)))
- (do ((i 0 (+ i 1)))
- ((= i 41000))
- (outa i (jncos gen)))))
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; use J0(cos)+J1(cos) to get full spectrum
-
- (defgenerator (j0j1cos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (index 1.0) (angle 0.0) fm)
-
-
- (define j0j1cos
-
- (let ((documentation "(make-j0j1cos frequency (index 1.0)) creates a j0j1cos generator. (j0j1cos gen (fm 0.0))
- returns a sum of cosines scaled in a very complicated way."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((dc (let ((j0 (bes-j0 (* 0.5 index))))
- (* j0 j0)))
- (arg (* index (cos angle))))
- (set! angle (+ angle fm frequency))
- (/ (- (+ (bes-j0 arg)
- (bes-j1 arg))
- dc) ; get rid of DC component
- 1.215)))))) ; not the best...
-
- ; need to normalize j0j1cos -- min depends on index, so peak depends on max and min and dc
- ; (max (- 1.2154 dc)
- ; (- -0.5530 dc)
-
- #|
- (let ((mx 0.0) (x 0.0) (saved-x 0.0))
- (do ((i 0 (+ i 1)))
- ((= i 1000))
- (let ((val (+ (bes-j0 x) (bes-j1 x))))
- (if (> (abs val) mx)
- (begin
- (set! mx (abs val))
- (set! saved-x x)))
- (set! x (+ x .001))))
- (list mx saved-x))
-
- (1.21533317877749 0.825000000000001)
- (1.21533318495717 0.824863000002882)
- (1.21533318495718 0.824863061409846)
-
- (-0.552933995255066 4.57000000000269)
- (-0.552933995483144 4.56997100028488)
-
- (do ((i 0 (+ i 1)))
- ((= i 10))
- (let ((pk (maxamp
- (with-sound ((make-float-vector 10000))
- (let ((gen (make-j0j1cos 100.0 i)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (j0j1cos gen))))))))
- (format () ";~A: ~A" i pk)))
- ;0: 0.0
- ;1: 0.555559098720551
- ;2: 0.938335597515106
- ;3: 0.953315675258636
- ;4: 1.16509592533112
- ;5: 1.21275520324707
- ;6: 1.14727067947388
- ;7: 1.07083106040955
- ;8: 1.05760526657104
- ;9: 1.11238932609558
- ;10: 1.1824289560318
- ;11: 1.21528387069702
- ;12: 1.19094204902649
- ;13: 1.14720714092255
- ;14: 1.12512302398682
-
- |#
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-j0j1cos 100.0 1.0)))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (outa i (j0j1cos gen)))))
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- (defgenerator (jycos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'r) (max .0001 (g 'r))) ; 0->inf in bes-y0
- (let ((a (g 'a)) ; "c"
- (r (g 'r))); "b"
- (if (<= r a)
- (format () ";jycos a: ~A must be < r: ~A" a r))
- (if (<= (+ (* a a) (* r r)) (* 2 a r))
- (format () ";jycos a: ~A, r: ~A will cause bes-y0 to return -inf!" a r)))
- g))
- (frequency *clm-default-frequency*) (r 1.0) (a 0.5) ; "b" and "c" in the docs
- (angle 0.0) fm)
-
-
- (define jycos
-
- (let ((documentation "(make-jycos frequency (r 1.0) (a 0.5)) creates a jycos generator. (jycos gen (fm 0.0))
- returns a sum of cosines scaled by Yn(r)*Jn(r)."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x angle)
- (b2c2 (+ (* r r) (* a a)))
- (dc (* (bes-y0 r) (bes-j0 a))))
- (let ((norm (abs (- (bes-y0 (sqrt (+ b2c2 (* -2 r a)))) dc))))
- (set! angle (+ angle fm frequency))
- (/ (- (bes-y0 (sqrt (+ b2c2 (* -2.0 r a (cos x))))) dc) norm)))))))
-
- ;;; oops -- bes-y0(0) is -inf!
- ;;; norm only works for "reasonable" a and r
-
- #|
- (with-sound (:clipped #f :statistics #t :play #f)
- (let ((gen (make-jycos 100.0 1.5 1.0))
- (af (make-env '(0 0 1 1) :length 30000))
- (rf (make-env '(0 3 1 3) :length 30000))
- (ampf (make-env '(0 0 1 1 10 1 11 0) :scaler 0.5 :length 30000)))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (set! (gen 'a) (env af))
- (set! (gen 'r) (env rf))
- (outa i (* (env ampf)
- (jycos gen))))))
-
- :(* (bes-yn 1 1.5) (bes-jn 1 1.0))
- -0.181436652807559
- :(* (bes-yn 2 1.5) (bes-jn 2 1.0))
- -0.107112311628537
- :(* (bes-yn 3 1.5) (bes-jn 3 1.0))
- -0.0405654243875417
-
- :(/ .107 .181)
- 0.591160220994475 [0.600]
- :(/ .040 .181)
- 0.220994475138122 [0.228]
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- #|
- (defgenerator (jcos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (n 0) (r 1.0) (a 0.5) ; "b" and "c" in the docs
- (angle 0.0) fm)
-
-
- (define jcos
-
- (let ((documentation "(make-jcos frequency (n 0) (r 1.0) (a 0.5)) creates a jcos generator. (jcos gen (fm 0.0))
- returns a sum of cosines scaled in some complex manner."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((x angle)
- (b r)
- (c a)
- (dc (* (bes-j0 b) (bes-j0 c))))
- (set! angle (+ angle fm frequency))
- (- (bes-jn n (* (+ n 1) (sqrt (+ (* b b) (* c c) (* -2.0 b c (cos x))))))
- dc))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-jcos 100.0 0 1.0 1.0)))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (outa i (jcos gen)))))
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- #|
- (defgenerator (sin2n
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (n 1) (r 1.0) (angle 0.0) fm)
-
-
- (define sin2n
- (let ((documentation "(make-sin2n frequency (n 0) (r 1.0)) creates a sin2n generator. (sin2n gen (fm 0.0)) returns (r*sin)^(2n)"))
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((x angle))
- (set! angle (+ angle fm frequency))
- (expt (* r (sin x)) (* 2 n)))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-sin2n 100.0 2 1.0)))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (outa i (sin2n gen)))))
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- #|
- ;;; do we need modulo 2*pi for the angles? (it is not used in clm.c)
-
- :(let ((ph 0.0)) (do ((i 0 (+ i 1))) ((= i 22050)) (set! ph (+ ph (hz->radians 100.0)))) ph)
- 628.31850751536
-
- :(let ((ph (* 2 pi 1000000))) (do ((i 0 (+ i 1))) ((= i 22050)) (set! ph (+ ph (hz->radians 100.0)))) (- ph (* 2 pi 1000000)))
- 628.318502381444
-
- :(let ((ph (* 2 pi 1000000000))) (do ((i 0 (+ i 1))) ((= i 22050)) (set! ph (+ ph (hz->radians 100.0)))) (- ph (* 2 pi 1000000000)))
- 628.311109542847
-
- :(let ((ph (* 2 pi 1000000000000))) (do ((i 0 (+ i 1))) ((= i 22050)) (set! ph (+ ph (hz->radians 100.0)))) (- ph (* 2 pi 1000000000000)))
- 624.462890625
-
- ;; similar results from running oscil with 0.0 initial-phase, and 2*pi*1000000000, or running one
- ;; oscil for 3 hours at 6000 Hz -- the sinusoid is clean even around an angle of a billion -- worst
- ;; case increment is pi, so we get (say) a billion samples before we may notice a sag => ca. 8 hours.
- ;; I think that's a long enough tone... (In clm.c and here, the phase and increment are both doubles;
- ;; 53 bits of mantissa, billion=30, so we still have about 23 bits, which actually matches results above).
- |#
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; blackman as a waveform -- all the other fft windows could be implemented
- ;;; perhaps most useful as an amplitude envelope
-
- #|
- (defgenerator (blackman
- :make-wrapper (lambda (g)
- (let ((n (g 'n)))
- (set! n (min (max n 1) 10))
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (case n
- ((1) (set! (g 'coeffs) (float-vector 0.54 -0.46)))
- ((2) (set! (g 'coeffs) (float-vector 0.34401 -0.49755 0.15844)))
- ((3) (set! (g 'coeffs) (float-vector 0.21747 -0.45325 0.28256 -0.04672)))
- ((4) (set! (g 'coeffs) (float-vector 0.084037 -0.29145 0.375696 -0.20762 0.041194)))
- ((5) (set! (g 'coeffs) (float-vector 0.097167 -0.3088448 0.3626224 -0.1889530 0.04020952 -0.0022008)))
- ((6) (set! (g 'coeffs) (float-vector 0.063964353 -0.239938736 0.3501594961 -0.247740954 0.0854382589
- -0.012320203 0.0004377882)))
- ((7) (set! (g 'coeffs) (float-vector 0.04210723 -0.18207621 0.3177137375 -0.284437984 0.1367622316
- -0.033403806 0.0034167722 -0.000081965)))
- ((8) (set! (g 'coeffs) (float-vector 0.027614462 -0.135382235 0.2752871215 -0.298843294 0.1853193194
- -0.064888448 0.0117641902 -0.000885987 0.0000148711)))
- ((9) (set! (g 'coeffs) (float-vector 0.01799071953 -0.098795950 0.2298837751 -0.294112951 0.2243389785
- -0.103248745 0.0275674108 -0.003839580 0.0002189716 -0.000002630)))
- ((10) (set! (g 'coeffs) (float-vector 0.0118717384 -0.071953468 0.1878870875 -0.275808066 0.2489042133
- -0.141729787 0.0502002984 -0.010458985 0.0011361511 -0.000049617
- 0.0000004343))))
- g))
- :methods (list
- (cons 'mus-reset
- (lambda (g)
- (set! (g 'angle) 0.0)))))
- (frequency *clm-default-frequency*) (n 4) (coeffs #f) (angle 0.0) fm)
-
-
- (define blackman
-
- (let ((documentation "(make-blackman frequency (n 4)) creates a blackman generator. (blackman gen (fm 0.0))
- returns the nth Blackman-Harris fft data window as a periodic waveform. (n <= 10)"))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x angle))
- (set! angle (+ angle fm frequency))
- (polynomial coeffs (cos x)))))))
- |#
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((black4 (make-blackman 440.0)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (blackman black4 0.0)))))
- |#
-
- ;;; but that is the same as polyshape/polywave!
-
- (define blackman polywave)
- (define blackman? polywave?)
-
- (define* (make-blackman (frequency 440.0) (n 4))
- (make-polywave frequency
- (case n
- ;; this data is from clm.c
- ((0) (list 0 0))
- ((1) (list 0 0.54 1 -0.46))
- ((2) (list 0 0.42323 1 -0.49755 2 0.078279))
- ((3) (list 0 0.35875 1 0.48829 2 0.14128 3 -0.01168))
- ((4) (list 0 0.287333 1 -0.44716 2 0.20844 3 -0.05190 4 0.005149))
- ((5) (list 0 .293557 1 -.451935 2 .201416 3 -.047926 4 .00502619 5 -.000137555))
- ((6) (list 0 .2712203 1 -.4334446 2 .2180041 3 -.0657853 4 .010761867 5 -.0007700127 6 .00001368088))
- ((7) (list 0 .2533176 1 -.4163269 2 .2288396 3 -.08157508 4 .017735924 5 -.0020967027 6 .00010677413 7 -.0000012807))
- ((8) (list 0 .2384331 1 -.4005545 2 .2358242 3 -.09527918 4 .025373955 5 -.0041524329 6 .00036856041 7 -.00001384355 8 .0000001161808))
- ((9) (list 0 .2257345 1 -.3860122 2 .2401294 3 -.1070542 4 .03325916 5 -.00687337
- 6 .0008751673 7 -.0000600859 8 .000001710716 9 -.00000001027272))
- ((10) (list 0 .2151527 1 -.3731348 2 .2424243 3 -.1166907 4 .04077422 5 -.01000904
- 6 .0016398069 7 -.0001651660 8 .000008884663 9 -.000000193817 10 .00000000084824)))))
-
-
-
-
- ;;; --------------------------------------------------------------------------------
-
-
- ;;; we can add the sin(cos) and sin(sin) cases, using -index in the latter to get
- ;;; asymmetric fm since Jn(-B) = (-1)^n Jn(B)
- ;;;
- ;;; the same trick would work in the other two cases -- gapped spectra
-
- (defgenerator (fmssb
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (ratio 1.0) (index 1.0) (angle 0.0) fm)
-
-
- (define fmssb
- (let ((documentation "(make-fmssb frequency (ratio 1.0) (index 1.0)) creates an fmssb generator. (fmssb gen (fm 0.0)) returns single-sideband FM."))
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((cx angle)
- (mx (* cx ratio)))
- (set! angle (+ angle fm frequency))
- (- (* (cos cx)
- (sin (* index (cos mx))))
- (* (sin cx)
- (sin (* index (sin mx)))))))))) ; use -index for the other side
-
- ;;; FM with complex index
- (define* (fpmc beg dur freq amp mc-ratio fm-index interp)
- (let ((start (seconds->samples beg)))
- (let ((end (+ start (seconds->samples dur)))
- (cr 0.0)
- (cr-frequency (hz->radians freq))
- (md-frequency (hz->radians (* freq mc-ratio)))
- (md 0.0))
- (do ((i start (+ i 1)))
- ((= i end))
- (let ((val (sin (+ cr (* fm-index (sin md))))))
- (outa i (* amp (+ (* (- 1.0 interp) (real-part val))
- (* interp (imag-part val)))))
- (set! cr (+ cr cr-frequency))
- (set! md (+ md md-frequency)))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-fmssb 1000.0 0.1 :index 8.0))) ; 1 3 7 11 ... -- interesting effect
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (* .3 (fmssb gen))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-fmssb 1000.0 0.1 :index 8.0))
- (ampf (make-env '(0 0 1 1 100 0) :base 32 :scaler .3 :length 30000))
- (indf (make-env '(0 1 1 0) :length 30000 :scaler 8)))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (set! (gen 'index) (env indf))
- (outa i (* (env ampf) (fmssb gen))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-fmssb 1000.0 0.05 :index 1.0))
- (ampf (make-env '(0 0 1 1 100 0) :base 32 :scaler .3 :length 30000))
- (indf (make-env '(0 1 1 0) :length 30000 :base 32 :scaler 10)))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (set! (gen 'index) (env indf))
- (outa i (* (env ampf) (fmssb gen))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-fmssb 100.0 5.4 :index 1.0)) ; also 100 700
- (ampf (make-env '(0 0 1 1 100 0) :base 32 :scaler .3 :length 30000)) ; also 0 0 1 1 3 1 100 0...
- ;; '(0 0 1 .75 2 1 3 .95 4 .5 10 0) -> bowed effect, '(0 0 1 .75 2 1 3 .125 4 .25 5 1 6 .8 20 0)
- ;; '(0 0 1 .75 2 1 3 .1 4 .7 5 1 6 .8 100 0) -> clickier attack (300 too)
- (indf (make-env '(0 1 1 0) :length 30000 :base 32 :scaler 10)))
- ;; '(0 0 1 1 3 0)
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (set! (gen 'index) (env indf))
- (outa i (* (env ampf) (fmssb gen))))))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-fmssb 10.0 2.0 :index 1.0))
- (ampf (make-env '(0 0 1 1 3 1 100 0) :base 32 :scaler .3 :length 30000))
- (indf (make-env '(0 1 1 0) :length 30000 :base 32 :scaler 10)))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (set! (gen 'index) (env indf))
- (outa i (* (env ampf) (fmssb gen))))))
-
- (with-sound (:statistics #t :scaled-to .5 :play #t)
- (let ((gen1 (make-fmssb 500 1))
- (gen2 (make-fmssb 1000 .2))
- (ampf (make-env '(0 0 1 1 100 0) :base 32 :length 30000))
- (indf (make-env '(0 1 1 1 10 0) :scaler 5.0 :base 32 :length 30000)))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (let ((ind (env indf)))
- (set! (gen1 'index) ind)
- (set! (gen2 'index) ind)
- (outa i (* (env ampf)
- (+ (fmssb gen1 0.0)
- (fmssb gen2 0.0))))))))
-
- ;;; imaginary machines (also imaginary beasts)
- |#
-
- (definstrument (machine1 beg dur cfreq mfreq amp index gliss)
- (let ((gen (make-fmssb cfreq (/ mfreq cfreq) :index 1.0))
- (start (seconds->samples beg))
- (stop (seconds->samples (+ beg dur)))
- (ampf (make-env '(0 0 1 .75 2 1 3 .1 4 .7 5 1 6 .8 100 0) :base 32 :scaler amp :duration dur))
- (indf (make-env '(0 0 1 1 3 0) :duration dur :base 32 :scaler index))
- (frqf (make-env (if (> gliss 0.0) '(0 0 1 1) '(0 1 1 0)) :duration dur :scaler (hz->radians (* (/ cfreq mfreq) (abs gliss))))))
- (do ((i start (+ i 1)))
- ((= i stop))
- (set! (gen 'index) (env indf))
- (outa i (* (env ampf) (fmssb gen (env frqf)))))))
- #|
-
- (with-sound (:statistics #t :play #t)
- (do ((i 0.0 (+ i .5)))
- ((>= i 2.0))
- (machine1 i .3 100 540 0.5 3.0 0.0)
- (machine1 i .1 100 1200 .5 10.0 200.0)
- (machine1 i .3 100 50 .75 10.0 0.0)
- (machine1 (+ i .1) .1 100 1200 .5 20.0 1200.0)
- (machine1 (+ i .3) .1 100 1200 .5 20.0 1200.0)
- (machine1 (+ i .3) .1 100 200 .5 10.0 200.0)
- (machine1 (+ i .36) .1 100 200 .5 10.0 200.0)
- (machine1 (+ i .4) .1 400 300 .5 10.0 -900.0)
- (machine1 (+ i .4) .21 100 50 .75 10.0 1000.0)
- ))
-
- (with-sound (:statistics #t :play #t)
- (do ((i 0.0 (+ i .2)))
- ((>= i 2.0))
- (machine1 i .3 100 540 0.5 4.0 0.0)
- (machine1 (+ i .1) .3 200 540 0.5 3.0 0.0))
- (do ((i 0.0 (+ i .6)))
- ((>= i 2.0))
- (machine1 i .3 1000 540 0.5 6.0 0.0)
- (machine1 (+ i .1) .1 2000 540 0.5 1.0 0.0)
- ))
-
- (with-sound (:statistics #t :play #t :scaled-to .5)
- (let ((gen (make-rkoddssb 1000.0 2.0 0.875))
- (noi (make-rand 15000 .02))
- (gen1 (make-rkoddssb 100.0 0.1 0.9))
- (ampf (make-env '(0 0 1 1 11 1 12 0) :duration 11.0 :scaler .5))
- (frqf (make-env '(0 0 1 1 2 0 10 0 11 1 12 0 20 0) :duration 11.0 :scaler (hz->radians 10.0))))
- (do ((i 0 (+ i 1)))
- ((= i (* 12 44100)))
- (outa i (* (env ampf)
- (+ (rkoddssb gen1 (env frqf))
- (* .2 (sin (rkoddssb gen (rand noi)))))))))
-
- (do ((i 0.0 (+ i 2)))
- ((>= i 10.0))
- (machine1 i 3 100 700 0.5 4.0 0.0)
- (machine1 (+ i 1) 3 200 700 0.5 3.0 0.0))
- (do ((i 0.0 (+ i 6)))
- ((>= i 10.0))
- (machine1 i 3 1000 540 0.5 6.0 0.0)
- (machine1 (+ i 1) 1 2000 540 0.5 1.0 0.0)
- ))
-
- (with-sound (:statistics #t :play #t)
- (do ((i 0.0 (+ i .2)))
- ((>= i 2.0))
- (machine1 i .3 1200 540 0.5 40.0 0.0)
- (machine1 (+ i .1) .3 2400 540 0.5 3.0 0.0))
- (do ((i 0.0 (+ i .6)))
- ((>= i 2.0))
- (machine1 i .3 1000 540 0.5 6.0 0.0)
- (machine1 (+ i .1) .1 2000 540 0.5 10.0 100.0)
- ))
-
- ;;; same as above but up octave
- (with-sound (:statistics #t :play #t)
- (do ((i 0.0 (+ i .1)))
- ((>= i 2.0))
- (machine1 i .15 2400 1080 0.25 40.0 0.0)
- (machine1 (+ i .05) .2 4800 1080 0.5 3.0 0.0))
- (do ((i 0.0 (+ i .3)))
- ((>= i 2.0))
- (machine1 i .15 2000 1080 0.5 6.0 0.0)
- (machine1 (+ i .05) .1 4000 1080 0.5 10.0 100.0)
- ))
- |#
-
- (define (fm-cancellation beg dur frequency ratio amp index)
- (let ((start (seconds->samples beg)))
- (let ((cx 0.0)
- (mx 0.0)
- (car-frequency (hz->radians frequency))
- (mod-frequency (hz->radians ratio))
- (stop (+ start (seconds->samples dur))))
- (do ((i start (+ i 1)))
- ((= i stop))
- (outa i (* amp (- (* (cos cx)
- (sin (* index (cos mx))))
- (* (sin cx)
- (sin (* index (sin mx))))))
- ;; use -index for reflection
- )
- (set! cx (+ cx car-frequency))
- (set! mx (+ mx mod-frequency))))))
-
- ;(with-sound () (fm-cancellation 0 1 1000.0 100.0 0.3 9.0))
-
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; k3sin
-
- (define k3sin-methods
- (list
- (cons 'mus-reset
- (lambda (g)
- (set! (g 'frequency) 0.0)
- (set! (g 'angle) 0.0)))))
-
- (defgenerator (k3sin
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'coeffs) (float-vector 0.0
- (/ (* pi pi) 6.0)
- (/ pi -4.0)
- 0.08333)) ; (/ 12.0)
- g)
- :methods k3sin-methods)
- (frequency *clm-default-frequency*) (angle 0.0) (coeffs #f) fm)
-
-
- (define k3sin
- (let ((documentation "(make-k3sin frequency) creates a k3sin generator. (k3sin gen (fm 0.0)) returns a sum of sines scaled by k^3."))
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x angle))
- (if (not (<= 0.0 x two-pi))
- (set! x (modulo x two-pi)))
- (set! angle (+ x fm frequency))
- (polynomial coeffs x))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-k3sin 100.0)))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (outa i (k3sin gen)))))
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; I(z) case A&S
-
- (define izcos-methods
- (list
- (cons 'mus-scaler
- (dilambda
- (lambda (g) (g 'r))
- (lambda (g val)
- (set! (g 'r) val)
- (set! (g 'dc) (bes-i0 val))
- (set! (g 'norm) (- (exp val) (g 'dc)))
- (set! (g 'inorm) (/ (g 'norm)))
- val)))))
-
- (defgenerator (izcos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'dc) (bes-i0 (g 'r)))
- (set! (g 'norm) (- (exp (g 'r)) (g 'dc)))
- (set! (g 'inorm) (/ (g 'norm)))
- g)
- :methods izcos-methods)
- (frequency *clm-default-frequency*) (r 1.0) (angle 0.0)
- (dc 0.0) (norm 1.0) inorm fm)
-
-
- (define izcos
- (let ((documentation "(make-izcos frequency (r 1.0)) creates an izcos generator. (izcos gen (fm 0.0)) returns a sum of sines scaled by In(r)."))
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x angle))
- (set! angle (+ angle fm frequency))
- (if (< (abs norm) nearly-zero)
- 1.0
- (* (- (exp (* r (cos x))) dc) inorm)))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-izcos 100.0 1.0)))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (outa i (* .5 (izcos gen))))))
-
- (with-sound (:clipped #f :statistics #t)
- (let ((gen (make-izcos 100.0 1.0))
- (indf (make-env '(0 0 1 3) :length 30000)))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (set! (mus-scaler gen) (env indf))
- (outa i (izcos gen)))))
-
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- (definstrument (organish beg dur freq amp fm-index amp-env)
- ;; this has an organ-style chiff (better than fm index sweep)
- (let ((start (seconds->samples beg))
- (carriers (make-vector 3 #f))
- (fmoscs (make-vector 3 #f))
- (ampfs (make-vector 3 #f))
- (pervib (make-triangle-wave 5 (hz->radians (* freq .003))))
- (ranvib (make-rand-interp 6 (hz->radians (* freq .002))))
- (resc (make-nrssb 340.0 1.0 5 .5))
- (resf (make-env (list 0 0 .05 1 .1 0 dur 0) :scaler (* amp .05) :duration dur)))
- (let ((stop (+ start (seconds->samples dur))))
- (do ((i 0 (+ i 1)))
- ((= i 3))
- (let ((frq (* freq (expt 2 i))))
- (let ((index1 (hz->radians (/ (* fm-index frq 5.0) (log frq))))
- (index2 (hz->radians (/ (* fm-index frq 3.0 (- 8.5 (log frq))) (+ 3.0 (* frq 0.001)))))
- (index3 (hz->radians (/ (* fm-index frq 4.0) (sqrt frq)))))
- (set! (carriers i) (make-oscil frq))
- (set! (fmoscs i) (make-polywave frq
- :partials (list 1 index1
- 3 index2
- 4 index3))))))
-
- (set! (ampfs 0) (make-env (or amp-env '(0 0 1 1 2 1 3 0)) :scaler amp :duration dur))
- (set! (ampfs 1) (make-env (list 0 0 .04 1 .075 0 dur 0) :scaler (* amp .0125) :duration dur))
- (set! (ampfs 2) (make-env (list 0 0 .02 1 .05 0 dur 0) :scaler (* amp .025) :duration dur))
-
- ;; also good:
- ;; (set! (ampfs 1) (make-env (list 0 0 .02 1 .05 0 (- dur .1) 0 (- dur .05) 1 dur 0) :scaler (* amp .025) :duration dur))
- ;; (set! (ampfs 2) (make-env (list 0 0 .01 1 .025 0 (- dur .15) 0 (- dur .1) 1 dur 0) :scaler (* amp .05) :duration dur))
-
- (do ((i start (+ i 1)))
- ((= i stop))
- (let ((vib (+ (triangle-wave pervib) (rand-interp ranvib))))
- (outa i (+ (* (env resf) (nrssb resc 0.0))
- (* (env (vector-ref ampfs 0))
- (oscil (vector-ref carriers 0)
- (+ vib (polywave (vector-ref fmoscs 0) vib))))
- (* (env (vector-ref ampfs 1))
- (oscil (vector-ref carriers 1)
- (+ (* 2 vib) (polywave (vector-ref fmoscs 1) (* 2 vib)))))
- (* (env (vector-ref ampfs 2))
- (oscil (vector-ref carriers 2)
- (+ (* 4 vib) (polywave (vector-ref fmoscs 2) (* 4 vib))))))))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (do ((i 0 (+ i 1)))
- ((= i 10))
- (organish (* i .3) .4 (+ 100 (* 50 i)) .5 1.0 #f)))
-
- (with-sound (:clipped #f :statistics #t :play #t)
- (do ((i 0 (+ i 1)))
- ((= i 10))
- (organish (* i .3) .4 (+ 100 (* 50 i)) .5 1.0 '(0 0 1 1 2 .5 3 .25 4 .125 10 0))))
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- (define adjustable-square-wave-methods
- (list
- (cons 'mus-frequency
- (dilambda
- (lambda (g) (mus-frequency (g 'p1)))
- (lambda (g val) (set! (mus-frequency (g 'p1)) val))))
- (cons 'mus-phase
- (dilambda
- (lambda (g) (mus-phase (g 'p1)))
- (lambda (g val) (set! (mus-phase (g 'p1)) val))))
- (cons 'mus-scaler
- (dilambda
- (lambda (g) (g 'duty-factor))
- (lambda (g val)
- (set! (g 'duty-factor) val)
- (set! (mus-phase (g 'p2)) (* two-pi (- 1.0 (g 'duty-factor))))
- val)))))
-
- (defgenerator (adjustable-square-wave
- :make-wrapper
- (lambda (g)
- (set! (g 'p1) (make-pulse-train
- (g 'frequency)
- (g 'amplitude)))
- (set! (g 'p2) (make-pulse-train
- (g 'frequency)
- (- (g 'amplitude))
- (* two-pi (- 1.0 (g 'duty-factor)))))
- g)
- :methods adjustable-square-wave-methods)
- (frequency *clm-default-frequency*) (duty-factor 0.5) (amplitude 1.0)
- (sum 0.0) (p1 #f) (p2 #f) fm)
-
-
- (define adjustable-square-wave
-
- (let ((documentation "(make-adjustable-square-wave frequency (duty-factor 0.5) (amplitude 1.0))
- creates an adjustable-square-wave generator. (adjustable-square-wave gen (fm 0.0)) returns a square-wave
- where the duty-factor sets the ratio of pulse duration to pulse period."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (set! sum (+ sum
- (pulse-train p1 fm)
- (pulse-train p2 fm)))))))
-
- #|
- (with-sound ()
- (let ((gen (make-adjustable-square-wave 100 .2 .5)))
- (do ((i 0 (+ i 1)))
- ((= i 22050))
- (outa i (adjustable-square-wave gen)))))
- |#
-
-
- (define adjustable-triangle-wave-methods
- (list
- (cons 'mus-frequency
- (dilambda
- (lambda (g) (mus-frequency (g 'gen)))
- (lambda (g val) (set! (mus-frequency (g 'gen)) val))))
- (cons 'mus-phase
- (dilambda
- (lambda (g) (mus-phase (g 'gen)))
- (lambda (g val) (set! (mus-phase (g 'gen)) val))))
- (cons 'mus-scaler
- (dilambda
- (lambda (g) (g 'duty-factor))
- (lambda (g val)
- (set! (g 'duty-factor) val)
- (set! (g 'top) (- 1.0 val))
- (if (not (= val 0.0))
- (set! (g 'scl) (/ (g 'amplitude) val)))
- val)))))
-
- (defgenerator (adjustable-triangle-wave
- :make-wrapper
- (lambda (g)
- (let ((df (g 'duty-factor)))
- (set! (g 'gen) (make-triangle-wave (g 'frequency)))
- (set! (g 'top) (- 1.0 df))
- (set! (g 'mtop) (- (g 'top)))
- (if (not (= df 0.0))
- (set! (g 'scl) (/ (g 'amplitude) df)))
- g))
- :methods adjustable-triangle-wave-methods)
- (frequency *clm-default-frequency*) (duty-factor 0.5) (amplitude 1.0)
- (gen #f) (top 0.0) (mtop 0.0) (scl 0.0) val fm)
-
-
- (define adjustable-triangle-wave
-
- (let ((documentation "(make-adjustable-triangle-wave frequency (duty-factor 0.5) (amplitude 1.0)) creates an
- adjustable-triangle-wave generator. (adjustable-triangle-wave gen (fm 0.0)) returns a triangle-wave where the
- duty-factor sets the ratio of pulse duration to pulse period."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (set! val (triangle-wave gen fm))
- (* scl (- val (max mtop (min top val))))))))
-
- #|
- (with-sound ()
- (let ((gen (make-adjustable-triangle-wave 100 .2 .5)))
- (do ((i 0 (+ i 1)))
- ((= i 22050))
- (outa i (adjustable-triangle-wave gen)))))
- |#
-
-
- (define adjustable-sawtooth-wave-methods
- (list
- (cons 'mus-frequency
- (dilambda
- (lambda (g) (mus-frequency (g 'gen)))
- (lambda (g val) (set! (mus-frequency (g 'gen)) val))))
- (cons 'mus-phase
- (dilambda
- (lambda (g) (mus-phase (g 'gen)))
- (lambda (g val) (set! (mus-phase (g 'gen)) val))))
- (cons 'mus-scaler
- (dilambda
- (lambda (g) (g 'duty-factor))
- (lambda (g val)
- (set! (g 'duty-factor) val)
- (set! (g 'top) (- 1.0 val))
- (set! (g 'mtop) (- val 1.0))
- (if (not (= val 0.0))
- (set! (g 'scl) (/ (g 'amplitude) val)))
- val)))))
-
- (defgenerator (adjustable-sawtooth-wave
- :make-wrapper
- (lambda (g)
- (let ((df (g 'duty-factor)))
- (set! (g 'gen) (make-sawtooth-wave (g 'frequency)))
- (set! (g 'top) (- 1.0 df))
- (set! (g 'mtop) (- df 1.0))
- (if (not (= df 0.0))
- (set! (g 'scl) (/ (g 'amplitude) df)))
- g))
- :methods adjustable-sawtooth-wave-methods)
- (frequency *clm-default-frequency*) (duty-factor 0.5) (amplitude 1.0)
- (gen #f) (top 0.0) (mtop 0.0) (scl 0.0) val fm)
-
-
- (define adjustable-sawtooth-wave
-
- (let ((documentation "(make-adjustable-sawtooth-wave frequency (duty-factor 0.5) (amplitude 1.0)) creates
- an adjustable-sawtooth-wave generator. (adjustable-sawtooth-wave gen (fm 0.0)) returns a sawtooth-wave where
- the duty-factor sets the ratio of pulse duration to pulse period."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (set! val (sawtooth-wave gen fm))
- (* scl (- val (max mtop (min top val))))))))
-
- #|
- (with-sound ()
- (let ((gen (make-adjustable-sawtooth-wave 100 .2 .5)))
- (do ((i 0 (+ i 1)))
- ((= i 22050))
- (outa i (adjustable-sawtooth-wave gen)))))
- |#
-
-
- ;;; and just for laughs... (almost anything would fit in this hack)
- (define adjustable-oscil-methods
- (let ((copy-func (lambda (g)
- (let ((e (inlet g))) ; (copy g) without invoking (g 'copy)
- (let-set! e 'gen (mus-copy (g 'gen)))
- e))))
- (list
- (cons 'mus-frequency
- (dilambda
- (lambda (g) (mus-frequency (g 'gen)))
- (lambda (g val) (set! (mus-frequency (g 'gen)) val))))
- (cons 'mus-phase
- (dilambda
- (lambda (g) (mus-phase (g 'gen)))
- (lambda (g val) (set! (mus-phase (g 'gen)) val))))
- (cons 'mus-scaler
- (dilambda
- (lambda (g) (g 'duty-factor))
- (lambda (g val)
- (set! (g 'duty-factor) val)
- (set! (g 'top) (- 1.0 val))
- (set! (g 'mtop) (- val 1.0))
- (if (not (= val 0.0))
- (set! (g 'scl) (/ val)))
- val)))
- (cons 'copy copy-func)
- (cons 'mus-copy copy-func))))
-
-
- (defgenerator (adjustable-oscil
- :make-wrapper (lambda (g)
- (let ((df (g 'duty-factor)))
- (set! (g 'gen) (make-oscil (g 'frequency)))
- (set! (g 'top) (- 1.0 df))
- (set! (g 'mtop) (- df 1.0))
- (if (not (= df 0.0))
- (set! (g 'scl) (/ df)))
- g))
- :methods adjustable-oscil-methods)
- (frequency *clm-default-frequency*) (duty-factor 0.5)
- (gen #f) (top 0.0) (mtop 0.0) (scl 0.0) val fm)
-
-
- (define adjustable-oscil
-
- (let ((documentation "(make-adjustable-oscil frequency (duty-factor 0.5)) creates an adjustable-oscil
- generator. (adjustable-oscil gen (fm 0.0)) returns a sinusoid where the duty-factor sets the ratio of pulse duration to pulse period."))
-
- (lambda* (g (fm 0.0))
- (let-set! g 'fm fm)
- (with-let g
- (set! val (oscil gen fm))
- (* scl (- val (max mtop (min top val))))))))
-
- #|
- (with-sound (:statistics #t)
- (let ((gen (make-adjustable-oscil 100 .2)))
- (do ((i 0 (+ i 1)))
- ((= i 22050))
- (outa i (adjustable-oscil gen)))))
- |#
-
-
-
-
- ;;;--------------------------------------------------------------------------------
-
- (define* (make-table-lookup-with-env frequency pulse-env size)
- (let ((len (or size *clm-table-size*)))
- (do ((ve (make-float-vector len))
- (e (make-env pulse-env :length len))
- (i 0 (+ i 1)))
- ((= i len)
- (make-table-lookup frequency 0.0 ve len))
- (float-vector-set! ve i (env e)))))
-
- (define* (make-wave-train-with-env frequency pulse-env size)
- (let ((len (or size *clm-table-size*)))
- (do ((ve (make-float-vector len))
- (e (make-env pulse-env :length len))
- (i 0 (+ i 1)))
- ((= i len)
- (make-wave-train frequency 0.0 ve len))
- (float-vector-set! ve i (env e)))))
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- (define round-interp-methods
- (list
- (cons 'mus-frequency
- (dilambda
- (lambda (g) (mus-frequency (g 'rnd)))
- (lambda (g val) (set! (mus-frequency (g 'rnd)) val))))
- (cons 'mus-phase
- (dilambda
- (lambda (g) (mus-phase (g 'rnd)))
- (lambda (g val) (set! (mus-phase (g 'rnd)) val))))))
-
- (defgenerator (round-interp
- :make-wrapper (lambda (g)
- (set! (g 'rnd) (make-rand-interp (g 'frequency) (g 'amplitude)))
- (set! (g 'flt) (make-moving-average (g 'n)))
- g)
- :methods round-interp-methods)
- (frequency *clm-default-frequency*) (n 1) (amplitude 1.0)
- (rnd #f) (flt #f) fm)
-
-
- (define round-interp
-
- (let ((documentation "(make-round-interp frequency (n 1) (amplitude 1.0)) creates a round-interp
- generator. (round-interp gen (fm 0.0)) returns a rand-interp sequence low-pass filtered by a moving-average generator of length n."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (moving-average flt (rand-interp rnd fm))))))
-
- #|
- (with-sound (:channels 5)
- (let ((gen0 (make-round-interp 100 1))
- (gen1 (make-round-interp 100 10))
- (gen2 (make-round-interp 100 100))
- (gen3 (make-round-interp 100 1000))
- (gen4 (make-round-interp 100 10000)))
- (do ((i 0 (+ i 1)))
- ((= i 100000))
- (out-any i (round-interp gen0 0.0) 0)
- (out-any i (round-interp gen1 0.0) 1)
- (out-any i (round-interp gen2 0.0) 2)
- (out-any i (round-interp gen3 0.0) 3)
- (out-any i (round-interp gen4 0.0) 4))))
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; env-any functions
-
- (define (sine-env e)
- (env-any e (lambda (y)
- (* 0.5 (+ 1.0 (sin (* pi (- y 0.5))))))))
-
- (define (square-env e)
- (env-any e (lambda (y)
- (* y y))))
-
- (define (blackman4-env e)
- (env-any e (lambda (y)
- (let ((cx (cos (* pi y))))
- (+ 0.084037 (* cx (- (* cx (+ 0.375696 (* cx (- (* cx 0.041194) 0.20762)))) 0.29145)))))))
-
- (define (multi-expt-env e expts)
- (env-any e (lambda (y)
- (let ((b (expts (modulo (channels e) (length expts)))))
- (/ (- (expt b y) 1.0) (- b 1.0))))))
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; pm with any generator that has mus-phase and mus-run:
-
- (define (run-with-fm-and-pm gen fm pm)
- (set! (mus-phase gen) (+ (mus-phase gen) pm))
- (let ((result (mus-run gen fm 0.0)))
- (set! (mus-phase gen) (- (mus-phase gen) pm))
- result))
-
- #|
- (let ((gen1 (make-oscil 440.0))
- (gen2 (make-oscil 440.0)))
- (do ((i 0 (+ i 1)))
- ((= i 1000))
- (let* ((pm (mus-random 1.0))
- (val1 (oscil gen1 0.0 pm))
- (val2 (run-with-fm-and-pm gen2 0.0 pm)))
- (if (fneq val1 val2)
- (format () ";run-with-fm-and-pm: ~A ~A" val1 val2)))))
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- ;;; cos^n J 121
-
- (defgenerator (nchoosekcos
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (ratio 1.0) (n 1) (angle 0.0) fm)
-
-
- (define nchoosekcos
-
- (let ((documentation "(make-nchoosekcos frequency (ratio 1.0) (n 1)) creates an nchoosekcos generator. (nchoosekcos gen (fm 0.0))
- returns a sum of cosines scaled by the binomial coeffcients."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((x angle)
- (y (* x ratio)))
- (set! angle (+ angle fm frequency))
- (real-part (* (cos x)
- (expt (cos y) n))))))))
-
- #|
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((gen (make-nchoosekcos 2000.0 0.05 10)))
- (do ((i 0 (+ i 1)))
- ((= i 30000))
- (outa i (* .5 (nchoosekcos gen))))))
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; sinc-train
-
- (define sinc-train-methods
- (list
- (cons 'mus-order
- (dilambda
- (lambda (g) (g 'original-n))
- (lambda (g val)
- (if (<= val 0)
- (begin
- (set! (g 'original-n) 1)
- (set! (g 'n) 3))
- (begin
- (set! (g 'original-n) val)
- (set! (g 'n) (+ 1 (* 2 val)))))
- (set! (g 'frequency) (* 0.5 (g 'n) (hz->radians (g 'original-frequency))))
- (g 'original-n))))
- (cons 'mus-frequency
- (dilambda
- (lambda (g) (g 'original-frequency))
- (lambda (g val)
- (set! (g 'original-frequency) val)
- (set! (g 'frequency) (* 0.5 (g 'n) (hz->radians val)))
- val)))))
-
- (defgenerator (sinc-train
- :make-wrapper (lambda (g)
- (if (<= (g 'n) 0)
- (begin
- (set! (g 'original-n) 1)
- (set! (g 'n) 3))
- (begin
- (set! (g 'original-n) (g 'n))
- (set! (g 'n) (+ 1 (* 2 (g 'n)))))) ; mimic ncos
- (set! (g 'original-frequency) (g 'frequency))
- (set! (g 'frequency) (* 0.5 (g 'n) (hz->radians (g 'frequency))))
- g)
- :methods sinc-train-methods)
- (frequency *clm-default-frequency*) (n 1) (angle 0.0)
- (original-n 1) (original-frequency 0.0) fm)
-
-
- (define sinc-train
- (let ((documentation "(make-sinc-train frequency (n 1)) creates a sinc-train generator with n components. (sinc-train gen (fm 0.0)) returns a sinc-train"))
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x angle))
- (let ((max-angle (* pi 0.5 n))
- (new-angle (+ x fm frequency))
- (DC (/ 1.0 n))
- (norm (/ n (- n 1))))
- (if (> new-angle max-angle)
- (set! new-angle (- new-angle (* pi n))))
- (set! angle new-angle)
- (if (< (abs x) nearly-zero)
- 1.0
- (* norm (- (/ (sin x) x) DC)))))))))
-
- #|
- (with-sound (:clipped #f :statistics #t)
- (let* ((g (make-sinc-train 100.0 40)))
- (do ((i 0 (+ i 1)))
- ((= i 44100))
- (outa i (* .5 (sinc-train g 0.0))))))
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; pink-noise (based on rand-bank idea of Orfanidis)
-
- #|
- (defgenerator (pink-noise
- :make-wrapper (lambda (g)
- (if (<= (g 'n) 0) (set! (g 'n) 1))
- (let ((n (g 'n)))
- (set! (g 'rands) (make-vector n))
- (do ((i 0 (+ i 1)))
- ((= i n))
- (set! ((g 'rands) i) (make-rand :frequency (/ *clm-srate* (expt 2 i))))
- (set! (mus-phase ((g 'rands) i)) (random pi))))
- g))
- (n 1) (rands #f))
-
-
- (define pink-noise
-
- (let ((documentation "(make-pink-noise (n 1)) creates a pink-noise generator with n octaves of rand (12 is recommended). (pink-noise gen)
- returns the next random value in the 1/f stream produced by gen."))
-
- (lambda (gen)
- (with-let gen
- (/ (rand-bank rands) (* 2.5 (sqrt n))))))) ; this normalization is not quite right
- |#
-
- (define* (make-pink-noise (n 1))
- (let ((v (make-float-vector (* n 2)))
- (amp (/ (* 2.5 (sqrt n)))))
- (set! (v 0) amp)
- (do ((i 2 (+ i 2)))
- ((= i (* 2 n)))
- (set! (v i) (mus-random amp))
- (set! (v (+ i 1)) (random 1.0)))
- v))
-
- (define pink-noise? float-vector?)
-
- ;;; pink-noise func is in clm2xen.c
-
- #|
- (define (pink-noise v)
- (let ((amp (v 0))
- (sum 0.0)
- (p 0.0)
- (len (length v)))
- (do ((i 2 (+ i 2))
- (x 0.5 (* x 0.5)))
- ((= i len)
- (+ sum (mus-random amp)))
- (set! sum (+ sum (v i)))
- (set! p (- (v (+ i 1)) x))
- (if (negative? p)
- (begin
- (set! (v (+ i 1)) (+ p 1.0))
- (set! (v i) (mus-random amp)))
- (set! (v (+ i 1)) p)))))
- |#
-
- #|
- (with-sound (:clipped #f :statistics #t)
- (let ((gen (make-pink-noise 12)))
- (do ((i 0 (+ i 1)))
- ((= i 44100))
- (outa i (pink-noise gen)))))
-
- (with-sound (:statistics #t) (let ((gen (make-pink-noise 12))) (do ((i 0 (+ i 1))) ((= i 441000)) (outa i (pink-noise gen)))))
- |#
-
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; brown-noise
-
-
- (defgenerator (brown-noise
- :make-wrapper (lambda (g)
- (set! (g 'gr) (make-rand (g 'frequency) (g 'amplitude)))
- g))
- (frequency *clm-default-frequency*) (amplitude 1.0) fm gr (sum 0.0) (prev 0.0))
-
-
- (define brown-noise
-
- (let ((documentation "(make-brown-noise frequency (amplitude 1.0)) returns a generator that produces
- brownian noise. (brown-noise gen (fm 0.0)) returns the next brownian noise sample."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((val (rand gr fm)))
- (if (not (= val prev))
- (begin
- (set! prev val)
- (set! sum (+ sum val))))
- sum)))))
-
- #|
- ;; this is slightly faster, but ugly
- (define* (make-brown-noise (frequency 440.0) (amplitude 1.0))
- (vector 0.0 0.0 (make-rand frequency amplitude)))
-
- (define (brown-noise? g)
- (and (vector? g)
- (= (length g) 3)
- (rand? (g 2))))
-
- (define* (brown-noise g (fm 0.0))
- (let ((val (rand (vector-ref g 2) fm)))
- (if (not (= val (vector-ref g 1)))
- (begin
- (vector-set! g 1 val)
- (vector-set! g 0 (+ (vector-ref g 0) val))))
- (vector-ref g 0)))
-
- (with-sound (:clipped #f :statistics #t)
- (let* ((gen (make-brown-noise 1000)))
- (do ((i 0 (+ i 1)))
- ((= i 44100))
- (outa i (* .01 (brown-noise gen))))))
- |#
-
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; green-noise
-
- (defgenerator (green-noise
- :make-wrapper (lambda (g)
- (set! (g 'gr) (make-rand (g 'frequency) (g 'amplitude)))
- (set! (g 'sum) (* 0.5 (+ (g 'low) (g 'high))))
- g))
- (frequency *clm-default-frequency*) (amplitude 1.0) (low -1.0) (high 1.0)
- fm gr (sum 0.0) (prev 0.0))
-
-
- (define green-noise
-
- (let ((documentation "(make-green-noise frequency (amplitude 1.0) (low -1.0) (high 1.0)) returns a new
- green-noise (bounded brownian noise) generator. (green-noise gen (fm 0.0)) returns the next sample in a
- sequence of bounded brownian noise samples."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((val (rand gr fm)))
- (if (not (= val prev))
- (begin
- (set! prev val)
- (set! sum (+ sum val))
- (if (not (<= low sum high))
- (set! sum (- sum (* 2 val))))))
- sum)))))
-
-
- #|
- (define* (make-green-noise (frequency 440.0) (amplitude 1.0) (low -1.0) (high 1.0))
- (vector 0.0 0.0 low high (make-rand frequency amplitude)))
-
- (define (green-noise? g)
- (and (vector? g)
- (= (length g) 5)
- (rand? (g 4))))
-
- (define* (green-noise g (fm 0.0))
- (let ((val (rand (g 4) fm)))
- (if (not (= val (g 1)))
- (begin
- (set! (g 1) val)
- (set! (g 0) (+ (g 0) val))
- (if (not (<= (g 2) (g 0) (g 3)))
- (set! (g 0) (- (g 0) (* 2.0 val))))))
- (g 0)))
-
- (with-sound (:clipped #f :statistics #t)
- (let* ((gen (make-green-noise 1000)))
- (do ((i 0 (+ i 1)))
- ((= i 44100))
- (outa i (green-noise gen)))))
- |#
-
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; green-noise-interp
-
- (defgenerator (green-noise-interp
- :make-wrapper (lambda (g)
- (set! (g 'sum) (* 0.5 (+ (g 'low) (g 'high))))
- (set! (g 'dv) (/ 1.0 (ceiling (/ *clm-srate* (max 1.0 (g 'frequency))))))
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- (set! (g 'incr) (* (mus-random (g 'amplitude)) dv))
- g))
- (frequency *clm-default-frequency*) (amplitude 1.0) (low -1.0) (high 1.0)
- (angle 0.0) (sum 0.0) (incr 0.0) fm dv)
-
-
- (define green-noise-interp
-
- (let ((documentation "(make-green-noise-interp frequency (amplitude 1.0) (low -1.0) (high 1.0)) returns a new
- interpolating green noise (bounded brownian noise) generator. (green-noise-interp gen (fm 0.0)) returns the next
- sample in a sequence of interpolated bounded brownian noise samples."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (if (not (<= 0.0 angle two-pi))
- (let ((val (mus-random amplitude)))
- (set! angle (modulo angle two-pi))
- (if (< angle 0.0) (set! angle (+ angle two-pi)))
- (if (not (<= low (+ sum val) high))
- (set! val (min (- high sum)
- (max (- low sum)
- (- val))))) ; at least bounce
- (set! incr (* dv val))))
- (set! angle (+ angle fm frequency))
- (set! sum (+ sum incr))))))
-
- #|
- (with-sound (:clipped #f :statistics #t)
- (let* ((gen (make-green-noise-interp 1000)))
- (do ((i 0 (+ i 1)))
- ((= i 44100))
- (outa i (green-noise-interp gen)))))
-
-
- (definstrument (green1 beg end freq amp lo hi)
- (let ((grn (make-green-noise :frequency freq :amplitude amp :high hi :low lo)))
- (do ((i beg (+ i 1)))
- ((= i end))
- (outa i (green-noise grn 0.0)))))
-
- (definstrument (green2 beg end freq amp lo hi)
- (let ((grn (make-green-noise-interp :frequency freq :amplitude amp :high hi :low lo)))
- (do ((i beg (+ i 1)))
- ((= i end))
- (outa i (green-noise-interp grn 0.0)))))
-
- (with-sound () (green1 0 10000 1000 0.1 -0.5 0.5) (green2 10000 20000 1000 0.1 -0.5 0.5))
-
- (definstrument (green3 start dur freq amp amp-env noise-freq noise-width noise-max-step)
- ;; brownian noise on amp env
- (let ((grn (make-green-noise-interp :frequency noise-freq :amplitude noise-max-step :high (* 0.5 noise-width) :low (* -0.5 noise-width)))
- (osc (make-oscil freq))
- (e (make-env amp-env :scaler amp :duration dur))
- (beg (seconds->samples start))
- (end (seconds->samples (+ start dur))))
- (do ((i beg (+ i 1)))
- ((= i end))
- (outa i (* (env e)
- (+ 1.0 (green-noise-interp grn 0.0))
- (oscil osc))))))
-
- (with-sound () (green3 0 2.0 440 .5 '(0 0 1 1 2 1 3 0) 100 .2 .02))
-
-
- (definstrument (green4 start dur freq amp freq-env gliss noise-freq noise-width noise-max-step)
- ;; same but on freq env
- (let ((grn (make-green-noise-interp :frequency noise-freq
- :amplitude (hz->radians noise-max-step)
- :high (hz->radians (* 0.5 noise-width))
- :low (hz->radians (* -0.5 noise-width))))
- (osc (make-oscil freq))
- (e (make-env freq-env :scaler (hz->radians gliss) :duration dur))
- (beg (seconds->samples start))
- (end (seconds->samples (+ start dur))))
- (do ((i beg (+ i 1)))
- ((= i end))
- (outa i (* amp (oscil osc (+ (env e) (green-noise-interp grn 0.0))))))))
-
- (with-sound (:statistics #t) (green4 0 2.0 440 .5 '(0 0 1 1 2 1 3 0) 440 100 100 10))
-
- |#
-
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; moving-sum
-
- (defgenerator (moving-sum
- :make-wrapper (lambda (g)
- (let ((dly (make-moving-average (g 'n))))
- (set! (g 'gen) dly)
- (set! (mus-increment dly) 1.0) ; this is 1/n by default
- g)))
- (n 128) (gen #f))
-
-
- (define moving-sum
-
- (let ((documentation "(make-moving-sum (n 128)) returns a moving-sum generator. (moving-sum gen input)
- returns the sum of the absolute values in a moving window over the last n inputs."))
-
- (lambda (gen y)
- (moving-average (gen 'gen) (abs y)))))
-
-
- (define (make-unmoving-sum) (make-one-pole 1.0 -1.0))
- (define unmoving-sum one-pole)
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; moving-variance
- ;;;
- ;;; this taken from the dsp bboard -- untested!
-
- (defgenerator (moving-variance
- :make-wrapper (lambda (g)
- (let ((g1 (make-moving-average (g 'n))))
- (set! (g 'gen1) g1)
- (set! (mus-increment g1) 1.0))
- (let ((g2 (make-moving-average (g 'n))))
- (set! (g 'gen2) g2)
- (set! (mus-increment g2) 1.0))
- g))
- (n 128) (gen1 #f) (gen2 #f) y)
-
-
- (define (moving-variance gen y)
- (let-set! gen 'y y)
- (with-let gen
- (let ((x1 (moving-average gen1 y))
- (x2 (moving-average gen2 (* y y))))
- (/ (- (* n x2)
- (* x1 x1))
- (* n (- n 1))))))
-
-
- #|
- (with-sound (:clipped #f)
- (let ((gen (make-moving-variance 128)))
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (outa i (moving-variance gen (random 1.0))))))
- |#
-
-
- ;;; similarly (moving-inner-product x y) -> (moving-sum (* x y)),
- ;;; (moving-distance x y) -> (sqrt (moving-sum (* (- x y) (- x y))))
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; moving-rms
-
- (defgenerator (moving-rms
- :make-wrapper (lambda (g)
- (set! (g 'gen) (make-moving-average (g 'n)))
- g))
- (n 128) (gen #f) y)
-
-
- (define moving-rms
-
- (let ((documentation "(make-moving-rms (n 128) returns a moving-rms generator. (moving-rms gen input) returns
- the rms of the values in a window over the last n inputs."))
-
- (lambda (gen y)
- (let-set! gen 'y y)
- (with-let gen
- (sqrt (max 0.0
- ;; this is tricky -- due to floating point inaccuracy, we can get negative output
- ;; from moving-rms even if all the inputs are positive! The sqrt then returns
- ;; a complex number and all hell breaks loose
- (moving-average gen (* y y))))))))
-
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; moving-length
-
- (defgenerator (moving-length
- :make-wrapper (lambda (g)
- (let ((dly (make-moving-average (g 'n))))
- (set! (g 'gen) dly)
- (set! (mus-increment dly) 1.0)
- g)))
- (n 128) (gen #f) y)
-
- (define moving-length moving-rms)
- #|
- (define moving-length
-
- (let ((documentation "(make-moving-length (n 128) returns a moving-length generator. (moving-length gen input)
- returns the length of the values in a window over the last few inputs."))
-
- (lambda (gen y)
- (moving-rms gen y))))
- (let-set! gen 'y y)
- (with-let gen
- (sqrt (max 0.0 (moving-average gen (* y y))))))))
- |#
-
- #|
- (let ((ml (make-moving-length 128))
- (rd (make-readin "oboe.snd")))
- (with-sound ()
- (do ((i 0 (+ i 1)))
- ((= i 50828))
- (outa i (moving-length ml (readin rd))))))
- |#
-
- #|
- ;; perhaps also use moving-rms gen to avoid amplifying noise-sections (or even squlech them)
- (define* (agc (ramp-speed .001) (window-size 512))
- (let ((maxer (make-moving-max window-size))
- (mult 1.0))
- (map-channel
- (lambda (y)
- (let* ((curmax (moving-max maxer y))
- (diff (- 0.5 (* mult curmax)))
- (this-incr (* diff ramp-speed)))
- (set! mult (+ mult this-incr))
- (* y mult))))))
-
- ;;; moving-mean = average
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; weighted-moving-average
- ;;;
- ;;; arithmetic (1/n) weights
-
- (defgenerator (weighted-moving-average
- :make-wrapper (lambda (g)
- (let ((n (g 'n)))
- (let ((dly (make-moving-average n)))
- (set! (mus-increment dly) 1.0)
- (set! (g 'dly) dly))
- (set! (g 'den) (* 0.5 (+ n 1) n)))
- g))
- (n 128) (dly #f) (num 0.0) (sum 0.0) y den)
-
-
- (define weighted-moving-average
-
- (let ((documentation "(make-weighted-moving-average (n 128)) returns a weighted-moving-average
- generator. (weighted-moving-average gen y) returns the sum of the last n inputs weighted by 1/n"))
-
- (lambda (gen y)
- (let-set! gen 'y y)
- (with-let gen
- (set! num (- (+ num (* n y)) sum))
- (set! sum (moving-average dly y))
- (/ num den)))))
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; exponentially-weighted-moving-average
- ;;;
- ;;; geometric (r^n) weights
-
- #|
- (defgenerator (exponentially-weighted-moving-average
- :make-wrapper (lambda (g)
- (let* ((n (g 'n))
- (flt (make-one-pole (/ 1.0 n) (/ (- n) (+ 1.0 n)))))
- (set! (g 'gen) flt)
- g)))
- (n 128) (gen #f))
-
-
- (define exponentially-weighted-moving-average
-
- (let ((documentation "(make-exponentially-weighted-moving-average (n 128) returns an
- exponentially-weighted-moving-average generator. (exponentially-weighted-moving-average gen y)
- returns the sum of the last n inputs weighted by (-n/(n+1))^k"))
-
- (lambda (gen y)
- (one-pole (gen 'gen) y))))
- |#
-
- (define* (make-exponentially-weighted-moving-average (n 128)) (make-one-pole (/ 1.0 n) (/ (- n) (+ 1.0 n))))
- (define exponentially-weighted-moving-average? one-pole?)
- (define exponentially-weighted-moving-average one-pole)
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; polyoid -- Tn + Un to get arbitrary initial-phases
-
- #|
- ;;; old form, now replaced by built-in code (clm.c)
-
- (defgenerator (polyoid
- :make-wrapper (lambda (g)
- (let* ((lst (g 'partial-amps-and-phases))
- (len (length lst))
- (topk (let ((n 0))
- (do ((i 0 (+ i 3)))
- ((>= i len))
- (set! n (max n (floor (lst i)))))
- n))
- (sin-amps (make-float-vector (+ topk 1) 0.0))
- (cos-amps (make-float-vector (+ topk 1) 0.0)))
- (do ((j 0 (+ j 3)))
- ((>= j len))
- (let ((n (floor (lst j)))
- (amp (lst (+ j 1)))
- (phase (lst (+ j 2))))
- (if (> n 0) ; constant only applies to cos side
- (set! (sin-amps n) (* amp (cos phase))))
- (set! (cos-amps n) (* amp (sin phase)))))
- (set! (g 'tn) cos-amps)
- (set! (g 'un) sin-amps)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
-
- :methods (list
- (cons 'mus-data
- (lambda (g) (g 'tn)))
- (cons 'mus-xcoeffs
- (lambda (g) (g 'tn)))
- (cons 'mus-ycoeffs
- (lambda (g) (g 'un)))
- (cons 'mus-xcoeff
- (dilambda
- (lambda (g ind) ((g 'tn) ind))
- (lambda (g ind val) (float-vector-set! (g 'tn) ind val))))
- (cons 'mus-ycoeff
- (dilambda
- (lambda (g ind) ((g 'un) ind))
- (lambda (g ind val) (float-vector-set! (g 'un) ind val))))))
-
- (frequency *clm-default-frequency*) (partial-amps-and-phases #f) (angle 0.0)
- (tn #f) (un #f) fm)
-
-
- (define* (polyoid gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let ((x angle))
- (set! angle (+ angle fm frequency))
- (mus-chebyshev-tu-sum x tn un))))
- |#
-
- (define polyoid polywave)
- (define (polyoid? g) (and (polywave? g) (= (mus-channel g) mus-chebyshev-both-kinds)))
- (define polyoid-tn mus-xcoeffs)
- (define polyoid-un mus-ycoeffs)
-
- (define* (make-polyoid (frequency *clm-default-frequency*) partial-amps-and-phases)
- (let* ((len (length partial-amps-and-phases))
- (topk (do ((n 0)
- (i 0 (+ i 3)))
- ((>= i len)
- (+ n 1))
- (set! n (max n (floor (partial-amps-and-phases i)))))))
- (let ((sin-amps (make-float-vector topk))
- (cos-amps (make-float-vector topk)))
- (do ((j 0 (+ j 3)))
- ((>= j len))
- (let ((n (floor (partial-amps-and-phases j)))
- (amp (partial-amps-and-phases (+ j 1)))
- (phase (partial-amps-and-phases (+ j 2))))
- (if (> n 0) ; constant only applies to cos side
- (set! (sin-amps n) (* amp (cos phase))))
- (set! (cos-amps n) (* amp (sin phase)))))
- (make-polywave frequency :xcoeffs cos-amps :ycoeffs sin-amps))))
-
-
- (define (polyoid-env gen fm amps phases)
- ;; amps and phases are the envelopes, one for each harmonic, setting the sample-wise amp and phase
- (let ((original-data (polyoid-partial-amps-and-phases gen)))
- (let ((data-len (length original-data))
- (amps-len (length amps))
- (tn (polyoid-tn gen))
- (un (polyoid-un gen)))
- (do ((i 0 (+ i 3))
- (j 0 (+ j 1)))
- ((or (= j amps-len)
- (= i data-len)))
- (let ((hn (floor (original-data i)))
- (amp (env (amps j)))
- (phase (env (phases j))))
- (set! (tn hn) (* amp (sin phase)))
- (set! (un hn) (* amp (cos phase)))))
- (polyoid gen fm))))
-
- #|
- (with-sound (:clipped #f)
- (let ((samps 44100)
- (gen (make-polyoid 100.0 (vector 1 1 0.0))))
- (do ((i 0 (+ i 1)))
- ((= i samps))
- (outa i (polyoid gen)))))
-
- (with-sound (:clipped #f)
- (let ((samps 44100)
- (gen (make-polywave 100.0 '(1 1) mus-chebyshev-second-kind))
- (gen1 (make-oscil 100.0)))
- (set! (mus-phase gen) (* 0.5 pi))
- (do ((i 0 (+ i 1)))
- ((= i samps))
- (outa i (* (oscil gen1) (polywave gen))))))
-
- (with-sound (:clipped #f :statistics #t)
- (let ((samps 44100)
- (gen (make-polyoid 100.0 (vector 1 0.5 0.0 51 0.25 0.0 64 .25 (/ pi 2)))))
- (do ((i 0 (+ i 1)))
- ((= i samps))
- (outa i (polyoid gen)))))
-
- (define (test-polyoid n)
- (let* ((res (with-sound (:channels 2 :clipped #f)
- (let ((freqs (make-float-vector n))
- (phases (make-float-vector n)) ; for oscil-bank
- (cur-phases (make-float-vector (* 3 n))) ; for polyoid
- (amp (/ 1.0 n)))
- (do ((i 0 (+ i 1))
- (j 0 (+ j 3)))
- ((= i n))
- (set! (cur-phases j) (+ i 1))
- (set! (cur-phases (+ j 1)) (/ 1.0 n))
- (set! (cur-phases (+ j 2)) (random (* 2 pi)))
-
- (set! (freqs i) (hz->radians (+ i 1.0)))
- (set! (phases i) (cur-phases (+ j 2))))
-
- (let ((gen (make-polyoid 1.0 cur-phases))
- (obank (make-oscil-bank freqs phases (make-float-vector n 1.0) #t)))
- (do ((i 0 (+ i 1)))
- ((= i 88200))
- (outa i (* amp (oscil-bank obank))))
- (do ((i 0 (+ i 1)))
- ((= i 88200))
- (outb i (polyoid gen 0.0)))))))
- (snd (find-sound res)))
- (channel-distance snd 0 snd 1)))
-
- ;;; 0 diff up to 4096 so far (unopt and opt) -- 1.0e-12 at 4096, opt is more than 20 times as fast
-
-
- (with-sound (:clipped #f :channels 2 :statistics #t)
- (let* ((samps 44100)
- (gen1 (make-polyoid 100.0 (vector 1 0.5 0.0 3 0.25 0.0 4 .25 0.0)))
- (gen2 (make-polyoid 100.0 (vector 1 0.5 0.0 3 0.25 0.0 4 .25 0.0)))
- (amps1 (vector (make-env '(0 0 1 1 2 0) :end samps :scaler 0.5)
- (make-env '(0 1 1 0 2 1) :end samps :scaler 0.25)
- (make-env '(0 1 1 0) :end samps :scaler 0.25)))
- (phases1 (vector (make-env '(0 0 1 1) :end samps :scaler (/ pi 2))
- (make-env '(0 0 1 1) :end samps :scaler (/ pi 2))
- (make-env '(0 1 1 0) :end samps :scaler (/ pi 2))))
- (amps2 (vector (make-env '(0 0 1 1 2 0) :end samps :scaler 0.5)
- (make-env '(0 1 1 0 2 1) :end samps :scaler 0.25)
- (make-env '(0 1 1 0) :end samps :scaler 0.25)))
- (phases2 (vector (make-env '(0 0 1 0) :end samps)
- (make-env '(0 0 1 0) :end samps)
- (make-env '(0 0 1 0) :end samps))))
- (do ((i 0 (+ i 1)))
- ((= i samps))
- (outa i (polyoid-env gen1 0.0 amps1 phases1))
- (outb i (polyoid-env gen2 0.0 amps2 phases2)))))
-
-
- (with-sound (:clipped #f :channels 2 :channels 3 :statistics #t)
- (let* ((samps 44100)
- (gen1 (make-polyoid 100.0 (vector 1 1 0 2 1 0 3 1 0)))
- (gen2 (make-polyoid 100.0 (vector 1 1 0 2 1 0 3 1 0)))
- (gen3 (make-polyoid 100.0 (vector 1 1 (/ pi 2) 2 1 (/ pi 2) 3 1 (/ pi 2))))
- (amps1 (vector (make-env '(0 1 1 1) :end samps) (make-env '(0 1 1 1) :end samps) (make-env '(0 1 1 1) :end samps)))
- (amps2 (vector (make-env '(0 1 1 1) :end samps) (make-env '(0 1 1 1) :end samps) (make-env '(0 1 1 1) :end samps)))
- (amps3 (vector (make-env '(0 1 1 1) :end samps) (make-env '(0 1 1 1) :end samps) (make-env '(0 1 1 1) :end samps)))
- (phases1 (vector (make-env '(0 0 1 0) :end samps) (make-env '(0 0 1 0) :end samps) (make-env '(0 0 1 0) :end samps)))
- (phases2 (vector (make-env '(0 0 .1 0 .9 1 1 1) :end samps :scaler (/ pi 2))
- (make-env '(0 0 .1 0 .9 1 1 1) :end samps :scaler (/ pi 2))
- (make-env '(0 0 .1 0 .9 1 1 1) :end samps :scaler (/ pi 2))))
- (phases3 (vector (make-env '(0 1 1 1) :end samps :scaler (/ pi 2))
- (make-env '(0 1 1 1) :end samps :scaler (/ pi 2))
- (make-env '(0 1 1 1) :end samps :scaler (/ pi 2)))))
- (do ((i 0 (+ i 1)))
- ((= i samps))
- (outa i (* .1 (polyoid-env gen1 0.0 amps1 phases1)))
- (outb i (* .1 (polyoid-env gen2 0.0 amps2 phases2)))
- (outc i (* .1 (polyoid-env gen3 0.0 amps3 phases3))))))
-
- |#
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; noid -- sum of n sinusoids at arbitrary (default=random) initial phases
- ;;;
- ;;; for max peak (all cos), set phases arg to (make-vector n (/ pi 2))
- ;;; for min peak, use one of the sets in peak-phases.scm (multiplied through by pi)
- ;;;
- ;;; since initial phases are 0 or pi in peak-phases.scm if n>20, this code could be optimized
-
- (define* (make-noid (frequency 0.0) (n 1) phases (choice 'all))
- (make-polyoid frequency
- (let ((amps (make-vector (* 3 n) 0.0)))
- (do ((i 1 (+ i 1))
- (j 0 (+ j 3)))
- ((> i n))
-
- (case choice
- ((all) (set! (amps j) i))
- ((odd) (set! (amps j) (- (* 2 i) 1)))
- ((prime) (set! (amps j) (some-primes (- i 1)))) ; defined below up to 1024th or so -- probably should use low-primes.scm
- ((even) (set! (amps j) (max 1 (* 2 (- i 1))))))
-
- (set! (amps (+ j 1)) (/ 1.0 n))
- (cond ((vector? phases) (set! (amps (+ j 2)) (phases (- i 1))))
- ((not phases) (set! (amps (+ j 2)) (random (* 2 pi))))
- ((eq? phases 'max-peak) (set! (amps (+ j 2)) (/ pi 2)))))
-
- (when (eq? phases 'min-peak)
- (let ((vector-find-if (lambda (func vect)
- (let ((len (length vect))
- (result #f))
- (do ((i 0 (+ i 1)))
- ((or (= i len)
- result)
- result)
- (set! result (func (vect i))))))))
-
- (if (not (defined? 'noid-min-peak-phases))
- (load "peak-phases.scm"))
-
- (let ((min-dat (vector-find-if
- (lambda (val)
- (and (vector? val)
- (= (val 0) n)
- (let* ((a-val (val 1))
- (a-len (length val))
- (a-data (list a-val (val 2))))
- (do ((k 2 (+ k 1)))
- ((= k a-len))
- (if (and (real? (val k))
- (< (val k) a-val))
- (begin
- (set! a-val (val k))
- (set! a-data (list a-val (val (+ k 1)))))))
- a-data)))
- (case choice
- ((all) noid-min-peak-phases)
- ((odd) nodd-min-peak-phases)
- ((prime) primoid-min-peak-phases)
- ((even) neven-min-peak-phases)))))
- (if min-dat
- (do ((rats (cadr min-dat))
- (i 1 (+ i 1))
- (j 0 (+ j 3)))
- ((> i n))
- (set! (amps (+ j 1)) (/ 1.0 n)) ;(/ 0.999 norm)) -- can't decide about this -- I guess it should be consistent with the #f case
- (set! (amps (+ j 2)) (* pi (rats (- i 1)))))))))
- amps)))
-
- (define noid polyoid)
- (define noid? polyoid?)
-
-
- (define some-primes (vector 1
- 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61
- 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151
- 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251
- 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359
- 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463
- 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593
- 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701
- 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827
- 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953
- 967 971 977 983 991 997 1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069
- 1087 1091 1093 1097 1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213
- 1217 1223 1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 1319 1321
- 1327 1361 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459 1471 1481
- 1483 1487 1489 1493 1499 1511 1523 1531 1543 1549 1553 1559 1567 1571 1579 1583 1597 1601
- 1607 1609 1613 1619 1621 1627 1637 1657 1663 1667 1669 1693 1697 1699 1709 1721 1723 1733
- 1741 1747 1753 1759 1777 1783 1787 1789 1801 1811 1823 1831 1847 1861 1867 1871 1873 1877
- 1879 1889 1901 1907 1913 1931 1933 1949 1951 1973 1979 1987 1993 1997 1999 2003 2011 2017
- 2027 2029 2039 2053 2063 2069 2081 2083 2087 2089 2099 2111 2113 2129 2131 2137 2141 2143
- 2153 2161 2179 2203 2207 2213 2221 2237 2239 2243 2251 2267 2269 2273 2281 2287 2293 2297
- 2309 2311 2333 2339 2341 2347 2351 2357 2371 2377 2381 2383 2389 2393 2399 2411 2417 2423
- 2437 2441 2447 2459 2467 2473 2477 2503 2521 2531 2539 2543 2549 2551 2557 2579 2591 2593
- 2609 2617 2621 2633 2647 2657 2659 2663 2671 2677 2683 2687 2689 2693 2699 2707 2711 2713
- 2719 2729 2731 2741 2749 2753 2767 2777 2789 2791 2797 2801 2803 2819 2833 2837 2843 2851
- 2857 2861 2879 2887 2897 2903 2909 2917 2927 2939 2953 2957 2963 2969 2971 2999 3001 3011
- 3019 3023 3037 3041 3049 3061 3067 3079 3083 3089 3109 3119 3121 3137 3163 3167 3169 3181
- 3187 3191 3203 3209 3217 3221 3229 3251 3253 3257 3259 3271 3299 3301 3307 3313 3319 3323
- 3329 3331 3343 3347 3359 3361 3371 3373 3389 3391 3407 3413 3433 3449 3457 3461 3463 3467
- 3469 3491 3499 3511 3517 3527 3529 3533 3539 3541 3547 3557 3559 3571 3581 3583 3593 3607
- 3613 3617 3623 3631 3637 3643 3659 3671 3673 3677 3691 3697 3701 3709 3719 3727 3733 3739
- 3761 3767 3769 3779 3793 3797 3803 3821 3823 3833 3847 3851 3853 3863 3877 3881 3889 3907
- 3911 3917 3919 3923 3929 3931 3943 3947 3967 3989 4001 4003 4007 4013 4019 4021 4027 4049
- 4051 4057 4073 4079 4091 4093 4099 4111 4127 4129 4133 4139 4153 4157 4159 4177 4201 4211
- 4217 4219 4229 4231 4241 4243 4253 4259 4261 4271 4273 4283 4289 4297 4327 4337 4339 4349
- 4357 4363 4373 4391 4397 4409 4421 4423 4441 4447 4451 4457 4463 4481 4483 4493 4507 4513
- 4517 4519 4523 4547 4549 4561 4567 4583 4591 4597 4603 4621 4637 4639 4643 4649 4651 4657
- 4663 4673 4679 4691 4703 4721 4723 4729 4733 4751 4759 4783 4787 4789 4793 4799 4801 4813
- 4817 4831 4861 4871 4877 4889 4903 4909 4919 4931 4933 4937 4943 4951 4957 4967 4969 4973
- 4987 4993 4999 5003 5009 5011 5021 5023 5039 5051 5059 5077 5081 5087 5099 5101 5107 5113
- 5119 5147 5153 5167 5171 5179 5189 5197 5209 5227 5231 5233 5237 5261 5273 5279 5281 5297
- 5303 5309 5323 5333 5347 5351 5381 5387 5393 5399 5407 5413 5417 5419 5431 5437 5441 5443
- 5449 5471 5477 5479 5483 5501 5503 5507 5519 5521 5527 5531 5557 5563 5569 5573 5581 5591
- 5623 5639 5641 5647 5651 5653 5657 5659 5669 5683 5689 5693 5701 5711 5717 5737 5741 5743
- 5749 5779 5783 5791 5801 5807 5813 5821 5827 5839 5843 5849 5851 5857 5861 5867 5869 5879
- 5881 5897 5903 5923 5927 5939 5953 5981 5987 6007 6011 6029 6037 6043 6047 6053 6067 6073
- 6079 6089 6091 6101 6113 6121 6131 6133 6143 6151 6163 6173 6197 6199 6203 6211 6217 6221
- 6229 6247 6257 6263 6269 6271 6277 6287 6299 6301 6311 6317 6323 6329 6337 6343 6353 6359
- 6361 6367 6373 6379 6389 6397 6421 6427 6449 6451 6469 6473 6481 6491 6521 6529 6547 6551
- 6553 6563 6569 6571 6577 6581 6599 6607 6619 6637 6653 6659 6661 6673 6679 6689 6691 6701
- 6703 6709 6719 6733 6737 6761 6763 6779 6781 6791 6793 6803 6823 6827 6829 6833 6841 6857
- 6863 6869 6871 6883 6899 6907 6911 6917 6947 6949 6959 6961 6967 6971 6977 6983 6991 6997
- 7001 7013 7019 7027 7039 7043 7057 7069 7079 7103 7109 7121 7127 7129 7151 7159 7177 7187
- 7193 7207 7211 7213 7219 7229 7237 7243 7247 7253 7283 7297 7307 7309 7321 7331 7333 7349
- 7351 7369 7393 7411 7417 7433 7451 7457 7459 7477 7481 7487 7489 7499 7507 7517 7523 7529
- 7537 7541 7547 7549 7559 7561 7573 7577 7583 7589 7591 7603 7607 7621 7639 7643 7649 7669
- 7673 7681 7687 7691 7699 7703 7717 7723 7727 7741 7753 7757 7759 7789 7793 7817 7823 7829
- 7841 7853 7867 7873 7877 7879 7883 7901 7907 7919 7927 7933 7937 7949 7951 7963 7993 8009
- 8011 8017 8039 8053 8059 8069 8081 8087 8089 8093 8101 8111 8117 8123 8147 8161 8167 8171))
-
-
- #|
- (with-sound (:clipped #f)
- (let ((samps 44100)
- (gen (make-noid 100.0 3)))
- (do ((i 0 (+ i 1)))
- ((= i samps))
- (outa i (noid gen)))))
-
- (with-sound (:clipped #f :channels 2)
- (let* ((samps 44100)
- (n 10)
- (gen (make-noid 1.0 n 'min-peak))
- (gen2 (make-oscil n ((polyoid-partial-amps-and-phases gen) (- (length (polyoid-partial-amps-and-phases gen)) 1)))))
- (do ((i 0 (+ i 1)))
- ((= i samps))
- (outa i (noid gen))
- (outb i (oscil gen2)))))
-
- (with-sound (:clipped #f)
- (let ((samps 44100)
- (gen (make-noid 100.0 10 'min-peak)))
- (do ((i 0 (+ i 1)))
- ((= i samps))
- (outa i (noid gen)))))
-
- (with-sound (:clipped #f :statistics #t)
- (let ((samps 44100)
- (gen (make-noid 10.0 1024 'min-peak)))
- (do ((i 0 (+ i 1)))
- ((= i samps))
- (outa i (noid gen)))))
-
- (with-sound (:clipped #f :channels 4)
- (let ((samps 44100)
- (gen1 (make-noid 100.0 32 'max-peak))
- (gen2 (make-noid 100.0 32 (make-vector 32 0.0)))
- (gen3 (make-noid 100.0 32))
- (gen4 (make-noid 100.0 32 'min-peak)))
- (do ((i 0 (+ i 1)))
- ((= i samps))
- (outa i (noid gen1 0.0))
- (outb i (noid gen2 0.0))
- (outc i (noid gen3 0.0))
- (outd i (noid gen4 0.0)))))
-
-
- (do ((i 0 (+ i 1)))
- ((= i 4))
- (with-sound (:clipped #f :output (string-append "test-noid-" (number->string i) ".snd"))
- (let ((samps 44100)
- (gen (make-noid 100.0 32 (if (= i 0) 'max-peak
- (if (= i 1) (make-vector 32 0.0)
- (if (= i 2) #f
- 'min-peak))))))
- (do ((i 0 (+ i 1)))
- ((= i samps))
- (outa i (noid gen))))))
-
- (define (knoid n)
- (with-sound (:channels 4 :statistics #t)
- (let ((samps 100000)
- (gen1 (make-noid 10.0 n 'min-peak 'all))
- (gen2 (make-noid 10.0 n 'min-peak 'odd))
- (gen3 (make-noid 10.0 n 'min-peak 'even))
- (gen4 (make-noid 10.0 n 'min-peak 'prime)))
- (do ((i 0 (+ i 1)))
- ((= i samps))
- (outa i (* 0.5 (noid gen1 0.0)))
- (outb i (* 0.5 (noid gen2 0.0)))
- (outc i (* 0.5 (noid gen3 0.0)))
- (outd i (* 0.5 (noid gen4 0.0)))))))
-
- (with-sound (:clipped #f)
- (let ((samps 44100)
- (gen (make-noid 100.0 19 (apply vector (map (lambda (n) (* pi n)) (list 0 1 0 0 1 1 1 1 1 1 0 0 1 1 1 0 1 0 1) )))))
- (do ((i 0 (+ i 1)))
- ((= i samps))
- (outa i (noid gen)))))
- |#
-
- #|
- ;;; --------------------------------------------------------------------------------
- ;;;
- ;;; roid -- sum of n sinusoids at arbitrary (default=random) initial phases and amp r^n
-
- (define* (make-roid (frequency 0.0) (n 1) (r 1.0) (phases #f))
- (make-polyoid frequency
- (let ((amps (make-vector (* 3 n) 0.0))
- (rn (/ 1.0 n)))
- (do ((i 1 (+ i 1))
- (j 0 (+ j 3)))
- ((> i n))
- (set! (amps j) i)
- (set! (amps (+ j 1)) rn)
- (set! rn (* rn r))
- (if (vector? phases)
- (set! (amps (+ j 2)) (phases (- i 1)))
- (if (not phases)
- (set! (amps (+ j 2)) (random (* 2 pi)))
- (if (eq? phases 'max-peak)
- (set! (amps (+ j 2)) (/ pi 2))
- ;; else min-peak, handled separately
- ))))
-
- (if (eq? phases 'min-peak)
- (let ((vector-find-if (lambda (func vect)
- (let ((len (length vect))
- (result #f))
- (do ((i 0 (+ i 1)))
- ((or (= i len)
- result)
- result)
- (set! result (func (vect i))))))))
-
- (if (not (defined? 'roid-min-peak-phases))
- (load "peak-phases.scm"))
-
- (let ((min-dat (vector-find-if
- (lambda (val)
- (and val
- (vector? val)
- (= (val 0) n)
- (let* ((a-val (val 1))
- (a-len (length val))
- (a-data (list a-val (val 2))))
- (do ((k 2 (+ k 1)))
- ((= k a-len))
- (if (and (number? (val k))
- (< (val k) a-val))
- (begin
- (set! a-val (val k))
- (set! a-data (list a-val (val (+ k 1)))))))
- a-data)))
- roid-min-peak-phases)))
- (if min-dat
- (let* ((norm (car min-dat))
- (rats (cadr min-dat))
- (rn (/ 0.999 norm)))
- (do ((i 1 (+ i 1))
- (j 0 (+ j 3)))
- ((> i n))
- (set! (amps (+ j 1)) rn)
- (set! rn (* rn r))
- (set! (amps (+ j 2)) (* pi (rats (- i 1))))))))))
-
- amps)))
-
- (define roid polyoid)
- (define roid? polyoid?)
- |#
-
- #|
- (with-sound (:clipped #f)
- (let ((samps 44100)
- (gen (make-roid 100.0 6 0.5 'min-peak)))
- (do ((i 0 (+ i 1)))
- ((= i samps))
- (outa i (roid gen)))))
- |#
-
-
-
- ;;; ---------------- old waveshape generator ----------------
-
- (define waveshape? polyshape?)
- (define waveshape polyshape)
-
- (define* (make-waveshape (frequency *clm-default-frequency*)
- (partials '(1 1))
- wave
- (size *clm-table-size*)) ; size arg is for backwards compatibility
- (make-polyshape frequency (if wave
- (values :coeffs wave)
- (values :partials partials))))
-
- (define* (partials->waveshape partials (size *clm-table-size*))
- (partials->polynomial partials))
-
-
-
-
- ;;; ---------------- tanh(sin(x)) ----------------
-
- (defgenerator (tanhsin
- :make-wrapper (lambda (g)
- (set! (g 'osc) (make-oscil (g 'frequency) (g 'initial-phase)))
- (set! (g 'frequency) (hz->radians (g 'frequency))) ; so that mus-frequency works at least read side
- g))
- (frequency *clm-default-frequency*) (r 1.0) (initial-phase 0.0)
- (osc #f) fm)
-
-
- (define tanhsin
-
- (let ((documentation "(make-tanhsin (frequency 0.0) (r 1.0) (initial-phase 0.0) returns a tanhsin
- generator. (tanhsin gen (fm 0.0)) produces tanh(r*sin) which approaches a square wave as r increases."))
-
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (tanh (* r (oscil osc fm)))))))
-
-
-
- ;;; ---------------- moving-fft ----------------
-
- (define last-moving-fft-window #f)
-
- (define moving-fft-methods
- (list
- (cons 'mus-data (lambda (g) (g 'data)))
- (cons 'mus-xcoeffs (lambda (g) (g 'rl)))
- (cons 'mus-ycoeffs (lambda (g) (g 'im)))
- (cons 'mus-run (lambda (g arg1 arg2) (moving-fft g)))))
-
- (defgenerator (moving-fft
- :make-wrapper (lambda (g)
- (let ((n (g 'n)))
- (set! (g 'rl) (make-float-vector n))
- (set! (g 'im) (make-float-vector n))
- (set! (g 'data) (make-float-vector n))
- (set! (g 'window)
- (if (and last-moving-fft-window
- (= n (length last-moving-fft-window)))
- last-moving-fft-window
- (set! last-moving-fft-window (make-fft-window hamming-window n))))
- (float-vector-scale! (g 'window) (/ 2.0 (* 0.54 n)))
- (set! (g 'outctr) (+ n 1)) ; first time fill flag
- g))
- :methods moving-fft-methods)
- (input #f) (n 512) (hop 128) (outctr 0)
- (rl #f) (im #f) (data #f)
- (window #f))
-
-
- (define moving-fft
-
- (let ((documentation "(make-moving-fft reader (size 512) (hop 128)) returns a moving-fft generator. (moving-fft gen)
- produces an FFT (polar form) of 'size' samples every 'hop' samples, taking input from the readin generator 'reader'.
- The magnitudes are available as mus-xcoeffs, the phases as mus-ycoeffs, and the current input data as mus-data."))
-
- (lambda (gen)
- (with-let gen
- (let ((new-data #f))
- (if (>= outctr hop)
- (let ((fft-window window))
- (if (> outctr n) ; must be first time through -- fill data array
- (do ((i 0 (+ i 1)))
- ((= i n))
- (float-vector-set! data i (readin input)))
- (let ((mid (- n hop)))
- (float-vector-move! data 0 hop)
- (do ((i mid (+ i 1)))
- ((= i n))
- (float-vector-set! data i (readin input)))))
- (set! outctr 0)
- (set! new-data #t)
- (fill! im 0.0)
- (float-vector-subseq data 0 n rl)
- (float-vector-multiply! rl fft-window)
- (mus-fft rl im n 1)
- (rectangular->polar rl im)))
- (set! outctr (+ outctr 1))
- new-data)))))
-
-
- #|
- (let* ((snd (new-sound))
- (rd (make-readin "oboe.snd"))
- (ft (make-moving-fft rd))
- (data (make-float-vector 256)))
- (set! (lisp-graph?) #t)
- (do ((i 0 (+ i 1)))
- ((= i 10000))
- (if (moving-fft ft)
- (begin
- (float-vector-subseq (mus-xcoeffs ft) 0 255 data)
- (graph data "fft" 0.0 11025.0 0.0 0.1 snd 0 #t))))
- (close-sound snd))
- |#
-
-
-
- ;;; ---------------- moving spectrum ----------------
-
- (defgenerator (moving-spectrum
- :make-wrapper (lambda (g)
- (let ((n (g 'n)))
- (set! (g 'amps) (make-float-vector n))
- (set! (g 'phases) (make-float-vector n))
- (set! (g 'amp-incs) (make-float-vector n))
- (set! (g 'freqs) (make-float-vector n))
- (set! (g 'freq-incs) (make-float-vector n))
- (set! (g 'new-freq-incs) (make-float-vector n))
- (set! (g 'data) (make-float-vector n))
- (set! (g 'fft-window) (make-fft-window hamming-window n))
- (float-vector-scale! (g 'fft-window) (/ 2.0 (* 0.54 n)))
- (set! (g 'outctr) (+ n 1)) ; first time fill flag
- g)))
- (input #f) (n 512) (hop 128)
- (outctr 0)
- (amps #f) (phases #f)
- (amp-incs #f) (freqs #f) (freq-incs #f) (new-freq-incs #f)
- (fft-window #f)
- (data #f) (dataloc 0))
-
- (define (moving-spectrum gen)
- (with-let gen
- (when (>= outctr hop)
- (if (> outctr n) ; must be first time through -- fill data array
- (do ((i 0 (+ i 1)))
- ((= i n))
- (float-vector-set! data i (readin input)))
- (begin
- (float-vector-move! data 0 hop)
- (do ((i (- n hop) (+ i 1)))
- ((= i n))
- (float-vector-set! data i (readin input)))))
-
- (set! outctr 0) ; -1??
- (set! dataloc (modulo dataloc n))
-
- (fill! new-freq-incs 0.0)
- (do ((i 0 (+ i 1))
- (j dataloc (+ j 1)))
- ((= j n))
- (float-vector-set! amp-incs j (* (float-vector-ref fft-window i) (float-vector-ref data i))))
-
- (if (> dataloc 0)
- (do ((i (- n dataloc) (+ i 1))
- (j 0 (+ j 1)))
- ((= j dataloc))
- (float-vector-set! amp-incs j (* (float-vector-ref fft-window i) (float-vector-ref data i)))))
-
- (set! dataloc (+ dataloc hop))
-
- (mus-fft amp-incs new-freq-incs n 1)
- (rectangular->polar amp-incs new-freq-incs)
-
- (let ((scl (/ 1.0 hop))
- (kscl (/ two-pi n)))
- (float-vector-subtract! amp-incs amps)
- (float-vector-scale! amp-incs scl)
-
- (do ((n2 (/ n 2))
- (i 0 (+ i 1))
- (ks 0.0 (+ ks kscl)))
- ((= i n2))
- (let ((diff (modulo (- (new-freq-incs i) (freq-incs i)) two-pi)))
- (set! (freq-incs i) (new-freq-incs i))
- (if (> diff pi) (set! diff (- diff (* 2 pi))))
- (if (< diff (- pi)) (set! diff (+ diff (* 2 pi))))
- (set! (new-freq-incs i) (+ (* diff scl) ks))))
-
- (float-vector-subtract! new-freq-incs freqs)
- (float-vector-scale! new-freq-incs scl)))
-
- (set! outctr (+ outctr 1))
-
- (float-vector-add! amps amp-incs)
- (float-vector-add! freqs new-freq-incs)
- (float-vector-add! phases freqs)))
-
-
- (define (test-sv)
- ;; sv-amps = pv-amps (but len is diff)
- ;; sv-phases = pv-phases
- ;; sv-freqs = pv-phase-increments
-
- (let ((pv (make-phase-vocoder (make-readin "oboe.snd") ))
- (sv (make-moving-spectrum (make-readin "oboe.snd"))))
- (let ((pv-amps (phase-vocoder-amps pv))
- (pv-incrs (phase-vocoder-phase-increments pv))
- (sv-amps (sv 'amps))
- (sv-freqs (sv 'freqs)))
- (call-with-exit
- (lambda (quit)
- (do ((k 0 (+ k 1)))
- ((= k 20))
- (do ((i 0 (+ i 1)))
- ((= i 2000))
- (phase-vocoder pv))
- (do ((i 0 (+ i 1)))
- ((= i 2000))
- (moving-spectrum sv))
- (do ((i 0 (+ i 1)))
- ((= i 256))
- (if (fneq (sv-amps i) (pv-amps i))
- (begin
- (format *stderr* ";test-sv (generators) ~D amps: ~A ~A" i (sv-amps i) (pv-amps i))
- (quit)))
- (if (> (abs (- (sv-freqs i) (pv-incrs i))) .25)
- (begin
- (format *stderr* ";test-sv (generators) ~D freqs: ~A ~A" i (sv-freqs i) (pv-incrs i))
- (quit))))))))))
-
- #|
- (define* (sine-bank amps phases size)
- (let ((len (or size (length amps)))
- (sum 0.0))
- (do ((i 0 (+ i 1)))
- ((= i len))
- (set! sum (+ sum (* (amps i)
- (sin (phases i))))))
- sum))
-
- (with-sound (:channels 2)
- (let* ((gen (make-moving-spectrum (make-readin "oboe.snd")))
- (pv (make-phase-vocoder (make-readin "oboe.snd")))
- (samps (framples "oboe.snd")))
- (do ((i 0 (+ i 1)))
- ((= i samps))
- (moving-spectrum gen)
- (outa i (sine-bank (gen 'amps) (gen 'phases) 256)) ; size = n/2 as in pv
- (outb i (phase-vocoder pv)))))
-
- ; :(channel-distance 0 0 0 1)
- ; 7.902601100022e-9
- |#
-
-
- ;;; moving spectrum returns freqs in radians, and does not try to find the interpolated peak,
- ;;; so we need another version that returns current freq/amp pairs that can be used directly in oscil
- ;;; This is the main portion of the "pins" instrument (also find-pitch in examp.scm)
-
-
-
-
- ;;; ---------------- moving scentroid ----------------
-
- (defgenerator (moving-scentroid
- :make-wrapper (lambda (g)
- (let ((n (g 'size)))
- (set! (g 'rl) (make-float-vector n))
- (set! (g 'im) (make-float-vector n))
- (set! (g 'dly) (make-delay n))
- (set! (g 'rms) (make-moving-rms n))
- (set! (g 'hop) (floor (/ *clm-srate* (g 'rfreq))))
- (set! (g 'binwidth) (/ *clm-srate* n))
- g)))
- (dbfloor -40.0) (rfreq 100.0)
- (size 4096) (hop 1024) (outctr 0)
- (curval 0.0) (binwidth 1.0)
- (rl #f) (im #f)
- (dly #f) (rms #f) x)
-
- (define* (moving-scentroid gen (x 0.0))
- (let-set! gen 'x x)
- (with-let gen
-
- (let ((rms (moving-rms rms x)))
- (if (>= outctr hop)
- (begin
- (set! outctr 0)
- (if (< (linear->db rms) dbfloor)
- (set! curval 0.0)
- (let* ((data (mus-data dly))
- (fft2 (/ size 2)))
- (fill! im 0.0)
- (float-vector-subseq data 0 (- size 1) rl)
- (mus-fft rl im size 1) ; we can use the delay line contents un-reordered because phases are ignored here
- (rectangular->magnitudes rl im)
- (do ((numsum 0.0)
- (densum 0.0)
- (k 0 (+ k 1)))
- ((= k fft2)
- (set! curval (/ (* binwidth numsum) densum)))
- (set! numsum (+ numsum (* k (rl k))))
- (set! densum (+ densum (rl k)))))))))
-
- (delay dly x) ; our "sliding window" on the input data
- (set! outctr (+ outctr 1))
- curval))
-
- #|
- (let* ((snd (open-sound "oboe.snd"))
- (cur-srate (srate snd))
- (old-srate *clm-srate*))
- (set! *clm-srate* cur-srate)
-
- (let ((scn (make-moving-scentroid -40.0 100.0 128))
- (vals (scentroid "oboe.snd" 0.0 1.1 -40.0 100.0 128))
- (k 0))
-
- (let ((data (channel->float-vector 0 22050 snd 0)))
- (close-sound snd)
- (do ((i 0 (+ i 1)))
- ((= i (scn 'size)))
- (moving-scentroid scn (data i)))
- (set! (scn 'outctr) (scn 'hop))
-
- (do ((i (scn 'size) (+ i 1))
- (j 0 (+ j 1)))
- ((= i 22050))
- (let ((val (moving-scentroid scn (data i))))
- (if (= (modulo j (scn 'hop)) 0)
- (begin
- (format () "[~A ~A]~%" val (vals k))
- (set! k (+ k 1)))))))
- (set! *clm-srate* old-srate)))
- |#
-
-
-
- ;;; ---------------- moving-autocorrelation ----------------
-
- (define moving-autocorrelation-methods
- (list
- (cons 'mus-run (lambda (g arg1 arg2) (moving-autocorrelation g)))
- (cons 'mus-data (lambda (g) (g 'rl)))))
-
- (defgenerator (moving-autocorrelation
- :make-wrapper (lambda (g)
- (let ((n (g 'n)))
- (set! (g 'rl) (make-float-vector n))
- (set! (g 'im) (make-float-vector n))
- (set! (g 'data) (make-float-vector n))
- (set! (g 'outctr) (+ n 1)) ; first time fill flag
- g))
- :methods moving-autocorrelation-methods)
- (input #f) (n 512) (hop 128) (outctr 0)
- (rl #f) (im #f) (data #f))
-
-
- (define moving-autocorrelation
-
- (let ((documentation "(make-moving-autocorrelation reader (size 512) (hop 128)) returns a moving-autocorrelation
- generator. (moving-autocorrelation gen) produces the autocorrelation of 'size' samples every 'hop' samples, taking
- input from the readin generator 'reader'. The output data is available via mus-data."))
-
- (lambda (gen)
- (with-let gen
- (let ((new-data #f))
- (if (>= outctr hop)
- (begin
- (if (> outctr n) ; must be first time through -- fill data array
- (do ((i 0 (+ i 1)))
- ((= i n))
- (float-vector-set! data i (readin input)))
- (begin
- (float-vector-move! data 0 hop)
- (do ((i (- n hop) (+ i 1)))
- ((= i n))
- (float-vector-set! data i (readin input)))))
- (set! outctr 0)
- (set! new-data #t)
- (fill! im 0.0)
- (float-vector-subseq data 0 (- n 1) rl)
- (autocorrelate rl)))
- (set! outctr (+ outctr 1))
- new-data)))))
-
-
-
-
- ;;; ---------------- moving-pitch ----------------
-
- (define moving-pitch-methods
- (list
- (cons 'mus-run (lambda (g arg1 arg2) (moving-pitch g)))))
-
- (defgenerator (moving-pitch
- :make-wrapper (lambda (g)
- (set! (g 'ac) (make-moving-autocorrelation
- (g 'input)
- (g 'n)
- (g 'hop)))
- g)
- :methods moving-pitch-methods)
- (input #f) (n 512) (hop 128)
- (ac #f) (val 0.0))
-
-
- (define (moving-pitch gen)
- (with-let gen
- (when (moving-autocorrelation ac)
- (let ((data (mus-data ac)))
- (let ((peak 0.0)
- (peak-loc 0)
- (len (length data)))
- (do ((i 8 (+ i 1))) ; assume we're not in the top few octaves
- ((= i len))
- (let ((apk (abs (data i))))
- (if (> apk peak)
- (begin
- (set! peak apk)
- (set! peak-loc i)))))
- (if (or (= peak 0.0)
- (= peak-loc 0))
- (set! val 0.0)
- (let ((la (data (- peak-loc 1)))
- (ra (data (+ peak-loc 1))))
- (let ((logla (log (/ (max la .0000001) peak) 10)) ; (positive la)?
- (logra (log (/ (max ra .0000001) peak) 10)))
- (set! val
- (/ *clm-srate*
- (+ peak-loc (/ (* 0.5 (- logla logra))
- (+ logla logra)))))))))))
- val))
-
- #|
- (let* ((rd (make-readin "oboe.snd"))
- (cur-srate (srate "oboe.snd"))
- (old-srate *clm-srate*))
- (set! *clm-srate* cur-srate)
- (let* ((scn (make-moving-pitch rd))
- (last-pitch 0.0)
- (pitch 0.0))
- (do ((i 0 (+ i 1)))
- ((= i 22050))
- (set! last-pitch pitch)
- (set! pitch (moving-pitch scn))
- (if (not (= last-pitch pitch))
- (format () "~A: ~A~%" (* 1.0 (/ i cur-srate)) pitch))))
- (set! *clm-srate* old-srate))
- |#
-
-
-
- #|
- (define (abel k)
- ;; sum i from 1 to k (-1)^(i + 1) * (sin i) / i
- (with-sound (:clipped #f :statistics #t)
- (let ((harmonics (make-float-vector (* 2 k))))
- (do ((i 1 (+ i 1))
- (j 0 (+ j 2))
- (n -1 (- n)))
- ((= i k))
- (set! (harmonics j) i)
- (set! (harmonics (+ j 1)) (/ n i)))
- (let ((gen (make-polywave 100.0 :partials (normalize-partials harmonics))))
- (do ((i 0 (+ i 1)))
- ((= i 100000))
- (outa i (polywave gen)))))))
-
- (define* (adds num freq e amp v (type mus-chebyshev-first-kind))
- (with-sound (:clipped #f :statistics #t :play #t)
- (let ((harmonics (make-float-vector (* 2 num)))
- (freqe (make-env e :length num)))
- (do ((i 1 (+ i 1))
- (j 0 (+ j 2)))
- ((= i num))
- (set! (harmonics j) i)
- (set! (harmonics (+ j 1)) (env freqe)))
- (let ((gen (make-polywave freq :partials (normalize-partials harmonics) :type type))
- (vib (make-oscil 5)))
- (do ((i 0 (+ i 1)))
- ((= i 100000))
- (outa i (* amp (polywave gen (* (hz->radians v) (oscil vib))))))))))
-
-
- ;(adds 200 20 '(0 0 10 1 12 0 20 0 24 .2 35 0 46 0 57 .1 68 0) .5 2)
- ;(adds 300 15 '(0 0 10 1 12 0 20 0 24 .2 35 0 46 0 57 .1 68 0) .5 3)
-
- |#
-
-
- #|
- (defgenerator (circler
- :make-wrapper (lambda (g)
- (set! (g 'frequency) (hz->radians (g 'frequency)))
- g))
- (frequency *clm-default-frequency*) (angle 0.0) fm)
-
- (define circler
- (let ((documentation "(make-circler (frequency 0.0) returns a circler generator. (circler gen (fm 0.0)) produces a waveform made up of half circles"))
- (lambda* (gen (fm 0.0))
- (let-set! gen 'fm fm)
- (with-let gen
- (let* ((x (modulo angle (* 2 pi)))
- (xx (/ (* 4 x) (* 2 pi)))
- (y (if (< xx 2)
- (sqrt (- 1 (* (- 1 xx) (- 1 xx))))
- (- (sqrt (- 1 (* (- 3 xx) (- 3 xx))))))))
- (set! angle (+ x fm frequency))
- y)))))
-
- (with-sound (:clipped #f :statistics #t)
- (let ((gen (make-circler 10.0)))
- (do ((i 0 (+ i 1)))
- ((= i 20000))
- (outa i (circler gen)))))
-
- ;;; odd harmonics: 1, .18 .081 .048 .033 .024, .019
- |#
-
-
- #|
- ;; "extremal trigonometric polynomials"
-
- (define (extremal-trig N freq)
- (with-sound ()
- (let ((tan-scl (tan (/ pi (* 2 (+ N 1)))))
- (incr (hz->radians freq)))
- (do ((k 1 (+ k 1)))
- ((= k N))
- (let ((cos-coeff (* tan-scl (sin (/ (* k pi) (+ N 1)))))
- (kincr (* k incr)))
- (do ((i 0 (+ i 1))
- (x 0.0 (+ x kincr)))
- ((= i 40000))
- (outa i (* cos-coeff (cos x)))))))))
- |#
-
-
-
- ;;; ---------------- flocsig (flanged locsig) ----------------
-
- (defgenerator (flocsig
- ;; assume stereo out/rev
- :make-wrapper (lambda (g)
- (set! (g 'maxd) (ceiling (g 'amplitude))) ; was amplitude?
- (set! (g 'out1) (make-float-vector (g 'maxd)))
- (set! (g 'out2) (make-float-vector (g 'maxd)))
- (set! (g 'ri) (make-rand-interp
- :frequency (g 'frequency)
- :amplitude (- (g 'amplitude) 1.0)))
- (if (not (g 'offset))
- (set! (g 'offset) (mus-random (* 0.3 (g 'amplitude)))))
- g))
- (reverb-amount 0.0) (frequency 1.0) (amplitude 2.0) offset
- (maxd 0)
- (out1 #f) (out2 #f) (outloc 0)
- (ri #f) samp input)
-
- (define 1/sqrt2 (/ 1.0 (sqrt 2.0)))
-
- (define (flocsig gen samp input)
- ;; signal position and per-channel-delay depends on rand-interp
- (let-set! gen 'samp samp)
- (let-set! gen 'input input)
- (with-let gen
- (let ((pos (min (max (+ (rand-interp ri) offset)
- (- amplitude))
- amplitude))
- (loc outloc))
- (let ((dly1 (abs (min 0.0 pos)))
- (dly2 (max 0.0 pos)))
- (let ((amp1 (if (<= pos -1.0) 1.0
- (if (>= pos 1.0) 0.0
- (* (sqrt (- 1.0 pos)) 1/sqrt2))))
- (amp2 (if (<= pos -1.0) 0.0
- (if (>= pos 1.0) 1.0
- (* (sqrt (+ 1.0 pos)) 1/sqrt2))))
- (frac1 (- dly1 (floor dly1)))
- (frac2 (- dly2 (floor dly2))))
- (let ((loc10 (modulo (+ loc (floor dly1)) maxd)))
- (set! (out1 loc10) (+ (out1 loc10) (* amp1 input (- 1.0 frac1)))))
- (let ((loc11 (modulo (+ loc 1 (floor dly1)) maxd)))
- (set! (out1 loc11) (+ (out1 loc11) (* amp1 input frac1))))
- (let ((loc20 (modulo (+ loc (floor dly2)) maxd)))
- (set! (out2 loc20) (+ (out2 loc20) (* amp2 input (- 1.0 frac2)))))
- (let ((loc21 (modulo (+ loc 1 (floor dly2)) maxd)))
- (set! (out2 loc21) (+ (out2 loc21) (* amp2 input frac2))))))
-
- (let ((val1 (out1 loc))
- (val2 (out2 loc)))
- (set! (out1 loc) 0.0)
- (set! (out2 loc) 0.0)
- (set! loc (+ loc 1))
- (if (= loc maxd) (set! loc 0))
- (outa samp val1)
- (outb samp val2)
- (if (> reverb-amount 0.0)
- (begin
- (outa samp (* reverb-amount val1) *reverb*)
- (outb samp (* reverb-amount val2) *reverb*)))
- (set! outloc loc)))))
-
-
-
- ;;; --------------------------------------------------------------------------------
- ;;; old version of one-pole-all-pass
- #|
- (defgenerator one-pole-allpass coeff input x1 y1)
-
- (define (one-pole-allpass gen input)
- (let-set! gen 'input input)
- (with-let gen
- (set! y1 (+ x1 (* coeff (- input y1))))
- (set! x1 input)
- y1))
-
- (defgenerator one-pole-allpass-bank coeff input x1 y1 x2 y2 x3 y3 x4 y4 x5 y5 x6 y6 x7 y7 x8 y8)
-
- (define (one-pole-allpass-bank gen input)
- (let-set! gen 'input input)
- (with-let gen
- (set! y1 (+ x1 (* coeff (- input y1))))
- (set! x1 input)
-
- (set! y2 (+ x2 (* coeff (- y1 y2))))
- (set! x2 y1)
-
- (set! y3 (+ x3 (* coeff (- y2 y3))))
- (set! x3 y2)
-
- (set! y4 (+ x4 (* coeff (- y3 y4))))
- (set! x4 y3)
-
- (set! y5 (+ x5 (* coeff (- y4 y5))))
- (set! x5 y4)
-
- (set! y6 (+ x6 (* coeff (- y5 y6))))
- (set! x6 y5)
-
- (set! y7 (+ x7 (* coeff (- y6 y7))))
- (set! x7 y6)
-
- (set! y8 (+ x8 (* coeff (- y7 y8))))
- (set! x8 y7)
- y8))
-
-
- (defgenerator expseg currentValue targetValue r)
-
- (define (expseg gen r)
- (let-set! gen 'r r)
- (with-let gen
- (set! currentValue (+ (* r targetValue) (* (- 1.0 r) currentValue)))))
- ;(set! currentValue (+ currentValue (* r (- targetValue currentValue))))))
- ;; (bil) this is slightly different (getting clicks)
-
-
- (define (make-one-pole-swept)
- (vector 0.0))
-
- (define (one-pole-swept gen input coef)
- ;; signal controlled one-pole lowpass filter
- (set! (gen 0) (- (* (+ 1.0 coef) input) (* coef (gen 0)))))
-
- (define (make-pnoise)
- (vector 16383))
-
- (define (pnoise gen x)
- ;; very special noise generator
- (set! (gen 0) (logand (floor (+ (* (gen 0) 1103515245) 12345)) #xffffffff))
- ;; (bil) added the logand -- otherwise we get an overflow somewhere
- (* x (- (* (modulo (floor (/ (gen 0) 65536.0)) 65536) 0.0000305185) 1.0)))
- ;; this looks nutty to me -- was it originally running in 32 bits?
-
-
- (define pn-gen 16383)
- (define (pnoise x)
- ;; very special noise generator
- (set! pn-gen (logand (+ (* pn-gen 1103515245) 12345) #xffffffff))
- ;; (bil) added the logand -- otherwise we get an overflow somewhere, also removed floor
- (* x (- (* pn-gen 4.6566128730774e-10) 1.0)))
-
- |#
-
-
-
-
- ;;; --------------------------------------------------------------------------------
-
- (define (calling-all-generators)
- ;; for snd-test
- (with-sound (:play #f)
- (lutish 0 1 440 .1)
- (oboish 1 1 300 .1 '(0 0 1 1 2 0))
- (nkssber 2 1 1000 100 5 5 0.5)
- (stringy 3 1 1000 .5)
- (ercoser 4 1 100 .5 0.1)
- (bouncy 5 2 300 .5 5 10)
- (pianoy 6 3 100 .5)
- (pianoy1 7 4 200 .5 1 .1)
- (pianoy2 8 1 100 .5)
- (glassy 9 .1 1000 .5)
- (machine1 10 .3 100 540 0.5 3.0 0.0)
- (organish 11 .4 100 .5 1.0 #f)
- (brassy 12 4 50 .5 '(0 0 1 1 10 1 11 0) '(0 1 1 0) 1000)))
-
-
|