CosyVoice commited on
Commit
f65eca6
1 Parent(s): cd26f11

add speech fade in out

Browse files
cosyvoice/cli/model.py CHANGED
@@ -40,6 +40,8 @@ class CosyVoiceModel:
40
  # hift cache
41
  self.mel_cache_len = 20
42
  self.source_cache_len = int(self.mel_cache_len * 256)
 
 
43
  # rtf and decoding related
44
  self.stream_scale_factor = 1
45
  assert self.stream_scale_factor >= 1, 'stream_scale_factor should be greater than 1, change it according to your actual rtf'
@@ -50,7 +52,6 @@ class CosyVoiceModel:
50
  self.llm_end_dict = {}
51
  self.mel_overlap_dict = {}
52
  self.hift_cache_dict = {}
53
- self.speech_window = np.hamming(2 * self.source_cache_len)
54
 
55
  def load(self, llm_model, flow_model, hift_model):
56
  self.llm.load_state_dict(torch.load(llm_model, map_location=self.device))
@@ -117,10 +118,9 @@ class CosyVoiceModel:
117
  tts_speech, tts_source = self.hift.inference(mel=tts_mel, cache_source=hift_cache_source)
118
  if self.hift_cache_dict[uuid] is not None:
119
  tts_speech = fade_in_out(tts_speech, self.hift_cache_dict[uuid]['speech'], self.speech_window)
120
- self.hift_cache_dict[uuid] = {
121
- 'mel': tts_mel[:, :, -self.mel_cache_len:],
122
- 'source': tts_source[:, :, -self.source_cache_len:],
123
- 'speech': tts_speech[:, -self.source_cache_len:]}
124
  tts_speech = tts_speech[:, :-self.source_cache_len]
125
  else:
126
  if speed != 1.0:
 
40
  # hift cache
41
  self.mel_cache_len = 20
42
  self.source_cache_len = int(self.mel_cache_len * 256)
43
+ # speech fade in out
44
+ self.speech_window = np.hamming(2 * self.source_cache_len)
45
  # rtf and decoding related
46
  self.stream_scale_factor = 1
47
  assert self.stream_scale_factor >= 1, 'stream_scale_factor should be greater than 1, change it according to your actual rtf'
 
52
  self.llm_end_dict = {}
53
  self.mel_overlap_dict = {}
54
  self.hift_cache_dict = {}
 
55
 
56
  def load(self, llm_model, flow_model, hift_model):
57
  self.llm.load_state_dict(torch.load(llm_model, map_location=self.device))
 
118
  tts_speech, tts_source = self.hift.inference(mel=tts_mel, cache_source=hift_cache_source)
119
  if self.hift_cache_dict[uuid] is not None:
120
  tts_speech = fade_in_out(tts_speech, self.hift_cache_dict[uuid]['speech'], self.speech_window)
121
+ self.hift_cache_dict[uuid] = {'mel': tts_mel[:, :, -self.mel_cache_len:],
122
+ 'source': tts_source[:, :, -self.source_cache_len:],
123
+ 'speech': tts_speech[:, -self.source_cache_len:]}
 
124
  tts_speech = tts_speech[:, :-self.source_cache_len]
125
  else:
126
  if speed != 1.0:
cosyvoice/utils/common.py CHANGED
@@ -139,7 +139,6 @@ def fade_in_out(fade_in_mel, fade_out_mel, window):
139
  device = fade_in_mel.device
140
  fade_in_mel, fade_out_mel = fade_in_mel.cpu(), fade_out_mel.cpu()
141
  mel_overlap_len = int(window.shape[0] / 2)
142
-
143
  fade_in_mel[..., :mel_overlap_len] = fade_in_mel[..., :mel_overlap_len] * window[:mel_overlap_len] + \
144
  fade_out_mel[..., -mel_overlap_len:] * window[mel_overlap_len:]
145
  return fade_in_mel.to(device)
 
139
  device = fade_in_mel.device
140
  fade_in_mel, fade_out_mel = fade_in_mel.cpu(), fade_out_mel.cpu()
141
  mel_overlap_len = int(window.shape[0] / 2)
 
142
  fade_in_mel[..., :mel_overlap_len] = fade_in_mel[..., :mel_overlap_len] * window[:mel_overlap_len] + \
143
  fade_out_mel[..., -mel_overlap_len:] * window[mel_overlap_len:]
144
  return fade_in_mel.to(device)