我如何进行用户输入&quot'n = str(输入(“给出str”))"在导入Numpy模块的同时?

发布于 2025-02-14 00:07:13 字数 1111 浏览 3 评论 0原文

我的问题是,在导入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)

关于如何克服这个问题的想法,尽管我不知道如何实现它们:

  1. 使代码创建另一个文件或其他文件,并粘贴了代码的后半部分,因为第二一半仅负责播放产生的声音(罪)。
  2. 将代码分为某种部分:一个没有numpy导入的部分,另一个未导入numpy。
  3. 在代码的后半部分中导入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:

  1. 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).
  2. Split the code into some kind of sections: one without numpy imported, another with numpy imported.
  3. Make the numpy import in the second half of the code, like time.sleep -> import numpy

Thanks so much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹤仙姿 2025-02-21 00:07:15

输入是Python中的一个内置功能。<​​br>
参考: https://docs.python.org/3/library/functions。 html#输入

您当前正在使用它作为numpy的变量阵列。
这将行不通。

而是将其重命名为另一个变量。

考虑此更新的代码。

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

A = np.sin(2 * np.pi * np.arange(fs * duration) * f / fs)

def play(A):
    sample = (A).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(A)

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.

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

A = np.sin(2 * np.pi * np.arange(fs * duration) * f / fs)

def play(A):
    sample = (A).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(A)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文