Skip to content

Commit

Permalink
An implementation of a new frequency mode, see issue #333
Browse files Browse the repository at this point in the history
  • Loading branch information
marler8997 committed Jan 22, 2022
1 parent 403fe4e commit 3aecb5d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
15 changes: 13 additions & 2 deletions runtimes/web/src/apu.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ const NOISE_LENGTH = 0x8000;
// 440.0 / 44100,
// ].reverse();

const twelvth_root_of_2 = Math.pow(2, 1/12);
function midiToFreq(pitch) {
return 440 * Math.pow(twelvth_root_of_2, (pitch / 256) - 69);
}

export class APU {
constructor () {
const ctx = new (window.AudioContext || window.webkitAudioContext)();
this.ctx = ctx;

this.frequency_mode = 0;
this.nodes = new Array(4);
this.gains = new Array(4);

Expand Down Expand Up @@ -58,8 +64,13 @@ export class APU {
}

tone (frequency, duration, volume, flags) {
const freq1 = frequency & 0xffff;
const freq2 = (frequency >> 16) & 0xffff;
var freq1 = frequency & 0xffff;
var freq2 = (frequency >> 16) & 0xffff;

if (this.frequency_mode == 1) {
freq1 = midiToFreq(freq1);
freq2 = (freq2 == 0) ? 0 : midiToFreq(freq2);
}

const sustain = (duration & 0xff) / 60;
const release = ((duration >> 8) & 0xff) / 60;
Expand Down
1 change: 1 addition & 0 deletions runtimes/web/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const MOUSE_MIDDLE = 4;

export const SYSTEM_PRESERVE_FRAMEBUFFER = 1;
export const SYSTEM_HIDE_GAMEPAD_OVERLAY = 2;
export const SYSTEM_MIDI_FREQUENCY_MODE = 4;

// Flags for Runtime.pauseState
export const PAUSE_UNFOCUSED = 1;
Expand Down
1 change: 1 addition & 0 deletions runtimes/web/src/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ export class Runtime {
if (!this.getSystemFlag(constants.SYSTEM_PRESERVE_FRAMEBUFFER)) {
this.framebuffer.clear();
}
this.apu.frequency_mode = this.getSystemFlag(constants.SYSTEM_MIDI_FREQUENCY_MODE) ? 1 : 0;

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

Expand Down

0 comments on commit 3aecb5d

Please sign in to comment.