clicktone 1.5.3 → 1.6.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/{.eslintignore → .prettierignore} +2 -1
- package/.prettierrc.json +11 -0
- package/README.md +2 -1
- package/dist/{index.d.ts → clicktone.d.ts} +2 -2
- package/dist/clicktone.d.ts.map +1 -0
- package/dist/index.js +25 -29
- package/dist/index.js.map +1 -1
- package/dist/index.module.js +25 -29
- package/dist/index.module.js.map +1 -1
- package/eslint.config.mjs +72 -0
- package/package.json +19 -11
- package/src/index.ts +30 -18
- package/tsconfig.json +9 -1
- package/.eslintrc.json +0 -22
- package/dist/index.d.ts.map +0 -1
package/.prettierrc.json
ADDED
package/README.md
CHANGED
|
@@ -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') },
|
|
@@ -11,8 +11,8 @@ export default class ClickTone {
|
|
|
11
11
|
throttle?: number;
|
|
12
12
|
debug?: boolean;
|
|
13
13
|
});
|
|
14
|
-
play
|
|
14
|
+
play(url?: string): void;
|
|
15
15
|
}
|
|
16
16
|
export default ClickTone;
|
|
17
17
|
|
|
18
|
-
//# sourceMappingURL=
|
|
18
|
+
//# sourceMappingURL=clicktone.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":"AAAA,QAAQ,MAAM,CAAC;IACb,UAAU,MAAM;QACd,kBAAkB,CAAC,EAAE,OAAO,YAAY,CAAC;KAC1C;CACF;AAED,qBAAM,SAAS;gBAUD,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;IA6GM,IAAI,CAAC,GAAG,GAAE,MAAkB,GAAG,IAAI;CAiB3C;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 private readonly volume: number;\n private readonly callback: ((error?: Error) => void) | null;\n private readonly throttle: number;\n private readonly debug: boolean;\n private lastClickTime: number;\n private readonly audioCache: Record<string, AudioBuffer>;\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 void this.audioContext!.resume()\n .then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n })\n .catch((error) => {\n if (this.debug) {\n console.error('AudioContext resume error:', error);\n }\n });\n }\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n }\n\n private async fetchAndDecodeAudio(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 console.error('Audio loading and decoding error: ', error);\n }\n\n throw new Error(\n `Something went wrong when loading and decoding the audio: ${(error as Error).message}`,\n );\n }\n }\n\n private async playAudio(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 = (): void => {\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 as Error).message}`);\n }\n }\n\n private throttleFn(func: () => Promise<void>): () => void {\n return () => {\n const now = Date.now();\n\n if (now - this.lastClickTime >= this.throttle) {\n void func().catch((error) => {\n if (this.debug) {\n console.error('Error in throttled function:', error);\n }\n });\n\n this.lastClickTime = now;\n }\n };\n }\n\n public play(url: string = this.file): void {\n const throttledPlay = this.throttleFn(() => this.playAudio(url));\n\n try {\n 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 as Error);\n } else {\n throw error;\n }\n }\n }\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"clicktone.d.ts.map"}
|
package/dist/index.js
CHANGED
|
@@ -11,14 +11,6 @@ $parcel$defineInteropFlag(module.exports);
|
|
|
11
11
|
|
|
12
12
|
$parcel$export(module.exports, "default", function () { return $a196c1ed25598f0e$export$2e2bcd8739ae039; });
|
|
13
13
|
class $a196c1ed25598f0e$var$ClickTone {
|
|
14
|
-
file;
|
|
15
|
-
volume;
|
|
16
|
-
callback;
|
|
17
|
-
throttle;
|
|
18
|
-
debug;
|
|
19
|
-
lastClickTime;
|
|
20
|
-
audioCache;
|
|
21
|
-
audioContext;
|
|
22
14
|
constructor({ file: file, volume: volume = 1.0, callback: callback = null, throttle: throttle = 0, debug: debug = false }){
|
|
23
15
|
this.file = file;
|
|
24
16
|
this.volume = volume;
|
|
@@ -35,19 +27,21 @@ class $a196c1ed25598f0e$var$ClickTone {
|
|
|
35
27
|
this.iOSFixAudioContext();
|
|
36
28
|
}
|
|
37
29
|
}
|
|
38
|
-
iOSFixAudioContext
|
|
39
|
-
if (this.audioContext && this.audioContext.state ===
|
|
30
|
+
iOSFixAudioContext() {
|
|
31
|
+
if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {
|
|
40
32
|
const unlock = ()=>{
|
|
41
|
-
if (this.audioContext.state ===
|
|
42
|
-
document.body.removeEventListener(
|
|
43
|
-
document.body.removeEventListener(
|
|
33
|
+
if (this.audioContext.state === 'suspended') this.audioContext.resume().then(()=>{
|
|
34
|
+
document.body.removeEventListener('touchstart', unlock);
|
|
35
|
+
document.body.removeEventListener('touchend', unlock);
|
|
36
|
+
}).catch((error)=>{
|
|
37
|
+
if (this.debug) console.error('AudioContext resume error:', error);
|
|
44
38
|
});
|
|
45
39
|
};
|
|
46
|
-
document.body.addEventListener(
|
|
47
|
-
document.body.addEventListener(
|
|
40
|
+
document.body.addEventListener('touchstart', unlock, false);
|
|
41
|
+
document.body.addEventListener('touchend', unlock, false);
|
|
48
42
|
}
|
|
49
|
-
}
|
|
50
|
-
|
|
43
|
+
}
|
|
44
|
+
async fetchAndDecodeAudio(url) {
|
|
51
45
|
try {
|
|
52
46
|
if (this.audioCache[url]) return this.audioCache[url];
|
|
53
47
|
const response = await fetch(url);
|
|
@@ -56,11 +50,11 @@ class $a196c1ed25598f0e$var$ClickTone {
|
|
|
56
50
|
this.audioCache[url] = audioData;
|
|
57
51
|
return audioData;
|
|
58
52
|
} catch (error) {
|
|
59
|
-
if (this.debug) console.error(
|
|
53
|
+
if (this.debug) console.error('Audio loading and decoding error: ', error);
|
|
60
54
|
throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);
|
|
61
55
|
}
|
|
62
|
-
}
|
|
63
|
-
|
|
56
|
+
}
|
|
57
|
+
async playAudio(url) {
|
|
64
58
|
this.initAudioContext();
|
|
65
59
|
try {
|
|
66
60
|
const audioData = await this.fetchAndDecodeAudio(url);
|
|
@@ -75,29 +69,31 @@ class $a196c1ed25598f0e$var$ClickTone {
|
|
|
75
69
|
};
|
|
76
70
|
source.start(0);
|
|
77
71
|
} catch (error) {
|
|
78
|
-
if (this.debug) console.error(
|
|
72
|
+
if (this.debug) console.error('Audio playback error: ', error);
|
|
79
73
|
throw new Error(`Something went wrong while playing audio: ${error.message}`);
|
|
80
74
|
}
|
|
81
|
-
}
|
|
82
|
-
throttleFn
|
|
75
|
+
}
|
|
76
|
+
throttleFn(func) {
|
|
83
77
|
return ()=>{
|
|
84
78
|
const now = Date.now();
|
|
85
79
|
if (now - this.lastClickTime >= this.throttle) {
|
|
86
|
-
func()
|
|
80
|
+
func().catch((error)=>{
|
|
81
|
+
if (this.debug) console.error('Error in throttled function:', error);
|
|
82
|
+
});
|
|
87
83
|
this.lastClickTime = now;
|
|
88
84
|
}
|
|
89
85
|
};
|
|
90
|
-
}
|
|
91
|
-
play
|
|
86
|
+
}
|
|
87
|
+
play(url = this.file) {
|
|
92
88
|
const throttledPlay = this.throttleFn(()=>this.playAudio(url));
|
|
93
89
|
try {
|
|
94
|
-
|
|
90
|
+
throttledPlay();
|
|
95
91
|
} catch (error) {
|
|
96
|
-
if (this.debug) console.error(
|
|
92
|
+
if (this.debug) console.error('Audio playback error: ', error);
|
|
97
93
|
if (this.callback) this.callback(error);
|
|
98
94
|
else throw error;
|
|
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F;IAEQ,WAAW,IAAyB,EAAc;QACxD,OAAO;YACL,MAAM,MAAM,KAAK,GAAG;YAEpB,IAAI,MAAM,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,QAAQ,EAAE;gBACxC,OAAO,KAAK,CAAC,CAAC;oBACjB,IAAI,IAAI,CAAC,KAAK,EACZ,QAAQ,KAAK,CAAC,gCAAgC;gBAElD;gBAEA,IAAI,CAAC,aAAa,GAAG;YACvB;QACF;IACF;IAEO,KAAK,MAAc,IAAI,CAAC,IAAI,EAAQ;QACzC,MAAM,gBAAgB,IAAI,CAAC,UAAU,CAAC,IAAM,IAAI,CAAC,SAAS,CAAC;QAE3D,IAAI;YACF;QACF,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;AACF;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 private readonly volume: number;\n private readonly callback: ((error?: Error) => void) | null;\n private readonly throttle: number;\n private readonly debug: boolean;\n private lastClickTime: number;\n private readonly audioCache: Record<string, AudioBuffer>;\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 void this.audioContext!.resume()\n .then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n })\n .catch((error) => {\n if (this.debug) {\n console.error('AudioContext resume error:', error);\n }\n });\n }\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n }\n\n private async fetchAndDecodeAudio(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 console.error('Audio loading and decoding error: ', error);\n }\n\n throw new Error(\n `Something went wrong when loading and decoding the audio: ${(error as Error).message}`,\n );\n }\n }\n\n private async playAudio(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 = (): void => {\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 as Error).message}`);\n }\n }\n\n private throttleFn(func: () => Promise<void>): () => void {\n return () => {\n const now = Date.now();\n\n if (now - this.lastClickTime >= this.throttle) {\n void func().catch((error) => {\n if (this.debug) {\n console.error('Error in throttled function:', error);\n }\n });\n\n this.lastClickTime = now;\n }\n };\n }\n\n public play(url: string = this.file): void {\n const throttledPlay = this.throttleFn(() => this.playAudio(url));\n\n try {\n 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 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
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
1
|
class $643fcf18b2d2e76f$var$ClickTone {
|
|
2
|
-
file;
|
|
3
|
-
volume;
|
|
4
|
-
callback;
|
|
5
|
-
throttle;
|
|
6
|
-
debug;
|
|
7
|
-
lastClickTime;
|
|
8
|
-
audioCache;
|
|
9
|
-
audioContext;
|
|
10
2
|
constructor({ file: file, volume: volume = 1.0, callback: callback = null, throttle: throttle = 0, debug: debug = false }){
|
|
11
3
|
this.file = file;
|
|
12
4
|
this.volume = volume;
|
|
@@ -23,19 +15,21 @@ class $643fcf18b2d2e76f$var$ClickTone {
|
|
|
23
15
|
this.iOSFixAudioContext();
|
|
24
16
|
}
|
|
25
17
|
}
|
|
26
|
-
iOSFixAudioContext
|
|
27
|
-
if (this.audioContext && this.audioContext.state ===
|
|
18
|
+
iOSFixAudioContext() {
|
|
19
|
+
if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {
|
|
28
20
|
const unlock = ()=>{
|
|
29
|
-
if (this.audioContext.state ===
|
|
30
|
-
document.body.removeEventListener(
|
|
31
|
-
document.body.removeEventListener(
|
|
21
|
+
if (this.audioContext.state === 'suspended') this.audioContext.resume().then(()=>{
|
|
22
|
+
document.body.removeEventListener('touchstart', unlock);
|
|
23
|
+
document.body.removeEventListener('touchend', unlock);
|
|
24
|
+
}).catch((error)=>{
|
|
25
|
+
if (this.debug) console.error('AudioContext resume error:', error);
|
|
32
26
|
});
|
|
33
27
|
};
|
|
34
|
-
document.body.addEventListener(
|
|
35
|
-
document.body.addEventListener(
|
|
28
|
+
document.body.addEventListener('touchstart', unlock, false);
|
|
29
|
+
document.body.addEventListener('touchend', unlock, false);
|
|
36
30
|
}
|
|
37
|
-
}
|
|
38
|
-
|
|
31
|
+
}
|
|
32
|
+
async fetchAndDecodeAudio(url) {
|
|
39
33
|
try {
|
|
40
34
|
if (this.audioCache[url]) return this.audioCache[url];
|
|
41
35
|
const response = await fetch(url);
|
|
@@ -44,11 +38,11 @@ class $643fcf18b2d2e76f$var$ClickTone {
|
|
|
44
38
|
this.audioCache[url] = audioData;
|
|
45
39
|
return audioData;
|
|
46
40
|
} catch (error) {
|
|
47
|
-
if (this.debug) console.error(
|
|
41
|
+
if (this.debug) console.error('Audio loading and decoding error: ', error);
|
|
48
42
|
throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);
|
|
49
43
|
}
|
|
50
|
-
}
|
|
51
|
-
|
|
44
|
+
}
|
|
45
|
+
async playAudio(url) {
|
|
52
46
|
this.initAudioContext();
|
|
53
47
|
try {
|
|
54
48
|
const audioData = await this.fetchAndDecodeAudio(url);
|
|
@@ -63,29 +57,31 @@ class $643fcf18b2d2e76f$var$ClickTone {
|
|
|
63
57
|
};
|
|
64
58
|
source.start(0);
|
|
65
59
|
} catch (error) {
|
|
66
|
-
if (this.debug) console.error(
|
|
60
|
+
if (this.debug) console.error('Audio playback error: ', error);
|
|
67
61
|
throw new Error(`Something went wrong while playing audio: ${error.message}`);
|
|
68
62
|
}
|
|
69
|
-
}
|
|
70
|
-
throttleFn
|
|
63
|
+
}
|
|
64
|
+
throttleFn(func) {
|
|
71
65
|
return ()=>{
|
|
72
66
|
const now = Date.now();
|
|
73
67
|
if (now - this.lastClickTime >= this.throttle) {
|
|
74
|
-
func()
|
|
68
|
+
func().catch((error)=>{
|
|
69
|
+
if (this.debug) console.error('Error in throttled function:', error);
|
|
70
|
+
});
|
|
75
71
|
this.lastClickTime = now;
|
|
76
72
|
}
|
|
77
73
|
};
|
|
78
|
-
}
|
|
79
|
-
play
|
|
74
|
+
}
|
|
75
|
+
play(url = this.file) {
|
|
80
76
|
const throttledPlay = this.throttleFn(()=>this.playAudio(url));
|
|
81
77
|
try {
|
|
82
|
-
|
|
78
|
+
throttledPlay();
|
|
83
79
|
} catch (error) {
|
|
84
|
-
if (this.debug) console.error(
|
|
80
|
+
if (this.debug) console.error('Audio playback error: ', error);
|
|
85
81
|
if (this.callback) this.callback(error);
|
|
86
82
|
else throw error;
|
|
87
83
|
}
|
|
88
|
-
}
|
|
84
|
+
}
|
|
89
85
|
}
|
|
90
86
|
var $643fcf18b2d2e76f$export$2e2bcd8739ae039 = $643fcf18b2d2e76f$var$ClickTone;
|
|
91
87
|
|
package/dist/index.module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AAMA,MAAM;
|
|
1
|
+
{"mappings":"AAMA,MAAM;IAUJ,YAAY,QACV,IAAI,UACJ,SAAS,eACT,WAAW,gBACX,WAAW,UACX,QAAQ,OAOT,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,aAAa,GAAG;QACrB,IAAI,CAAC,UAAU,GAAG,CAAC;QACnB,IAAI,CAAC,YAAY,GAAG;IACtB;IAEQ,mBAAyB;QAC/B,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE;YACtB,IAAI,CAAC,YAAY,GAAG,IAAK,CAAA,OAAO,YAAY,IAAI,OAAO,kBAAkB,AAAD;YACxE,IAAI,CAAC,kBAAkB;QACzB;IACF;IAEQ,qBAA2B;QACjC,IAAI,IAAI,CAAC,YAAY,IAAI,IAAI,CAAC,YAAY,CAAC,KAAK,KAAK,eAAe,kBAAkB,QAAQ;YAC5F,MAAM,SAAS;gBACb,IAAI,IAAI,CAAC,YAAY,CAAE,KAAK,KAAK,aAC1B,IAAI,CAAC,YAAY,CAAE,MAAM,GAC3B,IAAI,CAAC;oBACJ,SAAS,IAAI,CAAC,mBAAmB,CAAC,cAAc;oBAChD,SAAS,IAAI,CAAC,mBAAmB,CAAC,YAAY;gBAChD,GACC,KAAK,CAAC,CAAC;oBACN,IAAI,I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YAG1C,MAAM,IAAI,MAAM,CAAC,0CAA0C,EAAE,AAAC,MAAgB,OAAO,EAAE;QACzF;IACF;IAEQ,WAAW,IAAyB,EAAc;QACxD,OAAO;YACL,MAAM,MAAM,KAAK,GAAG;YAEpB,IAAI,MAAM,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,QAAQ,EAAE;gBACxC,OAAO,KAAK,CAAC,CAAC;oBACjB,IAAI,IAAI,CAAC,KAAK,EACZ,QAAQ,KAAK,CAAC,gCAAgC;gBAElD;gBAEA,IAAI,CAAC,aAAa,GAAG;YACvB;QACF;IACF;IAEO,KAAK,MAAc,IAAI,CAAC,IAAI,EAAQ;QACzC,MAAM,gBAAgB,IAAI,CAAC,UAAU,CAAC,IAAM,IAAI,CAAC,SAAS,CAAC;QAE3D,IAAI;YACF;QACF,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;AACF;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 private readonly volume: number;\n private readonly callback: ((error?: Error) => void) | null;\n private readonly throttle: number;\n private readonly debug: boolean;\n private lastClickTime: number;\n private readonly audioCache: Record<string, AudioBuffer>;\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 void this.audioContext!.resume()\n .then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n })\n .catch((error) => {\n if (this.debug) {\n console.error('AudioContext resume error:', error);\n }\n });\n }\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n }\n\n private async fetchAndDecodeAudio(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 console.error('Audio loading and decoding error: ', error);\n }\n\n throw new Error(\n `Something went wrong when loading and decoding the audio: ${(error as Error).message}`,\n );\n }\n }\n\n private async playAudio(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 = (): void => {\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 as Error).message}`);\n }\n }\n\n private throttleFn(func: () => Promise<void>): () => void {\n return () => {\n const now = Date.now();\n\n if (now - this.lastClickTime >= this.throttle) {\n void func().catch((error) => {\n if (this.debug) {\n console.error('Error in throttled function:', error);\n }\n });\n\n this.lastClickTime = now;\n }\n };\n }\n\n public play(url: string = this.file): void {\n const throttledPlay = this.throttleFn(() => this.playAudio(url));\n\n try {\n 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 as Error);\n } else {\n throw error;\n }\n }\n }\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.module.js.map"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import globals from 'globals';
|
|
2
|
+
import eslintPlugin from '@eslint/js';
|
|
3
|
+
import typescriptPlugin from '@typescript-eslint/eslint-plugin';
|
|
4
|
+
import typescriptParser from '@typescript-eslint/parser';
|
|
5
|
+
import prettierConfig from 'eslint-config-prettier';
|
|
6
|
+
import importPlugin from 'eslint-plugin-import';
|
|
7
|
+
|
|
8
|
+
/** @type {import("eslint").Linter.FlatConfig[]} */
|
|
9
|
+
export default [
|
|
10
|
+
{
|
|
11
|
+
ignores: ['node_modules', 'dist', '.parcel', '.parcel-cache'],
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
files: ['**/*.ts'],
|
|
15
|
+
languageOptions: {
|
|
16
|
+
ecmaVersion: 'latest',
|
|
17
|
+
sourceType: 'module',
|
|
18
|
+
parser: typescriptParser,
|
|
19
|
+
parserOptions: {
|
|
20
|
+
project: './tsconfig.json',
|
|
21
|
+
},
|
|
22
|
+
globals: {
|
|
23
|
+
...globals.browser,
|
|
24
|
+
...globals.es2022,
|
|
25
|
+
globalThis: 'readonly',
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
plugins: {
|
|
29
|
+
'@typescript-eslint': typescriptPlugin,
|
|
30
|
+
eslint: eslintPlugin,
|
|
31
|
+
import: importPlugin,
|
|
32
|
+
},
|
|
33
|
+
settings: {
|
|
34
|
+
'import/resolver': {
|
|
35
|
+
typescript: {
|
|
36
|
+
project: './tsconfig.json',
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
rules: {
|
|
41
|
+
...eslintPlugin.configs.recommended.rules,
|
|
42
|
+
...typescriptPlugin.configs.recommended.rules,
|
|
43
|
+
...typescriptPlugin.configs['recommended-requiring-type-checking'].rules,
|
|
44
|
+
'@typescript-eslint/no-unused-vars': 'error',
|
|
45
|
+
'@typescript-eslint/explicit-function-return-type': 'warn',
|
|
46
|
+
'@typescript-eslint/no-explicit-any': 'warn',
|
|
47
|
+
'import/order': [
|
|
48
|
+
'error',
|
|
49
|
+
{
|
|
50
|
+
groups: [['builtin', 'external', 'internal']],
|
|
51
|
+
'newlines-between': 'always',
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
'@typescript-eslint/triple-slash-reference': 'off',
|
|
55
|
+
'@typescript-eslint/no-empty-interface': 'off',
|
|
56
|
+
'import/no-unresolved': 'error',
|
|
57
|
+
'import/no-duplicates': 'error',
|
|
58
|
+
'max-len': [
|
|
59
|
+
'error',
|
|
60
|
+
{
|
|
61
|
+
code: 100,
|
|
62
|
+
ignoreUrls: true,
|
|
63
|
+
ignoreStrings: true,
|
|
64
|
+
ignoreTemplateLiterals: true,
|
|
65
|
+
ignoreComments: true,
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
'linebreak-style': ['error', 'unix'],
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
prettierConfig,
|
|
72
|
+
];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clicktone",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.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,22 +16,30 @@
|
|
|
16
16
|
"scripts": {
|
|
17
17
|
"clean": "rm -rf dist .parcel-cache",
|
|
18
18
|
"build": "yarn clean && parcel build",
|
|
19
|
-
"lint:js": "eslint
|
|
20
|
-
"
|
|
19
|
+
"lint:js": "eslint **/*.ts",
|
|
20
|
+
"lint:fix:js": "eslint **/*.ts --fix",
|
|
21
|
+
"format:js": "prettier --write **/*.ts",
|
|
22
|
+
"lint:fix": "yarn lint:fix:js && yarn format:js"
|
|
21
23
|
},
|
|
22
24
|
"browserslist": "> 0.5%, last 2 versions, not dead",
|
|
23
25
|
"source": "src/index.ts",
|
|
24
26
|
"main": "dist/index.js",
|
|
25
27
|
"module": "dist/index.module.js",
|
|
26
|
-
"types": "dist/
|
|
28
|
+
"types": "dist/clicktone.d.ts",
|
|
27
29
|
"devDependencies": {
|
|
28
|
-
"@
|
|
29
|
-
"@parcel/
|
|
30
|
-
"@parcel/transformer-typescript-types": "2.
|
|
31
|
-
"eslint": "
|
|
32
|
-
"eslint
|
|
33
|
-
"
|
|
34
|
-
"
|
|
30
|
+
"@eslint/js": "9.17.0",
|
|
31
|
+
"@parcel/packager-ts": "2.13.3",
|
|
32
|
+
"@parcel/transformer-typescript-types": "2.13.3",
|
|
33
|
+
"@typescript-eslint/eslint-plugin": "8.18.2",
|
|
34
|
+
"@typescript-eslint/parser": "8.18.2",
|
|
35
|
+
"eslint": "9.17.0",
|
|
36
|
+
"eslint-config-prettier": "9.1.0",
|
|
37
|
+
"eslint-import-resolver-typescript": "3.7.0",
|
|
38
|
+
"eslint-plugin-import": "2.31.0",
|
|
39
|
+
"globals": "15.14.0",
|
|
40
|
+
"parcel": "2.13.3",
|
|
41
|
+
"prettier": "3.4.2",
|
|
42
|
+
"typescript": "5.7.2"
|
|
35
43
|
},
|
|
36
44
|
"keywords": [
|
|
37
45
|
"MP3",
|
package/src/index.ts
CHANGED
|
@@ -44,23 +44,29 @@ class ClickTone {
|
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
private iOSFixAudioContext
|
|
47
|
+
private iOSFixAudioContext(): void {
|
|
48
48
|
if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {
|
|
49
49
|
const unlock = (): void => {
|
|
50
50
|
if (this.audioContext!.state === 'suspended') {
|
|
51
|
-
this.audioContext!.resume()
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
51
|
+
void this.audioContext!.resume()
|
|
52
|
+
.then(() => {
|
|
53
|
+
document.body.removeEventListener('touchstart', unlock);
|
|
54
|
+
document.body.removeEventListener('touchend', unlock);
|
|
55
|
+
})
|
|
56
|
+
.catch((error) => {
|
|
57
|
+
if (this.debug) {
|
|
58
|
+
console.error('AudioContext resume error:', error);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
55
61
|
}
|
|
56
62
|
};
|
|
57
63
|
|
|
58
64
|
document.body.addEventListener('touchstart', unlock, false);
|
|
59
65
|
document.body.addEventListener('touchend', unlock, false);
|
|
60
66
|
}
|
|
61
|
-
}
|
|
67
|
+
}
|
|
62
68
|
|
|
63
|
-
private
|
|
69
|
+
private async fetchAndDecodeAudio(url: string): Promise<AudioBuffer> {
|
|
64
70
|
try {
|
|
65
71
|
if (this.audioCache[url]) {
|
|
66
72
|
return this.audioCache[url];
|
|
@@ -78,11 +84,13 @@ class ClickTone {
|
|
|
78
84
|
console.error('Audio loading and decoding error: ', error);
|
|
79
85
|
}
|
|
80
86
|
|
|
81
|
-
throw new Error(
|
|
87
|
+
throw new Error(
|
|
88
|
+
`Something went wrong when loading and decoding the audio: ${(error as Error).message}`,
|
|
89
|
+
);
|
|
82
90
|
}
|
|
83
|
-
}
|
|
91
|
+
}
|
|
84
92
|
|
|
85
|
-
private
|
|
93
|
+
private async playAudio(url: string): Promise<void> {
|
|
86
94
|
this.initAudioContext();
|
|
87
95
|
|
|
88
96
|
try {
|
|
@@ -95,7 +103,7 @@ class ClickTone {
|
|
|
95
103
|
source.connect(gainNode);
|
|
96
104
|
gainNode.connect(this.audioContext!.destination);
|
|
97
105
|
|
|
98
|
-
source.onended = () => {
|
|
106
|
+
source.onended = (): void => {
|
|
99
107
|
if (this.callback) {
|
|
100
108
|
this.callback();
|
|
101
109
|
}
|
|
@@ -109,25 +117,29 @@ class ClickTone {
|
|
|
109
117
|
|
|
110
118
|
throw new Error(`Something went wrong while playing audio: ${(error as Error).message}`);
|
|
111
119
|
}
|
|
112
|
-
}
|
|
120
|
+
}
|
|
113
121
|
|
|
114
|
-
private throttleFn
|
|
122
|
+
private throttleFn(func: () => Promise<void>): () => void {
|
|
115
123
|
return () => {
|
|
116
124
|
const now = Date.now();
|
|
117
125
|
|
|
118
126
|
if (now - this.lastClickTime >= this.throttle) {
|
|
119
|
-
func()
|
|
127
|
+
void func().catch((error) => {
|
|
128
|
+
if (this.debug) {
|
|
129
|
+
console.error('Error in throttled function:', error);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
120
132
|
|
|
121
133
|
this.lastClickTime = now;
|
|
122
134
|
}
|
|
123
135
|
};
|
|
124
|
-
}
|
|
136
|
+
}
|
|
125
137
|
|
|
126
|
-
public play
|
|
138
|
+
public play(url: string = this.file): void {
|
|
127
139
|
const throttledPlay = this.throttleFn(() => this.playAudio(url));
|
|
128
140
|
|
|
129
141
|
try {
|
|
130
|
-
|
|
142
|
+
throttledPlay();
|
|
131
143
|
} catch (error) {
|
|
132
144
|
if (this.debug) {
|
|
133
145
|
console.error('Audio playback error: ', error);
|
|
@@ -139,7 +151,7 @@ class ClickTone {
|
|
|
139
151
|
throw error;
|
|
140
152
|
}
|
|
141
153
|
}
|
|
142
|
-
}
|
|
154
|
+
}
|
|
143
155
|
}
|
|
144
156
|
|
|
145
157
|
export default ClickTone;
|
package/tsconfig.json
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"isolatedModules": true,
|
|
4
|
-
"target": "
|
|
4
|
+
"target": "ESNext",
|
|
5
|
+
"module": "ESNext",
|
|
6
|
+
"strict": true,
|
|
7
|
+
"strictNullChecks": true,
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"forceConsistentCasingInFileNames": true,
|
|
11
|
+
"moduleResolution": "node",
|
|
12
|
+
"noImplicitAny": true
|
|
5
13
|
}
|
|
6
14
|
}
|
package/.eslintrc.json
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"env": {
|
|
3
|
-
"browser": true,
|
|
4
|
-
"es2021": true
|
|
5
|
-
},
|
|
6
|
-
"extends": [
|
|
7
|
-
"airbnb-base"
|
|
8
|
-
],
|
|
9
|
-
"parserOptions": {
|
|
10
|
-
"ecmaVersion": "latest",
|
|
11
|
-
"sourceType": "module"
|
|
12
|
-
},
|
|
13
|
-
"rules": {
|
|
14
|
-
"max-len": [
|
|
15
|
-
"off",
|
|
16
|
-
{
|
|
17
|
-
"code": 100,
|
|
18
|
-
"ignoreUrls": true
|
|
19
|
-
}
|
|
20
|
-
]
|
|
21
|
-
}
|
|
22
|
-
}
|
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;gBAUD,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,CAgB1D;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 private readonly volume: number;\n private readonly callback: ((error?: Error) => void) | null;\n private readonly throttle: number;\n private readonly debug: boolean;\n private lastClickTime: number;\n private readonly audioCache: Record<string, AudioBuffer>;\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 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 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 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 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 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"}
|