-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmu.py
More file actions
187 lines (156 loc) · 7.26 KB
/
Copy pathmu.py
File metadata and controls
187 lines (156 loc) · 7.26 KB
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
"""
伤感回忆钢琴曲 - 使用mingus生成(扩展版)
需要安装: pip install mingus
"""
from mingus.containers import Note, NoteContainer, Track, Composition
from mingus.midi import midi_file_out
import mingus.core.chords as chords
# 创建作曲
composition = Composition()
composition.set_author("AI Composer")
composition.set_title("Memories in Rain (雨中回忆) - Extended")
# 创建左右手音轨
right_hand = Track()
left_hand = Track()
# 设置速度 (BPM)
bpm = 65
# 定义和弦进行(左手伴奏)- 扩展版
chord_progression = [
# 第一部分:引子与主题
('Am', 4), ('F', 4), ('C', 4), ('G', 4),
('Am', 4), ('Dm', 4), ('E7', 4), ('Am', 4),
('F', 4), ('G', 4), ('Em', 4), ('Am', 4),
('Dm', 4), ('G', 4), ('C', 4), ('Am', 4),
# 第二部分:发展与回忆
('Am', 4), ('F', 4), ('C', 4), ('G', 4),
('Am', 4), ('Dm', 4), ('E7', 4), ('Am', 4),
('F', 4), ('G', 4), ('Em', 4), ('Am', 4),
('Dm', 4), ('G', 4), ('C', 4), ('E7', 4),
# 第三部分:变奏(新增)
('Am', 4), ('C', 4), ('F', 4), ('Dm', 4),
('Bdim', 4), ('E7', 4), ('Am', 4), ('Am', 4),
('F', 4), ('G', 4), ('C', 4), ('Am', 4),
('Dm', 4), ('E7', 4), ('Am', 4), ('Am', 4),
# 第四部分:情感高潮(新增)
('F', 4), ('G', 4), ('Am', 4), ('Am', 4),
('Dm', 4), ('E7', 4), ('Am', 4), ('C', 4),
('F', 4), ('G', 4), ('Em', 4), ('Am', 4),
('Dm', 4), ('G', 4), ('C', 4), ('Am', 4),
]
# 右手旋律音符
melody_phrases = [
# === 第一部分:引子与主题 ===
# 引子 - 孤独的开始
[('A-4', 4), ('C-5', 4), ('E-5', 4), ('A-5', 8), ('G-5', 8), ('E-5', 4)],
[('F-5', 4), ('E-5', 8), ('D-5', 8), ('C-5', 4), ('A-4', 4)],
[('G-4', 4), ('B-4', 4), ('D-5', 4), ('E-5', 2)],
[('E-5', 8), ('D-5', 8), ('C-5', 8), ('B-4', 8), ('A-4', 4), ('C-5', 4)],
# 主题 - 回忆涌现
[('A-5', 8), ('G-5', 8), ('E-5', 4), ('C-5', 8), ('D-5', 8), ('E-5', 4)],
[('F-5', 4), ('A-5', 8), ('G-5', 8), ('F-5', 4), ('D-5', 4)],
[('E-5', 8), ('F-5', 8), ('E-5', 8), ('D-5', 8), ('C-5', 4), ('B-4', 4)],
[('A-4', 4), ('C-5', 8), ('E-5', 8), ('A-5', 2)],
# 发展 - 情绪高涨
[('C-6', 8), ('B-5', 8), ('A-5', 4), ('G-5', 4), ('F-5', 4)],
[('G-5', 4), ('E-5', 8), ('F-5', 8), ('D-5', 4), ('E-5', 4)],
[('E-5', 8), ('D-5', 8), ('C-5', 4), ('B-4', 4), ('A-4', 4)],
[('A-4', 4), ('C-5', 4), ('E-5', 4), ('A-5', 4)],
# 回忆片段 - 温柔回想
[('D-5', 4), ('F-5', 8), ('E-5', 8), ('D-5', 4), ('C-5', 4)],
[('B-4', 4), ('D-5', 4), ('G-5', 4), ('F-5', 8), ('E-5', 8)],
[('C-5', 4), ('E-5', 4), ('G-5', 8), ('F-5', 8), ('E-5', 4)],
[('E-5', 8), ('D-5', 8), ('C-5', 4), ('B-4', 4), ('A-4', 4)],
# === 第二部分:深入回忆(优化旋律流畅度)===
[('A-4', 4), ('C-5', 4), ('E-5', 4), ('A-5', 8), ('G-5', 8)],
[('F-5', 4), ('E-5', 4), ('D-5', 4), ('C-5', 4)],
[('G-4', 4), ('B-4', 4), ('D-5', 4), ('E-5', 4)],
[('E-5', 4), ('D-5', 4), ('C-5', 4), ('B-4', 4)],
[('A-4', 4), ('C-5', 4), ('E-5', 4), ('G-5', 8), ('F-5', 8)],
[('F-5', 4), ('E-5', 4), ('D-5', 4), ('F-5', 4)],
[('E-5', 4), ('D-5', 4), ('C-5', 4), ('B-4', 4)],
[('C-5', 4), ('B-4', 4), ('A-4', 2)],
# === 第三部分:变奏(新增)===
# 更加细腻的情感表达
[('A-5', 8), ('B-5', 8), ('C-6', 4), ('B-5', 8), ('A-5', 8), ('G-5', 4)],
[('E-5', 4), ('G-5', 8), ('F-5', 8), ('E-5', 4), ('D-5', 4)],
[('C-5', 8), ('D-5', 8), ('E-5', 4), ('D-5', 8), ('C-5', 8), ('B-4', 4)],
[('A-4', 4), ('C-5', 4), ('E-5', 4), ('A-5', 4)],
[('F-5', 4), ('E-5', 8), ('D-5', 8), ('C-5', 4), ('A-4', 4)],
[('B-4', 4), ('D-5', 4), ('G-5', 8), ('F-5', 8), ('E-5', 4)],
[('A-5', 8), ('G-5', 8), ('E-5', 4), ('C-5', 4), ('A-4', 4)],
[('E-5', 8), ('D-5', 8), ('C-5', 4), ('B-4', 4), ('A-4', 4)],
# === 第四部分:情感高潮(新增)===
[('C-6', 4), ('B-5', 8), ('A-5', 8), ('G-5', 4), ('F-5', 4)],
[('E-5', 4), ('F-5', 4), ('G-5', 8), ('A-5', 8), ('E-5', 4)],
[('D-5', 4), ('E-5', 4), ('F-5', 4), ('E-5', 8), ('D-5', 8)],
[('C-5', 4), ('E-5', 8), ('D-5', 8), ('C-5', 4), ('A-4', 4)],
# 渐弱过渡
[('F-5', 8), ('E-5', 8), ('D-5', 4), ('C-5', 4), ('B-4', 4)],
[('B-4', 4), ('C-5', 4), ('D-5', 4), ('E-5', 4)],
[('E-5', 8), ('D-5', 8), ('C-5', 4), ('B-4', 4), ('A-4', 4)],
[('C-5', 8), ('B-4', 8), ('A-4', 4), ('E-4', 4), ('A-4', 4)],
]
def add_left_hand_pattern(track, chord_name, duration_per_note, variation=0):
"""添加左手分解和弦伴奏,支持不同变化"""
try:
chord_notes = chords.from_shorthand(chord_name)
if len(chord_notes) >= 3:
if variation == 0:
# 标准分解和弦
bass = Note(chord_notes[0], 3)
track.add_notes(bass, duration_per_note * 2)
nc1 = NoteContainer([Note(chord_notes[1], 3), Note(chord_notes[2], 4)])
track.add_notes(nc1, duration_per_note)
nc2 = NoteContainer([Note(chord_notes[0], 4), Note(chord_notes[2], 4)])
track.add_notes(nc2, duration_per_note)
elif variation == 1:
# 和弦型伴奏(情感高潮处)
bass = Note(chord_notes[0], 3)
track.add_notes(bass, duration_per_note)
nc = NoteContainer([Note(chord_notes[0], 4), Note(chord_notes[1], 4), Note(chord_notes[2], 4)])
track.add_notes(nc, duration_per_note * 3)
except:
track.add_notes(None, duration_per_note * 4)
# 生成音乐
print("正在创作《雨中回忆 - 扩展版》...")
print("结构:引子 → 主题 → 发展 → 回忆 → 变奏 → 高潮 → 尾声")
# 遍历每个小节
for i, (chord_name, duration) in enumerate(chord_progression):
# 在高潮部分使用不同的伴奏型
variation = 1 if i >= 32 else 0
# 左手:和弦伴奏
add_left_hand_pattern(left_hand, chord_name, 8, variation)
# 右手:旋律
if i < len(melody_phrases):
for note_name, note_duration in melody_phrases[i]:
right_hand.add_notes(Note(note_name), note_duration)
else:
right_hand.add_notes(None, 1)
# 尾声 - 渐弱消失
print("添加尾声...")
ending_melody = [
('E-5', 8), ('C-5', 8), ('A-4', 4), ('C-5', 4), ('E-5', 4),
('A-5', 8), ('G-5', 8), ('E-5', 4), ('C-5', 4), ('A-4', 4),
('E-5', 4), ('D-5', 4), ('C-5', 4), ('B-4', 4),
('A-4', 2), ('C-5', 4), ('E-4', 2), ('A-4', 1)
]
for note_name, note_duration in ending_melody:
right_hand.add_notes(Note(note_name), note_duration)
# 左手:结束和弦
left_hand.add_notes(Note('A-3'), 4)
left_hand.add_notes(Note('E-3'), 4)
left_hand.add_notes(Note('C-3'), 4)
left_hand.add_notes(NoteContainer([Note('A-2'), Note('E-3'), Note('A-3')]), 1)
# 添加音轨到作曲
composition.add_track(right_hand)
composition.add_track(left_hand)
# 导出MIDI文件
output_file = "memories_in_rain_extended.mid"
midi_file_out.write_Composition(output_file, composition, bpm=bpm)
print(f"\n✓ MIDI文件已生成: {output_file}")
print(f" 速度: {bpm} BPM")
print(f" 调性: A小调")
print(f" 时长: 约2分钟")
print(f" 段落: 7个部分,更丰富的情感层次")
print(f"\n请使用MIDI播放器播放此文件")
print("推荐播放器: Windows Media Player, VLC, MuseScore, GarageBand等")