@dgck81lnn/koishi-plugin-music 0.1.2 → 0.1.4

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 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
  }
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 base64 = (await page.evaluate(`synth(${data}).then(encodeWav).then(arrayBufferToBase64)`));
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
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@dgck81lnn/koishi-plugin-music",
3
3
  "description": "Synthesize melodies in Koishi",
4
- "version": "0.1.2",
4
+ "version": "0.1.4",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
@@ -9,8 +9,13 @@
9
9
  "browser"
10
10
  ],
11
11
  "license": "MIT",
12
+ "homepage": "https://github.com/DGCK81LNN/koishi-plugin-music",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/DGCK81LNN/koishi-plugin-music.git"
16
+ },
12
17
  "peerDependencies": {
13
- "koishi": "4.17.12"
18
+ "koishi": "^4.17.12"
14
19
  },
15
20
  "devDependencies": {
16
21
  "koishi-plugin-puppeteer": "^3.9.0"