clicktone 1.4.1 → 1.5.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/.eslintignore ADDED
@@ -0,0 +1,3 @@
1
+ node_modules
2
+ .parcel
3
+ dist
package/.eslintrc.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "env": {
3
+ "browser": true,
4
+ "es2021": true
5
+ },
6
+ "extends": [
7
+ "airbnb-base"
8
+ ],
9
+ "parserOptions": {
10
+ "ecmaVersion": "latest",
11
+ "sourceType": "module"
12
+ },
13
+ "rules": {
14
+ "max-len": [
15
+ "off",
16
+ {
17
+ "code": 100,
18
+ "ignoreUrls": true
19
+ }
20
+ ]
21
+ }
22
+ }
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 audio playback by web applications, with support for iOS devices.</p>
13
- <p align="center"><sup>800B gzipped</sup></p>
12
+ <p align="center">ClickTone is designed to control audio playback with various settings, including volume control, callback and debug mode. It also includes iOS support.</p>
13
+ <p align="center"><sup>850B 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
 
@@ -0,0 +1,18 @@
1
+ declare global {
2
+ interface Window {
3
+ webkitAudioContext?: typeof AudioContext;
4
+ }
5
+ }
6
+ export default class ClickTone {
7
+ constructor({ file, volume, callback, throttle, debug, }: {
8
+ file: string;
9
+ volume?: number;
10
+ callback?: ((error?: Error) => void) | null;
11
+ throttle?: number;
12
+ debug?: boolean;
13
+ });
14
+ play: (url?: string) => Promise<void>;
15
+ }
16
+ export default ClickTone;
17
+
18
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"mappings":"AAAA,QAAQ,MAAM,CAAC;IACb,UAAU,MAAM;QACd,kBAAkB,CAAC,EAAE,OAAO,YAAY,CAAC;KAC1C;CACF;AAED,qBAAM,SAAS;gBAUD,EACV,IAAI,EACJ,MAAY,EACZ,QAAe,EACf,QAAY,EACZ,KAAa,GACd,EAAE;QACD,IAAI,EAAE,MAAM,CAAC;QACb,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,EAAE,KAAK,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC;QAC5C,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,KAAK,CAAC,EAAE,OAAO,CAAC;KACjB;IAiGM,IAAI,SAAe,MAAM,KAAe,QAAQ,IAAI,CAAC,CAgB1D;CACH;AAED,eAAe,SAAS,CAAC","sources":["src/src/index.ts","src/index.ts"],"sourcesContent":[null,"declare global {\n interface Window {\n webkitAudioContext?: typeof AudioContext;\n }\n}\n\nclass ClickTone {\n private readonly file: string;\n private readonly volume: number;\n private readonly callback: ((error?: Error) => void) | null;\n private readonly throttle: number;\n private readonly debug: boolean;\n private lastClickTime: number;\n private readonly audioCache: Record<string, AudioBuffer>;\n private audioContext: AudioContext | null;\n\n constructor({\n file,\n volume = 1.0,\n callback = null,\n throttle = 0,\n debug = false,\n }: {\n file: string;\n volume?: number;\n callback?: ((error?: Error) => void) | null;\n throttle?: number;\n debug?: boolean;\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 this.audioContext = null;\n }\n\n private initAudioContext(): void {\n if (!this.audioContext) {\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n }\n\n private iOSFixAudioContext = (): void => {\n if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = (): void => {\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 private fetchAndDecodeAudio = async (url: string): Promise<AudioBuffer> => {\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 as Error).message}`);\n }\n };\n\n private playAudio = async (url: string): Promise<void> => {\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 as Error).message}`);\n }\n };\n\n private throttleFn = (func: () => void): (() => void) => {\n return () => {\n const now = Date.now();\n\n if (now - this.lastClickTime >= this.throttle) {\n func();\n\n this.lastClickTime = now;\n }\n };\n };\n\n public play = async (url: string = this.file): Promise<void> => {\n const throttledPlay = this.throttleFn(() => this.playAudio(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 as Error);\n } else {\n throw error;\n }\n }\n };\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.d.ts.map"}
package/dist/index.js CHANGED
@@ -9,38 +9,36 @@ function $parcel$export(e, n, v, s) {
9
9
 
10
10
  $parcel$defineInteropFlag(module.exports);
11
11
 
12
- $parcel$export(module.exports, "default", () => $5b968dfbc764795f$export$2e2bcd8739ae039);
13
- /**
14
- * @typedef {Object} window
15
- * @property {typeof AudioContext} webkitAudioContext
16
- */ class $5b968dfbc764795f$var$ClickTone {
17
- #file;
18
- #volume;
19
- #callback;
20
- #throttle;
21
- #debug;
22
- #lastClickTime;
23
- #audioCache;
24
- #audioContext;
12
+ $parcel$export(module.exports, "default", function () { return $a196c1ed25598f0e$export$2e2bcd8739ae039; });
13
+ class $a196c1ed25598f0e$var$ClickTone {
14
+ file;
15
+ volume;
16
+ callback;
17
+ throttle;
18
+ debug;
19
+ lastClickTime;
20
+ audioCache;
21
+ audioContext;
25
22
  constructor({ file: file, volume: volume = 1.0, callback: callback = null, throttle: throttle = 0, debug: debug = false }){
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 = {};
23
+ this.file = file;
24
+ this.volume = volume;
25
+ this.callback = callback;
26
+ this.throttle = throttle;
27
+ this.debug = debug;
28
+ this.lastClickTime = 0;
29
+ this.audioCache = {};
30
+ this.audioContext = null;
33
31
  }
34
- #initAudioContext() {
35
- if (!this.#audioContext) {
36
- this.#audioContext = new (window.AudioContext || window.webkitAudioContext)();
37
- this.#iOSFixAudioContext();
32
+ initAudioContext() {
33
+ if (!this.audioContext) {
34
+ this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
35
+ this.iOSFixAudioContext();
38
36
  }
39
37
  }
40
- #iOSFixAudioContext = ()=>{
41
- if (this.#audioContext && this.#audioContext.state === "suspended" && "ontouchstart" in window) {
38
+ iOSFixAudioContext = ()=>{
39
+ if (this.audioContext && this.audioContext.state === "suspended" && "ontouchstart" in window) {
42
40
  const unlock = ()=>{
43
- if (this.#audioContext.state === "suspended") this.#audioContext.resume().then(()=>{
41
+ if (this.audioContext.state === "suspended") this.audioContext.resume().then(()=>{
44
42
  document.body.removeEventListener("touchstart", unlock);
45
43
  document.body.removeEventListener("touchend", unlock);
46
44
  });
@@ -49,57 +47,59 @@ $parcel$export(module.exports, "default", () => $5b968dfbc764795f$export$2e2bcd8
49
47
  document.body.addEventListener("touchend", unlock, false);
50
48
  }
51
49
  };
52
- #fetchAndDecodeAudio = async (url)=>{
50
+ fetchAndDecodeAudio = async (url)=>{
53
51
  try {
54
- if (this.#audioCache[url]) return this.#audioCache[url];
52
+ if (this.audioCache[url]) return this.audioCache[url];
55
53
  const response = await fetch(url);
56
54
  const buffer = await response.arrayBuffer();
57
- const audioData = await this.#audioContext.decodeAudioData(buffer);
58
- this.#audioCache[url] = audioData;
55
+ const audioData = await this.audioContext.decodeAudioData(buffer);
56
+ this.audioCache[url] = audioData;
59
57
  return audioData;
60
58
  } catch (error) {
61
- if (this.#debug) console.error("Audio loading and decoding error: ", error);
59
+ if (this.debug) console.error("Audio loading and decoding error: ", error);
62
60
  throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);
63
61
  }
64
62
  };
65
- #audio = async (url)=>{
66
- this.#initAudioContext();
63
+ playAudio = async (url)=>{
64
+ this.initAudioContext();
67
65
  try {
68
- const audioData = await this.#fetchAndDecodeAudio(url);
69
- const source = this.#audioContext.createBufferSource();
70
- const gainNode = this.#audioContext.createGain();
66
+ const audioData = await this.fetchAndDecodeAudio(url);
67
+ const source = this.audioContext.createBufferSource();
68
+ const gainNode = this.audioContext.createGain();
71
69
  source.buffer = audioData;
72
- gainNode.gain.value = this.#volume;
70
+ gainNode.gain.value = this.volume;
73
71
  source.connect(gainNode);
74
- gainNode.connect(this.#audioContext.destination);
72
+ gainNode.connect(this.audioContext.destination);
75
73
  source.onended = ()=>{
76
- if (this.#callback) this.#callback?.();
74
+ if (this.callback) this.callback();
77
75
  };
78
76
  source.start(0);
79
77
  } catch (error) {
80
- if (this.#debug) console.error("Audio playback error: ", error);
78
+ if (this.debug) console.error("Audio playback error: ", error);
81
79
  throw new Error(`Something went wrong while playing audio: ${error.message}`);
82
80
  }
83
81
  };
84
- #throttleFn = (func)=>()=>{
82
+ throttleFn = (func)=>{
83
+ return ()=>{
85
84
  const now = Date.now();
86
- if (now - this.#lastClickTime >= this.#throttle) {
85
+ if (now - this.lastClickTime >= this.throttle) {
87
86
  func();
88
- this.#lastClickTime = now;
87
+ this.lastClickTime = now;
89
88
  }
90
89
  };
91
- play = async (url = this.#file)=>{
92
- const throttledPlay = this.#throttleFn(()=>this.#audio(url));
90
+ };
91
+ play = async (url = this.file)=>{
92
+ const throttledPlay = this.throttleFn(()=>this.playAudio(url));
93
93
  try {
94
94
  await throttledPlay();
95
95
  } catch (error) {
96
- if (this.#debug) console.error("Audio playback error: ", error);
97
- if (this.#callback) this.#callback?.(error);
96
+ if (this.debug) console.error("Audio playback error: ", error);
97
+ if (this.callback) this.callback(error);
98
98
  else throw error;
99
99
  }
100
100
  };
101
101
  }
102
- var $5b968dfbc764795f$export$2e2bcd8739ae039 = $5b968dfbc764795f$var$ClickTone;
102
+ var $a196c1ed25598f0e$export$2e2bcd8739ae039 = $a196c1ed25598f0e$var$ClickTone;
103
103
 
104
104
 
105
105
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
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,CAA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;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"}
1
+ {"mappings":";;;;;;;;;;;;AAMA,MAAM;IACa,KAAa;IACb,OAAe;IACf,SAA2C;IAC3C,SAAiB;IACjB,MAAe;IACxB,cAAsB;IACb,WAAwC;IACjD,aAAkC;IAE1C,YAAY,QACV,IAAI,UACJ,SAAS,eACT,WAAW,gBACX,WAAW,UACX,QAAQ,OAOT,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;QACnB,IAAI,CAAC,YAAY,GAAG;IACtB;IAEQ,mBAAyB;QAC/B,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE;YACtB,IAAI,CAAC,YAAY,GAAG,IAAK,CAAA,OAAO,YAAY,IAAI,OAAO,kBAAkB,AAAD;YACxE,IAAI,CAAC,kBAAkB;QACzB;IACF;IAEQ,qBAAqB;QAC3B,IAAI,IAAI,CAAC,YAAY,IAAI,IAAI,CAAC,YAAY,CAAC,KAAK,KAAK,eAAe,kBAAkB,QAAQ;YAC5F,MAAM,SAAS;gBACb,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,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,AAAC,MAAgB,OAAO,CAAC,CAAC;QACzF;IACF,EAAE;IAEM,aAAa,CAAC;QACpB,OAAO;YACL,MAAM,MAAM,KAAK,GAAG;YAEpB,IAAI,MAAM,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,QAAQ,EAAE;gBAC7C;gBAEA,IAAI,CAAC,aAAa,GAAG;YACvB;QACF;IACF,EAAE;IAEK,OAAO,OAAO,MAAc,IAAI,CAAC,IAAI;QAC1C,MAAM,gBAAgB,IAAI,CAAC,UAAU,CAAC,IAAM,IAAI,CAAC,SAAS,CAAC;QAE3D,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,CAAC;iBAEd,MAAM;QAEV;IACF,EAAE;AACJ;IAEA,2CAAe","sources":["src/index.ts"],"sourcesContent":["declare global {\n interface Window {\n webkitAudioContext?: typeof AudioContext;\n }\n}\n\nclass ClickTone {\n private readonly file: string;\n private readonly volume: number;\n private readonly callback: ((error?: Error) => void) | null;\n private readonly throttle: number;\n private readonly debug: boolean;\n private lastClickTime: number;\n private readonly audioCache: Record<string, AudioBuffer>;\n private audioContext: AudioContext | null;\n\n constructor({\n file,\n volume = 1.0,\n callback = null,\n throttle = 0,\n debug = false,\n }: {\n file: string;\n volume?: number;\n callback?: ((error?: Error) => void) | null;\n throttle?: number;\n debug?: boolean;\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 this.audioContext = null;\n }\n\n private initAudioContext(): void {\n if (!this.audioContext) {\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n }\n\n private iOSFixAudioContext = (): void => {\n if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = (): void => {\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 private fetchAndDecodeAudio = async (url: string): Promise<AudioBuffer> => {\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 as Error).message}`);\n }\n };\n\n private playAudio = async (url: string): Promise<void> => {\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 as Error).message}`);\n }\n };\n\n private throttleFn = (func: () => void): (() => void) => {\n return () => {\n const now = Date.now();\n\n if (now - this.lastClickTime >= this.throttle) {\n func();\n\n this.lastClickTime = now;\n }\n };\n };\n\n public play = async (url: string = this.file): Promise<void> => {\n const throttledPlay = this.throttleFn(() => this.playAudio(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 as Error);\n } else {\n throw error;\n }\n }\n };\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.js.map"}
@@ -1,34 +1,32 @@
1
- /**
2
- * @typedef {Object} window
3
- * @property {typeof AudioContext} webkitAudioContext
4
- */ class $a3b359dec00ad964$var$ClickTone {
5
- #file;
6
- #volume;
7
- #callback;
8
- #throttle;
9
- #debug;
10
- #lastClickTime;
11
- #audioCache;
12
- #audioContext;
1
+ class $643fcf18b2d2e76f$var$ClickTone {
2
+ file;
3
+ volume;
4
+ callback;
5
+ throttle;
6
+ debug;
7
+ lastClickTime;
8
+ audioCache;
9
+ audioContext;
13
10
  constructor({ file: file, volume: volume = 1.0, callback: callback = null, throttle: throttle = 0, debug: debug = false }){
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 = {};
11
+ this.file = file;
12
+ this.volume = volume;
13
+ this.callback = callback;
14
+ this.throttle = throttle;
15
+ this.debug = debug;
16
+ this.lastClickTime = 0;
17
+ this.audioCache = {};
18
+ this.audioContext = null;
21
19
  }
22
- #initAudioContext() {
23
- if (!this.#audioContext) {
24
- this.#audioContext = new (window.AudioContext || window.webkitAudioContext)();
25
- this.#iOSFixAudioContext();
20
+ initAudioContext() {
21
+ if (!this.audioContext) {
22
+ this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
23
+ this.iOSFixAudioContext();
26
24
  }
27
25
  }
28
- #iOSFixAudioContext = ()=>{
29
- if (this.#audioContext && this.#audioContext.state === "suspended" && "ontouchstart" in window) {
26
+ iOSFixAudioContext = ()=>{
27
+ if (this.audioContext && this.audioContext.state === "suspended" && "ontouchstart" in window) {
30
28
  const unlock = ()=>{
31
- if (this.#audioContext.state === "suspended") this.#audioContext.resume().then(()=>{
29
+ if (this.audioContext.state === "suspended") this.audioContext.resume().then(()=>{
32
30
  document.body.removeEventListener("touchstart", unlock);
33
31
  document.body.removeEventListener("touchend", unlock);
34
32
  });
@@ -37,58 +35,60 @@
37
35
  document.body.addEventListener("touchend", unlock, false);
38
36
  }
39
37
  };
40
- #fetchAndDecodeAudio = async (url)=>{
38
+ fetchAndDecodeAudio = async (url)=>{
41
39
  try {
42
- if (this.#audioCache[url]) return this.#audioCache[url];
40
+ if (this.audioCache[url]) return this.audioCache[url];
43
41
  const response = await fetch(url);
44
42
  const buffer = await response.arrayBuffer();
45
- const audioData = await this.#audioContext.decodeAudioData(buffer);
46
- this.#audioCache[url] = audioData;
43
+ const audioData = await this.audioContext.decodeAudioData(buffer);
44
+ this.audioCache[url] = audioData;
47
45
  return audioData;
48
46
  } catch (error) {
49
- if (this.#debug) console.error("Audio loading and decoding error: ", error);
47
+ if (this.debug) console.error("Audio loading and decoding error: ", error);
50
48
  throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);
51
49
  }
52
50
  };
53
- #audio = async (url)=>{
54
- this.#initAudioContext();
51
+ playAudio = async (url)=>{
52
+ this.initAudioContext();
55
53
  try {
56
- const audioData = await this.#fetchAndDecodeAudio(url);
57
- const source = this.#audioContext.createBufferSource();
58
- const gainNode = this.#audioContext.createGain();
54
+ const audioData = await this.fetchAndDecodeAudio(url);
55
+ const source = this.audioContext.createBufferSource();
56
+ const gainNode = this.audioContext.createGain();
59
57
  source.buffer = audioData;
60
- gainNode.gain.value = this.#volume;
58
+ gainNode.gain.value = this.volume;
61
59
  source.connect(gainNode);
62
- gainNode.connect(this.#audioContext.destination);
60
+ gainNode.connect(this.audioContext.destination);
63
61
  source.onended = ()=>{
64
- if (this.#callback) this.#callback?.();
62
+ if (this.callback) this.callback();
65
63
  };
66
64
  source.start(0);
67
65
  } catch (error) {
68
- if (this.#debug) console.error("Audio playback error: ", error);
66
+ if (this.debug) console.error("Audio playback error: ", error);
69
67
  throw new Error(`Something went wrong while playing audio: ${error.message}`);
70
68
  }
71
69
  };
72
- #throttleFn = (func)=>()=>{
70
+ throttleFn = (func)=>{
71
+ return ()=>{
73
72
  const now = Date.now();
74
- if (now - this.#lastClickTime >= this.#throttle) {
73
+ if (now - this.lastClickTime >= this.throttle) {
75
74
  func();
76
- this.#lastClickTime = now;
75
+ this.lastClickTime = now;
77
76
  }
78
77
  };
79
- play = async (url = this.#file)=>{
80
- const throttledPlay = this.#throttleFn(()=>this.#audio(url));
78
+ };
79
+ play = async (url = this.file)=>{
80
+ const throttledPlay = this.throttleFn(()=>this.playAudio(url));
81
81
  try {
82
82
  await throttledPlay();
83
83
  } catch (error) {
84
- if (this.#debug) console.error("Audio playback error: ", error);
85
- if (this.#callback) this.#callback?.(error);
84
+ if (this.debug) console.error("Audio playback error: ", error);
85
+ if (this.callback) this.callback(error);
86
86
  else throw error;
87
87
  }
88
88
  };
89
89
  }
90
- var $a3b359dec00ad964$export$2e2bcd8739ae039 = $a3b359dec00ad964$var$ClickTone;
90
+ var $643fcf18b2d2e76f$export$2e2bcd8739ae039 = $643fcf18b2d2e76f$var$ClickTone;
91
91
 
92
92
 
93
- export {$a3b359dec00ad964$export$2e2bcd8739ae039 as default};
93
+ export {$643fcf18b2d2e76f$export$2e2bcd8739ae039 as default};
94
94
  //# sourceMappingURL=index.module.js.map
@@ -1 +1 @@
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,QAA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AAC,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"}
1
+ {"mappings":"AAMA,MAAM;IACa,KAAa;IACb,OAAe;IACf,SAA2C;IAC3C,SAAiB;IACjB,MAAe;IACxB,cAAsB;IACb,WAAwC;IACjD,aAAkC;IAE1C,YAAY,QACV,IAAI,UACJ,SAAS,eACT,WAAW,gBACX,WAAW,UACX,QAAQ,OAOT,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;QACnB,IAAI,CAAC,YAAY,GAAG;IACtB;IAEQ,mBAAyB;QAC/B,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE;YACtB,IAAI,CAAC,YAAY,GAAG,IAAK,CAAA,OAAO,YAAY,IAAI,OAAO,kBAAkB,AAAD;YACxE,IAAI,CAAC,kBAAkB;QACzB;IACF;IAEQ,q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O,CAAC;YACf,SAAS,OAAO,CAAC,IAAI,CAAC,YAAY,CAAE,WAAW;YAE/C,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,AAAC,MAAgB,OAAO,CAAC,CAAC;QACzF;IACF,EAAE;IAEM,aAAa,CAAC;QACpB,OAAO;YACL,MAAM,MAAM,KAAK,GAAG;YAEpB,IAAI,MAAM,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,QAAQ,EAAE;gBAC7C;gBAEA,IAAI,CAAC,aAAa,GAAG;YACvB;QACF;IACF,EAAE;IAEK,OAAO,OAAO,MAAc,IAAI,CAAC,IAAI;QAC1C,MAAM,gBAAgB,IAAI,CAAC,UAAU,CAAC,IAAM,IAAI,CAAC,SAAS,CAAC;QAE3D,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,CAAC;iBAEd,MAAM;QAEV;IACF,EAAE;AACJ;IAEA,2CAAe","sources":["src/index.ts"],"sourcesContent":["declare global {\n interface Window {\n webkitAudioContext?: typeof AudioContext;\n }\n}\n\nclass ClickTone {\n private readonly file: string;\n private readonly volume: number;\n private readonly callback: ((error?: Error) => void) | null;\n private readonly throttle: number;\n private readonly debug: boolean;\n private lastClickTime: number;\n private readonly audioCache: Record<string, AudioBuffer>;\n private audioContext: AudioContext | null;\n\n constructor({\n file,\n volume = 1.0,\n callback = null,\n throttle = 0,\n debug = false,\n }: {\n file: string;\n volume?: number;\n callback?: ((error?: Error) => void) | null;\n throttle?: number;\n debug?: boolean;\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 this.audioContext = null;\n }\n\n private initAudioContext(): void {\n if (!this.audioContext) {\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n }\n\n private iOSFixAudioContext = (): void => {\n if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = (): void => {\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 private fetchAndDecodeAudio = async (url: string): Promise<AudioBuffer> => {\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 as Error).message}`);\n }\n };\n\n private playAudio = async (url: string): Promise<void> => {\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 as Error).message}`);\n }\n };\n\n private throttleFn = (func: () => void): (() => void) => {\n return () => {\n const now = Date.now();\n\n if (now - this.lastClickTime >= this.throttle) {\n func();\n\n this.lastClickTime = now;\n }\n };\n };\n\n public play = async (url: string = this.file): Promise<void> => {\n const throttledPlay = this.throttleFn(() => this.playAudio(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 as 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,7 +1,7 @@
1
1
  {
2
2
  "name": "clicktone",
3
- "version": "1.4.1",
4
- "description": "A simple class that provides a convenient abstraction for handling audio playback by web applications, with support for iOS devices.",
3
+ "version": "1.5.0",
4
+ "description": "ClickTone is designed to control audio playback with various settings, including volume control, callback and debug mode. It also includes iOS support.",
5
5
  "author": "ux-ui.pro",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -11,27 +11,27 @@
11
11
  "bugs": {
12
12
  "url": "https://github.com/ux-ui-pro/clicktone/issues"
13
13
  },
14
- "main": "dist/index.js",
15
- "module": "dist/index.module.js",
16
- "targets": {
17
- "main": {
18
- "source": "src/ClickTone.js"
19
- },
20
- "module": {
21
- "source": "src/ClickTone.js"
22
- }
23
- },
14
+ "homepage": "https://github.com/ux-ui-pro/clicktone",
15
+ "sideEffects": false,
24
16
  "scripts": {
25
17
  "clean": "rm -rf dist .parcel-cache",
26
18
  "build": "yarn clean && parcel build",
27
- "lint:js": "eslint --ext .js",
19
+ "lint:js": "eslint --ext .ts,.js",
28
20
  "lintfix": "yarn lint:js --fix"
29
21
  },
22
+ "browserslist": "> 0.5%, last 2 versions, not dead",
23
+ "source": "src/index.ts",
24
+ "main": "dist/index.js",
25
+ "module": "dist/index.module.js",
26
+ "types": "dist/index.d.ts",
30
27
  "devDependencies": {
28
+ "@parcel/packager-ts": "2.12.0",
29
+ "@parcel/transformer-sass": "2.12.0",
30
+ "@parcel/transformer-typescript-types": "2.12.0",
31
31
  "eslint": "^7.32.0 || ^8.57.0",
32
- "eslint-plugin-import": "^2.29.1",
33
32
  "eslint-config-airbnb-base": "^15.0.0",
34
- "parcel": "^2.12.0"
33
+ "parcel": "^2.12.0",
34
+ "typescript": "^5.4.5"
35
35
  },
36
36
  "keywords": [
37
37
  "MP3",
@@ -42,6 +42,19 @@
42
42
  "sound",
43
43
  "audio",
44
44
  "playback",
45
- "mobile"
45
+ "mobile",
46
+ "click",
47
+ "tone",
48
+ "audioContext",
49
+ "webAudio",
50
+ "javascript",
51
+ "web",
52
+ "html5",
53
+ "media",
54
+ "music",
55
+ "effects",
56
+ "browser",
57
+ "webapp",
58
+ "frontend"
46
59
  ]
47
60
  }
package/src/index.ts ADDED
@@ -0,0 +1,145 @@
1
+ declare global {
2
+ interface Window {
3
+ webkitAudioContext?: typeof AudioContext;
4
+ }
5
+ }
6
+
7
+ class ClickTone {
8
+ private readonly file: string;
9
+ private readonly volume: number;
10
+ private readonly callback: ((error?: Error) => void) | null;
11
+ private readonly throttle: number;
12
+ private readonly debug: boolean;
13
+ private lastClickTime: number;
14
+ private readonly audioCache: Record<string, AudioBuffer>;
15
+ private audioContext: AudioContext | null;
16
+
17
+ constructor({
18
+ file,
19
+ volume = 1.0,
20
+ callback = null,
21
+ throttle = 0,
22
+ debug = false,
23
+ }: {
24
+ file: string;
25
+ volume?: number;
26
+ callback?: ((error?: Error) => void) | null;
27
+ throttle?: number;
28
+ debug?: boolean;
29
+ }) {
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 = {};
37
+ this.audioContext = null;
38
+ }
39
+
40
+ private initAudioContext(): void {
41
+ if (!this.audioContext) {
42
+ this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
43
+ this.iOSFixAudioContext();
44
+ }
45
+ }
46
+
47
+ private iOSFixAudioContext = (): void => {
48
+ if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {
49
+ const unlock = (): void => {
50
+ if (this.audioContext!.state === 'suspended') {
51
+ this.audioContext!.resume().then(() => {
52
+ document.body.removeEventListener('touchstart', unlock);
53
+ document.body.removeEventListener('touchend', unlock);
54
+ });
55
+ }
56
+ };
57
+
58
+ document.body.addEventListener('touchstart', unlock, false);
59
+ document.body.addEventListener('touchend', unlock, false);
60
+ }
61
+ };
62
+
63
+ private fetchAndDecodeAudio = async (url: string): Promise<AudioBuffer> => {
64
+ try {
65
+ if (this.audioCache[url]) {
66
+ return this.audioCache[url];
67
+ }
68
+
69
+ const response = await fetch(url);
70
+ const buffer = await response.arrayBuffer();
71
+ const audioData = await this.audioContext!.decodeAudioData(buffer);
72
+
73
+ this.audioCache[url] = audioData;
74
+
75
+ return audioData;
76
+ } catch (error) {
77
+ if (this.debug) {
78
+ console.error('Audio loading and decoding error: ', error);
79
+ }
80
+
81
+ throw new Error(`Something went wrong when loading and decoding the audio: ${(error as Error).message}`);
82
+ }
83
+ };
84
+
85
+ private playAudio = async (url: string): Promise<void> => {
86
+ this.initAudioContext();
87
+
88
+ try {
89
+ const audioData = await this.fetchAndDecodeAudio(url);
90
+ const source = this.audioContext!.createBufferSource();
91
+ const gainNode = this.audioContext!.createGain();
92
+
93
+ source.buffer = audioData;
94
+ gainNode.gain.value = this.volume;
95
+ source.connect(gainNode);
96
+ gainNode.connect(this.audioContext!.destination);
97
+
98
+ source.onended = () => {
99
+ if (this.callback) {
100
+ this.callback();
101
+ }
102
+ };
103
+
104
+ source.start(0);
105
+ } catch (error) {
106
+ if (this.debug) {
107
+ console.error('Audio playback error: ', error);
108
+ }
109
+
110
+ throw new Error(`Something went wrong while playing audio: ${(error as Error).message}`);
111
+ }
112
+ };
113
+
114
+ private throttleFn = (func: () => void): (() => void) => {
115
+ return () => {
116
+ const now = Date.now();
117
+
118
+ if (now - this.lastClickTime >= this.throttle) {
119
+ func();
120
+
121
+ this.lastClickTime = now;
122
+ }
123
+ };
124
+ };
125
+
126
+ public play = async (url: string = this.file): Promise<void> => {
127
+ const throttledPlay = this.throttleFn(() => this.playAudio(url));
128
+
129
+ try {
130
+ await throttledPlay();
131
+ } catch (error) {
132
+ if (this.debug) {
133
+ console.error('Audio playback error: ', error);
134
+ }
135
+
136
+ if (this.callback) {
137
+ this.callback(error as Error);
138
+ } else {
139
+ throw error;
140
+ }
141
+ }
142
+ };
143
+ }
144
+
145
+ export default ClickTone;
package/tsconfig.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "compilerOptions": {
3
+ "isolatedModules": true,
4
+ "target": "es2022"
5
+ }
6
+ }
package/src/ClickTone.js DELETED
@@ -1,142 +0,0 @@
1
- /**
2
- * @typedef {Object} window
3
- * @property {typeof AudioContext} webkitAudioContext
4
- */
5
-
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
-
23
- constructor({
24
- file,
25
- volume = 1.0,
26
- callback = null,
27
- throttle = 0,
28
- debug = false,
29
- }) {
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 = {};
37
- }
38
-
39
- #initAudioContext() {
40
- if (!this.#audioContext) {
41
- this.#audioContext = new (window.AudioContext || window.webkitAudioContext)();
42
- this.#iOSFixAudioContext();
43
- }
44
- }
45
-
46
- #iOSFixAudioContext = () => {
47
- if (this.#audioContext && this.#audioContext.state === 'suspended' && 'ontouchstart' in window) {
48
- const unlock = () => {
49
- if (this.#audioContext.state === 'suspended') {
50
- this.#audioContext.resume().then(() => {
51
- document.body.removeEventListener('touchstart', unlock);
52
- document.body.removeEventListener('touchend', unlock);
53
- });
54
- }
55
- };
56
-
57
- document.body.addEventListener('touchstart', unlock, false);
58
- document.body.addEventListener('touchend', unlock, false);
59
- }
60
- };
61
-
62
- #fetchAndDecodeAudio = async (url) => {
63
- try {
64
- if (this.#audioCache[url]) {
65
- return this.#audioCache[url];
66
- }
67
-
68
- const response = await fetch(url);
69
- const buffer = await response.arrayBuffer();
70
- const audioData = await this.#audioContext.decodeAudioData(buffer);
71
-
72
- this.#audioCache[url] = audioData;
73
-
74
- return audioData;
75
- } catch (error) {
76
- if (this.#debug) {
77
- console.error('Audio loading and decoding error: ', error);
78
- }
79
-
80
- throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);
81
- }
82
- };
83
-
84
- #audio = async (url) => {
85
- this.#initAudioContext();
86
-
87
- try {
88
- const audioData = await this.#fetchAndDecodeAudio(url);
89
- const source = this.#audioContext.createBufferSource();
90
- const gainNode = this.#audioContext.createGain();
91
-
92
- source.buffer = audioData;
93
- gainNode.gain.value = this.#volume;
94
- source.connect(gainNode);
95
- gainNode.connect(this.#audioContext.destination);
96
-
97
- source.onended = () => {
98
- if (this.#callback) {
99
- this.#callback?.();
100
- }
101
- };
102
-
103
- source.start(0);
104
- } catch (error) {
105
- if (this.#debug) {
106
- console.error('Audio playback error: ', error);
107
- }
108
-
109
- throw new Error(`Something went wrong while playing audio: ${error.message}`);
110
- }
111
- };
112
-
113
- #throttleFn = (func) => () => {
114
- const now = Date.now();
115
-
116
- if (now - this.#lastClickTime >= this.#throttle) {
117
- func();
118
-
119
- this.#lastClickTime = now;
120
- }
121
- };
122
-
123
- play = async (url = this.#file) => {
124
- const throttledPlay = this.#throttleFn(() => this.#audio(url));
125
-
126
- try {
127
- await throttledPlay();
128
- } catch (error) {
129
- if (this.#debug) {
130
- console.error('Audio playback error: ', error);
131
- }
132
-
133
- if (this.#callback) {
134
- this.#callback?.(error);
135
- } else {
136
- throw error;
137
- }
138
- }
139
- };
140
- }
141
-
142
- export default ClickTone;