Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An implementation of a new frequency mode, see issue #333 #334

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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