File size: 349 Bytes
bb986e3
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import numpy as np
from pydub import AudioSegment


def np_to_audio(sr, x):
    def normalize_audio(x):
        return x / np.max(np.abs(x))

    channels = 2 if (x.ndim == 2 and x.shape[1] == 2) else 1

    x = normalize_audio(x)
    y = np.int16(x * 2 ** 15)

    return AudioSegment(y.tobytes(), frame_rate=sr, sample_width=2, channels=channels)