@jamesrock/rockjs 1.31.0 → 1.32.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 +40 -0
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -866,6 +866,46 @@ export class DeckOfPlayingCards {
866
866
  };
867
867
  };
868
868
 
869
+ export class SoundManager {
870
+ constructor(sounds) {
871
+
872
+ this.context = new AudioContext();
873
+ this.sounds = sounds;
874
+ this.buffers = {};
875
+
876
+ };
877
+ async load() {
878
+
879
+ return Promise.all(Object.keys(this.sounds).map((key) => this.loadBuffer(key, this.sounds[key]))).then((items) => {
880
+ items.forEach(([name, buffer]) => {
881
+ this.buffers[name] = buffer;
882
+ });
883
+ });
884
+
885
+ };
886
+ async loadBuffer(name, path) {
887
+
888
+ const response = await fetch(path);
889
+ const arrayBuffer = await response.arrayBuffer();
890
+ const audioBuffer = await this.context.decodeAudioData(arrayBuffer);
891
+ return [name, audioBuffer];
892
+
893
+ };
894
+ play(sound = 'point') {
895
+
896
+ if(!this.buffers[sound]) {
897
+ console.log(`SoundManager: '${sound}' not loaded!`);
898
+ return;
899
+ };
900
+
901
+ const source = this.context.createBufferSource();
902
+ source.buffer = this.buffers[sound];
903
+ source.connect(this.context.destination);
904
+ source.start();
905
+
906
+ };
907
+ };
908
+
869
909
  // temporary alias
870
910
  export const createNode = makeNode;
871
911
  export const createSVGNode = makeSVGNode;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jamesrock/rockjs",
3
- "version": "1.31.0",
3
+ "version": "1.32.0",
4
4
  "description": "utility bliss",
5
5
  "license": "ISC",
6
6
  "author": "James Rock",