clicktone 1.0.0 → 1.1.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 CHANGED
@@ -11,7 +11,7 @@
11
11
 
12
12
  <p align="center">A simple class that provides a convenient abstraction for handling<br>
13
13
  audio playback by web applications, with support for iOS devices.</p>
14
- <p align="center"><a href="https://n2q8x8.csb.app/">Demo</a></p>
14
+ <p align="center"><a href="https://codepen.io/ux-ui/pen/yLwbmMr">Demo</a></p>
15
15
  <br>
16
16
 
17
17
  &#10148; **Install**
@@ -33,9 +33,12 @@ import ClickTone from 'clicktone';
33
33
 
34
34
  <sub>Class uses the Web Audio API, which supports many audio file formats: MP3, WAV, OGG, AAC and others. Please note that not all browsers support these formats.</sub>
35
35
  ```javascript
36
- const clickSound = new ClickTone('./sound.mp3');
36
+ const click = new ClickTone({
37
+ file: './sound.mp3',
38
+ volume: 0.7,
39
+ });
37
40
 
38
- myButton.addEventListener('click', () => clickSound.play());
41
+ myButton.addEventListener('click', () => click.play());
39
42
  ```
40
43
  <br>
41
44
 
package/dist/index.js CHANGED
@@ -11,8 +11,9 @@ $parcel$defineInteropFlag(module.exports);
11
11
 
12
12
  $parcel$export(module.exports, "default", () => $4fa36e821943b400$export$2e2bcd8739ae039);
