clicktone 1.3.0 → 1.4.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.
package/README.md CHANGED
@@ -9,8 +9,8 @@
9
9
 
10
10
  </div>
11
11
 
12
- <p align="center">A simple class that provides a convenient abstraction for handling<br>
13
- audio playback by web applications, with support for iOS devices.</p>
12
+ <p align="center">A simple class that provides a convenient abstraction for handling audio playback by web applications, with support for iOS devices.</p>
13
+ <p align="center"><sup>800B gzipped</sup></p>
14
14
  <p align="center"><a href="https://codepen.io/ux-ui/pen/yLwbmMr">Demo</a></p>
15
15
  <br>
16
16
 
package/dist/index.js CHANGED
@@ -14,25 +14,33 @@ $parcel$export(module.exports, "default", () => $5b968dfbc764795f$export$2e2bcd8
14
14
  * @typedef {Object} window
15
15
  * @property {typeof AudioContext} webkitAudioContext
16
16
  */ class $5b968dfbc764795f$var$ClickTone {
17
+ #file;
18
+ #volume;
19
+ #callback;
20
+ #throttle;
21
+ #debug;
22
+ #lastClickTime;
23
+ #audioCache;
24
+ #audioContext;
17
25
  constructor({ file: file, volume: volume = 1.0, callback: callback = null, throttle: throttle = 0, debug: debug = false }){
18
- this.file = file;
19
- this.volume = volume;
20
- this.callback = callback;
21
- this.throttle = throttle;
22
- this.debug = debug;
23
- this.lastClickTime = 0;
24
- this.audioCache = {};
26
+ this.#file = file;
27
+ this.#volume = volume;
28
+ this.#callback = callback;
29
+ this.#throttle = throttle;
30
+ this.#debug = debug;
31
+ this.#lastClickTime = 0;
32
+ this.#audioCache = {};
25
33
  }
26
- initAudioContext() {
27
- if (!this.audioContext) {
28
- this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
29
- this.iOSFixAudioContext();
34
+ #initAudioContext() {
35
+ if (!this.#audioContext) {
36
+ this.#audioContext = new (window.AudioContext || window.webkitAudioContext)();
37
+ this.#iOSFixAudioContext();
30
38
  }
31
39
  }
32
- iOSFixAudioContext = ()=>{
33
- if (this.audioContext && this.audioContext.state === "suspended" && "ontouchstart" in window) {
40
+ #iOSFixAudioContext = ()=>{
41
+ if (this.#audioContext && this.#audioContext.state === "suspended" && "ontouchstart" in window) {
34
42
  const unlock = ()=>{
35
- if (this.audioContext.state === "suspended") this.audioContext.resume().then(()=>{
43
+ if (this.#audioContext.state === "suspended") this.#audioContext.resume().then(()=>{
36
44
  document.body.removeEventListener("touchstart", unlock);
37
45
  document.body.removeEventListener("touchend", unlock);
38
46
  });
@@ -41,52 +49,52 @@ $parcel$export(module.exports, "default", () => $5b968dfbc764795f$export$2e2bcd8
41
49
  document.body.addEventListener("touchend", unlock, false);
42
50
  }
43
51
  };
44
- fetchAndDecodeAudio = async (url)=>{
52
+ #fetchAndDecodeAudio = async (url)=>{
45
53
  try {
46
- if (this.audioCache[url]) return this.audioCache[url];
54
+ if (this.#audioCache[url]) return this.#audioCache[url];
47
55
  const response = await fetch(url);
48
56
  const buffer = await response.arrayBuffer();
49
- const audioData = await this.audioContext.decodeAudioData(buffer);
50
- this.audioCache[url] = audioData;
57
+ const audioData = await this.#audioContext.decodeAudioData(buffer);
58
+ this.#audioCache[url] = audioData;
51
59
  return audioData;
52
60
  } catch (error) {
53
- if (this.debug) console.error("Audio loading and decoding error: ", error);
61
+ if (this.#debug) console.error("Audio loading and decoding error: ", error);
54
62
  throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);
55
63
  }
56
64
  };
57
- audio = async (url)=>{
58
- this.initAudioContext();
65
+ #audio = async (url)=>{
66
+ this.#initAudioContext();
59
67
  try {
60
- const audioData = await this.fetchAndDecodeAudio(url);
61
- const source = this.audioContext.createBufferSource();
62
- const gainNode = this.audioContext.createGain();
68
+ const audioData = await this.#fetchAndDecodeAudio(url);
69
+ const source = this.#audioContext.createBufferSource();
70
+ const gainNode = this.#audioContext.createGain();
63
71
  source.buffer = audioData;
64
- gainNode.gain.value = this.volume;
72
+ gainNode.gain.value = this.#volume;
65
73
  source.connect(gainNode);
66
- gainNode.connect(this.audioContext.destination);
74
+ gainNode.connect(this.#audioContext.destination);
67
75
  source.onended = ()=>{
68
- if (this.callback) this.callback?.();
76
+ if (this.#callback) this.#callback?.();
69
77
  };
70
78
  source.start(0);
71
79
  } catch (error) {
72
- if (this.debug) console.error("Audio playback error: ", error);
80
+ if (this.#debug) console.error("Audio playback error: ", error);
73
81
  throw new Error(`Something went wrong while playing audio: ${error.message}`);
74
82
  }
75
83
  };
76
- throttleFn = (func)=>()=>{
84
+ #throttleFn = (func)=>()=>{
77
85
  const now = Date.now();
78
- if (now - this.lastClickTime >= this.throttle) {
86
+ if (now - this.#lastClickTime >= this.#throttle) {
79
87
  func();
80
- this.lastClickTime = now;
88
+ this.#lastClickTime = now;
81
89
  }
82
90
  };
83
- play = async (url = this.file)=>{
84
- const throttledPlay = this.throttleFn(()=>this.audio(url));
91
+ play = async (url = this.#file)=>{
92
+ const throttledPlay = this.#throttleFn(()=>this.#audio(url));
85
93
  try {
86
94
  await throttledPlay();
87
95
  } catch (error) {
88
- if (this.debug) console.error("Audio playback error: ", error);
89
- if (this.callback) this.callback?.(error);
96
+ if (this.#debug) console.error("Audio playback error: ", error);
97
+ if (this.#callback) this.#callback?.(error);
90
98
  else throw error;
91
99
  }
92
100
  };
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;;;;;AAAA;;;CAGC,GAED,MAAM;IACJ,YAAY,QACV,IAAI,UACJ,SAAS,eACT,WAAW,gBACX,WAAW,UACX,QAAQ,OACT,CAAE;QACD,IAAI,CAAC,IAAI,GAAG;QACZ,IAAI,CAAC,MAAM,GAAG;QACd,IAAI,CAAC,QAAQ,GAAG;QAChB,IAAI,CAAC,QAAQ,GAAG;QAChB,IAAI,CAAC,KAAK,GAAG;QACb,IAAI,CAAC,aAAa,GAAG;QACrB,IAAI,CAAC,UAAU,GAAG,CAAC;IACrB;IAEA,mBAAmB;QACjB,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE;YACtB,IAAI,CAAC,YAAY,GAAG,IAAK,CA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AAM,GAAG;YAChB,SAAS,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,MAAM;YACjC,OAAO,OAAO,CAAC;YACf,SAAS,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,WAAW;YAE9C,OAAO,OAAO,GAAG;gBACf,IAAI,IAAI,CAAC,QAAQ,EACf,IAAI,CAAC,QAAQ;YAEjB;YAEA,OAAO,KAAK,CAAC;QACf,EAAE,OAAO,OAAO;YACd,IAAI,IAAI,CAAC,KAAK,EACZ,QAAQ,KAAK,CAAC,0BAA0B;YAG1C,MAAM,IAAI,MAAM,CAAC,0CAA0C,EAAE,MAAM,OAAO,CAAC,CAAC;QAC9E;IACF,EAAE;IAEF,aAAa,CAAC,OAAS;YACrB,MAAM,MAAM,KAAK,GAAG;YAEpB,IAAI,MAAM,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,QAAQ,EAAE;gBAC7C;gBAEA,IAAI,CAAC,aAAa,GAAG;YACvB;QACF,EAAE;IAEF,OAAO,OAAO,MAAM,IAAI,CAAC,IAAI;QAC3B,MAAM,gBAAgB,IAAI,CAAC,UAAU,CAAC,IAAM,IAAI,CAAC,KAAK,CAAC;QAEvD,IAAI;YACF,MAAM;QACR,EAAE,OAAO,OAAO;YACd,IAAI,IAAI,CAAC,KAAK,EACZ,QAAQ,KAAK,CAAC,0BAA0B;YAG1C,IAAI,IAAI,CAAC,QAAQ,EACf,IAAI,CAAC,QAAQ,GAAG;iBAEhB,MAAM;QAEV;IACF,EAAE;AACJ;IAEA,2CAAe","sources":["src/ClickTone.js"],"sourcesContent":["/**\n * @typedef {Object} window\n * @property {typeof AudioContext} webkitAudioContext\n */\n\nclass ClickTone {\n constructor({\n file,\n volume = 1.0,\n callback = null,\n throttle = 0,\n debug = false,\n }) {\n this.file = file;\n this.volume = volume;\n this.callback = callback;\n this.throttle = throttle;\n this.debug = debug;\n this.lastClickTime = 0;\n this.audioCache = {};\n }\n\n initAudioContext() {\n if (!this.audioContext) {\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n }\n\n iOSFixAudioContext = () => {\n if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = () => {\n if (this.audioContext.state === 'suspended') {\n this.audioContext.resume().then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n });\n }\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n };\n\n fetchAndDecodeAudio = async (url) => {\n try {\n if (this.audioCache[url]) {\n return this.audioCache[url];\n }\n\n const response = await fetch(url);\n const buffer = await response.arrayBuffer();\n const audioData = await this.audioContext.decodeAudioData(buffer);\n\n this.audioCache[url] = audioData;\n\n return audioData;\n } catch (error) {\n if (this.debug) {\n console.error('Audio loading and decoding error: ', error);\n }\n\n throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);\n }\n };\n\n audio = async (url) => {\n this.initAudioContext();\n\n try {\n const audioData = await this.fetchAndDecodeAudio(url);\n const source = this.audioContext.createBufferSource();\n const gainNode = this.audioContext.createGain();\n\n source.buffer = audioData;\n gainNode.gain.value = this.volume;\n source.connect(gainNode);\n gainNode.connect(this.audioContext.destination);\n\n source.onended = () => {\n if (this.callback) {\n this.callback?.();\n }\n };\n\n source.start(0);\n } catch (error) {\n if (this.debug) {\n console.error('Audio playback error: ', error);\n }\n\n throw new Error(`Something went wrong while playing audio: ${error.message}`);\n }\n };\n\n throttleFn = (func) => () => {\n const now = Date.now();\n\n if (now - this.lastClickTime >= this.throttle) {\n func();\n\n this.lastClickTime = now;\n }\n };\n\n play = async (url = this.file) => {\n const throttledPlay = this.throttleFn(() => this.audio(url));\n\n try {\n await throttledPlay();\n } catch (error) {\n if (this.debug) {\n console.error('Audio playback error: ', error);\n }\n\n if (this.callback) {\n this.callback?.(error);\n } else {\n throw error;\n }\n }\n };\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.js.map"}
1
+ {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M,OAAO,CAAC,CAAC;QAC9E;IACF,EAAE;IAEF,CAAC,UAAU,GAAG,CAAC,OAAS;YACtB,MAAM,MAAM,KAAK,GAAG;YAEpB,IAAI,MAAM,IAAI,CAAC,CAAC,aAAa,IAAI,IAAI,CAAC,CAAC,QAAQ,EAAE;gBAC/C;gBAEA,IAAI,CAAC,CAAC,aAAa,GAAG;YACxB;QACF,EAAE;IAEF,OAAO,OAAO,MAAM,IAAI,CAAC,CAAC,IAAI;QAC5B,MAAM,gBAAgB,IAAI,CAAC,CAAC,UAAU,CAAC,IAAM,IAAI,CAAC,CAAC,KAAK,CAAC;QAEzD,IAAI;YACF,MAAM;QACR,EAAE,OAAO,OAAO;YACd,IAAI,IAAI,CAAC,CAAC,KAAK,EACb,QAAQ,KAAK,CAAC,0BAA0B;YAG1C,IAAI,IAAI,CAAC,CAAC,QAAQ,EAChB,IAAI,CAAC,CAAC,QAAQ,GAAG;iBAEjB,MAAM;QAEV;IACF,EAAE;AACJ;IAEA,2CAAe","sources":["src/ClickTone.js"],"sourcesContent":["/**\n * @typedef {Object} window\n * @property {typeof AudioContext} webkitAudioContext\n */\n\nclass ClickTone {\n #file;\n\n #volume;\n\n #callback;\n\n #throttle;\n\n #debug;\n\n #lastClickTime;\n\n #audioCache;\n\n #audioContext;\n\n constructor({\n file,\n volume = 1.0,\n callback = null,\n throttle = 0,\n debug = false,\n }) {\n this.#file = file;\n this.#volume = volume;\n this.#callback = callback;\n this.#throttle = throttle;\n this.#debug = debug;\n this.#lastClickTime = 0;\n this.#audioCache = {};\n }\n\n #initAudioContext() {\n if (!this.#audioContext) {\n this.#audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.#iOSFixAudioContext();\n }\n }\n\n #iOSFixAudioContext = () => {\n if (this.#audioContext && this.#audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = () => {\n if (this.#audioContext.state === 'suspended') {\n this.#audioContext.resume().then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n });\n }\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n };\n\n #fetchAndDecodeAudio = async (url) => {\n try {\n if (this.#audioCache[url]) {\n return this.#audioCache[url];\n }\n\n const response = await fetch(url);\n const buffer = await response.arrayBuffer();\n const audioData = await this.#audioContext.decodeAudioData(buffer);\n\n this.#audioCache[url] = audioData;\n\n return audioData;\n } catch (error) {\n if (this.#debug) {\n console.error('Audio loading and decoding error: ', error);\n }\n\n throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);\n }\n };\n\n #audio = async (url) => {\n this.#initAudioContext();\n\n try {\n const audioData = await this.#fetchAndDecodeAudio(url);\n const source = this.#audioContext.createBufferSource();\n const gainNode = this.#audioContext.createGain();\n\n source.buffer = audioData;\n gainNode.gain.value = this.#volume;\n source.connect(gainNode);\n gainNode.connect(this.#audioContext.destination);\n\n source.onended = () => {\n if (this.#callback) {\n this.#callback?.();\n }\n };\n\n source.start(0);\n } catch (error) {\n if (this.#debug) {\n console.error('Audio playback error: ', error);\n }\n\n throw new Error(`Something went wrong while playing audio: ${error.message}`);\n }\n };\n\n #throttleFn = (func) => () => {\n const now = Date.now();\n\n if (now - this.#lastClickTime >= this.#throttle) {\n func();\n\n this.#lastClickTime = now;\n }\n };\n\n play = async (url = this.#file) => {\n const throttledPlay = this.#throttleFn(() => this.#audio(url));\n\n try {\n await throttledPlay();\n } catch (error) {\n if (this.#debug) {\n console.error('Audio playback error: ', error);\n }\n\n if (this.#callback) {\n this.#callback?.(error);\n } else {\n throw error;\n }\n }\n };\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.js.map"}
@@ -2,25 +2,33 @@
2
2
  * @typedef {Object} window
3
3
  * @property {typeof AudioContext} webkitAudioContext
4
4
  */ class $a3b359dec00ad964$var$ClickTone {
5
+ #file;
6
+ #volume;
7
+ #callback;
8
+ #throttle;
9
+ #debug;
10
+ #lastClickTime;
11
+ #audioCache;
12
+ #audioContext;
5
13
  constructor({ file: file, volume: volume = 1.0, callback: callback = null, throttle: throttle = 0, debug: debug = false }){
6
- this.file = file;
7
- this.volume = volume;
8
- this.callback = callback;
9
- this.throttle = throttle;
10
- this.debug = debug;
11
- this.lastClickTime = 0;
12
- this.audioCache = {};
14
+ this.#file = file;
15
+ this.#volume = volume;
16
+ this.#callback = callback;
17
+ this.#throttle = throttle;
18
+ this.#debug = debug;
19
+ this.#lastClickTime = 0;
20
+ this.#audioCache = {};
13
21
  }
14
- initAudioContext() {
15
- if (!this.audioContext) {
16
- this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
17
- this.iOSFixAudioContext();
22
+ #initAudioContext() {
23
+ if (!this.#audioContext) {
24
+ this.#audioContext = new (window.AudioContext || window.webkitAudioContext)();
25
+ this.#iOSFixAudioContext();
18
26
  }
19
27
  }
20
- iOSFixAudioContext = ()=>{
21
- if (this.audioContext && this.audioContext.state === "suspended" && "ontouchstart" in window) {
28
+ #iOSFixAudioContext = ()=>{
29
+ if (this.#audioContext && this.#audioContext.state === "suspended" && "ontouchstart" in window) {
22
30
  const unlock = ()=>{
23
- if (this.audioContext.state === "suspended") this.audioContext.resume().then(()=>{
31
+ if (this.#audioContext.state === "suspended") this.#audioContext.resume().then(()=>{
24
32
  document.body.removeEventListener("touchstart", unlock);
25
33
  document.body.removeEventListener("touchend", unlock);
26
34
  });
@@ -29,52 +37,52 @@
29
37
  document.body.addEventListener("touchend", unlock, false);
30
38
  }
31
39
  };
32
- fetchAndDecodeAudio = async (url)=>{
40
+ #fetchAndDecodeAudio = async (url)=>{
33
41
  try {
34
- if (this.audioCache[url]) return this.audioCache[url];
42
+ if (this.#audioCache[url]) return this.#audioCache[url];
35
43
  const response = await fetch(url);
36
44
  const buffer = await response.arrayBuffer();
37
- const audioData = await this.audioContext.decodeAudioData(buffer);
38
- this.audioCache[url] = audioData;
45
+ const audioData = await this.#audioContext.decodeAudioData(buffer);
46
+ this.#audioCache[url] = audioData;
39
47
  return audioData;
40
48
  } catch (error) {
41
- if (this.debug) console.error("Audio loading and decoding error: ", error);
49
+ if (this.#debug) console.error("Audio loading and decoding error: ", error);
42
50
  throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);
43
51
  }
44
52
  };
45
- audio = async (url)=>{
46
- this.initAudioContext();
53
+ #audio = async (url)=>{
54
+ this.#initAudioContext();
47
55
  try {
48
- const audioData = await this.fetchAndDecodeAudio(url);
49
- const source = this.audioContext.createBufferSource();
50
- const gainNode = this.audioContext.createGain();
56
+ const audioData = await this.#fetchAndDecodeAudio(url);
57
+ const source = this.#audioContext.createBufferSource();
58
+ const gainNode = this.#audioContext.createGain();
51
59
  source.buffer = audioData;
52
- gainNode.gain.value = this.volume;
60
+ gainNode.gain.value = this.#volume;
53
61
  source.connect(gainNode);
54
- gainNode.connect(this.audioContext.destination);
62
+ gainNode.connect(this.#audioContext.destination);
55
63
  source.onended = ()=>{
56
- if (this.callback) this.callback?.();
64
+ if (this.#callback) this.#callback?.();
57
65
  };
58
66
  source.start(0);
59
67
  } catch (error) {
60
- if (this.debug) console.error("Audio playback error: ", error);
68
+ if (this.#debug) console.error("Audio playback error: ", error);
61
69
  throw new Error(`Something went wrong while playing audio: ${error.message}`);
62
70
  }
63
71
  };
64
- throttleFn = (func)=>()=>{
72
+ #throttleFn = (func)=>()=>{
65
73
  const now = Date.now();
66
- if (now - this.lastClickTime >= this.throttle) {
74
+ if (now - this.#lastClickTime >= this.#throttle) {
67
75
  func();
68
- this.lastClickTime = now;
76
+ this.#lastClickTime = now;
69
77
  }
70
78
  };
71
- play = async (url = this.file)=>{
72
- const throttledPlay = this.throttleFn(()=>this.audio(url));
79
+ play = async (url = this.#file)=>{
80
+ const throttledPlay = this.#throttleFn(()=>this.#audio(url));
73
81
  try {
74
82
  await throttledPlay();
75
83
  } catch (error) {
76
- if (this.debug) console.error("Audio playback error: ", error);
77
- if (this.callback) this.callback?.(error);
84
+ if (this.#debug) console.error("Audio playback error: ", error);
85
+ if (this.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I;YACF,MAAM;QACR,EAAE,OAAO,OAAO;YACd,IAAI,IAAI,CAAC,KAAK,EACZ,QAAQ,KAAK,CAAC,0BAA0B;YAG1C,IAAI,IAAI,CAAC,QAAQ,EACf,IAAI,CAAC,QAAQ,GAAG;iBAEhB,MAAM;QAEV;IACF,EAAE;AACJ;IAEA,2CAAe","sources":["src/ClickTone.js"],"sourcesContent":["/**\n * @typedef {Object} window\n * @property {typeof AudioContext} webkitAudioContext\n */\n\nclass ClickTone {\n constructor({\n file,\n volume = 1.0,\n callback = null,\n throttle = 0,\n debug = false,\n }) {\n this.file = file;\n this.volume = volume;\n this.callback = callback;\n this.throttle = throttle;\n this.debug = debug;\n this.lastClickTime = 0;\n this.audioCache = {};\n }\n\n initAudioContext() {\n if (!this.audioContext) {\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n }\n\n iOSFixAudioContext = () => {\n if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = () => {\n if (this.audioContext.state === 'suspended') {\n this.audioContext.resume().then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n });\n }\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n };\n\n fetchAndDecodeAudio = async (url) => {\n try {\n if (this.audioCache[url]) {\n return this.audioCache[url];\n }\n\n const response = await fetch(url);\n const buffer = await response.arrayBuffer();\n const audioData = await this.audioContext.decodeAudioData(buffer);\n\n this.audioCache[url] = audioData;\n\n return audioData;\n } catch (error) {\n if (this.debug) {\n console.error('Audio loading and decoding error: ', error);\n }\n\n throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);\n }\n };\n\n audio = async (url) => {\n this.initAudioContext();\n\n try {\n const audioData = await this.fetchAndDecodeAudio(url);\n const source = this.audioContext.createBufferSource();\n const gainNode = this.audioContext.createGain();\n\n source.buffer = audioData;\n gainNode.gain.value = this.volume;\n source.connect(gainNode);\n gainNode.connect(this.audioContext.destination);\n\n source.onended = () => {\n if (this.callback) {\n this.callback?.();\n }\n };\n\n source.start(0);\n } catch (error) {\n if (this.debug) {\n console.error('Audio playback error: ', error);\n }\n\n throw new Error(`Something went wrong while playing audio: ${error.message}`);\n }\n };\n\n throttleFn = (func) => () => {\n const now = Date.now();\n\n if (now - this.lastClickTime >= this.throttle) {\n func();\n\n this.lastClickTime = now;\n }\n };\n\n play = async (url = this.file) => {\n const throttledPlay = this.throttleFn(() => this.audio(url));\n\n try {\n await throttledPlay();\n } catch (error) {\n if (this.debug) {\n console.error('Audio playback error: ', error);\n }\n\n if (this.callback) {\n this.callback?.(error);\n } else {\n throw error;\n }\n }\n };\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.module.js.map"}
1
+ {"mappings":"AAAA;;;CAGC,GAED,MAAM;IACJ,CAAC,IAAI,CAAC;IAEN,CAAC,MAAM,CAAC;IAER,CAAC,QAAQ,CAAC;IAEV,CAAC,QAAQ,CAAC;IAEV,CAAC,KAAK,CAAC;IAEP,CAAC,aAAa,CAAC;IAEf,CAAC,UAAU,CAAC;IAEZ,CAAC,YAAY,CAAC;IAEd,YAAY,QACV,IAAI,UACJ,SAAS,eACT,WAAW,gBACX,WAAW,UACX,QAAQ,OACT,CAAE;QACD,IAAI,CAAC,CAAC,IAAI,GAAG;QACb,IAAI,CAAC,CAAC,MAAM,GAAG;QACf,IAAI,CAAC,CAAC,QAAQ,GAAG;QACjB,IAAI,CAAC,CAAC,QAAQ,GAAG;QACjB,IAAI,CAAC,CAAC,KAAK,GAAG;QACd,IAAI,CAAC,CAAC,aAAa,GAAG;QACtB,IAAI,CAAC,CAAC,UAAU,GAAG,CAAC;IACtB;IAEA,CAAC,gBAAgB;QACf,IAAI,CAAC,IAAI,CAAC,CAAC,YAAY,EAAE;YACvB,IAAI,CAAC,CAAC,YAAY,GAAG,IAAK,CAAA,OAAO,YAAY,IAAI,OAAO,kBAAkB,AAAD;YACzE,IA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YACpD,MAAM,WAAW,IAAI,CAAC,CAAC,YAAY,CAAC,UAAU;YAE9C,OAAO,MAAM,GAAG;YAChB,SAAS,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC,MAAM;YAClC,OAAO,OAAO,CAAC;YACf,SAAS,OAAO,CAAC,IAAI,CAAC,CAAC,YAAY,CAAC,WAAW;YAE/C,OAAO,OAAO,GAAG;gBACf,IAAI,IAAI,CAAC,CAAC,QAAQ,EAChB,IAAI,CAAC,CAAC,QAAQ;YAElB;YAEA,OAAO,KAAK,CAAC;QACf,EAAE,OAAO,OAAO;YACd,IAAI,IAAI,CAAC,CAAC,KAAK,EACb,QAAQ,KAAK,CAAC,0BAA0B;YAG1C,MAAM,IAAI,MAAM,CAAC,0CAA0C,EAAE,MAAM,OAAO,CAAC,CAAC;QAC9E;IACF,EAAE;IAEF,CAAC,UAAU,GAAG,CAAC,OAAS;YACtB,MAAM,MAAM,KAAK,GAAG;YAEpB,IAAI,MAAM,IAAI,CAAC,CAAC,aAAa,IAAI,IAAI,CAAC,CAAC,QAAQ,EAAE;gBAC/C;gBAEA,IAAI,CAAC,CAAC,aAAa,GAAG;YACxB;QACF,EAAE;IAEF,OAAO,OAAO,MAAM,IAAI,CAAC,CAAC,IAAI;QAC5B,MAAM,gBAAgB,IAAI,CAAC,CAAC,UAAU,CAAC,IAAM,IAAI,CAAC,CAAC,KAAK,CAAC;QAEzD,IAAI;YACF,MAAM;QACR,EAAE,OAAO,OAAO;YACd,IAAI,IAAI,CAAC,CAAC,KAAK,EACb,QAAQ,KAAK,CAAC,0BAA0B;YAG1C,IAAI,IAAI,CAAC,CAAC,QAAQ,EAChB,IAAI,CAAC,CAAC,QAAQ,GAAG;iBAEjB,MAAM;QAEV;IACF,EAAE;AACJ;IAEA,2CAAe","sources":["src/ClickTone.js"],"sourcesContent":["/**\n * @typedef {Object} window\n * @property {typeof AudioContext} webkitAudioContext\n */\n\nclass ClickTone {\n #file;\n\n #volume;\n\n #callback;\n\n #throttle;\n\n #debug;\n\n #lastClickTime;\n\n #audioCache;\n\n #audioContext;\n\n constructor({\n file,\n volume = 1.0,\n callback = null,\n throttle = 0,\n debug = false,\n }) {\n this.#file = file;\n this.#volume = volume;\n this.#callback = callback;\n this.#throttle = throttle;\n this.#debug = debug;\n this.#lastClickTime = 0;\n this.#audioCache = {};\n }\n\n #initAudioContext() {\n if (!this.#audioContext) {\n this.#audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.#iOSFixAudioContext();\n }\n }\n\n #iOSFixAudioContext = () => {\n if (this.#audioContext && this.#audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = () => {\n if (this.#audioContext.state === 'suspended') {\n this.#audioContext.resume().then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n });\n }\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n };\n\n #fetchAndDecodeAudio = async (url) => {\n try {\n if (this.#audioCache[url]) {\n return this.#audioCache[url];\n }\n\n const response = await fetch(url);\n const buffer = await response.arrayBuffer();\n const audioData = await this.#audioContext.decodeAudioData(buffer);\n\n this.#audioCache[url] = audioData;\n\n return audioData;\n } catch (error) {\n if (this.#debug) {\n console.error('Audio loading and decoding error: ', error);\n }\n\n throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);\n }\n };\n\n #audio = async (url) => {\n this.#initAudioContext();\n\n try {\n const audioData = await this.#fetchAndDecodeAudio(url);\n const source = this.#audioContext.createBufferSource();\n const gainNode = this.#audioContext.createGain();\n\n source.buffer = audioData;\n gainNode.gain.value = this.#volume;\n source.connect(gainNode);\n gainNode.connect(this.#audioContext.destination);\n\n source.onended = () => {\n if (this.#callback) {\n this.#callback?.();\n }\n };\n\n source.start(0);\n } catch (error) {\n if (this.#debug) {\n console.error('Audio playback error: ', error);\n }\n\n throw new Error(`Something went wrong while playing audio: ${error.message}`);\n }\n };\n\n #throttleFn = (func) => () => {\n const now = Date.now();\n\n if (now - this.#lastClickTime >= this.#throttle) {\n func();\n\n this.#lastClickTime = now;\n }\n };\n\n play = async (url = this.#file) => {\n const throttledPlay = this.#throttleFn(() => this.#audio(url));\n\n try {\n await throttledPlay();\n } catch (error) {\n if (this.#debug) {\n console.error('Audio playback error: ', error);\n }\n\n if (this.#callback) {\n this.#callback?.(error);\n } else {\n throw error;\n }\n }\n };\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.module.js.map"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clicktone",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "A simple class that provides a convenient abstraction for handling audio playback by web applications, with support for iOS devices.",
5
5
  "author": "ux-ui.pro",
6
6
  "license": "MIT",
package/src/ClickTone.js CHANGED
@@ -4,6 +4,22 @@
4
4
  */
5
5
 
6
6
  class ClickTone {
7
+ #file;
8
+
9
+ #volume;
10
+
11
+ #callback;
12
+
13
+ #throttle;
14
+
15
+ #debug;
16
+
17
+ #lastClickTime;
18
+
19
+ #audioCache;
20
+
21
+ #audioContext;
22
+
7
23
  constructor({
8
24
  file,
9
25
  volume = 1.0,
@@ -11,27 +27,27 @@ class ClickTone {
11
27
  throttle = 0,
12
28
  debug = false,
13
29
  }) {
14
- this.file = file;
15
- this.volume = volume;
16
- this.callback = callback;
17
- this.throttle = throttle;
18
- this.debug = debug;
19
- this.lastClickTime = 0;
20
- this.audioCache = {};
30
+ this.#file = file;
31
+ this.#volume = volume;
32
+ this.#callback = callback;
33
+ this.#throttle = throttle;
34
+ this.#debug = debug;
35
+ this.#lastClickTime = 0;
36
+ this.#audioCache = {};
21
37
  }
22
38
 
23
- initAudioContext() {
24
- if (!this.audioContext) {
25
- this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
26
- this.iOSFixAudioContext();
39
+ #initAudioContext() {
40
+ if (!this.#audioContext) {
41
+ this.#audioContext = new (window.AudioContext || window.webkitAudioContext)();
42
+ this.#iOSFixAudioContext();
27
43
  }
28
44
  }
29
45
 
30
- iOSFixAudioContext = () => {
31
- if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {
46
+ #iOSFixAudioContext = () => {
47
+ if (this.#audioContext && this.#audioContext.state === 'suspended' && 'ontouchstart' in window) {
32
48
  const unlock = () => {
33
- if (this.audioContext.state === 'suspended') {
34
- this.audioContext.resume().then(() => {
49
+ if (this.#audioContext.state === 'suspended') {
50
+ this.#audioContext.resume().then(() => {
35
51
  document.body.removeEventListener('touchstart', unlock);
36
52
  document.body.removeEventListener('touchend', unlock);
37
53
  });
@@ -43,21 +59,21 @@ class ClickTone {
43
59
  }
44
60
  };
45
61
 
46
- fetchAndDecodeAudio = async (url) => {
62
+ #fetchAndDecodeAudio = async (url) => {
47
63
  try {
48
- if (this.audioCache[url]) {
49
- return this.audioCache[url];
64
+ if (this.#audioCache[url]) {
65
+ return this.#audioCache[url];
50
66
  }
51
67
 
52
68
  const response = await fetch(url);
53
69
  const buffer = await response.arrayBuffer();
54
- const audioData = await this.audioContext.decodeAudioData(buffer);
70
+ const audioData = await this.#audioContext.decodeAudioData(buffer);
55
71
 
56
- this.audioCache[url] = audioData;
72
+ this.#audioCache[url] = audioData;
57
73
 
58
74
  return audioData;
59
75
  } catch (error) {
60
- if (this.debug) {
76
+ if (this.#debug) {
61
77
  console.error('Audio loading and decoding error: ', error);
62
78
  }
63
79
 
@@ -65,28 +81,28 @@ class ClickTone {
65
81
  }
66
82
  };
67
83
 
68
- audio = async (url) => {
69
- this.initAudioContext();
84
+ #audio = async (url) => {
85
+ this.#initAudioContext();
70
86
 
71
87
  try {
72
- const audioData = await this.fetchAndDecodeAudio(url);
73
- const source = this.audioContext.createBufferSource();
74
- const gainNode = this.audioContext.createGain();
88
+ const audioData = await this.#fetchAndDecodeAudio(url);
89
+ const source = this.#audioContext.createBufferSource();
90
+ const gainNode = this.#audioContext.createGain();
75
91
 
76
92
  source.buffer = audioData;
77
- gainNode.gain.value = this.volume;
93
+ gainNode.gain.value = this.#volume;
78
94
  source.connect(gainNode);
79
- gainNode.connect(this.audioContext.destination);
95
+ gainNode.connect(this.#audioContext.destination);
80
96
 
81
97
  source.onended = () => {
82
- if (this.callback) {
83
- this.callback?.();
98
+ if (this.#callback) {
99
+ this.#callback?.();
84
100
  }
85
101
  };
86
102
 
87
103
  source.start(0);
88
104
  } catch (error) {
89
- if (this.debug) {
105
+ if (this.#debug) {
90
106
  console.error('Audio playback error: ', error);
91
107
  }
92
108
 
@@ -94,28 +110,28 @@ class ClickTone {
94
110
  }
95
111
  };
96
112
 
97
- throttleFn = (func) => () => {
113
+ #throttleFn = (func) => () => {
98
114
  const now = Date.now();
99
115
 
100
- if (now - this.lastClickTime >= this.throttle) {
116
+ if (now - this.#lastClickTime >= this.#throttle) {
101
117
  func();
102
118
 
103
- this.lastClickTime = now;
119
+ this.#lastClickTime = now;
104
120
  }
105
121
  };
106
122
 
107
- play = async (url = this.file) => {
108
- const throttledPlay = this.throttleFn(() => this.audio(url));
123
+ play = async (url = this.#file) => {
124
+ const throttledPlay = this.#throttleFn(() => this.#audio(url));
109
125
 
110
126
  try {
111
127
  await throttledPlay();
112
128
  } catch (error) {
113
- if (this.debug) {
129
+ if (this.#debug) {
114
130
  console.error('Audio playback error: ', error);
115
131
  }
116
132
 
117
- if (this.callback) {
118
- this.callback?.(error);
133
+ if (this.#callback) {
134
+ this.#callback?.(error);
119
135
  } else {
120
136
  throw error;
121
137
  }