@dgck81lnn/koishi-plugin-music 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/browser/synth.js +31 -15
- package/lib/index.d.ts +2 -1
- package/lib/index.js +7 -1
- package/package.json +1 -1
package/browser/synth.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const sampleRate = 44100
|
|
2
2
|
|
|
3
|
-
async function synth(notes) {
|
|
3
|
+
async function synth(notes, { noise } = {}) {
|
|
4
4
|
const seconds = Math.max(...notes.map(i => i.end))
|
|
5
5
|
|
|
6
6
|
const ctx = new OfflineAudioContext(1, seconds * sampleRate, sampleRate)
|
|
@@ -26,6 +26,21 @@ async function synth(notes) {
|
|
|
26
26
|
gain.gain.linearRampToValueAtTime(0, note.end)
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
if (noise) {
|
|
30
|
+
// Add some white noise to avoid QQ's voice message encoding issues
|
|
31
|
+
const noiseBuf = ctx.createBuffer(1, Math.min(seconds, 10) * sampleRate, sampleRate)
|
|
32
|
+
for (let data = noiseBuf.getChannelData(0), i = 0; i < data.length; i++)
|
|
33
|
+
data[i] = Math.random() * 2 - 1
|
|
34
|
+
const noiseGain = ctx.createGain()
|
|
35
|
+
noiseGain.gain.value = 0.005
|
|
36
|
+
const noiseSrc = ctx.createBufferSource()
|
|
37
|
+
noiseSrc.buffer = noiseBuf
|
|
38
|
+
noiseSrc.loop = true
|
|
39
|
+
noiseSrc.connect(noiseGain)
|
|
40
|
+
noiseGain.connect(ctx.destination)
|
|
41
|
+
noiseSrc.start(0)
|
|
42
|
+
}
|
|
43
|
+
|
|
29
44
|
const buf = await ctx.startRendering()
|
|
30
45
|
return buf.getChannelData(0)
|
|
31
46
|
}
|
|
@@ -58,40 +73,41 @@ function encodeWav(samples) {
|
|
|
58
73
|
writeInt16(16) // sample depth
|
|
59
74
|
writeBytes("data")
|
|
60
75
|
writeInt32(samples.length * 2) // size of data chunk
|
|
61
|
-
for (let i = 0; i < samples.length; i++)
|
|
76
|
+
for (let i = 0; i < samples.length; i++)
|
|
77
|
+
writeInt16(Math.min(Math.max(samples[i] * 0x7fff, -32768), 32767))
|
|
62
78
|
|
|
63
79
|
return buffer
|
|
64
80
|
}
|
|
65
81
|
|
|
66
82
|
// https://gist.github.com/jonleighton/958841
|
|
67
|
-
|
|
68
|
-
var base64
|
|
69
|
-
var encodings =
|
|
70
|
-
var bytes
|
|
71
|
-
var byteLength
|
|
83
|
+
function arrayBufferToBase64(arrayBuffer) {
|
|
84
|
+
var base64 = ""
|
|
85
|
+
var encodings = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
86
|
+
var bytes = new Uint8Array(arrayBuffer)
|
|
87
|
+
var byteLength = bytes.byteLength
|
|
72
88
|
var byteRemainder = byteLength % 3
|
|
73
|
-
var mainLength
|
|
89
|
+
var mainLength = byteLength - byteRemainder
|
|
74
90
|
var a, b, c, d
|
|
75
91
|
var chunk
|
|
76
92
|
for (var i = 0; i < mainLength; i = i + 3) {
|
|
77
93
|
chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]
|
|
78
94
|
a = (chunk & 16515072) >> 18
|
|
79
|
-
b = (chunk & 258048)
|
|
80
|
-
c = (chunk & 4032)
|
|
95
|
+
b = (chunk & 258048) >> 12
|
|
96
|
+
c = (chunk & 4032) >> 6
|
|
81
97
|
d = chunk & 63
|
|
82
98
|
base64 += encodings[a] + encodings[b] + encodings[c] + encodings[d]
|
|
83
99
|
}
|
|
84
100
|
if (byteRemainder == 1) {
|
|
85
101
|
chunk = bytes[mainLength]
|
|
86
102
|
a = (chunk & 252) >> 2
|
|
87
|
-
b = (chunk & 3)
|
|
88
|
-
base64 += encodings[a] + encodings[b] +
|
|
103
|
+
b = (chunk & 3) << 4
|
|
104
|
+
base64 += encodings[a] + encodings[b] + "=="
|
|
89
105
|
} else if (byteRemainder == 2) {
|
|
90
106
|
chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1]
|
|
91
107
|
a = (chunk & 64512) >> 10
|
|
92
|
-
b = (chunk & 1008)
|
|
93
|
-
c = (chunk & 15)
|
|
94
|
-
base64 += encodings[a] + encodings[b] + encodings[c] +
|
|
108
|
+
b = (chunk & 1008) >> 4
|
|
109
|
+
c = (chunk & 15) << 2
|
|
110
|
+
base64 += encodings[a] + encodings[b] + encodings[c] + "="
|
|
95
111
|
}
|
|
96
112
|
return base64
|
|
97
113
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { Context, Schema } from "koishi";
|
|
1
|
+
import { Computed, Context, Schema } from "koishi";
|
|
2
2
|
export declare const name = "music";
|
|
3
3
|
export declare const inject: string[];
|
|
4
4
|
export interface Config {
|
|
5
5
|
evalCommand: "glot" | "eval";
|
|
6
|
+
noise: Computed<boolean>;
|
|
6
7
|
}
|
|
7
8
|
export declare const Config: Schema<Config>;
|
|
8
9
|
export interface Note {
|
package/lib/index.js
CHANGED
|
@@ -11,6 +11,9 @@ exports.Config = koishi_1.Schema.object({
|
|
|
11
11
|
evalCommand: koishi_1.Schema.union(["glot", "eval"])
|
|
12
12
|
.default("glot")
|
|
13
13
|
.description("用于安全执行 js 代码的指令。"),
|
|
14
|
+
noise: koishi_1.Schema.computed(Boolean)
|
|
15
|
+
.default(true)
|
|
16
|
+
.description("是否添加白噪音来尝试规避 QQ 的语音编码杂音问题。"),
|
|
14
17
|
});
|
|
15
18
|
const gutterFunc = (f) => {
|
|
16
19
|
function createCompatibilityBpmFunction(v) {
|
|
@@ -118,7 +121,10 @@ function apply(ctx, config) {
|
|
|
118
121
|
}
|
|
119
122
|
const page = await ctx.puppeteer.page();
|
|
120
123
|
await page.goto((0, url_1.pathToFileURL)((0, path_1.resolve)(__dirname, "../browser/index.html")).href);
|
|
121
|
-
const
|
|
124
|
+
const opt = {
|
|
125
|
+
noise: session.resolve(config.noise),
|
|
126
|
+
};
|
|
127
|
+
const base64 = (await page.evaluate(`synth(${data}, ${JSON.stringify(opt)}).then(encodeWav).then(arrayBufferToBase64)`));
|
|
122
128
|
page.close().catch(() => { });
|
|
123
129
|
return koishi_1.h.audio("data:audio/wav;base64," + base64);
|
|
124
130
|
});
|