libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmDspBuiltIn.c 160KB

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