streaming_merge. It merges the frame-level processed audio chunks in the streaming *simulation*. It is noted that, in real applications, the processed audio should be sent to the output channel frame by frame. You may refer to this function to manage your streaming output buf
(self, chunks, ilens=None)
| 158 | return output_wav * self._get_window_func() |
| 159 | |
| 160 | def streaming_merge(self, chunks, ilens=None): |
| 161 | """streaming_merge. It merges the frame-level processed audio chunks |
| 162 | in the streaming *simulation*. It is noted that, in real applications, |
| 163 | the processed audio should be sent to the output channel frame by frame. |
| 164 | You may refer to this function to manage your streaming output buffer. |
| 165 | |
| 166 | Args: |
| 167 | chunks: List [(B, frame_size),] |
| 168 | ilens: [B] |
| 169 | Returns: |
| 170 | merge_audio: [B, T] |
| 171 | """ # noqa: H405 |
| 172 | |
| 173 | frame_size = self.win_length |
| 174 | hop_size = self.hop_length |
| 175 | |
| 176 | num_chunks = len(chunks) |
| 177 | batch_size = chunks[0].shape[0] |
| 178 | audio_len = int(hop_size * num_chunks + frame_size - hop_size) |
| 179 | |
| 180 | output = torch.zeros((batch_size, audio_len), dtype=chunks[0].dtype).to( |
| 181 | chunks[0].device |
| 182 | ) |
| 183 | |
| 184 | for i, chunk in enumerate(chunks): |
| 185 | output[:, i * hop_size : i * hop_size + frame_size] += chunk |
| 186 | |
| 187 | window_sq = self._get_window_func().pow(2) |
| 188 | window_envelop = torch.zeros((batch_size, audio_len), dtype=chunks[0].dtype).to( |
| 189 | chunks[0].device |
| 190 | ) |
| 191 | for i in range(len(chunks)): |
| 192 | window_envelop[:, i * hop_size : i * hop_size + frame_size] += window_sq |
| 193 | output = output / window_envelop |
| 194 | |
| 195 | # We need to trim the front padding away if center. |
| 196 | start = (frame_size // 2) if self.center else 0 |
| 197 | end = -(frame_size // 2) if ilens.max() is None else start + ilens.max() |
| 198 | |
| 199 | return output[..., start:end] |
| 200 | |
| 201 | |
| 202 | if __name__ == "__main__": |