Skip to content

Commit 3aecb5d

Browse files
committed
An implementation of a new frequency mode, see issue #333
1 parent 403fe4e commit 3aecb5d

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

runtimes/web/src/apu.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,17 @@ const NOISE_LENGTH = 0x8000;
2020
// 440.0 / 44100,
2121
// ].reverse();
2222

23+
const twelvth_root_of_2 = Math.pow(2, 1/12);
24+
function midiToFreq(pitch) {
25+
return 440 * Math.pow(twelvth_root_of_2, (pitch / 256) - 69);
26+
}
27+
2328
export class APU {
2429
constructor () {
2530
const ctx = new (window.AudioContext || window.webkitAudioContext)();
2631
this.ctx = ctx;
2732

33+
this.frequency_mode = 0;
2834
this.nodes = new Array(4);
2935
this.gains = new Array(4);
3036

@@ -58,8 +64,13 @@ export class APU {
5864
}
5965

6066
tone (frequency, duration, volume, flags) {
61-
const freq1 = frequency & 0xffff;
62-
const freq2 = (frequency >> 16) & 0xffff;
67+
var freq1 = frequency & 0xffff;
68+
var freq2 = (frequency >> 16) & 0xffff;
69+
70+
if (this.frequency_mode == 1) {
71+
freq1 = midiToFreq(freq1);
72+
freq2 = (freq2 == 0) ? 0 : midiToFreq(freq2);
73+
}
6374

6475
const sustain = (duration & 0xff) / 60;
6576
const release = ((duration >> 8) & 0xff) / 60;

runtimes/web/src/constants.js

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export const MOUSE_MIDDLE = 4;
4545

4646
export const SYSTEM_PRESERVE_FRAMEBUFFER = 1;
4747
export const SYSTEM_HIDE_GAMEPAD_OVERLAY = 2;
48+
export const SYSTEM_MIDI_FREQUENCY_MODE = 4;
4849

4950
// Flags for Runtime.pauseState
5051
export const PAUSE_UNFOCUSED = 1;

runtimes/web/src/runtime.js

+1
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ export class Runtime {
324324
if (!this.getSystemFlag(constants.SYSTEM_PRESERVE_FRAMEBUFFER)) {
325325
this.framebuffer.clear();
326326
}
327+
this.apu.frequency_mode = this.getSystemFlag(constants.SYSTEM_MIDI_FREQUENCY_MODE) ? 1 : 0;
327328

328329
this.safeCall(this.wasm.exports.update);
329330

0 commit comments

Comments
 (0)