Old 01-17-2014, 02:30 AM   #1
Obe
Human being with feelings
 
Obe's Avatar
 
Join Date: Feb 2013
Location: France
Posts: 18
Default Feature request for JS readability

Jesusonic langage is dedicated to real-audio time.

I think there should be more function that go in this direction.

For instance, it should be easier to access the previous input and output sample for coding FIR and IIR sample or for accessing previous sample to perform FFT.


The size of the data to cache could be setted in the init part
Code:
@init

NbOfSampleIwantToCache = 16;
and then accessed directly in the sample part :

Code:
@sample

// For a 1 pole IIR Filter (NbOfSampleIwantToCache  could be set to 1)
spl0 = a0*spl0 + a1*spl0(-1) + b1*spl0_out(-1);

(NB : The syntax for the previous output sample (spl1_out) could be better...)


I don't know if it would increase the script efficiency. At minima, it could be seen as a syntaxic sugar that will improve the readability of the code.

-----

Is it a good idea ?
Obe is offline   Reply With Quote
Old 01-17-2014, 09:23 AM   #2
Justin
Administrator
 
Justin's Avatar
 
Join Date: Jan 2005
Location: NYC
Posts: 15,721
Default

How about this:
Code:
@init


function history.init(buf, sz)
(
  this.buf = buf;
  this.size= sz;
  this.pos = 0;
  memset(this.buf,0,this.size);
);
function history.add(val)
(
  this.buf[this.pos]=val;
  this.pos >= this.size-1 ? this.pos = 0 : this.pos += 1;
);

function history.get(offset) local(p)
(
  p=this.pos - min(offset+1,this.size);
  p < 0 ? p += this.size;
  this.buf[p];
);

hsz=16;
spl0.history.init(hsz*0,hsz);
spl1.history.init(hsz*1,hsz);
spl0_out.history.init(hsz*2,hsz);
spl1_out.history.init(hsz*3,hsz);

@sample
spl0.history.add(spl0);
spl1.history.add(spl1);

spl0 = a0*spl0.history.get(0) +
       a1*spl0.history.get(1) + 
       b1*spl0_out.history.get(0);

spl1 = a0*spl1.history.get(0) +
       a1*spl1.history.get(1) + 
       b1*spl1_out.history.get(0);

spl0_out.history.add(spl0);
spl1_out.history.add(spl1);
To go with these functions, I also like:
Code:
// window.init(buffer, size, 0=rect, 1=hamming, 2=blackman-harris, 3=blackman

function window.init(buf, sz, mode) local(i,isz,windowpos, dwindowpos)
(
  this.window != buf || this.window_sz != sz || this.window_mode != mode ?
  (
    this.window = buf;
    this.window_sz = sz;
    this.window_mode = mode;
    i=0;
    dwindowpos = $pi*2/(sz-1);
    windowpos=0;
    isz = 0.5/sz;

    loop(sz,
       buf[] = (
         mode==1 ? 0.53836 - cos(windowpos)*0.46164 :
         mode==2 ? 0.35875 - 0.48829 * cos(windowpos) + 0.14128 * cos(2*windowpos) - 0.01168 * cos(6*windowpos) :
         mode==3 ? 0.42 - 0.50 * cos(windowpos) + 0.08 * cos(2.0*windowpos) :
          1.0)*isz;
       windowpos+=dwindowpos;
       buf += 1;
    );
  );
);

function history.get_block(buf, sz, spacing) local(i,p)
(
  i=0;
  p=this.pos - min(sz,this.size);
  p < 0 ? p += this.size;
  loop(min(sz,this.size),
    buf[]=this.buf[p];
    (p+=1) >= this.size ? p=0;
    i+=1;
    buf+=spacing;
  );
  while (i < sz)
  (
    buf[] = 0;
    buf+=spacing;
    i+=1;
  );
);

function history.get_windowed(buf, sz, spacing, window*) local(i,p)
(
  i=0;
  p=this.pos - min(sz,this.size);
  p < 0 ? p += this.size;
  loop(min(sz,this.size),
    buf[]=this.buf[p] * window.window[i];
    (p+=1) >= this.size ? p=0;
    i+=1;
    buf+=spacing;
  );
  while (i < sz)
  (
    buf[] = 0;
    buf+=spacing;
    i+=1;
  );
);
...and to use it (for example in @gfx to do analysis -- you'd want a larger history buffer of at least fftsize):
Code:
mywindow.window.init(window_buf,fftsize,type);

t=fftworkspace;
spl0.history.get_windowed(fftworkspace,fftsize,2,mywindow);
loop(fftsize, t[1] = 0; t+=2; );
fft(fftworkspace,fftsize);
fft_permute(fftworkspace,fftsize);
...
Justin is offline   Reply With Quote
Old 01-17-2014, 10:27 AM   #3
Obe
Human being with feelings
 
Obe's Avatar
 
Join Date: Feb 2013
Location: France
Posts: 18
Default

That is an elegant answer. Thanks.

I am going to use it right away !
Obe is offline   Reply With Quote
Old 01-17-2014, 01:34 PM   #4
Obe
Human being with feelings
 
Obe's Avatar
 
Join Date: Feb 2013
Location: France
Posts: 18
Default

Once again, very nice solution, I like the idea to "fonctionalize" the spl.

By the way, it seems there is a typo, it is buf[i] and not buf[] in the function history.get_block and history.get_windowed.
Obe is offline   Reply With Quote
Old 01-17-2014, 03:38 PM   #5
Justin
Administrator
 
Justin's Avatar
 
Join Date: Jan 2005
Location: NYC
Posts: 15,721
Default

Quote:
Originally Posted by Obe View Post
Once again, very nice solution, I like the idea to "fonctionalize" the spl.

By the way, it seems there is a typo, it is buf[i] and not buf[] in the function history.get_block and history.get_windowed.
buf[] is correct (this is the same as buf[0]) -- buf is advanced by the spacing amount in addition to i being incremented.
Justin 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 07:11 AM.


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