Skip to content

Commit

Permalink
rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
KilledByAPixel committed Oct 7, 2024
1 parent 6219ece commit 50541e6
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 60 deletions.
59 changes: 39 additions & 20 deletions dist/littlejs.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,23 @@ function debugSaveDataURL(dataURL, filename)
downloadLink.click();
}

/** Show error as full page of red text
* @memberof Debug */
function debugShowErrors()
{
onunhandledrejection = (event)=>showError(event.reason);
onerror = (event, source, lineno, colno)=>
showError(`${event}\n${source}\nLn ${lineno}, Col ${colno}`);

const showError = (message)=>
{
document.body.style.backgroundColor = '#111';
document.body.innerHTML = `<pre style=color:#f00;font-size:50px>` + message;
}
}

///////////////////////////////////////////////////////////////////////////////
// Engine debug function (called automatically)
// Engine debug functions (called automatically)

function debugInit()
{
Expand Down Expand Up @@ -326,7 +341,7 @@ function debugRender()
const pos = worldToScreen(p.pos);
overlayContext.translate(pos.x|0, pos.y|0);
overlayContext.rotate(p.angle);
overlayContext.scale(1, p.text != undefined ? 1 : -1);
overlayContext.scale(1, p.text ? 1 : -1);
overlayContext.fillStyle = overlayContext.strokeStyle = p.color;

if (p.text != undefined)
Expand Down Expand Up @@ -2962,6 +2977,10 @@ function inputInit()
// mouse event handlers
onmousedown = (e)=>
{
// fix stalled audio requiring user interaction
if (soundEnable && !headlessMode && audioContext && audioContext.state != 'running')
audioContext.resume();

isUsingGamepad = false;
inputData[0][e.button] = 3;
mousePosScreen = mouseToScreen(e);
Expand Down Expand Up @@ -3130,8 +3149,8 @@ function touchInputInit()
function handleTouchDefault(e)
{
// fix stalled audio requiring user interaction
if (soundEnable && audioContext && audioContext.state != 'running')
zzfx(0);
if (soundEnable && !headlessMode && audioContext && audioContext.state != 'running')
audioContext.resume();

// check if touching and pass to mouse events
const touching = e.touches.length;
Expand Down Expand Up @@ -3344,6 +3363,7 @@ class Sound
// generate zzfx sound now for fast playback
const defaultRandomness = .05;
this.randomness = zzfxSound[1] || defaultRandomness;
zzfxSound[1] = 0; // generate without randomness
this.sampleChannels = [zzfxG(...zzfxSound)];
this.sampleRate = zzfxR;
}
Expand Down Expand Up @@ -3576,10 +3596,6 @@ function getNoteFrequency(semitoneOffset, rootFrequency=220)

///////////////////////////////////////////////////////////////////////////////

// internal tracking if audio was suspended when last sound was played
// allows first suspended sound to play when audio is resumed
let audioSuspended = false;

/** Play cached audio samples with given settings
* @param {Array} sampleChannels - Array of arrays of samples to play (for stereo playback)
* @param {Number} [volume] - How much to scale volume by
Expand All @@ -3595,20 +3611,21 @@ function playSamples(sampleChannels, volume=1, rate=1, pan=0, loop=false, sample
if (!soundEnable || headlessMode) return;

// prevent sounds from building up if they can't be played
const audioWasSuspended = audioSuspended;
if (audioSuspended = audioContext.state != 'running')
if (audioContext.state != 'running')
{
// fix stalled audio
audioContext.resume();
audioContext.resume().then(()=>
playSamples(sampleChannels, volume, rate, pan, loop, sampleRate, gainNode));

// prevent suspended sounds from building up
if (audioWasSuspended)
return;
return;
}

// create buffer and source
const buffer = audioContext.createBuffer(sampleChannels.length, sampleChannels[0].length, sampleRate),
source = audioContext.createBufferSource();
const channelCount = sampleChannels.length;
const sampleLength = sampleChannels[0].length;
const buffer = audioContext.createBuffer(channelCount, sampleLength, sampleRate);
const source = audioContext.createBufferSource();

// copy samples to buffer and setup source
sampleChannels.forEach((c,i)=> buffer.getChannelData(i).set(c));
Expand All @@ -3622,7 +3639,8 @@ function playSamples(sampleChannels, volume=1, rate=1, pan=0, loop=false, sample
gainNode.connect(audioGainNode);

// connect source to stereo panner and gain
source.connect(new StereoPannerNode(audioContext, {'pan':clamp(pan, -1, 1)})).connect(gainNode);
const pannerNode = new StereoPannerNode(audioContext, {'pan':clamp(pan, -1, 1)});
source.connect(pannerNode).connect(gainNode);

// play and return sound
source.start();
Expand All @@ -3638,7 +3656,7 @@ function playSamples(sampleChannels, volume=1, rate=1, pan=0, loop=false, sample
* @param {Array} zzfxSound - Array of ZzFX parameters, ex. [.5,.5]
* @return {AudioBufferSourceNode} - The audio node of the sound played
* @memberof Audio */
function zzfx(...zzfxSound) { return new Sound(zzfxSound).play(); }
function zzfx(...zzfxSound) { return playSamples([zzfxG(...zzfxSound)]); }

/** Sample rate used for all ZzFX sounds
* @default 44100
Expand All @@ -3647,7 +3665,7 @@ const zzfxR = 44100;

/** Generate samples for a ZzFX sound
* @param {Number} [volume] - Volume scale (percent)
* @param {Number} [randomness] - Unused in this fuction, handled by Sound class
* @param {Number} [randomness] - How much to randomize frequency (percent Hz)
* @param {Number} [frequency] - Frequency of sound (Hz)
* @param {Number} [attack] - Attack time, how fast sound starts (seconds)
* @param {Number} [sustain] - Sustain time, how long sound holds (seconds)
Expand All @@ -3673,7 +3691,7 @@ const zzfxR = 44100;
function zzfxG
(
// parameters
volume = 1, randomness = 0, frequency = 220, attack = 0, sustain = 0,
volume = 1, randomness = .05, frequency = 220, attack = 0, sustain = 0,
release = .1, shape = 0, shapeCurve = 1, slide = 0, deltaSlide = 0,
pitchJump = 0, pitchJumpTime = 0, repeatTime = 0, noise = 0, modulation = 0,
bitCrush = 0, delay = 0, sustainVolume = 1, decay = 0, tremolo = 0, filter = 0
Expand All @@ -3684,7 +3702,8 @@ function zzfxG
// init parameters
let PI2 = PI*2, sampleRate = zzfxR,
startSlide = slide *= 500 * PI2 / sampleRate / sampleRate,
startFrequency = frequency *= PI2 / sampleRate,
startFrequency = frequency *=
rand(1 + randomness, 1-randomness) * PI2 / sampleRate,
b = [], t = 0, tm = 0, i = 0, j = 1, r = 0, c = 0, s = 0, f, length,

// biquad LP/HP filter
Expand Down
2 changes: 1 addition & 1 deletion dist/littlejs.esm.min.js

Large diffs are not rendered by default.

59 changes: 39 additions & 20 deletions dist/littlejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,23 @@ function debugSaveDataURL(dataURL, filename)
downloadLink.click();
}

/** Show error as full page of red text
* @memberof Debug */
function debugShowErrors()
{
onunhandledrejection = (event)=>showError(event.reason);
onerror = (event, source, lineno, colno)=>
showError(`${event}\n${source}\nLn ${lineno}, Col ${colno}`);

const showError = (message)=>
{
document.body.style.backgroundColor = '#111';
document.body.innerHTML = `<pre style=color:#f00;font-size:50px>` + message;
}
}

///////////////////////////////////////////////////////////////////////////////
// Engine debug function (called automatically)
// Engine debug functions (called automatically)

function debugInit()
{
Expand Down Expand Up @@ -326,7 +341,7 @@ function debugRender()
const pos = worldToScreen(p.pos);
overlayContext.translate(pos.x|0, pos.y|0);
overlayContext.rotate(p.angle);
overlayContext.scale(1, p.text != undefined ? 1 : -1);
overlayContext.scale(1, p.text ? 1 : -1);
overlayContext.fillStyle = overlayContext.strokeStyle = p.color;

if (p.text != undefined)
Expand Down Expand Up @@ -2962,6 +2977,10 @@ function inputInit()
// mouse event handlers
onmousedown = (e)=>
{
// fix stalled audio requiring user interaction
if (soundEnable && !headlessMode && audioContext && audioContext.state != 'running')
audioContext.resume();

isUsingGamepad = false;
inputData[0][e.button] = 3;
mousePosScreen = mouseToScreen(e);
Expand Down Expand Up @@ -3130,8 +3149,8 @@ function touchInputInit()
function handleTouchDefault(e)
{
// fix stalled audio requiring user interaction
if (soundEnable && audioContext && audioContext.state != 'running')
zzfx(0);
if (soundEnable && !headlessMode && audioContext && audioContext.state != 'running')
audioContext.resume();

// check if touching and pass to mouse events
const touching = e.touches.length;
Expand Down Expand Up @@ -3344,6 +3363,7 @@ class Sound
// generate zzfx sound now for fast playback
const defaultRandomness = .05;
this.randomness = zzfxSound[1] || defaultRandomness;
zzfxSound[1] = 0; // generate without randomness
this.sampleChannels = [zzfxG(...zzfxSound)];
this.sampleRate = zzfxR;
}
Expand Down Expand Up @@ -3576,10 +3596,6 @@ function getNoteFrequency(semitoneOffset, rootFrequency=220)

///////////////////////////////////////////////////////////////////////////////

// internal tracking if audio was suspended when last sound was played
// allows first suspended sound to play when audio is resumed
let audioSuspended = false;

/** Play cached audio samples with given settings
* @param {Array} sampleChannels - Array of arrays of samples to play (for stereo playback)
* @param {Number} [volume] - How much to scale volume by
Expand All @@ -3595,20 +3611,21 @@ function playSamples(sampleChannels, volume=1, rate=1, pan=0, loop=false, sample
if (!soundEnable || headlessMode) return;

// prevent sounds from building up if they can't be played
const audioWasSuspended = audioSuspended;
if (audioSuspended = audioContext.state != 'running')
if (audioContext.state != 'running')
{
// fix stalled audio
audioContext.resume();
audioContext.resume().then(()=>
playSamples(sampleChannels, volume, rate, pan, loop, sampleRate, gainNode));

// prevent suspended sounds from building up
if (audioWasSuspended)
return;
return;
}

// create buffer and source
const buffer = audioContext.createBuffer(sampleChannels.length, sampleChannels[0].length, sampleRate),
source = audioContext.createBufferSource();
const channelCount = sampleChannels.length;
const sampleLength = sampleChannels[0].length;
const buffer = audioContext.createBuffer(channelCount, sampleLength, sampleRate);
const source = audioContext.createBufferSource();

// copy samples to buffer and setup source
sampleChannels.forEach((c,i)=> buffer.getChannelData(i).set(c));
Expand All @@ -3622,7 +3639,8 @@ function playSamples(sampleChannels, volume=1, rate=1, pan=0, loop=false, sample
gainNode.connect(audioGainNode);

// connect source to stereo panner and gain
source.connect(new StereoPannerNode(audioContext, {'pan':clamp(pan, -1, 1)})).connect(gainNode);
const pannerNode = new StereoPannerNode(audioContext, {'pan':clamp(pan, -1, 1)});
source.connect(pannerNode).connect(gainNode);

// play and return sound
source.start();
Expand All @@ -3638,7 +3656,7 @@ function playSamples(sampleChannels, volume=1, rate=1, pan=0, loop=false, sample
* @param {Array} zzfxSound - Array of ZzFX parameters, ex. [.5,.5]
* @return {AudioBufferSourceNode} - The audio node of the sound played
* @memberof Audio */
function zzfx(...zzfxSound) { return new Sound(zzfxSound).play(); }
function zzfx(...zzfxSound) { return playSamples([zzfxG(...zzfxSound)]); }

/** Sample rate used for all ZzFX sounds
* @default 44100
Expand All @@ -3647,7 +3665,7 @@ const zzfxR = 44100;

/** Generate samples for a ZzFX sound
* @param {Number} [volume] - Volume scale (percent)
* @param {Number} [randomness] - Unused in this fuction, handled by Sound class
* @param {Number} [randomness] - How much to randomize frequency (percent Hz)
* @param {Number} [frequency] - Frequency of sound (Hz)
* @param {Number} [attack] - Attack time, how fast sound starts (seconds)
* @param {Number} [sustain] - Sustain time, how long sound holds (seconds)
Expand All @@ -3673,7 +3691,7 @@ const zzfxR = 44100;
function zzfxG
(
// parameters
volume = 1, randomness = 0, frequency = 220, attack = 0, sustain = 0,
volume = 1, randomness = .05, frequency = 220, attack = 0, sustain = 0,
release = .1, shape = 0, shapeCurve = 1, slide = 0, deltaSlide = 0,
pitchJump = 0, pitchJumpTime = 0, repeatTime = 0, noise = 0, modulation = 0,
bitCrush = 0, delay = 0, sustainVolume = 1, decay = 0, tremolo = 0, filter = 0
Expand All @@ -3684,7 +3702,8 @@ function zzfxG
// init parameters
let PI2 = PI*2, sampleRate = zzfxR,
startSlide = slide *= 500 * PI2 / sampleRate / sampleRate,
startFrequency = frequency *= PI2 / sampleRate,
startFrequency = frequency *=
rand(1 + randomness, 1-randomness) * PI2 / sampleRate,
b = [], t = 0, tm = 0, i = 0, j = 1, r = 0, c = 0, s = 0, f, length,

// biquad LP/HP filter
Expand Down
2 changes: 1 addition & 1 deletion dist/littlejs.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 50541e6

Please sign in to comment.