python读取raw数据性能比较

python读取raw数据性能比较

1. 使用numpy.frombuffer
    耗时18s


import os
import time
import numpy as np

if __name__ == "__main__":
    file_path = os.path.join(".", "data/48000ofs_2ch_32bit.raw")
    sample_rate = 48000
    channel_num = 2
    print(file_path)

    begin_time = time.clock()
    for i in range(1000):
        # data, sample_rate1 = sf.read(file_path, samplerate=sample_rate, channels=channel_num, subtype="PCM_32")
        fid = open(file_path, 'rb')
        data = fid.read()
        fid.close()
        data = np.frombuffer(data, dtype=np.int32)
        data.shape = -1, channel_num
    end_time = time.clock()
    print(end_time - begin_time)
    print(data.shape)

2. 使用soundfile.read
    耗时36s


import os
import time
import soundfile as sf

if __name__ == "__main__":
    file_path = os.path.join(".", "data/48000ofs_2ch_32bit.raw")
    sample_rate = 48000
    channel_num = 2
    print(file_path)

    begin_time = time.clock()
    for i in range(1000):
        data, sample_rate1 = sf.read(file_path, samplerate=sample_rate, channels=channel_num, subtype="PCM_32")
    end_time = time.clock()
    print(end_time - begin_time)
    print(data.shape)
_subtypes = {
    'PCM_S8':    0x0001,  # Signed 8 bit data
    'PCM_16':    0x0002,  # Signed 16 bit data
    'PCM_24':    0x0003,  # Signed 24 bit data
    'PCM_32':    0x0004,  # Signed 32 bit data
    'PCM_U8':    0x0005,  # Unsigned 8 bit data (WAV and RAW only)
    'FLOAT':     0x0006,  # 32 bit float data
    'DOUBLE':    0x0007,  # 64 bit float data
    'ULAW':      0x0010,  # U-Law encoded.
    'ALAW':      0x0011,  # A-Law encoded.
    'IMA_ADPCM': 0x0012,  # IMA ADPCM.
    'MS_ADPCM':  0x0013,  # Microsoft ADPCM.
    'GSM610':    0x0020,  # GSM 6.10 encoding.
    'VOX_ADPCM': 0x0021,  # OKI / Dialogix ADPCM
    'G721_32':   0x0030,  # 32kbs G721 ADPCM encoding.
    'G723_24':   0x0031,  # 24kbs G723 ADPCM encoding.
    'G723_40':   0x0032,  # 40kbs G723 ADPCM encoding.
    'DWVW_12':   0x0040,  # 12 bit Delta Width Variable Word encoding.
    'DWVW_16':   0x0041,  # 16 bit Delta Width Variable Word encoding.
    'DWVW_24':   0x0042,  # 24 bit Delta Width Variable Word encoding.
    'DWVW_N':    0x0043,  # N bit Delta Width Variable Word encoding.
    'DPCM_8':    0x0050,  # 8 bit differential PCM (XI only)
    'DPCM_16':   0x0051,  # 16 bit differential PCM (XI only)
    'VORBIS':    0x0060,  # Xiph Vorbis encoding.
    'ALAC_16':   0x0070,  # Apple Lossless Audio Codec (16 bit).
    'ALAC_20':   0x0071,  # Apple Lossless Audio Codec (20 bit).
    'ALAC_24':   0x0072,  # Apple Lossless Audio Codec (24 bit).
    'ALAC_32':   0x0073,  # Apple Lossless Audio Codec (32 bit).
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值