From 6a251f7e7159f6f093e7289994699c26a624a592 Mon Sep 17 00:00:00 2001
From: unanmed <1319491857@qq.com>
Date: Thu, 15 Jun 2023 12:35:43 +0800
Subject: [PATCH] =?UTF-8?q?=E7=AB=8B=E4=BD=93=E5=A3=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
public/libs/extensions.js | 54 ---------------------------------------
public/main.js | 1 -
src/core/audio/sound.ts | 44 +++++++++++++++++++++++++++----
src/core/loader/load.ts | 2 +-
src/data/resource.json | 11 +++++---
5 files changed, 48 insertions(+), 64 deletions(-)
delete mode 100644 public/libs/extensions.js
diff --git a/public/libs/extensions.js b/public/libs/extensions.js
deleted file mode 100644
index a6b2eec..0000000
--- a/public/libs/extensions.js
+++ /dev/null
@@ -1,54 +0,0 @@
-///
-
-/*
-extensions.js:负责拓展插件
- */
-
-'use strict';
-
-function extensions() {}
-
-extensions.prototype._load = function (callback) {
- if (main.replayChecking) return callback();
- if (!window.fs) {
- this._loadJs(
- '_server/fs.js',
- function () {
- core.extensions._listExtensions(callback);
- },
- callback
- );
- } else this._listExtensions(callback);
-};
-
-extensions.prototype._loadJs = function (file, callback, onerror) {
- var script = document.createElement('script');
- script.src = file + '?v=' + main.version;
- script.onload = callback;
- script.onerror = onerror;
- main.dom.body.appendChild(script);
-};
-
-extensions.prototype._listExtensions = function (callback) {
- if (!window.fs) return callback();
- fs.readdir('extensions', function (error, data) {
- if (error || !(data instanceof Array)) return callback();
- var list = [];
- data.forEach(function (name) {
- if (/^[\w.-]+\.js$/.test(name)) {
- list.push(name);
- }
- });
- list.sort();
- core.extensions._loadExtensions(list, callback);
- });
-};
-
-extensions.prototype._loadExtensions = function (list, callback) {
- var i = 0;
- var load = function () {
- if (i == list.length) return callback();
- core.extensions._loadJs('extensions/' + list[i++], load, load);
- };
- load();
-};
diff --git a/public/main.js b/public/main.js
index 5fb19dd..6264119 100644
--- a/public/main.js
+++ b/public/main.js
@@ -92,7 +92,6 @@ function main() {
'actions',
'data',
'ui',
- 'extensions',
'core'
];
this.pureData = [
diff --git a/src/core/audio/sound.ts b/src/core/audio/sound.ts
index 5a1cb67..107c722 100644
--- a/src/core/audio/sound.ts
+++ b/src/core/audio/sound.ts
@@ -1,30 +1,62 @@
import { has } from '../../plugin/utils';
import { AudioPlayer } from './audio';
+import resource from '../../data/resource.json';
export class SoundEffect extends AudioPlayer {
static playIndex = 0;
private playing: Record = {};
private _stopingAll: boolean = false;
+ private playMap: Map = new Map();
+
+ stereo: boolean = false;
+
+ gain: GainNode = AudioPlayer.ac.createGain();
+ panner: PannerNode | null = null;
+ merger: ChannelMergerNode | null = null;
constructor(data: ArrayBuffer, stereo: boolean = false) {
super(data);
this.on('end', node => {
if (this._stopingAll) return;
- const entry = Object.entries(this.playing);
- const index = entry.find(v => node === v[1])?.[0];
+ const index = this.playMap.get(node);
if (!index) return;
delete this.playing[index];
});
- this.initAudio(stereo);
+ this.on('update', () => {
+ this.initAudio(this.stereo);
+ });
+
+ this.stereo = stereo;
}
/**
* 设置音频路由线路
* @param stereo 是否启用立体声
*/
- protected initAudio(stereo: boolean = false) {}
+ protected initAudio(stereo: boolean = false) {
+ const channel = this.buffer?.numberOfChannels;
+ const ac = AudioPlayer.ac;
+ if (!channel) return;
+ if (stereo) {
+ this.panner = ac.createPanner();
+ this.panner.connect(this.gain);
+ if (channel === 1) {
+ this.merger = ac.createChannelMerger();
+ this.merger.connect(this.panner);
+ this.baseNode = [
+ { node: this.merger, channel: 0 },
+ { node: this.merger, channel: 1 }
+ ];
+ } else {
+ this.baseNode = [{ node: this.panner }];
+ }
+ } else {
+ this.baseNode = [{ node: this.gain }];
+ }
+ this.gain.connect(this.getDestination());
+ }
/**
* 播放音频
@@ -35,6 +67,7 @@ export class SoundEffect extends AudioPlayer {
if (!node) return;
const index = SoundEffect.playIndex++;
this.playing[index] = node;
+ this.playMap.set(node, index);
return index;
}
@@ -71,7 +104,8 @@ class SoundController {
* @param data 音频的ArrayBuffer信息,会被解析为AudioBuffer
*/
add(uri: string, data: ArrayBuffer) {
- const se = new SoundEffect(data);
+ const stereo = resource.stereoSE.includes(uri);
+ const se = new SoundEffect(data, stereo);
if (this.list[uri]) {
console.warn(`Repeated sound effect: ${uri}.`);
}
diff --git a/src/core/loader/load.ts b/src/core/loader/load.ts
index 610c0a8..25eb67e 100644
--- a/src/core/loader/load.ts
+++ b/src/core/loader/load.ts
@@ -4,7 +4,7 @@ import { Resource, getTypeByResource } from './resource';
const info = resource;
export function readyAllResource() {
- info.forEach(v => {
+ info.resource.forEach(v => {
const type = getTypeByResource(v);
if (type === 'zip') {
ancTe.zipResource.set(v, new Resource(v, 'zip'));
diff --git a/src/data/resource.json b/src/data/resource.json
index 3f83663..1efd2eb 100644
--- a/src/data/resource.json
+++ b/src/data/resource.json
@@ -1,3 +1,8 @@
-[
- ""
-]
\ No newline at end of file
+{
+ "resource": [
+ ""
+ ],
+ "stereoSE": [
+ ""
+ ]
+}
\ No newline at end of file