马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
原文链接
https://mp.weixin.qq.com/s/dCoEmwZq8ZZDVfXISc-bMQ
导语
之前更新了一波音乐下载器:
喜欢爬虫的小伙伴们有福啦,手把手带大家分析一波百度音乐接口呗~
结果repo竟然只涨了一个star,太可怕了。图片为了吸引大家给我点赞,我决定带大家玩点更有意思的东西,在之前开源的音乐下载器的基础上,实现语音点歌功能,废话不多说,让我们愉快地开始吧~
原理简介
既然要实现语音在线点歌功能,首先当然要实现语音识别功能啦,这里我们主要借助百度提供的免费接口:
https://console.bce.baidu.com/ai ... eech/overview/index
具体而言就是注册登录之后,创建一个应用:
根据提示填写一些信息即可,一分钟就搞定了,创建完成之后,可以获得如下截图中的内容:
AppID,AppKey以及SecretKey被我打码了,我们需要使用这些参数的来调用百度的语音识别接口,具体而言,代码实现如下:'''语音识别模块'''
class SpeechRecognition():
def __init__(self, app_id, api_key, secret_key, **kwargs):
from aip import AipSpeech
self.aipspeech_api = AipSpeech(app_id, api_key, secret_key)
self.speech_path = kwargs.get('speech_path', 'recording.wav')
assert self.speech_path.endswith('.wav'), 'only support audio with wav format'
'''录音'''
def record(self, sample_rate=16000):
import speech_recognition as sr
rec = sr.Recognizer()
with sr.Microphone(sample_rate=sample_rate) as source:
audio = rec.listen(source)
with open(self.speech_path, 'wb') as fp:
fp.write(audio.get_wav_data())
'''识别'''
def recognition(self):
assert os.path.exists(self.speech_path)
with open(self.speech_path, 'rb') as fp:
content = fp.read()
result = self.aipspeech_api.asr(content, 'wav', 16000, {'dev_pid': 1536})
text = result['result'][0]
return text
'''合成并说话'''
def synthesisspeak(self, text=None, audiopath=None):
assert text is None or audiopath is None
import pygame
if audiopath is None:
audiopath = f'recording_{time.time()}.mp3'
result = self.aipspeech_api.synthesis(
text, 'zh', 1,
{'spd': 4, 'vol': 5, 'per': 4}
)
if not isinstance(result, dict):
with open(audiopath, 'wb') as fp:
fp.write(result)
pygame.mixer.init()
pygame.mixer.music.load(audiopath)
pygame.mixer.music.play()
else:
pygame.mixer.init()
pygame.mixer.music.load(audiopath)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
time.sleep(15)
代码是从官方示例代码改过来的,实名认证之后可以获得十几万次的免费接口调用额度,需要自己手动点击领取~
定义完语音识别模块,我们就可以开始实现我们的语音点歌功能啦。这个实现起来其实也很简单,只需要把之前的代码改成语音输入和语音提示就ok啦,代码如下:'''非开发人员外部调用-语音版'''
def runbyspeech(self, target_srcs=None, baiduspeech_params=None):
assert baiduspeech_params is not None, 'please visit to https://console.bce.baidu.com/ai/?fromai=1#/ai/speech/overview/index to obtain AppID, AppKey and SecretKey'
sr_api = SpeechRecognition(**baiduspeech_params)
while True:
print(BASICINFO % (__version__, self.config.get('savedir')))
# 音乐搜索
sr_api.synthesisspeak('请问您想听的歌曲名是什么呢?')
time.sleep(4)
target_srcs = ['migu'] if target_srcs is None else target_srcs
sr_api.record()
user_input = sr_api.recognition()
self.logger_handle.info(f'识别结果为: {user_input}')
search_results = self.search(user_input, target_srcs)
# 音乐下载
songinfos, songpaths = [], []
for key, values in search_results.items():
for value in values:
songinfos.append(value)
songpaths.append(os.path.join(value['savedir'], value['savename']+'.'+value['ext']))
sr_api.synthesisspeak(f'共搜索到{len(songinfos)}首和{user_input}相关的歌曲, 将依次为您下载播放.')
self.download(songinfos)
# 音乐播放
for songpath in songpaths:
sr_api.synthesisspeak(audiopath=songpath)
大功告成啦,完整源代码详见:
|