gpt4 book ai didi

python - 如何编写多频音频文件?

转载 作者:太空狗 更新时间:2023-10-29 12:19:07 27 4
gpt4 key购买 nike

我正在尝试编写一个音调从 440hz 到 600hz 的音频文件。该文件应从 440hz 开始,然后每个频率(按递增顺序)播放 1 秒,以 600hz 结束。我想出了 python 的 wave 模块,但我在这里做错了,因为我最终得到了一个没有声音的文件。 (如果有人有更好的建议,我真的不在乎它是否在 python 中。我使用的是 Linux,任何可以在该平台上运行的东西都可以。我只需要创建一个具有上述规范的音频文件。谢谢!)

frequencies = range(440,600)
data_size = len(frequencies)
fname = "WaveTest.wav"
frate = 11025.0 # framerate as a float
amp = 64000.0 # multiplier for amplitude

sine_list_x = []
for f in frequencies:
for x in range(data_size):
sine_list_x.append(math.sin(2*math.pi*f*(x/frate)))

wav_file = wave.open(fname, "w")

nchannels = 1
sampwidth = 2
framerate = int(frate)
nframes = data_size
comptype = "NONE"
compname = "not compressed"

wav_file.setparams((nchannels, sampwidth, framerate, nframes,
comptype, compname))

for s in sine_list_x:
# write the audio frames to file
wav_file.writeframes(struct.pack('h', int(s*amp/2)))

wav_file.close()

最佳答案

这样的事情应该可行:希望它至少是您继续下去的一个好的起点。

import numpy as N
import wave

towrite = ''
for freq in xrange(440,600):
duration = 1
samplerate = 44100
samples = duration*samplerate
period = samplerate / float(freq) # in sample points
omega = N.pi * 2 / period

xaxis = N.arange(samples,dtype = N.float)
ydata = 16384 * N.sin(xaxis*omega)

signal = N.resize(ydata, (samples,))

towrite += ''.join([wave.struct.pack('h',s) for s in signal])

f = wave.open('freqs.wav', 'wb')
f.setparams((1,2,44100, 44100*4, 'NONE', 'noncompressed'))
f.writeframes(towrite)
f.close()

Reference

关于python - 如何编写多频音频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15373859/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com