commit cee75e2bb82c9a8f9430164a7d5efe92ee581133
parent 49130e2ffe3fcf7bc3f2047fc38f613670fbbb71
Author: zavok <an2qzavok@gmail.com>
Date: Mon, 4 Nov 2019 17:01:59 +0300
moved synth code to separate file.
Diffstat:
M | fm.c | | | 51 | +++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | fm.h | | | 4 | ++++ |
M | usynth.c | | | 44 | +++----------------------------------------- |
3 files changed, 58 insertions(+), 41 deletions(-)
diff --git a/fm.c b/fm.c
@@ -1,2 +1,53 @@
#include <stdio.h>
#include "fm.h"
+
+double tune[]={
+16.35, //C 36
+17.32, //C# 37
+18.35, //D 38
+19.45, //D# 39
+20.60, //E 40
+21.83, //F 41
+23.12, //F# 42
+24.50, //G 43
+25.96, //G# 44
+27.50, //A 45 - 440 - 100
+29.14, //A# 46
+30.87, //B 47
+};
+
+size_t c, dt;
+int16_t vol;
+
+int16_t fmsynth(void);
+
+int16_t
+fm_synth(void)
+{
+ c++;
+ if (dt == 0) return 0;
+ if (c%dt > dt / 2) return vol;
+ return -vol;
+}
+
+void
+fm_fillbuf(int16_t *buf, size_t nsamples)
+{
+ size_t i;
+ for (i=0; i < nsamples; i+=2){
+ buf[i] = buf[i+1] = fm_synth();
+ }
+}
+
+void
+fm_note_on(uint8_t *mm)
+{
+ dt = 44100.0 / (tune[mm[2]%12] * (1 << mm[2]/12));
+ vol = 0x7fff * mm[3] / 0xff;
+}
+
+void
+fm_note_off(uint8_t *mm)
+{
+ vol = 0;
+}
diff --git a/fm.h b/fm.h
@@ -0,0 +1,4 @@
+void fm_fillbuf(int16_t*, size_t);
+void fm_note_on(uint8_t*);
+void fm_note_off(uint8_t*);
+
diff --git a/usynth.c b/usynth.c
@@ -9,22 +9,6 @@
struct sio_hdl *sh;
size_t bs;
-int16_t vol, dt;
-
-double tune[]={
-16.35, //C 36
-17.32, //C# 37
-18.35, //D 38
-19.45, //D# 39
-20.60, //E 40
-21.83, //F 41
-23.12, //F# 42
-24.50, //G 43
-25.96, //G# 44
-27.50, //A 45 - 440 - 100
-29.14, //A# 46
-30.87, //B 47
-};
size_t rt, wt;
@@ -59,45 +43,23 @@ sinit(void)
sio_start(sh);
}
-void
-note_on(uint8_t *mm)
-{
- dt = 44100 / (tune[mm[2]%12] * (1 << (mm[2]/12)));
- vol = mm[3]*100;
-}
-
-void
-note_off(uint8_t *mm)
-{
- vol = 0;
-}
-
int
main(int argc, char **argv)
{
- int64_t c;
int16_t *sbuf;
- vol = 0;
wt = 0;
rt = 0;
sinit();
m_init("rmidi/0");
- m_vector.note_on = note_on;
- m_vector.note_off = note_off;
+ m_vector.note_on = fm_note_on;
+ m_vector.note_off = fm_note_off;
sbuf = malloc(bs * 2 * sizeof(int16_t));
- c = 0;
- dt = 100;
printf("entering main loop\n");
while (1){
size_t n;
int i;
m_read();
- for (i=0; i<bs * 2; i+=2){
- c++;
- sbuf[i] = (c%dt > (dt/2))? vol : -vol;
- sbuf[i+1] = sbuf[i];
- }
- n = 0;
+ fm_fillbuf(sbuf, bs * 2);
n = sio_write(sh, sbuf, bs * 2 * sizeof(int16_t));
wt += n;
}