summaryrefslogtreecommitdiffhomepage
path: root/libs/auditok/signal_numpy.py
blob: bf5425197b2df3e3effebef1d8e14588fa75722b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import numpy as np
from .signal import (
    compute_average_channel_stereo,
    calculate_energy_single_channel,
    calculate_energy_multichannel,
)

FORMAT = {1: np.int8, 2: np.int16, 4: np.int32}


def to_array(data, sample_width, channels):
    fmt = FORMAT[sample_width]
    if channels == 1:
        return np.frombuffer(data, dtype=fmt).astype(np.float64)
    return separate_channels(data, fmt, channels).astype(np.float64)


def extract_single_channel(data, fmt, channels, selected):
    samples = np.frombuffer(data, dtype=fmt)
    return np.asanyarray(samples[selected::channels], order="C")


def compute_average_channel(data, fmt, channels):
    array = np.frombuffer(data, dtype=fmt).astype(np.float64)
    return array.reshape(-1, channels).mean(axis=1).round().astype(fmt)


def separate_channels(data, fmt, channels):
    array = np.frombuffer(data, dtype=fmt)
    return np.asanyarray(array.reshape(-1, channels).T, order="C")