12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033 |
- #include "cmPrefix.h"
- #include "cmGlobal.h"
- #include "cmFloatTypes.h"
- #include "cmComplexTypes.h"
- #include "cmRpt.h"
- #include "cmErr.h"
- #include "cmCtx.h"
- #include "cmMem.h"
- #include "cmMallocDebug.h"
- #include "cmLinkedHeap.h"
- #include "cmMath.h"
- #include "cmFile.h"
- #include "cmFileSys.h"
- #include "cmSymTbl.h"
- #include "cmJson.h"
- #include "cmPrefs.h"
- #include "cmProcObj.h"
- #include "cmDspValue.h"
- #include "cmDspCtx.h"
- #include "cmDspClass.h"
- #include "cmDspFx.h"
- #include "cmDspKr.h"
- #include "cmMsgProtocol.h"
- #include "cmThread.h"
- #include "cmUdpPort.h"
- #include "cmUdpNet.h"
- #include "cmAudioSys.h"
- #include "cmDspSys.h"
-
- #include "cmDspPreset.h" // required for cmDspNetSend
- #include "cmDspNet.h"
-
- #include "cmAudioFile.h"
- #include "cmThread.h" // used for threaded loading in wave table file mode
-
-
- #include "cmProcObj.h"
- #include "cmProcTemplateMain.h"
- #include "cmProc.h"
- #include "cmMidi.h"
- #include "cmProc2.h"
- #include "cmVectOpsTemplateMain.h"
-
- /*
- About variables:
- 1) Variables represent data fields within a DSP object.
-
- 2) Variables may also act as input (kInDsvFl) and/or output (kOutDsvFl) ports.
- A. Audio Ports
- - Audio output ports (kAudioBufDsvFl) publish a buffer of samples to subscribing
- audio input ports on other instances. (See cmDspSysConnectAudio(). )
-
- - Output audio ports are instantiated with physical buffers to hold samples of
- audio. Input audio ports contain internal pointers with point to the
- audio buffers of connected output ports.
-
- - Set cmDspVarArg_t.cn to the number of channels of audio the buffer will contain.
-
- B. Non-Audio Ports
- - Non-audio input ports may register to be called when output port values change.
- via cmDspSysInstallCb().
-
- - Type checking is done to guarantee that the output port data type matches one of the
- input port types or can be converted to one of the input port types.
- [TODO: Check how this is handled in cmDspSetEvent(). Let's say that an input port
- takes either a number or string. This conversion is not possible within the
- cmDspValue() framework - and yet it should work - or give an error ]
-
-
- 3) Creating variable instances.
- A. All variables must be given default values in the instance constructor
- _cmDspXXXAlloc(). This can be done automatically be giving default values
- in the var args section of the cmDspSysAllocInst() call or by explicitely
- setting default values via cmDspSetDefaultXXX() in _cmDspXXXAlloc().
-
- B. The _cmDspXXXReset() function automatically resets all the instance variables to their
- default value. By default, variables do not transmit their values when they are
- reset to their default value - unless the variable is marked with the kSendDfltDsvFl.
- (See cmDspClass.c:cmDspApplyDefault()). The 'out' port on cmDspScalar_t and cmDspButton_t
- are examples of instances that transmit their default value on reset.
-
- The order that instances are reset is determined by the order that they were created.
-
- A subtle case can arise when relying on default values to set the initial value of
- another object. Given two object instances A and B. Where The output of A has the
- the kSendDfltDsvFl set and is connected to an input port on B. If instance A was
- created before instance B then instance A will reset before instance B. During reset
- the output port of A will transmit it's default value to B - as expected. However
- when reset is called on instance B it will overwrite this value with it's own
- default value.
-
- In order for this scenario to work - that is the output of instance A sets the initial
- value of instance B - instance B must be created first.
-
- Since creation order also determines execution order this solution may not always
- be possible. It may be that we would simultaneously like instance A to exeute first
- and also recieve its initial value from instance B. Given the above example however
- these two goals are mutually exclusive.
-
- [*] This could be solved by explicitely allowing a variable to be reset via
- a callback. In this case if a variable was set via a callback during reset
- then it would not overwrite that value with its own internal value. This
- functionality would need to be set on a per instance bases - which might
- not work well with the current cmDspSysAllocInst() var args scheme.
-
- When a single output port is connected to multiple input ports the input ports
- are called in the order that the connections were made.
-
- To overcome this problem the reset cycle has been broken into two parts.
- In the first part the resetFunc is called on each instance in creation order.
- (as described above). Following this pass a second pass is made where the
- sysRecvFunc on any instance tagged with the '_reset' attribute is called.
- This allows an event callback chain to be executed prior to the first
- set of execFunc calls. Note that the event callback chain(s) are programmed
- by cmDspInstallCb() connections which are setup on a per application basis.
- This allows the initial state of the network to be set outside of the
- creation order.
-
- The 'button' instance implements a sysRecvFunc which will send the buttons
- value and symbol when called with the '_reset' attribute symbol. If a
- 'reset' button is created and assigned the '_reset' attribute symbol
- (via cmDspSysAssignAttrSymbol()) then the output of the button can be used
- to drive a networks initial state in an order determined by the application
- programmer.
-
-
- [TODO: Add a check that all instance variables have default values at the end of
- the network reset. This should be easy because the instance variable flag
- kDfltSetDsvFl is set in cmDspValueSet() when the default value is set.]
-
- C. The default value of variables may be set via the var args portion of cmDspSysAllocInst().
-
- There is currently a weakness during variable instance creation function
- cmDspInstAlloc() uses the cmDspVarArg_t.flags to determine the type of the var arg
- argument. cmDspVarArg_t.flags can therefore only be set to one DSV type - and the
- actual argument in the cmDspSysAllocInst() call must match that type. This is very
- easy to mess up - for example by setting the flag to kIntDsvFl and then putting
- 0.0 as the var arg value. This limitation also prevents ports which are also
- required arguments to cmDspSysAllocInst() (i.e. kReqArgDsvFl) from supporting
- multiple types.
-
- There are two possible ways to fix this:
- 1) Include the type flag in the var args list.
- 2) Specify a seperate var args flag in cmDspVarArg_t.
- Option 1 seems better because:
- a. [**] It would allow setting other per instance flags in the cmDspSysAllocInst() call [*].
- b. It would support setting a var arg termination flag rather than relying on the current
- explicit argument count.
- c. This is also the method used in cmJsonMemberValues() and it has worked well there.
- Either of these options requires a substantial change to existing code.
- This change should therefore be made sooner rather than later.
-
- As the system currently works it is possible, and it often happens, that
- an instance's recv function will be called before it is reset. It seems like
- this is a bad thing. Maybe the process of resetting and transmitting dflt
- values should be broken into seperate passes. This idea could be extended
- to type checking as follows:
- a. a reset-0 pass is made to set the internal state of each instance to
- known values.
-
- b. a type check pass is made where each output port
- sends a type msg to all connected input ports to provide the types that
- it will send.
-
- c. a reset-1 pass is made allowing all of the type information to be
- used to determine the initial state.
-
- d. an initial default value transmission pass is made where some instances
- (like scalars) may transmit initial values
-
- e. a reset-2 pass is made allowing the initial values to be acted on.
-
- f. runtime occurs
-
- D. Questions:
- 0) What are the ways that an instance variable can get set?
- a. cmDspValueSet() - This is the canonical value setting function.
- All other value setting function call this function.
- The cmDspSetXXX() are type safe wrappers to this function.
-
- b. cmDspApplyDefault() - assign the default value to the actual variable
- via a call to cmDspValueSet(). This function is automatically called
- by cmDspApplyAllDefaults() which is usually in the instance reset function.
-
- c. cmDspSetEvent() - assign the value embedded in an event message
- to a variable. This is a wrapper function for cmDspValueSet() which
- checks to see if the value need to be echoed to the UI. See
- more about UI echoing in the next section.
-
- 1) Where do events arriving at an instance receive function originate from?
- There are two sources of events:
- 1. The output ports of other instances.
- 2. The UI (client application).
-
- Events arriving from the UI can be distinguished from events arriving
- from other instances because they have the evt.kUiDspFl flag set.
-
- 2) How does UI updating and echoing actually work?
- a. Overview:
- On creation (in cmDsUi.c) variables whose value must be reflected to the UI
- are marked with the kUiDsvFl - these variables are called 'UI variables'.
- When a UI variable receive a new value
- the new value must be reflected in the associated UI GUI control.
-
- When and how this is accomplished depends on the source of the new
- variable value. There are two sources of events arriving at
- an instance's receive function: the output port of another instance
- or the UI.
-
- Events which originate from the UI are marked with a evt.kUiDspFl.
- (cmDspSys.c:_cmDspSysHandleUiMsg())
-
- When a UI variable receives a value from the output port of another
- instance (i.e. kUiDspFl not set) then it must always send that value to
- the UI. In other words if the evt.kUiDspFl is NOT set then the
- value must be sent to the UI.
-
- When a UI variable receives a value from the UI (i.e. the evt.kUiDspFl is set)
- it may or may not need to reflect the value. If the UI already
- reflects the value then the value does not need to be sent back
- otherwise it does.
-
- The UI control determines whether it wants to receive the value it
- is sending back by setting the kDuplexDuiFl flag in it's msg to the engine.
- Upon receipt of this msg, in cmDspSys.c:_cmDspSysHandleUiMsg(),
- the system converts the msg to an event. If the kDuplexDuiFl
- is set then the kUiEchoDspFl is set in the event.
-
- When a UI variable receives a value from the UI and the evt.kUiEchoDspFl
- is set then the value must be reflected, otherwise it must not.
-
- The rules for updating UI variables (var. w/ kUiDsvFl set) can
- be summarized as follows:
-
- kUiDspFl kUiEchoDspFl Send to UI Notes
- -------- ------------ ---------- -------------------------------------------------------------
- 0 0 Yes The value originated from another port and therefore must be reflected.
- 0 1 <invalid> If kUiEchoDspFl is set then so must kUiDspFl.
- 1 0 No
- 1 1 Yes The value originated from the UI and ehco was requested.
-
- This logic is automatically handled together by
- cmDspSetEvent() and cmDspValueSet().
-
-
- b. Variable values are sent and received to and from the UI using
- kValueDuiId messages.
-
- c. It is possible to prevent values generated in the engine from being
- reflected to the UI by setting the kNoUpdateUiDspFl in the call to
- cmDspValueSet().
-
- d. Instance variables are marked as UI variables by the cmDspUIXXXCreate()
- functions. Note that instances that have assoicated UI controls generally
- have multiple UI variables. (e.g. min,max,step,label,value).
-
- e. Messages arriving from the UI are handled by cmDspSys.c:_cmDspSysHandleUiMsg()
- where they are converted to cmDspEvt_t's. All events generated from
- msgs arriving from the UI are marked with the kUiDspFl. If the msg kDuplexDuiFl
- is set then the event flag kUiEchoDspFl is set - to indicate that the instance
- should send the value back to the UI.
-
- This leads to the following potential situation: The msg with kUiEchoDspFl
- set arrives at its target instance - which in turn calls cmDspSetEvent() to update
- the target variable value. Because kUiEchoDspFl is set the value is automatically
- reflected to the UI as expected. However as part of the call to
- cmDspValueSet() within cmDspSetEvent() the event is also sent to any connected
- instances - a problem would occur if kUiEchoDspFl remained set when it
- was sent to the connected instances - because they might then also try to update
- their own UI inappropriately. In fact this is not a problem because
- _cmDspSendEvt() zeros the the evt.flags value prior to resending the event.
-
-
- 3) Proposed Data Typing Framework:
- a. Instance variables are assigned 3 data types:
- 1. cmDspSysAllocInst() var args type. This is the type that the var args argument
- to the instance constructor must be. The value provided by this method
- becomes the default value for the variable. This type must be unique - multiple
- type flags cannot be used for this value.
-
- 2. Strict data type. This is the type used to define the variable value.
- Any values which will be used to set the value of this variable must be able
- to be converted to this type.
-
- If the variable is used as on input then the system will warn if an output port
- is assigned which cannot be converted to this type. If the variable is used as
- an output then this is the type the variable will publish as the output type.
-
- All values arriving at the functions 'recv' function are guaranteed to be
- able to be converted to this type.
-
- 3. Alternate data types. Multiple types may be given to this type.
- If no strict data type is given then this will be the set of types accepted
- for input and reported for output.
-
- All value messages for the instance which do not fit the strict data type,
- but do fit the alternate data type, will be sent to the 'altRecvFunc'
- function. The data type of events arriving at this function may therefore
- need to be decoded in order to use the assoicated values.
-
- b. Connections from an output with a strict data type can be type checked in
- advance - since they are guaranteed to emit a specific data type.
-
- Connections from outputs without strict data types can only be type checked
- at runtime - since it is possible for the type to change once the execution
- starts.
-
- c. Other notes about data types:
- It seems like cmDsvValues() should in general only allow one type flag (along with
- the kMtxDsvFl) to be set at a time if the flag state is legal. Is this always the
- case? Can a macro be included to routinely check this? Are we careful to
- not confuse the actual and possible type flags in DSP instance variables?
- This is important when checking the types of variables arriving at in input port.
- Include a macro to test the legality of the actual value type leaving output ports
- and entering input ports.
-
- Note that the cmXXXDsvFl flags have the problem that it is not possible to
- specify some multiple types. For example it is not possible to specify both
- scalar and matrix types simultaneously. Once the matrix flag is set it must
- be assumed that all specific data types then are matrices.
- THIS IS A FUNDAMENTAL PROBLEM THAT MUST BE ADDRESSED.
-
- Note that scalar numeric values can easily be cheaply converted to other numeric
- types. It could be expensive however if vectors required conversion.
- Vectors are currently being passed as pointers. No conversion is occurring.
-
- d. Another proposal:
- Following connection time there is a type determination pass. The network
- is traversed in execution order. Each instance computes and emits the single
- type assigned to each of its output ports. Once emited these types will not
- change during runtime.
-
- Note that this does not preclude an input receiving multiple types.
- If a variable arrives at an input port which does not match the type of
- the variable associated with that type then it is sent to the NoTypeRecv()
- instance function.
-
-
-
- 2. Do values only get sent on change? If not - why not?
- No - based on cmDspClass.ccmDspValueSet() values are transmitted
- whenever they are set - there is not check for a change of value.
-
-
- 3. Write a function to support generating multiple enumerated
- ports - as is required by AMix or ASplit.
- (This is now done: See cmDspClass.h:cmDspArgSetup() )
- Update the existing code to use this scheme. (This still needs to be done.)
- Similar functions need to be written for connecting groups of ports
- in cmDspPgm.c. (This still needs to be done.)
-
-
- 5. It does not appear that the kInDsvFl and kOutDsvFl are actually used during
- the connection process. This means that a variable marked as an output
- could be used as an input and v.v.. Likewise variable marked as neither input
- nor output could be accessed. What are the input and output flags actually
- used for?
-
- In fact kInDsvFl and kOutDsvFl are not used at all. (3/18/12).
-
- It might be nice to allow everything to be an output - but to
- force inputs to be explicitely named.
-
-
- 6. Is there any implication for marking a variable as both an input and an output?
- 7. The audio buffers allocated in cmDspInstAlloc() may not be memory aligned.
- 8. Is there a way to add a generic enable/disable function to all instances?
- 9. Should all output audio buffers be zeroed by default during _cmDspXXXReset()?
- 10. Is the kConstDsvFl used? respected? necessary?
- yes - it is necessary because some variables cannot be changed after the constructor
- is completed. For example any instance that take an argument giving the
- number of ports as a variable. The port count argument cannot change because it
- might invalidate connections which had been already made to the existing ports.
- TODO: find all variables which cannot be changed after the constructor and mark
- them as const and prevent them from being the target of connections or events.
-
- 11. Is it OK to not assign a variable as either an input or an output. (this would
- allow it to be set from cmDspSysAllocInst() but then only changed internally).
-
- 12. Write some template DSP instances that provide commented examples of the
- common scenarios which an actual instance might encounter.
-
- 13. All errors in instances should use cmDspClassErr() or cmDspInstErr() not cmErrMsg().
- Update existing code.
-
- 14. The way that the master controls are created is wrong. The master controls should
- be created during the cmDspSysLoad() process rather than being created in kcApp.cpp.
- This would make them essentially identical to other controls - and would allow the
- master controls to be manipulated easily from inside a DSP instance.
-
- 15. The code for creating and decoding messages seems to be distributed everywhere.
- All of this functionality should be moved to cmMsgProtocol.c. See the code
- for encoding/decoding messages in cmAudioSys.c as an example.
-
- 16. The default behavior of buttons should be to to NOT send out their default values or
- symbols on instance reset. Determining whether an output value is sent on instance
- reset (as they all are currently ??? or are only UI sent out on reset???)
- could be another argument flag setting [**].
-
- 17. cmDspInstAlloc() should include another version called cmDspInstAllocV()
- which takes the cmDspArg_t fields as var args. This would allow
- array variables which currently use cmDspArgSetupN() to be given
- in one call - which would be less error prone than using cmDspArgSetupN().
-
- 18. Add helper functions to create common dsp instances like:
- scalar,button,check,file,audio in,audio out. These functions should
- support default values through literals or through resource paths.
-
- 19. Design a sub-net function for making sub-nets of instance nets
- that can then be treated like instances themselves.
- For example make a network of audio sources:
- audio file, signal generator, audio input, with gain and frequency
- controls.
-
- 20. Network construction (cmDspPgm.c) is divided into two parts.
- First the instances are allocated and then they are connected.
- There should always be a test for a failure between construction
- phase and the connection phase and then again after the connection phase.
-
- 21. The existing instances are not using cmReal_t and cmSample_t as they should.
-
- 22. It is possible for cmDspInstAlloc() to fail in an instance constructor and
- yet we are not testing for it in many instances.
- When a failure occurs after cmDspInstAlloc() how is the instance deleted
- prior to returning? ... is it necessary to delete it prior to returning?
-
- 23. For instances which act as files and which take a file name as at an input
- port - the correct way to implement the object is to open/reopen the file
- both on reset and when a new file name is received.
- The reset open covers the case where the default filename is used.
- The receieve open covers the case where a filename is received via the input port.
-
- 24. After each call to an instance member function (reset,recv,exec,etc.) the
- interal error object should be checked. This way an invalid state can
- be signaled inside one of the functions without having to worry about
- propagating the error to the return value. THis means that as long as
- a member function can report and safely complete it doesn't have to do
- much error handling internally.
-
- 25. As it is currently implemented all audio system sub-system share a
- single UDP network managers. This is NOT thread-safe. If more than
- one audio sub-system is actually used the program will crash.
- This can be solved by giving each sub-system it's own UDP network
- manager, where each sub-system is given it's own port number.
-
- */
-
- //==========================================================================================================================================
- enum
- {
- kLblPrId,
- kMsPrId,
- kInPrId
-
- };
-
- cmDspClass_t _cmPrinterDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- unsigned limitCycles;
- } cmDspPrinter_t;
-
- cmDspInst_t* _cmDspPrinterAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "lbl", kLblPrId, 0, 0, kInDsvFl | kStrzDsvFl | kOptArgDsvFl, "Label" },
- { "ms", kMsPrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Period"},
- { "in", kInPrId, 0, 0, kInDsvFl | kTypeDsvMask, "Input port" },
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspPrinter_t* p = cmDspInstAlloc(cmDspPrinter_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
- cmDspSetDefaultUInt(ctx,&p->inst,kMsPrId, 0, 0 );
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspPrinterReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspPrinter_t* p = (cmDspPrinter_t*)inst;
- p->limitCycles = ctx->cycleCnt;
- return kOkDspRC;
- }
-
-
- cmDspRC_t _cmDspPrinterRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspPrinter_t* p = (cmDspPrinter_t*)inst;
-
- if( evt->dstVarId == kInPrId && ctx->cycleCnt >= p->limitCycles )
- {
- p->limitCycles = ctx->cycleCnt + (unsigned)(cmDspUInt(inst,kMsPrId) * cmDspSampleRate(ctx) / (1000.0 * cmDspSamplesPerCycle(ctx)) );
-
- const cmChar_t* lbl = cmDspStrcz(inst,kLblPrId);
-
- if( cmDsvIsSymbol(evt->valuePtr) )
- cmRptPrintf(ctx->rpt,"%s'%s'",lbl==NULL?"":lbl,cmStringNullGuard(cmSymTblLabel(ctx->stH,cmDsvSymbol(evt->valuePtr))));
- else
- cmDsvPrint(evt->valuePtr,lbl,ctx->rpt);
-
- cmRptPrint(ctx->rpt,"\n");
- }
-
- return kOkDspRC;
- }
-
- struct cmDspClass_str* cmPrinterClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmPrinterDC,ctx,"Printer",
- NULL,
- _cmDspPrinterAlloc,
- NULL,
- _cmDspPrinterReset,
- NULL,
- _cmDspPrinterRecv,
- NULL,
- NULL,
- "Print the value of any event arriving at 'in'.");
-
- return &_cmPrinterDC;
- }
-
- //==========================================================================================================================================
- enum
- {
- kMinCntId,
- kMaxCntId,
- kIncCntId,
- kWrapCntId,
- kOutCntId,
- kCycCntId,
- kNxtCntId,
- };
-
- cmDspClass_t _cmCounterDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- double val;
- bool disableFl;
- } cmDspCounter_t;
-
-
- cmDspRC_t _cmDspCounterValidate( cmDspInst_t* inst, double min, double max, double inc )
- {
- if( max < min )
- return cmErrMsg(&inst->classPtr->err,kInvalidArgDspRC,"The counter maximum (%f) value must be greater than the counter minimum (%f) value.",max,min);
-
- if( max - min < inc )
- 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);
-
- return kOkDspRC;
- }
-
- cmDspInst_t* _cmDspCounterAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "min", kMinCntId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "minimum" },
- { "max", kMaxCntId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "maximum" },
- { "inc", kIncCntId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "increment"},
- { "wrap", kWrapCntId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "wrap"},
- { "out", kOutCntId, 0, 0, kOutDsvFl | kDoubleDsvFl, "out"},
- { "cycles", kCycCntId, 0, 0, kOutDsvFl | kDoubleDsvFl, "cycles"},
- { "next", kNxtCntId, 0, 0, kInDsvFl | kTypeDsvMask, "next"},
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspCounter_t* p = cmDspInstAlloc(cmDspCounter_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
- double min = cmDspDefaultDouble( &p->inst, kMinCntId );
- double max = cmDspDefaultDouble( &p->inst, kMaxCntId );
- double inc = cmDspDefaultDouble( &p->inst, kIncCntId );
-
- if( _cmDspCounterValidate(&p->inst, min, max, inc ) != kOkDspRC )
- return NULL;
-
- cmDspSetDefaultBool( ctx, &p->inst, kWrapCntId, false, true);
- cmDspSetDefaultDouble( ctx, &p->inst, kOutCntId, 0.0, min );
- cmDspSetDefaultDouble( ctx, &p->inst, kCycCntId, 0.0, 0.0 );
- return &p->inst;
- }
-
- cmDspRC_t _cmDspCounterReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspCounter_t* p = (cmDspCounter_t*)inst;
- cmDspApplyAllDefaults(ctx,inst);
- p->val = cmDspDouble(inst,kMinCntId);
- p->disableFl = false; // the default values must be ok or the constructor fails
- return kOkDspRC;
- }
-
- void _cmDspCounterIncr( cmDspCtx_t* ctx, cmDspInst_t* inst )
- {
- cmDspCounter_t* p = (cmDspCounter_t*)inst;
- double min = cmDspDouble(inst,kMinCntId);
- double max = cmDspDouble(inst,kMaxCntId);
- double inc = cmDspDouble(inst,kIncCntId);
- bool wrapFl = cmDspBool(inst,kWrapCntId);
- bool limitFl = min <= max;
-
- // If min > max then no upper/lower limit is set on the value.
- // In this case the ouput will continue to increment and
- // no 'cycle' output will be generated.
-
- // If wrapFl is not set then the 'cycle' output will fire
- // exactly once when the counter crosses its limit.
-
- // if the new value is in range then send it
- if( min <= p->val && p->val < max )
- cmDspSetDouble( ctx, inst, kOutCntId, p->val );
-
-
- // the current value is out of range and wrap flag is not set
- if( limitFl && (p->val < min || p->val >= max) && (wrapFl==false))
- return;
-
- // do the increment
- p->val += inc;
-
- // if the new value is out of range
- if( limitFl && (p->val < min || p->val >= max) )
- {
- // if wrapping is allowed
- if( wrapFl )
- {
- if( p->val >= max )
- p->val = min + (p->val - max); // wrap to begin
- else
- if( p->val < min )
- p->val = max - (min - p->val); // wrap to end
- }
-
- // increment the cycle counter
- cmDspSetDouble( ctx, inst, kCycCntId, cmDspDouble( inst, kCycCntId ) + 1 );
- }
-
- }
-
- cmDspRC_t _cmDspCounterRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspCounter_t* p = (cmDspCounter_t*)inst;
- cmDspRC_t rc = kOkDspRC;
-
- switch( evt->dstVarId )
- {
- case kWrapCntId:
- cmDspSetEvent(ctx,inst, evt );
- break;
-
- case kMinCntId:
- case kMaxCntId:
- case kIncCntId:
- {
- cmDspSetEvent( ctx, inst, evt );
- double min = cmDspDouble( inst, kMinCntId );
- double max = cmDspDouble( inst, kMaxCntId );
- double inc = cmDspDouble( inst, kIncCntId );
- p->disableFl = (rc = _cmDspCounterValidate(inst, min, max, inc)) != kOkDspRC;
-
- }
- break;
-
- case kNxtCntId:
- if( !p->disableFl )
- _cmDspCounterIncr(ctx,inst);
- break;
-
- default:
- { assert(0); }
- }
-
- return rc;
- }
-
-
-
- struct cmDspClass_str* cmCounterClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmCounterDC,ctx,"Counter",
- NULL,
- _cmDspCounterAlloc,
- NULL,
- _cmDspCounterReset,
- NULL,
- _cmDspCounterRecv,
- NULL,
- NULL,
- "Counter object. Set min => max to have no limit on the value." );
-
- return &_cmCounterDC;
- }
-
-
-
- //==========================================================================================================================================
- enum
- {
- kMaxPhId,
- kMultPhId,
- kPhsPhId,
- kOutPhId
- };
-
- cmDspClass_t _cmPhasorDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- } cmDspPhasor_t;
-
-
- cmDspInst_t* _cmDspPhasorAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- unsigned chs = 1;
-
- cmDspVarArg_t args[] =
- {
- { "max", kMaxPhId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Maximum accumulator value" },
- { "mult", kMultPhId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Increment multiplier" },
- { "phs", kPhsPhId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Next phase value" },
- { "out", kOutPhId, 0, chs, kOutDsvFl | kAudioBufDsvFl, "Audio output." },
- { NULL, 0, 0, 0, 0 }
- };
-
- // allocate the instance
- cmDspPhasor_t* p = cmDspInstAlloc(cmDspPhasor_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
- // assign default values to any of the the optional arg's which may not have been set from vl.
- cmDspSetDefaultSample(ctx, &p->inst, kMaxPhId, 0.0, cmSample_MAX);
- cmDspSetDefaultSample(ctx, &p->inst, kMultPhId, 0.0, 1.0);
- cmDspSetDefaultDouble(ctx, &p->inst, kPhsPhId, 0.0, 0.0);
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspPhasorReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspApplyAllDefaults(ctx,inst);
- cmDspZeroAudioBuf( ctx, inst, kOutPhId );
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspPhasorExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmSample_t* bp = cmDspAudioBuf(ctx,inst,kOutPhId,0);
- const cmSample_t* ep = bp + cmDspAudioBufSmpCount(ctx,inst,kOutPhId,0);
- cmSample_t mult = cmDspSample(inst,kMultPhId);
- cmSample_t max = cmDspSample(inst,kMaxPhId);
- double phs = cmDspDouble(inst,kPhsPhId);
- cmSample_t inc = mult;
-
- for(; bp<ep; ++bp)
- {
- while( phs >= max )
- phs -= max;
-
- *bp = (cmSample_t)phs;
-
- phs += inc;
-
- }
-
- cmDspSetSample(ctx,inst,kPhsPhId,phs);
-
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspPhasorRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- switch( evt->dstVarId )
- {
- case kMultPhId:
- case kMaxPhId:
- case kPhsPhId:
- cmDspSetEvent(ctx, inst, evt );;
- break;
-
- default:
- { assert(0); }
- }
-
- return kOkDspRC;
- }
-
- struct cmDspClass_str* cmPhasorClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmPhasorDC,ctx,"Phasor",
- NULL,
- _cmDspPhasorAlloc,
- NULL,
- _cmDspPhasorReset,
- _cmDspPhasorExec,
- _cmDspPhasorRecv,
- NULL,
- NULL,
- "Ramp wave signal generator.");
-
- return &_cmPhasorDC;
- }
-
- //==========================================================================================================================================
- enum
- {
- kHzSgId,
- kShapeSgId,
- kGainSgId,
- kOtCntSgId,
- kOutSgId
- };
-
- enum
- {
- kWhiteSgId, // 0
- kPinkSgId, // 1
- kSineSgId, // 2
- kCosSgId, // 3
- kSawSgId, // 4
- kSqrSgId, // 5
- kTriSgId, // 6
- kPulseSgId, // 7
- kPhasorSgId // 8
- };
-
- cmDspClass_t _cmSigGenDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- cmReal_t phs;
- cmSample_t reg;
- } cmDspSigGen_t;
-
- cmDspInst_t* _cmDspSigGenAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "hz", kHzSgId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Signal frequency in Hertz." },
- { "shape", kShapeSgId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Wave shape 0=sine 1=cosine 2=white 3=pink" },
- { "gain", kGainSgId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Output gain."},
- { "ot", kOtCntSgId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Overtone count."},
- { "out", kOutSgId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output." },
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspSigGen_t* p = cmDspInstAlloc(cmDspSigGen_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
- cmDspSetDefaultDouble(ctx, &p->inst, kHzSgId, 0.0, 1000);
- cmDspSetDefaultUInt( ctx, &p->inst, kShapeSgId, 0, 0);
- cmDspSetDefaultUInt( ctx, &p->inst, kOtCntSgId, 0, 0);
- cmDspSetDefaultDouble(ctx, &p->inst, kGainSgId, 0.0, 0.9 );
-
- return &p->inst;
- }
-
-
- cmDspRC_t _cmDspSigGenReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspSigGen_t* p = (cmDspSigGen_t*)inst;
-
- p->phs = 0;
- cmDspApplyAllDefaults(ctx,inst);
- cmDspZeroAudioBuf( ctx, inst, kOutSgId );
- p->reg = 0;
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspSigGenExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
-
- cmDspSigGen_t* p = (cmDspSigGen_t*)inst;
- unsigned chIdx = 0;
- cmSample_t* bp = cmDspAudioBuf(ctx,inst,kOutSgId,chIdx);
- unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutSgId,chIdx);
- const cmSample_t* ep = bp + n;
- double hz = cmDspDouble(inst,kHzSgId);
- double sr = cmDspSampleRate(ctx);
- double fact = 2.0 * M_PI * hz / sr;
- int shape = cmDspUInt(inst,kShapeSgId);
- double offs = shape == kCosSgId ? (-M_PI / 2.0) : 0;
- double gain = cmDspDouble(inst,kGainSgId);
- unsigned otCnt = cmDspUInt(inst,kOtCntSgId);
-
- switch( shape )
- {
- case kWhiteSgId:
- while( bp < ep )
- *bp++ = gain * 2.0 * ((cmSample_t)rand() / RAND_MAX - 0.5);
- break;
-
- case kPinkSgId:
- while( bp < ep )
- {
- cmSample_t s = gain * 2.0 * ((cmSample_t)rand() / RAND_MAX - 0.5);
- *bp++ = (s + p->reg)/2;
- p->reg = s;
- }
- break;
-
- case kSineSgId:
- case kCosSgId:
- while( bp<ep )
- {
- *bp++ = (cmSample_t)(gain * sin( fact * p->phs + offs ));
-
- p->phs += 1.0;
-
- }
- break;
-
-
- case kSawSgId:
- p->phs = cmVOS_SynthSawtooth(bp,n,(unsigned)p->phs,sr,hz,otCnt);
- cmVOS_MultVS(bp,n,gain);
- break;
-
- case kSqrSgId:
- p->phs = cmVOS_SynthSquare( bp,n,(unsigned)p->phs,sr,hz,otCnt );
- cmVOS_MultVS(bp,n,gain);
- break;
-
- case kTriSgId:
- p->phs = cmVOS_SynthTriangle( bp,n,(unsigned)p->phs,sr,hz,otCnt );
- cmVOS_MultVS(bp,n,gain);
- break;
-
- case kPulseSgId:
- p->phs = cmVOS_SynthPulseCos( bp,n,(unsigned)p->phs,sr,hz,otCnt );
- cmVOS_MultVS(bp,n,gain);
- break;
-
- case kPhasorSgId:
- p->phs = cmVOS_SynthPhasor( bp,n,(unsigned)p->phs,sr,hz );
- cmVOS_MultVS(bp,n,gain);
- break;
-
- default:
- { assert(0); }
- }
- return kOkDspRC;
- }
-
-
- cmDspRC_t _cmDspSigGenRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc;
- if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
- {
- switch( evt->dstVarId )
- {
- case kShapeSgId:
- //printf("%s %i\n",cmDspInstLabel(ctx,inst),cmDspUInt(inst,kShapeSgId));
- break;
- }
- }
- return rc;
- }
-
-
-
- struct cmDspClass_str* cmSigGenClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmSigGenDC,ctx,"SigGen",
- NULL,
- _cmDspSigGenAlloc,
- NULL,
- _cmDspSigGenReset,
- _cmDspSigGenExec,
- _cmDspSigGenRecv,
- NULL,
- NULL,
- "Variable frequency and waveshape signal generator." );
-
- return &_cmSigGenDC;
- }
-
- //==========================================================================================================================================
- enum
- {
- kChAiId,
- kGainAiId,
- kOutAiId
- };
-
- cmDspClass_t _cmAudioInDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- bool errFl; // used to control error reporting
- } cmDspAudioIn_t;
-
- cmDspInst_t* _cmDspAudioInAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "ch", kChAiId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Audio input channel index"},
- { "gain", kGainAiId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Input gain multiplier" },
- { "out", kOutAiId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output" },
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspAudioIn_t* p = cmDspInstAlloc(cmDspAudioIn_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
- cmDspSetDefaultUInt( ctx, &p->inst, kChAiId, 0, 0);
- cmDspSetDefaultDouble( ctx, &p->inst, kGainAiId, 0, 1.0);
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspAudioInReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspAudioIn_t* p = (cmDspAudioIn_t*)inst;
- p->errFl = false;
- cmDspApplyAllDefaults(ctx,inst);
-
- return rc;
- }
-
-
- cmDspRC_t _cmDspAudioInExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- unsigned chIdx = cmDspUInt(inst,kChAiId);
- unsigned iChCnt = ctx->ctx->iChCnt;
- cmDspAudioIn_t* p = (cmDspAudioIn_t*)inst;
- double gain = cmDspDouble(inst,kGainAiId);
-
- if( chIdx >= iChCnt )
- {
- if( p->errFl== false )
- {
- cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The input channel index %i is invalid. Channel count:%i.",chIdx,iChCnt);
- p->errFl = true;
- }
-
- cmDspZeroAudioBuf(ctx,inst,kOutAiId);
-
- return kOkDspRC;
- }
-
- //unsigned n = cmDspSamplesPerCycle(ctx);
- unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutAiId,0);
- cmSample_t* dp = cmDspAudioBuf(ctx,inst,kOutAiId,0);
-
-
- assert( n == cmDspAudioBufSmpCount(ctx,inst,kOutAiId,chIdx));
- assert(dp != NULL);
-
- // if this channel is disabled then iChArray[chIdx] will be NULL
- if( ctx->ctx->iChArray[chIdx]!=NULL )
- cmVOS_MultVVS(dp,n,ctx->ctx->iChArray[chIdx],(cmSample_t)gain);
-
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspAudioInRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- switch( evt->dstVarId )
- {
- case kChAiId:
- if( cmDspSetEvent(ctx,inst,evt) != kOkDspRC )
- {
- // if the exec callback was previously disabled and the new channel value is valid then re-enable the exec callback.
- if( inst->execFunc==NULL && cmDspUInt(inst,kChAiId) < ctx->ctx->iChCnt )
- inst->execFunc = _cmDspAudioInExec;
- }
- break;
-
- case kGainAiId:
- cmDspSetEvent(ctx,inst,evt);
- break;
- }
- return rc;
- }
-
- struct cmDspClass_str* cmAudioInClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmAudioInDC,ctx,"AudioIn",
- NULL,
- _cmDspAudioInAlloc,
- NULL,
- _cmDspAudioInReset,
- _cmDspAudioInExec,
- _cmDspAudioInRecv,
- NULL,
- NULL,
- "Audio output port");
-
- return &_cmAudioInDC;
- }
-
-
-
-
- //==========================================================================================================================================
- enum
- {
- kChAoId,
- kGainAoId,
- kInAoId
- };
-
- cmDspClass_t _cmAudioOutDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- } cmDspAudioOut_t;
-
- cmDspInst_t* _cmDspAudioOutAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "ch", kChAoId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Audio output channel index"},
- { "gain",kGainAoId,0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Output gain multiplier"},
- { "in", kInAoId, 0, 1, kInDsvFl | kAudioBufDsvFl, "Audio input" },
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspAudioOut_t* p = cmDspInstAlloc(cmDspAudioOut_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
-
- cmDspSetDefaultUInt( ctx, &p->inst, kChAoId, 0, 0);
- cmDspSetDefaultDouble( ctx, &p->inst, kGainAoId, 0, 1.0);
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspAudioOutReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspApplyAllDefaults(ctx,inst);
- return rc;
- }
-
-
- cmDspRC_t _cmDspAudioOutExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- unsigned chIdx = cmDspUInt(inst,kChAoId);
- unsigned oChCnt = ctx->ctx->oChCnt;
- double gain = cmDspDouble(inst,kGainAoId);
-
- if( chIdx >= oChCnt )
- {
- rc = cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The output channel index %i is invalid. Channel count:%i.",chIdx,oChCnt);
- inst->execFunc = NULL; // disable callbacks - this prevents the error msg from repeating
- return rc;
- }
-
- const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kInAoId,0);
-
- if( sp == NULL )
- {
- inst->execFunc = NULL; // if there is no connected input then disable further callbacks
- return kOkDspRC;
- }
-
- unsigned n = cmDspSamplesPerCycle(ctx);
-
- assert( n == cmDspVarRows(inst,kInAoId) );
-
- // if this channel is disabled or set to pass-through then chArray[chIdx] will be NULL
- if( ctx->ctx->oChArray[chIdx] != NULL )
- cmVOS_MultVVS(ctx->ctx->oChArray[chIdx],n,sp,(cmSample_t)gain);
-
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspAudioOutRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- switch( evt->dstVarId )
- {
- case kChAoId:
- if( cmDspSetEvent(ctx,inst,evt) != kOkDspRC )
- {
- // if the exec callback was previously disabled and the new channel value is valid then re-enable the exec callback.
- if( inst->execFunc==NULL && cmDspUInt(inst,kChAoId) < ctx->ctx->oChCnt )
- inst->execFunc = _cmDspAudioOutExec;
- }
- break;
-
- case kGainAoId:
- cmDspSetEvent(ctx,inst,evt);
- break;
- }
- return rc;
- }
-
- struct cmDspClass_str* cmAudioOutClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmAudioOutDC,ctx,"AudioOut",
- NULL,
- _cmDspAudioOutAlloc,
- NULL,
- _cmDspAudioOutReset,
- _cmDspAudioOutExec,
- _cmDspAudioOutRecv,
- NULL,NULL,
- "Audio output port");
-
- return &_cmAudioOutDC;
- }
-
- //==========================================================================================================================================
- enum
- {
- kFnAofId,
- kChCntAofId,
- kGain0AofId,
- kGain1AofId,
- kIn0AofId,
- kIn1AofId,
- kSelAofId
- };
-
- cmDspClass_t _cmAudioFileOutDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- cmSample_t* smpBuf;
- unsigned smpCnt;
- unsigned bits;
- cmAudioFileH_t afH;
- unsigned openSymId;
- unsigned closeSymId;
- } cmDspAudioFileOut_t;
-
- cmDspRC_t _cmDspAudioFileOutCreateFile( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned chCnt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;
- const cmChar_t* fn = cmDspStrcz(inst,kFnAofId);
-
- if(cmAudioFileIsValid(p->afH) )
- cmAudioFileDelete(&p->afH);
-
- if( cmAudioFileIsValid(p->afH = cmAudioFileNewCreate(fn, cmDspSampleRate(ctx), p->bits, chCnt, &rc, ctx->rpt )) == false )
- rc = cmDspClassErr(ctx,inst->classPtr,kVarArgParseFailDspRC,"The output audio file '%s' create failed.",fn);
-
- return rc;
- }
-
- cmDspInst_t* _cmDspAudioFileOutAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "fn", kFnAofId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Audio file name"},
- { "chs", kChCntAofId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Channel count"},
- { "gain0", kGain0AofId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Output gain 0 multiplier"},
- { "gain1", kGain1AofId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Output gain 1 multiplier"},
- { "in0", kIn0AofId, 0, 1, kInDsvFl | kAudioBufDsvFl, "Audio input 0"},
- { "in1", kIn1AofId, 0, 1, kInDsvFl | kAudioBufDsvFl, "Audio input 1"},
- { "sel", kSelAofId, 0, 0, kInDsvFl, "Open | Close"},
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspAudioFileOut_t* p = cmDspInstAlloc(cmDspAudioFileOut_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
- cmDspValue_t chCntVal;
- cmDsvSetUInt(&chCntVal,cmMin(2,cmDspUInt(&p->inst,kChCntAofId)));
-
- cmDspSetDefault(ctx, &p->inst, kChCntAofId, &chCntVal );
-
- cmDspSetDefaultDouble( ctx, &p->inst, kGain0AofId, 0, 1.0);
- cmDspSetDefaultDouble( ctx, &p->inst, kGain1AofId, 0, 1.0);
-
- p->bits = 16;
- p->afH = cmNullAudioFileH;
- p->openSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"open");
- p->closeSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"close");
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspAudioFileOutReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;
- unsigned chCnt = cmDspUInt(inst,kChCntAofId);
-
- cmDspApplyAllDefaults(ctx,inst);
-
- p->smpCnt = cmDspSamplesPerCycle(ctx) * chCnt;
- p->smpBuf = cmLhResizeN(ctx->lhH, cmSample_t, p->smpBuf, p->smpCnt);
-
- rc = _cmDspAudioFileOutCreateFile( ctx, inst, chCnt );
-
- return rc;
- }
-
- cmDspRC_t _cmDspAudioFileOutExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;;
- unsigned chCnt = cmMin(2,cmDspUInt(inst,kChCntAofId));
- unsigned smpCnt = 0;
- cmSample_t* chArray[chCnt];
- unsigned i,j;
-
- for(i=0,j=0; i<chCnt; ++i)
- {
- unsigned chVarId = i == 0 ? kIn0AofId : kIn1AofId; // get audio buf var id for this ch
- unsigned iSmpCnt = cmDspVarRows(inst,chVarId);
-
- if( iSmpCnt == 0 )
- {
- chArray[j] = NULL;
- }
- else
- {
- cmSample_t gain = cmDspSample(inst,i==0?kGain0AofId:kGain1AofId); // get ch gain
-
- chArray[j] = cmDspAudioBuf(ctx,inst,chVarId,i); // get incoming audio buf ptr
-
- if( gain != 1.0 )
- cmVOS_MultVVS(chArray[j], iSmpCnt, chArray[j], gain); // apply gain
-
- ++j; // incr chArray[] index
- assert( smpCnt==0 || iSmpCnt==smpCnt);
- smpCnt = iSmpCnt; // set outgoing sample count
- }
-
- }
-
- // write the samples
- if( cmAudioFileWriteSample(p->afH, smpCnt, j, chArray ) != kOkAfRC )
- rc = cmDspClassErr(ctx,inst->classPtr,kFileWriteFailDspRC,"An audio output file write failed.");
-
- return rc;
- }
-
- cmDspRC_t _cmDspAudioFileOutRecv( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- switch( evt->dstVarId )
- {
- case kGain1AofId:
- case kGain0AofId:
- cmDspSetEvent(ctx,inst,evt);
- break;
-
- case kFnAofId:
- cmDspSetEvent(ctx,inst,evt);
- rc = _cmDspAudioFileOutCreateFile(ctx,inst,cmDspUInt(inst,kChCntAofId));
- break;
-
- case kSelAofId:
- {
- cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;
- unsigned symId = cmDsvSymbol(evt->valuePtr);
- if( symId == p->openSymId )
- rc = _cmDspAudioFileOutCreateFile(ctx,inst,cmDspUInt(inst,kChCntAofId));
- else
- {
- if( symId == p->closeSymId )
- {
- if(cmAudioFileIsValid(p->afH) )
- cmAudioFileDelete(&p->afH);
- }
- else
- {
- rc = cmErrMsg(&inst->classPtr->err,kInvalidArgDspRC,"Unknown selector symbol (%i) %s.",symId,cmStringNullGuard(cmSymTblLabel(ctx->stH,symId)));
- }
- }
- }
- }
- return rc;
- }
-
- cmDspRC_t _cmDspAudioFileOutFree( cmDspCtx_t* ctx, struct cmDspInst_str* inst, const cmDspEvt_t* evtPtr )
- {
- cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;
-
- if(cmAudioFileIsValid(p->afH) )
- cmAudioFileDelete(&p->afH);
-
- return kOkDspRC;
- }
-
- struct cmDspClass_str* cmAudioFileOutClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmAudioFileOutDC,ctx,"AudioFileOut",
- NULL,
- _cmDspAudioFileOutAlloc,
- _cmDspAudioFileOutFree,
- _cmDspAudioFileOutReset,
- _cmDspAudioFileOutExec,
- _cmDspAudioFileOutRecv,
- NULL,NULL,
- "Audio file output port");
-
- return &_cmAudioFileOutDC;
- }
-
- //==========================================================================================================================================
- enum
- {
- kTypScId,
- kMinScId,
- kMaxScId,
- kStpScId,
- kValScId,
- kLblScId,
- kSendScId
- };
-
- cmDspClass_t _cmScalarDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- } cmDspScalar_t;
-
- cmDspInst_t* _cmDspScalarAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "typ", kTypScId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Type" },
- { "min", kMinScId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Minimum value"},
- { "max", kMaxScId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Maximum value"},
- { "step", kStpScId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Step value (set to 0 to ignore)"},
- { "val", kValScId, 0, 0, kInDsvFl | kOutDsvFl | kDoubleDsvFl | kReqArgDsvFl | kSendDfltDsvFl, "Current value"},
- { "lbl", kLblScId, 0, 0, kStrzDsvFl | kOptArgDsvFl, "Label"},
- { "send", kSendScId, 0, 0, kInDsvFl | kTypeDsvMask, "Send value on any msg."},
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspScalar_t* p = cmDspInstAlloc(cmDspScalar_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
- cmDspSetDefaultUInt( ctx, &p->inst, kTypScId, 0, kNumberDuiId);
- cmDspSetDefaultDouble(ctx, &p->inst, kMinScId, 0.0, 0);
- cmDspSetDefaultDouble(ctx, &p->inst, kMaxScId, 0.0, 1);
- cmDspSetDefaultDouble(ctx, &p->inst, kStpScId, 0.0, 0);
-
- unsigned typeId = cmDspDefaultUInt(&p->inst,kTypScId);
-
- // create the UI control
- cmDspUiScalarCreate(ctx,&p->inst,typeId,kMinScId,kMaxScId,kStpScId,kValScId,kLblScId);
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspScalarReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspApplyAllDefaults(ctx,inst);
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspScalarRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
-
- if( evt->dstVarId == kSendScId )
- {
- // NOT SURE IF THIS IS CORRECT OR NOT - IT SHOULD FORCE THE CURRENT VALUE TO BE
- // SENT - THE CURRENT VALUE SHOULD ALREADY BE AT THE UI SO THERE DOESN'T SEEM
- // TO BE ANY REASON TO UPDATE IT THERE.
- cmDspSetDouble( ctx, inst, kValScId, cmDspDouble(inst,kValScId ));
- return kOkDspRC;
- }
-
- switch( evt->dstVarId )
- {
- case kMinScId:
- case kMaxScId:
- case kStpScId:
- case kValScId:
- cmDspSetEvent(ctx,inst,evt);
- break;
-
- default:
- {assert(0);}
- }
-
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspScalarPresetRdWr( cmDspCtx_t* ctx, cmDspInst_t* inst, bool storeFl )
- {
- return cmDspVarPresetRdWr(ctx,inst,kValScId,storeFl);
- }
-
- struct cmDspClass_str* cmScalarClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmScalarDC,ctx,"Scalar",
- NULL,
- _cmDspScalarAlloc,
- NULL,
- _cmDspScalarReset,
- NULL,
- _cmDspScalarRecv,
- _cmDspScalarPresetRdWr,
- NULL,
- "Scalar value control.");
-
- return &_cmScalarDC;
- }
-
- //==========================================================================================================================================
- enum
- {
- kValTxId,
- kLblTxId
- };
-
- cmDspClass_t _cmTextDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- } cmDspText_t;
-
- cmDspInst_t* _cmDspTextAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "val", kValTxId, 0, 0, kInDsvFl | kOutDsvFl | kStrzDsvFl | kReqArgDsvFl | kSendDfltDsvFl, "Current string"},
- { "lbl", kLblTxId, 0, 0, kStrzDsvFl | kOptArgDsvFl, "Label"},
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspText_t* p = cmDspInstAlloc(cmDspText_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
- // create the UI control
- cmDspUiTextCreate(ctx,&p->inst,kValTxId,kLblTxId);
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspTextReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspApplyAllDefaults(ctx,inst);
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspTextRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
-
- switch( evt->dstVarId )
- {
- case kValTxId:
- cmDspSetEvent(ctx,inst,evt);
- break;
-
- default:
- {assert(0);}
- }
-
- return kOkDspRC;
- }
-
- struct cmDspClass_str* cmTextClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmTextDC,ctx,"Text",
- NULL,
- _cmDspTextAlloc,
- NULL,
- _cmDspTextReset,
- NULL,
- _cmDspTextRecv,
- NULL,NULL,
- "Text value control.");
-
- return &_cmTextDC;
- }
-
-
- //==========================================================================================================================================
- enum
- {
- kInMtId,
- kMinMtId,
- kMaxMtId,
- kTimeMtId,
- kLblMtId,
- };
-
- cmDspClass_t _cmMeterDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- unsigned updSmpCnt;
- unsigned lastCycleCnt;
- double value;
- unsigned cnt;
- } cmDspMeter_t;
-
- cmDspInst_t* _cmDspMeterAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "in", kInMtId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Current value"},
- { "min", kMinMtId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Minimum value"},
- { "max", kMaxMtId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Maximum value"},
- { "time", kTimeMtId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "UI update time in milliseconds (default:50)"},
- { "label",kLblMtId, 0, 0, kStrzDsvFl, "Label"},
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspMeter_t* p = cmDspInstAlloc(cmDspMeter_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
- cmDspSetDefaultDouble(ctx, &p->inst, kInMtId, 0.0, 0);
- cmDspSetDefaultDouble(ctx, &p->inst, kMinMtId, 0.0, 0);
- cmDspSetDefaultDouble(ctx, &p->inst, kMaxMtId, 0.0, 1);
- cmDspSetDefaultDouble(ctx, &p->inst, kTimeMtId, 0.0, 50.0);
-
- // create the UI control
- cmDspUiMeterCreate(ctx,&p->inst,kMinMtId,kMaxMtId,kInMtId,kLblMtId);
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspMeterReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspMeter_t* p = (cmDspMeter_t*)inst;
-
- cmDspApplyAllDefaults(ctx,inst);
-
- double updateMs = cmDspDouble(inst,kTimeMtId);
-
- p->updSmpCnt = floor(cmDspSampleRate(ctx) * updateMs / 1000.0);
- p->lastCycleCnt = 0;
- p->cnt = 0;
- p->value = 0;
- return kOkDspRC;
- }
-
-
-
- cmDspRC_t _cmDspMeterExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspMeter_t* p = (cmDspMeter_t*)inst;
- double curVal = p->value;
- bool deltaFl = p->cnt!=0 && curVal != cmDspDouble(inst,kInMtId);
- bool expireFl = (ctx->cycleCnt - p->lastCycleCnt) * cmDspSamplesPerCycle(ctx) > p->updSmpCnt;
-
- // if the meter value changed and the update time has expired
- if( deltaFl && expireFl )
- {
- cmDspSetDouble(ctx,inst,kInMtId,curVal);
- p->value = 0;
- p->cnt = 0;
- p->lastCycleCnt = ctx->cycleCnt;
- }
-
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspMeterRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspMeter_t* p = (cmDspMeter_t*)inst;
-
- switch( evt->dstVarId )
- {
- case kInMtId:
- p->value = cmDsvGetDouble(evt->valuePtr);
- ++p->cnt;
- return kOkDspRC;
-
- case kTimeMtId:
- p->updSmpCnt = floor(cmDspSampleRate(ctx) * cmDsvGetDouble(evt->valuePtr) / 1000.0);
- break;
- }
-
- return cmDspSetEvent(ctx,inst,evt);
-
- }
-
- struct cmDspClass_str* cmMeterClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmMeterDC,ctx,"Meter",
- NULL,
- _cmDspMeterAlloc,
- NULL,
- _cmDspMeterReset,
- _cmDspMeterExec,
- _cmDspMeterRecv,
- NULL,NULL,
- "Meter display.");
-
- return &_cmMeterDC;
- }
-
-
- //==========================================================================================================================================
- enum
- {
- kInLbId,
- kAlignLbId
- };
-
- cmDspClass_t _cmLabelDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- } cmDspLabel_t;
-
- cmDspInst_t* _cmDspLabelAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "in", kInLbId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "LabelText" },
- { "align",kAlignLbId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Alignment 0=right 1=left 2=center" },
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspLabel_t* p = cmDspInstAlloc(cmDspLabel_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
- cmDspSetDefaultDouble(ctx, &p->inst, kAlignLbId, 0.0, kLeftAlignDuiId);
-
- // create the UI control
- cmDspUiLabelCreate(ctx,&p->inst,kInLbId,kAlignLbId);
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspLabelReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspApplyAllDefaults(ctx,inst);
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspLabelRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- return cmDspSetEvent(ctx,inst,evt);
- }
-
- struct cmDspClass_str* cmLabelClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmLabelDC,ctx,"Label",
- NULL,
- _cmDspLabelAlloc,
- NULL,
- _cmDspLabelReset,
- NULL,
- _cmDspLabelRecv,
- NULL,NULL,
- "Label control.");
-
- return &_cmLabelDC;
- }
-
-
- //==========================================================================================================================================
- enum
- {
- kTypBtId,
- kOutBtId,
- kSymBtId,
- kLblBtId,
- kInBtId
- };
-
- cmDspClass_t _cmButtonDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- unsigned resetSymId;
- } cmDspButton_t;
-
- cmDspInst_t* _cmDspButtonAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- va_list vl1;
- va_copy(vl1,vl);
-
- assert( va_cnt >= 1 );
- unsigned typeId = va_arg(vl,unsigned);
-
- // check buttons should transmit their default values - push buttons should not.
- unsigned sendDfltFl = typeId == kCheckDuiId ? kSendDfltDsvFl : 0;
-
- cmDspVarArg_t args[] =
- {
- { "typ", kTypBtId, 0, 0, kUIntDsvFl | kReqArgDsvFl, "Type" },
- { "out", kOutBtId, 0, 0, kOutDsvFl | kDoubleDsvFl | kOptArgDsvFl | sendDfltFl, "Value"},
- { "sym", kSymBtId, 0, 0, kOutDsvFl | kSymDsvFl | kOptArgDsvFl | sendDfltFl, "Symbol Value"},
- { "label",kLblBtId, 0, 0, kInDsvFl | kStrzDsvFl | kOptArgDsvFl, "Label"},
- { "in", kInBtId, 0, 0, kInDsvFl | kTypeDsvMask, "Simulate UI"},
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspButton_t* p = cmDspInstAlloc(cmDspButton_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl1);
-
-
- cmDspSetDefaultDouble(ctx, &p->inst, kOutBtId, 0.0, typeId==kCheckDuiId ? 0.0 : 1.0);
- cmDspSetDefaultSymbol(ctx, &p->inst, kSymBtId, instSymId );
- cmDspSetDefaultStrcz( ctx, &p->inst, kLblBtId, NULL, cmSymTblLabel(ctx->stH,instSymId));
-
- p->resetSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"_reset");
-
- // create the UI control
- cmDspUiButtonCreate(ctx,&p->inst,typeId,kOutBtId,kLblBtId);
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspButtonReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspApplyAllDefaults(ctx,inst);
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspButtonRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- // the 'in' port is the only input port
- // but the UI sends button pushes use kOutBtId - should this be changed?
- assert( evt->dstVarId == kInBtId || evt->dstVarId == kOutBtId );
-
- // We accept all types at the 'in' port but are only interested
- // in transmitting doubles from the 'out' port.
-
- if( cmDsvCanConvertFlags( kDoubleDsvFl, evt->valuePtr->flags ) )
- {
- // Redirect events which can be converted to type kDoubleDsvFl
- // to the output port.
- //
- // Convert the event dest var id from the 'kInBtId' to 'kOutBtId'
- // and update the UI with the incoming value
- cmDspSetEventUiId(ctx,inst,evt,kOutBtId);
-
- }
-
- // no matter what kind of msg enters the 'in' port send a symbol out the 'sym' port
- if( inst->symId != cmInvalidId )
- cmDspSetSymbol( ctx, inst, kSymBtId, inst->symId );
-
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspButtonPresetRdWr( cmDspCtx_t* ctx, cmDspInst_t* inst, bool storeFl )
- {
- cmDspRC_t rc = kOkDspRC;
- if( cmDspUInt(inst,kTypBtId) == kCheckDuiId )
- rc = cmDspVarPresetRdWr(ctx,inst,kOutBtId,storeFl);
- return rc;
- }
-
- cmDspRC_t _cmDspButtonSysRecvFunc( cmDspCtx_t* ctx, struct cmDspInst_str* inst, unsigned attrSymId, const cmDspValue_t* value )
- {
- cmDspButton_t* p = (cmDspButton_t*)inst;
- if( attrSymId == p->resetSymId )
- {
- cmDspSetSymbol( ctx, inst, kSymBtId, p->resetSymId );
- cmDspSetDouble(ctx,inst,kOutBtId, cmDspDouble(inst,kOutBtId));
- }
-
- return kOkDspRC;
- }
-
- struct cmDspClass_str* cmButtonClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmButtonDC,ctx,"Button",
- NULL,
- _cmDspButtonAlloc,
- NULL,
- _cmDspButtonReset,
- NULL,
- _cmDspButtonRecv,
- _cmDspButtonPresetRdWr,
- _cmDspButtonSysRecvFunc,
- "Button control.");
-
- return &_cmButtonDC;
- }
-
- //==========================================================================================================================================
- cmDspClass_t _cmReorderDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- unsigned portCnt; // count of input ports and count of output ports
- unsigned* execFlArray; // execFlArray[portCnt] - true for ports which should cause obj to generate output
- unsigned* orderArray; // orderArray[portCnt] - port output order map
-
- } cmDspReorder_t;
-
- cmDspInst_t* _cmDspReorderAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- if( va_cnt < 1 )
- {
- cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"Reorder objects must have arguments.");
- return NULL;
- }
-
- // the first argument is the count of input ports (which is also the count of output ports)
- int portCnt = va_arg(vl,int);
-
- if( portCnt < 2 )
- {
- cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"Reorder objects must have at least 2 ports.");
- return NULL;
- }
-
- cmDspVarArg_t args[portCnt*2+1];
- unsigned i;
- int maxLabelCharCnt = 15;
- cmChar_t label[ maxLabelCharCnt+1 ];
-
- label[maxLabelCharCnt] = 0;
-
- for(i=0; i<portCnt*2; ++i)
- {
- snprintf(label,maxLabelCharCnt,"%s-%i", (i<portCnt?"in":"out"), i%portCnt);
-
- unsigned symId = cmSymTblRegisterSymbol(ctx->stH,label);
-
- args[i].label = cmSymTblLabel(ctx->stH,symId);
- args[i].constId = i;
- args[i].rn = 0;
- args[i].cn = 0;
- args[i].flags = (i<portCnt ? kInDsvFl : kOutDsvFl) | kTypeDsvMask;
- args[i].doc = i<portCnt ? "Any input" : "Any output";
- }
-
- memset(args+i,0,sizeof(args[0]));
-
- cmDspReorder_t* p = cmDspInstAlloc(cmDspReorder_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
- if( p != NULL )
- {
- p->orderArray = cmLhAllocZ(ctx->lhH,unsigned,portCnt*2);
- p->execFlArray = p->orderArray + portCnt;
- p->portCnt = portCnt;
-
- if( va_cnt-1 < portCnt )
- cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The reorder constructor must contain an output order map.");
- else
- {
- for(i=0; i<portCnt; ++i)
- {
- int order = va_arg(vl,int);
-
- if( order >= portCnt )
- cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The port order index %i is outside the valid range of 0-%i.",order,portCnt-1);
- else
- p->orderArray[ order ] = i;
-
- }
-
- va_cnt -= portCnt+1;
-
- for(i=0; i<va_cnt; ++i)
- {
- int execPortIdx = va_arg(vl,int);
-
- if( execPortIdx >= portCnt )
- cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The port exec index %i is outside the valid range of 0-%i.",execPortIdx,portCnt-1);
- else
- p->execFlArray[ execPortIdx ] = true;
- }
-
- }
- }
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspReorderFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspReorder_t* p = (cmDspReorder_t*)inst;
- cmLhFree(ctx->lhH,p->orderArray);
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspReorderRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspReorder_t* p = (cmDspReorder_t*)inst;
-
- cmDspSetEvent(ctx,inst,evt);
-
- if( evt->dstVarId < p->portCnt )
- if( p->execFlArray[evt->dstVarId] )
- {
- unsigned i;
- for(i=0; i<p->portCnt; ++i)
- {
- unsigned inVarId = p->orderArray[i];
- unsigned outVarId = inVarId + p->portCnt;
- const cmDspVar_t* varPtr = cmDspVarIdToCPtr(inst, inVarId );
-
- assert(varPtr != NULL);
-
- if((rc = cmDspValueSet(ctx, inst, outVarId, &varPtr->value, 0 )) != kOkDspRC )
- break;
- }
- }
-
- return rc;
- }
-
- struct cmDspClass_str* cmReorderClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmReorderDC,ctx,"Reorder",
- NULL,
- _cmDspReorderAlloc,
- _cmDspReorderFree,
- NULL,
- NULL,
- _cmDspReorderRecv,
- NULL,NULL,
- "Reorder value control.");
-
- return &_cmReorderDC;
- }
-
-
- //==========================================================================================================================================
- enum
- {
- kDirFnId,
- kPatFnId,
- kValFnId,
- };
-
- cmDspClass_t _cmFnameDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- } cmDspFname_t;
-
- // Pattern string for HTML and image files:
- // "HTML Files (*.html)\tImage Files (*.{bmp,gif,jpg,png})"
-
- // The va_list must include 3 args:
- // A pointer to a string referning to a default filename or directory or NULL.
- // A pointer to a string referring to a pattern string or NULL.
- // A bool to set the 'dirFl'.
- cmDspInst_t* _cmDspFnameAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "dir", kDirFnId, 0, 0, kInDsvFl | kReqArgDsvFl | kBoolDsvFl, "Dir=true Filename=false" },
- { "pat", kPatFnId, 0, 0, kInDsvFl | kOptArgDsvFl | kStrzDsvFl, "File pattern string (e.g. HTML Files (*.html)\tImage Files (*.{bmp,gif,jpg,png}))" },
- { "out", kValFnId, 0, 0, kOutDsvFl | kOptArgDsvFl | kStrzDsvFl, "Current file or directory name." },
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspFname_t* p = cmDspInstAlloc(cmDspFname_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
- cmDspSetDefaultBool( ctx, &p->inst, kDirFnId, false, false);
- cmDspSetDefaultStrcz(ctx, &p->inst, kPatFnId, NULL, "All Files (*.*)");
- cmDspSetDefaultStrcz(ctx, &p->inst, kValFnId, NULL, cmFsUserDir());
-
- cmDspUiFnameCreate(ctx,&p->inst,kValFnId,kPatFnId,kDirFnId);
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspFnameReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspApplyAllDefaults(ctx,inst);
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspFnameRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspSetEvent(ctx,inst,evt);
-
- switch( evt->dstVarId )
- {
- case kDirFnId:
- case kPatFnId:
- case kValFnId:
- break;
-
- default:
- {assert(0);}
- }
-
- return kOkDspRC;
- }
-
- struct cmDspClass_str* cmFnameClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmFnameDC,ctx,"Fname",
- NULL,
- _cmDspFnameAlloc,
- NULL,
- _cmDspFnameReset,
- NULL,
- _cmDspFnameRecv,
- NULL,NULL,
- "File or directory chooser control.");
-
- return &_cmFnameDC;
- }
-
- //==========================================================================================================================================
-
- cmDspClass_t _cmMsgListDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- cmJsonH_t jsH; // current JSON tree
- cmJsonNode_t* np; // ptr to JSON array
- unsigned colCnt; // number of elements in the JSON sub-arrays (rows)
- 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)
- unsigned* typeIdArray; // JSON types for each column size: typeIdArray[ colCnt ]
- unsigned symColCnt; // same as number of elements of typeIdArray[] == kStringTId
- unsigned symRowCnt; // same as cmJsonChildCount(p->np)
- unsigned* symM; // symM[symRowCnt,symColCnt] symbol matrix
- } cmDspMsgList_t;
-
- // create a matrix to hold the symbol id's associated with any string values
- cmDspRC_t _cmDspMsgListLoadSymbolMtx( cmDspCtx_t* ctx, cmDspMsgList_t* p )
- {
- assert(cmJsonIsArray(p->np));
-
- unsigned i,j,k;
-
- // remove any existing symbols
- if( p->symM != NULL )
- for(i=0; i<p->symColCnt*p->symRowCnt; ++i)
- if( p->symM[i] != cmInvalidId )
- cmSymTblRemove(ctx->stH,p->symM[i]);
-
- // reallocate the symbol matrix
- p->symRowCnt = cmJsonChildCount(p->np);
- p->symM = cmLhResizeN(ctx->lhH,unsigned,p->symM,p->symRowCnt*p->symColCnt);
-
- // for each row in the JSON array
- for(i=0; i<p->symRowCnt; ++i)
- {
- const cmJsonNode_t* cnp = cmJsonArrayElementC(p->np,i);
-
- // for each column in row whose data type is a string
- for(j=0,k=0; k<p->symColCnt; ++j)
- if( p->typeIdArray[j] == kStringTId )
- {
- const cmJsonNode_t* vnp = cmJsonArrayElementC(cnp,j);
- unsigned idx = k*p->symRowCnt + i;
- const cmChar_t* text;
-
- // register the string with the symbol table
- if((text = vnp->u.stringVal) != NULL )
- p->symM[ idx ] = cmSymTblRegisterStaticSymbol(ctx->stH,text);
- else
- p->symM[ idx ] = cmInvalidId;
-
- ++k;
- }
- }
-
- return kOkDspRC;
- }
-
- // Load a JSON file and set the supplied cmJsonH_t handle.
- 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 )
- {
- cmDspRC_t rc = kOkDspRC; //
- cmJsonNode_t* np = NULL; //
- cmJsonH_t jsH = cmJsonNullHandle; //
-
- *hp = cmJsonNullHandle;
- *colCntPtr = 0;
- *npp = NULL;
-
- // if no file name was given ...
- if( fn==NULL || strlen(fn)==0 )
- {
- jsH = ctx->rsrcJsH; // ... use the rsrc file
-
- if( cmJsonIsValid(ctx->rsrcJsH) == false )
- return cmErrMsg(err,kJsonFailDspRC,"No JSON cfg resource exists for this DSP program.");
-
- fn = NULL;
- }
- else
- {
- if( cmJsonInitializeFromFile(&jsH,fn,ctx->cmCtx) != kOkJsRC )
- return cmErrMsg(err,kJsonFailDspRC,"The msg list JSON file load failed on '%s'.",fn);
- }
-
- // find the array named by rsrcLabel
- if((np = cmJsonFindValue(jsH,rsrcLabel,NULL,cmInvalidId)) == NULL)
- return cmErrMsg(err,kJsonFailDspRC,"The msg list JSON tree does not have an array named '%s'.",rsrcLabel);
-
- // be sure the msg list really is an array
- if( cmJsonIsArray(np) == false )
- return cmErrMsg(err,kJsonFailDspRC,"The msg list JSON element named '%s' is not an array.", rsrcLabel);
-
- if( fn == NULL )
- fn = "<resource file>";
-
- // count of elements in the array
- unsigned n = cmJsonChildCount(np);
- unsigned m = 0;
- unsigned i,j;
-
- // for each line in the array
- for(i=0; i<n; ++i)
- {
- const cmJsonNode_t* cnp;
-
- // get the ith sub-array (row)
- if((cnp = cmJsonArrayElementC(np,i)) != NULL )
- {
- // verify that it is an array
- if( cmJsonIsArray(cnp ) == false )
- return cmErrMsg(err,kJsonFailDspRC,"The msg list JSON element in '%s' at index %i is not an array.",fn,i);
-
- // track the number of elements (columns) per row
- unsigned q = cmJsonChildCount(cnp);
-
- // if this is the first row then use it to set the valid column count
- if( i==0 )
- m = q;
- else
- {
- if( m != q )
- 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);
- }
- }
- }
-
- //
- // determine and validate the column types
- //
-
- unsigned typeIdArray[m];
-
- // for each row
- for(i=0; i<n; ++i)
- {
- const cmJsonNode_t* cnp = cmJsonArrayElementC(np,i);
-
- // for each column
- for(j=0; j<m; ++j)
- {
- const cmJsonNode_t* sap = cmJsonArrayElementC(cnp,j);
- unsigned typeId = sap->typeId & kMaskTId;
-
- // the first row sets the expected type id for each column
- switch(i)
- {
- case 0:
- if( typeId != kStringTId )
- return cmErrMsg(err,kJsonFailDspRC,"The first row of a msg list (%s) file must contain string elements which set the colum labels.",rsrcLabel);
- break;
-
- case 1:
- typeIdArray[j] = typeId;
- break;
-
- default:
- {
- // if the type is a string then it can only
- // match if the column is a string or null
- // so we assume an error
- if( typeId == kStringTId )
- rc = kJsonFailDspRC;
-
- switch(typeIdArray[j])
- {
- case kStringTId:
- if( typeId != kStringTId && typeId != kNullTId)
- 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);
-
- rc = kOkDspRC; // clear the assummed error
- break;
-
- case kNullTId: // null can be converted to anything
- typeIdArray[j] = typeId;
- break;
-
- case kIntTId:
- if( typeId == kRealTId ) // ints may be promoted to reals
- typeIdArray[j] = kRealTId;
- break;
-
- case kRealTId:
- break;
-
- case kFalseTId:
- case kTrueTId: // bools may be promoted to ints or reals
- if( typeId == kIntTId || typeId == kRealTId )
- typeIdArray[j] = typeId;
- break;
-
- default:
- 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);
-
- }
-
- if( rc != kOkDspRC )
- 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);
- } // end dflt
- } // end switch
- } // end row
- } // end list
-
-
- // VERY TRICKY - store the column type id's in the label columns type id.
- // This is stupid but safe because the column type id's are known to be set to kStringTId.
- cmJsonNode_t* lnp = np->u.childPtr->u.childPtr;
- for(i=0; i<m; ++i,lnp=lnp->siblingPtr)
- lnp->typeId = typeIdArray[i];
-
- *hp = fn==NULL ? cmJsonNullHandle : jsH;
- *npp = np;
- *colCntPtr = m;
-
- return rc;
- }
-
- // use the JSON list labels to setup the cmDspVarArg_t records associated with each msg output var.
- 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 )
- {
- unsigned i,j;
- unsigned fixedArgCnt = 0;
- for(i=0; args[i].label != NULL; ++i)
- ++fixedArgCnt;
-
- cmDspVarArg_t a[ fixedArgCnt + colCnt + 1 ];
-
- // copy the fixed arg's into the first fixedArgCnt ele's of a[]
- for(j=0; j<fixedArgCnt; ++j)
- a[j] = args[j];
-
- // remove the label row from the JSON array
- cmJsonNode_t* lnp = np->u.childPtr; // store the pointer to the label row
- //np->u.childPtr = lnp->siblingPtr; // remove the labels from the array
-
- // for each column
- for(i=0; i<colCnt; ++i,++j)
- {
- cmJsonNode_t* lp = cmJsonArrayElement(lnp,i);
-
- // store a pointer to the label
- a[j].label = lp->u.stringVal;
- a[j].constId = j;
- a[j].rn = 0;
- a[j].cn = 0;
- a[j].flags = kOutDsvFl;
- a[j].doc = "Msg output";
-
- typeIdArray[i] = lp->typeId;
-
- // convert the JSON type to a DSV type
- switch( lp->typeId )
- {
- case kNullTId: a[j].flags |= kUIntDsvFl; break;
- case kIntTId: a[j].flags |= kIntDsvFl; break;
- case kRealTId: a[j].flags |= kDoubleDsvFl; break;
- case kTrueTId: a[j].flags |= kBoolDsvFl; break;
- case kFalseTId: a[j].flags |= kBoolDsvFl; break;
- case kStringTId: a[j].flags |= kSymDsvFl; break; // strings are treated as symbols (UInt)
-
- default:
- { assert(0); }
- }
-
- // undo the tricky bit with the label types
- lp->typeId = kStringTId;
- }
-
- // set the null sentinel at the end of the arg array
- memset(a + j,0,sizeof(cmDspVarArg_t));
-
- return (cmDspMsgList_t*)cmDspInstAlloc(cmDspMsgList_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
- }
-
- cmDspRC_t _cmDspMsgListReload( cmDspCtx_t* ctx, cmDspMsgList_t* p, const cmChar_t* rsrcLabel, const cmChar_t* fn )
- {
- cmDspRC_t rc = kOkDspRC;
- cmJsonH_t jsH = cmJsonNullHandle;
- unsigned colCnt = 0;
- cmJsonNode_t* np = NULL;
-
- // load the file
- if((rc = _cmDspMsgListLoadFile(ctx,&p->inst.classPtr->err,rsrcLabel,fn,&jsH,&colCnt,&np)) != kOkDspRC )
- return cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"The msg list file '%s' load failed.",fn);
-
- // verify that the col count is correct
- if( colCnt != p->colCnt )
- 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);
-
-
- unsigned fixArgCnt = p->inst.varCnt - p->colCnt;
- cmJsonNode_t* lnp = np->u.childPtr->u.childPtr;
- unsigned i,j;
-
- for(i=0,j=fixArgCnt; i<colCnt; ++i,++j, lnp=lnp->siblingPtr)
- {
- const cmChar_t* labelStr = cmSymTblLabel(ctx->stH,p->inst.varArray[j].symId);
-
- // the labels of the new file must match the labels of the previous file
- if( strcmp(lnp->u.stringVal,labelStr) )
- 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);
-
- // if the msg column is a string ....
- if( (p->inst.varArray[j].flags & kTypeDsvMask) == kStrzDsvFl )
- {
- // ... then the file column must also be a string or null
- if( lnp->typeId != kStringTId && np->typeId != kNullTId )
- 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);
- }
- else // otherwie if the msg column is a number ...
- {
- // ... then the file type can't be a string
- if( lnp->typeId == kStringTId )
- return cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"The data type of msg list file '%s' column index %i cannot be a string.",fn,i);
-
- // TODO: maybe there are other type conversions to check for here.
- }
-
- // reset the typeid of the labels
- // (this is a cleanup from the tricky bit at the end of _cmDspListLoadFile())
- lnp->typeId = kStringTId;
- }
-
- if( cmJsonIsValid( jsH ) )
- {
- if( cmHandlesAreNotEqual(p->jsH,p->dfltJsH) && cmHandlesAreNotEqual(p->jsH,ctx->rsrcJsH) )
- cmJsonFinalize(&p->jsH);
-
- p->jsH = jsH;
- p->np = np;
-
- _cmDspMsgListLoadSymbolMtx(ctx,p);
- }
-
- return rc;
- }
-
-
- enum
- {
- kRsrcMlId,
- kFnMlId,
- kSelMlId,
- kListMlId,
- kCntMlId,
- kOutBaseMlId // identify the first output port
- };
-
- cmDspInst_t* _cmDspMsgListAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- va_list vl2;
- cmDspVarArg_t args[] =
- {
- { "rsrc", kRsrcMlId, 0, 0, kInDsvFl | kReqArgDsvFl | kStrzDsvFl, "Msg list resource label"},
- { "fn", kFnMlId, 0, 0, kInDsvFl | kOptArgDsvFl | kStrzDsvFl, "Msg list file name" },
- { "sel", kSelMlId, 0, 0, kOutDsvFl | kInDsvFl | kOptArgDsvFl | kUIntDsvFl, "Current selection index" },
- { "list", kListMlId, 0, 0, kInDsvFl | kJsonDsvFl, "Msg list data as a JSON array"},
- { "cnt", kCntMlId, 0, 0, kOutDsvFl | kSendDfltDsvFl | kUIntDsvFl , "Count of elements."},
- { NULL, 0, 0, 0, 0 }
- };
-
- va_copy(vl2,vl);
-
- if( va_cnt < 1 )
- {
- cmErrMsg(&classPtr->err,kVarArgParseFailDspRC,"The message list constructor must contain at least two arguments.");
- goto errLabel;
- }
-
- const cmChar_t* rsrcLabel = va_arg(vl,const char*);
- const cmChar_t* fn = va_cnt>1 ? va_arg(vl,const char*) : NULL;
- unsigned colCnt = 0;
- cmJsonH_t jsH = cmJsonNullHandle;
- cmDspMsgList_t* p;
- cmJsonNode_t* np;
- cmDspRC_t rc;
-
- // be sure the rsrc label contains a valid string
- if( rsrcLabel==NULL || strlen(rsrcLabel)==0 )
- {
- cmErrMsg(&classPtr->err,kVarArgParseFailDspRC,"No msg list resource label was given.");
- goto errLabel;
- }
-
- // load and validate the JSON file
- if((rc = _cmDspMsgListLoadFile(ctx,&classPtr->err,rsrcLabel,fn,&jsH,&colCnt,&np)) == kOkDspRC )
- {
- unsigned i;
- unsigned typeIdArray[colCnt];
-
- // allocate the instance
- if((p = _cmDspMsgListCons(ctx,classPtr,storeSymId,instSymId,id,args,jsH,colCnt, np, typeIdArray, va_cnt, vl2 )) != NULL )
- {
- p->jsH = jsH;
- p->np = np;
- p->dfltJsH = jsH;
- p->colCnt = colCnt;
- p->typeIdArray = cmLhAllocZ( ctx->lhH, unsigned, colCnt );
- memcpy(p->typeIdArray,typeIdArray,sizeof(p->typeIdArray[0])*colCnt);
- p->symColCnt = 0;
-
- for(i=0; i<p->colCnt; ++i)
- if( p->typeIdArray[i] == kStringTId )
- ++p->symColCnt;
-
- _cmDspMsgListLoadSymbolMtx(ctx,p);
-
- cmDspSetDefaultStrcz( ctx,&p->inst, kRsrcMlId,NULL, rsrcLabel); // rsrc label
- cmDspSetDefaultStrcz( ctx,&p->inst, kFnMlId, NULL, fn); // file name var
- cmDspSetDefaultJson( ctx,&p->inst, kListMlId,NULL, np); // default tree
- cmDspSetDefaultUInt( ctx,&p->inst, kSelMlId, 0, 0); // selection
- cmDspSetDefaultUInt( ctx,&p->inst, kCntMlId, 0, p->symRowCnt);
-
- // if there is only one column then signal the UI to create a menu button rather
- // than a list by setting the height to zero.
- unsigned height = p->colCnt == 1 ? 0 : 5;
-
- // create the list UI element
- cmDspUiMsgListCreate(ctx, &p->inst, height, kListMlId, kSelMlId );
-
- return &p->inst;
- }
- }
-
- errLabel:
- return NULL;
- }
-
- cmDspRC_t _cmDspMsgListFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspMsgList_t* p = (cmDspMsgList_t*)inst;
-
-
- // be careful not release the ctx->rsrcJsH handle if is happens to be the p->dflJsH
- if( cmJsonIsValid(p->dfltJsH) && cmHandlesAreNotEqual(p->jsH,p->dfltJsH) && cmHandlesAreNotEqual(p->dfltJsH,ctx->rsrcJsH))
- if( cmJsonFinalize(&p->dfltJsH) != kOkJsRC )
- cmDspInstErr(ctx,inst,kJsonFailDspRC,"JSON default tree finalize failed.");
-
- // be careful not to release the ctx->rsrcJsH handle if it happens to be the p->jsH
- if( cmJsonIsValid(p->jsH) && cmHandlesAreNotEqual(p->dfltJsH,ctx->rsrcJsH) )
- if( cmJsonFinalize( &p->jsH ) != kOkJsRC )
- cmDspInstErr(ctx,inst,kJsonFailDspRC,"JSON finalization failed.");
-
- cmLhFree(ctx->lhH,p->typeIdArray);
- p->typeIdArray = NULL;
-
- cmLhFree(ctx->lhH,p->symM);
- p->symM = NULL;
-
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspMsgListReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspApplyAllDefaults(ctx,inst);
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspMsgOnSel( cmDspCtx_t* ctx, cmDspMsgList_t* p, unsigned rowIdx )
- {
- cmDspRC_t rc = kOkDspRC;
- unsigned i,j;
- unsigned symId;
- const cmJsonNode_t* cnp = cmJsonArrayElementC(p->np,rowIdx);
-
- assert( cnp != NULL );
-
- // for each output variable (column)
- for(i=0,j=0; i<p->colCnt; ++i)
- {
- unsigned varId = kOutBaseMlId + i; // variable id
- const cmJsonNode_t* np = cmJsonArrayElementC(cnp,i); // json row array
- assert( np != NULL );
-
- switch( p->typeIdArray[i] )
- {
- case kNullTId:
- break;
-
- case kIntTId:
- {
- int v;
- if( cmJsonIntValue(np,&v) != kOkJsRC )
- { assert(0); }
-
- rc = cmDspSetInt(ctx,&p->inst,varId,v);
- }
- break;
-
- case kRealTId:
- {
- double v;
- if( cmJsonRealValue(np,&v) != kOkJsRC )
- { assert(0); }
-
- rc = cmDspSetDouble(ctx,&p->inst,varId,v);
- }
- break;
-
- case kTrueTId:
- case kFalseTId:
- {
- bool v;
- if( cmJsonBoolValue(np,&v) != kOkJsRC )
- { assert(0); }
-
- rc = cmDspSetBool(ctx,&p->inst,varId,v);
- }
- break;
-
- case kStringTId:
- {
- assert( j < p->symColCnt );
-
- if((symId = p->symM[ (j*p->symRowCnt) + rowIdx ]) != cmInvalidId )
- if((rc = cmDspSetSymbol(ctx,&p->inst,varId,symId )) != kOkDspRC )
- break;
-
- ++j;
- }
- break;
-
- default:
- { assert(0); }
-
- } // end switch
- } // end for
-
- return rc;
- }
-
- cmDspRC_t _cmDspMsgListRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspMsgList_t* p = (cmDspMsgList_t*)inst;
-
- switch( evt->dstVarId )
- {
-
- case kRsrcMlId:
- {
- const cmChar_t* fn = cmDspStrcz(inst,kFnMlId);
- const cmChar_t* rsrcLabel = cmDsvStrz(evt->valuePtr);
-
- if( rsrcLabel != NULL )
- {
- if((rc = _cmDspMsgListReload(ctx,p,rsrcLabel,fn)) != kOkDspRC )
- return cmDspInstErr(ctx,&p->inst,kJsonFailDspRC,"The msg list file '%s' load failed.",fn);
-
- cmDspSetEvent(ctx,inst,evt);
- cmDspSetJson(ctx,inst,kListMlId,p->np);
- cmDspSetUInt(ctx,inst,kSelMlId,0);
- cmDspSetUInt(ctx,inst,kCntMlId,p->symRowCnt);
- }
- }
- break;
-
- case kFnMlId:
- // Store the new file name.
- // A new file will not be loaded until the next rsrc label is received.
- cmDspSetEvent(ctx,inst,evt);
- break;
-
- case kListMlId:
- break;
-
- case kSelMlId:
- {
- unsigned rowIdx = cmDsvGetUInt(evt->valuePtr);
-
- assert( rowIdx < p->symRowCnt);
-
- // set the current selection variable
- if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
- rc = _cmDspMsgOnSel(ctx,p,rowIdx);
- } //end case
- break;
- }
-
- return rc;
- }
-
- cmDspRC_t _cmDspMsgListPresetRdWr( cmDspCtx_t* ctx, cmDspInst_t* inst, bool storeFl )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspMsgList_t* p = (cmDspMsgList_t*)inst;
- if((rc = cmDspVarPresetRdWr(ctx,inst,kSelMlId,storeFl)) == kOkDspRC )
- {
- if( !storeFl )
- rc = _cmDspMsgOnSel(ctx,p, cmDspUInt(inst,kSelMlId) );
- }
- return rc;
- }
-
-
- struct cmDspClass_str* cmMsgListClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmMsgListDC,ctx,"MsgList",
- NULL,
- _cmDspMsgListAlloc,
- _cmDspMsgListFree,
- _cmDspMsgListReset,
- NULL,
- _cmDspMsgListRecv,
- _cmDspMsgListPresetRdWr,
- NULL,
- "Message list selection control.");
-
- return &_cmMsgListDC;
- }
-
- //==========================================================================================================================================
-
- enum
- {
- kLenWtId,
- kShapeWtId,
- kFnWtId,
- kLoopWtId,
- kBegWtId,
- kEndWtId,
- kCmdWtId,
- kOtWtId,
- kGainWtId,
- kPhsWtId,
- kOutWtId,
- kCntWtId,
- kFIdxWtId,
- kDoneWtId
- };
-
- enum
- {
- kSilenceWtId, // 0
- kFileWtId, // 1
- kWhiteWtId, // 2
- kPinkWtId, // 3
- kSineWtId, // 4
- kCosWtId, // 5
- kSqrWtId, // 6
- kTriWtId, // 7
- kSawWtId, // 8
- kPulseWtId, // 9
- kImpulseWtId, // 10
- kPhasorWtId, // 11
- kShapeWtCnt
- };
-
- cmDspClass_t _cmWaveTableDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- cmSample_t* wt; // wave table memory
- unsigned wti; // next location to write samples into the wavetable
- unsigned wtn; // count of empty samples (avail for writing over) in the wavetable.
- unsigned fi; // absolute index into the file of the next sample to read
- unsigned fn; // length of the file in samples
- unsigned cfi; // absolute index into the file of the beginning of the current audio vector
- unsigned cfn; // when cfi >= cfn and doneFl is set then the 'done' msg is sent
- unsigned loopCnt; // current loop count
- bool doneFl; // the wave table source is exhausted
- cmAudioFileH_t afH; // current audio file handle
- int nxtBegSmpIdx; // the beg/end sample index to use with the next filename to arrive at port 'fn'
- int nxtEndSmpIdx; //
- cmThreadH_t thH;
- bool loadFileFl;
- cmDspCtx_t* ctx;
- cmSample_t phsOffs;
- cmSample_t phsLast;
- unsigned onSymId;
- unsigned offSymId;
- unsigned doneSymId;
- bool useThreadFl;
- unsigned minAfIndexRptCnt; // min count of audio samples between transmitting the current audio file index
- unsigned afIndexRptCnt; // current audio file sample index count
- } cmDspWaveTable_t;
-
- bool _cmDspWaveTableThreadFunc( void* param);
-
- cmDspInst_t* _cmDspWaveTableAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "len", kLenWtId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Wave table length in samples" },
- { "shape", kShapeWtId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Wave shape 0=silent 1=file 2=sine 3=white" },
- { "fn", kFnWtId, 0, 0, kInDsvFl | kStrzDsvFl | kOptArgDsvFl, "Optional audio file name" },
- { "loop", kLoopWtId, 0, 0, kInDsvFl | kIntDsvFl | kOptArgDsvFl, "-1=loop forever >0=loop count (dflt:-1)"},
- { "beg", kBegWtId, 0, 0, kInDsvFl | kIntDsvFl | kOptArgDsvFl, "File begin sample index" },
- { "end", kEndWtId, 0, 0, kInDsvFl | kIntDsvFl | kOptArgDsvFl, "File end sample index (-1=play all)" },
- { "cmd", kCmdWtId, 0, 0, kInDsvFl | kSymDsvFl | kOptArgDsvFl, "Command: on off"},
- { "ot", kOtWtId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Overtone count"},
- { "gain", kGainWtId, 0, 0, kInDsvFl | kDoubleDsvFl|kOptArgDsvFl, "Gain"},
- { "phs", kPhsWtId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Driving phase" },
- { "out", kOutWtId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output" },
- { "cnt", kCntWtId, 0, 0, kOutDsvFl | kIntDsvFl, "Loop count event."},
- { "fidx", kFIdxWtId, 0, 0, kOutDsvFl | kUIntDsvFl, "Current audio file index."},
- { "done", kDoneWtId, 0, 0, kOutDsvFl | kSymDsvFl, "'done' sent after last loop."},
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspWaveTable_t* p = cmDspInstAlloc(cmDspWaveTable_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
- // get the filename given in the va_list (or NULL if no filename was given)
- const cmChar_t* fn = cmDspDefaultStrcz(&p->inst,kFnWtId);
-
- p->offSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"off");
- p->onSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"on");
- p->doneSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"done");
-
- double adCurFileIdxRptPeriodMs = 100.0;
- p->minAfIndexRptCnt = floor(adCurFileIdxRptPeriodMs * cmDspSampleRate(ctx) / 1000.0);
-
- cmDspSetDefaultUInt( ctx, &p->inst, kLenWtId, 0, cmDspSampleRate(ctx));
- cmDspSetDefaultUInt( ctx, &p->inst, kShapeWtId, 0, kSilenceWtId );
- cmDspSetDefaultStrcz( ctx, &p->inst, kFnWtId, NULL, fn );
- cmDspSetDefaultInt( ctx, &p->inst, kLoopWtId, 0, -1 );
- cmDspSetDefaultInt( ctx, &p->inst, kBegWtId, 0, 0 );
- cmDspSetDefaultInt( ctx, &p->inst, kEndWtId, 0, -1 );
- cmDspSetDefaultSymbol(ctx, &p->inst, kCmdWtId, p->onSymId );
- cmDspSetDefaultUInt( ctx, &p->inst, kOtWtId, 0, 5 );
- cmDspSetDefaultDouble(ctx, &p->inst, kGainWtId, 0, 1.0 );
- cmDspSetDefaultUInt( ctx, &p->inst, kFIdxWtId, 0, 0 );
-
- p->useThreadFl = false;
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspWaveTableFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspWaveTable_t* p = (cmDspWaveTable_t*)inst;
-
- if( cmThreadIsValid(p->thH) )
- if( cmThreadDestroy(&p->thH) != kOkThRC )
- cmDspInstErr(ctx,inst,kThreadFailDspRC,"The wavetable file reader thread failed to close.");
-
- if( cmAudioFileIsValid(p->afH) )
- cmAudioFileDelete(&p->afH);
-
- cmLhFree(ctx->lhH,p->wt);
-
- return kOkDspRC;
- }
-
- // Read the next block of 'rdSmpCnt' samples starting at the absolute file index 'p->fi'
- // into wt[rdSmpCnt].
- // If the end of the file segment marked by absolute file indexes 'begSmpIdx' to 'endSmpIdx'
- // is encountered in the middle of the requested block and the wave table is in loop
- // mode then the the function will automatically begin reading from the begining of the
- // file segment. If the end of the file segment is encountered and the wave table is not
- // in loop mode then the empty portion of wt[] will be set to zero.
- cmDspRC_t _cmDspWaveTableReadBlock( cmDspCtx_t* ctx, cmDspWaveTable_t* p, cmSample_t* wt, unsigned rdSmpCnt, int begSmpIdx, int endSmpIdx, int maxLoopCnt )
- {
- unsigned actFrmCnt = 0;
- unsigned chIdx = 0;
- unsigned chCnt = 1;
- unsigned fn = endSmpIdx - p->fi + 1; // count of samples between p->fi and endSmpIdx
- unsigned n0 = rdSmpCnt;
- unsigned n1 = 0;
-
- // if the requested sample count will go past the end of the file segment
- if( rdSmpCnt > fn )
- {
- n1 = rdSmpCnt - fn;
- n0 = rdSmpCnt - n1;
- }
-
- // if we don't have a valid file yet - then
- if( cmAudioFileIsValid(p->afH) == false )
- {
- cmVOS_Zero(wt,n0);
- return kOkDspRC;
- }
-
- // read the first block of samples
- if( cmAudioFileReadSample(p->afH, n0, chIdx, chCnt, &wt, &actFrmCnt ) != kOkAfRC )
- return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"An error occured while reading the wave table file.");
-
- // BUG BUG BUG
- // This assertion will fail if the file beg/end markers are not legal given the file length.
- // An error msg would be more appropriate.
- assert( actFrmCnt == n0 );
-
- // increment the wave table pointer
- wt += n0;
- p->fi += n0;
-
- // if n1 != 0 then we have encountered the end of the file segment
- if( n1 > 0 )
- {
- ++p->loopCnt;
-
- // send the loop count event
- cmDspSetInt(ctx,&p->inst,kCntWtId,p->loopCnt);
-
- // if we have played all the requested loops
- if( maxLoopCnt != -1 && p->loopCnt >= maxLoopCnt )
- {
- p->doneFl = true;
- cmVOS_Zero(wt,n1); // zero to the end of the buffer
-
- p->cfn = p->cfi + cmDspUInt((cmDspInst_t*)p,kLenWtId) - p->wtn - n0;
- assert( p->cfn >= p->cfi );
-
- }
- else
- {
- // seek to the first sample indicated by the 'beg' variable
- if( cmAudioFileSeek(p->afH,begSmpIdx) != kOkAfRC )
- return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"Seeking failed on the wave table file.",fn);
-
- // read the second block of samples
- if( cmAudioFileReadSample(p->afH, n1, chIdx, chCnt, &wt, &actFrmCnt ) != kOkAfRC )
- return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"An error occured while reading the wave table file.");
-
- assert( actFrmCnt == n1 );
-
- // reset the file index tracker
- p->fi = begSmpIdx + n1;
- p->cfi = begSmpIdx;
- }
- }
-
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspWaveTableReadAudioFile( cmDspCtx_t* ctx, cmDspWaveTable_t* p, unsigned wtSmpCnt, unsigned rdSmpCnt )
- {
- unsigned n0 = rdSmpCnt;
- unsigned n1 = 0;
- int begSmpIdx = cmDspInt(&p->inst,kBegWtId);
- int endSmpIdx = cmDspInt(&p->inst,kEndWtId);
- int maxLoopCnt= cmDspInt(&p->inst,kLoopWtId);
-
- if( endSmpIdx < begSmpIdx )
- endSmpIdx = p->fn-1;
-
- // if this read will wrap to the beginning of the wave table
- if( p->wti + rdSmpCnt > wtSmpCnt )
- {
- n0 = wtSmpCnt - p->wti; // count of samples to read into the end of p->wt[]
- n1 = rdSmpCnt - n0; // count of samples to read into the beg of p->wt[]
- }
-
- assert(n1<wtSmpCnt);
-
- // the first read always starts at p->wt + p->wti
- if( p->doneFl )
- cmVOS_Zero(p->wt + p->wti,n0);
- else
- if( _cmDspWaveTableReadBlock(ctx, p, p->wt+p->wti, n0,begSmpIdx,endSmpIdx,maxLoopCnt ) != kOkDspRC )
- return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"An error occured while reading the wave table file.");
-
- p->wtn -= n0; // decrease the count of available samples
- p->wti += n0;
-
- if( n1 > 0 )
- {
- // the second read always starts at the beginning of the wave table
- if( p->doneFl )
- cmVOS_Zero(p->wt,n1);
- else
- if( _cmDspWaveTableReadBlock(ctx, p, p->wt, n1,begSmpIdx,endSmpIdx,maxLoopCnt ) != kOkDspRC )
- return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"An error occured while reading the wave table file.");
-
- p->wtn -= n1; // decrease the count of available samples
- p->wti = n1;
- }
-
-
- //p->wtn -= rdSmpCnt; // decrease the count of available samples
-
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspWaveTableInitAudioFile( cmDspCtx_t* ctx, cmDspWaveTable_t* p )
- {
- cmDspRC_t rc;
- cmAudioFileH_t afH;
- cmRC_t afRC;
- cmAudioFileInfo_t afInfo;
-
- const cmChar_t* fn = cmDspStrcz(&p->inst,kFnWtId);
- unsigned wtSmpCnt = cmDspUInt(&p->inst,kLenWtId);
- int begSmpIdx= cmDspInt(&p->inst,kBegWtId);
-
- // if the file name is valid
- if( fn == NULL || strlen(fn)==0 )
- {
- cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"Audio file loading was requested for the wave table but no file name was given.");
- goto errLabel;
- }
-
- // open the audio file
- afH = cmAudioFileNewOpen(fn,&afInfo,&afRC,ctx->rpt);
-
- // check for file open errors
- if( afRC != kOkAfRC )
- {
- rc = cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"The audio file '%s' could not be opened. ('%s').",fn,cmAudioFileErrorMsg(afRC));
- goto errLabel;
- }
-
- // if the file opened but is invalid
- if( cmAudioFileIsValid(p->afH) )
- cmAudioFileDelete(&p->afH);
-
- // seek to the first sample indicated by the 'beg' variable
- if( cmAudioFileSeek(afH,begSmpIdx) != kOkAfRC )
- {
- rc = cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"Seeking failed on the audio file '%s'.",fn);
- goto errLabel;
- }
-
- p->afH = afH;
- p->fi = begSmpIdx;
- p->cfi = begSmpIdx;
- p->fn = afInfo.frameCnt;
- p->wti = 0;
- p->wtn = wtSmpCnt;
-
- // read the first block of samples
- if((rc= _cmDspWaveTableReadAudioFile(ctx,p,wtSmpCnt,wtSmpCnt))!= kOkDspRC )
- goto errLabel;
-
- //printf("Wt:%s %i %i\n",fn,begSmpIdx,cmDspInt(&p->inst,kEndWtId));
-
- // set the shape param to kFileWtId
- //if((rc= cmDspSetUInt(ctx,&p->inst,kShapeWtId,kFileWtId)) != kOkDspRC )
- // goto errLabel;
-
- errLabel:
-
- if( rc != kOkDspRC )
- cmDspSetUInt(ctx,&p->inst,kShapeWtId,kSilenceWtId);
-
- return rc;
- }
-
- bool _cmDspWaveTableThreadFunc( void* param)
- {
- cmDspWaveTable_t* p = (cmDspWaveTable_t*)param;
-
- if( p->loadFileFl )
- {
- p->loadFileFl = false;
-
- if( _cmDspWaveTableInitAudioFile(p->ctx,p) == kOkDspRC )
- {
- p->phsOffs = p->phsLast;
- cmDspSetUInt(p->ctx,&p->inst,kShapeWtId,kFileWtId);
-
- }
-
- cmThreadPause(p->thH,kPauseThFl);
- }
-
- return true;
- }
-
- // Files are loaded via a background thread.
- cmDspRC_t _cmDspWaveTableStartFileLoadThread( cmDspCtx_t* ctx, cmDspWaveTable_t* p, const cmChar_t* fn )
- {
- cmDspRC_t rc = kOkDspRC;
-
- if( fn == NULL )
- return rc;
-
- if( p->loadFileFl )
- 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));
-
- if(p->useThreadFl && cmThreadIsValid(p->thH) == false)
- cmThreadCreate(&p->thH,_cmDspWaveTableThreadFunc,p,ctx->rpt);
-
- if(p->useThreadFl && cmThreadIsValid(p->thH) == false )
- return cmDspInstErr(ctx,&p->inst,kInvalidStateDspRC,"The audio file '%s' was not loaded because the audio load thread is invalid.",cmStringNullGuard(fn));
-
- p->loadFileFl = true;
- p->ctx = ctx;
- cmDspSetUInt(ctx,&p->inst,kShapeWtId,kSilenceWtId);
- cmDspSetStrcz(ctx,&p->inst,kFnWtId,fn);
-
- if( p->useThreadFl == false )
- {
- // use non-threaded load
- if((rc = _cmDspWaveTableInitAudioFile(p->ctx,p)) != kOkDspRC )
- return cmDspInstErr(ctx,&p->inst,kVarNotValidDspRC,"The audio file '%s' was not loaded.",cmStringNullGuard(fn));
-
- p->phsOffs = p->phsLast;
- cmDspSetUInt(p->ctx,&p->inst,kShapeWtId,kFileWtId);
- p->loadFileFl = false;
- }
- else
- {
- // use threaded load - this works but it isn't really thread safe
- if( cmThreadPause(p->thH,0) != kOkThRC )
- return cmDspInstErr(ctx,&p->inst,kThreadFailDspRC,"The audio file '%s' was not loaded because audio thread enable failed.",cmStringNullGuard(fn));
- }
- return rc;
- }
-
- // This function is called whenever the source mode variable changes (or a new file name arrives)
- cmDspRC_t _cmDspWaveTableCreateTable( cmDspCtx_t* ctx, cmDspWaveTable_t* p )
- {
- cmDspRC_t rc = kOkDspRC;
- unsigned shapeId = cmDspUInt(&p->inst,kShapeWtId);
- unsigned wtSmpCnt = cmDspUInt(&p->inst,kLenWtId);
- unsigned otCnt = cmDspUInt(&p->inst,kOtWtId);
- cmSample_t gain = 0.9;
- double hz = 1.0;
- double sr = cmDspSampleRate(ctx);
-
- assert( wtSmpCnt > 0 );
-
- if( p->wt == NULL )
- p->wt = cmLhResizeNZ(ctx->lhH,cmSample_t,p->wt,wtSmpCnt);
- else
- cmVOS_Zero(p->wt,wtSmpCnt);
-
- p->wtn = wtSmpCnt; // all samples in the wt are avail for filling
- p->wti = 0; // beginning with the first sample
- p->loopCnt = 0; // we are starting from a new source so set the loop cnt to 0
- p->doneFl = false; // and the doneFl to false
-
- assert( p->wt != NULL );
-
- switch( shapeId )
- {
- case kSilenceWtId:
- break;
-
- case kFileWtId:
- printf("Loading:%i %i %s\n",p->nxtBegSmpIdx,p->nxtEndSmpIdx,cmDspStrcz(&p->inst,kFnWtId));
- rc = _cmDspWaveTableStartFileLoadThread(ctx,p,cmDspStrcz(&p->inst,kFnWtId));
- break;
-
- case kWhiteWtId:
- cmVOS_Random(p->wt,wtSmpCnt,-gain,gain);
- break;
-
- case kPinkWtId:
- cmVOS_SynthPinkNoise(p->wt,wtSmpCnt,0.0);
- cmVOS_MultVS(p->wt,wtSmpCnt,gain);
- break;
-
- case kSineWtId:
- cmVOS_SynthSine(p->wt,wtSmpCnt,0,sr,hz);
- cmVOS_MultVS(p->wt,wtSmpCnt,gain);
- break;
-
- case kCosWtId:
- cmVOS_SynthCosine(p->wt,wtSmpCnt,0,sr,hz);
- cmVOS_MultVS(p->wt,wtSmpCnt,gain);
- break;
-
- case kSawWtId:
- cmVOS_SynthSawtooth(p->wt,wtSmpCnt,0,sr,hz,otCnt);
- cmVOS_MultVS(p->wt,wtSmpCnt,gain);
- break;
-
- case kSqrWtId:
- cmVOS_SynthSquare( p->wt,wtSmpCnt,0,sr,hz,otCnt );
- cmVOS_MultVS(p->wt,wtSmpCnt,gain);
- break;
-
- case kTriWtId:
- cmVOS_SynthTriangle( p->wt,wtSmpCnt,0,sr,hz,otCnt );
- cmVOS_MultVS(p->wt,wtSmpCnt,gain);
- break;
-
- case kPulseWtId:
- cmVOS_SynthPulseCos( p->wt,wtSmpCnt,0,sr,hz,otCnt );
- cmVOS_MultVS(p->wt,wtSmpCnt,gain);
- break;
-
- case kPhasorWtId:
- cmVOS_SynthPhasor( p->wt,wtSmpCnt,0,sr,hz );
- cmVOS_MultVS(p->wt,wtSmpCnt,gain);
- break;
-
-
- }
- return rc;
- }
-
-
- cmDspRC_t _cmDspWaveTableReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspWaveTable_t* p = (cmDspWaveTable_t*)inst;
-
- cmDspApplyAllDefaults(ctx,inst);
- cmDspZeroAudioBuf(ctx,inst,kOutWtId);
-
- p->nxtBegSmpIdx = cmDspInt(&p->inst,kBegWtId);
- p->nxtEndSmpIdx = cmDspInt(&p->inst,kEndWtId);
-
- return _cmDspWaveTableCreateTable(ctx,p);
-
- }
-
-
- cmDspRC_t _cmDspWaveTableExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- const cmSample_t* phsV = cmDspAudioBuf(ctx,inst,kPhsWtId,0);
-
- if( phsV == NULL )
- {
- inst->execFunc = NULL; // disable this instance because it has no input
- return kOkDspRC;
- }
-
- cmDspWaveTable_t* p = (cmDspWaveTable_t*)inst;
-
- unsigned mode = cmDspSymbol(inst,kCmdWtId);
- unsigned srcId = cmDspUInt(inst,kShapeWtId);
-
- if( mode == p->offSymId || srcId == kSilenceWtId )
- {
- cmDspZeroAudioBuf(ctx,inst,kOutWtId);
- return kOkDspRC;
- }
-
- cmSample_t* outV = cmDspAudioBuf(ctx,inst,kOutWtId,0);
- unsigned outCnt = cmDspVarRows(inst,kOutWtId);
- unsigned wtSmpCnt = cmDspUInt(inst,kLenWtId);
- double gain = cmDspDouble(inst,kGainWtId);
- unsigned i;
-
- // for each output sample
- for(i=0; i<outCnt; ++i)
- {
- // get the wave table location
- //unsigned x = fmodf(phsV[i] - p->phsOffs,wtSmpCnt);
- unsigned x = fmodf(phsV[i],wtSmpCnt);
-
- // if the wt loctn is passed the end of the table
- /*
- if( x >= wtSmpCnt )
- {
- offs += wtSmpCnt;
- x -= wtSmpCnt;
- }
- */
-
- outV[i] = gain * p->wt[x];
- }
-
- p->phsLast = phsV[outCnt-1];
-
- // if we are reading from a file ...
- if( srcId == kFileWtId )
- {
- unsigned rdSmpCnt = 8192; // file read block sample count
-
- p->wtn += outCnt;
-
- // ... and there are rdSmpCnt avail locations in the wave table
- if( p->wtn >= rdSmpCnt )
- rc = _cmDspWaveTableReadAudioFile(ctx, p, wtSmpCnt, rdSmpCnt );
-
- // send the current audio file index
- if( p->doneFl && p->cfi < p->cfn && p->cfn <= (p->cfi + outCnt) )
- {
- cmDspSetUInt(ctx,inst,kFIdxWtId,p->cfn);
- cmDspSetSymbol(ctx,inst,kDoneWtId,p->doneSymId);
- }
- else
- {
- if( p->afIndexRptCnt >= p->minAfIndexRptCnt )
- {
- p->afIndexRptCnt -= p->minAfIndexRptCnt;
- cmDspSetUInt(ctx,inst,kFIdxWtId,p->cfi);
- }
- }
-
- p->afIndexRptCnt += outCnt;
- p->cfi += outCnt;
-
- }
-
- return rc;
- }
-
-
- cmDspRC_t _cmDspWaveTableRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspWaveTable_t* p = (cmDspWaveTable_t*)inst;
-
- switch( evt->dstVarId )
- {
- case kFnWtId: // a new file name arrived - this automatically switches the source mode to kFileWtId
- {
- const cmChar_t* fn = cmDsvStrz(evt->valuePtr);
-
- if( cmFsIsFile( fn )==false )
- cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"'%s' does not exist.",cmStringNullGuard(fn));
- else
- {
- cmDspSetEvent(ctx,inst,evt); // set the file name variable
- cmDspSetInt(ctx,inst,kBegWtId,p->nxtBegSmpIdx); // set the beg/end smp idx var's from the stored nxtBeg/EndSmpIdx values
- cmDspSetInt(ctx,inst,kEndWtId,p->nxtEndSmpIdx); //
- cmDspSetUInt(ctx,inst,kShapeWtId,kFileWtId); // switch to file mode
- rc = _cmDspWaveTableCreateTable(ctx,p); // reload the wavetable
- }
- }
- break;
-
- case kBegWtId:
- // store for next incoming file name msg
- p->nxtBegSmpIdx = cmDsvGetInt(evt->valuePtr);
- break;
-
- case kEndWtId:
- // store for next incoming file name msg
- p->nxtEndSmpIdx = cmDsvGetInt(evt->valuePtr);
- break;
-
- case kShapeWtId:
- if( cmDsvGetUInt(evt->valuePtr) < kShapeWtCnt )
- {
- cmDspSetEvent(ctx,inst,evt); // switch modes
- rc = _cmDspWaveTableCreateTable(ctx,p); // reload the wavetable
- }
- break;
-
- case kLenWtId: // we don't support table size changes
- break;
-
- case kPhsWtId:
- break;
-
- case kCmdWtId:
- if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
- {
- if( cmDspSymbol(inst,kCmdWtId) == p->onSymId )
- {
- //rc = _cmDspWaveTableReset(ctx,inst, evt );
- rc = _cmDspWaveTableCreateTable(ctx,p);
-
- cmDspSetSymbol(ctx,inst,kCmdWtId,p->onSymId);
- p->phsOffs = 0;
- p->phsLast = 0;
- }
- }
- break;
-
- case kOtWtId:
- if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC )
- rc = _cmDspWaveTableCreateTable(ctx,p); // reload the wavetable
- break;
-
- case kGainWtId:
- rc = cmDspSetEvent(ctx,inst,evt);
- break;
-
- default:
- { assert(0); }
- }
-
- return rc;
- }
-
-
-
- struct cmDspClass_str* cmWaveTableClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmWaveTableDC,ctx,"WaveTable",
- NULL,
- _cmDspWaveTableAlloc,
- _cmDspWaveTableFree,
- _cmDspWaveTableReset,
- _cmDspWaveTableExec,
- _cmDspWaveTableRecv,
- NULL,
- NULL,
- "Variable frequency and waveshape signal generator." );
-
- return &_cmWaveTableDC;
- }
-
- //==========================================================================================================================================
- enum
- {
- kFmtSpId,
- kOutSpId,
- kInSpId,
-
- kSprintfLabelCharCnt = 15,
- kSprintfDocCharCnt = 31,
- kSprintfBufCharCnt = 1023
- };
-
- cmDspClass_t _cmSprintfDC;
-
- typedef struct
- {
- unsigned flags; // dsv type id for this fmt conversion or 0 if it is a literal string
- unsigned fsi; // index into the fmt string where the fmt starts (always points to an '%' char)
- unsigned fsn; // length of the format string
- cmChar_t label[ kSprintfLabelCharCnt+1 ];
- cmChar_t doc[ kSprintfDocCharCnt+1 ];
- } cmDspSprintfFmt_t;
-
- typedef struct
- {
- cmDspInst_t inst;
- cmChar_t buf[ kSprintfBufCharCnt+1]; // output string buffer
- unsigned inCnt; // count conversion spec's in the fmtStr[]
- cmDspSprintfFmt_t* fmtArray; // fmtArray[inCnt]
- cmChar_t* fmtStr; // C-style printf format string.
- } cmDspSprintf_t;
-
- cmDspRC_t _cmSprintfGetInputCnt( cmDspCtx_t* ctx, cmDspClass_t* classPtr, const cmChar_t* fmt, unsigned* inCntPtr )
- {
- unsigned i,n;
- unsigned inCnt = 0;
-
- if( fmt== NULL || (n=strlen(fmt))==0 )
- return cmErrMsg(&classPtr->err,kInvalidArgDspRC,"Empty format string.");
-
- for(i=0; i<n; ++i)
- {
- // handle the escape character
- if( fmt[i] == '\\' )
- ++i; // for now we will just skip the next character
- else
- if( fmt[i] == '%' )
- ++inCnt;
- }
-
- *inCntPtr = inCnt;
- return kOkDspRC;
- }
-
- cmDspRC_t _cmSprintfGetInputTypes( cmDspCtx_t* ctx, cmDspClass_t* classPtr, const cmChar_t* fmt, cmDspSprintfFmt_t fmtArray[], unsigned inCnt )
- {
- unsigned i,j,n;
-
- if( fmt== NULL || (n=strlen(fmt))==0 )
- return cmErrMsg(&classPtr->err,kInvalidArgDspRC,"Empty format string.");
-
- n = strlen(fmt);
-
- for(i=0,j=0; i<n; ++i)
- {
- // handle the escape character
- if( fmt[i] == '\\' )
- ++i; // for now we will just skip the next character
- else
- if( fmt[i] == '%' )
- {
- unsigned fn;
- if((fn = strcspn(fmt+i,"diouxXfeEgGcs")) == 0 )
- return cmErrMsg(&classPtr->err,kInvalidArgDspRC,"Invalid format string on input conversion at index:%i.",j);
-
- ++fn;
-
- fmtArray[j].fsi = i;
- fmtArray[j].fsn = fn;
-
- snprintf(fmtArray[j].label,kSprintfLabelCharCnt,"in-%i",j);
-
- fmtArray[j].label[kSprintfLabelCharCnt]=0;
- fmtArray[j].label[kSprintfDocCharCnt] = 0;
-
- switch( fmt[ i + fn - 1 ] )
- {
- case 'd':
- case 'i':
- fmtArray[j].flags = kIntDsvFl;
- snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"Integer input %i.",j);
- break;
-
- case 'o':
- case 'u':
- case 'x':
- case 'X':
- fmtArray[j].flags = kUIntDsvFl;
- snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"Unsigned input %i.",j);
- break;
-
- case 'f':
- case 'e':
- case 'E':
- case 'g':
- case 'G':
- fmtArray[j].flags = kDoubleDsvFl;
- snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"Double input %i.",j);
- break;
-
- case 'c':
- fmtArray[j].flags = kUCharDsvFl;
- snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"Unsigned char input %i.",j);
- break;
-
- case 's':
- fmtArray[j].flags = kStrzDsvFl | kSymDsvFl;
- snprintf(fmtArray[j].doc,kSprintfDocCharCnt,"String input %i.",j);
- break;
-
- default:
- { assert(0); }
-
- }
-
- i += fn - 1;
-
- ++j;
- }
- }
-
- return kOkDspRC;
- }
-
- 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 )
- {
- cmDspRC_t rc;
- unsigned i,j;
- va_list vl2;
- unsigned inCnt = 0;
- unsigned fixedArgCnt = 0;
- cmDspSprintf_t* p = NULL;
- const cmChar_t* fmt = NULL;
- cmDspSprintfFmt_t* fmtArray = NULL;
-
- va_copy(vl2,vl);
-
- *pp = NULL;
-
- if( va_cnt > 0 )
- fmt = va_arg(vl,const char*);
-
- if( va_cnt < 1 || fmt == NULL )
- return cmErrMsg(&classPtr->err,kVarArgParseFailDspRC,"Expected the format string as the first argument.");
-
-
- // calc the number of fixed args
- for(i=0; args[i].label != NULL; ++i)
- ++fixedArgCnt;
-
- // calc the count of input args
- if((rc = _cmSprintfGetInputCnt(ctx, classPtr, fmt, &inCnt)) != kOkDspRC )
- return rc;
-
- if( inCnt > 0 )
- {
- fmtArray = cmLhAllocZ( ctx->lhH, cmDspSprintfFmt_t, inCnt );
-
- rc = _cmSprintfGetInputTypes(ctx, classPtr, fmt, fmtArray, inCnt );
- }
-
- if( rc == kOkDspRC )
- {
- unsigned argCnt = fixedArgCnt + inCnt;
- cmDspVarArg_t argArray[ argCnt+1 ];
-
- // copy in fixed args
- for(i=0; i<fixedArgCnt; ++i)
- argArray[i] = args[i];
-
- // calc input args
- for(j=0; i<argCnt; ++i,++j)
- {
- argArray[i].label = fmtArray[j].label;
- argArray[i].constId = i;
- argArray[i].rn = 0;
- argArray[i].cn = 0;
- argArray[i].flags = kInDsvFl | fmtArray[j].flags;
- argArray[i].doc = fmtArray[j].doc;
- }
-
- // set the sentinel arg to all zeros
- memset(argArray + argCnt,0,sizeof(cmDspVarArg_t));
-
- if((p = cmDspInstAlloc(cmDspSprintf_t,ctx,classPtr,argArray,instSymId,id,storeSymId,va_cnt,vl2)) != NULL )
- {
- // make a copy of the format string - we need to be sure that it is in
- // r/w memory in order that _cmDspSprintfGenString() can write to it
- p->fmtStr = cmLhResizeN(ctx->lhH,cmChar_t,p->fmtStr,strlen(fmt)+1);
- strcpy(p->fmtStr,fmt);
-
-
- cmLhFree(ctx->lhH,p->fmtArray);
- p->fmtArray = fmtArray;
-
- p->inCnt = inCnt;
- memset(p->buf,0,kSprintfBufCharCnt+1);
- }
-
- *pp = p;
-
- }
-
- if( cmErrLastRC(&classPtr->err) != kOkDspRC )
- cmLhFree(ctx->lhH,fmtArray);
-
- return rc;
- }
-
- cmDspRC_t _cmDspSprintfGenString(cmDspCtx_t* ctx, cmDspSprintf_t* p )
- {
- cmDspRC_t rc = kOkDspRC;
- unsigned fsi = 0; // format string index
- unsigned i = 0; // fmtArray[] index
- unsigned bi = 0; // string buffer index
- unsigned bn = kSprintfBufCharCnt; // available char's in the string buffer
- cmChar_t* fmt = p->fmtStr;
- unsigned fn = strlen(fmt)+1;
-
- // for each
- for(i=0; i<p->inCnt && bn>0; ++i)
- {
- const cmDspSprintfFmt_t* f = p->fmtArray + i;
- unsigned varId = kInSpId + i;
- const cmDspVar_t* varPtr = cmDspVarIdToCPtr(&p->inst, varId);
-
- assert(varPtr != NULL);
-
- // if there are literal char's to copy prior to the format
- if( fsi < f->fsi )
- {
- unsigned cn = cmMin(f->fsi-fsi,bn);
- strncpy(p->buf+bi,fmt+fsi,cn);
- bn -= cn;
- fsi += cn;
- bi += cn;
- }
-
- if( bn == 0 )
- {
- rc = cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"The internal string buffer is too small.");
- break;
- }
-
- unsigned pn = 0;
- char c = fmt[ f->fsi + f->fsn ];
-
- // zero terminate the format string for this input
- fmt[ f->fsi + f->fsn] = 0;
-
- // if the conversion fmt is for a string then the kSymDsvFl will be set
- // which will prevent the switch() from working - so clear the sym flag here.
- unsigned flags = cmClrFlag(f->flags,kSymDsvFl);
-
- switch(flags)
- {
- case kUCharDsvFl:
- // not implemented - need to implment a uchar variable type or
- // assume a one character strz.
- assert(0);
- break;
-
- case kIntDsvFl:
- pn = snprintf(p->buf+bi,bn,fmt + fsi, cmDspInt(&p->inst,varId));
- break;
-
- case kUIntDsvFl:
- pn = snprintf(p->buf+bi,bn,fmt + fsi, cmDspUInt(&p->inst,varId));
- break;
-
- case kDoubleDsvFl:
- pn = snprintf(p->buf+bi,bn,fmt + fsi, cmDspDouble(&p->inst,varId));
- break;
-
- case kStrzDsvFl:
- if( cmDspIsSymbol(&p->inst,varId) )
- pn = snprintf(p->buf+bi,bn,fmt + fsi, cmStringNullGuard(cmSymTblLabel(ctx->stH,cmDspSymbol(&p->inst,varId))));
- else
- pn = snprintf(p->buf+bi,bn,fmt + fsi, cmDspStrcz(&p->inst,varId));
- break;
-
- default:
- { assert(0); }
- }
-
- // restore the char written over by the termination zero
- fmt[ f->fsi + f->fsn] = c;
-
- assert(pn<=bn);
- bn -= pn;
- bi += pn;
- fsi += f->fsn;
-
- }
-
- // if there is literal text in the format string after the last conversion spec.
- if( fsi < fn )
- {
- unsigned cn = cmMin(fn-fsi,bn);
- strncpy(p->buf+bi,fmt+fsi,cn);
- }
-
- return rc;
- }
-
- cmDspInst_t* _cmDspSprintfAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "fmt", kFmtSpId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Format string" },
- { "out", kOutSpId, 0, 0, kOutDsvFl | kStrzDsvFl, "Output string" },
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspSprintf_t* p = NULL;
- if( _cmSprintfLoadFormat(&p, ctx, classPtr, instSymId, id, storeSymId, args, va_cnt, vl ) == kOkDspRC )
- return &p->inst;
- return NULL;
- }
-
- cmDspRC_t _cmDspSprintfFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspSprintf_t* p = (cmDspSprintf_t*)inst;
- cmLhFree(ctx->lhH,p->fmtArray);
- cmLhFree(ctx->lhH,p->fmtStr);
- p->fmtArray=NULL;
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspSprintfReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspSprintfRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc= kOkDspRC;
- cmDspSprintf_t* p = (cmDspSprintf_t*)inst;
-
- if( kInSpId <= evt->dstVarId && evt->dstVarId < kInSpId + p->inCnt )
- {
- cmDspSetEvent(ctx,inst,evt);
- //if( evt->dstVarId == kInSpId )
- if((rc = _cmDspSprintfGenString(ctx,p)) == kOkDspRC )
- cmDspSetStrcz(ctx,inst,kOutSpId,p->buf);
-
- }
-
- return rc;
- }
-
- struct cmDspClass_str* cmSprintfClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmSprintfDC,ctx,"Sprintf",
- NULL,
- _cmDspSprintfAlloc,
- _cmDspSprintfFree,
- _cmDspSprintfReset,
- NULL,
- _cmDspSprintfRecv,
- NULL,NULL,
- "String formatter." );
-
- return &_cmSprintfDC;
- }
-
- //==========================================================================================================================================
- enum
- {
- kOutAmId,
- kBaseInAmId
- };
-
- cmDspClass_t _cmAMixDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- unsigned inPortCnt;
- unsigned baseGainId;
- unsigned baseMuteId;
- } cmDspAMix_t;
-
- cmDspInst_t* _cmDspAMixAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- if( va_cnt < 1 )
- {
- cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'AMix' constructor must have a count of input ports.");
- return NULL;
- }
-
- // args:
- // <inPortCnt> <gain0>, <gain1> (the default gains are optional)
-
- unsigned i;
- int inPortCnt = va_arg(vl,int);
- unsigned baseGainAmId = kBaseInAmId + inPortCnt;
- unsigned baseMuteAmId = baseGainAmId + inPortCnt;
- double dfltGain[ inPortCnt ];
-
- if( va_cnt == 1 )
- cmVOD_Fill(dfltGain,inPortCnt,1.0);
- else
- if( va_cnt == 2 )
- {
- dfltGain[0] = va_arg(vl,double);
- cmVOD_Fill(dfltGain+1,inPortCnt-1,dfltGain[0]);
- }
- else
- if( va_cnt == inPortCnt + 1 )
- {
- for(i=0; i<inPortCnt; ++i)
- dfltGain[i] = va_arg(vl,double);
- }
- else
- {
- cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The mix argument list must contain no default gain values, one default gain value, or all default gain values.");
- return NULL;
- }
-
-
- cmDspAMix_t* p = cmDspInstAllocV(cmDspAMix_t,ctx,classPtr,instSymId,id,storeSymId,0,vl,
- 1, "out", kOutAmId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
- inPortCnt, "in", kBaseInAmId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input",
- inPortCnt, "gain", baseGainAmId, 0, 0, kInDsvFl | kDoubleDsvFl, "Gain input",
- inPortCnt, "mute", baseMuteAmId, 0, 0, kInDsvFl | kBoolDsvFl, "Mute input",
- 0 );
-
-
- p->inPortCnt = inPortCnt;
- p->baseGainId = baseGainAmId;
- p->baseMuteId = baseMuteAmId;
-
-
- for(i=0; i<inPortCnt; ++i)
- {
- cmDspSetDefaultDouble( ctx, &p->inst, p->baseGainId + i, 0.0, dfltGain[i]);
- cmDspSetDefaultBool( ctx, &p->inst, p->baseMuteId + i, false, false );
- }
- /*
- // read any default gain settings
- --va_cnt;
- for(i=0; i<inPortCnt; ++i)
- {
- // if excplicit gains are not given then default to 1.0.
- double dflt = 1.0;
- if( i < va_cnt )
- dflt = va_arg(vl,double);
-
- cmDspSetDefaultDouble( ctx, &p->inst, p->baseGainId + i, 0.0, dflt);
- }
-
- */
-
- return &p->inst;
-
-
- }
-
-
- cmDspRC_t _cmDspAMixReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspApplyAllDefaults(ctx,inst);
- cmDspZeroAudioBuf(ctx,inst,kOutAmId);
- return rc;
- }
-
-
- cmDspRC_t _cmDspAMixExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspAMix_t* p = (cmDspAMix_t*)inst;
- unsigned i;
-
- cmDspZeroAudioBuf(ctx,inst,kOutAmId);
-
- unsigned n = cmDspAudioBufSmpCount(ctx,inst,kOutAmId,0);
- cmSample_t* dp = cmDspAudioBuf(ctx,inst,kOutAmId,0);
-
- for(i=0; i<p->inPortCnt; ++i)
- {
- const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kBaseInAmId+i,0);
- if( sp != NULL )
- {
- double gain = cmDspDouble(inst,p->baseGainId+i);
- cmVOS_MultSumVVS(dp,n,sp,(cmSample_t)gain);
- }
- }
-
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspAMixRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspAMix_t* p = (cmDspAMix_t*)inst;
-
- if( p->baseGainId <= evt->dstVarId && evt->dstVarId < p->baseGainId + p->inPortCnt )
- {
- cmDspSetEvent(ctx,inst,evt);
- //printf("rcv:%i %f\n",evt->dstVarId,cmDspDouble(inst,evt->dstVarId));
- }
- return rc;
- }
-
- struct cmDspClass_str* cmAMixClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmAMixDC,ctx,"AMix",
- NULL,
- _cmDspAMixAlloc,
- NULL,
- _cmDspAMixReset,
- _cmDspAMixExec,
- _cmDspAMixRecv,
- NULL,NULL,
- "Audio mixer");
-
- return &_cmAMixDC;
- }
-
- //==========================================================================================================================================
- enum
- {
- kInAsId,
- kBaseOutAsId
- };
-
- cmDspClass_t _cmASplitDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- unsigned outPortCnt;
- unsigned baseGainId;
- } cmDspASplit_t;
-
- // A splitter has one audio input port and multiple audio output ports.
- // A gain input is automatically provided for each output port.
- cmDspInst_t* _cmDspASplitAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "in", kInAsId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input" },
- { NULL, 0, 0, 0, 0 }
- };
-
- if( va_cnt < 1 )
- {
- cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'ASplit' constructor must have a count of input ports.");
- return NULL;
- }
-
- // args:
- // <outPortCnt> <gain0>, <gain1> (the default gains are optional)
-
- unsigned i,j,k;
- int outPortCnt = va_arg(vl,int);
- unsigned fixArgCnt = sizeof(args)/sizeof(args[0]) - 1;
- unsigned argCnt = fixArgCnt + 2*outPortCnt + 1;
- cmDspVarArg_t argArray[ argCnt ];
- int labelCharCnt = 15;
- cmChar_t label[ labelCharCnt + 1 ];
- label[labelCharCnt] = 0;
-
- //
- for(i=0; i<fixArgCnt; ++i)
- argArray[i] = args[i];
-
- // define the audio output port specifications
- for(j=0,k=0; j<outPortCnt; ++i,++j,++k)
- {
- snprintf(label,labelCharCnt,"out-%i",j);
- unsigned symId = cmSymTblRegisterSymbol(ctx->stH,label);
- argArray[i].label = cmSymTblLabel(ctx->stH,symId);
- argArray[i].constId = kBaseOutAsId + k;
- argArray[i].rn = 0;
- argArray[i].cn = 1;
- argArray[i].flags = kOutDsvFl | kAudioBufDsvFl;
- argArray[i].doc = "Audio Input";
- }
-
- // define the gain input specifications
- for(j=0; j<outPortCnt; ++i,++j,++k)
- {
- snprintf(label,labelCharCnt,"gain-%i",j);
- unsigned symId = cmSymTblRegisterSymbol(ctx->stH,label);
- argArray[i].label = cmSymTblLabel(ctx->stH,symId);
- argArray[i].constId = kBaseOutAsId + k;
- argArray[i].rn = 0;
- argArray[i].cn = 0;
- argArray[i].flags = kInDsvFl | kDoubleDsvFl;
- argArray[i].doc = "Gain input";
- }
-
- // set the NULL end-of-arg-array sentinel
- memset(argArray + i, 0, sizeof(argArray[0]));
-
- cmDspASplit_t* p = cmDspInstAlloc(cmDspASplit_t,ctx,classPtr,argArray,instSymId,id,storeSymId,0,vl);
-
- p->outPortCnt = outPortCnt;
- p->baseGainId = kBaseOutAsId + outPortCnt;
-
- // read any default gain settings
- --va_cnt;
- for(i=0; i<outPortCnt; ++i)
- {
- double dflt = 1.0;
- if( i < va_cnt )
- dflt = va_arg(vl,double);
-
- cmDspSetDefaultDouble( ctx, &p->inst, p->baseGainId + i, 0.0, dflt);
- }
- return &p->inst;
- }
-
- cmDspRC_t _cmDspASplitReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspASplit_t* p = (cmDspASplit_t*)inst;
- int i;
-
- cmDspApplyAllDefaults(ctx,inst);
-
- for(i=0; i<p->outPortCnt; ++i)
- cmDspZeroAudioBuf(ctx,inst,kBaseOutAsId+i);
-
- return rc;
- }
-
-
- cmDspRC_t _cmDspASplitExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspASplit_t* p = (cmDspASplit_t*)inst;
- unsigned i;
-
- unsigned n = cmDspAudioBufSmpCount(ctx,inst,kBaseOutAsId,0);
- const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kInAsId,0);
-
- for(i=0; i<p->outPortCnt; ++i)
- {
- cmSample_t* dp = cmDspAudioBuf(ctx,inst,kBaseOutAsId+i,0);
- double gain = cmDspDouble(inst,p->baseGainId+i);
- cmVOS_MultVVS(dp,n,sp,(cmSample_t)gain);
- }
-
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspASplitRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspASplit_t* p = (cmDspASplit_t*)inst;
-
- if( p->baseGainId <= evt->dstVarId && evt->dstVarId < p->baseGainId + p->outPortCnt )
- {
- cmDspSetEvent(ctx,inst,evt);
- }
- return rc;
- }
-
- struct cmDspClass_str* cmASplitClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmASplitDC,ctx,"ASplit",
- NULL,
- _cmDspASplitAlloc,
- NULL,
- _cmDspASplitReset,
- _cmDspASplitExec,
- _cmDspASplitRecv,
- NULL,NULL,
- "Audio splitter");
-
- return &_cmASplitDC;
- }
-
- //==========================================================================================================================================
- enum
- {
- kInAmId,
- kMinAmId,
- kMaxAmId,
- kValAmId, // meter value
- kLblAmId,
- };
-
- #define cmDspMeter_MIN (-100)
- #define cmDspMeter_MAX (0)
-
- cmDspClass_t _cmAMeterDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- unsigned bufN;
- unsigned idx;
- cmReal_t sum;
- cmReal_t val;
- } cmDspAMeter_t;
-
- cmDspInst_t* _cmDspAMeterAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "in", kInAmId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input"},
- { "min", kMinAmId, 0, 0, kDoubleDsvFl, "Minimum value"},
- { "max", kMaxAmId, 0, 0, kDoubleDsvFl, "Maximum value"},
- { "val", kValAmId, 0, 0, kDoubleDsvFl, "Meter value"},
- { "label",kLblAmId, 0, 0, kStrzDsvFl, "Label."},
- { NULL, 0, 0, 0, 0 }
- };
-
- cmDspAMeter_t* p = cmDspInstAlloc(cmDspAMeter_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
- double updateMs = 100;
- double sr = cmDspSampleRate(ctx);
- unsigned spc = cmDspSamplesPerCycle(ctx);
- p->bufN = cmMax(1,floor(updateMs * sr/ (1000.0 * spc)));
-
-
- cmDspSetDefaultDouble(ctx, &p->inst, kValAmId, 0.0, cmDspMeter_MIN);
- cmDspSetDefaultDouble(ctx, &p->inst, kMinAmId, 0.0, cmDspMeter_MIN);
- cmDspSetDefaultDouble(ctx, &p->inst, kMaxAmId, 0.0, cmDspMeter_MAX);
-
- // create the UI control
- cmDspUiMeterCreate(ctx,&p->inst,kMinAmId,kMaxAmId,kValAmId,kLblAmId);
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspAMeterReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspAMeter_t* p = (cmDspAMeter_t*)inst;
-
- cmDspApplyAllDefaults(ctx,inst);
-
- //cmDspZeroAudioBuf(ctx,inst,kInAmId);
-
- p->idx = 0;
- p->sum = 0;
- p->val = 0;
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspAMeterExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspAMeter_t* p = (cmDspAMeter_t*)inst;
- unsigned n = cmDspAudioBufSmpCount(ctx,inst,kInAmId,0);
- const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kInAmId,0);
-
- if( sp == NULL )
- {
- inst->execFunc = NULL; // if there is no connected input then disable further callbacks
- return kOkDspRC;
- }
-
- p->sum += cmVOS_SquaredSum(sp,n);
- ++p->idx;
-
- if( p->idx == p->bufN )
- {
- cmReal_t coeff = 0.7;
- cmReal_t rms = sqrt(p->sum/(n*p->bufN));
-
- p->idx = 0;
- p->sum = 0;
-
- p->val = rms > p->val ? rms : (rms*(1.0-coeff)) + (p->val*coeff);
-
- double db = cmMax(cmDspMeter_MIN,cmMin(cmDspMeter_MAX,20.0 * log10(p->val)));
-
- cmDspSetDouble(ctx, inst, kValAmId, db);
- }
-
-
- return kOkDspRC;
- }
-
-
- struct cmDspClass_str* cmAMeterClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmAMeterDC,ctx,"AMeter",
- NULL,
- _cmDspAMeterAlloc,
- NULL,
- _cmDspAMeterReset,
- _cmDspAMeterExec,
- NULL,
- NULL,NULL,
- "Audio meter display.");
-
- return &_cmAMeterDC;
- }
-
- //==========================================================================================================================================
-
- //
- //
- // Read files created by this object with the Octave function cmTextFile().
- //
- //
-
- enum
- {
- kCntTfId,
- kFnTfId,
- kBaseTfId
- };
-
- cmDspClass_t _cmTextFileDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- int inPortCnt;
- cmFileH_t fH;
- } cmDspTextFile_t;
-
- cmDspInst_t* _cmDspTextFileAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "cnt", kCntTfId, 0, 0, kIntDsvFl | kReqArgDsvFl, "Input port count"},
- { "fn", kFnTfId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "File name"},
- { NULL, 0, 0, 0, 0 }
- };
-
- if( va_cnt < 1 )
- {
- cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The text file object must be given a input port count argument.");
- return NULL;
- }
-
- va_list vl1;
- va_copy(vl1,vl);
-
- int i,j;
- int inPortCnt = va_arg(vl1,int);
- unsigned fixArgCnt = sizeof(args)/sizeof(args[0]) - 1;
- unsigned argCnt = fixArgCnt + inPortCnt + 1;
- cmDspVarArg_t argArray[ argCnt ];
-
-
- if( inPortCnt <= 0 )
- {
- cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The text file input port count must be a positive integer.");
- return NULL;
- }
-
- //
- for(i=0; i<fixArgCnt; ++i)
- argArray[i] = args[i];
-
- // define the input port specifications
- for(j=0; j<inPortCnt; ++j,++i)
- {
- int labelCharCnt = 15;
- cmChar_t label[ labelCharCnt + 1 ];
- label[labelCharCnt] = 0;
-
- snprintf(label,labelCharCnt,"in-%i",j);
- unsigned symId = cmSymTblRegisterSymbol(ctx->stH,label);
- argArray[i].label = cmSymTblLabel(ctx->stH,symId);
- argArray[i].constId = kBaseTfId + j;
- argArray[i].rn = 0;
- argArray[i].cn = 0;
- argArray[i].flags = kInDsvFl | kDoubleDsvFl | kSymDsvFl;
- argArray[i].doc = "Data input";
- }
-
- // set the NULL end-of-arg-array sentinel
- memset(argArray + i, 0, sizeof(argArray[0]));
-
-
- cmDspTextFile_t* p = cmDspInstAlloc(cmDspTextFile_t,ctx,classPtr,argArray,instSymId,id,storeSymId,va_cnt,vl);
-
- p->inPortCnt = inPortCnt;
- p->fH = cmFileNullHandle;
- return &p->inst;
- }
-
- cmDspRC_t _cmDspTextFileFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspTextFile_t* p = (cmDspTextFile_t*)inst;
- if( cmFileClose(&p->fH) )
- return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Text file close failed.");
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspTextFileReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
-
- cmDspTextFile_t* p = (cmDspTextFile_t*)inst;
-
- cmDspApplyAllDefaults(ctx,inst);
-
- const cmChar_t* fn = cmDspStrcz(inst,kFnTfId);
-
- if( cmFileOpen( &p->fH, fn, kWriteFileFl, ctx->cmCtx->err.rpt ) != kOkFileRC )
- rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "Text file open failed.");
-
- return rc;
- }
-
- cmDspRC_t _cmDspTextFileRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspTextFile_t* p = (cmDspTextFile_t*)inst;
- cmDspSetEvent(ctx,inst,evt);
-
- if( cmFileIsValid(p->fH) && evt->dstVarId >= kBaseTfId )
- {
- double secs = ctx->ctx->begSmpIdx / cmDspSampleRate(ctx);
- cmFilePrintf(p->fH,"%f %f %i ", secs, secs, evt->dstVarId-kBaseTfId);
-
- if( cmIsFlag(evt->valuePtr->flags,kSymDsvFl) )
- cmFilePrintf(p->fH,"%s\n", cmStringNullGuard(cmSymTblLabel(ctx->stH,cmDsvSymbol(evt->valuePtr))));
- else
- cmFilePrintf(p->fH,"%f\n", cmDspDouble(inst,evt->dstVarId));
- }
-
- return kOkDspRC;
- }
-
- struct cmDspClass_str* cmTextFileClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmTextFileDC,ctx,"TextFile",
- NULL,
- _cmDspTextFileAlloc,
- _cmDspTextFileFree,
- _cmDspTextFileReset,
- NULL,
- _cmDspTextFileRecv,
- NULL,NULL,
- "Time tagged text file.");
-
- return &_cmTextFileDC;
- }
-
- //==========================================================================================================================================
- enum
- {
- kRsrcArId,
- kCmdArId,
- kIdxArId,
- kValArId,
- kCntArId,
- kDoneArId,
- kBaseOutArId
- };
-
- cmDspClass_t _cmArrayDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- unsigned cnt;
- cmReal_t* array;
- unsigned printSymId;
- unsigned sendSymId;
- unsigned cntSymId;
- unsigned doneSymId;
- } cmDspArray_t;
-
- cmDspInst_t* _cmDspArrayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- va_list vl1;
- va_copy(vl1,vl);
-
-
- if( va_cnt < 1 )
- {
- cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The array object must be given a 'rsrc' parameter.");
- return NULL;
- }
-
- const cmChar_t* rsrcStr = va_arg(vl,cmChar_t*);
- unsigned outPortCnt = 0;
- cmReal_t* array = NULL;
- unsigned doneSymId = cmDspSysRegisterStaticSymbol(ctx->dspH,"done");
- unsigned i;
-
- if( rsrcStr == NULL )
- {
- cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The array object 'rsrc' parameter appears to be missing.");
- return NULL;
- }
-
-
- if( cmDspRsrcRealArray( ctx->dspH, &outPortCnt, &array, rsrcStr, NULL ) != kOkDspRC )
- {
- cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The array resource '%s' could not be read.",cmStringNullGuard(rsrcStr));
- return NULL;
- }
-
- cmDspArray_t* p = cmDspInstAllocV(cmDspArray_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
- 1, "rsrc", kRsrcArId, 0, 0, kStrzDsvFl | kInDsvFl | kReqArgDsvFl, "Array data resource label.",
- 1, "cmd", kCmdArId, 0, 0, kSymDsvFl | kInDsvFl, "Command: send | print | count.",
- 1, "idx", kIdxArId, 0, 0, kUIntDsvFl | kInDsvFl, "Send value at index out 'val' port.",
- 1, "val", kValArId, 0, 0, kDoubleDsvFl | kOutDsvFl, "Index output value.",
- 1, "cnt", kCntArId, 0, 0, kUIntDsvFl | kOutDsvFl, "Count output value.",
- 1, "done", kDoneArId, 0, 0, kSymDsvFl | kOutDsvFl, "'done' after last send.",
- outPortCnt, "out", kBaseOutArId, 0, 0, kDoubleDsvFl | kOutDsvFl | kSendDfltDsvFl, "Individual real value outputs.",
- 0 );
-
- cmDspSetDefaultDouble( ctx, &p->inst, kValArId, 0, 0 );
- cmDspSetDefaultUInt( ctx, &p->inst, kCntArId, 0, outPortCnt );
- cmDspSetDefaultSymbol( ctx, &p->inst, kDoneArId, doneSymId);
-
- for(i=0; i<outPortCnt; ++i)
- cmDspSetDefaultDouble( ctx, &p->inst, kBaseOutArId+i, 0, array[i] );
-
- p->array = array;
- p->cnt = outPortCnt;
- p->sendSymId = cmDspSysRegisterStaticSymbol(ctx->dspH,"send");
- p->printSymId = cmDspSysRegisterStaticSymbol(ctx->dspH,"print");
- p->cntSymId = cmDspSysRegisterStaticSymbol(ctx->dspH,"count");
- p->doneSymId = doneSymId;
- return &p->inst;
- }
-
-
- cmDspRC_t _cmDspArrayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspArray_t* p = (cmDspArray_t*)inst;
- cmDspRC_t rc;
-
- // send 'out' values and then 'done' value
- if((rc = cmDspApplyAllDefaults(ctx,inst)) == kOkDspRC )
- cmDspSetSymbol(ctx,inst,kDoneArId,p->doneSymId);
-
- return rc;
- }
-
- cmDspRC_t _cmDspArrayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc;
- cmDspArray_t* p = (cmDspArray_t*)inst;
-
- if((rc = cmDspSetEvent(ctx,inst,evt)) != kOkDspRC )
- return rc;
-
- switch( evt->dstVarId )
- {
- case kCmdArId:
- {
- unsigned i;
- unsigned cmdSymId = cmDsvSymbol(evt->valuePtr);
-
- if( cmdSymId == p->printSymId )
- {
- for(i=0; i<p->cnt; ++i)
- cmRptPrintf(ctx->rpt,"%f ",p->array[i]);
- cmRptPrintf(ctx->rpt,"\n");
- }
- else
- if( cmdSymId == p->sendSymId )
- {
- for(i=0; i<p->cnt; ++i)
- cmDspSetDouble(ctx,inst,kBaseOutArId+i,p->array[i]);
- cmDspSetSymbol(ctx,inst,kDoneArId,p->doneSymId);
- }
- else
- if( cmdSymId == p->cntSymId )
- {
- cmDspSetUInt(ctx,inst,kCntArId,p->cnt);
- }
- }
- break;
-
- case kIdxArId:
- {
- unsigned idx = cmDsvUInt(evt->valuePtr);
- if( idx < p->cnt )
- cmDspSetDouble(ctx,inst,kValArId,p->array[idx]);
- }
- break;
- }
-
-
- return kOkDspRC;
- }
-
- struct cmDspClass_str* cmArrayClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmArrayDC,ctx,"Array",
- NULL,
- _cmDspArrayAlloc,
- NULL,
- _cmDspArrayReset,
- NULL,
- _cmDspArrayRecv,
- NULL,NULL,
- "Time tagged text file.");
-
- return &_cmArrayDC;
- }
-
-
- //==========================================================================================================================================
-
- enum
- {
- kMidiPcId,
- kHzPcId,
- kOffsPcId,
- kStrPcId,
- kRatioPcId
- };
-
- cmDspClass_t _cmPitchCvtDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- int midi;
- double hz;
- } cmDspPitchCvt_t;
-
- cmDspInst_t* _cmDspPitchCvtAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- va_cnt = 0; // ignore an errant arguments
-
- cmDspPitchCvt_t* p = cmDspInstAllocV(cmDspPitchCvt_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
- 1, "midi", kMidiPcId, 0, 0, kUIntDsvFl | kInDsvFl | kOutDsvFl | kOptArgDsvFl, "MIDI pitch value input.",
- 1, "hz", kHzPcId, 0, 0, kDoubleDsvFl | kInDsvFl | kOutDsvFl | kOptArgDsvFl, "Hz pitch value.",
- 1, "offs", kOffsPcId, 0, 0, kDoubleDsvFl | kInDsvFl | kInDsvFl | kOptArgDsvFl, "Semitone offset.",
- 1, "str", kStrPcId, 0, 0, kStrzDsvFl | kOutDsvFl, "Pitch string output.",
- 1, "ratio", kRatioPcId, 0, 0, kDoubleDsvFl | kOutDsvFl, "Offset as a ratio",
- 0 );
-
- cmDspSetDefaultUInt( ctx, &p->inst, kMidiPcId, 0, 0 );
- cmDspSetDefaultDouble( ctx, &p->inst, kHzPcId, 0, 0.0 );
- cmDspSetDefaultDouble( ctx, &p->inst, kOffsPcId, 0, 0 );
- cmDspSetDefaultStrcz( ctx, &p->inst, kStrPcId, NULL, "" );
- cmDspSetDefaultDouble( ctx, &p->inst, kRatioPcId, 0, 0 );
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspPitchCvtReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = cmDspApplyAllDefaults(ctx,inst);
- return rc;
- }
-
- cmDspRC_t _cmDspPitchCvtOutput( cmDspCtx_t* ctx, cmDspPitchCvt_t* p )
- {
- cmDspInst_t* inst = &p->inst;
- double offs = cmDspDouble( inst, kOffsPcId );
- unsigned midi = cmMax(0, p->midi + rint(offs) );
- double ratio= pow(2.0,offs/12.0);
- double hz = p->hz * ratio;
-
- //cmRptPrintf(ctx->rpt,"%i %i %f %f\n",offs,midi,hz,p->hz);
-
- cmDspSetStrcz( ctx, inst, kStrPcId, cmMidiToSciPitch(midi,NULL,0));
- cmDspSetUInt( ctx, inst, kMidiPcId, midi);
- cmDspSetDouble( ctx, inst, kHzPcId, hz);
- cmDspSetDouble( ctx, inst, kRatioPcId, ratio );
-
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspPitchCvtRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspPitchCvt_t* p = (cmDspPitchCvt_t*)inst;
-
- switch( evt->dstVarId )
- {
- case kMidiPcId:
- p->midi = cmMax(0,cmDsvGetInt(evt->valuePtr));
- p->hz = cmMidiToHz(p->midi);
- rc = _cmDspPitchCvtOutput(ctx,p);
- break;
-
- case kHzPcId:
- p->hz = cmDsvGetDouble(evt->valuePtr);
- p->midi = cmHzToMidi(p->hz);
- rc = _cmDspPitchCvtOutput(ctx,p);
- break;
-
- case kOffsPcId:
- if((rc = cmDspSetEvent(ctx, inst, evt )) == kOkDspRC )
- rc = _cmDspPitchCvtOutput(ctx,p);
- break;
- }
-
- return rc;
- }
-
- struct cmDspClass_str* cmPitchCvtClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmPitchCvtDC,ctx,"PitchCvt",
- NULL,
- _cmDspPitchCvtAlloc,
- NULL,
- _cmDspPitchCvtReset,
- NULL,
- _cmDspPitchCvtRecv,
- NULL,NULL,
- "Time tagged text file.");
-
- return &_cmPitchCvtDC;
- }
-
- //==========================================================================================================================================
-
- //
- //
- // Create a file which can be read by readBinFile.m
- //
- //
-
- enum
- {
- kCntBmId,
- kFnBmId,
- kBaseBmId
- };
-
- cmDspClass_t _cmBinMtxFileDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- int inPortCnt;
- cmBinMtxFile_t* bmfp;
- cmReal_t * valArray; // valArray[ inPortCnt ]
- } cmDspBinMtxFile_t;
-
- cmDspInst_t* _cmDspBinMtxFileAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "cnt", kCntBmId, 0, 0, kIntDsvFl | kReqArgDsvFl, "Input port count"},
- { "fn", kFnBmId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "File name"},
- };
-
-
- if( va_cnt < 1 )
- {
- cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The binary matrix file object must be given a input port count argument.");
- return NULL;
- }
-
- va_list vl1;
- va_copy(vl1,vl);
-
- int inPortCnt = va_arg(vl,int);
- unsigned fixArgCnt = sizeof(args)/sizeof(args[0]);
- unsigned argCnt = fixArgCnt + inPortCnt;
- cmDspVarArg_t a[ argCnt+1 ];
- cmDspBinMtxFile_t* p = NULL;
-
- if( inPortCnt <= 0 )
- {
- cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The binary matrix file must be a positive integer.");
- return NULL;
- }
-
- cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
- cmDspArgSetupN(ctx, a, argCnt, kBaseBmId, inPortCnt, "in", kBaseBmId, 0, 0, kInDsvFl | kDoubleDsvFl, "input ports");
- cmDspArgSetupNull( a+argCnt );
-
- if((p = cmDspInstAlloc(cmDspBinMtxFile_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl1)) == NULL )
- return NULL;
-
- p->bmfp = cmBinMtxFileAlloc(ctx->cmProcCtx, NULL, NULL );
- p->inPortCnt = inPortCnt;
- p->valArray = cmMemAllocZ(cmReal_t,inPortCnt);
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspBinMtxFileOpen( cmDspCtx_t* ctx, cmDspInst_t* inst )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspBinMtxFile_t* p = (cmDspBinMtxFile_t*)inst;
-
- if( p->bmfp != NULL )
- {
- // finalize the current file
- if( cmBinMtxFileFinal(p->bmfp) != cmOkRC )
- cmDspInstErr(ctx,inst,kFileCloseFailDspRC,"File close failed.");
-
- // open a new one
- if( cmBinMtxFileInit( p->bmfp, cmDspDefaultStrcz(&p->inst,kFnBmId) ) != cmOkRC)
- rc = cmDspInstErr(ctx,inst,kFileOpenFailDspRC,"File open failed.");
- }
-
- return rc;
- }
-
- cmDspRC_t _cmDspBinMtxFileFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspBinMtxFile_t* p = (cmDspBinMtxFile_t*)inst;
-
- cmBinMtxFileFree(&p->bmfp);
- cmMemFree(p->valArray);
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspBinMtxFileReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspApplyAllDefaults(ctx,inst);
- return _cmDspBinMtxFileOpen(ctx,inst);
- }
- cmDspRC_t _cmDspBinMtxFileExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspBinMtxFile_t* p = (cmDspBinMtxFile_t*)inst;
-
- // write the file
- if( cmBinMtxFileIsValid( p->bmfp ) )
- if( cmBinMtxFileExecR(p->bmfp, p->valArray, p->inPortCnt ) != cmOkRC )
- return cmDspInstErr(ctx,inst,kFileWriteFailDspRC,"File write failure.");
-
- return rc;
- }
- cmDspRC_t _cmDspBinMtxFileRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
- cmDspBinMtxFile_t* p = (cmDspBinMtxFile_t*)inst;
- cmDspSetEvent(ctx,inst,evt);
-
- // new file name - create new output file
- if( evt->dstVarId == kFnBmId )
- {
- rc = _cmDspBinMtxFileOpen(ctx,inst);
- }
- else
- // new value - store in p->valArray[]
- if( kBaseBmId <= evt->dstVarId && evt->dstVarId < kBaseBmId + p->inPortCnt )
- {
- p->valArray[ evt->dstVarId - kBaseBmId ] = cmDspDouble(inst,evt->dstVarId );
- }
- else
- { assert(0); }
-
- return rc;
- }
-
- struct cmDspClass_str* cmBinMtxFileClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmBinMtxFileDC,ctx,"BinMtxFile",
- NULL,
- _cmDspBinMtxFileAlloc,
- _cmDspBinMtxFileFree,
- _cmDspBinMtxFileReset,
- _cmDspBinMtxFileExec,
- _cmDspBinMtxFileRecv,
- NULL,NULL,
- "Time tagged text file.");
-
- return &_cmBinMtxFileDC;
- }
-
-
- //==========================================================================================================================================
- enum
- {
- kHopMsSbId,
- kWndFactSbId,
- kInSbId,
- kOutSbId
- };
-
- cmDspClass_t _cmShiftBufDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- cmShiftBuf* sbp;
- } cmDspShiftBuf_t;
-
- void _cmDspShiftBufSetup( cmDspCtx_t* ctx, cmDspShiftBuf_t* p )
- {
- double hopMs = cmDspDouble(&p->inst,kHopMsSbId);
- unsigned hopSmpCnt = lround(cmDspSampleRate(ctx) * hopMs / 1000.0 );
- unsigned wndSmpCnt = cmDspUInt(&p->inst,kWndFactSbId) * hopSmpCnt;
-
- if( p->sbp == NULL || hopSmpCnt != p->sbp->hopSmpCnt || wndSmpCnt != p->sbp->wndSmpCnt )
- {
- cmShiftBufFree(&p->sbp);
- p->sbp = cmShiftBufAlloc(ctx->cmProcCtx, NULL, cmDspSamplesPerCycle(ctx), wndSmpCnt, hopSmpCnt );
-
- }
- }
-
- cmDspInst_t* _cmDspShiftBufAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "hopMs", kHopMsSbId, 0, 0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Hop size on milliseconds"},
- { "wndFact", kWndFactSbId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Count of hops contained in each output buffer."},
- { "in", kInSbId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio input"},
- { "out", kOutSbId, 0, 0, kOutDsvFl | kAudioBufDsvFl, "Audio output"},
- { NULL, 0, 0, 0, 0 }
- };
-
- // Note: by setting the column count of the output audio variable to zero
- // we prevent it from being automatically assigned vector memory.
-
- cmDspShiftBuf_t* p = cmDspInstAlloc(cmDspShiftBuf_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
-
-
- return &p->inst;
- }
-
- cmDspRC_t _cmDspShiftBufFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspShiftBuf_t* p = (cmDspShiftBuf_t*)inst;
-
- cmShiftBufFree(&p->sbp);
-
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspShiftBufReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspRC_t rc = kOkDspRC;
-
- cmDspShiftBuf_t* p = (cmDspShiftBuf_t*)inst;
-
- cmDspApplyAllDefaults(ctx,inst);
-
- _cmDspShiftBufSetup(ctx,p);
-
- return rc;
- }
-
- cmDspRC_t _cmDspShiftBufRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspShiftBuf_t* p = (cmDspShiftBuf_t*)inst;
- cmDspSetEvent(ctx,inst,evt);
-
- switch(evt->dstVarId)
- {
- case kHopMsSbId:
- case kWndFactSbId:
- _cmDspShiftBufSetup( ctx, p );
- break;
- };
-
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspShiftBufExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspShiftBuf_t* p = (cmDspShiftBuf_t*)inst;
- unsigned sn = cmDspAudioBufSmpCount(ctx,inst,kInSbId,0);
- const cmSample_t* sp = cmDspAudioBuf(ctx,inst,kInSbId,0);
- cmDspVar_t* varPtr = cmDspVarIdToPtr(inst,kOutSbId);
-
- if( cmShiftBufExec(p->sbp, sp, sn ) )
- cmDsvSetSampleMtx( &varPtr->value, p->sbp->outV, p->sbp->outN, 1);
- else
- cmDsvSetSampleMtx( &varPtr->value, NULL, 0, 0);
-
- return kOkDspRC;
- }
-
- struct cmDspClass_str* cmShiftBufClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmShiftBufDC,ctx,"ShiftBuf",
- NULL,
- _cmDspShiftBufAlloc,
- _cmDspShiftBufFree,
- _cmDspShiftBufReset,
- _cmDspShiftBufExec,
- _cmDspShiftBufRecv,
- NULL,NULL,
- "Time tagged text file.");
-
- return &_cmShiftBufDC;
- }
-
- //==========================================================================================================================================
- enum
- {
- kInNsId
-
- };
-
- cmDspClass_t _cmNetSendDC;
-
- typedef struct
- {
- cmDspInst_t inst;
- _cmDspSrcConn_t* srcConnPtr;
- } cmDspNetSend_t;
-
- cmDspInst_t* _cmDspNetSendAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
- {
- cmDspVarArg_t args[] =
- {
- { "in", kInNsId, 0, 0, kInDsvFl | kTypeDsvMask, "Input port" },
- { NULL, 0, 0, 0, 0 }
- };
-
- assert( va_cnt == 1 );
- _cmDspSrcConn_t* srcConnPtr = va_arg(vl,_cmDspSrcConn_t*);
- assert( srcConnPtr != NULL );
-
- cmDspNetSend_t* p = cmDspInstAlloc(cmDspNetSend_t,ctx,classPtr,args,instSymId,id,storeSymId,0,vl);
- p->srcConnPtr = srcConnPtr;
- return &p->inst;
- }
-
- cmDspRC_t _cmDspNetSendReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspNetSendRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- cmDspNetSend_t* p = (cmDspNetSend_t*)inst;
- return _cmDspSysNetSendEvent(ctx->dspH, p->srcConnPtr->dstNetNodeId, p->srcConnPtr->dstId, evt );
- }
-
- struct cmDspClass_str* cmNetSendClassCons( cmDspCtx_t* ctx )
- {
- cmDspClassSetup(&_cmNetSendDC,ctx,"NetSend",
- NULL,
- _cmDspNetSendAlloc,
- NULL,
- _cmDspNetSendReset,
- NULL,
- _cmDspNetSendRecv,
- NULL,NULL,
- "Print the value of any event arriving at 'in'.");
-
- return &_cmNetSendDC;
- }
-
- //==========================================================================================================================================
-
- //==========================================================================================================================================
-
-
- cmDspClassConsFunc_t _cmDspClassBuiltInArray[] =
- {
- cmPrinterClassCons,
- cmCounterClassCons,
-
- cmPhasorClassCons,
- cmAudioInClassCons,
- cmAudioOutClassCons,
- cmAudioFileOutClassCons,
- cmSigGenClassCons,
-
- cmScalarClassCons,
- cmTextClassCons,
- cmMeterClassCons,
- cmLabelClassCons,
- cmButtonClassCons,
-
- cmReorderClassCons,
- cmFnameClassCons,
- cmMsgListClassCons,
- cmWaveTableClassCons,
-
- cmSprintfClassCons,
- cmAMixClassCons,
- cmASplitClassCons,
- cmAMeterClassCons,
- cmTextFileClassCons,
- cmBinMtxFileClassCons,
- cmArrayClassCons,
- cmPitchCvtClassCons,
-
- cmShiftBufClassCons,
- cmNetSendClassCons,
-
- cmDelayClassCons,
- cmPShiftClassCons,
- cmLoopRecdClassCons,
- cmRectifyClassCons,
- cmGateDetectClassCons,
- cmAutoGainClassCons,
- cmEnvFollowClassCons,
- cmXfaderClassCons,
- cmChCfgClassCons,
- cmChordDetectClassCons,
- cmFaderClassCons,
- cmNoteSelectClassCons,
- cmNetNoteSelectClassCons,
- cmCombFiltClassCons,
- cmScalarOpClassCons,
-
- cmGroupSelClassCons,
- cmAudioNofMClassCons,
- cmRingModClassCons,
- cmMsgDelayClassCons,
-
- cmLineClassCons,
- cmAdsrClassCons,
- cmCompressorClassCons,
- cmBiQuadEqClassCons,
- cmDistDsClassCons,
- cmDbToLinClassCons,
- cmMtDelayClassCons,
-
- cmNofMClassCons,
- cm1ofNClassCons,
- cm1UpClassCons,
- cmGateToSymClassCons,
- cmPortToSymClassCons,
- cmRouterClassCons,
- cmAvailChClassCons,
-
- cmPresetClassCons,
- cmBcastSymClassCons,
- cmSegLineClassCons,
-
- cmKrClassCons,
- cmTimeLineClassCons,
- cmScoreClassCons,
- cmMidiFilePlayClassCons,
- cmScFolClassCons,
- cmScModClassCons,
- cmGSwitchClassCons,
-
- NULL,
- };
-
- cmDspClassConsFunc_t cmDspClassGetBuiltIn( unsigned index )
- {
- unsigned n = sizeof(_cmDspClassBuiltInArray)/sizeof(cmDspClass_t*);
-
- if( index >= n )
- return NULL;
-
- return _cmDspClassBuiltInArray[index];
- }
|