commit be770c2636f10d9056698fae130238ecb4d523ec
parent 3791f09537852d24498881ab84855582fedf5a6b
Author: rpa <rpa@laika>
Date: Sat, 24 Dec 2022 21:49:19 +0000
mu: decay ctl is now in msec
Diffstat:
3 files changed, 74 insertions(+), 43 deletions(-)
diff --git a/src/mu/mubox.c b/src/mu/mubox.c
@@ -12,24 +12,25 @@
#include <thread.h>
#define MaxArgs 32
+#define MaxVoices 6
#define BSize 441
typedef struct Voice Voice;
struct Voice {
double stime;
- double freq;
- double amplitude;
double volume;
+ double amplitude;
double decay;
+ double freq;
double bend;
};
enum {
- VKick = 6,
- VNoise = 7,
+ VKick = MaxVoices,
+ VNoise = MaxVoices + 1,
};
-Voice v[8], vv, *vp;
+Voice v[MaxVoices + 2], vv, *vp;
double T, mix[2];
Biobuf *in;
int out;
@@ -59,11 +60,18 @@ struct {
{"B-", 493.88},
};
+double
+ms2decay(double x)
+{
+ if (x <= 0) return 1;
+ return 1/(x*44.1);
+}
+
Voice *
newvoice(double freq)
{
static int n = 0;
- n = (n + 1) % 6;
+ n = (n + 1) % MaxVoices;
v[n] = vv;
v[n].amplitude = 1;
v[n].stime = T;
@@ -98,28 +106,41 @@ note(char *s)
void
cmd(char *s)
{
- ulong i;
double f;
if (note(s) != 0) switch(s[0]) {
- // noise channel commands
+ /* noise channel commands */
+ case 'h':
+ v[VNoise].amplitude = 1;
+ v[VNoise].decay = ms2decay(25);
+ break;
+ case 'H':
+ v[VNoise].amplitude = 1;
+ v[VNoise].decay = ms2decay(100);
+ break;
case 'N':
- i = strtoul(s+1, nil, 16);
- v[VNoise].volume = (double)i / 256.0;
+ f = atof(s+1);
+ v[VNoise].volume = f;
v[VNoise].amplitude = 1;
break;
case 'n':
- i = strtoul(s+1, nil, 16);
- v[VNoise].decay = (double)i / 256.0;
+ f = atof(s+1);
+ v[VNoise].decay = ms2decay(f);
+ v[VNoise].amplitude = 1;
break;
- // kick channel commands
+ /* kick channel commands */
case 'k':
v[VKick].stime = T;
- v[VKick].freq = 200;
+ v[VKick].freq = 220;
+ v[VKick].amplitude = 1;
+ break;
+ case 'K':
+ v[VKick].stime = T;
+ v[VKick].freq = 440;
v[VKick].amplitude = 1;
break;
- // voice channel commands
+ /* voice channel commands */
case 'f': // freq
f = atof(s+1);
vp->freq = f;
@@ -132,8 +153,8 @@ cmd(char *s)
break;
case 'd': // decay;
f = atof(s+1);
- vp->decay = f;
- vv.decay = f;
+ vp->decay = ms2decay(f);
+ vv.decay = ms2decay(f);
break;
case 'v':
f = atof(s+1);
@@ -184,7 +205,8 @@ noise(void)
void
kick(void)
{
- double x = sin(2.0 * PI * (T - v[VKick].stime) * v[VKick].freq) * v[VKick].amplitude * v[VKick].volume;
+ double x = cos(2.0 * PI * (T - v[VKick].stime) * v[VKick].freq) *
+ v[VKick].amplitude * v[VKick].volume;
mix[0] += x;
mix[1] += x;
}
@@ -202,27 +224,20 @@ void
output(void)
{
s16int buf[BSize * 2];
- int i;
+ int i, j;
for (i = 0; i < BSize; i++) {
mix[0] = 0;
mix[1] = 0;
- voice(&v[0]);
- voice(&v[1]);
- voice(&v[2]);
- voice(&v[3]);
- voice(&v[4]);
- voice(&v[5]);
+ for (j = 0; j < MaxVoices; j++) voice(&v[j]);
noise();
kick();
+ for (j = 0; j < MaxVoices + 2; j++) modvoice(&v[j]);
buf[i * 2] = 0x7fff * (mix[0] / 8);
buf[i * 2 + 1] = 0x7fff * (mix[1] / 8);
T += 1.0/44100.0;
}
- for (i = 0; i < 8; i++) modvoice(&v[i]);
+
write(out, buf, BSize * 4);
- //Dir *d = dirfstat(out);
- //fprint(2, "%lld\n", d->length);
- //free(d);
}
void
@@ -235,6 +250,7 @@ usage(void)
void
threadmain(int argc, char **argv)
{
+ int i;
ARGBEGIN {
case 'r':
realtime = 1;
@@ -243,28 +259,44 @@ threadmain(int argc, char **argv)
usage();
} ARGEND;
if (argc != 0) usage();
+ for (i = 0; i < MaxVoices; i++) {
+ v[i] = (Voice) {
+ 0,
+ 1,
+ 0,
+ 0,
+ 0,
+ 1,
+ };
+ }
v[VKick] = (Voice) {
0,
- 200,
+ 1,
+ 0,
+ 0.0001,
+ 220,
+ 0.9995,
+ };
+ v[VNoise] = (Voice) {
0,
1,
- 0.25,
- 25.45/255.0,
+ 0,
+ 0.01,
+ 0,
+ 0,
};
- v[VNoise].decay = 1;
vv = (Voice) {
0,
- 440,
1,
1,
+ ms2decay(500),
0,
1,
};
in = Bfdopen(0, OREAD);
- // out = open("/dev/audio", OWRITE);
out = 1;
- vp = &v[1];
+ vp = &v[0];
proccreate(threadread, nil, 1024 * 1024 * 64);
vlong t = nsec() / 1000000;
diff --git a/src/mu/track b/src/mu/track
@@ -1,15 +1,14 @@
.bpm 120 2
-d0.01
+d100
-
-. k
+k
.
-. k
+k
.
-. k
+k
.
-. n02 N1f
+n02 N1f
.
##############
. n04 N7f k v110
diff --git a/src/mu/track2 b/src/mu/track2
@@ -1,5 +1,5 @@
.bpm 120 4
-d0.1
+d250
k C-3 C-5
----
C-6