libcm is a C development framework with an emphasis on audio signal processing applications.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

cmDspBuiltIn.c 160KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270
  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 "cmThread.h" // used for threaded loading in wave table file mode
  33. #include "cmProcObj.h"
  34. #include "cmProcTemplateMain.h"
  35. #include "cmProc.h"
  36. #include "cmMidi.h"
  37. #include "cmProc2.h"
  38. #include "cmVectOpsTemplateMain.h"
  39. #include "cmMidiPort.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. cmDspSetDefaultSample(ctx, &p->inst, kMaxPhId, 0.0, cmSample_MAX);
  581. cmDspSetDefaultSample(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. cmSample_t mult = cmDspSample(inst,kMultPhId);
  596. cmSample_t max = cmDspSample(inst,kMaxPhId);
  597. double phs = cmDspDouble(inst,kPhsPhId);
  598. cmSample_t inc = mult;
  599. for(; bp<ep; ++bp)
  600. {
  601. while( phs >= max )
  602. phs -= max;
  603. *bp = (cmSample_t)phs;
  604. phs += inc;
  605. }
  606. cmDspSetSample(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. kDeviceMiId,
  785. kPortMiId,
  786. kSmpIdxMiId,
  787. kStatusMiId,
  788. kD0MiId,
  789. kD1MiId
  790. };
  791. cmDspClass_t _cmMidiInDC;
  792. typedef struct
  793. {
  794. cmDspInst_t inst;
  795. unsigned midiSymId;
  796. unsigned prevSmpIdx;
  797. } cmDspMidiIn_t;
  798. cmDspInst_t* _cmDspMidiInAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  799. {
  800. cmDspVarArg_t args[] =
  801. {
  802. { "device", kDeviceMiId, 0, 0, kOutDsvFl | kUIntDsvFl, "MIDI device" },
  803. { "port", kPortMiId, 0, 0, kOutDsvFl | kUIntDsvFl, "MIDI device port"},
  804. { "smpidx", kSmpIdxMiId, 0, 0, kOutDsvFl | kUIntDsvFl, "Message time tag as sample index."},
  805. { "status", kStatusMiId, 0, 0, kOutDsvFl | kUIntDsvFl, "MIDI status" },
  806. { "d0", kD0MiId, 0, 0, kOutDsvFl | kUIntDsvFl, "MIDI channel message d0" },
  807. { "d1", kD1MiId, 0, 0, kOutDsvFl | kUIntDsvFl, "MIDI channel message d1" },
  808. { NULL, 0, 0, 0, 0 }
  809. };
  810. cmDspMidiIn_t* p = cmDspInstAlloc(cmDspMidiIn_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  811. p->midiSymId = cmDspSysAssignInstAttrSymbolStr( ctx->dspH, &p->inst, "_midi" );
  812. return &p->inst;
  813. }
  814. cmDspRC_t _cmDspMidiInReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  815. {
  816. cmDspRC_t rc = kOkDspRC;
  817. cmDspMidiIn_t* p = (cmDspMidiIn_t*)inst;
  818. cmDspApplyAllDefaults(ctx,inst);
  819. p->prevSmpIdx = 0;
  820. return rc;
  821. }
  822. cmDspRC_t _cmDspMidiInRecvFunc( cmDspCtx_t* ctx, struct cmDspInst_str* inst, unsigned attrSymId, const cmDspValue_t* value )
  823. {
  824. cmDspMidiIn_t* p = (cmDspMidiIn_t*)inst;
  825. if( attrSymId == p->midiSymId )
  826. {
  827. cmMidiPacket_t* pkt = (cmMidiPacket_t*)(value->u.m.u.vp);
  828. unsigned i;
  829. cmDspSetUInt(ctx, inst, kDeviceMiId, pkt->devIdx);
  830. cmDspSetUInt(ctx, inst, kPortMiId, pkt->portIdx);
  831. for(i=0; i<pkt->msgCnt; ++i)
  832. {
  833. cmMidiMsg* m = pkt->msgArray + i;
  834. unsigned deltaSmpCnt = floor((m->deltaUs * cmDspSampleRate(ctx)) / 1000000.0);
  835. if( p->prevSmpIdx == 0 )
  836. p->prevSmpIdx = ctx->cycleCnt * cmDspSamplesPerCycle(ctx);
  837. else
  838. p->prevSmpIdx += deltaSmpCnt;
  839. cmDspSetUInt(ctx, inst, kSmpIdxMiId, p->prevSmpIdx );
  840. cmDspSetUInt(ctx, inst, kD1MiId, m->d1 );
  841. cmDspSetUInt(ctx, inst, kD0MiId, m->d0 );
  842. cmDspSetUInt(ctx, inst, kStatusMiId, m->status );
  843. }
  844. }
  845. return kOkDspRC;
  846. }
  847. struct cmDspClass_str* cmMidiInClassCons( cmDspCtx_t* ctx )
  848. {
  849. cmDspClassSetup(&_cmMidiInDC,ctx,"MidiIn",
  850. NULL,
  851. _cmDspMidiInAlloc,
  852. NULL,
  853. _cmDspMidiInReset,
  854. NULL,
  855. NULL,
  856. NULL,
  857. _cmDspMidiInRecvFunc,
  858. "Midi input port");
  859. return &_cmMidiInDC;
  860. }
  861. //==========================================================================================================================================
  862. enum
  863. {
  864. kDeviceMoId,
  865. kPortMoId,
  866. kStatusMoId,
  867. kD0MoId,
  868. kD1MoId
  869. };
  870. cmDspClass_t _cmMidiOutDC;
  871. typedef struct
  872. {
  873. cmDspInst_t inst;
  874. unsigned devIdx;
  875. unsigned portIdx;
  876. } cmDspMidiOut_t;
  877. cmDspRC_t _cmDspMidiOutSetDevice( cmDspCtx_t* ctx, cmDspMidiOut_t* p, const cmChar_t* deviceStr )
  878. {
  879. cmDspRC_t rc = kOkDspRC;
  880. if( deviceStr != NULL )
  881. if((p->devIdx = cmMpDeviceNameToIndex(deviceStr)) == cmInvalidIdx )
  882. rc = cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The MIDI device '%s' could not be found.",cmStringNullGuard(deviceStr));
  883. return rc;
  884. }
  885. cmDspRC_t _cmDspMidiOutSetPort( cmDspCtx_t* ctx, cmDspMidiOut_t* p, const cmChar_t* portStr )
  886. {
  887. cmDspRC_t rc = kOkDspRC;
  888. if( portStr == NULL )
  889. return rc;
  890. if( p->devIdx == cmInvalidIdx )
  891. rc = cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The MIDI port cannot be set until the MIDI device is set.");
  892. else
  893. {
  894. if((p->portIdx = cmMpDevicePortNameToIndex(p->devIdx,kOutMpFl,portStr)) == cmInvalidIdx )
  895. rc = cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The MIDI port '%s' could not be found on device '%s'.",cmStringNullGuard(portStr),cmStringNullGuard(cmMpDeviceName(p->devIdx)));
  896. }
  897. return rc;
  898. }
  899. cmDspInst_t* _cmDspMidiOutAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  900. {
  901. cmDspVarArg_t args[] =
  902. {
  903. { "device", kDeviceMoId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "MIDI device name"},
  904. { "port", kPortMoId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "MIDI port name"},
  905. { "status", kStatusMoId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI status" },
  906. { "d0", kD0MoId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI channel message d0" },
  907. { "d1", kD1MoId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI channel message d1" },
  908. { NULL, 0, 0, 0, 0 }
  909. };
  910. cmDspMidiOut_t* p = cmDspInstAlloc(cmDspMidiOut_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  911. p->devIdx = cmInvalidIdx;
  912. p->portIdx = cmInvalidIdx;
  913. cmDspSetDefaultUInt(ctx,&p->inst, kStatusMoId, 0, 0 );
  914. cmDspSetDefaultUInt(ctx,&p->inst, kD0MoId, 0, 0 );
  915. cmDspSetDefaultUInt(ctx,&p->inst, kD1MoId, 0, 0 );
  916. return &p->inst;
  917. }
  918. cmDspRC_t _cmDspMidiOutReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  919. {
  920. cmDspRC_t rc = kOkDspRC;
  921. cmDspMidiOut_t* p = (cmDspMidiOut_t*)inst;
  922. cmDspApplyAllDefaults(ctx,inst);
  923. _cmDspMidiOutSetDevice(ctx,p,cmDspStrcz(inst,kDeviceMoId));
  924. _cmDspMidiOutSetPort( ctx,p,cmDspStrcz(inst,kPortMoId));
  925. return rc;
  926. }
  927. cmDspRC_t _cmDspMidiOutRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  928. {
  929. cmDspMidiOut_t* p = (cmDspMidiOut_t*)inst;
  930. switch( evt->dstVarId )
  931. {
  932. case kDeviceMoId:
  933. _cmDspMidiOutSetDevice(ctx, p, cmDsvStrcz(evt->valuePtr) );
  934. break;
  935. case kPortMoId:
  936. _cmDspMidiOutSetPort(ctx, p, cmDsvStrcz(evt->valuePtr) );
  937. break;
  938. case kStatusMoId:
  939. if( p->devIdx != cmInvalidIdx && p->portIdx != cmInvalidIdx )
  940. {
  941. unsigned status = cmDsvGetUInt(evt->valuePtr);
  942. unsigned d0 = cmDspUInt(inst,kD0MoId);
  943. unsigned d1 = cmDspUInt(inst,kD1MoId);
  944. if( cmMpDeviceSend( p->devIdx, p->portIdx, status, d0, d1 ) != kOkMpRC )
  945. cmDspInstErr(ctx,inst,kInvalidArgDspRC,"MIDI send failed.");
  946. }
  947. break;
  948. default:
  949. cmDspSetEvent(ctx,inst,evt);
  950. break;
  951. }
  952. return kOkDspRC;
  953. }
  954. struct cmDspClass_str* cmMidiOutClassCons( cmDspCtx_t* ctx )
  955. {
  956. cmDspClassSetup(&_cmMidiOutDC,ctx,"MidiOut",
  957. NULL,
  958. _cmDspMidiOutAlloc,
  959. NULL,
  960. _cmDspMidiOutReset,
  961. NULL,
  962. _cmDspMidiOutRecv,
  963. NULL,
  964. NULL,
  965. "Midi input port");
  966. return &_cmMidiOutDC;
  967. }
  968. //==========================================================================================================================================
  969. enum
  970. {
  971. kChAiId,
  972. kGainAiId,
  973. kOutAiId
  974. };
  975. cmDspClass_t _cmAudioInDC;
  976. typedef struct
  977. {
  978. cmDspInst_t inst;
  979. bool errFl; // used to control error reporting
  980. } cmDspAudioIn_t;
  981. cmDspInst_t* _cmDspAudioInAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  982. {
  983. cmDspVarArg_t args[] =
  984. {
  985. { "ch", kChAiId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Audio input channel index"},
  986. { "gain", kGainAiId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Input gain multiplier" },
  987. { "out", kOutAiId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output" },
  988. { NULL, 0, 0, 0, 0 }
  989. };
  990. cmDspAudioIn_t* p = cmDspInstAlloc(cmDspAudioIn_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  991. cmDspSetDefaultUInt( ctx, &p->inst, kChAiId, 0, 0);
  992. cmDspSetDefaultDouble( ctx, &p->inst, kGainAiId, 0, 1.0);
  993. return &p->inst;
  994. }
  995. cmDspRC_t _cmDspAudioInReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  996. {
  997. cmDspRC_t rc = kOkDspRC;
  998. cmDspAudioIn_t* p = (cmDspAudioIn_t*)inst;
  999. p->errFl = false;
  1000. cmDspApplyAllDefaults(ctx,inst);
  1001. return rc;
  1002. }
  1003. cmDspRC_t _cmDspAudioInExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1004. {
  1005. unsigned chIdx = cmDspUInt(inst,kChAiId);
  1006. unsigned iChCnt = ctx->ctx->iChCnt;
  1007. cmDspAudioIn_t* p = (cmDspAudioIn_t*)inst;
  1008. double gain = cmDspDouble(inst,kGainAiId);
  1009. if( chIdx >= iChCnt )
  1010. {
  1011. if( p->errFl== false )
  1012. {
  1013. cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The input channel index %i is invalid. Channel count:%i.",chIdx,iChCnt);
  1014. p->errFl = true;
  1015. }
  1016. cmDspZeroAudioBuf(ctx,inst,kOutAiId);
  1017. return kOkDspRC;
  1018. }
  1019. //unsigned n = cmDspSamplesPerCycle(ctx);
  1020. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutAiId,0);
  1021. cmSample_t* dp = cmDspAudioBuf(ctx,inst,kOutAiId,0);
  1022. assert( n == cmDspAudioBufSmpCount(ctx,inst,kOutAiId,chIdx));
  1023. assert(dp != NULL);
  1024. // if this channel is disabled then iChArray[chIdx] will be NULL
  1025. if( ctx->ctx->iChArray[chIdx]!=NULL )
  1026. cmVOS_MultVVS(dp,n,ctx->ctx->iChArray[chIdx],(cmSample_t)gain);
  1027. return kOkDspRC;
  1028. }
  1029. cmDspRC_t _cmDspAudioInRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1030. {
  1031. cmDspRC_t rc = kOkDspRC;
  1032. switch( evt->dstVarId )
  1033. {
  1034. case kChAiId:
  1035. if( cmDspSetEvent(ctx,inst,evt) != kOkDspRC )
  1036. {
  1037. // if the exec callback was previously disabled and the new channel value is valid then re-enable the exec callback.
  1038. if( inst->execFunc==NULL && cmDspUInt(inst,kChAiId) < ctx->ctx->iChCnt )
  1039. inst->execFunc = _cmDspAudioInExec;
  1040. }
  1041. break;
  1042. case kGainAiId:
  1043. cmDspSetEvent(ctx,inst,evt);
  1044. break;
  1045. }
  1046. return rc;
  1047. }
  1048. struct cmDspClass_str* cmAudioInClassCons( cmDspCtx_t* ctx )
  1049. {
  1050. cmDspClassSetup(&_cmAudioInDC,ctx,"AudioIn",
  1051. NULL,
  1052. _cmDspAudioInAlloc,
  1053. NULL,
  1054. _cmDspAudioInReset,
  1055. _cmDspAudioInExec,
  1056. _cmDspAudioInRecv,
  1057. NULL,
  1058. NULL,
  1059. "Audio output port");
  1060. return &_cmAudioInDC;
  1061. }
  1062. //==========================================================================================================================================
  1063. enum
  1064. {
  1065. kChAoId,
  1066. kGainAoId,
  1067. kInAoId
  1068. };
  1069. cmDspClass_t _cmAudioOutDC;
  1070. typedef struct
  1071. {
  1072. cmDspInst_t inst;
  1073. } cmDspAudioOut_t;
  1074. cmDspInst_t* _cmDspAudioOutAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1075. {
  1076. cmDspVarArg_t args[] =
  1077. {
  1078. { "ch", kChAoId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Audio output channel index"},
  1079. { "gain",kGainAoId,0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Output gain multiplier"},
  1080. { "in", kInAoId, 0, 1, kInDsvFl | kAudioBufDsvFl, "Audio input" },
  1081. { NULL, 0, 0, 0, 0 }
  1082. };
  1083. cmDspAudioOut_t* p = cmDspInstAlloc(cmDspAudioOut_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1084. cmDspSetDefaultUInt( ctx, &p->inst, kChAoId, 0, 0);
  1085. cmDspSetDefaultDouble( ctx, &p->inst, kGainAoId, 0, 1.0);
  1086. return &p->inst;
  1087. }
  1088. cmDspRC_t _cmDspAudioOutReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1089. {
  1090. cmDspRC_t rc = kOkDspRC;
  1091. cmDspApplyAllDefaults(ctx,inst);
  1092. return rc;
  1093. }
  1094. cmDspRC_t _cmDspAudioOutExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1095. {
  1096. cmDspRC_t rc = kOkDspRC;
  1097. unsigned chIdx = cmDspUInt(inst,kChAoId);
  1098. unsigned oChCnt = ctx->ctx->oChCnt;
  1099. double gain = cmDspDouble(inst,kGainAoId);
  1100. if( chIdx >= oChCnt )
  1101. {
  1102. rc = cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The output channel index %i is invalid. Channel count:%i.",chIdx,oChCnt);
  1103. inst->execFunc = NULL; // disable callbacks - this prevents the error msg from repeating
  1104. return rc;
  1105. }
  1106. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kInAoId,0);
  1107. if( sp == NULL )
  1108. {
  1109. inst->execFunc = NULL; // if there is no connected input then disable further callbacks
  1110. return kOkDspRC;
  1111. }
  1112. unsigned n = cmDspSamplesPerCycle(ctx);
  1113. assert( n == cmDspVarRows(inst,kInAoId) );
  1114. // if this channel is disabled or set to pass-through then chArray[chIdx] will be NULL
  1115. if( ctx->ctx->oChArray[chIdx] != NULL )
  1116. cmVOS_MultVVS(ctx->ctx->oChArray[chIdx],n,sp,(cmSample_t)gain);
  1117. return kOkDspRC;
  1118. }
  1119. cmDspRC_t _cmDspAudioOutRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1120. {
  1121. cmDspRC_t rc = kOkDspRC;
  1122. switch( evt->dstVarId )
  1123. {
  1124. case kChAoId:
  1125. if( cmDspSetEvent(ctx,inst,evt) != kOkDspRC )
  1126. {
  1127. // if the exec callback was previously disabled and the new channel value is valid then re-enable the exec callback.
  1128. if( inst->execFunc==NULL && cmDspUInt(inst,kChAoId) < ctx->ctx->oChCnt )
  1129. inst->execFunc = _cmDspAudioOutExec;
  1130. }
  1131. break;
  1132. case kGainAoId:
  1133. cmDspSetEvent(ctx,inst,evt);
  1134. break;
  1135. }
  1136. return rc;
  1137. }
  1138. struct cmDspClass_str* cmAudioOutClassCons( cmDspCtx_t* ctx )
  1139. {
  1140. cmDspClassSetup(&_cmAudioOutDC,ctx,"AudioOut",
  1141. NULL,
  1142. _cmDspAudioOutAlloc,
  1143. NULL,
  1144. _cmDspAudioOutReset,
  1145. _cmDspAudioOutExec,
  1146. _cmDspAudioOutRecv,
  1147. NULL,NULL,
  1148. "Audio output port");
  1149. return &_cmAudioOutDC;
  1150. }
  1151. //==========================================================================================================================================
  1152. enum
  1153. {
  1154. kFnAofId,
  1155. kChCntAofId,
  1156. kGain0AofId,
  1157. kGain1AofId,
  1158. kIn0AofId,
  1159. kIn1AofId,
  1160. kSelAofId
  1161. };
  1162. cmDspClass_t _cmAudioFileOutDC;
  1163. typedef struct
  1164. {
  1165. cmDspInst_t inst;
  1166. cmSample_t* smpBuf;
  1167. unsigned smpCnt;
  1168. unsigned bits;
  1169. cmAudioFileH_t afH;
  1170. unsigned openSymId;
  1171. unsigned closeSymId;
  1172. } cmDspAudioFileOut_t;
  1173. cmDspRC_t _cmDspAudioFileOutCreateFile( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned chCnt )
  1174. {
  1175. cmDspRC_t rc = kOkDspRC;
  1176. cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;
  1177. const cmChar_t* fn = cmDspStrcz(inst,kFnAofId);
  1178. if(cmAudioFileIsValid(p->afH) )
  1179. cmAudioFileDelete(&p->afH);
  1180. if( cmAudioFileIsValid(p->afH = cmAudioFileNewCreate(fn, cmDspSampleRate(ctx), p->bits, chCnt, &rc, ctx->rpt )) == false )
  1181. rc = cmDspClassErr(ctx,inst->classPtr,kVarArgParseFailDspRC,"The output audio file '%s' create failed.",fn);
  1182. return rc;
  1183. }
  1184. cmDspInst_t* _cmDspAudioFileOutAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1185. {
  1186. cmDspVarArg_t args[] =
  1187. {
  1188. { "fn", kFnAofId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Audio file name"},
  1189. { "chs", kChCntAofId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Channel count"},
  1190. { "gain0", kGain0AofId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Output gain 0 multiplier"},
  1191. { "gain1", kGain1AofId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Output gain 1 multiplier"},
  1192. { "in0", kIn0AofId, 0, 1, kInDsvFl | kAudioBufDsvFl, "Audio input 0"},
  1193. { "in1", kIn1AofId, 0, 1, kInDsvFl | kAudioBufDsvFl, "Audio input 1"},
  1194. { "sel", kSelAofId, 0, 0, kInDsvFl, "Open | Close"},
  1195. { NULL, 0, 0, 0, 0 }
  1196. };
  1197. cmDspAudioFileOut_t* p = cmDspInstAlloc(cmDspAudioFileOut_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1198. cmDspValue_t chCntVal;
  1199. cmDsvSetUInt(&chCntVal,cmMin(2,cmDspUInt(&p->inst,kChCntAofId)));
  1200. cmDspSetDefault(ctx, &p->inst, kChCntAofId, &chCntVal );
  1201. cmDspSetDefaultDouble( ctx, &p->inst, kGain0AofId, 0, 1.0);
  1202. cmDspSetDefaultDouble( ctx, &p->inst, kGain1AofId, 0, 1.0);
  1203. p->bits = 16;
  1204. p->afH = cmNullAudioFileH;
  1205. p->openSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"open");
  1206. p->closeSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"close");
  1207. return &p->inst;
  1208. }
  1209. cmDspRC_t _cmDspAudioFileOutReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1210. {
  1211. cmDspRC_t rc = kOkDspRC;
  1212. cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;
  1213. unsigned chCnt = cmDspUInt(inst,kChCntAofId);
  1214. cmDspApplyAllDefaults(ctx,inst);
  1215. p->smpCnt = cmDspSamplesPerCycle(ctx) * chCnt;
  1216. p->smpBuf = cmLhResizeN(ctx->lhH, cmSample_t, p->smpBuf, p->smpCnt);
  1217. rc = _cmDspAudioFileOutCreateFile( ctx, inst, chCnt );
  1218. return rc;
  1219. }
  1220. cmDspRC_t _cmDspAudioFileOutExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1221. {
  1222. cmDspRC_t rc = kOkDspRC;
  1223. cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;;
  1224. unsigned chCnt = cmMin(2,cmDspUInt(inst,kChCntAofId));
  1225. unsigned smpCnt = 0;
  1226. cmSample_t* chArray[chCnt];
  1227. unsigned i,j;
  1228. for(i=0,j=0; i<chCnt; ++i)
  1229. {
  1230. unsigned chVarId = i == 0 ? kIn0AofId : kIn1AofId; // get audio buf var id for this ch
  1231. unsigned iSmpCnt = cmDspVarRows(inst,chVarId);
  1232. if( iSmpCnt == 0 )
  1233. {
  1234. chArray[j] = NULL;
  1235. }
  1236. else
  1237. {
  1238. cmSample_t gain = cmDspSample(inst,i==0?kGain0AofId:kGain1AofId); // get ch gain
  1239. chArray[j] = cmDspAudioBuf(ctx,inst,chVarId,i); // get incoming audio buf ptr
  1240. if( gain != 1.0 )
  1241. cmVOS_MultVVS(chArray[j], iSmpCnt, chArray[j], gain); // apply gain
  1242. ++j; // incr chArray[] index
  1243. assert( smpCnt==0 || iSmpCnt==smpCnt);
  1244. smpCnt = iSmpCnt; // set outgoing sample count
  1245. }
  1246. }
  1247. // write the samples
  1248. if( cmAudioFileWriteSample(p->afH, smpCnt, j, chArray ) != kOkAfRC )
  1249. rc = cmDspClassErr(ctx,inst->classPtr,kFileWriteFailDspRC,"An audio output file write failed.");
  1250. return rc;
  1251. }
  1252. cmDspRC_t _cmDspAudioFileOutRecv( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1253. {
  1254. cmDspRC_t rc = kOkDspRC;
  1255. switch( evt->dstVarId )
  1256. {
  1257. case kGain1AofId:
  1258. case kGain0AofId:
  1259. cmDspSetEvent(ctx,inst,evt);
  1260. break;
  1261. case kFnAofId:
  1262. cmDspSetEvent(ctx,inst,evt);
  1263. rc = _cmDspAudioFileOutCreateFile(ctx,inst,cmDspUInt(inst,kChCntAofId));
  1264. break;
  1265. case kSelAofId:
  1266. {
  1267. cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;
  1268. unsigned symId = cmDsvSymbol(evt->valuePtr);
  1269. if( symId == p->openSymId )
  1270. rc = _cmDspAudioFileOutCreateFile(ctx,inst,cmDspUInt(inst,kChCntAofId));
  1271. else
  1272. {
  1273. if( symId == p->closeSymId )
  1274. {
  1275. if(cmAudioFileIsValid(p->afH) )
  1276. cmAudioFileDelete(&p->afH);
  1277. }
  1278. else
  1279. {
  1280. rc = cmErrMsg(&inst->classPtr->err,kInvalidArgDspRC,"Unknown selector symbol (%i) %s.",symId,cmStringNullGuard(cmSymTblLabel(ctx->stH,symId)));
  1281. }
  1282. }
  1283. }
  1284. }
  1285. return rc;
  1286. }
  1287. cmDspRC_t _cmDspAudioFileOutFree( cmDspCtx_t* ctx, struct cmDspInst_str* inst, const cmDspEvt_t* evtPtr )
  1288. {
  1289. cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;
  1290. if(cmAudioFileIsValid(p->afH) )
  1291. cmAudioFileDelete(&p->afH);
  1292. return kOkDspRC;
  1293. }
  1294. struct cmDspClass_str* cmAudioFileOutClassCons( cmDspCtx_t* ctx )
  1295. {
  1296. cmDspClassSetup(&_cmAudioFileOutDC,ctx,"AudioFileOut",
  1297. NULL,
  1298. _cmDspAudioFileOutAlloc,
  1299. _cmDspAudioFileOutFree,
  1300. _cmDspAudioFileOutReset,
  1301. _cmDspAudioFileOutExec,
  1302. _cmDspAudioFileOutRecv,
  1303. NULL,NULL,
  1304. "Audio file output port");
  1305. return &_cmAudioFileOutDC;
  1306. }
  1307. //==========================================================================================================================================
  1308. enum
  1309. {
  1310. kTypScId,
  1311. kMinScId,
  1312. kMaxScId,
  1313. kStpScId,
  1314. kValScId,
  1315. kLblScId,
  1316. kSendScId
  1317. };
  1318. cmDspClass_t _cmScalarDC;
  1319. typedef struct
  1320. {
  1321. cmDspInst_t inst;
  1322. } cmDspScalar_t;
  1323. cmDspInst_t* _cmDspScalarAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1324. {
  1325. cmDspVarArg_t args[] =
  1326. {
  1327. { "typ", kTypScId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Type" },
  1328. { "min", kMinScId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Minimum value"},
  1329. { "max", kMaxScId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Maximum value"},
  1330. { "step", kStpScId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Step value (set to 0 to ignore)"},
  1331. { "val", kValScId, 0, 0, kInDsvFl | kOutDsvFl | kDoubleDsvFl | kReqArgDsvFl | kSendDfltDsvFl, "Current value"},
  1332. { "lbl", kLblScId, 0, 0, kStrzDsvFl | kOptArgDsvFl, "Label"},
  1333. { "send", kSendScId, 0, 0, kInDsvFl | kTypeDsvMask, "Send value on any msg."},
  1334. { NULL, 0, 0, 0, 0 }
  1335. };
  1336. cmDspScalar_t* p = cmDspInstAlloc(cmDspScalar_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1337. cmDspSetDefaultUInt( ctx, &p->inst, kTypScId, 0, kNumberDuiId);
  1338. cmDspSetDefaultDouble(ctx, &p->inst, kMinScId, 0.0, 0);
  1339. cmDspSetDefaultDouble(ctx, &p->inst, kMaxScId, 0.0, 1);
  1340. cmDspSetDefaultDouble(ctx, &p->inst, kStpScId, 0.0, 0);
  1341. unsigned typeId = cmDspDefaultUInt(&p->inst,kTypScId);
  1342. // create the UI control
  1343. cmDspUiScalarCreate(ctx,&p->inst,typeId,kMinScId,kMaxScId,kStpScId,kValScId,kLblScId);
  1344. return &p->inst;
  1345. }
  1346. cmDspRC_t _cmDspScalarReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1347. {
  1348. cmDspApplyAllDefaults(ctx,inst);
  1349. return kOkDspRC;
  1350. }
  1351. cmDspRC_t _cmDspScalarRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1352. {
  1353. if( evt->dstVarId == kSendScId )
  1354. {
  1355. // NOT SURE IF THIS IS CORRECT OR NOT - IT SHOULD FORCE THE CURRENT VALUE TO BE
  1356. // SENT - THE CURRENT VALUE SHOULD ALREADY BE AT THE UI SO THERE DOESN'T SEEM
  1357. // TO BE ANY REASON TO UPDATE IT THERE.
  1358. cmDspSetDouble( ctx, inst, kValScId, cmDspDouble(inst,kValScId ));
  1359. return kOkDspRC;
  1360. }
  1361. switch( evt->dstVarId )
  1362. {
  1363. case kMinScId:
  1364. case kMaxScId:
  1365. case kStpScId:
  1366. case kValScId:
  1367. cmDspSetEvent(ctx,inst,evt);
  1368. break;
  1369. default:
  1370. {assert(0);}
  1371. }
  1372. return kOkDspRC;
  1373. }
  1374. cmDspRC_t _cmDspScalarPresetRdWr( cmDspCtx_t* ctx, cmDspInst_t* inst, bool storeFl )
  1375. {
  1376. return cmDspVarPresetRdWr(ctx,inst,kValScId,storeFl);
  1377. }
  1378. struct cmDspClass_str* cmScalarClassCons( cmDspCtx_t* ctx )
  1379. {
  1380. cmDspClassSetup(&_cmScalarDC,ctx,"Scalar",
  1381. NULL,
  1382. _cmDspScalarAlloc,
  1383. NULL,
  1384. _cmDspScalarReset,
  1385. NULL,
  1386. _cmDspScalarRecv,
  1387. _cmDspScalarPresetRdWr,
  1388. NULL,
  1389. "Scalar value control.");
  1390. return &_cmScalarDC;
  1391. }
  1392. //==========================================================================================================================================
  1393. enum
  1394. {
  1395. kValTxId,
  1396. kLblTxId
  1397. };
  1398. cmDspClass_t _cmTextDC;
  1399. typedef struct
  1400. {
  1401. cmDspInst_t inst;
  1402. } cmDspText_t;
  1403. cmDspInst_t* _cmDspTextAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1404. {
  1405. cmDspVarArg_t args[] =
  1406. {
  1407. { "val", kValTxId, 0, 0, kInDsvFl | kOutDsvFl | kStrzDsvFl | kReqArgDsvFl | kSendDfltDsvFl, "Current string"},
  1408. { "lbl", kLblTxId, 0, 0, kStrzDsvFl | kOptArgDsvFl, "Label"},
  1409. { NULL, 0, 0, 0, 0 }
  1410. };
  1411. cmDspText_t* p = cmDspInstAlloc(cmDspText_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1412. // create the UI control
  1413. cmDspUiTextCreate(ctx,&p->inst,kValTxId,kLblTxId);
  1414. return &p->inst;
  1415. }
  1416. cmDspRC_t _cmDspTextReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1417. {
  1418. cmDspApplyAllDefaults(ctx,inst);
  1419. return kOkDspRC;
  1420. }
  1421. cmDspRC_t _cmDspTextRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1422. {
  1423. switch( evt->dstVarId )
  1424. {
  1425. case kValTxId:
  1426. cmDspSetEvent(ctx,inst,evt);
  1427. break;
  1428. default:
  1429. {assert(0);}
  1430. }
  1431. return kOkDspRC;
  1432. }
  1433. struct cmDspClass_str* cmTextClassCons( cmDspCtx_t* ctx )
  1434. {
  1435. cmDspClassSetup(&_cmTextDC,ctx,"Text",
  1436. NULL,
  1437. _cmDspTextAlloc,
  1438. NULL,
  1439. _cmDspTextReset,
  1440. NULL,
  1441. _cmDspTextRecv,
  1442. NULL,NULL,
  1443. "Text value control.");
  1444. return &_cmTextDC;
  1445. }
  1446. //==========================================================================================================================================
  1447. enum
  1448. {
  1449. kInMtId,
  1450. kMinMtId,
  1451. kMaxMtId,
  1452. kTimeMtId,
  1453. kLblMtId,
  1454. };
  1455. cmDspClass_t _cmMeterDC;
  1456. typedef struct
  1457. {
  1458. cmDspInst_t inst;
  1459. unsigned updSmpCnt;
  1460. unsigned lastCycleCnt;
  1461. double value;
  1462. unsigned cnt;
  1463. } cmDspMeter_t;
  1464. cmDspInst_t* _cmDspMeterAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1465. {
  1466. cmDspVarArg_t args[] =
  1467. {
  1468. { "in", kInMtId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Current value"},
  1469. { "min", kMinMtId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Minimum value"},
  1470. { "max", kMaxMtId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Maximum value"},
  1471. { "time", kTimeMtId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "UI update time in milliseconds (default:50)"},
  1472. { "label",kLblMtId, 0, 0, kStrzDsvFl, "Label"},
  1473. { NULL, 0, 0, 0, 0 }
  1474. };
  1475. cmDspMeter_t* p = cmDspInstAlloc(cmDspMeter_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1476. cmDspSetDefaultDouble(ctx, &p->inst, kInMtId, 0.0, 0);
  1477. cmDspSetDefaultDouble(ctx, &p->inst, kMinMtId, 0.0, 0);
  1478. cmDspSetDefaultDouble(ctx, &p->inst, kMaxMtId, 0.0, 1);
  1479. cmDspSetDefaultDouble(ctx, &p->inst, kTimeMtId, 0.0, 50.0);
  1480. // create the UI control
  1481. cmDspUiMeterCreate(ctx,&p->inst,kMinMtId,kMaxMtId,kInMtId,kLblMtId);
  1482. return &p->inst;
  1483. }
  1484. cmDspRC_t _cmDspMeterReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1485. {
  1486. cmDspMeter_t* p = (cmDspMeter_t*)inst;
  1487. cmDspApplyAllDefaults(ctx,inst);
  1488. double updateMs = cmDspDouble(inst,kTimeMtId);
  1489. p->updSmpCnt = floor(cmDspSampleRate(ctx) * updateMs / 1000.0);
  1490. p->lastCycleCnt = 0;
  1491. p->cnt = 0;
  1492. p->value = 0;
  1493. return kOkDspRC;
  1494. }
  1495. cmDspRC_t _cmDspMeterExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1496. {
  1497. cmDspMeter_t* p = (cmDspMeter_t*)inst;
  1498. double curVal = p->value;
  1499. bool deltaFl = p->cnt!=0 && curVal != cmDspDouble(inst,kInMtId);
  1500. bool expireFl = (ctx->cycleCnt - p->lastCycleCnt) * cmDspSamplesPerCycle(ctx) > p->updSmpCnt;
  1501. // if the meter value changed and the update time has expired
  1502. if( deltaFl && expireFl )
  1503. {
  1504. cmDspSetDouble(ctx,inst,kInMtId,curVal);
  1505. p->value = 0;
  1506. p->cnt = 0;
  1507. p->lastCycleCnt = ctx->cycleCnt;
  1508. }
  1509. return kOkDspRC;
  1510. }
  1511. cmDspRC_t _cmDspMeterRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1512. {
  1513. cmDspMeter_t* p = (cmDspMeter_t*)inst;
  1514. switch( evt->dstVarId )
  1515. {
  1516. case kInMtId:
  1517. p->value = cmDsvGetDouble(evt->valuePtr);
  1518. ++p->cnt;
  1519. return kOkDspRC;
  1520. case kTimeMtId:
  1521. p->updSmpCnt = floor(cmDspSampleRate(ctx) * cmDsvGetDouble(evt->valuePtr) / 1000.0);
  1522. break;
  1523. }
  1524. return cmDspSetEvent(ctx,inst,evt);
  1525. }
  1526. struct cmDspClass_str* cmMeterClassCons( cmDspCtx_t* ctx )
  1527. {
  1528. cmDspClassSetup(&_cmMeterDC,ctx,"Meter",
  1529. NULL,
  1530. _cmDspMeterAlloc,
  1531. NULL,
  1532. _cmDspMeterReset,
  1533. _cmDspMeterExec,
  1534. _cmDspMeterRecv,
  1535. NULL,NULL,
  1536. "Meter display.");
  1537. return &_cmMeterDC;
  1538. }
  1539. //==========================================================================================================================================
  1540. enum
  1541. {
  1542. kInLbId,
  1543. kAlignLbId
  1544. };
  1545. cmDspClass_t _cmLabelDC;
  1546. typedef struct
  1547. {
  1548. cmDspInst_t inst;
  1549. } cmDspLabel_t;
  1550. cmDspInst_t* _cmDspLabelAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1551. {
  1552. cmDspVarArg_t args[] =
  1553. {
  1554. { "in", kInLbId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "LabelText" },
  1555. { "align",kAlignLbId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Alignment 0=right 1=left 2=center" },
  1556. { NULL, 0, 0, 0, 0 }
  1557. };
  1558. cmDspLabel_t* p = cmDspInstAlloc(cmDspLabel_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1559. cmDspSetDefaultDouble(ctx, &p->inst, kAlignLbId, 0.0, kLeftAlignDuiId);
  1560. // create the UI control
  1561. cmDspUiLabelCreate(ctx,&p->inst,kInLbId,kAlignLbId);
  1562. return &p->inst;
  1563. }
  1564. cmDspRC_t _cmDspLabelReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1565. {
  1566. cmDspApplyAllDefaults(ctx,inst);
  1567. return kOkDspRC;
  1568. }
  1569. cmDspRC_t _cmDspLabelRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1570. {
  1571. return cmDspSetEvent(ctx,inst,evt);
  1572. }
  1573. struct cmDspClass_str* cmLabelClassCons( cmDspCtx_t* ctx )
  1574. {
  1575. cmDspClassSetup(&_cmLabelDC,ctx,"Label",
  1576. NULL,
  1577. _cmDspLabelAlloc,
  1578. NULL,
  1579. _cmDspLabelReset,
  1580. NULL,
  1581. _cmDspLabelRecv,
  1582. NULL,NULL,
  1583. "Label control.");
  1584. return &_cmLabelDC;
  1585. }
  1586. //==========================================================================================================================================
  1587. enum
  1588. {
  1589. kTypBtId,
  1590. kOutBtId,
  1591. kSymBtId,
  1592. kLblBtId,
  1593. kInBtId
  1594. };
  1595. cmDspClass_t _cmButtonDC;
  1596. typedef struct
  1597. {
  1598. cmDspInst_t inst;
  1599. unsigned resetSymId;
  1600. } cmDspButton_t;
  1601. cmDspInst_t* _cmDspButtonAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1602. {
  1603. va_list vl1;
  1604. va_copy(vl1,vl);
  1605. assert( va_cnt >= 1 );
  1606. unsigned typeId = va_arg(vl,unsigned);
  1607. // check buttons should transmit their default values - push buttons should not.
  1608. unsigned sendDfltFl = typeId == kCheckDuiId ? kSendDfltDsvFl : 0;
  1609. cmDspVarArg_t args[] =
  1610. {
  1611. { "typ", kTypBtId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Type" },
  1612. { "out", kOutBtId, 0, 0, kOutDsvFl | kDoubleDsvFl | kOptArgDsvFl | sendDfltFl, "Value"},
  1613. { "sym", kSymBtId, 0, 0, kOutDsvFl | kSymDsvFl | kOptArgDsvFl | sendDfltFl, "Symbol Value"},
  1614. { "label",kLblBtId, 0, 0, kInDsvFl | kStrzDsvFl | kOptArgDsvFl, "Label"},
  1615. { "in", kInBtId, 0, 0, kInDsvFl | kTypeDsvMask, "Simulate UI"},
  1616. { NULL, 0, 0, 0, 0 }
  1617. };
  1618. cmDspButton_t* p = cmDspInstAlloc(cmDspButton_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl1);
  1619. cmDspSetDefaultDouble(ctx, &p->inst, kOutBtId, 0.0, typeId==kCheckDuiId ? 0.0 : 1.0);
  1620. cmDspSetDefaultSymbol(ctx, &p->inst, kSymBtId, instSymId );
  1621. cmDspSetDefaultStrcz( ctx, &p->inst, kLblBtId, NULL, cmSymTblLabel(ctx->stH,instSymId));
  1622. p->resetSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"_reset");
  1623. // create the UI control
  1624. cmDspUiButtonCreate(ctx,&p->inst,typeId,kOutBtId,kLblBtId);
  1625. return &p->inst;
  1626. }
  1627. cmDspRC_t _cmDspButtonReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1628. {
  1629. cmDspApplyAllDefaults(ctx,inst);
  1630. return kOkDspRC;
  1631. }
  1632. cmDspRC_t _cmDspButtonRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1633. {
  1634. // the 'in' port is the only input port
  1635. // but the UI sends button pushes use kOutBtId - should this be changed?
  1636. assert( evt->dstVarId == kInBtId || evt->dstVarId == kOutBtId );
  1637. // We accept all types at the 'in' port but are only interested
  1638. // in transmitting doubles from the 'out' port.
  1639. if( cmDsvCanConvertFlags( kDoubleDsvFl, evt->valuePtr->flags ) )
  1640. {
  1641. // Redirect events which can be converted to type kDoubleDsvFl
  1642. // to the output port.
  1643. //
  1644. // Convert the event dest var id from the 'kInBtId' to 'kOutBtId'
  1645. // and update the UI with the incoming value
  1646. cmDspSetEventUiId(ctx,inst,evt,kOutBtId);
  1647. }
  1648. // no matter what kind of msg enters the 'in' port send a symbol out the 'sym' port
  1649. if( inst->symId != cmInvalidId )
  1650. cmDspSetSymbol( ctx, inst, kSymBtId, inst->symId );
  1651. return kOkDspRC;
  1652. }
  1653. cmDspRC_t _cmDspButtonPresetRdWr( cmDspCtx_t* ctx, cmDspInst_t* inst, bool storeFl )
  1654. {
  1655. cmDspRC_t rc = kOkDspRC;
  1656. if( cmDspUInt(inst,kTypBtId) == kCheckDuiId )
  1657. rc = cmDspVarPresetRdWr(ctx,inst,kOutBtId,storeFl);
  1658. return rc;
  1659. }
  1660. cmDspRC_t _cmDspButtonSysRecvFunc( cmDspCtx_t* ctx, struct cmDspInst_str* inst, unsigned attrSymId, const cmDspValue_t* value )
  1661. {
  1662. cmDspButton_t* p = (cmDspButton_t*)inst;
  1663. if( attrSymId == p->resetSymId )
  1664. {
  1665. cmDspSetSymbol( ctx, inst, kSymBtId, p->resetSymId );
  1666. cmDspSetDouble(ctx,inst,kOutBtId, cmDspDouble(inst,kOutBtId));
  1667. }
  1668. return kOkDspRC;
  1669. }
  1670. struct cmDspClass_str* cmButtonClassCons( cmDspCtx_t* ctx )
  1671. {
  1672. cmDspClassSetup(&_cmButtonDC,ctx,"Button",
  1673. NULL,
  1674. _cmDspButtonAlloc,
  1675. NULL,
  1676. _cmDspButtonReset,
  1677. NULL,
  1678. _cmDspButtonRecv,
  1679. _cmDspButtonPresetRdWr,
  1680. _cmDspButtonSysRecvFunc,
  1681. "Button control.");
  1682. return &_cmButtonDC;
  1683. }
  1684. //==========================================================================================================================================
  1685. cmDspClass_t _cmReorderDC;
  1686. typedef struct
  1687. {
  1688. cmDspInst_t inst;
  1689. unsigned portCnt; // count of input ports and count of output ports
  1690. unsigned* execFlArray; // execFlArray[portCnt] - true for ports which should cause obj to generate output
  1691. unsigned* orderArray; // orderArray[portCnt] - port output order map
  1692. } cmDspReorder_t;
  1693. cmDspInst_t* _cmDspReorderAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1694. {
  1695. if( va_cnt < 1 )
  1696. {
  1697. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"Reorder objects must have arguments.");
  1698. return NULL;
  1699. }
  1700. // the first argument is the count of input ports (which is also the count of output ports)
  1701. int portCnt = va_arg(vl,int);
  1702. if( portCnt < 2 )
  1703. {
  1704. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"Reorder objects must have at least 2 ports.");
  1705. return NULL;
  1706. }
  1707. cmDspVarArg_t args[portCnt*2+1];
  1708. unsigned i;
  1709. int maxLabelCharCnt = 15;
  1710. cmChar_t label[ maxLabelCharCnt+1 ];
  1711. label[maxLabelCharCnt] = 0;
  1712. for(i=0; i<portCnt*2; ++i)
  1713. {
  1714. snprintf(label,maxLabelCharCnt,"%s-%i", (i<portCnt?"in":"out"), i%portCnt);
  1715. unsigned symId = cmSymTblRegisterSymbol(ctx->stH,label);
  1716. args[i].label = cmSymTblLabel(ctx->stH,symId);
  1717. args[i].constId = i;
  1718. args[i].rn = 0;
  1719. args[i].cn = 0;
  1720. args[i].flags = (i<portCnt ? kInDsvFl : kOutDsvFl) | kTypeDsvMask;
  1721. args[i].doc = i<portCnt ? "Any input" : "Any output";
  1722. }
  1723. memset(args+i,0,sizeof(args[0]));
  1724. cmDspReorder_t* p = cmDspInstAlloc(cmDspReorder_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1725. if( p != NULL )
  1726. {
  1727. p->orderArray = cmLhAllocZ(ctx->lhH,unsigned,portCnt*2);
  1728. p->execFlArray = p->orderArray + portCnt;
  1729. p->portCnt = portCnt;
  1730. if( va_cnt-1 < portCnt )
  1731. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The reorder constructor must contain an output order map.");
  1732. else
  1733. {
  1734. for(i=0; i<portCnt; ++i)
  1735. {
  1736. int order = va_arg(vl,int);
  1737. if( order >= portCnt )
  1738. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The port order index %i is outside the valid range of 0-%i.",order,portCnt-1);
  1739. else
  1740. p->orderArray[ order ] = i;
  1741. }
  1742. va_cnt -= portCnt+1;
  1743. for(i=0; i<va_cnt; ++i)
  1744. {
  1745. int execPortIdx = va_arg(vl,int);
  1746. if( execPortIdx >= portCnt )
  1747. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The port exec index %i is outside the valid range of 0-%i.",execPortIdx,portCnt-1);
  1748. else
  1749. p->execFlArray[ execPortIdx ] = true;
  1750. }
  1751. }
  1752. }
  1753. return &p->inst;
  1754. }
  1755. cmDspRC_t _cmDspReorderFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1756. {
  1757. cmDspReorder_t* p = (cmDspReorder_t*)inst;
  1758. cmLhFree(ctx->lhH,p->orderArray);
  1759. return kOkDspRC;
  1760. }
  1761. cmDspRC_t _cmDspReorderRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1762. {
  1763. cmDspRC_t rc = kOkDspRC;
  1764. cmDspReorder_t* p = (cmDspReorder_t*)inst;
  1765. cmDspSetEvent(ctx,inst,evt);
  1766. if( evt->dstVarId < p->portCnt )
  1767. if( p->execFlArray[evt->dstVarId] )
  1768. {
  1769. unsigned i;
  1770. for(i=0; i<p->portCnt; ++i)
  1771. {
  1772. unsigned inVarId = p->orderArray[i];
  1773. unsigned outVarId = inVarId + p->portCnt;
  1774. const cmDspVar_t* varPtr = cmDspVarIdToCPtr(inst, inVarId );
  1775. assert(varPtr != NULL);
  1776. if((rc = cmDspValueSet(ctx, inst, outVarId, &varPtr->value, 0 )) != kOkDspRC )
  1777. break;
  1778. }
  1779. }
  1780. return rc;
  1781. }
  1782. struct cmDspClass_str* cmReorderClassCons( cmDspCtx_t* ctx )
  1783. {
  1784. cmDspClassSetup(&_cmReorderDC,ctx,"Reorder",
  1785. NULL,
  1786. _cmDspReorderAlloc,
  1787. _cmDspReorderFree,
  1788. NULL,
  1789. NULL,
  1790. _cmDspReorderRecv,
  1791. NULL,NULL,
  1792. "Reorder value control.");
  1793. return &_cmReorderDC;
  1794. }
  1795. //==========================================================================================================================================
  1796. enum
  1797. {
  1798. kDirFnId,
  1799. kPatFnId,
  1800. kValFnId,
  1801. };
  1802. cmDspClass_t _cmFnameDC;
  1803. typedef struct
  1804. {
  1805. cmDspInst_t inst;
  1806. } cmDspFname_t;
  1807. // Pattern string for HTML and image files:
  1808. // "HTML Files (*.html)\tImage Files (*.{bmp,gif,jpg,png})"
  1809. // The va_list must include 3 args:
  1810. // A pointer to a string referning to a default filename or directory or NULL.
  1811. // A pointer to a string referring to a pattern string or NULL.
  1812. // A bool to set the 'dirFl'.
  1813. cmDspInst_t* _cmDspFnameAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1814. {
  1815. cmDspVarArg_t args[] =
  1816. {
  1817. { "dir", kDirFnId, 0, 0, kInDsvFl | kReqArgDsvFl | kBoolDsvFl, "Dir=true Filename=false" },
  1818. { "pat", kPatFnId, 0, 0, kInDsvFl | kOptArgDsvFl | kStrzDsvFl, "File pattern string (e.g. HTML Files (*.html)\tImage Files (*.{bmp,gif,jpg,png}))" },
  1819. { "out", kValFnId, 0, 0, kOutDsvFl | kOptArgDsvFl | kStrzDsvFl, "Current file or directory name." },
  1820. { NULL, 0, 0, 0, 0 }
  1821. };
  1822. cmDspFname_t* p = cmDspInstAlloc(cmDspFname_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1823. cmDspSetDefaultBool( ctx, &p->inst, kDirFnId, false, false);
  1824. cmDspSetDefaultStrcz(ctx, &p->inst, kPatFnId, NULL, "All Files (*.*)");
  1825. cmDspSetDefaultStrcz(ctx, &p->inst, kValFnId, NULL, cmFsUserDir());
  1826. cmDspUiFnameCreate(ctx,&p->inst,kValFnId,kPatFnId,kDirFnId);
  1827. return &p->inst;
  1828. }
  1829. cmDspRC_t _cmDspFnameReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1830. {
  1831. cmDspApplyAllDefaults(ctx,inst);
  1832. return kOkDspRC;
  1833. }
  1834. cmDspRC_t _cmDspFnameRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1835. {
  1836. cmDspSetEvent(ctx,inst,evt);
  1837. switch( evt->dstVarId )
  1838. {
  1839. case kDirFnId:
  1840. case kPatFnId:
  1841. case kValFnId:
  1842. break;
  1843. default:
  1844. {assert(0);}
  1845. }
  1846. return kOkDspRC;
  1847. }
  1848. struct cmDspClass_str* cmFnameClassCons( cmDspCtx_t* ctx )
  1849. {
  1850. cmDspClassSetup(&_cmFnameDC,ctx,"Fname",
  1851. NULL,
  1852. _cmDspFnameAlloc,
  1853. NULL,
  1854. _cmDspFnameReset,
  1855. NULL,
  1856. _cmDspFnameRecv,
  1857. NULL,NULL,
  1858. "File or directory chooser control.");
  1859. return &_cmFnameDC;
  1860. }
  1861. //==========================================================================================================================================
  1862. cmDspClass_t _cmMsgListDC;
  1863. typedef struct
  1864. {
  1865. cmDspInst_t inst;
  1866. cmJsonH_t jsH; // current JSON tree
  1867. cmJsonNode_t* np; // ptr to JSON array
  1868. unsigned colCnt; // number of elements in the JSON sub-arrays (rows)
  1869. 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)
  1870. unsigned* typeIdArray; // JSON types for each column size: typeIdArray[ colCnt ]
  1871. unsigned symColCnt; // same as number of elements of typeIdArray[] == kStringTId
  1872. unsigned symRowCnt; // same as cmJsonChildCount(p->np)
  1873. unsigned* symM; // symM[symRowCnt,symColCnt] symbol matrix
  1874. } cmDspMsgList_t;
  1875. // create a matrix to hold the symbol id's associated with any string values
  1876. cmDspRC_t _cmDspMsgListLoadSymbolMtx( cmDspCtx_t* ctx, cmDspMsgList_t* p )
  1877. {
  1878. assert(cmJsonIsArray(p->np));
  1879. unsigned i,j,k;
  1880. // remove any existing symbols
  1881. if( p->symM != NULL )
  1882. for(i=0; i<p->symColCnt*p->symRowCnt; ++i)
  1883. if( p->symM[i] != cmInvalidId )
  1884. cmSymTblRemove(ctx->stH,p->symM[i]);
  1885. // reallocate the symbol matrix
  1886. p->symRowCnt = cmJsonChildCount(p->np);
  1887. p->symM = cmLhResizeN(ctx->lhH,unsigned,p->symM,p->symRowCnt*p->symColCnt);
  1888. // for each row in the JSON array
  1889. for(i=0; i<p->symRowCnt; ++i)
  1890. {
  1891. const cmJsonNode_t* cnp = cmJsonArrayElementC(p->np,i);
  1892. // for each column in row whose data type is a string
  1893. for(j=0,k=0; k<p->symColCnt; ++j)
  1894. if( p->typeIdArray[j] == kStringTId )
  1895. {
  1896. const cmJsonNode_t* vnp = cmJsonArrayElementC(cnp,j);
  1897. unsigned idx = k*p->symRowCnt + i;
  1898. const cmChar_t* text;
  1899. // register the string with the symbol table
  1900. if((text = vnp->u.stringVal) != NULL )
  1901. p->symM[ idx ] = cmSymTblRegisterStaticSymbol(ctx->stH,text);
  1902. else
  1903. p->symM[ idx ] = cmInvalidId;
  1904. ++k;
  1905. }
  1906. }
  1907. return kOkDspRC;
  1908. }
  1909. // Load a JSON file and set the supplied cmJsonH_t handle.
  1910. 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 )
  1911. {
  1912. cmDspRC_t rc = kOkDspRC; //
  1913. cmJsonNode_t* np = NULL; //
  1914. cmJsonH_t jsH = cmJsonNullHandle; //
  1915. *hp = cmJsonNullHandle;
  1916. *colCntPtr = 0;
  1917. *npp = NULL;
  1918. // if no file name was given ...
  1919. if( fn==NULL || strlen(fn)==0 )
  1920. {
  1921. jsH = ctx->rsrcJsH; // ... use the rsrc file
  1922. if( cmJsonIsValid(ctx->rsrcJsH) == false )
  1923. return cmErrMsg(err,kJsonFailDspRC,"No JSON cfg resource exists for this DSP program.");
  1924. fn = NULL;
  1925. }
  1926. else
  1927. {
  1928. if( cmJsonInitializeFromFile(&jsH,fn,ctx->cmCtx) != kOkJsRC )
  1929. return cmErrMsg(err,kJsonFailDspRC,"The msg list JSON file load failed on '%s'.",fn);
  1930. }
  1931. // find the array named by rsrcLabel
  1932. if((np = cmJsonFindValue(jsH,rsrcLabel,NULL,cmInvalidId)) == NULL)
  1933. return cmErrMsg(err,kJsonFailDspRC,"The msg list JSON tree does not have an array named '%s'.",rsrcLabel);
  1934. // be sure the msg list really is an array
  1935. if( cmJsonIsArray(np) == false )
  1936. return cmErrMsg(err,kJsonFailDspRC,"The msg list JSON element named '%s' is not an array.", rsrcLabel);
  1937. if( fn == NULL )
  1938. fn = "<resource file>";
  1939. // count of elements in the array
  1940. unsigned n = cmJsonChildCount(np);
  1941. unsigned m = 0;
  1942. unsigned i,j;
  1943. // for each line in the array
  1944. for(i=0; i<n; ++i)
  1945. {
  1946. const cmJsonNode_t* cnp;
  1947. // get the ith sub-array (row)
  1948. if((cnp = cmJsonArrayElementC(np,i)) != NULL )
  1949. {
  1950. // verify that it is an array
  1951. if( cmJsonIsArray(cnp ) == false )
  1952. return cmErrMsg(err,kJsonFailDspRC,"The msg list JSON element in '%s' at index %i is not an array.",fn,i);
  1953. // track the number of elements (columns) per row
  1954. unsigned q = cmJsonChildCount(cnp);
  1955. // if this is the first row then use it to set the valid column count
  1956. if( i==0 )
  1957. m = q;
  1958. else
  1959. {
  1960. if( m != q )
  1961. 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);
  1962. }
  1963. }
  1964. }
  1965. //
  1966. // determine and validate the column types
  1967. //
  1968. unsigned typeIdArray[m];
  1969. // for each row
  1970. for(i=0; i<n; ++i)
  1971. {
  1972. const cmJsonNode_t* cnp = cmJsonArrayElementC(np,i);
  1973. // for each column
  1974. for(j=0; j<m; ++j)
  1975. {
  1976. const cmJsonNode_t* sap = cmJsonArrayElementC(cnp,j);
  1977. unsigned typeId = sap->typeId & kMaskTId;
  1978. // the first row sets the expected type id for each column
  1979. switch(i)
  1980. {
  1981. case 0:
  1982. if( typeId != kStringTId )
  1983. return cmErrMsg(err,kJsonFailDspRC,"The first row of a msg list (%s) file must contain string elements which set the colum labels.",rsrcLabel);
  1984. break;
  1985. case 1:
  1986. typeIdArray[j] = typeId;
  1987. break;
  1988. default:
  1989. {
  1990. // if the type is a string then it can only
  1991. // match if the column is a string or null
  1992. // so we assume an error
  1993. if( typeId == kStringTId )
  1994. rc = kJsonFailDspRC;
  1995. switch(typeIdArray[j])
  1996. {
  1997. case kStringTId:
  1998. if( typeId != kStringTId && typeId != kNullTId)
  1999. 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);
  2000. rc = kOkDspRC; // clear the assummed error
  2001. break;
  2002. case kNullTId: // null can be converted to anything
  2003. typeIdArray[j] = typeId;
  2004. break;
  2005. case kIntTId:
  2006. if( typeId == kRealTId ) // ints may be promoted to reals
  2007. typeIdArray[j] = kRealTId;
  2008. break;
  2009. case kRealTId:
  2010. break;
  2011. case kFalseTId:
  2012. case kTrueTId: // bools may be promoted to ints or reals
  2013. if( typeId == kIntTId || typeId == kRealTId )
  2014. typeIdArray[j] = typeId;
  2015. break;
  2016. default:
  2017. 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);
  2018. }
  2019. if( rc != kOkDspRC )
  2020. 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);
  2021. } // end dflt
  2022. } // end switch
  2023. } // end row
  2024. } // end list
  2025. // VERY TRICKY - store the column type id's in the label columns type id.
  2026. // This is stupid but safe because the column type id's are known to be set to kStringTId.
  2027. cmJsonNode_t* lnp = np->u.childPtr->u.childPtr;
  2028. for(i=0; i<m; ++i,lnp=lnp->siblingPtr)
  2029. lnp->typeId = typeIdArray[i];
  2030. *hp = fn==NULL ? cmJsonNullHandle : jsH;
  2031. *npp = np;
  2032. *colCntPtr = m;
  2033. return rc;
  2034. }
  2035. // use the JSON list labels to setup the cmDspVarArg_t records associated with each msg output var.
  2036. 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 )
  2037. {
  2038. unsigned i,j;
  2039. unsigned fixedArgCnt = 0;
  2040. for(i=0; args[i].label != NULL; ++i)
  2041. ++fixedArgCnt;
  2042. cmDspVarArg_t a[ fixedArgCnt + colCnt + 1 ];
  2043. // copy the fixed arg's into the first fixedArgCnt ele's of a[]
  2044. for(j=0; j<fixedArgCnt; ++j)
  2045. a[j] = args[j];
  2046. // remove the label row from the JSON array
  2047. cmJsonNode_t* lnp = np->u.childPtr; // store the pointer to the label row
  2048. //np->u.childPtr = lnp->siblingPtr; // remove the labels from the array
  2049. // for each column
  2050. for(i=0; i<colCnt; ++i,++j)
  2051. {
  2052. cmJsonNode_t* lp = cmJsonArrayElement(lnp,i);
  2053. // store a pointer to the label
  2054. a[j].label = lp->u.stringVal;
  2055. a[j].constId = j;
  2056. a[j].rn = 0;
  2057. a[j].cn = 0;
  2058. a[j].flags = kOutDsvFl;
  2059. a[j].doc = "Msg output";
  2060. typeIdArray[i] = lp->typeId;
  2061. // convert the JSON type to a DSV type
  2062. switch( lp->typeId )
  2063. {
  2064. case kNullTId: a[j].flags |= kUIntDsvFl; break;
  2065. case kIntTId: a[j].flags |= kIntDsvFl; break;
  2066. case kRealTId: a[j].flags |= kDoubleDsvFl; break;
  2067. case kTrueTId: a[j].flags |= kBoolDsvFl; break;
  2068. case kFalseTId: a[j].flags |= kBoolDsvFl; break;
  2069. case kStringTId: a[j].flags |= kSymDsvFl; break; // strings are treated as symbols (UInt)
  2070. default:
  2071. { assert(0); }
  2072. }
  2073. // undo the tricky bit with the label types
  2074. lp->typeId = kStringTId;
  2075. }
  2076. // set the null sentinel at the end of the arg array
  2077. memset(a + j,0,sizeof(cmDspVarArg_t));
  2078. return (cmDspMsgList_t*)cmDspInstAlloc(cmDspMsgList_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
  2079. }
  2080. cmDspRC_t _cmDspMsgListReload( cmDspCtx_t* ctx, cmDspMsgList_t* p, const cmChar_t* rsrcLabel, const cmChar_t* fn )
  2081. {
  2082. cmDspRC_t rc = kOkDspRC;
  2083. cmJsonH_t jsH = cmJsonNullHandle;
  2084. unsigned colCnt = 0;
  2085. cmJsonNode_t* np = NULL;
  2086. // load the file
  2087. if((rc = _cmDspMsgListLoadFile(ctx,&p->inst.classPtr->err,rsrcLabel,fn,&jsH,&colCnt,&np)) != kOkDspRC )
  2088. return cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"The msg list file '%s' load failed.",fn);
  2089. // verify that the col count is correct
  2090. if( colCnt != p->colCnt )
  2091. 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);
  2092. unsigned fixArgCnt = p->inst.varCnt - p->colCnt;
  2093. cmJsonNode_t* lnp = np->u.childPtr->u.childPtr;
  2094. unsigned i,j;
  2095. for(i=0,j=fixArgCnt; i<colCnt; ++i,++j, lnp=lnp->siblingPtr)
  2096. {
  2097. const cmChar_t* labelStr = cmSymTblLabel(ctx->stH,p->inst.varArray[j].symId);
  2098. // the labels of the new file must match the labels of the previous file
  2099. if( strcmp(lnp->u.stringVal,labelStr) )
  2100. 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);
  2101. // if the msg column is a string ....
  2102. if( (p->inst.varArray[j].flags & kTypeDsvMask) == kStrzDsvFl )
  2103. {
  2104. // ... then the file column must also be a string or null
  2105. if( lnp->typeId != kStringTId && np->typeId != kNullTId )
  2106. 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);
  2107. }
  2108. else // otherwie if the msg column is a number ...
  2109. {
  2110. // ... then the file type can't be a string
  2111. if( lnp->typeId == kStringTId )
  2112. return cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"The data type of msg list file '%s' column index %i cannot be a string.",fn,i);
  2113. // TODO: maybe there are other type conversions to check for here.
  2114. }
  2115. // reset the typeid of the labels
  2116. // (this is a cleanup from the tricky bit at the end of _cmDspListLoadFile())
  2117. lnp->typeId = kStringTId;
  2118. }
  2119. if( cmJsonIsValid( jsH ) )
  2120. {
  2121. if( cmHandlesAreNotEqual(p->jsH,p->dfltJsH) && cmHandlesAreNotEqual(p->jsH,ctx->rsrcJsH) )
  2122. cmJsonFinalize(&p->jsH);
  2123. p->jsH = jsH;
  2124. p->np = np;
  2125. _cmDspMsgListLoadSymbolMtx(ctx,p);
  2126. }
  2127. return rc;
  2128. }
  2129. enum
  2130. {
  2131. kRsrcMlId,
  2132. kFnMlId,
  2133. kSelMlId,
  2134. kListMlId,
  2135. kCntMlId,
  2136. kOutBaseMlId // identify the first output port
  2137. };
  2138. cmDspInst_t* _cmDspMsgListAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2139. {
  2140. va_list vl2;
  2141. cmDspVarArg_t args[] =
  2142. {
  2143. { "rsrc", kRsrcMlId, 0, 0, kInDsvFl | kReqArgDsvFl | kStrzDsvFl, "Msg list resource label"},
  2144. { "fn", kFnMlId, 0, 0, kInDsvFl | kOptArgDsvFl | kStrzDsvFl, "Msg list file name" },
  2145. { "sel", kSelMlId, 0, 0, kOutDsvFl | kInDsvFl | kOptArgDsvFl | kUIntDsvFl, "Current selection index" },
  2146. { "list", kListMlId, 0, 0, kInDsvFl | kJsonDsvFl, "Msg list data as a JSON array"},
  2147. { "cnt", kCntMlId, 0, 0, kOutDsvFl | kSendDfltDsvFl | kUIntDsvFl , "Count of elements."},
  2148. { NULL, 0, 0, 0, 0 }
  2149. };
  2150. va_copy(vl2,vl);
  2151. if( va_cnt < 1 )
  2152. {
  2153. cmErrMsg(&classPtr->err,kVarArgParseFailDspRC,"The message list constructor must contain at least two arguments.");
  2154. goto errLabel;
  2155. }
  2156. const cmChar_t* rsrcLabel = va_arg(vl,const char*);
  2157. const cmChar_t* fn = va_cnt>1 ? va_arg(vl,const char*) : NULL;
  2158. unsigned colCnt = 0;
  2159. cmJsonH_t jsH = cmJsonNullHandle;
  2160. cmDspMsgList_t* p;
  2161. cmJsonNode_t* np;
  2162. cmDspRC_t rc;
  2163. // be sure the rsrc label contains a valid string
  2164. if( rsrcLabel==NULL || strlen(rsrcLabel)==0 )
  2165. {
  2166. cmErrMsg(&classPtr->err,kVarArgParseFailDspRC,"No msg list resource label was given.");
  2167. goto errLabel;
  2168. }
  2169. // load and validate the JSON file
  2170. if((rc = _cmDspMsgListLoadFile(ctx,&classPtr->err,rsrcLabel,fn,&jsH,&colCnt,&np)) == kOkDspRC )
  2171. {
  2172. unsigned i;
  2173. unsigned typeIdArray[colCnt];
  2174. // allocate the instance
  2175. if((p = _cmDspMsgListCons(ctx,classPtr,storeSymId,instSymId,id,args,jsH,colCnt, np, typeIdArray, va_cnt, vl2 )) != NULL )
  2176. {
  2177. p->jsH = jsH;
  2178. p->np = np;
  2179. p->dfltJsH = jsH;
  2180. p->colCnt = colCnt;
  2181. p->typeIdArray = cmLhAllocZ( ctx->lhH, unsigned, colCnt );
  2182. memcpy(p->typeIdArray,typeIdArray,sizeof(p->typeIdArray[0])*colCnt);
  2183. p->symColCnt = 0;
  2184. for(i=0; i<p->colCnt; ++i)
  2185. if( p->typeIdArray[i] == kStringTId )
  2186. ++p->symColCnt;
  2187. _cmDspMsgListLoadSymbolMtx(ctx,p);
  2188. cmDspSetDefaultStrcz( ctx,&p->inst, kRsrcMlId,NULL, rsrcLabel); // rsrc label
  2189. cmDspSetDefaultStrcz( ctx,&p->inst, kFnMlId, NULL, fn); // file name var
  2190. cmDspSetDefaultJson( ctx,&p->inst, kListMlId,NULL, np); // default tree
  2191. cmDspSetDefaultUInt( ctx,&p->inst, kSelMlId, 0, 0); // selection
  2192. cmDspSetDefaultUInt( ctx,&p->inst, kCntMlId, 0, p->symRowCnt);
  2193. // if there is only one column then signal the UI to create a menu button rather
  2194. // than a list by setting the height to zero.
  2195. unsigned height = p->colCnt == 1 ? 0 : 5;
  2196. // create the list UI element
  2197. cmDspUiMsgListCreate(ctx, &p->inst, height, kListMlId, kSelMlId );
  2198. return &p->inst;
  2199. }
  2200. }
  2201. errLabel:
  2202. return NULL;
  2203. }
  2204. cmDspRC_t _cmDspMsgListFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2205. {
  2206. cmDspMsgList_t* p = (cmDspMsgList_t*)inst;
  2207. // be careful not release the ctx->rsrcJsH handle if is happens to be the p->dflJsH
  2208. if( cmJsonIsValid(p->dfltJsH) && cmHandlesAreNotEqual(p->jsH,p->dfltJsH) && cmHandlesAreNotEqual(p->dfltJsH,ctx->rsrcJsH))
  2209. if( cmJsonFinalize(&p->dfltJsH) != kOkJsRC )
  2210. cmDspInstErr(ctx,inst,kJsonFailDspRC,"JSON default tree finalize failed.");
  2211. // be careful not to release the ctx->rsrcJsH handle if it happens to be the p->jsH
  2212. if( cmJsonIsValid(p->jsH) && cmHandlesAreNotEqual(p->dfltJsH,ctx->rsrcJsH) )
  2213. if( cmJsonFinalize( &p->jsH ) != kOkJsRC )
  2214. cmDspInstErr(ctx,inst,kJsonFailDspRC,"JSON finalization failed.");
  2215. cmLhFree(ctx->lhH,p->typeIdArray);
  2216. p->typeIdArray = NULL;
  2217. cmLhFree(ctx->lhH,p->symM);
  2218. p->symM = NULL;
  2219. return kOkDspRC;
  2220. }
  2221. cmDspRC_t _cmDspMsgListReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2222. {
  2223. cmDspApplyAllDefaults(ctx,inst);
  2224. return kOkDspRC;
  2225. }
  2226. cmDspRC_t _cmDspMsgOnSel( cmDspCtx_t* ctx, cmDspMsgList_t* p, unsigned rowIdx )
  2227. {
  2228. cmDspRC_t rc = kOkDspRC;
  2229. unsigned i,j;
  2230. unsigned symId;
  2231. const cmJsonNode_t* cnp = cmJsonArrayElementC(p->np,rowIdx);
  2232. assert( cnp != NULL );
  2233. // for each output variable (column)
  2234. for(i=0,j=0; i<p->colCnt; ++i)
  2235. {
  2236. unsigned varId = kOutBaseMlId + i; // variable id
  2237. const cmJsonNode_t* np = cmJsonArrayElementC(cnp,i); // json row array
  2238. assert( np != NULL );
  2239. switch( p->typeIdArray[i] )
  2240. {
  2241. case kNullTId:
  2242. break;
  2243. case kIntTId:
  2244. {
  2245. int v;
  2246. if( cmJsonIntValue(np,&v) != kOkJsRC )
  2247. { assert(0); }
  2248. rc = cmDspSetInt(ctx,&p->inst,varId,v);
  2249. }
  2250. break;
  2251. case kRealTId:
  2252. {
  2253. double v;
  2254. if( cmJsonRealValue(np,&v) != kOkJsRC )
  2255. { assert(0); }
  2256. rc = cmDspSetDouble(ctx,&p->inst,varId,v);
  2257. }
  2258. break;
  2259. case kTrueTId:
  2260. case kFalseTId:
  2261. {
  2262. bool v;
  2263. if( cmJsonBoolValue(np,&v) != kOkJsRC )
  2264. { assert(0); }
  2265. rc = cmDspSetBool(ctx,&p->inst,varId,v);
  2266. }
  2267. break;
  2268. case kStringTId:
  2269. {
  2270. assert( j < p->symColCnt );
  2271. if((symId = p->symM[ (j*p->symRowCnt) + rowIdx ]) != cmInvalidId )
  2272. if((rc = cmDspSetSymbol(ctx,&p->inst,varId,symId )) != kOkDspRC )
  2273. break;
  2274. ++j;
  2275. }
  2276. break;
  2277. default:
  2278. { assert(0); }
  2279. } // end switch
  2280. } // end for
  2281. return rc;
  2282. }
  2283. cmDspRC_t _cmDspMsgListRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2284. {
  2285. cmDspRC_t rc = kOkDspRC;
  2286. cmDspMsgList_t* p = (cmDspMsgList_t*)inst;
  2287. switch( evt->dstVarId )
  2288. {
  2289. case kRsrcMlId:
  2290. {
  2291. const cmChar_t* fn = cmDspStrcz(inst,kFnMlId);
  2292. const cmChar_t* rsrcLabel = cmDsvStrz(evt->valuePtr);
  2293. if( rsrcLabel != NULL )
  2294. {
  2295. if((rc = _cmDspMsgListReload(ctx,p,rsrcLabel,fn)) != kOkDspRC )
  2296. return cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"The msg list file '%s' load failed.",fn);
  2297. cmDspSetEvent(ctx,inst,evt);
  2298. cmDspSetJson(ctx,inst,kListMlId,p->np);
  2299. cmDspSetUInt(ctx,inst,kSelMlId,0);
  2300. cmDspSetUInt(ctx,inst,kCntMlId,p->symRowCnt);
  2301. }
  2302. }
  2303. break;
  2304. case kFnMlId:
  2305. // Store the new file name.
  2306. // A new file will not be loaded until the next rsrc label is received.
  2307. cmDspSetEvent(ctx,inst,evt);
  2308. break;
  2309. case kListMlId:
  2310. break;
  2311. case kSelMlId:
  2312. {
  2313. unsigned rowIdx = cmDsvGetUInt(evt->valuePtr);
  2314. assert( rowIdx < p->symRowCnt);
  2315. // set the current selection variable
  2316. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  2317. rc = _cmDspMsgOnSel(ctx,p,rowIdx);
  2318. } //end case
  2319. break;
  2320. }
  2321. return rc;
  2322. }
  2323. cmDspRC_t _cmDspMsgListPresetRdWr( cmDspCtx_t* ctx, cmDspInst_t* inst, bool storeFl )
  2324. {
  2325. cmDspRC_t rc = kOkDspRC;
  2326. cmDspMsgList_t* p = (cmDspMsgList_t*)inst;
  2327. if((rc = cmDspVarPresetRdWr(ctx,inst,kSelMlId,storeFl)) == kOkDspRC )
  2328. {
  2329. if( !storeFl )
  2330. rc = _cmDspMsgOnSel(ctx,p, cmDspUInt(inst,kSelMlId) );
  2331. }
  2332. return rc;
  2333. }
  2334. struct cmDspClass_str* cmMsgListClassCons( cmDspCtx_t* ctx )
  2335. {
  2336. cmDspClassSetup(&_cmMsgListDC,ctx,"MsgList",
  2337. NULL,
  2338. _cmDspMsgListAlloc,
  2339. _cmDspMsgListFree,
  2340. _cmDspMsgListReset,
  2341. NULL,
  2342. _cmDspMsgListRecv,
  2343. _cmDspMsgListPresetRdWr,
  2344. NULL,
  2345. "Message list selection control.");
  2346. return &_cmMsgListDC;
  2347. }
  2348. //==========================================================================================================================================
  2349. enum
  2350. {
  2351. kLenWtId,
  2352. kShapeWtId,
  2353. kFnWtId,
  2354. kLoopWtId,
  2355. kBegWtId,
  2356. kEndWtId,
  2357. kCmdWtId,
  2358. kOtWtId,
  2359. kGainWtId,
  2360. kPhsWtId,
  2361. kOutWtId,
  2362. kCntWtId,
  2363. kFIdxWtId,
  2364. kDoneWtId
  2365. };
  2366. enum
  2367. {
  2368. kSilenceWtId, // 0
  2369. kFileWtId, // 1
  2370. kWhiteWtId, // 2
  2371. kPinkWtId, // 3
  2372. kSineWtId, // 4
  2373. kCosWtId, // 5
  2374. kSqrWtId, // 6
  2375. kTriWtId, // 7
  2376. kSawWtId, // 8
  2377. kPulseWtId, // 9
  2378. kImpulseWtId, // 10
  2379. kPhasorWtId, // 11
  2380. kShapeWtCnt
  2381. };
  2382. cmDspClass_t _cmWaveTableDC;
  2383. typedef struct
  2384. {
  2385. cmDspInst_t inst;
  2386. cmSample_t* wt; // wave table memory
  2387. unsigned wti; // next location to write samples into the wavetable
  2388. unsigned wtn; // count of empty samples (avail for writing over) in the wavetable.
  2389. unsigned fi; // absolute index into the file of the next sample to read
  2390. unsigned fn; // length of the file in samples
  2391. unsigned cfi; // absolute index into the file of the beginning of the current audio vector
  2392. unsigned cfn; // when cfi >= cfn and doneFl is set then the 'done' msg is sent
  2393. unsigned loopCnt; // current loop count
  2394. bool doneFl; // the wave table source is exhausted
  2395. cmAudioFileH_t afH; // current audio file handle
  2396. int nxtBegSmpIdx; // the beg/end sample index to use with the next filename to arrive at port 'fn'
  2397. int nxtEndSmpIdx; //
  2398. cmThreadH_t thH;
  2399. bool loadFileFl;
  2400. cmDspCtx_t* ctx;
  2401. cmSample_t phsOffs;
  2402. cmSample_t phsLast;
  2403. unsigned onSymId;
  2404. unsigned offSymId;
  2405. unsigned doneSymId;
  2406. bool useThreadFl;
  2407. unsigned minAfIndexRptCnt; // min count of audio samples between transmitting the current audio file index
  2408. unsigned afIndexRptCnt; // current audio file sample index count
  2409. } cmDspWaveTable_t;
  2410. bool _cmDspWaveTableThreadFunc( void* param);
  2411. cmDspInst_t* _cmDspWaveTableAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2412. {
  2413. cmDspVarArg_t args[] =
  2414. {
  2415. { "len", kLenWtId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Wave table length in samples" },
  2416. { "shape", kShapeWtId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Wave shape 0=silent 1=file 2=sine 3=white" },
  2417. { "fn", kFnWtId, 0, 0, kInDsvFl | kStrzDsvFl | kOptArgDsvFl, "Optional audio file name" },
  2418. { "loop", kLoopWtId, 0, 0, kInDsvFl | kIntDsvFl | kOptArgDsvFl, "-1=loop forever >0=loop count (dflt:-1)"},
  2419. { "beg", kBegWtId, 0, 0, kInDsvFl | kIntDsvFl | kOptArgDsvFl, "File begin sample index" },
  2420. { "end", kEndWtId, 0, 0, kInDsvFl | kIntDsvFl | kOptArgDsvFl, "File end sample index (-1=play all)" },
  2421. { "cmd", kCmdWtId, 0, 0, kInDsvFl | kSymDsvFl | kOptArgDsvFl, "Command: on off"},
  2422. { "ot", kOtWtId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Overtone count"},
  2423. { "gain", kGainWtId, 0, 0, kInDsvFl | kDoubleDsvFl|kOptArgDsvFl, "Gain"},
  2424. { "phs", kPhsWtId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Driving phase" },
  2425. { "out", kOutWtId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output" },
  2426. { "cnt", kCntWtId, 0, 0, kOutDsvFl | kIntDsvFl, "Loop count event."},
  2427. { "fidx", kFIdxWtId, 0, 0, kOutDsvFl | kUIntDsvFl, "Current audio file index."},
  2428. { "done", kDoneWtId, 0, 0, kOutDsvFl | kSymDsvFl, "'done' sent after last loop."},
  2429. { NULL, 0, 0, 0, 0 }
  2430. };
  2431. cmDspWaveTable_t* p = cmDspInstAlloc(cmDspWaveTable_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  2432. // get the filename given in the va_list (or NULL if no filename was given)
  2433. const cmChar_t* fn = cmDspDefaultStrcz(&p->inst,kFnWtId);
  2434. p->offSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"off");
  2435. p->onSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"on");
  2436. p->doneSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"done");
  2437. double adCurFileIdxRptPeriodMs = 100.0;
  2438. p->minAfIndexRptCnt = floor(adCurFileIdxRptPeriodMs * cmDspSampleRate(ctx) / 1000.0);
  2439. cmDspSetDefaultUInt( ctx, &p->inst, kLenWtId, 0, cmDspSampleRate(ctx));
  2440. cmDspSetDefaultUInt( ctx, &p->inst, kShapeWtId, 0, kSilenceWtId );
  2441. cmDspSetDefaultStrcz( ctx, &p->inst, kFnWtId, NULL, fn );
  2442. cmDspSetDefaultInt( ctx, &p->inst, kLoopWtId, 0, -1 );
  2443. cmDspSetDefaultInt( ctx, &p->inst, kBegWtId, 0, 0 );
  2444. cmDspSetDefaultInt( ctx, &p->inst, kEndWtId, 0, -1 );
  2445. cmDspSetDefaultSymbol(ctx, &p->inst, kCmdWtId, p->onSymId );
  2446. cmDspSetDefaultUInt( ctx, &p->inst, kOtWtId, 0, 5 );
  2447. cmDspSetDefaultDouble(ctx, &p->inst, kGainWtId, 0, 1.0 );
  2448. cmDspSetDefaultUInt( ctx, &p->inst, kFIdxWtId, 0, 0 );
  2449. p->useThreadFl = false;
  2450. return &p->inst;
  2451. }
  2452. cmDspRC_t _cmDspWaveTableFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2453. {
  2454. cmDspWaveTable_t* p = (cmDspWaveTable_t*)inst;
  2455. if( cmThreadIsValid(p->thH) )
  2456. if( cmThreadDestroy(&p->thH) != kOkThRC )
  2457. cmDspInstErr(ctx,inst,kThreadFailDspRC,"The wavetable file reader thread failed to close.");
  2458. if( cmAudioFileIsValid(p->afH) )
  2459. cmAudioFileDelete(&p->afH);
  2460. cmLhFree(ctx->lhH,p->wt);
  2461. return kOkDspRC;
  2462. }
  2463. // Read the next block of 'rdSmpCnt' samples starting at the absolute file index 'p->fi'
  2464. // into wt[rdSmpCnt].
  2465. // If the end of the file segment marked by absolute file indexes 'begSmpIdx' to 'endSmpIdx'
  2466. // is encountered in the middle of the requested block and the wave table is in loop
  2467. // mode then the the function will automatically begin reading from the begining of the
  2468. // file segment. If the end of the file segment is encountered and the wave table is not
  2469. // in loop mode then the empty portion of wt[] will be set to zero.
  2470. cmDspRC_t _cmDspWaveTableReadBlock( cmDspCtx_t* ctx, cmDspWaveTable_t* p, cmSample_t* wt, unsigned rdSmpCnt, int begSmpIdx, int endSmpIdx, int maxLoopCnt )
  2471. {
  2472. unsigned actFrmCnt = 0;
  2473. unsigned chIdx = 0;
  2474. unsigned chCnt = 1;
  2475. unsigned fn = endSmpIdx - p->fi + 1; // count of samples between p->fi and endSmpIdx
  2476. unsigned n0 = rdSmpCnt;
  2477. unsigned n1 = 0;
  2478. // if the requested sample count will go past the end of the file segment
  2479. if( rdSmpCnt > fn )
  2480. {
  2481. n1 = rdSmpCnt - fn;
  2482. n0 = rdSmpCnt - n1;
  2483. }
  2484. // if we don't have a valid file yet - then
  2485. if( cmAudioFileIsValid(p->afH) == false )
  2486. {
  2487. cmVOS_Zero(wt,n0);
  2488. return kOkDspRC;
  2489. }
  2490. // read the first block of samples
  2491. if( cmAudioFileReadSample(p->afH, n0, chIdx, chCnt, &wt, &actFrmCnt ) != kOkAfRC )
  2492. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"An error occured while reading the wave table file.");
  2493. // BUG BUG BUG
  2494. // This assertion will fail if the file beg/end markers are not legal given the file length.
  2495. // An error msg would be more appropriate.
  2496. assert( actFrmCnt == n0 );
  2497. // increment the wave table pointer
  2498. wt += n0;
  2499. p->fi += n0;
  2500. // if n1 != 0 then we have encountered the end of the file segment
  2501. if( n1 > 0 )
  2502. {
  2503. ++p->loopCnt;
  2504. // send the loop count event
  2505. cmDspSetInt(ctx,&p->inst,kCntWtId,p->loopCnt);
  2506. // if we have played all the requested loops
  2507. if( maxLoopCnt != -1 && p->loopCnt >= maxLoopCnt )
  2508. {
  2509. p->doneFl = true;
  2510. cmVOS_Zero(wt,n1); // zero to the end of the buffer
  2511. p->cfn = p->cfi + cmDspUInt((cmDspInst_t*)p,kLenWtId) - p->wtn - n0;
  2512. assert( p->cfn >= p->cfi );
  2513. }
  2514. else
  2515. {
  2516. // seek to the first sample indicated by the 'beg' variable
  2517. if( cmAudioFileSeek(p->afH,begSmpIdx) != kOkAfRC )
  2518. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"Seeking failed on the wave table file.",fn);
  2519. // read the second block of samples
  2520. if( cmAudioFileReadSample(p->afH, n1, chIdx, chCnt, &wt, &actFrmCnt ) != kOkAfRC )
  2521. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"An error occured while reading the wave table file.");
  2522. assert( actFrmCnt == n1 );
  2523. // reset the file index tracker
  2524. p->fi = begSmpIdx + n1;
  2525. p->cfi = begSmpIdx;
  2526. }
  2527. }
  2528. return kOkDspRC;
  2529. }
  2530. cmDspRC_t _cmDspWaveTableReadAudioFile( cmDspCtx_t* ctx, cmDspWaveTable_t* p, unsigned wtSmpCnt, unsigned rdSmpCnt )
  2531. {
  2532. unsigned n0 = rdSmpCnt;
  2533. unsigned n1 = 0;
  2534. int begSmpIdx = cmDspInt(&p->inst,kBegWtId);
  2535. int endSmpIdx = cmDspInt(&p->inst,kEndWtId);
  2536. int maxLoopCnt= cmDspInt(&p->inst,kLoopWtId);
  2537. if( endSmpIdx < begSmpIdx )
  2538. endSmpIdx = p->fn-1;
  2539. // if this read will wrap to the beginning of the wave table
  2540. if( p->wti + rdSmpCnt > wtSmpCnt )
  2541. {
  2542. n0 = wtSmpCnt - p->wti; // count of samples to read into the end of p->wt[]
  2543. n1 = rdSmpCnt - n0; // count of samples to read into the beg of p->wt[]
  2544. }
  2545. assert(n1<wtSmpCnt);
  2546. // the first read always starts at p->wt + p->wti
  2547. if( p->doneFl )
  2548. cmVOS_Zero(p->wt + p->wti,n0);
  2549. else
  2550. if( _cmDspWaveTableReadBlock(ctx, p, p->wt+p->wti, n0,begSmpIdx,endSmpIdx,maxLoopCnt ) != kOkDspRC )
  2551. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"An error occured while reading the wave table file.");
  2552. p->wtn -= n0; // decrease the count of available samples
  2553. p->wti += n0;
  2554. if( n1 > 0 )
  2555. {
  2556. // the second read always starts at the beginning of the wave table
  2557. if( p->doneFl )
  2558. cmVOS_Zero(p->wt,n1);
  2559. else
  2560. if( _cmDspWaveTableReadBlock(ctx, p, p->wt, n1,begSmpIdx,endSmpIdx,maxLoopCnt ) != kOkDspRC )
  2561. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"An error occured while reading the wave table file.");
  2562. p->wtn -= n1; // decrease the count of available samples
  2563. p->wti = n1;
  2564. }
  2565. //p->wtn -= rdSmpCnt; // decrease the count of available samples
  2566. return kOkDspRC;
  2567. }
  2568. cmDspRC_t _cmDspWaveTableInitAudioFile( cmDspCtx_t* ctx, cmDspWaveTable_t* p )
  2569. {
  2570. cmDspRC_t rc = kOkDspRC;
  2571. cmAudioFileH_t afH;
  2572. cmRC_t afRC;
  2573. cmAudioFileInfo_t afInfo;
  2574. const cmChar_t* fn = cmDspStrcz(&p->inst,kFnWtId);
  2575. unsigned wtSmpCnt = cmDspUInt(&p->inst,kLenWtId);
  2576. int begSmpIdx= cmDspInt(&p->inst,kBegWtId);
  2577. // if the file name is valid
  2578. if( fn == NULL || strlen(fn)==0 )
  2579. {
  2580. rc = cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"Audio file loading was requested for the wave table but no file name was given.");
  2581. goto errLabel;
  2582. }
  2583. // open the audio file
  2584. afH = cmAudioFileNewOpen(fn,&afInfo,&afRC,ctx->rpt);
  2585. // check for file open errors
  2586. if( afRC != kOkAfRC )
  2587. {
  2588. rc = cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"The audio file '%s' could not be opened. ('%s').",fn,cmAudioFileErrorMsg(afRC));
  2589. goto errLabel;
  2590. }
  2591. // if the file opened but is invalid
  2592. if( cmAudioFileIsValid(p->afH) )
  2593. cmAudioFileDelete(&p->afH);
  2594. // seek to the first sample indicated by the 'beg' variable
  2595. if( cmAudioFileSeek(afH,begSmpIdx) != kOkAfRC )
  2596. {
  2597. rc = cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"Seeking failed on the audio file '%s'.",fn);
  2598. goto errLabel;
  2599. }
  2600. p->afH = afH;
  2601. p->fi = begSmpIdx;
  2602. p->cfi = begSmpIdx;
  2603. p->fn = afInfo.frameCnt;
  2604. p->wti = 0;
  2605. p->wtn = wtSmpCnt;
  2606. // read the first block of samples
  2607. if((rc= _cmDspWaveTableReadAudioFile(ctx,p,wtSmpCnt,wtSmpCnt))!= kOkDspRC )
  2608. goto errLabel;
  2609. //printf("Wt:%s %i %i\n",fn,begSmpIdx,cmDspInt(&p->inst,kEndWtId));
  2610. // set the shape param to kFileWtId
  2611. //if((rc= cmDspSetUInt(ctx,&p->inst,kShapeWtId,kFileWtId)) != kOkDspRC )
  2612. // goto errLabel;
  2613. errLabel:
  2614. if( rc != kOkDspRC )
  2615. cmDspSetUInt(ctx,&p->inst,kShapeWtId,kSilenceWtId);
  2616. return rc;
  2617. }
  2618. bool _cmDspWaveTableThreadFunc( void* param)
  2619. {
  2620. cmDspWaveTable_t* p = (cmDspWaveTable_t*)param;
  2621. if( p->loadFileFl )
  2622. {
  2623. p->loadFileFl = false;
  2624. if( _cmDspWaveTableInitAudioFile(p->ctx,p) == kOkDspRC )
  2625. {
  2626. p->phsOffs = p->phsLast;
  2627. cmDspSetUInt(p->ctx,&p->inst,kShapeWtId,kFileWtId);
  2628. }
  2629. cmThreadPause(p->thH,kPauseThFl);
  2630. }
  2631. return true;
  2632. }
  2633. // Files are loaded via a background thread.
  2634. cmDspRC_t _cmDspWaveTableStartFileLoadThread( cmDspCtx_t* ctx, cmDspWaveTable_t* p, const cmChar_t* fn )
  2635. {
  2636. cmDspRC_t rc = kOkDspRC;
  2637. if( fn == NULL )
  2638. return rc;
  2639. if( p->loadFileFl )
  2640. 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));
  2641. if(p->useThreadFl && cmThreadIsValid(p->thH) == false)
  2642. cmThreadCreate(&p->thH,_cmDspWaveTableThreadFunc,p,ctx->rpt);
  2643. if(p->useThreadFl && cmThreadIsValid(p->thH) == false )
  2644. return cmDspInstErr(ctx,&p->inst,kInvalidStateDspRC,"The audio file '%s' was not loaded because the audio load thread is invalid.",cmStringNullGuard(fn));
  2645. p->loadFileFl = true;
  2646. p->ctx = ctx;
  2647. cmDspSetUInt(ctx,&p->inst,kShapeWtId,kSilenceWtId);
  2648. cmDspSetStrcz(ctx,&p->inst,kFnWtId,fn);
  2649. if( p->useThreadFl == false )
  2650. {
  2651. // use non-threaded load
  2652. if((rc = _cmDspWaveTableInitAudioFile(p->ctx,p)) != kOkDspRC )
  2653. return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"The audio file '%s' was not loaded.",cmStringNullGuard(fn));
  2654. p->phsOffs = p->phsLast;
  2655. cmDspSetUInt(p->ctx,&p->inst,kShapeWtId,kFileWtId);
  2656. p->loadFileFl = false;
  2657. }
  2658. else
  2659. {
  2660. // use threaded load - this works but it isn't really thread safe
  2661. if( cmThreadPause(p->thH,0) != kOkThRC )
  2662. return cmDspInstErr(ctx,&p->inst,kThreadFailDspRC,"The audio file '%s' was not loaded because audio thread enable failed.",cmStringNullGuard(fn));
  2663. }
  2664. return rc;
  2665. }
  2666. // This function is called whenever the source mode variable changes (or a new file name arrives)
  2667. cmDspRC_t _cmDspWaveTableCreateTable( cmDspCtx_t* ctx, cmDspWaveTable_t* p )
  2668. {
  2669. cmDspRC_t rc = kOkDspRC;
  2670. unsigned shapeId = cmDspUInt(&p->inst,kShapeWtId);
  2671. unsigned wtSmpCnt = cmDspUInt(&p->inst,kLenWtId);
  2672. unsigned otCnt = cmDspUInt(&p->inst,kOtWtId);
  2673. cmSample_t gain = 0.9;
  2674. double hz = 1.0;
  2675. double sr = cmDspSampleRate(ctx);
  2676. assert( wtSmpCnt > 0 );
  2677. if( p->wt == NULL )
  2678. p->wt = cmLhResizeNZ(ctx->lhH,cmSample_t,p->wt,wtSmpCnt);
  2679. else
  2680. cmVOS_Zero(p->wt,wtSmpCnt);
  2681. p->wtn = wtSmpCnt; // all samples in the wt are avail for filling
  2682. p->wti = 0; // beginning with the first sample
  2683. p->loopCnt = 0; // we are starting from a new source so set the loop cnt to 0
  2684. p->doneFl = false; // and the doneFl to false
  2685. assert( p->wt != NULL );
  2686. switch( shapeId )
  2687. {
  2688. case kSilenceWtId:
  2689. break;
  2690. case kFileWtId:
  2691. printf("Loading:%i %i %s\n",p->nxtBegSmpIdx,p->nxtEndSmpIdx,cmDspStrcz(&p->inst,kFnWtId));
  2692. rc = _cmDspWaveTableStartFileLoadThread(ctx,p,cmDspStrcz(&p->inst,kFnWtId));
  2693. break;
  2694. case kWhiteWtId:
  2695. cmVOS_Random(p->wt,wtSmpCnt,-gain,gain);
  2696. break;
  2697. case kPinkWtId:
  2698. cmVOS_SynthPinkNoise(p->wt,wtSmpCnt,0.0);
  2699. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2700. break;
  2701. case kSineWtId:
  2702. cmVOS_SynthSine(p->wt,wtSmpCnt,0,sr,hz);
  2703. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2704. break;
  2705. case kCosWtId:
  2706. cmVOS_SynthCosine(p->wt,wtSmpCnt,0,sr,hz);
  2707. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2708. break;
  2709. case kSawWtId:
  2710. cmVOS_SynthSawtooth(p->wt,wtSmpCnt,0,sr,hz,otCnt);
  2711. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2712. break;
  2713. case kSqrWtId:
  2714. cmVOS_SynthSquare( p->wt,wtSmpCnt,0,sr,hz,otCnt );
  2715. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2716. break;
  2717. case kTriWtId:
  2718. cmVOS_SynthTriangle( p->wt,wtSmpCnt,0,sr,hz,otCnt );
  2719. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2720. break;
  2721. case kPulseWtId:
  2722. cmVOS_SynthPulseCos( p->wt,wtSmpCnt,0,sr,hz,otCnt );
  2723. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2724. break;
  2725. case kPhasorWtId:
  2726. cmVOS_SynthPhasor( p->wt,wtSmpCnt,0,sr,hz );
  2727. cmVOS_MultVS(p->wt,wtSmpCnt,gain);
  2728. break;
  2729. }
  2730. return rc;
  2731. }
  2732. cmDspRC_t _cmDspWaveTableReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2733. {
  2734. cmDspWaveTable_t* p = (cmDspWaveTable_t*)inst;
  2735. cmDspApplyAllDefaults(ctx,inst);
  2736. cmDspZeroAudioBuf(ctx,inst,kOutWtId);
  2737. p->nxtBegSmpIdx = cmDspInt(&p->inst,kBegWtId);
  2738. p->nxtEndSmpIdx = cmDspInt(&p->inst,kEndWtId);
  2739. return _cmDspWaveTableCreateTable(ctx,p);
  2740. }
  2741. cmDspRC_t _cmDspWaveTableExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2742. {
  2743. cmDspRC_t rc = kOkDspRC;
  2744. const cmSample_t* phsV = cmDspAudioBuf(ctx,inst,kPhsWtId,0);
  2745. if( phsV == NULL )
  2746. {
  2747. inst->execFunc = NULL; // disable this instance because it has no input
  2748. return kOkDspRC;
  2749. }
  2750. cmDspWaveTable_t* p = (cmDspWaveTable_t*)inst;
  2751. unsigned mode = cmDspSymbol(inst,kCmdWtId);
  2752. unsigned srcId = cmDspUInt(inst,kShapeWtId);
  2753. if( mode == p->offSymId || srcId == kSilenceWtId )
  2754. {
  2755. cmDspZeroAudioBuf(ctx,inst,kOutWtId);
  2756. return kOkDspRC;
  2757. }
  2758. cmSample_t* outV = cmDspAudioBuf(ctx,inst,kOutWtId,0);
  2759. unsigned outCnt = cmDspVarRows(inst,kOutWtId);
  2760. unsigned wtSmpCnt = cmDspUInt(inst,kLenWtId);
  2761. double gain = cmDspDouble(inst,kGainWtId);
  2762. unsigned i;
  2763. // for each output sample
  2764. for(i=0; i<outCnt; ++i)
  2765. {
  2766. // get the wave table location
  2767. //unsigned x = fmodf(phsV[i] - p->phsOffs,wtSmpCnt);
  2768. unsigned x = fmodf(phsV[i],wtSmpCnt);
  2769. // if the wt loctn is passed the end of the table
  2770. /*
  2771. if( x >= wtSmpCnt )
  2772. {
  2773. offs += wtSmpCnt;
  2774. x -= wtSmpCnt;
  2775. }
  2776. */
  2777. outV[i] = gain * p->wt[x];
  2778. }
  2779. p->phsLast = phsV[outCnt-1];
  2780. // if we are reading from a file ...
  2781. if( srcId == kFileWtId )
  2782. {
  2783. unsigned rdSmpCnt = 8192; // file read block sample count
  2784. p->wtn += outCnt;
  2785. // ... and there are rdSmpCnt avail locations in the wave table
  2786. if( p->wtn >= rdSmpCnt )
  2787. rc = _cmDspWaveTableReadAudioFile(ctx, p, wtSmpCnt, rdSmpCnt );
  2788. // send the current audio file index
  2789. if( p->doneFl && p->cfi < p->cfn && p->cfn <= (p->cfi + outCnt) )
  2790. {
  2791. cmDspSetUInt(ctx,inst,kFIdxWtId,p->cfn);
  2792. cmDspSetSymbol(ctx,inst,kDoneWtId,p->doneSymId);
  2793. }
  2794. else
  2795. {
  2796. if( p->afIndexRptCnt >= p->minAfIndexRptCnt )
  2797. {
  2798. p->afIndexRptCnt -= p->minAfIndexRptCnt;
  2799. cmDspSetUInt(ctx,inst,kFIdxWtId,p->cfi);
  2800. }
  2801. }
  2802. p->afIndexRptCnt += outCnt;
  2803. p->cfi += outCnt;
  2804. }
  2805. return rc;
  2806. }
  2807. cmDspRC_t _cmDspWaveTableRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2808. {
  2809. cmDspRC_t rc = kOkDspRC;
  2810. cmDspWaveTable_t* p = (cmDspWaveTable_t*)inst;
  2811. switch( evt->dstVarId )
  2812. {
  2813. case kFnWtId: // a new file name arrived - this automatically switches the source mode to kFileWtId
  2814. {
  2815. const cmChar_t* fn = cmDsvStrz(evt->valuePtr);
  2816. if( cmFsIsFile( fn )==false )
  2817. cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"'%s' does not exist.",cmStringNullGuard(fn));
  2818. else
  2819. {
  2820. cmDspSetEvent(ctx,inst,evt); // set the file name variable
  2821. cmDspSetInt(ctx,inst,kBegWtId,p->nxtBegSmpIdx); // set the beg/end smp idx var's from the stored nxtBeg/EndSmpIdx values
  2822. cmDspSetInt(ctx,inst,kEndWtId,p->nxtEndSmpIdx); //
  2823. cmDspSetUInt(ctx,inst,kShapeWtId,kFileWtId); // switch to file mode
  2824. rc = _cmDspWaveTableCreateTable(ctx,p); // reload the wavetable
  2825. }
  2826. }
  2827. break;
  2828. case kBegWtId:
  2829. // store for next incoming file name msg
  2830. p->nxtBegSmpIdx = cmDsvGetInt(evt->valuePtr);
  2831. break;
  2832. case kEndWtId:
  2833. // store for next incoming file name msg
  2834. p->nxtEndSmpIdx = cmDsvGetInt(evt->valuePtr);
  2835. break;
  2836. case kShapeWtId:
  2837. if( cmDsvGetUInt(evt->valuePtr) < kShapeWtCnt )
  2838. {
  2839. cmDspSetEvent(ctx,inst,evt); // switch modes
  2840. rc = _cmDspWaveTableCreateTable(ctx,p); // reload the wavetable
  2841. }
  2842. break;
  2843. case kLenWtId: // we don't support table size changes
  2844. break;
  2845. case kPhsWtId:
  2846. break;
  2847. case kCmdWtId:
  2848. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  2849. {
  2850. if( cmDspSymbol(inst,kCmdWtId) == p->onSymId )
  2851. {
  2852. //rc = _cmDspWaveTableReset(ctx,inst, evt );
  2853. rc = _cmDspWaveTableCreateTable(ctx,p);
  2854. cmDspSetSymbol(ctx,inst,kCmdWtId,p->onSymId);
  2855. p->phsOffs = 0;
  2856. p->phsLast = 0;
  2857. }
  2858. }
  2859. break;
  2860. case kOtWtId:
  2861. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
  2862. rc = _cmDspWaveTableCreateTable(ctx,p); // reload the wavetable
  2863. break;
  2864. case kGainWtId:
  2865. rc = cmDspSetEvent(ctx,inst,evt);
  2866. break;
  2867. default:
  2868. { assert(0); }
  2869. }
  2870. return rc;
  2871. }
  2872. struct cmDspClass_str* cmWaveTableClassCons( cmDspCtx_t* ctx )
  2873. {
  2874. cmDspClassSetup(&_cmWaveTableDC,ctx,"WaveTable",
  2875. NULL,
  2876. _cmDspWaveTableAlloc,
  2877. _cmDspWaveTableFree,
  2878. _cmDspWaveTableReset,
  2879. _cmDspWaveTableExec,
  2880. _cmDspWaveTableRecv,
  2881. NULL,
  2882. NULL,
  2883. "Variable frequency and waveshape signal generator." );
  2884. return &_cmWaveTableDC;
  2885. }
  2886. //==========================================================================================================================================
  2887. enum
  2888. {
  2889. kFmtSpId,
  2890. kOutSpId,
  2891. kInSpId,
  2892. kSprintfLabelCharCnt = 15,
  2893. kSprintfDocCharCnt = 31,
  2894. kSprintfBufCharCnt = 1023
  2895. };
  2896. cmDspClass_t _cmSprintfDC;
  2897. typedef struct
  2898. {
  2899. unsigned flags; // dsv type id for this fmt conversion or 0 if it is a literal string
  2900. unsigned fsi; // index into the fmt string where the fmt starts (always points to an '%' char)
  2901. unsigned fsn; // length of the format string
  2902. cmChar_t label[ kSprintfLabelCharCnt+1 ];
  2903. cmChar_t doc[ kSprintfDocCharCnt+1 ];
  2904. } cmDspSprintfFmt_t;
  2905. typedef struct
  2906. {
  2907. cmDspInst_t inst;
  2908. cmChar_t buf[ kSprintfBufCharCnt+1]; // output string buffer
  2909. unsigned inCnt; // count conversion spec's in the fmtStr[]
  2910. cmDspSprintfFmt_t* fmtArray; // fmtArray[inCnt]
  2911. cmChar_t* fmtStr; // C-style printf format string.
  2912. } cmDspSprintf_t;
  2913. cmDspRC_t _cmSprintfGetInputCnt( cmDspCtx_t* ctx, cmDspClass_t* classPtr, const cmChar_t* fmt, unsigned* inCntPtr )
  2914. {
  2915. unsigned i,n;
  2916. unsigned inCnt = 0;
  2917. if( fmt== NULL || (n=strlen(fmt))==0 )
  2918. return cmErrMsg(&classPtr->err,kInvalidArgDspRC,"Empty format string.");
  2919. for(i=0; i<n; ++i)
  2920. {
  2921. // handle the escape character
  2922. if( fmt[i] == '\\' )
  2923. ++i; // for now we will just skip the next character
  2924. else
  2925. if( fmt[i] == '%' )
  2926. ++inCnt;
  2927. }
  2928. *inCntPtr = inCnt;
  2929. return kOkDspRC;
  2930. }
  2931. cmDspRC_t _cmSprintfGetInputTypes( cmDspCtx_t* ctx, cmDspClass_t* classPtr, const cmChar_t* fmt, cmDspSprintfFmt_t fmtArray[], unsigned inCnt )
  2932. {
  2933. unsigned i,j,n;
  2934. if( fmt== NULL || (n=strlen(fmt))==0 )
  2935. return cmErrMsg(&classPtr->err,kInvalidArgDspRC,"Empty format string.");
  2936. n = strlen(fmt);
  2937. for(i=0,j=0; i<n; ++i)
  2938. {
  2939. // handle the escape character
  2940. if( fmt[i] == '\\' )
  2941. ++i; // for now we will just skip the next character
  2942. else
  2943. if( fmt[i] == '%' )
  2944. {
  2945. unsigned fn;
  2946. if((fn = strcspn(fmt+i,"diouxXfeEgGcs")) == 0 )
  2947. return cmErrMsg(&classPtr->err,kInvalidArgDspRC,"Invalid format string on input conversion at index:%i.",j);
  2948. ++fn;
  2949. fmtArray[j].fsi = i;
  2950. fmtArray[j].fsn = fn;
  2951. snprintf(fmtArray[j].label,kSprintfLabelCharCnt,"in-%i",j);
  2952. fmtArray[j].label[kSprintfLabelCharCnt]=0;
  2953. fmtArray[j].doc[kSprintfDocCharCnt] = 0;
  2954. switch( fmt[ i + fn - 1 ] )
  2955. {
  2956. case 'd':
  2957. case 'i':
  2958. fmtArray[j].flags = kIntDsvFl;
  2959. snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"Integer input %i.",j);
  2960. break;
  2961. case 'o':
  2962. case 'u':
  2963. case 'x':
  2964. case 'X':
  2965. fmtArray[j].flags = kUIntDsvFl;
  2966. snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"Unsigned input %i.",j);
  2967. break;
  2968. case 'f':
  2969. case 'e':
  2970. case 'E':
  2971. case 'g':
  2972. case 'G':
  2973. fmtArray[j].flags = kDoubleDsvFl;
  2974. snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"Double input %i.",j);
  2975. break;
  2976. case 'c':
  2977. fmtArray[j].flags = kUCharDsvFl;
  2978. snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"Unsigned char input %i.",j);
  2979. break;
  2980. case 's':
  2981. fmtArray[j].flags = kStrzDsvFl | kSymDsvFl;
  2982. snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"String input %i.",j);
  2983. break;
  2984. default:
  2985. { assert(0); }
  2986. }
  2987. i += fn - 1;
  2988. ++j;
  2989. }
  2990. }
  2991. return kOkDspRC;
  2992. }
  2993. 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 )
  2994. {
  2995. cmDspRC_t rc;
  2996. unsigned i,j;
  2997. va_list vl2;
  2998. unsigned inCnt = 0;
  2999. unsigned fixedArgCnt = 0;
  3000. cmDspSprintf_t* p = NULL;
  3001. const cmChar_t* fmt = NULL;
  3002. cmDspSprintfFmt_t* fmtArray = NULL;
  3003. va_copy(vl2,vl);
  3004. *pp = NULL;
  3005. if( va_cnt > 0 )
  3006. fmt = va_arg(vl,const char*);
  3007. if( va_cnt < 1 || fmt == NULL )
  3008. return cmErrMsg(&classPtr->err,kVarArgParseFailDspRC,"Expected the format string as the first argument.");
  3009. // calc the number of fixed args
  3010. for(i=0; args[i].label != NULL; ++i)
  3011. ++fixedArgCnt;
  3012. // calc the count of input args
  3013. if((rc = _cmSprintfGetInputCnt(ctx, classPtr, fmt, &inCnt)) != kOkDspRC )
  3014. return rc;
  3015. if( inCnt > 0 )
  3016. {
  3017. fmtArray = cmLhAllocZ( ctx->lhH, cmDspSprintfFmt_t, inCnt );
  3018. rc = _cmSprintfGetInputTypes(ctx, classPtr, fmt, fmtArray, inCnt );
  3019. }
  3020. if( rc == kOkDspRC )
  3021. {
  3022. unsigned argCnt = fixedArgCnt + inCnt;
  3023. cmDspVarArg_t argArray[ argCnt+1 ];
  3024. // copy in fixed args
  3025. for(i=0; i<fixedArgCnt; ++i)
  3026. argArray[i] = args[i];
  3027. // calc input args
  3028. for(j=0; i<argCnt; ++i,++j)
  3029. {
  3030. argArray[i].label = fmtArray[j].label;
  3031. argArray[i].constId = i;
  3032. argArray[i].rn = 0;
  3033. argArray[i].cn = 0;
  3034. argArray[i].flags = kInDsvFl | fmtArray[j].flags;
  3035. argArray[i].doc = fmtArray[j].doc;
  3036. }
  3037. // set the sentinel arg to all zeros
  3038. memset(argArray + argCnt,0,sizeof(cmDspVarArg_t));
  3039. if((p = cmDspInstAlloc(cmDspSprintf_t,ctx,classPtr,argArray,instSymId,id,storeSymId,va_cnt,vl2)) != NULL )
  3040. {
  3041. // make a copy of the format string - we need to be sure that it is in
  3042. // r/w memory in order that _cmDspSprintfGenString() can write to it
  3043. p->fmtStr = cmLhResizeN(ctx->lhH,cmChar_t,p->fmtStr,strlen(fmt)+1);
  3044. strcpy(p->fmtStr,fmt);
  3045. cmLhFree(ctx->lhH,p->fmtArray);
  3046. p->fmtArray = fmtArray;
  3047. p->inCnt = inCnt;
  3048. memset(p->buf,0,kSprintfBufCharCnt+1);
  3049. }
  3050. *pp = p;
  3051. }
  3052. if( cmErrLastRC(&classPtr->err) != kOkDspRC )
  3053. cmLhFree(ctx->lhH,fmtArray);
  3054. return rc;
  3055. }
  3056. cmDspRC_t _cmDspSprintfGenString(cmDspCtx_t* ctx, cmDspSprintf_t* p )
  3057. {
  3058. cmDspRC_t rc = kOkDspRC;
  3059. unsigned fsi = 0; // format string index
  3060. unsigned i = 0; // fmtArray[] index
  3061. unsigned bi = 0; // string buffer index
  3062. unsigned bn = kSprintfBufCharCnt; // available char's in the string buffer
  3063. cmChar_t* fmt = p->fmtStr;
  3064. unsigned fn = strlen(fmt)+1;
  3065. // for each
  3066. for(i=0; i<p->inCnt && bn>0; ++i)
  3067. {
  3068. const cmDspSprintfFmt_t* f = p->fmtArray + i;
  3069. unsigned varId = kInSpId + i;
  3070. const cmDspVar_t* varPtr = cmDspVarIdToCPtr(&p->inst, varId);
  3071. assert(varPtr != NULL);
  3072. // if there are literal char's to copy prior to the format
  3073. if( fsi < f->fsi )
  3074. {
  3075. unsigned cn = cmMin(f->fsi-fsi,bn);
  3076. strncpy(p->buf+bi,fmt+fsi,cn);
  3077. bn -= cn;
  3078. fsi += cn;
  3079. bi += cn;
  3080. }
  3081. if( bn == 0 )
  3082. {
  3083. rc = cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The internal string buffer is too small.");
  3084. break;
  3085. }
  3086. unsigned pn = 0;
  3087. char c = fmt[ f->fsi + f->fsn ];
  3088. // zero terminate the format string for this input
  3089. fmt[ f->fsi + f->fsn] = 0;
  3090. // if the conversion fmt is for a string then the kSymDsvFl will be set
  3091. // which will prevent the switch() from working - so clear the sym flag here.
  3092. unsigned flags = cmClrFlag(f->flags,kSymDsvFl);
  3093. switch(flags)
  3094. {
  3095. case kUCharDsvFl:
  3096. // not implemented - need to implment a uchar variable type or
  3097. // assume a one character strz.
  3098. assert(0);
  3099. break;
  3100. case kIntDsvFl:
  3101. pn = snprintf(p->buf+bi,bn,fmt + fsi, cmDspInt(&p->inst,varId));
  3102. break;
  3103. case kUIntDsvFl:
  3104. pn = snprintf(p->buf+bi,bn,fmt + fsi, cmDspUInt(&p->inst,varId));
  3105. break;
  3106. case kDoubleDsvFl:
  3107. pn = snprintf(p->buf+bi,bn,fmt + fsi, cmDspDouble(&p->inst,varId));
  3108. break;
  3109. case kStrzDsvFl:
  3110. if( cmDspIsSymbol(&p->inst,varId) )
  3111. pn = snprintf(p->buf+bi,bn,fmt + fsi, cmStringNullGuard(cmSymTblLabel(ctx->stH,cmDspSymbol(&p->inst,varId))));
  3112. else
  3113. pn = snprintf(p->buf+bi,bn,fmt + fsi, cmDspStrcz(&p->inst,varId));
  3114. break;
  3115. default:
  3116. { assert(0); }
  3117. }
  3118. // restore the char written over by the termination zero
  3119. fmt[ f->fsi + f->fsn] = c;
  3120. assert(pn<=bn);
  3121. bn -= pn;
  3122. bi += pn;
  3123. fsi += f->fsn;
  3124. }
  3125. // if there is literal text in the format string after the last conversion spec.
  3126. if( fsi < fn )
  3127. {
  3128. unsigned cn = cmMin(fn-fsi,bn);
  3129. strncpy(p->buf+bi,fmt+fsi,cn);
  3130. }
  3131. return rc;
  3132. }
  3133. cmDspInst_t* _cmDspSprintfAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3134. {
  3135. cmDspVarArg_t args[] =
  3136. {
  3137. { "fmt", kFmtSpId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Format string" },
  3138. { "out", kOutSpId, 0, 0, kOutDsvFl | kStrzDsvFl, "Output string" },
  3139. { NULL, 0, 0, 0, 0 }
  3140. };
  3141. cmDspSprintf_t* p = NULL;
  3142. if( _cmSprintfLoadFormat(&p, ctx, classPtr, instSymId, id, storeSymId, args, va_cnt, vl ) == kOkDspRC )
  3143. return &p->inst;
  3144. return NULL;
  3145. }
  3146. cmDspRC_t _cmDspSprintfFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3147. {
  3148. cmDspSprintf_t* p = (cmDspSprintf_t*)inst;
  3149. cmLhFree(ctx->lhH,p->fmtArray);
  3150. cmLhFree(ctx->lhH,p->fmtStr);
  3151. p->fmtArray=NULL;
  3152. return kOkDspRC;
  3153. }
  3154. cmDspRC_t _cmDspSprintfReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3155. {
  3156. return kOkDspRC;
  3157. }
  3158. cmDspRC_t _cmDspSprintfRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3159. {
  3160. cmDspRC_t rc= kOkDspRC;
  3161. cmDspSprintf_t* p = (cmDspSprintf_t*)inst;
  3162. if( kInSpId <= evt->dstVarId && evt->dstVarId < kInSpId + p->inCnt )
  3163. {
  3164. cmDspSetEvent(ctx,inst,evt);
  3165. //if( evt->dstVarId == kInSpId )
  3166. if((rc = _cmDspSprintfGenString(ctx,p)) == kOkDspRC )
  3167. cmDspSetStrcz(ctx,inst,kOutSpId,p->buf);
  3168. }
  3169. return rc;
  3170. }
  3171. struct cmDspClass_str* cmSprintfClassCons( cmDspCtx_t* ctx )
  3172. {
  3173. cmDspClassSetup(&_cmSprintfDC,ctx,"Sprintf",
  3174. NULL,
  3175. _cmDspSprintfAlloc,
  3176. _cmDspSprintfFree,
  3177. _cmDspSprintfReset,
  3178. NULL,
  3179. _cmDspSprintfRecv,
  3180. NULL,NULL,
  3181. "String formatter." );
  3182. return &_cmSprintfDC;
  3183. }
  3184. //==========================================================================================================================================
  3185. enum
  3186. {
  3187. kOutAmId,
  3188. kBaseInAmId
  3189. };
  3190. cmDspClass_t _cmAMixDC;
  3191. typedef struct
  3192. {
  3193. cmDspInst_t inst;
  3194. unsigned inPortCnt;
  3195. unsigned baseGainId;
  3196. unsigned baseMuteId;
  3197. } cmDspAMix_t;
  3198. cmDspInst_t* _cmDspAMixAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3199. {
  3200. if( va_cnt < 1 )
  3201. {
  3202. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'AMix' constructor must have a count of input ports.");
  3203. return NULL;
  3204. }
  3205. // args:
  3206. // <inPortCnt> <gain0>, <gain1> (the default gains are optional)
  3207. unsigned i;
  3208. int inPortCnt = va_arg(vl,int);
  3209. unsigned baseGainAmId = kBaseInAmId + inPortCnt;
  3210. unsigned baseMuteAmId = baseGainAmId + inPortCnt;
  3211. double dfltGain[ inPortCnt ];
  3212. if( va_cnt == 1 )
  3213. cmVOD_Fill(dfltGain,inPortCnt,1.0);
  3214. else
  3215. if( va_cnt == 2 )
  3216. {
  3217. dfltGain[0] = va_arg(vl,double);
  3218. cmVOD_Fill(dfltGain+1,inPortCnt-1,dfltGain[0]);
  3219. }
  3220. else
  3221. if( va_cnt == inPortCnt + 1 )
  3222. {
  3223. for(i=0; i<inPortCnt; ++i)
  3224. dfltGain[i] = va_arg(vl,double);
  3225. }
  3226. else
  3227. {
  3228. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The mix argument list must contain no default gain values, one default gain value, or all default gain values.");
  3229. return NULL;
  3230. }
  3231. cmDspAMix_t* p = cmDspInstAllocV(cmDspAMix_t,ctx,classPtr,instSymId,id,storeSymId,0,vl,
  3232. 1, "out", kOutAmId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  3233. inPortCnt, "in", kBaseInAmId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input",
  3234. inPortCnt, "gain", baseGainAmId, 0, 0, kInDsvFl | kDoubleDsvFl, "Gain input",
  3235. inPortCnt, "mute", baseMuteAmId, 0, 0, kInDsvFl | kBoolDsvFl, "Mute input",
  3236. 0 );
  3237. p->inPortCnt = inPortCnt;
  3238. p->baseGainId = baseGainAmId;
  3239. p->baseMuteId = baseMuteAmId;
  3240. for(i=0; i<inPortCnt; ++i)
  3241. {
  3242. cmDspSetDefaultDouble( ctx, &p->inst, p->baseGainId + i, 0.0, dfltGain[i]);
  3243. cmDspSetDefaultBool( ctx, &p->inst, p->baseMuteId + i, false, false );
  3244. }
  3245. /*
  3246. // read any default gain settings
  3247. --va_cnt;
  3248. for(i=0; i<inPortCnt; ++i)
  3249. {
  3250. // if excplicit gains are not given then default to 1.0.
  3251. double dflt = 1.0;
  3252. if( i < va_cnt )
  3253. dflt = va_arg(vl,double);
  3254. cmDspSetDefaultDouble( ctx, &p->inst, p->baseGainId + i, 0.0, dflt);
  3255. }
  3256. */
  3257. return &p->inst;
  3258. }
  3259. cmDspRC_t _cmDspAMixReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3260. {
  3261. cmDspRC_t rc = kOkDspRC;
  3262. cmDspApplyAllDefaults(ctx,inst);
  3263. cmDspZeroAudioBuf(ctx,inst,kOutAmId);
  3264. return rc;
  3265. }
  3266. cmDspRC_t _cmDspAMixExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3267. {
  3268. cmDspAMix_t* p = (cmDspAMix_t*)inst;
  3269. unsigned i;
  3270. cmDspZeroAudioBuf(ctx,inst,kOutAmId);
  3271. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutAmId,0);
  3272. cmSample_t* dp = cmDspAudioBuf(ctx,inst,kOutAmId,0);
  3273. for(i=0; i<p->inPortCnt; ++i)
  3274. {
  3275. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kBaseInAmId+i,0);
  3276. if( sp != NULL )
  3277. {
  3278. double gain = cmDspDouble(inst,p->baseGainId+i);
  3279. cmVOS_MultSumVVS(dp,n,sp,(cmSample_t)gain);
  3280. }
  3281. }
  3282. return kOkDspRC;
  3283. }
  3284. cmDspRC_t _cmDspAMixRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3285. {
  3286. cmDspRC_t rc = kOkDspRC;
  3287. cmDspAMix_t* p = (cmDspAMix_t*)inst;
  3288. if( p->baseGainId <= evt->dstVarId && evt->dstVarId < p->baseGainId + p->inPortCnt )
  3289. {
  3290. cmDspSetEvent(ctx,inst,evt);
  3291. //printf("rcv:%i %f\n",evt->dstVarId,cmDspDouble(inst,evt->dstVarId));
  3292. }
  3293. return rc;
  3294. }
  3295. struct cmDspClass_str* cmAMixClassCons( cmDspCtx_t* ctx )
  3296. {
  3297. cmDspClassSetup(&_cmAMixDC,ctx,"AMix",
  3298. NULL,
  3299. _cmDspAMixAlloc,
  3300. NULL,
  3301. _cmDspAMixReset,
  3302. _cmDspAMixExec,
  3303. _cmDspAMixRecv,
  3304. NULL,NULL,
  3305. "Audio mixer");
  3306. return &_cmAMixDC;
  3307. }
  3308. //==========================================================================================================================================
  3309. enum
  3310. {
  3311. kInAsId,
  3312. kBaseOutAsId
  3313. };
  3314. cmDspClass_t _cmASplitDC;
  3315. typedef struct
  3316. {
  3317. cmDspInst_t inst;
  3318. unsigned outPortCnt;
  3319. unsigned baseGainId;
  3320. } cmDspASplit_t;
  3321. // A splitter has one audio input port and multiple audio output ports.
  3322. // A gain input is automatically provided for each output port.
  3323. cmDspInst_t* _cmDspASplitAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3324. {
  3325. cmDspVarArg_t args[] =
  3326. {
  3327. { "in", kInAsId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input" },
  3328. { NULL, 0, 0, 0, 0 }
  3329. };
  3330. if( va_cnt < 1 )
  3331. {
  3332. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'ASplit' constructor must have a count of input ports.");
  3333. return NULL;
  3334. }
  3335. // args:
  3336. // <outPortCnt> <gain0>, <gain1> (the default gains are optional)
  3337. unsigned i,j,k;
  3338. int outPortCnt = va_arg(vl,int);
  3339. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]) - 1;
  3340. unsigned argCnt = fixArgCnt + 2*outPortCnt + 1;
  3341. cmDspVarArg_t argArray[ argCnt ];
  3342. int labelCharCnt = 15;
  3343. cmChar_t label[ labelCharCnt + 1 ];
  3344. label[labelCharCnt] = 0;
  3345. //
  3346. for(i=0; i<fixArgCnt; ++i)
  3347. argArray[i] = args[i];
  3348. // define the audio output port specifications
  3349. for(j=0,k=0; j<outPortCnt; ++i,++j,++k)
  3350. {
  3351. snprintf(label,labelCharCnt,"out-%i",j);
  3352. unsigned symId = cmSymTblRegisterSymbol(ctx->stH,label);
  3353. argArray[i].label = cmSymTblLabel(ctx->stH,symId);
  3354. argArray[i].constId = kBaseOutAsId + k;
  3355. argArray[i].rn = 0;
  3356. argArray[i].cn = 1;
  3357. argArray[i].flags = kOutDsvFl | kAudioBufDsvFl;
  3358. argArray[i].doc = "Audio Input";
  3359. }
  3360. // define the gain input specifications
  3361. for(j=0; j<outPortCnt; ++i,++j,++k)
  3362. {
  3363. snprintf(label,labelCharCnt,"gain-%i",j);
  3364. unsigned symId = cmSymTblRegisterSymbol(ctx->stH,label);
  3365. argArray[i].label = cmSymTblLabel(ctx->stH,symId);
  3366. argArray[i].constId = kBaseOutAsId + k;
  3367. argArray[i].rn = 0;
  3368. argArray[i].cn = 0;
  3369. argArray[i].flags = kInDsvFl | kDoubleDsvFl;
  3370. argArray[i].doc = "Gain input";
  3371. }
  3372. // set the NULL end-of-arg-array sentinel
  3373. memset(argArray + i, 0, sizeof(argArray[0]));
  3374. cmDspASplit_t* p = cmDspInstAlloc(cmDspASplit_t,ctx,classPtr,argArray,instSymId,id,storeSymId,0,vl);
  3375. p->outPortCnt = outPortCnt;
  3376. p->baseGainId = kBaseOutAsId + outPortCnt;
  3377. // read any default gain settings
  3378. --va_cnt;
  3379. for(i=0; i<outPortCnt; ++i)
  3380. {
  3381. double dflt = 1.0;
  3382. if( i < va_cnt )
  3383. dflt = va_arg(vl,double);
  3384. cmDspSetDefaultDouble( ctx, &p->inst, p->baseGainId + i, 0.0, dflt);
  3385. }
  3386. return &p->inst;
  3387. }
  3388. cmDspRC_t _cmDspASplitReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3389. {
  3390. cmDspRC_t rc = kOkDspRC;
  3391. cmDspASplit_t* p = (cmDspASplit_t*)inst;
  3392. int i;
  3393. cmDspApplyAllDefaults(ctx,inst);
  3394. for(i=0; i<p->outPortCnt; ++i)
  3395. cmDspZeroAudioBuf(ctx,inst,kBaseOutAsId+i);
  3396. return rc;
  3397. }
  3398. cmDspRC_t _cmDspASplitExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3399. {
  3400. cmDspASplit_t* p = (cmDspASplit_t*)inst;
  3401. unsigned i;
  3402. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kBaseOutAsId,0);
  3403. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kInAsId,0);
  3404. for(i=0; i<p->outPortCnt; ++i)
  3405. {
  3406. cmSample_t* dp = cmDspAudioBuf(ctx,inst,kBaseOutAsId+i,0);
  3407. double gain = cmDspDouble(inst,p->baseGainId+i);
  3408. cmVOS_MultVVS(dp,n,sp,(cmSample_t)gain);
  3409. }
  3410. return kOkDspRC;
  3411. }
  3412. cmDspRC_t _cmDspASplitRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3413. {
  3414. cmDspRC_t rc = kOkDspRC;
  3415. cmDspASplit_t* p = (cmDspASplit_t*)inst;
  3416. if( p->baseGainId <= evt->dstVarId && evt->dstVarId < p->baseGainId + p->outPortCnt )
  3417. {
  3418. cmDspSetEvent(ctx,inst,evt);
  3419. }
  3420. return rc;
  3421. }
  3422. struct cmDspClass_str* cmASplitClassCons( cmDspCtx_t* ctx )
  3423. {
  3424. cmDspClassSetup(&_cmASplitDC,ctx,"ASplit",
  3425. NULL,
  3426. _cmDspASplitAlloc,
  3427. NULL,
  3428. _cmDspASplitReset,
  3429. _cmDspASplitExec,
  3430. _cmDspASplitRecv,
  3431. NULL,NULL,
  3432. "Audio splitter");
  3433. return &_cmASplitDC;
  3434. }
  3435. //==========================================================================================================================================
  3436. enum
  3437. {
  3438. kInAmId,
  3439. kMinAmId,
  3440. kMaxAmId,
  3441. kValAmId, // meter value
  3442. kLblAmId,
  3443. };
  3444. #define cmDspMeter_MIN (-100)
  3445. #define cmDspMeter_MAX (0)
  3446. cmDspClass_t _cmAMeterDC;
  3447. typedef struct
  3448. {
  3449. cmDspInst_t inst;
  3450. unsigned bufN;
  3451. unsigned idx;
  3452. cmReal_t sum;
  3453. cmReal_t val;
  3454. } cmDspAMeter_t;
  3455. cmDspInst_t* _cmDspAMeterAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3456. {
  3457. cmDspVarArg_t args[] =
  3458. {
  3459. { "in", kInAmId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input"},
  3460. { "min", kMinAmId, 0, 0, kDoubleDsvFl, "Minimum value"},
  3461. { "max", kMaxAmId, 0, 0, kDoubleDsvFl, "Maximum value"},
  3462. { "val", kValAmId, 0, 0, kDoubleDsvFl, "Meter value"},
  3463. { "label",kLblAmId, 0, 0, kStrzDsvFl, "Label."},
  3464. { NULL, 0, 0, 0, 0 }
  3465. };
  3466. cmDspAMeter_t* p = cmDspInstAlloc(cmDspAMeter_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  3467. double updateMs = 100;
  3468. double sr = cmDspSampleRate(ctx);
  3469. unsigned spc = cmDspSamplesPerCycle(ctx);
  3470. p->bufN = cmMax(1,floor(updateMs * sr/ (1000.0 * spc)));
  3471. cmDspSetDefaultDouble(ctx, &p->inst, kValAmId, 0.0, cmDspMeter_MIN);
  3472. cmDspSetDefaultDouble(ctx, &p->inst, kMinAmId, 0.0, cmDspMeter_MIN);
  3473. cmDspSetDefaultDouble(ctx, &p->inst, kMaxAmId, 0.0, cmDspMeter_MAX);
  3474. // create the UI control
  3475. cmDspUiMeterCreate(ctx,&p->inst,kMinAmId,kMaxAmId,kValAmId,kLblAmId);
  3476. return &p->inst;
  3477. }
  3478. cmDspRC_t _cmDspAMeterReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3479. {
  3480. cmDspAMeter_t* p = (cmDspAMeter_t*)inst;
  3481. cmDspApplyAllDefaults(ctx,inst);
  3482. //cmDspZeroAudioBuf(ctx,inst,kInAmId);
  3483. p->idx = 0;
  3484. p->sum = 0;
  3485. p->val = 0;
  3486. return kOkDspRC;
  3487. }
  3488. cmDspRC_t _cmDspAMeterExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3489. {
  3490. cmDspAMeter_t* p = (cmDspAMeter_t*)inst;
  3491. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kInAmId,0);
  3492. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kInAmId,0);
  3493. if( sp == NULL )
  3494. {
  3495. inst->execFunc = NULL; // if there is no connected input then disable further callbacks
  3496. return kOkDspRC;
  3497. }
  3498. p->sum += cmVOS_SquaredSum(sp,n);
  3499. ++p->idx;
  3500. if( p->idx == p->bufN )
  3501. {
  3502. cmReal_t coeff = 0.7;
  3503. cmReal_t rms = sqrt(p->sum/(n*p->bufN));
  3504. p->idx = 0;
  3505. p->sum = 0;
  3506. p->val = rms > p->val ? rms : (rms*(1.0-coeff)) + (p->val*coeff);
  3507. double db = cmMax(cmDspMeter_MIN,cmMin(cmDspMeter_MAX,20.0 * log10(p->val)));
  3508. cmDspSetDouble(ctx, inst, kValAmId, db);
  3509. }
  3510. return kOkDspRC;
  3511. }
  3512. struct cmDspClass_str* cmAMeterClassCons( cmDspCtx_t* ctx )
  3513. {
  3514. cmDspClassSetup(&_cmAMeterDC,ctx,"AMeter",
  3515. NULL,
  3516. _cmDspAMeterAlloc,
  3517. NULL,
  3518. _cmDspAMeterReset,
  3519. _cmDspAMeterExec,
  3520. NULL,
  3521. NULL,NULL,
  3522. "Audio meter display.");
  3523. return &_cmAMeterDC;
  3524. }
  3525. //==========================================================================================================================================
  3526. //
  3527. //
  3528. // Read files created by this object with the Octave function cmTextFile().
  3529. //
  3530. //
  3531. enum
  3532. {
  3533. kCntTfId,
  3534. kFnTfId,
  3535. kBaseTfId
  3536. };
  3537. cmDspClass_t _cmTextFileDC;
  3538. typedef struct
  3539. {
  3540. cmDspInst_t inst;
  3541. int inPortCnt;
  3542. cmFileH_t fH;
  3543. } cmDspTextFile_t;
  3544. cmDspInst_t* _cmDspTextFileAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3545. {
  3546. cmDspVarArg_t args[] =
  3547. {
  3548. { "cnt", kCntTfId, 0, 0, kIntDsvFl | kReqArgDsvFl, "Input port count"},
  3549. { "fn", kFnTfId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "File name"},
  3550. { NULL, 0, 0, 0, 0 }
  3551. };
  3552. if( va_cnt < 1 )
  3553. {
  3554. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The text file object must be given a input port count argument.");
  3555. return NULL;
  3556. }
  3557. va_list vl1;
  3558. va_copy(vl1,vl);
  3559. int i,j;
  3560. int inPortCnt = va_arg(vl1,int);
  3561. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]) - 1;
  3562. unsigned argCnt = fixArgCnt + inPortCnt + 1;
  3563. cmDspVarArg_t argArray[ argCnt ];
  3564. if( inPortCnt <= 0 )
  3565. {
  3566. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The text file input port count must be a positive integer.");
  3567. return NULL;
  3568. }
  3569. //
  3570. for(i=0; i<fixArgCnt; ++i)
  3571. argArray[i] = args[i];
  3572. // define the input port specifications
  3573. for(j=0; j<inPortCnt; ++j,++i)
  3574. {
  3575. int labelCharCnt = 15;
  3576. cmChar_t label[ labelCharCnt + 1 ];
  3577. label[labelCharCnt] = 0;
  3578. snprintf(label,labelCharCnt,"in-%i",j);
  3579. unsigned symId = cmSymTblRegisterSymbol(ctx->stH,label);
  3580. argArray[i].label = cmSymTblLabel(ctx->stH,symId);
  3581. argArray[i].constId = kBaseTfId + j;
  3582. argArray[i].rn = 0;
  3583. argArray[i].cn = 0;
  3584. argArray[i].flags = kInDsvFl | kDoubleDsvFl | kSymDsvFl;
  3585. argArray[i].doc = "Data input";
  3586. }
  3587. // set the NULL end-of-arg-array sentinel
  3588. memset(argArray + i, 0, sizeof(argArray[0]));
  3589. cmDspTextFile_t* p = cmDspInstAlloc(cmDspTextFile_t,ctx,classPtr,argArray,instSymId,id,storeSymId,va_cnt,vl);
  3590. p->inPortCnt = inPortCnt;
  3591. p->fH = cmFileNullHandle;
  3592. return &p->inst;
  3593. }
  3594. cmDspRC_t _cmDspTextFileFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3595. {
  3596. cmDspTextFile_t* p = (cmDspTextFile_t*)inst;
  3597. if( cmFileClose(&p->fH) )
  3598. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Text file close failed.");
  3599. return kOkDspRC;
  3600. }
  3601. cmDspRC_t _cmDspTextFileReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3602. {
  3603. cmDspRC_t rc = kOkDspRC;
  3604. cmDspTextFile_t* p = (cmDspTextFile_t*)inst;
  3605. cmDspApplyAllDefaults(ctx,inst);
  3606. const cmChar_t* fn = cmDspStrcz(inst,kFnTfId);
  3607. if( cmFileOpen( &p->fH, fn, kWriteFileFl, ctx->cmCtx->err.rpt ) != kOkFileRC )
  3608. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "Text file open failed.");
  3609. return rc;
  3610. }
  3611. cmDspRC_t _cmDspTextFileRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3612. {
  3613. cmDspTextFile_t* p = (cmDspTextFile_t*)inst;
  3614. cmDspSetEvent(ctx,inst,evt);
  3615. if( cmFileIsValid(p->fH) && evt->dstVarId >= kBaseTfId )
  3616. {
  3617. double secs = ctx->ctx->begSmpIdx / cmDspSampleRate(ctx);
  3618. cmFilePrintf(p->fH,"%f %f %i ", secs, secs, evt->dstVarId-kBaseTfId);
  3619. if( cmIsFlag(evt->valuePtr->flags,kSymDsvFl) )
  3620. cmFilePrintf(p->fH,"%s\n", cmStringNullGuard(cmSymTblLabel(ctx->stH,cmDsvSymbol(evt->valuePtr))));
  3621. else
  3622. cmFilePrintf(p->fH,"%f\n", cmDspDouble(inst,evt->dstVarId));
  3623. }
  3624. return kOkDspRC;
  3625. }
  3626. struct cmDspClass_str* cmTextFileClassCons( cmDspCtx_t* ctx )
  3627. {
  3628. cmDspClassSetup(&_cmTextFileDC,ctx,"TextFile",
  3629. NULL,
  3630. _cmDspTextFileAlloc,
  3631. _cmDspTextFileFree,
  3632. _cmDspTextFileReset,
  3633. NULL,
  3634. _cmDspTextFileRecv,
  3635. NULL,NULL,
  3636. "Time tagged text file.");
  3637. return &_cmTextFileDC;
  3638. }
  3639. //==========================================================================================================================================
  3640. enum
  3641. {
  3642. kRsrcArId,
  3643. kCmdArId,
  3644. kIdxArId,
  3645. kValArId,
  3646. kCntArId,
  3647. kDoneArId,
  3648. kBaseOutArId
  3649. };
  3650. cmDspClass_t _cmArrayDC;
  3651. typedef struct
  3652. {
  3653. cmDspInst_t inst;
  3654. unsigned cnt;
  3655. cmReal_t* array;
  3656. unsigned printSymId;
  3657. unsigned sendSymId;
  3658. unsigned cntSymId;
  3659. unsigned doneSymId;
  3660. } cmDspArray_t;
  3661. cmDspInst_t* _cmDspArrayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3662. {
  3663. va_list vl1;
  3664. va_copy(vl1,vl);
  3665. if( va_cnt < 1 )
  3666. {
  3667. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The array object must be given a 'rsrc' parameter.");
  3668. return NULL;
  3669. }
  3670. const cmChar_t* rsrcStr = va_arg(vl,cmChar_t*);
  3671. unsigned outPortCnt = 0;
  3672. cmReal_t* array = NULL;
  3673. unsigned doneSymId = cmDspSysRegisterStaticSymbol(ctx->dspH,"done");
  3674. unsigned i;
  3675. if( rsrcStr == NULL )
  3676. {
  3677. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The array object 'rsrc' parameter appears to be missing.");
  3678. return NULL;
  3679. }
  3680. if( cmDspRsrcRealArray( ctx->dspH, &outPortCnt, &array, rsrcStr, NULL ) != kOkDspRC )
  3681. {
  3682. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The array resource '%s' could not be read.",cmStringNullGuard(rsrcStr));
  3683. return NULL;
  3684. }
  3685. cmDspArray_t* p = cmDspInstAllocV(cmDspArray_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  3686. 1, "rsrc", kRsrcArId, 0, 0, kStrzDsvFl | kInDsvFl | kReqArgDsvFl, "Array data resource label.",
  3687. 1, "cmd", kCmdArId, 0, 0, kSymDsvFl | kInDsvFl, "Command: send | print | count.",
  3688. 1, "idx", kIdxArId, 0, 0, kUIntDsvFl | kInDsvFl, "Send value at index out 'val' port.",
  3689. 1, "val", kValArId, 0, 0, kDoubleDsvFl | kOutDsvFl, "Index output value.",
  3690. 1, "cnt", kCntArId, 0, 0, kUIntDsvFl | kOutDsvFl, "Count output value.",
  3691. 1, "done", kDoneArId, 0, 0, kSymDsvFl | kOutDsvFl, "'done' after last send.",
  3692. outPortCnt, "out", kBaseOutArId, 0, 0, kDoubleDsvFl | kOutDsvFl | kSendDfltDsvFl, "Individual real value outputs.",
  3693. 0 );
  3694. cmDspSetDefaultDouble( ctx, &p->inst, kValArId, 0, 0 );
  3695. cmDspSetDefaultUInt( ctx, &p->inst, kCntArId, 0, outPortCnt );
  3696. cmDspSetDefaultSymbol( ctx, &p->inst, kDoneArId, doneSymId);
  3697. for(i=0; i<outPortCnt; ++i)
  3698. cmDspSetDefaultDouble( ctx, &p->inst, kBaseOutArId+i, 0, array[i] );
  3699. p->array = array;
  3700. p->cnt = outPortCnt;
  3701. p->sendSymId = cmDspSysRegisterStaticSymbol(ctx->dspH,"send");
  3702. p->printSymId = cmDspSysRegisterStaticSymbol(ctx->dspH,"print");
  3703. p->cntSymId = cmDspSysRegisterStaticSymbol(ctx->dspH,"count");
  3704. p->doneSymId = doneSymId;
  3705. return &p->inst;
  3706. }
  3707. cmDspRC_t _cmDspArrayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3708. {
  3709. cmDspArray_t* p = (cmDspArray_t*)inst;
  3710. cmDspRC_t rc;
  3711. // send 'out' values and then 'done' value
  3712. if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
  3713. cmDspSetSymbol(ctx,inst,kDoneArId,p->doneSymId);
  3714. return rc;
  3715. }
  3716. cmDspRC_t _cmDspArrayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3717. {
  3718. cmDspRC_t rc;
  3719. cmDspArray_t* p = (cmDspArray_t*)inst;
  3720. if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
  3721. return rc;
  3722. switch( evt->dstVarId )
  3723. {
  3724. case kCmdArId:
  3725. {
  3726. unsigned i;
  3727. unsigned cmdSymId = cmDsvSymbol(evt->valuePtr);
  3728. if( cmdSymId == p->printSymId )
  3729. {
  3730. for(i=0; i<p->cnt; ++i)
  3731. cmRptPrintf(ctx->rpt,"%f ",p->array[i]);
  3732. cmRptPrintf(ctx->rpt,"\n");
  3733. }
  3734. else
  3735. if( cmdSymId == p->sendSymId )
  3736. {
  3737. for(i=0; i<p->cnt; ++i)
  3738. cmDspSetDouble(ctx,inst,kBaseOutArId+i,p->array[i]);
  3739. cmDspSetSymbol(ctx,inst,kDoneArId,p->doneSymId);
  3740. }
  3741. else
  3742. if( cmdSymId == p->cntSymId )
  3743. {
  3744. cmDspSetUInt(ctx,inst,kCntArId,p->cnt);
  3745. }
  3746. }
  3747. break;
  3748. case kIdxArId:
  3749. {
  3750. unsigned idx = cmDsvUInt(evt->valuePtr);
  3751. if( idx < p->cnt )
  3752. cmDspSetDouble(ctx,inst,kValArId,p->array[idx]);
  3753. }
  3754. break;
  3755. }
  3756. return kOkDspRC;
  3757. }
  3758. struct cmDspClass_str* cmArrayClassCons( cmDspCtx_t* ctx )
  3759. {
  3760. cmDspClassSetup(&_cmArrayDC,ctx,"Array",
  3761. NULL,
  3762. _cmDspArrayAlloc,
  3763. NULL,
  3764. _cmDspArrayReset,
  3765. NULL,
  3766. _cmDspArrayRecv,
  3767. NULL,NULL,
  3768. "Time tagged text file.");
  3769. return &_cmArrayDC;
  3770. }
  3771. //==========================================================================================================================================
  3772. enum
  3773. {
  3774. kMidiPcId,
  3775. kHzPcId,
  3776. kOffsPcId,
  3777. kStrPcId,
  3778. kRatioPcId
  3779. };
  3780. cmDspClass_t _cmPitchCvtDC;
  3781. typedef struct
  3782. {
  3783. cmDspInst_t inst;
  3784. int midi;
  3785. double hz;
  3786. } cmDspPitchCvt_t;
  3787. cmDspInst_t* _cmDspPitchCvtAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3788. {
  3789. va_cnt = 0; // ignore an errant arguments
  3790. cmDspPitchCvt_t* p = cmDspInstAllocV(cmDspPitchCvt_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3791. 1, "midi", kMidiPcId, 0, 0, kUIntDsvFl | kInDsvFl | kOutDsvFl | kOptArgDsvFl, "MIDI pitch value input.",
  3792. 1, "hz", kHzPcId, 0, 0, kDoubleDsvFl | kInDsvFl | kOutDsvFl | kOptArgDsvFl, "Hz pitch value.",
  3793. 1, "offs", kOffsPcId, 0, 0, kDoubleDsvFl | kInDsvFl | kInDsvFl | kOptArgDsvFl, "Semitone offset.",
  3794. 1, "str", kStrPcId, 0, 0, kStrzDsvFl | kOutDsvFl, "Pitch string output.",
  3795. 1, "ratio", kRatioPcId, 0, 0, kDoubleDsvFl | kOutDsvFl, "Offset as a ratio",
  3796. 0 );
  3797. cmDspSetDefaultUInt( ctx, &p->inst, kMidiPcId, 0, 0 );
  3798. cmDspSetDefaultDouble( ctx, &p->inst, kHzPcId, 0, 0.0 );
  3799. cmDspSetDefaultDouble( ctx, &p->inst, kOffsPcId, 0, 0 );
  3800. cmDspSetDefaultStrcz( ctx, &p->inst, kStrPcId, NULL, "" );
  3801. cmDspSetDefaultDouble( ctx, &p->inst, kRatioPcId, 0, 0 );
  3802. return &p->inst;
  3803. }
  3804. cmDspRC_t _cmDspPitchCvtReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3805. {
  3806. cmDspRC_t rc = cmDspApplyAllDefaults(ctx,inst);
  3807. return rc;
  3808. }
  3809. cmDspRC_t _cmDspPitchCvtOutput( cmDspCtx_t* ctx, cmDspPitchCvt_t* p )
  3810. {
  3811. cmDspInst_t* inst = &p->inst;
  3812. double offs = cmDspDouble( inst, kOffsPcId );
  3813. unsigned midi = cmMax(0, p->midi + rint(offs) );
  3814. double ratio= pow(2.0,offs/12.0);
  3815. double hz = p->hz * ratio;
  3816. //cmRptPrintf(ctx->rpt,"%i %i %f %f\n",offs,midi,hz,p->hz);
  3817. cmDspSetStrcz( ctx, inst, kStrPcId, cmMidiToSciPitch(midi,NULL,0));
  3818. cmDspSetUInt( ctx, inst, kMidiPcId, midi);
  3819. cmDspSetDouble( ctx, inst, kHzPcId, hz);
  3820. cmDspSetDouble( ctx, inst, kRatioPcId, ratio );
  3821. return kOkDspRC;
  3822. }
  3823. cmDspRC_t _cmDspPitchCvtRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3824. {
  3825. cmDspRC_t rc = kOkDspRC;
  3826. cmDspPitchCvt_t* p = (cmDspPitchCvt_t*)inst;
  3827. switch( evt->dstVarId )
  3828. {
  3829. case kMidiPcId:
  3830. p->midi = cmMax(0,cmDsvGetInt(evt->valuePtr));
  3831. p->hz = cmMidiToHz(p->midi);
  3832. rc = _cmDspPitchCvtOutput(ctx,p);
  3833. break;
  3834. case kHzPcId:
  3835. p->hz = cmDsvGetDouble(evt->valuePtr);
  3836. p->midi = cmHzToMidi(p->hz);
  3837. rc = _cmDspPitchCvtOutput(ctx,p);
  3838. break;
  3839. case kOffsPcId:
  3840. if((rc = cmDspSetEvent(ctx, inst, evt )) == kOkDspRC )
  3841. rc = _cmDspPitchCvtOutput(ctx,p);
  3842. break;
  3843. }
  3844. return rc;
  3845. }
  3846. struct cmDspClass_str* cmPitchCvtClassCons( cmDspCtx_t* ctx )
  3847. {
  3848. cmDspClassSetup(&_cmPitchCvtDC,ctx,"PitchCvt",
  3849. NULL,
  3850. _cmDspPitchCvtAlloc,
  3851. NULL,
  3852. _cmDspPitchCvtReset,
  3853. NULL,
  3854. _cmDspPitchCvtRecv,
  3855. NULL,NULL,
  3856. "Time tagged text file.");
  3857. return &_cmPitchCvtDC;
  3858. }
  3859. //==========================================================================================================================================
  3860. //
  3861. //
  3862. // Create a file which can be read by readBinFile.m
  3863. //
  3864. //
  3865. enum
  3866. {
  3867. kCntBmId,
  3868. kFnBmId,
  3869. kBaseBmId
  3870. };
  3871. cmDspClass_t _cmBinMtxFileDC;
  3872. typedef struct
  3873. {
  3874. cmDspInst_t inst;
  3875. int inPortCnt;
  3876. cmBinMtxFile_t* bmfp;
  3877. cmReal_t * valArray; // valArray[ inPortCnt ]
  3878. } cmDspBinMtxFile_t;
  3879. cmDspInst_t* _cmDspBinMtxFileAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3880. {
  3881. cmDspVarArg_t args[] =
  3882. {
  3883. { "cnt", kCntBmId, 0, 0, kIntDsvFl | kReqArgDsvFl, "Input port count"},
  3884. { "fn", kFnBmId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "File name"},
  3885. };
  3886. if( va_cnt < 1 )
  3887. {
  3888. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The binary matrix file object must be given a input port count argument.");
  3889. return NULL;
  3890. }
  3891. va_list vl1;
  3892. va_copy(vl1,vl);
  3893. int inPortCnt = va_arg(vl,int);
  3894. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]);
  3895. unsigned argCnt = fixArgCnt + inPortCnt;
  3896. cmDspVarArg_t a[ argCnt+1 ];
  3897. cmDspBinMtxFile_t* p = NULL;
  3898. if( inPortCnt <= 0 )
  3899. {
  3900. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The binary matrix file must be a positive integer.");
  3901. return NULL;
  3902. }
  3903. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  3904. cmDspArgSetupN(ctx, a, argCnt, kBaseBmId, inPortCnt, "in", kBaseBmId, 0, 0, kInDsvFl | kDoubleDsvFl, "input ports");
  3905. cmDspArgSetupNull( a+argCnt );
  3906. if((p = cmDspInstAlloc(cmDspBinMtxFile_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl1)) == NULL )
  3907. return NULL;
  3908. p->bmfp = cmBinMtxFileAlloc(ctx->cmProcCtx, NULL, NULL );
  3909. p->inPortCnt = inPortCnt;
  3910. p->valArray = cmMemAllocZ(cmReal_t,inPortCnt);
  3911. return &p->inst;
  3912. }
  3913. cmDspRC_t _cmDspBinMtxFileOpen( cmDspCtx_t* ctx, cmDspInst_t* inst )
  3914. {
  3915. cmDspRC_t rc = kOkDspRC;
  3916. cmDspBinMtxFile_t* p = (cmDspBinMtxFile_t*)inst;
  3917. if( p->bmfp != NULL )
  3918. {
  3919. // finalize the current file
  3920. if( cmBinMtxFileFinal(p->bmfp) != cmOkRC )
  3921. cmDspInstErr(ctx,inst,kFileCloseFailDspRC,"File close failed.");
  3922. // open a new one
  3923. if( cmBinMtxFileInit( p->bmfp, cmDspDefaultStrcz(&p->inst,kFnBmId) ) != cmOkRC)
  3924. rc = cmDspInstErr(ctx,inst,kFileOpenFailDspRC,"File open failed.");
  3925. }
  3926. return rc;
  3927. }
  3928. cmDspRC_t _cmDspBinMtxFileFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3929. {
  3930. cmDspBinMtxFile_t* p = (cmDspBinMtxFile_t*)inst;
  3931. cmBinMtxFileFree(&p->bmfp);
  3932. cmMemFree(p->valArray);
  3933. return kOkDspRC;
  3934. }
  3935. cmDspRC_t _cmDspBinMtxFileReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3936. {
  3937. cmDspApplyAllDefaults(ctx,inst);
  3938. return _cmDspBinMtxFileOpen(ctx,inst);
  3939. }
  3940. cmDspRC_t _cmDspBinMtxFileExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3941. {
  3942. cmDspRC_t rc = kOkDspRC;
  3943. cmDspBinMtxFile_t* p = (cmDspBinMtxFile_t*)inst;
  3944. // write the file
  3945. if( cmBinMtxFileIsValid( p->bmfp ) )
  3946. if( cmBinMtxFileExecR(p->bmfp, p->valArray, p->inPortCnt ) != cmOkRC )
  3947. return cmDspInstErr(ctx,inst,kFileWriteFailDspRC,"File write failure.");
  3948. return rc;
  3949. }
  3950. cmDspRC_t _cmDspBinMtxFileRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3951. {
  3952. cmDspRC_t rc = kOkDspRC;
  3953. cmDspBinMtxFile_t* p = (cmDspBinMtxFile_t*)inst;
  3954. cmDspSetEvent(ctx,inst,evt);
  3955. // new file name - create new output file
  3956. if( evt->dstVarId == kFnBmId )
  3957. {
  3958. rc = _cmDspBinMtxFileOpen(ctx,inst);
  3959. }
  3960. else
  3961. // new value - store in p->valArray[]
  3962. if( kBaseBmId <= evt->dstVarId && evt->dstVarId < kBaseBmId + p->inPortCnt )
  3963. {
  3964. p->valArray[ evt->dstVarId - kBaseBmId ] = cmDspDouble(inst,evt->dstVarId );
  3965. }
  3966. else
  3967. { assert(0); }
  3968. return rc;
  3969. }
  3970. struct cmDspClass_str* cmBinMtxFileClassCons( cmDspCtx_t* ctx )
  3971. {
  3972. cmDspClassSetup(&_cmBinMtxFileDC,ctx,"BinMtxFile",
  3973. NULL,
  3974. _cmDspBinMtxFileAlloc,
  3975. _cmDspBinMtxFileFree,
  3976. _cmDspBinMtxFileReset,
  3977. _cmDspBinMtxFileExec,
  3978. _cmDspBinMtxFileRecv,
  3979. NULL,NULL,
  3980. "Time tagged text file.");
  3981. return &_cmBinMtxFileDC;
  3982. }
  3983. //==========================================================================================================================================
  3984. enum
  3985. {
  3986. kHopMsSbId,
  3987. kWndFactSbId,
  3988. kInSbId,
  3989. kOutSbId
  3990. };
  3991. cmDspClass_t _cmShiftBufDC;
  3992. typedef struct
  3993. {
  3994. cmDspInst_t inst;
  3995. cmShiftBuf* sbp;
  3996. } cmDspShiftBuf_t;
  3997. void _cmDspShiftBufSetup( cmDspCtx_t* ctx, cmDspShiftBuf_t* p )
  3998. {
  3999. double hopMs = cmDspDouble(&p->inst,kHopMsSbId);
  4000. unsigned hopSmpCnt = lround(cmDspSampleRate(ctx) * hopMs / 1000.0 );
  4001. unsigned wndSmpCnt = cmDspUInt(&p->inst,kWndFactSbId) * hopSmpCnt;
  4002. if( p->sbp == NULL || hopSmpCnt != p->sbp->hopSmpCnt || wndSmpCnt != p->sbp->wndSmpCnt )
  4003. {
  4004. cmShiftBufFree(&p->sbp);
  4005. p->sbp = cmShiftBufAlloc(ctx->cmProcCtx, NULL, cmDspSamplesPerCycle(ctx), wndSmpCnt, hopSmpCnt );
  4006. }
  4007. }
  4008. cmDspInst_t* _cmDspShiftBufAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4009. {
  4010. cmDspVarArg_t args[] =
  4011. {
  4012. { "hopMs", kHopMsSbId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Hop size on milliseconds"},
  4013. { "wndFact", kWndFactSbId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Count of hops contained in each output buffer."},
  4014. { "in", kInSbId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input"},
  4015. { "out", kOutSbId, 0, 0, kOutDsvFl | kAudioBufDsvFl, "Audio output"},
  4016. { NULL, 0, 0, 0, 0 }
  4017. };
  4018. // Note: by setting the column count of the output audio variable to zero
  4019. // we prevent it from being automatically assigned vector memory.
  4020. cmDspShiftBuf_t* p = cmDspInstAlloc(cmDspShiftBuf_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  4021. return &p->inst;
  4022. }
  4023. cmDspRC_t _cmDspShiftBufFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4024. {
  4025. cmDspShiftBuf_t* p = (cmDspShiftBuf_t*)inst;
  4026. cmShiftBufFree(&p->sbp);
  4027. return kOkDspRC;
  4028. }
  4029. cmDspRC_t _cmDspShiftBufReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4030. {
  4031. cmDspRC_t rc = kOkDspRC;
  4032. cmDspShiftBuf_t* p = (cmDspShiftBuf_t*)inst;
  4033. cmDspApplyAllDefaults(ctx,inst);
  4034. _cmDspShiftBufSetup(ctx,p);
  4035. return rc;
  4036. }
  4037. cmDspRC_t _cmDspShiftBufRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4038. {
  4039. cmDspShiftBuf_t* p = (cmDspShiftBuf_t*)inst;
  4040. cmDspSetEvent(ctx,inst,evt);
  4041. switch(evt->dstVarId)
  4042. {
  4043. case kHopMsSbId:
  4044. case kWndFactSbId:
  4045. _cmDspShiftBufSetup( ctx, p );
  4046. break;
  4047. };
  4048. return kOkDspRC;
  4049. }
  4050. cmDspRC_t _cmDspShiftBufExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4051. {
  4052. cmDspShiftBuf_t* p = (cmDspShiftBuf_t*)inst;
  4053. unsigned sn = cmDspAudioBufSmpCount(ctx,inst,kInSbId,0);
  4054. const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kInSbId,0);
  4055. cmDspVar_t* varPtr = cmDspVarIdToPtr(inst,kOutSbId);
  4056. if( cmShiftBufExec(p->sbp, sp, sn ) )
  4057. cmDsvSetSampleMtx( &varPtr->value, p->sbp->outV, p->sbp->outN, 1);
  4058. else
  4059. cmDsvSetSampleMtx( &varPtr->value, NULL, 0, 0);
  4060. return kOkDspRC;
  4061. }
  4062. struct cmDspClass_str* cmShiftBufClassCons( cmDspCtx_t* ctx )
  4063. {
  4064. cmDspClassSetup(&_cmShiftBufDC,ctx,"ShiftBuf",
  4065. NULL,
  4066. _cmDspShiftBufAlloc,
  4067. _cmDspShiftBufFree,
  4068. _cmDspShiftBufReset,
  4069. _cmDspShiftBufExec,
  4070. _cmDspShiftBufRecv,
  4071. NULL,NULL,
  4072. "Time tagged text file.");
  4073. return &_cmShiftBufDC;
  4074. }
  4075. //==========================================================================================================================================
  4076. enum
  4077. {
  4078. kInNsId
  4079. };
  4080. cmDspClass_t _cmNetSendDC;
  4081. typedef struct
  4082. {
  4083. cmDspInst_t inst;
  4084. _cmDspSrcConn_t* srcConnPtr;
  4085. } cmDspNetSend_t;
  4086. cmDspInst_t* _cmDspNetSendAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  4087. {
  4088. cmDspVarArg_t args[] =
  4089. {
  4090. { "in", kInNsId, 0, 0, kInDsvFl | kTypeDsvMask, "Input port" },
  4091. { NULL, 0, 0, 0, 0 }
  4092. };
  4093. assert( va_cnt == 1 );
  4094. _cmDspSrcConn_t* srcConnPtr = va_arg(vl,_cmDspSrcConn_t*);
  4095. assert( srcConnPtr != NULL );
  4096. cmDspNetSend_t* p = cmDspInstAlloc(cmDspNetSend_t,ctx,classPtr,args,instSymId,id,storeSymId,0,vl);
  4097. p->srcConnPtr = srcConnPtr;
  4098. return &p->inst;
  4099. }
  4100. cmDspRC_t _cmDspNetSendReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4101. {
  4102. return kOkDspRC;
  4103. }
  4104. cmDspRC_t _cmDspNetSendRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  4105. {
  4106. cmDspNetSend_t* p = (cmDspNetSend_t*)inst;
  4107. return _cmDspSysNetSendEvent(ctx->dspH, p->srcConnPtr->dstNetNodeId, p->srcConnPtr->dstId, evt );
  4108. }
  4109. struct cmDspClass_str* cmNetSendClassCons( cmDspCtx_t* ctx )
  4110. {
  4111. cmDspClassSetup(&_cmNetSendDC,ctx,"NetSend",
  4112. NULL,
  4113. _cmDspNetSendAlloc,
  4114. NULL,
  4115. _cmDspNetSendReset,
  4116. NULL,
  4117. _cmDspNetSendRecv,
  4118. NULL,NULL,
  4119. "Print the value of any event arriving at 'in'.");
  4120. return &_cmNetSendDC;
  4121. }
  4122. //==========================================================================================================================================
  4123. //==========================================================================================================================================
  4124. cmDspClassConsFunc_t _cmDspClassBuiltInArray[] =
  4125. {
  4126. cmPrinterClassCons,
  4127. cmCounterClassCons,
  4128. cmPhasorClassCons,
  4129. cmMidiOutClassCons,
  4130. cmMidiInClassCons,
  4131. cmAudioInClassCons,
  4132. cmAudioOutClassCons,
  4133. cmAudioFileOutClassCons,
  4134. cmSigGenClassCons,
  4135. cmScalarClassCons,
  4136. cmTextClassCons,
  4137. cmMeterClassCons,
  4138. cmLabelClassCons,
  4139. cmButtonClassCons,
  4140. cmReorderClassCons,
  4141. cmFnameClassCons,
  4142. cmMsgListClassCons,
  4143. cmWaveTableClassCons,
  4144. cmSprintfClassCons,
  4145. cmAMixClassCons,
  4146. cmASplitClassCons,
  4147. cmAMeterClassCons,
  4148. cmTextFileClassCons,
  4149. cmBinMtxFileClassCons,
  4150. cmArrayClassCons,
  4151. cmPitchCvtClassCons,
  4152. cmShiftBufClassCons,
  4153. cmNetSendClassCons,
  4154. cmDelayClassCons,
  4155. cmPShiftClassCons,
  4156. cmLoopRecdClassCons,
  4157. cmRectifyClassCons,
  4158. cmGateDetectClassCons,
  4159. cmAutoGainClassCons,
  4160. cmEnvFollowClassCons,
  4161. cmXfaderClassCons,
  4162. cmChCfgClassCons,
  4163. cmChordDetectClassCons,
  4164. cmFaderClassCons,
  4165. cmNoteSelectClassCons,
  4166. cmNetNoteSelectClassCons,
  4167. cmCombFiltClassCons,
  4168. cmScalarOpClassCons,
  4169. cmGroupSelClassCons,
  4170. cmAudioNofMClassCons,
  4171. cmRingModClassCons,
  4172. cmMsgDelayClassCons,
  4173. cmLineClassCons,
  4174. cmAdsrClassCons,
  4175. cmCompressorClassCons,
  4176. cmBiQuadEqClassCons,
  4177. cmDistDsClassCons,
  4178. cmDbToLinClassCons,
  4179. cmMtDelayClassCons,
  4180. cmNofMClassCons,
  4181. cm1ofNClassCons,
  4182. cm1UpClassCons,
  4183. cmGateToSymClassCons,
  4184. cmPortToSymClassCons,
  4185. cmRouterClassCons,
  4186. cmAvailChClassCons,
  4187. cmPresetClassCons,
  4188. cmBcastSymClassCons,
  4189. cmSegLineClassCons,
  4190. cmKrClassCons,
  4191. cmTimeLineClassCons,
  4192. cmScoreClassCons,
  4193. cmMidiFilePlayClassCons,
  4194. cmScFolClassCons,
  4195. cmScModClassCons,
  4196. cmGSwitchClassCons,
  4197. cmScaleRangeClassCons,
  4198. NULL,
  4199. };
  4200. cmDspClassConsFunc_t cmDspClassGetBuiltIn( unsigned index )
  4201. {
  4202. unsigned n = sizeof(_cmDspClassBuiltInArray)/sizeof(cmDspClass_t*);
  4203. if( index >= n )
  4204. return NULL;
  4205. return _cmDspClassBuiltInArray[index];
  4206. }