@dgck81lnn/koishi-plugin-music 0.1.0 → 0.1.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/index.html +8 -0
- package/browser/synth.js +98 -0
- package/package.json +2 -2
package/browser/synth.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
const sampleRate = 44100
|
|
2
|
+
|
|
3
|
+
async function synth(notes) {
|
|
4
|
+
const seconds = Math.max(...notes.map(i => i.end))
|
|
5
|
+
|
|
6
|
+
const ctx = new OfflineAudioContext(1, seconds * sampleRate, sampleRate)
|
|
7
|
+
const cmp = ctx.createDynamicsCompressor()
|
|
8
|
+
const wav = ctx.createPeriodicWave([0, 0, 0, 0], [0, 1, 1, 1])
|
|
9
|
+
|
|
10
|
+
cmp.connect(ctx.destination)
|
|
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
|
|
18
|
+
|
|
19
|
+
osc.connect(gain)
|
|
20
|
+
gain.connect(cmp)
|
|
21
|
+
|
|
22
|
+
osc.addEventListener("ended", () => osc.disconnect())
|
|
23
|
+
osc.start(note.start)
|
|
24
|
+
osc.stop(note.end)
|
|
25
|
+
gain.gain.setValueAtTime(note.gain, note.start)
|
|
26
|
+
gain.gain.linearRampToValueAtTime(0, note.end)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const buf = await ctx.startRendering()
|
|
30
|
+
return buf.getChannelData(0)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function encodeWav(samples) {
|
|
34
|
+
const buffer = new ArrayBuffer(samples.length * 2 + 44)
|
|
35
|
+
const view = new DataView(buffer)
|
|
36
|
+
let p = 0
|
|
37
|
+
function writeBytes(bytes) {
|
|
38
|
+
for (let i = 0; i < bytes.length; i++) view.setUint8(p++, bytes.charCodeAt(i))
|
|
39
|
+
}
|
|
40
|
+
function writeInt32(n) {
|
|
41
|
+
view.setInt32(p, n, true)
|
|
42
|
+
p += 4
|
|
43
|
+
}
|
|
44
|
+
function writeInt16(n) {
|
|
45
|
+
view.setInt16(p, n, true)
|
|
46
|
+
p += 2
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
writeBytes("RIFF")
|
|
50
|
+
writeInt32(buffer.byteLength - 8) // size of whole container
|
|
51
|
+
writeBytes("WAVEfmt ")
|
|
52
|
+
writeInt32(16) // size of fmt chunk
|
|
53
|
+
writeInt16(1) // encoding format = pcm
|
|
54
|
+
writeInt16(1) // channel count
|
|
55
|
+
writeInt32(sampleRate)
|
|
56
|
+
writeInt32(sampleRate * 2) // bytes per second
|
|
57
|
+
writeInt16(2) // bytes per sample
|
|
58
|
+
writeInt16(16) // sample depth
|
|
59
|
+
writeBytes("data")
|
|
60
|
+
writeInt32(samples.length * 2) // size of data chunk
|
|
61
|
+
for (let i = 0; i < samples.length; i++)
|
|
62
|
+
writeInt16(Math.min(Math.max(samples[i] * 0x7fff, -32768), 32767))
|
|
63
|
+
|
|
64
|
+
return buffer
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// https://gist.github.com/jonleighton/958841
|
|
68
|
+
function arrayBufferToBase64(arrayBuffer) {
|
|
69
|
+
var base64 = ''
|
|
70
|
+
var encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
|
71
|
+
var bytes = new Uint8Array(arrayBuffer)
|
|
72
|
+
var byteLength = bytes.byteLength
|
|
73
|
+
var byteRemainder = byteLength % 3
|
|
74
|
+
var mainLength = byteLength - byteRemainder
|
|
75
|
+
var a, b, c, d
|
|
76
|
+
var chunk
|
|
77
|
+
for (var i = 0; i < mainLength; i = i + 3) {
|
|
78
|
+
chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]
|
|
79
|
+
a = (chunk & 16515072) >> 18
|
|
80
|
+
b = (chunk & 258048) >> 12
|
|
81
|
+
c = (chunk & 4032) >> 6
|
|
82
|
+
d = chunk & 63
|
|
83
|
+
base64 += encodings[a] + encodings[b] + encodings[c] + encodings[d]
|
|
84
|
+
}
|
|
85
|
+
if (byteRemainder == 1) {
|
|
86
|
+
chunk = bytes[mainLength]
|
|
87
|
+
a = (chunk & 252) >> 2
|
|
88
|
+
b = (chunk & 3) << 4
|
|
89
|
+
base64 += encodings[a] + encodings[b] + '=='
|
|
90
|
+
} else if (byteRemainder == 2) {
|
|
91
|
+
chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1]
|
|
92
|
+
a = (chunk & 64512) >> 10
|
|
93
|
+
b = (chunk & 1008) >> 4
|
|
94
|
+
c = (chunk & 15) << 2
|
|
95
|
+
base64 += encodings[a] + encodings[b] + encodings[c] + '='
|
|
96
|
+
}
|
|
97
|
+
return base64
|
|
98
|
+
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dgck81lnn/koishi-plugin-music",
|
|
3
3
|
"description": "Synthesize melodies in Koishi",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.2",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
7
7
|
"files": [
|
|
8
8
|
"lib",
|
|
9
|
-
"
|
|
9
|
+
"browser"
|
|
10
10
|
],
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"peerDependencies": {
|