File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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+
2328export 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 = midiToFreq ( freq2 ) ;
73+ }
6374
6475 const sustain = ( duration & 0xff ) / 60 ;
6576 const release = ( ( duration >> 8 ) & 0xff ) / 60 ;
Original file line number Diff line number Diff line change @@ -45,6 +45,7 @@ export const MOUSE_MIDDLE = 4;
4545
4646export const SYSTEM_PRESERVE_FRAMEBUFFER = 1 ;
4747export const SYSTEM_HIDE_GAMEPAD_OVERLAY = 2 ;
48+ export const SYSTEM_MIDI_FREQUENCY_MODE = 4 ;
4849
4950// Flags for Runtime.pauseState
5051export const PAUSE_UNFOCUSED = 1 ;
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments