@vnejs/plugins.audio 0.1.1

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/const/const.js ADDED
@@ -0,0 +1,4 @@
1
+ export const ACTIONS = { PLAY: "play", STOP: "stop", PAUSE: "pause", RESUME: "resume", VOLUME: "volume" };
2
+
3
+ export const LINE_EXEC_REG = /^((?<action>play|stop|pause|resume|volume) )?({(?<name>.+?)} )?(?<durations>[0-9:]+)?$/;
4
+ export const LINE_LOAD_REG = /{(?<name>.+?)}/;
@@ -0,0 +1,19 @@
1
+ export const SUBSCRIBE_EVENTS = {
2
+ PLAY: "vne:audio:play",
3
+ STOP: "vne:audio:stop",
4
+ PAUSE: "vne:audio:pause",
5
+ RESUME: "vne:audio:resume",
6
+
7
+ MEDIA_LOAD: "vne:audio:media_load",
8
+ MEDIA_GET: "vne:audio:media_get",
9
+
10
+ VOLUME: "vne:audio:volume",
11
+ VOLUME_GET: "vne:audio:volume_get",
12
+ VOLUME_CHANGED: "vne:audio:volume_changed",
13
+
14
+ PREPARE_PLAY: "vne:audio:prepare_play",
15
+
16
+ TMP_SET: "vne:audio:tmp_set",
17
+ TMP_GET: "vne:audio:tmp_get",
18
+ TMP_RM: "vne:audio:tmp_rm",
19
+ };
@@ -0,0 +1,3 @@
1
+ export const DEFAULT_VOLUME = 1;
2
+
3
+ export const QUALITY = "128k";
@@ -0,0 +1,3 @@
1
+ export const SETTINGS_KEYS = {
2
+ VOLUME: "vne:audio:volume",
3
+ };
package/index.js ADDED
@@ -0,0 +1,24 @@
1
+ import { regPlugin } from "@vnejs/shared";
2
+
3
+ import * as constants from "./const/const";
4
+ import { SUBSCRIBE_EVENTS } from "./const/events";
5
+ import * as params from "./const/params";
6
+ import { SETTINGS_KEYS } from "./const/settings";
7
+
8
+ import { AudioTmp } from "./modules/tmp";
9
+ import { AudioPlay } from "./modules/play";
10
+ import { AudioStop } from "./modules/stop";
11
+ import { AudioPause } from "./modules/pause";
12
+ import { AudioMedia } from "./modules/media";
13
+ import { AudioResume } from "./modules/resume";
14
+ import { AudioVolume } from "./modules/volume";
15
+
16
+ regPlugin("AUDIO", { constants, events: SUBSCRIBE_EVENTS, params, settings: SETTINGS_KEYS }, [
17
+ AudioTmp,
18
+ AudioPlay,
19
+ AudioStop,
20
+ AudioMedia,
21
+ AudioPause,
22
+ AudioVolume,
23
+ AudioResume,
24
+ ]);
@@ -0,0 +1,23 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ export class AudioMedia extends Module {
4
+ name = "audio.media";
5
+
6
+ subscribe = () => {
7
+ this.on(this.EVENTS.AUDIO.MEDIA_LOAD, this.onAudioMediaLoad);
8
+ this.on(this.EVENTS.AUDIO.MEDIA_GET, this.onAudioMediaGet);
9
+ };
10
+
11
+ init = () => {
12
+ this.quality = this.PARAMS.AUDIO.QUALITY;
13
+ };
14
+
15
+ onAudioMediaLoad = ({ name, layer: mediaType, priority = this.CONST.MEDIA.PRIORITIES.BACKGROUND } = {}) => {
16
+ const [type, quality] = [this.CONST.MEDIA.TYPES.AUDIO, this.quality];
17
+
18
+ return name && this.emit(this.EVENTS.MEDIA.LOAD, { type, quality, name, mediaType, priority });
19
+ };
20
+
21
+ onAudioMediaGet = ({ name, layer: mediaType } = {}) =>
22
+ this.emitOne(this.EVENTS.MEDIA.GET, { type: this.CONST.MEDIA.TYPES.AUDIO, quality: this.quality, mediaType, name });
23
+ }
@@ -0,0 +1,42 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ export class AudioPause extends Module {
4
+ name = "audio.pause";
5
+
6
+ subscribe = () => {
7
+ this.on(this.EVENTS.AUDIO.PAUSE, this.onAudioPause);
8
+ };
9
+
10
+ onAudioPause = async ({ layer, name, duration } = {}) => {
11
+ const media = await this.emitOne(this.EVENTS.AUDIO.MEDIA_GET, { layer, name });
12
+ const audio = await this.emitOne(this.EVENTS.AUDIO.TMP_GET, { src: media.src });
13
+
14
+ if (!duration) return audio.pause();
15
+
16
+ return new Promise(async (resolve) => {
17
+ const initialVolume = await this.emitOne(this.EVENTS.AUDIO.VOLUME_GET, { layer, name });
18
+ const initialTs = new Date().getTime();
19
+
20
+ const onTick = () => {
21
+ const ts = new Date().getTime();
22
+
23
+ if (ts - initialTs >= duration || audio.currentTime + 0.1 >= audio.duration) {
24
+ audio.pause();
25
+
26
+ this.emit(this.EVENTS.AUDIO.VOLUME, { layer, name, volume: initialVolume });
27
+
28
+ return resolve();
29
+ }
30
+
31
+ const delta = (ts - initialTs) / duration;
32
+ const newVolume = Math.max((1 - delta) * initialVolume, 0);
33
+
34
+ this.emit(this.EVENTS.AUDIO.VOLUME, { layer, name, volume: newVolume });
35
+
36
+ requestAnimationFrame(onTick);
37
+ };
38
+
39
+ requestAnimationFrame(onTick);
40
+ });
41
+ };
42
+ }
@@ -0,0 +1,56 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ export class AudioPlay extends Module {
4
+ name = "audio.play";
5
+
6
+ subscribe = () => {
7
+ this.on(this.EVENTS.AUDIO.PLAY, this.onAudioPlay);
8
+ };
9
+
10
+ onAudioPlay = ({ layer, name, durations: [startDuration, finishDuration], volume = 100, isLoop = true } = {}) =>
11
+ this.playAudio(layer, name, startDuration, finishDuration, volume, isLoop);
12
+
13
+ playAudio = async (layer, name, startDuration, finishDuration, volume, isLoop) => {
14
+ const media = await this.emitOne(this.EVENTS.AUDIO.MEDIA_GET, { layer, name });
15
+
16
+ const { audio, track, gainNode } = this.prepareMedia(media, startDuration, finishDuration);
17
+
18
+ await this.emit(this.EVENTS.AUDIO.TMP_SET, { media: audio });
19
+
20
+ const onEnded = async () => {
21
+ const newVolume = await this.emitOne(this.EVENTS.AUDIO.VOLUME_GET, { layer, name });
22
+
23
+ audio.removeEventListener("ended", onEnded);
24
+
25
+ track.disconnect();
26
+ gainNode.disconnect();
27
+
28
+ audio.remove();
29
+
30
+ await this.emit(this.EVENTS.AUDIO.TMP_RM, { src: media.src });
31
+
32
+ if (isLoop) return this.playAudio(layer, name, startDuration, finishDuration, newVolume, isLoop);
33
+ };
34
+
35
+ audio.addEventListener("ended", onEnded);
36
+
37
+ await audio.play();
38
+
39
+ await this.emit(this.EVENTS.AUDIO.VOLUME, { layer, name, volume });
40
+ };
41
+
42
+ prepareMedia = (media, startDuration, finishDuration) => {
43
+ const [audio, ctx] = [new Audio(media.src), new AudioContext()];
44
+ const [track, gainNode] = [ctx.createMediaElementSource(audio), ctx.createGain()];
45
+
46
+ track.connect(gainNode);
47
+ gainNode.connect(ctx.destination);
48
+
49
+ gainNode.gain.setValueAtTime(0, 0);
50
+ gainNode.gain.linearRampToValueAtTime(1, startDuration / 1000);
51
+ gainNode.gain.setValueAtTime(1, media.duration - finishDuration / 1000);
52
+ gainNode.gain.linearRampToValueAtTime(0, media.duration);
53
+
54
+ return { audio, track, gainNode };
55
+ };
56
+ }
@@ -0,0 +1,40 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ export class AudioResume extends Module {
4
+ name = "audio.resume";
5
+
6
+ subscribe = () => {
7
+ this.on(this.EVENTS.AUDIO.RESUME, this.onAudioResume);
8
+ };
9
+
10
+ onAudioResume = async ({ layer, name, duration } = {}) => {
11
+ const media = await this.emitOne(this.EVENTS.AUDIO.MEDIA_GET, { layer, name });
12
+ const audio = await this.emitOne(this.EVENTS.AUDIO.TMP_GET, { src: media.src });
13
+
14
+ if (!duration) return audio.play();
15
+
16
+ return new Promise(async (resolve) => {
17
+ const initialVolume = await this.emitOne(this.EVENTS.AUDIO.VOLUME_GET, { layer, name });
18
+ const initialTs = new Date().getTime();
19
+
20
+ await this.emit(this.EVENTS.AUDIO.VOLUME, { layer, name, volume: 0 });
21
+ await audio.play();
22
+
23
+ const onTick = () => {
24
+ const ts = new Date().getTime();
25
+
26
+ if (ts - initialTs >= duration || audio.currentTime + 0.1 >= audio.duration)
27
+ return this.emit(this.EVENTS.AUDIO.VOLUME, { layer, name, volume: initialVolume }).then(resolve);
28
+
29
+ const delta = (ts - initialTs) / duration;
30
+ const newVolume = delta * initialVolume;
31
+
32
+ this.emit(this.EVENTS.AUDIO.VOLUME, { layer, name, volume: newVolume });
33
+
34
+ requestAnimationFrame(onTick);
35
+ };
36
+
37
+ requestAnimationFrame(onTick);
38
+ });
39
+ };
40
+ }
@@ -0,0 +1,23 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ export class AudioStop extends Module {
4
+ name = "audio.stop";
5
+
6
+ subscribe = () => {
7
+ this.on(this.EVENTS.AUDIO.STOP, this.onAudioStop);
8
+ };
9
+
10
+ onAudioStop = async ({ layer, name, duration = 0 } = {}) => {
11
+ const media = await this.emitOne(this.EVENTS.AUDIO.MEDIA_GET, { layer, name });
12
+ const oldAudio = await this.emitOne(this.EVENTS.AUDIO.TMP_GET, { src: media.src });
13
+
14
+ if (!oldAudio) return;
15
+
16
+ if (duration) await this.emit(this.EVENTS.AUDIO.PAUSE, { layer, name, duration });
17
+ else oldAudio.pause();
18
+
19
+ oldAudio.remove();
20
+
21
+ return this.emit(this.EVENTS.AUDIO.TMP_RM, { src: media.src });
22
+ };
23
+ }
package/modules/tmp.js ADDED
@@ -0,0 +1,24 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ export class AudioTmp extends Module {
4
+ name = "audio.tmp";
5
+
6
+ medias = {};
7
+
8
+ subscribe = () => {
9
+ this.on(this.EVENTS.AUDIO.TMP_SET, this.onAudioTmpSet);
10
+ this.on(this.EVENTS.AUDIO.TMP_GET, this.onAudioTmpGet);
11
+ this.on(this.EVENTS.AUDIO.TMP_RM, this.onAudioTmpRm);
12
+ };
13
+
14
+ onAudioTmpSet = ({ media } = {}) => {
15
+ this.medias[media.src] = media;
16
+ };
17
+ onAudioTmpGet = ({ src } = {}) => this.medias[src];
18
+ onAudioTmpRm = ({ src } = {}) => {
19
+ if (!this.medias[src]) return;
20
+
21
+ this.medias[src].remove();
22
+ delete this.medias[src];
23
+ };
24
+ }
@@ -0,0 +1,34 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ export class AudioVolume extends Module {
4
+ name = "audio.volume";
5
+
6
+ subscribe = () => {
7
+ this.on(this.EVENTS.AUDIO.VOLUME, this.onAudioVolume);
8
+ this.on(this.EVENTS.AUDIO.VOLUME_GET, this.onAudioVolumeGet);
9
+
10
+ this.on(this.EVENTS.SETTINGS.CHANGED, this.onSettingsChanged);
11
+ };
12
+
13
+ init = () => this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.AUDIO.VOLUME, value: 100 });
14
+
15
+ onSettingsChanged = ({ name }) => name === this.SETTINGS.AUDIO.VOLUME && this.emit(this.EVENTS.AUDIO.VOLUME_CHANGED);
16
+
17
+ onAudioVolume = async ({ layer, name, volume = 100 } = {}) => {
18
+ const media = await this.emitOne(this.EVENTS.AUDIO.MEDIA_GET, { layer, name });
19
+ const audio = await this.emitOne(this.EVENTS.AUDIO.TMP_GET, { src: media.src });
20
+
21
+ const commonVolume = this.shared.settings[this.SETTINGS.AUDIO.VOLUME] / 100;
22
+
23
+ if (audio) audio.volume = (commonVolume * volume) / 100;
24
+ };
25
+
26
+ onAudioVolumeGet = async ({ layer, name } = {}) => {
27
+ const media = await this.emitOne(this.EVENTS.AUDIO.MEDIA_GET, { layer, name });
28
+ const audio = await this.emitOne(this.EVENTS.AUDIO.TMP_GET, { src: media.src });
29
+
30
+ const commonVolume = this.shared.settings[this.SETTINGS.AUDIO.VOLUME] / 100;
31
+
32
+ if (audio) return (100 * audio.volume) / commonVolume;
33
+ };
34
+ }
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@vnejs/plugins.audio",
3
+ "version": "0.1.1",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1",
7
+ "publish:major:plugin": "npm run publish:major",
8
+ "publish:minor:plugin": "npm run publish:minor",
9
+ "publish:patch:plugin": "npm run publish:patch",
10
+ "publish:major": "npm version major && npm publish --access public",
11
+ "publish:minor": "npm version minor && npm publish --access public",
12
+ "publish:patch": "npm version patch && npm publish --access public"
13
+ },
14
+ "author": "",
15
+ "license": "ISC",
16
+ "description": ""
17
+ }