@jamesrock/rockjs 1.35.0 → 1.37.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 +83 -3
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -412,6 +412,18 @@ export class DisplayObject {
412
412
  return this;
413
413
 
414
414
  };
415
+ on(event, handler, passive = true) {
416
+
417
+ this.node.addEventListener(event, handler, {passive});
418
+ return this;
419
+
420
+ };
421
+ once(event, handler, passive = true) {
422
+
423
+ this.node.addEventListener(event, handler, {passive, once: true});
424
+ return this;
425
+
426
+ };
415
427
  dispatchEvent(event) {
416
428
 
417
429
  this.node.dispatchEvent(new Event(event));
@@ -864,13 +876,18 @@ export class SoundManager {
864
876
  this.context = new AudioContext();
865
877
  this.sounds = sounds;
866
878
  this.buffers = {};
879
+ this.mixer = {};
880
+ this.keys = Object.keys(this.sounds);
881
+
882
+ this.listenForStateChange();
867
883
 
868
884
  };
869
885
  async load() {
870
886
 
871
- return Promise.all(Object.keys(this.sounds).map((key) => this.loadBuffer(key, this.sounds[key]))).then((items) => {
887
+ return Promise.all(this.keys.map((key) => this.loadBuffer(key, this.sounds[key]))).then((items) => {
872
888
  items.forEach(([name, buffer]) => {
873
889
  this.buffers[name] = buffer;
890
+ this.mixer[name] = [0.5, 0];
874
891
  });
875
892
  });
876
893
 
@@ -887,14 +904,77 @@ export class SoundManager {
887
904
 
888
905
  if(!this.buffers[sound]) {
889
906
  console.log(`SoundManager: '${sound}' not loaded!`);
890
- return;
907
+ return this;
891
908
  };
892
909
 
893
910
  const source = this.context.createBufferSource();
911
+ const gainer = this.context.createGain();
912
+ const panner = this.context.createStereoPanner();
913
+
894
914
  source.buffer = this.buffers[sound];
895
- source.connect(this.context.destination);
915
+ gainer.gain.value = this.mixer[sound][0];
916
+ panner.pan.value = this.mixer[sound][1];
917
+
918
+ source.connect(gainer).connect(panner).connect(this.context.destination);
919
+
896
920
  source.start();
897
921
 
922
+ return this;
923
+
924
+ };
925
+ volume(sound, value) {
926
+
927
+ this.mixer[sound][0] = value;
928
+ return this;
929
+
930
+ };
931
+ pan(sound, value) {
932
+
933
+ this.mixer[sound][1] = value;
934
+ return this;
935
+
936
+ };
937
+ listenForStateChange() {
938
+
939
+ this.context.addEventListener('statechange', async () => {
940
+ if(this.context.state === 'suspended') {
941
+ await this.context.resume();
942
+ };
943
+ });
944
+
945
+ return this;
946
+
947
+ };
948
+ };
949
+
950
+ export class Tempo {
951
+ constructor() {
952
+
953
+ this.taps = [];
954
+
955
+ };
956
+ tap(base = 120) {
957
+
958
+ const now = performance.now();
959
+ this.taps.push(now);
960
+
961
+ if(this.taps.length > 1 && now - this.taps[this.taps.length - 2] > 2000) {
962
+ taps = [now];
963
+ return base;
964
+ };
965
+
966
+ if(this.taps.length < 2) return base;
967
+ if(this.taps.length > 10) this.taps.shift();
968
+
969
+ const intervals = [];
970
+ for(let i = 1; i < this.taps.length; i++) {
971
+ intervals.push(this.taps[i] - this.taps[i - 1]);
972
+ };
973
+
974
+ const averageInterval = intervals.reduce((a, b) => a + b) / intervals.length;
975
+
976
+ return floorTo(60000 / averageInterval);
977
+
898
978
  };
899
979
  };
900
980
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jamesrock/rockjs",
3
- "version": "1.35.0",
3
+ "version": "1.37.0",
4
4
  "description": "utility bliss",
5
5
  "license": "ISC",
6
6
  "author": "James Rock",