• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 os
31
32from torch.utils.data import Dataset
33import numpy as np
34
35from utils.silk_features import silk_feature_factory
36from utils.pitch import hangover, calculate_acorr_window
37
38
39class SilkEnhancementSet(Dataset):
40    def __init__(self,
41                 path,
42                 frames_per_sample=100,
43                 no_pitch_value=256,
44                 preemph=0.85,
45                 skip=91,
46                 acorr_radius=2,
47                 pitch_hangover=8,
48                 num_bands_clean_spec=64,
49                 num_bands_noisy_spec=18,
50                 noisy_spec_scale='opus',
51                 noisy_apply_dct=True,
52                 add_double_lag_acorr=False,
53                 ):
54
55        assert frames_per_sample % 4 == 0
56
57        self.frame_size = 80
58        self.frames_per_sample = frames_per_sample
59        self.no_pitch_value = no_pitch_value
60        self.preemph = preemph
61        self.skip = skip
62        self.acorr_radius = acorr_radius
63        self.pitch_hangover = pitch_hangover
64        self.num_bands_clean_spec = num_bands_clean_spec
65        self.num_bands_noisy_spec = num_bands_noisy_spec
66        self.noisy_spec_scale = noisy_spec_scale
67        self.add_double_lag_acorr = add_double_lag_acorr
68
69        self.lpcs = np.fromfile(os.path.join(path, 'features_lpc.f32'), dtype=np.float32).reshape(-1, 16)
70        self.ltps = np.fromfile(os.path.join(path, 'features_ltp.f32'), dtype=np.float32).reshape(-1, 5)
71        self.periods = np.fromfile(os.path.join(path, 'features_period.s16'), dtype=np.int16)
72        self.gains = np.fromfile(os.path.join(path, 'features_gain.f32'), dtype=np.float32)
73        self.num_bits = np.fromfile(os.path.join(path, 'features_num_bits.s32'), dtype=np.int32)
74        self.num_bits_smooth = np.fromfile(os.path.join(path, 'features_num_bits_smooth.f32'), dtype=np.float32)
75
76        self.clean_signal_hp = np.fromfile(os.path.join(path, 'clean_hp.s16'), dtype=np.int16)
77        self.clean_signal    = np.fromfile(os.path.join(path, 'clean.s16'), dtype=np.int16)
78        self.coded_signal    = np.fromfile(os.path.join(path, 'coded.s16'), dtype=np.int16)
79
80        self.create_features = silk_feature_factory(no_pitch_value,
81                                                    acorr_radius,
82                                                    pitch_hangover,
83                                                    num_bands_clean_spec,
84                                                    num_bands_noisy_spec,
85                                                    noisy_spec_scale,
86                                                    noisy_apply_dct,
87                                                    add_double_lag_acorr)
88
89        self.history_len = 700 if add_double_lag_acorr else 350
90        # discard some frames to have enough signal history
91        self.skip_frames = 4 * ((skip + self.history_len + 319) // 320 + 2)
92
93        num_frames = self.clean_signal_hp.shape[0] // 80 - self.skip_frames
94
95        self.len = num_frames // frames_per_sample
96
97    def __len__(self):
98        return self.len
99
100    def __getitem__(self, index):
101
102        frame_start = self.frames_per_sample * index + self.skip_frames
103        frame_stop  = frame_start + self.frames_per_sample
104
105        signal_start = frame_start * self.frame_size - self.skip
106        signal_stop  = frame_stop  * self.frame_size - self.skip
107
108        clean_signal_hp = self.clean_signal_hp[signal_start : signal_stop].astype(np.float32) / 2**15
109        clean_signal    = self.clean_signal[signal_start : signal_stop].astype(np.float32) / 2**15
110        coded_signal    = self.coded_signal[signal_start : signal_stop].astype(np.float32) / 2**15
111
112        coded_signal_history = self.coded_signal[signal_start - self.history_len : signal_start].astype(np.float32) / 2**15
113
114        features, periods = self.create_features(
115              coded_signal,
116              coded_signal_history,
117              self.lpcs[frame_start : frame_stop],
118              self.gains[frame_start : frame_stop],
119              self.ltps[frame_start : frame_stop],
120              self.periods[frame_start : frame_stop]
121        )
122
123        if self.preemph > 0:
124            clean_signal[1:] -= self.preemph * clean_signal[: -1]
125            clean_signal_hp[1:] -= self.preemph * clean_signal_hp[: -1]
126            coded_signal[1:] -= self.preemph * coded_signal[: -1]
127
128        num_bits        = np.repeat(self.num_bits[frame_start // 4 : frame_stop // 4], 4).astype(np.float32).reshape(-1, 1)
129        num_bits_smooth = np.repeat(self.num_bits_smooth[frame_start // 4 : frame_stop // 4], 4).astype(np.float32).reshape(-1, 1)
130
131        numbits = np.concatenate((num_bits, num_bits_smooth), axis=-1)
132
133        return {
134            'features'    : features,
135            'periods'     : periods.astype(np.int64),
136            'target_orig' : clean_signal.astype(np.float32),
137            'target'      : clean_signal_hp.astype(np.float32),
138            'signals'     : coded_signal.reshape(-1, 1).astype(np.float32),
139            'numbits'     : numbits.astype(np.float32)
140            }
141