*/}}

la_audio.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. #include "miniaudio.h"
  19. #include "la_5.h"
  20. extern LA MAIN;
  21. laPropContainer* LA_PC_SYNTH;
  22. laNodeCategory* LA_NODE_CATEGORY_SYNTHESIZER;
  23. laNodeCategory* LA_NODE_CATEGORY_SYSTEM_SOUND;
  24. laBaseNodeType LA_IDN_FM;
  25. laBaseNodeType LA_IDN_VCA;
  26. laBaseNodeType LA_IDN_OUTPUT;
  27. laBaseNodeType LA_IDN_SCOPE;
  28. laPropContainer* LA_PC_IDN_FM;
  29. laPropContainer* LA_PC_IDN_VCA;
  30. laPropContainer* LA_PC_IDN_OUTPUT;
  31. laPropContainer* LA_PC_IDN_SCOPE;
  32. #define _2PI 6.283185307
  33. #define VAL2FREQ(val) \
  34. 16.0f*pow(2,val) // 0-10, 16-16384Hz
  35. #define WRAPPHASE(p) \
  36. while(p>_2PI){ p-=_2PI; }
  37. #define INITPACKET(arr) \
  38. if(!arr){ arr=memAcquireSimple(sizeof(real)*LA_SYNTH_PLEN); }
  39. #define INPUTPACKET(ptr,socket) \
  40. {if(socket->Source && socket->Source->DataType==(LA_PROP_FLOAT|LA_PROP_ARRAY) && socket->Source->ArrLen>=LA_SYNTH_PLEN){ \
  41. ptr=((real*)socket->Source->Data); }else{ ptr=0; } }
  42. void IDN_FMInit(laSynthNodeFM* n, int NoCreate){
  43. INITPACKET(n->OutSamples);
  44. if(NoCreate){ return; }
  45. n->InFrequency=laCreateInSocket("FREQ",0); n->Out=laCreateOutSocket(n,"OUT",LA_PROP_FLOAT|LA_PROP_ARRAY);
  46. strSafeSet(&n->Base.Name,"FM"); n->Frequency=4;
  47. }
  48. void IDN_FMDestroy(laSynthNodeFM* n){
  49. laDestroyInSocket(n->InFrequency); laDestroyOutSocket(n->Out); strSafeDestroy(&n->Base.Name); memFree(n->OutSamples);
  50. }
  51. int IDN_FMVisit(laSynthNodeFM* n, laNodeVisitInfo* vi){
  52. if(LA_SRC_AND_PARENT(n->InFrequency)){ laBaseNode*bn=n->InFrequency->Source->Parent; LA_VISIT_NODE(bn,vi); }
  53. LA_ADD_THIS_NODE(n,vi);
  54. return LA_DAG_FLAG_PERM;
  55. }
  56. int IDN_FMEval(laSynthNodeFM* n){
  57. real f=n->Frequency, constf=0; real* inputf;
  58. INPUTPACKET(inputf,n->InFrequency); LA_GET_SRC_AS_VALUE(constf,n->InFrequency);
  59. for(int i=0;i<LA_SYNTH_PLEN;i++){
  60. real useF=inputf?(inputf[i]+f):(f+constf); if(n->Slow) useF-=10;
  61. n->Phase+=MAIN.Audio->AudioFrameInterval*VAL2FREQ(useF)*_2PI;
  62. WRAPPHASE(n->Phase);
  63. n->OutSamples[i]=sin(n->Phase)*10;
  64. }
  65. n->Out->Data=n->OutSamples; n->Out->ArrLen=LA_SYNTH_PLEN;
  66. return 1;
  67. }
  68. void IDN_FMCopy(laSynthNodeFM* new, laSynthNodeFM* old, int DoRematch){
  69. if(DoRematch){ LA_IDN_NEW_LINK(InFrequency) return; }
  70. LA_IDN_OLD_DUPL(Out); new->Frequency=old->Frequency;
  71. }
  72. void laui_FMNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  73. laColumn* c=laFirstColumn(uil); laSynthNodeFM*n=This->EndInstance;
  74. LA_BASE_NODE_HEADER(uil,c,This);
  75. laUiItem* b=laBeginRow(uil,c,0,0);
  76. laShowNodeSocket(uil,c,This,"in_frequency",0)->Flags|=LA_UI_SOCKET_LABEL_E;
  77. laShowItem(uil,c,This,"slow");
  78. laShowItem(uil,c,This,"frequency")->Expand=1;
  79. laShowNodeSocket(uil,c,This,"out",0);
  80. laEndRow(uil,b);
  81. }
  82. void IDN_VCAInit(laSynthNodeVCA* n, int NoCreate){
  83. INITPACKET(n->OutSamples);
  84. if(NoCreate){ return; }
  85. n->InAmp=laCreateInSocket("AMP",0); n->In=laCreateInSocket("IN",0);
  86. n->Out=laCreateOutSocket(n,"OUT",LA_PROP_FLOAT|LA_PROP_ARRAY);
  87. strSafeSet(&n->Base.Name,"FM"); n->Amp=10;
  88. }
  89. void IDN_VCADestroy(laSynthNodeVCA* n){
  90. laDestroyInSocket(n->InAmp); laDestroyInSocket(n->In);
  91. laDestroyOutSocket(n->Out); strSafeDestroy(&n->Base.Name); memFree(n->OutSamples);
  92. }
  93. int IDN_VCAVisit(laSynthNodeVCA* n, laNodeVisitInfo* vi){
  94. if(LA_SRC_AND_PARENT(n->In)){ laBaseNode*bn=n->In->Source->Parent; LA_VISIT_NODE(bn,vi); }
  95. if(LA_SRC_AND_PARENT(n->InAmp)){ laBaseNode*bn=n->InAmp->Source->Parent; LA_VISIT_NODE(bn,vi); }
  96. LA_ADD_THIS_NODE(n,vi);
  97. return LA_DAG_FLAG_PERM;
  98. }
  99. int IDN_VCAEval(laSynthNodeVCA* n){
  100. real amp=n->Amp, constamp=amp; real* inputamp=0,*input=0;
  101. INPUTPACKET(input,n->In); INPUTPACKET(inputamp,n->InAmp);
  102. if(!inputamp) LA_GET_SRC_AS_VALUE(constamp,n->InAmp); constamp/=10;
  103. n->Out->Data=n->OutSamples; n->Out->ArrLen=LA_SYNTH_PLEN;
  104. if(!input){ memset(n->OutSamples,0,sizeof(real)*LA_SYNTH_PLEN); return 0; }
  105. for(int i=0;i<LA_SYNTH_PLEN;i++){
  106. real useA=(inputamp?(inputamp[i]/10):1)*constamp;
  107. n->OutSamples[i]= input[i]*useA;
  108. }
  109. return 1;
  110. }
  111. void IDN_VCACopy(laSynthNodeVCA* new, laSynthNodeVCA* old, int DoRematch){
  112. if(DoRematch){ LA_IDN_NEW_LINK(InAmp) LA_IDN_NEW_LINK(In) return; }
  113. LA_IDN_OLD_DUPL(Out); new->Amp=old->Amp;
  114. }
  115. void laui_VCANode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  116. laColumn* c=laFirstColumn(uil); laSynthNodeVCA*n=This->EndInstance;
  117. LA_BASE_NODE_HEADER(uil,c,This);
  118. laColumn* cl,*cr; laSplitColumn(uil,c,0.3); cl=laLeftColumn(c,0); cr=laRightColumn(c,0);
  119. laUiItem* b=laBeginRow(uil,c,0,0);
  120. laShowNodeSocket(uil,c,This,"in",0)->Flags|=LA_UI_SOCKET_LABEL_E;
  121. laShowNodeSocket(uil,c,This,"in_amp",0);
  122. laShowItem(uil,c,This,"amp")->Expand=1;
  123. laShowNodeSocket(uil,cr,This,"out",0);
  124. laEndRow(uil,b);
  125. }
  126. void IDN_ScopeInit(laSynthNodeScope* n, int NoCreate){
  127. if(!n->Display1){ n->Display1=memAcquireSimple(sizeof(real)*LA_SYNTH_PLEN*2); }
  128. if(!n->Display2){ n->Display2=memAcquireSimple(sizeof(real)*LA_SYNTH_PLEN*2); }
  129. pthread_spin_init(&n->Lock,0);
  130. if(NoCreate){ return; }
  131. n->In1=laCreateInSocket("CH1",0); n->In2=laCreateInSocket("CH2",0);
  132. strSafeSet(&n->Base.Name,"SCOPE"); n->Time=5; n->Brightness1=1.0f; n->Brightness2=0.5f;
  133. n->Gain1=1; n->Gain2=1;
  134. }
  135. void IDN_ScopeDestroy(laSynthNodeScope* n){
  136. laDestroyInSocket(n->In1); laDestroyOutSocket(n->In2); strSafeDestroy(&n->Base.Name);
  137. memFree(n->Display1); memFree(n->Display2);
  138. pthread_spin_destroy(&n->Lock);
  139. }
  140. int IDN_ScopeVisit(laSynthNodeScope* n, laNodeVisitInfo* vi){
  141. if(LA_SRC_AND_PARENT(n->In1)){ laBaseNode*bn=n->In1->Source->Parent; LA_VISIT_NODE(bn,vi); }
  142. if(LA_SRC_AND_PARENT(n->In2)){ laBaseNode*bn=n->In2->Source->Parent; LA_VISIT_NODE(bn,vi); }
  143. LA_ADD_THIS_NODE(n,vi);
  144. return LA_DAG_FLAG_PERM;
  145. }
  146. int IDN_ScopeEval(laSynthNodeScope* n){
  147. if(!n->Display1){ n->Display1=memAcquireSimple(sizeof(real)*LA_SYNTH_PLEN*2); }
  148. if(!n->Display2){ n->Display2=memAcquireSimple(sizeof(real)*LA_SYNTH_PLEN*2); }
  149. real* ch1,*ch2; INPUTPACKET(ch1,n->In1); INPUTPACKET(ch2,n->In2);
  150. int Frame=1<<(n->Time-1); TNS_CLAMP(Frame,1,LA_SYNTH_PLEN);
  151. int Times=LA_SYNTH_PLEN/Frame; int sp=0; pthread_spin_lock(&n->Lock);
  152. for(int t=0;t<Times;t++){
  153. real smin1=FLT_MAX,smax1=-FLT_MAX,smin2=FLT_MAX,smax2=-FLT_MAX;
  154. for(int i=0;i<Frame;i++){
  155. if(ch1){ smin1=TNS_MIN2(smin1,ch1[sp]); smax1=TNS_MAX2(smax1,ch1[sp]); }
  156. if(ch2){ smin2=TNS_MIN2(smin2,ch2[sp]); smax2=TNS_MAX2(smax2,ch2[sp]); }
  157. sp++;
  158. }
  159. if(!ch1){smin1=smax1=0;} if(!ch2){smin2=smax2=0;}
  160. int ns=n->NextSample;
  161. n->Display1[ns]=smin1; n->Display1[ns+LA_SYNTH_PLEN]=smax1;
  162. n->Display2[ns]=smin2; n->Display2[ns+LA_SYNTH_PLEN]=smax2;
  163. n->NextSample++; if(n->NextSample>=LA_SYNTH_PLEN) n->NextSample=0;
  164. }
  165. n->FromSynth=MAIN.Audio->AudioEvalSynth;
  166. pthread_spin_unlock(&n->Lock);
  167. return 1;
  168. }
  169. void IDN_ScopeCopy(laSynthNodeScope* new, laSynthNodeScope* old, int DoRematch){
  170. if(DoRematch){ LA_IDN_NEW_LINK(In1) LA_IDN_NEW_LINK(In2) return; }
  171. new->Brightness1=old->Brightness1; new->Brightness2=old->Brightness2;
  172. new->Gain1=old->Gain1; new->Gain2=old->Gain2;
  173. new->Offset1=old->Offset1; new->Offset2=old->Offset2;
  174. new->Time=old->Time;
  175. }
  176. void laui_ScopeNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  177. laColumn* c=laFirstColumn(uil); laSynthNodeScope*n=This->EndInstance;
  178. LA_BASE_NODE_HEADER(uil,c,This);
  179. laColumn* cl,*cr; laSplitColumn(uil,c,0.5); cl=laLeftColumn(c,0); cr=laRightColumn(c,0);
  180. laUiItem* b=laBeginRow(uil,c,0,0);
  181. laShowNodeSocket(uil,c,This,"in1",0)->Flags|=LA_UI_SOCKET_LABEL_E;
  182. laUiItem* u=laShowItem(uil,c,This,"gain1");u->Flags|=LA_UI_FLAGS_EXPAND; u->Expand=1;
  183. laEndRow(uil,b);
  184. b=laBeginRow(uil,c,0,0);
  185. laShowNodeSocket(uil,c,This,"in2",0)->Flags|=LA_UI_SOCKET_LABEL_E;
  186. u=laShowItem(uil,c,This,"gain2");u->Flags|=LA_UI_FLAGS_EXPAND; u->Expand=1;
  187. laEndRow(uil,b);
  188. laShowItem(uil,c,This,"scope")->Extra->HeightCoeff=6;
  189. laShowLabel(uil,cl,"CH1",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
  190. laShowItem(uil,cl,This,"bright1"); laShowItem(uil,cl,This,"offset1");
  191. laShowLabel(uil,cr,"CH2",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
  192. laShowItem(uil,cr,This,"bright2"); laShowItem(uil,cr,This,"offset2");
  193. laShowItem(uil,c,This,"time")->Flags|=LA_UI_FLAGS_EXPAND;
  194. }
  195. void IDN_OutputInit(laSynthNodeOutput* n, int NoCreate){
  196. if(NoCreate){ return; }
  197. n->In=laCreateInSocket("IN",0); strSafeSet(&n->Base.Name,"Output");
  198. }
  199. void IDN_OutputDestroy(laSynthNodeOutput* n){
  200. laDestroyInSocket(n->In); strSafeDestroy(&n->Base.Name);
  201. }
  202. int IDN_OutputVisit(laSynthNodeOutput* n, laNodeVisitInfo* vi){
  203. if(LA_SRC_AND_PARENT(n->In)){ laBaseNode*bn=n->In->Source->Parent; LA_VISIT_NODE(bn,vi); }
  204. LA_ADD_THIS_NODE(n,vi);
  205. return LA_DAG_FLAG_PERM;
  206. }
  207. int IDN_OutputEval(laSynthNodeOutput* n){
  208. real* input; INPUTPACKET(input,n->In);
  209. if(input) memcpy(MAIN.Audio->AudioEvalSynth->AudioSamples,input,sizeof(real)*LA_SYNTH_PLEN);
  210. else memset(MAIN.Audio->AudioEvalSynth->AudioSamples,0,sizeof(real)*LA_SYNTH_PLEN);
  211. return 1;
  212. }
  213. void IDN_OutputCopy(laSynthNodeOutput* new, laSynthNodeOutput* old, int DoRematch){
  214. if(DoRematch){ LA_IDN_NEW_LINK(In) return; }
  215. }
  216. void laui_OutputNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  217. laColumn* c=laFirstColumn(uil); laSynthNodeOutput*n=This->EndInstance;
  218. LA_BASE_NODE_HEADER(uil,c,This);
  219. laColumn* cl,*cr; laSplitColumn(uil,c,0.3); cl=laLeftColumn(c,0); cr=laRightColumn(c,0);
  220. laUiItem* b=laBeginRow(uil,c,0,0);
  221. laShowNodeSocket(uil,c,This,"in",0)->Flags|=LA_UI_SOCKET_LABEL_E;
  222. laShowLabel(uil,c,"🕪",0,0);
  223. laEndRow(uil,b);
  224. }
  225. void laRebuildSynthGraphs(){
  226. if(MAIN.Audio->CurrentSynth){
  227. pthread_spin_lock(&MAIN.Audio->CurrentSynth->Lock);
  228. laRebuildPageEval(MAIN.Audio->CurrentSynth->Page);
  229. pthread_spin_unlock(&MAIN.Audio->CurrentSynth->Lock);
  230. }
  231. }
  232. int laEvalSynthGraphs(){
  233. laSynth* ss; int any=0;
  234. memset(MAIN.Audio->AudioSamples,0,sizeof(real)*LA_SYNTH_PLEN);
  235. for(ss=MAIN.Audio->Synths.pFirst;ss;ss=ss->Item.pNext){
  236. pthread_spin_lock(&ss->Lock);
  237. if(!ss->Playing){ pthread_spin_unlock(&ss->Lock); continue; }
  238. MAIN.Audio->AudioEvalSynth=ss; MAIN.Audio->AudioEvalTime=0; INITPACKET(ss->AudioSamples);
  239. laRunPage(ss->Page,1);
  240. pthread_spin_unlock(&ss->Lock);
  241. for(int i=0;i<LA_SYNTH_PLEN;i++){
  242. MAIN.Audio->AudioSamples[i]+=ss->AudioSamples[i];
  243. }
  244. any=1;
  245. }
  246. return any;
  247. }
  248. void laaudio_DataCallback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount){
  249. float* out=pOutput; int any=0;
  250. for(int i=0;i<frameCount;i++){
  251. if(MAIN.Audio->NextAudioSample>=LA_SYNTH_PLEN){
  252. MAIN.Audio->NextAudioSample=0;
  253. if(laEvalSynthGraphs()){ any=1;
  254. MAIN.Audio->AudioEvalTotalSamples+=LA_SYNTH_PLEN;
  255. MAIN.Audio->AudioEvalTime=(real)MAIN.Audio->AudioEvalTotalSamples/MAIN.Audio->AudioSampleRate;
  256. }
  257. }
  258. out[i]=MAIN.Audio->AudioSamples[MAIN.Audio->NextAudioSample]/10;
  259. MAIN.Audio->NextAudioSample++;
  260. }
  261. pthread_spin_lock(&MAIN.Audio->AudioStatusLock);
  262. MAIN.Audio->AudioAny=any;
  263. pthread_spin_unlock(&MAIN.Audio->AudioStatusLock);
  264. }
  265. void laInitMiniAudio(){
  266. int SampleRate=48000;
  267. ma_context context;
  268. if (ma_context_init(NULL, 0, NULL, &context) != MA_SUCCESS) {
  269. printf("Can't init miniaudio context\n");
  270. }
  271. ma_device_info* pPlaybackInfos;
  272. ma_uint32 playbackCount;
  273. ma_device_info* pCaptureInfos;
  274. ma_uint32 captureCount;
  275. if (ma_context_get_devices(&context, &pPlaybackInfos, &playbackCount, &pCaptureInfos, &captureCount) != MA_SUCCESS) {
  276. printf("Can't get audio devices\n");
  277. }
  278. for (ma_uint32 iDevice = 0; iDevice < playbackCount; iDevice += 1) {
  279. printf("%d - %s\n", iDevice, pPlaybackInfos[iDevice].name);
  280. }
  281. ma_device_config config = ma_device_config_init(ma_device_type_playback);
  282. config.playback.pDeviceID= 0;
  283. config.playback.format = ma_format_f32;
  284. config.playback.channels = 1;
  285. config.sampleRate = SampleRate;
  286. config.dataCallback = laaudio_DataCallback;
  287. config.pUserData = 0;
  288. if (ma_device_init(NULL, &config, &MAIN.Audio->AudioDevice) != MA_SUCCESS){ return; }
  289. MAIN.Audio->AudioSampleRate=SampleRate;
  290. MAIN.Audio->NextAudioSample=LA_SYNTH_PLEN;
  291. MAIN.Audio->AudioFrameInterval=1.0f/SampleRate;
  292. INITPACKET(MAIN.Audio->AudioSamples);
  293. pthread_spin_init(&MAIN.Audio->AudioStatusLock,0);
  294. }
  295. void laDeinitMiniAudio(){
  296. pthread_spin_destroy(&MAIN.Audio->AudioStatusLock);
  297. ma_device_stop(&MAIN.Audio->AudioDevice);
  298. ma_device_uninit(&MAIN.Audio->AudioDevice);
  299. }
  300. // Operations
  301. laSynth* laNewSynth(char* Name){
  302. laSynth* ss=memAcquireHyper(sizeof(laSynth));
  303. ss->Page=memAcquire(sizeof(laRackPage));
  304. ss->Page->RackType=LA_RACK_TYPE_AUDIO;
  305. strSafeSet(&ss->Name,Name);
  306. lstAppendItem(&MAIN.Audio->Synths,ss);
  307. memAssignRef(MAIN.Audio,&MAIN.Audio->CurrentSynth,ss);
  308. pthread_spin_init(&ss->Lock,0);
  309. return ss;
  310. }
  311. void laRemoveSynth(laSynth* ss){ if(!ss) return;
  312. strSafeDestroy(&ss->Name);
  313. if(MAIN.Audio->CurrentSynth==ss){
  314. laSynth* nss=ss->Item.pNext?ss->Item.pNext:ss->Item.pPrev;
  315. memAssignRef(MAIN.Audio,&MAIN.Audio->CurrentSynth,nss);
  316. }laNotifyInstanceUsers(ss);
  317. while(ss->Page->Racks.pFirst){ laDestroyRack(ss->Page->Racks.pFirst); } memLeave(ss->Page); ss->Page=0;
  318. lstRemoveItem(&MAIN.Audio->Synths,ss);
  319. pthread_spin_destroy(&ss->Lock);
  320. memLeave(ss);
  321. }
  322. int OPINV_ShedNewSynth(laOperator* a, laEvent* e){
  323. laNewSynth("New Synth");
  324. laNotifyInstanceUsers(MAIN.Audio); laRecordAndPush(0,"la.audio","New synth", 0);
  325. return LA_FINISHED;
  326. }
  327. int OPCHK_IslaSynth(laPropPack *This, laStringSplitor *ss){
  328. if (This && This->EndInstance && la_EnsureSubTarget(This->LastPs->p,0) == LA_PC_SYNTH) return 1;
  329. return 0;
  330. }
  331. int OPINV_ShedRemoveSynth(laOperator* a, laEvent* e){
  332. laSynth* ss=a->This?a->This->EndInstance:0; if(!ss)return LA_CANCELED;
  333. if(strSame(strGetArgumentString(a->ExtraInstructionsP,"confirm"),"true")){
  334. laRemoveSynth(ss);
  335. laNotifyInstanceUsers(MAIN.Audio); laRecordAndPush(0,"la.audio","Remove la project", 0);
  336. return LA_FINISHED;
  337. }
  338. laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e);
  339. return LA_RUNNING;
  340. }
  341. void laui_RemoveSynth(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  342. laColumn* c=laFirstColumn(uil);
  343. laShowItemFull(uil,c,This,"remove_synth",0,"confirm=true;text=Confirm",0,0);
  344. }
  345. int OPINV_laSynthPlay(laOperator* a, laEvent* e){
  346. laSynth* ss=a->This?a->This->EndInstance:0; if(!ss)return LA_CANCELED;
  347. char* str=strGetArgumentString(a->ExtraInstructionsP, "mode");
  348. int play=0; if(str && strSame(str,"PLAY")) play=1;
  349. if(play){ MAIN.Audio->AudioAny=1; }
  350. pthread_spin_lock(&ss->Lock); ss->Playing=play; pthread_spin_unlock(&ss->Lock);
  351. ma_device_start(&MAIN.Audio->AudioDevice); laNotifyInstanceUsers(ss);
  352. return LA_FINISHED;
  353. }
  354. void lauidetached_Synthersizers(laPanel* p){
  355. la_MakeDetachedProp(p, "la.detached_view_switch", "detached");
  356. la_MakeDetachedProp(p, "la.audio.current_synth", "page");
  357. }
  358. void laui_Synthersizers(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  359. laColumn* c=laFirstColumn(uil),*cl, *cr,*crl,*crr;
  360. laSplitColumn(uil,c,0.4); cl=laLeftColumn(c,1); cr=laRightColumn(c,0);
  361. laSplitColumn(uil,cr,0.6); crl=laLeftColumn(cr,0); crr=laRightColumn(cr,2);
  362. laShowItemFull(uil,cl,Extra,"detached",0,0,0,0)->Flags|=LA_UI_FLAGS_HIGHLIGHT|LA_UI_FLAGS_ICON;
  363. #define ADD_PAGE \
  364. laUiItem* b=laBeginRow(uil,crl,0,0);\
  365. laShowItemFull(uil,crl,&rb->PP,"",LA_WIDGET_COLLECTION_SELECTOR,0,laui_IdentifierOnly,0)\
  366. ->Flags|=LA_UI_COLLECTION_SIMPLE_SELECTOR;\
  367. laUiItem* b3=laOnConditionThat(uil,cr,laPropExpression(&rb->PP,""));{\
  368. laShowItem(uil,crl,&rb->PP,"name");\
  369. laShowItem(uil,crl,0,"LA_new_synth")->Flags|=LA_UI_FLAGS_ICON;\
  370. laShowSeparator(uil,crl);\
  371. laShowItem(uil,crl,&rb->PP,"remove_synth")->Flags|=LA_UI_FLAGS_ICON;\
  372. laShowSeparator(uil,crl)->Expand=1;\
  373. }laElse(uil,b3);{\
  374. laShowItem(uil,crl,0,"LA_new_synth");\
  375. }laEndCondition(uil,b3);\
  376. laEndRow(uil,b);\
  377. b3=laOnConditionThat(uil,cr,laPropExpression(&rb->PP,""));{\
  378. laUiItem* b4=laOnConditionThat(uil,crr,laPropExpression(&rb->PP,"playing"));{\
  379. laShowItemFull(uil,crr,&rb->PP,"play",0,"text=❚❚;",0,0) \
  380. ->Flags|=LA_UI_FLAGS_EXIT_WHEN_TRIGGERED|LA_TEXT_ALIGN_CENTER; \
  381. }laElse(uil,b4);{\
  382. laShowItemFull(uil,crr,&rb->PP,"play",0,"icon=▶;mode=PLAY;",0,0) \
  383. ->Flags|=LA_UI_FLAGS_ICON|LA_UI_FLAGS_EXIT_WHEN_TRIGGERED; \
  384. }laEndCondition(uil,b4);\
  385. laShowItemFull(uil,c,&rb->PP,"page",LA_WIDGET_COLLECTION_SINGLE,0,laui_RackPage,0)->Flags|=LA_UI_FLAGS_NO_DECAL;\
  386. }laEndCondition(uil,b3);\
  387. laUiItem* b1=laOnConditionThat(uil,cr,laPropExpression(Extra,"detached"));{
  388. laUiItem* rb=laShowInvisibleItem(uil,cr,Extra,"page");
  389. ADD_PAGE
  390. }laElse(uil,b1);{
  391. laUiItem* rb=laShowInvisibleItem(uil,cr,0,"la.audio.current_synth");
  392. ADD_PAGE
  393. }laEndCondition(uil,b1);
  394. #undef ADD_PAGE
  395. }
  396. void* laget_FirstSynth(void* unused1,void* unused2){
  397. return MAIN.Audio->Synths.pFirst;
  398. }
  399. void lapost_Synth(laSynth* ss){
  400. pthread_spin_init(&ss->Lock,0);
  401. }
  402. int laget_SynthPlaying(laSynth* ss){
  403. int play=0;
  404. pthread_spin_lock(&ss->Lock); play=ss->Playing; pthread_spin_unlock(&ss->Lock);
  405. return play;
  406. }
  407. void laset_CurrentSynth(laAudio* a,laSynth* s){
  408. memAssignRef(a,&a->CurrentSynth,s);
  409. }
  410. void la_AudioPreFrame(){
  411. if(MAIN.GraphNeedsRebuild){ laRebuildSynthGraphs(); }
  412. int anyaudio=0;
  413. pthread_spin_lock(&MAIN.Audio->AudioStatusLock);
  414. anyaudio=MAIN.Audio->AudioAny;
  415. pthread_spin_unlock(&MAIN.Audio->AudioStatusLock);
  416. if(!anyaudio){ ma_device_stop(&MAIN.Audio->AudioDevice); }
  417. }
  418. void laInitAudio(){
  419. laPropContainer* pc; laProp* p; laSubProp* sp;
  420. laInitMiniAudio();
  421. laRegisterUiTemplate("LA_synthesizers","Synthesizers",laui_Synthersizers,lauidetached_Synthersizers,0,"Audio",0,0,0);
  422. laCreateOperatorType("LA_new_synth", "New Synthesizer", "Add a synthesizer",0,0,0,OPINV_ShedNewSynth,0,'+',0);
  423. laCreateOperatorType("LA_remove_synth", "Remove Synthesizer", "Remove a synthesizer",OPCHK_IslaSynth,0,0,OPINV_ShedRemoveSynth,OPMOD_FinishOnData,L'🗴',0)
  424. ->UiDefine=laui_RemoveSynth;
  425. laCreateOperatorType("LA_synth_play", "Play Synthesizer", "Play a synthesizer",OPCHK_IslaSynth,0,0,OPINV_laSynthPlay,0,0,0);
  426. pc = laAddPropertyContainer("la_synth", "LaGUI Synthesizer", "LaGUI synthesizer", 0,0,sizeof(laSynth),lapost_Synth,0,2);{
  427. LA_PC_SYNTH=pc;
  428. laAddStringProperty(pc,"name","Name","Name of the synthesizer",0,0,0,0,1,offsetof(laSynth,Name),0,0,0,0,LA_AS_IDENTIFIER);
  429. 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);
  430. 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);
  431. laAddEnumItemAs(p,"IDLE","Idle","Synth is not playing",0,0);
  432. laAddEnumItemAs(p,"PLAYING","Playing","Synth is playing",1,0);
  433. laAddOperatorProperty(pc,"remove_synth","Remove Synth", "Remove synth", "LA_remove_synth", L'🗴', 0);
  434. laAddOperatorProperty(pc,"play","Play Synth", "Play synth", "LA_synth_play", 0, 0);
  435. }
  436. pc=laAddPropertyContainer("la_node_synth_fm", "FM OSC Node", "Osilliator node with frequency modulation",0,laui_FMNode,sizeof(laSynthNodeFM),lapost_Node,0,1);
  437. LA_PC_IDN_FM=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
  438. laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  439. 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);
  440. laAddSubGroup(pc,"out", "Out","Output","la_out_socket",0,0,0,offsetof(laSynthNodeFM,Out),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  441. 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);
  442. 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);
  443. laAddEnumItemAs(p,"NONE","None","Regular frequency range",0,0);
  444. laAddEnumItemAs(p,"SLOW","Slow","Slow frequency range",1,0);
  445. pc=laAddPropertyContainer("la_node_synth_vca", "VCA Node", "Voltage controlled amplifier",0,laui_VCANode,sizeof(laSynthNodeVCA),lapost_Node,0,1);
  446. LA_PC_IDN_VCA=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
  447. laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  448. 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);
  449. laAddSubGroup(pc,"in", "Input","Input","la_in_socket",0,0,0,offsetof(laSynthNodeVCA,In),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  450. laAddSubGroup(pc,"out", "Out","Output","la_out_socket",0,0,0,offsetof(laSynthNodeVCA,Out),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  451. 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);
  452. pc=laAddPropertyContainer("la_node_synth_scope", "Scope Node", "Scope for synthesizer",0,laui_ScopeNode,sizeof(laSynthNodeScope),lapost_Node,0,1);
  453. LA_PC_IDN_SCOPE=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
  454. laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  455. 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);
  456. 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);
  457. 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);
  458. 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);
  459. #define _STR(a) #a
  460. #define ADD_TIME_FACTOR(a) laAddEnumItemAs(p,"FAC"_STR(a),_STR(a),"^"_STR(a)" time",a,0);
  461. ADD_TIME_FACTOR(1) ADD_TIME_FACTOR(2) ADD_TIME_FACTOR(3) ADD_TIME_FACTOR(4) ADD_TIME_FACTOR(5)
  462. ADD_TIME_FACTOR(6) ADD_TIME_FACTOR(7) ADD_TIME_FACTOR(8) ADD_TIME_FACTOR(9) ADD_TIME_FACTOR(10)
  463. #undef ADD_TIME_FACTOR
  464. 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);
  465. #define ADD_GAIN(a) laAddEnumItemAs(p,"GAIN"_STR(a),_STR(a),"^"_STR(a)" gain",a,0);
  466. ADD_GAIN(1) ADD_GAIN(2) ADD_GAIN(3) ADD_GAIN(4) ADD_GAIN(5) ADD_GAIN(6)
  467. 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);
  468. ADD_GAIN(1) ADD_GAIN(2) ADD_GAIN(3) ADD_GAIN(4) ADD_GAIN(5) ADD_GAIN(6)
  469. #undef ADD_GAIN
  470. 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);
  471. 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);
  472. 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);
  473. 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);
  474. pc=laAddPropertyContainer("la_node_synth_output", "Output Node", "Sound output to system",0,laui_OutputNode,sizeof(laSynthNodeOutput),lapost_Node,0,1);
  475. LA_PC_IDN_OUTPUT=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
  476. laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  477. 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);
  478. LA_IDN_REGISTER("VCO",'f',LA_IDN_FM, LA_PC_IDN_FM, IDN_FM, laSynthNodeFM);
  479. LA_IDN_REGISTER("VCA",'a',LA_IDN_VCA, LA_PC_IDN_VCA, IDN_VCA, laSynthNodeVCA);
  480. LA_IDN_REGISTER("Scope",'s',LA_IDN_SCOPE, LA_PC_IDN_SCOPE, IDN_Scope, laSynthNodeScope);
  481. LA_IDN_REGISTER("Sound Output",U'🕪',LA_IDN_OUTPUT, LA_PC_IDN_OUTPUT, IDN_Output, laSynthNodeOutput);
  482. LA_NODE_CATEGORY_SYNTHESIZER=laAddNodeCategory("OSC",0,LA_RACK_TYPE_AUDIO);
  483. LA_NODE_CATEGORY_SYSTEM_SOUND=laAddNodeCategory("System",0,LA_RACK_TYPE_AUDIO);
  484. laNodeCategoryAddNodeTypes(LA_NODE_CATEGORY_SYNTHESIZER, &LA_IDN_FM,0);
  485. laNodeCategoryAddNodeTypes(LA_NODE_CATEGORY_SYNTHESIZER, &LA_IDN_VCA,0);
  486. laNodeCategoryAddNodeTypes(LA_NODE_CATEGORY_SYNTHESIZER, &LA_IDN_SCOPE,0);
  487. laNodeCategoryAddNodeTypes(LA_NODE_CATEGORY_SYSTEM_SOUND, &LA_IDN_OUTPUT,0);
  488. }