Files
cocos4/pal/audio/audio-buffer-manager.ts
T
James Chen 37e1dcc4bd [v3.8.6] Optimize code size: audio (#18198)
* [v3.8.6] Optimize code size: mangle AudioBufferManager since it's not exported.
2025-01-16 10:15:48 +08:00

83 lines
2.6 KiB
TypeScript

/*
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
https://www.cocos.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { warnID } from '../../cocos/core/platform/debug';
/** @mangle */
interface CachedAudioBufferData {
usedCount: number,
audioBuffer: AudioBuffer,
}
interface AudioBufferDataMap {
[url: string]: CachedAudioBufferData;
}
/**
* This is a manager to manage the cache of audio buffer for web audio.
* @mangle
*/
class AudioBufferManager {
private _audioBufferDataMap: AudioBufferDataMap = {};
public addCache (url: string, audioBuffer: AudioBuffer): void {
const audioBufferData = this._audioBufferDataMap[url];
if (audioBufferData) {
warnID(5204, url);
return;
}
this._audioBufferDataMap[url] = {
usedCount: 1,
audioBuffer,
};
}
public retainCache (url: string): void {
const audioBufferData = this._audioBufferDataMap[url];
if (!audioBufferData) {
warnID(5203, url);
return;
}
audioBufferData.usedCount++;
}
public getCache (url: string): AudioBuffer | null | undefined {
const audioBufferData = this._audioBufferDataMap[url];
return audioBufferData?.audioBuffer;
}
public tryReleasingCache (url: string): void {
const audioBufferData = this._audioBufferDataMap[url];
if (!audioBufferData) {
warnID(5203, url);
return;
}
if (--audioBufferData.usedCount <= 0) {
delete this._audioBufferDataMap[url];
}
}
}
export const audioBufferManager = new AudioBufferManager();