libcm is a C development framework with an emphasis on audio signal processing applications.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cmDspBuiltIn.c 164KB

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