音频处理之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

上一篇:HUAWEI Harmony OS 更新升级以后附加了POKERTIME129263到底好用吗


下一篇:python人工智能第一篇