Skip to content

Commit

Permalink
Release JSAdditiv v1.3 (#359)
Browse files Browse the repository at this point in the history
replace zdf with rbj filters
fix note clicking 
fix volume knob clicking
  • Loading branch information
tiagolr committed Dec 28, 2023
1 parent 4bb2a26 commit d6b9eb1
Show file tree
Hide file tree
Showing 4 changed files with 476 additions and 479 deletions.
118 changes: 43 additions & 75 deletions Synth/tilr_JSAdditiv.jsfx
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
desc: JSAdditiv
author: tilr
version: 1.2
version: 1.3
changelog:
Fix support large wave files
Fix import wave samplerate
Support file drag and drop
replace zdf with rbj filters
fix note clicking
fix volume knob clicking
provides:
tilr_JSAdditiv/add.adsr.jsfx-inc
tilr_JSAdditiv/add.array.jsfx-inc
tilr_JSAdditiv/add.fft_real_synth.jsfx-inc
tilr_JSAdditiv/add.gfxlib.jsfx-inc
tilr_JSAdditiv/add.mouselib.jsfx-inc
tilr_JSAdditiv/add.wavetable.jsfx-inc
tilr_JSAdditiv/add.zdf_filter.jsfx-inc
[data] tilr_JSAdditiv/Complex 1.wav
[data] tilr_JSAdditiv/Complex 2.wav
[data] tilr_JSAdditiv/Complex 3.wav
Expand All @@ -38,6 +37,7 @@ provides:
[data] tilr_JSAdditiv/Square 2.wav
[data] tilr_JSAdditiv/Stairs.wav
[data] tilr_JSAdditiv/Triangle.wav
tilr_JSAdditiv/add.rbj_filter.jsfx-inc
screenshot: https://raw.githubusercontent.com/tiagolr/JSAdditiv/master/doc/ss.png
about:
# JSAdditiv
Expand Down Expand Up @@ -84,7 +84,7 @@ slider26:pitch_rel=500<1, 5000, 1:log>-Pitch Release
import add.wavetable.jsfx-inc
import add.array.jsfx-inc
import add.adsr.jsfx-inc
import add.zdf_filter.jsfx-inc
import add.rbj_filter.jsfx-inc
import add.gfxlib.jsfx-inc
import add.mouselib.jsfx-inc
import add.fft_real_synth.jsfx-inc
Expand Down Expand Up @@ -120,6 +120,20 @@ function db2gain (db) local (val) (
function normalize_vol_slider(val) ( val * 60 / 100 - 60 );
function note2freq(n) ( 440 * pow(2, (n - 69) / 12); );

function rc_set(rc)
instance(a) (
a = 1 / (rc * srate + 1);
);
function rc_lp(sample)
instance(lp, a) (
lp += a * (sample - lp);
);
function smooth()
instance (lp, smooth) (
lp = smooth;
smooth = this.rc_lp(this);
);

function normalize_wave(buf, len)
local (_min, _max, i)
(
Expand Down Expand Up @@ -189,7 +203,7 @@ function resynthesize() (
semi_normalize_wave(wavebuf, four.size * 2); // correct signal strenght after summing frequencies
);

function read_file(filehandle)
function read_file(filehandle)
local (len)
(
filehandle > 0 ? (
Expand Down Expand Up @@ -226,58 +240,12 @@ function read_file_string(str) (
read_file(filehandle);
);

// copy filter coeficients from buffer1 to buffer2
function filter_copy_coefs(buf1, buf2) (
buf2[2] = buf1[2];
buf2[3] = buf1[3];
buf2[4] = buf1[4];
);

// wraps filter function using buffers
function filter_setf(buf, freq, q) (
filter.zdf_setf(freq, q);
buf[2] = filter.g;
buf[3] = filter.r2;
buf[4] = filter.h;
);

// wraps filter function using buffers
function filter_lp(buf, sample) local(lp) (
filter.s1 = buf[0];
filter.s2 = buf[1];
filter.g = buf[2];
filter.r2 = buf[3];
filter.h = buf[4];
lp = filter.zdf_svf_lp(sample);
buf[0] = filter.s1;
buf[1] = filter.s2;
lp;
);

// wraps filter function using buffers
function filter_bp(buf, sample) local(bp) (
filter.s1 = buf[0];
filter.s2 = buf[1];
filter.g = buf[2];
filter.r2 = buf[3];
filter.h = buf[4];
bp = filter.zdf_svf_bp(sample);
buf[0] = filter.s1;
buf[1] = filter.s2;
bp;
);

// wraps filter function using buffers
function filter_hp(buf, sample) local(hp) (
filter.s1 = buf[0];
filter.s2 = buf[1];
filter.g = buf[2];
filter.r2 = buf[3];
filter.h = buf[4];
hp = filter.zdf_svf_hp(sample);
buf[0] = filter.s1;
buf[1] = filter.s2;
hp;
function copy_filter_coefs (f1, f2) (
f2[0] = f1[0]; // a1
f2[1] = f1[1]; // a2
f2[2] = f1[2]; // b0
f2[3] = f1[3]; // b1
f2[4] = f1[4]; // b2
);

function on_slider() (
Expand All @@ -296,6 +264,10 @@ function on_slider() (
// FIX - read initial wave from string instead of slider
read_file_string("tilr_JSAdditiv/Sine 1.wav");

// Init smoothing
gain.rc_set(0.0033);
gain.smooth = db2gain(normalize_vol_slider(vol));

@serialize
file_var(0, osc_free);
file_mem(0, wavebuf, wavelen + four.size * 4);
Expand Down Expand Up @@ -395,6 +367,7 @@ while (midirecv(offset, msg1, note, vel)) (
);

@sample
gain.smooth();

ptr = poly.array_first();
while(ptr >= 0) ( // for each note/voice
Expand Down Expand Up @@ -430,34 +403,29 @@ while(ptr >= 0) ( // for each note/voice
flt_shape != 0 ? (
filterbuf = filter_env + ptr[0] * 7; // envelope buffer
adsr_process(filterbuf);
filterbuf_l = filter_arr_l + ptr[0] * 11; // filter buffer left
filterbuf_r = filter_arr_r + ptr[0] * 11; // filter buffer right
multiplier = pow(20000/flt_freq, filterbuf[0] * flt_amt / 100);

filter_setf(filterbuf_l, flt_freq * multiplier, flt_q);
filter_copy_coefs(filterbuf_l, filterbuf_r);
filterbuf_l = filter_arr_l + ptr[0] * 11; // filter buffer left
filterbuf_r = filter_arr_r + ptr[0] * 11; // filter buffer left

flt_shape == 1 ? (
outl = filter_lp(filterbuf_l, outl);
outr = filter_lp(filterbuf_r, outr);
flt_shape == 1 ? (
rbj_lp(filterbuf_l, flt_freq * multiplier, flt_q);
) : flt_shape == 2 ? (
outl = filter_bp(filterbuf_l, outl);
outr = filter_bp(filterbuf_r, outr);
rbj_bp(filterbuf_l, flt_freq * multiplier, flt_q);
) : (
outl = filter_hp(filterbuf_l, outl);
outr = filter_hp(filterbuf_r, outr);
rbj_hp(filterbuf_l, flt_freq * multiplier, flt_q);
);
copy_filter_coefs(filterbuf_l, filterbuf_r);
outl = rbj_df1(filterbuf_l, outl);
outr = rbj_df1(filterbuf_r, outr);
);

spl0 += outl * gain;
spl1 += outr * gain;
spl0 += outl * gain.smooth;
spl1 += outr * gain.smooth;

ptr = poly.array_next(ptr);
);

spl0 += outl * gain;
spl1 += outr * gain;

@gfx 520 270

gfx_clear = COLOR_BG;
Expand Down
1 change: 0 additions & 1 deletion Synth/tilr_JSAdditiv/add.gfxlib.jsfx-inc
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ local (harm_w, amp, i, mouse_active, hx, ph, yamp, phase, real, img, yphase)
);
phase_mode && mouse_active && mouse_in_rect(hx, y, harm_w, h) ? (
yphase = mouse.control ? 0 : (mouse.y - y) / h * -2 - 1;
yphasee = yphase;
amp = harmonic_amplitude(buf[i], buf[i+1]);
buf[i+1] = amp * cos(yphase*$pi); // real
buf[i] = amp * sin(yphase*$pi); // img
Expand Down
Loading

0 comments on commit d6b9eb1

Please sign in to comment.