gpt4 book ai didi

python - 使用 Turtle 和 PyAudio 在 Python 中顺利进行 "Audialize"递归

转载 作者:行者123 更新时间:2023-12-02 22:54:39 24 4
gpt4 key购买 nike

我希望能够重现下面代码背后的想法,但声音不会太刺耳。理想情况下,我想要一个使用小学生可以理解的代码的解决方案(可以导入 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()

最佳答案

我不确定你到底需要什么。我试图运行你的代码。它遇到了一些问题。所以我修好了。然后将所有变量汇集在一起​​,以便于访问。修改了变量的函数,以便可以从主函数中设置它们。导入所需的模块。

我提供了freqduration递归之外。因此它们是固定的。在原始代码中,freq增加和持续时间减少相对于树的长度。这是产生噪音的原因之一,如 freqduration然后不是 60 秒的倍数,因此会产生导致噪音的间隙。带固定freqduration ,噪音最小。

由于创建新分支和递归启动的微秒延迟,会有一些噪音。
这里是 已编辑 代码。

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
>>>

输出图像
enter image description here

关于python - 使用 Turtle 和 PyAudio 在 Python 中顺利进行 "Audialize"递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40875503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com