forked from FloatTech/ZeroBot-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
midicreate.go
541 lines (520 loc) · 14.8 KB
/
midicreate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// Package midicreate 简易midi音乐制作
package midicreate
import (
"bytes"
"fmt"
"math"
"math/rand"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/FloatTech/floatbox/binary"
"github.com/FloatTech/floatbox/file"
"github.com/FloatTech/floatbox/web"
ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
"github.com/pkg/errors"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
"gitlab.com/gomidi/midi/v2"
"gitlab.com/gomidi/midi/v2/smf"
)
func init() {
engine := control.Register("midicreate", &ctrl.Options[*zero.Ctx]{
DisableOnDefault: false,
Brief: "midi音乐制作",
Help: "- midi制作 CCGGAAGR FFEEDDCR GGFFEEDR GGFFEEDR CCGGAAGR FFEEDDCR\n" +
"- 个人听音练习\n" +
"- 团队听音练习\n" +
"- *.mid (midi 转 txt)\n" +
"- midi制作*.txt (txt 转 midi)\n" +
"- 设置音色40 (0~127)\n" +
"重要事项: 该插件依赖timidity\n" +
"linux安装脚本可参考https://gitcode.net/anto_july/midi/-/raw/master/timidity.sh\nwindows安装脚本可参考https://gitcode.net/anto_july/midi/-/raw/master/timidity.bat?inline=false, windows需要管理员模式运行",
PrivateDataFolder: "midicreate",
})
cachePath := engine.DataFolder() + "cache/"
_ = os.RemoveAll(cachePath)
err := os.MkdirAll(cachePath, 0755)
if err != nil {
panic(err)
}
engine.OnPrefix("midi制作").SetBlock(true).Limit(ctxext.LimitByUser).
Handle(func(ctx *zero.Ctx) {
uid := ctx.Event.UserID
input := ctx.State["args"].(string)
midiFile := cachePath + strconv.FormatInt(uid, 10) + time.Now().Format("20060102150405") + "_midicreate.mid"
cmidiFile, err := str2music(ctx, input, midiFile)
if err != nil {
if file.IsExist(midiFile) {
ctx.UploadThisGroupFile(file.BOTPATH+"/"+midiFile, filepath.Base(midiFile), "")
return
}
ctx.SendChain(message.Text("ERROR: 无法转换midi文件,", err))
return
}
ctx.SendChain(message.Record("file:///" + file.BOTPATH + "/" + cmidiFile))
})
engine.OnRegex("^(个人|团队)听音练习$", zero.OnlyGroup).SetBlock(true).Limit(ctxext.LimitByUser).
Handle(func(ctx *zero.Ctx) {
uid := ctx.Event.UserID
ctx.SendChain(message.Text("欢迎来到听音练习, 一共有5个问题, 每个问题1分"))
var mode int
var next *zero.FutureEvent
var maxErrorCount int
if ctx.State["regex_matched"].([]string)[1] == "个人" {
mode = 0
next = zero.NewFutureEvent("message", 999, false, zero.RegexRule(`^[A-G][b|#]?\d{0,2}$`),
zero.OnlyGroup, ctx.CheckSession())
maxErrorCount = 3
} else {
mode = 1
next = zero.NewFutureEvent("message", 999, false, zero.RegexRule(`^[A-G][b|#]?\d{0,2}$`),
zero.OnlyGroup, zero.CheckGroup(ctx.Event.GroupID))
maxErrorCount = 10
}
recv, cancel := next.Repeat()
defer cancel()
score := make(map[int64]float64)
round := 1
maxRound := 6
errorCount := 0
target := uint8(55 + rand.Intn(34))
answer := name(target) + strconv.Itoa(int(target/12))
midiFile := cachePath + strconv.FormatInt(uid, 10) + time.Now().Format("20060102150405") + "_midicreate.mid"
cmidiFile, err := str2music(ctx, answer, midiFile)
if err != nil {
ctx.SendChain(message.Text("ERROR: 听音练习结束, 无法转换midi文件, ", err))
return
}
time.Sleep(time.Millisecond * 500)
ctx.SendChain(message.Record("file:///" + file.BOTPATH + "/" + cmidiFile))
ctx.Send(
message.ReplyWithMessage(ctx.Event.MessageID,
message.Text("判断上面的音频, 输入音符, 例如C#6"),
),
)
tick := time.NewTimer(45 * time.Second)
after := time.NewTimer(60 * time.Second)
for {
select {
case <-tick.C:
ctx.SendChain(message.Text("听音练习, 你还有15s作答时间"))
case <-after.C:
var text string
for k, v := range score {
text += fmt.Sprintf("%s: %.1f\n", ctx.CardOrNickName(k), v)
}
ctx.Send(
message.ReplyWithMessage(ctx.Event.MessageID,
message.Text("听音练习超时, 练习结束...答案是: ", answer, "\n所得分数如下:\n", text),
),
)
return
case c := <-recv:
tick.Reset(45 * time.Second)
after.Reset(60 * time.Second)
n := processOne(c.Event.Message.String())
if n != target {
errorCount++
}
if errorCount == maxErrorCount || n == target {
if n == target {
ctx.Send(
message.ReplyWithMessage(c.Event.MessageID,
message.Text("恭喜你回答正确, 答案是: ", answer),
),
)
} else if errorCount == maxErrorCount {
ctx.Send(
message.ReplyWithMessage(c.Event.MessageID,
message.Text("你的回答是: "),
),
)
midiFile = cachePath + strconv.FormatInt(uid, 10) + time.Now().Format("20060102150405") + "_midicreate.mid"
cmidiFile, err = str2music(ctx, c.Event.Message.String(), midiFile)
if err != nil {
ctx.SendChain(message.Text("ERROR: can't convert midi file,", err))
return
}
time.Sleep(time.Millisecond * 500)
ctx.SendChain(message.Record("file:///" + file.BOTPATH + "/" + cmidiFile))
ctx.Send(
message.ReplyWithMessage(c.Event.MessageID,
message.Text("回答错误, 答案是: ", answer, ", 错误次数已达3次, 进入下一关"),
),
)
}
// 统计分数
if mode == 0 {
switch errorCount {
case 0:
score[c.Event.UserID] += 1.0
case 1:
score[c.Event.UserID] += 0.5
case 2:
score[c.Event.UserID] += 0.2
}
} else if mode == 1 {
if errorCount != maxErrorCount {
score[c.Event.UserID] += 1.0
}
}
// 下一关
round++
if round != maxRound {
errorCount = 0
target = uint8(55 + rand.Intn(34))
answer = name(target) + strconv.Itoa(int(target/12))
midiFile = cachePath + strconv.FormatInt(uid, 10) + time.Now().Format("20060102150405") + "_midicreate.mid"
cmidiFile, err = str2music(ctx, answer, midiFile)
if err != nil {
ctx.SendChain(message.Text("ERROR: 听音练习结束, 无法转换midi文件, ", err))
return
}
time.Sleep(time.Millisecond * 500)
ctx.SendChain(message.Record("file:///" + file.BOTPATH + "/" + cmidiFile))
ctx.Send(
message.ReplyWithMessage(c.Event.MessageID,
message.Text("判断上面的音频, 输入音符, 例如C#6"),
),
)
}
} else if n != target {
ctx.Send(
message.ReplyWithMessage(c.Event.MessageID,
message.Text("你的回答是: "),
),
)
time.Sleep(time.Millisecond * 500)
midiFile = cachePath + strconv.FormatInt(uid, 10) + time.Now().Format("20060102150405") + "_midicreate.mid"
cmidiFile, err = str2music(ctx, c.Event.Message.String(), midiFile)
if err != nil {
ctx.SendChain(message.Text("ERROR: can't convert midi file,", err))
return
}
time.Sleep(time.Millisecond * 500)
ctx.SendChain(message.Record("file:///" + file.BOTPATH + "/" + cmidiFile))
ctx.Send(
message.ReplyWithMessage(c.Event.MessageID,
message.Text("回答错误, 错误次数为", errorCount, ", 请继续回答"),
),
)
}
if round == maxRound {
var text string
for k, v := range score {
text += fmt.Sprintf("%s: %.1f\n", ctx.CardOrNickName(k), v)
}
ctx.Send(
message.ReplyWithMessage(c.Event.MessageID,
message.Text("回答完毕, 所得分数如下:\n", text),
),
)
return
}
}
}
})
engine.On("notice/group_upload", func(ctx *zero.Ctx) bool {
return path.Ext(ctx.Event.File.Name) == ".mid"
}).SetBlock(false).Limit(ctxext.LimitByGroup).
Handle(func(ctx *zero.Ctx) {
fileURL := ctx.GetThisGroupFileUrl(ctx.Event.File.BusID, ctx.Event.File.ID)
data, err := web.GetData(fileURL)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
s, err := smf.ReadFrom(bytes.NewReader(data))
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
for i := 0; i < int(s.NumTracks()); i++ {
midStr := mid2txt(data, i)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
fileName := strings.ReplaceAll(cachePath+"/"+ctx.Event.File.Name, ".mid", fmt.Sprintf("-%d.txt", i))
_ = os.WriteFile(fileName, binary.StringToBytes(midStr), 0666)
ctx.UploadThisGroupFile(file.BOTPATH+"/"+fileName, filepath.Base(fileName), "")
}
})
engine.On("notice/group_upload", func(ctx *zero.Ctx) bool {
return path.Ext(ctx.Event.File.Name) == ".txt" && strings.Contains(ctx.Event.File.Name, "midi制作")
}).SetBlock(false).Limit(ctxext.LimitByGroup).
Handle(func(ctx *zero.Ctx) {
fileURL := ctx.GetThisGroupFileUrl(ctx.Event.File.BusID, ctx.Event.File.ID)
data, err := web.GetData(fileURL)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
uid := ctx.Event.UserID
midiFile := cachePath + strconv.FormatInt(uid, 10) + time.Now().Format("20060102150405") + "_midicreate.mid"
cmidiFile, err := str2music(ctx, binary.BytesToString(data), midiFile)
if err != nil {
ctx.SendChain(message.Text("ERROR: 无法转换midi文件,", err))
return
}
ctx.SendChain(message.Record("file:///" + file.BOTPATH + "/" + cmidiFile))
})
engine.OnPrefix("设置音色").SetBlock(true).
Handle(func(ctx *zero.Ctx) {
param := ctx.State["args"].(string)
timbre, err := strconv.Atoi(param)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
}
err = setTimbreMode(ctx, int64(timbre))
if err != nil {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(err))
return
}
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("成功"))
})
}
var (
noteMap = map[string]uint8{
"C": 60,
"Db": 61,
"D": 62,
"Eb": 63,
"E": 64,
"F": 65,
"Gb": 66,
"G": 67,
"Ab": 68,
"A": 69,
"Bb": 70,
"B": 71,
}
)
func str2music(ctx *zero.Ctx, input, midiFile string) (cmidiFile string, err error) {
err = mkMidi(ctx, midiFile, input)
if err != nil {
return
}
cmidiFile = strings.ReplaceAll(midiFile, ".mid", ".wav")
cmd := exec.Command("timidity", file.BOTPATH+"/"+midiFile, "-Ow", "-o", file.BOTPATH+"/"+cmidiFile)
err = cmd.Run()
return
}
func mkMidi(ctx *zero.Ctx, filePath, input string) error {
if file.IsExist(filePath) {
return nil
}
var (
clock smf.MetricTicks
tr smf.Track
)
tr.Add(0, smf.MetaMeter(4, 4))
tr.Add(0, smf.MetaTempo(72))
tr.Add(0, smf.MetaInstrument("Violin"))
timbre := getTimbreMode(ctx)
tr.Add(0, midi.ProgramChange(0, uint8(timbre)))
k := strings.ReplaceAll(input, " ", "")
var (
base uint8
level uint8
delay uint32
sleepFlag bool
lengthBytes = make([]byte, 0)
)
for i := 0; i < len(k); {
base = 0
level = 0
sleepFlag = false
lengthBytes = lengthBytes[:0]
for {
switch {
case k[i] == 'R':
sleepFlag = true
i++
case k[i] >= 'A' && k[i] <= 'G':
base = noteMap[k[i:i+1]] % 12
i++
case k[i] == 'b':
base--
i++
case k[i] == '#':
base++
i++
case k[i] >= '0' && k[i] <= '9':
level = level*10 + k[i] - '0'
i++
case k[i] == '<':
i++
for i < len(k) && (k[i] == '-' || (k[i] >= '0' && k[i] <= '9')) {
lengthBytes = append(lengthBytes, k[i])
i++
}
default:
return errors.Errorf("无法解析第%d个位置的%c字符", i, k[i])
}
if i >= len(k) || (k[i] >= 'A' && k[i] <= 'G') || k[i] == 'R' {
break
}
}
length, _ := strconv.Atoi(string(lengthBytes))
if sleepFlag {
if length >= 0 {
delay = clock.Ticks4th() * (1 << length)
} else {
delay = clock.Ticks4th() / (1 << -length)
}
continue
}
if level == 0 {
level = 5
}
tr.Add(delay, midi.NoteOn(0, o(base, level), 120))
if length >= 0 {
tr.Add(clock.Ticks4th()*(1<<length), midi.NoteOff(0, o(base, level)))
} else {
tr.Add(clock.Ticks4th()/(1<<-length), midi.NoteOff(0, o(base, level)))
}
delay = 0
}
tr.Close(0)
s := smf.New()
s.TimeFormat = clock
err := s.Add(tr)
if err != nil {
return err
}
f, err := os.Create(filePath)
if err != nil {
return err
}
_, err = s.WriteTo(f)
f.Close()
return err
}
func o(base uint8, oct uint8) uint8 {
if oct > 10 {
oct = 10
}
if oct == 0 {
return base
}
res := base + 12*oct
if res > 127 {
res -= 12
}
return res
}
func name(n uint8) string {
for k, v := range noteMap {
if v%12 == n%12 {
return k
}
}
return ""
}
func processOne(note string) uint8 {
k := strings.ReplaceAll(note, " ", "")
var (
base uint8
level uint8
)
for i := 0; i < len(k); i++ {
switch {
case k[i] >= 'A' && k[i] <= 'G':
base = noteMap[k[i:i+1]] % 12
case k[i] == 'b':
base--
case k[i] == '#':
base++
case k[i] >= '0' && k[i] <= '9':
level = level*10 + k[i] - '0'
}
}
if level == 0 {
level = 5
}
return o(base, level)
}
func mid2txt(midBytes []byte, trackNo int) (midStr string) {
var (
absTicksStart float64
absTicksEnd float64
startNote byte
endNote byte
defaultMetric = 960.0
)
_ = smf.ReadTracksFrom(bytes.NewReader(midBytes), trackNo).
Do(
func(te smf.TrackEvent) {
if !te.Message.IsMeta() {
b := te.Message.Bytes()
if te.Message.Is(midi.NoteOnMsg) && b[2] > 0 {
absTicksStart = float64(te.AbsTicks)
startNote = b[1]
}
if te.Message.Is(midi.NoteOffMsg) || (te.Message.Is(midi.NoteOnMsg) && b[2] == 0x00) {
absTicksEnd = float64(te.AbsTicks)
endNote = b[1]
if startNote == endNote {
sign := name(b[1])
level := b[1] / 12
length := (absTicksEnd - absTicksStart) / defaultMetric
midStr += sign
if level != 5 {
midStr += strconv.Itoa(int(level))
}
pow := int(math.Round(math.Log2(length)))
if pow >= -4 && pow != 0 {
midStr += "<" + strconv.Itoa(pow)
}
startNote = 0
endNote = 0
}
}
if (te.Message.Is(midi.NoteOnMsg) && b[2] > 0) && absTicksStart > absTicksEnd {
length := (absTicksStart - absTicksEnd) / defaultMetric
pow := int(math.Round(math.Log2(length)))
if pow == 0 {
midStr += "R"
} else if pow >= -4 {
midStr += "R<" + strconv.Itoa(pow)
}
}
}
},
)
return
}
func setTimbreMode(ctx *zero.Ctx, timbre int64) error {
gid := ctx.Event.GroupID
if gid == 0 {
gid = -ctx.Event.UserID
}
if timbre < 0 || timbre > 127 {
return errors.New("音色应该在0~127之间")
}
m, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
if !ok {
return errors.New("no such plugin")
}
return m.SetData(gid, timbre)
}
func getTimbreMode(ctx *zero.Ctx) (index int64) {
gid := ctx.Event.GroupID
if gid == 0 {
gid = -ctx.Event.UserID
}
m, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
if ok {
index := m.GetData(gid)
return index
}
return 40
}