我如何进行用户输入&quot'n = str(输入(“给出str”))"在导入Numpy模块的同时?
我的问题是,在导入numpy模块时,我无法从用户(“ n = str(“ give str”)”)中获取输入。我基本上是Python的所有内容,无法克服这个问题。
无论我做什么,
"TypeError: 'numpy.ndarray' object is not callable".
我正在使用的借来的代码:
import pyaudio
import numpy as np
noteslist = {"C":-10,"C#":-9,"D":-8,"D#":-7,"E":-6,"E#":-5,"F":-4,"F#":-3,"G":-2,"G#":-1,"A":0,"A#":1,"B":2}
n = input("Give")
fs = 44100
duration = 5.0
f = 432.0
#Until this point I need to be albe to code as if no modules (numpy) have been imported.
#Part 2 of the code
input = np.sin(2*np.pi*np.arange(fs*duration)*f/fs)
def play(input):
sample = (input).astype(np.float32)
volume = 0.25
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32, channels=1, rate=fs, output=True)
stream.write(volume*sample)
stream.stop_stream()
stream.close()
p.terminate()
play(input)
关于如何克服这个问题的想法,尽管我不知道如何实现它们:
- 使代码创建另一个文件或其他文件,并粘贴了代码的后半部分,因为第二一半仅负责播放产生的声音(罪)。
- 将代码分为某种部分:一个没有numpy导入的部分,另一个未导入numpy。
- 在代码的后半部分中导入Numpy,例如Time.Sleep-> Numpy !
非常感谢
my problem is that I can't take input from user ("n = str(input("Give str"))") while having the numpy module imported. I'm new to basically everything in Python and can't get past this problem.
No matter what I do,
"TypeError: 'numpy.ndarray' object is not callable".
The borrowed code I am working with:
import pyaudio
import numpy as np
noteslist = {"C":-10,"C#":-9,"D":-8,"D#":-7,"E":-6,"E#":-5,"F":-4,"F#":-3,"G":-2,"G#":-1,"A":0,"A#":1,"B":2}
n = input("Give")
fs = 44100
duration = 5.0
f = 432.0
#Until this point I need to be albe to code as if no modules (numpy) have been imported.
#Part 2 of the code
input = np.sin(2*np.pi*np.arange(fs*duration)*f/fs)
def play(input):
sample = (input).astype(np.float32)
volume = 0.25
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32, channels=1, rate=fs, output=True)
stream.write(volume*sample)
stream.stop_stream()
stream.close()
p.terminate()
play(input)
Ideas on how to overcome this problem although I have no idea how to realise them:
- Make the code create another file or something and paste second half of the code, since the second half is only responsible for playing generated sound (sin).
- Split the code into some kind of sections: one without numpy imported, another with numpy imported.
- Make the numpy import in the second half of the code, like time.sleep -> import numpy
Thanks so much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
输入
是Python中的一个内置功能。<br>参考: https://docs.python.org/3/library/functions。 html#输入
您当前正在使用它作为
numpy
的变量阵列。这将行不通。
而是将其重命名为另一个变量。
考虑此更新的代码。
input
is a built-in function in python.Reference: https://docs.python.org/3/library/functions.html#input
You are currently using it as a variable for your
numpy
array.This will not work.
Instead rename it to another variable.
Consider this updated code.