clicktone 1.0.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 +10 -4
- package/dist/index.js +61 -21
- package/dist/index.js.map +1 -1
- package/dist/index.module.js +61 -21
- package/dist/index.module.js.map +1 -1
- package/package.json +1 -1
- package/src/index.js +96 -30
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
<p align="center">A simple class that provides a convenient abstraction for handling<br>
|
|
13
13
|
audio playback by web applications, with support for iOS devices.</p>
|
|
14
|
-
<p align="center"><a href="https://
|
|
14
|
+
<p align="center"><a href="https://codepen.io/ux-ui/pen/yLwbmMr">Demo</a></p>
|
|
15
15
|
<br>
|
|
16
16
|
|
|
17
17
|
➤ **Install**
|
|
@@ -33,9 +33,15 @@ import ClickTone from 'clicktone';
|
|
|
33
33
|
|
|
34
34
|
<sub>Class uses the Web Audio API, which supports many audio file formats: MP3, WAV, OGG, AAC and others. Please note that not all browsers support these formats.</sub>
|
|
35
35
|
```javascript
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
const click = new ClickTone({
|
|
37
|
+
file: './sound.mp3',
|
|
38
|
+
volume: 0.7,
|
|
39
|
+
throttle: 400,
|
|
40
|
+
callback: () => { console.log('Playback ended') },
|
|
41
|
+
debug: true,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
myButton.addEventListener('click', () => click.play());
|
|
39
45
|
```
|
|
40
46
|
<br>
|
|
41
47
|
|
package/dist/index.js
CHANGED
|
@@ -11,15 +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(file){
|
|
14
|
+
constructor({ file: file, volume: volume = 1.0, callback: callback = null, throttle: throttle = 0, debug: debug = false }){
|
|
15
15
|
this.file = file;
|
|
16
|
+
this.volume = volume;
|
|
17
|
+
this.callback = callback;
|
|
18
|
+
this.throttle = throttle;
|
|
19
|
+
this.debug = debug;
|
|
16
20
|
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
|
17
21
|
this.iOSFixAudioContext();
|
|
22
|
+
this.lastClickTime = 0;
|
|
23
|
+
this.audioCache = {};
|
|
18
24
|
}
|
|
19
|
-
iOSFixAudioContext()
|
|
20
|
-
if (this.audioContext.state === "suspended" && "ontouchstart" in window) {
|
|
25
|
+
iOSFixAudioContext = ()=>{
|
|
26
|
+
if (this.audioContext && this.audioContext.state === "suspended" && "ontouchstart" in window) {
|
|
21
27
|
const unlock = ()=>{
|
|
22
|
-
this.audioContext.resume().then(()=>{
|
|
28
|
+
if (this.audioContext.state === "suspended") this.audioContext.resume().then(()=>{
|
|
23
29
|
document.body.removeEventListener("touchstart", unlock);
|
|
24
30
|
document.body.removeEventListener("touchend", unlock);
|
|
25
31
|
});
|
|
@@ -27,23 +33,57 @@ class $4fa36e821943b400$var$ClickTone {
|
|
|
27
33
|
document.body.addEventListener("touchstart", unlock, false);
|
|
28
34
|
document.body.addEventListener("touchend", unlock, false);
|
|
29
35
|
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
+
};
|
|
47
87
|
}
|
|
48
88
|
var $4fa36e821943b400$export$2e2bcd8739ae039 = $4fa36e821943b400$var$ClickTone;
|
|
49
89
|
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1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Q,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"}
|
package/dist/index.module.js
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
class $cf838c15c8b009ba$var$ClickTone {
|
|
2
|
-
constructor(file){
|
|
2
|
+
constructor({ file: file, volume: volume = 1.0, callback: callback = null, throttle: throttle = 0, debug: debug = false }){
|
|
3
3
|
this.file = file;
|
|
4
|
+
this.volume = volume;
|
|
5
|
+
this.callback = callback;
|
|
6
|
+
this.throttle = throttle;
|
|
7
|
+
this.debug = debug;
|
|
4
8
|
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
|
5
9
|
this.iOSFixAudioContext();
|
|
10
|
+
this.lastClickTime = 0;
|
|
11
|
+
this.audioCache = {};
|
|
6
12
|
}
|
|
7
|
-
iOSFixAudioContext()
|
|
8
|
-
if (this.audioContext.state === "suspended" && "ontouchstart" in window) {
|
|
13
|
+
iOSFixAudioContext = ()=>{
|
|
14
|
+
if (this.audioContext && this.audioContext.state === "suspended" && "ontouchstart" in window) {
|
|
9
15
|
const unlock = ()=>{
|
|
10
|
-
this.audioContext.resume().then(()=>{
|
|
16
|
+
if (this.audioContext.state === "suspended") this.audioContext.resume().then(()=>{
|
|
11
17
|
document.body.removeEventListener("touchstart", unlock);
|
|
12
18
|
document.body.removeEventListener("touchend", unlock);
|
|
13
19
|
});
|
|
@@ -15,23 +21,57 @@ class $cf838c15c8b009ba$var$ClickTone {
|
|
|
15
21
|
document.body.addEventListener("touchstart", unlock, false);
|
|
16
22
|
document.body.addEventListener("touchend", unlock, false);
|
|
17
23
|
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
+
};
|
|
35
75
|
}
|
|
36
76
|
var $cf838c15c8b009ba$export$2e2bcd8739ae039 = $cf838c15c8b009ba$var$ClickTone;
|
|
37
77
|
|
package/dist/index.module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AAAA,MAAM;IACJ,YAAY,IAAI,CAAE;
|
|
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,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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"}
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,47 +1,113 @@
|
|
|
1
1
|
class ClickTone {
|
|
2
|
-
constructor(
|
|
2
|
+
constructor({
|
|
3
|
+
file,
|
|
4
|
+
volume = 1.0,
|
|
5
|
+
callback = null,
|
|
6
|
+
throttle = 0,
|
|
7
|
+
debug = false,
|
|
8
|
+
}) {
|
|
3
9
|
this.file = file;
|
|
10
|
+
this.volume = volume;
|
|
11
|
+
this.callback = callback;
|
|
12
|
+
this.throttle = throttle;
|
|
13
|
+
this.debug = debug;
|
|
4
14
|
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
|
5
15
|
this.iOSFixAudioContext();
|
|
16
|
+
this.lastClickTime = 0;
|
|
17
|
+
this.audioCache = {};
|
|
6
18
|
}
|
|
7
19
|
|
|
8
|
-
iOSFixAudioContext() {
|
|
9
|
-
if (this.audioContext.state === 'suspended' && 'ontouchstart' in window) {
|
|
20
|
+
iOSFixAudioContext = () => {
|
|
21
|
+
if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {
|
|
10
22
|
const unlock = () => {
|
|
11
|
-
this.audioContext.
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
+
}
|
|
15
29
|
};
|
|
16
30
|
|
|
17
31
|
document.body.addEventListener('touchstart', unlock, false);
|
|
18
32
|
document.body.addEventListener('touchend', unlock, false);
|
|
19
33
|
}
|
|
20
|
-
}
|
|
34
|
+
};
|
|
21
35
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
.then((audioData) => {
|
|
28
|
-
const source = this.audioContext.createBufferSource();
|
|
29
|
-
|
|
30
|
-
source.buffer = audioData;
|
|
31
|
-
source.connect(this.audioContext.destination);
|
|
32
|
-
source.start(0);
|
|
33
|
-
|
|
34
|
-
resolve();
|
|
35
|
-
})
|
|
36
|
-
.catch((error) => {
|
|
37
|
-
reject(error);
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
}
|
|
36
|
+
fetchAndDecodeAudio = async (url) => {
|
|
37
|
+
try {
|
|
38
|
+
if (this.audioCache[url]) {
|
|
39
|
+
return this.audioCache[url];
|
|
40
|
+
}
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
+
};
|
|
45
111
|
}
|
|
46
112
|
|
|
47
113
|
export default ClickTone;
|