Old 06-27-2017, 12:52 PM   #1
Aesis
Human being with feelings
 
Join Date: Jan 2011
Posts: 445
Default Simple difference equations in JSFX?

I am trying to create simple difference equations of the form:

Y[t] = a*Y[t-1] + b*Y[t-2] + ... + X[t]

in JSFX.

My problem is that it's not very clear how the buffer works. Even looking at the very simplest example:

Quote:
desc:Echo
slider1:120<0,1000,1>Length [ms]

@slider
echolength = slider1 / 1000 * srate ;

@sample
bufpos[0] = spl0 ;
bufpos = bufpos + 1 ;
bufpos > echolength ? bufpos = 0;
spl0 = spl0 + bufpos[0] ;
spl1 = spl0 ;
It seems like this echo is supposed to loop forever, but it only has one tap instead. Is there an explanation on what's going on with the code?
Aesis is offline   Reply With Quote
Old 06-27-2017, 04:16 PM   #2
junh1024
Human being with feelings
 
Join Date: Feb 2014
Posts: 240
Default

May help http://forum.cockos.com/showpost.php...98&postcount=8
junh1024 is offline   Reply With Quote
Old 06-27-2017, 05:36 PM   #3
SaulT
Human being with feelings
 
Join Date: Oct 2013
Location: Seattle, WA
Posts: 876
Default

btw if you want it to loop (repeat) then you need a feedback.

Code:
slider2:-6<-60,0,0.1>Feedback (dB)

...

@slider

feedback = 10^(slider2/20);

...

@sample

bufpos[0] = spl0 + bufpos[0] * feedback;
bufpos += 1;
bufpos >= echolength ? bufpos -= echolength;
spl0 += bufpos[0]; 
spl1 += bufpos[0];
I wrote a plugin for this, one that combined delay and saturation. It stores the delay at half srate to get maximum delay time. At 96k that's 80 seconds, much longer times are possible at lower samplerates.

http://forum.cockos.com/showthread.php?t=170874
SaulT is offline   Reply With Quote
Old 06-28-2017, 10:57 AM   #4
Aesis
Human being with feelings
 
Join Date: Jan 2011
Posts: 445
Default

Quote:
Originally Posted by SaulT View Post
btw if you want it to loop (repeat) then you need a feedback.

Code:
slider2:-6<-60,0,0.1>Feedback (dB)

...

@slider

feedback = 10^(slider2/20);

...

@sample

bufpos[0] = spl0 + bufpos[0] * feedback;
bufpos += 1;
bufpos >= echolength ? bufpos -= echolength;
spl0 += bufpos[0]; 
spl1 += bufpos[0];
I wrote a plugin for this, one that combined delay and saturation. It stores the delay at half srate to get maximum delay time. At 96k that's 80 seconds, much longer times are possible at lower samplerates.

http://forum.cockos.com/showthread.php?t=170874
Feedback is no problem, however I don't know how to make the next sample value to be combination of past output values as the equation says. Any ideas on that?
Aesis is offline   Reply With Quote
Old 06-28-2017, 09:10 PM   #5
Aesis
Human being with feelings
 
Join Date: Jan 2011
Posts: 445
Default

I finally understand it. The guide I have to say, is useless.

Here is a code for 2nd order:

Quote:
desc:Echo
slider1:0<0,100000,1>Delay1 [sample]
slider2:0<0,100000,1>Delay2 [sample]
slider3:0<0,100,1>Feedback1 [sample]
slider4:0<0,100,1>Feedback2 [sample]

@slider
length0 = slider1-1;
length1 = slider2-1;
feedback0 = slider3 / 100;
feedback1 = slider4 / 100;

@init
buffer1 = length0+2

@sample

buffer0[0] = in0;
buffer0+=1;
buffer0 > length0 ? buffer0 = 0;

delay = -feedback0*buffer0[0];

buffer1[0] = in0;
buffer1+=1;
buffer1 > length0+2+length1 ? buffer1 = length0+2;

delay1 = -feedback1*buffer1[0];

spl0 = spl0 + delay + delay1;

spl1 = spl0 ;

in0 = spl0;
I will attempt to rewrite it in a way that only uses one buffer location. But in the meantime, there is a serious error. For the second delay to kick in, it takes time from the start of the playback. Is it the case that JSFX can't write to memory values that are outside of the number of played samples? Makes no sense...
Aesis is offline   Reply With Quote
Old 06-28-2017, 09:34 PM   #6
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 14,770
Default

@slider can be executed after @init (e.g. whenever you move one), so you need to set "buffer1" in @slider as it depends on @length1, which is set in @slider (or just do "buffer1 = 100000;" in @init.

-Michael
mschnell is offline   Reply With Quote
Old 06-28-2017, 11:13 PM   #7
Aesis
Human being with feelings
 
Join Date: Jan 2011
Posts: 445
Default

Quote:
Originally Posted by mschnell View Post
@slider can be executed after @init (e.g. whenever you move one), so you need to set "buffer1" in @slider as it depends on @length1, which is set in @slider (or just do "buffer1 = 100000;" in @init.

-Michael
Good catch. Here is a new improved code that writes to buffer only once.

Quote:
desc:Eecho313
slider1:0<0,100000,1>Delay1 [sample]
slider2:0<0,100000,1>Delay2 [sample]
slider3:0<0,100000,1>Delay3 [sample]
slider4:0<0,100000,1>Delay4 [sample]
slider5:0<-100,100,1>Feedback1 [%]
slider6:0<-100,100,1>Feedback2 [%]
slider7:0<-100,100,1>Feedback3 [%]
slider8:0<-100,100,1>Feedback4 [%]

@slider
length0 = slider1;
length1 = slider2;
length2 = slider3;
length3 = slider4;

feedback0 = slider5 / 100;
feedback1 = slider6 / 100;
feedback2 = slider7 / 100;
feedback3 = slider8 / 100;

maximum = max(max(max(length0,length1),length2),length3);
buffer1 = -length0 - 1;
buffer2 = -length1 - 1;
buffer3 = -length2 - 1;
buffer4 = -length3 - 1;

@sample

buffer1+=1;
buffer1 > maximum ? buffer1 = 0;

buffer2 +=1;
buffer2 > maximum ? buffer2 = 0;

buffer3 +=1;
buffer3 > maximum ? buffer3 = 0;

buffer4 +=1;
buffer4 > maximum ? buffer4 = 0;

delay = feedback0*buffer1[0]+feedback1*buffer2[0]+feedback2*buffer3[0]+feedback3*buffer4[0];


spl0 = spl0 + delay;

spl1 = spl0;

in0 = spl0;


buffer0[0] = in0;
buffer0 +=1;
buffer0 > maximum ? buffer0 = 0;
Up to 4 taps now. Useful for making filters and cancelling delay taps. There is a bit of an issue with the controls, how would I stop the sliders refreshing delay time when I only touch the feedback?

Last edited by Aesis; 06-29-2017 at 03:05 AM.
Aesis is offline   Reply With Quote
Old 06-29-2017, 06:55 AM   #8
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 14,770
Default

You can set a variable according to the slider value and check if they still are the same and only do the appropriate actions if they are not.

-Michael
mschnell is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 09:30 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.