libcm is a C development framework with an emphasis on audio signal processing applications.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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