如何删除通过将声音音调声音剪辑连接在一起构建的音频中的“砰砰”和“咔嗒”声?

我有这个 PyAudio 代码用于生成一系列音调:

import time
import math
import pyaudio

class Beeper(object):

    def __init__(self, **kwargs):
        self.bitrate = kwargs.pop('bitrate', 16000)
        self.channels = kwargs.pop('channels', 1)
        self._p = pyaudio.PyAudio()
        self.stream = self._p.open(
            format = self._p.get_format_from_width(1),
            channels = self.channels,
            rate = self.bitrate,
            output = True,
        )
        self._queue = []

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.stream.stop_stream()
        self.stream.close()

    def tone(self, frequency, length=1000, play=False, **kwargs):

        number_of_frames = int(self.bitrate * length/1000.)

        ##TODO:fix pops?
        g = get_generator()
        for x in xrange(number_of_frames):
            self._queue.append(chr(int(math.sin(x/((self.bitrate/float(frequency))/math.pi))*127+128)))

    def play(self):
        sound = ''.join(self._queue)
        self.stream.write(sound)
        time.sleep(0.1)

with Beeper(bitrate=88000, channels=2) as beeper:
    i = 0
    for f in xrange(1000, 800-1, int(round(-25/2.))):
        i += 1
        length = log(i+1) * 250/2./2.
        beeper.tone(frequency=f, length=length)
    beeper.play()

但是当音调发生变化时,音频中有一种独特的“流行”,我不知道如何将其删除。

起初,我认为流行音乐正在发生,因为我正在立即播放每个剪辑,而在我生成剪辑时每次播放之间的时间足以导致音频变平。但是,当我将所有剪辑连接成一个字符串并播放时,流行音乐仍然存在。

然后,我认为正弦波在每个剪辑的边界处不匹配,因此我尝试将当前音频剪辑的前 N ​​帧与前一个剪辑的最后 N 帧进行平均,但这也没有效果。

我究竟做错了什么?我该如何解决?

最佳答案

我最初怀疑单个波形没有对齐是正确的,我通过在 Audacity 中检查证实了这一点。我的解决方案是修改代码以在正弦波的峰值上启动和停止每个波形。

def tone(self, frequency, length=1000, play=False, **kwargs):

    number_of_frames = int(self.bitrate * length/1000.)

    record = False
    x = 0
    y = 0
    while 1:
        x += 1
        v = math.sin(x/((self.bitrate/float(frequency))/math.pi))

        # Find where the sin tip starts.
        if round(v, 3) == +1:
            record = True

        if record:
            self._queue.append(chr(int(v*127+128)))
            y += 1
            if y > number_of_frames and round(v, 3) == +1:
                # Always end on the high tip of the sin wave to clips align.
                break

关于python - 如何从 PyAudio 中的连接声音数据中删除爆破音,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36438850/

10-12 00:19
查看更多