简体   繁体   中英

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 . 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. You can adjust the duration (in seconds) and frequency.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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