From 9c07e57c6e522a40e2f42e0a9ff2b9d201559180 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Thu, 4 Jul 2019 19:09:34 +0800 Subject: [PATCH] wechat audio perf opt (#4821) --- cocos/3d/assets/audio/player-dom.ts | 8 ++--- cocos/3d/assets/audio/player-web.ts | 10 +++--- cocos/3d/assets/audio/player-wx.ts | 33 ++++++++++++++------ cocos/3d/assets/audio/player.ts | 2 +- cocos/3d/assets/effect-asset.ts | 17 +++------- cocos/3d/framework/audio-source-component.ts | 5 +++ 6 files changed, 39 insertions(+), 36 deletions(-) diff --git a/cocos/3d/assets/audio/player-dom.ts b/cocos/3d/assets/audio/player-dom.ts index 5b77afd959..bd846c2c69 100644 --- a/cocos/3d/assets/audio/player-dom.ts +++ b/cocos/3d/assets/audio/player-dom.ts @@ -133,12 +133,6 @@ export class AudioPlayerDOM extends AudioPlayer { return this._audio ? this._audio.currentTime : 0; } - public getDuration () { - if (!this._audio) { return this._duration; } - // electron & ios wechat browser doesn't have duration - return isNaN(this._audio.duration) ? this._duration : this._audio.duration; - } - public setVolume (val: number, immediate: boolean) { this._volume = val; /* note this won't work for ios devices, for there @@ -159,4 +153,6 @@ export class AudioPlayerDOM extends AudioPlayer { public getLoop () { return this._loop; } + + public destroy () {} } diff --git a/cocos/3d/assets/audio/player-web.ts b/cocos/3d/assets/audio/player-web.ts index 69fac1e9d8..398f48abc6 100644 --- a/cocos/3d/assets/audio/player-web.ts +++ b/cocos/3d/assets/audio/player-web.ts @@ -83,9 +83,9 @@ export class AudioPlayerWeb extends AudioPlayer { this._on_ended(); clearInterval(this._currentTimer); if (this._sourceNode.loop) { - this._currentTimer = window.setInterval(this._on_ended, this._audio!.duration * 1000); + this._currentTimer = window.setInterval(this._on_ended, this._audio.duration * 1000); } - }, (this._audio!.duration - this._offset) * 1000); + }, (this._audio.duration - this._offset) * 1000); }; this._on_gesture = () => { @@ -147,10 +147,6 @@ export class AudioPlayerWeb extends AudioPlayer { return this._context.currentTime - this._startTime + this._offset; } - public getDuration () { - return this._audio ? this._audio.duration : 0; - } - public setVolume (val: number, immediate: boolean) { this._volume = val; if (!immediate && this._gainNode.gain.setTargetAtTime) { @@ -172,4 +168,6 @@ export class AudioPlayerWeb extends AudioPlayer { public getLoop () { return this._loop; } + + public destroy () {} } diff --git a/cocos/3d/assets/audio/player-wx.ts b/cocos/3d/assets/audio/player-wx.ts index 303c85d508..adb9023339 100644 --- a/cocos/3d/assets/audio/player-wx.ts +++ b/cocos/3d/assets/audio/player-wx.ts @@ -29,7 +29,16 @@ import { AudioPlayer, IAudioInfo, PlayingState } from './player'; +/** + * The main lesson learnt during the porting is, + * be very careful with the getter methods on InnerAudioContext, + * serious performance issue have been encountered when accessed frequently. + * For now this includes duration, current time and volume, + * so those are all maintained manually here. + */ export class AudioPlayerWX extends AudioPlayer { + protected _startTime = 0; + protected _offset = 0; protected _volume = 1; protected _loop = false; protected _oneShoting = false; @@ -38,21 +47,25 @@ export class AudioPlayerWX extends AudioPlayer { constructor (info: IAudioInfo) { super(info); this._audio = info.clip; - this._audio.obeyMuteSwitch = false; this._audio.onPlay(() => { + this._startTime = performance.now(); this._state = PlayingState.PLAYING; this._eventTarget.emit('started'); }); this._audio.onPause(() => { + this._offset += performance.now() - this._startTime; this._state = PlayingState.STOPPED; this._oneShoting = false; }); this._audio.onStop(() => { this._state = PlayingState.STOPPED; this._oneShoting = false; + this._offset = 0; this._audio.seek(0); }); this._audio.onEnded(() => { + this._offset = 0; + this._startTime = performance.now(); this._state = PlayingState.STOPPED; this._eventTarget.emit('ended'); if (this._oneShoting) { @@ -61,6 +74,7 @@ export class AudioPlayerWX extends AudioPlayer { this._oneShoting = false; } }); + this._audio.onError((res: any) => console.error(res.errMsg)); } public play () { @@ -82,6 +96,7 @@ export class AudioPlayerWX extends AudioPlayer { /* InnerAudioContext doesn't support multiple playback at the same time so here we fall back to re-start style approach */ if (!this._audio) { return; } + this._offset = 0; this._audio.seek(0); this._audio.volume = volume; if (this._oneShoting) { return; } @@ -91,21 +106,20 @@ export class AudioPlayerWX extends AudioPlayer { } public getCurrentTime () { - return this._audio ? this._audio.currentTime : 0; + if (this._state !== PlayingState.PLAYING) { return this._offset / 1000; } + let current = (performance.now() - this._startTime + this._offset) / 1000; + if (current > this._duration) { current -= this._duration; this._startTime += this._duration * 1000; } + return current; } public setCurrentTime (val: number) { if (!this._audio) { return; } + this._startTime = performance.now(); + this._offset = val * 1000; this._audio.seek(val); } - public getDuration () { - return this._audio ? this._audio.duration : this._duration; - } - public getVolume () { - // be careful with the volume getter on InnerAudioContext, - // serious performance issue encountered when accessed frequently return this._volume; } @@ -124,7 +138,6 @@ export class AudioPlayerWX extends AudioPlayer { } public destroy () { - if (this._audio) { this._audio.destroy(); return true; } - return false; + if (this._audio) { this._audio.destroy(); } } } diff --git a/cocos/3d/assets/audio/player.ts b/cocos/3d/assets/audio/player.ts index f794dd2f54..f89605b04d 100644 --- a/cocos/3d/assets/audio/player.ts +++ b/cocos/3d/assets/audio/player.ts @@ -55,11 +55,11 @@ export abstract class AudioPlayer { public abstract playOneShot (volume: number): void; public abstract setCurrentTime (val: number): void; public abstract getCurrentTime (): number; - public abstract getDuration (): number; public abstract setVolume (val: number, immediate: boolean): void; public abstract getVolume (): number; public abstract setLoop (val: boolean): void; public abstract getLoop (): boolean; public getState () { return this._state; } + public getDuration () { return this._duration; } public destroy () {} } diff --git a/cocos/3d/assets/effect-asset.ts b/cocos/3d/assets/effect-asset.ts index d4365594a6..25b51135fe 100644 --- a/cocos/3d/assets/effect-asset.ts +++ b/cocos/3d/assets/effect-asset.ts @@ -29,9 +29,9 @@ import { Asset } from '../../assets/asset'; import { ccclass, property } from '../../core/data/class-decorator'; -import { GFXDynamicState, GFXPrimitiveMode, GFXType } from '../../gfx/define'; +import { GFXDynamicState, GFXPrimitiveMode } from '../../gfx/define'; import { GFXBlendState, GFXDepthStencilState, GFXRasterizerState } from '../../gfx/pipeline-state'; -import { GFXUniform } from '../../gfx/shader'; +import { GFXUniformBlock, GFXUniformSampler } from '../../gfx/shader'; import { RenderPassStage } from '../../pipeline/define'; import { programLib } from '../../renderer/core/program-lib'; @@ -61,20 +61,11 @@ export interface ITechniqueInfo { name?: string; } -export interface IBlockInfo { +export interface IBlockInfo extends GFXUniformBlock { defines: string[]; - // extends GFXUniformBlock - binding: number; - name: string; - members: GFXUniform[]; } -export interface ISamplerInfo { +export interface ISamplerInfo extends GFXUniformSampler { defines: string[]; - // extends GFXUniformSampler - binding: number; - name: string; - type: GFXType; - count: number; } export interface IDefineInfo { name: string; diff --git a/cocos/3d/framework/audio-source-component.ts b/cocos/3d/framework/audio-source-component.ts index cee4076aeb..e333ad4a92 100644 --- a/cocos/3d/framework/audio-source-component.ts +++ b/cocos/3d/framework/audio-source-component.ts @@ -29,6 +29,7 @@ import { Component } from '../../components/component'; import { ccclass, menu, property } from '../../core/data/class-decorator'; +import { clamp } from '../../core/vmath'; import { AudioClip } from '../assets/audio/clip'; /** @@ -112,6 +113,8 @@ export class AudioSourceComponent extends Component { */ @property set volume (val) { + if (isNaN(val)) { console.warn('illegal audio volume!'); return; } + val = clamp(val, 0, 1); if (this._clip) { this._clip.setVolume(val); // on some platform volume control may not be available @@ -196,6 +199,8 @@ export class AudioSourceComponent extends Component { * @param num 要跳转到的播放时间 */ set currentTime (num: number) { + if (isNaN(num)) { console.warn('illegal audio time!'); return; } + num = clamp(num, 0, this.duration); this._cachedCurrentTime = num; if (!this._clip) { return; } this._clip.setCurrentTime(this._cachedCurrentTime);