libcm is a C development framework with an emphasis on audio signal processing applications.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cmDspBuiltIn.c 158KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586
  1. //( { file_desc:"Built-in 'snap' processor units." kw:[snap]}
  2. #include "cmPrefix.h"
  3. #include "cmGlobal.h"
  4. #include "cmFloatTypes.h"
  5. #include "cmComplexTypes.h"
  6. #include "cmRpt.h"
  7. #include "cmErr.h"
  8. #include "cmCtx.h"
  9. #include "cmMem.h"
  10. #include "cmMallocDebug.h"
  11. #include "cmLinkedHeap.h"
  12. #include "cmMath.h"
  13. #include "cmFile.h"
  14. #include "cmFileSys.h"
  15. #include "cmSymTbl.h"
  16. #include "cmJson.h"
  17. #include "cmText.h"
  18. #include "cmPrefs.h"
  19. #include "cmProcObj.h"
  20. #include "cmDspValue.h"
  21. #include "cmDspCtx.h"
  22. #include "cmDspClass.h"
  23. #include "cmDspFx.h"
  24. #include "cmDspKr.h"
  25. #include "cmMsgProtocol.h"
  26. #include "cmThread.h"
  27. #include "cmUdpPort.h"
  28. #include "cmUdpNet.h"
  29. #include "cmTime.h"
  30. #include "cmAudioSys.h"
  31. #include "cmDspSys.h"
  32. #include "cmDspPreset.h" // required for cmDspNetSend
  33. #include "cmDspNet.h"
  34. #include "cmAudioFile.h"
  35. #include "cmThread.h" // used for threaded loading in wave table file mode
  36. #include "cmProcObj.h"
  37. #include "cmProcTemplateMain.h"
  38. #include "cmProc.h"
  39. #include "cmMidi.h"
  40. #include "cmProc2.h"
  41. #include "cmVectOpsTemplateMain.h"
  42. #include "cmMidiPort.h"
  43. #include "sa/cmSaProc.h"
  44. //------------------------------------------------------------------------------------------------------------
  45. //)
  46. //( { label:cmDspPrinter file_desc:"Console printing unit." kw:[sunit] }
  47. enum
  48. {
  49. kLblPrId,
  50. kMsPrId,
  51. kInPrId
  52. };
  53. cmDspClass_t _cmPrinterDC;
  54. typedef struct
  55. {
  56. cmDspInst_t inst;
  57. unsigned limitCycles;
  58. } cmDspPrinter_t;
  59. cmDspInst_t* _cmDspPrinterAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  60. {
  61. cmDspVarArg_t args[] =
  62. {
  63. { "lbl", kLblPrId, 0, 0, kInDsvFl | kStrzDsvFl | kOptArgDsvFl, "Label" },
  64. { "ms", kMsPrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Period"},
  65. { "in", kInPrId, 0, 0, kInDsvFl | kTypeDsvMask, "Input port" },
  66. { NULL, 0, 0, 0, 0 }
  67. };
  68. cmDspPrinter_t* p = cmDspInstAlloc(cmDspPrinter_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  69. cmDspSetDefaultUInt(ctx,&p->inst,kMsPrId, 0, 0 );
  70. return &p->inst;
  71. }
  72. cmDspRC_t _cmDspPrinterReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  73. {
  74. cmDspPrinter_t* p = (cmDspPrinter_t*)inst;
  75. p->limitCycles = ctx->cycleCnt;
  76. return kOkDspRC;
  77. }
  78. cmDspRC_t _cmDspPrinterRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  79. {
  80. cmDspPrinter_t* p = (cmDspPrinter_t*)inst;
  81. if( evt->dstVarId == kInPrId && ctx->cycleCnt >= p->limitCycles )
  82. {
  83. p->limitCycles = ctx->cycleCnt + (unsigned)(cmDspUInt(inst,kMsPrId) * cmDspSampleRate(ctx) / (1000.0 * cmDspSamplesPerCycle(ctx)) );
  84. const cmChar_t* lbl = cmDspStrcz(inst,kLblPrId);
  85. if( cmDsvIsSymbol(evt->valuePtr) )
  86. cmRptPrintf(ctx->rpt,"%s'%s'",lbl==NULL?"":lbl,cmStringNullGuard(cmSymTblLabel(ctx->stH,cmDsvSymbol(evt->valuePtr))));
  87. else
  88. cmDsvPrint(evt->valuePtr,lbl,ctx->rpt);
  89. cmRptPrint(ctx->rpt,"\n");
  90. }
  91. return kOkDspRC;
  92. }
  93. cmDspClass_t* cmPrinterClassCons( cmDspCtx_t* ctx )
  94. {
  95. cmDspClassSetup(&_cmPrinterDC,ctx,"Printer",
  96. NULL,
  97. _cmDspPrinterAlloc,
  98. NULL,
  99. _cmDspPrinterReset,
  100. NULL,
  101. _cmDspPrinterRecv,
  102. NULL,
  103. NULL,
  104. "Print the value of any event arriving at 'in'.");
  105. return &_cmPrinterDC;
  106. }
  107. //------------------------------------------------------------------------------------------------------------
  108. //)
  109. //( { label:cmDspCounter file_desc:"Counter unit." kw:[sunit] }
  110. enum
  111. {
  112. kMinCntId,
  113. kMaxCntId,
  114. kIncCntId,
  115. kWrapCntId,
  116. kResetCntId,
  117. kOutCntId,
  118. kCycCntId,
  119. kNxtCntId,
  120. };
  121. cmDspClass_t _cmCounterDC;
  122. typedef struct
  123. {
  124. cmDspInst_t inst;
  125. double val;
  126. bool disableFl;
  127. } cmDspCounter_t;
  128. cmDspRC_t _cmDspCounterValidate( cmDspInst_t* inst, double min, double max, double inc )
  129. {
  130. if( max < min )
  131. return cmErrMsg(&inst->classPtr->err,kInvalidArgDspRC,"The counter maximum (%f) value must be greater than the counter minimum (%f) value.",max,min);
  132. if( max - min < inc )
  133. return cmErrMsg(&inst->classPtr->err,kInvalidArgDspRC,"The counter increment value (%f) must be less than or equal to the maximum - minimum difference (%f).",inc,max-min);
  134. return kOkDspRC;
  135. }
  136. cmDspInst_t* _cmDspCounterAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  137. {
  138. cmDspVarArg_t args[] =
  139. {
  140. { "min", kMinCntId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "minimum" },
  141. { "max", kMaxCntId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "maximum" },
  142. { "inc", kIncCntId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "increment"},
  143. { "wrap", kWrapCntId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "wrap"},
  144. { "reset", kResetCntId,0, 0, kInDsvFl | kTypeDsvMask, "reset"},
  145. { "out", kOutCntId, 0, 0, kOutDsvFl | kDoubleDsvFl, "out"},
  146. { "cycles", kCycCntId, 0, 0, kOutDsvFl | kDoubleDsvFl, "cycles"},
  147. { "next", kNxtCntId, 0, 0, kInDsvFl | kTypeDsvMask, "next"},
  148. { NULL, 0, 0, 0, 0 }
  149. };
  150. cmDspCounter_t* p = cmDspInstAlloc(cmDspCounter_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  151. double min = cmDspDefaultDouble( &p->inst, kMinCntId );
  152. double max = cmDspDefaultDouble( &p->inst, kMaxCntId );
  153. double inc = cmDspDefaultDouble( &p->inst, kIncCntId );
  154. if( _cmDspCounterValidate(&p->inst, min, max, inc ) != kOkDspRC )
  155. return NULL;
  156. cmDspSetDefaultBool( ctx, &p->inst, kWrapCntId, false, true);
  157. cmDspSetDefaultDouble( ctx, &p->inst, kOutCntId, 0.0, min );
  158. cmDspSetDefaultDouble( ctx, &p->inst, kCycCntId, 0.0, 0.0 );
  159. return &p->inst;
  160. }
  161. cmDspRC_t _cmDspCounterReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  162. {
  163. cmDspCounter_t* p = (cmDspCounter_t*)inst;
  164. cmDspApplyAllDefaults(ctx,inst);
  165. p->val = cmDspDouble(inst,kMinCntId);
  166. p->disableFl = false; // the default values must be ok or the constructor fails
  167. return kOkDspRC;
  168. }
  169. void _cmDspCounterIncr( cmDspCtx_t* ctx, cmDspInst_t* inst )
  170. {
  171. cmDspCounter_t* p = (cmDspCounter_t*)inst;
  172. double min = cmDspDouble(inst,kMinCntId);
  173. double max = cmDspDouble(inst,kMaxCntId);
  174. double inc = cmDspDouble(inst,kIncCntId);
  175. bool wrapFl = cmDspBool(inst,kWrapCntId);
  176. bool limitFl = min <= max;
  177. // If min > max then no upper/lower limit is set on the value.
  178. // In this case the ouput will continue to increment and
  179. // no 'cycle' output will be generated.
  180. // If wrapFl is not set then the 'cycle' output will fire
  181. // exactly once when the counter crosses its limit.
  182. // if the new value is in range then send it
  183. if( min <= p->val && p->val < max )
  184. cmDspSetDouble( ctx, inst, kOutCntId, p->val );
  185. // the current value is out of range and wrap flag is not set
  186. if( limitFl && (p->val < min || p->val >= max) && (wrapFl==false))
  187. return;
  188. // do the increment
  189. p->val += inc;
  190. // if the new value is out of range
  191. if( limitFl && (p->val < min || p->val >= max) )
  192. {
  193. // if wrapping is allowed
  194. if( wrapFl )
  195. {
  196. if( p->val >= max )
  197. p->val = min + (p->val - max); // wrap to begin
  198. else
  199. if( p->val < min )
  200. p->val = max - (min - p->val); // wrap to end
  201. }
  202. // increment the cycle counter
  203. cmDspSetDouble( ctx, inst, kCycCntId, cmDspDouble( inst, kCycCntId ) + 1 );
  204. }
  205. }
  206. cmDspRC_t _cmDspCounterRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  207. {
  208. cmDspCounter_t* p = (cmDspCounter_t*)inst;
  209. cmDspRC_t rc = kOkDspRC;
  210. switch( evt->dstVarId )
  211. {
  212. case kWrapCntId:
  213. cmDspSetEvent(ctx,inst, evt );
  214. break;
  215. case kMinCntId:
  216. case kMaxCntId:
  217. case kIncCntId:
  218. {
  219. cmDspSetEvent( ctx, inst, evt );
  220. double min = cmDspDouble( inst, kMinCntId );
  221. double max = cmDspDouble( inst, kMaxCntId );
  222. double inc = cmDspDouble( inst, kIncCntId );
  223. p->disableFl = (rc = _cmDspCounterValidate(inst, min, max, inc)) != kOkDspRC;
  224. }
  225. break;
  226. case kNxtCntId:
  227. if( !p->disableFl )
  228. _cmDspCounterIncr(ctx,inst);
  229. break;
  230. case kResetCntId: // any msg on the 'reset' port causes the min value to be sent on the following 'next'
  231. p->val = cmDspDouble(inst,kMinCntId);
  232. break;
  233. default:
  234. { assert(0); }
  235. }
  236. return rc;
  237. }
  238. cmDspClass_t* cmCounterClassCons( cmDspCtx_t* ctx )
  239. {
  240. cmDspClassSetup(&_cmCounterDC,ctx,"Counter",
  241. NULL,
  242. _cmDspCounterAlloc,
  243. NULL,
  244. _cmDspCounterReset,
  245. NULL,
  246. _cmDspCounterRecv,
  247. NULL,
  248. NULL,
  249. "Counter object. Set min => max to have no limit on the value." );
  250. return &_cmCounterDC;
  251. }
  252. //------------------------------------------------------------------------------------------------------------
  253. //)
  254. //( { label:cmDspPhasor file_desc:"Ramp signal generator." kw:[sunit] }
  255. enum
  256. {
  257. kMaxPhId,
  258. kMultPhId,
  259. kPhsPhId,
  260. kOutPhId
  261. };
  262. cmDspClass_t _cmPhasorDC;
  263. typedef struct
  264. {
  265. cmDspInst_t inst;
  266. } cmDspPhasor_t;
  267. cmDspInst_t* _cmDspPhasorAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  268. {
  269. unsigned chs = 1;
  270. cmDspVarArg_t args[] =
  271. {
  272. { "max", kMaxPhId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Maximum accumulator value" },
  273. { "mult", kMultPhId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Increment multiplier" },
  274. { "phs", kPhsPhId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Next phase value" },
  275. { "out", kOutPhId, 0, chs, kOutDsvFl | kAudioBufDsvFl, "Audio output." },
  276. { NULL, 0, 0, 0, 0 }
  277. };
  278. // allocate the instance
  279. cmDspPhasor_t* p = cmDspInstAlloc(cmDspPhasor_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  280. // assign default values to any of the the optional arg's which may not have been set from vl.
  281. cmDspSetDefaultSample(ctx, &p->inst, kMaxPhId, 0.0, cmSample_MAX);
  282. cmDspSetDefaultSample(ctx, &p->inst, kMultPhId, 0.0, 1.0);
  283. cmDspSetDefaultDouble(ctx, &p->inst, kPhsPhId, 0.0, 0.0);
  284. return &p->inst;
  285. }
  286. cmDspRC_t _cmDspPhasorReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  287. {
  288. cmDspApplyAllDefaults(ctx,inst);
  289. cmDspZeroAudioBuf( ctx, inst, kOutPhId );
  290. return kOkDspRC;
  291. }
  292. cmDspRC_t _cmDspPhasorExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  293. {
  294. cmSample_t* bp = cmDspAudioBuf(ctx,inst,kOutPhId,0);
  295. const cmSample_t* ep = bp + cmDspAudioBufSmpCount(ctx,inst,kOutPhId,0);
  296. cmSample_t mult = cmDspSample(inst,kMultPhId);
  297. cmSample_t max = cmDspSample(inst,kMaxPhId);
  298. double phs = cmDspDouble(inst,kPhsPhId);
  299. cmSample_t inc = mult;
  300. for(; bp<ep; ++bp)
  301. {
  302. while( phs >= max )
  303. phs -= max;
  304. *bp = (cmSample_t)phs;
  305. phs += inc;
  306. }
  307. cmDspSetSample(ctx,inst,kPhsPhId,phs);
  308. return kOkDspRC;
  309. }
  310. cmDspRC_t _cmDspPhasorRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  311. {
  312. switch( evt->dstVarId )
  313. {
  314. case kMultPhId:
  315. case kMaxPhId:
  316. case kPhsPhId:
  317. cmDspSetEvent(ctx, inst, evt );;
  318. break;
  319. default:
  320. { assert(0); }
  321. }
  322. return kOkDspRC;
  323. }
  324. cmDspClass_t* cmPhasorClassCons( cmDspCtx_t* ctx )
  325. {
  326. cmDspClassSetup(&_cmPhasorDC,ctx,"Phasor",
  327. NULL,
  328. _cmDspPhasorAlloc,
  329. NULL,
  330. _cmDspPhasorReset,
  331. _cmDspPhasorExec,
  332. _cmDspPhasorRecv,
  333. NULL,
  334. NULL,
  335. "Ramp wave signal generator.");
  336. return &_cmPhasorDC;
  337. }
  338. //------------------------------------------------------------------------------------------------------------
  339. //)
  340. //( { label:cmDspSigGen file_desc:"Programmable periodic and noise signal generator." kw:[sunit] }
  341. enum
  342. {
  343. kHzSgId,
  344. kShapeSgId,
  345. kGainSgId,
  346. kOtCntSgId,
  347. kOutSgId
  348. };
  349. enum
  350. {
  351. kWhiteSgId, // 0
  352. kPinkSgId, // 1
  353. kSineSgId, // 2
  354. kCosSgId, // 3
  355. kSawSgId, // 4
  356. kSqrSgId, // 5
  357. kTriSgId, // 6
  358. kPulseSgId, // 7
  359. kPhasorSgId // 8
  360. };
  361. cmDspClass_t _cmSigGenDC;
  362. typedef struct
  363. {
  364. cmDspInst_t inst;
  365. cmReal_t phs;
  366. cmSample_t reg;
  367. } cmDspSigGen_t;
  368. cmDspInst_t* _cmDspSigGenAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  369. {
  370. cmDspVarArg_t args[] =
  371. {
  372. { "hz", kHzSgId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Signal frequency in Hertz." },
  373. { "shape", kShapeSgId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Wave shape 0=sine 1=cosine 2=white 3=pink" },
  374. { "gain", kGainSgId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Output gain."},
  375. { "ot", kOtCntSgId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Overtone count."},
  376. { "out", kOutSgId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output." },
  377. { NULL, 0, 0, 0, 0 }
  378. };
  379. cmDspSigGen_t* p = cmDspInstAlloc(cmDspSigGen_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  380. cmDspSetDefaultDouble(ctx, &p->inst, kHzSgId, 0.0, 1000);
  381. cmDspSetDefaultUInt( ctx, &p->inst, kShapeSgId, 0, 0);
  382. cmDspSetDefaultUInt( ctx, &p->inst, kOtCntSgId, 0, 0);
  383. cmDspSetDefaultDouble(ctx, &p->inst, kGainSgId, 0.0, 0.9 );
  384. return &p->inst;
  385. }
  386. cmDspRC_t _cmDspSigGenReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  387. {
  388. cmDspSigGen_t* p = (cmDspSigGen_t*)inst;
  389. p->phs = 0;
  390. cmDspApplyAllDefaults(ctx,inst);
  391. cmDspZeroAudioBuf( ctx, inst, kOutSgId );
  392. p->reg = 0;
  393. return kOkDspRC;
  394. }
  395. cmDspRC_t _cmDspSigGenExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  396. {
  397. cmDspSigGen_t* p = (cmDspSigGen_t*)inst;
  398. unsigned chIdx = 0;
  399. cmSample_t* bp = cmDspAudioBuf(ctx,inst,kOutSgId,chIdx);
  400. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutSgId,chIdx);
  401. const cmSample_t* ep = bp + n;
  402. double hz = cmDspDouble(inst,kHzSgId);
  403. double sr = cmDspSampleRate(ctx);
  404. double fact = 2.0 * M_PI * hz / sr;
  405. int shape = cmDspUInt(inst,kShapeSgId);
  406. double offs = shape == kCosSgId ? (-M_PI / 2.0) : 0;
  407. double gain = cmDspDouble(inst,kGainSgId);
  408. unsigned otCnt = cmDspUInt(inst,kOtCntSgId);
  409. switch( shape )
  410. {
  411. case kWhiteSgId:
  412. while( bp < ep )
  413. *bp++ = gain * 2.0 * ((cmSample_t)rand() / RAND_MAX - 0.5);
  414. break;
  415. case kPinkSgId:
  416. while( bp < ep )
  417. {
  418. cmSample_t s = gain * 2.0 * ((cmSample_t)rand() / RAND_MAX - 0.5);
  419. *bp++ = (s + p->reg)/2;
  420. p->reg = s;
  421. }
  422. break;
  423. case kSineSgId:
  424. case kCosSgId:
  425. while( bp<ep )
  426. {
  427. *bp++ = (cmSample_t)(gain * sin( fact * p->phs + offs ));
  428. p->phs += 1.0;
  429. }
  430. break;
  431. case kSawSgId:
  432. p->phs = cmVOS_SynthSawtooth(bp,n,(unsigned)p->phs,sr,hz,otCnt);
  433. cmVOS_MultVS(bp,n,gain);
  434. break;
  435. case kSqrSgId:
  436. p->phs = cmVOS_SynthSquare( bp,n,(unsigned)p->phs,sr,hz,otCnt );
  437. cmVOS_MultVS(bp,n,gain);
  438. break;
  439. case kTriSgId:
  440. p->phs = cmVOS_SynthTriangle( bp,n,(unsigned)p->phs,sr,hz,otCnt );
  441. cmVOS_MultVS(bp,n,gain);
  442. break;
  443. case kPulseSgId:
  444. p->phs = cmVOS_SynthPulseCos( bp,n,(unsigned)p->phs,sr,hz,otCnt );
  445. cmVOS_MultVS(bp,n,gain);
  446. break;
  447. case kPhasorSgId:
  448. p->phs = cmVOS_SynthPhasor( bp,n,(unsigned)p->phs,sr,hz );
  449. cmVOS_MultVS(bp,n,gain);
  450. break;
  451. default:
  452. { assert(0); }
  453. }
  454. return kOkDspRC;
  455. }
  456. cmDspRC_t _cmDspSigGenRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  457. {
  458. cmDspRC_t rc;
  459. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  460. {
  461. switch( evt->dstVarId )
  462. {
  463. case kShapeSgId:
  464. //printf("%s %i\n",cmDspInstLabel(ctx,inst),cmDspUInt(inst,kShapeSgId));
  465. break;
  466. }
  467. }
  468. return rc;
  469. }
  470. cmDspClass_t* cmSigGenClassCons( cmDspCtx_t* ctx )
  471. {
  472. cmDspClassSetup(&_cmSigGenDC,ctx,"SigGen",
  473. NULL,
  474. _cmDspSigGenAlloc,
  475. NULL,
  476. _cmDspSigGenReset,
  477. _cmDspSigGenExec,
  478. _cmDspSigGenRecv,
  479. NULL,
  480. NULL,
  481. "Variable frequency and waveshape signal generator." );
  482. return &_cmSigGenDC;
  483. }
  484. //------------------------------------------------------------------------------------------------------------
  485. //)
  486. //( { label:cmDspMidiIn file_desc:"MIDI input port." kw:[sunit] }
  487. enum
  488. {
  489. kInDeviceMiId,
  490. kInPortMiId,
  491. kDeviceMiId,
  492. kPortMiId,
  493. kSmpIdxMiId,
  494. kStatusMiId,
  495. kD0MiId,
  496. kD1MiId,
  497. kSecMiId,
  498. kNSecMiId
  499. };
  500. cmDspClass_t _cmMidiInDC;
  501. typedef struct
  502. {
  503. cmDspInst_t inst;
  504. unsigned midiSymId;
  505. unsigned prevSmpIdx;
  506. cmTimeSpec_t prevTimeStamp;
  507. unsigned inDevIdx;
  508. unsigned inPortIdx;
  509. } cmDspMidiIn_t;
  510. cmDspRC_t _cmDspMidiInSetDevice( cmDspCtx_t* ctx, cmDspMidiIn_t* p, const cmChar_t* deviceStr )
  511. {
  512. cmDspRC_t rc = kOkDspRC;
  513. if( deviceStr != NULL )
  514. {
  515. if((p->inDevIdx = cmMpDeviceNameToIndex(deviceStr)) == cmInvalidIdx )
  516. rc = cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The MIDI input device '%s' could not be found.",cmStringNullGuard(deviceStr));
  517. p->inPortIdx = cmInvalidIdx; // set the port as invalid until it is explicitely set.
  518. }
  519. return rc;
  520. }
  521. cmDspRC_t _cmDspMidiInSetPort( cmDspCtx_t* ctx, cmDspMidiIn_t* p, const cmChar_t* portStr )
  522. {
  523. cmDspRC_t rc = kOkDspRC;
  524. if( portStr == NULL )
  525. return rc;
  526. if( p->inDevIdx == cmInvalidIdx )
  527. rc = cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The MIDI input port cannot be set until the MIDI input device is set.");
  528. else
  529. {
  530. if((p->inPortIdx = cmMpDevicePortNameToIndex(p->inDevIdx,kOutMpFl,portStr)) == cmInvalidIdx )
  531. rc = cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The MIDI input port '%s' could not be found on device '%s'.",cmStringNullGuard(portStr),cmStringNullGuard(cmMpDeviceName(p->inDevIdx)));
  532. }
  533. return rc;
  534. }
  535. cmDspInst_t* _cmDspMidiInAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  536. {
  537. cmDspVarArg_t args[] =
  538. {
  539. { "indev", kInDeviceMiId, 0, 0, kInDsvFl | kStrzDsvFl | kOptArgDsvFl, "Listen only to this MIDI device"},
  540. { "inport", kInPortMiId, 0, 0, kInDsvFl | kStrzDsvFl | kOptArgDsvFl, "Listen only to this MIDI port"},
  541. { "device", kDeviceMiId, 0, 0, kOutDsvFl | kUIntDsvFl, "MIDI device" },
  542. { "port", kPortMiId, 0, 0, kOutDsvFl | kUIntDsvFl, "MIDI device port"},
  543. { "smpidx", kSmpIdxMiId, 0, 0, kOutDsvFl | kUIntDsvFl, "Message time tag as sample index."},
  544. { "status", kStatusMiId, 0, 0, kOutDsvFl | kUIntDsvFl, "MIDI status" },
  545. { "d0", kD0MiId, 0, 0, kOutDsvFl | kUIntDsvFl, "MIDI channel message d0" },
  546. { "d1", kD1MiId, 0, 0, kOutDsvFl | kUIntDsvFl, "MIDI channel message d1" },
  547. { "sec", kSecMiId, 0, 0, kOutDsvFl | kUIntDsvFl, "Time stamp integer seconds."},
  548. { "nsec", kNSecMiId, 0, 0, kOutDsvFl | kUIntDsvFl, "Time stamp fractional second (nanoseconds)."},
  549. { NULL, 0, 0, 0, 0 }
  550. };
  551. cmDspMidiIn_t* p = cmDspInstAlloc(cmDspMidiIn_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  552. p->inDevIdx = cmInvalidIdx;
  553. p->inPortIdx = cmInvalidIdx;
  554. p->midiSymId = cmDspSysAssignInstAttrSymbolStr( ctx->dspH, &p->inst, "_midi" );
  555. return &p->inst;
  556. }
  557. cmDspRC_t _cmDspMidiInReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  558. {
  559. cmDspRC_t rc = kOkDspRC;
  560. cmDspMidiIn_t* p = (cmDspMidiIn_t*)inst;
  561. cmDspApplyAllDefaults(ctx,inst);
  562. if(_cmDspMidiInSetDevice(ctx,p,cmDspStrcz(inst,kInDeviceMiId)) == kOkDspRC )
  563. _cmDspMidiInSetPort( ctx,p,cmDspStrcz(inst,kInPortMiId));
  564. p->prevSmpIdx = 0;
  565. return rc;
  566. }
  567. cmDspRC_t _cmDspMidiInRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  568. {
  569. cmDspMidiIn_t* p = (cmDspMidiIn_t*)inst;
  570. switch( evt->dstVarId )
  571. {
  572. case kInDeviceMiId:
  573. _cmDspMidiInSetDevice(ctx, p, cmDsvStrcz(evt->valuePtr));
  574. break;
  575. case kInPortMiId:
  576. _cmDspMidiInSetPort(ctx, p, cmDsvStrcz(evt->valuePtr));
  577. break;
  578. }
  579. return kOkDspRC;
  580. }
  581. cmDspRC_t _cmDspMidiInRecvFunc( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned attrSymId, const cmDspValue_t* value )
  582. {
  583. cmDspMidiIn_t* p = (cmDspMidiIn_t*)inst;
  584. if( attrSymId == p->midiSymId )
  585. {
  586. cmMidiPacket_t* pkt = (cmMidiPacket_t*)(value->u.m.u.vp);
  587. unsigned i;
  588. if( (p->inDevIdx==cmInvalidIdx || p->inDevIdx==pkt->devIdx) && (p->inPortIdx==cmInvalidIdx || p->inPortIdx==pkt->portIdx ) )
  589. {
  590. cmDspSetUInt(ctx, inst, kDeviceMiId, pkt->devIdx);
  591. cmDspSetUInt(ctx, inst, kPortMiId, pkt->portIdx);
  592. for(i=0; i<pkt->msgCnt; ++i)
  593. {
  594. cmMidiMsg* m = pkt->msgArray + i;
  595. unsigned deltaSmpCnt = 0;
  596. if( p->prevTimeStamp.tv_sec!=0 && p->prevTimeStamp.tv_nsec!=0 )
  597. deltaSmpCnt = floor(cmTimeElapsedMicros(&p->prevTimeStamp,&m->timeStamp) * cmDspSampleRate(ctx) / 1000000.0);
  598. if( p->prevSmpIdx == 0 )
  599. p->prevSmpIdx = ctx->cycleCnt * cmDspSamplesPerCycle(ctx);
  600. else
  601. p->prevSmpIdx += deltaSmpCnt;
  602. cmDspSetUInt(ctx, inst, kSmpIdxMiId, p->prevSmpIdx );
  603. cmDspSetUInt(ctx, inst, kSecMiId, m->timeStamp.tv_sec);
  604. cmDspSetUInt(ctx, inst, kNSecMiId, m->timeStamp.tv_nsec);
  605. cmDspSetUInt(ctx, inst, kD1MiId, m->d1 );
  606. cmDspSetUInt(ctx, inst, kD0MiId, m->d0 );
  607. cmDspSetUInt(ctx, inst, kStatusMiId, m->status );
  608. p->prevTimeStamp = m->timeStamp;
  609. }
  610. }
  611. }
  612. return kOkDspRC;
  613. }
  614. cmDspClass_t* cmMidiInClassCons( cmDspCtx_t* ctx )
  615. {
  616. cmDspClassSetup(&_cmMidiInDC,ctx,"MidiIn",
  617. NULL,
  618. _cmDspMidiInAlloc,
  619. NULL,
  620. _cmDspMidiInReset,
  621. NULL,
  622. _cmDspMidiInRecv,
  623. NULL,
  624. _cmDspMidiInRecvFunc,
  625. "Midi input port");
  626. return &_cmMidiInDC;
  627. }
  628. //------------------------------------------------------------------------------------------------------------
  629. //)
  630. //( { label:cmDspMidiOut file_desc:"MIDI output port." kw:[sunit] }
  631. enum
  632. {
  633. kDeviceMoId,
  634. kPortMoId,
  635. kStatusMoId,
  636. kD0MoId,
  637. kD1MoId,
  638. kResetMoId
  639. };
  640. cmDspClass_t _cmMidiOutDC;
  641. typedef struct
  642. {
  643. cmDspInst_t inst;
  644. unsigned devIdx;
  645. unsigned portIdx;
  646. bool enableFl;
  647. } cmDspMidiOut_t;
  648. cmDspRC_t _cmDspMidiOutSetDevice( cmDspCtx_t* ctx, cmDspMidiOut_t* p, const cmChar_t* deviceStr )
  649. {
  650. cmDspRC_t rc = kOkDspRC;
  651. if( deviceStr != NULL )
  652. if((p->devIdx = cmMpDeviceNameToIndex(deviceStr)) == cmInvalidIdx )
  653. rc = cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The MIDI output device '%s' could not be found.",cmStringNullGuard(deviceStr));
  654. return rc;
  655. }
  656. cmDspRC_t _cmDspMidiOutSetPort( cmDspCtx_t* ctx, cmDspMidiOut_t* p, const cmChar_t* portStr )
  657. {
  658. cmDspRC_t rc = kOkDspRC;
  659. if( portStr == NULL )
  660. return rc;
  661. if( p->devIdx == cmInvalidIdx )
  662. rc = cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The MIDI port cannot be set until the MIDI device is set.");
  663. else
  664. {
  665. if((p->portIdx = cmMpDevicePortNameToIndex(p->devIdx,kOutMpFl,portStr)) == cmInvalidIdx )
  666. rc = cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The MIDI out port '%s' could not be found on device '%s'.",cmStringNullGuard(portStr),cmStringNullGuard(cmMpDeviceName(p->devIdx)));
  667. }
  668. return rc;
  669. }
  670. cmDspInst_t* _cmDspMidiOutAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  671. {
  672. cmDspVarArg_t args[] =
  673. {
  674. { "device", kDeviceMoId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "MIDI device name"},
  675. { "port", kPortMoId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "MIDI port name"},
  676. { "status", kStatusMoId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI status" },
  677. { "d0", kD0MoId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI channel message d0" },
  678. { "d1", kD1MoId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI channel message d1" },
  679. { "reset", kResetMoId, 0, 0, kInDsvFl | kTypeDsvMask,"All notes off" },
  680. { NULL, 0, 0, 0, 0 }
  681. };
  682. cmDspMidiOut_t* p = cmDspInstAlloc(cmDspMidiOut_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  683. p->devIdx = cmInvalidIdx;
  684. p->portIdx = cmInvalidIdx;
  685. cmDspSetDefaultUInt(ctx,&p->inst, kStatusMoId, 0, 0 );
  686. cmDspSetDefaultUInt(ctx,&p->inst, kD0MoId, 0, 0 );
  687. cmDspSetDefaultUInt(ctx,&p->inst, kD1MoId, 0, 0 );
  688. return &p->inst;
  689. }
  690. cmDspRC_t _cmDspMidiOutReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  691. {
  692. cmDspRC_t rc = kOkDspRC;
  693. cmDspMidiOut_t* p = (cmDspMidiOut_t*)inst;
  694. cmDspApplyAllDefaults(ctx,inst);
  695. p->enableFl = false;
  696. if(_cmDspMidiOutSetDevice(ctx,p,cmDspStrcz(inst,kDeviceMoId)) == kOkDspRC )
  697. p->enableFl = _cmDspMidiOutSetPort( ctx,p,cmDspStrcz(inst,kPortMoId)) == kOkDspRC;
  698. return rc;
  699. }
  700. cmDspRC_t _cmDspMidiOutRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  701. {
  702. cmDspMidiOut_t* p = (cmDspMidiOut_t*)inst;
  703. switch( evt->dstVarId )
  704. {
  705. case kDeviceMoId:
  706. if(_cmDspMidiOutSetDevice(ctx, p, cmDsvStrcz(evt->valuePtr) ) != kOkDspRC )
  707. p->enableFl = false;
  708. break;
  709. case kPortMoId:
  710. if( _cmDspMidiOutSetPort(ctx, p, cmDsvStrcz(evt->valuePtr) ) != kOkDspRC )
  711. p->enableFl = false;
  712. break;
  713. case kStatusMoId:
  714. if( p->devIdx != cmInvalidIdx && p->portIdx != cmInvalidIdx )
  715. {
  716. unsigned status = cmDsvGetUInt(evt->valuePtr);
  717. unsigned d0 = cmDspUInt(inst,kD0MoId);
  718. unsigned d1 = cmDspUInt(inst,kD1MoId);
  719. if( p->enableFl )
  720. if( cmMpDeviceSend( p->devIdx, p->portIdx, status, d0, d1 ) != kOkMpRC )
  721. cmDspInstErr(ctx,inst,kInvalidArgDspRC,"MIDI send failed.");
  722. }
  723. break;
  724. case kResetMoId:
  725. {
  726. unsigned i;
  727. if( p->enableFl )
  728. for(i=0; i<kMidiChCnt; ++i)
  729. {
  730. cmMpDeviceSend(p->devIdx,p->portIdx,kCtlMdId+i,121,0); // reset all controllers
  731. cmMpDeviceSend(p->devIdx,p->portIdx,kCtlMdId+i,123,0); // turn all notes off
  732. cmSleepMs(15);
  733. }
  734. }
  735. break;
  736. default:
  737. cmDspSetEvent(ctx,inst,evt);
  738. break;
  739. }
  740. return kOkDspRC;
  741. }
  742. cmDspClass_t* cmMidiOutClassCons( cmDspCtx_t* ctx )
  743. {
  744. cmDspClassSetup(&_cmMidiOutDC,ctx,"MidiOut",
  745. NULL,
  746. _cmDspMidiOutAlloc,
  747. NULL,
  748. _cmDspMidiOutReset,
  749. NULL,
  750. _cmDspMidiOutRecv,
  751. NULL,
  752. NULL,
  753. "Midi input port");
  754. return &_cmMidiOutDC;
  755. }
  756. //------------------------------------------------------------------------------------------------------------
  757. //)
  758. //( { label:cmDspAudioIn file_desc:"Audio input port." kw:[sunit] }
  759. enum
  760. {
  761. kChAiId,
  762. kGainAiId,
  763. kOutAiId
  764. };
  765. cmDspClass_t _cmAudioInDC;
  766. typedef struct
  767. {
  768. cmDspInst_t inst;
  769. bool errFl; // used to control error reporting
  770. } cmDspAudioIn_t;
  771. cmDspInst_t* _cmDspAudioInAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  772. {
  773. cmDspVarArg_t args[] =
  774. {
  775. { "ch", kChAiId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Audio input channel index"},
  776. { "gain", kGainAiId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Input gain multiplier" },
  777. { "out", kOutAiId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output" },
  778. { NULL, 0, 0, 0, 0 }
  779. };
  780. cmDspAudioIn_t* p = cmDspInstAlloc(cmDspAudioIn_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  781. cmDspSetDefaultUInt( ctx, &p->inst, kChAiId, 0, 0);
  782. cmDspSetDefaultDouble( ctx, &p->inst, kGainAiId, 0, 1.0);
  783. return &p->inst;
  784. }
  785. cmDspRC_t _cmDspAudioInReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  786. {
  787. cmDspRC_t rc = kOkDspRC;
  788. cmDspAudioIn_t* p = (cmDspAudioIn_t*)inst;
  789. p->errFl = false;
  790. cmDspApplyAllDefaults(ctx,inst);
  791. return rc;
  792. }
  793. cmDspRC_t _cmDspAudioInExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  794. {
  795. unsigned chIdx = cmDspUInt(inst,kChAiId);
  796. unsigned iChCnt = ctx->ctx->iChCnt;
  797. cmDspAudioIn_t* p = (cmDspAudioIn_t*)inst;
  798. double gain = cmDspDouble(inst,kGainAiId);
  799. if( chIdx >= iChCnt )
  800. {
  801. if( p->errFl== false )
  802. {
  803. cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The input channel index %i is invalid. Channel count:%i.",chIdx,iChCnt);
  804. p->errFl = true;
  805. }
  806. cmDspZeroAudioBuf(ctx,inst,kOutAiId);
  807. return kOkDspRC;
  808. }
  809. //unsigned n = cmDspSamplesPerCycle(ctx);
  810. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutAiId,0);
  811. cmSample_t* dp = cmDspAudioBuf(ctx,inst,kOutAiId,0);
  812. assert( n == cmDspAudioBufSmpCount(ctx,inst,kOutAiId,chIdx));
  813. assert(dp != NULL);
  814. // if this channel is disabled then iChArray[chIdx] will be NULL
  815. if( ctx->ctx->iChArray[chIdx]!=NULL )
  816. cmVOS_MultVVS(dp,n,ctx->ctx->iChArray[chIdx],(cmSample_t)gain);
  817. return kOkDspRC;
  818. }
  819. cmDspRC_t _cmDspAudioInRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  820. {
  821. cmDspRC_t rc = kOkDspRC;
  822. switch( evt->dstVarId )
  823. {
  824. case kChAiId:
  825. if( cmDspSetEvent(ctx,inst,evt) != kOkDspRC )
  826. {
  827. // if the exec callback was previously disabled and the new channel value is valid then re-enable the exec callback.
  828. if( inst->execFunc==NULL && cmDspUInt(inst,kChAiId) < ctx->ctx->iChCnt )
  829. inst->execFunc = _cmDspAudioInExec;
  830. }
  831. break;
  832. case kGainAiId:
  833. cmDspSetEvent(ctx,inst,evt);
  834. break;
  835. }
  836. return rc;
  837. }
  838. cmDspClass_t* cmAudioInClassCons( cmDspCtx_t* ctx )
  839. {
  840. cmDspClassSetup(&_cmAudioInDC,ctx,"AudioIn",
  841. NULL,
  842. _cmDspAudioInAlloc,
  843. NULL,
  844. _cmDspAudioInReset,
  845. _cmDspAudioInExec,
  846. _cmDspAudioInRecv,
  847. NULL,
  848. NULL,
  849. "Audio output port");
  850. return &_cmAudioInDC;
  851. }
  852. //------------------------------------------------------------------------------------------------------------
  853. //)
  854. //( { label:cmDspAudioOut file_desc:"Audio output port." kw:[sunit] }
  855. enum
  856. {
  857. kChAoId,
  858. kGainAoId,
  859. kInAoId
  860. };
  861. cmDspClass_t _cmAudioOutDC;
  862. typedef struct
  863. {
  864. cmDspInst_t inst;
  865. } cmDspAudioOut_t;
  866. cmDspInst_t* _cmDspAudioOutAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  867. {
  868. cmDspVarArg_t args[] =
  869. {
  870. { "ch", kChAoId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Audio output channel index"},
  871. { "gain",kGainAoId,0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Output gain multiplier"},
  872. { "in", kInAoId, 0, 1, kInDsvFl | kAudioBufDsvFl, "Audio input" },
  873. { NULL, 0, 0, 0, 0 }
  874. };
  875. cmDspAudioOut_t* p = cmDspInstAlloc(cmDspAudioOut_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  876. cmDspSetDefaultUInt( ctx, &p->inst, kChAoId, 0, 0);
  877. cmDspSetDefaultDouble( ctx, &p->inst, kGainAoId, 0, 1.0);
  878. return &p->inst;
  879. }
  880. cmDspRC_t _cmDspAudioOutReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  881. {
  882. cmDspRC_t rc = kOkDspRC;
  883. cmDspApplyAllDefaults(ctx,inst);
  884. return rc;
  885. }
  886. cmDspRC_t _cmDspAudioOutExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  887. {
  888. cmDspRC_t rc = kOkDspRC;
  889. unsigned chIdx = cmDspUInt(inst,kChAoId);
  890. unsigned oChCnt = ctx->ctx->oChCnt;
  891. double gain = cmDspDouble(inst,kGainAoId);
  892. if( chIdx >= oChCnt )
  893. {
  894. rc = cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The output channel index %i is invalid. Channel count:%i.",chIdx,oChCnt);
  895. inst->execFunc = NULL; // disable callbacks - this prevents the error msg from repeating
  896. return rc;
  897. }
  898. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kInAoId,0);
  899. if( sp == NULL )
  900. {
  901. inst->execFunc = NULL; // if there is no connected input then disable further callbacks
  902. return kOkDspRC;
  903. }
  904. unsigned n = cmDspSamplesPerCycle(ctx);
  905. assert( n == cmDspVarRows(inst,kInAoId) );
  906. // if this channel is disabled or set to pass-through then chArray[chIdx] will be NULL
  907. if( ctx->ctx->oChArray[chIdx] != NULL )
  908. cmVOS_MultVVS(ctx->ctx->oChArray[chIdx],n,sp,(cmSample_t)gain);
  909. return kOkDspRC;
  910. }
  911. cmDspRC_t _cmDspAudioOutRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  912. {
  913. cmDspRC_t rc = kOkDspRC;
  914. switch( evt->dstVarId )
  915. {
  916. case kChAoId:
  917. if( cmDspSetEvent(ctx,inst,evt) != kOkDspRC )
  918. {
  919. // if the exec callback was previously disabled and the new channel value is valid then re-enable the exec callback.
  920. if( inst->execFunc==NULL && cmDspUInt(inst,kChAoId) < ctx->ctx->oChCnt )
  921. inst->execFunc = _cmDspAudioOutExec;
  922. }
  923. break;
  924. case kGainAoId:
  925. cmDspSetEvent(ctx,inst,evt);
  926. break;
  927. }
  928. return rc;
  929. }
  930. cmDspClass_t* cmAudioOutClassCons( cmDspCtx_t* ctx )
  931. {
  932. cmDspClassSetup(&_cmAudioOutDC,ctx,"AudioOut",
  933. NULL,
  934. _cmDspAudioOutAlloc,
  935. NULL,
  936. _cmDspAudioOutReset,
  937. _cmDspAudioOutExec,
  938. _cmDspAudioOutRecv,
  939. NULL,NULL,
  940. "Audio output port");
  941. return &_cmAudioOutDC;
  942. }
  943. //------------------------------------------------------------------------------------------------------------
  944. //)
  945. //( { label:cmDspAudioFileOut file_desc:"Audio output port which is sent to an audio file." kw:[sunit] }
  946. enum
  947. {
  948. kFnAofId,
  949. kChCntAofId,
  950. kGain0AofId,
  951. kGain1AofId,
  952. kIn0AofId,
  953. kIn1AofId,
  954. kSelAofId
  955. };
  956. cmDspClass_t _cmAudioFileOutDC;
  957. typedef struct
  958. {
  959. cmDspInst_t inst;
  960. cmSample_t* smpBuf;
  961. unsigned smpCnt;
  962. unsigned bits;
  963. cmAudioFileH_t afH;
  964. unsigned openSymId;
  965. unsigned closeSymId;
  966. const cmChar_t* afn;
  967. } cmDspAudioFileOut_t;
  968. cmDspRC_t _cmDspAudioFileOutCreateFile( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned chCnt )
  969. {
  970. cmDspRC_t rc = kOkDspRC;
  971. cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;
  972. const cmChar_t* fn = cmDspStrcz(inst,kFnAofId);
  973. if(cmAudioFileIsValid(p->afH) )
  974. cmAudioFileDelete(&p->afH);
  975. // if the supplied audio file name is actually a directory name then generate a file name
  976. if( cmFsIsDir(fn) )
  977. {
  978. cmMemPtrFree(&p->afn);
  979. if( cmFsGenFn(fn,"recd","aiff",&p->afn) != kOkFsRC )
  980. return cmDspInstErr(ctx,&p->inst,kFileSysFailDspRC,"An output audio file name could not be generated.");
  981. fn = p->afn;
  982. }
  983. if( cmAudioFileIsValid(p->afH = cmAudioFileNewCreate(fn, cmDspSampleRate(ctx), p->bits, chCnt, &rc, ctx->rpt )) == false )
  984. rc = cmDspClassErr(ctx,inst->classPtr,kVarArgParseFailDspRC,"The output audio file '%s' create failed.",fn);
  985. return rc;
  986. }
  987. cmDspInst_t* _cmDspAudioFileOutAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  988. {
  989. cmDspVarArg_t args[] =
  990. {
  991. { "fn", kFnAofId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Audio file or directory name"},
  992. { "chs", kChCntAofId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Channel count"},
  993. { "gain0", kGain0AofId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Output gain 0 multiplier"},
  994. { "gain1", kGain1AofId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Output gain 1 multiplier"},
  995. { "in0", kIn0AofId, 0, 1, kInDsvFl | kAudioBufDsvFl, "Audio input 0"},
  996. { "in1", kIn1AofId, 0, 1, kInDsvFl | kAudioBufDsvFl, "Audio input 1"},
  997. { "sel", kSelAofId, 0, 0, kInDsvFl, "Open | Close"},
  998. { NULL, 0, 0, 0, 0 }
  999. };
  1000. cmDspAudioFileOut_t* p = cmDspInstAlloc(cmDspAudioFileOut_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1001. cmDspValue_t chCntVal;
  1002. cmDsvSetUInt(&chCntVal,cmMin(2,cmDspUInt(&p->inst,kChCntAofId)));
  1003. cmDspSetDefault(ctx, &p->inst, kChCntAofId, &chCntVal );
  1004. cmDspSetDefaultDouble( ctx, &p->inst, kGain0AofId, 0, 1.0);
  1005. cmDspSetDefaultDouble( ctx, &p->inst, kGain1AofId, 0, 1.0);
  1006. p->bits = 16;
  1007. p->afH = cmNullAudioFileH;
  1008. p->openSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"open");
  1009. p->closeSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"close");
  1010. return &p->inst;
  1011. }
  1012. cmDspRC_t _cmDspAudioFileOutReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1013. {
  1014. cmDspRC_t rc = kOkDspRC;
  1015. cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;
  1016. unsigned chCnt = cmDspUInt(inst,kChCntAofId);
  1017. cmDspApplyAllDefaults(ctx,inst);
  1018. p->smpCnt = cmDspSamplesPerCycle(ctx) * chCnt;
  1019. p->smpBuf = cmLhResizeN(ctx->lhH, cmSample_t, p->smpBuf, p->smpCnt);
  1020. //rc = _cmDspAudioFileOutCreateFile( ctx, inst, chCnt );
  1021. return rc;
  1022. }
  1023. cmDspRC_t _cmDspAudioFileOutExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1024. {
  1025. cmDspRC_t rc = kOkDspRC;
  1026. cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;;
  1027. unsigned chCnt = cmMin(2,cmDspUInt(inst,kChCntAofId));
  1028. unsigned smpCnt = 0;
  1029. cmSample_t* chArray[chCnt];
  1030. unsigned i,j;
  1031. if(!cmAudioFileIsValid(p->afH) )
  1032. return rc;
  1033. for(i=0,j=0; i<chCnt; ++i)
  1034. {
  1035. unsigned chVarId = i == 0 ? kIn0AofId : kIn1AofId; // get audio buf var id for this ch
  1036. unsigned iSmpCnt = cmDspVarRows(inst,chVarId);
  1037. if( iSmpCnt == 0 )
  1038. {
  1039. chArray[j] = NULL;
  1040. }
  1041. else
  1042. {
  1043. cmSample_t gain = cmDspSample(inst,i==0?kGain0AofId:kGain1AofId); // get ch gain
  1044. chArray[j] = cmDspAudioBuf(ctx,inst,chVarId,0); // get incoming audio buf ptr
  1045. if( gain != 1.0 )
  1046. cmVOS_MultVVS(chArray[j], iSmpCnt, chArray[j], gain); // apply gain
  1047. ++j; // incr chArray[] index
  1048. assert( smpCnt==0 || iSmpCnt==smpCnt);
  1049. smpCnt = iSmpCnt; // set outgoing sample count
  1050. }
  1051. }
  1052. // write the samples
  1053. if( cmAudioFileWriteSample(p->afH, smpCnt, j, chArray ) != kOkAfRC )
  1054. rc = cmDspClassErr(ctx,inst->classPtr,kFileWriteFailDspRC,"An audio output file write failed.");
  1055. return rc;
  1056. }
  1057. cmDspRC_t _cmDspAudioFileOutRecv( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1058. {
  1059. cmDspRC_t rc = kOkDspRC;
  1060. switch( evt->dstVarId )
  1061. {
  1062. case kGain1AofId:
  1063. case kGain0AofId:
  1064. cmDspSetEvent(ctx,inst,evt);
  1065. break;
  1066. case kFnAofId:
  1067. cmDspSetEvent(ctx,inst,evt);
  1068. rc = _cmDspAudioFileOutCreateFile(ctx,inst,cmDspUInt(inst,kChCntAofId));
  1069. break;
  1070. case kSelAofId:
  1071. {
  1072. cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;
  1073. unsigned symId = cmDsvSymbol(evt->valuePtr);
  1074. if( symId == p->openSymId )
  1075. rc = _cmDspAudioFileOutCreateFile(ctx,inst,cmDspUInt(inst,kChCntAofId));
  1076. else
  1077. {
  1078. if( symId == p->closeSymId )
  1079. {
  1080. if(cmAudioFileIsValid(p->afH) )
  1081. cmAudioFileDelete(&p->afH);
  1082. }
  1083. else
  1084. {
  1085. rc = cmErrMsg(&inst->classPtr->err,kInvalidArgDspRC,"Unknown selector symbol (%i) %s.",symId,cmStringNullGuard(cmSymTblLabel(ctx->stH,symId)));
  1086. }
  1087. }
  1088. }
  1089. }
  1090. return rc;
  1091. }
  1092. cmDspRC_t _cmDspAudioFileOutFree( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evtPtr )
  1093. {
  1094. cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;
  1095. if(cmAudioFileIsValid(p->afH) )
  1096. cmAudioFileDelete(&p->afH);
  1097. cmMemPtrFree(&p->afn);
  1098. return kOkDspRC;
  1099. }
  1100. cmDspClass_t* cmAudioFileOutClassCons( cmDspCtx_t* ctx )
  1101. {
  1102. cmDspClassSetup(&_cmAudioFileOutDC,ctx,"AudioFileOut",
  1103. NULL,
  1104. _cmDspAudioFileOutAlloc,
  1105. _cmDspAudioFileOutFree,
  1106. _cmDspAudioFileOutReset,
  1107. _cmDspAudioFileOutExec,
  1108. _cmDspAudioFileOutRecv,
  1109. NULL,NULL,
  1110. "Audio file output port");
  1111. return &_cmAudioFileOutDC;
  1112. }
  1113. //------------------------------------------------------------------------------------------------------------
  1114. //)
  1115. //( { label:cmDspScalar file_desc:"User interface unit which represents a single scalar value." kw:[sunit] }
  1116. enum
  1117. {
  1118. kTypScId,
  1119. kMinScId,
  1120. kMaxScId,
  1121. kStpScId,
  1122. kValScId,
  1123. kLblScId,
  1124. kSendScId
  1125. };
  1126. cmDspClass_t _cmScalarDC;
  1127. typedef struct
  1128. {
  1129. cmDspInst_t inst;
  1130. } cmDspScalar_t;
  1131. cmDspInst_t* _cmDspScalarAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1132. {
  1133. cmDspVarArg_t args[] =
  1134. {
  1135. { "typ", kTypScId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Type" },
  1136. { "min", kMinScId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Minimum value"},
  1137. { "max", kMaxScId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Maximum value"},
  1138. { "step", kStpScId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Step value (set to 0 to ignore)"},
  1139. { "val", kValScId, 0, 0, kInDsvFl | kOutDsvFl | kDoubleDsvFl | kReqArgDsvFl | kSendDfltDsvFl, "Current value"},
  1140. { "lbl", kLblScId, 0, 0, kStrzDsvFl | kOptArgDsvFl, "Label"},
  1141. { "send", kSendScId, 0, 0, kInDsvFl | kTypeDsvMask, "Send value on any msg."},
  1142. { NULL, 0, 0, 0, 0 }
  1143. };
  1144. cmDspScalar_t* p = cmDspInstAlloc(cmDspScalar_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1145. cmDspSetDefaultUInt( ctx, &p->inst, kTypScId, 0, kNumberDuiId);
  1146. cmDspSetDefaultDouble(ctx, &p->inst, kMinScId, 0.0, 0);
  1147. cmDspSetDefaultDouble(ctx, &p->inst, kMaxScId, 0.0, 1);
  1148. cmDspSetDefaultDouble(ctx, &p->inst, kStpScId, 0.0, 0);
  1149. unsigned typeId = cmDspDefaultUInt(&p->inst,kTypScId);
  1150. // create the UI control
  1151. cmDspUiScalarCreate(ctx,&p->inst,typeId,kMinScId,kMaxScId,kStpScId,kValScId,kLblScId);
  1152. return &p->inst;
  1153. }
  1154. cmDspRC_t _cmDspScalarReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1155. {
  1156. cmDspApplyAllDefaults(ctx,inst);
  1157. return kOkDspRC;
  1158. }
  1159. cmDspRC_t _cmDspScalarRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1160. {
  1161. if( evt->dstVarId == kSendScId )
  1162. {
  1163. // NOT SURE IF THIS IS CORRECT OR NOT - IT SHOULD FORCE THE CURRENT VALUE TO BE
  1164. // SENT - THE CURRENT VALUE SHOULD ALREADY BE AT THE UI SO THERE DOESN'T SEEM
  1165. // TO BE ANY REASON TO UPDATE IT THERE.
  1166. cmDspSetDouble( ctx, inst, kValScId, cmDspDouble(inst,kValScId ));
  1167. return kOkDspRC;
  1168. }
  1169. switch( evt->dstVarId )
  1170. {
  1171. case kMinScId:
  1172. case kMaxScId:
  1173. case kStpScId:
  1174. case kValScId:
  1175. cmDspSetEvent(ctx,inst,evt);
  1176. break;
  1177. default:
  1178. {assert(0);}
  1179. }
  1180. return kOkDspRC;
  1181. }
  1182. cmDspRC_t _cmDspScalarPresetRdWr( cmDspCtx_t* ctx, cmDspInst_t* inst, bool storeFl )
  1183. {
  1184. return cmDspVarPresetRdWr(ctx,inst,kValScId,storeFl);
  1185. }
  1186. cmDspClass_t* cmScalarClassCons( cmDspCtx_t* ctx )
  1187. {
  1188. cmDspClassSetup(&_cmScalarDC,ctx,"Scalar",
  1189. NULL,
  1190. _cmDspScalarAlloc,
  1191. NULL,
  1192. _cmDspScalarReset,
  1193. NULL,
  1194. _cmDspScalarRecv,
  1195. _cmDspScalarPresetRdWr,
  1196. NULL,
  1197. "Scalar value control.");
  1198. return &_cmScalarDC;
  1199. }
  1200. //------------------------------------------------------------------------------------------------------------
  1201. //)
  1202. //( { label:cmDspText file_desc:"User interface unit which allows text input." kw:[sunit] }
  1203. enum
  1204. {
  1205. kValTxId,
  1206. kLblTxId
  1207. };
  1208. cmDspClass_t _cmTextDC;
  1209. typedef struct
  1210. {
  1211. cmDspInst_t inst;
  1212. } cmDspText_t;
  1213. cmDspInst_t* _cmDspTextAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1214. {
  1215. cmDspVarArg_t args[] =
  1216. {
  1217. { "val", kValTxId, 0, 0, kInDsvFl | kOutDsvFl | kStrzDsvFl | kReqArgDsvFl | kSendDfltDsvFl, "Current string"},
  1218. { "lbl", kLblTxId, 0, 0, kStrzDsvFl | kOptArgDsvFl, "Label"},
  1219. { NULL, 0, 0, 0, 0 }
  1220. };
  1221. cmDspText_t* p = cmDspInstAlloc(cmDspText_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1222. // create the UI control
  1223. cmDspUiTextCreate(ctx,&p->inst,kValTxId,kLblTxId);
  1224. return &p->inst;
  1225. }
  1226. cmDspRC_t _cmDspTextReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1227. {
  1228. cmDspApplyAllDefaults(ctx,inst);
  1229. return kOkDspRC;
  1230. }
  1231. cmDspRC_t _cmDspTextRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1232. {
  1233. switch( evt->dstVarId )
  1234. {
  1235. case kValTxId:
  1236. cmDspSetEvent(ctx,inst,evt);
  1237. break;
  1238. default:
  1239. {assert(0);}
  1240. }
  1241. return kOkDspRC;
  1242. }
  1243. cmDspClass_t* cmTextClassCons( cmDspCtx_t* ctx )
  1244. {
  1245. cmDspClassSetup(&_cmTextDC,ctx,"Text",
  1246. NULL,
  1247. _cmDspTextAlloc,
  1248. NULL,
  1249. _cmDspTextReset,
  1250. NULL,
  1251. _cmDspTextRecv,
  1252. NULL,NULL,
  1253. "Text value control.");
  1254. return &_cmTextDC;
  1255. }
  1256. //------------------------------------------------------------------------------------------------------------
  1257. //)
  1258. //( { label:cmDspMeter file_desc:"User interface progress bar which displays the current value of a scalar variable." kw:[sunit] }
  1259. enum
  1260. {
  1261. kInMtId,
  1262. kMinMtId,
  1263. kMaxMtId,
  1264. kTimeMtId,
  1265. kLblMtId,
  1266. };
  1267. cmDspClass_t _cmMeterDC;
  1268. typedef struct
  1269. {
  1270. cmDspInst_t inst;
  1271. unsigned updSmpCnt;
  1272. unsigned lastCycleCnt;
  1273. double value;
  1274. unsigned cnt;
  1275. } cmDspMeter_t;
  1276. cmDspInst_t* _cmDspMeterAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1277. {
  1278. cmDspVarArg_t args[] =
  1279. {
  1280. { "in", kInMtId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Current value"},
  1281. { "min", kMinMtId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Minimum value"},
  1282. { "max", kMaxMtId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Maximum value"},
  1283. { "time", kTimeMtId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "UI update time in milliseconds (default:50)"},
  1284. { "label",kLblMtId, 0, 0, kStrzDsvFl, "Label"},
  1285. { NULL, 0, 0, 0, 0 }
  1286. };
  1287. cmDspMeter_t* p = cmDspInstAlloc(cmDspMeter_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1288. cmDspSetDefaultDouble(ctx, &p->inst, kInMtId, 0.0, 0);
  1289. cmDspSetDefaultDouble(ctx, &p->inst, kMinMtId, 0.0, 0);
  1290. cmDspSetDefaultDouble(ctx, &p->inst, kMaxMtId, 0.0, 1);
  1291. cmDspSetDefaultDouble(ctx, &p->inst, kTimeMtId, 0.0, 50.0);
  1292. // create the UI control
  1293. cmDspUiMeterCreate(ctx,&p->inst,kMinMtId,kMaxMtId,kInMtId,kLblMtId);
  1294. return &p->inst;
  1295. }
  1296. cmDspRC_t _cmDspMeterReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1297. {
  1298. cmDspMeter_t* p = (cmDspMeter_t*)inst;
  1299. cmDspApplyAllDefaults(ctx,inst);
  1300. double updateMs = cmDspDouble(inst,kTimeMtId);
  1301. p->updSmpCnt = floor(cmDspSampleRate(ctx) * updateMs / 1000.0);
  1302. p->lastCycleCnt = 0;
  1303. p->cnt = 0;
  1304. p->value = 0;
  1305. return kOkDspRC;
  1306. }
  1307. cmDspRC_t _cmDspMeterExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1308. {
  1309. cmDspMeter_t* p = (cmDspMeter_t*)inst;
  1310. double curVal = p->value;
  1311. bool deltaFl = p->cnt!=0 && curVal != cmDspDouble(inst,kInMtId);
  1312. bool expireFl = (ctx->cycleCnt - p->lastCycleCnt) * cmDspSamplesPerCycle(ctx) > p->updSmpCnt;
  1313. // if the meter value changed and the update time has expired
  1314. if( deltaFl && expireFl )
  1315. {
  1316. cmDspSetDouble(ctx,inst,kInMtId,curVal);
  1317. p->value = 0;
  1318. p->cnt = 0;
  1319. p->lastCycleCnt = ctx->cycleCnt;
  1320. }
  1321. return kOkDspRC;
  1322. }
  1323. cmDspRC_t _cmDspMeterRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1324. {
  1325. cmDspMeter_t* p = (cmDspMeter_t*)inst;
  1326. switch( evt->dstVarId )
  1327. {
  1328. case kInMtId:
  1329. p->value = cmDsvGetDouble(evt->valuePtr);
  1330. ++p->cnt;
  1331. return kOkDspRC;
  1332. case kTimeMtId:
  1333. p->updSmpCnt = floor(cmDspSampleRate(ctx) * cmDsvGetDouble(evt->valuePtr) / 1000.0);
  1334. break;
  1335. }
  1336. return cmDspSetEvent(ctx,inst,evt);
  1337. }
  1338. cmDspClass_t* cmMeterClassCons( cmDspCtx_t* ctx )
  1339. {
  1340. cmDspClassSetup(&_cmMeterDC,ctx,"Meter",
  1341. NULL,
  1342. _cmDspMeterAlloc,
  1343. NULL,
  1344. _cmDspMeterReset,
  1345. _cmDspMeterExec,
  1346. _cmDspMeterRecv,
  1347. NULL,NULL,
  1348. "Meter display.");
  1349. return &_cmMeterDC;
  1350. }
  1351. //------------------------------------------------------------------------------------------------------------
  1352. //)
  1353. //( { label:cmDspLabel file_desc:"User interface unit which displays read-only text." kw:[sunit] }
  1354. enum
  1355. {
  1356. kInLbId,
  1357. kAlignLbId
  1358. };
  1359. cmDspClass_t _cmLabelDC;
  1360. typedef struct
  1361. {
  1362. cmDspInst_t inst;
  1363. } cmDspLabel_t;
  1364. cmDspInst_t* _cmDspLabelAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1365. {
  1366. cmDspVarArg_t args[] =
  1367. {
  1368. { "in", kInLbId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "LabelText" },
  1369. { "align",kAlignLbId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Alignment 0=right 1=left 2=center" },
  1370. { NULL, 0, 0, 0, 0 }
  1371. };
  1372. cmDspLabel_t* p = cmDspInstAlloc(cmDspLabel_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1373. cmDspSetDefaultDouble(ctx, &p->inst, kAlignLbId, 0.0, kLeftAlignDuiId);
  1374. // create the UI control
  1375. cmDspUiLabelCreate(ctx,&p->inst,kInLbId,kAlignLbId);
  1376. return &p->inst;
  1377. }
  1378. cmDspRC_t _cmDspLabelReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1379. {
  1380. cmDspApplyAllDefaults(ctx,inst);
  1381. return kOkDspRC;
  1382. }
  1383. cmDspRC_t _cmDspLabelRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1384. {
  1385. return cmDspSetEvent(ctx,inst,evt);
  1386. }
  1387. cmDspClass_t* cmLabelClassCons( cmDspCtx_t* ctx )
  1388. {
  1389. cmDspClassSetup(&_cmLabelDC,ctx,"Label",
  1390. NULL,
  1391. _cmDspLabelAlloc,
  1392. NULL,
  1393. _cmDspLabelReset,
  1394. NULL,
  1395. _cmDspLabelRecv,
  1396. NULL,NULL,
  1397. "Label control.");
  1398. return &_cmLabelDC;
  1399. }
  1400. //------------------------------------------------------------------------------------------------------------
  1401. //)
  1402. //( { label:cmDspButton file_desc:"User interface sends a user defined value from a graphic button press." kw:[sunit] }
  1403. enum
  1404. {
  1405. kTypBtId,
  1406. kOutBtId,
  1407. kSymBtId,
  1408. kLblBtId,
  1409. kInBtId
  1410. };
  1411. cmDspClass_t _cmButtonDC;
  1412. typedef struct
  1413. {
  1414. cmDspInst_t inst;
  1415. unsigned resetSymId;
  1416. } cmDspButton_t;
  1417. cmDspInst_t* _cmDspButtonAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1418. {
  1419. va_list vl1;
  1420. va_copy(vl1,vl);
  1421. assert( va_cnt >= 1 );
  1422. unsigned typeId = va_arg(vl,unsigned);
  1423. // check buttons should transmit their default values - push buttons should not.
  1424. unsigned sendDfltFl = typeId == kCheckDuiId ? kSendDfltDsvFl : 0;
  1425. cmDspVarArg_t args[] =
  1426. {
  1427. { "typ", kTypBtId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Type" },
  1428. { "out", kOutBtId, 0, 0, kOutDsvFl | kDoubleDsvFl | kOptArgDsvFl | sendDfltFl, "Value"},
  1429. { "sym", kSymBtId, 0, 0, kOutDsvFl | kSymDsvFl | kOptArgDsvFl | sendDfltFl, "Symbol Value"},
  1430. { "label",kLblBtId, 0, 0, kInDsvFl | kStrzDsvFl | kOptArgDsvFl, "Label"},
  1431. { "in", kInBtId, 0, 0, kInDsvFl | kTypeDsvMask, "Simulate UI"},
  1432. { NULL, 0, 0, 0, 0 }
  1433. };
  1434. cmDspButton_t* p = cmDspInstAlloc(cmDspButton_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl1);
  1435. cmDspSetDefaultDouble(ctx, &p->inst, kOutBtId, 0.0, typeId==kCheckDuiId ? 0.0 : 1.0);
  1436. cmDspSetDefaultSymbol(ctx, &p->inst, kSymBtId, instSymId );
  1437. cmDspSetDefaultStrcz( ctx, &p->inst, kLblBtId, NULL, cmSymTblLabel(ctx->stH,instSymId));
  1438. p->resetSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"_reset");
  1439. // create the UI control
  1440. cmDspUiButtonCreate(ctx,&p->inst,typeId,kOutBtId,kLblBtId);
  1441. return &p->inst;
  1442. }
  1443. cmDspRC_t _cmDspButtonReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1444. {
  1445. cmDspApplyAllDefaults(ctx,inst);
  1446. return kOkDspRC;
  1447. }
  1448. cmDspRC_t _cmDspButtonRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1449. {
  1450. // the 'in' port is the only input port
  1451. // but the UI sends button pushes use kOutBtId - should this be changed?
  1452. assert( evt->dstVarId == kInBtId || evt->dstVarId == kOutBtId );
  1453. // We accept all types at the 'in' port but are only interested
  1454. // in transmitting doubles from the 'out' port.
  1455. if( cmDsvCanConvertFlags( kDoubleDsvFl, evt->valuePtr->flags ) )
  1456. {
  1457. // Redirect events which can be converted to type kDoubleDsvFl
  1458. // to the output port.
  1459. //
  1460. // Convert the event dest var id from the 'kInBtId' to 'kOutBtId'
  1461. // and update the UI with the incoming value
  1462. cmDspSetEventUiId(ctx,inst,evt,kOutBtId);
  1463. }
  1464. // no matter what kind of msg enters the 'in' port send a symbol out the 'sym' port
  1465. if( inst->symId != cmInvalidId )
  1466. cmDspSetSymbol( ctx, inst, kSymBtId, inst->symId );
  1467. return kOkDspRC;
  1468. }
  1469. cmDspRC_t _cmDspButtonPresetRdWr( cmDspCtx_t* ctx, cmDspInst_t* inst, bool storeFl )
  1470. {
  1471. cmDspRC_t rc = kOkDspRC;
  1472. if( cmDspUInt(inst,kTypBtId) == kCheckDuiId )
  1473. rc = cmDspVarPresetRdWr(ctx,inst,kOutBtId,storeFl);
  1474. return rc;
  1475. }
  1476. cmDspRC_t _cmDspButtonSysRecvFunc( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned attrSymId, const cmDspValue_t* value )
  1477. {
  1478. cmDspButton_t* p = (cmDspButton_t*)inst;
  1479. if( attrSymId == p->resetSymId )
  1480. {
  1481. cmDspSetSymbol( ctx, inst, kSymBtId, p->resetSymId );
  1482. cmDspSetDouble(ctx,inst,kOutBtId, cmDspDouble(inst,kOutBtId));
  1483. }
  1484. return kOkDspRC;
  1485. }
  1486. cmDspClass_t* cmButtonClassCons( cmDspCtx_t* ctx )
  1487. {
  1488. cmDspClassSetup(&_cmButtonDC,ctx,"Button",
  1489. NULL,
  1490. _cmDspButtonAlloc,
  1491. NULL,
  1492. _cmDspButtonReset,
  1493. NULL,
  1494. _cmDspButtonRecv,
  1495. _cmDspButtonPresetRdWr,
  1496. _cmDspButtonSysRecvFunc,
  1497. "Button control.");
  1498. return &_cmButtonDC;
  1499. }
  1500. //------------------------------------------------------------------------------------------------------------
  1501. //)
  1502. //( { label:cmDspCheckbox file_desc:"Graphic checkbox user interface unit." kw:[sunit] }
  1503. enum
  1504. {
  1505. kLblCbId,
  1506. kSym1CbId,
  1507. kSym0CbId,
  1508. kVal1CbId,
  1509. kVal0CbId,
  1510. kOutCbId,
  1511. kSymCbId,
  1512. kInCbId
  1513. };
  1514. cmDspClass_t _cmCheckboxDC;
  1515. typedef struct
  1516. {
  1517. cmDspInst_t inst;
  1518. unsigned resetSymId;
  1519. unsigned onSymId;
  1520. unsigned offSymId;
  1521. } cmDspCheckbox_t;
  1522. cmDspInst_t* _cmDspCheckboxAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1523. {
  1524. // Check buttons should transmit their default values. Set kSendDfltDsvFl on outputs to send default values.
  1525. cmDspVarArg_t args[] =
  1526. {
  1527. { "label",kLblCbId, 0, 0, kInDsvFl | kStrzDsvFl | kOptArgDsvFl, "Label"},
  1528. { "sym1", kSym1CbId, 0, 0, kInDsvFl | kStrzDsvFl | kOptArgDsvFl, "'on' symbol value"},
  1529. { "sym0", kSym0CbId, 0, 0, kInDsvFl | kStrzDsvFl | kOptArgDsvFl, "'off' symbol value"},
  1530. { "val1", kVal1CbId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "'on' value"},
  1531. { "val0", kVal0CbId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "'off' value"},
  1532. { "out", kOutCbId, 0, 0, kOutDsvFl | kDoubleDsvFl | kOptArgDsvFl | kSendDfltDsvFl, "Value"},
  1533. { "sym", kSymCbId, 0, 0, kOutDsvFl | kSymDsvFl | kSendDfltDsvFl, "Symbol Value"},
  1534. { "in", kInCbId, 0, 0, kInDsvFl | kTypeDsvMask, "Simulate UI"},
  1535. { NULL, 0, 0, 0, 0 }
  1536. };
  1537. cmDspCheckbox_t* p = cmDspInstAlloc(cmDspCheckbox_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1538. p->resetSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"_reset");
  1539. cmDspSetDefaultStrcz( ctx, &p->inst, kSym1CbId, NULL, "on");
  1540. cmDspSetDefaultStrcz( ctx, &p->inst, kSym0CbId, NULL, "off");
  1541. p->onSymId = cmSymTblRegisterSymbol(ctx->stH, cmDspDefaultStrcz(&p->inst,kSym1CbId));
  1542. p->offSymId = cmSymTblRegisterSymbol(ctx->stH, cmDspDefaultStrcz(&p->inst,kSym0CbId));
  1543. bool fl = cmDspDefaultDouble(&p->inst,kOutCbId)!=0;
  1544. cmDspSetDefaultDouble(ctx, &p->inst, kVal1CbId, 0.0, 1.0);
  1545. cmDspSetDefaultDouble(ctx, &p->inst, kVal0CbId, 0.0, 0.0);
  1546. cmDspSetDefaultDouble(ctx, &p->inst, kOutCbId, 0.0, 0.0);
  1547. cmDspSetDefaultSymbol(ctx, &p->inst, kSymCbId, fl ? p->onSymId : p->offSymId );
  1548. cmDspSetDefaultStrcz( ctx, &p->inst, kLblCbId, NULL, cmSymTblLabel(ctx->stH,instSymId));
  1549. // create the UI control
  1550. cmDspUiButtonCreate(ctx,&p->inst,kCheckDuiId,kOutCbId,kLblCbId);
  1551. return &p->inst;
  1552. }
  1553. cmDspRC_t _cmDspCheckboxReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1554. {
  1555. cmDspApplyAllDefaults(ctx,inst);
  1556. return kOkDspRC;
  1557. }
  1558. cmDspRC_t _cmDspCheckboxRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1559. {
  1560. cmDspRC_t rc = kOkDspRC;
  1561. cmDspCheckbox_t* p = (cmDspCheckbox_t*)inst;
  1562. switch( evt->dstVarId )
  1563. {
  1564. case kLblCbId:
  1565. // TODO: we have no function for changing a UI control label.
  1566. return rc;
  1567. case kVal1CbId:
  1568. case kVal0CbId:
  1569. case kSym1CbId:
  1570. case kSym0CbId:
  1571. cmDspSetEvent(ctx,inst,evt);
  1572. if( evt->dstVarId == kSym1CbId )
  1573. p->onSymId = cmSymTblRegisterSymbol(ctx->stH, cmDspStrcz(&p->inst,kSym1CbId));
  1574. if( evt->dstVarId == kSym0CbId )
  1575. p->offSymId = cmSymTblRegisterSymbol(ctx->stH, cmDspStrcz(&p->inst,kSym0CbId));
  1576. return rc;
  1577. }
  1578. // the 'in' port is the only input port
  1579. // but the UI button pushes use kOutCbId.
  1580. assert( evt->dstVarId == kInCbId || evt->dstVarId == kOutCbId );
  1581. // We accept all types at the 'in' port but are only interested
  1582. // in transmitting doubles from the 'out' port and symbols from
  1583. // the 'sym' port.
  1584. if( cmDsvCanConvertFlags( kDoubleDsvFl, evt->valuePtr->flags ) )
  1585. {
  1586. bool checkFl = cmDsvGetDouble(evt->valuePtr)!=0;
  1587. unsigned valId = checkFl ? kVal1CbId : kVal0CbId;
  1588. unsigned symId = checkFl ? p->onSymId : p->offSymId;
  1589. // Redirect events which can be converted to type kDoubleDsvFl
  1590. // to the output port.
  1591. //
  1592. // Convert the event dest var id from the 'kInCbId' to 'kOutCbId'
  1593. // and update the UI with the incoming value
  1594. cmDspEvt_t e;
  1595. cmDspValue_t v;
  1596. cmDspEvtCopy(&e,evt);
  1597. e.valuePtr = &v;
  1598. cmDsvSetDouble(&v,cmDspDouble(inst,valId));
  1599. cmDspSetEventUiId(ctx,inst,evt,kOutCbId);
  1600. cmDspSetSymbol( ctx, inst, kSymCbId, symId);
  1601. }
  1602. return kOkDspRC;
  1603. }
  1604. cmDspRC_t _cmDspCheckboxPresetRdWr( cmDspCtx_t* ctx, cmDspInst_t* inst, bool storeFl )
  1605. {
  1606. return cmDspVarPresetRdWr(ctx,inst,kOutCbId,storeFl);
  1607. }
  1608. cmDspRC_t _cmDspCheckboxSysRecvFunc( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned attrSymId, const cmDspValue_t* value )
  1609. {
  1610. cmDspCheckbox_t* p = (cmDspCheckbox_t*)inst;
  1611. if( attrSymId == p->resetSymId )
  1612. {
  1613. cmDspSetSymbol( ctx, inst, kSymCbId, p->resetSymId );
  1614. cmDspSetDouble(ctx,inst,kOutCbId, cmDspDouble(inst,kOutCbId));
  1615. }
  1616. return kOkDspRC;
  1617. }
  1618. cmDspClass_t* cmCheckboxClassCons( cmDspCtx_t* ctx )
  1619. {
  1620. cmDspClassSetup(&_cmCheckboxDC,ctx,"Checkbox",
  1621. NULL,
  1622. _cmDspCheckboxAlloc,
  1623. NULL,
  1624. _cmDspCheckboxReset,
  1625. NULL,
  1626. _cmDspCheckboxRecv,
  1627. _cmDspCheckboxPresetRdWr,
  1628. _cmDspCheckboxSysRecvFunc,
  1629. "Checkbox control.");
  1630. return &_cmCheckboxDC;
  1631. }
  1632. //------------------------------------------------------------------------------------------------------------
  1633. //)
  1634. //( { label:cmDspReorder file_desc:"Collect inputs in any order and transmit them in a defined order." kw:[sunit] }
  1635. cmDspClass_t _cmReorderDC;
  1636. typedef struct
  1637. {
  1638. cmDspInst_t inst;
  1639. unsigned portCnt; // count of input ports and count of output ports
  1640. unsigned* execFlArray; // execFlArray[portCnt] - true for ports which should cause obj to generate output
  1641. unsigned* orderArray; // orderArray[portCnt] - port output order map
  1642. } cmDspReorder_t;
  1643. cmDspInst_t* _cmDspReorderAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1644. {
  1645. if( va_cnt < 1 )
  1646. {
  1647. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"Reorder objects must have arguments.");
  1648. return NULL;
  1649. }
  1650. // the first argument is the count of input ports (which is also the count of output ports)
  1651. int portCnt = va_arg(vl,int);
  1652. if( portCnt < 2 )
  1653. {
  1654. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"Reorder objects must have at least 2 ports.");
  1655. return NULL;
  1656. }
  1657. cmDspVarArg_t args[portCnt*2+1];
  1658. unsigned i;
  1659. int maxLabelCharCnt = 15;
  1660. cmChar_t label[ maxLabelCharCnt+1 ];
  1661. label[maxLabelCharCnt] = 0;
  1662. for(i=0; i<portCnt*2; ++i)
  1663. {
  1664. snprintf(label,maxLabelCharCnt,"%s-%i", (i<portCnt?"in":"out"), i%portCnt);
  1665. unsigned symId = cmSymTblRegisterSymbol(ctx->stH,label);
  1666. args[i].label = cmSymTblLabel(ctx->stH,symId);
  1667. args[i].constId = i;
  1668. args[i].rn = 0;
  1669. args[i].cn = 0;
  1670. args[i].flags = (i<portCnt ? kInDsvFl : kOutDsvFl) | kTypeDsvMask;
  1671. args[i].doc = i<portCnt ? "Any input" : "Any output";
  1672. }
  1673. memset(args+i,0,sizeof(args[0]));
  1674. cmDspReorder_t* p = cmDspInstAlloc(cmDspReorder_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1675. if( p != NULL )
  1676. {
  1677. p->orderArray = cmLhAllocZ(ctx->lhH,unsigned,portCnt*2);
  1678. p->execFlArray = p->orderArray + portCnt;
  1679. p->portCnt = portCnt;
  1680. if( va_cnt-1 < portCnt )
  1681. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The reorder constructor must contain an output order map.");
  1682. else
  1683. {
  1684. for(i=0; i<portCnt; ++i)
  1685. {
  1686. int order = va_arg(vl,int);
  1687. if( order >= portCnt )
  1688. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The port order index %i is outside the valid range of 0-%i.",order,portCnt-1);
  1689. else
  1690. p->orderArray[ order ] = i;
  1691. }
  1692. va_cnt -= portCnt+1;
  1693. for(i=0; i<va_cnt; ++i)
  1694. {
  1695. int execPortIdx = va_arg(vl,int);
  1696. if( execPortIdx >= portCnt )
  1697. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The port exec index %i is outside the valid range of 0-%i.",execPortIdx,portCnt-1);
  1698. else
  1699. p->execFlArray[ execPortIdx ] = true;
  1700. }
  1701. }
  1702. }
  1703. return &p->inst;
  1704. }
  1705. cmDspRC_t _cmDspReorderFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1706. {
  1707. cmDspReorder_t* p = (cmDspReorder_t*)inst;
  1708. cmLhFree(ctx->lhH,p->orderArray);
  1709. return kOkDspRC;
  1710. }
  1711. cmDspRC_t _cmDspReorderRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1712. {
  1713. cmDspRC_t rc = kOkDspRC;
  1714. cmDspReorder_t* p = (cmDspReorder_t*)inst;
  1715. cmDspSetEvent(ctx,inst,evt);
  1716. if( evt->dstVarId < p->portCnt )
  1717. if( p->execFlArray[evt->dstVarId] )
  1718. {
  1719. unsigned i;
  1720. for(i=0; i<p->portCnt; ++i)
  1721. {
  1722. unsigned inVarId = p->orderArray[i];
  1723. unsigned outVarId = inVarId + p->portCnt;
  1724. const cmDspVar_t* varPtr = cmDspVarIdToCPtr(inst, inVarId );
  1725. assert(varPtr != NULL);
  1726. if((rc = cmDspValueSet(ctx, inst, outVarId, &varPtr->value, 0 )) != kOkDspRC )
  1727. break;
  1728. }
  1729. }
  1730. return rc;
  1731. }
  1732. cmDspClass_t* cmReorderClassCons( cmDspCtx_t* ctx )
  1733. {
  1734. cmDspClassSetup(&_cmReorderDC,ctx,"Reorder",
  1735. NULL,
  1736. _cmDspReorderAlloc,
  1737. _cmDspReorderFree,
  1738. NULL,
  1739. NULL,
  1740. _cmDspReorderRecv,
  1741. NULL,NULL,
  1742. "Reorder value control.");
  1743. return &_cmReorderDC;
  1744. }
  1745. //------------------------------------------------------------------------------------------------------------
  1746. //)
  1747. //( { label:cmDspFname file_desc:"User interface file or directory name input unit." kw:[sunit] }
  1748. enum
  1749. {
  1750. kDirFnId,
  1751. kPatFnId,
  1752. kValFnId,
  1753. kSendFnId
  1754. };
  1755. cmDspClass_t _cmFnameDC;
  1756. typedef struct
  1757. {
  1758. cmDspInst_t inst;
  1759. } cmDspFname_t;
  1760. // Pattern string for HTML and image files:
  1761. // "HTML Files (*.html)\tImage Files (*.{bmp,gif,jpg,png})"
  1762. // The va_list must include 3 args:
  1763. // A pointer to a string referning to a default filename or directory or NULL.
  1764. // A pointer to a string referring to a pattern string or NULL.
  1765. // A bool to set the 'dirFl'.
  1766. cmDspInst_t* _cmDspFnameAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1767. {
  1768. cmDspVarArg_t args[] =
  1769. {
  1770. { "dir", kDirFnId, 0, 0, kInDsvFl | kReqArgDsvFl | kBoolDsvFl, "Dir=true Filename=false" },
  1771. { "pat", kPatFnId, 0, 0, kInDsvFl | kOptArgDsvFl | kStrzDsvFl, "File pattern string (e.g. HTML Files (*.html)\tImage Files (*.{bmp,gif,jpg,png}))" },
  1772. { "out", kValFnId, 0, 0, kOutDsvFl | kOptArgDsvFl | kStrzDsvFl, "Current file or directory name." },
  1773. { "send", kSendFnId, 0, 0, kInDsvFl | kTypeDsvMask, "Send file name on any msg."},
  1774. { NULL, 0, 0, 0, 0 }
  1775. };
  1776. cmDspFname_t* p = cmDspInstAlloc(cmDspFname_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1777. cmDspSetDefaultBool( ctx, &p->inst, kDirFnId, false, false);
  1778. cmDspSetDefaultStrcz(ctx, &p->inst, kPatFnId, NULL, "All Files (*.*)");
  1779. cmDspSetDefaultStrcz(ctx, &p->inst, kValFnId, NULL, cmFsUserDir());
  1780. cmDspUiFnameCreate(ctx,&p->inst,kValFnId,kPatFnId,kDirFnId);
  1781. return &p->inst;
  1782. }
  1783. cmDspRC_t _cmDspFnameReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1784. {
  1785. cmDspApplyAllDefaults(ctx,inst);
  1786. return kOkDspRC;
  1787. }
  1788. cmDspRC_t _cmDspFnameRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1789. {
  1790. cmDspSetEvent(ctx,inst,evt);
  1791. switch( evt->dstVarId )
  1792. {
  1793. case kSendFnId:
  1794. cmDspSetStrcz(ctx, inst, kValFnId, cmDspStrcz(inst,kValFnId) );
  1795. break;
  1796. case kDirFnId:
  1797. case kPatFnId:
  1798. case kValFnId:
  1799. break;
  1800. default:
  1801. {assert(0);}
  1802. }
  1803. return kOkDspRC;
  1804. }
  1805. cmDspClass_t* cmFnameClassCons( cmDspCtx_t* ctx )
  1806. {
  1807. cmDspClassSetup(&_cmFnameDC,ctx,"Fname",
  1808. NULL,
  1809. _cmDspFnameAlloc,
  1810. NULL,
  1811. _cmDspFnameReset,
  1812. NULL,
  1813. _cmDspFnameRecv,
  1814. NULL,NULL,
  1815. "File or directory chooser control.");
  1816. return &_cmFnameDC;
  1817. }
  1818. //------------------------------------------------------------------------------------------------------------
  1819. //)
  1820. //( { label:cmDspMsgList file_desc:"User interface list unit." kw:[sunit] }
  1821. cmDspClass_t _cmMsgListDC;
  1822. typedef struct
  1823. {
  1824. cmDspInst_t inst;
  1825. cmJsonH_t jsH; // current JSON tree
  1826. cmJsonNode_t* np; // ptr to JSON array
  1827. unsigned colCnt; // number of elements in the JSON sub-arrays (rows)
  1828. cmJsonH_t dfltJsH; // default JSON tree (the default JSON array node ptr (np) is stored in the var array but we must also maintain the assoc'd JSON handle othwerwise the pointer will not be valid)
  1829. unsigned* typeIdArray; // JSON types for each column size: typeIdArray[ colCnt ]
  1830. unsigned symColCnt; // same as number of elements of typeIdArray[] == kStringTId
  1831. unsigned symRowCnt; // same as cmJsonChildCount(p->np)
  1832. unsigned* symM; // symM[symRowCnt,symColCnt] symbol matrix
  1833. } cmDspMsgList_t;
  1834. // create a matrix to hold the symbol id's associated with any string values
  1835. cmDspRC_t _cmDspMsgListLoadSymbolMtx( cmDspCtx_t* ctx, cmDspMsgList_t* p )
  1836. {
  1837. assert(cmJsonIsArray(p->np));
  1838. unsigned i,j,k;
  1839. // remove any existing symbols
  1840. if( p->symM != NULL )
  1841. for(i=0; i<p->symColCnt*p->symRowCnt; ++i)
  1842. if( p->symM[i] != cmInvalidId )
  1843. cmSymTblRemove(ctx->stH,p->symM[i]);
  1844. // reallocate the symbol matrix
  1845. p->symRowCnt = cmJsonChildCount(p->np);
  1846. p->symM = cmLhResizeN(ctx->lhH,unsigned,p->symM,p->symRowCnt*p->symColCnt);
  1847. // for each row in the JSON array
  1848. for(i=0; i<p->symRowCnt; ++i)
  1849. {
  1850. const cmJsonNode_t* cnp = cmJsonArrayElementC(p->np,i);
  1851. // for each column in row whose data type is a string
  1852. for(j=0,k=0; k<p->symColCnt; ++j)
  1853. if( p->typeIdArray[j] == kStringTId )
  1854. {
  1855. const cmJsonNode_t* vnp = cmJsonArrayElementC(cnp,j);
  1856. unsigned idx = k*p->symRowCnt + i;
  1857. const cmChar_t* text;
  1858. // register the string with the symbol table
  1859. if((text = vnp->u.stringVal) != NULL )
  1860. p->symM[ idx ] = cmSymTblRegisterStaticSymbol(ctx->stH,text);
  1861. else
  1862. p->symM[ idx ] = cmInvalidId;
  1863. ++k;
  1864. }
  1865. }
  1866. return kOkDspRC;
  1867. }
  1868. // Load a JSON file and set the supplied cmJsonH_t handle.
  1869. cmDspRC_t _cmDspMsgListLoadFile( cmDspCtx_t* ctx, cmErr_t* err, const cmChar_t* rsrcLabel, const cmChar_t* fn, cmJsonH_t* hp, unsigned* colCntPtr, cmJsonNode_t** npp )
  1870. {
  1871. cmDspRC_t rc = kOkDspRC; //
  1872. cmJsonNode_t* np = NULL; //
  1873. cmJsonH_t jsH = cmJsonNullHandle; //
  1874. *hp = cmJsonNullHandle;
  1875. *colCntPtr = 0;
  1876. *npp = NULL;
  1877. // if no file name was given ...
  1878. if( fn==NULL || strlen(fn)==0 )
  1879. {
  1880. jsH = ctx->rsrcJsH; // ... use the rsrc file
  1881. if( cmJsonIsValid(ctx->rsrcJsH) == false )
  1882. return cmErrMsg(err,kJsonFailDspRC,"No JSON cfg resource exists for this DSP program.");
  1883. fn = NULL;
  1884. }
  1885. else
  1886. {
  1887. if( cmJsonInitializeFromFile(&jsH,fn,ctx->cmCtx) != kOkJsRC )
  1888. return cmErrMsg(err,kJsonFailDspRC,"The msg list JSON file load failed on '%s'.",fn);
  1889. }
  1890. // find the array named by rsrcLabel
  1891. if((np = cmJsonFindValue(jsH,rsrcLabel,NULL,cmInvalidId)) == NULL)
  1892. return cmErrMsg(err,kJsonFailDspRC,"The msg list JSON tree does not have an array named '%s'.",rsrcLabel);
  1893. // be sure the msg list really is an array
  1894. if( cmJsonIsArray(np) == false )
  1895. return cmErrMsg(err,kJsonFailDspRC,"The msg list JSON element named '%s' is not an array.", rsrcLabel);
  1896. if( fn == NULL )
  1897. fn = "<resource file>";
  1898. // count of elements in the array
  1899. unsigned n = cmJsonChildCount(np);
  1900. unsigned m = 0;
  1901. unsigned i,j;
  1902. // for each line in the array
  1903. for(i=0; i<n; ++i)
  1904. {
  1905. const cmJsonNode_t* cnp;
  1906. // get the ith sub-array (row)
  1907. if((cnp = cmJsonArrayElementC(np,i)) != NULL )
  1908. {
  1909. // verify that it is an array
  1910. if( cmJsonIsArray(cnp ) == false )
  1911. return cmErrMsg(err,kJsonFailDspRC,"The msg list JSON element in '%s' at index %i is not an array.",fn,i);
  1912. // track the number of elements (columns) per row
  1913. unsigned q = cmJsonChildCount(cnp);
  1914. // if this is the first row then use it to set the valid column count
  1915. if( i==0 )
  1916. m = q;
  1917. else
  1918. {
  1919. if( m != q )
  1920. return cmErrMsg(err,kJsonFailDspRC,"The msg list sub-array at index %i has a different number of elements than the preceding sub-arrays in '%s'.",i,fn);
  1921. }
  1922. }
  1923. }
  1924. //
  1925. // determine and validate the column types
  1926. //
  1927. unsigned typeIdArray[m];
  1928. // for each row
  1929. for(i=0; i<n; ++i)
  1930. {
  1931. const cmJsonNode_t* cnp = cmJsonArrayElementC(np,i);
  1932. // for each column
  1933. for(j=0; j<m; ++j)
  1934. {
  1935. const cmJsonNode_t* sap = cmJsonArrayElementC(cnp,j);
  1936. unsigned typeId = sap->typeId & kMaskTId;
  1937. // the first row sets the expected type id for each column
  1938. switch(i)
  1939. {
  1940. case 0:
  1941. if( typeId != kStringTId )
  1942. return cmErrMsg(err,kJsonFailDspRC,"The first row of a msg list (%s) file must contain string elements which set the colum labels.",rsrcLabel);
  1943. break;
  1944. case 1:
  1945. typeIdArray[j] = typeId;
  1946. break;
  1947. default:
  1948. {
  1949. // if the type is a string then it can only
  1950. // match if the column is a string or null
  1951. // so we assume an error
  1952. if( typeId == kStringTId )
  1953. rc = kJsonFailDspRC;
  1954. switch(typeIdArray[j])
  1955. {
  1956. case kStringTId:
  1957. if( typeId != kStringTId && typeId != kNullTId)
  1958. return cmErrMsg(err,kJsonFailDspRC,"The msg list element at row index %i column index %i cannot be converted to a string in '%s'.",i,j,fn);
  1959. rc = kOkDspRC; // clear the assummed error
  1960. break;
  1961. case kNullTId: // null can be converted to anything
  1962. typeIdArray[j] = typeId;
  1963. break;
  1964. case kIntTId:
  1965. if( typeId == kRealTId ) // ints may be promoted to reals
  1966. typeIdArray[j] = kRealTId;
  1967. break;
  1968. case kRealTId:
  1969. break;
  1970. case kFalseTId:
  1971. case kTrueTId: // bools may be promoted to ints or reals
  1972. if( typeId == kIntTId || typeId == kRealTId )
  1973. typeIdArray[j] = typeId;
  1974. break;
  1975. default:
  1976. return cmErrMsg(err,kJsonFailDspRC,"The msg list element at row index %i column index %i is not a string,int,real,bool, or null type in '%s'.",i,j,fn);
  1977. }
  1978. if( rc != kOkDspRC )
  1979. return cmErrMsg(err,kJsonFailDspRC,"The string msg list element at row index %i column index %i cannot be converted to the column type in '%s'.",i,j,fn);
  1980. } // end dflt
  1981. } // end switch
  1982. } // end row
  1983. } // end list
  1984. // VERY TRICKY - store the column type id's in the label columns type id.
  1985. // This is stupid but safe because the column type id's are known to be set to kStringTId.
  1986. cmJsonNode_t* lnp = np->u.childPtr->u.childPtr;
  1987. for(i=0; i<m; ++i,lnp=lnp->siblingPtr)
  1988. lnp->typeId = typeIdArray[i];
  1989. *hp = fn==NULL ? cmJsonNullHandle : jsH;
  1990. *npp = np;
  1991. *colCntPtr = m;
  1992. return rc;
  1993. }
  1994. // use the JSON list labels to setup the cmDspVarArg_t records associated with each msg output var.
  1995. cmDspMsgList_t* _cmDspMsgListCons( cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, const cmDspVarArg_t* args, cmJsonH_t jsH, unsigned colCnt, cmJsonNode_t* np, unsigned typeIdArray[], unsigned va_cnt, va_list vl )
  1996. {
  1997. unsigned i,j;
  1998. unsigned fixedArgCnt = 0;
  1999. for(i=0; args[i].label != NULL; ++i)
  2000. ++fixedArgCnt;
  2001. cmDspVarArg_t a[ fixedArgCnt + colCnt + 1 ];
  2002. // copy the fixed arg's into the first fixedArgCnt ele's of a[]
  2003. for(j=0; j<fixedArgCnt; ++j)
  2004. a[j] = args[j];
  2005. // remove the label row from the JSON array
  2006. cmJsonNode_t* lnp = np->u.childPtr; // store the pointer to the label row
  2007. //np->u.childPtr = lnp->siblingPtr; // remove the labels from the array
  2008. // for each column
  2009. for(i=0; i<colCnt; ++i,++j)
  2010. {
  2011. cmJsonNode_t* lp = cmJsonArrayElement(lnp,i);
  2012. // store a pointer to the label
  2013. a[j].label = lp->u.stringVal;
  2014. a[j].constId = j;
  2015. a[j].rn = 0;
  2016. a[j].cn = 0;
  2017. a[j].flags = kOutDsvFl;
  2018. a[j].doc = "Msg output";
  2019. typeIdArray[i] = lp->typeId;
  2020. // convert the JSON type to a DSV type
  2021. switch( lp->typeId )
  2022. {
  2023. case kNullTId: a[j].flags |= kUIntDsvFl; break;
  2024. case kIntTId: a[j].flags |= kIntDsvFl; break;
  2025. case kRealTId: a[j].flags |= kDoubleDsvFl; break;
  2026. case kTrueTId: a[j].flags |= kBoolDsvFl; break;
  2027. case kFalseTId: a[j].flags |= kBoolDsvFl; break;
  2028. case kStringTId: a[j].flags |= kSymDsvFl; break; // strings are treated as symbols (UInt)
  2029. default:
  2030. { assert(0); }
  2031. }
  2032. // undo the tricky bit with the label types
  2033. lp->typeId = kStringTId;
  2034. }
  2035. // set the null sentinel at the end of the arg array
  2036. memset(a + j,0,sizeof(cmDspVarArg_t));
  2037. return (cmDspMsgList_t*)cmDspInstAlloc(cmDspMsgList_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
  2038. }
  2039. cmDspRC_t _cmDspMsgListReload( cmDspCtx_t* ctx, cmDspMsgList_t* p, const cmChar_t* rsrcLabel, const cmChar_t* fn )
  2040. {
  2041. cmDspRC_t rc = kOkDspRC;
  2042. cmJsonH_t jsH = cmJsonNullHandle;
  2043. unsigned colCnt = 0;
  2044. cmJsonNode_t* np = NULL;
  2045. // load the file
  2046. if((rc = _cmDspMsgListLoadFile(ctx,&p->inst.classPtr->err,rsrcLabel,fn,&jsH,&colCnt,&np)) != kOkDspRC )
  2047. return cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"The msg list file '%s' load failed.",fn);
  2048. // verify that the col count is correct
  2049. if( colCnt != p->colCnt )
  2050. return cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"The column count (%i) of msg list file '%s does not match the msg list column count %i.",colCnt,p->colCnt);
  2051. unsigned fixArgCnt = p->inst.varCnt - p->colCnt;
  2052. cmJsonNode_t* lnp = np->u.childPtr->u.childPtr;
  2053. unsigned i,j;
  2054. for(i=0,j=fixArgCnt; i<colCnt; ++i,++j, lnp=lnp->siblingPtr)
  2055. {
  2056. const cmChar_t* labelStr = cmSymTblLabel(ctx->stH,p->inst.varArray[j].symId);
  2057. // the labels of the new file must match the labels of the previous file
  2058. if( strcmp(lnp->u.stringVal,labelStr) )
  2059. return cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"The msg list file '%s' label '%s' does not match the msg list label '%s' in column index %i.",fn,labelStr,lnp->u.stringVal,i);
  2060. // if the msg column is a string ....
  2061. if( (p->inst.varArray[j].flags & kTypeDsvMask) == kStrzDsvFl )
  2062. {
  2063. // ... then the file column must also be a string or null
  2064. if( lnp->typeId != kStringTId && np->typeId != kNullTId )
  2065. return cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"The data type of msg list file '%s' column index %i must be a string or null.",fn,i);
  2066. }
  2067. else // otherwie if the msg column is a number ...
  2068. {
  2069. // ... then the file type can't be a string
  2070. if( lnp->typeId == kStringTId )
  2071. return cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"The data type of msg list file '%s' column index %i cannot be a string.",fn,i);
  2072. // TODO: maybe there are other type conversions to check for here.
  2073. }
  2074. // reset the typeid of the labels
  2075. // (this is a cleanup from the tricky bit at the end of _cmDspListLoadFile())
  2076. lnp->typeId = kStringTId;
  2077. }
  2078. if( cmJsonIsValid( jsH ) )
  2079. {
  2080. if( cmHandlesAreNotEqual(p->jsH,p->dfltJsH) && cmHandlesAreNotEqual(p->jsH,ctx->rsrcJsH) )
  2081. cmJsonFinalize(&p->jsH);
  2082. p->jsH = jsH;
  2083. p->np = np;
  2084. _cmDspMsgListLoadSymbolMtx(ctx,p);
  2085. }
  2086. return rc;
  2087. }
  2088. enum
  2089. {
  2090. kRsrcMlId,
  2091. kFnMlId,
  2092. kSelMlId,
  2093. kListMlId,
  2094. kCntMlId,
  2095. kOutBaseMlId // identify the first output port
  2096. };
  2097. cmDspInst_t* _cmDspMsgListAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2098. {
  2099. va_list vl2;
  2100. cmDspVarArg_t args[] =
  2101. {
  2102. { "rsrc", kRsrcMlId, 0, 0, kInDsvFl | kReqArgDsvFl | kStrzDsvFl, "Msg list resource label"},
  2103. { "fn", kFnMlId, 0, 0, kInDsvFl | kOptArgDsvFl | kStrzDsvFl, "Msg list file name" },
  2104. { "sel", kSelMlId, 0, 0, kOutDsvFl | kInDsvFl | kOptArgDsvFl | kUIntDsvFl, "Current selection index" },
  2105. { "list", kListMlId, 0, 0, kInDsvFl | kJsonDsvFl, "Msg list data as a JSON array"},
  2106. { "cnt", kCntMlId, 0, 0, kOutDsvFl | kSendDfltDsvFl | kUIntDsvFl , "Count of elements."},
  2107. { NULL, 0, 0, 0, 0 }
  2108. };
  2109. va_copy(vl2,vl);
  2110. if( va_cnt < 1 )
  2111. {
  2112. cmErrMsg(&classPtr->err,kVarArgParseFailDspRC,"The message list constructor must contain at least two arguments.");
  2113. goto errLabel;
  2114. }
  2115. const cmChar_t* rsrcLabel = va_arg(vl,const char*);
  2116. const cmChar_t* fn = va_cnt>1 ? va_arg(vl,const char*) : NULL;
  2117. unsigned colCnt = 0;
  2118. cmJsonH_t jsH = cmJsonNullHandle;
  2119. cmDspMsgList_t* p;
  2120. cmJsonNode_t* np;
  2121. cmDspRC_t rc;
  2122. // be sure the rsrc label contains a valid string
  2123. if( rsrcLabel==NULL || strlen(rsrcLabel)==0 )
  2124. {
  2125. cmErrMsg(&classPtr->err,kVarArgParseFailDspRC,"No msg list resource label was given.");
  2126. goto errLabel;
  2127. }
  2128. // load and validate the JSON file
  2129. if((rc = _cmDspMsgListLoadFile(ctx,&classPtr->err,rsrcLabel,fn,&jsH,&colCnt,&np)) == kOkDspRC )
  2130. {
  2131. unsigned i;
  2132. unsigned typeIdArray[colCnt];
  2133. // allocate the instance
  2134. if((p = _cmDspMsgListCons(ctx,classPtr,storeSymId,instSymId,id,args,jsH,colCnt, np, typeIdArray, va_cnt, vl2 )) != NULL )
  2135. {
  2136. p->jsH = jsH;
  2137. p->np = np;
  2138. p->dfltJsH = jsH;
  2139. p->colCnt = colCnt;
  2140. p->typeIdArray = cmLhAllocZ( ctx->lhH, unsigned, colCnt );
  2141. memcpy(p->typeIdArray,typeIdArray,sizeof(p->typeIdArray[0])*colCnt);
  2142. p->symColCnt = 0;
  2143. for(i=0; i<p->colCnt; ++i)
  2144. if( p->typeIdArray[i] == kStringTId )
  2145. ++p->symColCnt;
  2146. _cmDspMsgListLoadSymbolMtx(ctx,p);
  2147. if( cmDspUInt( (cmDspInst_t*)p, kSelMlId ) > p->symRowCnt )
  2148. cmErrMsg(&classPtr->err,kVarNotValidDspRC,"The default msgList selection index %i greater than the count of elements %i.",cmDspUInt( (cmDspInst_t*)p, kSelMlId ),p->symRowCnt);
  2149. cmDspSetDefaultStrcz( ctx,&p->inst, kRsrcMlId,NULL, rsrcLabel); // rsrc label
  2150. cmDspSetDefaultStrcz( ctx,&p->inst, kFnMlId, NULL, fn); // file name var
  2151. cmDspSetDefaultJson( ctx,&p->inst, kListMlId,NULL, np); // default tree
  2152. cmDspSetDefaultUInt( ctx,&p->inst, kSelMlId, 0, 0); // selection
  2153. cmDspSetDefaultUInt( ctx,&p->inst, kCntMlId, 0, p->symRowCnt);
  2154. // if there is only one column then signal the UI to create a menu button rather
  2155. // than a list by setting the height to zero.
  2156. unsigned height = p->colCnt == 1 ? 0 : 5;
  2157. // create the list UI element
  2158. cmDspUiMsgListCreate(ctx, &p->inst, height, kListMlId, kSelMlId );
  2159. return &p->inst;
  2160. }
  2161. }
  2162. errLabel:
  2163. return NULL;
  2164. }
  2165. cmDspRC_t _cmDspMsgListFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2166. {
  2167. cmDspMsgList_t* p = (cmDspMsgList_t*)inst;
  2168. // be careful not release the ctx->rsrcJsH handle if is happens to be the p->dflJsH
  2169. if( cmJsonIsValid(p->dfltJsH) && cmHandlesAreNotEqual(p->jsH,p->dfltJsH) && cmHandlesAreNotEqual(p->dfltJsH,ctx->rsrcJsH))
  2170. if( cmJsonFinalize(&p->dfltJsH) != kOkJsRC )
  2171. cmDspInstErr(ctx,inst,kJsonFailDspRC,"JSON default tree finalize failed.");
  2172. // be careful not to release the ctx->rsrcJsH handle if it happens to be the p->jsH
  2173. if( cmJsonIsValid(p->jsH) && cmHandlesAreNotEqual(p->dfltJsH,ctx->rsrcJsH) )
  2174. if( cmJsonFinalize( &p->jsH ) != kOkJsRC )
  2175. cmDspInstErr(ctx,inst,kJsonFailDspRC,"JSON finalization failed.");
  2176. cmLhFree(ctx->lhH,p->typeIdArray);
  2177. p->typeIdArray = NULL;
  2178. cmLhFree(ctx->lhH,p->symM);
  2179. p->symM = NULL;
  2180. return kOkDspRC;
  2181. }
  2182. cmDspRC_t _cmDspMsgListReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2183. {
  2184. cmDspApplyAllDefaults(ctx,inst);
  2185. return kOkDspRC;
  2186. }
  2187. cmDspRC_t _cmDspMsgOnSel( cmDspCtx_t* ctx, cmDspMsgList_t* p, unsigned rowIdx )
  2188. {
  2189. cmDspRC_t rc = kOkDspRC;
  2190. unsigned i,j;
  2191. unsigned symId;
  2192. const cmJsonNode_t* cnp = cmJsonArrayElementC(p->np,rowIdx);
  2193. assert( cnp != NULL );
  2194. // for each output variable (column)
  2195. for(i=0,j=0; i<p->colCnt; ++i)
  2196. {
  2197. unsigned varId = kOutBaseMlId + i; // variable id
  2198. const cmJsonNode_t* np = cmJsonArrayElementC(cnp,i); // json row array
  2199. assert( np != NULL );
  2200. switch( p->typeIdArray[i] )
  2201. {
  2202. case kNullTId:
  2203. break;
  2204. case kIntTId:
  2205. {
  2206. int v;
  2207. if( cmJsonIntValue(np,&v) != kOkJsRC )
  2208. { assert(0); }
  2209. rc = cmDspSetInt(ctx,&p->inst,varId,v);
  2210. }
  2211. break;
  2212. case kRealTId:
  2213. {
  2214. double v;
  2215. if( cmJsonRealValue(np,&v) != kOkJsRC )
  2216. { assert(0); }
  2217. rc = cmDspSetDouble(ctx,&p->inst,varId,v);
  2218. }
  2219. break;
  2220. case kTrueTId:
  2221. case kFalseTId:
  2222. {
  2223. bool v;
  2224. if( cmJsonBoolValue(np,&v) != kOkJsRC )
  2225. { assert(0); }
  2226. rc = cmDspSetBool(ctx,&p->inst,varId,v);
  2227. }
  2228. break;
  2229. case kStringTId:
  2230. {
  2231. assert( j < p->symColCnt );
  2232. if((symId = p->symM[ (j*p->symRowCnt) + rowIdx ]) != cmInvalidId )
  2233. if((rc = cmDspSetSymbol(ctx,&p->inst,varId,symId )) != kOkDspRC )
  2234. break;
  2235. ++j;
  2236. }
  2237. break;
  2238. default:
  2239. { assert(0); }
  2240. } // end switch
  2241. } // end for
  2242. return rc;
  2243. }
  2244. cmDspRC_t _cmDspMsgListRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2245. {
  2246. cmDspRC_t rc = kOkDspRC;
  2247. cmDspMsgList_t* p = (cmDspMsgList_t*)inst;
  2248. switch( evt->dstVarId )
  2249. {
  2250. case kRsrcMlId:
  2251. {
  2252. const cmChar_t* fn = cmDspStrcz(inst,kFnMlId);
  2253. const cmChar_t* rsrcLabel = cmDsvStrz(evt->valuePtr);
  2254. if( rsrcLabel != NULL )
  2255. {
  2256. if((rc = _cmDspMsgListReload(ctx,p,rsrcLabel,fn)) != kOkDspRC )
  2257. return cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"The msg list file '%s' load failed.",fn);
  2258. cmDspSetEvent(ctx,inst,evt);
  2259. cmDspSetJson(ctx,inst,kListMlId,p->np);
  2260. cmDspSetUInt(ctx,inst,kSelMlId,0);
  2261. cmDspSetUInt(ctx,inst,kCntMlId,p->symRowCnt);
  2262. }
  2263. }
  2264. break;
  2265. case kFnMlId:
  2266. // Store the new file name.
  2267. // A new file will not be loaded until the next rsrc label is received.
  2268. cmDspSetEvent(ctx,inst,evt);
  2269. break;
  2270. case kListMlId:
  2271. break;
  2272. case kSelMlId:
  2273. {
  2274. unsigned rowIdx = cmDsvGetUInt(evt->valuePtr);
  2275. assert( rowIdx < p->symRowCnt);
  2276. // set the current selection variable
  2277. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  2278. rc = _cmDspMsgOnSel(ctx,p,rowIdx);
  2279. } //end case
  2280. break;
  2281. }
  2282. return rc;
  2283. }
  2284. cmDspRC_t _cmDspMsgListPresetRdWr( cmDspCtx_t* ctx, cmDspInst_t* inst, bool storeFl )
  2285. {
  2286. cmDspRC_t rc = kOkDspRC;
  2287. cmDspMsgList_t* p = (cmDspMsgList_t*)inst;
  2288. if((rc = cmDspVarPresetRdWr(ctx,inst,kSelMlId,storeFl)) == kOkDspRC )
  2289. {
  2290. if( !storeFl )
  2291. rc = _cmDspMsgOnSel(ctx,p, cmDspUInt(inst,kSelMlId) );
  2292. }
  2293. return rc;
  2294. }
  2295. cmDspClass_t* cmMsgListClassCons( cmDspCtx_t* ctx )
  2296. {
  2297. cmDspClassSetup(&_cmMsgListDC,ctx,"MsgList",
  2298. NULL,
  2299. _cmDspMsgListAlloc,
  2300. _cmDspMsgListFree,
  2301. _cmDspMsgListReset,
  2302. NULL,
  2303. _cmDspMsgListRecv,
  2304. _cmDspMsgListPresetRdWr,
  2305. NULL,
  2306. "Message list selection control.");
  2307. return &_cmMsgListDC;
  2308. }
  2309. //------------------------------------------------------------------------------------------------------------
  2310. //)
  2311. //( { label:cmDspWavetable file_desc:"Programmable wavetable unit." kw:[sunit] }
  2312. enum
  2313. {
  2314. kLenWtId,
  2315. kShapeWtId,
  2316. kFnWtId,
  2317. kLoopWtId,
  2318. kBegWtId,
  2319. kEndWtId,
  2320. kCmdWtId,
  2321. kOtWtId,
  2322. kGainWtId,
  2323. kPhsWtId,
  2324. kOutWtId,
  2325. kCntWtId,
  2326. kFIdxWtId,
  2327. kDoneWtId
  2328. };
  2329. enum
  2330. {
  2331. kSilenceWtId, // 0
  2332. kFileWtId, // 1
  2333. kWhiteWtId, // 2
  2334. kPinkWtId, // 3
  2335. kSineWtId, // 4
  2336. kCosWtId, // 5
  2337. kSqrWtId, // 6
  2338. kTriWtId, // 7
  2339. kSawWtId, // 8
  2340. kPulseWtId, // 9
  2341. kImpulseWtId, // 10
  2342. kPhasorWtId, // 11
  2343. kShapeWtCnt
  2344. };
  2345. cmDspClass_t _cmWaveTableDC;
  2346. typedef struct
  2347. {
  2348. cmDspInst_t inst;
  2349. cmSample_t* wt; // wave table memory
  2350. unsigned wti; // next location to write samples into the wavetable
  2351. unsigned wtn; // count of empty samples (avail for writing over) in the wavetable.
  2352. unsigned fi; // absolute index into the file of the next sample to read
  2353. unsigned fn; // length of the file in samples
  2354. unsigned cfi; // absolute index into the file of the beginning of the current audio vector
  2355. unsigned cfn; // when cfi >= cfn and doneFl is set then the 'done' msg is sent
  2356. unsigned loopCnt; // current loop count
  2357. bool doneFl; // the wave table source is exhausted
  2358. cmAudioFileH_t afH; // current audio file handle
  2359. int nxtBegSmpIdx; // the beg/end sample index to use with the next filename to arrive at port 'fn'
  2360. int nxtEndSmpIdx; //
  2361. cmThreadH_t thH;
  2362. bool loadFileFl;
  2363. cmDspCtx_t* ctx;
  2364. cmSample_t phsOffs;
  2365. cmSample_t phsLast;
  2366. unsigned onSymId;
  2367. unsigned offSymId;
  2368. unsigned doneSymId;
  2369. bool useThreadFl;
  2370. unsigned minAfIndexRptCnt; // min count of audio samples between transmitting the current audio file index
  2371. unsigned afIndexRptCnt; // current audio file sample index count
  2372. } cmDspWaveTable_t;
  2373. bool _cmDspWaveTableThreadFunc( void* param);
  2374. cmDspInst_t* _cmDspWaveTableAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2375. {
  2376. cmDspVarArg_t args[] =
  2377. {
  2378. { "len", kLenWtId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Wave table length in samples" },
  2379. { "shape", kShapeWtId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Wave shape 0=silent 1=file 2=white 3=pink 4=sine, 5=cos 6=sqr 7=tri 8=saw 9=pulse 10=impulse 11=phasor" },
  2380. { "fn", kFnWtId, 0, 0, kInDsvFl | kStrzDsvFl | kOptArgDsvFl, "Optional audio file name" },
  2381. { "loop", kLoopWtId, 0, 0, kInDsvFl | kIntDsvFl | kOptArgDsvFl, "-1=loop forever >0=loop count (dflt:-1)"},
  2382. { "beg", kBegWtId, 0, 0, kInDsvFl | kIntDsvFl | kOptArgDsvFl, "File begin sample index" },
  2383. { "end", kEndWtId, 0, 0, kInDsvFl | kIntDsvFl | kOptArgDsvFl, "File end sample index (-1=play all)" },
  2384. { "cmd", kCmdWtId, 0, 0, kInDsvFl | kSymDsvFl | kOptArgDsvFl, "Command: on off"},
  2385. { "ot", kOtWtId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Overtone count"},
  2386. { "gain", kGainWtId, 0, 0, kInDsvFl | kDoubleDsvFl|kOptArgDsvFl, "Gain"},
  2387. { "phs", kPhsWtId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Driving phase" },
  2388. { "out", kOutWtId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output" },
  2389. { "cnt", kCntWtId, 0, 0, kOutDsvFl | kIntDsvFl, "Loop count event."},
  2390. { "fidx", kFIdxWtId, 0, 0, kOutDsvFl | kUIntDsvFl, "Current audio file index."},
  2391. { "done", kDoneWtId, 0, 0, kOutDsvFl | kSymDsvFl, "'done' sent after last loop."},
  2392. { NULL, 0, 0, 0, 0 }
  2393. };
  2394. cmDspWaveTable_t* p = cmDspInstAlloc(cmDspWaveTable_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  2395. // get the filename given in the va_list (or NULL if no filename was given)
  2396. const cmChar_t* fn = cmDspDefaultStrcz(&p->inst,kFnWtId);
  2397. p->offSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"off");
  2398. p->onSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"on");
  2399. p->doneSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"done");
  2400. double adCurFileIdxRptPeriodMs = 100.0;
  2401. p->minAfIndexRptCnt = floor(adCurFileIdxRptPeriodMs * cmDspSampleRate(ctx) / 1000.0);
  2402. cmDspSetDefaultUInt( ctx, &p->inst, kLenWtId, 0, cmDspSampleRate(ctx));
  2403. cmDspSetDefaultUInt( ctx, &p->inst, kShapeWtId, 0, kSilenceWtId );
  2404. cmDspSetDefaultStrcz( ctx, &p->inst, kFnWtId, NULL, fn );
  2405. cmDspSetDefaultInt( ctx, &p->inst, kLoopWtId, 0, -1 );
  2406. cmDspSetDefaultInt( ctx, &p->inst, kBegWtId, 0, 0 );
  2407. cmDspSetDefaultInt( ctx, &p->inst, kEndWtId, 0, -1 );
  2408. cmDspSetDefaultSymbol(ctx, &p->inst, kCmdWtId, p->onSymId );
  2409. cmDspSetDefaultUInt( ctx, &p->inst, kOtWtId, 0, 5 );
  2410. cmDspSetDefaultDouble(ctx, &p->inst, kGainWtId, 0, 1.0 );
  2411. cmDspSetDefaultUInt( ctx, &p->inst, kFIdxWtId, 0, 0 );
  2412. p->useThreadFl = false;
  2413. return &p->inst;
  2414. }
  2415. cmDspRC_t _cmDspWaveTableFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2416. {
  2417. cmDspWaveTable_t* p = (cmDspWaveTable_t*)inst;
  2418. if( cmThreadIsValid(p->thH) )
  2419. if( cmThreadDestroy(&p->thH) != kOkThRC )
  2420. cmDspInstErr(ctx,inst,kThreadFailDspRC,"The wavetable file reader thread failed to close.");
  2421. if( cmAudioFileIsValid(p->afH) )
  2422. cmAudioFileDelete(&p->afH);
  2423. cmLhFree(ctx->lhH,p->wt);
  2424. return kOkDspRC;
  2425. }
  2426. // Read the next block of 'rdSmpCnt' samples starting at the absolute file index 'p->fi'
  2427. // into wt[rdSmpCnt].
  2428. // If the end of the file segment marked by absolute file indexes 'begSmpIdx' to 'endSmpIdx'
  2429. // is encountered in the middle of the requested block and the wave table is in loop
  2430. // mode then the the function will automatically begin reading from the begining of the
  2431. // file segment. If the end of the file segment is encountered and the wave table is not
  2432. // in loop mode then the empty portion of wt[] will be set to zero.
  2433. cmDspRC_t _cmDspWaveTableReadBlock( cmDspCtx_t* ctx, cmDspWaveTable_t* p, cmSample_t* wt, unsigned rdSmpCnt, int begSmpIdx, int endSmpIdx, int maxLoopCnt )
  2434. {
  2435. unsigned actFrmCnt = 0;
  2436. unsigned chIdx = 0;
  2437. unsigned chCnt = 1;
  2438. unsigned fn = endSmpIdx - p->fi + 1; // count of samples between p->fi and endSmpIdx
  2439. unsigned n0 = rdSmpCnt;
  2440. unsigned n1 = 0;
  2441. // if the requested sample count will go past the end of the file segment
  2442. if( rdSmpCnt > fn )
  2443. {
  2444. n1 = rdSmpCnt - fn;
  2445. n0 = rdSmpCnt - n1;
  2446. }
  2447. // if we don't have a valid file yet - then
  2448. if( cmAudioFileIsValid(p->afH) == false )
  2449. {
  2450. cmVOS_Zero(wt,n0);
  2451. return kOkDspRC;
  2452. }
  2453. // read the first block of samples
  2454. if( cmAudioFileReadSample(p->afH, n0, chIdx, chCnt, &wt, &actFrmCnt ) != kOkAfRC )
  2455. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"An error occured while reading the wave table file.");
  2456. // BUG BUG BUG
  2457. // This assertion will fail if the file beg/end markers are not legal given the file length.
  2458. // An error msg would be more appropriate.
  2459. assert( actFrmCnt == n0 );
  2460. // increment the wave table pointer
  2461. wt += n0;
  2462. p->fi += n0;
  2463. // if n1 != 0 then we have encountered the end of the file segment
  2464. if( n1 > 0 )
  2465. {
  2466. ++p->loopCnt;
  2467. // send the loop count event
  2468. cmDspSetInt(ctx,&p->inst,kCntWtId,p->loopCnt);
  2469. // if we have played all the requested loops
  2470. if( maxLoopCnt != -1 && p->loopCnt >= maxLoopCnt )
  2471. {
  2472. p->doneFl = true;
  2473. cmVOS_Zero(wt,n1); // zero to the end of the buffer
  2474. p->cfn = p->cfi + cmDspUInt((cmDspInst_t*)p,kLenWtId) - p->wtn - n0;
  2475. assert( p->cfn >= p->cfi );
  2476. }
  2477. else
  2478. {
  2479. // seek to the first sample indicated by the 'beg' variable
  2480. if( cmAudioFileSeek(p->afH,begSmpIdx) != kOkAfRC )
  2481. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"Seeking failed on the wave table file.",fn);
  2482. // read the second block of samples
  2483. if( cmAudioFileReadSample(p->afH, n1, chIdx, chCnt, &wt, &actFrmCnt ) != kOkAfRC )
  2484. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"An error occured while reading the wave table file.");
  2485. assert( actFrmCnt == n1 );
  2486. // reset the file index tracker
  2487. p->fi = begSmpIdx + n1;
  2488. p->cfi = begSmpIdx;
  2489. }
  2490. }
  2491. return kOkDspRC;
  2492. }
  2493. cmDspRC_t _cmDspWaveTableReadAudioFile( cmDspCtx_t* ctx, cmDspWaveTable_t* p, unsigned wtSmpCnt, unsigned rdSmpCnt )
  2494. {
  2495. unsigned n0 = rdSmpCnt;
  2496. unsigned n1 = 0;
  2497. int begSmpIdx = cmDspInt(&p->inst,kBegWtId);
  2498. int endSmpIdx = cmDspInt(&p->inst,kEndWtId);
  2499. int maxLoopCnt= cmDspInt(&p->inst,kLoopWtId);
  2500. if( endSmpIdx < begSmpIdx )
  2501. endSmpIdx = p->fn-1;
  2502. // if this read will wrap to the beginning of the wave table
  2503. if( p->wti + rdSmpCnt > wtSmpCnt )
  2504. {
  2505. n0 = wtSmpCnt - p->wti; // count of samples to read into the end of p->wt[]
  2506. n1 = rdSmpCnt - n0; // count of samples to read into the beg of p->wt[]
  2507. }
  2508. assert(n1<wtSmpCnt);
  2509. // the first read always starts at p->wt + p->wti
  2510. if( p->doneFl )
  2511. cmVOS_Zero(p->wt + p->wti,n0);
  2512. else
  2513. if( _cmDspWaveTableReadBlock(ctx, p, p->wt+p->wti, n0,begSmpIdx,endSmpIdx,maxLoopCnt ) != kOkDspRC )
  2514. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"An error occured while reading the wave table file.");
  2515. p->wtn -= n0; // decrease the count of available samples
  2516. p->wti += n0;
  2517. if( n1 > 0 )
  2518. {
  2519. // the second read always starts at the beginning of the wave table
  2520. if( p->doneFl )
  2521. cmVOS_Zero(p->wt,n1);
  2522. else
  2523. if( _cmDspWaveTableReadBlock(ctx, p, p->wt, n1,begSmpIdx,endSmpIdx,maxLoopCnt ) != kOkDspRC )
  2524. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"An error occured while reading the wave table file.");
  2525. p->wtn -= n1; // decrease the count of available samples
  2526. p->wti = n1;
  2527. }
  2528. //p->wtn -= rdSmpCnt; // decrease the count of available samples
  2529. return kOkDspRC;
  2530. }
  2531. cmDspRC_t _cmDspWaveTableInitAudioFile( cmDspCtx_t* ctx, cmDspWaveTable_t* p )
  2532. {
  2533. cmDspRC_t rc = kOkDspRC;
  2534. cmAudioFileH_t afH;
  2535. cmRC_t afRC;
  2536. cmAudioFileInfo_t afInfo;
  2537. const cmChar_t* fn = cmDspStrcz(&p->inst,kFnWtId);
  2538. unsigned wtSmpCnt = cmDspUInt(&p->inst,kLenWtId);
  2539. int begSmpIdx= cmDspInt(&p->inst,kBegWtId);
  2540. // if the file name is valid
  2541. if( fn == NULL || strlen(fn)==0 )
  2542. {
  2543. rc = cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"Audio file loading was requested for the wave table but no file name was given.");
  2544. goto errLabel;
  2545. }
  2546. // open the audio file
  2547. afH = cmAudioFileNewOpen(fn,&afInfo,&afRC,ctx->rpt);
  2548. // check for file open errors
  2549. if( afRC != kOkAfRC )
  2550. {
  2551. rc = cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"The audio file '%s' could not be opened. ('%s').",fn,cmAudioFileErrorMsg(afRC));
  2552. goto errLabel;
  2553. }
  2554. // if the file opened but is invalid
  2555. if( cmAudioFileIsValid(p->afH) )
  2556. cmAudioFileDelete(&p->afH);
  2557. // seek to the first sample indicated by the 'beg' variable
  2558. if( cmAudioFileSeek(afH,begSmpIdx) != kOkAfRC )
  2559. {
  2560. rc = cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"Seeking failed on the audio file '%s'.",fn);
  2561. goto errLabel;
  2562. }
  2563. p->afH = afH;
  2564. p->fi = begSmpIdx;
  2565. p->cfi = begSmpIdx;
  2566. p->fn = afInfo.frameCnt;
  2567. p->wti = 0;
  2568. p->wtn = wtSmpCnt;
  2569. // read the first block of samples
  2570. if((rc= _cmDspWaveTableReadAudioFile(ctx,p,wtSmpCnt,wtSmpCnt))!= kOkDspRC )
  2571. goto errLabel;
  2572. //printf("Wt:%s %i %i\n",fn,begSmpIdx,cmDspInt(&p->inst,kEndWtId));
  2573. // set the shape param to kFileWtId
  2574. //if((rc= cmDspSetUInt(ctx,&p->inst,kShapeWtId,kFileWtId)) != kOkDspRC )
  2575. // goto errLabel;
  2576. errLabel:
  2577. if( rc != kOkDspRC )
  2578. cmDspSetUInt(ctx,&p->inst,kShapeWtId,kSilenceWtId);
  2579. return rc;
  2580. }
  2581. bool _cmDspWaveTableThreadFunc( void* param)
  2582. {
  2583. cmDspWaveTable_t* p = (cmDspWaveTable_t*)param;
  2584. if( p->loadFileFl )
  2585. {
  2586. p->loadFileFl = false;
  2587. if( _cmDspWaveTableInitAudioFile(p->ctx,p) == kOkDspRC )
  2588. {
  2589. p->phsOffs = p->phsLast;
  2590. cmDspSetUInt(p->ctx,&p->inst,kShapeWtId,kFileWtId);
  2591. }
  2592. cmThreadPause(p->thH,kPauseThFl);
  2593. }
  2594. return true;
  2595. }
  2596. // Files are loaded via a background thread.
  2597. cmDspRC_t _cmDspWaveTableStartFileLoadThread( cmDspCtx_t* ctx, cmDspWaveTable_t* p, const cmChar_t* fn )
  2598. {
  2599. cmDspRC_t rc = kOkDspRC;
  2600. if( fn == NULL )
  2601. return rc;
  2602. if( p->loadFileFl )
  2603. return cmDspInstErr(ctx,&p->inst,kInvalidStateDspRC,"The audio file '%s' was not loaded because another file is in the process of being loaded.",cmStringNullGuard(fn));
  2604. if(p->useThreadFl && cmThreadIsValid(p->thH) == false)
  2605. cmThreadCreate(&p->thH,_cmDspWaveTableThreadFunc,p,ctx->rpt);
  2606. if(p->useThreadFl && cmThreadIsValid(p->thH) == false )
  2607. return cmDspInstErr(ctx,&p->inst,kInvalidStateDspRC,"The audio file '%s' was not loaded because the audio load thread is invalid.",cmStringNullGuard(fn));
  2608. p->loadFileFl = true;
  2609. p->ctx = ctx;
  2610. cmDspSetUInt(ctx,&p->inst,kShapeWtId,kSilenceWtId);
  2611. cmDspSetStrcz(ctx,&p->inst,kFnWtId,fn);
  2612. if( p->useThreadFl == false )
  2613. {
  2614. // use non-threaded load
  2615. if((rc = _cmDspWaveTableInitAudioFile(p->ctx,p)) != kOkDspRC )
  2616. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"The audio file '%s' was not loaded.",cmStringNullGuard(fn));
  2617. p->phsOffs = p->phsLast;
  2618. cmDspSetUInt(p->ctx,&p->inst,kShapeWtId,kFileWtId);
  2619. p->loadFileFl = false;
  2620. }
  2621. else
  2622. {
  2623. // use threaded load - this works but it isn't really thread safe
  2624. if( cmThreadPause(p->thH,0) != kOkThRC )
  2625. return cmDspInstErr(ctx,&p->inst,kThreadFailDspRC,"The audio file '%s' was not loaded because audio thread enable failed.",cmStringNullGuard(fn));
  2626. }
  2627. return rc;
  2628. }
  2629. // This function is called whenever the source mode variable changes (or a new file name arrives)
  2630. cmDspRC_t _cmDspWaveTableCreateTable( cmDspCtx_t* ctx, cmDspWaveTable_t* p )
  2631. {
  2632. cmDspRC_t rc = kOkDspRC;
  2633. unsigned shapeId = cmDspUInt(&p->inst,kShapeWtId);
  2634. unsigned wtSmpCnt = cmDspUInt(&p->inst,kLenWtId);
  2635. unsigned otCnt = cmDspUInt(&p->inst,kOtWtId);
  2636. cmSample_t gain = 0.9;
  2637. double hz = 1.0;
  2638. double sr = cmDspSampleRate(ctx);
  2639. assert( wtSmpCnt > 0 );
  2640. if( p->wt == NULL )
  2641. p->wt = cmLhResizeNZ(ctx->lhH,cmSample_t,p->wt,wtSmpCnt);
  2642. else
  2643. cmVOS_Zero(p->wt,wtSmpCnt);
  2644. p->wtn = wtSmpCnt; // all samples in the wt are avail for filling
  2645. p->wti = 0; // beginning with the first sample
  2646. p->loopCnt = 0; // we are starting from a new source so set the loop cnt to 0
  2647. p->doneFl = false; // and the doneFl to false
  2648. assert( p->wt != NULL );
  2649. switch( shapeId )
  2650. {
  2651. case kSilenceWtId:
  2652. break;
  2653. case kFileWtId:
  2654. printf("Loading:%i %i %s\n",p->nxtBegSmpIdx,p->nxtEndSmpIdx,cmDspStrcz(&p->inst,kFnWtId));
  2655. rc = _cmDspWaveTableStartFileLoadThread(ctx,p,cmDspStrcz(&p->inst,kFnWtId));
  2656. break;
  2657. case kWhiteWtId:
  2658. cmVOS_Random(p->wt,wtSmpCnt,-gain,gain);
  2659. break;
  2660. case kPinkWtId:
  2661. cmVOS_SynthPinkNoise(p->wt,wtSmpCnt,0.0);
  2662. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2663. break;
  2664. case kSineWtId:
  2665. cmVOS_SynthSine(p->wt,wtSmpCnt,0,sr,hz);
  2666. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2667. break;
  2668. case kCosWtId:
  2669. cmVOS_SynthCosine(p->wt,wtSmpCnt,0,sr,hz);
  2670. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2671. break;
  2672. case kSawWtId:
  2673. cmVOS_SynthSawtooth(p->wt,wtSmpCnt,0,sr,hz,otCnt);
  2674. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2675. break;
  2676. case kSqrWtId:
  2677. cmVOS_SynthSquare( p->wt,wtSmpCnt,0,sr,hz,otCnt );
  2678. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2679. break;
  2680. case kTriWtId:
  2681. cmVOS_SynthTriangle( p->wt,wtSmpCnt,0,sr,hz,otCnt );
  2682. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2683. break;
  2684. case kPulseWtId:
  2685. cmVOS_SynthPulseCos( p->wt,wtSmpCnt,0,sr,hz,otCnt );
  2686. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2687. break;
  2688. case kPhasorWtId:
  2689. cmVOS_SynthPhasor( p->wt,wtSmpCnt,0,sr,hz );
  2690. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2691. break;
  2692. }
  2693. return rc;
  2694. }
  2695. cmDspRC_t _cmDspWaveTableReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2696. {
  2697. cmDspWaveTable_t* p = (cmDspWaveTable_t*)inst;
  2698. cmDspApplyAllDefaults(ctx,inst);
  2699. cmDspZeroAudioBuf(ctx,inst,kOutWtId);
  2700. p->nxtBegSmpIdx = cmDspInt(&p->inst,kBegWtId);
  2701. p->nxtEndSmpIdx = cmDspInt(&p->inst,kEndWtId);
  2702. return _cmDspWaveTableCreateTable(ctx,p);
  2703. }
  2704. cmDspRC_t _cmDspWaveTableExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2705. {
  2706. cmDspRC_t rc = kOkDspRC;
  2707. const cmSample_t* phsV = cmDspAudioBuf(ctx,inst,kPhsWtId,0);
  2708. if( phsV == NULL )
  2709. {
  2710. inst->execFunc = NULL; // disable this instance because it has no input
  2711. return kOkDspRC;
  2712. }
  2713. cmDspWaveTable_t* p = (cmDspWaveTable_t*)inst;
  2714. unsigned mode = cmDspSymbol(inst,kCmdWtId);
  2715. unsigned srcId = cmDspUInt(inst,kShapeWtId);
  2716. if( mode == p->offSymId || srcId == kSilenceWtId )
  2717. {
  2718. cmDspZeroAudioBuf(ctx,inst,kOutWtId);
  2719. return kOkDspRC;
  2720. }
  2721. cmSample_t* outV = cmDspAudioBuf(ctx,inst,kOutWtId,0);
  2722. unsigned outCnt = cmDspVarRows(inst,kOutWtId);
  2723. unsigned wtSmpCnt = cmDspUInt(inst,kLenWtId);
  2724. double gain = cmDspDouble(inst,kGainWtId);
  2725. unsigned i;
  2726. // for each output sample
  2727. for(i=0; i<outCnt; ++i)
  2728. {
  2729. // get the wave table location
  2730. //unsigned x = fmodf(phsV[i] - p->phsOffs,wtSmpCnt);
  2731. unsigned x = fmodf(phsV[i],wtSmpCnt);
  2732. // if the wt loctn is passed the end of the table
  2733. /*
  2734. if( x >= wtSmpCnt )
  2735. {
  2736. offs += wtSmpCnt;
  2737. x -= wtSmpCnt;
  2738. }
  2739. */
  2740. outV[i] = gain * p->wt[x];
  2741. }
  2742. p->phsLast = phsV[outCnt-1];
  2743. // if we are reading from a file ...
  2744. if( srcId == kFileWtId )
  2745. {
  2746. unsigned rdSmpCnt = 8192; // file read block sample count
  2747. p->wtn += outCnt;
  2748. // ... and there are rdSmpCnt avail locations in the wave table
  2749. if( p->wtn >= rdSmpCnt )
  2750. rc = _cmDspWaveTableReadAudioFile(ctx, p, wtSmpCnt, rdSmpCnt );
  2751. // send the current audio file index
  2752. if( p->doneFl && p->cfi < p->cfn && p->cfn <= (p->cfi + outCnt) )
  2753. {
  2754. cmDspSetUInt(ctx,inst,kFIdxWtId,p->cfn);
  2755. cmDspSetSymbol(ctx,inst,kDoneWtId,p->doneSymId);
  2756. }
  2757. else
  2758. {
  2759. if( p->afIndexRptCnt >= p->minAfIndexRptCnt )
  2760. {
  2761. p->afIndexRptCnt -= p->minAfIndexRptCnt;
  2762. cmDspSetUInt(ctx,inst,kFIdxWtId,p->cfi);
  2763. }
  2764. }
  2765. p->afIndexRptCnt += outCnt;
  2766. p->cfi += outCnt;
  2767. }
  2768. return rc;
  2769. }
  2770. cmDspRC_t _cmDspWaveTableRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2771. {
  2772. cmDspRC_t rc = kOkDspRC;
  2773. cmDspWaveTable_t* p = (cmDspWaveTable_t*)inst;
  2774. switch( evt->dstVarId )
  2775. {
  2776. case kFnWtId: // a new file name arrived - this automatically switches the source mode to kFileWtId
  2777. {
  2778. const cmChar_t* fn = cmDsvStrz(evt->valuePtr);
  2779. if( cmFsIsFile( fn )==false )
  2780. cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"'%s' does not exist.",cmStringNullGuard(fn));
  2781. else
  2782. {
  2783. cmDspSetEvent(ctx,inst,evt); // set the file name variable
  2784. cmDspSetInt(ctx,inst,kBegWtId,p->nxtBegSmpIdx); // set the beg/end smp idx var's from the stored nxtBeg/EndSmpIdx values
  2785. cmDspSetInt(ctx,inst,kEndWtId,p->nxtEndSmpIdx); //
  2786. cmDspSetUInt(ctx,inst,kShapeWtId,kFileWtId); // switch to file mode
  2787. rc = _cmDspWaveTableCreateTable(ctx,p); // reload the wavetable
  2788. }
  2789. }
  2790. break;
  2791. case kBegWtId:
  2792. // store for next incoming file name msg
  2793. p->nxtBegSmpIdx = cmDsvGetInt(evt->valuePtr);
  2794. break;
  2795. case kEndWtId:
  2796. // store for next incoming file name msg
  2797. p->nxtEndSmpIdx = cmDsvGetInt(evt->valuePtr);
  2798. break;
  2799. case kShapeWtId:
  2800. if( cmDsvGetUInt(evt->valuePtr) < kShapeWtCnt )
  2801. {
  2802. cmDspSetEvent(ctx,inst,evt); // switch modes
  2803. rc = _cmDspWaveTableCreateTable(ctx,p); // reload the wavetable
  2804. }
  2805. break;
  2806. case kLenWtId: // we don't support table size changes
  2807. break;
  2808. case kPhsWtId:
  2809. break;
  2810. case kCmdWtId:
  2811. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  2812. {
  2813. if( cmDspSymbol(inst,kCmdWtId) == p->onSymId )
  2814. {
  2815. //rc = _cmDspWaveTableReset(ctx,inst, evt );
  2816. rc = _cmDspWaveTableCreateTable(ctx,p);
  2817. cmDspSetSymbol(ctx,inst,kCmdWtId,p->onSymId);
  2818. p->phsOffs = 0;
  2819. p->phsLast = 0;
  2820. }
  2821. }
  2822. break;
  2823. case kOtWtId:
  2824. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  2825. rc = _cmDspWaveTableCreateTable(ctx,p); // reload the wavetable
  2826. break;
  2827. case kGainWtId:
  2828. rc = cmDspSetEvent(ctx,inst,evt);
  2829. break;
  2830. default:
  2831. { assert(0); }
  2832. }
  2833. return rc;
  2834. }
  2835. cmDspClass_t* cmWaveTableClassCons( cmDspCtx_t* ctx )
  2836. {
  2837. cmDspClassSetup(&_cmWaveTableDC,ctx,"WaveTable",
  2838. NULL,
  2839. _cmDspWaveTableAlloc,
  2840. _cmDspWaveTableFree,
  2841. _cmDspWaveTableReset,
  2842. _cmDspWaveTableExec,
  2843. _cmDspWaveTableRecv,
  2844. NULL,
  2845. NULL,
  2846. "Variable frequency and waveshape signal generator." );
  2847. return &_cmWaveTableDC;
  2848. }
  2849. //------------------------------------------------------------------------------------------------------------
  2850. //)
  2851. //( { label:cmDspSprintf file_desc:"Printf like string formatting unit." kw:[sunit] }
  2852. enum
  2853. {
  2854. kFmtSpId,
  2855. kOutSpId,
  2856. kInSpId,
  2857. kSprintfLabelCharCnt = 15,
  2858. kSprintfDocCharCnt = 31,
  2859. kSprintfBufCharCnt = 1023
  2860. };
  2861. cmDspClass_t _cmSprintfDC;
  2862. typedef struct
  2863. {
  2864. unsigned flags; // dsv type id for this fmt conversion or 0 if it is a literal string
  2865. unsigned fsi; // index into the fmt string where the fmt starts (always points to an '%' char)
  2866. unsigned fsn; // length of the format string
  2867. cmChar_t label[ kSprintfLabelCharCnt+1 ];
  2868. cmChar_t doc[ kSprintfDocCharCnt+1 ];
  2869. } cmDspSprintfFmt_t;
  2870. typedef struct
  2871. {
  2872. cmDspInst_t inst;
  2873. cmChar_t buf[ kSprintfBufCharCnt+1]; // output string buffer
  2874. unsigned inCnt; // count conversion spec's in the fmtStr[]
  2875. cmDspSprintfFmt_t* fmtArray; // fmtArray[inCnt]
  2876. cmChar_t* fmtStr; // C-style printf format string.
  2877. } cmDspSprintf_t;
  2878. cmDspRC_t _cmSprintfGetInputCnt( cmDspCtx_t* ctx, cmDspClass_t* classPtr, const cmChar_t* fmt, unsigned* inCntPtr )
  2879. {
  2880. unsigned i,n;
  2881. unsigned inCnt = 0;
  2882. if( fmt== NULL || (n=strlen(fmt))==0 )
  2883. return cmErrMsg(&classPtr->err,kInvalidArgDspRC,"Empty format string.");
  2884. for(i=0; i<n; ++i)
  2885. {
  2886. // handle the escape character
  2887. if( fmt[i] == '\\' )
  2888. ++i; // for now we will just skip the next character
  2889. else
  2890. if( fmt[i] == '%' )
  2891. ++inCnt;
  2892. }
  2893. *inCntPtr = inCnt;
  2894. return kOkDspRC;
  2895. }
  2896. cmDspRC_t _cmSprintfGetInputTypes( cmDspCtx_t* ctx, cmDspClass_t* classPtr, const cmChar_t* fmt, cmDspSprintfFmt_t fmtArray[], unsigned inCnt )
  2897. {
  2898. unsigned i,j,n;
  2899. if( fmt== NULL || (n=strlen(fmt))==0 )
  2900. return cmErrMsg(&classPtr->err,kInvalidArgDspRC,"Empty format string.");
  2901. n = strlen(fmt);
  2902. for(i=0,j=0; i<n; ++i)
  2903. {
  2904. // handle the escape character
  2905. if( fmt[i] == '\\' )
  2906. ++i; // for now we will just skip the next character
  2907. else
  2908. if( fmt[i] == '%' )
  2909. {
  2910. unsigned fn;
  2911. if((fn = strcspn(fmt+i,"diouxXfeEgGcs")) == 0 )
  2912. return cmErrMsg(&classPtr->err,kInvalidArgDspRC,"Invalid format string on input conversion at index:%i.",j);
  2913. ++fn;
  2914. fmtArray[j].fsi = i;
  2915. fmtArray[j].fsn = fn;
  2916. snprintf(fmtArray[j].label,kSprintfLabelCharCnt,"in-%i",j);
  2917. fmtArray[j].label[kSprintfLabelCharCnt]=0;
  2918. fmtArray[j].doc[kSprintfDocCharCnt] = 0;
  2919. switch( fmt[ i + fn - 1 ] )
  2920. {
  2921. case 'd':
  2922. case 'i':
  2923. fmtArray[j].flags = kIntDsvFl;
  2924. snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"Integer input %i.",j);
  2925. break;
  2926. case 'o':
  2927. case 'u':
  2928. case 'x':
  2929. case 'X':
  2930. fmtArray[j].flags = kUIntDsvFl;
  2931. snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"Unsigned input %i.",j);
  2932. break;
  2933. case 'f':
  2934. case 'e':
  2935. case 'E':
  2936. case 'g':
  2937. case 'G':
  2938. fmtArray[j].flags = kDoubleDsvFl;
  2939. snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"Double input %i.",j);
  2940. break;
  2941. case 'c':
  2942. fmtArray[j].flags = kUCharDsvFl;
  2943. snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"Unsigned char input %i.",j);
  2944. break;
  2945. case 's':
  2946. fmtArray[j].flags = kStrzDsvFl | kSymDsvFl;
  2947. snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"String input %i.",j);
  2948. break;
  2949. default:
  2950. { assert(0); }
  2951. }
  2952. i += fn - 1;
  2953. ++j;
  2954. }
  2955. }
  2956. return kOkDspRC;
  2957. }
  2958. cmDspRC_t _cmSprintfLoadFormat(cmDspSprintf_t** pp, cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned instSymId, unsigned id, unsigned storeSymId, cmDspVarArg_t args[], unsigned va_cnt, va_list vl )
  2959. {
  2960. cmDspRC_t rc;
  2961. unsigned i,j;
  2962. va_list vl2;
  2963. unsigned inCnt = 0;
  2964. unsigned fixedArgCnt = 0;
  2965. cmDspSprintf_t* p = NULL;
  2966. const cmChar_t* fmt = NULL;
  2967. cmDspSprintfFmt_t* fmtArray = NULL;
  2968. va_copy(vl2,vl);
  2969. *pp = NULL;
  2970. if( va_cnt > 0 )
  2971. fmt = va_arg(vl,const char*);
  2972. if( va_cnt < 1 || fmt == NULL )
  2973. return cmErrMsg(&classPtr->err,kVarArgParseFailDspRC,"Expected the format string as the first argument.");
  2974. // calc the number of fixed args
  2975. for(i=0; args[i].label != NULL; ++i)
  2976. ++fixedArgCnt;
  2977. // calc the count of input args
  2978. if((rc = _cmSprintfGetInputCnt(ctx, classPtr, fmt, &inCnt)) != kOkDspRC )
  2979. return rc;
  2980. if( inCnt > 0 )
  2981. {
  2982. fmtArray = cmLhAllocZ( ctx->lhH, cmDspSprintfFmt_t, inCnt );
  2983. rc = _cmSprintfGetInputTypes(ctx, classPtr, fmt, fmtArray, inCnt );
  2984. }
  2985. if( rc == kOkDspRC )
  2986. {
  2987. unsigned argCnt = fixedArgCnt + inCnt;
  2988. cmDspVarArg_t argArray[ argCnt+1 ];
  2989. // copy in fixed args
  2990. for(i=0; i<fixedArgCnt; ++i)
  2991. argArray[i] = args[i];
  2992. // calc input args
  2993. for(j=0; i<argCnt; ++i,++j)
  2994. {
  2995. argArray[i].label = fmtArray[j].label;
  2996. argArray[i].constId = i;
  2997. argArray[i].rn = 0;
  2998. argArray[i].cn = 0;
  2999. argArray[i].flags = kInDsvFl | fmtArray[j].flags;
  3000. argArray[i].doc = fmtArray[j].doc;
  3001. }
  3002. // set the sentinel arg to all zeros
  3003. memset(argArray + argCnt,0,sizeof(cmDspVarArg_t));
  3004. if((p = cmDspInstAlloc(cmDspSprintf_t,ctx,classPtr,argArray,instSymId,id,storeSymId,va_cnt,vl2)) != NULL )
  3005. {
  3006. // make a copy of the format string - we need to be sure that it is in
  3007. // r/w memory in order that _cmDspSprintfGenString() can write to it
  3008. p->fmtStr = cmLhResizeN(ctx->lhH,cmChar_t,p->fmtStr,strlen(fmt)+1);
  3009. strcpy(p->fmtStr,fmt);
  3010. cmLhFree(ctx->lhH,p->fmtArray);
  3011. p->fmtArray = fmtArray;
  3012. p->inCnt = inCnt;
  3013. memset(p->buf,0,kSprintfBufCharCnt+1);
  3014. }
  3015. *pp = p;
  3016. }
  3017. if( cmErrLastRC(&classPtr->err) != kOkDspRC )
  3018. cmLhFree(ctx->lhH,fmtArray);
  3019. return rc;
  3020. }
  3021. cmDspRC_t _cmDspSprintfGenString(cmDspCtx_t* ctx, cmDspSprintf_t* p )
  3022. {
  3023. cmDspRC_t rc = kOkDspRC;
  3024. unsigned fsi = 0; // format string index
  3025. unsigned i = 0; // fmtArray[] index
  3026. unsigned bi = 0; // string buffer index
  3027. unsigned bn = kSprintfBufCharCnt; // available char's in the string buffer
  3028. cmChar_t* fmt = p->fmtStr;
  3029. unsigned fn = strlen(fmt)+1;
  3030. // for each
  3031. for(i=0; i<p->inCnt && bn>0; ++i)
  3032. {
  3033. const cmDspSprintfFmt_t* f = p->fmtArray + i;
  3034. unsigned varId = kInSpId + i;
  3035. const cmDspVar_t* varPtr = cmDspVarIdToCPtr(&p->inst, varId);
  3036. assert(varPtr != NULL);
  3037. // if there are literal char's to copy prior to the format
  3038. if( fsi < f->fsi )
  3039. {
  3040. unsigned cn = cmMin(f->fsi-fsi,bn);
  3041. strncpy(p->buf+bi,fmt+fsi,cn);
  3042. bn -= cn;
  3043. fsi += cn;
  3044. bi += cn;
  3045. }
  3046. if( bn == 0 )
  3047. {
  3048. rc = cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The internal string buffer is too small.");
  3049. break;
  3050. }
  3051. unsigned pn = 0;
  3052. char c = fmt[ f->fsi + f->fsn ];
  3053. // zero terminate the format string for this input
  3054. fmt[ f->fsi + f->fsn] = 0;
  3055. // if the conversion fmt is for a string then the kSymDsvFl will be set
  3056. // which will prevent the switch() from working - so clear the sym flag here.
  3057. unsigned flags = cmClrFlag(f->flags,kSymDsvFl);
  3058. switch(flags)
  3059. {
  3060. case kUCharDsvFl:
  3061. // not implemented - need to implment a uchar variable type or
  3062. // assume a one character strz.
  3063. assert(0);
  3064. break;
  3065. case kIntDsvFl:
  3066. pn = snprintf(p->buf+bi,bn,fmt + fsi, cmDspInt(&p->inst,varId));
  3067. break;
  3068. case kUIntDsvFl:
  3069. pn = snprintf(p->buf+bi,bn,fmt + fsi, cmDspUInt(&p->inst,varId));
  3070. break;
  3071. case kDoubleDsvFl:
  3072. pn = snprintf(p->buf+bi,bn,fmt + fsi, cmDspDouble(&p->inst,varId));
  3073. break;
  3074. case kStrzDsvFl:
  3075. if( cmDspIsSymbol(&p->inst,varId) )
  3076. pn = snprintf(p->buf+bi,bn,fmt + fsi, cmStringNullGuard(cmSymTblLabel(ctx->stH,cmDspSymbol(&p->inst,varId))));
  3077. else
  3078. pn = snprintf(p->buf+bi,bn,fmt + fsi, cmDspStrcz(&p->inst,varId));
  3079. break;
  3080. default:
  3081. { assert(0); }
  3082. }
  3083. // restore the char written over by the termination zero
  3084. fmt[ f->fsi + f->fsn] = c;
  3085. assert(pn<=bn);
  3086. bn -= pn;
  3087. bi += pn;
  3088. fsi += f->fsn;
  3089. }
  3090. // if there is literal text in the format string after the last conversion spec.
  3091. if( fsi < fn )
  3092. {
  3093. unsigned cn = cmMin(fn-fsi,bn);
  3094. strncpy(p->buf+bi,fmt+fsi,cn);
  3095. }
  3096. return rc;
  3097. }
  3098. cmDspInst_t* _cmDspSprintfAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3099. {
  3100. cmDspVarArg_t args[] =
  3101. {
  3102. { "fmt", kFmtSpId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Format string" },
  3103. { "out", kOutSpId, 0, 0, kOutDsvFl | kStrzDsvFl, "Output string" },
  3104. { NULL, 0, 0, 0, 0 }
  3105. };
  3106. cmDspSprintf_t* p = NULL;
  3107. if( _cmSprintfLoadFormat(&p, ctx, classPtr, instSymId, id, storeSymId, args, va_cnt, vl ) == kOkDspRC )
  3108. return &p->inst;
  3109. return NULL;
  3110. }
  3111. cmDspRC_t _cmDspSprintfFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3112. {
  3113. cmDspSprintf_t* p = (cmDspSprintf_t*)inst;
  3114. cmLhFree(ctx->lhH,p->fmtArray);
  3115. cmLhFree(ctx->lhH,p->fmtStr);
  3116. p->fmtArray=NULL;
  3117. return kOkDspRC;
  3118. }
  3119. cmDspRC_t _cmDspSprintfReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3120. {
  3121. return kOkDspRC;
  3122. }
  3123. cmDspRC_t _cmDspSprintfRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3124. {
  3125. cmDspRC_t rc= kOkDspRC;
  3126. cmDspSprintf_t* p = (cmDspSprintf_t*)inst;
  3127. if( kInSpId <= evt->dstVarId && evt->dstVarId < kInSpId + p->inCnt )
  3128. {
  3129. cmDspSetEvent(ctx,inst,evt);
  3130. //if( evt->dstVarId == kInSpId )
  3131. if((rc = _cmDspSprintfGenString(ctx,p)) == kOkDspRC )
  3132. cmDspSetStrcz(ctx,inst,kOutSpId,p->buf);
  3133. }
  3134. return rc;
  3135. }
  3136. cmDspClass_t* cmSprintfClassCons( cmDspCtx_t* ctx )
  3137. {
  3138. cmDspClassSetup(&_cmSprintfDC,ctx,"Sprintf",
  3139. NULL,
  3140. _cmDspSprintfAlloc,
  3141. _cmDspSprintfFree,
  3142. _cmDspSprintfReset,
  3143. NULL,
  3144. _cmDspSprintfRecv,
  3145. NULL,NULL,
  3146. "String formatter." );
  3147. return &_cmSprintfDC;
  3148. }
  3149. //------------------------------------------------------------------------------------------------------------
  3150. //)
  3151. //( { label:cmDspAMix file_desc:"Audio mixer." kw:[sunit] }
  3152. enum
  3153. {
  3154. kOutAmId,
  3155. kBaseInAmId
  3156. };
  3157. cmDspClass_t _cmAMixDC;
  3158. typedef struct
  3159. {
  3160. cmDspInst_t inst;
  3161. unsigned inPortCnt;
  3162. unsigned baseGainId;
  3163. unsigned baseMuteId;
  3164. } cmDspAMix_t;
  3165. cmDspInst_t* _cmDspAMixAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3166. {
  3167. if( va_cnt < 1 )
  3168. {
  3169. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'AMix' constructor must have a count of input ports.");
  3170. return NULL;
  3171. }
  3172. // args:
  3173. // <inPortCnt> <gain0>, <gain1> (the default gains are optional)
  3174. unsigned i;
  3175. int inPortCnt = va_arg(vl,int);
  3176. unsigned baseGainAmId = kBaseInAmId + inPortCnt;
  3177. unsigned baseMuteAmId = baseGainAmId + inPortCnt;
  3178. double dfltGain[ inPortCnt ];
  3179. if( va_cnt == 1 )
  3180. cmVOD_Fill(dfltGain,inPortCnt,1.0);
  3181. else
  3182. if( va_cnt == 2 )
  3183. {
  3184. dfltGain[0] = va_arg(vl,double);
  3185. cmVOD_Fill(dfltGain+1,inPortCnt-1,dfltGain[0]);
  3186. }
  3187. else
  3188. if( va_cnt == inPortCnt + 1 )
  3189. {
  3190. for(i=0; i<inPortCnt; ++i)
  3191. dfltGain[i] = va_arg(vl,double);
  3192. }
  3193. else
  3194. {
  3195. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The mix argument list must contain no default gain values, one default gain value, or all default gain values.");
  3196. return NULL;
  3197. }
  3198. cmDspAMix_t* p = cmDspInstAllocV(cmDspAMix_t,ctx,classPtr,instSymId,id,storeSymId,0,vl,
  3199. 1, "out", kOutAmId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  3200. inPortCnt, "in", kBaseInAmId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input",
  3201. inPortCnt, "gain", baseGainAmId, 0, 0, kInDsvFl | kDoubleDsvFl, "Gain input",
  3202. inPortCnt, "mute", baseMuteAmId, 0, 0, kInDsvFl | kBoolDsvFl, "Mute input",
  3203. 0 );
  3204. p->inPortCnt = inPortCnt;
  3205. p->baseGainId = baseGainAmId;
  3206. p->baseMuteId = baseMuteAmId;
  3207. for(i=0; i<inPortCnt; ++i)
  3208. {
  3209. cmDspSetDefaultDouble( ctx, &p->inst, p->baseGainId + i, 0.0, dfltGain[i]);
  3210. cmDspSetDefaultBool( ctx, &p->inst, p->baseMuteId + i, false, false );
  3211. }
  3212. /*
  3213. // read any default gain settings
  3214. --va_cnt;
  3215. for(i=0; i<inPortCnt; ++i)
  3216. {
  3217. // if excplicit gains are not given then default to 1.0.
  3218. double dflt = 1.0;
  3219. if( i < va_cnt )
  3220. dflt = va_arg(vl,double);
  3221. cmDspSetDefaultDouble( ctx, &p->inst, p->baseGainId + i, 0.0, dflt);
  3222. }
  3223. */
  3224. return &p->inst;
  3225. }
  3226. cmDspRC_t _cmDspAMixReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3227. {
  3228. cmDspRC_t rc = kOkDspRC;
  3229. cmDspApplyAllDefaults(ctx,inst);
  3230. cmDspZeroAudioBuf(ctx,inst,kOutAmId);
  3231. return rc;
  3232. }
  3233. cmDspRC_t _cmDspAMixExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3234. {
  3235. cmDspAMix_t* p = (cmDspAMix_t*)inst;
  3236. unsigned i;
  3237. cmDspZeroAudioBuf(ctx,inst,kOutAmId);
  3238. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutAmId,0);
  3239. cmSample_t* dp = cmDspAudioBuf(ctx,inst,kOutAmId,0);
  3240. for(i=0; i<p->inPortCnt; ++i)
  3241. {
  3242. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kBaseInAmId+i,0);
  3243. if( sp != NULL )
  3244. {
  3245. double gain = cmDspDouble(inst,p->baseGainId+i);
  3246. cmVOS_MultSumVVS(dp,n,sp,(cmSample_t)gain);
  3247. }
  3248. }
  3249. return kOkDspRC;
  3250. }
  3251. cmDspRC_t _cmDspAMixRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3252. {
  3253. cmDspRC_t rc = kOkDspRC;
  3254. cmDspAMix_t* p = (cmDspAMix_t*)inst;
  3255. if( p->baseGainId <= evt->dstVarId && evt->dstVarId < p->baseGainId + p->inPortCnt )
  3256. {
  3257. cmDspSetEvent(ctx,inst,evt);
  3258. //printf("rcv:%i %f\n",evt->dstVarId,cmDspDouble(inst,evt->dstVarId));
  3259. }
  3260. return rc;
  3261. }
  3262. cmDspClass_t* cmAMixClassCons( cmDspCtx_t* ctx )
  3263. {
  3264. cmDspClassSetup(&_cmAMixDC,ctx,"AMix",
  3265. NULL,
  3266. _cmDspAMixAlloc,
  3267. NULL,
  3268. _cmDspAMixReset,
  3269. _cmDspAMixExec,
  3270. _cmDspAMixRecv,
  3271. NULL,NULL,
  3272. "Audio mixer");
  3273. return &_cmAMixDC;
  3274. }
  3275. //------------------------------------------------------------------------------------------------------------
  3276. //)
  3277. //( { label:cmDspASplit file_desc:"Audio splitter with individual gain control." kw:[sunit] }
  3278. enum
  3279. {
  3280. kInAsId,
  3281. kBaseOutAsId
  3282. };
  3283. cmDspClass_t _cmASplitDC;
  3284. typedef struct
  3285. {
  3286. cmDspInst_t inst;
  3287. unsigned outPortCnt;
  3288. unsigned baseGainId;
  3289. } cmDspASplit_t;
  3290. // A splitter has one audio input port and multiple audio output ports.
  3291. // A gain input is automatically provided for each output port.
  3292. cmDspInst_t* _cmDspASplitAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3293. {
  3294. cmDspVarArg_t args[] =
  3295. {
  3296. { "in", kInAsId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input" },
  3297. { NULL, 0, 0, 0, 0 }
  3298. };
  3299. if( va_cnt < 1 )
  3300. {
  3301. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'ASplit' constructor must have a count of input ports.");
  3302. return NULL;
  3303. }
  3304. // args:
  3305. // <outPortCnt> <gain0>, <gain1> (the default gains are optional)
  3306. unsigned i,j,k;
  3307. int outPortCnt = va_arg(vl,int);
  3308. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]) - 1;
  3309. unsigned argCnt = fixArgCnt + 2*outPortCnt + 1;
  3310. cmDspVarArg_t argArray[ argCnt ];
  3311. int labelCharCnt = 15;
  3312. cmChar_t label[ labelCharCnt + 1 ];
  3313. label[labelCharCnt] = 0;
  3314. //
  3315. for(i=0; i<fixArgCnt; ++i)
  3316. argArray[i] = args[i];
  3317. // define the audio output port specifications
  3318. for(j=0,k=0; j<outPortCnt; ++i,++j,++k)
  3319. {
  3320. snprintf(label,labelCharCnt,"out-%i",j);
  3321. unsigned symId = cmSymTblRegisterSymbol(ctx->stH,label);
  3322. argArray[i].label = cmSymTblLabel(ctx->stH,symId);
  3323. argArray[i].constId = kBaseOutAsId + k;
  3324. argArray[i].rn = 0;
  3325. argArray[i].cn = 1;
  3326. argArray[i].flags = kOutDsvFl | kAudioBufDsvFl;
  3327. argArray[i].doc = "Audio Input";
  3328. }
  3329. // define the gain input specifications
  3330. for(j=0; j<outPortCnt; ++i,++j,++k)
  3331. {
  3332. snprintf(label,labelCharCnt,"gain-%i",j);
  3333. unsigned symId = cmSymTblRegisterSymbol(ctx->stH,label);
  3334. argArray[i].label = cmSymTblLabel(ctx->stH,symId);
  3335. argArray[i].constId = kBaseOutAsId + k;
  3336. argArray[i].rn = 0;
  3337. argArray[i].cn = 0;
  3338. argArray[i].flags = kInDsvFl | kDoubleDsvFl;
  3339. argArray[i].doc = "Gain input";
  3340. }
  3341. // set the NULL end-of-arg-array sentinel
  3342. memset(argArray + i, 0, sizeof(argArray[0]));
  3343. cmDspASplit_t* p = cmDspInstAlloc(cmDspASplit_t,ctx,classPtr,argArray,instSymId,id,storeSymId,0,vl);
  3344. p->outPortCnt = outPortCnt;
  3345. p->baseGainId = kBaseOutAsId + outPortCnt;
  3346. // read any default gain settings
  3347. --va_cnt;
  3348. for(i=0; i<outPortCnt; ++i)
  3349. {
  3350. double dflt = 1.0;
  3351. if( i < va_cnt )
  3352. dflt = va_arg(vl,double);
  3353. cmDspSetDefaultDouble( ctx, &p->inst, p->baseGainId + i, 0.0, dflt);
  3354. }
  3355. return &p->inst;
  3356. }
  3357. cmDspRC_t _cmDspASplitReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3358. {
  3359. cmDspRC_t rc = kOkDspRC;
  3360. cmDspASplit_t* p = (cmDspASplit_t*)inst;
  3361. int i;
  3362. cmDspApplyAllDefaults(ctx,inst);
  3363. for(i=0; i<p->outPortCnt; ++i)
  3364. cmDspZeroAudioBuf(ctx,inst,kBaseOutAsId+i);
  3365. return rc;
  3366. }
  3367. cmDspRC_t _cmDspASplitExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3368. {
  3369. cmDspASplit_t* p = (cmDspASplit_t*)inst;
  3370. unsigned i;
  3371. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kBaseOutAsId,0);
  3372. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kInAsId,0);
  3373. for(i=0; i<p->outPortCnt; ++i)
  3374. {
  3375. cmSample_t* dp = cmDspAudioBuf(ctx,inst,kBaseOutAsId+i,0);
  3376. double gain = cmDspDouble(inst,p->baseGainId+i);
  3377. cmVOS_MultVVS(dp,n,sp,(cmSample_t)gain);
  3378. }
  3379. return kOkDspRC;
  3380. }
  3381. cmDspRC_t _cmDspASplitRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3382. {
  3383. cmDspRC_t rc = kOkDspRC;
  3384. cmDspASplit_t* p = (cmDspASplit_t*)inst;
  3385. if( p->baseGainId <= evt->dstVarId && evt->dstVarId < p->baseGainId + p->outPortCnt )
  3386. {
  3387. cmDspSetEvent(ctx,inst,evt);
  3388. }
  3389. return rc;
  3390. }
  3391. cmDspClass_t* cmASplitClassCons( cmDspCtx_t* ctx )
  3392. {
  3393. cmDspClassSetup(&_cmASplitDC,ctx,"ASplit",
  3394. NULL,
  3395. _cmDspASplitAlloc,
  3396. NULL,
  3397. _cmDspASplitReset,
  3398. _cmDspASplitExec,
  3399. _cmDspASplitRecv,
  3400. NULL,NULL,
  3401. "Audio splitter");
  3402. return &_cmASplitDC;
  3403. }
  3404. //------------------------------------------------------------------------------------------------------------
  3405. //)
  3406. //( { label:cmDspAMeter file_desc:"Audio level meter." kw:[sunit] }
  3407. enum
  3408. {
  3409. kInAmId,
  3410. kMinAmId,
  3411. kMaxAmId,
  3412. kValAmId, // meter value
  3413. kLblAmId,
  3414. };
  3415. #define cmDspMeter_MIN (-100)
  3416. #define cmDspMeter_MAX (0)
  3417. cmDspClass_t _cmAMeterDC;
  3418. typedef struct
  3419. {
  3420. cmDspInst_t inst;
  3421. unsigned bufN;
  3422. unsigned idx;
  3423. cmReal_t sum;
  3424. cmReal_t val;
  3425. } cmDspAMeter_t;
  3426. cmDspInst_t* _cmDspAMeterAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3427. {
  3428. cmDspVarArg_t args[] =
  3429. {
  3430. { "in", kInAmId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input"},
  3431. { "min", kMinAmId, 0, 0, kDoubleDsvFl, "Minimum value"},
  3432. { "max", kMaxAmId, 0, 0, kDoubleDsvFl, "Maximum value"},
  3433. { "val", kValAmId, 0, 0, kDoubleDsvFl, "Meter value"},
  3434. { "label",kLblAmId, 0, 0, kStrzDsvFl, "Label."},
  3435. { NULL, 0, 0, 0, 0 }
  3436. };
  3437. cmDspAMeter_t* p = cmDspInstAlloc(cmDspAMeter_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  3438. double updateMs = 100;
  3439. double sr = cmDspSampleRate(ctx);
  3440. unsigned spc = cmDspSamplesPerCycle(ctx);
  3441. p->bufN = cmMax(1,floor(updateMs * sr/ (1000.0 * spc)));
  3442. cmDspSetDefaultDouble(ctx, &p->inst, kValAmId, 0.0, cmDspMeter_MIN);
  3443. cmDspSetDefaultDouble(ctx, &p->inst, kMinAmId, 0.0, cmDspMeter_MIN);
  3444. cmDspSetDefaultDouble(ctx, &p->inst, kMaxAmId, 0.0, cmDspMeter_MAX);
  3445. // create the UI control
  3446. cmDspUiMeterCreate(ctx,&p->inst,kMinAmId,kMaxAmId,kValAmId,kLblAmId);
  3447. return &p->inst;
  3448. }
  3449. cmDspRC_t _cmDspAMeterReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3450. {
  3451. cmDspAMeter_t* p = (cmDspAMeter_t*)inst;
  3452. cmDspApplyAllDefaults(ctx,inst);
  3453. //cmDspZeroAudioBuf(ctx,inst,kInAmId);
  3454. p->idx = 0;
  3455. p->sum = 0;
  3456. p->val = 0;
  3457. return kOkDspRC;
  3458. }
  3459. cmDspRC_t _cmDspAMeterExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3460. {
  3461. cmDspAMeter_t* p = (cmDspAMeter_t*)inst;
  3462. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kInAmId,0);
  3463. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kInAmId,0);
  3464. if( sp == NULL )
  3465. {
  3466. inst->execFunc = NULL; // if there is no connected input then disable further callbacks
  3467. return kOkDspRC;
  3468. }
  3469. p->sum += cmVOS_SquaredSum(sp,n);
  3470. ++p->idx;
  3471. if( p->idx == p->bufN )
  3472. {
  3473. cmReal_t coeff = 0.7;
  3474. cmReal_t rms = sqrt(p->sum/(n*p->bufN));
  3475. p->idx = 0;
  3476. p->sum = 0;
  3477. p->val = rms > p->val ? rms : (rms*(1.0-coeff)) + (p->val*coeff);
  3478. double db = cmMax(cmDspMeter_MIN,cmMin(cmDspMeter_MAX,20.0 * log10(p->val)));
  3479. cmDspSetDouble(ctx, inst, kValAmId, db);
  3480. }
  3481. return kOkDspRC;
  3482. }
  3483. cmDspClass_t* cmAMeterClassCons( cmDspCtx_t* ctx )
  3484. {
  3485. cmDspClassSetup(&_cmAMeterDC,ctx,"AMeter",
  3486. NULL,
  3487. _cmDspAMeterAlloc,
  3488. NULL,
  3489. _cmDspAMeterReset,
  3490. _cmDspAMeterExec,
  3491. NULL,
  3492. NULL,NULL,
  3493. "Audio meter display.");
  3494. return &_cmAMeterDC;
  3495. }
  3496. //------------------------------------------------------------------------------------------------------------
  3497. //)
  3498. //( { label:cmDspTextFile file_desc:"Create text files which can be read by the Octave function cmTextFile.m." kw:[sunit] }
  3499. //
  3500. //
  3501. // Read files created by this object with the Octave function cmTextFile().
  3502. //
  3503. //
  3504. enum
  3505. {
  3506. kCntTfId,
  3507. kFnTfId,
  3508. kBaseTfId
  3509. };
  3510. cmDspClass_t _cmTextFileDC;
  3511. typedef struct
  3512. {
  3513. cmDspInst_t inst;
  3514. int inPortCnt;
  3515. cmFileH_t fH;
  3516. } cmDspTextFile_t;
  3517. cmDspInst_t* _cmDspTextFileAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3518. {
  3519. cmDspVarArg_t args[] =
  3520. {
  3521. { "cnt", kCntTfId, 0, 0, kIntDsvFl | kReqArgDsvFl, "Input port count"},
  3522. { "fn", kFnTfId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "File name"},
  3523. { NULL, 0, 0, 0, 0 }
  3524. };
  3525. if( va_cnt < 1 )
  3526. {
  3527. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The text file object must be given a input port count argument.");
  3528. return NULL;
  3529. }
  3530. va_list vl1;
  3531. va_copy(vl1,vl);
  3532. int i,j;
  3533. int inPortCnt = va_arg(vl1,int);
  3534. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]) - 1;
  3535. unsigned argCnt = fixArgCnt + inPortCnt + 1;
  3536. cmDspVarArg_t argArray[ argCnt ];
  3537. if( inPortCnt <= 0 )
  3538. {
  3539. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The text file input port count must be a positive integer.");
  3540. return NULL;
  3541. }
  3542. //
  3543. for(i=0; i<fixArgCnt; ++i)
  3544. argArray[i] = args[i];
  3545. // define the input port specifications
  3546. for(j=0; j<inPortCnt; ++j,++i)
  3547. {
  3548. int labelCharCnt = 15;
  3549. cmChar_t label[ labelCharCnt + 1 ];
  3550. label[labelCharCnt] = 0;
  3551. snprintf(label,labelCharCnt,"in-%i",j);
  3552. unsigned symId = cmSymTblRegisterSymbol(ctx->stH,label);
  3553. argArray[i].label = cmSymTblLabel(ctx->stH,symId);
  3554. argArray[i].constId = kBaseTfId + j;
  3555. argArray[i].rn = 0;
  3556. argArray[i].cn = 0;
  3557. argArray[i].flags = kInDsvFl | kDoubleDsvFl | kSymDsvFl;
  3558. argArray[i].doc = "Data input";
  3559. }
  3560. // set the NULL end-of-arg-array sentinel
  3561. memset(argArray + i, 0, sizeof(argArray[0]));
  3562. cmDspTextFile_t* p = cmDspInstAlloc(cmDspTextFile_t,ctx,classPtr,argArray,instSymId,id,storeSymId,va_cnt,vl);
  3563. p->inPortCnt = inPortCnt;
  3564. p->fH = cmFileNullHandle;
  3565. return &p->inst;
  3566. }
  3567. cmDspRC_t _cmDspTextFileFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3568. {
  3569. cmDspTextFile_t* p = (cmDspTextFile_t*)inst;
  3570. if( cmFileClose(&p->fH) )
  3571. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Text file close failed.");
  3572. return kOkDspRC;
  3573. }
  3574. cmDspRC_t _cmDspTextFileReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3575. {
  3576. cmDspRC_t rc = kOkDspRC;
  3577. cmDspTextFile_t* p = (cmDspTextFile_t*)inst;
  3578. cmDspApplyAllDefaults(ctx,inst);
  3579. const cmChar_t* fn = cmDspStrcz(inst,kFnTfId);
  3580. if( cmFileOpen( &p->fH, fn, kWriteFileFl, ctx->cmCtx->err.rpt ) != kOkFileRC )
  3581. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "Text file open failed.");
  3582. return rc;
  3583. }
  3584. cmDspRC_t _cmDspTextFileRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3585. {
  3586. cmDspTextFile_t* p = (cmDspTextFile_t*)inst;
  3587. cmDspSetEvent(ctx,inst,evt);
  3588. if( cmFileIsValid(p->fH) && evt->dstVarId >= kBaseTfId )
  3589. {
  3590. double secs = ctx->ctx->begSmpIdx / cmDspSampleRate(ctx);
  3591. cmFilePrintf(p->fH,"%f %f %i ", secs, secs, evt->dstVarId-kBaseTfId);
  3592. if( cmIsFlag(evt->valuePtr->flags,kSymDsvFl) )
  3593. cmFilePrintf(p->fH,"%s\n", cmStringNullGuard(cmSymTblLabel(ctx->stH,cmDsvSymbol(evt->valuePtr))));
  3594. else
  3595. cmFilePrintf(p->fH,"%f\n", cmDspDouble(inst,evt->dstVarId));
  3596. }
  3597. return kOkDspRC;
  3598. }
  3599. cmDspClass_t* cmTextFileClassCons( cmDspCtx_t* ctx )
  3600. {
  3601. cmDspClassSetup(&_cmTextFileDC,ctx,"TextFile",
  3602. NULL,
  3603. _cmDspTextFileAlloc,
  3604. _cmDspTextFileFree,
  3605. _cmDspTextFileReset,
  3606. NULL,
  3607. _cmDspTextFileRecv,
  3608. NULL,NULL,
  3609. "Time tagged text file.");
  3610. return &_cmTextFileDC;
  3611. }
  3612. //------------------------------------------------------------------------------------------------------------
  3613. //)
  3614. //( { label:cmDspArray file_desc:"Read in a symbol/value list from a resource and selectively transmit values." kw:[sunit] }
  3615. enum
  3616. {
  3617. kRsrcArId,
  3618. kCmdArId,
  3619. kIdxArId,
  3620. kValArId,
  3621. kCntArId,
  3622. kDoneArId,
  3623. kBaseOutArId
  3624. };
  3625. cmDspClass_t _cmArrayDC;
  3626. typedef struct
  3627. {
  3628. cmDspInst_t inst;
  3629. unsigned cnt;
  3630. cmReal_t* array;
  3631. unsigned printSymId;
  3632. unsigned sendSymId;
  3633. unsigned cntSymId;
  3634. unsigned doneSymId;
  3635. } cmDspArray_t;
  3636. cmDspInst_t* _cmDspArrayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3637. {
  3638. va_list vl1;
  3639. va_copy(vl1,vl);
  3640. if( va_cnt < 1 )
  3641. {
  3642. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The array object must be given a 'rsrc' parameter.");
  3643. return NULL;
  3644. }
  3645. const cmChar_t* rsrcStr = va_arg(vl,cmChar_t*);
  3646. unsigned outPortCnt = 0;
  3647. cmReal_t* array = NULL;
  3648. unsigned doneSymId = cmDspSysRegisterStaticSymbol(ctx->dspH,"done");
  3649. unsigned i;
  3650. if( rsrcStr == NULL )
  3651. {
  3652. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The array object 'rsrc' parameter appears to be missing.");
  3653. return NULL;
  3654. }
  3655. if( cmDspRsrcRealArray( ctx->dspH, &outPortCnt, &array, rsrcStr, NULL ) != kOkDspRC )
  3656. {
  3657. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The array resource '%s' could not be read.",cmStringNullGuard(rsrcStr));
  3658. return NULL;
  3659. }
  3660. cmDspArray_t* p = cmDspInstAllocV(cmDspArray_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  3661. 1, "rsrc", kRsrcArId, 0, 0, kStrzDsvFl | kInDsvFl | kReqArgDsvFl, "Array data resource label.",
  3662. 1, "cmd", kCmdArId, 0, 0, kSymDsvFl | kInDsvFl, "Command: send | print | count.",
  3663. 1, "idx", kIdxArId, 0, 0, kUIntDsvFl | kInDsvFl, "Send value at index out 'val' port.",
  3664. 1, "val", kValArId, 0, 0, kDoubleDsvFl | kOutDsvFl, "Index output value.",
  3665. 1, "cnt", kCntArId, 0, 0, kUIntDsvFl | kOutDsvFl, "Count output value.",
  3666. 1, "done", kDoneArId, 0, 0, kSymDsvFl | kOutDsvFl, "'done' after last send.",
  3667. outPortCnt, "out", kBaseOutArId, 0, 0, kDoubleDsvFl | kOutDsvFl | kSendDfltDsvFl, "Individual real value outputs.",
  3668. 0 );
  3669. cmDspSetDefaultDouble( ctx, &p->inst, kValArId, 0, 0 );
  3670. cmDspSetDefaultUInt( ctx, &p->inst, kCntArId, 0, outPortCnt );
  3671. cmDspSetDefaultSymbol( ctx, &p->inst, kDoneArId, doneSymId);
  3672. for(i=0; i<outPortCnt; ++i)
  3673. cmDspSetDefaultDouble( ctx, &p->inst, kBaseOutArId+i, 0, array[i] );
  3674. p->array = array;
  3675. p->cnt = outPortCnt;
  3676. p->sendSymId = cmDspSysRegisterStaticSymbol(ctx->dspH,"send");
  3677. p->printSymId = cmDspSysRegisterStaticSymbol(ctx->dspH,"print");
  3678. p->cntSymId = cmDspSysRegisterStaticSymbol(ctx->dspH,"count");
  3679. p->doneSymId = doneSymId;
  3680. return &p->inst;
  3681. }
  3682. cmDspRC_t _cmDspArrayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3683. {
  3684. cmDspArray_t* p = (cmDspArray_t*)inst;
  3685. cmDspRC_t rc;
  3686. // send 'out' values and then 'done' value
  3687. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  3688. cmDspSetSymbol(ctx,inst,kDoneArId,p->doneSymId);
  3689. return rc;
  3690. }
  3691. cmDspRC_t _cmDspArrayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3692. {
  3693. cmDspRC_t rc;
  3694. cmDspArray_t* p = (cmDspArray_t*)inst;
  3695. if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
  3696. return rc;
  3697. switch( evt->dstVarId )
  3698. {
  3699. case kCmdArId:
  3700. {
  3701. unsigned i;
  3702. unsigned cmdSymId = cmDsvSymbol(evt->valuePtr);
  3703. if( cmdSymId == p->printSymId )
  3704. {
  3705. for(i=0; i<p->cnt; ++i)
  3706. cmRptPrintf(ctx->rpt,"%f ",p->array[i]);
  3707. cmRptPrintf(ctx->rpt,"\n");
  3708. }
  3709. else
  3710. if( cmdSymId == p->sendSymId )
  3711. {
  3712. for(i=0; i<p->cnt; ++i)
  3713. cmDspSetDouble(ctx,inst,kBaseOutArId+i,p->array[i]);
  3714. cmDspSetSymbol(ctx,inst,kDoneArId,p->doneSymId);
  3715. }
  3716. else
  3717. if( cmdSymId == p->cntSymId )
  3718. {
  3719. cmDspSetUInt(ctx,inst,kCntArId,p->cnt);
  3720. }
  3721. }
  3722. break;
  3723. case kIdxArId:
  3724. {
  3725. unsigned idx = cmDsvUInt(evt->valuePtr);
  3726. if( idx < p->cnt )
  3727. cmDspSetDouble(ctx,inst,kValArId,p->array[idx]);
  3728. }
  3729. break;
  3730. }
  3731. return kOkDspRC;
  3732. }
  3733. cmDspClass_t* cmArrayClassCons( cmDspCtx_t* ctx )
  3734. {
  3735. cmDspClassSetup(&_cmArrayDC,ctx,"Array",
  3736. NULL,
  3737. _cmDspArrayAlloc,
  3738. NULL,
  3739. _cmDspArrayReset,
  3740. NULL,
  3741. _cmDspArrayRecv,
  3742. NULL,NULL,
  3743. "Time tagged text file.");
  3744. return &_cmArrayDC;
  3745. }
  3746. //------------------------------------------------------------------------------------------------------------
  3747. //)
  3748. //( { label:cmDspPitchCvt file_desc:"Convert between MIDI,scientific pitch, and pitch ratio values." kw:[sunit] }
  3749. enum
  3750. {
  3751. kMidiPcId,
  3752. kHzPcId,
  3753. kOffsPcId,
  3754. kStrPcId,
  3755. kRatioPcId
  3756. };
  3757. cmDspClass_t _cmPitchCvtDC;
  3758. typedef struct
  3759. {
  3760. cmDspInst_t inst;
  3761. int midi;
  3762. double hz;
  3763. } cmDspPitchCvt_t;
  3764. cmDspInst_t* _cmDspPitchCvtAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3765. {
  3766. va_cnt = 0; // ignore an errant arguments
  3767. cmDspPitchCvt_t* p = cmDspInstAllocV(cmDspPitchCvt_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3768. 1, "midi", kMidiPcId, 0, 0, kUIntDsvFl | kInDsvFl | kOutDsvFl | kOptArgDsvFl, "MIDI pitch value input.",
  3769. 1, "hz", kHzPcId, 0, 0, kDoubleDsvFl | kInDsvFl | kOutDsvFl | kOptArgDsvFl, "Hz pitch value.",
  3770. 1, "offs", kOffsPcId, 0, 0, kDoubleDsvFl | kInDsvFl | kInDsvFl | kOptArgDsvFl, "Semitone offset.",
  3771. 1, "str", kStrPcId, 0, 0, kStrzDsvFl | kOutDsvFl, "Pitch string output.",
  3772. 1, "ratio", kRatioPcId, 0, 0, kDoubleDsvFl | kOutDsvFl, "Offset as a ratio",
  3773. 0 );
  3774. cmDspSetDefaultUInt( ctx, &p->inst, kMidiPcId, 0, 0 );
  3775. cmDspSetDefaultDouble( ctx, &p->inst, kHzPcId, 0, 0.0 );
  3776. cmDspSetDefaultDouble( ctx, &p->inst, kOffsPcId, 0, 0 );
  3777. cmDspSetDefaultStrcz( ctx, &p->inst, kStrPcId, NULL, "" );
  3778. cmDspSetDefaultDouble( ctx, &p->inst, kRatioPcId, 0, 0 );
  3779. return &p->inst;
  3780. }
  3781. cmDspRC_t _cmDspPitchCvtReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3782. {
  3783. cmDspRC_t rc = cmDspApplyAllDefaults(ctx,inst);
  3784. return rc;
  3785. }
  3786. cmDspRC_t _cmDspPitchCvtOutput( cmDspCtx_t* ctx, cmDspPitchCvt_t* p )
  3787. {
  3788. cmDspInst_t* inst = &p->inst;
  3789. double offs = cmDspDouble( inst, kOffsPcId );
  3790. unsigned midi = cmMax(0, p->midi + rint(offs) );
  3791. double ratio= pow(2.0,offs/12.0);
  3792. double hz = p->hz * ratio;
  3793. //cmRptPrintf(ctx->rpt,"%i %i %f %f\n",offs,midi,hz,p->hz);
  3794. cmDspSetStrcz( ctx, inst, kStrPcId, cmMidiToSciPitch(midi,NULL,0));
  3795. cmDspSetUInt( ctx, inst, kMidiPcId, midi);
  3796. cmDspSetDouble( ctx, inst, kHzPcId, hz);
  3797. cmDspSetDouble( ctx, inst, kRatioPcId, ratio );
  3798. return kOkDspRC;
  3799. }
  3800. cmDspRC_t _cmDspPitchCvtRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3801. {
  3802. cmDspRC_t rc = kOkDspRC;
  3803. cmDspPitchCvt_t* p = (cmDspPitchCvt_t*)inst;
  3804. switch( evt->dstVarId )
  3805. {
  3806. case kMidiPcId:
  3807. p->midi = cmMax(0,cmDsvGetInt(evt->valuePtr));
  3808. p->hz = cmMidiToHz(p->midi);
  3809. rc = _cmDspPitchCvtOutput(ctx,p);
  3810. break;
  3811. case kHzPcId:
  3812. p->hz = cmDsvGetDouble(evt->valuePtr);
  3813. p->midi = cmHzToMidi(p->hz);
  3814. rc = _cmDspPitchCvtOutput(ctx,p);
  3815. break;
  3816. case kOffsPcId:
  3817. if((rc = cmDspSetEvent(ctx, inst, evt )) == kOkDspRC )
  3818. rc = _cmDspPitchCvtOutput(ctx,p);
  3819. break;
  3820. }
  3821. return rc;
  3822. }
  3823. cmDspClass_t* cmPitchCvtClassCons( cmDspCtx_t* ctx )
  3824. {
  3825. cmDspClassSetup(&_cmPitchCvtDC,ctx,"PitchCvt",
  3826. NULL,
  3827. _cmDspPitchCvtAlloc,
  3828. NULL,
  3829. _cmDspPitchCvtReset,
  3830. NULL,
  3831. _cmDspPitchCvtRecv,
  3832. NULL,NULL,
  3833. "Time tagged text file.");
  3834. return &_cmPitchCvtDC;
  3835. }
  3836. //------------------------------------------------------------------------------------------------------------
  3837. //)
  3838. //( { label:cmDspBinMtx file_desc:"Create a file which can be read by the Octave function readBinFile.m." kw:[sunit] }
  3839. enum
  3840. {
  3841. kCntBmId,
  3842. kFnBmId,
  3843. kBaseBmId
  3844. };
  3845. cmDspClass_t _cmBinMtxFileDC;
  3846. typedef struct
  3847. {
  3848. cmDspInst_t inst;
  3849. int inPortCnt;
  3850. cmBinMtxFile_t* bmfp;
  3851. cmReal_t * valArray; // valArray[ inPortCnt ]
  3852. } cmDspBinMtxFile_t;
  3853. cmDspInst_t* _cmDspBinMtxFileAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3854. {
  3855. cmDspVarArg_t args[] =
  3856. {
  3857. { "cnt", kCntBmId, 0, 0, kIntDsvFl | kReqArgDsvFl, "Input port count"},
  3858. { "fn", kFnBmId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "File name"},
  3859. };
  3860. if( va_cnt < 1 )
  3861. {
  3862. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The binary matrix file object must be given a input port count argument.");
  3863. return NULL;
  3864. }
  3865. va_list vl1;
  3866. va_copy(vl1,vl);
  3867. int inPortCnt = va_arg(vl,int);
  3868. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]);
  3869. unsigned argCnt = fixArgCnt + inPortCnt;
  3870. cmDspVarArg_t a[ argCnt+1 ];
  3871. cmDspBinMtxFile_t* p = NULL;
  3872. if( inPortCnt <= 0 )
  3873. {
  3874. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The binary matrix file must be a positive integer.");
  3875. return NULL;
  3876. }
  3877. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  3878. cmDspArgSetupN(ctx, a, argCnt, kBaseBmId, inPortCnt, "in", kBaseBmId, 0, 0, kInDsvFl | kDoubleDsvFl, "input ports");
  3879. cmDspArgSetupNull( a+argCnt );
  3880. if((p = cmDspInstAlloc(cmDspBinMtxFile_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl1)) == NULL )
  3881. return NULL;
  3882. p->bmfp = cmBinMtxFileAlloc(ctx->cmProcCtx, NULL, NULL );
  3883. p->inPortCnt = inPortCnt;
  3884. p->valArray = cmMemAllocZ(cmReal_t,inPortCnt);
  3885. return &p->inst;
  3886. }
  3887. cmDspRC_t _cmDspBinMtxFileOpen( cmDspCtx_t* ctx, cmDspInst_t* inst )
  3888. {
  3889. cmDspRC_t rc = kOkDspRC;
  3890. cmDspBinMtxFile_t* p = (cmDspBinMtxFile_t*)inst;
  3891. if( p->bmfp != NULL )
  3892. {
  3893. // finalize the current file
  3894. if( cmBinMtxFileFinal(p->bmfp) != cmOkRC )
  3895. cmDspInstErr(ctx,inst,kFileCloseFailDspRC,"File close failed.");
  3896. // open a new one
  3897. if( cmBinMtxFileInit( p->bmfp, cmDspDefaultStrcz(&p->inst,kFnBmId) ) != cmOkRC)
  3898. rc = cmDspInstErr(ctx,inst,kFileOpenFailDspRC,"File open failed.");
  3899. }
  3900. return rc;
  3901. }
  3902. cmDspRC_t _cmDspBinMtxFileFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3903. {
  3904. cmDspBinMtxFile_t* p = (cmDspBinMtxFile_t*)inst;
  3905. cmBinMtxFileFree(&p->bmfp);
  3906. cmMemFree(p->valArray);
  3907. return kOkDspRC;
  3908. }
  3909. cmDspRC_t _cmDspBinMtxFileReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3910. {
  3911. cmDspApplyAllDefaults(ctx,inst);
  3912. return _cmDspBinMtxFileOpen(ctx,inst);
  3913. }
  3914. cmDspRC_t _cmDspBinMtxFileExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3915. {
  3916. cmDspRC_t rc = kOkDspRC;
  3917. cmDspBinMtxFile_t* p = (cmDspBinMtxFile_t*)inst;
  3918. // write the file
  3919. if( cmBinMtxFileIsValid( p->bmfp ) )
  3920. if( cmBinMtxFileExecR(p->bmfp, p->valArray, p->inPortCnt ) != cmOkRC )
  3921. return cmDspInstErr(ctx,inst,kFileWriteFailDspRC,"File write failure.");
  3922. return rc;
  3923. }
  3924. cmDspRC_t _cmDspBinMtxFileRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3925. {
  3926. cmDspRC_t rc = kOkDspRC;
  3927. cmDspBinMtxFile_t* p = (cmDspBinMtxFile_t*)inst;
  3928. cmDspSetEvent(ctx,inst,evt);
  3929. // new file name - create new output file
  3930. if( evt->dstVarId == kFnBmId )
  3931. {
  3932. rc = _cmDspBinMtxFileOpen(ctx,inst);
  3933. }
  3934. else
  3935. // new value - store in p->valArray[]
  3936. if( kBaseBmId <= evt->dstVarId && evt->dstVarId < kBaseBmId + p->inPortCnt )
  3937. {
  3938. p->valArray[ evt->dstVarId - kBaseBmId ] = cmDspDouble(inst,evt->dstVarId );
  3939. }
  3940. else
  3941. { assert(0); }
  3942. return rc;
  3943. }
  3944. cmDspClass_t* cmBinMtxFileClassCons( cmDspCtx_t* ctx )
  3945. {
  3946. cmDspClassSetup(&_cmBinMtxFileDC,ctx,"BinMtxFile",
  3947. NULL,
  3948. _cmDspBinMtxFileAlloc,
  3949. _cmDspBinMtxFileFree,
  3950. _cmDspBinMtxFileReset,
  3951. _cmDspBinMtxFileExec,
  3952. _cmDspBinMtxFileRecv,
  3953. NULL,NULL,
  3954. "Time tagged text file.");
  3955. return &_cmBinMtxFileDC;
  3956. }
  3957. //------------------------------------------------------------------------------------------------------------
  3958. //)
  3959. //( { label:cmDspShiftBuf file_desc:"Real-time shift buffer." kw:[sunit] }
  3960. enum
  3961. {
  3962. kHopMsSbId,
  3963. kWndFactSbId,
  3964. kInSbId,
  3965. kOutSbId
  3966. };
  3967. cmDspClass_t _cmShiftBufDC;
  3968. typedef struct
  3969. {
  3970. cmDspInst_t inst;
  3971. cmShiftBuf* sbp;
  3972. } cmDspShiftBuf_t;
  3973. void _cmDspShiftBufSetup( cmDspCtx_t* ctx, cmDspShiftBuf_t* p )
  3974. {
  3975. double hopMs = cmDspDouble(&p->inst,kHopMsSbId);
  3976. unsigned hopSmpCnt = lround(cmDspSampleRate(ctx) * hopMs / 1000.0 );
  3977. unsigned wndSmpCnt = cmDspUInt(&p->inst,kWndFactSbId) * hopSmpCnt;
  3978. if( p->sbp == NULL || hopSmpCnt != p->sbp->hopSmpCnt || wndSmpCnt != p->sbp->wndSmpCnt )
  3979. {
  3980. cmShiftBufFree(&p->sbp);
  3981. p->sbp = cmShiftBufAlloc(ctx->cmProcCtx, NULL, cmDspSamplesPerCycle(ctx), wndSmpCnt, hopSmpCnt );
  3982. }
  3983. }
  3984. cmDspInst_t* _cmDspShiftBufAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3985. {
  3986. cmDspVarArg_t args[] =
  3987. {
  3988. { "hopMs", kHopMsSbId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Hop size on milliseconds"},
  3989. { "wndFact", kWndFactSbId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Count of hops contained in each output buffer."},
  3990. { "in", kInSbId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input"},
  3991. { "out", kOutSbId, 0, 0, kOutDsvFl | kAudioBufDsvFl, "Audio output"},
  3992. { NULL, 0, 0, 0, 0 }
  3993. };
  3994. // Note: by setting the column count of the output audio variable to zero
  3995. // we prevent it from being automatically assigned vector memory.
  3996. cmDspShiftBuf_t* p = cmDspInstAlloc(cmDspShiftBuf_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  3997. return &p->inst;
  3998. }
  3999. cmDspRC_t _cmDspShiftBufFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4000. {
  4001. cmDspShiftBuf_t* p = (cmDspShiftBuf_t*)inst;
  4002. cmShiftBufFree(&p->sbp);
  4003. return kOkDspRC;
  4004. }
  4005. cmDspRC_t _cmDspShiftBufReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4006. {
  4007. cmDspRC_t rc = kOkDspRC;
  4008. cmDspShiftBuf_t* p = (cmDspShiftBuf_t*)inst;
  4009. cmDspApplyAllDefaults(ctx,inst);
  4010. _cmDspShiftBufSetup(ctx,p);
  4011. return rc;
  4012. }
  4013. cmDspRC_t _cmDspShiftBufRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4014. {
  4015. cmDspShiftBuf_t* p = (cmDspShiftBuf_t*)inst;
  4016. cmDspSetEvent(ctx,inst,evt);
  4017. switch(evt->dstVarId)
  4018. {
  4019. case kHopMsSbId:
  4020. case kWndFactSbId:
  4021. _cmDspShiftBufSetup( ctx, p );
  4022. break;
  4023. };
  4024. return kOkDspRC;
  4025. }
  4026. cmDspRC_t _cmDspShiftBufExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4027. {
  4028. cmDspShiftBuf_t* p = (cmDspShiftBuf_t*)inst;
  4029. unsigned sn = cmDspAudioBufSmpCount(ctx,inst,kInSbId,0);
  4030. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kInSbId,0);
  4031. cmDspVar_t* varPtr = cmDspVarIdToPtr(inst,kOutSbId);
  4032. if( cmShiftBufExec(p->sbp, sp, sn ) )
  4033. cmDsvSetSampleMtx( &varPtr->value, p->sbp->outV, p->sbp->outN, 1);
  4034. else
  4035. cmDsvSetSampleMtx( &varPtr->value, NULL, 0, 0);
  4036. return kOkDspRC;
  4037. }
  4038. cmDspClass_t* cmShiftBufClassCons( cmDspCtx_t* ctx )
  4039. {
  4040. cmDspClassSetup(&_cmShiftBufDC,ctx,"ShiftBuf",
  4041. NULL,
  4042. _cmDspShiftBufAlloc,
  4043. _cmDspShiftBufFree,
  4044. _cmDspShiftBufReset,
  4045. _cmDspShiftBufExec,
  4046. _cmDspShiftBufRecv,
  4047. NULL,NULL,
  4048. "Time tagged text file.");
  4049. return &_cmShiftBufDC;
  4050. }
  4051. //------------------------------------------------------------------------------------------------------------
  4052. //)
  4053. //( { label:cmDspNetSend file_desc:"Transmit a value to a remote 'snap' host over the 'snap' UDP network." kw:[sunit] }
  4054. enum
  4055. {
  4056. kInNsId
  4057. };
  4058. cmDspClass_t _cmNetSendDC;
  4059. typedef struct
  4060. {
  4061. cmDspInst_t inst;
  4062. _cmDspSrcConn_t* srcConnPtr;
  4063. } cmDspNetSend_t;
  4064. cmDspInst_t* _cmDspNetSendAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4065. {
  4066. cmDspVarArg_t args[] =
  4067. {
  4068. { "in", kInNsId, 0, 0, kInDsvFl | kTypeDsvMask, "Input port" },
  4069. { NULL, 0, 0, 0, 0 }
  4070. };
  4071. assert( va_cnt == 1 );
  4072. _cmDspSrcConn_t* srcConnPtr = va_arg(vl,_cmDspSrcConn_t*);
  4073. assert( srcConnPtr != NULL );
  4074. cmDspNetSend_t* p = cmDspInstAlloc(cmDspNetSend_t,ctx,classPtr,args,instSymId,id,storeSymId,0,vl);
  4075. p->srcConnPtr = srcConnPtr;
  4076. return &p->inst;
  4077. }
  4078. cmDspRC_t _cmDspNetSendReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4079. {
  4080. return kOkDspRC;
  4081. }
  4082. cmDspRC_t _cmDspNetSendRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4083. {
  4084. cmDspNetSend_t* p = (cmDspNetSend_t*)inst;
  4085. return _cmDspSysNetSendEvent(ctx->dspH, p->srcConnPtr->dstNetNodeId, p->srcConnPtr->dstId, evt );
  4086. }
  4087. cmDspClass_t* cmNetSendClassCons( cmDspCtx_t* ctx )
  4088. {
  4089. cmDspClassSetup(&_cmNetSendDC,ctx,"NetSend",
  4090. NULL,
  4091. _cmDspNetSendAlloc,
  4092. NULL,
  4093. _cmDspNetSendReset,
  4094. NULL,
  4095. _cmDspNetSendRecv,
  4096. NULL,NULL,
  4097. "Print the value of any event arriving at 'in'.");
  4098. return &_cmNetSendDC;
  4099. }
  4100. //------------------------------------------------------------------------------------------------------------
  4101. //)
  4102. //( { label:cmDspRsrWr file_desc:"Set a 'snap' resource value." kw:[sunit] }
  4103. enum
  4104. {
  4105. kBaseInPtsId,
  4106. };
  4107. cmDspClass_t _cmRsrcWrDC;
  4108. typedef struct
  4109. {
  4110. cmDspInst_t inst;
  4111. char** pathArray;
  4112. unsigned pathCnt;
  4113. } cmDspRsrcWr_t;
  4114. cmDspInst_t* _cmDspRsrcWrAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4115. {
  4116. va_list vl1;
  4117. va_copy(vl1,vl);
  4118. if( va_cnt < 1 )
  4119. {
  4120. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'RsrcWr' constructor argument list must contain at least one resource path specificiation.");
  4121. return NULL;
  4122. }
  4123. unsigned pathCnt = va_cnt;
  4124. unsigned argCnt = pathCnt;
  4125. cmDspVarArg_t args[argCnt+1];
  4126. char** pathArray = cmMemAllocZ(char*,pathCnt);
  4127. unsigned i;
  4128. for(i=0; i<pathCnt; ++i)
  4129. {
  4130. // get the path
  4131. const cmChar_t* pathLabel = va_arg(vl,const char*);
  4132. assert( pathLabel != NULL );
  4133. // store the path name
  4134. pathArray[i] = cmMemAllocStr(pathLabel);
  4135. cmDspArgSetup(ctx, args+kBaseInPtsId+i, pathLabel, cmInvalidId, kBaseInPtsId+i, 0, 0, kInDsvFl | kTypeDsvMask, cmTsPrintfH(ctx->lhH,"%s Input.",pathLabel) );
  4136. }
  4137. cmDspArgSetupNull(args + argCnt);
  4138. cmDspRsrcWr_t* p = cmDspInstAlloc(cmDspRsrcWr_t,ctx,classPtr,args,instSymId,id,storeSymId,0,vl1);
  4139. p->pathCnt = pathCnt;
  4140. p->pathArray = pathArray;
  4141. return &p->inst;
  4142. }
  4143. cmDspRC_t _cmDspRsrcWrFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4144. {
  4145. cmDspRsrcWr_t* p = (cmDspRsrcWr_t*)inst;
  4146. int i;
  4147. for(i=0; i<p->pathCnt; ++i)
  4148. cmMemFree(p->pathArray[i]);
  4149. cmMemFree(p->pathArray);
  4150. return kOkDspRC;
  4151. }
  4152. cmDspRC_t _cmDspRsrcWrReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4153. {
  4154. return kOkDspRC;
  4155. }
  4156. cmDspRC_t _cmDspRsrcWrRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4157. {
  4158. cmDspRsrcWr_t* p = (cmDspRsrcWr_t*)inst;
  4159. cmDspRC_t rc = kOkDspRC;
  4160. // if a msg of any type is recieved on an input port - send out the associated symbol
  4161. if( kBaseInPtsId <= evt->dstVarId && evt->dstVarId < kBaseInPtsId + p->pathCnt )
  4162. {
  4163. unsigned idx = evt->dstVarId - kBaseInPtsId;
  4164. assert( idx < p->pathCnt );
  4165. if( cmDsvIsStrz(evt->valuePtr) )
  4166. {
  4167. rc = cmDspRsrcWriteString( ctx->dspH, cmDsvStrz(evt->valuePtr), p->pathArray[idx], NULL );
  4168. }
  4169. }
  4170. return rc;
  4171. }
  4172. cmDspClass_t* cmRsrcWrClassCons( cmDspCtx_t* ctx )
  4173. {
  4174. cmDspClassSetup(&_cmRsrcWrDC,ctx,"RsrcWr",
  4175. NULL,
  4176. _cmDspRsrcWrAlloc,
  4177. _cmDspRsrcWrFree,
  4178. _cmDspRsrcWrReset,
  4179. NULL,
  4180. _cmDspRsrcWrRecv,
  4181. NULL,
  4182. NULL,
  4183. "Set the value of a resource variable.");
  4184. return &_cmRsrcWrDC;
  4185. }
  4186. //------------------------------------------------------------------------------------------------------------
  4187. //)
  4188. //( { label:cmDspBinEnc file_desc:"HRTF binaural encoder." kw:[sunit] }
  4189. enum
  4190. {
  4191. kModeBeId,
  4192. kAzimBeId,
  4193. kElevBeId,
  4194. kDistBeId,
  4195. kAudioInBeId,
  4196. kAudioOut0BeId,
  4197. kAudioOut1BeId
  4198. };
  4199. typedef struct
  4200. {
  4201. cmDspInst_t inst;
  4202. cmBinEnc* bep;
  4203. } cmDspBinEnc_t;
  4204. cmDspClass_t _cmBeDC;
  4205. cmDspInst_t* _cmDspBinEncAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4206. {
  4207. cmDspVarArg_t args[] =
  4208. {
  4209. { "mode", kModeBeId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Mode" },
  4210. { "azim", kAzimBeId, 0, 0, kInDsvFl | kDoubleDsvFl, "Azimuth" },
  4211. { "elev", kElevBeId, 0, 0, kInDsvFl | kDoubleDsvFl, "Elevation" },
  4212. { "dist", kDistBeId, 0, 0, kInDsvFl | kDoubleDsvFl, "Distance" },
  4213. { "in", kAudioInBeId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio Input" },
  4214. { "out0", kAudioOut0BeId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio Output 0" },
  4215. { "out1", kAudioOut1BeId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio Output 1" },
  4216. { NULL, 0, 0, 0, 0 }
  4217. };
  4218. cmDspBinEnc_t* p = cmDspInstAlloc(cmDspBinEnc_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  4219. cmDspSetDefaultUInt( ctx,&p->inst, kModeBeId, 0, 0.0 );
  4220. cmDspSetDefaultDouble( ctx,&p->inst, kAzimBeId, 0, 0.0 );
  4221. cmDspSetDefaultDouble( ctx,&p->inst, kElevBeId, 0, 0.0 );
  4222. cmDspSetDefaultDouble( ctx,&p->inst, kDistBeId, 0, 0.0 );
  4223. return &p->inst;
  4224. }
  4225. cmDspRC_t _cmDspBinEncFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4226. {
  4227. cmDspRC_t rc = kOkDspRC;
  4228. cmDspBinEnc_t* p = (cmDspBinEnc_t*)inst;
  4229. cmBinEncFree(&p->bep);
  4230. return rc;
  4231. }
  4232. cmDspRC_t _cmDspBinEncSetup(cmDspCtx_t* ctx, cmDspBinEnc_t* p )
  4233. {
  4234. cmDspRC_t rc = kOkDspRC;
  4235. cmBinEncFree(&p->bep);
  4236. p->bep = cmBinEncAlloc(ctx->cmProcCtx,NULL,cmDspSampleRate(ctx), cmDspSamplesPerCycle(ctx));
  4237. return rc;
  4238. }
  4239. cmDspRC_t _cmDspBinEncReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4240. {
  4241. cmDspBinEnc_t* p = (cmDspBinEnc_t*)inst;
  4242. cmDspRC_t rc;
  4243. if((rc = cmDspApplyAllDefaults(ctx,inst)) != kOkDspRC )
  4244. return rc;
  4245. return _cmDspBinEncSetup(ctx,p);
  4246. }
  4247. cmDspRC_t _cmDspBinEncExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4248. {
  4249. cmDspBinEnc_t* p = (cmDspBinEnc_t*)inst;
  4250. cmDspRC_t rc = kOkDspRC;
  4251. unsigned iChIdx = 0;
  4252. const cmSample_t* ip = cmDspAudioBuf(ctx,inst,kAudioInBeId,iChIdx);
  4253. unsigned iSmpCnt = cmDspVarRows(inst,kAudioInBeId);
  4254. // if no connected
  4255. if( iSmpCnt == 0 )
  4256. return rc;
  4257. unsigned oChIdx = 0;
  4258. cmSample_t* o0p = cmDspAudioBuf(ctx,inst,kAudioOut0BeId,oChIdx);
  4259. unsigned oSmp0Cnt = cmDspVarRows(inst,kAudioOut0BeId);
  4260. cmSample_t* o1p = cmDspAudioBuf(ctx,inst,kAudioOut1BeId,oChIdx);
  4261. unsigned oSmp1Cnt = cmDspVarRows(inst,kAudioOut0BeId);
  4262. assert( iSmpCnt==oSmp0Cnt && iSmpCnt==oSmp1Cnt );
  4263. cmBinEncExec( p->bep, ip, o0p, o1p, iSmpCnt );
  4264. return rc;
  4265. }
  4266. cmDspRC_t _cmDspBinEncRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4267. {
  4268. cmDspBinEnc_t* p = (cmDspBinEnc_t*)inst;
  4269. cmDspRC_t rc = kOkDspRC;
  4270. cmDspSetEvent(ctx,inst,evt);
  4271. switch( evt->dstVarId )
  4272. {
  4273. case kModeBeId:
  4274. cmBinEncSetMode(p->bep, cmDspUInt(inst,kModeBeId));
  4275. break;
  4276. case kAzimBeId:
  4277. case kElevBeId:
  4278. case kDistBeId:
  4279. {
  4280. float azim = cmDspDouble(inst,kAzimBeId);
  4281. float elev = cmDspDouble(inst,kElevBeId);
  4282. float dist = cmDspDouble(inst,kDistBeId);
  4283. cmBinEncSetLoc(p->bep, azim, elev, dist );
  4284. }
  4285. break;
  4286. default:
  4287. { assert(0); }
  4288. }
  4289. return rc;
  4290. }
  4291. cmDspClass_t* cmBinEncClassCons( cmDspCtx_t* ctx )
  4292. {
  4293. cmDspClassSetup(&_cmBeDC,ctx,"BinauralEnc",
  4294. NULL,
  4295. _cmDspBinEncAlloc,
  4296. _cmDspBinEncFree,
  4297. _cmDspBinEncReset,
  4298. _cmDspBinEncExec,
  4299. _cmDspBinEncRecv,
  4300. NULL,NULL,
  4301. "Binaural filter.");
  4302. return &_cmBeDC;
  4303. }
  4304. //------------------------------------------------------------------------------------------------------------
  4305. //)
  4306. //( { label:cmDsp2d file_desc:"Two dimension graphic user interface controller." kw:[sunit] }
  4307. enum
  4308. {
  4309. kX2dId,
  4310. kY2dId,
  4311. kRadius2dId,
  4312. kAngle2dId
  4313. };
  4314. cmDspClass_t _cm2dDC;
  4315. typedef struct
  4316. {
  4317. cmDspInst_t inst;
  4318. } cmDsp2d_t;
  4319. cmDspInst_t* _cmDsp2dAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4320. {
  4321. cmDspVarArg_t args[] =
  4322. {
  4323. { "x", kX2dId, 0, 0, kOutDsvFl | kDoubleDsvFl, "X coordinate" },
  4324. { "y", kY2dId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Y coordinate"},
  4325. { "radius",kRadius2dId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Radius"},
  4326. { "angle", kAngle2dId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Angle"},
  4327. { NULL, 0, 0, 0, 0 }
  4328. };
  4329. cmDsp2d_t* p = cmDspInstAlloc(cmDsp2d_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  4330. cmDspSetDefaultDouble(ctx, &p->inst, kX2dId, 0.0, 0.0);
  4331. cmDspSetDefaultDouble(ctx, &p->inst, kY2dId, 0.0, 0.0);
  4332. cmDspSetDefaultDouble(ctx, &p->inst, kRadius2dId, 0.0, 0.0);
  4333. cmDspSetDefaultDouble(ctx, &p->inst, kAngle2dId, 0.0, 0.0);
  4334. // create the UI control
  4335. cmDspUi2dCreate(ctx,&p->inst,kX2dId,kY2dId,kRadius2dId,kAngle2dId);
  4336. return &p->inst;
  4337. }
  4338. cmDspRC_t _cmDsp2dReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4339. {
  4340. cmDspApplyAllDefaults(ctx,inst);
  4341. return kOkDspRC;
  4342. }
  4343. cmDspRC_t _cmDsp2dRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4344. {
  4345. cmDspSetEvent(ctx,inst,evt);
  4346. return kOkDspRC;
  4347. }
  4348. cmDspClass_t* cm2dClassCons( cmDspCtx_t* ctx )
  4349. {
  4350. cmDspClassSetup(&_cm2dDC,ctx,"twod",
  4351. NULL,
  4352. _cmDsp2dAlloc,
  4353. NULL,
  4354. _cmDsp2dReset,
  4355. NULL,
  4356. _cmDsp2dRecv,
  4357. NULL,
  4358. NULL,
  4359. "2d value control.");
  4360. return &_cm2dDC;
  4361. }
  4362. //)
  4363. //==========================================================================================================================================
  4364. //(
  4365. cmDspClassConsFunc_t _cmDspClassBuiltInArray[] =
  4366. {
  4367. cmPrinterClassCons,
  4368. cmCounterClassCons,
  4369. cmPhasorClassCons,
  4370. cmMidiOutClassCons,
  4371. cmMidiInClassCons,
  4372. cmAudioInClassCons,
  4373. cmAudioOutClassCons,
  4374. cmAudioFileOutClassCons,
  4375. cmSigGenClassCons,
  4376. cmScalarClassCons,
  4377. cmTextClassCons,
  4378. cmMeterClassCons,
  4379. cmLabelClassCons,
  4380. cmButtonClassCons,
  4381. cmCheckboxClassCons,
  4382. cmReorderClassCons,
  4383. cmFnameClassCons,
  4384. cmMsgListClassCons,
  4385. cmWaveTableClassCons,
  4386. cmSprintfClassCons,
  4387. cmAMixClassCons,
  4388. cmASplitClassCons,
  4389. cmAMeterClassCons,
  4390. cmTextFileClassCons,
  4391. cmBinMtxFileClassCons,
  4392. cmArrayClassCons,
  4393. cmPitchCvtClassCons,
  4394. cmShiftBufClassCons,
  4395. cmNetSendClassCons,
  4396. cmRsrcWrClassCons,
  4397. cmBinEncClassCons,
  4398. cm2dClassCons,
  4399. cmDelayClassCons,
  4400. cmPShiftClassCons,
  4401. cmLoopRecdClassCons,
  4402. cmRectifyClassCons,
  4403. cmGateDetectClassCons,
  4404. cmAutoGainClassCons,
  4405. cmEnvFollowClassCons,
  4406. cmXfaderClassCons,
  4407. cmChCfgClassCons,
  4408. cmChordDetectClassCons,
  4409. cmFaderClassCons,
  4410. cmNoteSelectClassCons,
  4411. cmNetNoteSelectClassCons,
  4412. cmCombFiltClassCons,
  4413. cmScalarOpClassCons,
  4414. cmGroupSelClassCons,
  4415. cmAudioNofMClassCons,
  4416. cmRingModClassCons,
  4417. cmMsgDelayClassCons,
  4418. cmLineClassCons,
  4419. cmAdsrClassCons,
  4420. cmCompressorClassCons,
  4421. cmBiQuadEqClassCons,
  4422. cmDistDsClassCons,
  4423. cmDbToLinClassCons,
  4424. cmMtDelayClassCons,
  4425. cmNofMClassCons,
  4426. cm1ofNClassCons,
  4427. cm1UpClassCons,
  4428. cmGateToSymClassCons,
  4429. cmPortToSymClassCons,
  4430. cmRouterClassCons,
  4431. cmAvailChClassCons,
  4432. cmPresetClassCons,
  4433. cmBcastSymClassCons,
  4434. cmSegLineClassCons,
  4435. cmKrClassCons,
  4436. cmKr2ClassCons,
  4437. cmTimeLineClassCons,
  4438. cmScoreClassCons,
  4439. cmMidiFilePlayClassCons,
  4440. cmScFolClassCons,
  4441. cmScModClassCons,
  4442. cmGSwitchClassCons,
  4443. cmScaleRangeClassCons,
  4444. cmActiveMeasClassCons,
  4445. cmAmSyncClassCons,
  4446. cmNanoMapClassCons,
  4447. cmRecdPlayClassCons,
  4448. cmGoertzelClassCons,
  4449. cmSyncRecdClassCons,
  4450. cmTakeSeqBldrClassCons,
  4451. cmTakeSeqRendClassCons,
  4452. cmReflectCalcClassCons,
  4453. cmEchoCancelClassCons,
  4454. NULL,
  4455. };
  4456. cmDspClassConsFunc_t cmDspClassGetBuiltIn( unsigned index )
  4457. {
  4458. unsigned n = sizeof(_cmDspClassBuiltInArray)/sizeof(cmDspClass_t*);
  4459. if( index >= n )
  4460. return NULL;
  4461. return _cmDspClassBuiltInArray[index];
  4462. }
  4463. //)