-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
65 lines (47 loc) · 1.59 KB
/
train.py
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
import os
from midiutil import MIDIFile
import music21
import json
import collections
# 分析midi文件中的和弦
def analyze_chords(midi_files):
chords = []
transition_pairs = []
for i in range(len(chords)-1):
from_chord = chords[i]
to_chord = chords[i+1]
transition_pairs.append( (from_chord, to_chord) )
transitions = {}
# 分析过程
for t in transition_pairs:
from_chord, to_chord = t
if from_chord not in transitions:
transitions[from_chord] = {}
if to_chord not in transitions[from_chord]:
transitions[from_chord][to_chord] = 0
transitions[from_chord][to_chord] += 1
return chords, transitions
# 根据分析结果生成配置
def generate_config(chords, transitions):
config = {
"chord_probs": {},
"chord_start_probs": {}
}
# 填充chord_probs
for k, v in transitions.items():
config["chord_probs"][k] = v
# 填充chord_start_probs
counts = collections.Counter(chords)
for chord, count in counts.items():
config["chord_start_probs"][chord] = count / len(chords)
return config
if __name__ == "__main__":
# 待分析的midi文件
midi_files = [f for f in os.listdir('resources') if f.endswith('.mid')]
# 分析和弦
chords, transitions = analyze_chords(midi_files)
# 生成配置
config = generate_config(chords, transitions)
# 保存配置文件
with open('config-output.json', 'w') as f:
json.dump(config, f, indent=4)