*/}}
Browse Source

Envelope and some slight changes to scope for positive direction

YimingWu 9 months ago
parent
commit
f85b94db08
3 changed files with 132 additions and 8 deletions
  1. 117 4
      la_audio.c
  2. 11 0
      la_interface.h
  3. 4 4
      resources/la_widgets.c

+ 117 - 4
la_audio.c

@@ -33,12 +33,14 @@ laBaseNodeType LA_IDN_VCA;
 laBaseNodeType LA_IDN_NOISE;
 laBaseNodeType LA_IDN_OUTPUT;
 laBaseNodeType LA_IDN_SCOPE;
+laBaseNodeType LA_IDN_ENVELOPE;
 
 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;
+laPropContainer* LA_PC_IDN_ENVELOPE;
 
 #define _2PI 6.283185307
 
@@ -55,6 +57,9 @@ laPropContainer* LA_PC_IDN_SCOPE;
     {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; } }
 
+#define FRAME_INTERVAL (MAIN.Audio->AudioFrameInterval)
+
+#define SAMPLE_RATE (MAIN.Audio->AudioSampleRate)
 
 void IDN_FMInit(laSynthNodeFM* n, int NoCreate){
     INITPACKET(n->OutSamples);
@@ -76,7 +81,7 @@ int IDN_FMEval(laSynthNodeFM* n){
     INPUTPACKET(inputf,n->InFrequency); LA_GET_SRC_AS_VALUE(constf,n->InFrequency); 
     for(int i=0;i<LA_SYNTH_PLEN;i++){
         real useF=inputf?(inputf[i]+f):(f+constf); if(n->Slow) useF-=10;
-        n->Phase+=MAIN.Audio->AudioFrameInterval*VAL2FREQ(useF)*_2PI;
+        n->Phase+=FRAME_INTERVAL*VAL2FREQ(useF)*_2PI;
         WRAPPHASE(n->Phase);
         n->OutSamples[i]=sin(n->Phase)*10;
     }
@@ -85,7 +90,7 @@ int IDN_FMEval(laSynthNodeFM* n){
 }
 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;
+    LA_IDN_OLD_DUPL(Out); new->Frequency=old->Frequency; new->Slow=old->Slow;
 }
 void laui_FMNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
     laColumn* c=laFirstColumn(uil); laSynthNodeFM*n=This->EndInstance;
@@ -232,7 +237,8 @@ int IDN_ScopeEval(laSynthNodeScope* n){
             if(ch2){ smin2=TNS_MIN2(smin2,ch2[sp]); smax2=TNS_MAX2(smax2,ch2[sp]); }
             sp++;
         }
-        if(!ch1){smin1=smax1=0;} if(!ch2){smin2=smax2=0;}
+        if(!ch1){smin1=smax1=0; LA_GET_SRC_AS_VALUE(smin1,n->In1); smax1=smin1; }
+        if(!ch2){smin2=smax2=0; LA_GET_SRC_AS_VALUE(smin2,n->In2); smax2=smin2; }
         int ns=n->NextSample;
         n->Display1[ns]=smin1; n->Display1[ns+LA_SYNTH_PLEN]=smax1;
         n->Display2[ns]=smin2; n->Display2[ns+LA_SYNTH_PLEN]=smax2;
@@ -306,6 +312,95 @@ void laui_OutputNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColum
     laEndRow(uil,b);
 }
 
