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 30""" This module implements the SILK upsampler from 16kHz to 24 or 48 kHz """ 31 32import torch 33from torch import nn 34import torch.nn.functional as F 35 36import numpy as np 37 38frac_fir = np.array( 39 [ 40 [189, -600, 617, 30567, 2996, -1375, 425, -46], 41 [117, -159, -1070, 29704, 5784, -2143, 611, -71], 42 [52, 221, -2392, 28276, 8798, -2865, 773, -91], 43 [-4, 529, -3350, 26341, 11950, -3487, 896, -103], 44 [-48, 758, -3956, 23973, 15143, -3957, 967, -107], 45 [-80, 905, -4235, 21254, 18278, -4222, 972, -99], 46 [-99, 972, -4222, 18278, 21254, -4235, 905, -80], 47 [-107, 967, -3957, 15143, 23973, -3956, 758, -48], 48 [-103, 896, -3487, 11950, 26341, -3350, 529, -4], 49 [-91, 773, -2865, 8798, 28276, -2392, 221, 52], 50 [-71, 611, -2143, 5784, 29704, -1070, -159, 117], 51 [-46, 425, -1375, 2996, 30567, 617, -600, 189] 52 ], 53 dtype=np.float32 54) / 2**15 55 56 57hq_2x_up_c_even = [x / 2**16 for x in [1746, 14986, 39083 - 65536]] 58hq_2x_up_c_odd = [x / 2**16 for x in [6854, 25769, 55542 - 65536]] 59 60 61def get_impz(coeffs, n): 62 s = 3*[0] 63 y = np.zeros(n) 64 x = 1 65 66 for i in range(n): 67 Y = x - s[0] 68 X = Y * coeffs[0] 69 tmp1 = s[0] + X 70 s[0] = x + X 71 72 Y = tmp1 - s[1] 73 X = Y * coeffs[1] 74 tmp2 = s[1] + X 75 s[1] = tmp1 + X 76 77 Y = tmp2 - s[2] 78 X = Y * (1 + coeffs[2]) 79 tmp3 = s[2] + X 80 s[2] = tmp2 + X 81 82 y[i] = tmp3 83 x = 0 84 85 return y 86 87 88 89class SilkUpsampler(nn.Module): 90 SUPPORTED_TARGET_RATES = {24000, 48000} 91 SUPPORTED_SOURCE_RATES = {16000} 92 def __init__(self, 93 fs_in=16000, 94 fs_out=48000): 95 96 super().__init__() 97 self.fs_in = fs_in 98 self.fs_out = fs_out 99 100 if fs_in not in self.SUPPORTED_SOURCE_RATES: 101 raise ValueError(f'SilkUpsampler currently only supports upsampling from {self.SUPPORTED_SOURCE_RATES} Hz') 102 103 104 if fs_out not in self.SUPPORTED_TARGET_RATES: 105 raise ValueError(f'SilkUpsampler currently only supports upsampling to {self.SUPPORTED_TARGET_RATES} Hz') 106 107 108 # hq 2x upsampler as FIR approximation 109 hq_2x_up_even = get_impz(hq_2x_up_c_even, 128)[::-1].copy() 110 hq_2x_up_odd = get_impz(hq_2x_up_c_odd , 128)[::-1].copy() 111 112 self.hq_2x_up_even = nn.Parameter(torch.from_numpy(hq_2x_up_even).float().view(1, 1, -1), requires_grad=False) 113 self.hq_2x_up_odd = nn.Parameter(torch.from_numpy(hq_2x_up_odd ).float().view(1, 1, -1), requires_grad=False) 114 self.hq_2x_up_padding = [127, 0] 115 116 # interpolation filters 117 frac_01_24 = frac_fir[0] 118 frac_17_24 = frac_fir[8] 119 frac_09_24 = frac_fir[4] 120 121 self.frac_01_24 = nn.Parameter(torch.from_numpy(frac_01_24).view(1, 1, -1), requires_grad=False) 122 self.frac_17_24 = nn.Parameter(torch.from_numpy(frac_17_24).view(1, 1, -1), requires_grad=False) 123 self.frac_09_24 = nn.Parameter(torch.from_numpy(frac_09_24).view(1, 1, -1), requires_grad=False) 124 125 self.stride = 1 if fs_out == 48000 else 2 126 127 def hq_2x_up(self, x): 128 129 num_channels = x.size(1) 130 131 weight_even = torch.repeat_interleave(self.hq_2x_up_even, num_channels, 0) 132 weight_odd = torch.repeat_interleave(self.hq_2x_up_odd , num_channels, 0) 133 134 x_pad = F.pad(x, self.hq_2x_up_padding) 135 y_even = F.conv1d(x_pad, weight_even, groups=num_channels) 136 y_odd = F.conv1d(x_pad, weight_odd , groups=num_channels) 137 138 y = torch.cat((y_even.unsqueeze(-1), y_odd.unsqueeze(-1)), dim=-1).flatten(2) 139 140 return y 141 142 def interpolate_3_2(self, x): 143 144 num_channels = x.size(1) 145 146 weight_01_24 = torch.repeat_interleave(self.frac_01_24, num_channels, 0) 147 weight_17_24 = torch.repeat_interleave(self.frac_17_24, num_channels, 0) 148 weight_09_24 = torch.repeat_interleave(self.frac_09_24, num_channels, 0) 149 150 x_pad = F.pad(x, [8, 0]) 151 y_01_24 = F.conv1d(x_pad, weight_01_24, stride=2, groups=num_channels) 152 y_17_24 = F.conv1d(x_pad, weight_17_24, stride=2, groups=num_channels) 153 y_09_24_sh1 = F.conv1d(torch.roll(x_pad, -1, -1), weight_09_24, stride=2, groups=num_channels) 154 155 156 y = torch.cat( 157 (y_01_24.unsqueeze(-1), y_17_24.unsqueeze(-1), y_09_24_sh1.unsqueeze(-1)), 158 dim=-1).flatten(2) 159 160 return y[..., :-3] 161 162 def forward(self, x): 163 164 y_2x = self.hq_2x_up(x) 165 y_3x = self.interpolate_3_2(y_2x) 166 167 return y_3x[:, :, ::self.stride] 168