libcm is a C development framework with an emphasis on audio signal processing applications.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

cmDspBuiltIn.c 152KB

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