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

cmDspBuiltIn.c 170KB

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