[英]How to play a custom sound frequency
I'm starting with python and currently working an a simple frequency "generator".我从 python 开始,目前正在使用一个简单的频率“发生器”。 I'm using pysine to play the frequencies but it can only play a frequency that is defined by a variable like this我正在使用 pysine 来播放频率,但它只能播放由这样的变量定义的频率
frequency1 = 555
frequency2 = 700
frequency3 = 1000
duration = 1 # 1 second duration
In terminal, the sound options is selected by inputs在终端中,声音选项由输入选择
freq = input("\nOption: ")
if freq == '1':
print('Producing sine wave with ' + str(frequency1) + "hz and duration of " + str(duration) + " seconds.")
pysine.sine(frequency1, duration)
invalidcommand = False
The problem is that the custom input don't work问题是自定义输入不起作用
elif freq == '4':
custom = input('\nCustom frequency: ')
print('Producing sine wave with ' + str(custom) + "hz and duration of " + str(duration) + " seconds.")
pysine.sine(str(custom), duration)
invalidcommand = False
The errors:错误:
Traceback (most recent call last):
File "\PycharmProjects\pythonProject1\venv\lib\site-packages\pysine\pysine.py", line 44, in sine
data = np.array((np.sin(times*frequency*2*np.pi) + 1.0)*127.5, dtype=np.int8).tostring()
numpy.core._exceptions.UFuncTypeError: ufunc 'multiply' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('<U32')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/PycharmProjects/pythonProject1/doppler.py", line 51, in <module>
doppler()
File "/PycharmProjects/pythonProject1/doppler.py", line 47, in doppler
pysine.sine(str(custom), duracao)
File "\PycharmProjects\pythonProject1\venv\lib\site-packages\pysine\pysine.py", line 56, in sine
return PYSINE.sine(frequency=frequency, duration=duration)
File "\PycharmProjects\pythonProject1\venv\lib\site-packages\pysine\pysine.py", line 47, in sine
omega = 2.0*pi*frequency/self.BITRATE
NameError: name 'pi' is not defined
Is there any other way to this work?这项工作还有其他方法吗?
pysine.sine()
function takes an integer type argument, but you were passing a string which caused an error, so you can convert the input from the user to int using int()
: pysine.sine()
function 采用 integer 类型参数,但您传递的字符串导致错误,因此您可以使用int()
将用户输入转换为 int :
import pysine
frequency1 = 555
frequency2 = 700
frequency3 = 1000
duration = 1
freq = input("\nOption: ")
if freq == '1':
print('Producing sine wave with ' + str(frequency1) + "hz and duration of " + str(duration) + " seconds.")
pysine.sine(frequency1, duration)
invalidcommand = False
elif freq == '4':
custom = int(input('\nCustom frequency: '))
print('Producing sine wave with ' + str(custom) + "hz and duration of " + str(duration) + " seconds.")
pysine.sine(custom, duration)
invalidcommand = False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.