AI语音合成:一个b站的问答主播实现过程

一个b站的问答主播(实验)

思路:

  • 参照:手把手教你打造自己的 AI 虚拟主播 https://blog.csdn.net/weixin_53072519/article/details/130872710 ,在b站开启了生平第一次直播,按帖子和GPT-vup项目的说明操作,直播没有问题,但或许控制表情、语音、模型操作,都失败,或许是openai的key无效,或许是其他原因,总之未有成功。
  • GPT-vup项目github:https://github.com/jiran214/GPT-vup
  • 再参照:写个AI虚拟主播:看懂弹幕,妙语连珠,悲欢形于色,以一种简单的实现 https://blog.csdn.net/u012419550/article/details/129254795 ,准备按其操作进行一番实战。考虑到其文章中需要涉及的组件很多,极大增加了项目失败的可能性。
  • 故考虑只使用其中的读取弹幕模块、chatgpt模块、文本转语音模块,构建一个简单的读取用户的弹幕,然后文本转为语音,通过直播形象的语音输出给直播间用户。
  • 完成此基本功能之后,再考虑使用文章中的模块来控制模型形象的嘴型、动作、表情等。

过程:


– 20230913

  • 获取b站直播间弹幕,这里使用bilibili-api-python库 https://pypi.org/project/bilibili-api-python/ ,其直播间相关API示例:https://nemo2011.github.io/bilibili-api/#/examples/live
  • 对弹幕文字的回复,准备使用chatgpt,但由于网络原因,这里直接采用讯飞的星火,参考官方文档:https://www.xfyun.cn/doc/platform/quickguide.html ,使用前记得要先下单购买tokens
  • 将文字转语音tts,仍然使用讯飞服务:https://console.xfyun.cn/services/tts
  • 让vts接收电脑播放的音频,这里使用VB-CABLE,设置虚拟声卡的方式,让vts接收电脑播放的声音:https://vb-audio.com/Cable/
  • vts自带有音频驱动口型,所以这里用python播放转换好的音频,直播间就能看到图像和声音了!
  • 关于vts的设置,参考:https://www.bilibili.com/video/BV1jf4y1p743

– 20230914

  • 讯飞的免费语音实在太机械了,受不了,于是准备本机训练一个tts模型来播放语音。
  • 参考:https://juejin.cn/post/6872260516966842382
  • Real-Time-Voice-Cloning项目:https://github.com/CorentinJ/Real-Time-Voice-Cloning
  • 安装PyTorch,由于我本机是Intel核显,所以用CPU版本:pip install torch torchvision torchaudio
  • 安装项目需要的依赖:pip install -r requirements.txt
  • 注意,这里由于本机原来是python3.10的环境,Real-Time-Voice-Cloning要求3.7,我这里切换为3.8.9版本,以便后续运行另一个项目
  • 最终经过切换python版本,重新安装依赖包,多次调整后,确定能运行版requirements.txt的配置如下:
inflect==5.3.0
librosa==0.8.1
matplotlib==3.5.1
numpy==1.20.3
numba===0.51.2
Pillow==10.0.0
PyQt5==5.15.6
scikit-learn==1.0.2
scipy==1.7.3
sounddevice==0.4.3
SoundFile==0.10.3.post1
tqdm==4.66.1
umap-learn==0.5.2
Unidecode==1.3.2
urllib3==1.26.7
visdom==0.1.8.9
webrtcvad==2.0.10
lws==1.2.7
pysoundfile==0.9.0.post1
  • 在项目下新建saved_models\default目录,下载预训练的模型放入该目录中
  • 找到参考语音音频,要求要wav或者flac格式,编写代码进行生成:
import os.path

from IPython.core.display_functions import display
from IPython.display import Audio
from IPython.utils import io
from synthesizer.inference import Synthesizer
from encoder import inference as encoder
from vocoder import inference as vocoder
from pathlib import Path
import numpy as np
import librosa
import soundfile as sf

'''加载模型'''
encoder_weights = Path("../saved_models/default/encoder.pt")
vocoder_weights = Path("../saved_models/default/vocoder.pt")
syn_dir = Path("../saved_models/default/synthesizer.pt")
encoder.load_model(encoder_weights)
synthesizer = Synthesizer(syn_dir)
vocoder.load_model(vocoder_weights)


def synth(text):
    '''根据文本,目标音频,调用模型合成语音'''
    #in_fpath = Path("./target-1.wav")
    in_fpath = os.path.join(r'E:\\Real-Time-Voice-Cloning\调用模型合成语音','652-130726-0004.flac')
    reprocessed_wav = encoder.preprocess_wav(in_fpath)
    original_wav, sampling_rate = librosa.load(in_fpath)
    preprocessed_wav = encoder.preprocess_wav(original_wav, sampling_rate)
    embed = encoder.embed_utterance(preprocessed_wav)
    print("Synthesizing new audio...")
    with io.capture_output() as captured:
        specs = synthesizer.synthesize_spectrograms([text], [embed])
    generated_wav = vocoder.infer_waveform(specs[0])
    generated_wav = np.pad(generated_wav, (0, synthesizer.sample_rate), mode="constant")
    #librosa.output.write_wav("output_trump_voice.wav", generated_wav, synthesizer.sample_rate)
    sf.write('output_trump_voice.wav', generated_wav, synthesizer.sample_rate, 'PCM_24')
    display(Audio(generated_wav, rate=synthesizer.sample_rate))


synth("oh......Hello, hahahahahaha,  haven't seen you for a long time, how are you? what are you busy with recently?")

最终的效果:英文的声音还行,而且取决于你给的参考语音的效果好坏。

中文的稀碎......这个模型就不认识中文......


猜你喜欢

转载自blog.csdn.net/AJian759447583/article/details/132901151
今日推荐