Skip to content

Commit

Permalink
Fixed Sound for TS Builds, and FireFox JS Benchmarking (#218)
Browse files Browse the repository at this point in the history
relates to #216 

This fixes the sound inconsistencies between Wasm and JS, as well as fixes the JS performance of Firefox.

Also, snuck in some changes for faster travis and deploying, for less redundant building of the lib.

**Example of new firefox results for JS core**

<img width="827" alt="screen shot 2018-12-21 at 10 48 00 pm" src="https://user-images.githubusercontent.com/1448289/50371655-0503ef80-0574-11e9-8932-efb0bed39fc6.png">

**Old Travis Build Time**

<img width="961" alt="screen shot 2018-12-21 at 11 37 43 pm" src="https://user-images.githubusercontent.com/1448289/50371952-8742e280-0579-11e9-9fae-1ed8e7a57cc3.png">


**New Travis Build Time**

<img width="960" alt="screen shot 2018-12-21 at 11 37 37 pm" src="https://user-images.githubusercontent.com/1448289/50371953-8f028700-0579-11e9-8756-a89de78e7eaf.png">
  • Loading branch information
torch2424 authored Dec 22, 2018
1 parent 693b160 commit 838af61
Show file tree
Hide file tree
Showing 10 changed files with 585 additions and 534 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ node_js:
install:
- npm install
script:
- npm run prettier:lint && npm run demo:build && npm test
- npm run prettier:lint && npm run demo:build:apps && npm test
4 changes: 3 additions & 1 deletion core/sound/accumulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Channel1 } from './channel1';
import { Channel2 } from './channel2';
import { Channel3 } from './channel3';
import { Channel4 } from './channel4';
import { i32Portable } from '../portable/portable';

// Another class simply for accumulating samples
// Default everything to silence
Expand Down Expand Up @@ -94,7 +95,8 @@ export function accumulateSound(numberOfCycles: i32): void {
// Don't allow our audioQueueIndex to overflow into other parts of the wasmBoy memory map
// https://docs.google.com/spreadsheets/d/17xrEzJk5-sCB9J2mMJcVnzhbE-XH_NvczVSQH9OHvRk/edit#gid=0
// Not 0xFFFF because we need half of 64kb since we store left and right channel
if (Sound.audioQueueIndex >= Sound.wasmBoyMemoryMaxBufferSize / 2 - 1) {
let maxIndex: i32 = i32Portable(Sound.wasmBoyMemoryMaxBufferSize / 2) - 1;
if (Sound.audioQueueIndex >= maxIndex) {
Sound.audioQueueIndex -= 1;
}
}
Expand Down
3 changes: 2 additions & 1 deletion core/sound/channel3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
storeBooleanDirectlyToWasmMemory
} from '../memory/index';
import { checkBitOnByte, hexLog } from '../helpers/index';
import { i32Portable } from '../portable/portable';

export class Channel3 {
// Cycle Counter for our sound accumulator
Expand Down Expand Up @@ -182,7 +183,7 @@ export class Channel3 {
let sample: i32 = 0;

// Will Find the position, and knock off any remainder
let positionIndexToAdd: i32 = Channel3.waveTablePosition / 2;
let positionIndexToAdd: i32 = i32Portable(Channel3.waveTablePosition / 2);
let memoryLocationWaveSample: i32 = Channel3.memoryLocationWaveTable + positionIndexToAdd;

sample = eightBitLoadFromGBMemory(memoryLocationWaveSample);
Expand Down
19 changes: 10 additions & 9 deletions core/sound/sound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,10 @@ function calculateSound(numberOfCycles: i32): void {
// All samples will be returned as 0 to 30
// 0 being -1.0, and 30 being 1.0
// (see blurb at top)
let channel1Sample: i32 = Channel1.getSample(numberOfCycles);
let channel2Sample: i32 = Channel2.getSample(numberOfCycles);
let channel3Sample: i32 = Channel3.getSample(numberOfCycles);
let channel4Sample: i32 = Channel4.getSample(numberOfCycles);
let channel1Sample: i32 = i32Portable(Channel1.getSample(numberOfCycles));
let channel2Sample: i32 = i32Portable(Channel2.getSample(numberOfCycles));
let channel3Sample: i32 = i32Portable(Channel3.getSample(numberOfCycles));
let channel4Sample: i32 = i32Portable(Channel4.getSample(numberOfCycles));
// TODO: Allow individual channels to be muted
// let channel1Sample: i32 = 15;
// let channel2Sample: i32 = 15;
Expand Down Expand Up @@ -248,7 +248,8 @@ function calculateSound(numberOfCycles: i32): void {
// Don't allow our audioQueueIndex to overflow into other parts of the wasmBoy memory map
// https://docs.google.com/spreadsheets/d/17xrEzJk5-sCB9J2mMJcVnzhbE-XH_NvczVSQH9OHvRk/edit#gid=0
// Not 0xFFFF because we need half of 64kb since we store left and right channel
if (Sound.audioQueueIndex >= Sound.wasmBoyMemoryMaxBufferSize / 2 - 1) {
let maxIndex: i32 = i32Portable(Sound.wasmBoyMemoryMaxBufferSize / 2) - 1;
if (Sound.audioQueueIndex >= maxIndex) {
Sound.audioQueueIndex -= 1;
}
}
Expand Down Expand Up @@ -429,10 +430,10 @@ function getSampleAsUnsignedByte(sample: i32, mixerVolume: i32): i32 {
convertedSample = convertedSample * precision;

// Multiply by the mixer volume fraction (to find the actual volume)
convertedSample = (convertedSample * mixerVolume) / 8;
convertedSample = i32Portable((convertedSample * mixerVolume) / 8);

// Convert back to scale of 0 to 120
convertedSample = convertedSample / precision;
convertedSample = i32Portable(convertedSample / precision);
convertedSample = convertedSample + 60;

// Finally, convert to an unsigned byte scale
Expand All @@ -442,8 +443,8 @@ function getSampleAsUnsignedByte(sample: i32, mixerVolume: i32): i32 {
// For example, 120 / 254 = 0.47244094488188976
// Multiply by 1000 to increase the float into an int
// so, 120 * 1000 / (0.47244094488188976 * 1000) should give approximate answer for max mixer volume
let maxDivider: i32 = (120 * precision) / 254;
convertedSample = (convertedSample * precision) / maxDivider;
let maxDivider: i32 = i32Portable((120 * precision) / 254);
convertedSample = i32Portable((convertedSample * precision) / maxDivider);

// Ensure we have an i32 and not a float for JS builds
convertedSample = i32Portable(convertedSample);
Expand Down
Binary file modified dist/core/core.untouched.wasm
Binary file not shown.
Loading

0 comments on commit 838af61

Please sign in to comment.