xref: /aosp_15_r20/external/libopus/dnn/torch/osce/utils/pitch.py (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1"""
2/* Copyright (c) 2023 Amazon
3   Written by Jan Buethe */
4/*
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   - Redistributions of source code must retain the above copyright
10   notice, this list of conditions and the following disclaimer.
11
12   - Redistributions in binary form must reproduce the above copyright
13   notice, this list of conditions and the following disclaimer in the
14   documentation and/or other materials provided with the distribution.
15
16   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28"""
29
30import numpy as np
31
32def hangover(lags, num_frames=10):
33    lags = lags.copy()
34    count = 0
35    last_lag = 0
36
37    for i in range(len(lags)):
38        lag = lags[i]
39
40        if lag == 0:
41            if count < num_frames:
42                lags[i] = last_lag
43                count += 1
44        else:
45            count = 0
46            last_lag = lag
47
48    return lags
49
50
51def smooth_pitch_lags(lags, d=2):
52
53    assert d < 4
54
55    num_silk_frames = len(lags) // 4
56
57    smoothed_lags = lags.copy()
58
59    tmp = np.arange(1, d+1)
60    kernel = np.concatenate((tmp, [d+1], tmp[::-1]), dtype=np.float32)
61    kernel = kernel / np.sum(kernel)
62
63    last = lags[0:d][::-1]
64    for i in range(num_silk_frames):
65        frame = lags[i * 4: (i+1) * 4]
66
67        if np.max(np.abs(frame)) == 0:
68            last = frame[4-d:]
69            continue
70
71        if i == num_silk_frames - 1:
72            next = frame[4-d:][::-1]
73        else:
74            next = lags[(i+1) * 4 : (i+1) * 4 + d]
75
76        if np.max(np.abs(next)) == 0:
77            next = frame[4-d:][::-1]
78
79        if np.max(np.abs(last)) == 0:
80            last = frame[0:d][::-1]
81
82        smoothed_frame = np.convolve(np.concatenate((last, frame, next), dtype=np.float32), kernel, mode='valid')
83
84        smoothed_lags[i * 4: (i+1) * 4] = np.round(smoothed_frame)
85
86        last = frame[4-d:]
87
88    return smoothed_lags
89
90def calculate_acorr_window(x, frame_size, lags, history=None, max_lag=300, radius=2, add_double_lag_acorr=False, no_pitch_threshold=32):
91    eps = 1e-9
92
93    lag_multiplier = 2 if add_double_lag_acorr else 1
94
95    if history is None:
96        history = np.zeros(lag_multiplier * max_lag + radius, dtype=x.dtype)
97
98    offset = len(history)
99
100    assert offset >= max_lag + radius
101    assert len(x) % frame_size == 0
102
103    num_frames = len(x) // frame_size
104    lags = lags.copy()
105
106    x_ext = np.concatenate((history, x), dtype=x.dtype)
107
108    d = radius
109    num_acorrs = 2 * d + 1
110    acorrs = np.zeros((num_frames, lag_multiplier * num_acorrs), dtype=x.dtype)
111
112    for idx in range(num_frames):
113        lag = lags[idx].item()
114        frame = x_ext[offset + idx * frame_size : offset + (idx + 1) * frame_size]
115
116        for k in range(lag_multiplier):
117            lag1 = (k + 1) * lag if lag >= no_pitch_threshold else lag
118            for j in range(num_acorrs):
119                past = x_ext[offset + idx * frame_size - lag1 + j - d : offset + (idx + 1) * frame_size - lag1 + j - d]
120                acorrs[idx, j + k * num_acorrs] = np.dot(frame, past) / np.sqrt(np.dot(frame, frame) * np.dot(past, past) + eps)
121
122    return acorrs, lags