libcm is a C development framework with an emphasis on audio signal processing applications.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

cmDspKr.c 147KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678
  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. };
  2269. cmDspClass_t _cmPicadaeDC;
  2270. typedef struct
  2271. {
  2272. unsigned pitch;
  2273. unsigned velMapA[10];
  2274. } cmDspPicadaeVelMap_t;
  2275. // This table is generated from picadae_ac_3/velTableToDataStruct.py
  2276. cmDspPicadaeVelMap_t cmDspPicadaeVelMap[] =
  2277. {
  2278. { 21,{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } },
  2279. { 22,{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } },
  2280. { 23, { 12800, 12950, 13175, 13500, 13750, 14750, 15375, 17500, 23000, 37000, } },
  2281. { 24, { 12425, 12800, 13175, 14225, 14750, 15500, 17500, 22500, 32000, 39000, } },
  2282. { 25, { 14150, 14375, 14975, 14625, 15500, 16500, 20000, 28500, 40000, 40000, } },
  2283. { 26, { 13000, 13175, 13500, 13700, 13925, 14250, 15000, 16250, 19000, 26500, } },
  2284. { 27, { 13625, 13925, 14075, 14250, 14500, 14875, 15375, 16500, 18750, 25000, } },
  2285. { 28, { 12625, 13750, 13775, 14225, 14500, 16500, 18000, 20000, 25500, 34000, } },
  2286. { 29, { 12125, 12725, 13000, 12950, 14150, 15500, 16250, 17750, 21500, 28000, } },
  2287. { 30, { 13175, 13325, 13550, 14450, 14875, 15500, 16250, 17750, 21500, 27000, } },
  2288. { 31, { 13925, 14075, 14450, 14625, 15500, 16250, 16750, 17750, 19500, 23500, } },
  2289. { 32, { 13250, 14150, 14975, 14750, 15250, 16000, 17500, 21000, 27000, 38000, } },
  2290. { 33, { 11825, 13025, 14075, 14825, 14375, 14875, 16250, 17500, 22000, 28000, } },
  2291. { 34, { 13025, 13375, 13325, 13775, 14375, 14500, 15250, 18000, 22000, 27000, } },
  2292. { 35, { 11375, 12250, 12350, 12725, 14225, 13750, 15375, 17000, 20500, 25000, } },
  2293. { 36, { 11750, 13875, 14125, 14225, 14675, 14750, 16500, 18500, 22500, 32000, } },
  2294. { 37, { 12425, 12575, 13000, 13025, 13375, 15000, 16000, 18750, 25500, 35000, } },
  2295. { 38, { 13750, 13875, 14075, 14600, 14750, 15500, 17750, 21500, 27500, 37000, } },
  2296. { 39, { 11000, 12500, 12950, 13700, 14875, 15500, 16250, 20000, 26500, 37000, } },
  2297. { 40, { 11525, 11750, 12125, 12500, 12875, 13500, 14625, 18250, 23500, 29000, } },
  2298. { 41, { 11675, 11750, 12500, 13000, 13925, 15250, 17000, 20000, 26500, 36000, } },
  2299. { 42, { 11875, 12000, 11975, 12050, 12275, 13375, 15000, 17250, 22000, 29000, } },
  2300. { 43, { 11500, 11625, 11750, 11750, 12625, 12250, 13625, 16750, 19500, 25500, } },
  2301. { 44, { 12425, 12500, 12750, 12650, 13000, 14000, 15250, 16500, 20000, 27000, } },
  2302. { 45, { 11250, 11600, 11875, 12000, 12250, 13100, 14750, 15500, 18250, 25500, } },
  2303. { 46, { 11450, 11525, 11600, 11625, 11875, 12250, 14000, 15750, 17750, 21500, } },
  2304. { 47, { 11900, 11975, 12125, 12375, 13125, 14375, 15750, 18750, 22500, 28500, } },
  2305. { 48, { 11750, 13100, 13325, 13625, 14300, 14975, 15750, 19000, 24000, 30000, } },
  2306. { 49, { 11975, 12050, 12500, 12750, 13125, 14000, 17000, 20000, 25500, 40000, } },
  2307. { 50, { 11625, 11525, 11750, 11825, 12125, 12375, 14750, 16250, 19000, 25500, } },
  2308. { 51, { 12050, 12125, 12125, 12275, 12350, 12500, 12875, 16250, 18500, 22500, } },
  2309. { 52, { 12950, 13025, 13125, 13175, 13250, 13500, 13875, 15750, 18000, 22000, } },
  2310. { 53, { 10600, 10250, 10350, 10450, 10900, 11375, 13025, 14750, 18250, 26500, } },
  2311. { 54, { 12650, 12625, 12725, 12800, 13000, 13625, 16250, 18500, 23000, 32000, } },
  2312. { 55, { 11875, 12125, 12250, 12425, 12875, 13175, 13750, 17250, 20000, 26000, } },
  2313. { 56, { 11625, 11750, 12000, 12200, 12500, 13125, 14375, 17000, 20500, 26500, } },
  2314. { 57, { 11625, 11750, 12125, 12275, 12750, 14625, 16750, 20000, 25500, 39000, } },
  2315. { 58, { 12000, 12500, 12750, 12875, 13100, 13375, 15000, 17750, 21000, 28000, } },
  2316. { 59, { 11625, 11525, 12050, 13375, 13625, 14150, 16500, 21000, 24500, 30000, } },
  2317. { 60, { 12250, 12250, 12375, 12350, 13000, 13500, 16000, 17750, 22000, 29000, } },
  2318. { 61, { 11375, 11500, 11625, 11750, 12000, 12200, 12725, 13625, 17500, 21000, } },
  2319. { 62, { 11600, 11675, 11825, 12125, 12650, 13375, 14375, 18500, 24500, 32000, } },
  2320. { 63, { 12125, 12200, 12350, 12500, 13025, 13625, 16250, 18750, 24500, 36000, } },
  2321. { 64, { 10550, 10650, 10850, 11250, 11875, 12250, 14000, 16250, 19500, 26500, } },
  2322. { 65, { 12750, 12800, 12875, 13175, 13250, 14625, 14975, 17500, 20500, 26000, } },
  2323. { 66, { 10750, 11000, 11250, 11500, 12000, 12875, 15375, 17000, 20500, 28500, } },
  2324. { 67, { 10950, 11125, 11250, 11500, 11875, 13875, 15750, 17750, 23000, 37000, } },
  2325. { 68, { 10150, 10300, 10550, 10800, 11125, 11875, 13000, 16000, 19000, 25000, } },
  2326. { 69, { 11750, 11875, 12375, 12500, 12750, 13500, 16250, 18250, 23500, 31000, } },
  2327. { 70, { 10700, 10850, 10950, 11125, 11625, 13875, 14500, 15750, 18750, 24500, } },
  2328. { 71, { 10200, 10700, 11000, 11250, 11625, 14000, 14875, 16250, 22000, 27000, } },
  2329. { 72, { 9800, 10100, 10400, 10550, 11000, 11625, 13000, 15500, 17750, 23000, } },
  2330. { 73, { 10750, 10900, 11125, 11375, 11625, 12750, 14750, 15500, 18500, 23000, } },
  2331. { 74, { 10300, 10450, 10600, 10850, 11250, 12000, 14250, 15000, 17500, 21000, } },
  2332. { 75, { 10600, 10750, 10900, 11125, 12500, 14500, 14750, 15000, 21000, 31000, } },
  2333. { 76, { 10200, 11625, 12375, 12875, 13500, 15750, 19000, 22500, 27500, 39000, } },
  2334. { 77, { 10500, 10700, 11125, 11375, 11750, 14000, 14875, 16500, 20500, 27000, } },
  2335. { 78, { 10450, 10800, 11000, 11625, 12000, 13125, 15500, 18250, 22000, 34000, } },
  2336. { 79, { 12250, 13500, 14125, 14750, 16250, 17500, 19000, 24500, 31000, 40000, } },
  2337. { 80, { 10400, 10450, 10750, 11125, 12125, 13375, 14750, 17750, 23000, 39000, } },
  2338. { 81, { 10800, 10950, 11125, 11375, 12625, 13875, 14875, 16000, 19000, 23500, } },
  2339. { 82, { 12000, 12375, 13750, 13750, 12625, 14000, 17000, 19000, 21000, 24500, } },
  2340. { 83, { 12250, 12500, 13625, 13875, 14375, 16500, 17750, 20500, 25000, 35000, } },
  2341. { 84, { 11500, 12000, 12250, 12500, 13125, 14250, 15375, 16750, 19500, 25500, } },
  2342. { 85, { 10400, 10500, 10600, 11250, 12250, 13375, 15000, 16750, 20000, 26000, } },
  2343. { 86, { 11500, 11750, 11875, 12000, 12250, 12500, 13500, 15000, 20500, 21000, } },
  2344. { 87, { 10650, 11500, 13125, 13375, 13750, 14500, 16500, 18000, 20000, 24000, } },
  2345. { 88, { 11375, 11375, 11500, 12375, 12000, 13375, 14500, 16500, 19000, 23000, } },
  2346. { 89, { 9200, 10900, 11500, 12125, 22000, 12875, 14125, 16000, 19000, 26500, } },
  2347. { 90, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } },
  2348. { 91, { 9450, 9950, 10000, 10150, 10600, 11250, 12125, 13875, 15250, 19000, } },
  2349. { 92, { 9050, 9500, 9600, 10100, 10900, 11875, 13000, 16000, 20500, 31000, } },
  2350. { 93, { 11250, 11375, 12000, 12375, 12875, 13625, 14250, 17500, 21500, 39000, } },
  2351. { 94, { 11125, 11375, 11750, 13500, 14000, 14875, 15750, 17750, 22000, 25500, } },
  2352. { 95, { 10200, 10350, 11500, 12250, 12500, 13125, 13875, 15250, 19000, 21000, } },
  2353. { 96, { 9050, 9550, 10100, 13875, 13000, 14000, 18500, 22000, 27000, 39000, } },
  2354. { 97, { 11000, 12500, 13250, 13000, 13750, 15750, 15000, 18000, 19000, 22500, } },
  2355. { 98, { 10400, 10850, 12125, 12125, 13250, 13875, 16000, 18750, 26500, 37000, } },
  2356. { 99, { 11000, 12625, 13125, 14000, 15500, 16750, 19000, 21500, 25000, 36000, } },
  2357. { 100,{ 9650, 10450, 11500, 12375, 12500, 12875, 13500, 15500, 17500, 21500, } },
  2358. { 101,{ 10950, 11250, 11500, 11875, 12250, 12875, 13500, 14375, 22500, 39000, } },
  2359. { 102,{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } },
  2360. { 103,{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } },
  2361. { 104,{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } },
  2362. { 105,{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } },
  2363. { 106,{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } },
  2364. { 107,{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } },
  2365. { 108,{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } },
  2366. { 0, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }
  2367. };
  2368. typedef struct
  2369. {
  2370. int us;
  2371. int dutyPct;
  2372. } cmDspPicadaeDutyMap_t;
  2373. typedef struct
  2374. {
  2375. unsigned pitch;
  2376. cmDspPicadaeDutyMap_t dutyMapA[5];
  2377. } cmDspPicadaeDuty_t;
  2378. // This table is generated from picadae_ac_3/velTableToDataStruct.py
  2379. cmDspPicadaeDuty_t cmDspPicadaeDutyMap[] =
  2380. {
  2381. 21, {{ -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2382. 22, {{ -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2383. 23, {{ 0, 70 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2384. 24, {{ 0, 75 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2385. 25, {{ 0, 70 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2386. 26, {{ 0, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2387. 27, {{ 0, 70 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2388. 28, {{ 0, 70 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2389. 29, {{ 0, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2390. 30, {{ 0, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2391. 31, {{ 0, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2392. 32, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2393. 33, {{ 0, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2394. 34, {{ 0, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2395. 35, {{ 0, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2396. 36, {{ 0, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2397. 37, {{ 0, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2398. 38, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2399. 39, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2400. 40, {{ 0, 55 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2401. 41, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2402. 42, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2403. 43, {{ 0, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2404. 44, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2405. 45, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2406. 46, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2407. 47, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2408. 48, {{ 0, 70 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2409. 49, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2410. 50, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2411. 51, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2412. 52, {{ 0, 55 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2413. 53, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2414. 54, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2415. 55, {{ 0, 50 }, { 22000, 55 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2416. 56, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2417. 57, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2418. 58, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2419. 59, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2420. 60, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2421. 61, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2422. 62, {{ 0, 55 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2423. 63, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2424. 64, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2425. 65, {{ 0, 50 }, { 17000, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2426. 66, {{ 0, 53 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2427. 67, {{ 0, 55 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2428. 68, {{ 0, 53 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2429. 69, {{ 0, 55 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2430. 70, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2431. 71, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2432. 72, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2433. 73, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2434. 74, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2435. 75, {{ 0, 55 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2436. 76, {{ 0, 70 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2437. 77, {{ 0, 50 }, { 15000, 60 }, { 19000, 70 }, { -1, -1 }, { -1, -1 }, },
  2438. 78, {{ 0, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2439. 79, {{ 0, 50 }, { 15000, 60 }, { 19000, 70 }, { -1, -1 }, { -1, -1 }, },
  2440. 80, {{ 0, 45 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2441. 81, {{ 0, 50 }, { 15000, 70 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2442. 82, {{ 0, 50 }, { 12500, 60 }, { 14000, 65 }, { 17000, 70 }, { -1, -1 }, },
  2443. 83, {{ 0, 50 }, { 15000, 65 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2444. 84, {{ 0, 50 }, { 12500, 60 }, { 14000, 65 }, { 17000, 70 }, { -1, -1 }, },
  2445. 85, {{ 0, 50 }, { 12500, 60 }, { 14000, 65 }, { 17000, 70 }, { -1, -1 }, },
  2446. 86, {{ 0, 50 }, { 12500, 60 }, { 14000, 65 }, { 17000, 70 }, { -1, -1 }, },
  2447. 87, {{ 0, 50 }, { 14000, 60 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2448. 88, {{ 0, 50 }, { 12500, 60 }, { 14000, 65 }, { 17000, 70 }, { -1, -1 }, },
  2449. 89, {{ 0, 50 }, { 12500, 60 }, { 14000, 65 }, { 17000, 70 }, { -1, -1 }, },
  2450. 90, {{ -1, -1}, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2451. 91, {{ 0, 40 }, { 12500, 50 }, { 14000, 60 }, { 17000, 65 }, { -1, -1 }, },
  2452. 92, {{ 0, 40 }, { 14000, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2453. 93, {{ 0, 40 }, { 12500, 50 }, { 14000, 60 }, { 17000, 65 }, { -1, -1 }, },
  2454. 94, {{ 0, 40 }, { 14000, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2455. 95, {{ 0, 40 }, { 12500, 50 }, { 14000, 60 }, { 17000, 65 }, { -1, -1 }, },
  2456. 96, {{ 0, 40 }, { 12500, 50 }, { 14000, 60 }, { 17000, 65 }, { -1, -1 }, },
  2457. 97, {{ 0, 40 }, { 14000, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2458. 98, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2459. 99, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2460. 100, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2461. 101, {{ 0, 50 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2462. 102, {{ -1, -1}, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2463. 103, {{ -1, -1}, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2464. 104, {{ -1, -1}, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2465. 105, {{ -1, -1}, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2466. 106, {{ -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2467. 107, {{ -1, -1}, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2468. 108, {{ -1, -1}, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2469. 0, {{ -1, -1}, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, },
  2470. };
  2471. typedef struct
  2472. {
  2473. cmDspInst_t inst;
  2474. unsigned curDutyPctA[127];
  2475. } cmDspPicadae_t;
  2476. unsigned _cmDspPicadaeVelToMicroseconds( unsigned pitch, unsigned vel )
  2477. {
  2478. unsigned i;
  2479. unsigned velN = sizeof(cmDspPicadaeVelMap[i].velMapA)/sizeof(cmDspPicadaeVelMap[i].velMapA[0]);
  2480. long velIdx = lround( vel * velN / 127.0 );
  2481. assert( pitch >= cmDspPicadaeVelMap[0].pitch );
  2482. long pitchIdx = pitch - cmDspPicadaeVelMap[0].pitch;
  2483. return cmDspPicadaeVelMap[pitchIdx].velMapA[velIdx];
  2484. }
  2485. int _cmDspPicadaeVelToDutyCycle( unsigned pitch, unsigned usec )
  2486. {
  2487. unsigned i;
  2488. int dutyPct = -1;
  2489. long pitchIdx = pitch - cmDspPicadaeDutyMap[0].pitch;
  2490. cmDspPicadaeDuty_t* r = cmDspPicadaeDutyMap + pitchIdx;
  2491. for(i=0; r->dutyMapA[i].us != -1; ++i)
  2492. if( r->dutyMapA[i].us <= usec )
  2493. dutyPct = r->dutyMapA[i].dutyPct;
  2494. return dutyPct;
  2495. }
  2496. void _cmDspPicadaeSerialXmitNoteOff( cmDspCtx_t* ctx, cmDspInst_t* inst, uint8_t pitch )
  2497. {
  2498. const uint8_t note_off_op = 3;
  2499. uint8_t serialBuf[] = { 'w', pitch, note_off_op, 1, 0 };
  2500. cmSeSend( cmDspSysSerialPort(ctx->dspH), serialBuf, sizeof(serialBuf) );
  2501. }
  2502. void _cmDspPicadaeSerialXmitNoteOn( cmDspCtx_t* ctx, cmDspInst_t* inst, uint8_t pitch, uint8_t coarse_tick, uint8_t fine_tick )
  2503. {
  2504. const uint8_t note_on_usec_op = 2;
  2505. uint8_t serialBuf[] = { 'w', pitch, note_on_usec_op, 2, coarse_tick, fine_tick };
  2506. cmSeSend( cmDspSysSerialPort(ctx->dspH), serialBuf, sizeof(serialBuf) );
  2507. }
  2508. void _cmDspPicadaeSerialXmitDuty( cmDspCtx_t* ctx, cmDspPicadae_t* p, uint8_t pitch, uint8_t dutyPct )
  2509. {
  2510. if( p->curDutyPctA[ pitch ] != dutyPct )
  2511. {
  2512. uint8_t duty_pct_op = 0;
  2513. unsigned char serialBuf[] = { 'w', pitch, duty_pct_op, 1, dutyPct };
  2514. cmSeSend( cmDspSysSerialPort(ctx->dspH), serialBuf, sizeof(serialBuf) );
  2515. }
  2516. }
  2517. void _cmDspPicadaeSendNote( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2518. {
  2519. cmDspPicadae_t* p = (cmDspPicadae_t*)inst;
  2520. unsigned prescaler_usec = 16;
  2521. unsigned status = cmDspUInt(inst,kStatusPcId);
  2522. uint8_t pitch = (uint8_t)cmDspUInt(inst,kD0PcId);
  2523. unsigned velocity = cmDspUInt(inst,kD1PcId);
  2524. assert( 21<=pitch && pitch<=108 );
  2525. // if this is a note-off
  2526. if( status == kNoteOffMdId || velocity == 0 )
  2527. {
  2528. _cmDspPicadaeSerialXmitNoteOff( ctx, inst, pitch );
  2529. return;
  2530. }
  2531. unsigned usec = _cmDspPicadaeVelToMicroseconds(pitch, velocity );
  2532. // if the pitch is invalid then 'usec' will be set to 0
  2533. if( usec == 0 )
  2534. return;
  2535. unsigned dutyPct = _cmDspPicadaeVelToDutyCycle( pitch, usec );
  2536. // if the pitch is invalid then dutyPct will be set to -1
  2537. if( dutyPct == -1 )
  2538. return;
  2539. unsigned coarse_usec = prescaler_usec*255; // usec's in one coarse tick
  2540. uint8_t coarse_ticks = (uint8_t)(usec / coarse_usec);
  2541. uint8_t fine_ticks = (uint8_t)((usec - coarse_ticks*coarse_usec) / prescaler_usec);
  2542. _cmDspPicadaeSerialXmitDuty( ctx, p, pitch, dutyPct );
  2543. _cmDspPicadaeSerialXmitNoteOn( ctx, inst, pitch, coarse_ticks, fine_ticks );
  2544. }
  2545. cmDspInst_t* _cmDspPicadaeAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2546. {
  2547. cmDspVarArg_t args[] =
  2548. {
  2549. { "status", kStatusPcId, 0, 0, kOutDsvFl | kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "MIDI status" },
  2550. { "d0", kD0PcId, 0, 0, kOutDsvFl | kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "MIDI channel message d0" },
  2551. { "d1", kD1PcId, 0, 0, kOutDsvFl | kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "MIDI channel message d1" },
  2552. { NULL, 0, 0, 0, 0 }
  2553. };
  2554. cmDspPicadae_t* p = cmDspInstAlloc(cmDspPicadae_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  2555. return &p->inst;
  2556. }
  2557. void _cmDspPicadaReset( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2558. {
  2559. cmDspPicadae_t* p = (cmDspPicadae_t*)inst;
  2560. memset(p->curDutyPctA,0,sizeof(p->curDutyPctA));
  2561. // send note-off messages to all keys
  2562. unsigned i = 0;
  2563. for(i=0; cmDspPicadaeVelMap[i].pitch !=0; ++i)
  2564. if( cmDspPicadaeVelMap[i].velMapA[0] != 0 )
  2565. {
  2566. _cmDspPicadaeSerialXmitNoteOff( ctx, inst, (uint8_t)cmDspPicadaeVelMap[i].pitch );
  2567. cmSleepMs(10);
  2568. }
  2569. // send pedal-up message
  2570. cmDspSetUInt(ctx,inst,kD0PcId, kSustainCtlMdId );
  2571. cmDspSetUInt(ctx,inst,kD0PcId, 0 );
  2572. cmDspSetUInt(ctx,inst,kStatusPcId,kCtlMdId);
  2573. }
  2574. cmDspRC_t _cmDspPicadaeReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2575. {
  2576. cmDspRC_t rc = kOkDspRC;
  2577. cmDspApplyAllDefaults(ctx,inst);
  2578. return rc;
  2579. }
  2580. cmDspRC_t _cmDspPicadaeRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2581. {
  2582. //cmDspPicadae_t* p = (cmDspPicadae_t*)inst;
  2583. switch( evt->dstVarId )
  2584. {
  2585. case kStatusPcId:
  2586. {
  2587. // note: the status byte arrives last (after d0 and d1)
  2588. unsigned status = cmDsvGetUInt(evt->valuePtr);
  2589. unsigned stat_no_ch = status & 0xf0;
  2590. // if this is a note-on or channel
  2591. if( stat_no_ch == kNoteOnMdId || stat_no_ch == kNoteOffMdId || stat_no_ch == kCtlMdId )
  2592. {
  2593. cmDspSetUInt(ctx,inst,kStatusPcId,stat_no_ch);
  2594. if( stat_no_ch == kNoteOnMdId || stat_no_ch == kNoteOffMdId )
  2595. _cmDspPicadaeSendNote( ctx, inst );
  2596. }
  2597. }
  2598. break;
  2599. default:
  2600. // store
  2601. cmDspSetEvent(ctx,inst,evt);
  2602. break;
  2603. }
  2604. return kOkDspRC;
  2605. }
  2606. cmDspClass_t* cmPicadaeClassCons( cmDspCtx_t* ctx )
  2607. {
  2608. cmDspClassSetup(&_cmPicadaeDC,ctx,"Picadae",
  2609. NULL,
  2610. _cmDspPicadaeAlloc,
  2611. NULL,
  2612. _cmDspPicadaeReset,
  2613. NULL,
  2614. _cmDspPicadaeRecv,
  2615. NULL,
  2616. NULL,
  2617. "Picadaesynth Mapper");
  2618. return &_cmPicadaeDC;
  2619. }
  2620. //------------------------------------------------------------------------------------------------------------
  2621. //)
  2622. //( { label:cmDspRecdPlay file_desc:"Record audio segments from a live perfromance and play them back at a later time" kw:[sunit] }
  2623. enum
  2624. {
  2625. kChCntPrId,
  2626. kFnPrId,
  2627. kSecsPrId,
  2628. kMaxLaSecsPrId,
  2629. kCurLaSecsPrId,
  2630. kFadeRatePrId,
  2631. kScInitLocIdxPrId,
  2632. kScLocIdxPrId,
  2633. kCmdPrId,
  2634. kInAudioBasePrId
  2635. };
  2636. cmDspClass_t _cmRecdPlayDC;
  2637. typedef struct
  2638. {
  2639. cmDspInst_t inst;
  2640. cmRecdPlay* rcdply;
  2641. cmScH_t scH;
  2642. unsigned onSymId;
  2643. unsigned offSymId;
  2644. unsigned audioOutBaseId;
  2645. unsigned chCnt;
  2646. //unsigned scLocIdx;
  2647. } cmDspRecdPlay_t;
  2648. cmDspRC_t _cmDspRecdPlayParseRsrc( cmDspCtx_t* ctx, cmDspInst_t* inst, cmRecdPlay* rcdply )
  2649. {
  2650. cmDspRC_t rc = kOkDspRC;
  2651. const cmChar_t* path = NULL;
  2652. // read the 'recdplay' audio file path
  2653. if( cmDspRsrcString( ctx->dspH, &path, "recdPlayPath", NULL ) != kOkDspRC )
  2654. {
  2655. cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The 'recdPlayPath' resource string was not found.");
  2656. }
  2657. if( path == NULL )
  2658. path = "";
  2659. cmJsonH_t jsH = cmDspSysPgmRsrcHandle(ctx->dspH);
  2660. cmJsonNode_t* jnp = cmJsonFindValue(jsH,"recdPlay",NULL, kArrayTId);
  2661. if( jnp == NULL || cmJsonIsArray(jnp)==false )
  2662. {
  2663. // this is really a warning - the object does not require preloaded segments.
  2664. cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"Warning: The 'recdPlay' resource used to define pre-loaded segments was not found.");
  2665. return kOkDspRC;
  2666. }
  2667. unsigned n = cmJsonChildCount(jnp);
  2668. unsigned i;
  2669. // for each 'recdplay' segment record
  2670. for(i=0; i<n && rc==kOkDspRC; ++i)
  2671. {
  2672. cmJsonNode_t* cnp = cmJsonArrayElement(jnp,i);
  2673. const cmChar_t* label = NULL;
  2674. unsigned segSymId = cmInvalidId;
  2675. const cmChar_t* errLabel = NULL;
  2676. const cmChar_t* fn = NULL;
  2677. // read the ith segment record
  2678. if( cmJsonMemberValues(cnp,&errLabel,
  2679. "label", kStringTId, &label,
  2680. "file", kStringTId, &fn,
  2681. NULL) != kOkJsRC )
  2682. {
  2683. rc = cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The record at index %i in the 'recdPlay' pre-loaded segment list could not be parsed.",i);
  2684. goto errLabel;
  2685. }
  2686. // find or generate the symbol id for the segment label symbol
  2687. if((segSymId = cmSymTblRegisterSymbol(ctx->stH,label)) == cmInvalidId )
  2688. {
  2689. rc = cmDspInstErr(ctx,inst,kSymNotFoundDspRC,"The 'recdPlay' pre-load segment symbol '%s' could not be found or registered.",cmStringNullGuard(label));
  2690. goto errLabel;
  2691. }
  2692. // create the full path name for the segment audio file
  2693. if((fn = cmFsMakeFn( path, fn, NULL, NULL )) == NULL )
  2694. {
  2695. rc = cmDspInstErr(ctx,inst,kFileSysFailDspRC,"The 'recdPlay' file name '%s/%s' could not be generated.",cmStringNullGuard(path),cmStringNullGuard(fn));
  2696. goto errLabel;
  2697. }
  2698. // pre-load the segment
  2699. if( cmRecdPlayInsertRecord(rcdply,segSymId,fn) != cmOkRC )
  2700. rc = cmDspInstErr(ctx,inst,kSubSysFailDspRC,"The 'recdPlay' segment label:'%s' file:'%s' could not be loaded.",cmStringNullGuard(label),cmStringNullGuard(fn));
  2701. cmFsFreeFn(fn);
  2702. }
  2703. errLabel:
  2704. return rc;
  2705. }
  2706. cmDspRC_t _cmDspRecdPlayOpenScore( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2707. {
  2708. cmDspRC_t rc =kOkDspRC;
  2709. const cmChar_t* fn;
  2710. cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
  2711. //p->scLocIdx = 0;
  2712. if((fn = cmDspStrcz(inst,kFnPrId)) == NULL || strlen(fn)==0 )
  2713. return cmDspInstErr(ctx,inst, kInvalidArgDspRC, "No score file name supplied.");
  2714. if( cmScoreInitialize(ctx->cmCtx, &p->scH, fn, cmDspSampleRate(ctx), NULL, 0, NULL, NULL, ctx->stH ) != kOkScRC )
  2715. return cmDspInstErr(ctx,inst, kSubSysFailDspRC, "Unable to open the score '%s'.",fn);
  2716. if( cmScoreIsValid(p->scH) )
  2717. {
  2718. unsigned i;
  2719. unsigned markerCnt = cmScoreMarkerLabelCount(p->scH);
  2720. double initFragSecs = cmDspDouble(inst,kSecsPrId);
  2721. double maxLaSecs = cmDspDouble(inst,kMaxLaSecsPrId);
  2722. double curLaSecs = cmDspDouble(inst,kCurLaSecsPrId);
  2723. printf("2 max la secs:%f\n",cmDspDouble(inst,kMaxLaSecsPrId));
  2724. if((p->rcdply = cmRecdPlayAlloc(ctx->cmProcCtx, NULL, cmDspSampleRate(ctx), markerCnt, p->chCnt, initFragSecs, maxLaSecs, curLaSecs)) == NULL)
  2725. return cmErrMsg(&inst->classPtr->err,kSubSysFailDspRC,"Unable to create the internal recorder-player object.");
  2726. for(i=0; i<markerCnt; ++i)
  2727. cmRecdPlayRegisterFrag(p->rcdply,i, cmScoreMarkerLabelSymbolId(p->scH,i ));
  2728. if((rc = _cmDspRecdPlayParseRsrc(ctx,inst,p->rcdply)) != kOkDspRC )
  2729. rc = cmDspInstErr(ctx,inst,kInstResetFailDspRC,"The 'recdplay' segment pre-load failed.");
  2730. //p->scLocIdx = cmDspUInt(inst,kScInitLocIdxPrId);
  2731. }
  2732. return rc;
  2733. }
  2734. cmDspInst_t* _cmDspRecdPlayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2735. {
  2736. if( va_cnt < 1 )
  2737. {
  2738. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'RecdPlay' constructor must have a count of input ports.");
  2739. return NULL;
  2740. }
  2741. va_list vl1;
  2742. va_copy(vl1,vl);
  2743. int chCnt = va_arg(vl,int);
  2744. unsigned audioOutBase = kInAudioBasePrId + chCnt;
  2745. cmDspRecdPlay_t* p = cmDspInstAllocV(cmDspRecdPlay_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  2746. 1, "chs", kChCntPrId, 0,0, kUIntDsvFl | kReqArgDsvFl, "channel count.",
  2747. 1, "fn", kFnPrId, 0,0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score file." ,
  2748. 1, "secs", kSecsPrId, 0,0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Initial fragment allocation in seconds.",
  2749. 1, "maxla", kMaxLaSecsPrId, 0,0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Maximum look-ahead buffer in seconds.",
  2750. 1, "curla", kCurLaSecsPrId, 0,0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Current look-head buffer in seconds.",
  2751. 1, "frate", kFadeRatePrId, 0,0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Fade rate in dB per second.",
  2752. 1, "initIdx",kScInitLocIdxPrId,0,0,kInDsvFl | kUIntDsvFl, "Score search start location.",
  2753. 1, "index", kScLocIdxPrId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower location index.",
  2754. 1, "cmd", kCmdPrId, 0,0, kInDsvFl | kSymDsvFl, "on=reset off=stop.",
  2755. chCnt, "in", kInAudioBasePrId,0,1, kInDsvFl | kAudioBufDsvFl, "Audio input",
  2756. chCnt, "out", audioOutBase, 0,1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  2757. 0 );
  2758. va_end(vl1);
  2759. p->onSymId = cmSymTblId(ctx->stH,"on");
  2760. p->offSymId = cmSymTblId(ctx->stH,"off");
  2761. p->audioOutBaseId = audioOutBase;
  2762. p->chCnt = chCnt;
  2763. //p->scLocIdx = 0;
  2764. printf("0 max la secs:%f\n",cmDspDouble(&p->inst,kMaxLaSecsPrId));
  2765. cmDspSetDefaultDouble(ctx,&p->inst, kSecsPrId, 0.0, 10.0 );
  2766. cmDspSetDefaultDouble(ctx,&p->inst, kMaxLaSecsPrId,0.0, 2.0);
  2767. cmDspSetDefaultDouble(ctx,&p->inst, kCurLaSecsPrId,0.0, 0.1);
  2768. cmDspSetDefaultDouble(ctx,&p->inst, kFadeRatePrId, 0.0, 1.0);
  2769. cmDspSetDefaultUInt( ctx,&p->inst, kScInitLocIdxPrId,0,0);
  2770. printf("1 max la secs:%f\n",cmDspDouble(&p->inst,kMaxLaSecsPrId));
  2771. return &p->inst;
  2772. }
  2773. cmDspRC_t _cmDspRecdPlayFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2774. {
  2775. cmDspRC_t rc = kOkDspRC;
  2776. cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
  2777. cmRecdPlayFree(&p->rcdply);
  2778. cmScoreFinalize(&p->scH);
  2779. return rc;
  2780. }
  2781. cmDspRC_t _cmDspRecdPlayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2782. {
  2783. cmDspRC_t rc;
  2784. if((rc = _cmDspRecdPlayOpenScore(ctx,inst)) == kOkDspRC )
  2785. cmDspApplyAllDefaults(ctx,inst);
  2786. return rc;
  2787. }
  2788. cmDspRC_t _cmDspRecdPlayExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2789. {
  2790. cmDspRC_t rc = kOkDspRC;
  2791. cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
  2792. const cmSample_t* x[ p->chCnt ];
  2793. cmSample_t* y[ p->chCnt ];
  2794. unsigned n = 0;
  2795. unsigned i;
  2796. unsigned actChCnt = 0;
  2797. for(i=0; i<p->chCnt; ++i)
  2798. {
  2799. if( cmDspIsAudioInputConnected(ctx,inst,kInAudioBasePrId+i) == false )
  2800. {
  2801. x[i] = NULL;
  2802. y[i] = NULL;
  2803. }
  2804. else
  2805. {
  2806. if( i==0 )
  2807. n = cmDspAudioBufSmpCount(ctx,inst,kInAudioBasePrId+i,0);
  2808. else
  2809. {
  2810. assert( n == cmDspAudioBufSmpCount(ctx,inst,kInAudioBasePrId+i,0));
  2811. }
  2812. x[i] = cmDspAudioBuf(ctx,inst,kInAudioBasePrId+i,0);
  2813. if( x[i] != NULL )
  2814. {
  2815. y[i] = cmDspAudioBuf(ctx,inst,p->audioOutBaseId+i,0);
  2816. if( y[i] != NULL )
  2817. {
  2818. assert( n == cmDspAudioBufSmpCount(ctx,inst,p->audioOutBaseId+i,0));
  2819. cmVOS_Zero(y[i],n);
  2820. actChCnt += 1;
  2821. }
  2822. }
  2823. }
  2824. }
  2825. cmRecdPlayExec(p->rcdply,x,y,actChCnt,n);
  2826. return rc;
  2827. }
  2828. cmDspRC_t _cmDspRecdPlayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2829. {
  2830. cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
  2831. if( p->rcdply == NULL )
  2832. return kOkDspRC;
  2833. cmDspSetEvent(ctx,inst,evt);
  2834. switch( evt->dstVarId )
  2835. {
  2836. case kCmdPrId:
  2837. if( cmDspSymbol(inst,kCmdPrId) == p->onSymId )
  2838. {
  2839. printf("rewind\n");
  2840. cmRecdPlayRewind(p->rcdply);
  2841. //p->scLocIdx = cmDspUInt(inst,kScInitLocIdxPrId);
  2842. }
  2843. else
  2844. if( cmDspSymbol(inst,kCmdPrId) == p->offSymId )
  2845. {
  2846. }
  2847. break;
  2848. case kCurLaSecsPrId:
  2849. cmRecdPlaySetLaSecs(p->rcdply, cmDspDouble(inst,kCurLaSecsPrId));
  2850. break;
  2851. case kScInitLocIdxPrId:
  2852. printf("init-idx:%i\n",cmDspUInt(inst,kScInitLocIdxPrId));
  2853. break;
  2854. case kScLocIdxPrId:
  2855. {
  2856. unsigned endScLocIdx = cmDspUInt(inst,kScLocIdxPrId);
  2857. if( endScLocIdx < cmDspUInt(inst,kScInitLocIdxPrId) )
  2858. break;
  2859. cmScoreLoc_t* loc = cmScoreLoc(p->scH, endScLocIdx );
  2860. if( loc == NULL )
  2861. break;
  2862. cmScoreMarker_t* mp = loc->markList;
  2863. for(; mp!=NULL; mp=mp->link)
  2864. switch( mp->markTypeId )
  2865. {
  2866. case kRecdBegScMId:
  2867. printf("recd-beg %s\n",cmSymTblLabel(ctx->stH,mp->labelSymId));
  2868. cmRecdPlayBeginRecord(p->rcdply, mp->labelSymId );
  2869. break;
  2870. case kRecdEndScMId:
  2871. printf("recd-end %s\n",cmSymTblLabel(ctx->stH,mp->labelSymId));
  2872. cmRecdPlayEndRecord(p->rcdply, mp->labelSymId );
  2873. break;
  2874. case kPlayBegScMId:
  2875. printf("play-beg\n");
  2876. cmRecdPlayBeginPlay(p->rcdply, mp->labelSymId );
  2877. break;
  2878. case kPlayEndScMId:
  2879. printf("play-end\n");
  2880. cmRecdPlayEndPlay(p->rcdply, mp->labelSymId );
  2881. break;
  2882. case kFadeScMId:
  2883. printf("fade-beg\n");
  2884. cmRecdPlayBeginFade(p->rcdply, mp->labelSymId, cmDspDouble(inst,kFadeRatePrId) );
  2885. break;
  2886. default:
  2887. break;
  2888. }
  2889. //p->scLocIdx = endScLocIdx+1;
  2890. }
  2891. break;
  2892. }
  2893. return kOkDspRC;
  2894. }
  2895. cmDspClass_t* cmRecdPlayClassCons( cmDspCtx_t* ctx )
  2896. {
  2897. cmDspClassSetup(&_cmRecdPlayDC,ctx,"RecdPlay",
  2898. NULL,
  2899. _cmDspRecdPlayAlloc,
  2900. _cmDspRecdPlayFree,
  2901. _cmDspRecdPlayReset,
  2902. _cmDspRecdPlayExec,
  2903. _cmDspRecdPlayRecv,
  2904. NULL,
  2905. NULL,
  2906. "Score controlled live recorder/player");
  2907. return &_cmRecdPlayDC;
  2908. }
  2909. //------------------------------------------------------------------------------------------------------------
  2910. //)
  2911. //( { label:cmDspGoertzel file_desc:"Goertzel tone detection filter" kw:[sunit] }
  2912. enum
  2913. {
  2914. kHopFactGrId,
  2915. kInGrId,
  2916. kHzBaseGrId,
  2917. };
  2918. cmDspClass_t _cmGoertzelDC;
  2919. typedef struct
  2920. {
  2921. cmDspInst_t inst;
  2922. cmGoertzel* g;
  2923. double outPhs;
  2924. unsigned outBaseGrId;
  2925. unsigned chCnt;
  2926. } cmDspGoertzel_t;
  2927. cmDspInst_t* _cmDspGoertzelAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2928. {
  2929. if( va_cnt !=3 )
  2930. {
  2931. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'Goertzel' constructor must have two arguments: a channel count and frequency array.");
  2932. return NULL;
  2933. }
  2934. va_list vl1;
  2935. va_copy(vl1,vl);
  2936. unsigned hopFact = va_arg(vl,unsigned);
  2937. int chCnt = va_arg(vl,int);
  2938. double* hzV = va_arg(vl,double*);
  2939. unsigned outBaseGrId = kHzBaseGrId + chCnt;
  2940. unsigned i;
  2941. cmDspGoertzel_t* p = cmDspInstAllocV(cmDspGoertzel_t,ctx,classPtr,instSymId,id,storeSymId,1,vl1,
  2942. 1, "hop", kHopFactGrId, 0,0, kInDsvFl | kDoubleDsvFl, "Hop factor",
  2943. 1, "in", kInGrId, 0,1, kInDsvFl | kAudioBufDsvFl, "Audio input",
  2944. chCnt, "hz", kHzBaseGrId, 0,0, kInDsvFl | kDoubleDsvFl, "Hz input.",
  2945. chCnt, "out", outBaseGrId, 0,1, kOutDsvFl | kDoubleDsvFl, "Detector output",
  2946. 0 );
  2947. va_end(vl1);
  2948. p->outBaseGrId = outBaseGrId;
  2949. p->chCnt = chCnt;
  2950. p->g = cmGoertzelAlloc(ctx->cmProcCtx, NULL, 0, NULL, 0,0,0,0 );
  2951. cmDspSetDefaultUInt(ctx,&p->inst, kHopFactGrId, 0, cmMax(hopFact,1));
  2952. for(i=0; i<chCnt; ++i)
  2953. cmDspSetDefaultDouble(ctx,&p->inst, kHzBaseGrId+i, 0.0, hzV[i] );
  2954. return &p->inst;
  2955. }
  2956. cmDspRC_t _cmDspGoertzelSetup( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2957. {
  2958. cmDspRC_t rc = kOkDspRC;
  2959. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2960. unsigned hopFact = cmDspUInt(inst,kHopFactGrId);
  2961. unsigned procSmpCnt = cmDspAudioBufSmpCount(ctx,inst,kInGrId,0);
  2962. unsigned wndSmpCnt = procSmpCnt * hopFact;
  2963. double fcHzV[ p->chCnt ];
  2964. unsigned i;
  2965. for(i=0; i<p->chCnt; ++i)
  2966. {
  2967. double hz;
  2968. if( p->g->ch == NULL || p->g->ch[i].hz == 0 )
  2969. hz = cmDspDouble(inst,kHzBaseGrId);
  2970. else
  2971. hz = p->g->ch[i].hz;
  2972. fcHzV[i] = hz;
  2973. }
  2974. if( cmGoertzelInit(p->g,cmDspSysSampleRate(ctx->dspH),fcHzV,p->chCnt,procSmpCnt,procSmpCnt,wndSmpCnt) != cmOkRC )
  2975. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to initialize the internal Goertzel detector.");
  2976. return rc;
  2977. }
  2978. cmDspRC_t _cmDspGoertzelFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2979. {
  2980. cmDspRC_t rc = kOkDspRC;
  2981. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2982. cmGoertzelFree(&p->g);
  2983. return rc;
  2984. }
  2985. cmDspRC_t _cmDspGoertzelReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2986. {
  2987. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2988. cmDspApplyAllDefaults(ctx,inst);
  2989. p->outPhs = 0;
  2990. return _cmDspGoertzelSetup(ctx, inst );
  2991. }
  2992. cmDspRC_t _cmDspGoertzelExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2993. {
  2994. cmDspRC_t rc = kOkDspRC;
  2995. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2996. const cmSample_t* x = cmDspAudioBuf(ctx,inst,kInGrId,0);
  2997. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kInGrId,0);
  2998. double outMs = 50.0;
  2999. double outPhsMax = outMs * cmDspSysSampleRate(ctx->dspH) / 1000.0;
  3000. double outV[ p->chCnt ];
  3001. unsigned i;
  3002. if( x != NULL )
  3003. {
  3004. cmGoertzelExec(p->g,x,n,outV,p->chCnt);
  3005. p->outPhs += n;
  3006. if( p->outPhs > outPhsMax )
  3007. {
  3008. while( p->outPhs > outPhsMax )
  3009. p->outPhs -= outPhsMax;
  3010. for(i=0; i<p->chCnt; ++i)
  3011. {
  3012. cmDspSetDouble(ctx,inst,p->outBaseGrId+i,outV[i]);
  3013. //printf("%f ",outV[i]);
  3014. }
  3015. //printf("\n");
  3016. }
  3017. }
  3018. return rc;
  3019. }
  3020. cmDspRC_t _cmDspGoertzelRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3021. {
  3022. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  3023. cmDspSetEvent(ctx,inst,evt);
  3024. if( kHzBaseGrId <= evt->dstVarId && evt->dstVarId < kHzBaseGrId+p->chCnt )
  3025. cmGoertzelSetFcHz(p->g, evt->dstVarId - kHzBaseGrId, cmDspDouble(inst,evt->dstVarId));
  3026. else
  3027. {
  3028. if( evt->dstVarId==kHopFactGrId )
  3029. {
  3030. _cmDspGoertzelSetup(ctx,inst);
  3031. }
  3032. }
  3033. return kOkDspRC;
  3034. }
  3035. cmDspClass_t* cmGoertzelClassCons( cmDspCtx_t* ctx )
  3036. {
  3037. cmDspClassSetup(&_cmGoertzelDC,ctx,"Goertzel",
  3038. NULL,
  3039. _cmDspGoertzelAlloc,
  3040. _cmDspGoertzelFree,
  3041. _cmDspGoertzelReset,
  3042. _cmDspGoertzelExec,
  3043. _cmDspGoertzelRecv,
  3044. NULL,
  3045. NULL,
  3046. "Goertzel Tone Detector Filter");
  3047. return &_cmGoertzelDC;
  3048. }
  3049. //------------------------------------------------------------------------------------------------------------
  3050. //)
  3051. //( { label:cmDspSyncRecd file_desc:"Time align a MIDI and associated audio recording" kw:[sunit] }
  3052. enum
  3053. {
  3054. kRecdDirSrId,
  3055. kSrFnSrId,
  3056. kAfSrId,
  3057. kBitsSrId,
  3058. kCmdSrId,
  3059. kStatusSrId,
  3060. kD0SrId,
  3061. kD1SrId,
  3062. kSecSrId,
  3063. kNSecSrId,
  3064. kAinBaseSrId
  3065. };
  3066. cmDspClass_t _cmSyncRecdDC;
  3067. typedef struct
  3068. {
  3069. cmDspInst_t inst;
  3070. unsigned chCnt;
  3071. cmTimeSpec_t ats;
  3072. cmSyncRecdH_t srH;
  3073. unsigned openSymId;
  3074. unsigned closeSymId;
  3075. const cmChar_t* aFn;
  3076. const cmChar_t* srFn;
  3077. unsigned smpIdx;
  3078. } cmDspSyncRecd_t;
  3079. cmDspRC_t _cmDspSyncRecdCreateFile( cmDspCtx_t* ctx, cmDspInst_t* inst )
  3080. {
  3081. cmDspSyncRecd_t* p = (cmDspSyncRecd_t*)inst;
  3082. const cmChar_t* aFn = cmDspStrcz(inst,kAfSrId);
  3083. const cmChar_t* srFn = cmDspStrcz(inst,kSrFnSrId);
  3084. const cmChar_t* dir = cmDspStrcz(inst,kRecdDirSrId);
  3085. if( !cmFsIsDir(dir) )
  3086. return cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"'%s' is not a valid directory.",cmStringNullGuard(dir));
  3087. cmMemPtrFree(&p->aFn);
  3088. if( cmFsGenFn(dir,aFn,"aiff",&p->aFn) != kOkFsRC )
  3089. return cmDspInstErr(ctx,&p->inst,kFileSysFailDspRC,"Audio file name generation failed for dir='%s' and prefix='%s'.",cmStringNullGuard(dir),cmStringNullGuard(aFn));
  3090. cmMemPtrFree(&p->srFn);
  3091. if( cmFsGenFn(dir,srFn,"sr",&p->srFn) != kOkFsRC )
  3092. return cmDspInstErr(ctx,&p->inst,kFileSysFailDspRC,"Sync-recd file name generation failed for dir='%s' and prefix='%s'.",cmStringNullGuard(dir),cmStringNullGuard(srFn));
  3093. unsigned bits = cmDspUInt(inst,kBitsSrId);
  3094. if( cmSyncRecdCreate( ctx->cmCtx, &p->srH, p->srFn, p->aFn, cmDspSampleRate(ctx), p->chCnt, bits ) != kOkSyRC )
  3095. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Sync-recd file create failed for '%s'.",p->srFn);
  3096. p->smpIdx = 0;
  3097. return kOkDspRC;
  3098. }
  3099. cmDspInst_t* _cmDspSyncRecdAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3100. {
  3101. cmDspSyncRecd_t* p = cmDspInstAllocV(cmDspSyncRecd_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3102. 1, "dir", kRecdDirSrId, 0,0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Recording directory.",
  3103. 1, "srfn", kSrFnSrId, 0,0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "SyncRecd file prefix.",
  3104. 1, "afn", kAfSrId, 0,0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Audio file prefix.",
  3105. 1, "bits", kBitsSrId, 0,0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Audio file bits per sample.",
  3106. 1, "cmd", kCmdSrId, 0,0, kInDsvFl | kSymDsvFl, "Command: open | close",
  3107. 1, "status", kStatusSrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI status",
  3108. 1, "d0", kD0SrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI d0",
  3109. 1, "d1", kD1SrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI d1",
  3110. 1, "sec", kSecSrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI Timestamp Seconds",
  3111. 1, "nsec", kNSecSrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI Timestamp Nanoseconds",
  3112. 2, "ain", kAinBaseSrId, 0,1, kInDsvFl | kAudioBufDsvFl, "Audio Input",
  3113. 0 );
  3114. p->chCnt = 2;
  3115. p->openSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"open");
  3116. p->closeSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"close");
  3117. cmDspSetDefaultUInt(ctx,&p->inst,kBitsSrId,0,16);
  3118. return &p->inst;
  3119. }
  3120. cmDspRC_t _cmDspSyncRecdFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3121. {
  3122. cmDspRC_t rc = kOkDspRC;
  3123. cmDspSyncRecd_t* p = (cmDspSyncRecd_t*)inst;
  3124. cmMemPtrFree(&p->aFn);
  3125. cmMemPtrFree(&p->srFn);
  3126. cmSyncRecdFinal(&p->srH);
  3127. return rc;
  3128. }
  3129. cmDspRC_t _cmDspSyncRecdReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3130. {
  3131. cmDspRC_t rc = kOkDspRC;
  3132. cmDspApplyAllDefaults(ctx,inst);
  3133. return rc;
  3134. }
  3135. cmDspRC_t _cmDspSyncRecdExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3136. {
  3137. cmDspRC_t rc = kOkDspRC;
  3138. cmDspSyncRecd_t* p = (cmDspSyncRecd_t*)inst;
  3139. const cmSample_t* x[ p->chCnt ];
  3140. unsigned n = 0;
  3141. unsigned i;
  3142. //const cmTimeSpec_t* ts = &ctx->ctx->oTimeStamp;
  3143. //printf("SR: %ld %ld\n",ts->tv_sec,ts->tv_nsec);
  3144. p->ats = ctx->ctx->iTimeStamp;
  3145. for(i=0; i<p->chCnt; ++i)
  3146. {
  3147. if( i==0 )
  3148. n = cmDspAudioBufSmpCount(ctx,inst,kAinBaseSrId+i,0);
  3149. else
  3150. { assert( n == cmDspAudioBufSmpCount(ctx,inst,kAinBaseSrId+i,0)); }
  3151. x[i] = cmDspAudioBuf(ctx,inst,kAinBaseSrId+i,0);
  3152. }
  3153. if( n>0 && cmSyncRecdIsValid(p->srH ) )
  3154. if( cmSyncRecdAudioWrite( p->srH, &ctx->ctx->iTimeStamp, p->smpIdx, x, p->chCnt, n ) != kOkSyRC )
  3155. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Sync-recd audio update failed.");
  3156. p->smpIdx += n;
  3157. return rc;
  3158. }
  3159. cmDspRC_t _cmDspSyncRecdRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3160. {
  3161. cmDspRC_t rc = kOkDspRC;
  3162. cmDspSyncRecd_t* p = (cmDspSyncRecd_t*)inst;
  3163. cmDspSetEvent(ctx,inst,evt);
  3164. switch( evt->dstVarId )
  3165. {
  3166. case kStatusSrId:
  3167. if(cmMidiIsChStatus( cmDspUInt(inst,kStatusSrId) ) )
  3168. {
  3169. cmTimeSpec_t ts;
  3170. ts.tv_sec = cmDspUInt(inst,kSecSrId);
  3171. ts.tv_nsec = cmDspUInt(inst,kNSecSrId);
  3172. //printf("%i %i\n",cmDspUInt(inst,kD1SrId),cmTimeElapsedMicros(&ts,&p->ats));
  3173. if( cmSyncRecdIsValid(p->srH ) )
  3174. if( cmSyncRecdMidiWrite(p->srH, &ts, cmDspUInt(inst,kStatusSrId), cmDspUInt(inst,kD0SrId), cmDspUInt(inst,kD1SrId) ) != kOkSyRC )
  3175. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Sync-recd MIDI update failed.");
  3176. }
  3177. break;
  3178. case kCmdSrId:
  3179. {
  3180. unsigned cmdId = cmDspSymbol(inst,kCmdSrId);
  3181. if( cmdId == p->openSymId )
  3182. rc = _cmDspSyncRecdCreateFile(ctx,inst);
  3183. else
  3184. if( cmdId == p->closeSymId && cmSyncRecdIsValid(p->srH))
  3185. {
  3186. cmSyncRecdFinal(&p->srH);
  3187. //cmSyncRecdTest(ctx->cmCtx);
  3188. }
  3189. }
  3190. break;
  3191. }
  3192. return rc;
  3193. }
  3194. cmDspClass_t* cmSyncRecdClassCons( cmDspCtx_t* ctx )
  3195. {
  3196. cmDspClassSetup(&_cmSyncRecdDC,ctx,"SyncRecd",
  3197. NULL,
  3198. _cmDspSyncRecdAlloc,
  3199. _cmDspSyncRecdFree,
  3200. _cmDspSyncRecdReset,
  3201. _cmDspSyncRecdExec,
  3202. _cmDspSyncRecdRecv,
  3203. NULL,
  3204. NULL,
  3205. "Synchronized Audio and MIDI recorder.");
  3206. return &_cmSyncRecdDC;
  3207. }
  3208. //------------------------------------------------------------------------------------------------------------
  3209. //)
  3210. //( { label:cmDspTakeSeqBldr file_desc:"User interface unit for creating a single sequence from multiple, score aligned, MIDI fragments." kw:[sunit] }
  3211. enum
  3212. {
  3213. kFnTsbId,
  3214. kBldrTsbId,
  3215. kSelTsbId,
  3216. kRefreshTsbId,
  3217. kSendTsbId
  3218. };
  3219. cmDspClass_t _cmTakeSeqBldrDC;
  3220. typedef struct
  3221. {
  3222. cmDspInst_t inst;
  3223. cmTakeSeqBldrH_t h;
  3224. bool errFl;
  3225. } cmDspTakeSeqBldr_t;
  3226. cmDspInst_t* _cmDspTakeSeqBldrAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3227. {
  3228. cmDspTakeSeqBldr_t* p = cmDspInstAllocV(cmDspTakeSeqBldr_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3229. 1, "fn", kFnTsbId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score Tracking file.",
  3230. 1, "bldr", kBldrTsbId, 0, 0, kOutDsvFl | kPtrDsvFl, "Bldr Ref",
  3231. 1, "sel", kSelTsbId, 0, 0, kOutDsvFl | kUIntDsvFl, "Selected score element location index.",
  3232. 1, "refresh",kRefreshTsbId, 0, 0, kOutDsvFl | kUIntDsvFl, "Refresh",
  3233. 1, "send", kSendTsbId, 0, 0, kInDsvFl | kTypeDsvMask, "Resend last selected score element location.",
  3234. 0 );
  3235. p->errFl = false;
  3236. cmDspSetDefaultInt( ctx, &p->inst, kRefreshTsbId, 0, 0);
  3237. if( cmTakeSeqBldrAlloc(ctx->cmCtx, &p->h ) != kOkTsbRC )
  3238. cmErrMsg(&p->inst.classPtr->err, kSubSysFailDspRC, "Allocate TaskSeqBldr object.");
  3239. else
  3240. {
  3241. cmDspUiTakeSeqBldrCreate(ctx,&p->inst,kFnTsbId,kBldrTsbId,kSelTsbId,kRefreshTsbId);
  3242. }
  3243. return &p->inst;
  3244. }
  3245. cmDspRC_t _cmDspTakeSeqBldrSetup( cmDspCtx_t* ctx, cmDspInst_t* inst )
  3246. {
  3247. cmDspRC_t rc = kOkDspRC;
  3248. cmDspTakeSeqBldr_t* p = (cmDspTakeSeqBldr_t*)inst;
  3249. if( cmTakeSeqBldrInitialize(p->h, cmDspStrcz(inst,kFnTsbId) ) != kOkTsbRC )
  3250. {
  3251. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to initialize the internal TakeSeqBldr object with %s.",cmStringNullGuard(cmDspStrcz(inst,kFnTsbId)));
  3252. }
  3253. else
  3254. {
  3255. cmDspSetPtr(ctx,inst,kBldrTsbId,p->h.h);
  3256. p->errFl = false;
  3257. }
  3258. return rc;
  3259. }
  3260. cmDspRC_t _cmDspTakeSeqBldrFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3261. {
  3262. cmDspRC_t rc = kOkDspRC;
  3263. cmDspTakeSeqBldr_t* p = (cmDspTakeSeqBldr_t*)inst;
  3264. cmTakeSeqBldrFree(&p->h);
  3265. return rc;
  3266. }
  3267. cmDspRC_t _cmDspTakeSeqBldrReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3268. {
  3269. //cmDspTakeSeqBldr_t* p = (cmDspTakeSeqBldr_t*)inst;
  3270. cmDspApplyAllDefaults(ctx,inst);
  3271. return _cmDspTakeSeqBldrSetup(ctx, inst );
  3272. }
  3273. cmDspRC_t _cmDspTakeSeqBldrExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3274. {
  3275. cmDspRC_t rc = kOkDspRC;
  3276. return rc;
  3277. }
  3278. cmDspRC_t _cmDspTakeSeqBldrRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3279. {
  3280. // not matter what arrives at the 'send' input ....
  3281. if( evt->dstVarId == kSendTsbId )
  3282. {
  3283. // send the last score loc
  3284. unsigned selIdx;
  3285. if((selIdx = cmDspUInt(inst,kSelTsbId)) != cmInvalidIdx )
  3286. cmDspSetUInt(ctx,inst,kSelTsbId, selIdx );
  3287. return kOkDspRC;
  3288. }
  3289. cmDspSetEvent(ctx,inst,evt);
  3290. switch(evt->dstVarId)
  3291. {
  3292. case kFnTsbId:
  3293. _cmDspMidiFilePlayOpen(ctx, inst );
  3294. break;
  3295. }
  3296. return kOkDspRC;
  3297. }
  3298. cmDspClass_t* cmTakeSeqBldrClassCons( cmDspCtx_t* ctx )
  3299. {
  3300. cmDspClassSetup(&_cmTakeSeqBldrDC,ctx,"TakeSeqBldr",
  3301. NULL,
  3302. _cmDspTakeSeqBldrAlloc,
  3303. _cmDspTakeSeqBldrFree,
  3304. _cmDspTakeSeqBldrReset,
  3305. _cmDspTakeSeqBldrExec,
  3306. _cmDspTakeSeqBldrRecv,
  3307. NULL,
  3308. NULL,
  3309. "TakeSeqBldr");
  3310. return &_cmTakeSeqBldrDC;
  3311. }
  3312. //------------------------------------------------------------------------------------------------------------
  3313. //)
  3314. //( { label:cmDspTakeSeqRend file_desc:"User interface unit for graphically rendering the MIDI sequences created by cmDspTakeSeqBldr." kw:[sunit] }
  3315. enum
  3316. {
  3317. kBldrTsrId,
  3318. kRefreshTsrId,
  3319. kCmdTsrId,
  3320. kSelTsrId,
  3321. kStatusTsrId,
  3322. kD0TsrId,
  3323. kD1TsrId,
  3324. kSmpIdxTsrId
  3325. };
  3326. cmDspClass_t _cmTakeSeqRendDC;
  3327. typedef struct
  3328. {
  3329. cmDspInst_t inst;
  3330. cmTakeSeqBldrH_t h;
  3331. unsigned startSymId;
  3332. unsigned stopSymId;
  3333. unsigned contSymId;
  3334. unsigned onSymId;
  3335. unsigned offSymId;
  3336. bool errFl;
  3337. } cmDspTakeSeqRend_t;
  3338. cmDspInst_t* _cmDspTakeSeqRendAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3339. {
  3340. cmDspTakeSeqRend_t* p = cmDspInstAllocV(cmDspTakeSeqRend_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3341. 1, "bldr", kBldrTsrId, 0, 0, kInDsvFl | kPtrDsvFl | kOptArgDsvFl, "Take Sequene Builder Ref",
  3342. 1, "refresh",kRefreshTsrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Refresh",
  3343. 1, "cmd", kCmdTsrId, 0, 0, kInDsvFl | kSymDsvFl, "start | stop | continue" ,
  3344. 1, "sel", kSelTsrId, 0, 0, kInDsvFl | kUIntDsvFl, "Selected score element location index input.",
  3345. 1, "status", kStatusTsrId, 0, 0, kOutDsvFl | kIntDsvFl, "Status value output",
  3346. 1, "d0", kD0TsrId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 0" ,
  3347. 1, "d1", kD1TsrId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 1",
  3348. 1, "smpidx", kSmpIdxTsrId, 0, 0, kOutDsvFl | kUIntDsvFl, "Msg time tag as a sample index.",
  3349. 0 );
  3350. p->startSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"start");
  3351. p->stopSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"stop");
  3352. p->contSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"continue");
  3353. p->onSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"on");
  3354. p->offSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"off");
  3355. p->errFl = false;
  3356. cmDspSetDefaultInt( ctx, &p->inst, kRefreshTsrId, 0, 0);
  3357. cmDspSetDefaultSymbol(ctx, &p->inst, kCmdTsrId, p->stopSymId);
  3358. cmDspSetDefaultInt( ctx, &p->inst, kSmpIdxTsrId, 0, 0);
  3359. cmDspSetDefaultUInt( ctx, &p->inst, kStatusTsrId, 0, 0);
  3360. cmDspSetDefaultUInt( ctx, &p->inst, kD0TsrId, 0, 0);
  3361. cmDspSetDefaultUInt( ctx, &p->inst, kD1TsrId, 0, 0);
  3362. cmDspUiTakeSeqRendCreate(ctx,&p->inst,kBldrTsrId,kRefreshTsrId,kSelTsrId);
  3363. return &p->inst;
  3364. }
  3365. cmDspRC_t _cmDspTakeSeqRendFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3366. {
  3367. cmDspRC_t rc = kOkDspRC;
  3368. return rc;
  3369. }
  3370. cmDspRC_t _cmDspTakeSeqRendReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3371. {
  3372. //cmDspTakeSeqRend_t* p = (cmDspTakeSeqRend_t*)inst;
  3373. cmDspApplyAllDefaults(ctx,inst);
  3374. return kOkDspRC;
  3375. }
  3376. typedef struct
  3377. {
  3378. cmDspCtx_t* ctx;
  3379. cmDspInst_t* inst;
  3380. } _cmDspTakeSeqRendCbArg_t;
  3381. // Called from cmDspTakeSeqRendExec() -> cmTakeSeqRendPlayExec() to
  3382. // deliver MIDI messages which need to be transmitted.
  3383. void _cmDspTakeSeqRendMidiCb( void* arg, const cmTksbEvent_t* e )
  3384. {
  3385. _cmDspTakeSeqRendCbArg_t* a = (_cmDspTakeSeqRendCbArg_t*)arg;
  3386. switch( e->status )
  3387. {
  3388. case kNoteOffMdId:
  3389. case kNoteOnMdId:
  3390. case kCtlMdId:
  3391. //if( !cmMidiIsPedal(e->status,e->d0))
  3392. {
  3393. cmDspSetUInt(a->ctx,a->inst, kSmpIdxTsrId, e->smpIdx);
  3394. cmDspSetUInt(a->ctx,a->inst, kD1TsrId, e->d1);
  3395. cmDspSetUInt(a->ctx,a->inst, kD0TsrId, e->d0);
  3396. cmDspSetUInt(a->ctx,a->inst, kStatusTsrId, e->status);
  3397. }
  3398. break;
  3399. }
  3400. }
  3401. void _cmDspTakeSeqRendPedalsUp( cmDspCtx_t* ctx, cmDspInst_t* inst )
  3402. {
  3403. _cmDspTakeSeqRendCbArg_t a;
  3404. a.ctx = ctx;
  3405. a.inst = inst;
  3406. cmTksbEvent_t e[] =
  3407. {
  3408. { 0, kCtlMdId, kSustainCtlMdId, 0 },
  3409. { 0, kCtlMdId, kPortamentoCtlMdId,0 },
  3410. { 0, kCtlMdId, kSostenutoCtlMdId, 0 },
  3411. { 0, kCtlMdId, kSoftPedalCtlMdId, 0 },
  3412. { 0, kCtlMdId, kLegatoCtlMdId, 0 }
  3413. };
  3414. unsigned n = sizeof(e)/sizeof(e[0]);
  3415. unsigned i;
  3416. for(i=0; i<n; ++i)
  3417. _cmDspTakeSeqRendMidiCb(&a,e+i);
  3418. }
  3419. cmDspRC_t _cmDspTakeSeqRendExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3420. {
  3421. cmDspRC_t rc = kOkDspRC;
  3422. cmDspTakeSeqRend_t* p = (cmDspTakeSeqRend_t*)inst;
  3423. if( cmDspSymbol(inst,kCmdTsrId) != p->stopSymId )
  3424. {
  3425. if( cmTakeSeqBldrIsValid(p->h) == false )
  3426. {
  3427. if( p->errFl==false )
  3428. {
  3429. rc = cmErrMsg(&inst->classPtr->err, kInvalidStateDspRC,"The Take Sequence Builder not been given a valid file.");
  3430. p->errFl = true;
  3431. }
  3432. return rc;
  3433. }
  3434. unsigned sPc = cmDspSamplesPerCycle(ctx);
  3435. _cmDspTakeSeqRendCbArg_t arg;
  3436. arg.inst = inst;
  3437. arg.ctx = ctx;
  3438. // This call may result in multiple callbacks
  3439. // to _cmDspTakeSeqRendMidiCb() from within the function.
  3440. cmTakeSeqBldrPlayExec(p->h,sPc,_cmDspTakeSeqRendMidiCb, &arg );
  3441. }
  3442. return rc;
  3443. }
  3444. cmDspRC_t _cmDspTakeSeqRendRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3445. {
  3446. cmDspTakeSeqRend_t* p = (cmDspTakeSeqRend_t*)inst;
  3447. printf("Recv:%s\n",cmStringNullGuard(cmDspVarLabel(ctx, inst, evt->dstVarId)) );
  3448. cmDspSetEvent(ctx,inst,evt);
  3449. switch(evt->dstVarId)
  3450. {
  3451. case kBldrTsrId:
  3452. p->h.h = cmDspPtr(inst,kBldrTsrId);
  3453. break;
  3454. case kCmdTsrId:
  3455. {
  3456. unsigned symId = cmDspSymbol(inst,kCmdTsrId);
  3457. if( symId == p->onSymId )
  3458. cmDspSetSymbol( ctx, inst, kCmdTsrId, p->startSymId );
  3459. else
  3460. if( symId == p->offSymId )
  3461. cmDspSetSymbol( ctx, inst, kCmdTsrId, p->stopSymId );
  3462. if( cmTakeSeqBldrIsValid(p->h) && cmDspSymbol(inst,kCmdTsrId)==p->startSymId )
  3463. {
  3464. _cmDspTakeSeqRendPedalsUp( ctx, inst );
  3465. cmTakeSeqBldrPlaySeekLoc(p->h, cmInvalidId );
  3466. }
  3467. }
  3468. break;
  3469. case kSelTsrId:
  3470. {
  3471. // seek the playback position to the scLocIdx.
  3472. unsigned scLocIdx = cmDspUInt(inst,kSelTsrId);
  3473. if( cmTakeSeqBldrIsValid(p->h) && cmTakeSeqBldrPlaySeekLoc(p->h, scLocIdx ) != kOkTsbRC )
  3474. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Take Sequence Bldr Seek failed on score location index %i.", scLocIdx);
  3475. }
  3476. break;
  3477. }
  3478. return kOkDspRC;
  3479. }
  3480. cmDspClass_t* cmTakeSeqRendClassCons( cmDspCtx_t* ctx )
  3481. {
  3482. cmDspClassSetup(&_cmTakeSeqRendDC,ctx,"TakeSeqRend",
  3483. NULL,
  3484. _cmDspTakeSeqRendAlloc,
  3485. _cmDspTakeSeqRendFree,
  3486. _cmDspTakeSeqRendReset,
  3487. _cmDspTakeSeqRendExec,
  3488. _cmDspTakeSeqRendRecv,
  3489. NULL,
  3490. NULL,
  3491. "TakeSeqRend");
  3492. return &_cmTakeSeqRendDC;
  3493. }
  3494. //------------------------------------------------------------------------------------------------------------
  3495. //)
  3496. //( { label:cmDspReflectCalc file_desc:"Estimate the time-of-flight of from an acoustic signal from a speaker to a microphone." kw:[sunit] }
  3497. enum
  3498. {
  3499. kLfsrN_RcId,
  3500. kMlsCoeff0RcId,
  3501. kMlsCoeff1RcId,
  3502. kSmpPerChipRcId,
  3503. kRcosBetaRcId,
  3504. kRcosOS_RcId,
  3505. kCarrierHzRcId,
  3506. kAtkDcyMsRcId,
  3507. kPhatAlphaRcId,
  3508. kPhatMultRcId,
  3509. kInRcId,
  3510. kOutRcId,
  3511. };
  3512. cmDspClass_t _cmReflectCalcDC;
  3513. typedef struct
  3514. {
  3515. cmDspInst_t inst;
  3516. cmReflectCalc_t* r;
  3517. } cmDspReflectCalc_t;
  3518. cmDspInst_t* _cmDspReflectCalcAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3519. {
  3520. // if( va_cnt !=3 )
  3521. // {
  3522. // cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'ReflectCalc' constructor must have two arguments: a channel count and frequency array.");
  3523. // return NULL;
  3524. // }
  3525. cmDspReflectCalc_t* p = cmDspInstAllocV(cmDspReflectCalc_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3526. 1, "lfsrN", kLfsrN_RcId, 0,0, kInDsvFl | kUIntDsvFl, "Gold code generator LFSR length",
  3527. 1, "mlsc0", kMlsCoeff0RcId, 0,0, kInDsvFl | kUIntDsvFl, "LFSR coefficient 0",
  3528. 1, "mlsc1", kMlsCoeff1RcId, 0,0, kInDsvFl | kUIntDsvFl, "LFSR coefficient 0",
  3529. 1, "spchip",kSmpPerChipRcId,0,0, kInDsvFl | kUIntDsvFl, "Samples per spreading code bit.",
  3530. 1, "rcosb", kRcosBetaRcId, 0,0, kInDsvFl | kDoubleDsvFl, "Raised cosine beta",
  3531. 1, "rcosos",kRcosOS_RcId, 0,0, kInDsvFl | kUIntDsvFl, "Raised cosine oversample factor.",
  3532. 1, "carhz", kCarrierHzRcId, 0,0, kInDsvFl | kDoubleDsvFl, "Carrier frequency in Hertz.",
  3533. 1, "envms", kAtkDcyMsRcId, 0,0, kInDsvFl | kDoubleDsvFl, "Signal Attack/Decay milliseconds.",
  3534. 1, "alpha", kPhatAlphaRcId, 0,0, kInDsvFl | kDoubleDsvFl, "PHAT alpha coefficient.",
  3535. 1, "mult", kPhatMultRcId, 0,0, kInDsvFl | kUIntDsvFl, "PHAT multiplier coefficient.",
  3536. 1, "in", kInRcId, 0,1, kInDsvFl | kAudioBufDsvFl,"Audio input",
  3537. 1, "out", kOutRcId, 0,1, kOutDsvFl | kAudioBufDsvFl,"Audio output",
  3538. 0 );
  3539. p->r = cmReflectCalcAlloc(ctx->cmProcCtx, NULL, NULL, 0, 0 );
  3540. cmDspSetDefaultUInt( ctx, &p->inst, kLfsrN_RcId, 0, 8);
  3541. cmDspSetDefaultUInt( ctx, &p->inst, kMlsCoeff0RcId, 0, 0x8e);
  3542. cmDspSetDefaultUInt( ctx, &p->inst, kMlsCoeff1RcId, 0, 0x96);
  3543. cmDspSetDefaultUInt( ctx, &p->inst, kSmpPerChipRcId, 0, 64);
  3544. cmDspSetDefaultDouble( ctx, &p->inst, kRcosBetaRcId, 0, 0.5);
  3545. cmDspSetDefaultUInt( ctx, &p->inst, kRcosOS_RcId, 0, 4);
  3546. cmDspSetDefaultDouble( ctx, &p->inst, kCarrierHzRcId, 0, 2500.0);
  3547. cmDspSetDefaultDouble( ctx, &p->inst, kAtkDcyMsRcId, 0, 50.0);
  3548. cmDspSetDefaultDouble( ctx, &p->inst, kPhatAlphaRcId, 0, 0.5);
  3549. cmDspSetDefaultUInt( ctx, &p->inst, kPhatMultRcId, 0, 1);
  3550. return &p->inst;
  3551. }
  3552. cmDspRC_t _cmDspReflectCalcSetup( cmDspCtx_t* ctx, cmDspInst_t* inst )
  3553. {
  3554. cmDspRC_t rc = kOkDspRC;
  3555. cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  3556. cmGoldSigArg_t gsa;
  3557. gsa.chN = 1;
  3558. gsa.srate = cmDspSampleRate(ctx);
  3559. gsa.lfsrN = cmDspUInt(inst,kLfsrN_RcId);
  3560. gsa.mlsCoeff0 = cmDspUInt(inst,kMlsCoeff0RcId);
  3561. gsa.mlsCoeff1 = cmDspUInt(inst,kMlsCoeff1RcId);
  3562. gsa.samplesPerChip = cmDspUInt(inst,kSmpPerChipRcId);
  3563. gsa.rcosBeta = cmDspDouble(inst,kRcosBetaRcId);
  3564. gsa.rcosOSFact = cmDspUInt(inst,kRcosOS_RcId);
  3565. gsa.carrierHz = cmDspDouble(inst,kCarrierHzRcId);
  3566. gsa.envMs = cmDspDouble(inst,kAtkDcyMsRcId);
  3567. double phatAlpha = cmDspDouble(inst,kPhatAlphaRcId);
  3568. unsigned phatMult = cmDspUInt(inst,kPhatMultRcId);
  3569. if( cmReflectCalcInit(p->r,&gsa,phatAlpha,phatMult) != cmOkRC )
  3570. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to initialize the internal ReflectCalc detector.");
  3571. return rc;
  3572. }
  3573. cmDspRC_t _cmDspReflectCalcFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3574. {
  3575. cmDspRC_t rc = kOkDspRC;
  3576. cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  3577. cmReflectCalcFree(&p->r);
  3578. return rc;
  3579. }
  3580. cmDspRC_t _cmDspReflectCalcReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3581. {
  3582. //cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  3583. cmDspApplyAllDefaults(ctx,inst);
  3584. return _cmDspReflectCalcSetup(ctx, inst );
  3585. }
  3586. cmDspRC_t _cmDspReflectCalcExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3587. {
  3588. cmDspRC_t rc = kOkDspRC;
  3589. cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  3590. const cmSample_t* xV = cmDspAudioBuf(ctx,inst,kInRcId,0);
  3591. unsigned xN = cmDspAudioBufSmpCount(ctx,inst,kInRcId,0);
  3592. cmSample_t* yV = cmDspAudioBuf(ctx,inst,kOutRcId,0);
  3593. unsigned yN = cmDspAudioBufSmpCount(ctx,inst,kOutRcId,0);
  3594. if( xV != NULL && yV != NULL )
  3595. {
  3596. assert( xN == yN );
  3597. cmReflectCalcExec(p->r,xV,yV,xN);
  3598. }
  3599. return rc;
  3600. }
  3601. cmDspRC_t _cmDspReflectCalcRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3602. {
  3603. //cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  3604. cmDspSetEvent(ctx,inst,evt);
  3605. return kOkDspRC;
  3606. }
  3607. cmDspClass_t* cmReflectCalcClassCons( cmDspCtx_t* ctx )
  3608. {
  3609. cmDspClassSetup(&_cmReflectCalcDC,ctx,"ReflectCalc",
  3610. NULL,
  3611. _cmDspReflectCalcAlloc,
  3612. _cmDspReflectCalcFree,
  3613. _cmDspReflectCalcReset,
  3614. _cmDspReflectCalcExec,
  3615. _cmDspReflectCalcRecv,
  3616. NULL,
  3617. NULL,
  3618. "Reflecttion time calculator");
  3619. return &_cmReflectCalcDC;
  3620. }
  3621. //------------------------------------------------------------------------------------------------------------
  3622. //)
  3623. //( { label:cmDspEchoCancel file_desc:"Normalized least mean squares echo canceller." kw:[sunit] }
  3624. enum
  3625. {
  3626. kMuEcId,
  3627. kImpRespN_EcId,
  3628. kDelayN_EcId,
  3629. kBypassEcId,
  3630. kUnfiltInEcId,
  3631. kFiltInEcId,
  3632. kOutEcId
  3633. };
  3634. cmDspClass_t _cmEchoCancelDC;
  3635. typedef struct
  3636. {
  3637. cmDspInst_t inst;
  3638. cmNlmsEc_t* r;
  3639. } cmDspEchoCancel_t;
  3640. cmDspInst_t* _cmDspEchoCancelAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3641. {
  3642. // if( va_cnt !=3 )
  3643. // {
  3644. // cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'EchoCancel' constructor must have two arguments: a channel count and frequency array.");
  3645. // return NULL;
  3646. // }
  3647. cmDspEchoCancel_t* p = cmDspInstAllocV(cmDspEchoCancel_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3648. 1, "mu", kMuEcId, 0,0, kInDsvFl | kDoubleDsvFl, "NLSM mu coefficient.",
  3649. 1, "irN", kImpRespN_EcId, 0,0, kInDsvFl | kUIntDsvFl, "Filter impulse response length in samples.",
  3650. 1, "delayN", kDelayN_EcId, 0,0, kInDsvFl | kUIntDsvFl, "Fixed feedback delay in samples.",
  3651. 1, "bypass", kBypassEcId, 0,0, kInDsvFl | kBoolDsvFl, "Bypass enable flag.",
  3652. 1, "uf_in", kUnfiltInEcId, 0,1, kInDsvFl | kAudioBufDsvFl, "Unfiltered audio input",
  3653. 1, "f_in", kFiltInEcId, 0,1, kInDsvFl | kAudioBufDsvFl, "Filtered audio input",
  3654. 1, "out", kOutEcId, 0,1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  3655. 0 );
  3656. p->r = cmNlmsEcAlloc(ctx->cmProcCtx, NULL, 0, 0,0,0 );
  3657. cmDspSetDefaultDouble( ctx, &p->inst, kMuEcId, 0, 0.1);
  3658. cmDspSetDefaultUInt( ctx, &p->inst, kImpRespN_EcId, 0, 2048);
  3659. cmDspSetDefaultUInt( ctx, &p->inst, kDelayN_EcId, 0, 1765);
  3660. cmDspSetDefaultBool( ctx, &p->inst, kBypassEcId, 0, false);
  3661. return &p->inst;
  3662. }
  3663. cmDspRC_t _cmDspEchoCancelSetup( cmDspCtx_t* ctx, cmDspInst_t* inst )
  3664. {
  3665. cmDspRC_t rc = kOkDspRC;
  3666. cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3667. double mu = cmDspDouble(inst,kMuEcId);
  3668. unsigned hN = cmDspUInt(inst,kImpRespN_EcId);
  3669. unsigned delayN = cmDspUInt(inst,kDelayN_EcId);
  3670. if( cmNlmsEcInit(p->r,cmDspSampleRate(ctx),mu,hN,delayN) != cmOkRC )
  3671. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to initialize the internal echo canceller.");
  3672. return rc;
  3673. }
  3674. cmDspRC_t _cmDspEchoCancelFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3675. {
  3676. cmDspRC_t rc = kOkDspRC;
  3677. cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3678. cmNlmsEcWrite(p->r, "/Users/kevin/temp/kc");
  3679. cmNlmsEcFree(&p->r);
  3680. return rc;
  3681. }
  3682. cmDspRC_t _cmDspEchoCancelReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3683. {
  3684. //cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3685. cmDspApplyAllDefaults(ctx,inst);
  3686. return _cmDspEchoCancelSetup(ctx, inst );
  3687. }
  3688. cmDspRC_t _cmDspEchoCancelExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3689. {
  3690. cmDspRC_t rc = kOkDspRC;
  3691. cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3692. bool bypassFl = true; //cmDspBool(inst,kBypassEcId);
  3693. const cmSample_t* fV = cmDspAudioBuf(ctx,inst,kFiltInEcId,0);
  3694. unsigned fN = cmDspAudioBufSmpCount(ctx,inst,kFiltInEcId,0);
  3695. const cmSample_t* uV = cmDspAudioBuf(ctx,inst,kUnfiltInEcId,0);
  3696. unsigned uN = cmDspAudioBufSmpCount(ctx,inst,kUnfiltInEcId,0);
  3697. cmSample_t* yV = cmDspAudioBuf(ctx,inst,kOutEcId,0);
  3698. unsigned yN = cmDspAudioBufSmpCount(ctx,inst,kOutEcId,0);
  3699. assert( fN==uN && fN==yN );
  3700. if( bypassFl )
  3701. {
  3702. cmVOS_Copy(yV,yN,uV);
  3703. }
  3704. else
  3705. {
  3706. if( fV !=NULL && uV != NULL && yV != NULL )
  3707. {
  3708. assert( uN == yN && fN == yN );
  3709. cmNlmsEcExec(p->r,uV,fV,yV,yN);
  3710. }
  3711. }
  3712. return rc;
  3713. }
  3714. cmDspRC_t _cmDspEchoCancelRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3715. {
  3716. cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3717. cmDspSetEvent(ctx,inst,evt);
  3718. if( p->r != NULL )
  3719. {
  3720. switch(evt->dstVarId)
  3721. {
  3722. case kMuEcId:
  3723. cmNlmsEcSetMu( p->r, cmDspReal(inst,kMuEcId));
  3724. break;
  3725. case kImpRespN_EcId:
  3726. cmNlmsEcSetIrN( p->r, cmDspUInt(inst,kImpRespN_EcId));
  3727. break;
  3728. case kDelayN_EcId:
  3729. cmNlmsEcSetDelayN( p->r, cmDspUInt(inst,kDelayN_EcId));
  3730. break;
  3731. case kBypassEcId:
  3732. printf("EC bypass:%i\n",cmDspBool(inst,kBypassEcId));
  3733. break;
  3734. }
  3735. }
  3736. printf("mu:%f dN:%i hN:%i\n",p->r->mu,p->r->delayN,p->r->hN);
  3737. return kOkDspRC;
  3738. }
  3739. cmDspClass_t* cmEchoCancelClassCons( cmDspCtx_t* ctx )
  3740. {
  3741. cmDspClassSetup(&_cmEchoCancelDC,ctx,"EchoCancel",
  3742. NULL,
  3743. _cmDspEchoCancelAlloc,
  3744. _cmDspEchoCancelFree,
  3745. _cmDspEchoCancelReset,
  3746. _cmDspEchoCancelExec,
  3747. _cmDspEchoCancelRecv,
  3748. NULL,
  3749. NULL,
  3750. "Echo canceller");
  3751. return &_cmEchoCancelDC;
  3752. }
  3753. //)