pyauido中的奇怪的白噪声

发布于 2025-01-26 02:08:26 字数 1203 浏览 1 评论 0原文

我正在尝试通过与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 技术交流群。

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

发布评论

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

评论(1

转身泪倾城 2025-02-02 02:08:26

如果要处理任意WAV文件,则应确保Numpy数据类型与流的格式匹配:

def audio_datalist_set_volume(datalist, volume, numpy_type):
    """ Change value of list of audio chunks """
    sound_level = (volume / 100.)
    chunk = numpy.frombuffer(datalist, numpy_type)
    chunk = chunk * sound_level
    return chunk

# ...

numpy_type = {
    pyaudio.paFloat32: np.float32,
    pyaudio.paInt32: np.int32,
    # numpy can't do 24-bit ints :(
    pyaudio.paInt16: np.int16,
    pyaudio.paInt8: np.int8,
    pyaudio.paUInt8: np.uint8,
}[stream._format]

If you want to handle arbitrary wav files, you should make sure the numpy data type matches the format of the stream:

def audio_datalist_set_volume(datalist, volume, numpy_type):
    """ Change value of list of audio chunks """
    sound_level = (volume / 100.)
    chunk = numpy.frombuffer(datalist, numpy_type)
    chunk = chunk * sound_level
    return chunk

# ...

numpy_type = {
    pyaudio.paFloat32: np.float32,
    pyaudio.paInt32: np.int32,
    # numpy can't do 24-bit ints :(
    pyaudio.paInt16: np.int16,
    pyaudio.paInt8: np.int8,
    pyaudio.paUInt8: np.uint8,
}[stream._format]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文