clicktone 1.2.0 → 1.3.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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022 ux-ui.pro
3
+ Copyright (c) 2024 ux-ui.pro
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/dist/index.js CHANGED
@@ -9,19 +9,26 @@ function $parcel$export(e, n, v, s) {
9
9
 
10
10
  $parcel$defineInteropFlag(module.exports);
11
11
 
12
- $parcel$export(module.exports, "default", () => $4fa36e821943b400$export$2e2bcd8739ae039);
13
- class $4fa36e821943b400$var$ClickTone {
12
+ $parcel$export(module.exports, "default", () => $5b968dfbc764795f$export$2e2bcd8739ae039);
13
+ /**
14
+ * @typedef {Object} window
15
+ * @property {typeof AudioContext} webkitAudioContext
16
+ */ class $5b968dfbc764795f$var$ClickTone {
14
17
  constructor({ file: file, volume: volume = 1.0, callback: callback = null, throttle: throttle = 0, debug: debug = false }){
15
18
  this.file = file;
16
19
  this.volume = volume;
17
20
  this.callback = callback;
18
21
  this.throttle = throttle;
19
22
  this.debug = debug;
20
- this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
21
- this.iOSFixAudioContext();
22
23
  this.lastClickTime = 0;
23
24
  this.audioCache = {};
24
25
  }
26
+ initAudioContext() {
27
+ if (!this.audioContext) {
28
+ this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
29
+ this.iOSFixAudioContext();
30
+ }
31
+ }
25
32
  iOSFixAudioContext = ()=>{
26
33
  if (this.audioContext && this.audioContext.state === "suspended" && "ontouchstart" in window) {
27
34
  const unlock = ()=>{
@@ -48,6 +55,7 @@ class $4fa36e821943b400$var$ClickTone {
48
55
  }
49
56
  };
50
57
  audio = async (url)=>{
58
+ this.initAudioContext();
51
59
  try {
52
60
  const audioData = await this.fetchAndDecodeAudio(url);
53
61
  const source = this.audioContext.createBufferSource();
@@ -57,7 +65,7 @@ class $4fa36e821943b400$var$ClickTone {
57
65
  source.connect(gainNode);
58
66
  gainNode.connect(this.audioContext.destination);
59
67
  source.onended = ()=>{
60
- if (this.callback) this.callback();
68
+ if (this.callback) this.callback?.();
61
69
  };
62
70
  source.start(0);
63
71
  } catch (error) {
@@ -65,27 +73,25 @@ class $4fa36e821943b400$var$ClickTone {
65
73
  throw new Error(`Something went wrong while playing audio: ${error.message}`);
66
74
  }
67
75
  };
68
- throttleFn = (func)=>{
69
- return ()=>{
76
+ throttleFn = (func)=>()=>{
70
77
  const now = Date.now();
71
78
  if (now - this.lastClickTime >= this.throttle) {
72
79
  func();
73
80
  this.lastClickTime = now;
74
81
  }
75
82
  };
76
- };
77
83
  play = async (url = this.file)=>{
78
84
  const throttledPlay = this.throttleFn(()=>this.audio(url));
79
85
  try {
80
86
  await throttledPlay();
81
87
  } catch (error) {
82
88
  if (this.debug) console.error("Audio playback error: ", error);
83
- if (this.callback) this.callback(error);
89
+ if (this.callback) this.callback?.(error);
84
90
  else throw error;
85
91
  }
86
92
  };
87
93
  }
88
- var $4fa36e821943b400$export$2e2bcd8739ae039 = $4fa36e821943b400$var$ClickTone;
94
+ var $5b968dfbc764795f$export$2e2bcd8739ae039 = $5b968dfbc764795f$var$ClickTone;
89
95
 
90
96
 
91
97
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;;;;;AAAA,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,YAAY,GAAG,IAAK,CAAA,OAAO,YAAY,IAAI,O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7C,OAAO,MAAM,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;YAE1C,MAAM,IAAI,MAAM,CAAC,0CAA0C,EAAE,MAAM,OAAO,CAAC,CAAC;QAC9E;IACF,EAAE;IAEF,aAAa,CAAC;QACZ,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;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,CAAC;iBAEd,MAAM;QAEV;IACF,EAAE;AACJ;IAEA,2CAAe","sources":["src/index.js"],"sourcesContent":["class 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.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n this.lastClickTime = 0;\n this.audioCache = {};\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 return audioData;\n } catch (error) {\n if (this.debug) {\n console.error('Audio loading and decoding error: ', error);\n }\n throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);\n }\n };\n\n audio = async (url) => {\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 throw new Error(`Something went wrong while playing audio: ${error.message}`);\n }\n };\n\n throttleFn = (func) => {\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 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":";;;;;;;;;;;;AAAA;;;CAGC,GAED,MAAM;IACJ,YAAY,QACV,IAAI,UACJ,SAAS,eACT,WAAW,gBACX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AQ,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,15 +1,22 @@
1
- class $cf838c15c8b009ba$var$ClickTone {
1
+ /**
2
+ * @typedef {Object} window
3
+ * @property {typeof AudioContext} webkitAudioContext
4
+ */ class $a3b359dec00ad964$var$ClickTone {
2
5
  constructor({ file: file, volume: volume = 1.0, callback: callback = null, throttle: throttle = 0, debug: debug = false }){
3
6
  this.file = file;
4
7
  this.volume = volume;
5
8
  this.callback = callback;
6
9
  this.throttle = throttle;
7
10
  this.debug = debug;
8
- this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
9
- this.iOSFixAudioContext();
10
11
  this.lastClickTime = 0;
11
12
  this.audioCache = {};
12
13
  }
14
+ initAudioContext() {
15
+ if (!this.audioContext) {
16
+ this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
17
+ this.iOSFixAudioContext();
18
+ }
19
+ }
13
20
  iOSFixAudioContext = ()=>{
14
21
  if (this.audioContext && this.audioContext.state === "suspended" && "ontouchstart" in window) {
15
22
  const unlock = ()=>{
@@ -36,6 +43,7 @@ class $cf838c15c8b009ba$var$ClickTone {
36
43
  }
37
44
  };
38
45
  audio = async (url)=>{
46
+ this.initAudioContext();
39
47
  try {
40
48
  const audioData = await this.fetchAndDecodeAudio(url);
41
49
  const source = this.audioContext.createBufferSource();
@@ -45,7 +53,7 @@ class $cf838c15c8b009ba$var$ClickTone {
45
53
  source.connect(gainNode);
46
54
  gainNode.connect(this.audioContext.destination);
47
55
  source.onended = ()=>{
48
- if (this.callback) this.callback();
56
+ if (this.callback) this.callback?.();
49
57
  };
50
58
  source.start(0);
51
59
  } catch (error) {
@@ -53,28 +61,26 @@ class $cf838c15c8b009ba$var$ClickTone {
53
61
  throw new Error(`Something went wrong while playing audio: ${error.message}`);
54
62
  }
55
63
  };
56
- throttleFn = (func)=>{
57
- return ()=>{
64
+ throttleFn = (func)=>()=>{
58
65
  const now = Date.now();
59
66
  if (now - this.lastClickTime >= this.throttle) {
60
67
  func();
61
68
  this.lastClickTime = now;
62
69
  }
63
70
  };
64
- };
65
71
  play = async (url = this.file)=>{
66
72
  const throttledPlay = this.throttleFn(()=>this.audio(url));
67
73
  try {
68
74
  await throttledPlay();
69
75
  } catch (error) {
70
76
  if (this.debug) console.error("Audio playback error: ", error);
71
- if (this.callback) this.callback(error);
77
+ if (this.callback) this.callback?.(error);
72
78
  else throw error;
73
79
  }
74
80
  };
75
81
  }
76
- var $cf838c15c8b009ba$export$2e2bcd8739ae039 = $cf838c15c8b009ba$var$ClickTone;
82
+ var $a3b359dec00ad964$export$2e2bcd8739ae039 = $a3b359dec00ad964$var$ClickTone;
77
83
 
78
84
 
79
- export {$cf838c15c8b009ba$export$2e2bcd8739ae039 as default};
85
+ export {$a3b359dec00ad964$export$2e2bcd8739ae039 as default};
80
86
  //# sourceMappingURL=index.module.js.map
@@ -1 +1 @@
1
- {"mappings":"AAAA,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,YAAY,GAAG,IAAK,CAAA,OAAO,YAAY,IAAI,OAAO,kBAAkB,AAAD;QACxE,IAAI,CAAC,kBAAkB;QACvB,IAAI,CAAC,aAAa,GAAG;Q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ACjC,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;YAE1C,MAAM,IAAI,MAAM,CAAC,0CAA0C,EAAE,MAAM,OAAO,CAAC,CAAC;QAC9E;IACF,EAAE;IAEF,aAAa,CAAC;QACZ,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;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,CAAC;iBAEd,MAAM;QAEV;IACF,EAAE;AACJ;IAEA,2CAAe","sources":["src/index.js"],"sourcesContent":["class 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.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n this.lastClickTime = 0;\n this.audioCache = {};\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 return audioData;\n } catch (error) {\n if (this.debug) {\n console.error('Audio loading and decoding error: ', error);\n }\n throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);\n }\n };\n\n audio = async (url) => {\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 throw new Error(`Something went wrong while playing audio: ${error.message}`);\n }\n };\n\n throttleFn = (func) => {\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 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,YAAY,QACV,IAAI,UACJ,SAAS,eACT,WAAW,gBACX,WAAW,UACX,QAAQ,OACT,CAAE;QACD,IAAI,CAAC,IAAI,GAAG;QACZ,IAAI,CAAC,MAAM,GAAG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":["/**\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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clicktone",
3
- "version": "1.2.0",
3
+ "version": "1.3.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",
@@ -15,10 +15,10 @@
15
15
  "module": "dist/index.module.js",
16
16
  "targets": {
17
17
  "main": {
18
- "source": "src/index.js"
18
+ "source": "src/ClickTone.js"
19
19
  },
20
20
  "module": {
21
- "source": "src/index.js"
21
+ "source": "src/ClickTone.js"
22
22
  }
23
23
  },
24
24
  "scripts": {
@@ -28,11 +28,10 @@
28
28
  "lintfix": "yarn lint:js --fix"
29
29
  },
30
30
  "devDependencies": {
31
- "eslint": "^7.32.0 || ^8.56.0",
32
- "eslint-config-airbnb-base": "^15.0.0",
31
+ "eslint": "^7.32.0 || ^8.57.0",
33
32
  "eslint-plugin-import": "^2.29.1",
34
- "parcel": "^2.11.0",
35
- "postcss-html": "^1.6.0"
33
+ "eslint-config-airbnb-base": "^15.0.0",
34
+ "parcel": "^2.12.0"
36
35
  },
37
36
  "keywords": [
38
37
  "MP3",
@@ -1,3 +1,8 @@
1
+ /**
2
+ * @typedef {Object} window
3
+ * @property {typeof AudioContext} webkitAudioContext
4
+ */
5
+
1
6
  class ClickTone {
2
7
  constructor({
3
8
  file,
@@ -11,12 +16,17 @@ class ClickTone {
11
16
  this.callback = callback;
12
17
  this.throttle = throttle;
13
18
  this.debug = debug;
14
- this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
15
- this.iOSFixAudioContext();
16
19
  this.lastClickTime = 0;
17
20
  this.audioCache = {};
18
21
  }
19
22
 
23
+ initAudioContext() {
24
+ if (!this.audioContext) {
25
+ this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
26
+ this.iOSFixAudioContext();
27
+ }
28
+ }
29
+
20
30
  iOSFixAudioContext = () => {
21
31
  if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {
22
32
  const unlock = () => {
@@ -44,16 +54,20 @@ class ClickTone {
44
54
  const audioData = await this.audioContext.decodeAudioData(buffer);
45
55
 
46
56
  this.audioCache[url] = audioData;
57
+
47
58
  return audioData;
48
59
  } catch (error) {
49
60
  if (this.debug) {
50
61
  console.error('Audio loading and decoding error: ', error);
51
62
  }
63
+
52
64
  throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);
53
65
  }
54
66
  };
55
67
 
56
68
  audio = async (url) => {
69
+ this.initAudioContext();
70
+
57
71
  try {
58
72
  const audioData = await this.fetchAndDecodeAudio(url);
59
73
  const source = this.audioContext.createBufferSource();
@@ -66,7 +80,7 @@ class ClickTone {
66
80
 
67
81
  source.onended = () => {
68
82
  if (this.callback) {
69
- this.callback();
83
+ this.callback?.();
70
84
  }
71
85
  };
72
86
 
@@ -75,20 +89,19 @@ class ClickTone {
75
89
  if (this.debug) {
76
90
  console.error('Audio playback error: ', error);
77
91
  }
92
+
78
93
  throw new Error(`Something went wrong while playing audio: ${error.message}`);
79
94
  }
80
95
  };
81
96
 
82
- throttleFn = (func) => {
83
- return () => {
84
- const now = Date.now();
97
+ throttleFn = (func) => () => {
98
+ const now = Date.now();
85
99
 
86
- if (now - this.lastClickTime >= this.throttle) {
87
- func();
100
+ if (now - this.lastClickTime >= this.throttle) {
101
+ func();
88
102
 
89
- this.lastClickTime = now;
90
- }
91
- };
103
+ this.lastClickTime = now;
104
+ }
92
105
  };
93
106
 
94
107
  play = async (url = this.file) => {
@@ -102,7 +115,7 @@ class ClickTone {
102
115
  }
103
116
 
104
117
  if (this.callback) {
105
- this.callback(error);
118
+ this.callback?.(error);
106
119
  } else {
107
120
  throw error;
108
121
  }