将蜂鸣声保存在.wav文件中
我在 Python 中生成了一个嘟嘟声,该声音持续 5 毫秒,并在接下来的 10 秒内每 1 秒重复一次。
代码如下:
## Import modules
import time
import sys
import winsound
import soundfile as sf
## Set frequency and duration of beep sound
frequency = 37
duration = 5 # duration of beep sound is 5ms
for i in range(1,10): # create beep sound for 10s
## Create beep sound for 5 ms, after every 1 s
sys.stdout.write('\r\a{i}'.format(i=i))
sys.stdout.flush()
winsound.Beep(frequency, duration)
time.sleep(1) # Repeat beep sound after 1s
现在,我想将此模式保存在 .wav 文件中。因此,我更改了代码:
## Import modules
import time
import sys
import winsound
import soundfile as sf
## Set frequency and duration of beep sound
frequency = 37
duration = 5 # duration of beep sound is 5ms
for i in range(1,10): # create beep spund for 10 s
## Create beep sound for 5 ms, after every 1 s
sys.stdout.write('\r\a{i}'.format(i=i))
sys.stdout.flush()
winsound.Beep(frequency, duration)
time.sleep(1)
## Save the beep spund as a .wav file
sf.write("Beep.wav", winsound.Beep(frequency, duration))
但是,我不断收到错误。
有人可以告诉我如何将其保存在 .wav 文件中吗?
I generaed a beep sound in Python, that exists for 5ms, and repeats after every 1s, for next 10s.
The codes are as such:
## Import modules
import time
import sys
import winsound
import soundfile as sf
## Set frequency and duration of beep sound
frequency = 37
duration = 5 # duration of beep sound is 5ms
for i in range(1,10): # create beep sound for 10s
## Create beep sound for 5 ms, after every 1 s
sys.stdout.write('\r\a{i}'.format(i=i))
sys.stdout.flush()
winsound.Beep(frequency, duration)
time.sleep(1) # Repeat beep sound after 1s
Now, I want to save this pattern in a .wav file. Hence, I changed the codes as such:
## Import modules
import time
import sys
import winsound
import soundfile as sf
## Set frequency and duration of beep sound
frequency = 37
duration = 5 # duration of beep sound is 5ms
for i in range(1,10): # create beep spund for 10 s
## Create beep sound for 5 ms, after every 1 s
sys.stdout.write('\r\a{i}'.format(i=i))
sys.stdout.flush()
winsound.Beep(frequency, duration)
time.sleep(1)
## Save the beep spund as a .wav file
sf.write("Beep.wav", winsound.Beep(frequency, duration))
However, I keep geeting errors.
Can somebady please let me know how do I save this in a .wav file ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误是这样的:
来自这里的文档:
https://pysoundfile.readthedocs.io/en/latest/
我们可以看到函数需要 3 个参数:
问题中的代码仅提供了两个参数。
这 3 个争论是:
缺少
samplerate
。根据给出的文档,这是有效的:
The error is this:
from the docs here:
https://pysoundfile.readthedocs.io/en/latest/
we can see that the function requires 3 arguments:
The code in the question has provided only two arguements.
The 3 arguements are:
It
samplerate
is missing.With the docs given, this works: