/*
* Tiny la: A light weight game engine.
* Copyright (C) 2023 Wu Yiming
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"
#include "la_5.h"
extern LA MAIN;
laPropContainer* LA_PC_SYNTH;
laNodeCategory* LA_NODE_CATEGORY_SYNTHESIZER;
laNodeCategory* LA_NODE_CATEGORY_SYSTEM_SOUND;
laBaseNodeType LA_IDN_FM;
laBaseNodeType LA_IDN_VCA;
laBaseNodeType LA_IDN_NOISE;
laBaseNodeType LA_IDN_OUTPUT;
laBaseNodeType LA_IDN_SCOPE;
laPropContainer* LA_PC_IDN_FM;
laPropContainer* LA_PC_IDN_VCA;
laPropContainer* LA_PC_IDN_NOISE;
laPropContainer* LA_PC_IDN_OUTPUT;
laPropContainer* LA_PC_IDN_SCOPE;
#define _2PI 6.283185307
#define VAL2FREQ(val) \
16.0f*pow(2,val) // 0-10, 16-16384Hz
#define WRAPPHASE(p) \
while(p>_2PI){ p-=_2PI; }
#define INITPACKET(arr) \
if(!arr){ arr=memAcquireSimple(sizeof(real)*LA_SYNTH_PLEN); }
#define INPUTPACKET(ptr,socket) \
{if(socket->Source && socket->Source->DataType==(LA_PROP_FLOAT|LA_PROP_ARRAY) && socket->Source->ArrLen>=LA_SYNTH_PLEN){ \
ptr=((real*)socket->Source->Data); }else{ ptr=0; } }
void IDN_FMInit(laSynthNodeFM* n, int NoCreate){
INITPACKET(n->OutSamples);
if(NoCreate){ return; }
n->InFrequency=laCreateInSocket("FREQ",0); n->Out=laCreateOutSocket(n,"OUT",LA_PROP_FLOAT|LA_PROP_ARRAY);
strSafeSet(&n->Base.Name,"FM"); n->Frequency=4;
}
void IDN_FMDestroy(laSynthNodeFM* n){
laDestroyInSocket(n->InFrequency); laDestroyOutSocket(n->Out); strSafeDestroy(&n->Base.Name); memFree(n->OutSamples);
}
int IDN_FMVisit(laSynthNodeFM* n, laNodeVisitInfo* vi){
LA_GUARD_THIS_NODE(n,vi);
if(LA_SRC_AND_PARENT(n->InFrequency)){ laBaseNode*bn=n->InFrequency->Source->Parent; LA_VISIT_NODE(bn,vi); }
LA_ADD_THIS_NODE(n,vi);
return LA_DAG_FLAG_PERM;
}
int IDN_FMEval(laSynthNodeFM* n){
real f=n->Frequency, constf=0; real* inputf;
INPUTPACKET(inputf,n->InFrequency); LA_GET_SRC_AS_VALUE(constf,n->InFrequency);
for(int i=0;iSlow) useF-=10;
n->Phase+=MAIN.Audio->AudioFrameInterval*VAL2FREQ(useF)*_2PI;
WRAPPHASE(n->Phase);
n->OutSamples[i]=sin(n->Phase)*10;
}
n->Out->Data=n->OutSamples; n->Out->ArrLen=LA_SYNTH_PLEN;
return 1;
}
void IDN_FMCopy(laSynthNodeFM* new, laSynthNodeFM* old, int DoRematch){
if(DoRematch){ LA_IDN_NEW_LINK(InFrequency) return; }
LA_IDN_OLD_DUPL(Out); new->Frequency=old->Frequency;
}
void laui_FMNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
laColumn* c=laFirstColumn(uil); laSynthNodeFM*n=This->EndInstance;
LA_BASE_NODE_HEADER(uil,c,This);
laUiItem* b=laBeginRow(uil,c,0,0);
laShowNodeSocket(uil,c,This,"in_frequency",0)->Flags|=LA_UI_SOCKET_LABEL_E;
laShowItem(uil,c,This,"slow");
laShowItem(uil,c,This,"frequency")->Expand=1;
laShowNodeSocket(uil,c,This,"out",0);
laEndRow(uil,b);
}
void IDN_VCAInit(laSynthNodeVCA* n, int NoCreate){
INITPACKET(n->OutSamples);
if(NoCreate){ return; }
n->InAmp=laCreateInSocket("AMP",0); n->In=laCreateInSocket("IN",0);
n->Out=laCreateOutSocket(n,"OUT",LA_PROP_FLOAT|LA_PROP_ARRAY);
strSafeSet(&n->Base.Name,"FM"); n->Amp=10;
}
void IDN_VCADestroy(laSynthNodeVCA* n){
laDestroyInSocket(n->InAmp); laDestroyInSocket(n->In);
laDestroyOutSocket(n->Out); strSafeDestroy(&n->Base.Name); memFree(n->OutSamples);
}
int IDN_VCAVisit(laSynthNodeVCA* n, laNodeVisitInfo* vi){
LA_GUARD_THIS_NODE(n,vi);
if(LA_SRC_AND_PARENT(n->In)){ laBaseNode*bn=n->In->Source->Parent; LA_VISIT_NODE(bn,vi); }
if(LA_SRC_AND_PARENT(n->InAmp)){ laBaseNode*bn=n->InAmp->Source->Parent; LA_VISIT_NODE(bn,vi); }
LA_ADD_THIS_NODE(n,vi);
return LA_DAG_FLAG_PERM;
}
int IDN_VCAEval(laSynthNodeVCA* n){
real amp=n->Amp, constamp=amp; real* inputamp=0,*input=0;
INPUTPACKET(input,n->In); INPUTPACKET(inputamp,n->InAmp);
if(!inputamp) LA_GET_SRC_AS_VALUE(constamp,n->InAmp); constamp/=10;
n->Out->Data=n->OutSamples; n->Out->ArrLen=LA_SYNTH_PLEN;
if(!input){ memset(n->OutSamples,0,sizeof(real)*LA_SYNTH_PLEN); return 0; }
for(int i=0;iOutSamples[i]= input[i]*useA;
}
return 1;
}
void IDN_VCACopy(laSynthNodeVCA* new, laSynthNodeVCA* old, int DoRematch){
if(DoRematch){ LA_IDN_NEW_LINK(InAmp) LA_IDN_NEW_LINK(In) return; }
LA_IDN_OLD_DUPL(Out); new->Amp=old->Amp;
}
void laui_VCANode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
laColumn* c=laFirstColumn(uil); laSynthNodeVCA*n=This->EndInstance;
LA_BASE_NODE_HEADER(uil,c,This);
laColumn* cl,*cr; laSplitColumn(uil,c,0.3); cl=laLeftColumn(c,0); cr=laRightColumn(c,0);
laUiItem* b=laBeginRow(uil,c,0,0);
laShowNodeSocket(uil,c,This,"in",0)->Flags|=LA_UI_SOCKET_LABEL_E;
laShowNodeSocket(uil,c,This,"in_amp",0);
laShowItem(uil,c,This,"amp")->Expand=1;
laShowNodeSocket(uil,cr,This,"out",0);
laEndRow(uil,b);
}
void IDN_NoiseInit(laSynthNodeNoise* n, int NoCreate){
INITPACKET(n->OutWhiteSamples); INITPACKET(n->OutPinkSamples);
n->PinkA[0]=0.02109238; n->PinkA[1]=0.07113478; n->PinkA[2]=0.68873558;
n->PinkP[0]=0.3190; n->PinkP[1]=0.7756; n->PinkP[2]=0.9613;
if(NoCreate){ return; }
n->OutWhite=laCreateOutSocket(n,"WHITE",LA_PROP_FLOAT|LA_PROP_ARRAY);
n->OutPink=laCreateOutSocket(n,"PINK",LA_PROP_FLOAT|LA_PROP_ARRAY);
strSafeSet(&n->Base.Name,"Noise");
}
void IDN_NoiseDestroy(laSynthNodeNoise* n){
laDestroyOutSocket(n->OutWhite); laDestroyOutSocket(n->OutPink); strSafeDestroy(&n->Base.Name);
memFree(n->OutWhiteSamples); memFree(n->OutPinkSamples);
}
int IDN_NoiseVisit(laSynthNodeNoise* n, laNodeVisitInfo* vi){
LA_GUARD_THIS_NODE(n,vi); LA_ADD_THIS_NODE(n,vi);
return LA_DAG_FLAG_PERM;
}
int IDN_NoiseEval(laSynthNodeNoise* n){
real RMI2 = 2.0 / (real)RAND_MAX; // + 1.0; // change for range [0,1)
real offset = n->PinkA[0] + n->PinkA[1] + n->PinkA[2];
for(int i=0;iOutWhiteSamples[i]=X*2.5;
real temp; for(int i=0;i<3;i++){ temp=(real)rand();
n->PinkStates[i] = n->PinkP[i] * (n->PinkStates[i] - temp) + temp;
}
n->OutPinkSamples[i]=(n->PinkA[0]*n->PinkStates[0]+
n->PinkA[1]*n->PinkStates[1]+
n->PinkA[2]*n->PinkStates[2])*RMI2-offset;
n->OutPinkSamples[i]*=40;
}
n->OutWhite->Data=n->OutWhiteSamples; n->OutWhite->ArrLen=LA_SYNTH_PLEN;
n->OutPink->Data=n->OutPinkSamples; n->OutPink->ArrLen=LA_SYNTH_PLEN;
return 1;
}
void IDN_NoiseCopy(laSynthNodeNoise* new, laSynthNodeNoise* old, int DoRematch){
if(DoRematch){ return; }
LA_IDN_OLD_DUPL(OutWhite);
}
void laui_NoiseNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
laColumn* c=laFirstColumn(uil); laSynthNodeNoise*n=This->EndInstance;
LA_BASE_NODE_HEADER(uil,c,This);
laUiItem* b=laBeginRow(uil,c,0,0);
laShowNodeSocket(uil,c,This,"out_white",0)->Flags|=LA_UI_SOCKET_LABEL_N;
laShowNodeSocket(uil,c,This,"out_pink",0)->Flags|=LA_UI_SOCKET_LABEL_N;
laEndRow(uil,b);
}
void IDN_ScopeInit(laSynthNodeScope* n, int NoCreate){
if(!n->Display1){ n->Display1=memAcquireSimple(sizeof(real)*LA_SYNTH_PLEN*2); }
if(!n->Display2){ n->Display2=memAcquireSimple(sizeof(real)*LA_SYNTH_PLEN*2); }
pthread_spin_init(&n->Lock,0);
if(NoCreate){ return; }
n->In1=laCreateInSocket("CH1",0); n->In2=laCreateInSocket("CH2",0);
strSafeSet(&n->Base.Name,"SCOPE"); n->Time=5; n->Brightness1=0.7f; n->Brightness2=0.4f;
n->Gain1=1; n->Gain2=1;
}
void IDN_ScopeDestroy(laSynthNodeScope* n){
laDestroyInSocket(n->In1); laDestroyOutSocket(n->In2); strSafeDestroy(&n->Base.Name);
memFree(n->Display1); memFree(n->Display2);
pthread_spin_destroy(&n->Lock);
}
int IDN_ScopeVisit(laSynthNodeScope* n, laNodeVisitInfo* vi){
LA_GUARD_THIS_NODE(n,vi);
if(LA_SRC_AND_PARENT(n->In1)){ laBaseNode*bn=n->In1->Source->Parent; LA_VISIT_NODE(bn,vi); }
if(LA_SRC_AND_PARENT(n->In2)){ laBaseNode*bn=n->In2->Source->Parent; LA_VISIT_NODE(bn,vi); }
LA_ADD_THIS_NODE(n,vi);
return LA_DAG_FLAG_PERM;
}
int IDN_ScopeEval(laSynthNodeScope* n){
if(!n->Display1){ n->Display1=memAcquireSimple(sizeof(real)*LA_SYNTH_PLEN*2); }
if(!n->Display2){ n->Display2=memAcquireSimple(sizeof(real)*LA_SYNTH_PLEN*2); }
real* ch1,*ch2; INPUTPACKET(ch1,n->In1); INPUTPACKET(ch2,n->In2);
int Frame=1<<(n->Time-1); TNS_CLAMP(Frame,1,LA_SYNTH_PLEN);
int Times=LA_SYNTH_PLEN/Frame; int sp=0; pthread_spin_lock(&n->Lock);
for(int t=0;tNextSample;
n->Display1[ns]=smin1; n->Display1[ns+LA_SYNTH_PLEN]=smax1;
n->Display2[ns]=smin2; n->Display2[ns+LA_SYNTH_PLEN]=smax2;
n->NextSample++; if(n->NextSample>=LA_SYNTH_PLEN) n->NextSample=0;
}
n->FromSynth=MAIN.Audio->AudioEvalSynth;
pthread_spin_unlock(&n->Lock);
return 1;
}
void IDN_ScopeCopy(laSynthNodeScope* new, laSynthNodeScope* old, int DoRematch){
if(DoRematch){ LA_IDN_NEW_LINK(In1) LA_IDN_NEW_LINK(In2) return; }
new->Brightness1=old->Brightness1; new->Brightness2=old->Brightness2;
new->Gain1=old->Gain1; new->Gain2=old->Gain2;
new->Offset1=old->Offset1; new->Offset2=old->Offset2;
new->Time=old->Time;
}
void laui_ScopeNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
laColumn* c=laFirstColumn(uil); laSynthNodeScope*n=This->EndInstance;
LA_BASE_NODE_HEADER(uil,c,This);
laColumn* cl,*cr; laSplitColumn(uil,c,0.5); cl=laLeftColumn(c,0); cr=laRightColumn(c,0);
laUiItem* b=laBeginRow(uil,c,0,0);
laUiItem* u=laShowItem(uil,c,This,"gain1");u->Flags|=LA_UI_FLAGS_EXPAND; u->Expand=1;
laShowNodeSocket(uil,c,This,"in1",0)->Flags|=LA_UI_SOCKET_LABEL_W;
laEndRow(uil,b);
b=laBeginRow(uil,c,0,0);
u=laShowItem(uil,c,This,"gain2");u->Flags|=LA_UI_FLAGS_EXPAND; u->Expand=1;
laShowNodeSocket(uil,c,This,"in2",0)->Flags|=LA_UI_SOCKET_LABEL_W;
laEndRow(uil,b);
laShowItem(uil,c,This,"scope")->Extra->HeightCoeff=6;
laShowLabel(uil,cl,"CH1",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
laShowItem(uil,cl,This,"bright1"); laShowItem(uil,cl,This,"offset1");
laShowLabel(uil,cr,"CH2",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
laShowItem(uil,cr,This,"bright2"); laShowItem(uil,cr,This,"offset2");
laShowItem(uil,c,This,"time")->Flags|=LA_UI_FLAGS_EXPAND;
}
void IDN_OutputInit(laSynthNodeOutput* n, int NoCreate){
if(NoCreate){ return; }
n->In=laCreateInSocket("IN",0); strSafeSet(&n->Base.Name,"Output");
}
void IDN_OutputDestroy(laSynthNodeOutput* n){
laDestroyInSocket(n->In); strSafeDestroy(&n->Base.Name);
}
int IDN_OutputVisit(laSynthNodeOutput* n, laNodeVisitInfo* vi){
if(LA_SRC_AND_PARENT(n->In)){ laBaseNode*bn=n->In->Source->Parent; LA_VISIT_NODE(bn,vi); }
LA_ADD_THIS_NODE(n,vi);
return LA_DAG_FLAG_PERM;
}
int IDN_OutputEval(laSynthNodeOutput* n){
real* input; INPUTPACKET(input,n->In);
if(input) memcpy(MAIN.Audio->AudioEvalSynth->AudioSamples,input,sizeof(real)*LA_SYNTH_PLEN);
else memset(MAIN.Audio->AudioEvalSynth->AudioSamples,0,sizeof(real)*LA_SYNTH_PLEN);
return 1;
}
void IDN_OutputCopy(laSynthNodeOutput* new, laSynthNodeOutput* old, int DoRematch){
if(DoRematch){ LA_IDN_NEW_LINK(In) return; }
}
void laui_OutputNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
laColumn* c=laFirstColumn(uil); laSynthNodeOutput*n=This->EndInstance;
LA_BASE_NODE_HEADER(uil,c,This);
laColumn* cl,*cr; laSplitColumn(uil,c,0.3); cl=laLeftColumn(c,0); cr=laRightColumn(c,0);
laUiItem* b=laBeginRow(uil,c,0,0);
laShowNodeSocket(uil,c,This,"in",0)->Flags|=LA_UI_SOCKET_LABEL_E;
laShowLabel(uil,c,"🕪",0,0);
laEndRow(uil,b);
}
void laRebuildSynthGraphs(){
if(MAIN.Audio->CurrentSynth){
pthread_spin_lock(&MAIN.Audio->CurrentSynth->Lock);
laRebuildPageEval(MAIN.Audio->CurrentSynth->Page);
pthread_spin_unlock(&MAIN.Audio->CurrentSynth->Lock);
}
}
int laEvalSynthGraphs(){
laSynth* ss; int any=0;
memset(MAIN.Audio->AudioSamples,0,sizeof(real)*LA_SYNTH_PLEN);
for(ss=MAIN.Audio->Synths.pFirst;ss;ss=ss->Item.pNext){
pthread_spin_lock(&ss->Lock);
if(!ss->Playing){ pthread_spin_unlock(&ss->Lock); continue; }
MAIN.Audio->AudioEvalSynth=ss; MAIN.Audio->AudioEvalTime=0; INITPACKET(ss->AudioSamples);
laRunPage(ss->Page,1);
pthread_spin_unlock(&ss->Lock);
for(int i=0;iAudioSamples[i]+=ss->AudioSamples[i];
}
any=1;
}
return any;
}
void laaudio_DataCallback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount){
float* out=pOutput; int any=1;
for(int i=0;iNextAudioSample>=LA_SYNTH_PLEN){
MAIN.Audio->NextAudioSample=0;
if(laEvalSynthGraphs()){
MAIN.Audio->AudioEvalTotalSamples+=LA_SYNTH_PLEN;
MAIN.Audio->AudioEvalTime=(real)MAIN.Audio->AudioEvalTotalSamples/MAIN.Audio->AudioSampleRate;
}else{ any=0;}
}
out[i]=MAIN.Audio->AudioSamples[MAIN.Audio->NextAudioSample]/10;
MAIN.Audio->NextAudioSample++;
}
pthread_spin_lock(&MAIN.Audio->AudioStatusLock);
MAIN.Audio->AudioAny=any;
pthread_spin_unlock(&MAIN.Audio->AudioStatusLock);
}
void laInitMiniAudio(){
int SampleRate=48000;
ma_context context;
if (ma_context_init(NULL, 0, NULL, &context) != MA_SUCCESS) {
printf("Can't init miniaudio context\n");
}
ma_device_info* pPlaybackInfos;
ma_uint32 playbackCount;
ma_device_info* pCaptureInfos;
ma_uint32 captureCount;
if (ma_context_get_devices(&context, &pPlaybackInfos, &playbackCount, &pCaptureInfos, &captureCount) != MA_SUCCESS) {
printf("Can't get audio devices\n");
}
for (ma_uint32 iDevice = 0; iDevice < playbackCount; iDevice += 1) {
printf("%d - %s\n", iDevice, pPlaybackInfos[iDevice].name);
}
ma_device_config config = ma_device_config_init(ma_device_type_playback);
config.playback.pDeviceID= 0;
config.playback.format = ma_format_f32;
config.playback.channels = 1;
config.sampleRate = SampleRate;
config.dataCallback = laaudio_DataCallback;
config.pUserData = 0;
if (ma_device_init(NULL, &config, &MAIN.Audio->AudioDevice) != MA_SUCCESS){ return; }
MAIN.Audio->AudioSampleRate=SampleRate;
MAIN.Audio->NextAudioSample=LA_SYNTH_PLEN;
MAIN.Audio->AudioFrameInterval=1.0f/SampleRate;
INITPACKET(MAIN.Audio->AudioSamples);
pthread_spin_init(&MAIN.Audio->AudioStatusLock,0);
}
void laDeinitAudio(){
pthread_spin_destroy(&MAIN.Audio->AudioStatusLock);
ma_device_stop(&MAIN.Audio->AudioDevice);
ma_device_uninit(&MAIN.Audio->AudioDevice);
}
// Operations
laSynth* laNewSynth(char* Name){
laSynth* ss=memAcquireHyper(sizeof(laSynth));
ss->Page=memAcquire(sizeof(laRackPage));
ss->Page->RackType=LA_RACK_TYPE_AUDIO;
strSafeSet(&ss->Name,Name);
lstAppendItem(&MAIN.Audio->Synths,ss);
memAssignRef(MAIN.Audio,&MAIN.Audio->CurrentSynth,ss);
pthread_spin_init(&ss->Lock,0);
return ss;
}
void laRemoveSynth(laSynth* ss){ if(!ss) return;
strSafeDestroy(&ss->Name);
if(MAIN.Audio->CurrentSynth==ss){
laSynth* nss=ss->Item.pNext?ss->Item.pNext:ss->Item.pPrev;
memAssignRef(MAIN.Audio,&MAIN.Audio->CurrentSynth,nss);
}laNotifyInstanceUsers(ss);
while(ss->Page->Racks.pFirst){ laDestroyRack(ss->Page->Racks.pFirst); } memLeave(ss->Page); ss->Page=0;
lstRemoveItem(&MAIN.Audio->Synths,ss);
pthread_spin_destroy(&ss->Lock);
memLeave(ss);
}
int OPINV_ShedNewSynth(laOperator* a, laEvent* e){
laNewSynth("New Synth");
laNotifyInstanceUsers(MAIN.Audio); laRecordAndPush(0,"la.audio","New synth", 0);
return LA_FINISHED;
}
int OPCHK_IslaSynth(laPropPack *This, laStringSplitor *ss){
if (This && This->EndInstance && la_EnsureSubTarget(This->LastPs->p,0) == LA_PC_SYNTH) return 1;
return 0;
}
int OPINV_ShedRemoveSynth(laOperator* a, laEvent* e){
laSynth* ss=a->This?a->This->EndInstance:0; if(!ss)return LA_CANCELED;
if(strSame(strGetArgumentString(a->ExtraInstructionsP,"confirm"),"true")){
laRemoveSynth(ss);
laNotifyInstanceUsers(MAIN.Audio); laRecordAndPush(0,"la.audio","Remove la project", 0);
return LA_FINISHED;
}
laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e);
return LA_RUNNING;
}
void laui_RemoveSynth(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
laColumn* c=laFirstColumn(uil);
laShowItemFull(uil,c,This,"remove_synth",0,"confirm=true;text=Confirm",0,0);
}
int OPINV_laSynthPlay(laOperator* a, laEvent* e){
laSynth* ss=a->This?a->This->EndInstance:0; if(!ss)return LA_CANCELED;
char* str=strGetArgumentString(a->ExtraInstructionsP, "mode");
int play=0; if(str && strSame(str,"PLAY")) play=1;
if(play){ MAIN.Audio->AudioAny=1; }
pthread_spin_lock(&ss->Lock); ss->Playing=play; pthread_spin_unlock(&ss->Lock);
ma_device_start(&MAIN.Audio->AudioDevice); laNotifyInstanceUsers(ss);
return LA_FINISHED;
}
void lauidetached_Synthersizers(laPanel* p){
la_MakeDetachedProp(p, "la.detached_view_switch", "detached");
la_MakeDetachedProp(p, "la.audio.current_synth", "page");
}
void laui_Synthersizers(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
laColumn* c=laFirstColumn(uil),*cl, *cr,*crl,*crr;
laSplitColumn(uil,c,0.4); cl=laLeftColumn(c,1); cr=laRightColumn(c,0);
laSplitColumn(uil,cr,0.6); crl=laLeftColumn(cr,0); crr=laRightColumn(cr,2);
laShowItemFull(uil,cl,Extra,"detached",0,0,0,0)->Flags|=LA_UI_FLAGS_HIGHLIGHT|LA_UI_FLAGS_ICON;
#define ADD_PAGE \
laUiItem* b=laBeginRow(uil,crl,0,0);\
laShowItemFull(uil,crl,&rb->PP,"",LA_WIDGET_COLLECTION_SELECTOR,0,laui_IdentifierOnly,0)\
->Flags|=LA_UI_COLLECTION_SIMPLE_SELECTOR;\
laUiItem* b3=laOnConditionThat(uil,cr,laPropExpression(&rb->PP,""));{\
laShowItem(uil,crl,&rb->PP,"name");\
laShowItem(uil,crl,0,"LA_new_synth")->Flags|=LA_UI_FLAGS_ICON;\
laShowSeparator(uil,crl);\
laShowItem(uil,crl,&rb->PP,"remove_synth")->Flags|=LA_UI_FLAGS_ICON;\
laShowSeparator(uil,crl)->Expand=1;\
}laElse(uil,b3);{\
laShowItem(uil,crl,0,"LA_new_synth");\
}laEndCondition(uil,b3);\
laEndRow(uil,b);\
b3=laOnConditionThat(uil,cr,laPropExpression(&rb->PP,""));{\
laUiItem* b4=laOnConditionThat(uil,crr,laPropExpression(&rb->PP,"playing"));{\
laShowItemFull(uil,crr,&rb->PP,"play",0,"text=❚❚;",0,0) \
->Flags|=LA_UI_FLAGS_EXIT_WHEN_TRIGGERED|LA_TEXT_ALIGN_CENTER; \
}laElse(uil,b4);{\
laShowItemFull(uil,crr,&rb->PP,"play",0,"icon=▶;mode=PLAY;",0,0) \
->Flags|=LA_UI_FLAGS_ICON|LA_UI_FLAGS_EXIT_WHEN_TRIGGERED; \
}laEndCondition(uil,b4);\
laShowItemFull(uil,c,&rb->PP,"page",LA_WIDGET_COLLECTION_SINGLE,0,laui_RackPage,0)->Flags|=LA_UI_FLAGS_NO_DECAL;\
}laEndCondition(uil,b3);\
laUiItem* b1=laOnConditionThat(uil,cr,laPropExpression(Extra,"detached"));{
laUiItem* rb=laShowInvisibleItem(uil,cr,Extra,"page");
ADD_PAGE
}laElse(uil,b1);{
laUiItem* rb=laShowInvisibleItem(uil,cr,0,"la.audio.current_synth");
ADD_PAGE
}laEndCondition(uil,b1);
#undef ADD_PAGE
}
void* laget_FirstSynth(void* unused1,void* unused2){
return MAIN.Audio->Synths.pFirst;
}
void lapost_Synth(laSynth* ss){
pthread_spin_init(&ss->Lock,0);
}
int laget_SynthPlaying(laSynth* ss){
int play=0;
pthread_spin_lock(&ss->Lock); play=ss->Playing; pthread_spin_unlock(&ss->Lock);
return play;
}
void laset_CurrentSynth(laAudio* a,laSynth* s){
memAssignRef(a,&a->CurrentSynth,s);
}
void la_AudioPreFrame(){
if(MAIN.GraphNeedsRebuild){ laRebuildSynthGraphs(); }
int anyaudio=0;
pthread_spin_lock(&MAIN.Audio->AudioStatusLock);
anyaudio=MAIN.Audio->AudioAny;
pthread_spin_unlock(&MAIN.Audio->AudioStatusLock);
if(!anyaudio){ ma_device_stop(&MAIN.Audio->AudioDevice); }
}
void laInitAudio(){
laPropContainer* pc; laProp* p; laSubProp* sp;
laInitMiniAudio();
laRegisterUiTemplate("LA_synthesizers","Synthesizers",laui_Synthersizers,lauidetached_Synthersizers,0,"Audio",0,0,0);
laCreateOperatorType("LA_new_synth", "New Synthesizer", "Add a synthesizer",0,0,0,OPINV_ShedNewSynth,0,'+',0);
laCreateOperatorType("LA_remove_synth", "Remove Synthesizer", "Remove a synthesizer",OPCHK_IslaSynth,0,0,OPINV_ShedRemoveSynth,OPMOD_FinishOnData,L'🗴',0)
->UiDefine=laui_RemoveSynth;
laCreateOperatorType("LA_synth_play", "Play Synthesizer", "Play a synthesizer",OPCHK_IslaSynth,0,0,OPINV_laSynthPlay,0,0,0);
pc = laAddPropertyContainer("la_synth", "LaGUI Synthesizer", "LaGUI synthesizer", 0,0,sizeof(laSynth),lapost_Synth,0,2);{
LA_PC_SYNTH=pc;
laAddStringProperty(pc,"name","Name","Name of the synthesizer",0,0,0,0,1,offsetof(laSynth,Name),0,0,0,0,LA_AS_IDENTIFIER);
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);
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);
laAddEnumItemAs(p,"IDLE","Idle","Synth is not playing",0,0);
laAddEnumItemAs(p,"PLAYING","Playing","Synth is playing",1,0);
laAddOperatorProperty(pc,"remove_synth","Remove Synth", "Remove synth", "LA_remove_synth", L'🗴', 0);
laAddOperatorProperty(pc,"play","Play Synth", "Play synth", "LA_synth_play", 0, 0);
}
pc=laAddPropertyContainer("la_node_synth_fm", "FM OSC Node", "Osilliator node with frequency modulation",0,laui_FMNode,sizeof(laSynthNodeFM),lapost_Node,0,1);
LA_PC_IDN_FM=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
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);
laAddSubGroup(pc,"out", "Out","Output","la_out_socket",0,0,0,offsetof(laSynthNodeFM,Out),0,0,0,0,0,0,0,LA_UDF_SINGLE);
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);
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);
laAddEnumItemAs(p,"NONE","None","Regular frequency range",0,0);
laAddEnumItemAs(p,"SLOW","Slow","Slow frequency range",1,0);
pc=laAddPropertyContainer("la_node_synth_vca", "VCA Node", "Voltage controlled amplifier",0,laui_VCANode,sizeof(laSynthNodeVCA),lapost_Node,0,1);
LA_PC_IDN_VCA=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
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);
laAddSubGroup(pc,"in", "Input","Input","la_in_socket",0,0,0,offsetof(laSynthNodeVCA,In),0,0,0,0,0,0,0,LA_UDF_SINGLE);
laAddSubGroup(pc,"out", "Out","Output","la_out_socket",0,0,0,offsetof(laSynthNodeVCA,Out),0,0,0,0,0,0,0,LA_UDF_SINGLE);
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);
pc=laAddPropertyContainer("la_node_synth_noise", "Noise Node", "Noise node",0,laui_NoiseNode,sizeof(laSynthNodeFM),lapost_Node,0,1);
LA_PC_IDN_NOISE=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
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);
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);
pc=laAddPropertyContainer("la_node_synth_scope", "Scope Node", "Scope for synthesizer",0,laui_ScopeNode,sizeof(laSynthNodeScope),lapost_Node,0,1);
LA_PC_IDN_SCOPE=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
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);
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);
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);
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);
#define _STR(a) #a
#define ADD_TIME_FACTOR(a) laAddEnumItemAs(p,"FAC"_STR(a),_STR(a),"^"_STR(a)" time",a,0);
ADD_TIME_FACTOR(1) ADD_TIME_FACTOR(2) ADD_TIME_FACTOR(3) ADD_TIME_FACTOR(4) ADD_TIME_FACTOR(5)
ADD_TIME_FACTOR(6) ADD_TIME_FACTOR(7) ADD_TIME_FACTOR(8) ADD_TIME_FACTOR(9) ADD_TIME_FACTOR(10)
#undef ADD_TIME_FACTOR
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);
#define ADD_GAIN(a) laAddEnumItemAs(p,"GAIN"_STR(a),_STR(a),"^"_STR(a)" gain",a,0);
#define ADD_GAIN_S(a,s) laAddEnumItemAs(p,"GAIN"_STR(a),s,"^"s" gain",a,0);
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)
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);
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)
#undef ADD_GAIN
#undef ADD_GAIN_S
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);
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);
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);
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);
pc=laAddPropertyContainer("la_node_synth_output", "Output Node", "Sound output to system",0,laui_OutputNode,sizeof(laSynthNodeOutput),lapost_Node,0,1);
LA_PC_IDN_OUTPUT=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
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);
LA_IDN_REGISTER("VCO",'f',LA_IDN_FM, LA_PC_IDN_FM, IDN_FM, laSynthNodeFM);
LA_IDN_REGISTER("VCA",'a',LA_IDN_VCA, LA_PC_IDN_VCA, IDN_VCA, laSynthNodeVCA);
LA_IDN_REGISTER("Noise",'~',LA_IDN_NOISE, LA_PC_IDN_NOISE, IDN_Noise, laSynthNodeNoise);
LA_IDN_REGISTER("Scope",'s',LA_IDN_SCOPE, LA_PC_IDN_SCOPE, IDN_Scope, laSynthNodeScope);
LA_IDN_REGISTER("Sound Output",U'🕪',LA_IDN_OUTPUT, LA_PC_IDN_OUTPUT, IDN_Output, laSynthNodeOutput);
LA_NODE_CATEGORY_SYNTHESIZER=laAddNodeCategory("OSC",0,LA_RACK_TYPE_AUDIO);
LA_NODE_CATEGORY_SYSTEM_SOUND=laAddNodeCategory("System",0,LA_RACK_TYPE_AUDIO);
laNodeCategoryAddNodeTypes(LA_NODE_CATEGORY_SYNTHESIZER, &LA_IDN_FM,&LA_IDN_VCA,&LA_IDN_NOISE,&LA_IDN_SCOPE,0);
laNodeCategoryAddNodeTypes(LA_NODE_CATEGORY_SYSTEM_SOUND, &LA_IDN_OUTPUT,0);
}
void laAudioDummy(){
return 0;
}