@dgck81lnn/koishi-plugin-music 0.1.0 → 0.1.1

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.
@@ -0,0 +1,8 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ </head>
5
+ <body>
6
+ <script src="synth.js"></script>
7
+ </body>
8
+ </html>
@@ -0,0 +1,97 @@
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++) writeInt16(samples[i] * 0x7fff)
62
+
63
+ return buffer
64
+ }
65
+
66
+ // https://gist.github.com/jonleighton/958841
67
+ window.arrayBufferToBase64 = function (arrayBuffer) {
68
+ var base64 = ''
69
+ var encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
70
+ var bytes = new Uint8Array(arrayBuffer)
71
+ var byteLength = bytes.byteLength
72
+ var byteRemainder = byteLength % 3
73
+ var mainLength = byteLength - byteRemainder
74
+ var a, b, c, d
75
+ var chunk
76
+ for (var i = 0; i < mainLength; i = i + 3) {
77
+ chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]
78
+ a = (chunk & 16515072) >> 18
79
+ b = (chunk & 258048) >> 12
80
+ c = (chunk & 4032) >> 6
81
+ d = chunk & 63
82
+ base64 += encodings[a] + encodings[b] + encodings[c] + encodings[d]
83
+ }
84
+ if (byteRemainder == 1) {
85
+ chunk = bytes[mainLength]
86
+ a = (chunk & 252) >> 2
87
+ b = (chunk & 3) << 4
88
+ base64 += encodings[a] + encodings[b] + '=='
89
+ } else if (byteRemainder == 2) {
90
+ chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1]
91
+ a = (chunk & 64512) >> 10
92
+ b = (chunk & 1008) >> 4
93
+ c = (chunk & 15) << 2
94
+ base64 += encodings[a] + encodings[b] + encodings[c] + '='
95
+ }
96
+ return base64
97
+ }
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.0",
4
+ "version": "0.1.1",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
8
8
  "lib",
9
- "dist"
9
+ "browser"
10
10
  ],
11
11
  "license": "MIT",
12
12
  "peerDependencies": {