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).
}