usynth

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

commit a2acd5f1c8acbcab54a64c00fddbda89b52c2e85
parent c1e257d07042dd6435b5b6238c56e6b0ac119172
Author: prenev <an2qzavok@gmail.com>
Date:   Fri,  9 Apr 2021 01:34:05 +0300

make some noice with usynth, but vms args appear to not work properly

Diffstat:
MMakefile | 3++-
Mmachine.c | 19+++++++++----------
Mmachine.h | 2--
Mmidi.c | 3+++
Mmidi.h | 3+--
Musynth.c | 12+++++++++++-
Mwavetable.c | 9+++++----
Mwavetable.h | 2+-
8 files changed, 32 insertions(+), 21 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,6 +1,7 @@ include config.mk -SRC = usynth.c fm.c machine.c midi.c operator.c wavetable.c +# SRC = usynth.c fm.c machine.c midi.c operator.c wavetable.c +SRC = usynth.c midi.c machine.c wavetable.c OBJ = ${SRC:.c=.o} BIN = progsynth diff --git a/machine.c b/machine.c @@ -44,9 +44,11 @@ instruction instructions[128] = { /*** simple hard-coded program ***/ int16_t sprog[] = { - //I_LIT, 1, - //I_OP|0x0000, - //I_WAVE|0x0003, + I_NOTE, + I_2DT, + I_OP|0x0000, + I_WAVE|0x0000, + //I_NOTE, //I_2DT, //I_OP|0x0000, @@ -76,8 +78,7 @@ vm_run(VM *vm) vm_adsr(vm); while (vm->halted == 0) { inst = instructions[0xff & vm->prog[vm->pc]]; - if (inst == 0) - vm_fatal(vm, "illegal instruction"); + if (inst == 0) vm_fatal(vm, "illegal instruction"); vm->pc++; inst(vm, vm->prog[vm->pc]>>8); } @@ -139,11 +140,9 @@ 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[arg][d]; + d = ((uint16_t)vm->stack[vm->sp-1]) / WT_LENGTH; + d = d%WT_LENGTH; + vm->stack[vm->sp-1] = wavetable[arg][d]; } void diff --git a/machine.h b/machine.h @@ -33,8 +33,6 @@ struct VM { int16_t pc; }; -static VM vms[32]; - void vm_set(VM*); int16_t vm_run(VM*); diff --git a/midi.c b/midi.c @@ -2,6 +2,7 @@ #include <stdlib.h> #include <stdint.h> #include <sndio.h> +#include <assert.h> #include "midi.h" uint8_t mmessage[4]; @@ -9,6 +10,7 @@ uint8_t mlen; uint8_t mi; struct mio_hdl *mh; uint8_t mbuf[32]; +Mvector m_vector; enum { NOTE_OFF = 0x80, @@ -65,6 +67,7 @@ m_exec(void) break; case NOTE_ON: f = m_vector.note_on; + assert(f != NULL); break; case KEY_PRESSURE: f = m_vector.key_pressure; diff --git a/midi.h b/midi.h @@ -10,8 +10,7 @@ struct Mvector { void(*sys_message)(uint8_t*); }; -static Mvector m_vector; +extern Mvector m_vector; void m_init(char *mdev); void m_read(void); - diff --git a/usynth.c b/usynth.c @@ -3,7 +3,9 @@ #include <unistd.h> #include <stdint.h> #include <sndio.h> +#include <assert.h> #include "midi.h" +#include "wavetable.h" #include "machine.h" struct sio_hdl *sh; @@ -39,22 +41,30 @@ int main(void) { s_init("snd/0"); + wt_init(); m_init("midithru/0"); m_vector.note_on = note_on; m_vector.note_off = note_off; vm_set(&vm); + + //vm.active = 1; + //vm.status = 1; + //vm.key = 45; + while (1){ size_t n; m_read(); fillbuf(sbuf, bs * 2); n = sio_write(sh, sbuf, bs * 2 * sizeof(int16_t)); - // write(1, sbuf, bs * 2 * sizeof(int16_t)); } } void note_on(uint8_t *m) { + vm.active = 1; + vm.status = 1; + vm.key = m[2]; } void diff --git a/wavetable.c b/wavetable.c @@ -10,6 +10,8 @@ void wt_pulse(int16_t*); void wt_saw(int16_t*); void wt_silence(int16_t*); +int16_t **wavetable; + void wt_init(void) { @@ -17,7 +19,7 @@ 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_sin(wavetable[0]); + wt_pulse(wavetable[0]); wt_silence(wavetable[1]); wt_silence(wavetable[2]); wt_silence(wavetable[3]); @@ -39,9 +41,8 @@ void 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; + for (i=0; i<WT_LENGTH/2; i++) w[i] = -0x07ff; + for (; i<WT_LENGTH; i++) w[i] = 0x07ff; } void diff --git a/wavetable.h b/wavetable.h @@ -1,5 +1,5 @@ #define WT_LENGTH 256 #define WT_SIZE 8 -static int16_t **wavetable; +extern int16_t **wavetable; void wt_init(void);