clicktone 1.1.0 → 1.2.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
@@ -36,6 +36,9 @@ import ClickTone from 'clicktone';
36
36
  const click = new ClickTone({
37
37
  file: './sound.mp3',
38
38
  volume: 0.7,
39
+ throttle: 400,
40
+ callback: () => { console.log('Playback ended') },
41
+ debug: true,
39
42
  });
40
43
 
41
44
  myButton.addEventListener('click', () => click.play());
package/dist/index.js CHANGED
@@ -11,16 +11,21 @@ $parcel$defineInteropFlag(module.exports);
11
11
 
12
12
  $parcel$export(module.exports, "default", () => $4fa36e821943b400$export$2e2bcd8739ae039);
13
13
  class $4fa36e821943b400$var$ClickTone {
14
- constructor(options){
15
- this.file = options.file;
16
- this.volume = options.volume || 1.0;
14
+ constructor({ file: file, volume: volume = 1.0, callback: callback = null, throttle: throttle = 0, debug: debug = false }){
15
+ this.file = file;
16
+ this.volume = volume;
17
+ this.callback = callback;
18
+ this.throttle = throttle;
19
+ this.debug = debug;
17
20
  this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
18
21
  this.iOSFixAudioContext();
22
+ this.lastClickTime = 0;
23
+ this.audioCache = {};
19
24
  }
20
- iOSFixAudioContext() {
21
- if (this.audioContext.state === "suspended" && "ontouchstart" in window) {
25
+ iOSFixAudioContext = ()=>{
26
+ if (this.audioContext && this.audioContext.state === "suspended" && "ontouchstart" in window) {
22
27
  const unlock = ()=>{
23
- this.audioContext.resume().then(()=>{
28
+ if (this.audioContext.state === "suspended") this.audioContext.resume().then(()=>{
24
29
  document.body.removeEventListener("touchstart", unlock);
25
30
  document.body.removeEventListener("touchend", unlock);
26
31
  });
@@ -28,26 +33,57 @@ class $4fa36e821943b400$var$ClickTone {
28
33
  document.body.addEventListener("touchstart", unlock, false);
29
34
  document.body.addEventListener("touchend", unlock, false);
30
35
  }
31
- }
32
- audio(url) {
33
- return new Promise((resolve, reject)=>{
34
- fetch(url).then((response)=>response.arrayBuffer()).then((buffer)=>this.audioContext.decodeAudioData(buffer)).then((audioData)=>{
35
- const source = this.audioContext.createBufferSource();
36
- const gainNode = this.audioContext.createGain();
37
- source.buffer = audioData;
38
- gainNode.gain.value = this.volume;
39
- source.connect(gainNode);
40
- gainNode.connect(this.audioContext.destination);
41
- source.start(0);
42
- resolve();
43
- }).catch((error)=>{
44
- reject(error);
45
- });
46
- });
47
- }
48
- play(url = this.file) {
49
- return this.audio(url);
50
- }
36
+ };
37
+ fetchAndDecodeAudio = async (url)=>{
38
+ try {
39
+ if (this.audioCache[url]) return this.audioCache[url];
40
+ const response = await fetch(url);
41
+ const buffer = await response.arrayBuffer();
42
+ const audioData = await this.audioContext.decodeAudioData(buffer);
43
+ this.audioCache[url] = audioData;
44
+ return audioData;
45
+ } catch (error) {
46
+ if (this.debug) console.error("Audio loading and decoding error: ", error);
47
+ throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);
48
+ }
49
+ };
50
+ audio = async (url)=>{
51
+ try {
52
+ const audioData = await this.fetchAndDecodeAudio(url);
53
+ const source = this.audioContext.createBufferSource();
54
+ const gainNode = this.audioContext.createGain();
55
+ source.buffer = audioData;
56
+ gainNode.gain.value = this.volume;
57
+ source.connect(gainNode);
58
+ gainNode.connect(this.audioContext.destination);
59
+ source.onended = ()=>{
60
+ if (this.callback) this.callback();
61
+ };
62
+ source.start(0);
63
+ } catch (error) {
64
+ if (this.debug) console.error("Audio playback error: ", error);
65
+ throw new Error(`Something went wrong while playing audio: ${error.message}`);
66
+ }
67
+ };
68
+ throttleFn = (func)=>{
69
+ return ()=>{
70
+ const now = Date.now();
71
+ if (now - this.lastClickTime >= this.throttle) {
72
+ func();
73
+ this.lastClickTime = now;
74
+ }
75
+ };
76
+ };
77
+ play = async (url = this.file)=>{
78
+ const throttledPlay = this.throttleFn(()=>this.audio(url));
79
+ try {
80
+ await throttledPlay();
81
+ } catch (error) {
82
+ if (this.debug) console.error("Audio playback error: ", error);
83
+ if (this.callback) this.callback(error);
84
+ else throw error;
85
+ }
86
+ };
51
87
  }
52
88
  var $4fa36e821943b400$export$2e2bcd8739ae039 = $4fa36e821943b400$var$ClickTone;
53
89
 
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;;;;;AAAA,MAAM;IACJ,YAAY,OAAO,CAAE;QACnB,IAAI,CAAC,IAAI,GAAG,QAAQ,IAAI;QACxB,IAAI,CAAC,MAAM,GAAG,QAAQ,MAAM,IAAI;QAChC,IAAI,CAAC,YAAY,GAAG,IAAK,CAAA,OAAO,YAAY,IAAI,OAAO,kBAAkB,AAAD;QACxE,IAAI,CAAC,kBAAkB;IACzB;IAEA,qBAAqB;QACnB,IAAI,IAAI,CAAC,YAAY,CAAC,KAAK,KAAK,eAAe,kBAAkB,QAAQ;YACvE,MAAM,SAAS;gBACb,IAAI,CAAC,YAAY,CAAC,MAAM,GAAG,IAAI,CAAC;oBAC9B,SAAS,IAAI,CAAC,mBAAmB,CAAC,cAAc;oBAChD,SAAS,IAAI,CAAC,mBAAmB,CAAC,YAAY;gBAChD;YACF;YAEA,SAAS,IAAI,CAAC,gBAAgB,CAAC,cAAc,QAAQ;YACrD,SAAS,IAAI,CAAC,gBAAgB,CAAC,YAAY,QAAQ;QACrD;IACF;IAEA,MAAM,GAAG,EAAE;QACT,OAAO,IAAI,QAAQ,CAAC,SAAS;YAC3B,MAAM,KACH,IAAI,CAAC,CAAC,WAAa,SAAS,WAAW,IACvC,IAAI,CAAC,CAAC,SAAW,IAAI,CAAC,YAAY,CAAC,eAAe,CAAC,SACnD,IAAI,CAAC,CAAC;gBACL,MAAM,SAAS,IAAI,CAAC,YAAY,CAAC,kBAAkB;gBACnD,MAAM,WAAW,IAAI,CAAC,YAAY,CAAC,UAAU;gBAE7C,OAAO,MAAM,GAAG;gBAChB,SAAS,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,MAAM;gBACjC,OAAO,OAAO,CAAC;gBACf,SAAS,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,WAAW;gBAC9C,OAAO,KAAK,CAAC;gBAEb;YACF,GACC,KAAK,CAAC,CAAC;gBACN,OAAO;YACT;QACJ;IACF;IAEA,KAAK,MAAM,IAAI,CAAC,IAAI,EAAE;QACpB,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB;AACF;IAEA,2CAAe","sources":["src/index.js"],"sourcesContent":["class ClickTone {\n constructor(options) {\n this.file = options.file;\n this.volume = options.volume || 1.0;\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n\n iOSFixAudioContext() {\n if (this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = () => {\n this.audioContext.resume().then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n });\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n }\n\n audio(url) {\n return new Promise((resolve, reject) => {\n fetch(url)\n .then((response) => response.arrayBuffer())\n .then((buffer) => this.audioContext.decodeAudioData(buffer))\n .then((audioData) => {\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 source.start(0);\n\n resolve();\n })\n .catch((error) => {\n reject(error);\n });\n });\n }\n\n play(url = this.file) {\n return this.audio(url);\n }\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.js.map"}
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,GA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,IAAI,CAAC,YAAY,CAAC,UAAU;YAE7C,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,14 +1,19 @@
1
1
  class $cf838c15c8b009ba$var$ClickTone {
2
- constructor(options){
3
- this.file = options.file;
4
- this.volume = options.volume || 1.0;
2
+ constructor({ file: file, volume: volume = 1.0, callback: callback = null, throttle: throttle = 0, debug: debug = false }){
3
+ this.file = file;
4
+ this.volume = volume;
5
+ this.callback = callback;
6
+ this.throttle = throttle;
7
+ this.debug = debug;
5
8
  this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
6
9
  this.iOSFixAudioContext();
10
+ this.lastClickTime = 0;
11
+ this.audioCache = {};
7
12
  }
8
- iOSFixAudioContext() {
9
- if (this.audioContext.state === "suspended" && "ontouchstart" in window) {
13
+ iOSFixAudioContext = ()=>{
14
+ if (this.audioContext && this.audioContext.state === "suspended" && "ontouchstart" in window) {
10
15
  const unlock = ()=>{
11
- this.audioContext.resume().then(()=>{
16
+ if (this.audioContext.state === "suspended") this.audioContext.resume().then(()=>{
12
17
  document.body.removeEventListener("touchstart", unlock);
13
18
  document.body.removeEventListener("touchend", unlock);
14
19
  });
@@ -16,26 +21,57 @@ class $cf838c15c8b009ba$var$ClickTone {
16
21
  document.body.addEventListener("touchstart", unlock, false);
17
22
  document.body.addEventListener("touchend", unlock, false);
18
23
  }
19
- }
20
- audio(url) {
21
- return new Promise((resolve, reject)=>{
22
- fetch(url).then((response)=>response.arrayBuffer()).then((buffer)=>this.audioContext.decodeAudioData(buffer)).then((audioData)=>{
23
- const source = this.audioContext.createBufferSource();
24
- const gainNode = this.audioContext.createGain();
25
- source.buffer = audioData;
26
- gainNode.gain.value = this.volume;
27
- source.connect(gainNode);
28
- gainNode.connect(this.audioContext.destination);
29
- source.start(0);
30
- resolve();
31
- }).catch((error)=>{
32
- reject(error);
33
- });
34
- });
35
- }
36
- play(url = this.file) {
37
- return this.audio(url);
38
- }
24
+ };
25
+ fetchAndDecodeAudio = async (url)=>{
26
+ try {
27
+ if (this.audioCache[url]) return this.audioCache[url];
28
+ const response = await fetch(url);
29
+ const buffer = await response.arrayBuffer();
30
+ const audioData = await this.audioContext.decodeAudioData(buffer);
31
+ this.audioCache[url] = audioData;
32
+ return audioData;
33
+ } catch (error) {
34
+ if (this.debug) console.error("Audio loading and decoding error: ", error);
35
+ throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);
36
+ }
37
+ };
38
+ audio = async (url)=>{
39
+ try {
40
+ const audioData = await this.fetchAndDecodeAudio(url);
41
+ const source = this.audioContext.createBufferSource();
42
+ const gainNode = this.audioContext.createGain();
43
+ source.buffer = audioData;
44
+ gainNode.gain.value = this.volume;
45
+ source.connect(gainNode);
46
+ gainNode.connect(this.audioContext.destination);
47
+ source.onended = ()=>{
48
+ if (this.callback) this.callback();
49
+ };
50
+ source.start(0);
51
+ } catch (error) {
52
+ if (this.debug) console.error("Audio playback error: ", error);
53
+ throw new Error(`Something went wrong while playing audio: ${error.message}`);
54
+ }
55
+ };
56
+ throttleFn = (func)=>{
57
+ return ()=>{
58
+ const now = Date.now();
59
+ if (now - this.lastClickTime >= this.throttle) {
60
+ func();
61
+ this.lastClickTime = now;
62
+ }
63
+ };
64
+ };
65
+ play = async (url = this.file)=>{
66
+ const throttledPlay = this.throttleFn(()=>this.audio(url));
67
+ try {
68
+ await throttledPlay();
69
+ } catch (error) {
70
+ if (this.debug) console.error("Audio playback error: ", error);
71
+ if (this.callback) this.callback(error);
72
+ else throw error;
73
+ }
74
+ };
39
75
  }
40
76
  var $cf838c15c8b009ba$export$2e2bcd8739ae039 = $cf838c15c8b009ba$var$ClickTone;
41
77
 
@@ -1 +1 @@
1
- {"mappings":"AAAA,MAAM;IACJ,YAAY,OAAO,CAAE;QACnB,IAAI,CAAC,IAAI,GAAG,QAAQ,IAAI;QACxB,IAAI,CAAC,MAAM,GAAG,QAAQ,MAAM,IAAI;QAChC,IAAI,CAAC,YAAY,GAAG,IAAK,CAAA,OAAO,YAAY,IAAI,OAAO,kBAAkB,AAAD;QACxE,IAAI,CAAC,kBAAkB;IACzB;IAEA,qBAAqB;QACnB,IAAI,IAAI,CAAC,YAAY,CAAC,KAAK,KAAK,eAAe,kBAAkB,QAAQ;YACvE,MAAM,SAAS;gBACb,IAAI,CAAC,YAAY,CAAC,MAAM,GAAG,IAAI,CAAC;oBAC9B,SAAS,IAAI,CAAC,mBAAmB,CAAC,cAAc;oBAChD,SAAS,IAAI,CAAC,mBAAmB,CAAC,YAAY;gBAChD;YACF;YAEA,SAAS,IAAI,CAAC,gBAAgB,CAAC,cAAc,QAAQ;YACrD,SAAS,IAAI,CAAC,gBAAgB,CAAC,YAAY,QAAQ;QACrD;IACF;IAEA,MAAM,GAAG,EAAE;QACT,OAAO,IAAI,QAAQ,CAAC,SAAS;YAC3B,MAAM,KACH,IAAI,CAAC,CAAC,WAAa,SAAS,WAAW,IACvC,IAAI,CAAC,CAAC,SAAW,IAAI,CAAC,YAAY,CAAC,eAAe,CAAC,SACnD,IAAI,CAAC,CAAC;gBACL,MAAM,SAAS,IAAI,CAAC,YAAY,CAAC,kBAAkB;gBACnD,MAAM,WAAW,IAAI,CAAC,YAAY,CAAC,UAAU;gBAE7C,OAAO,MAAM,GAAG;gBAChB,SAAS,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,MAAM;gBACjC,OAAO,OAAO,CAAC;gBACf,SAAS,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,WAAW;gBAC9C,OAAO,KAAK,CAAC;gBAEb;YACF,GACC,KAAK,CAAC,CAAC;gBACN,OAAO;YACT;QACJ;IACF;IAEA,KAAK,MAAM,IAAI,CAAC,IAAI,EAAE;QACpB,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB;AACF;IAEA,2CAAe","sources":["src/index.js"],"sourcesContent":["class ClickTone {\n constructor(options) {\n this.file = options.file;\n this.volume = options.volume || 1.0;\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n\n iOSFixAudioContext() {\n if (this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = () => {\n this.audioContext.resume().then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n });\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n }\n\n audio(url) {\n return new Promise((resolve, reject) => {\n fetch(url)\n .then((response) => response.arrayBuffer())\n .then((buffer) => this.audioContext.decodeAudioData(buffer))\n .then((audioData) => {\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 source.start(0);\n\n resolve();\n })\n .catch((error) => {\n reject(error);\n });\n });\n }\n\n play(url = this.file) {\n return this.audio(url);\n }\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.module.js.map"}
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;QACrB,IAAI,CAAC,UAAU,GAAG,CAAC;IACrB;IAEA,qBAAqB;QACnB,IAAI,IAAI,CAAC,YAAY,IAAI,IAAI,CAAC,YAAY,CAAC,KAAK,KAAK,eAAe,kBAAkB,QAAQ;YAC5F,MAAM,SAAS;gBACb,IAAI,IAAI,CAAC,YAAY,CAAC,KAAK,KAAK,aAC9B,IAAI,CAAC,YAAY,CAAC,MAAM,GAAG,IAAI,CAAC;oBAC9B,SAAS,IAAI,CAAC,mBAAmB,CAAC,cAAc;oBAChD,SAAS,IAAI,CAAC,mBAAmB,CAAC,YAAY;gBAChD;YAEJ;YAEA,SAAS,IAAI,CAAC,g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AM,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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clicktone",
3
- "version": "1.1.0",
3
+ "version": "1.2.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/index.js CHANGED
@@ -1,51 +1,113 @@
1
1
  class ClickTone {
2
- constructor(options) {
3
- this.file = options.file;
4
- this.volume = options.volume || 1.0;
2
+ constructor({
3
+ file,
4
+ volume = 1.0,
5
+ callback = null,
6
+ throttle = 0,
7
+ debug = false,
8
+ }) {
9
+ this.file = file;
10
+ this.volume = volume;
11
+ this.callback = callback;
12
+ this.throttle = throttle;
13
+ this.debug = debug;
5
14
  this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
6
15
  this.iOSFixAudioContext();
16
+ this.lastClickTime = 0;
17
+ this.audioCache = {};
7
18
  }
8
19
 
9
- iOSFixAudioContext() {
10
- if (this.audioContext.state === 'suspended' && 'ontouchstart' in window) {
20
+ iOSFixAudioContext = () => {
21
+ if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {
11
22
  const unlock = () => {
12
- this.audioContext.resume().then(() => {
13
- document.body.removeEventListener('touchstart', unlock);
14
- document.body.removeEventListener('touchend', unlock);
15
- });
23
+ if (this.audioContext.state === 'suspended') {
24
+ this.audioContext.resume().then(() => {
25
+ document.body.removeEventListener('touchstart', unlock);
26
+ document.body.removeEventListener('touchend', unlock);
27
+ });
28
+ }
16
29
  };
17
30
 
18
31
  document.body.addEventListener('touchstart', unlock, false);
19
32
  document.body.addEventListener('touchend', unlock, false);
20
33
  }
21
- }
34
+ };
22
35
 
23
- audio(url) {
24
- return new Promise((resolve, reject) => {
25
- fetch(url)
26
- .then((response) => response.arrayBuffer())
27
- .then((buffer) => this.audioContext.decodeAudioData(buffer))
28
- .then((audioData) => {
29
- const source = this.audioContext.createBufferSource();
30
- const gainNode = this.audioContext.createGain();
31
-
32
- source.buffer = audioData;
33
- gainNode.gain.value = this.volume;
34
- source.connect(gainNode);
35
- gainNode.connect(this.audioContext.destination);
36
- source.start(0);
37
-
38
- resolve();
39
- })
40
- .catch((error) => {
41
- reject(error);
42
- });
43
- });
44
- }
36
+ fetchAndDecodeAudio = async (url) => {
37
+ try {
38
+ if (this.audioCache[url]) {
39
+ return this.audioCache[url];
40
+ }
45
41
 
46
- play(url = this.file) {
47
- return this.audio(url);
48
- }
42
+ const response = await fetch(url);
43
+ const buffer = await response.arrayBuffer();
44
+ const audioData = await this.audioContext.decodeAudioData(buffer);
45
+
46
+ this.audioCache[url] = audioData;
47
+ return audioData;
48
+ } catch (error) {
49
+ if (this.debug) {
50
+ console.error('Audio loading and decoding error: ', error);
51
+ }
52
+ throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);
53
+ }
54
+ };
55
+
56
+ audio = async (url) => {
57
+ try {
58
+ const audioData = await this.fetchAndDecodeAudio(url);
59
+ const source = this.audioContext.createBufferSource();
60
+ const gainNode = this.audioContext.createGain();
61
+
62
+ source.buffer = audioData;
63
+ gainNode.gain.value = this.volume;
64
+ source.connect(gainNode);
65
+ gainNode.connect(this.audioContext.destination);
66
+
67
+ source.onended = () => {
68
+ if (this.callback) {
69
+ this.callback();
70
+ }
71
+ };
72
+
73
+ source.start(0);
74
+ } catch (error) {
75
+ if (this.debug) {
76
+ console.error('Audio playback error: ', error);
77
+ }
78
+ throw new Error(`Something went wrong while playing audio: ${error.message}`);
79
+ }
80
+ };
81
+
82
+ throttleFn = (func) => {
83
+ return () => {
84
+ const now = Date.now();
85
+
86
+ if (now - this.lastClickTime >= this.throttle) {
87
+ func();
88
+
89
+ this.lastClickTime = now;
90
+ }
91
+ };
92
+ };
93
+
94
+ play = async (url = this.file) => {
95
+ const throttledPlay = this.throttleFn(() => this.audio(url));
96
+
97
+ try {
98
+ await throttledPlay();
99
+ } catch (error) {
100
+ if (this.debug) {
101
+ console.error('Audio playback error: ', error);
102
+ }
103
+
104
+ if (this.callback) {
105
+ this.callback(error);
106
+ } else {
107
+ throw error;
108
+ }
109
+ }
110
+ };
49
111
  }
50
112
 
51
113
  export default ClickTone;