libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmDspKr.c 147KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmFloatTypes.h"
  4. #include "cmComplexTypes.h"
  5. #include "cmRpt.h"
  6. #include "cmErr.h"
  7. #include "cmCtx.h"
  8. #include "cmMem.h"
  9. #include "cmMallocDebug.h"
  10. #include "cmLinkedHeap.h"
  11. #include "cmFile.h"
  12. #include "cmSymTbl.h"
  13. #include "cmJson.h"
  14. #include "cmText.h"
  15. #include "cmPrefs.h"
  16. #include "cmDspValue.h"
  17. #include "cmMsgProtocol.h"
  18. #include "cmThread.h"
  19. #include "cmUdpPort.h"
  20. #include "cmUdpNet.h"
  21. #include "cmSerialPort.h"
  22. //( { file_desc:"'snap' audio effects performance analysis units." kw:[snap]}
  23. #include "cmTime.h"
  24. #include "cmAudioSys.h"
  25. #include "cmDspCtx.h"
  26. #include "cmDspClass.h"
  27. #include "cmDspStore.h"
  28. #include "cmDspUi.h"
  29. #include "cmDspSys.h"
  30. #include "cmMath.h"
  31. #include "cmAudioFile.h"
  32. #include "cmFileSys.h"
  33. #include "cmProcObj.h"
  34. #include "cmProcTemplateMain.h"
  35. #include "cmProc.h"
  36. #include "cmMidi.h"
  37. #include "cmProc2.h"
  38. #include "cmVectOpsTemplateMain.h"
  39. #include "cmAudioFile.h"
  40. #include "cmMidiFile.h"
  41. #include "cmTimeLine.h"
  42. #include "cmScore.h"
  43. #include "cmProc4.h"
  44. #include "cmProc5.h"
  45. #include "cmSyncRecd.h"
  46. #include "cmTakeSeqBldr.h"
  47. //------------------------------------------------------------------------------------------------------------
  48. //)
  49. //( { label:cmDspKr file_desc:"Spectral non-linear distortion effect." kw:[sunit] }
  50. enum
  51. {
  52. kWndSmpCntKrId,
  53. kHopFactKrId,
  54. kModeKrId,
  55. kThreshKrId,
  56. kLwrSlopeKrId,
  57. kUprSlopeKrId,
  58. kOffsetKrId,
  59. kInvertKrId,
  60. kBypassKrId,
  61. kWetKrId,
  62. kAudioInKrId,
  63. kAudioOutKrId
  64. };
  65. typedef struct
  66. {
  67. cmDspInst_t inst;
  68. cmCtx* ctx;
  69. cmSpecDist_t* sdp;
  70. } cmDspKr_t;
  71. cmDspClass_t _cmKrDC;
  72. cmDspInst_t* _cmDspKrAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  73. {
  74. cmDspVarArg_t args[] =
  75. {
  76. { "wndn", kWndSmpCntKrId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Window sample count" },
  77. { "hopf", kHopFactKrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Hop factor" },
  78. { "mode", kModeKrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Mode 0=bypass 1=basic 2=spec cnt 3=amp env" },
  79. { "thrh", kThreshKrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Threshold" },
  80. { "lwrs", kLwrSlopeKrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Lower Slope"},
  81. { "uprs", kUprSlopeKrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Upper Slope"},
  82. { "offs", kOffsetKrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Offset"},
  83. { "invt", kInvertKrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Invert"},
  84. { "bypass", kBypassKrId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Bypass enable flag." },
  85. { "wet", kWetKrId, 0, 0, kInDsvFl | kSampleDsvFl, "Wet mix level."},
  86. { "in", kAudioInKrId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio Input" },
  87. { "out", kAudioOutKrId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio Output" },
  88. { NULL, 0, 0, 0, 0 }
  89. };
  90. cmDspKr_t* p = cmDspInstAlloc(cmDspKr_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  91. unsigned defWndSmpCnt = cmDspDefaultUInt(&p->inst,kWndSmpCntKrId);
  92. unsigned wndSmpCnt = cmNextPowerOfTwo( defWndSmpCnt );
  93. cmDspSetDefaultUInt( ctx,&p->inst, kWndSmpCntKrId, defWndSmpCnt, wndSmpCnt );
  94. cmDspSetDefaultUInt( ctx,&p->inst, kHopFactKrId, 0, 4 );
  95. cmDspSetDefaultUInt( ctx,&p->inst, kModeKrId, 0, kBasicModeSdId );
  96. cmDspSetDefaultDouble( ctx,&p->inst, kThreshKrId, 0, 60.0 );
  97. cmDspSetDefaultDouble( ctx,&p->inst, kLwrSlopeKrId, 0, 2.0 );
  98. cmDspSetDefaultDouble( ctx,&p->inst, kUprSlopeKrId, 0, 0.0 );
  99. cmDspSetDefaultDouble( ctx,&p->inst, kOffsetKrId, 0, 30.0);
  100. cmDspSetDefaultUInt( ctx,&p->inst, kInvertKrId, 0, 0 );
  101. cmDspSetDefaultUInt( ctx,&p->inst, kBypassKrId, 0, 0 );
  102. cmDspSetDefaultSample( ctx,&p->inst, kWetKrId, 0, 1.0);
  103. //_cmDspKrCmInit(ctx,p); // initialize the cm library
  104. p->ctx = cmCtxAlloc(NULL,ctx->rpt,ctx->lhH,ctx->stH);
  105. return &p->inst;
  106. }
  107. cmDspRC_t _cmDspKrFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  108. {
  109. cmDspRC_t rc = kOkDspRC;
  110. cmDspKr_t* p = (cmDspKr_t*)inst;
  111. cmSpecDistFree(&p->sdp);
  112. cmCtxFree(&p->ctx);
  113. //_cmDspKrCmFinal(ctx,p); // finalize the cm library
  114. return rc;
  115. }
  116. cmDspRC_t _cmDspKrSetup(cmDspCtx_t* ctx, cmDspKr_t* p )
  117. {
  118. cmDspRC_t rc = kOkDspRC;
  119. unsigned wndSmpCnt = cmDspUInt(&p->inst,kWndSmpCntKrId);
  120. unsigned hopFact = cmDspUInt(&p->inst,kHopFactKrId);
  121. unsigned olaWndTypeId =kHannWndId;
  122. cmSpecDistFree(&p->sdp);
  123. p->sdp = cmSpecDistAlloc(p->ctx, NULL, cmDspSamplesPerCycle(ctx), cmDspSampleRate(ctx), wndSmpCnt, hopFact, olaWndTypeId);
  124. assert(p->sdp != NULL );
  125. if((rc = cmDspZeroAudioBuf(ctx,&p->inst,kAudioOutKrId)) != kOkDspRC )
  126. return rc;
  127. return rc;
  128. }
  129. cmDspRC_t _cmDspKrReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  130. {
  131. cmDspKr_t* p = (cmDspKr_t*)inst;
  132. cmDspRC_t rc;
  133. if((rc = cmDspApplyAllDefaults(ctx,inst)) != kOkDspRC )
  134. return rc;
  135. return _cmDspKrSetup(ctx,p);
  136. }
  137. cmDspRC_t _cmDspKrExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  138. {
  139. cmDspKr_t* p = (cmDspKr_t*)inst;
  140. cmDspRC_t rc = kOkDspRC;
  141. unsigned iChIdx = 0;
  142. const cmSample_t* ip = cmDspAudioBuf(ctx,inst,kAudioInKrId,iChIdx);
  143. unsigned iSmpCnt = cmDspVarRows(inst,kAudioInKrId);
  144. // if no connected
  145. if( iSmpCnt == 0 )
  146. return rc;
  147. unsigned oChIdx = 0;
  148. cmSample_t* op = cmDspAudioBuf(ctx,inst,kAudioOutKrId,oChIdx);
  149. unsigned oSmpCnt = cmDspVarRows(inst,kAudioOutKrId);
  150. const cmSample_t* sp;
  151. cmSample_t wet = cmDspSample(inst,kWetKrId);
  152. cmSpecDistExec(p->sdp,ip,iSmpCnt);
  153. if((sp = cmSpecDistOut(p->sdp)) != NULL )
  154. {
  155. cmVOS_MultVVS(op,oSmpCnt,sp,wet);
  156. }
  157. if( wet<1.0 )
  158. cmVOS_MultSumVVS(op,oSmpCnt,ip,1.0-wet);
  159. return rc;
  160. }
  161. cmDspRC_t _cmDspKrRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  162. {
  163. cmDspKr_t* p = (cmDspKr_t*)inst;
  164. cmDspRC_t rc = kOkDspRC;
  165. cmDspSetEvent(ctx,inst,evt);
  166. switch( evt->dstVarId )
  167. {
  168. case kWndSmpCntKrId:
  169. case kHopFactKrId:
  170. _cmDspKrSetup(ctx,p);
  171. // THIS IS A HACK
  172. // WHEN WND OR HOP CHANGE THE RESULTING CHANGES
  173. // SHOULD BE ISOLATED IN cmSpecDist() AND THE
  174. // CURRENT STATE OF THE PARAMETERS SHOULD NOT BE
  175. // LOST - IF THE CHANGES WERE ISOLATED WITHIN PVANL
  176. // AND PVSYN IT MIGHT BE POSSIBLE TO DO WITH
  177. // MINIMAL AUDIO INTERUPTION.
  178. p->sdp->mode = cmDspUInt(inst,kModeKrId);
  179. p->sdp->thresh = cmDspDouble(inst,kThreshKrId);
  180. p->sdp->uprSlope = cmDspDouble(inst,kUprSlopeKrId);
  181. p->sdp->lwrSlope = cmDspDouble(inst,kLwrSlopeKrId);
  182. p->sdp->offset = cmDspDouble(inst,kOffsetKrId);
  183. p->sdp->invertFl = cmDspUInt(inst,kInvertKrId)!=0;
  184. printf("wsn:%i hsn:%i\n",p->sdp->wndSmpCnt,p->sdp->hopSmpCnt);
  185. break;
  186. case kModeKrId:
  187. p->sdp->mode = cmDspUInt(inst,kModeKrId);
  188. printf("mode:%i\n",p->sdp->mode);
  189. break;
  190. case kThreshKrId:
  191. p->sdp->thresh = cmDspDouble(inst,kThreshKrId);
  192. //printf("thr:p:%p sdp:%p %f\n",p,p->sdp,p->sdp->thresh);
  193. break;
  194. case kUprSlopeKrId:
  195. p->sdp->uprSlope = cmDspDouble(inst,kUprSlopeKrId);
  196. //printf("upr slope:%f\n",p->sdp->uprSlope);
  197. break;
  198. case kLwrSlopeKrId:
  199. p->sdp->lwrSlope = cmDspDouble(inst,kLwrSlopeKrId);
  200. //printf("upr slope:%f\n",p->sdp->lwrSlope);
  201. break;
  202. case kOffsetKrId:
  203. p->sdp->offset = cmDspDouble(inst,kOffsetKrId);
  204. break;
  205. case kInvertKrId:
  206. p->sdp->invertFl = cmDspUInt(inst,kInvertKrId)!=0;
  207. break;
  208. case kWetKrId:
  209. break;
  210. default:
  211. { assert(0); }
  212. }
  213. return rc;
  214. }
  215. cmDspClass_t* cmKrClassCons( cmDspCtx_t* ctx )
  216. {
  217. cmDspClassSetup(&_cmKrDC,ctx,"Kr",
  218. NULL,
  219. _cmDspKrAlloc,
  220. _cmDspKrFree,
  221. _cmDspKrReset,
  222. _cmDspKrExec,
  223. _cmDspKrRecv,
  224. NULL,NULL,
  225. "Fourier based non-linear transformer.");
  226. return &_cmKrDC;
  227. }
  228. //------------------------------------------------------------------------------------------------------------
  229. //)
  230. //( { label:cmDspKr2 file_desc:"Spectral non-linear distortion effect." kw:[sunit] }
  231. enum
  232. {
  233. kWndSmpCntKr2Id,
  234. kHopFactKr2Id,
  235. kCeilKr2Id,
  236. kExpoKr2Id,
  237. kThreshKr2Id,
  238. kLwrSlopeKr2Id,
  239. kUprSlopeKr2Id,
  240. kMixKr2Id,
  241. kWetKr2Id,
  242. kAudioInKr2Id,
  243. kAudioOutKr2Id
  244. };
  245. typedef struct
  246. {
  247. cmDspInst_t inst;
  248. cmCtx* ctx;
  249. cmSpecDist2_t* sdp;
  250. } cmDspKr2_t;
  251. cmDspClass_t _cmKr2DC;
  252. cmDspInst_t* _cmDspKr2Alloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  253. {
  254. cmDspVarArg_t args[] =
  255. {
  256. { "wndn", kWndSmpCntKr2Id, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Window sample count" },
  257. { "hopf", kHopFactKr2Id, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Hop factor" },
  258. { "ceil", kCeilKr2Id, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Ceiling" },
  259. { "expo", kExpoKr2Id, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Exponent" },
  260. { "thrh", kThreshKr2Id, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Threshold" },
  261. { "lwrs", kLwrSlopeKr2Id, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Lower Slope"},
  262. { "uprs", kUprSlopeKr2Id, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Upper Slope"},
  263. { "mix", kMixKr2Id, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Mix"},
  264. { "wet", kWetKr2Id, 0, 0, kInDsvFl | kSampleDsvFl, "Wet mix level."},
  265. { "in", kAudioInKr2Id, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio Input" },
  266. { "out", kAudioOutKr2Id, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio Output" },
  267. { NULL, 0, 0, 0, 0 }
  268. };
  269. cmDspKr2_t* p = cmDspInstAlloc(cmDspKr2_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  270. unsigned defWndSmpCnt = cmDspDefaultUInt(&p->inst,kWndSmpCntKr2Id);
  271. unsigned wndSmpCnt = cmNextPowerOfTwo( defWndSmpCnt );
  272. cmDspSetDefaultUInt( ctx,&p->inst, kWndSmpCntKr2Id, defWndSmpCnt, wndSmpCnt );
  273. cmDspSetDefaultUInt( ctx,&p->inst, kHopFactKr2Id, 0, 4 );
  274. cmDspSetDefaultDouble( ctx,&p->inst, kCeilKr2Id, 0, 20.0 );
  275. cmDspSetDefaultDouble( ctx,&p->inst, kExpoKr2Id, 0, 2.0 );
  276. cmDspSetDefaultDouble( ctx,&p->inst, kThreshKr2Id, 0, 60.0 );
  277. cmDspSetDefaultDouble( ctx,&p->inst, kLwrSlopeKr2Id, 0, 2.0 );
  278. cmDspSetDefaultDouble( ctx,&p->inst, kUprSlopeKr2Id, 0, 0.0 );
  279. cmDspSetDefaultDouble( ctx,&p->inst, kMixKr2Id, 0, 0.0 );
  280. cmDspSetDefaultSample( ctx,&p->inst, kWetKr2Id, 0, 1.0);
  281. //_cmDspKr2CmInit(ctx,p); // initialize the cm library
  282. p->ctx = cmCtxAlloc(NULL,ctx->rpt,ctx->lhH,ctx->stH);
  283. return &p->inst;
  284. }
  285. cmDspRC_t _cmDspKr2Free(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  286. {
  287. cmDspRC_t rc = kOkDspRC;
  288. cmDspKr2_t* p = (cmDspKr2_t*)inst;
  289. cmSpecDist2Free(&p->sdp);
  290. cmCtxFree(&p->ctx);
  291. //_cmDspKr2CmFinal(ctx,p); // finalize the cm library
  292. return rc;
  293. }
  294. cmDspRC_t _cmDspKr2Setup(cmDspCtx_t* ctx, cmDspKr2_t* p )
  295. {
  296. cmDspRC_t rc = kOkDspRC;
  297. unsigned wndSmpCnt = cmDspUInt(&p->inst,kWndSmpCntKr2Id);
  298. unsigned hopFact = cmDspUInt(&p->inst,kHopFactKr2Id);
  299. unsigned olaWndTypeId =kHannWndId;
  300. cmSpecDist2Free(&p->sdp);
  301. p->sdp = cmSpecDist2Alloc(p->ctx, NULL, cmDspSamplesPerCycle(ctx), cmDspSampleRate(ctx), wndSmpCnt, hopFact, olaWndTypeId);
  302. assert(p->sdp != NULL );
  303. if((rc = cmDspZeroAudioBuf(ctx,&p->inst,kAudioOutKr2Id)) != kOkDspRC )
  304. return rc;
  305. return rc;
  306. }
  307. cmDspRC_t _cmDspKr2Reset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  308. {
  309. cmDspKr2_t* p = (cmDspKr2_t*)inst;
  310. cmDspRC_t rc;
  311. if((rc = cmDspApplyAllDefaults(ctx,inst)) != kOkDspRC )
  312. return rc;
  313. return _cmDspKr2Setup(ctx,p);
  314. }
  315. cmDspRC_t _cmDspKr2Exec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  316. {
  317. cmDspKr2_t* p = (cmDspKr2_t*)inst;
  318. cmDspRC_t rc = kOkDspRC;
  319. unsigned iChIdx = 0;
  320. const cmSample_t* ip = cmDspAudioBuf(ctx,inst,kAudioInKr2Id,iChIdx);
  321. unsigned iSmpCnt = cmDspVarRows(inst,kAudioInKr2Id);
  322. // if no connected
  323. if( iSmpCnt == 0 )
  324. return rc;
  325. unsigned oChIdx = 0;
  326. cmSample_t* op = cmDspAudioBuf(ctx,inst,kAudioOutKr2Id,oChIdx);
  327. unsigned oSmpCnt = cmDspVarRows(inst,kAudioOutKr2Id);
  328. const cmSample_t* sp;
  329. cmSample_t wet = cmDspSample(inst,kWetKr2Id);
  330. cmSpecDist2Exec(p->sdp,ip,iSmpCnt);
  331. if((sp = cmSpecDist2Out(p->sdp)) != NULL )
  332. {
  333. cmVOS_MultVVS(op,oSmpCnt,sp,wet);
  334. }
  335. if( wet<1.0 )
  336. cmVOS_MultSumVVS(op,oSmpCnt,ip,1.0-wet);
  337. return rc;
  338. }
  339. cmDspRC_t _cmDspKr2Recv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  340. {
  341. cmDspKr2_t* p = (cmDspKr2_t*)inst;
  342. cmDspRC_t rc = kOkDspRC;
  343. cmDspSetEvent(ctx,inst,evt);
  344. switch( evt->dstVarId )
  345. {
  346. case kWndSmpCntKr2Id:
  347. case kHopFactKr2Id:
  348. _cmDspKr2Setup(ctx,p);
  349. // THIS IS A HACK
  350. // WHEN WND OR HOP CHANGE THE RESULTING CHANGES
  351. // SHOULD BE ISOLATED IN cmSpecDist() AND THE
  352. // CURRENT STATE OF THE PARAMETERS SHOULD NOT BE
  353. // LOST - IF THE CHANGES WERE ISOLATED WITHIN PVANL
  354. // AND PVSYN IT MIGHT BE POSSIBLE TO DO WITH
  355. // MINIMAL AUDIO INTERUPTION.
  356. p->sdp->ceiling = cmDspDouble(inst,kCeilKr2Id);
  357. p->sdp->expo = cmDspDouble(inst,kExpoKr2Id);
  358. p->sdp->thresh = cmDspDouble(inst,kThreshKr2Id);
  359. p->sdp->uprSlope = cmDspDouble(inst,kUprSlopeKr2Id);
  360. p->sdp->lwrSlope = cmDspDouble(inst,kLwrSlopeKr2Id);
  361. p->sdp->mix = cmDspDouble(inst,kMixKr2Id);
  362. printf("wsn:%i hsn:%i\n",p->sdp->wndSmpCnt,p->sdp->hopSmpCnt);
  363. break;
  364. case kCeilKr2Id:
  365. p->sdp->ceiling = cmDspDouble(inst,kCeilKr2Id);
  366. break;
  367. case kExpoKr2Id:
  368. p->sdp->expo = cmDspDouble(inst,kExpoKr2Id);
  369. break;
  370. case kThreshKr2Id:
  371. p->sdp->thresh = cmDspDouble(inst,kThreshKr2Id);
  372. break;
  373. case kUprSlopeKr2Id:
  374. p->sdp->uprSlope = cmDspDouble(inst,kUprSlopeKr2Id);
  375. break;
  376. case kLwrSlopeKr2Id:
  377. p->sdp->lwrSlope = cmDspDouble(inst,kLwrSlopeKr2Id);
  378. break;
  379. case kMixKr2Id:
  380. p->sdp->mix = cmDspDouble(inst,kMixKr2Id);
  381. break;
  382. case kWetKr2Id:
  383. break;
  384. default:
  385. { assert(0); }
  386. }
  387. cmSpecDist2Report(p->sdp);
  388. return rc;
  389. }
  390. cmDspClass_t* cmKr2ClassCons( cmDspCtx_t* ctx )
  391. {
  392. cmDspClassSetup(&_cmKr2DC,ctx,"Kr2",
  393. NULL,
  394. _cmDspKr2Alloc,
  395. _cmDspKr2Free,
  396. _cmDspKr2Reset,
  397. _cmDspKr2Exec,
  398. _cmDspKr2Recv,
  399. NULL,NULL,
  400. "Fourier based non-linear transformer two.");
  401. return &_cmKr2DC;
  402. }
  403. //------------------------------------------------------------------------------------------------------------
  404. //)
  405. //( { label:cmDspTimeLine file_desc:"Time line user interface unit." kw:[sunit] }
  406. enum
  407. {
  408. kTlFileTlId,
  409. kPrefixPathTlId,
  410. kSelTlId,
  411. kMeasTlId,
  412. kCursTlId,
  413. kResetTlId,
  414. kAudFnTlId,
  415. kAudLblTlId,
  416. kMidiFnTlId,
  417. kMidiLblTlId,
  418. kBegAudSmpIdxTlId,
  419. kEndAudSmpIdxTlId,
  420. kBegMidiSmpIdxTlId,
  421. kEndMidiSmpIdxTlId,
  422. };
  423. cmDspClass_t _cmTimeLineDC;
  424. typedef struct
  425. {
  426. cmDspInst_t inst;
  427. cmTlH_t tlH;
  428. unsigned afIdx;
  429. } cmDspTimeLine_t;
  430. cmDspInst_t* _cmDspTimeLineAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  431. {
  432. cmDspVarArg_t args[] =
  433. {
  434. { "tlfile", kTlFileTlId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Time line file." },
  435. { "path", kPrefixPathTlId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Time line data file prefix path" },
  436. { "sel", kSelTlId, 0, 0, kInDsvFl | kOutDsvFl | kUIntDsvFl, "Selected marker id."},
  437. { "meas", kMeasTlId, 0, 0, kInDsvFl | kUIntDsvFl, "Select a bar marker and generate a 'sel' output."},
  438. { "curs", kCursTlId, 0, 0, kInDsvFl | kUIntDsvFl, "Current audio file index."},
  439. { "reset", kResetTlId, 0, 0, kInDsvFl | kSymDsvFl, "Resend all outputs." },
  440. { "afn", kAudFnTlId, 0, 0, kOutDsvFl | kStrzDsvFl, "Selected Audio file." },
  441. { "albl", kAudLblTlId, 0, 0, kOutDsvFl | kStrzDsvFl, "Select Audio file time line label."},
  442. { "mfn", kMidiFnTlId, 0, 0, kOutDsvFl | kStrzDsvFl, "Selected MIDI file." },
  443. { "mlbl", kMidiLblTlId, 0, 0, kOutDsvFl | kStrzDsvFl, "Select MIDI file time line label."},
  444. { "absi", kBegAudSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "Begin audio sample index."},
  445. { "aesi", kEndAudSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "End audio sample index."},
  446. { "mbsi", kBegMidiSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "Begin MIDI sample index."},
  447. { "mesi", kEndMidiSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "End MIDI sample index."},
  448. { NULL, 0, 0, 0, 0 }
  449. };
  450. cmDspTimeLine_t* p = cmDspInstAlloc(cmDspTimeLine_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  451. cmDspSetDefaultUInt( ctx, &p->inst, kSelTlId, 0, cmInvalidId);
  452. cmDspSetDefaultUInt( ctx, &p->inst, kCursTlId, 0, 0);
  453. cmDspSetDefaultStrcz(ctx, &p->inst, kAudFnTlId, NULL, "");
  454. cmDspSetDefaultStrcz(ctx, &p->inst, kAudLblTlId, NULL, "");
  455. cmDspSetDefaultStrcz(ctx, &p->inst, kMidiFnTlId, NULL, "");
  456. cmDspSetDefaultStrcz(ctx, &p->inst, kMidiLblTlId, NULL, "");
  457. cmDspSetDefaultInt( ctx, &p->inst, kBegAudSmpIdxTlId, 0, cmInvalidIdx);
  458. cmDspSetDefaultInt( ctx, &p->inst, kEndAudSmpIdxTlId, 0, cmInvalidIdx);
  459. cmDspSetDefaultInt( ctx, &p->inst, kBegMidiSmpIdxTlId, 0, cmInvalidIdx);
  460. cmDspSetDefaultInt( ctx, &p->inst, kEndMidiSmpIdxTlId, 0, cmInvalidIdx);
  461. // create the UI control
  462. cmDspUiTimeLineCreate(ctx,&p->inst,kTlFileTlId,kPrefixPathTlId,kSelTlId,kMeasTlId,kCursTlId);
  463. p->tlH = cmTimeLineNullHandle;
  464. return &p->inst;
  465. }
  466. cmDspRC_t _cmDspTimeLineFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  467. {
  468. cmDspRC_t rc = kOkDspRC;
  469. cmDspTimeLine_t* p = (cmDspTimeLine_t*)inst;
  470. if( cmTimeLineFinalize(&p->tlH) != kOkTlRC )
  471. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Time-line finalize failed.");
  472. return rc;
  473. }
  474. cmDspRC_t _cmDspTimeLineReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  475. {
  476. cmDspRC_t rc = kOkDspRC;
  477. cmDspTimeLine_t* p = (cmDspTimeLine_t*)inst;
  478. cmDspApplyAllDefaults(ctx,inst);
  479. const cmChar_t* tlFn;
  480. const cmChar_t* tlPrePath = cmDspStrcz(inst,kPrefixPathTlId);
  481. if((tlFn = cmDspStrcz(inst, kTlFileTlId )) != NULL )
  482. if( cmTimeLineInitializeFromFile(ctx->cmCtx, &p->tlH, NULL, NULL, tlFn, tlPrePath ) != kOkTlRC )
  483. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "Time-line file open failed.");
  484. return rc;
  485. }
  486. cmDspRC_t _cmDspTimeLineRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  487. {
  488. cmDspTimeLine_t* p = (cmDspTimeLine_t*)inst;
  489. switch( evt->dstVarId )
  490. {
  491. case kPrefixPathTlId:
  492. cmDspSetEvent(ctx,inst,evt);
  493. break;
  494. case kCursTlId:
  495. cmDspSetEvent(ctx,inst,evt);
  496. break;
  497. case kResetTlId:
  498. case kSelTlId:
  499. {
  500. unsigned markerId;
  501. cmDspSetEvent(ctx,inst,evt);
  502. // get the id of the selected marker
  503. if((markerId = cmDspUInt(inst,kSelTlId)) != cmInvalidId )
  504. {
  505. // get the marker object
  506. cmTlObj_t* op;
  507. if((op = cmTimeLineIdToObj(p->tlH, cmInvalidId, markerId )) != NULL )
  508. {
  509. assert(op->typeId == kMarkerTlId || op->typeId == kMidiEvtTlId );
  510. p->afIdx = op->begSmpIdx;
  511. cmDspSetInt(ctx, inst, kBegAudSmpIdxTlId, op->begSmpIdx );
  512. cmDspSetInt(ctx, inst, kEndAudSmpIdxTlId, op->begSmpIdx + op->durSmpCnt );
  513. // locate the audio file assoc'd with the marker
  514. cmTlAudioFile_t* afp;
  515. if((afp = cmTimeLineAudioFileAtTime(p->tlH,op->seqId,op->seqSmpIdx)) != NULL)
  516. {
  517. cmDspSetStrcz(ctx, inst, kAudFnTlId, afp->fn );
  518. cmDspSetStrcz(ctx, inst, kAudLblTlId, afp->obj.name );
  519. }
  520. // locate the midi file assoc'd with the marker
  521. cmTlMidiFile_t* mfp;
  522. if((mfp = cmTimeLineMidiFileAtTime(p->tlH,op->seqId,op->seqSmpIdx)) != NULL )
  523. {
  524. cmDspSetInt(ctx, inst, kBegMidiSmpIdxTlId, op->seqSmpIdx - mfp->obj.seqSmpIdx );
  525. cmDspSetInt(ctx, inst, kEndMidiSmpIdxTlId, op->seqSmpIdx + op->durSmpCnt - mfp->obj.seqSmpIdx );
  526. cmDspSetStrcz(ctx, inst, kMidiFnTlId, mfp->fn );
  527. cmDspSetStrcz(ctx, inst, kMidiLblTlId,mfp->obj.name );
  528. }
  529. }
  530. }
  531. }
  532. break;
  533. case kMeasTlId:
  534. cmDspSetEvent(ctx,inst,evt);
  535. break;
  536. default:
  537. {assert(0);}
  538. }
  539. return kOkDspRC;
  540. }
  541. cmDspClass_t* cmTimeLineClassCons( cmDspCtx_t* ctx )
  542. {
  543. cmDspClassSetup(&_cmTimeLineDC,ctx,"TimeLine",
  544. NULL,
  545. _cmDspTimeLineAlloc,
  546. _cmDspTimeLineFree,
  547. _cmDspTimeLineReset,
  548. NULL,
  549. _cmDspTimeLineRecv,
  550. NULL,NULL,
  551. "Time Line control.");
  552. return &_cmTimeLineDC;
  553. }
  554. //------------------------------------------------------------------------------------------------------------
  555. //)
  556. //( { label:cmDspScore file_desc:"Musical score user interface unit." kw:[sunit] }
  557. enum
  558. {
  559. kFnScId,
  560. kSelScId,
  561. kSendScId,
  562. kStatusScId,
  563. kD0ScId,
  564. kD1ScId,
  565. kSmpIdxScId,
  566. kLocIdxScId,
  567. kCmdScId,
  568. kEvtIdxScId,
  569. kDynScId,
  570. kValTypeScId,
  571. kValueScId,
  572. kMeasScId,
  573. };
  574. cmDspClass_t _cmScoreDC;
  575. typedef struct
  576. {
  577. cmDspInst_t inst;
  578. cmScH_t scH;
  579. cmDspCtx_t* ctx; // temporary ctx ptr used during cmScore callback in _cmDspScoreRecv()
  580. unsigned printSymId;
  581. } cmDspScore_t;
  582. cmDspInst_t* _cmDspScoreAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  583. {
  584. cmDspVarArg_t args[] =
  585. {
  586. { "fn", kFnScId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score file." },
  587. { "sel", kSelScId, 0, 0, kInDsvFl | kOutDsvFl | kUIntDsvFl, "Selected score element index input."},
  588. { "send", kSendScId, 0, 0, kInDsvFl | kTypeDsvMask, "Resend last selected score element."},
  589. { "status", kStatusScId, 0, 0, kInDsvFl | kIntDsvFl, "Performed MIDI status value output" },
  590. { "d0", kD0ScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performed MIDI msg data byte 0" },
  591. { "d1", kD1ScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performed MIDI msg data byte 1" },
  592. { "smpidx", kSmpIdxScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performed MIDi msg time tag as a sample index." },
  593. { "loc", kLocIdxScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performance score location."},
  594. { "cmd", kCmdScId, 0, 0, kInDsvFl | kSymDsvFl, "cmd: dump "},
  595. { "evtidx", kEvtIdxScId, 0, 0, kOutDsvFl | kUIntDsvFl, "Performed event index of following dynamcis level."},
  596. { "dyn", kDynScId, 0, 0, kOutDsvFl | kUIntDsvFl, "Dynamic level of previous event index."},
  597. { "type", kValTypeScId,0, 0, kOutDsvFl | kUIntDsvFl, "Output variable type."},
  598. { "value", kValueScId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Output variable value."},
  599. { "meas", kMeasScId, 0, 0, kInDsvFl | kUIntDsvFl, "Trigger this measures location to emit from 'sel'."},
  600. { NULL, 0, 0, 0, 0 }
  601. };
  602. cmDspScore_t* p = cmDspInstAlloc(cmDspScore_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  603. cmDspSetDefaultUInt( ctx, &p->inst, kSelScId, 0, cmInvalidId);
  604. p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"dump");
  605. // create the UI control
  606. cmDspUiScoreCreate(ctx,&p->inst,kFnScId,kSelScId,kSmpIdxScId,kD0ScId,kD1ScId,kLocIdxScId,kEvtIdxScId,kDynScId,kValTypeScId,kValueScId,kMeasScId);
  607. p->scH = cmScNullHandle;
  608. return &p->inst;
  609. }
  610. cmDspRC_t _cmDspScoreFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  611. {
  612. cmDspRC_t rc = kOkDspRC;
  613. cmDspScore_t* p = (cmDspScore_t*)inst;
  614. if( cmScoreFinalize(&p->scH) != kOkTlRC )
  615. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Score finalize failed.");
  616. return rc;
  617. }
  618. // Callback from cmScore triggered from _cmDspScoreRecv() during call to cmScoreSetPerfEvent().
  619. void _cmDspScoreCb( void* arg, const void* data, unsigned byteCnt )
  620. {
  621. cmDspInst_t* inst = (cmDspInst_t*)arg;
  622. cmDspScore_t* p = (cmDspScore_t*)inst;
  623. cmScMsg_t m;
  624. if( cmScoreDecode(data,byteCnt,&m) == kOkScRC )
  625. {
  626. switch( m.typeId )
  627. {
  628. case kDynMsgScId:
  629. cmDspSetUInt( p->ctx,inst, kEvtIdxScId, m.u.dyn.evtIdx );
  630. cmDspSetUInt( p->ctx,inst, kDynScId, m.u.dyn.dynLvl );
  631. break;
  632. case kVarMsgScId:
  633. cmDspSetUInt( p->ctx,inst, kValTypeScId, m.u.meas.varId);
  634. cmDspSetDouble(p->ctx,inst, kValueScId, m.u.meas.value);
  635. break;
  636. default:
  637. { assert(0); }
  638. }
  639. }
  640. }
  641. cmDspRC_t _cmDspScoreReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  642. {
  643. cmDspRC_t rc = kOkDspRC;
  644. cmDspScore_t* p = (cmDspScore_t*)inst;
  645. const cmChar_t* tlFn = NULL;
  646. unsigned* dynRefArray = NULL;
  647. unsigned dynRefCnt = 0;
  648. cmDspApplyAllDefaults(ctx,inst);
  649. if( cmDspRsrcUIntArray(ctx->dspH, &dynRefCnt, &dynRefArray, "dynRef", NULL ) != kOkDspRC )
  650. {
  651. rc = cmErrMsg(&inst->classPtr->err, kRsrcNotFoundDspRC, "The dynamics reference array resource was not found.");
  652. goto errLabel;
  653. }
  654. if((tlFn = cmDspStrcz(inst, kFnScId )) != NULL )
  655. {
  656. if( cmScoreInitialize(ctx->cmCtx, &p->scH, tlFn, cmDspSampleRate(ctx), dynRefArray, dynRefCnt, _cmDspScoreCb, p, ctx->stH ) != kOkTlRC )
  657. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "Score file open failed.");
  658. //else
  659. // cmScorePrintLoc(p->scH);
  660. }
  661. errLabel:
  662. return rc;
  663. }
  664. cmDspRC_t _cmDspScoreRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  665. {
  666. cmDspScore_t* p = (cmDspScore_t*)inst;
  667. if( evt->dstVarId == kSendScId )
  668. {
  669. unsigned selIdx;
  670. if((selIdx = cmDspUInt(inst,kSelScId)) != cmInvalidIdx )
  671. {
  672. cmDspSetUInt(ctx,inst,kSelScId, selIdx );
  673. cmScoreClearPerfInfo(p->scH);
  674. }
  675. return kOkDspRC;
  676. }
  677. cmDspSetEvent(ctx,inst,evt);
  678. switch( evt->dstVarId )
  679. {
  680. case kSelScId:
  681. cmScoreClearPerfInfo(p->scH);
  682. break;
  683. case kStatusScId:
  684. //printf("st:%x\n",cmDspUInt(inst,kStatusScId));
  685. break;
  686. case kLocIdxScId:
  687. {
  688. assert( cmDspUInt(inst,kStatusScId ) == kNoteOnMdId );
  689. p->ctx = ctx; // setup p->ctx for use in _cmDspScoreCb()
  690. // this call may result in callbacks to _cmDspScoreCb()
  691. cmScoreExecPerfEvent(p->scH, cmDspUInt(inst,kLocIdxScId), cmDspUInt(inst,kSmpIdxScId), cmDspUInt(inst,kD0ScId), cmDspUInt(inst,kD1ScId) );
  692. }
  693. break;
  694. case kCmdScId:
  695. if( cmDspSymbol(inst,kCmdScId) == p->printSymId )
  696. cmScorePrintLoc(p->scH);
  697. break;
  698. case kMeasScId:
  699. break;
  700. }
  701. return kOkDspRC;
  702. }
  703. cmDspClass_t* cmScoreClassCons( cmDspCtx_t* ctx )
  704. {
  705. cmDspClassSetup(&_cmScoreDC,ctx,"Score",
  706. NULL,
  707. _cmDspScoreAlloc,
  708. _cmDspScoreFree,
  709. _cmDspScoreReset,
  710. NULL,
  711. _cmDspScoreRecv,
  712. NULL,NULL,
  713. "Score control.");
  714. return &_cmScoreDC;
  715. }
  716. //------------------------------------------------------------------------------------------------------------
  717. //)
  718. //( { label:cmDspMidiFilePlay file_desc:"MIDI file player." kw:[sunit] }
  719. enum
  720. {
  721. kFnMfId,
  722. kSelMfId,
  723. kBsiMfId,
  724. kEsiMfId,
  725. kStatusMfId,
  726. kD0MfId,
  727. kD1MfId,
  728. kSmpIdxMfId,
  729. kIdMfId
  730. };
  731. cmDspClass_t _cmMidiFilePlayDC;
  732. typedef struct
  733. {
  734. cmDspInst_t inst;
  735. cmMidiFileH_t mfH;
  736. unsigned curMsgIdx; // current midi file msg index
  737. int csi; // current sample index
  738. int bsi; // starting sample index
  739. int esi; // ending sample index
  740. unsigned startSymId;
  741. unsigned stopSymId;
  742. unsigned contSymId;
  743. bool errFl;
  744. } cmDspMidiFilePlay_t;
  745. // 'bsi' and 'esi' give the starting and ending sample for MIDI file playback.
  746. // These indexes are relative to the start of the file.
  747. // When the player recieves a 'start' msg it sets the current sample index
  748. // 'si' to 'bsi' and begins scanning for the next note to play.
  749. // On each call to the _cmDspMidiFilePlayExec() msgs that fall in the interval
  750. // si:si+sPc-1 will be transmitted. (where sPc are the number of samples per DSP cycle).
  751. cmDspInst_t* _cmDspMidiFilePlayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  752. {
  753. cmDspVarArg_t args[] =
  754. {
  755. { "fn", kFnMfId, 0, 0, kInDsvFl | kStrzDsvFl, "File name"},
  756. { "sel", kSelMfId, 0, 0, kInDsvFl | kSymDsvFl, "start | stop | continue" },
  757. { "bsi", kBsiMfId, 0, 0, kInDsvFl | kIntDsvFl, "Starting sample." },
  758. { "esi", kEsiMfId, 0, 0, kInDsvFl | kIntDsvFl, "Ending sample."},
  759. { "status", kStatusMfId, 0, 0, kOutDsvFl | kIntDsvFl, "Status value output" },
  760. { "d0", kD0MfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 0" },
  761. { "d1", kD1MfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 1" },
  762. { "smpidx", kSmpIdxMfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Msg time tag as a sample index." },
  763. { "id", kIdMfId, 0, 0, kOutDsvFl | kUIntDsvFl, "MIDI file msg unique id."},
  764. { NULL, 0, 0, 0, 0 }
  765. };
  766. cmDspMidiFilePlay_t* p = cmDspInstAlloc(cmDspMidiFilePlay_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  767. p->startSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"start");
  768. p->stopSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"stop");
  769. p->contSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"continue");
  770. p->mfH = cmMidiFileNullHandle;
  771. cmDspSetDefaultStrcz( ctx, &p->inst, kFnMfId, NULL, "");
  772. cmDspSetDefaultSymbol(ctx, &p->inst, kSelMfId, p->stopSymId);
  773. cmDspSetDefaultInt( ctx, &p->inst, kBsiMfId, 0, 0);
  774. cmDspSetDefaultInt( ctx, &p->inst, kEsiMfId, 0, 0);
  775. cmDspSetDefaultUInt( ctx, &p->inst, kStatusMfId, 0, 0);
  776. cmDspSetDefaultUInt( ctx, &p->inst, kD0MfId, 0, 0);
  777. cmDspSetDefaultUInt( ctx, &p->inst, kD1MfId, 0, 0);
  778. return &p->inst;
  779. }
  780. cmDspRC_t _cmDspMidiFilePlayFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  781. {
  782. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  783. if( cmMidiFileClose(&p->mfH) )
  784. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "MIDI file close failed.");
  785. return kOkDspRC;
  786. }
  787. // return the index of the msg following smpIdx
  788. unsigned _cmDspMidiFilePlaySeekMsgIdx( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned smpIdx )
  789. {
  790. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  791. if( cmMidiFileIsValid(p->mfH) == false )
  792. {
  793. cmErrMsg(&inst->classPtr->err, kInvalidStateDspRC,"The MIDI file player has not been given a valid MIDI file.");
  794. return cmInvalidIdx;
  795. }
  796. unsigned i;
  797. unsigned n = cmMidiFileMsgCount(p->mfH);
  798. const cmMidiTrackMsg_t** a = cmMidiFileMsgArray(p->mfH);
  799. for(i=0; i<n; ++i)
  800. if( (a[i]->amicro * cmDspSampleRate(ctx) / 1000000.0) >= smpIdx )
  801. break;
  802. return i==n ? cmInvalidIdx : i;
  803. }
  804. cmDspRC_t _cmDspMidiFilePlayOpen(cmDspCtx_t* ctx, cmDspInst_t* inst )
  805. {
  806. cmDspRC_t rc = kOkDspRC;
  807. const cmChar_t* fn = cmDspStrcz(inst,kFnMfId);
  808. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  809. p->errFl = false;
  810. if( fn==NULL || strlen(fn)==0 )
  811. return rc;
  812. if( cmMidiFileOpen( ctx->cmCtx, &p->mfH, fn ) != kOkFileRC )
  813. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "MIDI file open failed.");
  814. else
  815. {
  816. p->curMsgIdx = 0;
  817. p->bsi = cmDspInt(inst,kBsiMfId);
  818. p->esi = cmDspInt(inst,kEsiMfId);
  819. p->csi = 0;
  820. // force the first msg to occurr one quarter note into the file
  821. cmMidiFileSetDelay(p->mfH, cmMidiFileTicksPerQN(p->mfH) );
  822. // convert midi msg times to absolute time in samples
  823. //cmMidiFileTickToSamples(p->mfH,cmDspSampleRate(ctx),true);
  824. }
  825. return rc;
  826. }
  827. cmDspRC_t _cmDspMidiFilePlayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  828. {
  829. cmDspApplyAllDefaults(ctx,inst);
  830. return _cmDspMidiFilePlayOpen(ctx,inst);
  831. }
  832. cmDspRC_t _cmDspMidiFilePlayExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  833. {
  834. cmDspRC_t rc = kOkDspRC;
  835. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  836. unsigned sPc = cmDspSamplesPerCycle(ctx);
  837. if( cmDspSymbol(inst,kSelMfId) != p->stopSymId )
  838. {
  839. if( cmMidiFileIsValid(p->mfH) == false )
  840. {
  841. if( p->errFl==false )
  842. {
  843. rc = cmErrMsg(&inst->classPtr->err, kInvalidStateDspRC,"The MIDI file player has not been given a valid MIDI file.");
  844. p->errFl = true;
  845. }
  846. return rc;
  847. }
  848. const cmMidiTrackMsg_t** mpp = cmMidiFileMsgArray(p->mfH);
  849. unsigned msgN = cmMidiFileMsgCount(p->mfH);
  850. do
  851. {
  852. if( p->curMsgIdx >= msgN )
  853. break;
  854. const cmMidiTrackMsg_t* mp = mpp[p->curMsgIdx];
  855. // convert the absolute time in microseconds to samples
  856. unsigned curMsgTimeSmp = round(mp->amicro * cmDspSampleRate(ctx) / 1000000.0);
  857. // if this midi event falls inside this execution window
  858. if( p->csi > curMsgTimeSmp || curMsgTimeSmp >= (p->csi + sPc))
  859. break;
  860. switch( mp->status )
  861. {
  862. case kNoteOffMdId:
  863. case kNoteOnMdId:
  864. case kCtlMdId:
  865. cmDspSetUInt(ctx,inst, kSmpIdxMfId, curMsgTimeSmp);
  866. cmDspSetUInt(ctx,inst, kD1MfId, mp->u.chMsgPtr->d1);
  867. cmDspSetUInt(ctx,inst, kD0MfId, mp->u.chMsgPtr->d0);
  868. cmDspSetUInt(ctx,inst, kStatusMfId, mp->status);
  869. cmDspSetUInt(ctx,inst, kIdMfId, mp->uid);
  870. break;
  871. }
  872. p->curMsgIdx += 1;
  873. }while(1);
  874. }
  875. p->csi += sPc;
  876. return rc;
  877. }
  878. cmDspRC_t _cmDspMidiFilePlayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  879. {
  880. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  881. cmDspSetEvent(ctx,inst,evt);
  882. switch(evt->dstVarId)
  883. {
  884. case kFnMfId:
  885. _cmDspMidiFilePlayOpen(ctx, inst );
  886. break;
  887. case kSelMfId:
  888. {
  889. if( cmDspSymbol(inst,kSelMfId)==p->startSymId )
  890. {
  891. p->csi = cmDspInt(inst,kBsiMfId);
  892. p->curMsgIdx = _cmDspMidiFilePlaySeekMsgIdx(ctx, inst, p->csi );
  893. }
  894. break;
  895. }
  896. }
  897. return kOkDspRC;
  898. }
  899. cmDspClass_t* cmMidiFilePlayClassCons( cmDspCtx_t* ctx )
  900. {
  901. cmDspClassSetup(&_cmMidiFilePlayDC,ctx,"MidiFilePlay",
  902. NULL,
  903. _cmDspMidiFilePlayAlloc,
  904. _cmDspMidiFilePlayFree,
  905. _cmDspMidiFilePlayReset,
  906. _cmDspMidiFilePlayExec,
  907. _cmDspMidiFilePlayRecv,
  908. NULL,NULL,
  909. "MIDI file player.");
  910. return &_cmMidiFilePlayDC;
  911. }
  912. //------------------------------------------------------------------------------------------------------------
  913. //)
  914. //( { label:cmDspScFol file_desc:"MIDI performance score follower." kw:[sunit] }
  915. enum
  916. {
  917. kFnSfId,
  918. kBufCntSfId,
  919. kMaxWndCntSfId,
  920. kMinVelSfId,
  921. kMeasflSfId,
  922. kIndexSfId,
  923. kMuidSfId,
  924. kStatusSfId,
  925. kD0SfId,
  926. kD1SfId,
  927. kSmpIdxSfId,
  928. kCmdSfId,
  929. kOutSfId,
  930. kRecentSfId,
  931. kVlocSfId,
  932. kVtypSfId,
  933. kVvalSfId,
  934. kVcostSfId,
  935. kSymSfId
  936. };
  937. cmDspClass_t _cmScFolDC;
  938. struct cmDspScFol_str;
  939. typedef struct
  940. {
  941. cmDspCtx_t* ctx;
  942. struct cmDspScFol_str* sfp;
  943. } cmDspScFolCbArg_t;
  944. typedef struct cmDspScFol_str
  945. {
  946. cmDspInst_t inst;
  947. cmScMatcher* sfp;
  948. cmScMeas* smp;
  949. cmScH_t scH;
  950. cmDspScFolCbArg_t arg;
  951. unsigned printSymId;
  952. unsigned quietSymId;
  953. unsigned maxScLocIdx;
  954. bool liveFl;
  955. } cmDspScFol_t;
  956. cmDspInst_t* _cmDspScFolAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  957. {
  958. cmDspVarArg_t args[] =
  959. {
  960. { "fn", kFnSfId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score file." },
  961. { "bufcnt",kBufCntSfId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Event buffer element count." },
  962. { "wndcnt",kMaxWndCntSfId,0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Maximum window length."},
  963. { "minvel",kMinVelSfId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Minimum velocity."},
  964. { "measfl",kMeasflSfId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Enable measurements"},
  965. { "index", kIndexSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Tracking start location."},
  966. { "muid", kMuidSfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI msg file unique id"},
  967. { "status",kStatusSfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI status byte"},
  968. { "d0", kD0SfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI data byte 0"},
  969. { "d1", kD1SfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI data byte 1"},
  970. { "smpidx",kSmpIdxSfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI time tag as a sample index"},
  971. { "cmd", kCmdSfId, 0, 0, kInDsvFl | kSymDsvFl, "Command input: print | quiet"},
  972. { "out", kOutSfId, 0, 0, kOutDsvFl| kUIntDsvFl, "Maximum score location index."},
  973. { "recent",kRecentSfId, 0, 0, kOutDsvFl| kUIntDsvFl, "Most recent score location index."},
  974. { "vloc", kVlocSfId, 0, 0, kOutDsvFl| kUIntDsvFl, "Score location at which the variable value becomes active."},
  975. { "vtyp", kVtypSfId, 0, 0, kOutDsvFl| kUIntDsvFl, "Variable type: 0=even=kEvenVarScId 1=dyn=kDynVarScId 2=tempo=kTempoVarScId."},
  976. { "vval", kVvalSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Variable value."},
  977. { "vcost", kVcostSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Variable match cost value."},
  978. { "sym", kSymSfId, 0, 0, kOutDsvFl| kSymDsvFl, "Symbol associated with a global variable which has changed value."},
  979. { NULL, 0, 0, 0, 0, NULL }
  980. };
  981. cmDspScFol_t* p;
  982. if((p = cmDspInstAlloc(cmDspScFol_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl)) == NULL )
  983. return NULL;
  984. p->sfp = cmScMatcherAlloc(ctx->cmProcCtx, NULL, 0, cmScNullHandle, 0, 0, NULL, NULL );
  985. p->smp = cmScMeasAlloc( ctx->cmProcCtx, NULL, cmScNullHandle, 0, NULL, 0 );
  986. p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"print");
  987. p->quietSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"quiet");
  988. p->maxScLocIdx= cmInvalidIdx;
  989. cmDspSetDefaultUInt( ctx, &p->inst, kBufCntSfId, 0, 7);
  990. cmDspSetDefaultUInt( ctx, &p->inst, kMaxWndCntSfId, 0, 10);
  991. cmDspSetDefaultUInt( ctx, &p->inst, kMinVelSfId, 0, 5);
  992. cmDspSetDefaultBool( ctx, &p->inst, kMeasflSfId, 0, 0);
  993. cmDspSetDefaultUInt( ctx, &p->inst, kIndexSfId, 0, 0);
  994. cmDspSetDefaultUInt( ctx, &p->inst, kOutSfId, 0, 0);
  995. cmDspSetDefaultUInt( ctx, &p->inst, kRecentSfId, 0, 0);
  996. cmDspSetDefaultSymbol(ctx,&p->inst, kCmdSfId, p->quietSymId );
  997. return &p->inst;
  998. }
  999. cmDspRC_t _cmDspScFolFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1000. {
  1001. cmDspScFol_t* p = (cmDspScFol_t*)inst;
  1002. cmScMatcherFree(&p->sfp);
  1003. cmScMeasFree(&p->smp);
  1004. cmScoreFinalize(&p->scH);
  1005. return kOkDspRC;
  1006. }
  1007. // This is a callback function from cmScMatcherExec() which is called when
  1008. // this cmDspFol object receives a new score location index.
  1009. void _cmScFolMatcherCb( cmScMatcher* p, void* arg, cmScMatcherResult_t* rp )
  1010. {
  1011. cmDspScFolCbArg_t* ap = (cmDspScFolCbArg_t*)arg;
  1012. if( cmScMeasExec(ap->sfp->smp, rp->mni, rp->locIdx, rp->scEvtIdx, rp->flags, rp->smpIdx, rp->pitch, rp->vel ) == cmOkRC )
  1013. {
  1014. cmDspInst_t* inst = &(ap->sfp->inst);
  1015. // send 'set' values that were calculated on the previous call to cmScMeasExec()
  1016. unsigned i;
  1017. for(i=ap->sfp->smp->vsi; i<ap->sfp->smp->nsi; ++i)
  1018. if(ap->sfp->smp->set[i].value != DBL_MAX )
  1019. {
  1020. // switch( ap->sfp->smp->set[i].sp->varId )
  1021. // {
  1022. // case kEvenVarScId:
  1023. // cmDspSetDouble(ap->ctx,inst,kEvenSfId,ap->sfp->smp->set[i].value);
  1024. // break;
  1025. //
  1026. // case kDynVarScId:
  1027. // cmDspSetDouble(ap->ctx,inst,kDynSfId,ap->sfp->smp->set[i].value);
  1028. // break;
  1029. //
  1030. // case kTempoVarScId:
  1031. // cmDspSetDouble(ap->ctx,inst,kTempoSfId,ap->sfp->smp->set[i].value);
  1032. // break;
  1033. //
  1034. // default:
  1035. // { assert(0); }
  1036. // }
  1037. //
  1038. // cmDspSetDouble(ap->ctx,inst,kCostSfId,ap->sfp->smp->set[i].match_cost);
  1039. // Set the values in the global variable storage
  1040. cmDspValue_t vv,cv;
  1041. unsigned j;
  1042. cmDsvSetDouble(&vv,ap->sfp->smp->set[i].value);
  1043. cmDsvSetDouble(&cv,ap->sfp->smp->set[i].match_cost);
  1044. for(j=0; j<ap->sfp->smp->set[i].sp->sectCnt; ++j)
  1045. {
  1046. cmDspStoreSetValueViaSym(ap->ctx->dsH, ap->sfp->smp->set[i].sp->symArray[j], &vv );
  1047. cmDspStoreSetValueViaSym(ap->ctx->dsH, ap->sfp->smp->set[i].sp->costSymArray[j], &cv );
  1048. cmDspSetSymbol(ap->ctx,inst,kSymSfId,ap->sfp->smp->set[i].sp->symArray[j]);
  1049. cmDspSetSymbol(ap->ctx,inst,kSymSfId,ap->sfp->smp->set[i].sp->costSymArray[j]);
  1050. if( cmDspBool(inst,kMeasflSfId) )
  1051. {
  1052. cmDspSetUInt( ap->ctx, inst, kVlocSfId, ap->sfp->smp->set[i].sp->sectArray[j]->locPtr->index);
  1053. cmDspSetDouble( ap->ctx, inst, kVvalSfId, ap->sfp->smp->set[i].value);
  1054. cmDspSetDouble( ap->ctx, inst, kVcostSfId, ap->sfp->smp->set[i].match_cost);
  1055. cmDspSetUInt( ap->ctx, inst, kVtypSfId, ap->sfp->smp->set[i].sp->varId);
  1056. }
  1057. }
  1058. }
  1059. // // trigger 'section' starts
  1060. // for(i=ap->sfp->smp->vsli; i<ap->sfp->smp->nsli; ++i)
  1061. // {
  1062. // const cmScoreLoc_t* locPtr = cmScoreLoc(ap->sfp->smp->mp->scH,i);
  1063. // if( locPtr->begSectPtr != NULL )
  1064. // cmDspSetUInt(ap->ctx,inst,kSectIndexSfId,locPtr->begSectPtr->index);
  1065. // }
  1066. }
  1067. }
  1068. cmDspRC_t _cmDspScFolOpenScore( cmDspCtx_t* ctx, cmDspInst_t* inst )
  1069. {
  1070. cmDspRC_t rc = kOkDspRC;
  1071. cmDspScFol_t* p = (cmDspScFol_t*)inst;
  1072. const cmChar_t* fn;
  1073. if((fn = cmDspStrcz(inst,kFnSfId)) == NULL || strlen(fn)==0 )
  1074. return cmErrMsg(&inst->classPtr->err, kInvalidArgDspRC, "No score file name supplied.");
  1075. if( cmScoreInitialize(ctx->cmCtx, &p->scH, fn, cmDspSampleRate(ctx), NULL, 0, NULL, NULL, ctx->stH ) != kOkScRC )
  1076. return cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to open the score '%s'.",fn);
  1077. if( cmScoreIsValid(p->scH) )
  1078. {
  1079. unsigned* dynRefArray = NULL;
  1080. unsigned dynRefCnt = 0;
  1081. // initialize the cmScMatcher
  1082. if( cmScMatcherInit(p->sfp, cmDspSampleRate(ctx), p->scH, cmDspUInt(inst,kMaxWndCntSfId), cmDspUInt(inst,kBufCntSfId), _cmScFolMatcherCb, p->smp ) != cmOkRC )
  1083. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Internal score follower allocation failed.");
  1084. // read the dynamics reference array
  1085. if( cmDspRsrcUIntArray(ctx->dspH, &dynRefCnt, &dynRefArray, "dynRef", NULL ) != kOkDspRC )
  1086. {
  1087. rc = cmErrMsg(&inst->classPtr->err, kRsrcNotFoundDspRC, "The dynamics reference array resource was not found.");
  1088. goto errLabel;
  1089. }
  1090. // initialize the cmScMeas object.
  1091. if( cmScMeasInit(p->smp, p->scH, cmDspSampleRate(ctx), dynRefArray, dynRefCnt ) != cmOkRC )
  1092. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Internal scMeas object initialization failed.");
  1093. }
  1094. errLabel:
  1095. return rc;
  1096. }
  1097. cmDspRC_t _cmDspScFolReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1098. {
  1099. cmDspRC_t rc;
  1100. if((rc = cmDspApplyAllDefaults(ctx,inst)) != kOkDspRC )
  1101. return rc;
  1102. return _cmDspScFolOpenScore(ctx,inst);
  1103. }
  1104. cmDspRC_t _cmDspScFolRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1105. {
  1106. cmDspRC_t rc = kOkDspRC;
  1107. cmDspScFol_t* p = (cmDspScFol_t*)inst;
  1108. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC && p->sfp != NULL )
  1109. {
  1110. switch( evt->dstVarId )
  1111. {
  1112. case kIndexSfId:
  1113. if( cmScoreIsValid(p->scH) )
  1114. {
  1115. p->maxScLocIdx = cmInvalidIdx;
  1116. if( cmScMeasReset( p->smp ) != cmOkRC )
  1117. cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Score measure unit reset to score index '%i' failed.");
  1118. if( cmScMatcherReset( p->sfp, cmDspUInt(inst,kIndexSfId) ) != cmOkRC )
  1119. cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Score follower reset to score index '%i' failed.");
  1120. }
  1121. break;
  1122. case kStatusSfId:
  1123. if( cmScoreIsValid(p->scH))
  1124. {
  1125. unsigned scLocIdx = cmInvalidIdx;
  1126. // setup the cmScMeas() callback arg.
  1127. p->arg.ctx = ctx;
  1128. p->arg.sfp = p;
  1129. p->sfp->cbArg = &p->arg;
  1130. // this call may result in a callback to _cmScFolMatcherCb()
  1131. if( cmScMatcherExec(p->sfp, cmDspUInt(inst,kSmpIdxSfId), cmDspUInt(inst,kMuidSfId), cmDspUInt(inst,kStatusSfId), cmDspUInt(inst,kD0SfId), cmDspUInt(inst,kD1SfId), &scLocIdx) == cmOkRC )
  1132. if( scLocIdx != cmInvalidIdx )
  1133. {
  1134. // It is possible that the internal score follower may go backwards.
  1135. // In this case it will report a given score location multiple times or out of time order.
  1136. // The 'out' port will only be updated under the circumstances that no later
  1137. // score location has been seen - so the last output from 'out' always reports
  1138. // the furthest possible progress in the score. THe 'recent' output simply reports
  1139. // the most recent output from the internal score follower which may include
  1140. // previously reported or out of order score locations.
  1141. cmDspSetUInt(ctx,inst,kRecentSfId,scLocIdx);
  1142. if( p->maxScLocIdx==cmInvalidIdx || p->maxScLocIdx < scLocIdx )
  1143. {
  1144. p->maxScLocIdx = scLocIdx;
  1145. cmDspSetUInt(ctx,inst,kOutSfId,scLocIdx);
  1146. }
  1147. }
  1148. }
  1149. break;
  1150. case kFnSfId:
  1151. _cmDspScFolOpenScore(ctx,inst);
  1152. break;
  1153. case kCmdSfId:
  1154. if( cmDspSymbol(inst,kCmdSfId) == p->printSymId )
  1155. p->sfp->printFl = true;
  1156. else
  1157. if( cmDspSymbol(inst,kCmdSfId) == p->quietSymId )
  1158. p->sfp->printFl = false;
  1159. break;
  1160. }
  1161. }
  1162. return rc;
  1163. }
  1164. cmDspClass_t* cmScFolClassCons( cmDspCtx_t* ctx )
  1165. {
  1166. cmDspClassSetup(&_cmScFolDC,ctx,"ScFol",
  1167. NULL,
  1168. _cmDspScFolAlloc,
  1169. _cmDspScFolFree,
  1170. _cmDspScFolReset,
  1171. NULL,
  1172. _cmDspScFolRecv,
  1173. NULL,NULL,
  1174. "Score Follower");
  1175. return &_cmScFolDC;
  1176. }
  1177. //------------------------------------------------------------------------------------------------------------
  1178. //)
  1179. //( { label:cmDspScMod file_desc:"Score driven parameter automation." kw:[sunit] }
  1180. enum
  1181. {
  1182. kScLocIdxMdId,
  1183. kResetIdxMdId,
  1184. kCmdMdId,
  1185. kSelMdId,
  1186. kPostMdId
  1187. };
  1188. cmDspClass_t _cmModulatorDC;
  1189. typedef struct
  1190. {
  1191. cmDspInst_t inst;
  1192. cmScModulator* mp;
  1193. cmDspCtx_t* tmp_ctx; // used to temporarily hold the current cmDspCtx during callback
  1194. cmChar_t* fn;
  1195. cmChar_t* modLabel;
  1196. unsigned onSymId;
  1197. unsigned offSymId;
  1198. unsigned postSymId;
  1199. unsigned dumpSymId;
  1200. unsigned minInVarId;
  1201. unsigned maxInVarId;
  1202. unsigned* inVarIdMap;
  1203. unsigned inVarIdOffs;
  1204. } cmDspScMod_t;
  1205. void _cmDspScModCb( void* arg, unsigned varSymId, double value, bool postFl )
  1206. {
  1207. cmDspScMod_t* p = (cmDspScMod_t*)arg;
  1208. cmDspVar_t* varPtr;
  1209. if((varPtr = cmDspVarSymbolToPtr( p->tmp_ctx, &p->inst, varSymId, 0 )) == NULL )
  1210. return;
  1211. cmDspSetDouble(p->tmp_ctx,&p->inst,varPtr->constId,value);
  1212. if( postFl )
  1213. cmDspSetSymbol(p->tmp_ctx,&p->inst,kPostMdId,p->postSymId);
  1214. }
  1215. cmDspInst_t* _cmDspScModAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1216. {
  1217. va_list vl1;
  1218. va_copy(vl1,vl);
  1219. cmDspVarArg_t args[] =
  1220. {
  1221. { "index", kScLocIdxMdId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower index input."},
  1222. { "reset", kResetIdxMdId, 0,0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Reset the modulator and go to the score index."},
  1223. { "cmd", kCmdMdId, 0,0, kInDsvFl | kSymDsvFl | kOptArgDsvFl, "on | off."},
  1224. { "sel", kSelMdId, 0,0, kInDsvFl | kSymDsvFl | kOptArgDsvFl, "Set the next active entry group name."},
  1225. { "post", kPostMdId, 0,0, kOutDsvFl | kSymDsvFl, "Sends 'post' symbol after a message transmission if the 'post' flag is set in scMod."},
  1226. { NULL, 0, 0, 0, 0 }
  1227. };
  1228. // validate the argument count
  1229. if( va_cnt != 2 )
  1230. {
  1231. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The Modulator requires at least two arguments.");
  1232. return NULL;
  1233. }
  1234. // read the modulator file and label strings
  1235. const cmChar_t* fn = va_arg(vl1,const cmChar_t*);
  1236. const cmChar_t* modLabel = va_arg(vl1,const cmChar_t*);
  1237. va_end(vl1);
  1238. // validate the file
  1239. if( fn==NULL || cmFsIsFile(fn)==false )
  1240. {
  1241. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The Modulator file '%s' is not valid.",cmStringNullGuard(fn));
  1242. return NULL;
  1243. }
  1244. // allocate the internal modulator object
  1245. cmScModulator* mp = cmScModulatorAlloc(ctx->cmProcCtx, NULL, ctx->cmCtx, ctx->stH, cmDspSampleRate(ctx), cmDspSamplesPerCycle(ctx), fn, modLabel, _cmDspScModCb, NULL );
  1246. if(mp == NULL )
  1247. {
  1248. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The internal modulator object initialization failed.");
  1249. return NULL;
  1250. }
  1251. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]) - 1;
  1252. unsigned outVarCnt = cmScModulatorOutVarCount(mp);
  1253. unsigned inVarCnt = cmScModulatorInVarCount(mp);
  1254. unsigned argCnt = fixArgCnt + inVarCnt + outVarCnt;
  1255. cmDspVarArg_t a[ argCnt+1 ];
  1256. unsigned i;
  1257. unsigned* inVarIdMap = cmMemAllocZ( unsigned, inVarCnt );
  1258. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  1259. for(i=fixArgCnt; i<argCnt; ++i)
  1260. {
  1261. unsigned inVarIdx = i - fixArgCnt;
  1262. unsigned outVarIdx = inVarIdx - inVarCnt;
  1263. bool inputFl = inVarIdx < inVarCnt;
  1264. const cmScModVar_t* vp = inputFl ? cmScModulatorInVar(mp,inVarIdx) : cmScModulatorOutVar(mp,outVarIdx);
  1265. const cmChar_t* label = cmSymTblLabel( ctx->stH, vp->varSymId );
  1266. const cmChar_t* docStr = cmTsPrintfS("Variable %s for %s",inputFl?"input":"output",label);
  1267. unsigned flags = inputFl ? kInDsvFl : kOutDsvFl;
  1268. if( inputFl )
  1269. inVarIdMap[ inVarIdx ] = vp->varSymId;
  1270. cmDspArgSetup(ctx, a + i, label, cmInvalidId, i, 0, 0, flags | kDoubleDsvFl, docStr );
  1271. }
  1272. cmDspArgSetupNull(a+argCnt); // set terminating arg. flags
  1273. cmDspScMod_t* p = cmDspInstAlloc(cmDspScMod_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
  1274. p->fn = cmMemAllocStr(fn);
  1275. p->modLabel = cmMemAllocStr(modLabel);
  1276. p->mp = mp;
  1277. p->onSymId = cmSymTblId(ctx->stH,"on");
  1278. p->offSymId = cmSymTblId(ctx->stH,"off");
  1279. p->postSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"post");
  1280. p->dumpSymId = cmSymTblId(ctx->stH,"dump");
  1281. p->minInVarId = fixArgCnt;
  1282. p->maxInVarId = p->minInVarId + inVarCnt - 1;
  1283. p->inVarIdMap = inVarIdMap;
  1284. p->inVarIdOffs = fixArgCnt;
  1285. mp->cbArg = p; // set the modulator callback arg
  1286. cmDspSetDefaultUInt(ctx,&p->inst,kScLocIdxMdId,0,0);
  1287. cmDspSetDefaultSymbol(ctx,&p->inst,kCmdMdId,p->offSymId);
  1288. cmDspSetDefaultSymbol(ctx,&p->inst,kSelMdId,cmInvalidId);
  1289. return &p->inst;
  1290. }
  1291. cmDspRC_t _cmDspScModFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1292. {
  1293. cmDspRC_t rc = kOkDspRC;
  1294. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1295. if( cmScModulatorFree(&p->mp) != kOkTlRC )
  1296. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Modulator release failed.");
  1297. cmMemFree(p->fn);
  1298. cmMemFree(p->modLabel);
  1299. cmMemFree(p->inVarIdMap);
  1300. return rc;
  1301. }
  1302. cmDspRC_t _cmDspScModReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1303. {
  1304. cmDspRC_t rc = kOkDspRC;
  1305. cmDspApplyAllDefaults(ctx,inst);
  1306. return rc;
  1307. }
  1308. cmDspRC_t _cmDspScModRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1309. {
  1310. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1311. cmDspSetEvent(ctx,inst,evt);
  1312. if( p->minInVarId <= evt->dstVarId && evt->dstVarId <= p->maxInVarId )
  1313. {
  1314. double v = cmDspDouble(inst,evt->dstVarId);
  1315. //printf("%s : %i %f\n",__FUNCTION__,evt->dstVarId,v);
  1316. cmScModulatorSetValue( p->mp, p->inVarIdMap[ evt->dstVarId - p->inVarIdOffs ], v );
  1317. }
  1318. switch( evt->dstVarId )
  1319. {
  1320. case kResetIdxMdId:
  1321. cmDspSetUInt(ctx,inst,kScLocIdxMdId,cmDspUInt(inst,kResetIdxMdId));
  1322. break;
  1323. case kCmdMdId:
  1324. {
  1325. unsigned symId = cmDspSymbol(inst,kCmdMdId);
  1326. if( symId == p->onSymId )
  1327. cmScModulatorReset(p->mp, ctx->cmCtx, cmDspUInt(inst,kScLocIdxMdId), cmDspSymbol(inst,kSelMdId));
  1328. if( symId == p->dumpSymId )
  1329. cmScModulatorDump(p->mp);
  1330. }
  1331. break;
  1332. }
  1333. return kOkDspRC;
  1334. }
  1335. cmDspRC_t _cmDspScModExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1336. {
  1337. cmDspRC_t rc = kOkDspRC;
  1338. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1339. if( cmDspSymbol(inst,kCmdMdId) != p->offSymId )
  1340. {
  1341. p->tmp_ctx = ctx;
  1342. cmScModulatorExec(p->mp,cmDspUInt(inst,kScLocIdxMdId));
  1343. }
  1344. return rc;
  1345. }
  1346. cmDspClass_t* cmScModClassCons( cmDspCtx_t* ctx )
  1347. {
  1348. cmDspClassSetup(&_cmModulatorDC,ctx,"ScMod",
  1349. NULL,
  1350. _cmDspScModAlloc,
  1351. _cmDspScModFree,
  1352. _cmDspScModReset,
  1353. _cmDspScModExec,
  1354. _cmDspScModRecv,
  1355. NULL,NULL,
  1356. "Score Driven Variable Modulator.");
  1357. return &_cmModulatorDC;
  1358. }
  1359. //------------------------------------------------------------------------------------------------------------
  1360. //)
  1361. //( { label:cmDspGSwitch file_desc:"Route all inputs to one of a group of outputs." kw:[sunit] }
  1362. enum
  1363. {
  1364. kInChCntGsId,
  1365. kOutGroupCntGsId,
  1366. kGroupSelIdxGsId,
  1367. kBaseInFloatGsId
  1368. };
  1369. cmDspClass_t _cmGSwitchDC;
  1370. typedef struct
  1371. {
  1372. cmDspInst_t inst;
  1373. unsigned iChCnt;
  1374. unsigned oGroupCnt;
  1375. unsigned baseInFloatGsId;
  1376. unsigned baseInSymGsId;
  1377. unsigned baseInBoolGsId;
  1378. unsigned baseOutFloatGsId;
  1379. unsigned baseOutSymGsId;
  1380. unsigned baseOutBoolGsId;
  1381. } cmDspGSwitch_t;
  1382. cmDspInst_t* _cmDspGSwitchAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1383. {
  1384. va_list vl1;
  1385. va_copy(vl1,vl);
  1386. cmDspVarArg_t args[] =
  1387. {
  1388. { "ichs", kInChCntGsId, 0,0, kUIntDsvFl | kReqArgDsvFl, "Input channel count."},
  1389. { "ochs", kOutGroupCntGsId, 0,0, kUIntDsvFl | kReqArgDsvFl, "Output group count."},
  1390. { "sel", kGroupSelIdxGsId, 0,0, kInDsvFl | kUIntDsvFl, "Group select index."},
  1391. { NULL, 0, 0, 0, 0 }
  1392. };
  1393. // validate the argument count
  1394. if( va_cnt != 2 )
  1395. {
  1396. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The GSwitch requires at least two arguments.");
  1397. return NULL;
  1398. }
  1399. // read the input ch and output group count
  1400. unsigned iChCnt = va_arg(vl1,unsigned);
  1401. unsigned oGroupCnt = va_arg(vl1,unsigned);
  1402. va_end(vl1);
  1403. // validate the channel counts
  1404. if( iChCnt == 0 || oGroupCnt==0 )
  1405. {
  1406. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The GSwitch input channel count and group count must be greater than zero.");
  1407. return NULL;
  1408. }
  1409. unsigned typeCnt = 3; // i.e. float,sym,bool
  1410. unsigned baseInFloatGsId = kBaseInFloatGsId;
  1411. unsigned baseInSymGsId = baseInFloatGsId + iChCnt;
  1412. unsigned baseInBoolGsId = baseInSymGsId + iChCnt;
  1413. unsigned baseOutFloatGsId = baseInBoolGsId + iChCnt;
  1414. unsigned baseOutSymGsId = baseOutFloatGsId + (iChCnt * oGroupCnt);
  1415. unsigned baseOutBoolGsId = baseOutSymGsId + (iChCnt * oGroupCnt);
  1416. unsigned fixArgCnt = 3;
  1417. unsigned varArgCnt = (iChCnt * typeCnt) + (iChCnt * typeCnt * oGroupCnt);
  1418. unsigned argCnt = fixArgCnt + varArgCnt;
  1419. cmDspVarArg_t a[ argCnt+1 ];
  1420. unsigned i;
  1421. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  1422. cmDspArgSetupN( ctx, a, argCnt, baseInFloatGsId, iChCnt, "f-in", baseInFloatGsId, 0, 0, kInDsvFl | kDoubleDsvFl, "Float input");
  1423. cmDspArgSetupN( ctx, a, argCnt, baseInSymGsId, iChCnt, "s-in", baseInSymGsId, 0, 0, kInDsvFl | kSymDsvFl, "Symbol input");
  1424. cmDspArgSetupN( ctx, a, argCnt, baseInBoolGsId, iChCnt, "b-in", baseInBoolGsId, 0, 0, kInDsvFl | kBoolDsvFl, "Bool input");
  1425. unsigned labelCharCnt = 63;
  1426. cmChar_t label[labelCharCnt+1];
  1427. label[labelCharCnt] = 0;
  1428. unsigned gsid = baseOutFloatGsId;
  1429. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1430. {
  1431. snprintf(label,labelCharCnt,"f-out-%i",i);
  1432. cmDspArgSetupN( ctx, a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kDoubleDsvFl, "Float output");
  1433. }
  1434. gsid = baseOutSymGsId;
  1435. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1436. {
  1437. snprintf(label,labelCharCnt,"s-out-%i",i);
  1438. cmDspArgSetupN( ctx, a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kSymDsvFl, "Symbol output");
  1439. }
  1440. gsid = baseOutBoolGsId;
  1441. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1442. {
  1443. snprintf(label,labelCharCnt,"b-out-%i",i);
  1444. cmDspArgSetupN( ctx,a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kBoolDsvFl, "Bool output");
  1445. }
  1446. cmDspArgSetupNull(a+argCnt); // set terminating arg. flags
  1447. cmDspGSwitch_t* p = cmDspInstAlloc(cmDspGSwitch_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
  1448. p->iChCnt = iChCnt;
  1449. p->oGroupCnt = oGroupCnt;
  1450. p->baseInFloatGsId = baseInFloatGsId;
  1451. p->baseInSymGsId = baseInSymGsId;
  1452. p->baseInBoolGsId = baseInBoolGsId;
  1453. p->baseOutFloatGsId = baseOutFloatGsId;
  1454. p->baseOutSymGsId = baseOutSymGsId;
  1455. p->baseOutBoolGsId = baseOutBoolGsId;
  1456. cmDspSetDefaultUInt(ctx,&p->inst,kGroupSelIdxGsId,0,0);
  1457. return &p->inst;
  1458. }
  1459. cmDspRC_t _cmDspGSwitchReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1460. {
  1461. cmDspRC_t rc = kOkDspRC;
  1462. cmDspApplyAllDefaults(ctx,inst);
  1463. return rc;
  1464. }
  1465. cmDspRC_t _cmDspGSwitchRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1466. {
  1467. cmDspRC_t rc = kOkDspRC;
  1468. cmDspGSwitch_t* p = (cmDspGSwitch_t*)inst;
  1469. // if this is the group selector
  1470. if( evt->dstVarId == kGroupSelIdxGsId )
  1471. {
  1472. unsigned idx;
  1473. if( (idx = cmDsvGetUInt(evt->valuePtr)) > p->oGroupCnt )
  1474. cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The GSwitch group select index %i is out of range %i.",idx,p->oGroupCnt);
  1475. else
  1476. cmDspSetEvent(ctx,inst,evt);
  1477. return rc;
  1478. }
  1479. // get the group selector
  1480. unsigned groupIdx = cmDspUInt(inst,kGroupSelIdxGsId);
  1481. assert( groupIdx < p->oGroupCnt);
  1482. // if this is a float input
  1483. if( p->baseInFloatGsId <= evt->dstVarId && evt->dstVarId < p->baseInFloatGsId + p->iChCnt )
  1484. {
  1485. unsigned outVarId = p->baseOutFloatGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInFloatGsId);
  1486. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1487. return rc;
  1488. }
  1489. // if this is a symbol input
  1490. if( p->baseInSymGsId <= evt->dstVarId && evt->dstVarId < p->baseInSymGsId + p->iChCnt )
  1491. {
  1492. unsigned outVarId = p->baseOutSymGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInSymGsId);
  1493. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1494. return rc;
  1495. }
  1496. // if this is a bool input
  1497. if( p->baseInBoolGsId <= evt->dstVarId && evt->dstVarId < p->baseInBoolGsId + p->iChCnt )
  1498. {
  1499. unsigned outVarId = p->baseOutBoolGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInBoolGsId);
  1500. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1501. return rc;
  1502. }
  1503. return rc;
  1504. }
  1505. cmDspClass_t* cmGSwitchClassCons( cmDspCtx_t* ctx )
  1506. {
  1507. cmDspClassSetup(&_cmGSwitchDC,ctx,"GSwitch",
  1508. NULL,
  1509. _cmDspGSwitchAlloc,
  1510. NULL,
  1511. _cmDspGSwitchReset,
  1512. NULL,
  1513. _cmDspGSwitchRecv,
  1514. NULL,NULL,
  1515. "Ganged switch.");
  1516. return &_cmGSwitchDC;
  1517. }
  1518. //------------------------------------------------------------------------------------------------------------
  1519. //)
  1520. //( { label:cmDspScaleRange file_desc:"Offset and scale a scalar value." kw:[sunit] }
  1521. enum
  1522. {
  1523. kMinInSrId,
  1524. kMaxInSrId,
  1525. kMinOutSrId,
  1526. kMaxOutSrId,
  1527. kValInSrId,
  1528. kValOutSrId,
  1529. };
  1530. cmDspClass_t _cmScaleRangeDC;
  1531. typedef struct
  1532. {
  1533. cmDspInst_t inst;
  1534. } cmDspScaleRange_t;
  1535. cmDspInst_t* _cmDspScaleRangeAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1536. {
  1537. va_list vl1;
  1538. va_copy(vl1,vl);
  1539. cmDspVarArg_t args[] =
  1540. {
  1541. { "min_in", kMinInSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1542. { "max_in", kMaxInSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Max Input value."},
  1543. { "min_out", kMinOutSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Output value."},
  1544. { "max_out", kMaxOutSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Max Output value."},
  1545. { "val_in", kValInSrId, 0,0, kInDsvFl | kDoubleDsvFl, "Input value."},
  1546. { "val_out", kValOutSrId, 0,0, kOutDsvFl | kDoubleDsvFl, "Output value"},
  1547. { NULL, 0, 0, 0, 0 }
  1548. };
  1549. cmDspScaleRange_t* p = cmDspInstAlloc(cmDspScaleRange_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1550. cmDspSetDefaultDouble(ctx,&p->inst,kMinInSrId,0,0);
  1551. cmDspSetDefaultDouble(ctx,&p->inst,kMaxInSrId,0,1.0);
  1552. cmDspSetDefaultDouble(ctx,&p->inst,kMinOutSrId,0,0);
  1553. cmDspSetDefaultDouble(ctx,&p->inst,kMaxOutSrId,0,1.0);
  1554. return &p->inst;
  1555. }
  1556. cmDspRC_t _cmDspScaleRangeReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1557. {
  1558. cmDspRC_t rc = kOkDspRC;
  1559. cmDspApplyAllDefaults(ctx,inst);
  1560. return rc;
  1561. }
  1562. cmDspRC_t _cmDspScaleRangeRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1563. {
  1564. cmDspRC_t rc = kOkDspRC;
  1565. //cmDspScaleRange_t* p = (cmDspScaleRange_t*)inst;
  1566. cmDspSetEvent(ctx,inst,evt);
  1567. if( evt->dstVarId == kValInSrId )
  1568. {
  1569. double val = cmDspDouble(inst,kValInSrId);
  1570. double min_in = cmDspDouble(inst,kMinInSrId);
  1571. double max_in = cmDspDouble(inst,kMaxInSrId);
  1572. double min_out = cmDspDouble(inst,kMinOutSrId);
  1573. double max_out = cmDspDouble(inst,kMaxOutSrId);
  1574. double x = cmMax(min_in,cmMin(max_in,val));
  1575. x = (x - min_in)/(max_in - min_in);
  1576. x = min_out + x * (max_out - min_out);
  1577. cmDspSetDouble(ctx,inst,kValOutSrId, x );
  1578. //printf("%f (%f %f) : (%f %f) %f\n",val,min_in,max_in,min_out,max_out,x);
  1579. }
  1580. return rc;
  1581. }
  1582. cmDspClass_t* cmScaleRangeClassCons( cmDspCtx_t* ctx )
  1583. {
  1584. cmDspClassSetup(&_cmScaleRangeDC,ctx,"ScaleRange",
  1585. NULL,
  1586. _cmDspScaleRangeAlloc,
  1587. NULL,
  1588. _cmDspScaleRangeReset,
  1589. NULL,
  1590. _cmDspScaleRangeRecv,
  1591. NULL,NULL,
  1592. "Scale a value inside an input range to a value in the output range.");
  1593. return &_cmScaleRangeDC;
  1594. }
  1595. //------------------------------------------------------------------------------------------------------------
  1596. //)
  1597. //( { label:cmDspActiveMeas file_desc:"Issue stored parameter values at specified score locations." kw:[sunit] }
  1598. enum
  1599. {
  1600. kCntAmId,
  1601. kSflocAmId,
  1602. kLocAmId,
  1603. kTypeAmId,
  1604. kValueAmId,
  1605. kCstAmId,
  1606. kCmdAmId,
  1607. kScLocAmId,
  1608. kEvenAmId,
  1609. kDynAmId,
  1610. kTempoAmId,
  1611. kCostAmId
  1612. };
  1613. cmDspClass_t _cmActiveMeasDC;
  1614. typedef struct cmDspAmRecd_str
  1615. {
  1616. unsigned loc;
  1617. unsigned type;
  1618. double value;
  1619. double cost;
  1620. struct cmDspAmRecd_str* link;
  1621. } cmDspAmRecd_t;
  1622. //int cmDspActiveMeasRecdCompare(const void * p0, const void * p1)
  1623. //{
  1624. // return ((int)((cmDspActiveMeasRecd_t*)p0)->loc) - (int)(((cmDspActiveMeasRecd_t*)p1)->loc);
  1625. //}
  1626. typedef struct
  1627. {
  1628. cmDspInst_t inst;
  1629. unsigned addSymId;
  1630. unsigned clearSymId;
  1631. unsigned printSymId;
  1632. unsigned rewindSymId;
  1633. cmDspAmRecd_t* array; // array[cnt]
  1634. unsigned cnt;
  1635. cmDspAmRecd_t* list; // first recd in list sorted on 'loc'.
  1636. cmDspAmRecd_t* avail; // next empty recd
  1637. cmDspAmRecd_t* sent; // last recd sent
  1638. } cmDspActiveMeas_t;
  1639. void _cmDspAmAllocList( cmDspActiveMeas_t* p, unsigned cnt )
  1640. {
  1641. assert(p->array == NULL );
  1642. cmDspAmRecd_t* r = cmMemAllocZ(cmDspAmRecd_t,cnt);
  1643. p->cnt = cnt;
  1644. p->array = r;
  1645. p->list = NULL;
  1646. p->avail = r;
  1647. p->sent = NULL;
  1648. }
  1649. cmDspRC_t _cmDspActiveMeasAdd( cmDspCtx_t* ctx, cmDspActiveMeas_t* p, unsigned loc, unsigned type, double value, double cost)
  1650. {
  1651. assert( type != kInvalidVarScId );
  1652. cmDspAmRecd_t* rp = p->list;
  1653. cmDspAmRecd_t* pp = NULL;
  1654. // search for the location to add the new record
  1655. for(; rp!=NULL; rp=rp->link)
  1656. {
  1657. // if this loc and type already exists then replace the value and cost fields
  1658. if( rp->loc==loc && rp->type==type )
  1659. goto foundLabel;
  1660. // if this loc should be inserted before rp
  1661. if( loc < rp->loc )
  1662. break;
  1663. pp = rp;
  1664. }
  1665. // if the pre-allocated list is full
  1666. if( p->avail >= p->array+p->cnt )
  1667. return cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"Unable to store new measurement record. All preallocated active measurement slots are in use.");
  1668. // if prepending to the list
  1669. if( pp == NULL )
  1670. {
  1671. rp = p->avail;
  1672. rp->link = p->list;
  1673. p->list = rp;
  1674. }
  1675. else
  1676. {
  1677. // if appending to the list after pp
  1678. if( rp == NULL )
  1679. {
  1680. // nothing to do
  1681. }
  1682. else // if inserting between pp and rp
  1683. {
  1684. p->avail->link = rp;
  1685. }
  1686. rp = p->avail;
  1687. pp->link = rp;
  1688. }
  1689. p->avail += 1;
  1690. foundLabel:
  1691. rp->loc = loc;
  1692. rp->type = type;
  1693. rp->value = value;
  1694. rp->cost = cost;
  1695. return kOkDspRC;
  1696. }
  1697. cmDspInst_t* _cmDspActiveMeasAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1698. {
  1699. cmDspVarArg_t args[] =
  1700. {
  1701. { "cnt", kCntAmId, 0,0, kInDsvFl | kUIntDsvFl, "Maximum count of active measurements."},
  1702. { "sfloc", kSflocAmId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower location input." },
  1703. { "loc", kLocAmId, 0,0, kInDsvFl | kUIntDsvFl, "Meas. location." },
  1704. { "type", kTypeAmId, 0,0, kInDsvFl | kUIntDsvFl, "Meas. Type. (even,dyn,...)" },
  1705. { "val", kValueAmId, 0,0, kInDsvFl | kDoubleDsvFl, "Meas. Value."},
  1706. { "cst", kCstAmId, 0,0, kInDsvFl | kDoubleDsvFl, "Meas. Cost."},
  1707. { "cmd", kCmdAmId, 0,0, kInDsvFl | kSymDsvFl, "Commands:add | clear | dump | rewind"},
  1708. { "scloc", kScLocAmId, 0,0, kOutDsvFl | kUIntDsvFl, "Score location"},
  1709. { "even", kEvenAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Even out"},
  1710. { "dyn", kDynAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Dyn out"},
  1711. { "tempo", kTempoAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Tempo out"},
  1712. { "cost", kCostAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Cost out"},
  1713. { NULL, 0, 0, 0, 0 }
  1714. };
  1715. cmDspActiveMeas_t* p = cmDspInstAlloc(cmDspActiveMeas_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1716. p->addSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"add");
  1717. p->clearSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"clear");
  1718. p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"dump");
  1719. p->rewindSymId= cmSymTblRegisterStaticSymbol(ctx->stH,"rewind");
  1720. cmDspSetDefaultUInt( ctx,&p->inst,kCntAmId, 0,256);
  1721. cmDspSetDefaultUInt( ctx,&p->inst,kScLocAmId,0,0);
  1722. cmDspSetDefaultDouble(ctx,&p->inst,kEvenAmId, 0,0);
  1723. cmDspSetDefaultDouble(ctx,&p->inst,kDynAmId, 0,0);
  1724. cmDspSetDefaultDouble(ctx,&p->inst,kTempoAmId,0,0);
  1725. cmDspSetDefaultDouble(ctx,&p->inst,kTempoAmId,0,0);
  1726. return &p->inst;
  1727. }
  1728. cmDspRC_t _cmDspActiveMeasPrint(cmDspCtx_t* ctx, cmDspActiveMeas_t* p )
  1729. {
  1730. cmDspAmRecd_t* rp = p->list;
  1731. for(; rp!=NULL; rp=rp->link)
  1732. {
  1733. const cmChar_t* label = "<null>";
  1734. switch( rp->type )
  1735. {
  1736. case kEvenVarScId: label="even "; break;
  1737. case kDynVarScId: label="dyn "; break;
  1738. case kTempoVarScId: label="tempo"; break;
  1739. default:
  1740. { assert(0); }
  1741. }
  1742. cmRptPrintf(ctx->rpt,"loc:%i %s %f %f\n",rp->loc,label,rp->value,rp->cost);
  1743. }
  1744. return kOkDspRC;
  1745. }
  1746. cmDspRC_t _cmDspActiveMeasClear(cmDspCtx_t* ctx, cmDspActiveMeas_t* p )
  1747. {
  1748. memset(p->array,0,sizeof(p->array[0])*p->cnt);
  1749. p->avail = p->array;
  1750. p->list = NULL;
  1751. p->avail = p->array;
  1752. return kOkDspRC;
  1753. }
  1754. cmDspRC_t _cmDspActiveMeasFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1755. {
  1756. cmDspActiveMeas_t* p = (cmDspActiveMeas_t*)inst;
  1757. cmMemPtrFree(&p->array);
  1758. return kOkDspRC;
  1759. }
  1760. cmDspRC_t _cmDspActiveMeasReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1761. {
  1762. cmDspRC_t rc = kOkDspRC;
  1763. cmDspActiveMeas_t* p = (cmDspActiveMeas_t*)inst;
  1764. cmDspApplyAllDefaults(ctx,inst);
  1765. unsigned cnt = cmMax(100,cmDspUInt(inst,kCntAmId));
  1766. _cmDspActiveMeasFree(ctx,inst,evt);
  1767. _cmDspAmAllocList(p,cnt);
  1768. return rc;
  1769. }
  1770. cmDspRC_t _cmDspActiveMeasRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1771. {
  1772. cmDspRC_t rc = kOkDspRC;
  1773. cmDspActiveMeas_t* p = (cmDspActiveMeas_t*)inst;
  1774. cmDspSetEvent(ctx,inst,evt);
  1775. switch( evt->dstVarId )
  1776. {
  1777. case kSflocAmId:
  1778. {
  1779. unsigned sfloc = cmDspUInt(inst,kSflocAmId); // get the recv'd score location
  1780. cmDspAmRecd_t* rp = p->sent==NULL ? p->list : p->sent->link; // get the next recd to send
  1781. bool fl = false;
  1782. for(; rp!=NULL; rp=rp->link)
  1783. if( rp->loc <= sfloc )
  1784. {
  1785. // deterimine the records type
  1786. unsigned varId = cmInvalidId;
  1787. switch( rp->type )
  1788. {
  1789. case kEvenVarScId: varId = kEvenAmId; break;
  1790. case kDynVarScId: varId = kDynAmId; break;
  1791. case kTempoVarScId: varId = kTempoAmId; break;
  1792. default:
  1793. { assert(0); }
  1794. }
  1795. // Sending the location triggers the avail-ch to switch - so the location should only
  1796. // be sent once.
  1797. if( !fl )
  1798. {
  1799. cmDspSetUInt(ctx,inst,kScLocAmId,rp->loc);
  1800. fl = true;
  1801. }
  1802. // transmit the records value and cost
  1803. cmDspSetDouble(ctx,inst,varId,rp->value);
  1804. cmDspSetDouble(ctx,inst,kCostAmId,rp->cost);
  1805. p->sent = rp;
  1806. }
  1807. }
  1808. break;
  1809. case kCmdAmId:
  1810. {
  1811. unsigned cmdSymId = cmDspSymbol(inst,kCmdAmId);
  1812. if( cmdSymId == p->addSymId )
  1813. rc = _cmDspActiveMeasAdd(ctx,p,cmDspUInt(inst,kLocAmId),cmDspUInt(inst,kTypeAmId),cmDspDouble(inst,kValueAmId),cmDspDouble(inst,kCstAmId));
  1814. else
  1815. if( cmdSymId == p->clearSymId )
  1816. rc = _cmDspActiveMeasClear(ctx,p);
  1817. else
  1818. if( cmdSymId == p->printSymId )
  1819. rc = _cmDspActiveMeasPrint(ctx,p);
  1820. else
  1821. if(cmdSymId == p->rewindSymId )
  1822. p->sent = NULL;
  1823. }
  1824. break;
  1825. }
  1826. // switch( evt->dstVarId )
  1827. // {
  1828. // case kSflocAmId:
  1829. // if( p->nextFullIdx != cmInvalidIdx )
  1830. // {
  1831. // // get the recv'd score location
  1832. // unsigned sflocIdx = cmDspUInt(inst,kSflocAmId);
  1833. //
  1834. // unsigned prvLoc = cmInvalidIdx;
  1835. //
  1836. // // for each remaining avail record
  1837. // for(; p->nextFullIdx < p->nextEmptyIdx; p->nextFullIdx++)
  1838. // {
  1839. // cmDspActiveMeasRecd_t* r = p->array + p->nextFullIdx;
  1840. //
  1841. // // if this records score location is after the recv'd score loc then we're done
  1842. // if( r->loc > sflocIdx )
  1843. // break;
  1844. //
  1845. // // deterimine the records type
  1846. // unsigned varId = cmInvalidId;
  1847. // switch( r->type )
  1848. // {
  1849. // case kEvenVarScId: varId = kEvenAmId; break;
  1850. // case kDynVarScId: varId = kDynAmId; break;
  1851. // case kTempoVarScId: varId = kTempoAmId; break;
  1852. // default:
  1853. // { assert(0); }
  1854. // }
  1855. //
  1856. // // if this score location has not yet been sent then send it now
  1857. // if( prvLoc != r->loc )
  1858. // cmDspSetUInt(ctx,inst,kScLocAmId,r->loc);
  1859. //
  1860. // // transmit the records value and cost
  1861. // cmDspSetDouble(ctx,inst,varId,r->value);
  1862. // cmDspSetDouble(ctx,inst,kCostAmId,r->cost);
  1863. //
  1864. // prvLoc = r->loc;
  1865. // }
  1866. //
  1867. //
  1868. // }
  1869. // break;
  1870. //
  1871. // case kCmdAmId:
  1872. // {
  1873. // unsigned cmdSymId = cmDspSymbol(inst,kCmdAmId);
  1874. //
  1875. // if( cmdSymId == p->addSymId )
  1876. // {
  1877. // if( p->nextEmptyIdx >= p->cnt )
  1878. // cmDspInstErr(ctx,inst,kProcFailDspRC,"The active measurement list is full cnt=%i.",p->cnt);
  1879. // else
  1880. // {
  1881. // cmDspActiveMeasRecd_t* r = p->array + p->nextEmptyIdx;
  1882. // r->loc = cmDspUInt( inst,kLocAmId);
  1883. // r->type = cmDspUInt( inst,kTypeAmId);
  1884. // r->value = cmDspDouble(inst,kValueAmId);
  1885. // r->cost = cmDspDouble(inst,kCstAmId);
  1886. // p->nextEmptyIdx += 1;
  1887. //
  1888. // qsort(p->array,p->nextEmptyIdx,sizeof(p->array[0]),cmDspActiveMeasRecdCompare);
  1889. //
  1890. // if( p->nextEmptyIdx == 1 && p->nextFullIdx == cmInvalidIdx )
  1891. // p->nextFullIdx = 0;
  1892. //
  1893. // }
  1894. // }
  1895. //
  1896. // if( cmdSymId == p->clearSymId )
  1897. // rc = _cmDspActiveMeasClear(ctx,p);
  1898. // else
  1899. // if( cmdSymId == p->printSymId )
  1900. // rc = _cmDspActiveMeasPrint(ctx,p);
  1901. // else
  1902. // if(cmdSymId == p->rewindSymId )
  1903. // p->nextFullIdx = 0;
  1904. // }
  1905. // break;
  1906. //
  1907. // }
  1908. return rc;
  1909. }
  1910. cmDspClass_t* cmActiveMeasClassCons( cmDspCtx_t* ctx )
  1911. {
  1912. cmDspClassSetup(&_cmActiveMeasDC,ctx,"ActiveMeas",
  1913. NULL,
  1914. _cmDspActiveMeasAlloc,
  1915. _cmDspActiveMeasFree,
  1916. _cmDspActiveMeasReset,
  1917. NULL,
  1918. _cmDspActiveMeasRecv,
  1919. NULL,NULL,
  1920. "Scale a value inside an input range to a value in the output range.");
  1921. return &_cmActiveMeasDC;
  1922. }
  1923. //------------------------------------------------------------------------------------------------------------
  1924. //)
  1925. //( { label:cmDspAmSync file_desc:"Calculate MIDI to Audio latency offsets." kw:[sunit] }
  1926. //
  1927. //
  1928. // Usage:
  1929. // 1) In the program resource file setup a list of sync points.
  1930. // 'asmp' refers to a sample offset into the audio file 'af'
  1931. // which should match to the midi event index 'mid' in the
  1932. // midi file 'mf'.
  1933. //
  1934. // amSync :
  1935. // [
  1936. // { af:"af-16" asmp:34735276 mf:"mf-10" mid:350 }
  1937. // { af:"af-16" asmp:71802194 mf:"mf-10" mid:787 }
  1938. // ]
  1939. //
  1940. // 2) Feed the 'fidx' output from a wave table loaded with 'af' into the 'asmp' input port of this amSync object.
  1941. // Feed the 'id' output from the MIDI file player loaded with 'mf' into the 'mid' input port of this amSync object.
  1942. //
  1943. // 3) Run the players.
  1944. // 4) When the run is complete send any message to the 'sel' port of this amSync object.
  1945. // The 'frm:' field of the printed output gives the difference in samples between
  1946. // MIDI and audio sync points.
  1947. //
  1948. // If the value is positive then the MIDI point is after the Audio point.
  1949. // If the value is negative then the MIDI point is before the audio point.
  1950. //
  1951. enum
  1952. {
  1953. kSelAmId,
  1954. kAFnAmId,
  1955. kASmpAmId,
  1956. kMFnAmId,
  1957. kMIdAmId,
  1958. };
  1959. enum
  1960. {
  1961. kAfnAmFl = 0x01,
  1962. kMfnAmFl = 0x02,
  1963. kAsmpAmFl = 0x04,
  1964. kMidAmFl = 0x08,
  1965. };
  1966. cmDspClass_t _cmAmSyncDC;
  1967. typedef struct cmDspAmSyncEntry_str
  1968. {
  1969. const cmChar_t* afn; // audio file name
  1970. const cmChar_t* mfn; // midi file name
  1971. unsigned asmp; // Audio sample index to sync to MIDI event
  1972. unsigned mid; // MIDI event unique id (cmMidiTrackMsg_t.uid)
  1973. int afi; // closest DSP system cycle index to the reference audio sample index (asmp).
  1974. int mfi; // DSP system cycle on which the reference MIDI event (mid) arrived.
  1975. unsigned state; // as incoming msg match this record the state is updated with kXXXAmFl flags
  1976. } cmDspAmSyncEntry_t;
  1977. typedef struct
  1978. {
  1979. cmDspInst_t inst;
  1980. cmDspAmSyncEntry_t* array;
  1981. unsigned arrayCnt;
  1982. cmDspAmSyncEntry_t* acur;
  1983. cmDspAmSyncEntry_t* mcur;
  1984. } cmDspAmSync_t;
  1985. cmDspInst_t* _cmDspAmSyncAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1986. {
  1987. cmDspVarArg_t args[] =
  1988. {
  1989. { "sel", kSelAmId, 0, 0, kInDsvFl | kTypeDsvMask, "Print and reset" },
  1990. { "afn", kAFnAmId, 0, 0, kInDsvFl | kStrzDsvFl, "Audio File name"},
  1991. { "asmp", kASmpAmId, 0, 0, kInDsvFl | kIntDsvFl, "Audio sample index"},
  1992. { "mfn", kMFnAmId, 0, 0, kInDsvFl | kStrzDsvFl, "MIDI File name"},
  1993. { "mid", kMIdAmId, 0, 0, kInDsvFl | kIntDsvFl, "MIDI Event Unique Id"},
  1994. { NULL, 0, 0, 0, 0 }
  1995. };
  1996. cmDspAmSync_t* p = cmDspInstAlloc(cmDspAmSync_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1997. return &p->inst;
  1998. }
  1999. cmDspRC_t _cmDspAmSyncFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2000. {
  2001. cmDspAmSync_t* p = (cmDspAmSync_t*)inst;
  2002. cmMemFree(p->array);
  2003. return kOkDspRC;
  2004. }
  2005. cmDspRC_t _cmDspAmSyncReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2006. {
  2007. cmDspRC_t rc = kOkDspRC;
  2008. cmDspAmSync_t* p = (cmDspAmSync_t*)inst;
  2009. cmDspApplyAllDefaults(ctx,inst);
  2010. cmJsonH_t jsH = cmDspSysPgmRsrcHandle(ctx->dspH);
  2011. cmJsonNode_t* np;
  2012. const cmChar_t* errLabelPtr;
  2013. unsigned i;
  2014. cmJsRC_t jsRC;
  2015. if((np = cmJsonFindValue(jsH, "amSync", NULL, kArrayTId)) == NULL )
  2016. {
  2017. rc = cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The AUDIO MIDI Sync cfg. record was not found.");
  2018. goto errLabel;
  2019. }
  2020. p->arrayCnt = cmJsonChildCount(np);
  2021. p->array = cmMemResizeZ(cmDspAmSyncEntry_t,p->array,p->arrayCnt);
  2022. for(i=0; i<p->arrayCnt; ++i)
  2023. {
  2024. cmJsonNode_t* anp = cmJsonArrayElement(np,i);
  2025. cmDspAmSyncEntry_t* r = p->array + i;
  2026. if( (jsRC = cmJsonMemberValues(anp,&errLabelPtr,
  2027. "af", kStringTId,&r->afn,
  2028. "asmp",kIntTId, &r->asmp,
  2029. "mf", kStringTId,&r->mfn,
  2030. "mid", kIntTId, &r->mid,
  2031. NULL)) != kOkJsRC )
  2032. {
  2033. if( jsRC == kNodeNotFoundJsRC )
  2034. rc = cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The Audio-MIDI Sync cfg. field '%s' was missing in the cfg. record at index %i.",errLabelPtr,i);
  2035. else
  2036. rc = cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The AUDIO MIDI Sync cfg. parse failed on the record at index %i.",i);
  2037. break;
  2038. }
  2039. r->afi = cmInvalidIdx;
  2040. r->mfi = cmInvalidIdx;
  2041. r->state = 0;
  2042. }
  2043. errLabel:
  2044. return rc;
  2045. }
  2046. cmDspRC_t _cmDspAmSyncRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2047. {
  2048. cmDspAmSync_t* p = (cmDspAmSync_t*)inst;
  2049. unsigned i;
  2050. if( evt->dstVarId != kSelAmId )
  2051. cmDspSetEvent(ctx,inst,evt);
  2052. switch(evt->dstVarId)
  2053. {
  2054. case kSelAmId:
  2055. {
  2056. double srate = cmDspSysSampleRate(ctx->dspH);
  2057. int fpc = cmDspSamplesPerCycle(ctx);
  2058. for(i=0; i<p->arrayCnt; ++i)
  2059. {
  2060. cmDspAmSyncEntry_t* r = p->array + i;
  2061. int dframes = r->mfi-r->afi;
  2062. cmRptPrintf(ctx->rpt,"0x%x : %s %i %i - %s %i %i : frm:%i smp:%i sec:%f\n",
  2063. r->state,
  2064. r->afn,
  2065. r->asmp,
  2066. r->afi,
  2067. r->mfn,
  2068. r->mid,
  2069. r->mfi,
  2070. dframes,
  2071. dframes*fpc,dframes*fpc/srate);
  2072. r->afi = cmInvalidIdx;
  2073. r->mfi = cmInvalidIdx;
  2074. r->state = 0;
  2075. }
  2076. p->acur = NULL;
  2077. p->mcur = NULL;
  2078. }
  2079. break;
  2080. case kAFnAmId:
  2081. {
  2082. // an audio file name just arrived - set p->acur to point to it
  2083. const cmChar_t* fn = cmDspStrcz(inst, kAFnAmId );
  2084. for(i=0; i<p->arrayCnt; ++i)
  2085. if( strcmp(fn,p->array[i].afn) == 0 )
  2086. {
  2087. p->array[i].state = cmSetFlag(p->array[i].state,kAfnAmFl);
  2088. p->acur = p->array + i;
  2089. }
  2090. }
  2091. break;
  2092. case kMFnAmId:
  2093. {
  2094. // a midi file name just arrived - set p->mcur to point to it
  2095. const cmChar_t* fn = cmDspStrcz(inst, kMFnAmId );
  2096. for(i=0; i<p->arrayCnt; ++i)
  2097. if( strcmp(fn,p->array[i].mfn) == 0 )
  2098. {
  2099. p->array[i].state = cmSetFlag(p->array[i].state,kMfnAmFl);
  2100. p->mcur = p->array + i;
  2101. }
  2102. }
  2103. break;
  2104. case kASmpAmId:
  2105. {
  2106. // a audio file sample index has just arrived
  2107. int v = cmDspInt(inst,kASmpAmId);
  2108. // if a valid audio file has been set
  2109. if( p->acur != NULL )
  2110. for(i=0; i<p->arrayCnt; ++i)
  2111. {
  2112. // if the audio sync point is before or on the new audio file sample index then
  2113. // this is the closest audio file index to the audio sync point - record the
  2114. // associated cycleCnt
  2115. cmDspAmSyncEntry_t* r = p->array + i;
  2116. if( cmIsNotFlag(r->state,kAsmpAmFl) && r->asmp <= v && strcmp(p->acur->afn,r->afn)==0 )
  2117. {
  2118. r->afi = ctx->cycleCnt;
  2119. r->state = cmSetFlag(r->state,kAsmpAmFl);
  2120. break;
  2121. }
  2122. }
  2123. }
  2124. break;
  2125. case kMIdAmId:
  2126. {
  2127. // a new MIDI event was received
  2128. int v = cmDspInt(inst,kMIdAmId);
  2129. if( p->mcur != NULL )
  2130. for(i=0; i<p->arrayCnt; ++i)
  2131. {
  2132. // if the new MIDI event matched the MIDI sync point then record the
  2133. // current cycleCnt.
  2134. cmDspAmSyncEntry_t* r = p->array + i;
  2135. if( cmIsNotFlag(r->state,kMidAmFl) && r->mid == v && strcmp(p->mcur->mfn,r->mfn)==0 )
  2136. {
  2137. r->mfi = ctx->cycleCnt;
  2138. r->state = cmSetFlag(r->state,kMidAmFl);
  2139. break;
  2140. }
  2141. }
  2142. }
  2143. break;
  2144. }
  2145. return kOkDspRC;
  2146. }
  2147. cmDspClass_t* cmAmSyncClassCons( cmDspCtx_t* ctx )
  2148. {
  2149. cmDspClassSetup(&_cmAmSyncDC,ctx,"AmSync",
  2150. NULL,
  2151. _cmDspAmSyncAlloc,
  2152. _cmDspAmSyncFree,
  2153. _cmDspAmSyncReset,
  2154. NULL,
  2155. _cmDspAmSyncRecv,
  2156. NULL,NULL,
  2157. "Audio - MIDI Sync Object.");
  2158. return &_cmAmSyncDC;
  2159. }
  2160. //------------------------------------------------------------------------------------------------------------
  2161. //)
  2162. //( { label:cmDspNanoMap file_desc:"Control a MIDI synth." kw:[sunit] }
  2163. enum
  2164. {
  2165. kPgmNmId,
  2166. kStatusNmId,
  2167. kD0NmId,
  2168. kD1NmId,
  2169. kThruNmId
  2170. };
  2171. cmDspClass_t _cmNanoMapDC;
  2172. typedef struct
  2173. {
  2174. cmDspInst_t inst;
  2175. } cmDspNanoMap_t;
  2176. cmDspRC_t _cmDspNanoMapSend( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned st, unsigned d0, unsigned d1 )
  2177. {
  2178. cmDspSetUInt(ctx,inst,kD1NmId,d1);
  2179. cmDspSetUInt(ctx,inst,kD0NmId,d0);
  2180. cmDspSetUInt(ctx,inst,kStatusNmId,st);
  2181. return kOkDspRC;
  2182. }
  2183. void _cmDspNanoMapPgm( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned pgm )
  2184. {
  2185. //cmDspNanoMap_t* p = (cmDspNanoMap_t*)inst;
  2186. unsigned i;
  2187. for(i=0; i<kMidiChCnt; ++i)
  2188. {
  2189. _cmDspNanoMapSend(ctx,inst,kCtlMdId+i,121,0); // reset all controllers
  2190. _cmDspNanoMapSend(ctx,inst,kCtlMdId+i,123,0); // turn all notes off
  2191. _cmDspNanoMapSend(ctx,inst,kCtlMdId+i,0,0); // switch to bank 0
  2192. _cmDspNanoMapSend(ctx,inst,kPgmMdId+i,pgm,0); // send pgm change
  2193. cmSleepMs(15);
  2194. }
  2195. }
  2196. cmDspInst_t* _cmDspNanoMapAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2197. {
  2198. cmDspVarArg_t args[] =
  2199. {
  2200. { "pgm", kPgmNmId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Reprogram all channels to this pgm." },
  2201. { "status", kStatusNmId, 0, 0, kOutDsvFl | kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "MIDI status" },
  2202. { "d0", kD0NmId, 0, 0, kOutDsvFl | kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "MIDI channel message d0" },
  2203. { "d1", kD1NmId, 0, 0, kOutDsvFl | kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "MIDI channel message d1" },
  2204. { "thru", kThruNmId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Enable pass through."},
  2205. { NULL, 0, 0, 0, 0 }
  2206. };
  2207. cmDspNanoMap_t* p = cmDspInstAlloc(cmDspNanoMap_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  2208. cmDspSetDefaultUInt(ctx,&p->inst, kPgmNmId, 0, 0 );
  2209. return &p->inst;
  2210. }
  2211. cmDspRC_t _cmDspNanoMapReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2212. {
  2213. cmDspRC_t rc = kOkDspRC;
  2214. cmDspApplyAllDefaults(ctx,inst);
  2215. _cmDspNanoMapPgm(ctx,inst,cmDspUInt(inst,kPgmNmId));
  2216. return rc;
  2217. }
  2218. cmDspRC_t _cmDspNanoMapRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2219. {
  2220. //cmDspNanoMap_t* p = (cmDspNanoMap_t*)inst;
  2221. switch( evt->dstVarId )
  2222. {
  2223. case kPgmNmId:
  2224. cmDspSetEvent(ctx,inst,evt);
  2225. _cmDspNanoMapPgm(ctx,inst,cmDspUInt(inst,kPgmNmId));
  2226. break;
  2227. case kStatusNmId:
  2228. {
  2229. unsigned status = cmDsvGetUInt(evt->valuePtr);
  2230. unsigned stat_no_ch = status & 0xf0;
  2231. if( stat_no_ch == kNoteOnMdId || stat_no_ch == kNoteOffMdId || stat_no_ch == kCtlMdId )
  2232. {
  2233. //unsigned d0 = cmDspUInt(inst,kD0NmId);
  2234. unsigned ch = 0; //d0 % 8;
  2235. status = (status & 0xf0) + ch;
  2236. cmDspSetUInt(ctx,inst,kStatusNmId,status);
  2237. }
  2238. }
  2239. break;
  2240. default:
  2241. cmDspSetEvent(ctx,inst,evt);
  2242. break;
  2243. }
  2244. return kOkDspRC;
  2245. }
  2246. cmDspClass_t* cmNanoMapClassCons( cmDspCtx_t* ctx )
  2247. {
  2248. cmDspClassSetup(&_cmNanoMapDC,ctx,"NanoMap",
  2249. NULL,
  2250. _cmDspNanoMapAlloc,
  2251. NULL,
  2252. _cmDspNanoMapReset,
  2253. NULL,
  2254. _cmDspNanoMapRecv,
  2255. NULL,
  2256. NULL,
  2257. "Nanosynth Mapper");
  2258. return &_cmNanoMapDC;
  2259. }
  2260. //------------------------------------------------------------------------------------------------------------
  2261. //)
  2262. //( { label:cmDspPicadae file_desc:"Control a MIDI synth." kw:[sunit] }
  2263. enum
  2264. {
  2265. kStatusPcId,
  2266. kD0PcId,
  2267. kD1PcId,
  2268. kAllOffPcId
  2269. };
  2270. cmDspClass_t _cmPicadaeDC;
  2271. typedef struct
  2272. {
  2273. unsigned pitch;
  2274. unsigned velMapA[10];
  2275. } cmDspPicadaeVelMap_t;
  2276. // This table is generated from picadae_ac_3/velTableToDataStruct.py
  2277. cmDspPicadaeVelMap_t cmDspPicadaeVelMap[] =
  2278. {
  2279. { 21,{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } },
  2280. { 22,{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } },
  2281. { 23, { 12800, 12950, 13175, 13500, 13750, 14750, 15375, 17500, 23000, 37000, } },
  2282. { 24, { 12425, 12800, 13175, 14225, 14750, 15500, 17500, 22500, 32000, 39000, } },
  2283. { 25, { 14150, 14375, 14975, 14625, 15500, 16500, 20000, 28500, 40000, 40000, } },
  2284. { 26, { 13000, 13175, 13500, 13700, 13925, 14250, 15000, 16250, 19000, 26500, } },
  2285. { 27, { 13625, 13925, 14075, 14250, 14500, 14875, 15375, 16500, 18750, 25000, } },
  2286. { 28, { 12625, 13750, 13775, 14225, 14500, 16500, 18000, 20000, 25500, 34000, } },
  2287. { 29, { 12125, 12725, 13000, 12950, 14150, 15500, 16250, 17750, 21500, 28000, } },
  2288. { 30, { 13175, 13325, 13550, 14450, 14875, 15500, 16250, 17750, 21500, 27000, } },
  2289. { 31, { 13925, 14075, 14450, 14625, 15500, 16250, 16750, 17750, 19500, 23500, } },
  2290. { 32, { 13250, 14150, 14975, 14750, 15250, 16000, 17500, 21000, 27000, 38000, } },
  2291. { 33, { 11825, 13025, 14075, 14825, 14375, 14875, 16250, 17500, 22000, 28000, } },
  2292. { 34, { 13025, 13375, 13325, 13775, 14375, 14500, 15250, 18000, 22000, 27000, } },
  2293. { 35, { 11375, 12250, 12350, 12725, 14225, 13750, 15375, 17000, 20500, 25000, } },
  2294. { 36, { 11750, 13875, 14125, 14225, 14675, 14750, 16500, 18500, 22500, 32000, } },
  2295. { 37, { 12425, 12575, 13000, 13025, 13375, 15000, 16000, 18750, 25500, 35000, } },
  2296. { 38, { 13750, 13875, 14075, 14600, 14750, 15500, 17750, 21500, 27500, 37000, } },
  2297. { 39, { 11000, 12500, 12950, 13700, 14875, 15500, 16250, 20000, 26500, 37000, } },
  2298. { 40, { 11525, 11750, 12125, 12500, 12875, 13500, 14625, 18250, 23500, 29000, } },
  2299. { 41, { 11675, 11750, 12500, 13000, 13925, 15250, 17000, 20000, 26500, 36000, } },
  2300. { 42, { 11875, 12000, 11975, 12050, 12275, 13375, 15000, 17250, 22000, 29000, } },
  2301. { 43, { 11500, 11625, 11750, 11750, 12625, 12250, 13625, 16750, 19500, 25500, } },
  2302. { 44, { 12425, 12500, 12750, 12650, 13000, 14000, 15250, 16500, 20000, 27000, } },
  2303. { 45, { 11250, 11600, 11875, 12000, 12250, 13100, 14750, 15500, 18250, 25500, } },
  2304. { 46, { 11450, 11525, 11600, 11625, 11875, 12250, 14000, 15750, 17750, 21500, } },
  2305. { 47, { 11900, 11975, 12125, 12375, 13125, 14375, 15750, 18750, 22500, 28500, } },
  2306. { 48, { 11750, 13100, 13325, 13625, 14300, 14975, 15750, 19000, 24000, 30000, } },
  2307. { 49, { 11975, 12050, 12500, 12750, 13125, 14000, 17000, 20000, 25500, 40000, } },
  2308. { 50, { 11625, 11525, 11750, 11825, 12125, 12375, 14750, 16250, 19000, 25500, } },
  2309. { 51, { 12050, 12125, 12125, 12275, 12350, 12500, 12875, 16250, 18500, 22500, } },
  2310. { 52, { 12950, 13025, 13125, 13175, 13250, 13500, 13875, 15750, 18000, 22000, } },
  2311. { 53, { 10600, 10250, 10350, 10450, 10900, 11375, 13025, 14750, 18250, 26500, } },
  2312. { 54, { 12650, 12625, 12725, 12800, 13000, 13625, 16250, 18500, 23000, 32000, } },
  2313. { 55, { 11875, 12125, 12250, 12425, 12875, 13175, 13750, 17250, 20000, 26000, } },
  2314. { 56, { 11625, 11750, 12000, 12200, 12500, 13125, 14375, 17000, 20500, 26500, } },
  2315. { 57, { 11625, 11750, 12125, 12275, 12750, 14625, 16750, 20000, 25500, 39000, } },
  2316. { 58, { 12000, 12500, 12750, 12875, 13100, 13375, 15000, 17750, 21000, 28000, } },
  2317. { 59, { 11625, 11525, 12050, 13375, 13625, 14150, 16500, 21000, 24500, 30000, } },
  2318. { 60, { 12250, 12250, 12375, 12350, 13000, 13500, 16000, 17750, 22000, 29000, } },
  2319. { 61, { 11375, 11500, 11625, 11750, 12000, 12200, 12725, 13625, 17500, 21000, } },
  2320. { 62, { 11600, 11675, 11825, 12125, 12650, 13375, 14375, 18500, 24500, 32000, } },
  2321. { 63, { 12125, 12200, 12350, 12500, 13025, 13625, 16250, 18750, 24500, 36000, } },
  2322. { 64, { 10550, 10650, 10850, 11250, 11875, 12250, 14000, 16250, 19500, 26500, } },
  2323. { 65, { 12750, 12800, 12875, 13175, 13250, 14625, 14975, 17500, 20500, 26000, } },
  2324. { 66, { 10750, 11000, 11250, 11500, 12000, 12875, 15375, 17000, 20500, 28500, } },
  2325. { 67, { 10950, 11125, 11250, 11500, 11875, 13875, 15750, 17750, 23000, 37000, } },
  2326. { 68, { 10150, 10300, 10550, 10800, 11125, 11875, 13000, 16000, 19000, 25000, } },
  2327. { 69, { 11750, 11875, 12375, 12500, 12750, 13500, 16250, 18250, 23500, 31000, } },
  2328. { 70, { 10700, 10850, 10950, 11125, 11625, 13875, 14500, 15750, 18750, 24500, } },
  2329. { 71, { 10200, 10700, 11000, 11250, 11625, 14000, 14875, 16250, 22000, 27000, } },
  2330. { 72, { 9800, 10100, 10400, 10550, 11000, 11625, 13000, 15500, 17750, 23000, } },
  2331. { 73, { 10750, 10900, 11125, 11375, 11625, 12750, 14750, 15500, 18500, 23000, } },
  2332. { 74, { 10300, 10450, 10600, 10850, 11250, 12000, 14250, 15000, 17500, 21000, } },
  2333. { 75, { 10600, 10750, 10900, 11125, 12500, 14500, 14750, 15000, 21000, 31000, } },
  2334. { 76, { 10200, 11625, 12375, 12875, 13500, 15750, 19000, 22500, 27500, 39000, } },
  2335. { 77, { 10500, 10700, 11125, 11375, 11750, 14000, 14875, 16500, 20500, 27000, } },
  2336. { 78, { 10450, 10800, 11000, 11625, 12000, 13125, 15500, 18250, 22000, 34000, } },
  2337. { 79, { 12250, 13500, 14125, 14750, 16250, 17500, 19000, 24500, 31000, 40000, } },
  2338. { 80, { 10400, 10450, 10750, 11125, 12125, 13375, 14750, 17750, 23000, 39000, } },
  2339. { 81, { 10800, 10950, 11125, 11375, 12625, 13875, 14875, 16000, 19000, 23500, } },
  2340. { 82, { 12000, 12375, 13750, 13750, 12625, 14000, 17000, 19000, 21000, 24500, } },
  2341. { 83, { 12250, 12500, 13625, 13875, 14375, 16500, 17750, 20500, 25000, 35000, } },
  2342. { 84, { 11500, 12000, 12250, 12500, 13125, 14250, 15375, 16750, 19500, 25500, } },
  2343. { 85, { 10400, 10500, 10600, 11250, 12250, 13375, 15000, 16750, 20000, 26000, } },
  2344. { 86, { 11500, 11750, 11875, 12000, 12250, 12500, 13500, 15000, 20500, 21000, } },
  2345. { 87, { 10650, 11500, 13125, 13375, 13750, 14500, 16500, 18000, 20000, 24000, } },
  2346. { 88, { 11375, 11375, 11500, 12375, 12000, 13375, 14500, 16500, 19000, 23000, } },
  2347. { 89, { 9200, 10900, 11500, 12125, 22000, 12875, 14125, 16000, 19000, 26500, } },
  2348. { 90, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } },
  2349. { 91, { 9450, 9950, 10000, 10150, 10600, 11250, 12125, 13875, 15250, 19000, } },
  2350. { 92, { 9050, 9500, 9600, 10100, 10900, 11875, 13000, 16000, 20500, 31000, } },
  2351. { 93, { 11250, 11375, 12000, 12375, 12875, 13625, 14250, 17500, 21500, 39000, } },
  2352. { 94, { 11125, 11375, 11750, 13500, 14000, 14875, 15750, 17750, 22000, 25500, } },
  2353. { 95, { 10200, 10350, 11500, 12250, 12500, 13125, 13875, 15250, 19000, 21000, } },
  2354. { 96, { 9050, 9550, 10100, 13875, 13000, 14000, 18500, 22000, 27000, 39000, } },
  2355. { 97, { 11000, 12500, 13250, 13000, 13750, 15750, 15000, 18000, 19000, 22500, } },
  2356. { 98, { 10400, 10850, 12125, 12125, 13250, 13875, 16000, 18750, 26500, 37000, } },
  2357. { 99, { 11000, 12625, 13125, 14000, 15500, 16750, 19000, 21500, 25000, 36000, } },
  2358. { 100,{ 9650, 10450, 11500, 12375, 12500, 12875, 13500, 15500, 17500, 21500, } },
  2359. { 101,{ 10950, 11250, 11500, 11875, 12250, 12875, 13500, 14375, 22500, 39000, } },
  2360. { 102,{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } },
  2361. { 103,{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } },
  2362. { 104,{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } },
  2363. { 105,{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } },
  2364. { 106,{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } },
  2365. { 107,{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } },
  2366. { 108,{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } },
  2367. { 0, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }
  2368. };
  2369. typedef struct
  2370. {
  2371. int us;
  2372. int dutyPct;
  2373. } cmDspPicadaeDutyMap_t;
  2374. typedef struct
  2375. {
  2376. unsigned pitch;
  2377. cmDspPicadaeDutyMap_t dutyMapA[5];
  2378. } cmDspPicadaeDuty_t;
  2379. // This table is generated from picadae_ac_3/velTableToDataStruct.py
  2380. cmDspPicadaeDuty_t cmDspPicadaeDutyMap[] =
  2381. {
  2382. {21, {{ -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2383. {22, {{ -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2384. {23, {{ 0, 70 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2385. {24, {{ 0, 75 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2386. {25, {{ 0, 70 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2387. {26, {{ 0, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2388. {27, {{ 0, 70 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2389. {28, {{ 0, 70 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2390. {29, {{ 0, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2391. {30, {{ 0, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2392. {31, {{ 0, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2393. {32, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2394. {33, {{ 0, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2395. {34, {{ 0, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2396. {35, {{ 0, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2397. {36, {{ 0, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2398. {37, {{ 0, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2399. {38, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2400. {39, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2401. {40, {{ 0, 55 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2402. {41, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2403. {42, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2404. {43, {{ 0, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2405. {44, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2406. {45, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2407. {46, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2408. {47, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2409. {48, {{ 0, 70 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2410. {49, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2411. {50, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2412. {51, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2413. {52, {{ 0, 55 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2414. {53, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2415. {54, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2416. {55, {{ 0, 50 }, { 22000, 55 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2417. {56, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2418. {57, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2419. {58, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2420. {59, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2421. {60, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2422. {61, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2423. {62, {{ 0, 55 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2424. {63, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2425. {64, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2426. {65, {{ 0, 50 }, { 17000, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2427. {66, {{ 0, 53 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2428. {67, {{ 0, 55 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2429. {68, {{ 0, 53 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2430. {69, {{ 0, 55 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2431. {70, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2432. {71, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2433. {72, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2434. {73, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2435. {74, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2436. {75, {{ 0, 55 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2437. {76, {{ 0, 70 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2438. {77, {{ 0, 50 }, { 15000, 60 }, { 19000, 70 }, { -1, -1 }, { -1, -1 }, }},
  2439. {78, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2440. {79, {{ 0, 50 }, { 15000, 60 }, { 19000, 70 }, { -1, -1 }, { -1, -1 }, }},
  2441. {80, {{ 0, 45 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2442. {81, {{ 0, 50 }, { 15000, 70 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2443. {82, {{ 0, 50 }, { 12500, 60 }, { 14000, 65 }, { 17000, 70 }, { -1, -1 }, }},
  2444. {83, {{ 0, 50 }, { 15000, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2445. {84, {{ 0, 50 }, { 12500, 60 }, { 14000, 65 }, { 17000, 70 }, { -1, -1 }, }},
  2446. {85, {{ 0, 50 }, { 12500, 60 }, { 14000, 65 }, { 17000, 70 }, { -1, -1 }, }},
  2447. {86, {{ 0, 50 }, { 12500, 60 }, { 14000, 65 }, { 17000, 70 }, { -1, -1 }, }},
  2448. {87, {{ 0, 50 }, { 14000, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2449. {88, {{ 0, 50 }, { 12500, 60 }, { 14000, 65 }, { 17000, 70 }, { -1, -1 }, }},
  2450. {89, {{ 0, 50 }, { 12500, 60 }, { 14000, 65 }, { 17000, 70 }, { -1, -1 }, }},
  2451. {90, {{ -1, -1}, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2452. {91, {{ 0, 40 }, { 12500, 50 }, { 14000, 60 }, { 17000, 65 }, { -1, -1 }, }},
  2453. {92, {{ 0, 40 }, { 14000, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2454. {93, {{ 0, 40 }, { 12500, 50 }, { 14000, 60 }, { 17000, 65 }, { -1, -1 }, }},
  2455. {94, {{ 0, 40 }, { 14000, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2456. {95, {{ 0, 40 }, { 12500, 50 }, { 14000, 60 }, { 17000, 65 }, { -1, -1 }, }},
  2457. {96, {{ 0, 40 }, { 12500, 50 }, { 14000, 60 }, { 17000, 65 }, { -1, -1 }, }},
  2458. {97, {{ 0, 40 }, { 14000, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2459. {98, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2460. {99, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2461. {100, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2462. {101, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2463. {102, {{ -1, -1}, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2464. {103, {{ -1, -1}, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2465. {104, {{ -1, -1}, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2466. {105, {{ -1, -1}, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2467. {106, {{ -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2468. {107, {{ -1, -1}, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2469. {108, {{ -1, -1}, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2470. { 0, {{ -1, -1}, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, }},
  2471. };
  2472. typedef struct
  2473. {
  2474. cmDspInst_t inst;
  2475. unsigned curDutyPctA[127];
  2476. } cmDspPicadae_t;
  2477. unsigned _cmDspPicadaeVelToMicroseconds( unsigned pitch, unsigned vel )
  2478. {
  2479. unsigned i;
  2480. unsigned velN = sizeof(cmDspPicadaeVelMap[i].velMapA)/sizeof(cmDspPicadaeVelMap[i].velMapA[0]);
  2481. long velIdx = lround( vel * velN / 127.0 );
  2482. assert( pitch >= cmDspPicadaeVelMap[0].pitch );
  2483. long pitchIdx = pitch - cmDspPicadaeVelMap[0].pitch;
  2484. return cmDspPicadaeVelMap[pitchIdx].velMapA[velIdx];
  2485. }
  2486. int _cmDspPicadaeVelToDutyCycle( unsigned pitch, unsigned usec )
  2487. {
  2488. unsigned i;
  2489. int dutyPct = -1;
  2490. long pitchIdx = pitch - cmDspPicadaeDutyMap[0].pitch;
  2491. cmDspPicadaeDuty_t* r = cmDspPicadaeDutyMap + pitchIdx;
  2492. for(i=0; r->dutyMapA[i].us != -1; ++i)
  2493. if( r->dutyMapA[i].us <= usec )
  2494. dutyPct = r->dutyMapA[i].dutyPct;
  2495. return dutyPct;
  2496. }
  2497. void _cmDspPicadaeSerialXmitNoteOff( cmDspCtx_t* ctx, cmDspInst_t* inst, uint8_t pitch )
  2498. {
  2499. const uint8_t note_off_op = 3;
  2500. uint8_t serialBuf[] = { 'w', pitch, note_off_op, 1, 0 };
  2501. cmSeSend( cmDspSysSerialPort(ctx->dspH), serialBuf, sizeof(serialBuf) );
  2502. }
  2503. void _cmDspPicadaeSerialXmitNoteOn( cmDspCtx_t* ctx, cmDspInst_t* inst, uint8_t pitch, uint8_t coarse_tick, uint8_t fine_tick )
  2504. {
  2505. const uint8_t note_on_usec_op = 2;
  2506. uint8_t serialBuf[] = { 'w', pitch, note_on_usec_op, 2, coarse_tick, fine_tick };
  2507. cmSeSend( cmDspSysSerialPort(ctx->dspH), serialBuf, sizeof(serialBuf) );
  2508. }
  2509. void _cmDspPicadaeSerialXmitDuty( cmDspCtx_t* ctx, cmDspPicadae_t* p, uint8_t pitch, uint8_t dutyPct )
  2510. {
  2511. if( p->curDutyPctA[ pitch ] != dutyPct )
  2512. {
  2513. uint8_t duty_pct_op = 0;
  2514. unsigned char serialBuf[] = { 'w', pitch, duty_pct_op, 1, dutyPct };
  2515. cmSeSend( cmDspSysSerialPort(ctx->dspH), serialBuf, sizeof(serialBuf) );
  2516. }
  2517. }
  2518. void _cmDspPicadaeSendNote( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2519. {
  2520. cmDspPicadae_t* p = (cmDspPicadae_t*)inst;
  2521. unsigned prescaler_usec = 16;
  2522. unsigned status = cmDspUInt(inst,kStatusPcId);
  2523. uint8_t pitch = (uint8_t)cmDspUInt(inst,kD0PcId);
  2524. unsigned velocity = cmDspUInt(inst,kD1PcId);
  2525. assert( 21<=pitch && pitch<=108 );
  2526. // if this is a note-off
  2527. if( status == kNoteOffMdId || velocity == 0 )
  2528. {
  2529. _cmDspPicadaeSerialXmitNoteOff( ctx, inst, pitch );
  2530. return;
  2531. }
  2532. unsigned usec = _cmDspPicadaeVelToMicroseconds(pitch, velocity );
  2533. // if the pitch is invalid then 'usec' will be set to 0
  2534. if( usec == 0 )
  2535. return;
  2536. unsigned dutyPct = _cmDspPicadaeVelToDutyCycle( pitch, usec );
  2537. // if the pitch is invalid then dutyPct will be set to -1
  2538. if( dutyPct == -1 )
  2539. return;
  2540. unsigned coarse_usec = prescaler_usec*255; // usec's in one coarse tick
  2541. uint8_t coarse_ticks = (uint8_t)(usec / coarse_usec);
  2542. uint8_t fine_ticks = (uint8_t)((usec - coarse_ticks*coarse_usec) / prescaler_usec);
  2543. _cmDspPicadaeSerialXmitDuty( ctx, p, pitch, dutyPct );
  2544. _cmDspPicadaeSerialXmitNoteOn( ctx, inst, pitch, coarse_ticks, fine_ticks );
  2545. }
  2546. cmDspInst_t* _cmDspPicadaeAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2547. {
  2548. cmDspVarArg_t args[] =
  2549. {
  2550. { "status", kStatusPcId, 0, 0, kOutDsvFl | kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "MIDI status" },
  2551. { "d0", kD0PcId, 0, 0, kOutDsvFl | kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "MIDI channel message d0" },
  2552. { "d1", kD1PcId, 0, 0, kOutDsvFl | kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "MIDI channel message d1" },
  2553. { "alloff", kAllOffPcId, 0, 0, kOutDsvFl | kInDsvFl | kSymDsvFl | kOptArgDsvFl, "All notes off."},
  2554. { NULL, 0, 0, 0, 0 }
  2555. };
  2556. cmDspPicadae_t* p = cmDspInstAlloc(cmDspPicadae_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  2557. return &p->inst;
  2558. }
  2559. void _cmDspPicadaReset( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2560. {
  2561. cmDspPicadae_t* p = (cmDspPicadae_t*)inst;
  2562. memset(p->curDutyPctA,0,sizeof(p->curDutyPctA));
  2563. // send note-off messages to all keys
  2564. unsigned i = 0;
  2565. for(i=0; cmDspPicadaeVelMap[i].pitch !=0; ++i)
  2566. if( cmDspPicadaeVelMap[i].velMapA[0] != 0 )
  2567. {
  2568. _cmDspPicadaeSerialXmitNoteOff( ctx, inst, (uint8_t)cmDspPicadaeVelMap[i].pitch );
  2569. cmSleepMs(10);
  2570. }
  2571. // send pedal-up message
  2572. cmDspSetUInt(ctx,inst,kD0PcId, kSustainCtlMdId );
  2573. cmDspSetUInt(ctx,inst,kD0PcId, 0 );
  2574. cmDspSetUInt(ctx,inst,kStatusPcId,kCtlMdId);
  2575. }
  2576. cmDspRC_t _cmDspPicadaeReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2577. {
  2578. cmDspRC_t rc = kOkDspRC;
  2579. cmDspApplyAllDefaults(ctx,inst);
  2580. return rc;
  2581. }
  2582. cmDspRC_t _cmDspPicadaeRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2583. {
  2584. //cmDspPicadae_t* p = (cmDspPicadae_t*)inst;
  2585. switch( evt->dstVarId )
  2586. {
  2587. case kStatusPcId:
  2588. {
  2589. // note: the status byte arrives last (after d0 and d1)
  2590. unsigned status = cmDsvGetUInt(evt->valuePtr);
  2591. unsigned stat_no_ch = status & 0xf0;
  2592. // if this is a note-on or channel
  2593. if( stat_no_ch == kNoteOnMdId || stat_no_ch == kNoteOffMdId || stat_no_ch == kCtlMdId )
  2594. {
  2595. cmDspSetUInt(ctx,inst,kStatusPcId,stat_no_ch);
  2596. if( stat_no_ch == kNoteOnMdId || stat_no_ch == kNoteOffMdId )
  2597. _cmDspPicadaeSendNote( ctx, inst );
  2598. }
  2599. }
  2600. break;
  2601. case kAllOffPcId:
  2602. printf("Picadae All Off\n");
  2603. _cmDspPicadaReset( ctx, inst );
  2604. break;
  2605. default:
  2606. // store
  2607. cmDspSetEvent(ctx,inst,evt);
  2608. break;
  2609. }
  2610. return kOkDspRC;
  2611. }
  2612. cmDspClass_t* cmPicadaeClassCons( cmDspCtx_t* ctx )
  2613. {
  2614. cmDspClassSetup(&_cmPicadaeDC,ctx,"Picadae",
  2615. NULL,
  2616. _cmDspPicadaeAlloc,
  2617. NULL,
  2618. _cmDspPicadaeReset,
  2619. NULL,
  2620. _cmDspPicadaeRecv,
  2621. NULL,
  2622. NULL,
  2623. "Picadaesynth Mapper");
  2624. return &_cmPicadaeDC;
  2625. }
  2626. //------------------------------------------------------------------------------------------------------------
  2627. //)
  2628. //( { label:cmDspRecdPlay file_desc:"Record audio segments from a live perfromance and play them back at a later time" kw:[sunit] }
  2629. enum
  2630. {
  2631. kChCntPrId,
  2632. kFnPrId,
  2633. kSecsPrId,
  2634. kMaxLaSecsPrId,
  2635. kCurLaSecsPrId,
  2636. kFadeRatePrId,
  2637. kScInitLocIdxPrId,
  2638. kScLocIdxPrId,
  2639. kCmdPrId,
  2640. kInAudioBasePrId
  2641. };
  2642. cmDspClass_t _cmRecdPlayDC;
  2643. typedef struct
  2644. {
  2645. cmDspInst_t inst;
  2646. cmRecdPlay* rcdply;
  2647. cmScH_t scH;
  2648. unsigned onSymId;
  2649. unsigned offSymId;
  2650. unsigned audioOutBaseId;
  2651. unsigned chCnt;
  2652. //unsigned scLocIdx;
  2653. } cmDspRecdPlay_t;
  2654. cmDspRC_t _cmDspRecdPlayParseRsrc( cmDspCtx_t* ctx, cmDspInst_t* inst, cmRecdPlay* rcdply )
  2655. {
  2656. cmDspRC_t rc = kOkDspRC;
  2657. const cmChar_t* path = NULL;
  2658. // read the 'recdplay' audio file path
  2659. if( cmDspRsrcString( ctx->dspH, &path, "recdPlayPath", NULL ) != kOkDspRC )
  2660. {
  2661. cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The 'recdPlayPath' resource string was not found.");
  2662. }
  2663. if( path == NULL )
  2664. path = "";
  2665. cmJsonH_t jsH = cmDspSysPgmRsrcHandle(ctx->dspH);
  2666. cmJsonNode_t* jnp = cmJsonFindValue(jsH,"recdPlay",NULL, kArrayTId);
  2667. if( jnp == NULL || cmJsonIsArray(jnp)==false )
  2668. {
  2669. // this is really a warning - the object does not require preloaded segments.
  2670. cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"Warning: The 'recdPlay' resource used to define pre-loaded segments was not found.");
  2671. return kOkDspRC;
  2672. }
  2673. unsigned n = cmJsonChildCount(jnp);
  2674. unsigned i;
  2675. // for each 'recdplay' segment record
  2676. for(i=0; i<n && rc==kOkDspRC; ++i)
  2677. {
  2678. cmJsonNode_t* cnp = cmJsonArrayElement(jnp,i);
  2679. const cmChar_t* label = NULL;
  2680. unsigned segSymId = cmInvalidId;
  2681. const cmChar_t* errLabel = NULL;
  2682. const cmChar_t* fn = NULL;
  2683. // read the ith segment record
  2684. if( cmJsonMemberValues(cnp,&errLabel,
  2685. "label", kStringTId, &label,
  2686. "file", kStringTId, &fn,
  2687. NULL) != kOkJsRC )
  2688. {
  2689. rc = cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The record at index %i in the 'recdPlay' pre-loaded segment list could not be parsed.",i);
  2690. goto errLabel;
  2691. }
  2692. // find or generate the symbol id for the segment label symbol
  2693. if((segSymId = cmSymTblRegisterSymbol(ctx->stH,label)) == cmInvalidId )
  2694. {
  2695. rc = cmDspInstErr(ctx,inst,kSymNotFoundDspRC,"The 'recdPlay' pre-load segment symbol '%s' could not be found or registered.",cmStringNullGuard(label));
  2696. goto errLabel;
  2697. }
  2698. // create the full path name for the segment audio file
  2699. if((fn = cmFsMakeFn( path, fn, NULL, NULL )) == NULL )
  2700. {
  2701. rc = cmDspInstErr(ctx,inst,kFileSysFailDspRC,"The 'recdPlay' file name '%s/%s' could not be generated.",cmStringNullGuard(path),cmStringNullGuard(fn));
  2702. goto errLabel;
  2703. }
  2704. // pre-load the segment
  2705. if( cmRecdPlayInsertRecord(rcdply,segSymId,fn) != cmOkRC )
  2706. rc = cmDspInstErr(ctx,inst,kSubSysFailDspRC,"The 'recdPlay' segment label:'%s' file:'%s' could not be loaded.",cmStringNullGuard(label),cmStringNullGuard(fn));
  2707. cmFsFreeFn(fn);
  2708. }
  2709. errLabel:
  2710. return rc;
  2711. }
  2712. cmDspRC_t _cmDspRecdPlayOpenScore( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2713. {
  2714. cmDspRC_t rc =kOkDspRC;
  2715. const cmChar_t* fn;
  2716. cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
  2717. //p->scLocIdx = 0;
  2718. if((fn = cmDspStrcz(inst,kFnPrId)) == NULL || strlen(fn)==0 )
  2719. return cmDspInstErr(ctx,inst, kInvalidArgDspRC, "No score file name supplied.");
  2720. if( cmScoreInitialize(ctx->cmCtx, &p->scH, fn, cmDspSampleRate(ctx), NULL, 0, NULL, NULL, ctx->stH ) != kOkScRC )
  2721. return cmDspInstErr(ctx,inst, kSubSysFailDspRC, "Unable to open the score '%s'.",fn);
  2722. if( cmScoreIsValid(p->scH) )
  2723. {
  2724. unsigned i;
  2725. unsigned markerCnt = cmScoreMarkerLabelCount(p->scH);
  2726. double initFragSecs = cmDspDouble(inst,kSecsPrId);
  2727. double maxLaSecs = cmDspDouble(inst,kMaxLaSecsPrId);
  2728. double curLaSecs = cmDspDouble(inst,kCurLaSecsPrId);
  2729. printf("2 max la secs:%f\n",cmDspDouble(inst,kMaxLaSecsPrId));
  2730. if((p->rcdply = cmRecdPlayAlloc(ctx->cmProcCtx, NULL, cmDspSampleRate(ctx), markerCnt, p->chCnt, initFragSecs, maxLaSecs, curLaSecs)) == NULL)
  2731. return cmErrMsg(&inst->classPtr->err,kSubSysFailDspRC,"Unable to create the internal recorder-player object.");
  2732. for(i=0; i<markerCnt; ++i)
  2733. cmRecdPlayRegisterFrag(p->rcdply,i, cmScoreMarkerLabelSymbolId(p->scH,i ));
  2734. if((rc = _cmDspRecdPlayParseRsrc(ctx,inst,p->rcdply)) != kOkDspRC )
  2735. rc = cmDspInstErr(ctx,inst,kInstResetFailDspRC,"The 'recdplay' segment pre-load failed.");
  2736. //p->scLocIdx = cmDspUInt(inst,kScInitLocIdxPrId);
  2737. }
  2738. return rc;
  2739. }
  2740. cmDspInst_t* _cmDspRecdPlayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2741. {
  2742. if( va_cnt < 1 )
  2743. {
  2744. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'RecdPlay' constructor must have a count of input ports.");
  2745. return NULL;
  2746. }
  2747. va_list vl1;
  2748. va_copy(vl1,vl);
  2749. int chCnt = va_arg(vl,int);
  2750. unsigned audioOutBase = kInAudioBasePrId + chCnt;
  2751. cmDspRecdPlay_t* p = cmDspInstAllocV(cmDspRecdPlay_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  2752. 1, "chs", kChCntPrId, 0,0, kUIntDsvFl | kReqArgDsvFl, "channel count.",
  2753. 1, "fn", kFnPrId, 0,0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score file." ,
  2754. 1, "secs", kSecsPrId, 0,0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Initial fragment allocation in seconds.",
  2755. 1, "maxla", kMaxLaSecsPrId, 0,0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Maximum look-ahead buffer in seconds.",
  2756. 1, "curla", kCurLaSecsPrId, 0,0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Current look-head buffer in seconds.",
  2757. 1, "frate", kFadeRatePrId, 0,0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Fade rate in dB per second.",
  2758. 1, "initIdx",kScInitLocIdxPrId,0,0,kInDsvFl | kUIntDsvFl, "Score search start location.",
  2759. 1, "index", kScLocIdxPrId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower location index.",
  2760. 1, "cmd", kCmdPrId, 0,0, kInDsvFl | kSymDsvFl, "on=reset off=stop.",
  2761. chCnt, "in", kInAudioBasePrId,0,1, kInDsvFl | kAudioBufDsvFl, "Audio input",
  2762. chCnt, "out", audioOutBase, 0,1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  2763. 0 );
  2764. va_end(vl1);
  2765. p->onSymId = cmSymTblId(ctx->stH,"on");
  2766. p->offSymId = cmSymTblId(ctx->stH,"off");
  2767. p->audioOutBaseId = audioOutBase;
  2768. p->chCnt = chCnt;
  2769. //p->scLocIdx = 0;
  2770. printf("0 max la secs:%f\n",cmDspDouble(&p->inst,kMaxLaSecsPrId));
  2771. cmDspSetDefaultDouble(ctx,&p->inst, kSecsPrId, 0.0, 10.0 );
  2772. cmDspSetDefaultDouble(ctx,&p->inst, kMaxLaSecsPrId,0.0, 2.0);
  2773. cmDspSetDefaultDouble(ctx,&p->inst, kCurLaSecsPrId,0.0, 0.1);
  2774. cmDspSetDefaultDouble(ctx,&p->inst, kFadeRatePrId, 0.0, 1.0);
  2775. cmDspSetDefaultUInt( ctx,&p->inst, kScInitLocIdxPrId,0,0);
  2776. printf("1 max la secs:%f\n",cmDspDouble(&p->inst,kMaxLaSecsPrId));
  2777. return &p->inst;
  2778. }
  2779. cmDspRC_t _cmDspRecdPlayFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2780. {
  2781. cmDspRC_t rc = kOkDspRC;
  2782. cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
  2783. cmRecdPlayFree(&p->rcdply);
  2784. cmScoreFinalize(&p->scH);
  2785. return rc;
  2786. }
  2787. cmDspRC_t _cmDspRecdPlayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2788. {
  2789. cmDspRC_t rc;
  2790. if((rc = _cmDspRecdPlayOpenScore(ctx,inst)) == kOkDspRC )
  2791. cmDspApplyAllDefaults(ctx,inst);
  2792. return rc;
  2793. }
  2794. cmDspRC_t _cmDspRecdPlayExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2795. {
  2796. cmDspRC_t rc = kOkDspRC;
  2797. cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
  2798. const cmSample_t* x[ p->chCnt ];
  2799. cmSample_t* y[ p->chCnt ];
  2800. unsigned n = 0;
  2801. unsigned i;
  2802. unsigned actChCnt = 0;
  2803. for(i=0; i<p->chCnt; ++i)
  2804. {
  2805. if( cmDspIsAudioInputConnected(ctx,inst,kInAudioBasePrId+i) == false )
  2806. {
  2807. x[i] = NULL;
  2808. y[i] = NULL;
  2809. }
  2810. else
  2811. {
  2812. if( i==0 )
  2813. n = cmDspAudioBufSmpCount(ctx,inst,kInAudioBasePrId+i,0);
  2814. else
  2815. {
  2816. assert( n == cmDspAudioBufSmpCount(ctx,inst,kInAudioBasePrId+i,0));
  2817. }
  2818. x[i] = cmDspAudioBuf(ctx,inst,kInAudioBasePrId+i,0);
  2819. if( x[i] != NULL )
  2820. {
  2821. y[i] = cmDspAudioBuf(ctx,inst,p->audioOutBaseId+i,0);
  2822. if( y[i] != NULL )
  2823. {
  2824. assert( n == cmDspAudioBufSmpCount(ctx,inst,p->audioOutBaseId+i,0));
  2825. cmVOS_Zero(y[i],n);
  2826. actChCnt += 1;
  2827. }
  2828. }
  2829. }
  2830. }
  2831. cmRecdPlayExec(p->rcdply,x,y,actChCnt,n);
  2832. return rc;
  2833. }
  2834. cmDspRC_t _cmDspRecdPlayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2835. {
  2836. cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
  2837. if( p->rcdply == NULL )
  2838. return kOkDspRC;
  2839. cmDspSetEvent(ctx,inst,evt);
  2840. switch( evt->dstVarId )
  2841. {
  2842. case kCmdPrId:
  2843. if( cmDspSymbol(inst,kCmdPrId) == p->onSymId )
  2844. {
  2845. printf("rewind\n");
  2846. cmRecdPlayRewind(p->rcdply);
  2847. //p->scLocIdx = cmDspUInt(inst,kScInitLocIdxPrId);
  2848. }
  2849. else
  2850. if( cmDspSymbol(inst,kCmdPrId) == p->offSymId )
  2851. {
  2852. }
  2853. break;
  2854. case kCurLaSecsPrId:
  2855. cmRecdPlaySetLaSecs(p->rcdply, cmDspDouble(inst,kCurLaSecsPrId));
  2856. break;
  2857. case kScInitLocIdxPrId:
  2858. printf("init-idx:%i\n",cmDspUInt(inst,kScInitLocIdxPrId));
  2859. break;
  2860. case kScLocIdxPrId:
  2861. {
  2862. unsigned endScLocIdx = cmDspUInt(inst,kScLocIdxPrId);
  2863. if( endScLocIdx < cmDspUInt(inst,kScInitLocIdxPrId) )
  2864. break;
  2865. cmScoreLoc_t* loc = cmScoreLoc(p->scH, endScLocIdx );
  2866. if( loc == NULL )
  2867. break;
  2868. cmScoreMarker_t* mp = loc->markList;
  2869. for(; mp!=NULL; mp=mp->link)
  2870. switch( mp->markTypeId )
  2871. {
  2872. case kRecdBegScMId:
  2873. printf("recd-beg %s\n",cmSymTblLabel(ctx->stH,mp->labelSymId));
  2874. cmRecdPlayBeginRecord(p->rcdply, mp->labelSymId );
  2875. break;
  2876. case kRecdEndScMId:
  2877. printf("recd-end %s\n",cmSymTblLabel(ctx->stH,mp->labelSymId));
  2878. cmRecdPlayEndRecord(p->rcdply, mp->labelSymId );
  2879. break;
  2880. case kPlayBegScMId:
  2881. printf("play-beg\n");
  2882. cmRecdPlayBeginPlay(p->rcdply, mp->labelSymId );
  2883. break;
  2884. case kPlayEndScMId:
  2885. printf("play-end\n");
  2886. cmRecdPlayEndPlay(p->rcdply, mp->labelSymId );
  2887. break;
  2888. case kFadeScMId:
  2889. printf("fade-beg\n");
  2890. cmRecdPlayBeginFade(p->rcdply, mp->labelSymId, cmDspDouble(inst,kFadeRatePrId) );
  2891. break;
  2892. default:
  2893. break;
  2894. }
  2895. //p->scLocIdx = endScLocIdx+1;
  2896. }
  2897. break;
  2898. }
  2899. return kOkDspRC;
  2900. }
  2901. cmDspClass_t* cmRecdPlayClassCons( cmDspCtx_t* ctx )
  2902. {
  2903. cmDspClassSetup(&_cmRecdPlayDC,ctx,"RecdPlay",
  2904. NULL,
  2905. _cmDspRecdPlayAlloc,
  2906. _cmDspRecdPlayFree,
  2907. _cmDspRecdPlayReset,
  2908. _cmDspRecdPlayExec,
  2909. _cmDspRecdPlayRecv,
  2910. NULL,
  2911. NULL,
  2912. "Score controlled live recorder/player");
  2913. return &_cmRecdPlayDC;
  2914. }
  2915. //------------------------------------------------------------------------------------------------------------
  2916. //)
  2917. //( { label:cmDspGoertzel file_desc:"Goertzel tone detection filter" kw:[sunit] }
  2918. enum
  2919. {
  2920. kHopFactGrId,
  2921. kInGrId,
  2922. kHzBaseGrId,
  2923. };
  2924. cmDspClass_t _cmGoertzelDC;
  2925. typedef struct
  2926. {
  2927. cmDspInst_t inst;
  2928. cmGoertzel* g;
  2929. double outPhs;
  2930. unsigned outBaseGrId;
  2931. unsigned chCnt;
  2932. } cmDspGoertzel_t;
  2933. cmDspInst_t* _cmDspGoertzelAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2934. {
  2935. if( va_cnt !=3 )
  2936. {
  2937. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'Goertzel' constructor must have two arguments: a channel count and frequency array.");
  2938. return NULL;
  2939. }
  2940. va_list vl1;
  2941. va_copy(vl1,vl);
  2942. unsigned hopFact = va_arg(vl,unsigned);
  2943. int chCnt = va_arg(vl,int);
  2944. double* hzV = va_arg(vl,double*);
  2945. unsigned outBaseGrId = kHzBaseGrId + chCnt;
  2946. unsigned i;
  2947. cmDspGoertzel_t* p = cmDspInstAllocV(cmDspGoertzel_t,ctx,classPtr,instSymId,id,storeSymId,1,vl1,
  2948. 1, "hop", kHopFactGrId, 0,0, kInDsvFl | kDoubleDsvFl, "Hop factor",
  2949. 1, "in", kInGrId, 0,1, kInDsvFl | kAudioBufDsvFl, "Audio input",
  2950. chCnt, "hz", kHzBaseGrId, 0,0, kInDsvFl | kDoubleDsvFl, "Hz input.",
  2951. chCnt, "out", outBaseGrId, 0,1, kOutDsvFl | kDoubleDsvFl, "Detector output",
  2952. 0 );
  2953. va_end(vl1);
  2954. p->outBaseGrId = outBaseGrId;
  2955. p->chCnt = chCnt;
  2956. p->g = cmGoertzelAlloc(ctx->cmProcCtx, NULL, 0, NULL, 0,0,0,0 );
  2957. cmDspSetDefaultUInt(ctx,&p->inst, kHopFactGrId, 0, cmMax(hopFact,1));
  2958. for(i=0; i<chCnt; ++i)
  2959. cmDspSetDefaultDouble(ctx,&p->inst, kHzBaseGrId+i, 0.0, hzV[i] );
  2960. return &p->inst;
  2961. }
  2962. cmDspRC_t _cmDspGoertzelSetup( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2963. {
  2964. cmDspRC_t rc = kOkDspRC;
  2965. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2966. unsigned hopFact = cmDspUInt(inst,kHopFactGrId);
  2967. unsigned procSmpCnt = cmDspAudioBufSmpCount(ctx,inst,kInGrId,0);
  2968. unsigned wndSmpCnt = procSmpCnt * hopFact;
  2969. double fcHzV[ p->chCnt ];
  2970. unsigned i;
  2971. for(i=0; i<p->chCnt; ++i)
  2972. {
  2973. double hz;
  2974. if( p->g->ch == NULL || p->g->ch[i].hz == 0 )
  2975. hz = cmDspDouble(inst,kHzBaseGrId);
  2976. else
  2977. hz = p->g->ch[i].hz;
  2978. fcHzV[i] = hz;
  2979. }
  2980. if( cmGoertzelInit(p->g,cmDspSysSampleRate(ctx->dspH),fcHzV,p->chCnt,procSmpCnt,procSmpCnt,wndSmpCnt) != cmOkRC )
  2981. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to initialize the internal Goertzel detector.");
  2982. return rc;
  2983. }
  2984. cmDspRC_t _cmDspGoertzelFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2985. {
  2986. cmDspRC_t rc = kOkDspRC;
  2987. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2988. cmGoertzelFree(&p->g);
  2989. return rc;
  2990. }
  2991. cmDspRC_t _cmDspGoertzelReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2992. {
  2993. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2994. cmDspApplyAllDefaults(ctx,inst);
  2995. p->outPhs = 0;
  2996. return _cmDspGoertzelSetup(ctx, inst );
  2997. }
  2998. cmDspRC_t _cmDspGoertzelExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2999. {
  3000. cmDspRC_t rc = kOkDspRC;
  3001. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  3002. const cmSample_t* x = cmDspAudioBuf(ctx,inst,kInGrId,0);
  3003. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kInGrId,0);
  3004. double outMs = 50.0;
  3005. double outPhsMax = outMs * cmDspSysSampleRate(ctx->dspH) / 1000.0;
  3006. double outV[ p->chCnt ];
  3007. unsigned i;
  3008. if( x != NULL )
  3009. {
  3010. cmGoertzelExec(p->g,x,n,outV,p->chCnt);
  3011. p->outPhs += n;
  3012. if( p->outPhs > outPhsMax )
  3013. {
  3014. while( p->outPhs > outPhsMax )
  3015. p->outPhs -= outPhsMax;
  3016. for(i=0; i<p->chCnt; ++i)
  3017. {
  3018. cmDspSetDouble(ctx,inst,p->outBaseGrId+i,outV[i]);
  3019. //printf("%f ",outV[i]);
  3020. }
  3021. //printf("\n");
  3022. }
  3023. }
  3024. return rc;
  3025. }
  3026. cmDspRC_t _cmDspGoertzelRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3027. {
  3028. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  3029. cmDspSetEvent(ctx,inst,evt);
  3030. if( kHzBaseGrId <= evt->dstVarId && evt->dstVarId < kHzBaseGrId+p->chCnt )
  3031. cmGoertzelSetFcHz(p->g, evt->dstVarId - kHzBaseGrId, cmDspDouble(inst,evt->dstVarId));
  3032. else
  3033. {
  3034. if( evt->dstVarId==kHopFactGrId )
  3035. {
  3036. _cmDspGoertzelSetup(ctx,inst);
  3037. }
  3038. }
  3039. return kOkDspRC;
  3040. }
  3041. cmDspClass_t* cmGoertzelClassCons( cmDspCtx_t* ctx )
  3042. {
  3043. cmDspClassSetup(&_cmGoertzelDC,ctx,"Goertzel",
  3044. NULL,
  3045. _cmDspGoertzelAlloc,
  3046. _cmDspGoertzelFree,
  3047. _cmDspGoertzelReset,
  3048. _cmDspGoertzelExec,
  3049. _cmDspGoertzelRecv,
  3050. NULL,
  3051. NULL,
  3052. "Goertzel Tone Detector Filter");
  3053. return &_cmGoertzelDC;
  3054. }
  3055. //------------------------------------------------------------------------------------------------------------
  3056. //)
  3057. //( { label:cmDspSyncRecd file_desc:"Time align a MIDI and associated audio recording" kw:[sunit] }
  3058. enum
  3059. {
  3060. kRecdDirSrId,
  3061. kSrFnSrId,
  3062. kAfSrId,
  3063. kBitsSrId,
  3064. kCmdSrId,
  3065. kStatusSrId,
  3066. kD0SrId,
  3067. kD1SrId,
  3068. kSecSrId,
  3069. kNSecSrId,
  3070. kAinBaseSrId
  3071. };
  3072. cmDspClass_t _cmSyncRecdDC;
  3073. typedef struct
  3074. {
  3075. cmDspInst_t inst;
  3076. unsigned chCnt;
  3077. cmTimeSpec_t ats;
  3078. cmSyncRecdH_t srH;
  3079. unsigned openSymId;
  3080. unsigned closeSymId;
  3081. const cmChar_t* aFn;
  3082. const cmChar_t* srFn;
  3083. unsigned smpIdx;
  3084. } cmDspSyncRecd_t;
  3085. cmDspRC_t _cmDspSyncRecdCreateFile( cmDspCtx_t* ctx, cmDspInst_t* inst )
  3086. {
  3087. cmDspSyncRecd_t* p = (cmDspSyncRecd_t*)inst;
  3088. const cmChar_t* aFn = cmDspStrcz(inst,kAfSrId);
  3089. const cmChar_t* srFn = cmDspStrcz(inst,kSrFnSrId);
  3090. const cmChar_t* dir = cmDspStrcz(inst,kRecdDirSrId);
  3091. if( !cmFsIsDir(dir) )
  3092. return cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"'%s' is not a valid directory.",cmStringNullGuard(dir));
  3093. cmMemPtrFree(&p->aFn);
  3094. if( cmFsGenFn(dir,aFn,"aiff",&p->aFn) != kOkFsRC )
  3095. return cmDspInstErr(ctx,&p->inst,kFileSysFailDspRC,"Audio file name generation failed for dir='%s' and prefix='%s'.",cmStringNullGuard(dir),cmStringNullGuard(aFn));
  3096. cmMemPtrFree(&p->srFn);
  3097. if( cmFsGenFn(dir,srFn,"sr",&p->srFn) != kOkFsRC )
  3098. return cmDspInstErr(ctx,&p->inst,kFileSysFailDspRC,"Sync-recd file name generation failed for dir='%s' and prefix='%s'.",cmStringNullGuard(dir),cmStringNullGuard(srFn));
  3099. unsigned bits = cmDspUInt(inst,kBitsSrId);
  3100. if( cmSyncRecdCreate( ctx->cmCtx, &p->srH, p->srFn, p->aFn, cmDspSampleRate(ctx), p->chCnt, bits ) != kOkSyRC )
  3101. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Sync-recd file create failed for '%s'.",p->srFn);
  3102. p->smpIdx = 0;
  3103. return kOkDspRC;
  3104. }
  3105. cmDspInst_t* _cmDspSyncRecdAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3106. {
  3107. cmDspSyncRecd_t* p = cmDspInstAllocV(cmDspSyncRecd_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3108. 1, "dir", kRecdDirSrId, 0,0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Recording directory.",
  3109. 1, "srfn", kSrFnSrId, 0,0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "SyncRecd file prefix.",
  3110. 1, "afn", kAfSrId, 0,0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Audio file prefix.",
  3111. 1, "bits", kBitsSrId, 0,0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Audio file bits per sample.",
  3112. 1, "cmd", kCmdSrId, 0,0, kInDsvFl | kSymDsvFl, "Command: open | close",
  3113. 1, "status", kStatusSrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI status",
  3114. 1, "d0", kD0SrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI d0",
  3115. 1, "d1", kD1SrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI d1",
  3116. 1, "sec", kSecSrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI Timestamp Seconds",
  3117. 1, "nsec", kNSecSrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI Timestamp Nanoseconds",
  3118. 2, "ain", kAinBaseSrId, 0,1, kInDsvFl | kAudioBufDsvFl, "Audio Input",
  3119. 0 );
  3120. p->chCnt = 2;
  3121. p->openSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"open");
  3122. p->closeSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"close");
  3123. cmDspSetDefaultUInt(ctx,&p->inst,kBitsSrId,0,16);
  3124. return &p->inst;
  3125. }
  3126. cmDspRC_t _cmDspSyncRecdFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3127. {
  3128. cmDspRC_t rc = kOkDspRC;
  3129. cmDspSyncRecd_t* p = (cmDspSyncRecd_t*)inst;
  3130. cmMemPtrFree(&p->aFn);
  3131. cmMemPtrFree(&p->srFn);
  3132. cmSyncRecdFinal(&p->srH);
  3133. return rc;
  3134. }
  3135. cmDspRC_t _cmDspSyncRecdReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3136. {
  3137. cmDspRC_t rc = kOkDspRC;
  3138. cmDspApplyAllDefaults(ctx,inst);
  3139. return rc;
  3140. }
  3141. cmDspRC_t _cmDspSyncRecdExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3142. {
  3143. cmDspRC_t rc = kOkDspRC;
  3144. cmDspSyncRecd_t* p = (cmDspSyncRecd_t*)inst;
  3145. const cmSample_t* x[ p->chCnt ];
  3146. unsigned n = 0;
  3147. unsigned i;
  3148. //const cmTimeSpec_t* ts = &ctx->ctx->oTimeStamp;
  3149. //printf("SR: %ld %ld\n",ts->tv_sec,ts->tv_nsec);
  3150. p->ats = ctx->ctx->iTimeStamp;
  3151. for(i=0; i<p->chCnt; ++i)
  3152. {
  3153. if( i==0 )
  3154. n = cmDspAudioBufSmpCount(ctx,inst,kAinBaseSrId+i,0);
  3155. else
  3156. { assert( n == cmDspAudioBufSmpCount(ctx,inst,kAinBaseSrId+i,0)); }
  3157. x[i] = cmDspAudioBuf(ctx,inst,kAinBaseSrId+i,0);
  3158. }
  3159. if( n>0 && cmSyncRecdIsValid(p->srH ) )
  3160. if( cmSyncRecdAudioWrite( p->srH, &ctx->ctx->iTimeStamp, p->smpIdx, x, p->chCnt, n ) != kOkSyRC )
  3161. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Sync-recd audio update failed.");
  3162. p->smpIdx += n;
  3163. return rc;
  3164. }
  3165. cmDspRC_t _cmDspSyncRecdRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3166. {
  3167. cmDspRC_t rc = kOkDspRC;
  3168. cmDspSyncRecd_t* p = (cmDspSyncRecd_t*)inst;
  3169. cmDspSetEvent(ctx,inst,evt);
  3170. switch( evt->dstVarId )
  3171. {
  3172. case kStatusSrId:
  3173. if(cmMidiIsChStatus( cmDspUInt(inst,kStatusSrId) ) )
  3174. {
  3175. cmTimeSpec_t ts;
  3176. ts.tv_sec = cmDspUInt(inst,kSecSrId);
  3177. ts.tv_nsec = cmDspUInt(inst,kNSecSrId);
  3178. //printf("%i %i\n",cmDspUInt(inst,kD1SrId),cmTimeElapsedMicros(&ts,&p->ats));
  3179. if( cmSyncRecdIsValid(p->srH ) )
  3180. if( cmSyncRecdMidiWrite(p->srH, &ts, cmDspUInt(inst,kStatusSrId), cmDspUInt(inst,kD0SrId), cmDspUInt(inst,kD1SrId) ) != kOkSyRC )
  3181. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Sync-recd MIDI update failed.");
  3182. }
  3183. break;
  3184. case kCmdSrId:
  3185. {
  3186. unsigned cmdId = cmDspSymbol(inst,kCmdSrId);
  3187. if( cmdId == p->openSymId )
  3188. rc = _cmDspSyncRecdCreateFile(ctx,inst);
  3189. else
  3190. if( cmdId == p->closeSymId && cmSyncRecdIsValid(p->srH))
  3191. {
  3192. cmSyncRecdFinal(&p->srH);
  3193. //cmSyncRecdTest(ctx->cmCtx);
  3194. }
  3195. }
  3196. break;
  3197. }
  3198. return rc;
  3199. }
  3200. cmDspClass_t* cmSyncRecdClassCons( cmDspCtx_t* ctx )
  3201. {
  3202. cmDspClassSetup(&_cmSyncRecdDC,ctx,"SyncRecd",
  3203. NULL,
  3204. _cmDspSyncRecdAlloc,
  3205. _cmDspSyncRecdFree,
  3206. _cmDspSyncRecdReset,
  3207. _cmDspSyncRecdExec,
  3208. _cmDspSyncRecdRecv,
  3209. NULL,
  3210. NULL,
  3211. "Synchronized Audio and MIDI recorder.");
  3212. return &_cmSyncRecdDC;
  3213. }
  3214. //------------------------------------------------------------------------------------------------------------
  3215. //)
  3216. //( { label:cmDspTakeSeqBldr file_desc:"User interface unit for creating a single sequence from multiple, score aligned, MIDI fragments." kw:[sunit] }
  3217. enum
  3218. {
  3219. kFnTsbId,
  3220. kBldrTsbId,
  3221. kSelTsbId,
  3222. kRefreshTsbId,
  3223. kSendTsbId
  3224. };
  3225. cmDspClass_t _cmTakeSeqBldrDC;
  3226. typedef struct
  3227. {
  3228. cmDspInst_t inst;
  3229. cmTakeSeqBldrH_t h;
  3230. bool errFl;
  3231. } cmDspTakeSeqBldr_t;
  3232. cmDspInst_t* _cmDspTakeSeqBldrAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3233. {
  3234. cmDspTakeSeqBldr_t* p = cmDspInstAllocV(cmDspTakeSeqBldr_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3235. 1, "fn", kFnTsbId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score Tracking file.",
  3236. 1, "bldr", kBldrTsbId, 0, 0, kOutDsvFl | kPtrDsvFl, "Bldr Ref",
  3237. 1, "sel", kSelTsbId, 0, 0, kOutDsvFl | kUIntDsvFl, "Selected score element location index.",
  3238. 1, "refresh",kRefreshTsbId, 0, 0, kOutDsvFl | kUIntDsvFl, "Refresh",
  3239. 1, "send", kSendTsbId, 0, 0, kInDsvFl | kTypeDsvMask, "Resend last selected score element location.",
  3240. 0 );
  3241. p->errFl = false;
  3242. cmDspSetDefaultInt( ctx, &p->inst, kRefreshTsbId, 0, 0);
  3243. if( cmTakeSeqBldrAlloc(ctx->cmCtx, &p->h ) != kOkTsbRC )
  3244. cmErrMsg(&p->inst.classPtr->err, kSubSysFailDspRC, "Allocate TaskSeqBldr object.");
  3245. else
  3246. {
  3247. cmDspUiTakeSeqBldrCreate(ctx,&p->inst,kFnTsbId,kBldrTsbId,kSelTsbId,kRefreshTsbId);
  3248. }
  3249. return &p->inst;
  3250. }
  3251. cmDspRC_t _cmDspTakeSeqBldrSetup( cmDspCtx_t* ctx, cmDspInst_t* inst )
  3252. {
  3253. cmDspRC_t rc = kOkDspRC;
  3254. cmDspTakeSeqBldr_t* p = (cmDspTakeSeqBldr_t*)inst;
  3255. if( cmTakeSeqBldrInitialize(p->h, cmDspStrcz(inst,kFnTsbId) ) != kOkTsbRC )
  3256. {
  3257. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to initialize the internal TakeSeqBldr object with %s.",cmStringNullGuard(cmDspStrcz(inst,kFnTsbId)));
  3258. }
  3259. else
  3260. {
  3261. cmDspSetPtr(ctx,inst,kBldrTsbId,p->h.h);
  3262. p->errFl = false;
  3263. }
  3264. return rc;
  3265. }
  3266. cmDspRC_t _cmDspTakeSeqBldrFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3267. {
  3268. cmDspRC_t rc = kOkDspRC;
  3269. cmDspTakeSeqBldr_t* p = (cmDspTakeSeqBldr_t*)inst;
  3270. cmTakeSeqBldrFree(&p->h);
  3271. return rc;
  3272. }
  3273. cmDspRC_t _cmDspTakeSeqBldrReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3274. {
  3275. //cmDspTakeSeqBldr_t* p = (cmDspTakeSeqBldr_t*)inst;
  3276. cmDspApplyAllDefaults(ctx,inst);
  3277. return _cmDspTakeSeqBldrSetup(ctx, inst );
  3278. }
  3279. cmDspRC_t _cmDspTakeSeqBldrExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3280. {
  3281. cmDspRC_t rc = kOkDspRC;
  3282. return rc;
  3283. }
  3284. cmDspRC_t _cmDspTakeSeqBldrRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3285. {
  3286. // not matter what arrives at the 'send' input ....
  3287. if( evt->dstVarId == kSendTsbId )
  3288. {
  3289. // send the last score loc
  3290. unsigned selIdx;
  3291. if((selIdx = cmDspUInt(inst,kSelTsbId)) != cmInvalidIdx )
  3292. cmDspSetUInt(ctx,inst,kSelTsbId, selIdx );
  3293. return kOkDspRC;
  3294. }
  3295. cmDspSetEvent(ctx,inst,evt);
  3296. switch(evt->dstVarId)
  3297. {
  3298. case kFnTsbId:
  3299. _cmDspMidiFilePlayOpen(ctx, inst );
  3300. break;
  3301. }
  3302. return kOkDspRC;
  3303. }
  3304. cmDspClass_t* cmTakeSeqBldrClassCons( cmDspCtx_t* ctx )
  3305. {
  3306. cmDspClassSetup(&_cmTakeSeqBldrDC,ctx,"TakeSeqBldr",
  3307. NULL,
  3308. _cmDspTakeSeqBldrAlloc,
  3309. _cmDspTakeSeqBldrFree,
  3310. _cmDspTakeSeqBldrReset,
  3311. _cmDspTakeSeqBldrExec,
  3312. _cmDspTakeSeqBldrRecv,
  3313. NULL,
  3314. NULL,
  3315. "TakeSeqBldr");
  3316. return &_cmTakeSeqBldrDC;
  3317. }
  3318. //------------------------------------------------------------------------------------------------------------
  3319. //)
  3320. //( { label:cmDspTakeSeqRend file_desc:"User interface unit for graphically rendering the MIDI sequences created by cmDspTakeSeqBldr." kw:[sunit] }
  3321. enum
  3322. {
  3323. kBldrTsrId,
  3324. kRefreshTsrId,
  3325. kCmdTsrId,
  3326. kSelTsrId,
  3327. kStatusTsrId,
  3328. kD0TsrId,
  3329. kD1TsrId,
  3330. kSmpIdxTsrId
  3331. };
  3332. cmDspClass_t _cmTakeSeqRendDC;
  3333. typedef struct
  3334. {
  3335. cmDspInst_t inst;
  3336. cmTakeSeqBldrH_t h;
  3337. unsigned startSymId;
  3338. unsigned stopSymId;
  3339. unsigned contSymId;
  3340. unsigned onSymId;
  3341. unsigned offSymId;
  3342. bool errFl;
  3343. } cmDspTakeSeqRend_t;
  3344. cmDspInst_t* _cmDspTakeSeqRendAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3345. {
  3346. cmDspTakeSeqRend_t* p = cmDspInstAllocV(cmDspTakeSeqRend_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3347. 1, "bldr", kBldrTsrId, 0, 0, kInDsvFl | kPtrDsvFl | kOptArgDsvFl, "Take Sequene Builder Ref",
  3348. 1, "refresh",kRefreshTsrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Refresh",
  3349. 1, "cmd", kCmdTsrId, 0, 0, kInDsvFl | kSymDsvFl, "start | stop | continue" ,
  3350. 1, "sel", kSelTsrId, 0, 0, kInDsvFl | kUIntDsvFl, "Selected score element location index input.",
  3351. 1, "status", kStatusTsrId, 0, 0, kOutDsvFl | kIntDsvFl, "Status value output",
  3352. 1, "d0", kD0TsrId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 0" ,
  3353. 1, "d1", kD1TsrId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 1",
  3354. 1, "smpidx", kSmpIdxTsrId, 0, 0, kOutDsvFl | kUIntDsvFl, "Msg time tag as a sample index.",
  3355. 0 );
  3356. p->startSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"start");
  3357. p->stopSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"stop");
  3358. p->contSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"continue");
  3359. p->onSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"on");
  3360. p->offSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"off");
  3361. p->errFl = false;
  3362. cmDspSetDefaultInt( ctx, &p->inst, kRefreshTsrId, 0, 0);
  3363. cmDspSetDefaultSymbol(ctx, &p->inst, kCmdTsrId, p->stopSymId);
  3364. cmDspSetDefaultInt( ctx, &p->inst, kSmpIdxTsrId, 0, 0);
  3365. cmDspSetDefaultUInt( ctx, &p->inst, kStatusTsrId, 0, 0);
  3366. cmDspSetDefaultUInt( ctx, &p->inst, kD0TsrId, 0, 0);
  3367. cmDspSetDefaultUInt( ctx, &p->inst, kD1TsrId, 0, 0);
  3368. cmDspUiTakeSeqRendCreate(ctx,&p->inst,kBldrTsrId,kRefreshTsrId,kSelTsrId);
  3369. return &p->inst;
  3370. }
  3371. cmDspRC_t _cmDspTakeSeqRendFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3372. {
  3373. cmDspRC_t rc = kOkDspRC;
  3374. return rc;
  3375. }
  3376. cmDspRC_t _cmDspTakeSeqRendReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3377. {
  3378. //cmDspTakeSeqRend_t* p = (cmDspTakeSeqRend_t*)inst;
  3379. cmDspApplyAllDefaults(ctx,inst);
  3380. return kOkDspRC;
  3381. }
  3382. typedef struct
  3383. {
  3384. cmDspCtx_t* ctx;
  3385. cmDspInst_t* inst;
  3386. } _cmDspTakeSeqRendCbArg_t;
  3387. // Called from cmDspTakeSeqRendExec() -> cmTakeSeqRendPlayExec() to
  3388. // deliver MIDI messages which need to be transmitted.
  3389. void _cmDspTakeSeqRendMidiCb( void* arg, const cmTksbEvent_t* e )
  3390. {
  3391. _cmDspTakeSeqRendCbArg_t* a = (_cmDspTakeSeqRendCbArg_t*)arg;
  3392. switch( e->status )
  3393. {
  3394. case kNoteOffMdId:
  3395. case kNoteOnMdId:
  3396. case kCtlMdId:
  3397. //if( !cmMidiIsPedal(e->status,e->d0))
  3398. {
  3399. cmDspSetUInt(a->ctx,a->inst, kSmpIdxTsrId, e->smpIdx);
  3400. cmDspSetUInt(a->ctx,a->inst, kD1TsrId, e->d1);
  3401. cmDspSetUInt(a->ctx,a->inst, kD0TsrId, e->d0);
  3402. cmDspSetUInt(a->ctx,a->inst, kStatusTsrId, e->status);
  3403. }
  3404. break;
  3405. }
  3406. }
  3407. void _cmDspTakeSeqRendPedalsUp( cmDspCtx_t* ctx, cmDspInst_t* inst )
  3408. {
  3409. _cmDspTakeSeqRendCbArg_t a;
  3410. a.ctx = ctx;
  3411. a.inst = inst;
  3412. cmTksbEvent_t e[] =
  3413. {
  3414. { 0, kCtlMdId, kSustainCtlMdId, 0 },
  3415. { 0, kCtlMdId, kPortamentoCtlMdId,0 },
  3416. { 0, kCtlMdId, kSostenutoCtlMdId, 0 },
  3417. { 0, kCtlMdId, kSoftPedalCtlMdId, 0 },
  3418. { 0, kCtlMdId, kLegatoCtlMdId, 0 }
  3419. };
  3420. unsigned n = sizeof(e)/sizeof(e[0]);
  3421. unsigned i;
  3422. for(i=0; i<n; ++i)
  3423. _cmDspTakeSeqRendMidiCb(&a,e+i);
  3424. }
  3425. cmDspRC_t _cmDspTakeSeqRendExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3426. {
  3427. cmDspRC_t rc = kOkDspRC;
  3428. cmDspTakeSeqRend_t* p = (cmDspTakeSeqRend_t*)inst;
  3429. if( cmDspSymbol(inst,kCmdTsrId) != p->stopSymId )
  3430. {
  3431. if( cmTakeSeqBldrIsValid(p->h) == false )
  3432. {
  3433. if( p->errFl==false )
  3434. {
  3435. rc = cmErrMsg(&inst->classPtr->err, kInvalidStateDspRC,"The Take Sequence Builder not been given a valid file.");
  3436. p->errFl = true;
  3437. }
  3438. return rc;
  3439. }
  3440. unsigned sPc = cmDspSamplesPerCycle(ctx);
  3441. _cmDspTakeSeqRendCbArg_t arg;
  3442. arg.inst = inst;
  3443. arg.ctx = ctx;
  3444. // This call may result in multiple callbacks
  3445. // to _cmDspTakeSeqRendMidiCb() from within the function.
  3446. cmTakeSeqBldrPlayExec(p->h,sPc,_cmDspTakeSeqRendMidiCb, &arg );
  3447. }
  3448. return rc;
  3449. }
  3450. cmDspRC_t _cmDspTakeSeqRendRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3451. {
  3452. cmDspTakeSeqRend_t* p = (cmDspTakeSeqRend_t*)inst;
  3453. printf("Recv:%s\n",cmStringNullGuard(cmDspVarLabel(ctx, inst, evt->dstVarId)) );
  3454. cmDspSetEvent(ctx,inst,evt);
  3455. switch(evt->dstVarId)
  3456. {
  3457. case kBldrTsrId:
  3458. p->h.h = cmDspPtr(inst,kBldrTsrId);
  3459. break;
  3460. case kCmdTsrId:
  3461. {
  3462. unsigned symId = cmDspSymbol(inst,kCmdTsrId);
  3463. if( symId == p->onSymId )
  3464. cmDspSetSymbol( ctx, inst, kCmdTsrId, p->startSymId );
  3465. else
  3466. if( symId == p->offSymId )
  3467. cmDspSetSymbol( ctx, inst, kCmdTsrId, p->stopSymId );
  3468. if( cmTakeSeqBldrIsValid(p->h) && cmDspSymbol(inst,kCmdTsrId)==p->startSymId )
  3469. {
  3470. _cmDspTakeSeqRendPedalsUp( ctx, inst );
  3471. cmTakeSeqBldrPlaySeekLoc(p->h, cmInvalidId );
  3472. }
  3473. }
  3474. break;
  3475. case kSelTsrId:
  3476. {
  3477. // seek the playback position to the scLocIdx.
  3478. unsigned scLocIdx = cmDspUInt(inst,kSelTsrId);
  3479. if( cmTakeSeqBldrIsValid(p->h) && cmTakeSeqBldrPlaySeekLoc(p->h, scLocIdx ) != kOkTsbRC )
  3480. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Take Sequence Bldr Seek failed on score location index %i.", scLocIdx);
  3481. }
  3482. break;
  3483. }
  3484. return kOkDspRC;
  3485. }
  3486. cmDspClass_t* cmTakeSeqRendClassCons( cmDspCtx_t* ctx )
  3487. {
  3488. cmDspClassSetup(&_cmTakeSeqRendDC,ctx,"TakeSeqRend",
  3489. NULL,
  3490. _cmDspTakeSeqRendAlloc,
  3491. _cmDspTakeSeqRendFree,
  3492. _cmDspTakeSeqRendReset,
  3493. _cmDspTakeSeqRendExec,
  3494. _cmDspTakeSeqRendRecv,
  3495. NULL,
  3496. NULL,
  3497. "TakeSeqRend");
  3498. return &_cmTakeSeqRendDC;
  3499. }
  3500. //------------------------------------------------------------------------------------------------------------
  3501. //)
  3502. //( { label:cmDspReflectCalc file_desc:"Estimate the time-of-flight of from an acoustic signal from a speaker to a microphone." kw:[sunit] }
  3503. enum
  3504. {
  3505. kLfsrN_RcId,
  3506. kMlsCoeff0RcId,
  3507. kMlsCoeff1RcId,
  3508. kSmpPerChipRcId,
  3509. kRcosBetaRcId,
  3510. kRcosOS_RcId,
  3511. kCarrierHzRcId,
  3512. kAtkDcyMsRcId,
  3513. kPhatAlphaRcId,
  3514. kPhatMultRcId,
  3515. kInRcId,
  3516. kOutRcId,
  3517. };
  3518. cmDspClass_t _cmReflectCalcDC;
  3519. typedef struct
  3520. {
  3521. cmDspInst_t inst;
  3522. cmReflectCalc_t* r;
  3523. } cmDspReflectCalc_t;
  3524. cmDspInst_t* _cmDspReflectCalcAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3525. {
  3526. // if( va_cnt !=3 )
  3527. // {
  3528. // cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'ReflectCalc' constructor must have two arguments: a channel count and frequency array.");
  3529. // return NULL;
  3530. // }
  3531. cmDspReflectCalc_t* p = cmDspInstAllocV(cmDspReflectCalc_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3532. 1, "lfsrN", kLfsrN_RcId, 0,0, kInDsvFl | kUIntDsvFl, "Gold code generator LFSR length",
  3533. 1, "mlsc0", kMlsCoeff0RcId, 0,0, kInDsvFl | kUIntDsvFl, "LFSR coefficient 0",
  3534. 1, "mlsc1", kMlsCoeff1RcId, 0,0, kInDsvFl | kUIntDsvFl, "LFSR coefficient 0",
  3535. 1, "spchip",kSmpPerChipRcId,0,0, kInDsvFl | kUIntDsvFl, "Samples per spreading code bit.",
  3536. 1, "rcosb", kRcosBetaRcId, 0,0, kInDsvFl | kDoubleDsvFl, "Raised cosine beta",
  3537. 1, "rcosos",kRcosOS_RcId, 0,0, kInDsvFl | kUIntDsvFl, "Raised cosine oversample factor.",
  3538. 1, "carhz", kCarrierHzRcId, 0,0, kInDsvFl | kDoubleDsvFl, "Carrier frequency in Hertz.",
  3539. 1, "envms", kAtkDcyMsRcId, 0,0, kInDsvFl | kDoubleDsvFl, "Signal Attack/Decay milliseconds.",
  3540. 1, "alpha", kPhatAlphaRcId, 0,0, kInDsvFl | kDoubleDsvFl, "PHAT alpha coefficient.",
  3541. 1, "mult", kPhatMultRcId, 0,0, kInDsvFl | kUIntDsvFl, "PHAT multiplier coefficient.",
  3542. 1, "in", kInRcId, 0,1, kInDsvFl | kAudioBufDsvFl,"Audio input",
  3543. 1, "out", kOutRcId, 0,1, kOutDsvFl | kAudioBufDsvFl,"Audio output",
  3544. 0 );
  3545. p->r = cmReflectCalcAlloc(ctx->cmProcCtx, NULL, NULL, 0, 0 );
  3546. cmDspSetDefaultUInt( ctx, &p->inst, kLfsrN_RcId, 0, 8);
  3547. cmDspSetDefaultUInt( ctx, &p->inst, kMlsCoeff0RcId, 0, 0x8e);
  3548. cmDspSetDefaultUInt( ctx, &p->inst, kMlsCoeff1RcId, 0, 0x96);
  3549. cmDspSetDefaultUInt( ctx, &p->inst, kSmpPerChipRcId, 0, 64);
  3550. cmDspSetDefaultDouble( ctx, &p->inst, kRcosBetaRcId, 0, 0.5);
  3551. cmDspSetDefaultUInt( ctx, &p->inst, kRcosOS_RcId, 0, 4);
  3552. cmDspSetDefaultDouble( ctx, &p->inst, kCarrierHzRcId, 0, 2500.0);
  3553. cmDspSetDefaultDouble( ctx, &p->inst, kAtkDcyMsRcId, 0, 50.0);
  3554. cmDspSetDefaultDouble( ctx, &p->inst, kPhatAlphaRcId, 0, 0.5);
  3555. cmDspSetDefaultUInt( ctx, &p->inst, kPhatMultRcId, 0, 1);
  3556. return &p->inst;
  3557. }
  3558. cmDspRC_t _cmDspReflectCalcSetup( cmDspCtx_t* ctx, cmDspInst_t* inst )
  3559. {
  3560. cmDspRC_t rc = kOkDspRC;
  3561. cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  3562. cmGoldSigArg_t gsa;
  3563. gsa.chN = 1;
  3564. gsa.srate = cmDspSampleRate(ctx);
  3565. gsa.lfsrN = cmDspUInt(inst,kLfsrN_RcId);
  3566. gsa.mlsCoeff0 = cmDspUInt(inst,kMlsCoeff0RcId);
  3567. gsa.mlsCoeff1 = cmDspUInt(inst,kMlsCoeff1RcId);
  3568. gsa.samplesPerChip = cmDspUInt(inst,kSmpPerChipRcId);
  3569. gsa.rcosBeta = cmDspDouble(inst,kRcosBetaRcId);
  3570. gsa.rcosOSFact = cmDspUInt(inst,kRcosOS_RcId);
  3571. gsa.carrierHz = cmDspDouble(inst,kCarrierHzRcId);
  3572. gsa.envMs = cmDspDouble(inst,kAtkDcyMsRcId);
  3573. double phatAlpha = cmDspDouble(inst,kPhatAlphaRcId);
  3574. unsigned phatMult = cmDspUInt(inst,kPhatMultRcId);
  3575. if( cmReflectCalcInit(p->r,&gsa,phatAlpha,phatMult) != cmOkRC )
  3576. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to initialize the internal ReflectCalc detector.");
  3577. return rc;
  3578. }
  3579. cmDspRC_t _cmDspReflectCalcFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3580. {
  3581. cmDspRC_t rc = kOkDspRC;
  3582. cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  3583. cmReflectCalcFree(&p->r);
  3584. return rc;
  3585. }
  3586. cmDspRC_t _cmDspReflectCalcReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3587. {
  3588. //cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  3589. cmDspApplyAllDefaults(ctx,inst);
  3590. return _cmDspReflectCalcSetup(ctx, inst );
  3591. }
  3592. cmDspRC_t _cmDspReflectCalcExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3593. {
  3594. cmDspRC_t rc = kOkDspRC;
  3595. cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  3596. const cmSample_t* xV = cmDspAudioBuf(ctx,inst,kInRcId,0);
  3597. unsigned xN = cmDspAudioBufSmpCount(ctx,inst,kInRcId,0);
  3598. cmSample_t* yV = cmDspAudioBuf(ctx,inst,kOutRcId,0);
  3599. unsigned yN = cmDspAudioBufSmpCount(ctx,inst,kOutRcId,0);
  3600. if( xV != NULL && yV != NULL )
  3601. {
  3602. assert( xN == yN );
  3603. cmReflectCalcExec(p->r,xV,yV,xN);
  3604. }
  3605. return rc;
  3606. }
  3607. cmDspRC_t _cmDspReflectCalcRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3608. {
  3609. //cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  3610. cmDspSetEvent(ctx,inst,evt);
  3611. return kOkDspRC;
  3612. }
  3613. cmDspClass_t* cmReflectCalcClassCons( cmDspCtx_t* ctx )
  3614. {
  3615. cmDspClassSetup(&_cmReflectCalcDC,ctx,"ReflectCalc",
  3616. NULL,
  3617. _cmDspReflectCalcAlloc,
  3618. _cmDspReflectCalcFree,
  3619. _cmDspReflectCalcReset,
  3620. _cmDspReflectCalcExec,
  3621. _cmDspReflectCalcRecv,
  3622. NULL,
  3623. NULL,
  3624. "Reflecttion time calculator");
  3625. return &_cmReflectCalcDC;
  3626. }
  3627. //------------------------------------------------------------------------------------------------------------
  3628. //)
  3629. //( { label:cmDspEchoCancel file_desc:"Normalized least mean squares echo canceller." kw:[sunit] }
  3630. enum
  3631. {
  3632. kMuEcId,
  3633. kImpRespN_EcId,
  3634. kDelayN_EcId,
  3635. kBypassEcId,
  3636. kUnfiltInEcId,
  3637. kFiltInEcId,
  3638. kOutEcId
  3639. };
  3640. cmDspClass_t _cmEchoCancelDC;
  3641. typedef struct
  3642. {
  3643. cmDspInst_t inst;
  3644. cmNlmsEc_t* r;
  3645. } cmDspEchoCancel_t;
  3646. cmDspInst_t* _cmDspEchoCancelAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3647. {
  3648. // if( va_cnt !=3 )
  3649. // {
  3650. // cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'EchoCancel' constructor must have two arguments: a channel count and frequency array.");
  3651. // return NULL;
  3652. // }
  3653. cmDspEchoCancel_t* p = cmDspInstAllocV(cmDspEchoCancel_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3654. 1, "mu", kMuEcId, 0,0, kInDsvFl | kDoubleDsvFl, "NLSM mu coefficient.",
  3655. 1, "irN", kImpRespN_EcId, 0,0, kInDsvFl | kUIntDsvFl, "Filter impulse response length in samples.",
  3656. 1, "delayN", kDelayN_EcId, 0,0, kInDsvFl | kUIntDsvFl, "Fixed feedback delay in samples.",
  3657. 1, "bypass", kBypassEcId, 0,0, kInDsvFl | kBoolDsvFl, "Bypass enable flag.",
  3658. 1, "uf_in", kUnfiltInEcId, 0,1, kInDsvFl | kAudioBufDsvFl, "Unfiltered audio input",
  3659. 1, "f_in", kFiltInEcId, 0,1, kInDsvFl | kAudioBufDsvFl, "Filtered audio input",
  3660. 1, "out", kOutEcId, 0,1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  3661. 0 );
  3662. p->r = cmNlmsEcAlloc(ctx->cmProcCtx, NULL, 0, 0,0,0 );
  3663. cmDspSetDefaultDouble( ctx, &p->inst, kMuEcId, 0, 0.1);
  3664. cmDspSetDefaultUInt( ctx, &p->inst, kImpRespN_EcId, 0, 2048);
  3665. cmDspSetDefaultUInt( ctx, &p->inst, kDelayN_EcId, 0, 1765);
  3666. cmDspSetDefaultBool( ctx, &p->inst, kBypassEcId, 0, false);
  3667. return &p->inst;
  3668. }
  3669. cmDspRC_t _cmDspEchoCancelSetup( cmDspCtx_t* ctx, cmDspInst_t* inst )
  3670. {
  3671. cmDspRC_t rc = kOkDspRC;
  3672. cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3673. double mu = cmDspDouble(inst,kMuEcId);
  3674. unsigned hN = cmDspUInt(inst,kImpRespN_EcId);
  3675. unsigned delayN = cmDspUInt(inst,kDelayN_EcId);
  3676. if( cmNlmsEcInit(p->r,cmDspSampleRate(ctx),mu,hN,delayN) != cmOkRC )
  3677. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to initialize the internal echo canceller.");
  3678. return rc;
  3679. }
  3680. cmDspRC_t _cmDspEchoCancelFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3681. {
  3682. cmDspRC_t rc = kOkDspRC;
  3683. cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3684. cmNlmsEcWrite(p->r, "/Users/kevin/temp/kc");
  3685. cmNlmsEcFree(&p->r);
  3686. return rc;
  3687. }
  3688. cmDspRC_t _cmDspEchoCancelReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3689. {
  3690. //cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3691. cmDspApplyAllDefaults(ctx,inst);
  3692. return _cmDspEchoCancelSetup(ctx, inst );
  3693. }
  3694. cmDspRC_t _cmDspEchoCancelExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3695. {
  3696. cmDspRC_t rc = kOkDspRC;
  3697. cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3698. bool bypassFl = true; //cmDspBool(inst,kBypassEcId);
  3699. const cmSample_t* fV = cmDspAudioBuf(ctx,inst,kFiltInEcId,0);
  3700. unsigned fN = cmDspAudioBufSmpCount(ctx,inst,kFiltInEcId,0);
  3701. const cmSample_t* uV = cmDspAudioBuf(ctx,inst,kUnfiltInEcId,0);
  3702. unsigned uN = cmDspAudioBufSmpCount(ctx,inst,kUnfiltInEcId,0);
  3703. cmSample_t* yV = cmDspAudioBuf(ctx,inst,kOutEcId,0);
  3704. unsigned yN = cmDspAudioBufSmpCount(ctx,inst,kOutEcId,0);
  3705. assert( fN==uN && fN==yN );
  3706. if( bypassFl )
  3707. {
  3708. cmVOS_Copy(yV,yN,uV);
  3709. }
  3710. else
  3711. {
  3712. if( fV !=NULL && uV != NULL && yV != NULL )
  3713. {
  3714. assert( uN == yN && fN == yN );
  3715. cmNlmsEcExec(p->r,uV,fV,yV,yN);
  3716. }
  3717. }
  3718. return rc;
  3719. }
  3720. cmDspRC_t _cmDspEchoCancelRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3721. {
  3722. cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3723. cmDspSetEvent(ctx,inst,evt);
  3724. if( p->r != NULL )
  3725. {
  3726. switch(evt->dstVarId)
  3727. {
  3728. case kMuEcId:
  3729. cmNlmsEcSetMu( p->r, cmDspReal(inst,kMuEcId));
  3730. break;
  3731. case kImpRespN_EcId:
  3732. cmNlmsEcSetIrN( p->r, cmDspUInt(inst,kImpRespN_EcId));
  3733. break;
  3734. case kDelayN_EcId:
  3735. cmNlmsEcSetDelayN( p->r, cmDspUInt(inst,kDelayN_EcId));
  3736. break;
  3737. case kBypassEcId:
  3738. printf("EC bypass:%i\n",cmDspBool(inst,kBypassEcId));
  3739. break;
  3740. }
  3741. }
  3742. printf("mu:%f dN:%i hN:%i\n",p->r->mu,p->r->delayN,p->r->hN);
  3743. return kOkDspRC;
  3744. }
  3745. cmDspClass_t* cmEchoCancelClassCons( cmDspCtx_t* ctx )
  3746. {
  3747. cmDspClassSetup(&_cmEchoCancelDC,ctx,"EchoCancel",
  3748. NULL,
  3749. _cmDspEchoCancelAlloc,
  3750. _cmDspEchoCancelFree,
  3751. _cmDspEchoCancelReset,
  3752. _cmDspEchoCancelExec,
  3753. _cmDspEchoCancelRecv,
  3754. NULL,
  3755. NULL,
  3756. "Echo canceller");
  3757. return &_cmEchoCancelDC;
  3758. }
  3759. //)