shithub: opus

ref: 35ee397e060283d30c098ae5e17836316bbec08b
dir: /dnn/torch/lpcnet/utils/wav.py/

View raw version
import wave

def wavwrite16(filename, x, fs):
    """ writes x as int16 to file with name filename

        If x.dtype is int16 x is written as is. Otherwise,
        it is scaled by 2**15 - 1 and converted to int16.
    """
    if x.dtype != 'int16':
        x = ((2**15 - 1) * x).astype('int16')

    with wave.open(filename, 'wb') as f:
        f.setparams((1, 2, fs, len(x), 'NONE', ""))
        f.writeframes(x.tobytes())