clicktone 1.5.4 → 1.7.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 +3 -2
- package/dist/index.cjs.js +1 -0
- package/dist/index.d.ts +15 -4
- package/dist/index.es.js +71 -0
- package/dist/index.umd.js +1 -0
- package/package.json +31 -16
- package/.eslintignore +0 -3
- package/.eslintrc.json +0 -26
- package/code_of_conduct.md +0 -37
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -106
- package/dist/index.js.map +0 -1
- package/dist/index.module.js +0 -95
- package/dist/index.module.js.map +0 -1
- package/src/index.ts +0 -153
- package/tsconfig.json +0 -6
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
</div>
|
|
11
11
|
|
|
12
12
|
<p align="center">ClickTone is designed to control audio playback with various settings, including volume control, callback and debug mode. It also includes iOS support.</p>
|
|
13
|
-
<p align="center"><sup>
|
|
13
|
+
<p align="center"><sup>1kB 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
|
|
|
@@ -34,7 +34,8 @@ import ClickTone from 'clicktone';
|
|
|
34
34
|
<sub>ClickTone uses the Web Audio API, which supports many audio file formats: MP3, WAV, OGG, AAC and others. Note that not all browsers support these formats.</sub>
|
|
35
35
|
```javascript
|
|
36
36
|
const sound = new ClickTone({
|
|
37
|
-
file: './sound.mp3',
|
|
37
|
+
// file: './sound.mp3',
|
|
38
|
+
file: new URL('./sound.mp3', import.meta.url).href,
|
|
38
39
|
volume: 0.7,
|
|
39
40
|
throttle: 100,
|
|
40
41
|
callback: () => { console.log('Playback ended') },
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var s=Object.defineProperty;var h=(t,e,i)=>e in t?s(t,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):t[e]=i;var o=(t,e,i)=>h(t,typeof e!="symbol"?e+"":e,i);module.exports=class{constructor({file:t,volume:e=1,callback:i=null,throttle:a=0,debug:n=!1}){o(this,"file");o(this,"volume");o(this,"callback");o(this,"throttle");o(this,"debug");o(this,"lastClickTime");o(this,"audioCache");o(this,"audioContext");this.file=t,this.volume=e,this.callback=i,this.throttle=a,this.debug=n,this.lastClickTime=0,this.audioCache={},this.audioContext=null}initAudioContext(){this.audioContext||(this.audioContext=new(window.AudioContext||window.webkitAudioContext),this.iOSFixAudioContext())}iOSFixAudioContext(){if(this.audioContext&&this.audioContext.state==="suspended"&&"ontouchstart"in window){const t=()=>{this.audioContext.state==="suspended"&&this.audioContext.resume().then(()=>{document.body.removeEventListener("touchstart",t),document.body.removeEventListener("touchend",t)}).catch(e=>{this.debug})};document.body.addEventListener("touchstart",t,!1),document.body.addEventListener("touchend",t,!1)}}async fetchAndDecodeAudio(t){try{if(this.audioCache[t])return this.audioCache[t];const e=await fetch(t),i=await e.arrayBuffer(),a=await this.audioContext.decodeAudioData(i);return this.audioCache[t]=a,a}catch(e){throw this.debug,new Error(`Something went wrong when loading and decoding the audio: ${e.message}`)}}async playAudio(t){this.initAudioContext();try{const e=await this.fetchAndDecodeAudio(t),i=this.audioContext.createBufferSource(),a=this.audioContext.createGain();i.buffer=e,a.gain.value=this.volume,i.connect(a),a.connect(this.audioContext.destination),i.onended=()=>{this.callback&&this.callback()},i.start(0)}catch(e){throw this.debug,new Error(`Something went wrong while playing audio: ${e.message}`)}}throttleFn(t){return()=>{const e=Date.now();e-this.lastClickTime>=this.throttle&&(t().catch(i=>{this.debug}),this.lastClickTime=e)}}play(t=this.file){const e=this.throttleFn(()=>this.playAudio(t));try{e()}catch(i){if(this.debug,!this.callback)throw i;this.callback(i)}}};
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,15 @@ declare global {
|
|
|
3
3
|
webkitAudioContext?: typeof AudioContext;
|
|
4
4
|
}
|
|
5
5
|
}
|
|
6
|
-
|
|
6
|
+
declare class ClickTone {
|
|
7
|
+
private readonly file;
|
|
8
|
+
private readonly volume;
|
|
9
|
+
private readonly callback;
|
|
10
|
+
private readonly throttle;
|
|
11
|
+
private readonly debug;
|
|
12
|
+
private lastClickTime;
|
|
13
|
+
private readonly audioCache;
|
|
14
|
+
private audioContext;
|
|
7
15
|
constructor({ file, volume, callback, throttle, debug, }: {
|
|
8
16
|
file: string;
|
|
9
17
|
volume?: number;
|
|
@@ -11,8 +19,11 @@ export default class ClickTone {
|
|
|
11
19
|
throttle?: number;
|
|
12
20
|
debug?: boolean;
|
|
13
21
|
});
|
|
14
|
-
|
|
22
|
+
private initAudioContext;
|
|
23
|
+
private iOSFixAudioContext;
|
|
24
|
+
private fetchAndDecodeAudio;
|
|
25
|
+
private playAudio;
|
|
26
|
+
private throttleFn;
|
|
27
|
+
play(url?: string): void;
|
|
15
28
|
}
|
|
16
29
|
export default ClickTone;
|
|
17
|
-
|
|
18
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.es.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
var d = Object.defineProperty;
|
|
2
|
+
var h = (n, t, e) => t in n ? d(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
|
|
3
|
+
var o = (n, t, e) => h(n, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
+
class u {
|
|
5
|
+
constructor({ file: t, volume: e = 1, callback: i = null, throttle: a = 0, debug: s = !1 }) {
|
|
6
|
+
o(this, "file");
|
|
7
|
+
o(this, "volume");
|
|
8
|
+
o(this, "callback");
|
|
9
|
+
o(this, "throttle");
|
|
10
|
+
o(this, "debug");
|
|
11
|
+
o(this, "lastClickTime");
|
|
12
|
+
o(this, "audioCache");
|
|
13
|
+
o(this, "audioContext");
|
|
14
|
+
this.file = t, this.volume = e, this.callback = i, this.throttle = a, this.debug = s, this.lastClickTime = 0, this.audioCache = {}, this.audioContext = null;
|
|
15
|
+
}
|
|
16
|
+
initAudioContext() {
|
|
17
|
+
this.audioContext || (this.audioContext = new (window.AudioContext || window.webkitAudioContext)(), this.iOSFixAudioContext());
|
|
18
|
+
}
|
|
19
|
+
iOSFixAudioContext() {
|
|
20
|
+
if (this.audioContext && this.audioContext.state === "suspended" && "ontouchstart" in window) {
|
|
21
|
+
const t = () => {
|
|
22
|
+
this.audioContext.state === "suspended" && this.audioContext.resume().then(() => {
|
|
23
|
+
document.body.removeEventListener("touchstart", t), document.body.removeEventListener("touchend", t);
|
|
24
|
+
}).catch((e) => {
|
|
25
|
+
this.debug;
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
document.body.addEventListener("touchstart", t, !1), document.body.addEventListener("touchend", t, !1);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async fetchAndDecodeAudio(t) {
|
|
32
|
+
try {
|
|
33
|
+
if (this.audioCache[t]) return this.audioCache[t];
|
|
34
|
+
const e = await fetch(t), i = await e.arrayBuffer(), a = await this.audioContext.decodeAudioData(i);
|
|
35
|
+
return this.audioCache[t] = a, a;
|
|
36
|
+
} catch (e) {
|
|
37
|
+
throw this.debug, new Error(`Something went wrong when loading and decoding the audio: ${e.message}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async playAudio(t) {
|
|
41
|
+
this.initAudioContext();
|
|
42
|
+
try {
|
|
43
|
+
const e = await this.fetchAndDecodeAudio(t), i = this.audioContext.createBufferSource(), a = this.audioContext.createGain();
|
|
44
|
+
i.buffer = e, a.gain.value = this.volume, i.connect(a), a.connect(this.audioContext.destination), i.onended = () => {
|
|
45
|
+
this.callback && this.callback();
|
|
46
|
+
}, i.start(0);
|
|
47
|
+
} catch (e) {
|
|
48
|
+
throw this.debug, new Error(`Something went wrong while playing audio: ${e.message}`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
throttleFn(t) {
|
|
52
|
+
return () => {
|
|
53
|
+
const e = Date.now();
|
|
54
|
+
e - this.lastClickTime >= this.throttle && (t().catch((i) => {
|
|
55
|
+
this.debug;
|
|
56
|
+
}), this.lastClickTime = e);
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
play(t = this.file) {
|
|
60
|
+
const e = this.throttleFn(() => this.playAudio(t));
|
|
61
|
+
try {
|
|
62
|
+
e();
|
|
63
|
+
} catch (i) {
|
|
64
|
+
if (this.debug, !this.callback) throw i;
|
|
65
|
+
this.callback(i);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export {
|
|
70
|
+
u as default
|
|
71
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(e,t){typeof exports=="object"&&typeof module<"u"?module.exports=t():typeof define=="function"&&define.amd?define(t):(e=typeof globalThis<"u"?globalThis:e||self).ClickTone=t()})(this,function(){"use strict";var d=Object.defineProperty;var s=(e,t,i)=>t in e?d(e,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):e[t]=i;var o=(e,t,i)=>s(e,typeof t!="symbol"?t+"":t,i);return class{constructor({file:e,volume:t=1,callback:i=null,throttle:n=0,debug:a=!1}){o(this,"file");o(this,"volume");o(this,"callback");o(this,"throttle");o(this,"debug");o(this,"lastClickTime");o(this,"audioCache");o(this,"audioContext");this.file=e,this.volume=t,this.callback=i,this.throttle=n,this.debug=a,this.lastClickTime=0,this.audioCache={},this.audioContext=null}initAudioContext(){this.audioContext||(this.audioContext=new(window.AudioContext||window.webkitAudioContext),this.iOSFixAudioContext())}iOSFixAudioContext(){if(this.audioContext&&this.audioContext.state==="suspended"&&"ontouchstart"in window){const e=()=>{this.audioContext.state==="suspended"&&this.audioContext.resume().then(()=>{document.body.removeEventListener("touchstart",e),document.body.removeEventListener("touchend",e)}).catch(t=>{this.debug})};document.body.addEventListener("touchstart",e,!1),document.body.addEventListener("touchend",e,!1)}}async fetchAndDecodeAudio(e){try{if(this.audioCache[e])return this.audioCache[e];const t=await fetch(e),i=await t.arrayBuffer(),n=await this.audioContext.decodeAudioData(i);return this.audioCache[e]=n,n}catch(t){throw this.debug,new Error(`Something went wrong when loading and decoding the audio: ${t.message}`)}}async playAudio(e){this.initAudioContext();try{const t=await this.fetchAndDecodeAudio(e),i=this.audioContext.createBufferSource(),n=this.audioContext.createGain();i.buffer=t,n.gain.value=this.volume,i.connect(n),n.connect(this.audioContext.destination),i.onended=()=>{this.callback&&this.callback()},i.start(0)}catch(t){throw this.debug,new Error(`Something went wrong while playing audio: ${t.message}`)}}throttleFn(e){return()=>{const t=Date.now();t-this.lastClickTime>=this.throttle&&(e().catch(i=>{this.debug}),this.lastClickTime=t)}}play(e=this.file){const t=this.throttleFn(()=>this.playAudio(e));try{t()}catch(i){if(this.debug,!this.callback)throw i;this.callback(i)}}}});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clicktone",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "ClickTone is designed to control audio playback with various settings, including volume control, callback and debug mode. It also includes iOS support.",
|
|
5
5
|
"author": "ux-ui.pro",
|
|
6
6
|
"license": "MIT",
|
|
@@ -14,26 +14,41 @@
|
|
|
14
14
|
"homepage": "https://github.com/ux-ui-pro/clicktone",
|
|
15
15
|
"sideEffects": false,
|
|
16
16
|
"scripts": {
|
|
17
|
-
"clean": "
|
|
18
|
-
"build": "
|
|
17
|
+
"clean": "rimraf dist",
|
|
18
|
+
"build": "vite build",
|
|
19
19
|
"lint:js": "eslint **/*.{ts,js}",
|
|
20
|
-
"
|
|
20
|
+
"lint:fix:js": "eslint **/*.{ts,js} --fix",
|
|
21
|
+
"format:js": "prettier --write **/*.{ts,js}",
|
|
22
|
+
"lint:fix": "yarn lint:fix:js && yarn format:js"
|
|
21
23
|
},
|
|
22
|
-
"browserslist": "> 0.5%, last 2 versions, not dead",
|
|
23
24
|
"source": "src/index.ts",
|
|
24
|
-
"main": "dist/index.js",
|
|
25
|
-
"module": "dist/index.
|
|
25
|
+
"main": "dist/index.cjs.js",
|
|
26
|
+
"module": "dist/index.es.js",
|
|
27
|
+
"browser": "./dist/index.umd.js",
|
|
26
28
|
"types": "dist/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
"import": "./dist/index.es.js",
|
|
31
|
+
"require": "./dist/index.cjs.js"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist/"
|
|
35
|
+
],
|
|
27
36
|
"devDependencies": {
|
|
28
|
-
"@
|
|
29
|
-
"@
|
|
30
|
-
"@
|
|
31
|
-
"@typescript-eslint/
|
|
32
|
-
"eslint": "
|
|
33
|
-
"eslint
|
|
34
|
-
"eslint-
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
+
"@eslint/js": "9.21.0",
|
|
38
|
+
"@rollup/plugin-terser": "0.4.4",
|
|
39
|
+
"@types/node": "22.13.8",
|
|
40
|
+
"@typescript-eslint/eslint-plugin": "8.25.0",
|
|
41
|
+
"@typescript-eslint/parser": "8.25.0",
|
|
42
|
+
"eslint": "9.18.0",
|
|
43
|
+
"eslint-config-prettier": "10.0.2",
|
|
44
|
+
"eslint-import-resolver-typescript": "3.8.3",
|
|
45
|
+
"eslint-plugin-import": "2.31.0",
|
|
46
|
+
"globals": "16.0.0",
|
|
47
|
+
"prettier": "3.5.2",
|
|
48
|
+
"rimraf": "6.0.1",
|
|
49
|
+
"typescript": "5.8.2",
|
|
50
|
+
"vite": "6.2.0",
|
|
51
|
+
"vite-plugin-dts": "4.5.1"
|
|
37
52
|
},
|
|
38
53
|
"keywords": [
|
|
39
54
|
"MP3",
|
package/.eslintignore
DELETED
package/.eslintrc.json
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"env": {
|
|
3
|
-
"browser": true,
|
|
4
|
-
"es2022": true
|
|
5
|
-
},
|
|
6
|
-
"extends": [
|
|
7
|
-
"airbnb-base",
|
|
8
|
-
"plugin:@typescript-eslint/recommended"
|
|
9
|
-
],
|
|
10
|
-
"parser": "@typescript-eslint/parser",
|
|
11
|
-
"parserOptions": {
|
|
12
|
-
"ecmaVersion": "latest",
|
|
13
|
-
"sourceType": "module",
|
|
14
|
-
"project": "./tsconfig.json"
|
|
15
|
-
},
|
|
16
|
-
"plugins": ["@typescript-eslint"],
|
|
17
|
-
"rules": {
|
|
18
|
-
"max-len": [
|
|
19
|
-
"off",
|
|
20
|
-
{
|
|
21
|
-
"code": 100,
|
|
22
|
-
"ignoreUrls": true
|
|
23
|
-
}
|
|
24
|
-
]
|
|
25
|
-
}
|
|
26
|
-
}
|
package/code_of_conduct.md
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
# Contributor Covenant Code of Conduct
|
|
2
|
-
|
|
3
|
-
## Our Pledge
|
|
4
|
-
|
|
5
|
-
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, ethnicity, level of experience, nationality, personal appearance, race, or religion.
|
|
6
|
-
|
|
7
|
-
## Our Standards
|
|
8
|
-
|
|
9
|
-
Examples of behavior that contributes to creating a positive environment include:
|
|
10
|
-
|
|
11
|
-
* Using welcoming language
|
|
12
|
-
* Being respectful of differing viewpoints and experiences
|
|
13
|
-
* Gracefully accepting constructive criticism
|
|
14
|
-
* Focusing on what is best for the community
|
|
15
|
-
|
|
16
|
-
Examples of unacceptable behavior by participants include:
|
|
17
|
-
|
|
18
|
-
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
19
|
-
* Public or private harassment
|
|
20
|
-
* Publishing others' private information, such as a physical or electronic address, without explicit permission
|
|
21
|
-
* Other conduct which could reasonably be considered inappropriate in a professional setting
|
|
22
|
-
|
|
23
|
-
## Our Responsibilities
|
|
24
|
-
|
|
25
|
-
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
|
|
26
|
-
|
|
27
|
-
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
|
|
28
|
-
|
|
29
|
-
## Scope
|
|
30
|
-
|
|
31
|
-
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
|
|
32
|
-
|
|
33
|
-
## Enforcement
|
|
34
|
-
|
|
35
|
-
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
36
|
-
|
|
37
|
-
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":"AAAA,QAAQ,MAAM,CAAC;IACb,UAAU,MAAM;QACd,kBAAkB,CAAC,EAAE,OAAO,YAAY,CAAC;KAC1C;CACF;AAED,qBAAM,SAAS;gBAiBD,EACV,IAAI,EACJ,MAAY,EACZ,QAAe,EACf,QAAY,EACZ,KAAa,GACd,EAAE;QACD,IAAI,EAAE,MAAM,CAAC;QACb,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,EAAE,KAAK,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC;QAC5C,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,KAAK,CAAC,EAAE,OAAO,CAAC;KACjB;IAiGM,IAAI,SAAe,MAAM,KAAe,QAAQ,IAAI,CAAC,CAiB1D;CACH;AAED,eAAe,SAAS,CAAC","sources":["src/src/index.ts","src/index.ts"],"sourcesContent":[null,"declare global {\n interface Window {\n webkitAudioContext?: typeof AudioContext;\n }\n}\n\nclass ClickTone {\n private readonly file: string;\n\n private readonly volume: number;\n\n private readonly callback: ((error?: Error) => void) | null;\n\n private readonly throttle: number;\n\n private readonly debug: boolean;\n\n private lastClickTime: number;\n\n private readonly audioCache: Record<string, AudioBuffer>;\n\n private audioContext: AudioContext | null;\n\n constructor({\n file,\n volume = 1.0,\n callback = null,\n throttle = 0,\n debug = false,\n }: {\n file: string;\n volume?: number;\n callback?: ((error?: Error) => void) | null;\n throttle?: number;\n debug?: boolean;\n }) {\n this.file = file;\n this.volume = volume;\n this.callback = callback;\n this.throttle = throttle;\n this.debug = debug;\n this.lastClickTime = 0;\n this.audioCache = {};\n this.audioContext = null;\n }\n\n private initAudioContext(): void {\n if (!this.audioContext) {\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n }\n\n private iOSFixAudioContext = (): void => {\n if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = (): void => {\n if (this.audioContext!.state === 'suspended') {\n this.audioContext!.resume().then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n });\n }\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n };\n\n private fetchAndDecodeAudio = async (url: string): Promise<AudioBuffer> => {\n try {\n if (this.audioCache[url]) {\n return this.audioCache[url];\n }\n\n const response = await fetch(url);\n const buffer = await response.arrayBuffer();\n const audioData = await this.audioContext!.decodeAudioData(buffer);\n\n this.audioCache[url] = audioData;\n\n return audioData;\n } catch (error) {\n if (this.debug) {\n // eslint-disable-next-line no-console\n console.error('Audio loading and decoding error: ', error);\n }\n\n throw new Error(`Something went wrong when loading and decoding the audio: ${(error as Error).message}`);\n }\n };\n\n private playAudio = async (url: string): Promise<void> => {\n this.initAudioContext();\n\n try {\n const audioData = await this.fetchAndDecodeAudio(url);\n const source = this.audioContext!.createBufferSource();\n const gainNode = this.audioContext!.createGain();\n\n source.buffer = audioData;\n gainNode.gain.value = this.volume;\n source.connect(gainNode);\n gainNode.connect(this.audioContext!.destination);\n\n source.onended = () => {\n if (this.callback) {\n this.callback();\n }\n };\n\n source.start(0);\n } catch (error) {\n if (this.debug) {\n // eslint-disable-next-line no-console\n console.error('Audio playback error: ', error);\n }\n\n throw new Error(`Something went wrong while playing audio: ${(error as Error).message}`);\n }\n };\n\n private throttleFn = (func: () => void): (() => void) => () => {\n const now = Date.now();\n\n if (now - this.lastClickTime >= this.throttle) {\n func();\n\n this.lastClickTime = now;\n }\n };\n\n public play = async (url: string = this.file): Promise<void> => {\n const throttledPlay = this.throttleFn(() => this.playAudio(url));\n\n try {\n await throttledPlay();\n } catch (error) {\n if (this.debug) {\n // eslint-disable-next-line no-console\n console.error('Audio playback error: ', error);\n }\n\n if (this.callback) {\n this.callback(error as Error);\n } else {\n throw error;\n }\n }\n };\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.d.ts.map"}
|
package/dist/index.js
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
function $parcel$defineInteropFlag(a) {
|
|
3
|
-
Object.defineProperty(a, '__esModule', {value: true, configurable: true});
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
function $parcel$export(e, n, v, s) {
|
|
7
|
-
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
$parcel$defineInteropFlag(module.exports);
|
|
11
|
-
|
|
12
|
-
$parcel$export(module.exports, "default", function () { return $a196c1ed25598f0e$export$2e2bcd8739ae039; });
|
|
13
|
-
class $a196c1ed25598f0e$var$ClickTone {
|
|
14
|
-
file;
|
|
15
|
-
volume;
|
|
16
|
-
callback;
|
|
17
|
-
throttle;
|
|
18
|
-
debug;
|
|
19
|
-
lastClickTime;
|
|
20
|
-
audioCache;
|
|
21
|
-
audioContext;
|
|
22
|
-
constructor({ file: file, volume: volume = 1.0, callback: callback = null, throttle: throttle = 0, debug: debug = false }){
|
|
23
|
-
this.file = file;
|
|
24
|
-
this.volume = volume;
|
|
25
|
-
this.callback = callback;
|
|
26
|
-
this.throttle = throttle;
|
|
27
|
-
this.debug = debug;
|
|
28
|
-
this.lastClickTime = 0;
|
|
29
|
-
this.audioCache = {};
|
|
30
|
-
this.audioContext = null;
|
|
31
|
-
}
|
|
32
|
-
initAudioContext() {
|
|
33
|
-
if (!this.audioContext) {
|
|
34
|
-
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
|
35
|
-
this.iOSFixAudioContext();
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
iOSFixAudioContext = ()=>{
|
|
39
|
-
if (this.audioContext && this.audioContext.state === "suspended" && "ontouchstart" in window) {
|
|
40
|
-
const unlock = ()=>{
|
|
41
|
-
if (this.audioContext.state === "suspended") this.audioContext.resume().then(()=>{
|
|
42
|
-
document.body.removeEventListener("touchstart", unlock);
|
|
43
|
-
document.body.removeEventListener("touchend", unlock);
|
|
44
|
-
});
|
|
45
|
-
};
|
|
46
|
-
document.body.addEventListener("touchstart", unlock, false);
|
|
47
|
-
document.body.addEventListener("touchend", unlock, false);
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
fetchAndDecodeAudio = async (url)=>{
|
|
51
|
-
try {
|
|
52
|
-
if (this.audioCache[url]) return this.audioCache[url];
|
|
53
|
-
const response = await fetch(url);
|
|
54
|
-
const buffer = await response.arrayBuffer();
|
|
55
|
-
const audioData = await this.audioContext.decodeAudioData(buffer);
|
|
56
|
-
this.audioCache[url] = audioData;
|
|
57
|
-
return audioData;
|
|
58
|
-
} catch (error) {
|
|
59
|
-
if (this.debug) // eslint-disable-next-line no-console
|
|
60
|
-
console.error("Audio loading and decoding error: ", error);
|
|
61
|
-
throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
playAudio = async (url)=>{
|
|
65
|
-
this.initAudioContext();
|
|
66
|
-
try {
|
|
67
|
-
const audioData = await this.fetchAndDecodeAudio(url);
|
|
68
|
-
const source = this.audioContext.createBufferSource();
|
|
69
|
-
const gainNode = this.audioContext.createGain();
|
|
70
|
-
source.buffer = audioData;
|
|
71
|
-
gainNode.gain.value = this.volume;
|
|
72
|
-
source.connect(gainNode);
|
|
73
|
-
gainNode.connect(this.audioContext.destination);
|
|
74
|
-
source.onended = ()=>{
|
|
75
|
-
if (this.callback) this.callback();
|
|
76
|
-
};
|
|
77
|
-
source.start(0);
|
|
78
|
-
} catch (error) {
|
|
79
|
-
if (this.debug) // eslint-disable-next-line no-console
|
|
80
|
-
console.error("Audio playback error: ", error);
|
|
81
|
-
throw new Error(`Something went wrong while playing audio: ${error.message}`);
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
throttleFn = (func)=>()=>{
|
|
85
|
-
const now = Date.now();
|
|
86
|
-
if (now - this.lastClickTime >= this.throttle) {
|
|
87
|
-
func();
|
|
88
|
-
this.lastClickTime = now;
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
play = async (url = this.file)=>{
|
|
92
|
-
const throttledPlay = this.throttleFn(()=>this.playAudio(url));
|
|
93
|
-
try {
|
|
94
|
-
await throttledPlay();
|
|
95
|
-
} catch (error) {
|
|
96
|
-
if (this.debug) // eslint-disable-next-line no-console
|
|
97
|
-
console.error("Audio playback error: ", error);
|
|
98
|
-
if (this.callback) this.callback(error);
|
|
99
|
-
else throw error;
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
var $a196c1ed25598f0e$export$2e2bcd8739ae039 = $a196c1ed25598f0e$var$ClickTone;
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;;AAMA,MAAM;IACa,KAAa;IAEb,OAAe;IAEf,SAA2C;IAE3C,SAAiB;IAEjB,MAAe;IAExB,cAAsB;IAEb,WAAwC;IAEjD,aAAkC;IAE1C,YAAY,QACV,IAAI,UACJ,SAAS,eACT,WAAW,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,sCAAsC;YACtC,QAAQ,KAAK,CAAC,0BAA0B;YAG1C,IAAI,IAAI,CAAC,QAAQ,EACf,IAAI,CAAC,QAAQ,CAAC;iBAEd,MAAM;QAEV;IACF,EAAE;AACJ;IAEA,2CAAe","sources":["src/index.ts"],"sourcesContent":["declare global {\n interface Window {\n webkitAudioContext?: typeof AudioContext;\n }\n}\n\nclass ClickTone {\n private readonly file: string;\n\n private readonly volume: number;\n\n private readonly callback: ((error?: Error) => void) | null;\n\n private readonly throttle: number;\n\n private readonly debug: boolean;\n\n private lastClickTime: number;\n\n private readonly audioCache: Record<string, AudioBuffer>;\n\n private audioContext: AudioContext | null;\n\n constructor({\n file,\n volume = 1.0,\n callback = null,\n throttle = 0,\n debug = false,\n }: {\n file: string;\n volume?: number;\n callback?: ((error?: Error) => void) | null;\n throttle?: number;\n debug?: boolean;\n }) {\n this.file = file;\n this.volume = volume;\n this.callback = callback;\n this.throttle = throttle;\n this.debug = debug;\n this.lastClickTime = 0;\n this.audioCache = {};\n this.audioContext = null;\n }\n\n private initAudioContext(): void {\n if (!this.audioContext) {\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n }\n\n private iOSFixAudioContext = (): void => {\n if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = (): void => {\n if (this.audioContext!.state === 'suspended') {\n this.audioContext!.resume().then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n });\n }\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n };\n\n private fetchAndDecodeAudio = async (url: string): Promise<AudioBuffer> => {\n try {\n if (this.audioCache[url]) {\n return this.audioCache[url];\n }\n\n const response = await fetch(url);\n const buffer = await response.arrayBuffer();\n const audioData = await this.audioContext!.decodeAudioData(buffer);\n\n this.audioCache[url] = audioData;\n\n return audioData;\n } catch (error) {\n if (this.debug) {\n // eslint-disable-next-line no-console\n console.error('Audio loading and decoding error: ', error);\n }\n\n throw new Error(`Something went wrong when loading and decoding the audio: ${(error as Error).message}`);\n }\n };\n\n private playAudio = async (url: string): Promise<void> => {\n this.initAudioContext();\n\n try {\n const audioData = await this.fetchAndDecodeAudio(url);\n const source = this.audioContext!.createBufferSource();\n const gainNode = this.audioContext!.createGain();\n\n source.buffer = audioData;\n gainNode.gain.value = this.volume;\n source.connect(gainNode);\n gainNode.connect(this.audioContext!.destination);\n\n source.onended = () => {\n if (this.callback) {\n this.callback();\n }\n };\n\n source.start(0);\n } catch (error) {\n if (this.debug) {\n // eslint-disable-next-line no-console\n console.error('Audio playback error: ', error);\n }\n\n throw new Error(`Something went wrong while playing audio: ${(error as Error).message}`);\n }\n };\n\n private throttleFn = (func: () => void): (() => void) => () => {\n const now = Date.now();\n\n if (now - this.lastClickTime >= this.throttle) {\n func();\n\n this.lastClickTime = now;\n }\n };\n\n public play = async (url: string = this.file): Promise<void> => {\n const throttledPlay = this.throttleFn(() => this.playAudio(url));\n\n try {\n await throttledPlay();\n } catch (error) {\n if (this.debug) {\n // eslint-disable-next-line no-console\n console.error('Audio playback error: ', error);\n }\n\n if (this.callback) {\n this.callback(error as Error);\n } else {\n throw error;\n }\n }\n };\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.js.map"}
|
package/dist/index.module.js
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
class $643fcf18b2d2e76f$var$ClickTone {
|
|
2
|
-
file;
|
|
3
|
-
volume;
|
|
4
|
-
callback;
|
|
5
|
-
throttle;
|
|
6
|
-
debug;
|
|
7
|
-
lastClickTime;
|
|
8
|
-
audioCache;
|
|
9
|
-
audioContext;
|
|
10
|
-
constructor({ file: file, volume: volume = 1.0, callback: callback = null, throttle: throttle = 0, debug: debug = false }){
|
|
11
|
-
this.file = file;
|
|
12
|
-
this.volume = volume;
|
|
13
|
-
this.callback = callback;
|
|
14
|
-
this.throttle = throttle;
|
|
15
|
-
this.debug = debug;
|
|
16
|
-
this.lastClickTime = 0;
|
|
17
|
-
this.audioCache = {};
|
|
18
|
-
this.audioContext = null;
|
|
19
|
-
}
|
|
20
|
-
initAudioContext() {
|
|
21
|
-
if (!this.audioContext) {
|
|
22
|
-
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
|
23
|
-
this.iOSFixAudioContext();
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
iOSFixAudioContext = ()=>{
|
|
27
|
-
if (this.audioContext && this.audioContext.state === "suspended" && "ontouchstart" in window) {
|
|
28
|
-
const unlock = ()=>{
|
|
29
|
-
if (this.audioContext.state === "suspended") this.audioContext.resume().then(()=>{
|
|
30
|
-
document.body.removeEventListener("touchstart", unlock);
|
|
31
|
-
document.body.removeEventListener("touchend", unlock);
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
|
-
document.body.addEventListener("touchstart", unlock, false);
|
|
35
|
-
document.body.addEventListener("touchend", unlock, false);
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
fetchAndDecodeAudio = async (url)=>{
|
|
39
|
-
try {
|
|
40
|
-
if (this.audioCache[url]) return this.audioCache[url];
|
|
41
|
-
const response = await fetch(url);
|
|
42
|
-
const buffer = await response.arrayBuffer();
|
|
43
|
-
const audioData = await this.audioContext.decodeAudioData(buffer);
|
|
44
|
-
this.audioCache[url] = audioData;
|
|
45
|
-
return audioData;
|
|
46
|
-
} catch (error) {
|
|
47
|
-
if (this.debug) // eslint-disable-next-line no-console
|
|
48
|
-
console.error("Audio loading and decoding error: ", error);
|
|
49
|
-
throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
playAudio = async (url)=>{
|
|
53
|
-
this.initAudioContext();
|
|
54
|
-
try {
|
|
55
|
-
const audioData = await this.fetchAndDecodeAudio(url);
|
|
56
|
-
const source = this.audioContext.createBufferSource();
|
|
57
|
-
const gainNode = this.audioContext.createGain();
|
|
58
|
-
source.buffer = audioData;
|
|
59
|
-
gainNode.gain.value = this.volume;
|
|
60
|
-
source.connect(gainNode);
|
|
61
|
-
gainNode.connect(this.audioContext.destination);
|
|
62
|
-
source.onended = ()=>{
|
|
63
|
-
if (this.callback) this.callback();
|
|
64
|
-
};
|
|
65
|
-
source.start(0);
|
|
66
|
-
} catch (error) {
|
|
67
|
-
if (this.debug) // eslint-disable-next-line no-console
|
|
68
|
-
console.error("Audio playback error: ", error);
|
|
69
|
-
throw new Error(`Something went wrong while playing audio: ${error.message}`);
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
throttleFn = (func)=>()=>{
|
|
73
|
-
const now = Date.now();
|
|
74
|
-
if (now - this.lastClickTime >= this.throttle) {
|
|
75
|
-
func();
|
|
76
|
-
this.lastClickTime = now;
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
play = async (url = this.file)=>{
|
|
80
|
-
const throttledPlay = this.throttleFn(()=>this.playAudio(url));
|
|
81
|
-
try {
|
|
82
|
-
await throttledPlay();
|
|
83
|
-
} catch (error) {
|
|
84
|
-
if (this.debug) // eslint-disable-next-line no-console
|
|
85
|
-
console.error("Audio playback error: ", error);
|
|
86
|
-
if (this.callback) this.callback(error);
|
|
87
|
-
else throw error;
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
var $643fcf18b2d2e76f$export$2e2bcd8739ae039 = $643fcf18b2d2e76f$var$ClickTone;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
export {$643fcf18b2d2e76f$export$2e2bcd8739ae039 as default};
|
|
95
|
-
//# sourceMappingURL=index.module.js.map
|
package/dist/index.module.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":"AAMA,MAAM;IACa,KAAa;IAEb,OAAe;IAEf,SAA2C;IAE3C,SAAiB;IAEjB,MAAe;IAExB,cAAsB;IAEb,WAAwC;IAEjD,aAAkC;IAE1C,YAAY,QACV,IAAI,UACJ,SAAS,eACT,WAA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ACZ,sCAAsC;YACtC,QAAQ,KAAK,CAAC,0BAA0B;YAG1C,IAAI,IAAI,CAAC,QAAQ,EACf,IAAI,CAAC,QAAQ,CAAC;iBAEd,MAAM;QAEV;IACF,EAAE;AACJ;IAEA,2CAAe","sources":["src/index.ts"],"sourcesContent":["declare global {\n interface Window {\n webkitAudioContext?: typeof AudioContext;\n }\n}\n\nclass ClickTone {\n private readonly file: string;\n\n private readonly volume: number;\n\n private readonly callback: ((error?: Error) => void) | null;\n\n private readonly throttle: number;\n\n private readonly debug: boolean;\n\n private lastClickTime: number;\n\n private readonly audioCache: Record<string, AudioBuffer>;\n\n private audioContext: AudioContext | null;\n\n constructor({\n file,\n volume = 1.0,\n callback = null,\n throttle = 0,\n debug = false,\n }: {\n file: string;\n volume?: number;\n callback?: ((error?: Error) => void) | null;\n throttle?: number;\n debug?: boolean;\n }) {\n this.file = file;\n this.volume = volume;\n this.callback = callback;\n this.throttle = throttle;\n this.debug = debug;\n this.lastClickTime = 0;\n this.audioCache = {};\n this.audioContext = null;\n }\n\n private initAudioContext(): void {\n if (!this.audioContext) {\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n }\n\n private iOSFixAudioContext = (): void => {\n if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = (): void => {\n if (this.audioContext!.state === 'suspended') {\n this.audioContext!.resume().then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n });\n }\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n };\n\n private fetchAndDecodeAudio = async (url: string): Promise<AudioBuffer> => {\n try {\n if (this.audioCache[url]) {\n return this.audioCache[url];\n }\n\n const response = await fetch(url);\n const buffer = await response.arrayBuffer();\n const audioData = await this.audioContext!.decodeAudioData(buffer);\n\n this.audioCache[url] = audioData;\n\n return audioData;\n } catch (error) {\n if (this.debug) {\n // eslint-disable-next-line no-console\n console.error('Audio loading and decoding error: ', error);\n }\n\n throw new Error(`Something went wrong when loading and decoding the audio: ${(error as Error).message}`);\n }\n };\n\n private playAudio = async (url: string): Promise<void> => {\n this.initAudioContext();\n\n try {\n const audioData = await this.fetchAndDecodeAudio(url);\n const source = this.audioContext!.createBufferSource();\n const gainNode = this.audioContext!.createGain();\n\n source.buffer = audioData;\n gainNode.gain.value = this.volume;\n source.connect(gainNode);\n gainNode.connect(this.audioContext!.destination);\n\n source.onended = () => {\n if (this.callback) {\n this.callback();\n }\n };\n\n source.start(0);\n } catch (error) {\n if (this.debug) {\n // eslint-disable-next-line no-console\n console.error('Audio playback error: ', error);\n }\n\n throw new Error(`Something went wrong while playing audio: ${(error as Error).message}`);\n }\n };\n\n private throttleFn = (func: () => void): (() => void) => () => {\n const now = Date.now();\n\n if (now - this.lastClickTime >= this.throttle) {\n func();\n\n this.lastClickTime = now;\n }\n };\n\n public play = async (url: string = this.file): Promise<void> => {\n const throttledPlay = this.throttleFn(() => this.playAudio(url));\n\n try {\n await throttledPlay();\n } catch (error) {\n if (this.debug) {\n // eslint-disable-next-line no-console\n console.error('Audio playback error: ', error);\n }\n\n if (this.callback) {\n this.callback(error as Error);\n } else {\n throw error;\n }\n }\n };\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.module.js.map"}
|
package/src/index.ts
DELETED
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
declare global {
|
|
2
|
-
interface Window {
|
|
3
|
-
webkitAudioContext?: typeof AudioContext;
|
|
4
|
-
}
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
class ClickTone {
|
|
8
|
-
private readonly file: string;
|
|
9
|
-
|
|
10
|
-
private readonly volume: number;
|
|
11
|
-
|
|
12
|
-
private readonly callback: ((error?: Error) => void) | null;
|
|
13
|
-
|
|
14
|
-
private readonly throttle: number;
|
|
15
|
-
|
|
16
|
-
private readonly debug: boolean;
|
|
17
|
-
|
|
18
|
-
private lastClickTime: number;
|
|
19
|
-
|
|
20
|
-
private readonly audioCache: Record<string, AudioBuffer>;
|
|
21
|
-
|
|
22
|
-
private audioContext: AudioContext | null;
|
|
23
|
-
|
|
24
|
-
constructor({
|
|
25
|
-
file,
|
|
26
|
-
volume = 1.0,
|
|
27
|
-
callback = null,
|
|
28
|
-
throttle = 0,
|
|
29
|
-
debug = false,
|
|
30
|
-
}: {
|
|
31
|
-
file: string;
|
|
32
|
-
volume?: number;
|
|
33
|
-
callback?: ((error?: Error) => void) | null;
|
|
34
|
-
throttle?: number;
|
|
35
|
-
debug?: boolean;
|
|
36
|
-
}) {
|
|
37
|
-
this.file = file;
|
|
38
|
-
this.volume = volume;
|
|
39
|
-
this.callback = callback;
|
|
40
|
-
this.throttle = throttle;
|
|
41
|
-
this.debug = debug;
|
|
42
|
-
this.lastClickTime = 0;
|
|
43
|
-
this.audioCache = {};
|
|
44
|
-
this.audioContext = null;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
private initAudioContext(): void {
|
|
48
|
-
if (!this.audioContext) {
|
|
49
|
-
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
|
50
|
-
this.iOSFixAudioContext();
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
private iOSFixAudioContext = (): void => {
|
|
55
|
-
if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {
|
|
56
|
-
const unlock = (): void => {
|
|
57
|
-
if (this.audioContext!.state === 'suspended') {
|
|
58
|
-
this.audioContext!.resume().then(() => {
|
|
59
|
-
document.body.removeEventListener('touchstart', unlock);
|
|
60
|
-
document.body.removeEventListener('touchend', unlock);
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
document.body.addEventListener('touchstart', unlock, false);
|
|
66
|
-
document.body.addEventListener('touchend', unlock, false);
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
private fetchAndDecodeAudio = async (url: string): Promise<AudioBuffer> => {
|
|
71
|
-
try {
|
|
72
|
-
if (this.audioCache[url]) {
|
|
73
|
-
return this.audioCache[url];
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const response = await fetch(url);
|
|
77
|
-
const buffer = await response.arrayBuffer();
|
|
78
|
-
const audioData = await this.audioContext!.decodeAudioData(buffer);
|
|
79
|
-
|
|
80
|
-
this.audioCache[url] = audioData;
|
|
81
|
-
|
|
82
|
-
return audioData;
|
|
83
|
-
} catch (error) {
|
|
84
|
-
if (this.debug) {
|
|
85
|
-
// eslint-disable-next-line no-console
|
|
86
|
-
console.error('Audio loading and decoding error: ', error);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
throw new Error(`Something went wrong when loading and decoding the audio: ${(error as Error).message}`);
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
private playAudio = async (url: string): Promise<void> => {
|
|
94
|
-
this.initAudioContext();
|
|
95
|
-
|
|
96
|
-
try {
|
|
97
|
-
const audioData = await this.fetchAndDecodeAudio(url);
|
|
98
|
-
const source = this.audioContext!.createBufferSource();
|
|
99
|
-
const gainNode = this.audioContext!.createGain();
|
|
100
|
-
|
|
101
|
-
source.buffer = audioData;
|
|
102
|
-
gainNode.gain.value = this.volume;
|
|
103
|
-
source.connect(gainNode);
|
|
104
|
-
gainNode.connect(this.audioContext!.destination);
|
|
105
|
-
|
|
106
|
-
source.onended = () => {
|
|
107
|
-
if (this.callback) {
|
|
108
|
-
this.callback();
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
source.start(0);
|
|
113
|
-
} catch (error) {
|
|
114
|
-
if (this.debug) {
|
|
115
|
-
// eslint-disable-next-line no-console
|
|
116
|
-
console.error('Audio playback error: ', error);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
throw new Error(`Something went wrong while playing audio: ${(error as Error).message}`);
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
private throttleFn = (func: () => void): (() => void) => () => {
|
|
124
|
-
const now = Date.now();
|
|
125
|
-
|
|
126
|
-
if (now - this.lastClickTime >= this.throttle) {
|
|
127
|
-
func();
|
|
128
|
-
|
|
129
|
-
this.lastClickTime = now;
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
public play = async (url: string = this.file): Promise<void> => {
|
|
134
|
-
const throttledPlay = this.throttleFn(() => this.playAudio(url));
|
|
135
|
-
|
|
136
|
-
try {
|
|
137
|
-
await throttledPlay();
|
|
138
|
-
} catch (error) {
|
|
139
|
-
if (this.debug) {
|
|
140
|
-
// eslint-disable-next-line no-console
|
|
141
|
-
console.error('Audio playback error: ', error);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
if (this.callback) {
|
|
145
|
-
this.callback(error as Error);
|
|
146
|
-
} else {
|
|
147
|
-
throw error;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
export default ClickTone;
|