将蜂鸣声保存在.wav文件中

发布于 2025-01-17 17:02:56 字数 1262 浏览 7 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(1

老旧海报 2025-01-24 17:02:56

错误是这样的:

write() missing 1 required positional argument: 'samplerate'

来自这里的文档:
https://pysoundfile.readthedocs.io/en/latest/

我们可以看到函数需要 3 个参数:

sf.write('new_file.flac', data, samplerate)

问题中的代码仅提供了两个参数。

这 3 个争论是:

  1. 文件(str 或 int 或类似文件的对象)
  2. 数据(array_like)
  3. 采样率(int)

缺少 samplerate

根据给出的文档,这是有效的:

## Import modules

import time
import sys
import numpy as np
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
file = 'G:\\My Drive\\darren\\02_programming\python\\tests\\Beep.wav'
with sf.SoundFile(file, 'w', 44100, 2, 'PCM_24') as f:
    f.write(np.random.randn(10, 2))

The error is this:

write() missing 1 required positional argument: 'samplerate'

from the docs here:
https://pysoundfile.readthedocs.io/en/latest/

we can see that the function requires 3 arguments:

sf.write('new_file.flac', data, samplerate)

The code in the question has provided only two arguements.

The 3 arguements are:

  1. file (str or int or file-like object)
  2. data (array_like)
  3. samplerate (int)

It samplerate is missing.

With the docs given, this works:

## Import modules

import time
import sys
import numpy as np
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
file = 'G:\\My Drive\\darren\\02_programming\python\\tests\\Beep.wav'
with sf.SoundFile(file, 'w', 44100, 2, 'PCM_24') as f:
    f.write(np.random.randn(10, 2))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文