+void IDN_EnvelopeInit(laSynthNodeEnvelope* n, int NoCreate){
+    if(!NoCreate){ strSafeSet(&n->Base.Name,"Envelope");
+        n->Attack=laCreateInSocket("ATTACK",0); n->Delay=laCreateInSocket("DELAY",0);
+        n->Sustain=laCreateInSocket("SUSTAIN",0); n->Release=laCreateInSocket("RELEASE",0);
+        n->Trigger=laCreateInSocket("TRIGGER",0); n->Out=laCreateOutSocket(n, "OUT",0);
+    }
+    if(!n->OutSamples){ n->OutSamples=memAcquireSimple(sizeof(real)*LA_SYNTH_PLEN); }
+    n->Out->Data = n->OutSamples; n->Out->DataType = LA_PROP_FLOAT|LA_PROP_ARRAY; n->Out->ArrLen=LA_SYNTH_PLEN;
+    n->Time=20; n->rAttack=1; n->rDelay=5; n->rSustain=5; n->rRelease=7;
+}
+void IDN_EnvelopeDestroy(laSynthNodeEnvelope* n){
+    laDestroyInSocket(n->Attack); laDestroyInSocket(n->Delay);
+    laDestroyInSocket(n->Sustain); laDestroyInSocket(n->Release);
+    laDestroyInSocket(n->Trigger); laDestroyOutSocket(n->Out);
+    memFree(n->OutSamples);
+    strSafeDestroy(&n->Base.Name);
+}
+int IDN_EnvelopeVisit(laSynthNodeEnvelope* n, laNodeVisitInfo* vi){
+    LA_GUARD_THIS_NODE(n,vi);
+    if(LA_SRC_AND_PARENT(n->Attack)){ laBaseNode*bn=n->Attack->Source->Parent; LA_VISIT_NODE(bn,vi); }
+    if(LA_SRC_AND_PARENT(n->Delay)){ laBaseNode*bn=n->Delay->Source->Parent; LA_VISIT_NODE(bn,vi); }
+    if(LA_SRC_AND_PARENT(n->Sustain)){ laBaseNode*bn=n->Sustain->Source->Parent; LA_VISIT_NODE(bn,vi); }
+    if(LA_SRC_AND_PARENT(n->Release)){ laBaseNode*bn=n->Release->Source->Parent; LA_VISIT_NODE(bn,vi); }
+    if(LA_SRC_AND_PARENT(n->Trigger)){ laBaseNode*bn=n->Trigger->Source->Parent; LA_VISIT_NODE(bn,vi); }
+    LA_ADD_THIS_NODE(n,vi);
+    return LA_DAG_FLAG_PERM;
+}
+int IDN_EnvelopeEval(laSynthNodeEnvelope* n){
+    int trigger_at=-1; real trig=n->bTrigger*100;
+    real* trigger; INPUTPACKET(trigger,n->Trigger); if(!trigger){ LA_GET_SRC_AS_VALUE(trig,n->Trigger); }
+    real attack=n->rAttack,delay=n->rDelay,sustain=n->rSustain,release=n->rRelease;
+    LA_GET_SRC_AS_VALUE(attack,n->Attack); LA_GET_SRC_AS_VALUE(delay,n->Delay);
+    LA_GET_SRC_AS_VALUE(sustain,n->Sustain); LA_GET_SRC_AS_VALUE(release,n->Release);
+    attack=attack?pow(attack/10,2):0; delay=delay?pow(delay/10,2):0; release=release?pow(release/10,2):0;
+    for(int i=0;i<LA_SYNTH_PLEN;i++){
+        if(trigger){ trig=trigger[i]; }
+        if(!n->Triggered){ if(trig > 5.0f){ n->Triggered = 1; n->Time = 0; n->ReleaseTime=10000; n->AtLevel=0; } }
+        else{ if(trig < 2.0f){ n->Triggered = 0; n->ReleaseTime = n->Time; } }
+        if(n->Time < n->ReleaseTime){
+            if(n->Time<=attack){
+                n->OutSamples[i]=10.0f*pow(n->Time/attack,0.3);
+            }elif(n->Time<=delay + attack){
+                real fac=pow((n->Time-attack)/delay,0.3);
+                n->OutSamples[i]= tnsLinearItp(10.0f, sustain, fac);
+            }else{
+                n->OutSamples[i]=sustain;
+            }
+            n->AtLevel=n->OutSamples[i];
+        }else{
+            if(n->Time - n->ReleaseTime < release){
+                real fac=pow((n->Time - n->ReleaseTime)/release,0.3);
+                n->OutSamples[i]= tnsLinearItp(n->AtLevel,0.0f, fac);
+            }else{
+                n->OutSamples[i]=0;
+            }
+        }
+        n->Time+=FRAME_INTERVAL;
+    }
+    return 1;
+}
+void IDN_EnvelopeCopy(laSynthNodeEnvelope* new, laSynthNodeEnvelope* old, int DoRematch){
+    if(DoRematch){
+        LA_IDN_NEW_LINK(Attack) LA_IDN_NEW_LINK(Delay)
+        LA_IDN_NEW_LINK(Sustain) LA_IDN_NEW_LINK(Release) LA_IDN_NEW_LINK(Trigger) return;
+    }
+    LA_IDN_OLD_DUPL(Out);
+    new->rAttack=old->rAttack;new->rSustain=old->rSustain;new->rDelay=old->rDelay;new->rRelease=old->rRelease;
+}
+void laui_EnvelopeNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
+    laColumn* c=laFirstColumn(uil); laSynthNodeEnvelope*n=This->EndInstance;
+    LA_BASE_NODE_HEADER(uil,c,This);
+
+    laUiItem* b,*b1;
+#define ADSR_ROW(what) \
+    b=laBeginRow(uil,c,0,0); \
+    laShowNodeSocket(uil,c,This,"in_" what,0)->Flags|=LA_UI_SOCKET_LABEL_E; \
+    b1=laOnConditionThat(uil,c,laNot(laPropExpression(This,"in_" what ".source")));{ \
+    laShowItem(uil,c,This,what)->Expand=1; \
+    } laEndCondition(uil,b1); \
+    laEndRow(uil,b);
+
+    ADSR_ROW("attack") ADSR_ROW("delay") ADSR_ROW("sustain") ADSR_ROW("release")
+    
+    b=laBeginRow(uil,c,0,0);
+    laShowItem(uil,c,This,"in_trigger"); laShowItem(uil,c,This,"trigger")->Flags|=LA_UI_FLAGS_CYCLE;
+    laShowSeparator(uil,c); laShowItem(uil,c,This,"out");
+    laEndRow(uil,b);
+}
+
 void laRebuildSynthGraphs(){
     if(MAIN.Audio->CurrentSynth){
         laSpinLock(&MAIN.Audio->CurrentSynth->Lock);
@@ -636,16 +731,34 @@ void laInitAudio(){
     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);
 
+    pc=laAddPropertyContainer("la_node_synth_envelope", "Envelope Node", "Sound envelope",0,laui_EnvelopeNode,sizeof(laSynthNodeEnvelope),lapost_Node,0,1);
+    LA_PC_IDN_ENVELOPE=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_attack", "Attack","Attack input","la_in_socket",0,0,0,offsetof(laSynthNodeEnvelope,Attack),0,0,0,0,0,0,0,LA_UDF_SINGLE);
+    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);
+    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);
+    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);
+    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);
+    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);
+    laAddFloatProperty(pc,"attack","Attack","Attack value",0,0,0,10,0,0.1,0,0,offsetof(laSynthNodeEnvelope,rAttack),0,0,0,0,0,0,0,0,0,0,0);
+    laAddFloatProperty(pc,"delay","Delay","Delay value",0,0,0,10,0,0.1,0,0,offsetof(laSynthNodeEnvelope,rDelay),0,0,0,0,0,0,0,0,0,0,0);
+    laAddFloatProperty(pc,"sustain","Sustain","Sustain value",0,0,0,10,0,0.1,0,0,offsetof(laSynthNodeEnvelope,rSustain),0,0,0,0,0,0,0,0,0,0,0);
+    laAddFloatProperty(pc,"release","Release","Release value",0,0,0,10,0,0.1,0,0,offsetof(laSynthNodeEnvelope,rRelease),0,0,0,0,0,0,0,0,0,0,0);
+    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);
+    laAddEnumItemAs(p,"IDLE","Idle","Trigger idle",0,0);
+    laAddEnumItemAs(p,"TRIG","Trigger","Trigger active",1,0);
+
     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_IDN_REGISTER("Envelope",U'^',LA_IDN_ENVELOPE, LA_PC_IDN_ENVELOPE, IDN_Envelope, laSynthNodeEnvelope);
 
     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_SYNTHESIZER, &LA_IDN_FM,&LA_IDN_NOISE,&LA_IDN_VCA,&LA_IDN_ENVELOPE,&LA_IDN_SCOPE,0);
     laNodeCategoryAddNodeTypes(LA_NODE_CATEGORY_SYSTEM_SOUND, &LA_IDN_OUTPUT,0);
 }
 

+ 11 - 0
la_interface.h

@@ -1772,6 +1772,17 @@ STRUCTURE(laSynthNodeOutput){
     laNodeInSocket* In;
 };
 
+STRUCTURE(laSynthNodeEnvelope){
+    laBaseNode Base;
+    laNodeInSocket* Trigger; int bTrigger; int Triggered;
+    laNodeInSocket* Attack; real rAttack;
+    laNodeInSocket* Delay; real rDelay;
+    laNodeInSocket* Sustain; real rSustain;
+    laNodeInSocket* Release; real rRelease;
+    laNodeOutSocket* Out; real* OutSamples;
+    real Time,ReleaseTime,AtLevel;
+};
+
 #include "miniaudio.h"
 
 STRUCTURE(laAudioDevice){

+ 4 - 4
resources/la_widgets.c

@@ -1590,8 +1590,8 @@ void la_ScopeDraw(laUiItem *ui, int h){
 
     for(int i=0;i<plen;i++){
         arrx[i]=tnsInterpolate(cl,cr,((real)i)/plen);
-        arru1[i]=tnsInterpolate(cc,cb,yfac1*s->Display1[i])-off1;
-        arrb1[i]=tnsInterpolate(cc,cb,yfac1*s->Display1[i+plen])-off1;
+        arru1[i]=tnsInterpolate(cc,cu,yfac1*s->Display1[i])-off1;
+        arrb1[i]=tnsInterpolate(cc,cu,yfac1*s->Display1[i+plen])-off1;
         TNS_CLAMP(arru1[i],cu,cb); TNS_CLAMP(arrb1[i],cu,cb);
         tnsVertex2d(arrx[i],arru1[i]); tnsVertex2d(arrx[i],arrb1[i]);
     }
@@ -1599,8 +1599,8 @@ void la_ScopeDraw(laUiItem *ui, int h){
     tnsPackAs(GL_TRIANGLE_STRIP); tnsFlush();
 
     for(int i=0;i<plen;i++){
-        arru2[i]=tnsInterpolate(cc,cb,yfac2*s->Display2[i])-off2;
-        arrb2[i]=tnsInterpolate(cc,cb,yfac2*s->Display2[i+plen])-off2;
+        arru2[i]=tnsInterpolate(cc,cu,yfac2*s->Display2[i])-off2;
+        arrb2[i]=tnsInterpolate(cc,cu,yfac2*s->Display2[i+plen])-off2;
         TNS_CLAMP(arru2[i],cu,cb); TNS_CLAMP(arrb2[i],cu,cb);
         tnsVertex2d(arrx[i],arru2[i]); tnsVertex2d(arrx[i],arrb2[i]);
     }