我希望能够在下面的代码中重现这个想法,但是声音听起来并不刺耳。理想情况下,我想要一个使用小学生可以理解的代码的解决方案(可以导入play_note函数,这样他们就不必担心它的工作原理了)。答案here建议在连续的音符之间单击是由于声音循环不完整,但我不知道如何解决此变化的持续时间。
有人可以帮忙吗?可以通过一些调整使其工作还是该方法存在某种缺陷?
import turtle
import pyaudio
import numpy as np
def play_note(freq, dur):
p = pyaudio.PyAudio()
volume = 0.5 # range [0.0, 1.0]
fs = 44100 # sampling rate, Hz, must be integer
duration = dur # in seconds, may be float
f = freq # sine frequency, Hz, may be float
# generate samples, note conversion to float32 array
samples = (np.sin(2*np.pi*np.arange(fs*duration)*f/fs)).astype(np.float32)
# for paFloat32 sample values must be in range [-1.0, 1.0]
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=fs,
output=True)
# play. May repeat with different volume values (if done interactively)
stream.write(volume*samples)
stream.stop_stream()
stream.close()
p.terminate()
def tree(branchLen,t):
if branchLen > 5:
freq = branchLen * 2 + 400
dur = branchLen / 100.0
print freq, dur
play_note(freq, dur)
t.forward(branchLen)
t.right(20)
tree(branchLen-15,t)
t.left(40)
tree(branchLen-15,t)
t.right(20)
t.backward(branchLen)
def main():
t = turtle.Turtle()
t.speed(0)
myWin = turtle.Screen()
t.left(90)
t.up()
t.backward(100)
t.down()
t.color("green")
tree(75,t)
myWin.exitonclick()
main()
最佳答案
我不确定您到底需要什么。我试图运行您的代码。它遇到了一些问题。所以我修好了。然后将所有变量汇总在一起,以方便访问。变量的修改函数,以便可以从主函数进行设置。导入所需的模块。
我在递归之外提供了freq
和duration
。因此,它们是固定的。在原始代码中,freq
相对于树的长度增加而持续时间减少。这是产生噪音的原因之一,因为freq
和duration
并非60秒的倍数,因此会产生导致噪音的间隙。使用固定的freq
和duration
,噪声最小。
由于创建新分支和执行递归操作的微秒延迟会产生一些噪音。
这是编辑的代码。
import pyaudio
import turtle
import numpy as np
def play_note(freq, dur):
p = pyaudio.PyAudio()
volume = 0.5 # range [0.0, 1.0]
fs = 44100 # sampling rate, Hz, must be integer
duration = dur # in seconds, may be float
f = freq # sine frequency, Hz, may be float
# generate samples, note conversion to float32 array
samples = (np.sin(2*np.pi*np.arange(fs*duration)*f/fs)).astype(np.float32)
# for paFloat32 sample values must be in range [-1.0, 1.0]
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=fs,
output=True)
# play. May repeat with different volume values (if done interactively)
stream.write(volume*samples)
stream.stop_stream()
stream.close()
p.terminate()
def tree(branchLen,t):
if branchLen > 5:
freq = branchLen * 2 + 420
dur = branchLen / 60.0
print "branchLen={} , Freq = {}, Duration={}".format(branchLen, freq, dur)
t.forward(branchLen)
play_note(freq, dur)
t.right(20)
tree(branchLen-15,t)
play_note(freq, dur)
t.left(40)
play_note(freq, dur)
tree(branchLen-15,t)
play_note(freq, dur)
t.right(20)
play_note(freq, dur)
t.backward(branchLen)
play_note(freq, dur)
def main():
t = turtle.Turtle()
t.speed(turtle_speed)
myWin = turtle.Screen()
t.left(branch_left_turn)
t.up()
t.backward(branch_back_turn)
t.down()
t.color(tree_color)
tree(tree_length,t)
myWin.exitonclick()
if __name__ == '__main__':
#Global Variables brought together for easy setting
turtle_speed = 0.5 #speed of turtle graphics <1 -> faster ; >1 -> slower
tree_length = 60 #keep within multiples of 60 for smoothness
branch_left_turn = 90
branch_back_turn = 100
tree_color = "green" #can experiment with "red" "blue" etc
#Call main function
main()
输出数据:
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
branchLen=60 , Freq = 540, Duration=1.0
branchLen=45 , Freq = 510, Duration=0.75
branchLen=30 , Freq = 480, Duration=0.5
branchLen=15 , Freq = 450, Duration=0.25
branchLen=15 , Freq = 450, Duration=0.25
branchLen=30 , Freq = 480, Duration=0.5
branchLen=15 , Freq = 450, Duration=0.25
branchLen=15 , Freq = 450, Duration=0.25
branchLen=45 , Freq = 510, Duration=0.75
branchLen=30 , Freq = 480, Duration=0.5
branchLen=15 , Freq = 450, Duration=0.25
branchLen=15 , Freq = 450, Duration=0.25
branchLen=30 , Freq = 480, Duration=0.5
branchLen=15 , Freq = 450, Duration=0.25
branchLen=15 , Freq = 450, Duration=0.25
>>>
输出图像
关于python - 使用Turtle和PyAudio在Python中平稳地实现 “Audialize”递归,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40875503/