1#!/usr/bin/env python3
2#
3# Copyright 2023 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18import numpy as np
19from scipy import signal
20import sys
21
22KERNEL_Q = 512
23KERNEL_A = 16
24
25KAISER_BETA = 12.5
26
27#
28# Transfer function
29#
30
31a = KERNEL_A
32q = KERNEL_Q
33beta = KAISER_BETA
34
35w = signal.kaiser(2 * a * q + 1, beta)
36k = np.sinc(np.linspace(-a, a, 2 * a * q + 1)) * w
37
38h = k[:-1].reshape((2 * a, q)).T
39h = np.append(h, [np.roll(h[0], -1)], axis=0)
40h = np.flip(h, axis=0)
41
42d = h[1:] - h[:-1]
43
44#
45# File header
46#
47
48print("""\
49/*
50 * Copyright 2023 The Android Open Source Project
51 *
52 * Licensed under the Apache License, Version 2.0 (the "License");
53 * you may not use this file except in compliance with the License.
54 * You may obtain a copy of the License at
55 *
56 *      http://www.apache.org/licenses/LICENSE-2.0
57 *
58 * Unless required by applicable law or agreed to in writing, software
59 * distributed under the License is distributed on an "AS IS" BASIS,
60 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
61 * See the License for the specific language governing permissions and
62 * limitations under the License.
63 */
64
65/* This file is auto-generated using "{}".  DO NOT EDIT. */
66
67#include "asrc_tables.h"
68
69namespace bluetooth::audio::asrc {{
70""".format(sys.argv[0]))
71
72#
73# 32 bits tables
74#
75
76h32 = np.clip(np.rint(h * 2**31), -(1 << 31), (1 << 31) - 1).astype(np.int32)
77d32 = np.clip(np.rint(d * 2**23), -(1 << 23), (1 << 23) - 1).astype(np.int16)
78
79print("""\
80// clang-format off
81const ResamplerTables resampler_tables = {
82
83  .h = {
84""")
85for q in range(len(h) - 1):
86    layout = "  {{" + " {:10d}," * 8 + "\n" + \
87              "   " + " {:10d}," * 8 + "\n" + \
88              "   " + " {:10d}," * 8 + "\n" + \
89              "   " + " {:10d}," * 6 + " {:10d} }},"
90    print(layout.format(*h32[q]))
91print("""
92  },
93""")
94
95print("""\
96  .d = {
97""")
98for q in range(len(h) - 1):
99    layout = "  {{" + " {:6d}," * 10 + "\n" + \
100              "   " + " {:6d}," * 10 + "\n" + \
101              "   " + " {:6d}," * 10 + " {:2d} }},"
102    print(layout.format(*d32[q]))
103print("""
104  }
105};
106// clang-format off""")
107
108#
109# File footer
110#
111
112print("""
113} // namespace bluetooth::audio::asrc""")
114