[英]How do I take user input "n = str(input("Give str"))" while having the numpy module imported?
我的問題是在導入 numpy 模塊時我無法從用戶 ("n = str(input("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)
關於如何克服這個問題的想法雖然我不知道如何實現它們:
非常感謝!
input
是python中的內置函數。
參考: https ://docs.python.org/3/library/functions.html#input
您當前正在將它用作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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.