其他分享
首页 > 其他分享> > 音频处理之numpy类型转pcm二进制

音频处理之numpy类型转pcm二进制

作者:互联网

import sounddevice as sd
import numpy as np
import contextlib
from scipy.io import wavfile
import librosa
import wave
def write_wave(path, audio, sample_rate):
    """Writes a .wav file.
    Takes path, PCM audio data, and sample rate.
    """
    with contextlib.closing(wave.open(path, 'wb')) as wf:
        wf.setnchannels(1)
        wf.setsampwidth(2)
        wf.setframerate(sample_rate)
        wf.writeframes(audio)


fs = 16000  # Sample rate
seconds = 5  # Duration of recording

myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=1)
sd.wait()
# wavfile.write('recording.wav', fs, myrecording)\
#    幅值归一化
import time
start=time.time()
librosa.load("recording.wav",16000)
print(time.time()-start)
start=time.time()
ints = (((myrecording-myrecording.mean())/np.max(np.abs(myrecording)))*int(32767//16)).astype(np.int16)
little_endian = ints.astype('<u2')
buf = little_endian.tobytes()
print(time.time()-start)
# write_wave("output.wav",buf,sample_rate=fs)




if __name__ == '__main__':
    pass

标签:wf,音频,pcm,np,rate,time,myrecording,import,numpy
来源: https://blog.csdn.net/weixin_32759777/article/details/120321991