@jamesrock/rockjs 1.35.0 → 1.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +37 -2
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -864,13 +864,18 @@ export class SoundManager {
864
864
  this.context = new AudioContext();
865
865
  this.sounds = sounds;
866
866
  this.buffers = {};
867
+ this.mixer = {};
868
+ this.keys = Object.keys(this.sounds);
869
+
870
+ this.listenForStateChange();
867
871
 
868
872
  };
869
873
  async load() {
870
874
 
871
- return Promise.all(Object.keys(this.sounds).map((key) => this.loadBuffer(key, this.sounds[key]))).then((items) => {
875
+ return Promise.all(this.keys.map((key) => this.loadBuffer(key, this.sounds[key]))).then((items) => {
872
876
  items.forEach(([name, buffer]) => {
873
877
  this.buffers[name] = buffer;
878
+ this.mixer[name] = [0.5, 0];
874
879
  });
875
880
  });
876
881
 
@@ -891,11 +896,41 @@ export class SoundManager {
891
896
  };
892
897
 
893
898
  const source = this.context.createBufferSource();
899
+ const gainer = this.context.createGain();
900
+ const panner = this.context.createStereoPanner();
901
+
894
902
  source.buffer = this.buffers[sound];
895
- source.connect(this.context.destination);
903
+ gainer.gain.value = this.mixer[sound][0];
904
+ panner.pan.value = this.mixer[sound][1];
905
+
906
+ source.connect(gainer).connect(panner).connect(this.context.destination);
907
+
896
908
  source.start();
897
909
 
898
910
  };
911
+ volume(sound, value) {
912
+
913
+ this.mixer[sound][0] = value;
914
+ return this;
915
+
916
+ };
917
+ pan(sound, value) {
918
+
919
+ this.mixer[sound][1] = value;
920
+ return this;
921
+
922
+ };
923
+ listenForStateChange() {
924
+
925
+ this.context.addEventListener('statechange', async () => {
926
+ if(this.context.state === 'suspended') {
927
+ await this.context.resume();
928
+ };
929
+ });
930
+
931
+ return this;
932
+
933
+ };
899
934
  };
900
935
 
901
936
  // temporary alias
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jamesrock/rockjs",
3
- "version": "1.35.0",
3
+ "version": "1.36.0",
4
4
  "description": "utility bliss",
5
5
  "license": "ISC",
6
6
  "author": "James Rock",