Error in user YAML: (<unknown>): found a tab character that violate indentation while scanning a plain scalar at line 3 column 3
---
- oeasy Python 0749
- 这是 oeasy 系统化 Python 教程,从基础一步步讲,扎实、完整、不跳步。愿意花时间学,就能真正学会。
- 本教程同步发布在:
- 个人网站: `https://oeasy.org`
- 蓝桥云课: `https://www.lanqiao.cn/courses/3584`
- GitHub: `https://github.com/overmind1980/oeasy-python-tutorial`
- Gitee: `https://gitee.com/overmind1980/oeasypython`
---- 上次次我们了解了
- 中国五声调式
- 宫商角徵羽 五种调式
- 一次 可以把五种调式 全写出来吗?
使用 Python 生成 MIDI, 要求这个 MIDI 呢,分别生成宫、商、角、徵、羽五种调式的 MIDI。 每个调式呢,比如说宫,宫到宫总共六个音符,商也是这样。从主音开始结束于高八度的主音。
from mido import Message, MetaMessage, MidiFile, MidiTrack
import os
# 五声调式正确音符映射(以主音为起点,MIDI音符编号)
# 每个调式包含从主音到高八度主音的六个音符
modes = {
"宫": [60, 62, 64, 67, 69, 72], # C宫:C, D, E, G, A, C(正确)
"商": [62, 64, 67, 69, 72, 74], # D商:D, E, G, A, C, D(高八度)
"角": [64, 67, 69, 72, 74, 76], # E角:E, G, A, C, D, E(高八度)
"徵": [67, 69, 72, 74, 76, 79], # G徵:G, A, C, D, E, G(高八度)
"羽": [69, 72, 74, 76, 79, 81] # A羽:A, C, D, E, G, A(高八度)
}
# 生成MIDI文件的函数
def generate_mode_midi(mode_name, notes, output_dir="midi_files"):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
mid = MidiFile()
track = MidiTrack()
mid.tracks.append(track)
# 设置速度(中等速度)
track.append(MetaMessage('set_tempo', tempo=500000))
# 生成音符事件(每个音符持续0.5秒)
for i, note in enumerate(notes):
track.append(Message('note_on', note=note, velocity=64, time=0))
track.append(Message('note_off', note=note, velocity=64, time=500))
# 保存文件
output_file = os.path.join(output_dir, f"{mode_name}调式.mid")
mid.save(output_file)
print(f"已生成 {mode_name} 调式MIDI: {output_file}")
# 主函数
def main():
for mode_name, notes in modes.items():
generate_mode_midi(mode_name, notes)
print("所有调式MIDI生成完成!")
if __name__ == "__main__":
main()
- "宫": [60, 62, 64, 67, 69, 72]
- "商": [62, 64, 67, 69, 72, 74]
- "角": [64, 67, 69, 72, 74, 76]
- "徵": [67, 69, 72, 74, 76, 79]
- "羽": [69, 72, 74, 76, 79, 81]
- 宫调式本质上是 一套音程关系
- 大二、大二、小三、大二、小三
- 只要满足这个关系
- 就算是 宫调式
- 我们可以来一个D宫调式的音阶吗?
from mido import MetaMessage, Message, MidiFile, MidiTrack
import os
# D宫调式音符(MIDI音符编号)
# D(62), E(64), #F(66), A(69), B(71), D(高八度74)
d_gong_notes = [62, 64, 66, 69, 71, 74]
def generate_midi(notes, output_file="D宫调式.mid"):
mid = MidiFile()
track = MidiTrack()
mid.tracks.append(track)
# 设置速度(中等速度)
track.append(MetaMessage('set_tempo', tempo=500000))
# 生成每个音符的演奏事件(持续0.6秒)
for note in notes:
track.append(Message('note_on', note=note, velocity=80, time=0))
track.append(Message('note_off', note=note, velocity=80, time=600))
# 保存MIDI文件
mid.save(output_file)
print(f"D宫调式音阶MIDI已生成:{output_file}")
if __name__ == "__main__":
generate_midi(d_gong_notes)
- D宫
- 音程关系 满足 宫调式
- 主音位置 为 D
- 总共有多少种调呢?
- 去总结一下
- 这次我们了解了
- 中国五声调式
- 宫商角徵羽 五种调式
- 调有什么用呢?
- 下次再说 👋
- 本文来自 oeasy Python 系统教程。
- 想完整、扎实学 Python,
- 搜索 oeasy 即可。










