usynth

simple midi synth for sndio
git clone git://nsmpr.xyz/usynth.git
Log | Files | Refs

commit 50e1e1851facbee0d9d90d8c05a7663adc7c1cf3
parent a221ba50e3897d3a3dd15ff46c7aec27274b6830
Author: zavok <an2qzavok@gmail.com>
Date:   Wed, 12 Feb 2020 17:32:29 +0300

Merge branch 'master' of ssh://solid.at-home.me:/home/git/pub/usynth

Diffstat:
MMakefile | 2+-
Mmachine.c | 16++++++++++------
Musynth.c | 1+
Mwavetable.c | 39++++++++++++++++++++++++++++++---------
Mwavetable.h | 2+-
5 files changed, 43 insertions(+), 17 deletions(-)

diff --git a/Makefile b/Makefile @@ -8,7 +8,7 @@ MAN = $(BIN).1 OBJ = $(BIN:=.o) fm.o midi.o\ machine.o operator.o wavetable.o -LDFLAGS=-lsndio +LDFLAGS=-lsndio -lm all: $(BIN) diff --git a/machine.c b/machine.c @@ -1,4 +1,5 @@ #include <stdlib.h> +#include <assert.h> #include <stdio.h> #include <stdint.h> #include "operator.h" @@ -45,15 +46,14 @@ instruction instructions[128] = { /*** simple hard-coded program ***/ int16_t sprog[] = { - I_LIT, 25, - I_2DT, - I_OP|0x0000, - I_WAVE|0x0000, + //I_LIT, 1, + //I_OP|0x0000, + //I_WAVE|0x0003, I_NOTE, I_2DT, I_OP|0x0000, - I_ADD, I_WAVE|0x0000, + //I_COMB, I_ADSR|0x0000, I_COMB, I_HALT @@ -138,8 +138,12 @@ i_adsr(VM *vm, uint8_t arg) void i_wave(VM *vm, uint8_t arg) { + uint16_t d; + d = ((uint16_t)vm->stack[vm->sp-1]) >> 8; + assert( d >= 0 ); + assert( d < WT_LENGTH ); vm->stack[vm->sp-1] = - wavetable[0][vm->stack[vm->sp-1]>>8]; + wavetable[arg][d]; } void diff --git a/usynth.c b/usynth.c @@ -49,6 +49,7 @@ main(int argc, char **argv) m_read(); fm_fillbuf(sbuf, bs * 2); n = sio_write(sh, sbuf, bs * 2 * sizeof(int16_t)); + write (1, sbuf, bs * 2 * sizeof (int16_t)); } return 0; } diff --git a/wavetable.c b/wavetable.c @@ -1,40 +1,61 @@ #include <stdio.h> #include <stdlib.h> #include <stdint.h> +#include <math.h> #include "wavetable.h" void wt_triangle(int16_t*); void wt_sin(int16_t*); void wt_pulse(int16_t*); void wt_saw(int16_t*); +void wt_silence(int16_t*); void wt_init(void) { int i; wavetable = malloc(sizeof(int16_t*) * WT_SIZE); - for (i=0;i<8;i++) wavetable[i] = malloc(sizeof(int16_t) * WT_LENGTH); - //wt_sin(wavetable[0]); - wt_triangle(wavetable[0]); - wt_pulse(wavetable[1]); + for (i=0;i<WT_SIZE;i++) + wavetable[i] = malloc(sizeof(int16_t) * WT_LENGTH); + wt_sin(wavetable[0]); + wt_silence(wavetable[1]); + wt_silence(wavetable[2]); + wt_silence(wavetable[3]); } void -wt_triangle(int16_t* w) +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; i++){ pos = (i-WT_LENGTH/4)%WT_LENGTH; - if (i < WT_LENGTH/2) w[pos] = -0x7fff + 0xffff*i/WT_LENGTH; - else w[pos] = 0x7fff - 0xffff*i/WT_LENGTH; + if (i < WT_LENGTH/2) w[pos] = 0x7fff + 0xffff*2*i/WT_LENGTH; + else w[pos] = 0x7fff - 0xffff*i*2/WT_LENGTH; } } void -wt_pulse(int16_t* w) +wt_pulse(int16_t *w) { size_t i; for (i=0; i<WT_LENGTH; i++) if (i < WT_LENGTH/2) w[i] = 0x7fff; else w[i] = -0x7fff; } + +void +wt_sin(int16_t *w) +{ + size_t i; + for (i=0; i<WT_LENGTH; i++){ + w[i] = sin(2 * M_PI * (double)i/(double)WT_LENGTH) * 0x7fff; + } +} + +void +wt_silence(int16_t *w) +{ + size_t i; + for (i=0; i<WT_LENGTH; i++) w[i] = 0; +} diff --git a/wavetable.h b/wavetable.h @@ -1,4 +1,4 @@ -#define WT_LENGTH 0x100 +#define WT_LENGTH 256 #define WT_SIZE 8 int16_t **wavetable;