起因
按照网上教程 Python3使用PySynth制作音乐
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"D:\test.wav")
print("ok")
运行时发现函数不存在 报错 AttributeError: module ‘pysynth’ has no attribute ‘make_wav’
原因
经查找,作者将pysynth 从库中移除,所以直接pip install pysynth
的方法下载的不是正常的包
解决方案
1、电脑中带有Git
克隆仓库
git clone https://github.com/mdoege/PySynth.git
进入到克隆的仓库中运行
python3 setup.py install
ok 当运行完毕时,就成功了(这里忘了截图)
2、不用git的方法
a、 下载
点此下载
[备份](链接: https://pan.baidu.com/s/1OKhxix8kj00dMI4jnya53A?pwd=vaxb 提取码: vaxb 复制这段内容后打开百度网盘手机App,操作更方便哦)
b、解压
解压如下
c、 打开Power shell
在文件夹内按住Shift
+鼠标左键 --> 打开Power shell
d、运行
在打开的窗口执行 python setup.py install
ps:小编这里是由于装过了。
e、ok
当运行完毕时,就成功了