zxsipola123456 commited on
Commit
c6aa473
1 Parent(s): 88b439a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -5
app.py CHANGED
@@ -39,30 +39,55 @@ async def text_to_speech_edge(text, language_code):
39
  #out_wav = knn_vc.match(query_seq, matching_set, topk=4)
40
  #torchaudio.save('output.wav', out_wav[None], 16000)
41
  #return 'output.wav'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  def voice_change(audio_in, audio_ref):
43
  samplerate1, data1 = wavfile.read(audio_in)
44
  samplerate2, data2 = wavfile.read(audio_ref)
45
 
 
 
 
 
 
 
 
 
46
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_audio_in, \
47
  tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_audio_ref:
48
  audio_in_path = tmp_audio_in.name
49
  audio_ref_path = tmp_audio_ref.name
50
- write(audio_in_path, samplerate1, data1)
51
- write(audio_ref_path, samplerate2, data2)
52
 
53
  query_seq = knn_vc.get_features(audio_in_path)
54
  matching_set = knn_vc.get_matching_set([audio_ref_path])
55
  out_wav = knn_vc.match(query_seq, matching_set, topk=4)
56
  output_path = 'output.wav'
57
- torchaudio.save(output_path, out_wav[None], 16000)
58
  return output_path
59
-
60
  # 文字转语音(OpenAI)
61
  def tts(text, model, voice, api_key):
62
  if len(text) > 300:
63
  raise gr.Error('您输入的文本字符多于300个,请缩短您的文本')
64
  if api_key == '':
65
- raise gr.Error('请填写您的 OpenAI API Key')
66
 
67
  try:
68
  client = OpenAI(api_key=api_key, base_url='https://lmzh.top/v1')
 
39
  #out_wav = knn_vc.match(query_seq, matching_set, topk=4)
40
  #torchaudio.save('output.wav', out_wav[None], 16000)
41
  #return 'output.wav'
42
+ # def voice_change(audio_in, audio_ref):
43
+ # samplerate1, data1 = wavfile.read(audio_in)
44
+ # samplerate2, data2 = wavfile.read(audio_ref)
45
+
46
+ # with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_audio_in, \
47
+ # tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_audio_ref:
48
+ # audio_in_path = tmp_audio_in.name
49
+ # audio_ref_path = tmp_audio_ref.name
50
+ # write(audio_in_path, samplerate1, data1)
51
+ # write(audio_ref_path, samplerate2, data2)
52
+
53
+ # query_seq = knn_vc.get_features(audio_in_path)
54
+ # matching_set = knn_vc.get_matching_set([audio_ref_path])
55
+ # out_wav = knn_vc.match(query_seq, matching_set, topk=4)
56
+ # output_path = 'output.wav'
57
+ # torchaudio.save(output_path, out_wav[None], 16000)
58
+ # return output_path
59
+
60
  def voice_change(audio_in, audio_ref):
61
  samplerate1, data1 = wavfile.read(audio_in)
62
  samplerate2, data2 = wavfile.read(audio_ref)
63
 
64
+ # 强制匹配音频文件的长度
65
+ max_length = max(data1.shape[0], data2.shape[0])
66
+
67
+ if data1.shape[0] < max_length:
68
+ data1 = np.pad(data1, (0, max_length - data1.shape[0]), mode='constant')
69
+ if data2.shape[0] < max_length:
70
+ data2 = np.pad(data2, (0, max_length - data2.shape[0]), mode='constant')
71
+
72
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_audio_in, \
73
  tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_audio_ref:
74
  audio_in_path = tmp_audio_in.name
75
  audio_ref_path = tmp_audio_ref.name
76
+ wavfile.write(audio_in_path, samplerate1, data1)
77
+ wavfile.write(audio_ref_path, samplerate2, data2)
78
 
79
  query_seq = knn_vc.get_features(audio_in_path)
80
  matching_set = knn_vc.get_matching_set([audio_ref_path])
81
  out_wav = knn_vc.match(query_seq, matching_set, topk=4)
82
  output_path = 'output.wav'
83
+ torchaudio.save(output_path, torch.tensor(out_wav)[None], 16000)
84
  return output_path
 
85
  # 文字转语音(OpenAI)
86
  def tts(text, model, voice, api_key):
87
  if len(text) > 300:
88
  raise gr.Error('您输入的文本字符多于300个,请缩短您的文本')
89
  if api_key == '':
90
+ raise gr.Error('请填写您的 中转API Key')
91
 
92
  try:
93
  client = OpenAI(api_key=api_key, base_url='https://lmzh.top/v1')