Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: follow [email protected] (AudioAsset#loopOffset) #298

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main
- feat-loopoffset

env:
NODE_VERSION: 18
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 2.9.0-beta.0
* @akashic/[email protected] に追従
* `AudioAsset#loopOffset` に対応

## 2.8.3
* `MouseTouchEventHandler` において `button` の値が一部 `PointerEventHandler` と異なってしまう問題を修正

Expand Down
12 changes: 6 additions & 6 deletions e2e/tests/audio/HTMLAudio.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("HTMLAudio", () => {
const { HTMLAudioPlugin } = require("@akashic/pdi-browser") as typeof index;
const plugin = new HTMLAudioPlugin();
const system = new window.__mock__.MockAudioSystem({id: "voice"});
const asset = plugin.createAsset("id", audioAssetPath, 100, system, false, {}, 10);
const asset = plugin.createAsset("id", audioAssetPath, 100, system, false, {}, 10, undefined);
return new Promise<AudioAsset>((resolve, reject) => {
const loader: AssetLoadHandler = {
_onAssetLoad: (asset: AudioAsset) => {
Expand All @@ -65,7 +65,7 @@ describe("HTMLAudio", () => {
const { HTMLAudioPlugin } = require("@akashic/pdi-browser") as typeof index;
const plugin = new HTMLAudioPlugin();
const system = new window.__mock__.MockAudioSystem({id: "voice"});
const asset = plugin.createAsset("id", audioAssetPath + "?" + query, 100, system, false, {}, 0);
const asset = plugin.createAsset("id", audioAssetPath + "?" + query, 100, system, false, {}, 0, undefined);
return new Promise<AudioAsset>((resolve, reject) => {
const loader: AssetLoadHandler = {
_onAssetLoad: (asset: AudioAsset) => {
Expand All @@ -87,7 +87,7 @@ describe("HTMLAudio", () => {
const { HTMLAudioPlugin } = require("@akashic/pdi-browser") as typeof index;
const plugin = new HTMLAudioPlugin();
const system = new window.__mock__.MockAudioSystem({id: "voice"});
const asset = plugin.createAsset("id", "not_found_audio", 100, system, false, {}, 0);
const asset = plugin.createAsset("id", "not_found_audio", 100, system, false, {}, 0, undefined);
return new Promise<AssetLoadError>((resolve, reject) => {
const loader: AssetLoadHandler = {
_onAssetLoad: (_asset: AudioAsset) => {
Expand All @@ -112,7 +112,7 @@ describe("HTMLAudio", () => {
const plugin = new HTMLAudioPlugin();
plugin.supportedFormats = [format];
const system = new window.__mock__.MockAudioSystem({id: "voice"});
const asset = plugin.createAsset("id", audioAssetPath, 100, system, false, {}, 0);
const asset = plugin.createAsset("id", audioAssetPath, 100, system, false, {}, 0, undefined);
return new Promise<AudioAsset>((resolve) => {
const loader: AssetLoadHandler = {
_onAssetLoad: (asset: AudioAsset) => {
Expand All @@ -139,7 +139,7 @@ describe("HTMLAudio", () => {
const plugin = new HTMLAudioPlugin();
plugin.supportedFormats = ["aac", "mp4"];
const system = new window.__mock__.MockAudioSystem({id: "voice"});
const asset = plugin.createAsset("id", audioAsset2Path + "?" + query, 100, system, false, {}, 0);
const asset = plugin.createAsset("id", audioAsset2Path + "?" + query, 100, system, false, {}, 0, undefined);
return new Promise<AudioAsset>((resolve) => {
const loader: AssetLoadHandler = {
_onAssetLoad: (asset: AudioAsset) => {
Expand Down Expand Up @@ -168,7 +168,7 @@ describe("HTMLAudio", () => {
const plugin = new HTMLAudioPlugin();
plugin.supportedFormats = ["aac", "mp4"];
const system = new window.__mock__.MockAudioSystem({id: "voice"});
const asset = plugin.createAsset("id", seAssetPath, 100, system, false, {}, 0);
const asset = plugin.createAsset("id", seAssetPath, 100, system, false, {}, 0, undefined);
const player = asset.play();
player.changeVolume(0.1);
return new Promise<[AudioAsset, AudioPlayer]>((resolve, reject) => {
Expand Down
12 changes: 6 additions & 6 deletions e2e/tests/audio/WebAudio.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("WebAudio", () => {
const { WebAudioPlugin } = require("@akashic/pdi-browser") as typeof index;
const plugin = new WebAudioPlugin();
const system = new window.__mock__.MockAudioSystem({id: "voice"});
const asset = plugin.createAsset("id", audioAssetPath, 100, system, false, {}, 10);
const asset = plugin.createAsset("id", audioAssetPath, 100, system, false, {}, 10, undefined);
return new Promise<AudioAsset>((resolve, reject) => {
const loader: AssetLoadHandler = {
_onAssetLoad: (asset: AudioAsset) => {
Expand All @@ -65,7 +65,7 @@ describe("WebAudio", () => {
const { WebAudioPlugin } = require("@akashic/pdi-browser") as typeof index;
const plugin = new WebAudioPlugin();
const system = new window.__mock__.MockAudioSystem({id: "voice"});
const asset = plugin.createAsset("id", audioAssetPath + "?" + query, 100, system, false, {}, 0);
const asset = plugin.createAsset("id", audioAssetPath + "?" + query, 100, system, false, {}, 0, undefined);
return new Promise<AudioAsset>((resolve, reject) => {
const loader: AssetLoadHandler = {
_onAssetLoad: (asset: AudioAsset) => {
Expand All @@ -87,7 +87,7 @@ describe("WebAudio", () => {
const { WebAudioPlugin } = require("@akashic/pdi-browser") as typeof index;
const plugin = new WebAudioPlugin();
const system = new window.__mock__.MockAudioSystem({id: "voice"});
const asset = plugin.createAsset("id", "not_found_audio", 100, system, false, {}, 0);
const asset = plugin.createAsset("id", "not_found_audio", 100, system, false, {}, 0, undefined);
return new Promise<AssetLoadError>((resolve, reject) => {
const loader: AssetLoadHandler = {
_onAssetLoad: (_asset: AudioAsset) => {
Expand All @@ -113,7 +113,7 @@ describe("WebAudio", () => {
const plugin = new WebAudioPlugin();
plugin.supportedFormats = [format];
const system = new window.__mock__.MockAudioSystem({id: "voice"});
const asset = plugin.createAsset("id", audioAssetPath, 100, system, false, {}, 0);
const asset = plugin.createAsset("id", audioAssetPath, 100, system, false, {}, 0, undefined);
return new Promise<AudioAsset>((resolve) => {
const loader: AssetLoadHandler = {
_onAssetLoad: (asset: AudioAsset) => {
Expand All @@ -140,7 +140,7 @@ describe("WebAudio", () => {
const plugin = new WebAudioPlugin();
plugin.supportedFormats = ["aac"];
const system = new window.__mock__.MockAudioSystem({id: "voice"});
const asset = plugin.createAsset("id", audioAsset2Path + "?" + query, 100, system, false, {}, 0);
const asset = plugin.createAsset("id", audioAsset2Path + "?" + query, 100, system, false, {}, 0, undefined);
return new Promise<AudioAsset>((resolve) => {
const loader: AssetLoadHandler = {
_onAssetLoad: (asset: AudioAsset) => {
Expand Down Expand Up @@ -168,7 +168,7 @@ describe("WebAudio", () => {
const { WebAudioPlugin } = require("@akashic/pdi-browser") as typeof index;
const plugin = new WebAudioPlugin();
const system = new window.__mock__.MockAudioSystem({id: "voice"});
const asset = plugin.createAsset("id", seAssetPath, 100, system, false, {}, 0);
const asset = plugin.createAsset("id", seAssetPath, 100, system, false, {}, 0, undefined);
const player = asset.play();
player.changeVolume(0.1);
return new Promise<[AudioAsset, AudioPlayer]>((resolve, reject) => {
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@akashic/pdi-browser",
"version": "2.8.3",
"version": "2.9.0-beta.0",
"description": "An akashic-pdi implementation for Web browsers",
"main": "index.js",
"typings": "lib/full/index.d.ts",
Expand Down Expand Up @@ -43,7 +43,7 @@
"devDependencies": {
"@akashic/amflow": "^3.3.0",
"@akashic/eslint-config": "^1.1.1",
"@akashic/pdi-types": "^1.12.0",
"@akashic/pdi-types": "^1.13.0-beta.0",
"@akashic/playlog": "^3.3.0",
"@types/jest": "^29.2.0",
"@types/node": "^18.0.0",
Expand Down Expand Up @@ -74,6 +74,7 @@
"@akashic/trigger": "^2.0.1"
},
"publishConfig": {
"@akashic:registry": "https://registry.npmjs.org/"
"@akashic:registry": "https://registry.npmjs.org/",
"tag": "next"
}
}
5 changes: 3 additions & 2 deletions src/ResourceFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ export class ResourceFactory implements pdi.ResourceFactory {
system: pdi.AudioSystem,
loop: boolean,
hint: pdi.AudioAssetHint,
offset: number
offset: number,
loopOffset: number
): AudioAsset {
const activePlugin = this._audioPluginManager.getActivePlugin();
if (!activePlugin) {
throw new Error("ResourceFactory#createAudioAsset(): could not initialize ActivePlugin");
}
const audioAsset = activePlugin.createAsset(id, assetPath, duration, system, loop, hint, offset);
const audioAsset = activePlugin.createAsset(id, assetPath, duration, system, loop, hint, offset, loopOffset);
this._audioManager.registerAudioAsset(audioAsset);
audioAsset.onDestroyed.addOnce(this._onAudioAssetDestroyed, this);
return audioAsset;
Expand Down
5 changes: 4 additions & 1 deletion src/asset/AudioAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export abstract class AudioAsset extends Asset implements pdi.AudioAsset {
loop: boolean;
hint: pdi.AudioAssetHint;
offset: number;
loopOffset: number | undefined;
_system: pdi.AudioSystem;
_lastPlayedPlayer: pdi.AudioPlayer | undefined;

Expand All @@ -18,14 +19,16 @@ export abstract class AudioAsset extends Asset implements pdi.AudioAsset {
system: pdi.AudioSystem,
loop: boolean,
hint: pdi.AudioAssetHint,
offset: number
offset: number,
loopOffset: number | undefined,
) {
super(id, path);
this.duration = duration;
this.loop = loop;
this.hint = hint;
this._system = system;
this.offset = offset;
this.loopOffset = loopOffset;
this.path = this._modifyPath(this.path);
}

Expand Down
3 changes: 2 additions & 1 deletion src/plugin/AudioPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export interface AudioPlugin {
system: pdi.AudioSystem,
loop: boolean,
hint: pdi.AudioAssetHint,
offset: number
offset: number,
loopOffset: number,
) => AudioAsset;

createPlayer: (system: pdi.AudioSystem, manager: AudioManager) => AudioPlayer;
Expand Down
145 changes: 145 additions & 0 deletions src/plugin/HTMLAudioPlugin/AudioElementPlayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/* eslint-disable @typescript-eslint/typedef */
/* eslint-disable @typescript-eslint/explicit-function-return-type */

import { Trigger } from "@akashic/trigger";
import { setupChromeMEIWorkaround } from "./HTMLAudioAutoplayHelper";

export interface AudioElementPlayerParameterObject {
id: string;
element: HTMLAudioElement | null;
duration: number;
offset: number;
loop: boolean;
loopOffset: number;
}

export class AudioElementPlayer {
onStop: Trigger<void>;

id: string;
element: HTMLAudioElement | null;
offsetStart: number;
offsetEnd: number;
loopOffset: number;
duration: number;
loop: boolean;

_dummyTimerId: number | null;
_reachEndTimerId: number | null;
_previousCurrentTime: number;

constructor({ id, element, duration, offset, loopOffset, loop }: AudioElementPlayerParameterObject) {
this.id = id;
this.duration = duration;
this.offsetStart = offset;
this.offsetEnd = offset + duration;
this.loop = loop;
this.loopOffset = loopOffset;
this.onStop = new Trigger();
this._dummyTimerId = null;
this._reachEndTimerId = null;

if (element) {
setupChromeMEIWorkaround(element);
element.addEventListener("timeupdate", this._onTimeupdate, false);
element.addEventListener("ended", this._onEnded, false);
element.currentTime = this.offsetStart / 1000;
} else {
if (!loop && duration != null) {
this._setDummyTimer(this.duration);
}
}
this.element = element;
this._previousCurrentTime = element?.currentTime ?? 0;
}

rewind() {
this.pause();
this.setCurrentTime(this.loopOffset || this.offsetStart);
this.play();
}

play() {
this.element?.play().catch((_err) => {
// user interact の前に play() を呼ぶとエラーになる。これは HTMLAudioAutoplayHelper で吸収する
});
}

pause() {
this.element?.pause();
this._clearRewindTimer();
}

setCurrentTime(offset: number) {
if (!this.element) return;
this.element.currentTime = offset / 1000;
this._previousCurrentTime = this.element.currentTime;
}

setVolume(volume: number) {
if (!this.element) return;
this.element.volume = volume;
}

destroy() {
this.onStop.destroy();
const element = this.element;
if (element) {
element.removeEventListener("timeupdate", this._onTimeupdate, false);
element.removeEventListener("ended", this._onEnded, false);
}
this._clearDummyTimer();
this._clearRewindTimer();
}

_setDummyTimer(duration: number) {
this._clearDummyTimer();
this._dummyTimerId = window.setTimeout(() => this.pause(), duration);
}

_clearDummyTimer() {
if (this._dummyTimerId == null) return;
window.clearTimeout(this._dummyTimerId);
this._dummyTimerId = null;
}

_setRewindTimer(duration: number) {
this._clearRewindTimer();
this._reachEndTimerId = window.setTimeout(() => this._onReachEnd(), duration);
}

_clearRewindTimer() {
if (this._reachEndTimerId == null) return;
window.clearTimeout(this._reachEndTimerId);
this._reachEndTimerId = null;
}

_onReachEnd() {
if (this.loop) {
this.rewind();
} else {
this.pause();
this.onStop.fire();
}

this._clearRewindTimer();
}

_onTimeupdate = () => {
const element = this.element!; // this.element が存在する場合にのみ呼び出される
if (element.paused) return;

const currentOffset = element.currentTime * 1000;
const deltaSinceLastCall = Math.max(element.currentTime * 1000 - this._previousCurrentTime, 0);
const deltaUntilEnd = Math.max(this.offsetEnd - element.currentTime * 1000, 0);
this._previousCurrentTime = element.currentTime * 1000;

if (this.offsetEnd < currentOffset + deltaSinceLastCall || this.offsetEnd <= currentOffset) {
this._setRewindTimer(deltaUntilEnd);
}
};

_onEnded = () => {
this._onReachEnd();
};
}
Loading
Loading