语⾳处理-VC(voiceconversion)语⾳转换_API实现
代码为标贝开源的资源 来实现语⾳的转换VC
简单介绍:
需要⼀批VC的数据,通过现存的接⼝来进⾏调⽤达到⽩嫖数据,⾄此本⽂章内容就出来了,主要的⽂档格式 还是⼈家API的包含,⾃⼰进⾏相关的信息的修改,增加了⽂件批量化的修改,参数的量化等等等等~
有问题欢迎来讨论
# -*- coding: utf-8
import argparse
import json
import wave
from scipy.io import wavfile
import sys
import requests
import websocket
from pathlib import Path
import fnmatch
import os
import time
'''
* 发⾳⼈列表
*  邻家⼥声_静静 Vc_jingjing
*  优雅⼥声_娇娇 Vc_jiaojiao
*  绅⼠男⽣_天天 Vc_tiantian
*  恐龙贝克_童声 Vc_baklong
*  可爱⼥声_未眠 Vc_weimian
'''
getsavefilename
class Client:
def __init__(self, data, uri, save_path):
self.data = data
self.uri = uri
self.save_path = save_path
# 建⽴连接
def connect(self):
ws_app = websocket.WebSocketApp(uri,
on__open,
on__message,
on__error,
on__close)
ws_app.run_forever()
# 建⽴连接后发送消息
def on_open(self, ws):
print("sending..")
for message in self.data:
ws.send(message, websocket.ABNF.OPCODE_BINARY)
# 接收消息
def on_message(self, ws, message):
length = int.from_bytes(message[:4], byteorder='big', signed=False)
json_data = json.loads((message[4: length + 4]).decode())
#json_data = json.loads((message[4: length + 4]))
if json_data['lastpkg']:
with wave.open(self.save_path, 'wb') as wavfile:
wavfile.setparams((1, 2, 16000, 0, 'NONE', 'NONE'))
wavfile.verted_data)
ws.close()
print("task finished successfully")
code = json.loads(message).get("errcode")
print(str(json.loads(message)))
if code != 0:
# 打印接⼝错误
print(message)
# 打印错误
def on_error(slef, ws, error):
print("error: ", str(error))
# 关闭连接
def on_close(ws):
print("client closed.")
# 准备数据
def prepare_data(args, access_token):
# 填写Header信息
voice_name = args.voice_name
with open(args.file_path, 'rb') as f:
file = f.read()
data = []
for i in range(0, len(file), 32000):
if i + 32000 > len(file):
tts_params = {"access_token": access_token, "voice_name": voice_name, 'enable_vad': True,                          'align_input': True, "lastpkg": True}
else:
tts_params = {"access_token": access_token, "voice_name": voice_name, 'enable_vad': True,                          'align_input': True, "lastpkg": False}
json_data = json.dumps(tts_params)
json_data_bi = de()
length = len(json_data)
head_data = _bytes(4, byteorder='big')
if i + 32000 > len(file):
data.append(head_data + json_data_bi + file[i:])
else:
data.append(head_data + json_data_bi + file[i: i + 32000])
return data
# 获取命令⾏输⼊参数
def get_args(file_path,file_save_path,id,secert):
text = "北京的天因为有你才会变得这么美丽好看"
parser = argparse.ArgumentParser(description='ASR')
parser.add_argument('-client_secret', type=str, default=secert)
parser.add_argument('-client_id', type=str, default=id)
parser.add_argument('-file_path', type=str, default=file_path)
parser.add_argument('-file_save_path', type=str, default=file_save_path)
parser.add_argument('--voice_name', type=str, default='Vc_tiantian')
args = parser.parse_args()
return args
# 获取access_token⽤于鉴权
def get_access_token(client_secret, client_id):
grant_type = "client_credentials"
url = "openapi.data-baker/oauth/2.0/token?grant_type={}&client_secret={}&client_id={}" \        .format(grant_type, client_secret, client_id)
response = requests.post(url)
try:
#response = requests.post(url)
response.raise_for_status()
except Exception as e:
)
raise Exception
else:
access_token = json.).get('access_token')
return access_token
def find_all_files(files_path):
"""遍历指定⽂件夹所有指定类型⽂件"""
p = Path(files_path)
files_names = []  # 存储⽂件路径名称
for file lob('*.wav'):  # 寻所有wav⽂件
x = str(file).split('\\')[-1]
if fnmatch.fnmatch(x, '._*.wav'):
continue
else:
files_names.append(str(file))  # 以字符串形式保存
return files_names
if __name__ == '__main__':
try:
'''
⾸先下进⾏批量化数据的导⼊
⽂件存储位置:
G:/1k
⽂件读取位置
G:/VC
先读取所有的⽂件位置路径
id和secret需要做⼀批量化的替换⼯作
'''
file_names = find_all_files('G:/1k')
id = 'appid⾃⼰填'
secret='appserect⾃⼰填'
#获取所有的⽂件索引后,直接开始继续做之前的事情
count = 0
access_token = get_access_token(secret, id)
#print(access_token)
for filename in enumerate(file_names):
#i是单独⼀个⽂件的名称
print(type(filename))
source_file_name = filename[1]
file_save_name = filename[1].replace("1k", "VC")
wave_name = filename[1].split('\\')[-1]
file_save_package= file_place("\\"+wave_name,'')
print(file_save_name)
if (ists(file_save_package)):
print('1')
file = open(file_save_name, 'w')
file.close()
#wavfile.write(wave_name,16000,None)
else:
os.makedirs(file_save_package)
file = open(file_save_name, 'w')
file.close()
# wavfile.write(wave_name)
args = get_args(filename[1],file_save_name,id=id,secert=secret)            # 获取access_token
client_secret = args.client_secret
client_id = args.client_id
#access_token = get_access_token(client_secret, client_id)
#print(access_token)
# 准备数据
data = prepare_data(args, access_token)
uri = "wss://openapi.data-baker/ws/voice_conversion"
# 建⽴Websocket连接
client = Client(data, uri, args.file_save_path)
#成功后直接将之前的⽂件进⾏删除⼯作避免出现重复的情况
#打印当前⽂件的进度,并且记录下来
count+=1
#count_str = count+" "
print('当前⽂件'+source_file_name+'当前次数'+str(count))
if ists(filename[1]):  # 原⽂件存在
# 删除⽂件,可使⽤以下两种⽅法。
print('⽂件已经删除了')
# os.unlink(path)
else:
print('no such file:%s' % filename[1])
except Exception as e:
print(e)
欢迎交流

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。