*/}}

la_audio.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  1. /*
  2. * Tiny la: A light weight game engine.
  3. * Copyright (C) 2023 Wu Yiming
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define MINIAUDIO_IMPLEMENTATION
  19. #include "miniaudio.h"
  20. #include "la_5.h"
  21. extern LA MAIN;
  22. laPropContainer* LA_PC_SYNTH;
  23. laPropContainer* LA_PC_CHANNEL;
  24. laNodeCategory* LA_NODE_CATEGORY_SYNTHESIZER;
  25. laNodeCategory* LA_NODE_CATEGORY_SYSTEM_SOUND;
  26. laBaseNodeType LA_IDN_INPUT;
  27. laBaseNodeType LA_IDN_PULSE;
  28. laBaseNodeType LA_IDN_FM;
  29. laBaseNodeType LA_IDN_VCA;
  30. laBaseNodeType LA_IDN_NOISE;
  31. laBaseNodeType LA_IDN_OUTPUT;
  32. laBaseNodeType LA_IDN_SCOPE;
  33. laBaseNodeType LA_IDN_ENVELOPE;
  34. laBaseNodeType LA_IDN_QUANTIZE;
  35. laBaseNodeType LA_IDN_SYNTH_DRIVER;
  36. laBaseNodeType LA_IDN_SYNTH_TRIGGER;
  37. laPropContainer* LA_PC_IDN_INPUT;
  38. laPropContainer* LA_PC_IDN_PULSE;
  39. laPropContainer* LA_PC_IDN_FM;
  40. laPropContainer* LA_PC_IDN_VCA;
  41. laPropContainer* LA_PC_IDN_NOISE;
  42. laPropContainer* LA_PC_IDN_OUTPUT;
  43. laPropContainer* LA_PC_IDN_SCOPE;
  44. laPropContainer* LA_PC_IDN_ENVELOPE;
  45. laPropContainer* LA_PC_IDN_QUANTIZE;
  46. laPropContainer* LA_PC_IDN_SYNTH_DRIVER;
  47. laPropContainer* LA_PC_IDN_SYNTH_TRIGGER;
  48. #define _2PI 6.283185307
  49. #define VAL2FREQ(val) \
  50. 16.0f*pow(2,val) // 0-10, 16-16384Hz
  51. #define VAL13 -0.218640286
  52. #define VAL27 0.78135971352
  53. #define VAL440 4.78135971352 // 2^FREQC*16=440
  54. #define VALHALF (1.0f/12.0f)
  55. #define WRAPPHASE(p) \
  56. while(p>_2PI){ p-=_2PI; }
  57. #define INITPACKET(arr) \
  58. if(!arr){ arr=memAcquireSimple(sizeof(real)*LA_SYNTH_PLEN); }
  59. #define INPUTPACKET(ptr,socket) \
  60. {if(socket->Source && socket->Source->DataType==(LA_PROP_FLOAT|LA_PROP_ARRAY) && socket->Source->ArrLen>=LA_SYNTH_PLEN){ \
  61. ptr=((real*)socket->Source->Data); }else{ ptr=0; } }
  62. #define FRAME_INTERVAL (MAIN.Audio->AudioFrameInterval)
  63. #define SAMPLE_RATE (MAIN.Audio->AudioSampleRate)
  64. void IDN_InputInit(laSynthNodeInput* n, int NoCreate){
  65. INITPACKET(n->rTime); INITPACKET(n->rTrigger); //INITPACKET(n->rSamplesL); INITPACKET(n->rSamplesR);
  66. if(NoCreate){
  67. if(!n->OutL) n->OutL=laCreateOutSocket(n,"L",LA_PROP_FLOAT|LA_PROP_ARRAY);
  68. if(!n->OutR) n->OutR=laCreateOutSocket(n,"R",LA_PROP_FLOAT|LA_PROP_ARRAY);
  69. return;
  70. }
  71. n->OutTime=laCreateOutSocket(n,"TIME",LA_PROP_FLOAT|LA_PROP_ARRAY);
  72. n->OutTrigger=laCreateOutSocket(n,"TRIGGER",LA_PROP_FLOAT|LA_PROP_ARRAY);
  73. strSafeSet(&n->Base.Name,"Input");
  74. }
  75. void IDN_InputDestroy(laSynthNodeInput* n){
  76. laDestroyOutSocket(n->OutTime); laDestroyOutSocket(n->OutTrigger);
  77. laDestroyOutSocket(n->OutL); laDestroyOutSocket(n->OutR); strSafeDestroy(&n->Base.Name);
  78. memFree(n->rTime); memFree(n->rTrigger); memFree(n->rSamplesL); memFree(n->rSamplesL);
  79. }
  80. int IDN_InputVisit(laSynthNodeInput* n, laNodeVisitInfo* vi){
  81. LA_GUARD_THIS_NODE(n,vi);
  82. LA_ADD_THIS_NODE(n,vi);
  83. return LA_DAG_FLAG_PERM;
  84. }
  85. int IDN_InputEval(laSynthNodeInput* n){
  86. laSynth*ss=MAIN.Audio->AudioEvalSynth;
  87. real time=ss->EvalTime,target_time=time+(real)LA_SYNTH_PLEN/(real)MAIN.Audio->AudioSampleRate;
  88. for(int i=0;i<LA_SYNTH_PLEN;i++){
  89. real fac=(real)i/(real)LA_SYNTH_PLEN;
  90. n->rTime[i] = tnsLinearItp(time,target_time,fac);
  91. }
  92. if(ss->EvalSamples==0){ n->rTrigger[0] = 10; }else{ n->rTrigger[0]=0; }
  93. n->OutTime->Data=n->rTime; n->OutTrigger->Data=n->rTrigger; n->OutTime->ArrLen=LA_SYNTH_PLEN; n->OutTrigger->ArrLen=LA_SYNTH_PLEN;
  94. n->OutL->Data=MAIN.Audio->InputSamples; n->OutR->Data=MAIN.Audio->InputSamples; n->OutL->ArrLen=LA_SYNTH_PLEN; n->OutR->ArrLen=LA_SYNTH_PLEN;
  95. return 1;
  96. }
  97. void IDN_InputCopy(laSynthNodeInput* new, laSynthNodeInput* old, int DoRematch){
  98. if(DoRematch){ return; }
  99. LA_IDN_OLD_DUPL(OutTime); LA_IDN_OLD_DUPL(OutTrigger);
  100. }
  101. void laui_InputNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  102. laColumn* c=laFirstColumn(uil); laSynthNodeInput*n=This->EndInstance;
  103. LA_BASE_NODE_HEADER(uil,c,This);
  104. laUiItem* b=laBeginRow(uil,c,0,0);
  105. laUiItem* ui=laShowLabel(uil,c,"Capture",0,0); ui->Expand=1; ui->Flags|=LA_TEXT_ALIGN_RIGHT;
  106. laShowNodeSocket(uil,c,This,"out_l",0)->Flags|=LA_UI_SOCKET_LABEL_E;
  107. laShowNodeSocket(uil,c,This,"out_r",0)->Flags|=LA_UI_SOCKET_LABEL_E;
  108. laEndRow(uil,b);
  109. b=laBeginRow(uil,c,0,0);
  110. ui=laShowLabel(uil,c,"Synth",0,0); ui->Expand=1; ui->Flags|=LA_TEXT_ALIGN_RIGHT;
  111. laShowNodeSocket(uil,c,This,"out_time",0)->Flags|=LA_UI_SOCKET_LABEL_E;
  112. laShowNodeSocket(uil,c,This,"out_trigger",0)->Flags|=LA_UI_SOCKET_LABEL_E;
  113. laEndRow(uil,b);
  114. }
  115. void IDN_PulseInit(laSynthNodePulse* n, int NoCreate){
  116. INITPACKET(n->rOut);
  117. if(NoCreate){ return; }
  118. n->InTrigger = laCreateInSocket("TRIGGER",0);
  119. n->InWidth = laCreateInSocket("WIDTH",0);
  120. n->InGate = laCreateInSocket("GATE",0);
  121. n->Out=laCreateOutSocket(n,"OUT",LA_PROP_FLOAT|LA_PROP_ARRAY);
  122. strSafeSet(&n->Base.Name,"Pulse");
  123. }
  124. void IDN_PulseDestroy(laSynthNodePulse* n){
  125. laDestroyOutSocket(n->Out); laDestroyInSocket(n->InWidth); laDestroyInSocket(n->InTrigger); laDestroyInSocket(n->InGate);
  126. strSafeDestroy(&n->Base.Name); memFree(n->rOut);
  127. }
  128. int IDN_PulseVisit(laSynthNodePulse* n, laNodeVisitInfo* vi){
  129. LA_GUARD_THIS_NODE(n,vi);
  130. if(LA_SRC_AND_PARENT(n->InTrigger)){ laBaseNode*bn=n->InTrigger->Source->Parent; LA_VISIT_NODE(bn,vi); }
  131. if(LA_SRC_AND_PARENT(n->InWidth)){ laBaseNode*bn=n->InWidth->Source->Parent; LA_VISIT_NODE(bn,vi); }
  132. if(LA_SRC_AND_PARENT(n->InGate)){ laBaseNode*bn=n->InGate->Source->Parent; LA_VISIT_NODE(bn,vi); }
  133. n->Out->Data = n->rOut; n->Out->ArrLen=LA_SYNTH_PLEN;
  134. LA_ADD_THIS_NODE(n,vi);
  135. return LA_DAG_FLAG_PERM;
  136. }
  137. int IDN_PulseEval(laSynthNodePulse* n){
  138. laSynth*ss=MAIN.Audio->AudioEvalSynth;
  139. real* trigger=0; real trig=n->iTrigger?100:-100; INPUTPACKET(trigger,n->InTrigger); LA_GET_SRC_AS_VALUE(trig,n->InTrigger);
  140. real* gate=0; real gt=n->rGate; INPUTPACKET(gate,n->InGate); LA_GET_SRC_AS_VALUE(gt,n->InGate);
  141. real* width=0; real w=n->rWidth; INPUTPACKET(width,n->InWidth);
  142. real wi=0; LA_GET_SRC_AS_VALUE(wi,n->InWidth); if(!width){ w=n->rWidth+wi*n->rInfluence; }
  143. for(int i=0;i<LA_SYNTH_PLEN;i++){
  144. if(trigger){ trig=trigger[i]; } if(gate){ gt=gate[i]; } if(width){ w=n->rWidth+width[i]*n->rInfluence; }
  145. if(!n->Triggered){ if(trig>gt){ n->Triggered=1; n->EvalSamples=1; } }
  146. else{ if(trig<=gt){ n->Triggered=0; } }
  147. int totalsamples=w*MAIN.Audio->AudioSampleRate;
  148. if(n->EvalSamples) n->EvalSamples++;
  149. n->rOut[i]= (n->Triggered || (n->EvalSamples > 0 && n->EvalSamples < totalsamples)) ? 10.0f : 0.0f;
  150. if(n->EvalSamples > totalsamples && !n->Triggered){ n->EvalSamples=0; }
  151. }
  152. return 1;
  153. }
  154. void IDN_PulseCopy(laSynthNodePulse* new, laSynthNodePulse* old, int DoRematch){
  155. if(DoRematch){ LA_IDN_NEW_LINK(InTrigger); LA_IDN_NEW_LINK(InWidth); LA_IDN_NEW_LINK(InGate); return; }
  156. LA_IDN_OLD_DUPL(Out); new->rGate = old->rGate; new->rInfluence=old->rInfluence; new->rWidth=old->rWidth;
  157. }
  158. void laui_PulseNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  159. laColumn* c=laFirstColumn(uil); laSynthNodePulse*n=This->EndInstance;
  160. LA_BASE_NODE_HEADER(uil,c,This);
  161. laUiItem* b;
  162. b=laBeginRow(uil,c,0,0);
  163. laShowNodeSocket(uil,c,This,"in_width",0)->Flags|=LA_UI_SOCKET_LABEL_W;
  164. laShowItem(uil,c,This,"influence");
  165. laShowItem(uil,c,This,"width")->Expand=1;
  166. laEndRow(uil,b);
  167. b=laBeginRow(uil,c,0,0);
  168. laShowItem(uil,c,This,"gate")->Expand=1;
  169. laShowNodeSocket(uil,c,This,"in_gate",0)->Flags|=LA_UI_SOCKET_LABEL_W;
  170. laShowNodeSocket(uil,c,This,"in_trigger",0)->Flags|=LA_UI_SOCKET_LABEL_E;
  171. laEndRow(uil,b);
  172. b=laBeginRow(uil,c,0,0);
  173. laShowSeparator(uil,c)->Expand=1;
  174. laShowItem(uil,c,This,"trigger")->Flags|=LA_UI_FLAGS_MOMENTARY|LA_UI_FLAGS_HIGHLIGHT;
  175. laShowItem(uil,c,This,"out")->Flags|=LA_UI_SOCKET_LABEL_W;
  176. laEndRow(uil,b);
  177. }
  178. void IDN_FMInit(laSynthNodeFM* n, int NoCreate){
  179. INITPACKET(n->OutSamples);
  180. if(NoCreate){ return; }
  181. n->InFrequency=laCreateInSocket("FREQ",0); n->Out=laCreateOutSocket(n,"OUT",LA_PROP_FLOAT|LA_PROP_ARRAY);
  182. strSafeSet(&n->Base.Name,"FM"); n->Frequency=4;
  183. }
  184. void IDN_FMDestroy(laSynthNodeFM* n){
  185. laDestroyInSocket(n->InFrequency); laDestroyOutSocket(n->Out); strSafeDestroy(&n->Base.Name); memFree(n->OutSamples);
  186. }
  187. int IDN_FMVisit(laSynthNodeFM* n, laNodeVisitInfo* vi){
  188. LA_GUARD_THIS_NODE(n,vi);
  189. if(LA_SRC_AND_PARENT(n->InFrequency)){ laBaseNode*bn=n->InFrequency->Source->Parent; LA_VISIT_NODE(bn,vi); }
  190. LA_ADD_THIS_NODE(n,vi);
  191. return LA_DAG_FLAG_PERM;
  192. }
  193. int IDN_FMEval(laSynthNodeFM* n){
  194. real f=n->Frequency, constf=0; real* inputf;
  195. INPUTPACKET(inputf,n->InFrequency); LA_GET_SRC_AS_VALUE(constf,n->InFrequency);
  196. for(int i=0;i<LA_SYNTH_PLEN;i++){
  197. real useF=inputf?(inputf[i]*n->rInfluence+f):(f+constf); if(n->Slow) useF-=10;
  198. n->Phase+=FRAME_INTERVAL*VAL2FREQ(useF)*_2PI;
  199. WRAPPHASE(n->Phase);
  200. n->OutSamples[i]=sin(n->Phase)*10;
  201. }
  202. n->Out->Data=n->OutSamples; n->Out->ArrLen=LA_SYNTH_PLEN;
  203. return 1;
  204. }
  205. void IDN_FMCopy(laSynthNodeFM* new, laSynthNodeFM* old, int DoRematch){
  206. if(DoRematch){ LA_IDN_NEW_LINK(InFrequency) return; }
  207. LA_IDN_OLD_DUPL(Out); new->Frequency=old->Frequency; new->Slow=old->Slow; new->rInfluence=old->rInfluence;
  208. }
  209. void laui_FMNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  210. laColumn* c=laFirstColumn(uil); laSynthNodeFM*n=This->EndInstance;
  211. LA_BASE_NODE_HEADER(uil,c,This);
  212. laUiItem* b=laBeginRow(uil,c,0,0);
  213. laShowItem(uil,c,This,"slow");
  214. laShowNodeSocket(uil,c,This,"in_frequency",0)->Flags|=LA_UI_SOCKET_LABEL_E;
  215. laShowItem(uil,c,This,"influence");
  216. laShowItem(uil,c,This,"frequency")->Expand=1;
  217. laShowNodeSocket(uil,c,This,"out",0);
  218. laEndRow(uil,b);
  219. }
  220. void IDN_VCAInit(laSynthNodeVCA* n, int NoCreate){
  221. INITPACKET(n->OutSamples);
  222. if(NoCreate){ return; }
  223. n->InAmp=laCreateInSocket("AMP",0); n->In=laCreateInSocket("IN",0);
  224. n->Out=laCreateOutSocket(n,"OUT",LA_PROP_FLOAT|LA_PROP_ARRAY);
  225. strSafeSet(&n->Base.Name,"FM"); n->Amp=10;
  226. }
  227. void IDN_VCADestroy(laSynthNodeVCA* n){
  228. laDestroyInSocket(n->InAmp); laDestroyInSocket(n->In);
  229. laDestroyOutSocket(n->Out); strSafeDestroy(&n->Base.Name); memFree(n->OutSamples);
  230. }
  231. int IDN_VCAVisit(laSynthNodeVCA* n, laNodeVisitInfo* vi){
  232. LA_GUARD_THIS_NODE(n,vi);
  233. if(LA_SRC_AND_PARENT(n->In)){ laBaseNode*bn=n->In->Source->Parent; LA_VISIT_NODE(bn,vi); }
  234. if(LA_SRC_AND_PARENT(n->InAmp)){ laBaseNode*bn=n->InAmp->Source->Parent; LA_VISIT_NODE(bn,vi); }
  235. LA_ADD_THIS_NODE(n,vi);
  236. return LA_DAG_FLAG_PERM;
  237. }
  238. int IDN_VCAEval(laSynthNodeVCA* n){
  239. real amp=n->Amp, constamp=amp; real* inputamp=0,*input=0;
  240. INPUTPACKET(input,n->In); INPUTPACKET(inputamp,n->InAmp);
  241. if(!inputamp) LA_GET_SRC_AS_VALUE(constamp,n->InAmp); constamp/=10;
  242. n->Out->Data=n->OutSamples; n->Out->ArrLen=LA_SYNTH_PLEN;
  243. if(!input){ memset(n->OutSamples,0,sizeof(real)*LA_SYNTH_PLEN); return 0; }
  244. for(int i=0;i<LA_SYNTH_PLEN;i++){
  245. real useA= inputamp?tnsInterpolate(1.0f,inputamp[i]/10.0f,n->rInfluence)*constamp:constamp;
  246. n->OutSamples[i]= input[i]*useA;
  247. }
  248. return 1;
  249. }
  250. void IDN_VCACopy(laSynthNodeVCA* new, laSynthNodeVCA* old, int DoRematch){
  251. if(DoRematch){ LA_IDN_NEW_LINK(InAmp) LA_IDN_NEW_LINK(In) return; }
  252. LA_IDN_OLD_DUPL(Out); new->Amp=old->Amp; new->rInfluence = old->rInfluence;
  253. }
  254. void laui_VCANode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  255. laColumn* c=laFirstColumn(uil); laSynthNodeVCA*n=This->EndInstance;
  256. LA_BASE_NODE_HEADER(uil,c,This);
  257. laColumn* cl,*cr; laSplitColumn(uil,c,0.3); cl=laLeftColumn(c,0); cr=laRightColumn(c,0);
  258. laUiItem* b=laBeginRow(uil,c,0,0);
  259. laShowNodeSocket(uil,c,This,"in",0)->Flags|=LA_UI_SOCKET_LABEL_E;
  260. laShowNodeSocket(uil,c,This,"in_amp",0);
  261. laShowItem(uil,c,This,"influence");
  262. laShowItem(uil,c,This,"amp")->Expand=1;
  263. laShowNodeSocket(uil,cr,This,"out",0);
  264. laEndRow(uil,b);
  265. }
  266. void IDN_NoiseInit(laSynthNodeNoise* n, int NoCreate){
  267. INITPACKET(n->OutWhiteSamples); INITPACKET(n->OutPinkSamples);
  268. n->PinkA[0]=0.02109238; n->PinkA[1]=0.07113478; n->PinkA[2]=0.68873558;
  269. n->PinkP[0]=0.3190; n->PinkP[1]=0.7756; n->PinkP[2]=0.9613;
  270. if(NoCreate){ return; }
  271. n->OutWhite=laCreateOutSocket(n,"WHITE",LA_PROP_FLOAT|LA_PROP_ARRAY);
  272. n->OutPink=laCreateOutSocket(n,"PINK",LA_PROP_FLOAT|LA_PROP_ARRAY);
  273. strSafeSet(&n->Base.Name,"Noise");
  274. }
  275. void IDN_NoiseDestroy(laSynthNodeNoise* n){
  276. laDestroyOutSocket(n->OutWhite); laDestroyOutSocket(n->OutPink); strSafeDestroy(&n->Base.Name);
  277. memFree(n->OutWhiteSamples); memFree(n->OutPinkSamples);
  278. }
  279. int IDN_NoiseVisit(laSynthNodeNoise* n, laNodeVisitInfo* vi){
  280. LA_GUARD_THIS_NODE(n,vi); LA_ADD_THIS_NODE(n,vi);
  281. return LA_DAG_FLAG_PERM;
  282. }
  283. int IDN_NoiseEval(laSynthNodeNoise* n){
  284. real RMI2 = 2.0 / (real)RAND_MAX; // + 1.0; // change for range [0,1)
  285. real offset = n->PinkA[0] + n->PinkA[1] + n->PinkA[2];
  286. for(int i=0;i<LA_SYNTH_PLEN;i++){
  287. real R1=(real)rand()/(real)RAND_MAX;
  288. real R2=(real)rand()/(real)RAND_MAX;
  289. real X=(real)sqrt(-2.0f*log(R1))*cos(2.0f*TNS_PI*R2);
  290. n->OutWhiteSamples[i]=X*2.5;
  291. real temp; for(int i=0;i<3;i++){ temp=(real)rand();
  292. n->PinkStates[i] = n->PinkP[i] * (n->PinkStates[i] - temp) + temp;
  293. }
  294. n->OutPinkSamples[i]=(n->PinkA[0]*n->PinkStates[0]+
  295. n->PinkA[1]*n->PinkStates[1]+
  296. n->PinkA[2]*n->PinkStates[2])*RMI2-offset;
  297. n->OutPinkSamples[i]*=40;
  298. }
  299. n->OutWhite->Data=n->OutWhiteSamples; n->OutWhite->ArrLen=LA_SYNTH_PLEN;
  300. n->OutPink->Data=n->OutPinkSamples; n->OutPink->ArrLen=LA_SYNTH_PLEN;
  301. return 1;
  302. }
  303. void IDN_NoiseCopy(laSynthNodeNoise* new, laSynthNodeNoise* old, int DoRematch){
  304. if(DoRematch){ return; }
  305. LA_IDN_OLD_DUPL(OutWhite);
  306. }
  307. void laui_NoiseNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  308. laColumn* c=laFirstColumn(uil); laSynthNodeNoise*n=This->EndInstance;
  309. LA_BASE_NODE_HEADER(uil,c,This);
  310. laUiItem* b=laBeginRow(uil,c,0,0);
  311. laShowNodeSocket(uil,c,This,"out_white",0)->Flags|=LA_UI_SOCKET_LABEL_N;
  312. laShowNodeSocket(uil,c,This,"out_pink",0)->Flags|=LA_UI_SOCKET_LABEL_N;
  313. laEndRow(uil,b);
  314. }
  315. void IDN_ScopeInit(laSynthNodeScope* n, int NoCreate){
  316. if(!n->Display1){ n->Display1=memAcquireSimple(sizeof(real)*LA_SYNTH_PLEN*2); }
  317. if(!n->Display2){ n->Display2=memAcquireSimple(sizeof(real)*LA_SYNTH_PLEN*2); }
  318. laSpinInit(&n->Lock);
  319. if(NoCreate){ return; }
  320. n->In1=laCreateInSocket("CH1",0); n->In2=laCreateInSocket("CH2",0);
  321. strSafeSet(&n->Base.Name,"SCOPE"); n->Time=5; n->Brightness1=0.7f; n->Brightness2=0.4f;
  322. n->Gain1=1; n->Gain2=1;
  323. }
  324. void IDN_ScopeDestroy(laSynthNodeScope* n){
  325. laDestroyInSocket(n->In1); laDestroyOutSocket(n->In2); strSafeDestroy(&n->Base.Name);
  326. memFree(n->Display1); memFree(n->Display2);
  327. laSpinDestroy(&n->Lock);
  328. }
  329. int IDN_ScopeVisit(laSynthNodeScope* n, laNodeVisitInfo* vi){
  330. LA_GUARD_THIS_NODE(n,vi);
  331. if(LA_SRC_AND_PARENT(n->In1)){ laBaseNode*bn=n->In1->Source->Parent; LA_VISIT_NODE(bn,vi); }
  332. if(LA_SRC_AND_PARENT(n->In2)){ laBaseNode*bn=n->In2->Source->Parent; LA_VISIT_NODE(bn,vi); }
  333. LA_ADD_THIS_NODE(n,vi);
  334. return LA_DAG_FLAG_PERM;
  335. }
  336. int IDN_ScopeEval(laSynthNodeScope* n){
  337. if(!n->Display1){ n->Display1=memAcquireSimple(sizeof(real)*LA_SYNTH_PLEN*2); }
  338. if(!n->Display2){ n->Display2=memAcquireSimple(sizeof(real)*LA_SYNTH_PLEN*2); }
  339. real* ch1,*ch2; INPUTPACKET(ch1,n->In1); INPUTPACKET(ch2,n->In2);
  340. int Frame=1<<(n->Time-1); TNS_CLAMP(Frame,1,LA_SYNTH_PLEN);
  341. int Times=LA_SYNTH_PLEN/Frame; int sp=0; laSpinLock(&n->Lock);
  342. for(int t=0;t<Times;t++){
  343. real smin1=FLT_MAX,smax1=-FLT_MAX,smin2=FLT_MAX,smax2=-FLT_MAX;
  344. for(int i=0;i<Frame;i++){
  345. if(ch1){ smin1=TNS_MIN2(smin1,ch1[sp]); smax1=TNS_MAX2(smax1,ch1[sp]); }
  346. if(ch2){ smin2=TNS_MIN2(smin2,ch2[sp]); smax2=TNS_MAX2(smax2,ch2[sp]); }
  347. sp++;
  348. }
  349. if(!ch1){smin1=smax1=0; LA_GET_SRC_AS_VALUE(smin1,n->In1); smax1=smin1; }
  350. if(!ch2){smin2=smax2=0; LA_GET_SRC_AS_VALUE(smin2,n->In2); smax2=smin2; }
  351. int ns=n->NextSample;
  352. n->Display1[ns]=smin1; n->Display1[ns+LA_SYNTH_PLEN]=smax1;
  353. n->Display2[ns]=smin2; n->Display2[ns+LA_SYNTH_PLEN]=smax2;
  354. n->NextSample++; if(n->NextSample>=LA_SYNTH_PLEN) n->NextSample=0;
  355. }
  356. n->FromSynth=MAIN.Audio->AudioEvalSynth;
  357. laSpinUnlock(&n->Lock);
  358. return 1;
  359. }
  360. void IDN_ScopeCopy(laSynthNodeScope* new, laSynthNodeScope* old, int DoRematch){
  361. if(DoRematch){ LA_IDN_NEW_LINK(In1) LA_IDN_NEW_LINK(In2) return; }
  362. new->Brightness1=old->Brightness1; new->Brightness2=old->Brightness2;
  363. new->Gain1=old->Gain1; new->Gain2=old->Gain2;
  364. new->Offset1=old->Offset1; new->Offset2=old->Offset2;
  365. new->Time=old->Time;
  366. }
  367. void laui_ScopeNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  368. laColumn* c=laFirstColumn(uil); laSynthNodeScope*n=This->EndInstance;
  369. LA_BASE_NODE_HEADER(uil,c,This);
  370. laColumn* cl,*cr; laSplitColumn(uil,c,0.5); cl=laLeftColumn(c,0); cr=laRightColumn(c,0);
  371. laUiItem* b=laBeginRow(uil,c,0,0);
  372. laUiItem* u=laShowItem(uil,c,This,"gain1");u->Flags|=LA_UI_FLAGS_EXPAND; u->Expand=1;
  373. laShowNodeSocket(uil,c,This,"in1",0)->Flags|=LA_UI_SOCKET_LABEL_W;
  374. laEndRow(uil,b);
  375. b=laBeginRow(uil,c,0,0);
  376. u=laShowItem(uil,c,This,"gain2");u->Flags|=LA_UI_FLAGS_EXPAND; u->Expand=1;
  377. laShowNodeSocket(uil,c,This,"in2",0)->Flags|=LA_UI_SOCKET_LABEL_W;
  378. laEndRow(uil,b);
  379. laShowItem(uil,c,This,"scope")->Extra->HeightCoeff=6;
  380. laShowLabel(uil,cl,"CH1",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
  381. laShowItem(uil,cl,This,"bright1"); laShowItem(uil,cl,This,"offset1");
  382. laShowLabel(uil,cr,"CH2",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
  383. laShowItem(uil,cr,This,"bright2"); laShowItem(uil,cr,This,"offset2");
  384. laShowItem(uil,c,This,"time")->Flags|=LA_UI_FLAGS_EXPAND;
  385. }
  386. void IDN_OutputInit(laSynthNodeOutput* n, int NoCreate){
  387. if(NoCreate){
  388. laAudioChannel* outchannel = laGetAudioChannel(SSTR(n->SendName));
  389. if(outchannel) memAssignRef(n,&n->Send,outchannel);
  390. return; }
  391. n->In=laCreateInSocket("IN",0); strSafeSet(&n->Base.Name,"Output");
  392. }
  393. void IDN_OutputDestroy(laSynthNodeOutput* n){
  394. laDestroyInSocket(n->In); strSafeDestroy(&n->Base.Name);
  395. }
  396. int IDN_OutputVisit(laSynthNodeOutput* n, laNodeVisitInfo* vi){
  397. if(LA_SRC_AND_PARENT(n->In)){ laBaseNode*bn=n->In->Source->Parent; LA_VISIT_NODE(bn,vi); }
  398. LA_ADD_THIS_NODE(n,vi);
  399. laAudioChannel* outchannel = laGetAudioChannel(SSTR(n->SendName));
  400. memAssignRef(n,&n->Send,outchannel);
  401. return LA_DAG_FLAG_PERM;
  402. }
  403. int IDN_OutputEval(laSynthNodeOutput* n){
  404. if(!n->Send)return 1;
  405. real* input; INPUTPACKET(input,n->In); if(!input) return 1;
  406. real Samples[LA_SYNTH_PLEN];
  407. for(int i=0;i<LA_SYNTH_PLEN;i++){
  408. n->Send->Samples[i] += input[i];
  409. }
  410. return 1;
  411. }
  412. void IDN_OutputCopy(laSynthNodeOutput* new, laSynthNodeOutput* old, int DoRematch){
  413. if(DoRematch){ LA_IDN_NEW_LINK(In) return; }
  414. strSafeSet(&new->SendName,SSTR(old->SendName)); new->Send=old->Send;
  415. }
  416. void laui_OutputNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  417. laColumn* c=laFirstColumn(uil); laSynthNodeOutput*n=This->EndInstance;
  418. LA_BASE_NODE_HEADER(uil,c,This);
  419. laColumn* cl,*cr; laSplitColumn(uil,c,0.3); cl=laLeftColumn(c,0); cr=laRightColumn(c,0);
  420. laUiItem* b=laBeginRow(uil,c,0,0);
  421. laShowNodeSocket(uil,c,This,"in",0)->Flags|=LA_UI_SOCKET_LABEL_E;
  422. laShowLabel(uil,c,"🕪",0,0);
  423. laShowItemFull(uil,c,This,"send_channel_selector",LA_WIDGET_COLLECTION_SELECTOR,0,laui_IdentifierOnly,0)->Flags|=LA_UI_COLLECTION_SIMPLE_SELECTOR;
  424. laShowItem(uil,c,This,"send_channel_name")->Expand = 1;
  425. laEndRow(uil,b);
  426. }
  427. void IDN_EnvelopeInit(laSynthNodeEnvelope* n, int NoCreate){
  428. if(!NoCreate){ strSafeSet(&n->Base.Name,"Envelope");
  429. n->Attack=laCreateInSocket("ATTACK",0); n->Delay=laCreateInSocket("DELAY",0);
  430. n->Sustain=laCreateInSocket("SUSTAIN",0); n->Release=laCreateInSocket("RELEASE",0);
  431. n->Trigger=laCreateInSocket("TRIGGER",0); n->Out=laCreateOutSocket(n, "OUT",0);
  432. n->Time=20; n->rAttack=1; n->rDelay=5; n->rSustain=5; n->rRelease=7; n->rGate=5;
  433. }
  434. if(!n->Gate){ n->Gate=laCreateInSocket("GATE",0); }
  435. if(!n->Restart){ n->Restart=laCreateInSocket("RESTART",0); }
  436. if(!n->OutSamples){ n->OutSamples=memAcquireSimple(sizeof(real)*LA_SYNTH_PLEN); }
  437. n->Out->Data = n->OutSamples; n->Out->DataType = LA_PROP_FLOAT|LA_PROP_ARRAY; n->Out->ArrLen=LA_SYNTH_PLEN;
  438. }
  439. void IDN_EnvelopeDestroy(laSynthNodeEnvelope* n){
  440. laDestroyInSocket(n->Attack); laDestroyInSocket(n->Delay); laDestroyInSocket(n->Gate);
  441. laDestroyInSocket(n->Sustain); laDestroyInSocket(n->Release);
  442. laDestroyInSocket(n->Trigger); laDestroyOutSocket(n->Out);
  443. memFree(n->OutSamples);
  444. strSafeDestroy(&n->Base.Name);
  445. }
  446. int IDN_EnvelopeVisit(laSynthNodeEnvelope* n, laNodeVisitInfo* vi){
  447. LA_GUARD_THIS_NODE(n,vi);
  448. if(LA_SRC_AND_PARENT(n->Attack)){ laBaseNode*bn=n->Attack->Source->Parent; LA_VISIT_NODE(bn,vi); }
  449. if(LA_SRC_AND_PARENT(n->Delay)){ laBaseNode*bn=n->Delay->Source->Parent; LA_VISIT_NODE(bn,vi); }
  450. if(LA_SRC_AND_PARENT(n->Sustain)){ laBaseNode*bn=n->Sustain->Source->Parent; LA_VISIT_NODE(bn,vi); }
  451. if(LA_SRC_AND_PARENT(n->Release)){ laBaseNode*bn=n->Release->Source->Parent; LA_VISIT_NODE(bn,vi); }
  452. if(LA_SRC_AND_PARENT(n->Trigger)){ laBaseNode*bn=n->Trigger->Source->Parent; LA_VISIT_NODE(bn,vi); }
  453. if(LA_SRC_AND_PARENT(n->Restart)){ laBaseNode*bn=n->Restart->Source->Parent; LA_VISIT_NODE(bn,vi); }
  454. LA_ADD_THIS_NODE(n,vi);
  455. return LA_DAG_FLAG_PERM;
  456. }
  457. int IDN_EnvelopeEval(laSynthNodeEnvelope* n){
  458. int trigger_at=-1; real trig=n->bTrigger?n->bTrigger*100:-100; real res=0;
  459. real* trigger; INPUTPACKET(trigger,n->Trigger); if(!trigger){ LA_GET_SRC_AS_VALUE(trig,n->Trigger); }
  460. real* restart; INPUTPACKET(restart,n->Restart); if(!restart){ LA_GET_SRC_AS_VALUE(res,n->Restart); }
  461. real *att,*del,*sus,*rel,*gat;
  462. real attack=n->rAttack,delay=n->rDelay,sustain=n->rSustain,release=n->rRelease;
  463. real gate = n->rGate; INPUTPACKET(gat,n->Gate); if(!gat) LA_GET_SRC_AS_VALUE(gate,n->Gate);
  464. INPUTPACKET(att,n->Attack); if(!att) LA_GET_SRC_AS_VALUE(attack,n->Attack);
  465. INPUTPACKET(del,n->Delay); if(!del) LA_GET_SRC_AS_VALUE(delay,n->Delay);
  466. INPUTPACKET(sus,n->Sustain); if(!sus) LA_GET_SRC_AS_VALUE(sustain,n->Sustain);
  467. INPUTPACKET(rel,n->Release); if(!rel) LA_GET_SRC_AS_VALUE(release,n->Release);
  468. attack=attack?pow(attack/10,2):0; delay=delay?pow(delay/10,2):0; release=release?pow(release/10,2):0;
  469. for(int i=0;i<LA_SYNTH_PLEN;i++){
  470. if(trigger){ trig=trigger[i]; } if(gat){ gate=gat[i]; } if(restart){ res=restart[i]; }
  471. if(att){ attack=att[i]*n->iAttack+n->rAttack; }
  472. if(del){ delay=del[i]*n->iDelay+n->rDelay; }
  473. if(sus){ sustain=sus[i]*n->iSustain+n->rSustain; }
  474. if(rel){ release=rel[i]*n->iRelease+n->rRelease; }
  475. if(res){ n->Time=0; n->ReleaseTime=-10000; n->Triggered=0;}
  476. if(!n->Triggered){ if(trig > gate){ n->Triggered = 1; n->Time = 0; n->ReleaseTime=10000; n->AtLevel=0; } }
  477. else{ if(trig < gate-0.0001f){ n->Triggered = 0; n->ReleaseTime = n->Time; } }
  478. if(n->Time < n->ReleaseTime){
  479. if(n->Time<=attack){
  480. n->OutSamples[i]=10.0f*pow(n->Time/attack,0.3);
  481. }elif(n->Time<=delay + attack){
  482. real fac=pow((n->Time-attack)/delay,0.3);
  483. n->OutSamples[i]= tnsLinearItp(10.0f, sustain, fac);
  484. }else{
  485. n->OutSamples[i]=sustain;
  486. }
  487. n->AtLevel=n->OutSamples[i];
  488. }else{
  489. if(n->Time - n->ReleaseTime < release){
  490. real fac=pow((n->Time - n->ReleaseTime)/release,0.3);
  491. n->OutSamples[i]= tnsLinearItp(n->AtLevel,0.0f, fac);
  492. }else{
  493. n->OutSamples[i]=0;
  494. }
  495. }
  496. n->Time+=FRAME_INTERVAL;
  497. }
  498. return 1;
  499. }
  500. void IDN_EnvelopeCopy(laSynthNodeEnvelope* new, laSynthNodeEnvelope* old, int DoRematch){
  501. if(DoRematch){
  502. LA_IDN_NEW_LINK(Attack) LA_IDN_NEW_LINK(Delay)
  503. LA_IDN_NEW_LINK(Sustain) LA_IDN_NEW_LINK(Release) LA_IDN_NEW_LINK(Trigger) return;
  504. }
  505. LA_IDN_OLD_DUPL(Out);
  506. new->rGate=old->rGate;
  507. new->rAttack=old->rAttack;new->rSustain=old->rSustain;new->rDelay=old->rDelay;new->rRelease=old->rRelease;
  508. new->iAttack=old->iAttack;new->iSustain=old->iSustain;new->iDelay=old->iDelay;new->iRelease=old->rRelease;
  509. }
  510. void laui_EnvelopeNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  511. laColumn* c=laFirstColumn(uil); laSynthNodeEnvelope*n=This->EndInstance;
  512. LA_BASE_NODE_HEADER(uil,c,This);
  513. laUiItem* b,*b1;
  514. #define ADSR_ROW(what) \
  515. b=laBeginRow(uil,c,0,0);{ \
  516. laShowNodeSocket(uil,c,This,"in_" what,0)->Flags|=LA_UI_SOCKET_LABEL_E; \
  517. laUiItem* value=laShowItem(uil,c,This,"i" what); \
  518. value=laShowItem(uil,c,This,what);value->Expand=1; \
  519. }laEndRow(uil,b);
  520. ADSR_ROW("attack") ADSR_ROW("delay") ADSR_ROW("sustain") ADSR_ROW("release")
  521. b=laBeginRow(uil,c,0,0);
  522. laShowItem(uil,c,This,"gate")->Expand=1;
  523. laShowNodeSocket(uil,c,This,"in_gate",0)->Flags|=LA_UI_SOCKET_LABEL_W;
  524. laShowNodeSocket(uil,c,This,"in_trigger",0)->Flags|=LA_UI_SOCKET_LABEL_E;
  525. laEndRow(uil,b);
  526. b=laBeginRow(uil,c,0,0);
  527. laShowSeparator(uil,c)->Expand=1;
  528. laShowItem(uil,c,This,"in_restart")->Flags|=LA_UI_SOCKET_LABEL_E;
  529. laShowItem(uil,c,This,"trigger")->Flags|=LA_UI_FLAGS_MOMENTARY|LA_UI_FLAGS_HIGHLIGHT;
  530. laShowItem(uil,c,This,"out")->Flags|=LA_UI_SOCKET_LABEL_W;
  531. laEndRow(uil,b);
  532. }
  533. void IDN_QuantizeInit(laSynthNodeQuantize* n, int NoCreate){
  534. if(!NoCreate){ strSafeSet(&n->Base.Name,"Envelope");
  535. n->In=laCreateInSocket("IN",0); n->Out=laCreateOutSocket(n, "OUT",0);
  536. }
  537. if(!n->OutSamples){ n->OutSamples=memAcquireSimple(sizeof(real)*LA_SYNTH_PLEN); }
  538. n->Out->Data = n->OutSamples; n->Out->DataType = LA_PROP_FLOAT|LA_PROP_ARRAY; n->Out->ArrLen=LA_SYNTH_PLEN;
  539. int Enabled[12]={1,0,1,0,1,1,0,1,0,1,0,1};
  540. memcpy(n->EnabledBits,Enabled,sizeof(Enabled));
  541. }
  542. void IDN_QuantizeDestroy(laSynthNodeQuantize* n){
  543. laDestroyInSocket(n->In); laDestroyOutSocket(n->Out);
  544. memFree(n->OutSamples);
  545. strSafeDestroy(&n->Base.Name);
  546. }
  547. int IDN_QuantizeVisit(laSynthNodeQuantize* n, laNodeVisitInfo* vi){
  548. LA_GUARD_THIS_NODE(n,vi);
  549. if(LA_SRC_AND_PARENT(n->In)){ laBaseNode*bn=n->In->Source->Parent; LA_VISIT_NODE(bn,vi); }
  550. LA_ADD_THIS_NODE(n,vi);
  551. return LA_DAG_FLAG_PERM;
  552. }
  553. static const real QTABLE[12]={ VAL13-VALHALF*9, VAL13-VALHALF*8, VAL13-VALHALF*7, VAL13-VALHALF*6,
  554. VAL13-VALHALF*5, VAL13-VALHALF*4, VAL13-VALHALF*3, VAL13-VALHALF*2,
  555. VAL13-VALHALF*1, VAL13, VAL13+VALHALF*1, VAL13+VALHALF*2 };
  556. static void set_quantize_out_bit(laSynthNodeQuantize* n, int which){
  557. for(int i=0;i<12;i++){
  558. if(n->EnabledBits[i]){ n->EnabledBits[i]=1; }
  559. if(i==which && n->EnabledBits){ n->EnabledBits[i]=3; }
  560. }
  561. }
  562. int IDN_QuantizeEval(laSynthNodeQuantize* n){ real cv0=QTABLE[0]+5; /*C4*/ LA_GET_SRC_AS_VALUE(cv0, n->In);
  563. real* cv; INPUTPACKET(cv,n->In); real dists[12];
  564. for(int i=0;i<LA_SYNTH_PLEN;i++){
  565. int octave = (int)(cv0-QTABLE[0]); int minkey=0;
  566. if(cv){ cv0=cv[i]; }
  567. real use_cv = cv0 - octave;
  568. for(int j=0;j<12;j++){ dists[j] = fabs(use_cv - QTABLE[j]); }
  569. for(int j=1;j<12;j++){ if(n->EnabledBits[j] && dists[j] < dists[minkey]){ minkey=j; } }
  570. set_quantize_out_bit(n,minkey);
  571. n->OutSamples[i] = QTABLE[minkey]+octave;
  572. }
  573. return 1;
  574. }
  575. void IDN_QuantizeCopy(laSynthNodeQuantize* new, laSynthNodeQuantize* old, int DoRematch){
  576. if(DoRematch){
  577. LA_IDN_NEW_LINK(In) return;
  578. }
  579. LA_IDN_OLD_DUPL(Out);
  580. memcpy(new->EnabledBits,old->EnabledBits,sizeof(int)*12);
  581. }
  582. void laui_QuantizeNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  583. laColumn* c=laFirstColumn(uil); laSynthNodeQuantize*n=This->EndInstance;
  584. LA_BASE_NODE_HEADER(uil,c,This);
  585. laUiItem* b;
  586. b=laBeginRow(uil,c,0,0);
  587. laUiItem* ui=laShowItem(uil,c,This,"enabled_keys");
  588. ui->Flags|=LA_UI_FLAGS_CYCLE|LA_UI_FLAGS_HIGHLIGHT|LA_UI_FLAGS_TRANSPOSE|LA_UI_FLAGS_ICON; ui->Expand=1;
  589. laShowNodeSocket(uil,c,This,"in",0)->Flags|=LA_UI_SOCKET_LABEL_W;
  590. laShowNodeSocket(uil,c,This,"out",0)->Flags|=LA_UI_SOCKET_LABEL_W;
  591. laEndRow(uil,b);
  592. }
  593. void IDN_SynthDriverInit(laSynthNodeDriver* n, int NoCreate){
  594. if(NoCreate){ return; }
  595. n->Prev=laCreateInSocket("PREV",0); n->Next=laCreateOutSocket(n,"NEXT",0);
  596. n->InTransport=laCreateInSocket("TRANSPORT",LA_PROP_FLOAT|LA_PROP_ARRAY);
  597. n->InReset=laCreateInSocket("RESET",LA_PROP_FLOAT|LA_PROP_ARRAY);
  598. strSafeSet(&n->Base.Name,"Sound Player");
  599. }
  600. void IDN_SynthDriverDestroy(laSynthNodeDriver* n){
  601. laDestroyInSocket(n->InTransport); laDestroyInSocket(n->InReset); strSafeDestroy(&n->Base.Name);
  602. laDestroyInSocket(n->Prev); laDestroyOutSocket(n->Next);
  603. }
  604. int IDN_SynthDriverVisit(laSynthNodeDriver* n, laNodeVisitInfo* vi){
  605. LA_GUARD_THIS_NODE(n,vi);
  606. if(LA_SRC_AND_PARENT(n->Prev)){ laBaseNode*bn=n->Prev->Source->Parent; LA_VISIT_NODE(bn,vi); }
  607. if(LA_SRC_AND_PARENT(n->InTransport)){ laBaseNode*bn=n->InTransport->Source->Parent; LA_VISIT_NODE(bn,vi); }
  608. if(LA_SRC_AND_PARENT(n->InReset)){ laBaseNode*bn=n->InReset->Source->Parent; LA_VISIT_NODE(bn,vi); }
  609. LA_ADD_THIS_NODE(n,vi);
  610. return LA_DAG_FLAG_PERM;
  611. }
  612. int IDN_SynthDriverEval(laSynthNodeDriver* n){
  613. laSynth*ss=n->Target; if(!ss){ return 0; }
  614. int trans=n->iTransport; LA_GET_SRC_AS_VALUE(trans,n->InTransport);
  615. int res=n->iReset; LA_GET_SRC_AS_VALUE(res,n->InReset); int dif=0;
  616. if(n->AlreadyReset && res==0){ n->AlreadyReset=0; res=0; }
  617. elif(!n->AlreadyReset && res!=0){ n->AlreadyReset=1; }
  618. else{ res=0; }
  619. laSpinLock(&ss->Lock);
  620. if(ss->Playing!=trans){ dif=1; }
  621. ss->Playing = trans; if(res){ ss->EvalSamples=0; ss->EvalTime=0; }
  622. laSpinUnlock(&ss->Lock);
  623. if(dif){ laNotifyInstanceUsers(ss); }
  624. return 1;
  625. }
  626. void IDN_SynthDriverCopy(laSynthNodeDriver* new, laSynthNodeDriver* old, int DoRematch){
  627. if(DoRematch){ LA_IDN_NEW_LINK(InTransport); LA_IDN_NEW_LINK(InReset); return; }
  628. new->iTransport = old->iTransport; new->iReset = old->iReset;
  629. memAssignRef(new,&new->Target,old->Target);
  630. }
  631. void laui_SynthDriverNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  632. laColumn* c=laFirstColumn(uil),*cl,*cr,*cll,*clr;
  633. LA_BASE_NODE_HEADER(uil,c,This);
  634. laSplitColumn(uil,c,0.6); cl=laLeftColumn(c,0); cr=laRightColumn(c,3);
  635. //laSplitColumn(uil,cl,0.4); cll=laLeftColumn(cl,3); clr=laRightColumn(cl,0);
  636. laUiItem* b2, *rui;
  637. laShowNodeSocket(uil,cr,This,"prev",0)->Flags|=LA_UI_SOCKET_LABEL_W;
  638. laShowNodeSocket(uil,cr,This,"next",0)->Flags|=LA_UI_SOCKET_LABEL_W;
  639. laUiItem* b=laBeginRow(uil,cl,0,0);
  640. laShowNodeSocket(uil,cl,This,"in_transport",0); b2=laOnConditionThat(uil,c,laNot(laPropExpression(This,"in_transport.source")));{
  641. laShowItemFull(uil,cl,This,"transport",0,"text=Transport",0,0)->Expand=1;
  642. }laElse(uil,b2);{
  643. laShowLabel(uil,cl,"Transport",0,0)->Expand=1;
  644. }laEndCondition(uil,b2);
  645. laShowNodeSocket(uil,cl,This,"in_reset",0); b2=laOnConditionThat(uil,c,laNot(laPropExpression(This,"in_reset.source")));{
  646. laUiItem* u=laShowItemFull(uil,cl,This,"reset",0,"text=Reset",0,0);u->Expand=1;u->Flags|=LA_UI_FLAGS_MOMENTARY;
  647. }laElse(uil,b2);{
  648. laShowLabel(uil,cl,"Reset",0,0)->Expand=1;
  649. }laEndCondition(uil,b2);
  650. laEndRow(uil,b);
  651. laShowItemFull(uil,cl,This,"target",LA_WIDGET_COLLECTION_SELECTOR,0,laui_IdentifierOnly,0);
  652. }
  653. void IDN_SynthTriggerInit(laSynthNodeTrigger* n, int NoCreate){
  654. if(NoCreate){ return; }
  655. n->Prev=laCreateInSocket("PREV",0); n->Next=laCreateOutSocket(n,"NEXT",0);
  656. n->InTrigger=laCreateInSocket("TRIGGER",LA_PROP_FLOAT|LA_PROP_ARRAY);
  657. strSafeSet(&n->Base.Name,"Sound Trigger"); n->TriggerEdge=1;
  658. }
  659. void IDN_SynthTriggerDestroy(laSynthNodeTrigger* n){
  660. laDestroyInSocket(n->InTrigger); strSafeDestroy(&n->Base.Name);
  661. laDestroyInSocket(n->Prev); laDestroyOutSocket(n->Next);
  662. }
  663. int IDN_SynthTriggerVisit(laSynthNodeTrigger* n, laNodeVisitInfo* vi){
  664. LA_GUARD_THIS_NODE(n,vi);
  665. if(LA_SRC_AND_PARENT(n->Prev)){ laBaseNode*bn=n->Prev->Source->Parent; LA_VISIT_NODE(bn,vi); }
  666. if(LA_SRC_AND_PARENT(n->InTrigger)){ laBaseNode*bn=n->InTrigger->Source->Parent; LA_VISIT_NODE(bn,vi); }
  667. LA_ADD_THIS_NODE(n,vi);
  668. return LA_DAG_FLAG_PERM;
  669. }
  670. int IDN_SynthTriggerEval(laSynthNodeTrigger* n){
  671. laSynth*ss=n->Target; if(!ss){ return 0; }
  672. real trig=0; real* trigger=0; INPUTPACKET(trigger,n->InTrigger); LA_GET_SRC_AS_VALUE(trig,n->InTrigger);
  673. if(MAIN.Audio->AudioEvalSynth == ss){ return 0; }
  674. if(trigger){
  675. for(int i=0;i<LA_SYNTH_PLEN;i++){
  676. if(trigger[i]>=5){
  677. if(n->TriggerEdge){ if(!n->Triggered){ n->Triggered=1; laSynthTriggerLater(ss); } }
  678. else{ laSynthTriggerLater(ss); }
  679. }else{ n->Triggered=0; }
  680. }
  681. }else{
  682. if(trig>=0.5){
  683. if(n->TriggerEdge){ if(!n->Triggered){ n->Triggered=1; laSynthTriggerLater(ss); } }
  684. else{ laSynthTriggerLater(ss); }
  685. }else{ n->Triggered=0; }
  686. }
  687. return 1;
  688. }
  689. void IDN_SynthTriggerCopy(laSynthNodeTrigger* new, laSynthNodeTrigger* old, int DoRematch){
  690. if(DoRematch){ LA_IDN_NEW_LINK(InTrigger); return; }
  691. new->TriggerEdge = old->TriggerEdge;
  692. memAssignRef(new,&new->Target,old->Target);
  693. }
  694. void laui_SynthTriggerNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  695. laColumn* c=laFirstColumn(uil),*cl,*cr,*cll,*clr;
  696. LA_BASE_NODE_HEADER(uil,c,This);
  697. laSplitColumn(uil,c,0.6); cl=laLeftColumn(c,0); cr=laRightColumn(c,3);
  698. //laSplitColumn(uil,cl,0.4); cll=laLeftColumn(cl,3); clr=laRightColumn(cl,0);
  699. laUiItem* b2, *rui;
  700. laShowNodeSocket(uil,cr,This,"prev",0)->Flags|=LA_UI_SOCKET_LABEL_W;
  701. laShowNodeSocket(uil,cr,This,"next",0)->Flags|=LA_UI_SOCKET_LABEL_W;
  702. laUiItem* b=laBeginRow(uil,cl,0,0);
  703. laShowItemFull(uil,cl,This,"trigger_edge",0,0,0,0)->Flags|=LA_UI_FLAGS_ICON;
  704. laShowNodeSocket(uil,cl,This,"in_trigger",0);
  705. laShowItemFull(uil,cl,This,"trigger",0,"text=Trigger",0,0)->Flags|=LA_UI_FLAGS_MOMENTARY|LA_UI_FLAGS_HIGHLIGHT;
  706. laShowSeparator(uil,cl)->Expand=1;
  707. laEndRow(uil,b);
  708. laShowItemFull(uil,cl,This,"target",LA_WIDGET_COLLECTION_SELECTOR,0,laui_IdentifierOnly,0);
  709. }
  710. void laRebuildSynthGraphs(int current_only){
  711. if(MAIN.Audio->CurrentSynth && current_only){
  712. laSpinLock(&MAIN.Audio->CurrentSynth->Lock);
  713. laRebuildPageEval(MAIN.Audio->CurrentSynth->Page);
  714. laSpinUnlock(&MAIN.Audio->CurrentSynth->Lock);
  715. }else{
  716. for(laSynth* s=MAIN.Audio->Synths.pFirst;s;s=s->Item.pNext){
  717. laSpinLock(&s->Lock);
  718. laRebuildPageEval(s->Page);
  719. laSpinUnlock(&s->Lock);
  720. }
  721. }
  722. }
  723. int laEvalSingleSynth(laSynth* ss){
  724. laSynth* FirstInst=0; int any=0;
  725. laSpinLock(&ss->Lock);
  726. if(ss->Playing){
  727. MAIN.Audio->AudioEvalSynth=ss;
  728. laRunPage(ss->Page,1);
  729. ss->EvalSamples += LA_SYNTH_PLEN;
  730. ss->EvalTime = (real)ss->EvalSamples/MAIN.Audio->AudioSampleRate;
  731. any=1;
  732. }
  733. FirstInst=ss->PolyphonicInstances.pFirst;
  734. laSpinUnlock(&ss->Lock);
  735. laSpinLock(&MAIN.Audio->AudioStatusLock);
  736. laSynth* pending; while(pending=lstPopPointer(&MAIN.Audio->PolyLaterTrigger)){ laSynthTriggerNew(pending); }
  737. laSpinUnlock(&MAIN.Audio->AudioStatusLock);
  738. laListHandle to_remove={0}; int count=0;
  739. for(laSynth* inst=FirstInst;inst;inst=inst->Item.pNext){
  740. any+=laEvalSingleSynth(inst); count++;
  741. if(inst->EvalTime > ss->Length || count>ss->MaxPoly){ lstAppendPointer(&to_remove,inst); }
  742. }
  743. laSynth* inst; while(inst=lstPopPointer(&to_remove)){
  744. la_SynthRemovePolyInstance(ss,inst);
  745. }
  746. return any;
  747. }
  748. int laEvalSynthGraphs(){
  749. laSynth* ss; int any=0;
  750. memset(MAIN.Audio->OutputSamples,0,sizeof(real)*LA_SYNTH_PLEN);
  751. for(laAudioChannel* ac=MAIN.Audio->Channels.pFirst;ac;ac=ac->Item.pNext){
  752. memset(ac->Samples,0,sizeof(real)*LA_SYNTH_PLEN);
  753. }
  754. for(ss=MAIN.Audio->Synths.pFirst;ss;ss=ss->Item.pNext){
  755. if(laEvalSingleSynth(ss)){ any=1; }
  756. }
  757. for(laAudioChannel* ac=MAIN.Audio->Channels.pFirst;ac;ac=ac->Item.pNext){
  758. for(int i=0;i<LA_SYNTH_PLEN;i++){
  759. MAIN.Audio->OutputSamples[i] += ac->Samples[i]*(ac->Volume/10);
  760. }
  761. }
  762. return any;
  763. }
  764. void laaudio_DataCallback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount){
  765. float* out=pOutput,*in=pInput; int Paused; int any=0;
  766. laSpinLock(&MAIN.Audio->AudioStatusLock);
  767. Paused = MAIN.Audio->Paused;
  768. laSpinUnlock(&MAIN.Audio->AudioStatusLock);
  769. if(Paused){ memset(out,0,sizeof(float)*LA_SYNTH_PLEN);
  770. memset(MAIN.Audio->InputSamples,0,sizeof(float)*LA_SYNTH_PLEN); return;
  771. }
  772. for(int i=0;i<frameCount;i++){
  773. if(MAIN.Audio->NextAudioSample>=LA_SYNTH_PLEN){
  774. MAIN.Audio->NextAudioSample=0;
  775. if(laEvalSynthGraphs()){ any=1; }
  776. }
  777. out[i]=MAIN.Audio->OutputSamples[MAIN.Audio->NextAudioSample]/10;
  778. MAIN.Audio->InputSamples[MAIN.Audio->NextAudioSample] = in[i] * 10;
  779. MAIN.Audio->NextAudioSample++;
  780. }
  781. }
  782. void la_SelectAudioDevice(laAudioDevice* ad){
  783. memAssignRef(MAIN.Audio, &MAIN.Audio->UsingDevice, ad);
  784. ma_device_stop(&MAIN.Audio->AudioDevice);
  785. ma_device_uninit(&MAIN.Audio->AudioDevice);
  786. if(!ad) return;
  787. int SampleRate=48000;
  788. ma_device_config config = ma_device_config_init(ma_device_type_duplex);
  789. config.capture.pDeviceID = ad->MiniAudioID;
  790. config.capture.format = ma_format_f32;
  791. config.capture.channels = 1;
  792. config.playback.pDeviceID= ad->MiniAudioID;
  793. config.playback.format = ma_format_f32;
  794. config.playback.channels = 1;
  795. config.sampleRate = SampleRate;
  796. config.dataCallback = laaudio_DataCallback;
  797. config.pUserData = 0;
  798. if (ma_device_init(NULL, &config, &MAIN.Audio->AudioDevice) != MA_SUCCESS){ return; }
  799. MAIN.Audio->AudioSampleRate=SampleRate;
  800. MAIN.Audio->NextAudioSample=LA_SYNTH_PLEN;
  801. MAIN.Audio->AudioFrameInterval=1.0f/SampleRate;
  802. }
  803. void la_AddAudioDevice(int id, char* Name){
  804. laAudioDevice* ad=memAcquire(sizeof(laAudioDevice));
  805. strSafeSet(&ad->Name,Name); ad->MiniAudioID=id;
  806. lstAppendItem(&MAIN.Audio->AudioDevices,ad);
  807. }
  808. void la_ClearAudioDevices(){
  809. laAudioDevice* ad; while(ad=lstPopItem(&MAIN.Audio->AudioDevices)){
  810. strSafeDestroy(&ad->Name); memFree(ad);
  811. }
  812. }
  813. void laRefreshAudioDevices(){
  814. if(!MAIN.Audio) return; la_ClearAudioDevices();
  815. ma_device_info* pPlaybackInfos;
  816. ma_uint32 playbackCount;
  817. ma_device_info* pCaptureInfos;
  818. ma_uint32 captureCount;
  819. if (ma_context_get_devices(&MAIN.Audio->MiniAudioContext, &pPlaybackInfos, &playbackCount, &pCaptureInfos, &captureCount) != MA_SUCCESS) {
  820. logPrintNew("Can't get audio devices.\n");
  821. }
  822. for (ma_uint32 iDevice = 0; iDevice < playbackCount; iDevice += 1) {
  823. la_AddAudioDevice(iDevice, pPlaybackInfos[iDevice].name);
  824. }
  825. }
  826. void laInitMiniAudio(){
  827. if (ma_context_init(NULL, 0, NULL, &MAIN.Audio->MiniAudioContext) != MA_SUCCESS) {
  828. printf("Can't init miniaudio context\n");
  829. }
  830. laRefreshAudioDevices();
  831. laSpinInit(&MAIN.Audio->AudioStatusLock);
  832. INITPACKET(MAIN.Audio->InputSamples);
  833. INITPACKET(MAIN.Audio->OutputSamples);
  834. la_SelectAudioDevice(MAIN.Audio->AudioDevices.pFirst);
  835. }
  836. void laDeinitAudio(){
  837. memFree(MAIN.Audio->InputSamples);
  838. memFree(MAIN.Audio->OutputSamples);
  839. laSpinDestroy(&MAIN.Audio->AudioStatusLock);
  840. ma_device_stop(&MAIN.Audio->AudioDevice);
  841. ma_device_uninit(&MAIN.Audio->AudioDevice);
  842. ma_context_uninit(&MAIN.Audio->MiniAudioContext);
  843. }
  844. void laStartAudio(){
  845. laSpinLock(&MAIN.Audio->AudioStatusLock);
  846. MAIN.Audio->Paused = 0;
  847. laSpinUnlock(&MAIN.Audio->AudioStatusLock);
  848. ma_device_start(&MAIN.Audio->AudioDevice);
  849. }
  850. void laStopAudio(){
  851. ma_device_start(&MAIN.Audio->AudioDevice);
  852. }
  853. void laPauseAudio(){
  854. laSpinLock(&MAIN.Audio->AudioStatusLock);
  855. MAIN.Audio->Paused = 1;
  856. laSpinUnlock(&MAIN.Audio->AudioStatusLock);
  857. }
  858. void laResetAudio(){
  859. for(laSynth* s=MAIN.Audio->Synths.pFirst;s;s=s->Item.pNext){
  860. laSpinLock(&s->Lock);
  861. s->EvalTime = 0; s->EvalSamples = 0; s->Playing = 0;
  862. laSpinUnlock(&s->Lock);
  863. }
  864. }
  865. // Operations
  866. int OPINV_RefreshAudioDevices(laOperator* a, laEvent* e){
  867. laRefreshAudioDevices(); return LA_FINISHED;
  868. }
  869. laSynth* laFindSynth(const char* Name){
  870. for(laSynth* ss=MAIN.Audio->Synths.pFirst;ss;ss=ss->Item.pNext){
  871. if(strSame(SSTR(ss->Name),Name)) return ss;
  872. }
  873. return 0;
  874. }
  875. laSynth* laNewSynth(char* Name){
  876. laSynth* ss=memAcquireHyper(sizeof(laSynth));
  877. ss->Page=memAcquire(sizeof(laRackPage));
  878. ss->Page->RackType=LA_RACK_TYPE_AUDIO;
  879. ss->Length=5; ss->MaxPoly=8;
  880. strSafeSet(&ss->Name,Name);
  881. lstAppendItem(&MAIN.Audio->Synths,ss);
  882. memAssignRef(MAIN.Audio,&MAIN.Audio->CurrentSynth,ss);
  883. laSpinInit(&ss->Lock);
  884. return ss;
  885. }
  886. void laRemoveSynth(laSynth* ss){ if(!ss) return;
  887. strSafeDestroy(&ss->Name);
  888. if(MAIN.Audio->CurrentSynth==ss){
  889. laSynth* nss=ss->Item.pNext?ss->Item.pNext:ss->Item.pPrev;
  890. memAssignRef(MAIN.Audio,&MAIN.Audio->CurrentSynth,nss);
  891. }laNotifyInstanceUsers(ss);
  892. while(ss->Page->Racks.pFirst){ laDestroyRack(ss->Page->Racks.pFirst); } memLeave(ss->Page); //ss->Page=0;
  893. lstRemoveItem(&MAIN.Audio->Synths,ss);
  894. laSpinDestroy(&ss->Lock);
  895. memLeave(ss);
  896. }
  897. int OPINV_ShedNewSynth(laOperator* a, laEvent* e){
  898. laNewSynth("New Synth");
  899. laNotifyInstanceUsers(MAIN.Audio); laRecordAndPush(0,"la.audio","New synth", 0);
  900. return LA_FINISHED;
  901. }
  902. int OPCHK_IslaSynth(laPropPack *This, laStringSplitor *ss){
  903. if (This && This->EndInstance && la_EnsureSubTarget(This->LastPs->p,0) == LA_PC_SYNTH) return 1;
  904. return 0;
  905. }
  906. int OPINV_ShedRemoveSynth(laOperator* a, laEvent* e){
  907. laSynth* ss=a->This?a->This->EndInstance:0; if(!ss)return LA_CANCELED;
  908. if(strSame(strGetArgumentString(a->ExtraInstructionsP,"confirm"),"true")){
  909. laRemoveSynth(ss);
  910. laNotifyInstanceUsers(MAIN.Audio); laRecordAndPush(0,"la.audio","Remove synthesizer", 0);
  911. return LA_FINISHED;
  912. }
  913. laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e);
  914. return LA_RUNNING;
  915. }
  916. void laui_RemoveSynth(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  917. laColumn* c=laFirstColumn(uil);
  918. laShowItemFull(uil,c,This,"remove_synth",0,"confirm=true;text=Confirm",0,0);
  919. }
  920. int OPINV_SynthPlay(laOperator* a, laEvent* e){
  921. laSynth* ss=a->This?a->This->EndInstance:0; if(!ss)return LA_CANCELED;
  922. char* str=strGetArgumentString(a->ExtraInstructionsP, "mode");
  923. int play=0; if(str && strSame(str,"PLAY")) play=1;
  924. laRebuildPageEval(ss->Page);
  925. laSpinLock(&ss->Lock);
  926. ss->Playing=play; if(!play){ ss->EvalSamples=0; ss->EvalTime=0; }
  927. laSpinUnlock(&ss->Lock);
  928. if(play){ laStartAudio(); } laNotifyInstanceUsers(ss);
  929. return LA_FINISHED;
  930. }
  931. int OPINV_SynthTriggerNew(laOperator* a, laEvent* e){
  932. laSynth* ss=a->This?a->This->EndInstance:0; if(!ss)return LA_CANCELED;
  933. laSynthTriggerNew(ss);
  934. laStartAudio();
  935. return LA_FINISHED;
  936. }
  937. void la_SynthAddPolyInstance(laSynth* s){
  938. int count=0;
  939. laSpinLock(&s->Lock); count = s->PolyCount; laSpinUnlock(&s->Lock);
  940. if(s->PolyCount>s->MaxPoly){ return; }
  941. laSpinLock(&s->Lock);
  942. laSynth* ns=memAcquire(sizeof(laSynth)); ns->Length=s->Length; ns->Playing=1;
  943. ns->Page = laDuplicateRackPage(0,s->Page);
  944. laRebuildPageEval(ns->Page);
  945. laSpinInit(&ns->Lock);
  946. //laSpinLock(&s->Lock);
  947. lstPushItem(&s->PolyphonicInstances,ns);
  948. s->PolyCount++;
  949. laSpinUnlock(&s->Lock);
  950. }
  951. void la_SynthRemovePolyInstance(laSynth* s, laSynth* inst){
  952. laSpinLock(&inst->Lock);
  953. while(inst->Page->Racks.pFirst){ laDestroyRack(inst->Page->Racks.pFirst); } memFree(inst->Page); //ss->Page=0;
  954. laSpinUnlock(&inst->Lock);
  955. laSpinDestroy(&inst->Lock);
  956. laSpinLock(&s->Lock);
  957. lstRemoveItem(&s->PolyphonicInstances,inst);
  958. s->PolyCount--;
  959. laSpinUnlock(&s->Lock);
  960. memFree(inst);
  961. }
  962. void laSynthTriggerNew(laSynth* s){
  963. if(!s || !s->Poly){ return; } la_SynthAddPolyInstance(s);
  964. }
  965. void laSynthTriggerLater(laSynth* s){
  966. if(!s || !s->Poly){ return; }
  967. laSpinLock(&MAIN.Audio->AudioStatusLock);
  968. lstAppendPointer(&MAIN.Audio->PolyLaterTrigger,s);
  969. laSpinUnlock(&MAIN.Audio->AudioStatusLock);
  970. }
  971. laAudioChannel* laNewAudioChannel(char* Name){
  972. laAudioChannel* ac=memAcquireHyper(sizeof(laAudioChannel)); lstAppendItem(&MAIN.Audio->Channels,ac);
  973. strSafeSet(&ac->Name,Name);
  974. ac->Volume = 10.0f;
  975. }
  976. void laRemoveAudioChannel(laAudioChannel* ac){
  977. lstRemoveItem(&MAIN.Audio->Channels,ac); strSafeDestroy(&ac->Name);
  978. memLeave(ac);
  979. }
  980. laAudioChannel* laGetAudioChannel(char* Name){
  981. for(laAudioChannel* ac=MAIN.Audio->Channels.pFirst;ac;ac=ac->Item.pNext){
  982. if(strSame(SSTR(ac->Name),Name)) return ac;
  983. }
  984. return 0;
  985. }
  986. int OPCHK_IslaAudioChannel(laPropPack *This, laStringSplitor *ss){
  987. if (This && This->EndInstance && la_EnsureSubTarget(This->LastPs->p,0) == LA_PC_CHANNEL) return 1;
  988. return 0;
  989. }
  990. int OPINV_ShedNewAudioChannel(laOperator* a, laEvent* e){
  991. laNewAudioChannel("Output");
  992. laNotifyInstanceUsers(MAIN.Audio); laRecordAndPush(0,"la.audio","New channel", 0);
  993. return LA_FINISHED;
  994. }
  995. int OPINV_ShedRemoveAudioChannel(laOperator* a, laEvent* e){
  996. laAudioChannel*ac=a->This?a->This->EndInstance:0; if(!ac)return LA_CANCELED;
  997. if(strSame(strGetArgumentString(a->ExtraInstructionsP,"confirm"),"true")){
  998. laRemoveAudioChannel(ac); laNotifyInstanceUsers(MAIN.Audio); laRecordAndPush(0,"la.audio","Remove audio channel", 0);
  999. return LA_FINISHED;
  1000. }
  1001. laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e);
  1002. return LA_RUNNING;
  1003. }
  1004. void laui_RemoveAudioChannel(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  1005. laColumn* c=laFirstColumn(uil);
  1006. laShowItemFull(uil,c,This,"remove",0,"confirm=true;text=Confirm",0,0);
  1007. }
  1008. void lauidetached_Synthersizers(laPanel* p){
  1009. la_MakeDetachedProp(p, "la.detached_view_switch", "detached");
  1010. la_MakeDetachedProp(p, "la.audio.current_synth", "page");
  1011. }
  1012. void laui_Synthersizers(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  1013. laColumn* c=laFirstColumn(uil),*cl, *cr,*crl,*crr;
  1014. laSplitColumn(uil,c,0.4); cl=laLeftColumn(c,1); cr=laRightColumn(c,0);
  1015. laSplitColumn(uil,cr,0.6); crl=laLeftColumn(cr,0); crr=laRightColumn(cr,2);
  1016. laShowItemFull(uil,cl,Extra,"detached",0,0,0,0)->Flags|=LA_UI_FLAGS_HIGHLIGHT|LA_UI_FLAGS_ICON;
  1017. #define ADD_PAGE \
  1018. laUiItem* b=laBeginRow(uil,crl,0,0); \
  1019. laShowItemFull(uil,crl,&rb->PP,"",LA_WIDGET_COLLECTION_SELECTOR,0,laui_IdentifierOnly,0) \
  1020. ->Flags|=LA_UI_COLLECTION_SIMPLE_SELECTOR; \
  1021. laUiItem* b3=laOnConditionThat(uil,cr,laPropExpression(&rb->PP,""));{ \
  1022. laShowItem(uil,crl,&rb->PP,"name"); \
  1023. laShowItem(uil,crl,0,"LA_new_synth")->Flags|=LA_UI_FLAGS_ICON; \
  1024. laShowSeparator(uil,crl); \
  1025. laShowItem(uil,crl,&rb->PP,"remove_synth")->Flags|=LA_UI_FLAGS_ICON; \
  1026. laShowSeparator(uil,crl)->Expand=1; \
  1027. laUiItem* b4=laOnConditionThat(uil,cr,laPropExpression(&rb->PP,"poly"));{ \
  1028. laShowItem(uil,crl,&rb->PP,"max_poly"); \
  1029. laShowItem(uil,crl,&rb->PP,"length"); \
  1030. laShowItemFull(uil,crl,&rb->PP,"trigger",0,"icon=▶;",0,0)->Flags|=LA_UI_FLAGS_ICON; \
  1031. }laEndCondition(uil,b4); \
  1032. laUiItem* poly=laShowItem(uil,crl,&rb->PP,"poly");poly->Flags|=LA_UI_FLAGS_EXPAND; \
  1033. }laElse(uil,b3);{ \
  1034. laShowItem(uil,crl,0,"LA_new_synth"); \
  1035. }laEndCondition(uil,b3); \
  1036. laEndRow(uil,b); \
  1037. b3=laOnConditionThat(uil,cr,laPropExpression(&rb->PP,""));{ \
  1038. laUiItem* b4=laOnConditionThat(uil,crr,laPropExpression(&rb->PP,"playing"));{ \
  1039. laShowItemFull(uil,crr,&rb->PP,"play",0,"text=❚❚;",0,0) \
  1040. ->Flags|=LA_UI_FLAGS_EXIT_WHEN_TRIGGERED|LA_TEXT_ALIGN_CENTER; \
  1041. }laElse(uil,b4);{ \
  1042. laShowItemFull(uil,crr,&rb->PP,"play",0,"icon=▶;mode=PLAY;",0,0) \
  1043. ->Flags|=LA_UI_FLAGS_ICON|LA_UI_FLAGS_EXIT_WHEN_TRIGGERED; \
  1044. }laEndCondition(uil,b4); \
  1045. laShowItemFull(uil,c,&rb->PP,"page",LA_WIDGET_COLLECTION_SINGLE,0,laui_RackPage,0)->Flags|=LA_UI_FLAGS_NO_DECAL; \
  1046. }laEndCondition(uil,b3); \
  1047. laUiItem* b1=laOnConditionThat(uil,cr,laPropExpression(Extra,"detached"));{
  1048. laUiItem* rb=laShowInvisibleItem(uil,cr,Extra,"page");
  1049. ADD_PAGE
  1050. }laElse(uil,b1);{
  1051. laUiItem* rb=laShowInvisibleItem(uil,cr,0,"la.audio.current_synth");
  1052. ADD_PAGE
  1053. }laEndCondition(uil,b1);
  1054. #undef ADD_PAGE
  1055. }
  1056. void laui_AudioChannel(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  1057. laColumn* c=laFirstColumn(uil); laSplitColumn(uil,c,0.3); laColumn* c1=laLeftColumn(c,1),*c2=laRightColumn(c,0); laUiItem* b;
  1058. laShowHeightAdjuster(uil,c1,This,"__move",0);
  1059. b=laBeginRow(uil,c2,0,0);
  1060. laShowItem(uil,c2,This,"name")->Expand=1;
  1061. laShowItem(uil,c2,This,"remove")->Flags|=LA_UI_FLAGS_ICON;
  1062. laEndRow(uil,b);
  1063. laShowItem(uil,c2,This,"volume");
  1064. laShowSeparator(uil,c);
  1065. }
  1066. void laui_AudioChannels(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  1067. laColumn* c=laFirstColumn(uil); laUiItem* b;
  1068. b=laBeginRow(uil,c,0,0);
  1069. laShowItem(uil,c,0,"LA_new_channel");
  1070. laEndRow(uil,b);
  1071. // if horizontal look:
  1072. // laUiItem* g=laMakeGroup(uil,c,"Racks",0);{ g->Flags|=/*LA_UI_FLAGS_NO_GAP|LA_UI_FLAGS_NO_DECAL|*/LA_UI_FLAGS_NODE_CONTAINER;
  1073. // laUiList* gu=g->Page; laColumn* gc=laFirstColumn(gu); gu->HeightCoeff=-1; g->State=LA_UI_ACTIVE;
  1074. // laShowItemFull(gu,gc,0,"la.audio.channels",0,0,laui_AudioChannel,0)->Expand=5;
  1075. // }
  1076. laShowItemFull(uil,c,0,"la.audio.channels",0,0,laui_AudioChannel,0);
  1077. }
  1078. void* laget_FirstAudioDevice(void* unused1,void* unused2){
  1079. return MAIN.Audio->AudioDevices.pFirst;
  1080. }
  1081. void* laget_FirstSynth(void* unused1,void* unused2){
  1082. return MAIN.Audio->Synths.pFirst;
  1083. }
  1084. void* laget_FirstAudioChannel(void* unused1, void* unused2){
  1085. return MAIN.Audio->Channels.pFirst;
  1086. }
  1087. void lapost_Synth(laSynth* ss){
  1088. laSpinInit(&ss->Lock);
  1089. laRebuildPageEval(ss->Page);
  1090. }
  1091. int laget_SynthPlaying(laSynth* ss){
  1092. int play=0;
  1093. laSpinLock(&ss->Lock); play=ss->Playing; laSpinUnlock(&ss->Lock);
  1094. return play;
  1095. }
  1096. void laset_CurrentSynth(laAudio* a,laSynth* s){
  1097. memAssignRef(a,&a->CurrentSynth,s);
  1098. }
  1099. void laset_CurrentAudioDevice(laAudio* a,laAudioDevice* ad){
  1100. la_SelectAudioDevice(ad);
  1101. }
  1102. void laset_OutputSendChannel(laSynthNodeOutput* n,laAudioChannel* ac){
  1103. if(ac){ strSafeSet(&n->SendName, SSTR(ac->Name)); } else { strSafeSet(&n->SendName,""); }
  1104. laAudioChannel* outchannel = laGetAudioChannel(SSTR(n->SendName));
  1105. memAssignRef(n,&n->Send,outchannel);
  1106. }
  1107. void laset_OutputSendName(laSynthNodeOutput* n,char* name){
  1108. strSafeSet(&n->SendName,name);
  1109. laAudioChannel* outchannel = laGetAudioChannel(name);
  1110. memAssignRef(n,&n->Send,outchannel);
  1111. }
  1112. void laset_QuantizeEnabledKeys(laSynthNodeQuantize* n, int index, int val_UNUSED){
  1113. if(n->EnabledBits[index] & 0x1){ n->EnabledBits[index]=0; }
  1114. else{ n->EnabledBits[index]=1; }
  1115. }
  1116. void laset_AudioChannelMove(laAudioChannel* ac, int move){
  1117. if(move<0 && ac->Item.pPrev){ lstMoveUp(&MAIN.Audio->Channels, ac); laNotifyUsers("la.audio"); }
  1118. elif(move>0 && ac->Item.pNext){ lstMoveDown(&MAIN.Audio->Channels, ac); laNotifyUsers("la.audio"); }
  1119. }
  1120. void laset_SynthTriggerTrigger(laSynthNodeTrigger* n, int trig){
  1121. if(trig){ laSynthTriggerNew(n->Target); } n->iTrigger=trig;
  1122. }
  1123. void la_AudioPreFrame(){
  1124. if(MAIN.GraphNeedsRebuild){ laRebuildSynthGraphs(1); }
  1125. }
  1126. void laInitAudio(){
  1127. laPropContainer* pc; laProp* p; laSubProp* sp;
  1128. laInitMiniAudio();
  1129. laRegisterUiTemplate("LAUI_synthesizers","Synthesizers",laui_Synthersizers,lauidetached_Synthersizers,0,"Audio",0,0,0);
  1130. laRegisterUiTemplate("LAUI_audio_channels","Mixer",laui_AudioChannels,0,0,0,0,0,0);
  1131. laCreateOperatorType("LA_new_synth", "New Synthesizer", "Add a synthesizer",0,0,0,OPINV_ShedNewSynth,0,'+',0);
  1132. laCreateOperatorType("LA_remove_synth", "Remove Synthesizer", "Remove a synthesizer",OPCHK_IslaSynth,0,0,OPINV_ShedRemoveSynth,OPMOD_FinishOnData,L'🗴',0)
  1133. ->UiDefine=laui_RemoveSynth;
  1134. laCreateOperatorType("LA_synth_play", "Play Synthesizer", "Play a synthesizer",OPCHK_IslaSynth,0,0,OPINV_SynthPlay,0,0,0);
  1135. laCreateOperatorType("LA_synth_trigger_new", "Trigger New", "Trigger a new polyphonic note",OPCHK_IslaSynth,0,0,OPINV_SynthTriggerNew,0,0,0);
  1136. laCreateOperatorType("LA_refresh_audio_devices", "Refresh Audio Devices", "Enumerate audio devices for selection",0,0,0,OPINV_RefreshAudioDevices,0,U'🗘',0);
  1137. laCreateOperatorType("LA_new_channel", "New Channel", "Add an audio channel",0,0,0,OPINV_ShedNewAudioChannel,0,'+',0);
  1138. laCreateOperatorType("LA_remove_channel", "Remove Channel", "Remove an audio channel",OPCHK_IslaAudioChannel,0,0,OPINV_ShedRemoveAudioChannel,OPMOD_FinishOnData,L'🗴',0)
  1139. ->UiDefine=laui_RemoveAudioChannel;
  1140. pc = laAddPropertyContainer("la_audio_device","LaGUI Audio Device","LaGUI enumerated audio device",0,0,sizeof(laAudioDevice),0,0,1);{
  1141. laAddStringProperty(pc,"name","Name","Name of the audio device",0,0,0,0,1,offsetof(laAudioDevice,Name),0,0,0,0,LA_AS_IDENTIFIER);
  1142. }
  1143. pc = laAddPropertyContainer("la_synth", "LaGUI Synthesizer", "LaGUI synthesizer", 0,0,sizeof(laSynth),lapost_Synth,0,2);{
  1144. LA_PC_SYNTH=pc;
  1145. laAddStringProperty(pc,"name","Name","Name of the synthesizer",0,0,0,0,1,offsetof(laSynth,Name),0,0,0,0,LA_AS_IDENTIFIER);
  1146. laAddSubGroup(pc, "page", "Page", "Rack page","la_rack_page",0,0,0,offsetof(laSynth,Page),0,0,0,0,0,0,0,LA_UDF_SINGLE|LA_HIDE_IN_SAVE);
  1147. p=laAddEnumProperty(pc,"playing","Playing","Synth is playing",0,0,0,0,0,-1,laget_SynthPlaying,0,0,0,0,0,0,0,0,LA_READ_ONLY);
  1148. laAddEnumItemAs(p,"IDLE","Idle","Synth is not playing",0,0);
  1149. laAddEnumItemAs(p,"PLAYING","Playing","Synth is playing",1,0);
  1150. laAddFloatProperty(pc,"length","Length","Length of this sound",0,0,"s",10,0,0,5,0,offsetof(laSynth,Length),0,0,0,0,0,0,0,0,0,0,0);
  1151. p=laAddEnumProperty(pc,"poly","Polyphonic","Whether this synth supports polyphonic triggering",0,0,0,0,0,offsetof(laSynth,Poly),0,0,0,0,0,0,0,0,0,0);
  1152. p->ElementBytes=2;
  1153. laAddEnumItemAs(p,"MONO","Mono","Synth is monophonic",0,0);
  1154. laAddEnumItemAs(p,"POLY","Poly","Synth is polyphonic",1,0);
  1155. laAddIntProperty(pc,"max_poly","Max Poly","Max polyphonic sound count",0,0,0,16,0,0,0,0,offsetof(laSynth,MaxPoly),0,0,0,0,0,0,0,0,0,0,0)->ElementBytes=2;
  1156. laAddOperatorProperty(pc,"remove_synth","Remove Synth", "Remove synth", "LA_remove_synth", L'🗴', 0);
  1157. laAddOperatorProperty(pc,"play","Play Synth", "Play synth", "LA_synth_play", 0, 0);
  1158. laAddOperatorProperty(pc,"trigger","Trigger", "Trigger a new polyphonic node", "LA_synth_trigger_new", 0, 0);
  1159. }
  1160. pc = laAddPropertyContainer("la_audio_channel", "LaGUI Audio Channel", "LaGUI Audio Channel", 0,0,sizeof(laAudioChannel),0,0,2);{
  1161. LA_PC_CHANNEL=pc;
  1162. laAddStringProperty(pc,"name","Name","Name of the channel",0,0,0,0,1,offsetof(laAudioChannel,Name),0,0,0,0,LA_AS_IDENTIFIER);
  1163. laAddFloatProperty(pc,"volume","Volume","Volume of this channel",0,0,0,15,0,0.05,0,0,offsetof(laAudioChannel,Volume),0,0,0,0,0,0,0,0,0,0,0);
  1164. laAddOperatorProperty(pc,"remove","Remove Channel", "Remove this channel", "LA_remove_channel", L'🗴', 0);
  1165. laAddIntProperty(pc,"__move","Move Slider","Move Slider",LA_WIDGET_HEIGHT_ADJUSTER,0,0,0,0,0,0,0,0,0,laset_AudioChannelMove,0,0,0,0,0,0,0,0,LA_UDF_IGNORE);
  1166. }
  1167. pc=laAddPropertyContainer("la_node_synth_input", "Synth Input Node", "Synthesizer time and trigger input",0,laui_InputNode,sizeof(laSynthNodeInput),lapost_Node,0,1);
  1168. LA_PC_IDN_INPUT=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
  1169. laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  1170. laAddSubGroup(pc,"out_time", "Out Time","Time output","la_out_socket",0,0,0,offsetof(laSynthNodeInput,OutTime),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1171. laAddSubGroup(pc,"out_trigger", "Out Trigger","Trigger output","la_out_socket",0,0,0,offsetof(laSynthNodeInput,OutTrigger),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1172. laAddSubGroup(pc,"out_l", "Out Left","Capture left","la_out_socket",0,0,0,offsetof(laSynthNodeInput,OutL),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1173. laAddSubGroup(pc,"out_r", "Out Right","Capture right","la_out_socket",0,0,0,offsetof(laSynthNodeInput,OutR),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1174. pc=laAddPropertyContainer("la_node_synth_pulse", "Synth Pulse Node", "Pulse generate node for synth",0,laui_PulseNode,sizeof(laSynthNodePulse),lapost_Node,0,1);
  1175. LA_PC_IDN_PULSE=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
  1176. laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  1177. laAddSubGroup(pc,"out", "Out","Pulse output","la_out_socket",0,0,0,offsetof(laSynthNodePulse,Out),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1178. laAddSubGroup(pc,"in_trigger", "In Trigger","Trigger input","la_in_socket",0,0,0,offsetof(laSynthNodePulse,InTrigger),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1179. laAddSubGroup(pc,"in_width", "In Width","Width input","la_in_socket",0,0,0,offsetof(laSynthNodePulse,InWidth),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1180. laAddSubGroup(pc,"in_gate", "In Gate","Gate input","la_in_socket",0,0,0,offsetof(laSynthNodePulse,InGate),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1181. laAddFloatProperty(pc,"influence","Influence","Influence of the width input",LA_WIDGET_KNOB,0,0,1,-1,0.01,0,0,offsetof(laSynthNodePulse,rInfluence),0,0,0,0,0,0,0,0,0,0,0);
  1182. laAddFloatProperty(pc,"width","Width","Width of the pulse",0,0,0,10,0,0.1,0,0,offsetof(laSynthNodePulse,rWidth),0,0,0,0,0,0,0,0,0,0,0);
  1183. laAddFloatProperty(pc,"gate","Gate","Gate of the trigger",0,0,0,10,-10,0.1,0,0,offsetof(laSynthNodePulse,rGate),0,0,0,0,0,0,0,0,0,0,0);
  1184. p=laAddEnumProperty(pc,"trigger","Trigger","Trigger the envelope",LA_WIDGET_ENUM_CYCLE,0,0,0,0,offsetof(laSynthNodePulse,iTrigger),0,0,0,0,0,0,0,0,0,0);
  1185. laAddEnumItemAs(p,"IDLE","Idle","Trigger idle",0,0);
  1186. laAddEnumItemAs(p,"TRIG","Trigger","Trigger active",1,0);
  1187. pc=laAddPropertyContainer("la_node_synth_fm", "FM OSC Node", "Osilliator node with frequency modulation",0,laui_FMNode,sizeof(laSynthNodeFM),lapost_Node,0,1);
  1188. LA_PC_IDN_FM=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
  1189. laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  1190. laAddSubGroup(pc,"in_frequency", "In Frequency","Input frequency","la_in_socket",0,0,0,offsetof(laSynthNodeFM,InFrequency),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1191. laAddSubGroup(pc,"out", "Out","Output","la_out_socket",0,0,0,offsetof(laSynthNodeFM,Out),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1192. laAddFloatProperty(pc,"frequency","Frequency","Frequency of the oscilliator",0,0,0,10,0,0.1,0,0,offsetof(laSynthNodeFM,Frequency),0,0,0,0,0,0,0,0,0,0,0);
  1193. laAddFloatProperty(pc,"influence","Influence","Influence of the molulation input",LA_WIDGET_KNOB,0,0,1,-1,0.01,0,0,offsetof(laSynthNodeFM,rInfluence),0,0,0,0,0,0,0,0,0,0,0);
  1194. p=laAddEnumProperty(pc,"slow","Slow","Low frequency oscilliator",LA_WIDGET_ENUM_HIGHLIGHT,0,0,0,0,offsetof(laSynthNodeFM,Slow),0,0,0,0,0,0,0,0,0,0);
  1195. laAddEnumItemAs(p,"NONE","None","Regular frequency range",0,0);
  1196. laAddEnumItemAs(p,"SLOW","Slow","Slow frequency range",1,0);
  1197. pc=laAddPropertyContainer("la_node_synth_vca", "VCA Node", "Voltage controlled amplifier",0,laui_VCANode,sizeof(laSynthNodeVCA),lapost_Node,0,1);
  1198. LA_PC_IDN_VCA=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
  1199. laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  1200. laAddSubGroup(pc,"in_amp", "In Amptitude","Input amptitude","la_in_socket",0,0,0,offsetof(laSynthNodeVCA,InAmp),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1201. laAddSubGroup(pc,"in", "Input","Input","la_in_socket",0,0,0,offsetof(laSynthNodeVCA,In),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1202. laAddSubGroup(pc,"out", "Out","Output","la_out_socket",0,0,0,offsetof(laSynthNodeVCA,Out),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1203. laAddFloatProperty(pc,"amp","Amptitude","Amptitude of the output signal",0,0,0,10,0,0.1,0,0,offsetof(laSynthNodeVCA,Amp),0,0,0,0,0,0,0,0,0,0,0);
  1204. laAddFloatProperty(pc,"influence","Influence","Influence of the modulating signal",LA_WIDGET_KNOB,0,0,1,-1,0.01,0,0,offsetof(laSynthNodeVCA,rInfluence),0,0,0,0,0,0,0,0,0,0,0);
  1205. pc=laAddPropertyContainer("la_node_synth_noise", "Noise Node", "Noise node",0,laui_NoiseNode,sizeof(laSynthNodeNoise),lapost_Node,0,1);
  1206. LA_PC_IDN_NOISE=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
  1207. laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  1208. laAddSubGroup(pc,"out_white", "White","White noise output","la_out_socket",0,0,0,offsetof(laSynthNodeNoise,OutWhite),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1209. laAddSubGroup(pc,"out_pink", "Pink","Pinknoise output","la_out_socket",0,0,0,offsetof(laSynthNodeNoise,OutPink),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1210. pc=laAddPropertyContainer("la_node_synth_scope", "Scope Node", "Scope for synthesizer",0,laui_ScopeNode,sizeof(laSynthNodeScope),lapost_Node,0,1);
  1211. LA_PC_IDN_SCOPE=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
  1212. laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  1213. laAddSubGroup(pc,"scope", "Scope","Synth scope","la_node_synth_scope",0,LA_WIDGET_SCOPE,0,-1,0,laget_InstanceSelf,0,0,0,0,0,LA_UDF_REFER|LA_UDF_IGNORE);
  1214. laAddSubGroup(pc,"in1", "In 1","Input Channel 1","la_in_socket",0,0,0,offsetof(laSynthNodeScope,In1),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1215. laAddSubGroup(pc,"in2", "In 2","Input Channel 2","la_in_socket",0,0,0,offsetof(laSynthNodeScope,In2),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1216. p=laAddEnumProperty(pc,"time","Time","Time factor of the scope",0,0,0,5,0,offsetof(laSynthNodeScope,Time),0,0,0,0,0,0,0,0,0,0);
  1217. #define _STR(a) #a
  1218. #define ADD_TIME_FACTOR(a) laAddEnumItemAs(p,"FAC"_STR(a),_STR(a),"^"_STR(a)" time",a,0);
  1219. ADD_TIME_FACTOR(1) ADD_TIME_FACTOR(2) ADD_TIME_FACTOR(3) ADD_TIME_FACTOR(4) ADD_TIME_FACTOR(5)
  1220. ADD_TIME_FACTOR(6) ADD_TIME_FACTOR(7) ADD_TIME_FACTOR(8) ADD_TIME_FACTOR(9) ADD_TIME_FACTOR(10)
  1221. #undef ADD_TIME_FACTOR
  1222. p=laAddEnumProperty(pc,"gain1","Gain 1","Channel 1 Gain",0,0,0,5,0,offsetof(laSynthNodeScope,Gain1),0,0,0,0,0,0,0,0,0,0);
  1223. #define ADD_GAIN(a) laAddEnumItemAs(p,"GAIN"_STR(a),_STR(a),"^"_STR(a)" gain",a,0);
  1224. #define ADD_GAIN_S(a,s) laAddEnumItemAs(p,"GAIN"_STR(a),s,"^"s" gain",a,0);
  1225. ADD_GAIN_S(-1,"¼") ADD_GAIN_S(0,"½") ADD_GAIN(1) ADD_GAIN(2) ADD_GAIN(3) ADD_GAIN(4) ADD_GAIN(5) ADD_GAIN(6)
  1226. p=laAddEnumProperty(pc,"gain2","Gain 2","Channel 2 Gain",0,0,0,5,0,offsetof(laSynthNodeScope,Gain2),0,0,0,0,0,0,0,0,0,0);
  1227. ADD_GAIN_S(-1,"¼") ADD_GAIN_S(0,"½") ADD_GAIN(1) ADD_GAIN(2) ADD_GAIN(3) ADD_GAIN(4) ADD_GAIN(5) ADD_GAIN(6)
  1228. #undef ADD_GAIN
  1229. #undef ADD_GAIN_S
  1230. laAddFloatProperty(pc,"offset1","Offset 1","Channel 1 offset",0,0,0,10,-10,0.1,0,0,offsetof(laSynthNodeScope,Offset1),0,0,0,0,0,0,0,0,0,0,0);
  1231. laAddFloatProperty(pc,"offset2","Offset 2","Channel 2 offset",0,0,0,10,-10,0.1,0,0,offsetof(laSynthNodeScope,Offset2),0,0,0,0,0,0,0,0,0,0,0);
  1232. laAddFloatProperty(pc,"bright1","Brightness 1","Channel 1 brightness",0,0,0,1,0,0.1,0,0,offsetof(laSynthNodeScope,Brightness1),0,0,0,0,0,0,0,0,0,0,0);
  1233. laAddFloatProperty(pc,"bright2","Brightness 2","Channel 2 brightness",0,0,0,1,0,0.1,0,0,offsetof(laSynthNodeScope,Brightness2),0,0,0,0,0,0,0,0,0,0,0);
  1234. pc=laAddPropertyContainer("la_node_synth_output", "Output Node", "Sound output to system",0,laui_OutputNode,sizeof(laSynthNodeOutput),lapost_Node,0,1);
  1235. LA_PC_IDN_OUTPUT=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
  1236. laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  1237. laAddSubGroup(pc,"in", "Input","Input sound","la_in_socket",0,0,0,offsetof(laSynthNodeOutput,In),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1238. laAddStringProperty(pc,"send_channel_name","Channel Name","Channel name of this send target",0,0,0,0,1,offsetof(laSynthNodeOutput,SendName),0,0,laset_OutputSendName,0,0);
  1239. laAddSubGroup(pc,"send_channel_selector","Send","Send to channel","la_audio_channel",0,0,0,-1,laget_FirstAudioChannel,0,laget_ListNext,laset_OutputSendChannel,0,0,0,LA_UDF_REFER|LA_UDF_IGNORE);
  1240. pc=laAddPropertyContainer("la_node_synth_envelope", "Envelope Node", "Sound envelope",0,laui_EnvelopeNode,sizeof(laSynthNodeEnvelope),lapost_Node,0,1);
  1241. LA_PC_IDN_ENVELOPE=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
  1242. laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  1243. laAddSubGroup(pc,"in_gate", "Gate","Gate input","la_in_socket",0,0,0,offsetof(laSynthNodeEnvelope,Gate),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1244. laAddSubGroup(pc,"in_attack", "Attack","Attack input","la_in_socket",0,0,0,offsetof(laSynthNodeEnvelope,Attack),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1245. laAddSubGroup(pc,"in_delay", "Delay","Delay input","la_in_socket",0,0,0,offsetof(laSynthNodeEnvelope,Delay),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1246. laAddSubGroup(pc,"in_sustain", "Sustain","Sustain input","la_in_socket",0,0,0,offsetof(laSynthNodeEnvelope,Sustain),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1247. laAddSubGroup(pc,"in_release", "Release","Release input","la_in_socket",0,0,0,offsetof(laSynthNodeEnvelope,Release),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1248. laAddSubGroup(pc,"in_trigger", "Trigger","Trigger input","la_in_socket",0,0,0,offsetof(laSynthNodeEnvelope,Trigger),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1249. laAddSubGroup(pc,"in_restart", "Restart","Restart input","la_in_socket",0,0,0,offsetof(laSynthNodeEnvelope,Restart),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1250. laAddSubGroup(pc,"out", "Output","Envelope output","la_out_socket",0,0,0,offsetof(laSynthNodeEnvelope,Out),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1251. laAddFloatProperty(pc,"gate","Gate","Gate for the trigger",0,0,0,10,-10,0.01,0,0,offsetof(laSynthNodeEnvelope,rGate),0,0,0,0,0,0,0,0,0,0,0);
  1252. laAddFloatProperty(pc,"attack","Attack","Attack value",0,0,0,10,0,0.01,0,0,offsetof(laSynthNodeEnvelope,rAttack),0,0,0,0,0,0,0,0,0,0,0);
  1253. laAddFloatProperty(pc,"delay","Delay","Delay value",0,0,0,10,0,0.01,0,0,offsetof(laSynthNodeEnvelope,rDelay),0,0,0,0,0,0,0,0,0,0,0);
  1254. laAddFloatProperty(pc,"sustain","Sustain","Sustain value",0,0,0,10,0,0.01,0,0,offsetof(laSynthNodeEnvelope,rSustain),0,0,0,0,0,0,0,0,0,0,0);
  1255. laAddFloatProperty(pc,"release","Release","Release value",0,0,0,10,0,0.01,0,0,offsetof(laSynthNodeEnvelope,rRelease),0,0,0,0,0,0,0,0,0,0,0);
  1256. laAddFloatProperty(pc,"iattack","Attack Influence","Attack modulation influence value",LA_WIDGET_KNOB,0,0,1,-1,0.01,0,0,offsetof(laSynthNodeEnvelope,iAttack),0,0,0,0,0,0,0,0,0,0,0);
  1257. laAddFloatProperty(pc,"idelay","Delay Influence","Delay modulation influence value",LA_WIDGET_KNOB,0,0,1,-1,0.01,0,0,offsetof(laSynthNodeEnvelope,iDelay),0,0,0,0,0,0,0,0,0,0,0);
  1258. laAddFloatProperty(pc,"isustain","Sustain Influence","Sustain modulation influence value",LA_WIDGET_KNOB,0,0,1,-1,0.01,0,0,offsetof(laSynthNodeEnvelope,iSustain),0,0,0,0,0,0,0,0,0,0,0);
  1259. laAddFloatProperty(pc,"irelease","Release Influence","Release modulation influence value",LA_WIDGET_KNOB,0,0,1,-1,0.01,0,0,offsetof(laSynthNodeEnvelope,iRelease),0,0,0,0,0,0,0,0,0,0,0);
  1260. p=laAddEnumProperty(pc,"trigger","Trigger","Trigger the envelope",LA_WIDGET_ENUM_CYCLE,0,0,0,0,offsetof(laSynthNodeEnvelope,bTrigger),0,0,0,0,0,0,0,0,0,0);
  1261. laAddEnumItemAs(p,"IDLE","Idle","Trigger idle",0,0);
  1262. laAddEnumItemAs(p,"TRIG","Trigger","Trigger active",1,0);
  1263. pc=laAddPropertyContainer("la_node_synth_driver", "Synth Driver Node", "Control playing status of the synth",0,laui_SynthDriverNode,sizeof(laSynthNodeDriver),lapost_Node,0,1);
  1264. LA_PC_IDN_SYNTH_DRIVER=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
  1265. laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  1266. laAddSubGroup(pc,"prev", "Previous","Previous node","la_in_socket",0,0,0,offsetof(laSynthNodeDriver,Prev),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1267. laAddSubGroup(pc,"next", "Next","Next node","la_out_socket",0,0,0,offsetof(laSynthNodeDriver,Next),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1268. laAddSubGroup(pc,"in_transport", "In Transport","Transport input","la_in_socket",0,0,0,offsetof(laSynthNodeDriver,InTransport),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1269. laAddSubGroup(pc,"in_reset", "In Reset","Reset input (trigger on rising edge)","la_in_socket",0,0,0,offsetof(laSynthNodeDriver,InReset),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1270. p=laAddEnumProperty(pc,"transport","Transport","Play or stop the synth playing",LA_WIDGET_ENUM_HIGHLIGHT,0,0,0,0,offsetof(laSynthNodeDriver,iTransport),0,0,0,0,0,0,0,0,0,0);
  1271. laAddEnumItemAs(p,"STOPPED","Stopped","Synth is stopped",0,0);
  1272. laAddEnumItemAs(p,"PLAYING","Playing","Synth is playing",1,0);
  1273. p=laAddEnumProperty(pc,"reset","Reset","Reset Synth",LA_WIDGET_ENUM_HIGHLIGHT,0,0,0,0,offsetof(laSynthNodeDriver,iReset),0,0,0,0,0,0,0,0,0,0);
  1274. laAddEnumItemAs(p,"IDLE","Idle","Trigger idle",0,0);
  1275. laAddEnumItemAs(p,"TRIG","Trigger","Trigger active",1,0);
  1276. laAddSubGroup(pc,"target","Target","Target synth to control","la_synth",0,0,0,offsetof(laSynthNodeDriver,Target),laget_FirstSynth,0,laget_ListNext,0,0,0,0,LA_UDF_REFER);
  1277. pc=laAddPropertyContainer("la_node_synth_trigger", "Synth Trigger Node", "Trigger a new polyphonic note",0,laui_SynthTriggerNode,sizeof(laSynthNodeTrigger),lapost_Node,0,1);
  1278. LA_PC_IDN_SYNTH_TRIGGER=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
  1279. laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  1280. laAddSubGroup(pc,"prev", "Previous","Previous node","la_in_socket",0,0,0,offsetof(laSynthNodeTrigger,Prev),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1281. laAddSubGroup(pc,"next", "Next","Next node","la_out_socket",0,0,0,offsetof(laSynthNodeTrigger,Next),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1282. laAddSubGroup(pc,"in_trigger", "In Trigger","Trigger input","la_in_socket",0,0,0,offsetof(laSynthNodeTrigger,InTrigger),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1283. p=laAddEnumProperty(pc,"trigger","Trigger","Trigger new note",LA_WIDGET_ENUM_HIGHLIGHT,0,0,0,0,offsetof(laSynthNodeTrigger,iTrigger),0,laset_SynthTriggerTrigger,0,0,0,0,0,0,0,0);
  1284. laAddEnumItemAs(p,"IDLE","Idle","Trigger idle",0,0);
  1285. laAddEnumItemAs(p,"TRIG","Trigger","Trigger active",1,0);
  1286. p=laAddEnumProperty(pc,"trigger_edge","Edge","Trigger on rising edge",LA_WIDGET_ENUM_HIGHLIGHT,0,0,0,0,offsetof(laSynthNodeTrigger,TriggerEdge),0,0,0,0,0,0,0,0,0,0);
  1287. laAddEnumItemAs(p,"LEVEL","Level","Trigger on high level",0,'-');
  1288. laAddEnumItemAs(p,"RISING_EDGE","Rising Edge","Trigger on rising edge",1,U'⮥');
  1289. laAddSubGroup(pc,"target","Target","Target synth to trigger","la_synth",0,0,0,offsetof(laSynthNodeTrigger,Target),laget_FirstSynth,0,laget_ListNext,0,0,0,0,LA_UDF_REFER);
  1290. pc=laAddPropertyContainer("la_node_synth_quantize", "Quantize Node", "Quantize control voltages",0,laui_QuantizeNode,sizeof(laSynthNodeQuantize),lapost_Node,0,1);
  1291. LA_PC_IDN_QUANTIZE=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
  1292. laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  1293. laAddSubGroup(pc,"in", "Input","CV input","la_in_socket",0,0,0,offsetof(laSynthNodeQuantize,In),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1294. laAddSubGroup(pc,"out", "Output","CV output","la_out_socket",0,0,0,offsetof(laSynthNodeQuantize,Out),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1295. p=laAddEnumProperty(pc,"enabled_keys","Keys","Enabled keys",LA_WIDGET_ENUM_CYCLE,0,0,0,0,offsetof(laSynthNodeQuantize,EnabledBits),0,0,12,0,laset_QuantizeEnabledKeys,0,0,0,0,0);
  1296. laAddEnumItemAs(p,"DISABLED","Disabled","Key is disabled",0,' ');
  1297. laAddEnumItemAs(p,"ENABLED","Enabled","Key is enabled",1,' ');
  1298. laAddEnumItemAs(p,"OUTPUT","Outputting","Key is Outputting",3,U'🌑');
  1299. LA_IDN_REGISTER("Inputs",U'🔌',LA_IDN_INPUT, LA_PC_IDN_INPUT, IDN_Input, laSynthNodeInput);
  1300. LA_IDN_REGISTER("Pulse",U'⎍',LA_IDN_PULSE, LA_PC_IDN_PULSE, IDN_Pulse, laSynthNodePulse);
  1301. LA_IDN_REGISTER("VCO",'f',LA_IDN_FM, LA_PC_IDN_FM, IDN_FM, laSynthNodeFM);
  1302. LA_IDN_REGISTER("VCA",'a',LA_IDN_VCA, LA_PC_IDN_VCA, IDN_VCA, laSynthNodeVCA);
  1303. LA_IDN_REGISTER("Noise",'~',LA_IDN_NOISE, LA_PC_IDN_NOISE, IDN_Noise, laSynthNodeNoise);
  1304. LA_IDN_REGISTER("Scope",'s',LA_IDN_SCOPE, LA_PC_IDN_SCOPE, IDN_Scope, laSynthNodeScope);
  1305. LA_IDN_REGISTER("Sound Output",U'🕪',LA_IDN_OUTPUT, LA_PC_IDN_OUTPUT, IDN_Output, laSynthNodeOutput);
  1306. LA_IDN_REGISTER("Envelope",U'^',LA_IDN_ENVELOPE, LA_PC_IDN_ENVELOPE, IDN_Envelope, laSynthNodeEnvelope);
  1307. LA_IDN_REGISTER("Quantize",U'#',LA_IDN_QUANTIZE, LA_PC_IDN_QUANTIZE, IDN_Quantize, laSynthNodeQuantize);
  1308. LA_IDN_REGISTER("Sound Player",U'⏯',LA_IDN_SYNTH_DRIVER, LA_PC_IDN_SYNTH_DRIVER, IDN_SynthDriver, laSynthNodeDriver);
  1309. LA_IDN_REGISTER("Sound Trigger",U'🎵',LA_IDN_SYNTH_TRIGGER, LA_PC_IDN_SYNTH_TRIGGER, IDN_SynthTrigger, laSynthNodeTrigger);
  1310. LA_NODE_CATEGORY_SYNTHESIZER=laEnsureNodeCategory("OSC",0,LA_RACK_TYPE_AUDIO);
  1311. LA_NODE_CATEGORY_SYSTEM_SOUND=laEnsureNodeCategory("System",0,LA_RACK_TYPE_AUDIO);
  1312. laNodeCategoryAddNodeTypes(LA_NODE_CATEGORY_SYNTHESIZER,
  1313. &LA_IDN_INPUT,&LA_IDN_PULSE,&LA_IDN_FM,&LA_IDN_NOISE,&LA_IDN_VCA,&LA_IDN_ENVELOPE,&LA_IDN_QUANTIZE,&LA_IDN_SCOPE,&LA_IDN_SYNTH_TRIGGER,0LL);
  1314. laNodeCategoryAddNodeTypes(LA_NODE_CATEGORY_SYSTEM_SOUND, &LA_IDN_OUTPUT,0LL);
  1315. LA_NODE_CATEGORY_DRIVER=laEnsureNodeCategory("Driver",0,LA_RACK_TYPE_DRIVER);
  1316. laNodeCategoryAddNodeTypes(LA_NODE_CATEGORY_DRIVER, &LA_IDN_SYNTH_DRIVER, &LA_IDN_SYNTH_TRIGGER,0LL);
  1317. }