frequency control (bad)

This commit is contained in:
Felix Roos
2024-01-10 18:27:40 +01:00
parent 7c4ed7e05f
commit 26793aec91
5 changed files with 11 additions and 3 deletions
Binary file not shown.
+2 -2
View File
@@ -1,5 +1,5 @@
const std = @import("std");
export fn saw(t: f64) f64 {
return ((@mod(110.0 * t, 1.0)) - 0.5) * 2.0;
export fn saw(t: f64, f: f64) f64 {
return ((@mod(f * t, 1.0)) - 0.5) * 2.0;
}
+1
View File
@@ -5,6 +5,7 @@
</head>
<body>
<button id="play">play</button>
<input type="range" min="55" max="880" id="freq" />
</body>
<script src="./main.js"></script>
</html>
+4
View File
@@ -14,4 +14,8 @@ document.getElementById('play').addEventListener('click', async () => {
};
node.port.postMessage({ webassembly: buffer });
node.connect(ac.destination);
document.getElementById('freq').addEventListener('input', async (e) => {
node.port.postMessage({ frequency: e.target.value });
});
});
+4 -1
View File
@@ -2,6 +2,7 @@ class SawProcessor extends AudioWorkletProcessor {
constructor() {
super();
this.t = 0; // samples passed
this.f = 110;
this.port.onmessage = (e) => {
const key = Object.keys(e.data)[0];
const value = e.data[key];
@@ -12,6 +13,8 @@ class SawProcessor extends AudioWorkletProcessor {
this.port.postMessage('OK');
});
break;
case 'frequency':
this.f = value;
}
};
}
@@ -22,7 +25,7 @@ class SawProcessor extends AudioWorkletProcessor {
for (let i = 0; i < output[0].length; i++) {
let t = this.t;
let out = 0;
out = this.api.saw(t / 44100, 220);
out = this.api.saw(t / 44100, this.f);
output.forEach((channel) => {
channel[i] = out;
});