File tree 3 files changed +15
-2
lines changed
3 files changed +15
-2
lines changed Original file line number Diff line number Diff line change @@ -20,11 +20,17 @@ const NOISE_LENGTH = 0x8000;
20
20
// 440.0 / 44100,
21
21
// ].reverse();
22
22
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
+
23
28
export class APU {
24
29
constructor ( ) {
25
30
const ctx = new ( window . AudioContext || window . webkitAudioContext ) ( ) ;
26
31
this . ctx = ctx ;
27
32
33
+ this . frequency_mode = 0 ;
28
34
this . nodes = new Array ( 4 ) ;
29
35
this . gains = new Array ( 4 ) ;
30
36
@@ -58,8 +64,13 @@ export class APU {
58
64
}
59
65
60
66
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
+ }
63
74
64
75
const sustain = ( duration & 0xff ) / 60 ;
65
76
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;
45
45
46
46
export const SYSTEM_PRESERVE_FRAMEBUFFER = 1 ;
47
47
export const SYSTEM_HIDE_GAMEPAD_OVERLAY = 2 ;
48
+ export const SYSTEM_MIDI_FREQUENCY_MODE = 4 ;
48
49
49
50
// Flags for Runtime.pauseState
50
51
export const PAUSE_UNFOCUSED = 1 ;
Original file line number Diff line number Diff line change @@ -324,6 +324,7 @@ export class Runtime {
324
324
if ( ! this . getSystemFlag ( constants . SYSTEM_PRESERVE_FRAMEBUFFER ) ) {
325
325
this . framebuffer . clear ( ) ;
326
326
}
327
+ this . apu . frequency_mode = this . getSystemFlag ( constants . SYSTEM_MIDI_FREQUENCY_MODE ) ? 1 : 0 ;
327
328
328
329
this . safeCall ( this . wasm . exports . update ) ;
329
330
You can’t perform that action at this time.
0 commit comments