1""" 2/* Copyright (c) 2023 Amazon 3 Written by Jean-Marc Valin */ 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 torch 31from torch import nn 32import torch.nn.functional as F 33 34# x is (batch, nb_in_channels, nb_frames*frame_size) 35# kernels is (batch, nb_out_channels, nb_in_channels, nb_frames, coeffs) 36def adaconv_kernel(x, kernels, half_window, fft_size=256): 37 device=x.device 38 overlap_size=half_window.size(-1) 39 nb_frames=kernels.size(3) 40 nb_batches=kernels.size(0) 41 nb_out_channels=kernels.size(1) 42 nb_in_channels=kernels.size(2) 43 kernel_size = kernels.size(-1) 44 x = x.reshape(nb_batches, 1, nb_in_channels, nb_frames, -1) 45 frame_size = x.size(-1) 46 # build window: [zeros, rising window, ones, falling window, zeros] 47 window = torch.cat( 48 [ 49 torch.zeros(frame_size, device=device), 50 half_window, 51 torch.ones(frame_size - overlap_size, device=device), 52 1 - half_window, 53 torch.zeros(fft_size - 2 * frame_size - overlap_size,device=device) 54 ]) 55 x_prev = torch.cat([torch.zeros_like(x[:, :, :, :1, :]), x[:, :, :, :-1, :]], dim=-2) 56 x_next = torch.cat([x[:, :, :, 1:, :overlap_size], torch.zeros_like(x[:, :, :, -1:, :overlap_size])], dim=-2) 57 x_padded = torch.cat([x_prev, x, x_next, torch.zeros(nb_batches, 1, nb_in_channels, nb_frames, fft_size - 2 * frame_size - overlap_size, device=device)], -1) 58 k_padded = torch.cat([torch.flip(kernels, [-1]), torch.zeros(nb_batches, nb_out_channels, nb_in_channels, nb_frames, fft_size-kernel_size, device=device)], dim=-1) 59 60 # compute convolution 61 X = torch.fft.rfft(x_padded, dim=-1) 62 K = torch.fft.rfft(k_padded, dim=-1) 63 64 out = torch.fft.irfft(X * K, dim=-1) 65 # combine in channels 66 out = torch.sum(out, dim=2) 67 # apply the cross-fading 68 out = window.reshape(1, 1, 1, -1)*out 69 crossfaded = out[:,:,:,frame_size:2*frame_size] + torch.cat([torch.zeros(nb_batches, nb_out_channels, 1, frame_size, device=device), out[:, :, :-1, 2*frame_size:3*frame_size]], dim=-2) 70 71 return crossfaded.reshape(nb_batches, nb_out_channels, -1)