libcm is a C development framework with an emphasis on audio signal processing applications.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

cmDspBuiltIn.c 158KB

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