简体   繁体   English

如何将winsound.Beep()保存到python中的音频.wav文件?

[英]How do I save a winsound.Beep() to an audio .wav file in python?

I've been trying to do something that I thought would be fairly simple, and that si to save winsound.Beep(8000,1000) to a .wav file called LongBeep.wav . 我一直在尝试做一些我认为非常简单的事情,并且将winsound.Beep(8000,1000)保存到名为LongBeep.wav的.wav文件中。 Can someone pls reply with a solution. 有人可以提供解决方案吗? I have searched and searched for an answer but have found nothing. 我已经搜索了答案,但是什么也没找到。 Here is the code that I have: 这是我的代码:

import sounddevice as sd
import soundfile as sf 
import winsound
import time


sr = 44100
duration = 5
myrecording = sd.rec(int(duration * sr), samplerate=sr, channels=2)
winsound.Beep(8000,1000)
sd.wait()  
time.sleep(1)
sd.play(myrecording, sr)
sf.write("LongBeep.wav", myrecording, sr)

This works for me: 这对我有用:

import numpy as np
from scipy.io.wavfile import write

sps = 44100
freq_hz = 440.0
duration = 5
vol = 0.3

esm = np.arange(duration * sps)
wf = np.sin(2 * np.pi * esm * freq_hz / sps)
wf_quiet = wf * vol
wf_int = np.int16(wf_quiet * 32767)
write("sample.wav", sps, wf_int)

This writes a file called sample.wav to the same directory of the program. 这会将名为sample.wav的文件写入程序的同一目录。 You can adjust the duration (in seconds) and frequency. 您可以调整持续时间(以秒为单位)和频率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM