pyauido中的奇怪的白噪声
我正在尝试通过与Numpy阵列混乱来动态修改歌曲的音量。我做到了,但是除了主歌之外,我还可以听到一些奇怪的声音。我认为问题是“ numpy.int8”。
import pyaudio
import wave
import numpy
def audio_datalist_set_volume(datalist, volume):
""" Change value of list of audio chunks """
sound_level = (volume / 100.)
chunk = numpy.frombuffer(datalist, numpy.int8)
chunk = chunk * sound_level
return chunk.astype(numpy.int8)
CHUNK = 1024
# if len(sys.argv) < 2:
# print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
# sys.exit(-1)
wf = wave.open("your_audio.wav", 'rb')
# instantiate PyAudio (1)
p = pyaudio.PyAudio()
# open stream (2)
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
# read data
data = wf.readframes(CHUNK)
# play stream (3)
while len(data) > 0:
nda = audio_datalist_set_volume(data, 20)
stream.write(nda)
data = wf.readframes(CHUNK)
# stop stream (4)
stream.stop_stream()
stream.close()
# close PyAudio (5)
p.terminate()
更新:我认为我发现了问题:numpy.int8
应该为“ B”
或“ I1”
更改。这是一个适当的决定吗?
I am trying to dynamically modify the volume of a song by messing with numpy arrays. And I did it, but I can hear some strange noises in addition to the main song. I think the problem is "numpy.int8".
import pyaudio
import wave
import numpy
def audio_datalist_set_volume(datalist, volume):
""" Change value of list of audio chunks """
sound_level = (volume / 100.)
chunk = numpy.frombuffer(datalist, numpy.int8)
chunk = chunk * sound_level
return chunk.astype(numpy.int8)
CHUNK = 1024
# if len(sys.argv) < 2:
# print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
# sys.exit(-1)
wf = wave.open("your_audio.wav", 'rb')
# instantiate PyAudio (1)
p = pyaudio.PyAudio()
# open stream (2)
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
# read data
data = wf.readframes(CHUNK)
# play stream (3)
while len(data) > 0:
nda = audio_datalist_set_volume(data, 20)
stream.write(nda)
data = wf.readframes(CHUNK)
# stop stream (4)
stream.stop_stream()
stream.close()
# close PyAudio (5)
p.terminate()
Update: I think I have found the problem: numpy.int8
should be changed for "b"
or "i1"
. Is it a proper decision?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要处理任意WAV文件,则应确保Numpy数据类型与流的格式匹配:
If you want to handle arbitrary wav files, you should make sure the numpy data type matches the format of the stream: