Skip to content

Instantly share code, notes, and snippets.

@siddontang
Created January 31, 2024 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siddontang/b0b6778db223a19fa69f5ecbb0d06c9a to your computer and use it in GitHub Desktop.
Save siddontang/b0b6778db223a19fa69f5ecbb0d06c9a to your computer and use it in GitHub Desktop.
Use ChatGPT to build a song
from music21 import stream, note, meter, tempo, expressions, instrument
# 创建一个音乐流
melody = stream.Stream()
# 设置拍号为4/4
melody.append(meter.TimeSignature('4/4'))
# # 设置曲速
# melody.append(tempo.MetronomeMark(number=120))
# # 为每行歌词定义的音符序列
# melody_lines = [
# ['C5', 'D5', 'E5', 'F5', 'G5', 'A5', 'G5', 'F5', 'E5', 'D5', 'C5', 'B4', 'C5', 'D5', 'E5', 'F5', 'E5', 'D5', 'C5'],
# ['E5', 'F5', 'G5', 'A5', 'G5', 'F5', 'E5', 'D5', 'C5', 'D5', 'E5', 'F5', 'G5', 'A5', 'G5', 'F5', 'E5', 'D5', 'C5'],
# ['G5', 'A5', 'B5', 'C6', 'D6', 'C6', 'B5', 'A5', 'G5', 'F5', 'E5', 'D5', 'E5', 'F5', 'G5', 'A5', 'G5', 'F5', 'E5', 'D5'],
# ['C5', 'D5', 'E5', 'F5', 'G5', 'A5', 'G5', 'F5', 'E5', 'D5', 'C5', 'D5', 'E5', 'F5', 'G5', 'A5', 'G5', 'F5', 'E5', 'D5']
# ]
# # 将音符序列添加到音乐流中
# for line in melody_lines:
# for n in line:
# melody.append(note.Note(n, quarterLength=1))
# # 调整后的音符序列
# melody_lines = [
# ['C5', 'D5', 'E5', 'F5', 'G5', 'A5', 'G5', 'F5', 'E5', 'D5', 'C5', 'B4', 'C5', 'D5', 'E5', 'F5', 'E5', 'D5', 'C5', 'REST'],
# ['E5', 'F5', 'G5', 'A5', 'G5', 'F5', 'E5', 'D5', 'C5', 'D5', 'E5', 'F5', 'G5', 'A5', 'G5', 'F5', 'E5', 'D5', 'C5', 'REST'],
# ['G5', 'A5', 'B5', 'C6', 'D6', 'C6', 'B5', 'A5', 'G5', 'F5', 'E5', 'D5', 'E5', 'F5', 'G5', 'A5', 'G5', 'F5', 'E5', 'D5', 'REST'],
# ['C5', 'D5', 'E5', 'F5', 'G5', 'A5', 'G5', 'F5', 'E5', 'D5', 'C5', 'D5', 'E5', 'F5', 'G5', 'A5', 'G5', 'F5', 'E5', 'D5']
# ]
# # 将音符序列添加到音乐流中,并添加转音和换气处理
# for line in melody_lines:
# for n in line:
# if n == 'REST':
# # 添加换气处理(休止符)
# melody.append(note.Rest(quarterLength=2))
# else:
# melody_note = note.Note(n, quarterLength=1)
# # 添加颤音装饰
# trill = expressions.Trill()
# melody_note.expressions.append(trill)
# melody.append(melody_note)
# # 导出为MIDI文件
# midi_path = './demo.mid'
# melody.write('midi', fp=midi_path)
# 春风拂过绿意浓,花开满园梦开始盎然。
# 柳絮轻飞舞蝶忙,日暖云轻天更蓝。
# 江水悠悠绕古城,山色远淡烟波澜。
# 笑看桃红柳绿时,春意盎然心自宽。
# 设置曲速为轻快的速度
# melody.append(tempo.MetronomeMark(number=100))
# # 构建一个简单明快的旋律,类似于《茉莉花》的风格
# # 针对 "梅花香满旧长巷,岁月静好云水间。春风细数枝头花,悄然间又是新年。"
# notes = ['C5', 'D5', 'E5', 'G5', 'E5', 'D5', 'C5', # 梅花香满旧长巷
# 'D5', 'E5', 'F5', 'G5', 'A5', 'G5', 'F5', # 岁月静好云水间
# 'E5', 'D5', 'C5', 'D5', 'E5', 'F5', 'E5', # 春风细数枝头花
# 'D5', 'C5', 'D5', 'E5', 'C5', 'D5', 'C5'] # 悄然间又是新年
# # 将音符添加到音乐流中
# for n in notes:
# melody.append(note.Note(n, quarterLength=1))
# # 导出为MIDI文件
# melody.write('midi', 'Melody_Similar_To_Jasmine_Flower.mid')
melody.append(tempo.MetronomeMark(number=100))
# 构建一个简单明快的旋律,类似于《茉莉花》的风格
# 针对 "梅花香满旧长巷,岁月静好云水间。春风细数枝头花,悄然间又是新年。"
melody_lines = [
['C5', 'D5', 'E5', 'G5', 'E5', 'D5', 'C5', 'REST'], # 梅花香满旧长巷
['D5', 'E5', 'F5', 'G5', 'A5', 'G5', 'F5', 'REST'], # 岁月静好云水间
['E5', 'D5', 'C5', 'D5', 'E5', 'F5', 'E5', 'REST'], # 春风细数枝头花
['D5', 'C5', 'D5', 'E5', 'C5', 'D5', 'C5', 'REST'] # 悄然间又是新年
]
# 将音符序列添加到音乐流中,并添加转音和换气处理
for line in melody_lines:
for n in line:
if n == 'REST':
# 添加换气处理(休止符)
melody.append(note.Rest(quarterLength=2))
else:
melody_note = note.Note(n, quarterLength=1)
# 添加颤音装饰
trill = expressions.Trill()
melody_note.expressions.append(trill)
melody.append(melody_note)
# # 导出为MIDI文件
# midi_path = './Simple_Melody_Like_Jasmine_Flower_Enhanced.mid'
# melody.write('midi', fp=midi_path)
# 创建一个音乐流用于伴奏
accompaniment = stream.Stream()
# 设置拍号为4/4
accompaniment.append(meter.TimeSignature('4/4'))
# 设置曲速为轻快的速度
accompaniment.append(tempo.MetronomeMark(number=100))
# 使用吉他来近似代表琵琶的声音
accompaniment.append(instrument.Guitar())
# 简单的伴奏音符序列
accompaniment_notes = ['G3', 'C4', 'E4', 'G4', 'C4', 'E4', 'G3', 'REST',
'A3', 'D4', 'F4', 'A4', 'D4', 'F4', 'A3', 'REST',
'B3', 'E4', 'G4', 'B4', 'E4', 'G4', 'B3', 'REST',
'C4', 'E4', 'G4', 'C5', 'E4', 'G4', 'C4', 'REST']
# 将音符添加到伴奏音乐流中
for n in accompaniment_notes:
if n == 'REST':
accompaniment.append(note.Rest(quarterLength=2))
else:
accompaniment.append(note.Note(n, quarterLength=1))
# 导出为MIDI文件
accompaniment.write('midi', 'Approximate_Pipa_Accompaniment.mid')
# 创建一个音乐流用于钢琴伴奏
piano_accompaniment = stream.Stream()
# 设置拍号为4/4
piano_accompaniment.append(meter.TimeSignature('4/4'))
# 设置曲速为轻快的速度
piano_accompaniment.append(tempo.MetronomeMark(number=100))
# 设置钢琴乐器
piano_accompaniment.append(instrument.Piano())
# 简单的钢琴伴奏音符序列,使用不同的和弦进行
# 使用一些常见和弦,如C大调的I-IV-V进程:C, F, G
piano_chords = [
'C4', 'E4', 'G4', 'C5', 'E4', 'G4', 'C4', 'REST', # C大和弦
'F3', 'A3', 'C4', 'F4', 'A3', 'C4', 'F3', 'REST', # F大和弦
'G3', 'B3', 'D4', 'G4', 'B3', 'D4', 'G3', 'REST', # G大和弦
'C4', 'E4', 'G4', 'C5', 'E4', 'G4', 'C4', 'REST' # C大和弦
]
# 将音符添加到钢琴伴奏音乐流中
for n in piano_chords:
if n == 'REST':
piano_accompaniment.append(note.Rest(quarterLength=2))
else:
piano_accompaniment.append(note.Note(n, quarterLength=1))
# 导出为MIDI文件
piano_accompaniment.write('midi', 'Piano_Accompaniment.mid')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment