@dgck81lnn/koishi-plugin-music 0.1.2 → 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 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
  }
@@ -66,33 +81,33 @@ function encodeWav(samples) {
66
81
 
67
82
  // https://gist.github.com/jonleighton/958841
68
83
  function arrayBufferToBase64(arrayBuffer) {
69
- var base64 = ''
70
- var encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
71
- var bytes = new Uint8Array(arrayBuffer)
72
- var byteLength = bytes.byteLength
84
+ var base64 = ""
85
+ var encodings = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
86
+ var bytes = new Uint8Array(arrayBuffer)
87
+ var byteLength = bytes.byteLength
73
88
  var byteRemainder = byteLength % 3
74
- var mainLength = byteLength - byteRemainder
89
+ var mainLength = byteLength - byteRemainder
75
90
  var a, b, c, d
76
91
  var chunk
77
92
  for (var i = 0; i < mainLength; i = i + 3) {
78
93
  chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]
79
94
  a = (chunk & 16515072) >> 18
80
- b = (chunk & 258048) >> 12
81
- c = (chunk & 4032) >> 6
95
+ b = (chunk & 258048) >> 12
96
+ c = (chunk & 4032) >> 6
82
97
  d = chunk & 63
83
98
  base64 += encodings[a] + encodings[b] + encodings[c] + encodings[d]
84
99
  }
85
100
  if (byteRemainder == 1) {
86
101
  chunk = bytes[mainLength]
87
102
  a = (chunk & 252) >> 2
88
- b = (chunk & 3) << 4
89
- base64 += encodings[a] + encodings[b] + '=='
103
+ b = (chunk & 3) << 4
104
+ base64 += encodings[a] + encodings[b] + "=="
90
105
  } else if (byteRemainder == 2) {
91
106
  chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1]
92
107
  a = (chunk & 64512) >> 10
93
- b = (chunk & 1008) >> 4
94
- c = (chunk & 15) << 2
95
- 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] + "="
96
111
  }
97
112
  return base64
98
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 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.3",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [