mirror of
https://github.com/exoticorn/microw8.git
synced 2026-01-20 11:16:42 +01:00
add pulse width support to other wave types
This commit is contained in:
@@ -37,7 +37,7 @@ export fn upd() {
|
||||
data 80 {
|
||||
i8(
|
||||
0, 64, 0, 128, 0, 0x90,
|
||||
0, 128, 0, 128, 0, 0x4c,
|
||||
0, 64, 0, 128, 0, 0x4c,
|
||||
5, 128, 0, 128, 0, 0x4c,
|
||||
5, 128, 0, 128, 0, 0x4c,
|
||||
0xf8, 0x85,
|
||||
|
||||
Binary file not shown.
@@ -67,7 +67,10 @@ export fn gesSnd(t: i32) -> f32 {
|
||||
if wave < 2 {
|
||||
if wave {
|
||||
loop sawLoop {
|
||||
i!(GesBufferOffset + 128*4) = (phase & 65535) - 32768;
|
||||
let p = (phase ^ 32768) << 16;
|
||||
p = ((p ^ (p >> 31)) >> 15) + phaseShift;
|
||||
p = (p < 0 | p > 65535) * 65535;
|
||||
i!(GesBufferOffset + 128*4) = (phase ^ p & 65535) - 32768;
|
||||
phase = phase + phaseInc;
|
||||
branch_if (i := i + 4) < 64*4: sawLoop;
|
||||
}
|
||||
@@ -83,9 +86,12 @@ export fn gesSnd(t: i32) -> f32 {
|
||||
}
|
||||
} else {
|
||||
if wave == 2 {
|
||||
let scale = pulseWidth + 256;
|
||||
loop triLoop {
|
||||
let s = phase << 16;
|
||||
s = s ^ (s >> 31);
|
||||
s = (s ^ (s >> 31));
|
||||
s = (s >> 8) * scale;
|
||||
s = (s ^ (s >> 31));
|
||||
i!(GesBufferOffset + 128*4) = (s >> 15) - 32768;
|
||||
phase = phase + phaseInc;
|
||||
branch_if (i := i + 4) < 64*4: triLoop;
|
||||
@@ -93,9 +99,10 @@ export fn gesSnd(t: i32) -> f32 {
|
||||
} else {
|
||||
loop noiseLoop {
|
||||
let s = (phase >> 12) & 4095;
|
||||
let pulse = ((phase >> 8) & 255) >= pulseWidth;
|
||||
s = s * 0x6746ba73;
|
||||
s = s ^ (s >> 15);
|
||||
i!(GesBufferOffset + 128*4) = (s * 0x83567a92) >> 16;
|
||||
s = s ^ (s >> 15) * pulse;
|
||||
i!(GesBufferOffset + 128*4) = (s * 0x83567a92 + (phase << 16)) >> 16;
|
||||
phase = phase + phaseInc;
|
||||
branch_if (i := i + 4) < 64*4: noiseLoop;
|
||||
}
|
||||
@@ -124,8 +131,12 @@ export fn gesSnd(t: i32) -> f32 {
|
||||
let ctrl = filter?0x6a;
|
||||
let note = i32.load16_u(filter * 2, 0x6c);
|
||||
let inline freq = 440 as f32 * pow(2.0, (note - 69*256) as f32 / (12*256) as f32);
|
||||
let F = min(4096 as f32, 8192 as f32 * sin(freq * (3.1415 / 44100.0))) as i32;
|
||||
let Q = 7350 - ctrl * (6530/255);
|
||||
let F = (8192 as f32 * sin(min(0.25, freq / 44100 as f32) * 3.1415)) as i32;
|
||||
let Q = 8192 - (ctrl >> 4) * (7000/15);
|
||||
let Qlimit = (8192*4096/F - F/2) * 3 / 4;
|
||||
if Q > Qlimit {
|
||||
Q = Qlimit;
|
||||
}
|
||||
let low_out = ctrl & 1;
|
||||
let high_out = (ctrl >> 1) & 1;
|
||||
let band_out = (ctrl >> 2) & 1;
|
||||
|
||||
File diff suppressed because one or more lines are too long
59
tests/plot_ges.cwa
Normal file
59
tests/plot_ges.cwa
Normal file
@@ -0,0 +1,59 @@
|
||||
include "../examples/include/microw8-api.cwa"
|
||||
|
||||
export fn upd() {
|
||||
80?0 = (32!32 >> 11 << 6) | 5;
|
||||
80?1 = (sin(time() * 6 as f32) * 95 as f32) as i32 + 128;
|
||||
plotGes();
|
||||
}
|
||||
|
||||
data 80 { i8 (
|
||||
1, 128, 0, 69, 0, 15,
|
||||
0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0,
|
||||
0xff, 0xff,
|
||||
0xc1, 0, 0, 110, 0, 0
|
||||
) }
|
||||
|
||||
//import "env.gesSnd" fn gesSnd(i32) -> f32;
|
||||
|
||||
include "../platform/src/ges.cwa"
|
||||
|
||||
export fn snd(t: i32) -> f32 {
|
||||
gesSnd(t)
|
||||
}
|
||||
|
||||
global mut samplePos: i32 = 0;
|
||||
|
||||
const SoundBuffer = 0x30000;
|
||||
|
||||
fn plotGes() {
|
||||
rectangle(0 as f32, 10 as f32, 320 as f32, 320 as f32, 0);
|
||||
let count = (time() * 44100 as f32) as i32 * 2 - samplePos;
|
||||
let i: i32;
|
||||
loop genLoop {
|
||||
(i*4)$SoundBuffer = gesSnd(samplePos + i);
|
||||
branch_if (i := i + 1) < count: genLoop;
|
||||
}
|
||||
samplePos = samplePos + count;
|
||||
|
||||
let ch: i32;
|
||||
loop channelLoop {
|
||||
let offset = 159;
|
||||
i = 0;
|
||||
|
||||
loop searchLoop {
|
||||
offset = offset + 1;
|
||||
branch_if (offset * 8 + ch - 8)$SoundBuffer < 0 as f32 | (offset * 8 + ch)$SoundBuffer >= 0 as f32 & offset + 160 < count: searchLoop;
|
||||
}
|
||||
|
||||
offset = ch + (offset - 160) * 8;
|
||||
i = 0;
|
||||
loop plotLoop {
|
||||
setPixel(i, floor((i * 8 + offset)$SoundBuffer * 127 as f32) as i32 + 60 + ch * (120/8), 15);
|
||||
branch_if (i := i + 1) < 320: plotLoop;
|
||||
}
|
||||
|
||||
branch_if (ch := ch + 8) < 16: channelLoop;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user