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

cmDspBuiltIn.c 169KB

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