13
13
  class $4fa36e821943b400$var$ClickTone {
14
- constructor(file){
15
- this.file = file;
14
+ constructor(options){
15
+ this.file = options.file;
16
+ this.volume = options.volume || 1.0;
16
17
  this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
17
18
  this.iOSFixAudioContext();
18
19
  }
@@ -32,8 +33,11 @@ class $4fa36e821943b400$var$ClickTone {
32
33
  return new Promise((resolve, reject)=>{
33
34
  fetch(url).then((response)=>response.arrayBuffer()).then((buffer)=>this.audioContext.decodeAudioData(buffer)).then((audioData)=>{
34
35
  const source = this.audioContext.createBufferSource();
36
+ const gainNode = this.audioContext.createGain();
35
37
  source.buffer = audioData;
36
- source.connect(this.audioContext.destination);
38
+ gainNode.gain.value = this.volume;
39
+ source.connect(gainNode);
40
+ gainNode.connect(this.audioContext.destination);
37
41
  source.start(0);
38
42
  resolve();
39
43
  }).catch((error)=>{
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;;;;;AAAA,MAAM;IACJ,YAAY,IAAI,CAAE;QAChB,IAAI,CAAC,IAAI,GAAG;QACZ,IAAI,CAAC,YAAY,GAAG,IAAK,CAAA,OAAO,YAAY,IAAI,OAAO,kBAAkB,AAAD;QACxE,IAAI,CAAC,kBAAkB;IACzB;IAEA,qBAAqB;QACnB,IAAI,IAAI,CAAC,YAAY,CAAC,KAAK,KAAK,eAAe,kBAAkB,QAAQ;YACvE,MAAM,SAAS;gBACb,IAAI,CAAC,YAAY,CAAC,MAAM,GAAG,IAAI,CAAC;oBAC9B,SAAS,IAAI,CAAC,mBAAmB,CAAC,cAAc;oBAChD,SAAS,IAAI,CAAC,mBAAmB,CAAC,YAAY;gBAChD;YACF;YAEA,SAAS,IAAI,CAAC,gBAAgB,CAAC,cAAc,QAAQ;YACrD,SAAS,IAAI,CAAC,gBAAgB,CAAC,YAAY,QAAQ;QACrD;IACF;IAEA,MAAM,GAAG,EAAE;QACT,OAAO,IAAI,QAAQ,CAAC,SAAS;YAC3B,MAAM,KACH,IAAI,CAAC,CAAC,WAAa,SAAS,WAAW,IACvC,IAAI,CAAC,CAAC,SAAW,IAAI,CAAC,YAAY,CAAC,eAAe,CAAC,SACnD,IAAI,CAAC,CAAC;gBACL,MAAM,SAAS,IAAI,CAAC,YAAY,CAAC,kBAAkB;gBAEnD,OAAO,MAAM,GAAG;gBAChB,OAAO,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,WAAW;gBAC5C,OAAO,KAAK,CAAC;gBAEb;YACF,GACC,KAAK,CAAC,CAAC;gBACN,OAAO;YACT;QACJ;IACF;IAEA,KAAK,MAAM,IAAI,CAAC,IAAI,EAAE;QACpB,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB;AACF;IAEA,2CAAe","sources":["src/index.js"],"sourcesContent":["class ClickTone {\n constructor(file) {\n this.file = file;\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n\n iOSFixAudioContext() {\n if (this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = () => {\n this.audioContext.resume().then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n });\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n }\n\n audio(url) {\n return new Promise((resolve, reject) => {\n fetch(url)\n .then((response) => response.arrayBuffer())\n .then((buffer) => this.audioContext.decodeAudioData(buffer))\n .then((audioData) => {\n const source = this.audioContext.createBufferSource();\n\n source.buffer = audioData;\n source.connect(this.audioContext.destination);\n source.start(0);\n\n resolve();\n })\n .catch((error) => {\n reject(error);\n });\n });\n }\n\n play(url = this.file) {\n return this.audio(url);\n }\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.js.map"}
1
+ {"mappings":";;;;;;;;;;;;AAAA,MAAM;IACJ,YAAY,OAAO,CAAE;QACnB,IAAI,CAAC,IAAI,GAAG,QAAQ,IAAI;QACxB,IAAI,CAAC,MAAM,GAAG,QAAQ,MAAM,IAAI;QAChC,IAAI,CAAC,YAAY,GAAG,IAAK,CAAA,OAAO,YAAY,IAAI,OAAO,kBAAkB,AAAD;QACxE,IAAI,CAAC,kBAAkB;IACzB;IAEA,qBAAqB;QACnB,IAAI,IAAI,CAAC,YAAY,CAAC,KAAK,KAAK,eAAe,kBAAkB,QAAQ;YACvE,MAAM,SAAS;gBACb,IAAI,CAAC,YAAY,CAAC,MAAM,GAAG,IAAI,CAAC;oBAC9B,SAAS,IAAI,CAAC,mBAAmB,CAAC,cAAc;oBAChD,SAAS,IAAI,CAAC,mBAAmB,CAAC,YAAY;gBAChD;YACF;YAEA,SAAS,IAAI,CAAC,gBAAgB,CAAC,cAAc,QAAQ;YACrD,SAAS,IAAI,CAAC,gBAAgB,CAAC,YAAY,QAAQ;QACrD;IACF;IAEA,MAAM,GAAG,EAAE;QACT,OAAO,IAAI,QAAQ,CAAC,SAAS;YAC3B,MAAM,KACH,IAAI,CAAC,CAAC,WAAa,SAAS,WAAW,IACvC,IAAI,CAAC,CAAC,SAAW,IAAI,CAAC,YAAY,CAAC,eAAe,CAAC,SACnD,IAAI,CAAC,CAAC;gBACL,MAAM,SAAS,IAAI,CAAC,YAAY,CAAC,kBAAkB;gBACnD,MAAM,WAAW,IAAI,CAAC,YAAY,CAAC,UAAU;gBAE7C,OAAO,MAAM,GAAG;gBAChB,SAAS,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,MAAM;gBACjC,OAAO,OAAO,CAAC;gBACf,SAAS,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,WAAW;gBAC9C,OAAO,KAAK,CAAC;gBAEb;YACF,GACC,KAAK,CAAC,CAAC;gBACN,OAAO;YACT;QACJ;IACF;IAEA,KAAK,MAAM,IAAI,CAAC,IAAI,EAAE;QACpB,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB;AACF;IAEA,2CAAe","sources":["src/index.js"],"sourcesContent":["class ClickTone {\n constructor(options) {\n this.file = options.file;\n this.volume = options.volume || 1.0;\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n\n iOSFixAudioContext() {\n if (this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = () => {\n this.audioContext.resume().then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n });\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n }\n\n audio(url) {\n return new Promise((resolve, reject) => {\n fetch(url)\n .then((response) => response.arrayBuffer())\n .then((buffer) => this.audioContext.decodeAudioData(buffer))\n .then((audioData) => {\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 source.start(0);\n\n resolve();\n })\n .catch((error) => {\n reject(error);\n });\n });\n }\n\n play(url = this.file) {\n return this.audio(url);\n }\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.js.map"}
@@ -1,6 +1,7 @@
1
1
  class $cf838c15c8b009ba$var$ClickTone {
2
- constructor(file){
3
- this.file = file;
2
+ constructor(options){
3
+ this.file = options.file;
4
+ this.volume = options.volume || 1.0;
4
5
  this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
5
6
  this.iOSFixAudioContext();
6
7
  }
@@ -20,8 +21,11 @@ class $cf838c15c8b009ba$var$ClickTone {
20
21
  return new Promise((resolve, reject)=>{
21
22
  fetch(url).then((response)=>response.arrayBuffer()).then((buffer)=>this.audioContext.decodeAudioData(buffer)).then((audioData)=>{
22
23
  const source = this.audioContext.createBufferSource();
24
+ const gainNode = this.audioContext.createGain();
23
25
  source.buffer = audioData;
24
- source.connect(this.audioContext.destination);
26
+ gainNode.gain.value = this.volume;
27
+ source.connect(gainNode);
28
+ gainNode.connect(this.audioContext.destination);
25
29
  source.start(0);
26
30
  resolve();
27
31
  }).catch((error)=>{
@@ -1 +1 @@
1
- {"mappings":"AAAA,MAAM;IACJ,YAAY,IAAI,CAAE;QAChB,IAAI,CAAC,IAAI,GAAG;QACZ,IAAI,CAAC,YAAY,GAAG,IAAK,CAAA,OAAO,YAAY,IAAI,OAAO,kBAAkB,AAAD;QACxE,IAAI,CAAC,kBAAkB;IACzB;IAEA,qBAAqB;QACnB,IAAI,IAAI,CAAC,YAAY,CAAC,KAAK,KAAK,eAAe,kBAAkB,QAAQ;YACvE,MAAM,SAAS;gBACb,IAAI,CAAC,YAAY,CAAC,MAAM,GAAG,IAAI,CAAC;oBAC9B,SAAS,IAAI,CAAC,mBAAmB,CAAC,cAAc;oBAChD,SAAS,IAAI,CAAC,mBAAmB,CAAC,YAAY;gBAChD;YACF;YAEA,SAAS,IAAI,CAAC,gBAAgB,CAAC,cAAc,QAAQ;YACrD,SAAS,IAAI,CAAC,gBAAgB,CAAC,YAAY,QAAQ;QACrD;IACF;IAEA,MAAM,GAAG,EAAE;QACT,OAAO,IAAI,QAAQ,CAAC,SAAS;YAC3B,MAAM,KACH,IAAI,CAAC,CAAC,WAAa,SAAS,WAAW,IACvC,IAAI,CAAC,CAAC,SAAW,IAAI,CAAC,YAAY,CAAC,eAAe,CAAC,SACnD,IAAI,CAAC,CAAC;gBACL,MAAM,SAAS,IAAI,CAAC,YAAY,CAAC,kBAAkB;gBAEnD,OAAO,MAAM,GAAG;gBAChB,OAAO,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,WAAW;gBAC5C,OAAO,KAAK,CAAC;gBAEb;YACF,GACC,KAAK,CAAC,CAAC;gBACN,OAAO;YACT;QACJ;IACF;IAEA,KAAK,MAAM,IAAI,CAAC,IAAI,EAAE;QACpB,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB;AACF;IAEA,2CAAe","sources":["src/index.js"],"sourcesContent":["class ClickTone {\n constructor(file) {\n this.file = file;\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n\n iOSFixAudioContext() {\n if (this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = () => {\n this.audioContext.resume().then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n });\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n }\n\n audio(url) {\n return new Promise((resolve, reject) => {\n fetch(url)\n .then((response) => response.arrayBuffer())\n .then((buffer) => this.audioContext.decodeAudioData(buffer))\n .then((audioData) => {\n const source = this.audioContext.createBufferSource();\n\n source.buffer = audioData;\n source.connect(this.audioContext.destination);\n source.start(0);\n\n resolve();\n })\n .catch((error) => {\n reject(error);\n });\n });\n }\n\n play(url = this.file) {\n return this.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YACT;QACJ;IACF;IAEA,KAAK,MAAM,IAAI,CAAC,IAAI,EAAE;QACpB,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB;AACF;IAEA,2CAAe","sources":["src/index.js"],"sourcesContent":["class ClickTone {\n constructor(options) {\n this.file = options.file;\n this.volume = options.volume || 1.0;\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n\n iOSFixAudioContext() {\n if (this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = () => {\n this.audioContext.resume().then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n });\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n }\n\n audio(url) {\n return new Promise((resolve, reject) => {\n fetch(url)\n .then((response) => response.arrayBuffer())\n .then((buffer) => this.audioContext.decodeAudioData(buffer))\n .then((audioData) => {\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 source.start(0);\n\n resolve();\n })\n .catch((error) => {\n reject(error);\n });\n });\n }\n\n play(url = this.file) {\n return this.audio(url);\n }\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.module.js.map"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clicktone",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "A simple class that provides a convenient abstraction for handling audio playback by web applications, with support for iOS devices.",
5
5
  "author": "ux-ui.pro",
6
6
  "license": "MIT",
package/src/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  class ClickTone {
2
- constructor(file) {
3
- this.file = file;
2
+ constructor(options) {
3
+ this.file = options.file;
4
+ this.volume = options.volume || 1.0;
4
5
  this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
5
6
  this.iOSFixAudioContext();
6
7
  }
@@ -26,9 +27,12 @@ class ClickTone {
26
27
  .then((buffer) => this.audioContext.decodeAudioData(buffer))
27
28
  .then((audioData) => {
28
29
  const source = this.audioContext.createBufferSource();
30
+ const gainNode = this.audioContext.createGain();
29
31
 
30
32
  source.buffer = audioData;
31
- source.connect(this.audioContext.destination);
33
+ gainNode.gain.value = this.volume;
34
+ source.connect(gainNode);
35
+ gainNode.connect(this.audioContext.destination);
32
36
  source.start(0);
33
37
 
34
38
  resolve();