Skip to content

Commit

Permalink
stereo panning
Browse files Browse the repository at this point in the history
  • Loading branch information
KilledByAPixel authored Jun 17, 2020
1 parent 46983ec commit ef9ec8e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
13 changes: 9 additions & 4 deletions ZzFX.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,30 @@ constructor()
this.samples = 0; // last played samples
}

Play(sound)
Play(sound, pan=0)
{
// check if sound object was passed in
const params = sound && typeof sound == 'object' ?
this.SoundToArray(sound) : arguments;

// build samples and start sound
const samples = this.BuildSamples(...params);
return this.PlaySamples(samples);
return this.PlaySamples(samples, pan);
}

PlaySamples(samples)
PlaySamples(samples, pan=0)
{
// create streo panner
const panner = this.x.createStereoPanner();
panner.pan.value = pan;
panner.connect(this.x.destination);

// play an array of audio samples
const buffer = this.x.createBuffer(1, samples.length, this.sampleRate);
const source = this.x.createBufferSource();
buffer.getChannelData(0).set(samples);
source.buffer = buffer;
source.connect(this.x.destination);
source.connect(panner);
source.start();
this.samples = samples;
return source;
Expand Down
2 changes: 1 addition & 1 deletion ZzFX.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
<td style=text-align:center>
<div id=div_masterVolume></div>
<input id=slider_masterVolume title='Volume to scale all sounds by in percent' type=range min=0 max=100 value=30 style=width:300px oninput=UpdateSettings();SaveLocalStorage()>
<div id=div_masterPanning></div>
<input id=slider_masterPanning title='Stero panning from -1 to 1' type=range min=-1 max=1 value=0 step=.1 style=width:300px oninput=UpdateSettings();SaveLocalStorage()>
</td></tr>
<tr>
<td colspan=2>
Expand Down Expand Up @@ -138,14 +140,15 @@
ZzFX © <a href=http://www.frankforce.com target=_blank>Frank Force</a> 2019 ☮♥☻␌
<a hidden id=a_downloadLink></a>
<input hidden id=input_importFile type=file accept=.txt>
<script src=ZzFX.js?915></script>
<script src=ZzFX.js?916></script>
<script>

'use strict'; // strict mode
let sounds = [];
let generatedSoundCount = 0;
let loadedSound;
let lastPlayedSound;
let stereoPanning = 0;

const SeededRandom=_=>
(
Expand Down Expand Up @@ -269,7 +272,7 @@
const sound = BuildSoundFromSettings();
if (ZZFX.volume > 0)
{
lastPlayedSound = ZZFX.Play(sound);
lastPlayedSound = ZZFX.Play(sound, stereoPanning);
DrawSoundWave(ZZFX.samples, ZZFX.volume, sound);
}
else
Expand Down Expand Up @@ -306,6 +309,10 @@
div_masterVolume.innerHTML = 'Master Volume ' + v + '%';
ZZFX.volume = v / 100;

const p = slider_masterPanning.value;
div_masterPanning.innerHTML = 'Stereo Panning ' + p;
stereoPanning = p;

const sound = GetSelectedSound();
if (sound)
{
Expand Down Expand Up @@ -780,6 +787,7 @@
if (!confirm('Are you sure you want clear all sounds?\nThis will DELETE your favorites!'))
return;
slider_masterVolume.value = 30;
slider_masterPanning.value = 0;
input_shortenCode.checked = 1;
}
ClearSounds(clearFavorites);
Expand Down Expand Up @@ -1024,7 +1032,8 @@
{
sounds,
generatedSoundCount,
volume:slider_masterVolume.value
volume:slider_masterVolume.value,
panning:slider_masterPanning.value
}

return JSON.stringify(data);
Expand All @@ -1041,6 +1050,8 @@
const data = JSON.parse(dataJSON);
if (data.volume)
slider_masterVolume.value = data.volume;
if (data.panning)
slider_masterPanning.value = data.panning;
if (data.generatedSoundCount)
generatedSoundCount = data.generatedSoundCount;
if (data.sounds)
Expand Down

0 comments on commit ef9ec8e

Please sign in to comment.