commit eadf2ce1fa6978a1d9e201d9b4235fa3f5bd33a3
parent 98274b9e971e78f0481ba0180f19aafd9ef21f04
Author: prenev <an2qzavok@gmail.com>
Date: Sat, 10 Apr 2021 19:41:22 +0300
wavetable: enlarge, fix triangle
Diffstat:
3 files changed, 13 insertions(+), 11 deletions(-)
diff --git a/machine.c b/machine.c
@@ -142,7 +142,7 @@ void
i_wave(int16_t *vm, uint8_t arg)
{
uint16_t d;
- d = ((uint16_t)TOS(vm)) / WT_LENGTH;
+ d = ((uint16_t)TOS(vm)) * WT_LENGTH / 0xffff;
d = d%WT_LENGTH;
TOS(vm) = wavetable[arg][d];
}
diff --git a/wavetable.c b/wavetable.c
@@ -19,21 +19,23 @@ wt_init(void)
wavetable = malloc(sizeof(int16_t*) * WT_SIZE);
for (i=0;i<WT_SIZE;i++)
wavetable[i] = malloc(sizeof(int16_t) * WT_LENGTH);
- wt_pulse(wavetable[0]);
- wt_silence(wavetable[1]);
- wt_silence(wavetable[2]);
+ wt_sin(wavetable[0]);
+ wt_triangle(wavetable[1]);
+ wt_pulse(wavetable[2]);
wt_silence(wavetable[3]);
}
void
wt_triangle(int16_t *w)
{
- /* TODO: fix clipping */
size_t i, pos;
- for (i = 0; i < WT_LENGTH; i++){
+ for (i = 0; i < WT_LENGTH/2; i++){
pos = (i-WT_LENGTH/4)%WT_LENGTH;
- if (i < WT_LENGTH/2) w[pos] = 0x7fff + 0xffff*2*i/WT_LENGTH;
- else w[pos] = 0x7fff - 0xffff*i*2/WT_LENGTH;
+ w[pos] = -0x7fff + i * 0xffff * 2 / WT_LENGTH;
+ }
+ for (i = 0; i < WT_LENGTH/2; i++){
+ pos = (i+WT_LENGTH/4)%WT_LENGTH;
+ w[pos] = 0x7fff - i * 0xffff * 2 / WT_LENGTH;
}
}
@@ -41,8 +43,8 @@ void
wt_pulse(int16_t *w)
{
size_t i;
- for (i=0; i<WT_LENGTH/2; i++) w[i] = -0x07ff;
- for (; i<WT_LENGTH; i++) w[i] = 0x07ff;
+ for (i=0; i<WT_LENGTH/2; i++) w[i] = -0x7fff;
+ for (; i<WT_LENGTH; i++) w[i] = 0x7fff;
}
void
diff --git a/wavetable.h b/wavetable.h
@@ -1,4 +1,4 @@
-#define WT_LENGTH 256
+#define WT_LENGTH 1024
#define WT_SIZE 8
extern int16_t **wavetable;