@jamesrock/rockjs 1.31.0 → 1.33.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 +50 -7
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -675,27 +675,30 @@ export class Time {
675
675
  getMillisecondsInDay(ms) {
676
676
  return (ms % this.day);
677
677
  };
678
+ getMillisecondsInYear(ms) {
679
+ return (ms % this.year);
680
+ };
678
681
  getSecondsInDay(ms) {
679
- return (this.getSeconds(ms) % this.day);
682
+ return this.getSeconds(this.getMillisecondsInDay(ms));
680
683
  };
681
684
  getMinutesInDay(ms) {
682
- return (this.getMinutes(ms) % this.day);
685
+ return this.getMinutes(this.getMillisecondsInDay(ms));
683
686
  };
684
687
  getHoursInDay(ms) {
685
- return (this.getHours(ms) % this.day);
688
+ return this.getHours(this.getMillisecondsInDay(ms));
686
689
  };
687
690
  getDaysInYear(ms) {
688
- return (this.getDays(ms) % this.year);
691
+ return this.getDays(this.getMillisecondsInYear(ms));
689
692
  };
690
693
  getMinutesInHour(ms) {
691
- return floorTo(this.getMillisecondsInHour(ms) / this.minute);
694
+ return this.getMinutes(this.getMillisecondsInHour(ms));
692
695
  };
693
696
  getSecondsInMinute(ms) {
694
- return floorTo(this.getMillisecondsInMinute(ms) / this.second);
697
+ return this.getSeconds(this.getMillisecondsInMinute(ms));
695
698
  };
696
699
  formatSeconds = (ms) => pad(this.getSecondsInMinute(ms));
697
700
  formatMinutes = (ms) => pad(this.getMinutesInHour(ms));
698
- formatHours = (ms) => pad(this.getHours(ms));
701
+ formatHours = (ms) => pad(this.getHoursInDay(ms));
699
702
  format = (ms) => `${this.formatMinutes(ms)}:${this.formatSeconds(ms)}`;
700
703
  };
701
704
 
@@ -866,6 +869,46 @@ export class DeckOfPlayingCards {
866
869
  };
867
870
  };
868
871
 
872
+ export class SoundManager {
873
+ constructor(sounds) {
874
+
875
+ this.context = new AudioContext();
876
+ this.sounds = sounds;
877
+ this.buffers = {};
878
+
879
+ };
880
+ async load() {
881
+
882
+ return Promise.all(Object.keys(this.sounds).map((key) => this.loadBuffer(key, this.sounds[key]))).then((items) => {
883
+ items.forEach(([name, buffer]) => {
884
+ this.buffers[name] = buffer;
885
+ });
886
+ });
887
+
888
+ };
889
+ async loadBuffer(name, path) {
890
+
891
+ const response = await fetch(path);
892
+ const arrayBuffer = await response.arrayBuffer();
893
+ const audioBuffer = await this.context.decodeAudioData(arrayBuffer);
894
+ return [name, audioBuffer];
895
+
896
+ };
897
+ play(sound = 'point') {
898
+
899
+ if(!this.buffers[sound]) {
900
+ console.log(`SoundManager: '${sound}' not loaded!`);
901
+ return;
902
+ };
903
+
904
+ const source = this.context.createBufferSource();
905
+ source.buffer = this.buffers[sound];
906
+ source.connect(this.context.destination);
907
+ source.start();
908
+
909
+ };
910
+ };
911
+
869
912
  // temporary alias
870
913
  export const createNode = makeNode;
871
914
  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.33.0",
4
4
  "description": "utility bliss",
5
5
  "license": "ISC",
6
6
  "author": "James Rock",