用python制作音乐_Python3使用PySynth制作音乐的方法

本人虽然五音不全,但是听歌还是很喜欢的。希望能利用机器自动制作音乐,本我发现了一个比较适合入门的有趣的开源音乐生成模块 PySynth ,文我们主要讲解下如何Python3使用PySynth制作音乐。

乐理

声音:物体振动次数越多,声音越大;振动次数越少,声音越低

[En]

Sound: the more times the object vibrates, the higher the sound; the less the number of vibrations, the lower the sound

可听:人耳能听到的声音大约是每秒16-2000次:振动是有规律的,而且声音高低不一。

[En]

Audible: the sound that can be heard by the human ear is about 16-2000 per second: the vibration is regular, and it sounds high and low.

噪音:振动不规律,听起来不明显。

[En]

Noise: the vibration is irregular and it doesn’t sound obvious.

音乐:通过艺术形象表达人们的思想感情。

[En]

Music: to express people’s thoughts and feelings through artistic images.

音阶:do、re、mi、sol、la、(do)

调性:C、D、E、F、G、A、B

安装

pip3 install pysynth

示例

import pysynth

例1:C大调

song1 = [

[‘c’,4],[‘d’,4],[‘e’,4],[‘f’,4],[‘g’,4],[‘a’,4],[‘b’,4],[‘c5’,2],[‘r’,1],

[‘c3’,4],[‘d3’,4],[‘e3’,4],[‘f3’,4],[‘g3’,4],[‘a3’,4],[‘b3’,4],[‘c4’,2],[‘r’,1],

[‘c1‘, 1], [‘c2‘, 1], [‘c3‘, 1], [‘c4‘, 1], [‘c5‘, 1], [‘c6‘, 1], [‘c7‘, 1], [‘c8‘, 1],

]

pysynth.make_wav(song1, fn = “linuxidc.com.wav”)

在当前目录下生成linuxidc.com.wav文件。

在Linux终端输入以下命令试听一下

[linuxidc@localhost www.linuxidc.com]$ aplay -D plughw:0,0 linuxidc.com.wav

试下周杰伦的晴天,bmp是67下每分钟:

import pysynth

songx=((‘e3’,8),(‘d3’,8),(‘f3’,8),(‘e3’,4),(‘c3’,8),(‘g3’,8),(‘b3’,8),(‘c4’,8),(‘b3’,8),(‘c3’,8),(‘c3’,4),(‘c3’,8),(‘a3’,8),(‘a3’,8),

(‘r’,16),(‘a3’,16),(‘g3’,8),(‘g3’,4),(‘g3’,8),(‘f3’,8),(‘e3’,8),(‘d3’,8),(‘e3’,8),(‘f3’,8),(‘e3’,2.25),(‘e3’,8),(‘f#3’,8),(‘g#3’,8),

(‘e3’,4),(‘f3’,8),(‘g3’,8),(‘b3’,8),(‘d4’,8),(‘b3’,8),(‘c4’,8),(‘c4’,6),(‘c4’,16),(‘c4’,8),(‘g3’,8),(‘g3’,8),(‘a3’,8),(‘g3’,8),(‘f3’,8),

(‘a2’,8),(‘b2’,8),(‘c3’,8),(‘d3’,8),(‘e3’,8),(‘d3’,3),(‘e3’,8),(‘c3’,2))

pysynth.make_wav(songx, bpm=67, repeat=0, fn=”linuxidc.wav”)

随机生成中国风音乐旋律

import pysynth

import numpy as np

import re

先限定音符12356 中国风五声调式 这样听起来比较自然

notes=np.array([“c4″,”d4″,”e4″,”g4″,”a4”,])

音符时值

durations=np.array([1,2,4,-2,-4,-8])

随机生成音符 重音穿插其中

sn=[]

for t in range(16):

n=np.random.randint(0,len(notes))

note=notes[n]+”*”

sn.append(note)

for i in range(np.random.randint(3,5)):

note0=notes[np.random.randint(0,len(notes))]

sn.append(note0)

随机生成音符时值序列 形成长短参差变幻的节奏

dn=[]

for i in range(len(sn)):

duration=durations[np.random.randint(0,len(durations))]

nn=sn[i]

dn.append(duration)

将音符和时值合并成旋律

melody=tuple(zip(sn,dn))

print(melody)

将乐谱合成到声音文件

pysynth.make_wav(melody,fn =r”linuxmi.com.wav”)

print(“ok”)

备注:

‘song’ 是一个被定义的列表或元组,格式是这样 [‘音’, 长度]

音符是’a’,’g’这些; 升半音以 ‘#’ 表示,降半音以 ‘b’ 表示;以octave 结束 (默认为四分音符);asterisk 在最后代表重音; ‘r’ 是空.

音的长度用数字表示:1=全音符; 2=二分音符; 4=四分音符, 等.

浮点音符写法:

1.33 = -2 = 二分浮点音符

2.66 = -4 = 四分浮点音符

5.33 = -8 = 八分浮点音符

一些参数:

节奏:每分钟节拍数; bpm = 95

八度转变 (neg. 降八度; pos. 升八度); transpose = 0

音符间停顿 (0. = 连音 ; 0.5 = 断音); pause = 0.05

Volume boost:音量变高 (1. = 音量无变化); boost = 1.2

Output file name 输出文件名;fn = ‘pysynth_output.wav’

其他参数:

Influences the decay of harmonics over frequency. Lowering the value eliminates even more harmonics at high frequencies.

Suggested range: between 3. and 5., depending on the frequency response of speakers/headphones used

harm_max = 4.

总结

以上所述是小编给大家介绍的Python3使用PySynth制作音乐的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

如果你认为这篇文章对你有帮助,欢迎你转载,请注明来源,谢谢!

[En]

If you think this article is helpful to you, you are welcome to reprint it, please indicate the source, thank you!

Original: https://blog.csdn.net/weixin_36036447/article/details/111888206
Author: editage意得辑
Title: 用python制作音乐_Python3使用PySynth制作音乐的方法

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/524833/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球