stew

a monorepo of some sort
git clone git://git.nsmpr.xyz/stew.git
Log | Files | Refs

wavetable.c (974B)


      1 #include <u.h>
      2 #include <libc.h>
      3 #include "wavetable.h"
      4 
      5 int
      6 pstep(Phasor *p, int freq, int phase, int linear)
      7 {
      8 	int t = (p->t + phase) * WFSize / 44100;
      9 	p->t += freq;
     10 	if (linear == 0) return t % WFSize;
     11 	else return t;
     12 }
     13 
     14 u32int
     15 wtfunc(s16int *wtp, int phase, int mod, int vol, int pan)
     16 {
     17 	int m1, m2, x, y, d, v, mono;
     18 	union {
     19 		u32int i;
     20 		s16int s[2];
     21 	} out;
     22 	d = mod % WTSize;
     23 	m1 = phase + (mod / WTSize) * WFSize;
     24 	m2 = m1 + WFSize;
     25 	x = wtp[m1 % (WTSize * WFSize)];
     26 	y = wtp[m2 % (WTSize * WFSize)];
     27 	v = (x * (WTSize - d) + y * d) / WTSize;
     28 	mono = v * vol / 0x7fff;
     29 	out.s[0] = mono;
     30 	out.s[1] = mono;
     31 	if (pan > 0) out.s[0] = mono * (0x7fff - pan) / 0x7fff;
     32 	else if (pan < 0) out.s[1] = mono * (0x7fff + pan) / 0x7fff;
     33 	return out.i;
     34 }
     35 
     36 u32int
     37 wtstep(Synth *s, Phasor *p)
     38 {
     39 	s16int *wtp;
     40 	int phase;
     41 	wtp = s->table[s->mod.wtsel % 64];
     42 	phase = pstep(p, s->mod.freq, s->mod.phase, s->flag);
     43 	return wtfunc(wtp, phase, s->mod.mod, s->mod.vol, s->mod.pan);
     44 }