clicktone 1.7.0 → 1.8.0-dev.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.cjs.js +1 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.es.js +46 -24
- package/dist/index.umd.js +1 -1
- package/package.json +19 -15
package/README.md
CHANGED
|
@@ -34,8 +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
|
-
|
|
38
|
-
file: new URL('./sound.mp3', import.meta.url).href,
|
|
37
|
+
file: './sound.mp3',
|
|
38
|
+
// file: new URL('./sound.mp3', import.meta.url).href,
|
|
39
39
|
volume: 0.7,
|
|
40
40
|
throttle: 100,
|
|
41
41
|
callback: () => { console.log('Playback ended') },
|
package/dist/index.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var s=Object.defineProperty;var
|
|
1
|
+
"use strict";var s=Object.defineProperty;var a=(t,e,i)=>e in t?s(t,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):t[e]=i;var n=(t,e,i)=>a(t,typeof e!="symbol"?e+"":e,i);module.exports=class{constructor({file:t,volume:e=1,callback:i=null,throttle:o=0,debug:r=!1}){n(this,"fileSource");n(this,"volume");n(this,"callback");n(this,"throttle");n(this,"debug");n(this,"lastClickTime");n(this,"audioCache");n(this,"audioContext");this.fileSource=t,this.volume=e,this.callback=i,this.throttle=o,this.debug=r,this.lastClickTime=0,this.audioCache={},this.audioContext=null}resolveFileUrl(t){if(typeof t=="string")return t;if(t instanceof HTMLSourceElement){if(!t.src)throw new Error('<source> element has no "src" attribute.');return t.src}if(typeof t=="object"&&t!==null&&"id"in t){const e=document.getElementById(String(t.id));if(!e)throw new Error(`No element found with id "${t.id}".`);if(!(e instanceof HTMLSourceElement))throw new Error(`Element with id "${t.id}" is not a <source> element.`);if(!e.src)throw new Error(`<source> element with id "${t.id}" has no "src" attribute.`);return e.src}throw new Error('Invalid "file" value. Expected string, HTMLSourceElement, or { id: string }.')}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(),o=await this.audioContext.decodeAudioData(i);return this.audioCache[t]=o,o}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(),o=this.audioContext.createGain();i.buffer=e,o.gain.value=this.volume,i.connect(o),o.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){let e;try{e=this.resolveFileUrl(t??this.fileSource)}catch(o){if(this.callback)return void this.callback(o);throw o}const i=this.throttleFn(()=>this.playAudio(e));try{i()}catch(o){if(this.debug,!this.callback)throw o;this.callback(o)}}};
|
package/dist/index.d.ts
CHANGED
|
@@ -3,8 +3,11 @@ declare global {
|
|
|
3
3
|
webkitAudioContext?: typeof AudioContext;
|
|
4
4
|
}
|
|
5
5
|
}
|
|
6
|
+
type FileSource = string | HTMLSourceElement | {
|
|
7
|
+
id: string;
|
|
8
|
+
};
|
|
6
9
|
declare class ClickTone {
|
|
7
|
-
private readonly
|
|
10
|
+
private readonly fileSource;
|
|
8
11
|
private readonly volume;
|
|
9
12
|
private readonly callback;
|
|
10
13
|
private readonly throttle;
|
|
@@ -13,17 +16,18 @@ declare class ClickTone {
|
|
|
13
16
|
private readonly audioCache;
|
|
14
17
|
private audioContext;
|
|
15
18
|
constructor({ file, volume, callback, throttle, debug, }: {
|
|
16
|
-
file:
|
|
19
|
+
file: FileSource;
|
|
17
20
|
volume?: number;
|
|
18
21
|
callback?: ((error?: Error) => void) | null;
|
|
19
22
|
throttle?: number;
|
|
20
23
|
debug?: boolean;
|
|
21
24
|
});
|
|
25
|
+
private resolveFileUrl;
|
|
22
26
|
private initAudioContext;
|
|
23
27
|
private iOSFixAudioContext;
|
|
24
28
|
private fetchAndDecodeAudio;
|
|
25
29
|
private playAudio;
|
|
26
30
|
private throttleFn;
|
|
27
|
-
play(
|
|
31
|
+
play(file?: FileSource): void;
|
|
28
32
|
}
|
|
29
33
|
export default ClickTone;
|
package/dist/index.es.js
CHANGED
|
@@ -1,17 +1,32 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
class
|
|
5
|
-
constructor({ file: t, volume: e = 1, callback:
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
this.
|
|
1
|
+
var s = Object.defineProperty;
|
|
2
|
+
var c = (r, t, e) => t in r ? s(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
|
|
3
|
+
var n = (r, t, e) => c(r, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
+
class h {
|
|
5
|
+
constructor({ file: t, volume: e = 1, callback: o = null, throttle: i = 0, debug: a = !1 }) {
|
|
6
|
+
n(this, "fileSource");
|
|
7
|
+
n(this, "volume");
|
|
8
|
+
n(this, "callback");
|
|
9
|
+
n(this, "throttle");
|
|
10
|
+
n(this, "debug");
|
|
11
|
+
n(this, "lastClickTime");
|
|
12
|
+
n(this, "audioCache");
|
|
13
|
+
n(this, "audioContext");
|
|
14
|
+
this.fileSource = t, this.volume = e, this.callback = o, this.throttle = i, this.debug = a, this.lastClickTime = 0, this.audioCache = {}, this.audioContext = null;
|
|
15
|
+
}
|
|
16
|
+
resolveFileUrl(t) {
|
|
17
|
+
if (typeof t == "string") return t;
|
|
18
|
+
if (t instanceof HTMLSourceElement) {
|
|
19
|
+
if (!t.src) throw new Error('<source> element has no "src" attribute.');
|
|
20
|
+
return t.src;
|
|
21
|
+
}
|
|
22
|
+
if (typeof t == "object" && t !== null && "id" in t) {
|
|
23
|
+
const e = document.getElementById(String(t.id));
|
|
24
|
+
if (!e) throw new Error(`No element found with id "${t.id}".`);
|
|
25
|
+
if (!(e instanceof HTMLSourceElement)) throw new Error(`Element with id "${t.id}" is not a <source> element.`);
|
|
26
|
+
if (!e.src) throw new Error(`<source> element with id "${t.id}" has no "src" attribute.`);
|
|
27
|
+
return e.src;
|
|
28
|
+
}
|
|
29
|
+
throw new Error('Invalid "file" value. Expected string, HTMLSourceElement, or { id: string }.');
|
|
15
30
|
}
|
|
16
31
|
initAudioContext() {
|
|
17
32
|
this.audioContext || (this.audioContext = new (window.AudioContext || window.webkitAudioContext)(), this.iOSFixAudioContext());
|
|
@@ -31,8 +46,8 @@ class u {
|
|
|
31
46
|
async fetchAndDecodeAudio(t) {
|
|
32
47
|
try {
|
|
33
48
|
if (this.audioCache[t]) return this.audioCache[t];
|
|
34
|
-
const e = await fetch(t),
|
|
35
|
-
return this.audioCache[t] =
|
|
49
|
+
const e = await fetch(t), o = await e.arrayBuffer(), i = await this.audioContext.decodeAudioData(o);
|
|
50
|
+
return this.audioCache[t] = i, i;
|
|
36
51
|
} catch (e) {
|
|
37
52
|
throw this.debug, new Error(`Something went wrong when loading and decoding the audio: ${e.message}`);
|
|
38
53
|
}
|
|
@@ -40,10 +55,10 @@ class u {
|
|
|
40
55
|
async playAudio(t) {
|
|
41
56
|
this.initAudioContext();
|
|
42
57
|
try {
|
|
43
|
-
const e = await this.fetchAndDecodeAudio(t),
|
|
44
|
-
|
|
58
|
+
const e = await this.fetchAndDecodeAudio(t), o = this.audioContext.createBufferSource(), i = this.audioContext.createGain();
|
|
59
|
+
o.buffer = e, i.gain.value = this.volume, o.connect(i), i.connect(this.audioContext.destination), o.onended = () => {
|
|
45
60
|
this.callback && this.callback();
|
|
46
|
-
},
|
|
61
|
+
}, o.start(0);
|
|
47
62
|
} catch (e) {
|
|
48
63
|
throw this.debug, new Error(`Something went wrong while playing audio: ${e.message}`);
|
|
49
64
|
}
|
|
@@ -51,15 +66,22 @@ class u {
|
|
|
51
66
|
throttleFn(t) {
|
|
52
67
|
return () => {
|
|
53
68
|
const e = Date.now();
|
|
54
|
-
e - this.lastClickTime >= this.throttle && (t().catch((
|
|
69
|
+
e - this.lastClickTime >= this.throttle && (t().catch((o) => {
|
|
55
70
|
this.debug;
|
|
56
71
|
}), this.lastClickTime = e);
|
|
57
72
|
};
|
|
58
73
|
}
|
|
59
|
-
play(t
|
|
60
|
-
|
|
74
|
+
play(t) {
|
|
75
|
+
let e;
|
|
76
|
+
try {
|
|
77
|
+
e = this.resolveFileUrl(t ?? this.fileSource);
|
|
78
|
+
} catch (i) {
|
|
79
|
+
if (this.callback) return void this.callback(i);
|
|
80
|
+
throw i;
|
|
81
|
+
}
|
|
82
|
+
const o = this.throttleFn(() => this.playAudio(e));
|
|
61
83
|
try {
|
|
62
|
-
|
|
84
|
+
o();
|
|
63
85
|
} catch (i) {
|
|
64
86
|
if (this.debug, !this.callback) throw i;
|
|
65
87
|
this.callback(i);
|
|
@@ -67,5 +89,5 @@ class u {
|
|
|
67
89
|
}
|
|
68
90
|
}
|
|
69
91
|
export {
|
|
70
|
-
|
|
92
|
+
h as default
|
|
71
93
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(e
|
|
1
|
+
(function(t,e){typeof exports=="object"&&typeof module<"u"?module.exports=e():typeof define=="function"&&define.amd?define(e):(t=typeof globalThis<"u"?globalThis:t||self).ClickTone=e()})(this,function(){"use strict";var s=Object.defineProperty;var a=(t,e,i)=>e in t?s(t,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):t[e]=i;var n=(t,e,i)=>a(t,typeof e!="symbol"?e+"":e,i);return class{constructor({file:t,volume:e=1,callback:i=null,throttle:o=0,debug:r=!1}){n(this,"fileSource");n(this,"volume");n(this,"callback");n(this,"throttle");n(this,"debug");n(this,"lastClickTime");n(this,"audioCache");n(this,"audioContext");this.fileSource=t,this.volume=e,this.callback=i,this.throttle=o,this.debug=r,this.lastClickTime=0,this.audioCache={},this.audioContext=null}resolveFileUrl(t){if(typeof t=="string")return t;if(t instanceof HTMLSourceElement){if(!t.src)throw new Error('<source> element has no "src" attribute.');return t.src}if(typeof t=="object"&&t!==null&&"id"in t){const e=document.getElementById(String(t.id));if(!e)throw new Error(`No element found with id "${t.id}".`);if(!(e instanceof HTMLSourceElement))throw new Error(`Element with id "${t.id}" is not a <source> element.`);if(!e.src)throw new Error(`<source> element with id "${t.id}" has no "src" attribute.`);return e.src}throw new Error('Invalid "file" value. Expected string, HTMLSourceElement, or { id: string }.')}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(),o=await this.audioContext.decodeAudioData(i);return this.audioCache[t]=o,o}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(),o=this.audioContext.createGain();i.buffer=e,o.gain.value=this.volume,i.connect(o),o.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){let e;try{e=this.resolveFileUrl(t??this.fileSource)}catch(o){if(this.callback)return void this.callback(o);throw o}const i=this.throttleFn(()=>this.playAudio(e));try{i()}catch(o){if(this.debug,!this.callback)throw o;this.callback(o)}}}});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clicktone",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0-dev.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",
|
|
@@ -16,9 +16,9 @@
|
|
|
16
16
|
"scripts": {
|
|
17
17
|
"clean": "rimraf dist",
|
|
18
18
|
"build": "vite build",
|
|
19
|
-
"lint:js": "eslint
|
|
20
|
-
"lint:fix:js": "eslint
|
|
21
|
-
"format:js": "prettier --write
|
|
19
|
+
"lint:js": "eslint src/**/*.{ts,js}",
|
|
20
|
+
"lint:fix:js": "eslint src/**/*.{ts,js} --fix",
|
|
21
|
+
"format:js": "prettier --write src/**/*.{ts,js}",
|
|
22
22
|
"lint:fix": "yarn lint:fix:js && yarn format:js"
|
|
23
23
|
},
|
|
24
24
|
"source": "src/index.ts",
|
|
@@ -27,28 +27,32 @@
|
|
|
27
27
|
"browser": "./dist/index.umd.js",
|
|
28
28
|
"types": "dist/index.d.ts",
|
|
29
29
|
"exports": {
|
|
30
|
-
"
|
|
31
|
-
|
|
30
|
+
".": {
|
|
31
|
+
"require": "./dist/index.cjs.js",
|
|
32
|
+
"import": "./dist/index.es.js",
|
|
33
|
+
"default": "./dist/index.umd.js"
|
|
34
|
+
},
|
|
35
|
+
"./dist/*": "./dist/*"
|
|
32
36
|
},
|
|
33
37
|
"files": [
|
|
34
38
|
"dist/"
|
|
35
39
|
],
|
|
36
40
|
"devDependencies": {
|
|
37
|
-
"@eslint/js": "9.
|
|
41
|
+
"@eslint/js": "9.22.0",
|
|
38
42
|
"@rollup/plugin-terser": "0.4.4",
|
|
39
|
-
"@types/node": "22.13.
|
|
40
|
-
"@typescript-eslint/eslint-plugin": "8.
|
|
41
|
-
"@typescript-eslint/parser": "8.
|
|
42
|
-
"eslint": "9.
|
|
43
|
-
"eslint-config-prettier": "10.
|
|
43
|
+
"@types/node": "22.13.10",
|
|
44
|
+
"@typescript-eslint/eslint-plugin": "8.26.0",
|
|
45
|
+
"@typescript-eslint/parser": "8.26.0",
|
|
46
|
+
"eslint": "9.22.0",
|
|
47
|
+
"eslint-config-prettier": "10.1.1",
|
|
44
48
|
"eslint-import-resolver-typescript": "3.8.3",
|
|
45
49
|
"eslint-plugin-import": "2.31.0",
|
|
46
50
|
"globals": "16.0.0",
|
|
47
|
-
"prettier": "3.5.
|
|
51
|
+
"prettier": "3.5.3",
|
|
48
52
|
"rimraf": "6.0.1",
|
|
49
53
|
"typescript": "5.8.2",
|
|
50
|
-
"vite": "6.2.
|
|
51
|
-
"vite-plugin-dts": "4.5.
|
|
54
|
+
"vite": "6.2.1",
|
|
55
|
+
"vite-plugin-dts": "4.5.3"
|
|
52
56
|
},
|
|
53
57
|
"keywords": [
|
|
54
58
|
"MP3",
|