马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 zltzlt 于 2020-2-24 20:36 编辑
Python 制作音乐
Python 可以做许多事,当然也能制作音乐。这篇帖子我教大家如何用 Python 制作简单的音乐。
准备材料
music_syn 第三方库(使用 pip 安装即可)
正式开始
例子:
from music_syn import pysynth as p
song = (('c4', 2), ('c4', 2), ('g4', 2), ('g4', 2), ('a4', 2), ('a4', 2), ('g4', 1),
('f4', 2), ('f4', 2), ('e4', 2), ('e4', 2), ('d4', 2), ('d4', 2), ('c4', 1),
('g4', 2), ('g4', 2), ('f4', 2), ('f4', 2), ('e4', 2), ('e4', 2), ('d4', 1),
('g4', 2), ('g4', 2), ('f4', 2), ('f4', 2), ('e4', 2), ('e4', 2), ('d4', 1),
('c4', 2), ('c4', 2), ('g4', 2), ('g4', 2), ('a4', 2), ('a4', 2), ('g4', 1),
('f4', 2), ('f4', 2), ('e4', 2), ('e4', 2), ('d4', 2), ('d4', 2), ('c4', 1))
p.make_wav(bpm=255, song=song, fn="E:/music.wav")
重点看 song 的定义:
song = (('c4', 2), ('c4', 2), ('g4', 2), ('g4', 2), ('a4', 2), ('a4', 2), ('g4', 1),
('f4', 2), ('f4', 2), ('e4', 2), ('e4', 2), ('d4', 2), ('d4', 2), ('c4', 1),
('g4', 2), ('g4', 2), ('f4', 2), ('f4', 2), ('e4', 2), ('e4', 2), ('d4', 1),
('g4', 2), ('g4', 2), ('f4', 2), ('f4', 2), ('e4', 2), ('e4', 2), ('d4', 1),
('c4', 2), ('c4', 2), ('g4', 2), ('g4', 2), ('a4', 2), ('a4', 2), ('g4', 1),
('f4', 2), ('f4', 2), ('e4', 2), ('e4', 2), ('d4', 2), ('d4', 2), ('c4', 1))
song 为一个元组,保存着一串音符。其中每个音符为以二元组 (string, number)。
string 代表音符的位置和音调,以 'x[#]y' 表示。
x 是音符的音调,可以是 a (6)、b (7)、c (1) 、d (2) 、e (3) 、f (4) 、g (5)。
# 号是可选的,相当于五线谱中的升记号。
y 是音符所在的区域,如图:
'c#4' 代表以下音符:
'a5' 代表以下音符:
number 代表音符的持续时间(不以秒为单位)。一般 number 越大,音符持续时间越短。
最后一行是生成 .wav 音频文件。
bpm 参数指定音频的节拍,bpm 越高音频节奏越快。
song 参数指定乐曲(也就是之前定义的一串音符 song)。
fn 参数指定音频的保存路径,这里设置为 E 盘的 music.wav 。
运行程序,打开生成的 wav 音频。有没有听出来呢,音乐旋律为《小星星》
合成器
music_syn 提供了多个音频合成器,可以在这里查看 --> https://mdoege.github.io/PySynth/#m
利用不同的合成器生成的音频也不一样,例如:
from music_syn import pysynth_b as p
song = (('c#4', 2), ('c#4', 2), ('g#4', 2), ('g#4', 2), ('a#4', 2), ('a#4', 2), ('g#4', 1),
('f#4', 2), ('f#4', 2), ('e#4', 2), ('e#4', 2), ('d#4', 2), ('d#4', 2), ('c#4', 1),
('g#4', 2), ('g#4', 2), ('f#4', 2), ('f#4', 2), ('e#4', 2), ('e#4', 2), ('d#4', 1),
('g#4', 2), ('g#4', 2), ('f#4', 2), ('f#4', 2), ('e#4', 2), ('e#4', 2), ('d#4', 1),
('c#4', 2), ('c#4', 2), ('g#4', 2), ('g#4', 2), ('a#4', 2), ('a#4', 2), ('g#4', 1),
('f#4', 2), ('f#4', 2), ('e#4', 2), ('e#4', 2), ('d#4', 2), ('d#4', 2), ('c#4', 1))
p.make_wav(bpm=255, song=song, fn="E:/music.wav")
from music_syn import pysynth_c as p
song = (('c#4', 2), ('c#4', 2), ('g#4', 2), ('g#4', 2), ('a#4', 2), ('a#4', 2), ('g#4', 1),
('f#4', 2), ('f#4', 2), ('e#4', 2), ('e#4', 2), ('d#4', 2), ('d#4', 2), ('c#4', 1),
('g#4', 2), ('g#4', 2), ('f#4', 2), ('f#4', 2), ('e#4', 2), ('e#4', 2), ('d#4', 1),
('g#4', 2), ('g#4', 2), ('f#4', 2), ('f#4', 2), ('e#4', 2), ('e#4', 2), ('d#4', 1),
('c#4', 2), ('c#4', 2), ('g#4', 2), ('g#4', 2), ('a#4', 2), ('a#4', 2), ('g#4', 1),
('f#4', 2), ('f#4', 2), ('e#4', 2), ('e#4', 2), ('d#4', 2), ('d#4', 2), ('c#4', 1))
p.make_wav(bpm=255, song=song, fn="E:/music.wav")
from music_syn import pysynth_s as p
song = (('c#4', 2), ('c#4', 2), ('g#4', 2), ('g#4', 2), ('a#4', 2), ('a#4', 2), ('g#4', 1),
('f#4', 2), ('f#4', 2), ('e#4', 2), ('e#4', 2), ('d#4', 2), ('d#4', 2), ('c#4', 1),
('g#4', 2), ('g#4', 2), ('f#4', 2), ('f#4', 2), ('e#4', 2), ('e#4', 2), ('d#4', 1),
('g#4', 2), ('g#4', 2), ('f#4', 2), ('f#4', 2), ('e#4', 2), ('e#4', 2), ('d#4', 1),
('c#4', 2), ('c#4', 2), ('g#4', 2), ('g#4', 2), ('a#4', 2), ('a#4', 2), ('g#4', 1),
('f#4', 2), ('f#4', 2), ('e#4', 2), ('e#4', 2), ('d#4', 2), ('d#4', 2), ('c#4', 1))
p.make_wav(bpm=255, song=song, fn="E:/music.wav")
…… |