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