Skip to content

Latest commit

 

History

History
173 lines (120 loc) · 3.27 KB

662-1817168-搭建基础环境.sy.md

File metadata and controls

173 lines (120 loc) · 3.27 KB
show version enable_checker
step
1.0
true

python_音频处理_midi制作

开始

  • 我们首先要从零开始
  • 安装midi软件
sudo apt update
yes | sudo apt install rosegarden

图片描述

  • 也可以直接运行

rosegarden

rosegarden
  • 运行成功

图片描述

  • 尝试使用python写mid文件

安装类库

pip install MIDIUtil
  • 安装成功后
    • 准备用python写midi文件

大调音阶

from midiutil import MIDIFile

# create MIDI file
midi_file = MIDIFile(1)

# add a track
track = 0
time = 0
midi_file.addTrackName(track, time, "Sample Track")
midi_file.addTempo(track, time, 120)

# add notes to the track
channel = 0
pitch = 60    # C3
duration = 1  # 1 beat
volume = 100  # max volume
for i in range(0, 8):
    midi_file.addNote(track, channel, pitch+i, time+i*duration, duration, volume)

# write MIDI file to disk
with open("output.mid", "wb") as output_file:
    midi_file.writeFile(output_file)
  • 运行之后
    • 尝试打开output.mid

打开效果

图片描述

  • 查看钢琴卷帘

图片描述

  • 可以修改为大调音阶吗?

大调音阶

from midiutil import MIDIFile

# create MIDI file
midi_file = MIDIFile(1)

# add a track
track = 0
time = 0
midi_file.addTrackName(track, time, "Sample Track")
midi_file.addTempo(track, time, 120)

# add notes to the track
channel = 0
pitch = 72    # C4
duration = 1  # 1 beat
volume = 100  # max volume
j = 0
for i in [0,2,4,5,7,9,11,12]:
    midi_file.addNote(track, channel, pitch+i, time+j*duration, duration, volume)
    j = j + 1

# write MIDI file to disk
with open("output.mid", "wb") as output_file:
    midi_file.writeFile(output_file)
  • 修改后结果

图片描述

  • 有更标准的写法吗?

帮助文档

firefox https://pypi.org/project/MIDIUtil/
  • 文档状态

图片描述

  • 可以跑跑其中的代码吗?

运行

#!/usr/bin/env python

from midiutil import MIDIFile

degrees  = [60, 62, 64, 65, 67, 69, 71, 72]  # MIDI note number
track    = 0
channel  = 0
time     = 0    # In beats
duration = 1    # In beats
tempo    = 60   # In BPM
volume   = 100  # 0-127, as per the MIDI standard

MyMIDI = MIDIFile(1)  # One track, defaults to format 1 (tempo track is created
                      # automatically)
MyMIDI.addTempo(track, time, tempo)

for i, pitch in enumerate(degrees):
    MyMIDI.addNote(track, channel, pitch, time + i, duration, volume)

with open("major-scale.mid", "wb") as output_file:
    MyMIDI.writeFile(output_file)
  • 输出了一系列的 大调音阶
    • major-scale

图片描述

  • 打开之后查看到mid状态
    • 确实是大调音阶

总结

  • 这次从零开始
    • 安装了midi软件
    • 使用python制作了mid文件
  • 不过
    • 究竟什么是mid呢?🤔
  • 我们下次再说!👋