audio clips

This commit is contained in:
Yun Hsiao Wu
2019-01-25 18:00:22 +08:00
parent 5df4a92501
commit 7c1754dff4
4 changed files with 100 additions and 163 deletions
@@ -22,99 +22,63 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
// @ts-check
import { _decorator } from "../../core/data/index";
import { ccclass, mixins } from "../../core/data/class-decorator";
import { Asset } from "../../assets/asset";
import { EventTarget } from "../../core/event/index";
const { ccclass, mixins } = _decorator;
/**
* @typedef {number} PlayingState
* @enum {number}
*/
export const PlayingState = {
INITIALIZING: 0,
PLAYING: 1,
STOPPED: 2,
INITIALIZING: 0,
PLAYING: 1,
STOPPED: 2,
};
/**
* Enums indicating the load mode of an audio clip
* @typedef {number} AudioSourceType
* @enum {number}
*/
export const AudioSourceType = {
UNKNOWN_AUDIO: -1,
UNKNOWN_AUDIO: -1,
/**
* load through Web Audio API interface
*/
WEB_AUDIO: 0,
/**
* load through an audio DOM element
*/
DOM_AUDIO: 1,
/**
* load through Web Audio API interface
*/
WEB_AUDIO: 0,
/**
* load through an audio DOM element
*/
DOM_AUDIO: 1,
WX_GAME_AUDIO: 2,
WX_GAME_AUDIO: 2,
};
/**
* @typedef {AudioBuffer|HTMLAudioElement|any} AudioSource
*/
export interface IAudioInfo {
length: number;
}
/**
* The base class for audio clip asset.
* @mixes {EventTarget}
*/
@ccclass('cc.AudioClip')
@mixins(EventTarget)
export class AudioClip extends Asset {
/**
* @type {AudioSource}
*/
_audio = null;
protected _audio: any = null;
protected _duration = 0;
protected _state = PlayingState.INITIALIZING;
protected _loadMode = AudioSourceType.UNKNOWN_AUDIO;
/**
* @type {number}
*/
_duration = 0;
/**
* @type { PlayingState }
*/
_state = PlayingState.INITIALIZING;
/**
* @type { AudioSourceType }
*/
loadMode = AudioSourceType.UNKNOWN_AUDIO;
/**
* Set the actual audio clip asset
* @param {AudioSource} clip
* @param {Object} info
*/
setNativeAsset(clip, info) {
this._audio = clip;
this._duration = info ? info.length : 0;
if (clip) {
super.loaded = true;
this._state = PlayingState.STOPPED;
} else {
super.loaded = false;
this._state = PlayingState.INITIALIZING;
this.duration = 0;
public setNativeAsset (clip: any, info: IAudioInfo) {
this._audio = clip;
this._duration = info ? info.length : 0;
if (clip) {
super.loaded = true;
this._state = PlayingState.STOPPED;
} else {
super.loaded = false;
this._state = PlayingState.INITIALIZING;
this._duration = 0;
}
}
}
/**
* Get current state of the clip
* @return {PlayingState}
*/
getState() {
return this._state;
}
get loadMode () {
return this._loadMode;
}
get state() {
return this._state;
}
}
cc.AudioClip = AudioClip;
@@ -22,45 +22,34 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
// @ts-check
import { _decorator } from "../../core/data/index";
const { ccclass } = _decorator;
import { AudioClip, AudioSourceType, PlayingState } from "./audio-clip";
import { ccclass } from "../../core/data/class-decorator";
import { AudioClip, AudioSourceType, PlayingState, IAudioInfo } from "./audio-clip";
@ccclass('cc.DOMAudioClip')
export class DOMAudioClip extends AudioClip {
/**
* @type {number}
*/
_volume = 1;
protected _volume = 1;
protected _loop = false;
protected _currentTimer = 0;
protected _oneShoting = false;
protected _audio: HTMLAudioElement | null = null;
/**
* @type {boolean}
*/
_loop = false;
/**
* @type {number}
*/
_currentTimer = 0;
/**
* @type {boolean}
*/
_oneShoting = false;
private _post_play: () => void;
private _on_gesture: () => void;
private _alreadyDelayed = false;
constructor() {
super();
this.loadMode = AudioSourceType.DOM_AUDIO;
this._loadMode = AudioSourceType.DOM_AUDIO;
this._post_play = () => {
this._state = AudioSourceType.PLAYING;
// @ts-ignore
this._state = PlayingState.PLAYING;
this.emit('started');
};
this._on_gesture = () => {
if (!this._audio) { return; }
let promise = this._audio.play();
if (!promise) {
console.warn('no promise returned from HTMLMediaElement.play()');
@@ -68,14 +57,14 @@ export class DOMAudioClip extends AudioClip {
}
promise.then(() => {
if (this._alreadyDelayed) this._post_play();
else { this._audio.pause(); this._audio.currentTime = 0; }
else { this._audio!.pause(); this._audio!.currentTime = 0; }
window.removeEventListener('touchend', this._on_gesture);
document.removeEventListener('mouseup', this._on_gesture);
});
};
}
setNativeAsset(clip, info) {
setNativeAsset(clip: HTMLAudioElement, info: IAudioInfo) {
super.setNativeAsset(clip, info);
clip.volume = this._volume;
clip.loop = this._loop;
@@ -83,7 +72,7 @@ export class DOMAudioClip extends AudioClip {
clip.addEventListener('ended', () => {
if (this._oneShoting) return;
this._state = PlayingState.STOPPED;
this._audio.currentTime = 0;
clip.currentTime = 0;
// @ts-ignore
this.emit('ended');
});
@@ -104,13 +93,14 @@ export class DOMAudioClip extends AudioClip {
}
pause() {
if (this._state !== PlayingState.PLAYING) return;
if (!this._audio || this._state !== PlayingState.PLAYING) return;
this._audio.pause();
this._state = PlayingState.STOPPED;
this._oneShoting = false;
}
stop() {
if (!this._audio) return;
this._audio.currentTime = 0;
if (this._state !== PlayingState.PLAYING) return;
this._audio.pause();
@@ -121,23 +111,24 @@ export class DOMAudioClip extends AudioClip {
playOneShot(volume = 1) {
/* HTMLMediaElement doesn't support multiple playback at the
same time so here we fall back to re-start style approach */
if (!this._audio) return;
this._audio.currentTime = 0;
this._audio.volume = volume;
const clip = this._audio;
if (!clip) return;
clip.currentTime = 0;
clip.volume = volume;
if (this._oneShoting) return;
this._audio.loop = false;
clip.loop = false;
this._oneShoting = true;
this._audio.play().then(() => {
this._audio.addEventListener('ended', () => {
this._audio.currentTime = 0;
this._audio.volume = this._volume;
this._audio.loop = this._loop;
clip.play().then(() => {
clip.addEventListener('ended', () => {
clip.currentTime = 0;
clip.volume = this._volume;
clip.loop = this._loop;
this._oneShoting = false;
}, { once: true });
}).catch(() => { this._oneShoting = false; });
}
setCurrentTime(val) {
setCurrentTime(val: number) {
if (!this._audio) return;
this._audio.currentTime = val;
}
@@ -152,7 +143,7 @@ export class DOMAudioClip extends AudioClip {
return isNaN(this._audio.duration) ? this._duration : this._audio.duration;
}
setVolume(val) {
setVolume(val: number) {
this._volume = val;
/* note this won't work for ios devices, for there
is just no way to set HTMLMediaElement's volume */
@@ -164,7 +155,7 @@ export class DOMAudioClip extends AudioClip {
return this._volume;
}
setLoop(val) {
setLoop(val: boolean) {
this._loop = val;
if (this._audio) this._audio.loop = val;
}
@@ -174,4 +165,4 @@ export class DOMAudioClip extends AudioClip {
}
}
cc.DOMAudioClip = DOMAudioClip;
cc.DOMAudioClip = DOMAudioClip;
@@ -22,49 +22,32 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
// @ts-check
import { _decorator } from "../../core/data/index";
const { ccclass } = _decorator;
import { AudioClip, AudioSourceType, PlayingState } from "./audio-clip";
import { ccclass } from "../../core/data/class-decorator";
import { AudioClip, AudioSourceType, PlayingState, IAudioInfo } from "./audio-clip";
@ccclass('cc.WebAudioClip')
export default class WebAudioClip extends AudioClip {
/**
* @type {number}
*/
_startTime;
protected _startTime = 0;
protected _offset = 0;
protected _volume = 1;
protected _loop = false;
protected _currentTimer = 0;
protected _audio: AudioBuffer | null = null;
/**
* @type {number}
*/
_offset;
private _context: AudioContext;
private _sourceNode: AudioBufferSourceNode;
private _gainNode: GainNode;
private _on_ended: () => void;
private _do_play: () => void;
private _on_gesture: () => void;
private _alreadyDelayed = false;
/**
* @type {number}
*/
_volume;
/**
* @type {boolean}
*/
_loop;
/**
* @type {number}
*/
_currentTimer;
constructor(context, app) {
constructor(context: AudioContext) {
super();
this.loadMode = AudioSourceType.WEB_AUDIO;
this._currentTimer = 0;
this._startTime = 0;
this._offset = 0;
this._loop = false;
this._volume = 1;
this._loadMode = AudioSourceType.WEB_AUDIO;
this._app = app;
this._context = context;
this._sourceNode = this._context.createBufferSource();
this._gainNode = this._context.createGain();
@@ -88,19 +71,18 @@ export default class WebAudioClip extends AudioClip {
this._state = PlayingState.PLAYING;
this._startTime = this._context.currentTime;
// delay eval here to yield uniform behavior with other platforms
// @ts-ignore
this._app.once('tick', () => { this.emit('started'); });
cc.director.once(cc.Director.EVENT_AFTER_UPDATE, () => { this.emit('started'); });
/* still not supported by all platforms *
this._sourceNode.onended = this._on_ended;
/* doing it manually for now */
clearInterval(this._currentTimer);
this._currentTimer = setInterval(() => {
this._currentTimer = window.setInterval(() => {
this._on_ended();
clearInterval(this._currentTimer);
if (this._sourceNode.loop) {
this._currentTimer = 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 = () => {
@@ -112,7 +94,7 @@ export default class WebAudioClip extends AudioClip {
};
}
setNativeAsset(clip, info) {
setNativeAsset(clip: AudioBuffer, info: IAudioInfo) {
super.setNativeAsset(clip, info);
if (this._context.state === 'running') return;
window.addEventListener('touchend', this._on_gesture);
@@ -154,7 +136,7 @@ export default class WebAudioClip extends AudioClip {
sourceNode.start();
}
setCurrentTime(val) {
setCurrentTime(val: number) {
this._offset = val;
if (this._state !== PlayingState.PLAYING) return;
this._sourceNode.stop(); this._do_play();
@@ -169,7 +151,7 @@ export default class WebAudioClip extends AudioClip {
return this._audio ? this._audio.duration : 0;
}
setVolume(val) {
setVolume(val: number) {
this._volume = val;
if (this._gainNode.gain.setTargetAtTime) {
this._gainNode.gain.setTargetAtTime(val, this._context.currentTime, 0.01);
@@ -182,7 +164,7 @@ export default class WebAudioClip extends AudioClip {
return this._volume;
}
setLoop(val) {
setLoop(val: boolean) {
this._loop = val;
this._sourceNode.loop = val;
}
@@ -192,4 +174,4 @@ export default class WebAudioClip extends AudioClip {
}
}
cc.WebAudioClip = WebAudioClip;
cc.WebAudioClip = WebAudioClip;