音頻重採樣

音頻重採樣

計算公式

pcm 數據計算公式:

數據量Byte=採樣頻率Hz ×(採樣位數/8)× 聲道數 × 時間s

bytes=samplerate×(samplewidth÷8)×channels×time

time=bytes÷(samplerate×channels×samplewidth)×8

重採樣策略

1)線性插值。只需通過插值計算來自兩個最近鄰居的期望時間索引的樣本值。

2)使用脫機處理以2倍的倍數對樣本表進行過採樣,顯然使存儲器需求翻倍。在那些過採樣的採樣上使用相同的線性插值。

3)還可以使重採樣器進程的音頻採樣速率加倍,使用半帶低通濾波器和最後一個抽取級來回到音頻回放速率。

4)不是線性插值而是使用短窗口的sinc插值器。

重採樣

    def resample(data: bytes,
                 orig_sample_width: int,
                 orig_channels: int,
                 orig_sample_rate: int,
                 target_sample_rate: int):
        '''
        將多聲道採樣的音頻數據,重採樣到目標採樣率的音頻數據。信道和採樣精度不改變,值改變採樣率。
        :param data                 原始音頻數據
        :param orig_sample_width    原始採樣精度
        :param orig_channels        原始信道
        :param orig_sample_rate     原始採樣率
        :param target_sample_rate   目標採樣率
        :return:
        '''
        if orig_sample_rate == target_sample_rate:
            return data
        ntype = numpy.int8 if 1 == orig_sample_width else numpy.int16
        np_data = numpy.fromstring(data, ntype)
        if orig_channels > 1:
            np_data = np_data[::orig_channels]
            orig_channels = 1
        orig_len = len(np_data)
        np_orgi_x = numpy.linspace(0, orig_len, orig_len)
        f = scipy.interpolate.interp1d(np_orgi_x, np_data, 'cubic')
        targ_len = target_sample_rate * orig_len / orig_sample_rate
        np_targ_x = numpy.linspace(0, orig_len, targ_len)
        np_targ_data = f(np_targ_x)
        return np_targ_data.astype(ntype).tostring()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章