@dgck81lnn/koishi-plugin-music 0.2.0 → 0.2.2

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
@@ -9,32 +9,50 @@ async function synth(notes, { noise } = {}) {
9
9
 
10
10
  cmp.connect(ctx.destination)
11
11
 
12
- for (const note of notes) {
13
- const osc = ctx.createOscillator()
14
- const gain = ctx.createGain()
15
-
16
- osc.setPeriodicWave(wav)
17
- osc.frequency.value = note.frequency
12
+ let noiseBuf
13
+ const getNoise = () => {
14
+ if (!noiseBuf) {
15
+ noiseBuf = ctx.createBuffer(1, Math.min(seconds, 10) * sampleRate, sampleRate)
16
+ for (let data = noiseBuf.getChannelData(0), i = 0; i < data.length; i++)
17
+ data[i] = Math.random() * 2 - 1
18
+ }
19
+ return noiseBuf
20
+ }
18
21
 
19
- osc.connect(gain)
20
- gain.connect(cmp)
22
+ for (const note of notes) {
23
+ if (!(note.frequency && note.end > note.start && note.gain > 0)) continue
21
24
 
22
- osc.addEventListener("ended", () => osc.disconnect())
23
- osc.start(note.start)
24
- osc.stop(note.end)
25
+ const gain = ctx.createGain()
25
26
  gain.gain.setValueAtTime(note.gain, note.start)
26
27
  gain.gain.linearRampToValueAtTime(0, note.end)
28
+ gain.connect(cmp)
29
+
30
+ let src
31
+ if (note.frequency < 0) {
32
+ src = ctx.createBufferSource()
33
+ src.buffer = getNoise()
34
+ src.loop = true
35
+ src.playbackRate.value = -note.frequency / sampleRate
36
+ src.connect(gain)
37
+ } else {
38
+ src = ctx.createOscillator()
39
+ src.setPeriodicWave(wav)
40
+ src.frequency.value = note.frequency
41
+ src.connect(gain)
42
+ }
43
+
44
+ src.start(note.start)
45
+ src.stop(note.end)
46
+ src.addEventListener("ended", () => gain.disconnect())
27
47
  }
28
48
 
29
49
  if (noise) {
30
50
  // 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
51
+ getNoise()
34
52
  const noiseGain = ctx.createGain()
35
53
  noiseGain.gain.value = 0.005
36
54
  const noiseSrc = ctx.createBufferSource()
37
- noiseSrc.buffer = noiseBuf
55
+ noiseSrc.buffer = getNoise()
38
56
  noiseSrc.loop = true
39
57
  noiseSrc.connect(noiseGain)
40
58
  noiseGain.connect(ctx.destination)
package/lib/index.js CHANGED
@@ -45,10 +45,14 @@ const gutterFunc = (f) => {
45
45
  this.noteHz(baseFrequency * ratio, beats);
46
46
  },
47
47
  noteHz(frequency, beats) {
48
- notes.push({ start: time, end: (time += (beats / +bpm) * 60), frequency, gain });
48
+ if (![frequency, beats].every(Number.isFinite))
49
+ return;
50
+ notes.push({ start: time, end: (time += (60 / +bpm) * beats), frequency, gain });
49
51
  },
50
52
  rest(beats) {
51
- time += (beats / +bpm) * 60;
53
+ if (!Number.isFinite(beats))
54
+ return;
55
+ time += (60 / +bpm) * beats;
52
56
  },
53
57
  get bpm() {
54
58
  return bpm;
@@ -128,14 +132,18 @@ function apply(ctx, config) {
128
132
  return koishi_1.h.text(data);
129
133
  }
130
134
  const page = await ctx.puppeteer.page();
131
- const opt = {
132
- noise: session.resolve(config.noise),
133
- };
134
- ctx.logger.debug("synth options: %o", opt);
135
- const base64 = (await page.evaluate(
136
- // prettier-ignore
137
- `${await synthCode}; synth(${data}, ${JSON.stringify(opt)}).then(encodeWav).then(arrayBufferToBase64)`));
138
- page.close().catch(() => { });
139
- return koishi_1.h.audio("data:audio/wav;base64," + base64);
135
+ try {
136
+ const opt = {
137
+ noise: session.resolve(config.noise),
138
+ };
139
+ ctx.logger.debug("synth options: %o", opt);
140
+ const base64 = (await page.evaluate(
141
+ // prettier-ignore
142
+ `${await synthCode}; synth(${data}, ${JSON.stringify(opt)}).then(encodeWav).then(arrayBufferToBase64)`));
143
+ return koishi_1.h.audio("data:audio/wav;base64," + base64);
144
+ }
145
+ finally {
146
+ page.close();
147
+ }
140
148
  });
141
149
  }
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.2.0",
4
+ "version": "0.2.2",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [