Charles未晞 发表于 2021-8-31 07:59:51

【回复送鱼币啦】带大家玩点有意思的东西呀,一起来实现一波语音在线点歌小程序呗~

原文链接
https://mp.weixin.qq.com/s/dCoEmwZq8ZZDVfXISc-bMQ

导语
之前更新了一波音乐下载器:
喜欢爬虫的小伙伴们有福啦,手把手带大家分析一波百度音乐接口呗~
结果repo竟然只涨了一个star,太可怕了。图片为了吸引大家给我点赞,我决定带大家玩点更有意思的东西,在之前开源的音乐下载器的基础上,实现语音点歌功能,废话不多说,让我们愉快地开始吧~

原理简介
既然要实现语音在线点歌功能,首先当然要实现语音识别功能啦,这里我们主要借助百度提供的免费接口:
https://console.bce.baidu.com/ai/?fromai=1#/ai/speech/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']
      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)
大功告成啦,完整源代码详见:
**** Hidden Message *****

Charles未晞 发表于 2021-9-1 23:19:33

图好像没显示。。。原文可以看这里https://mp.weixin.qq.com/s/dCoEmwZq8ZZDVfXISc-bMQ

Charles未晞 发表于 2021-9-1 10:59:44

顶一下

hrpzcf 发表于 2021-9-1 11:09:56

居然没有人

hornwong 发表于 2021-9-1 11:13:30

{:5_95:}

晴雨皆宜 发表于 2021-9-1 11:18:02

来啦,来啦,领鱼币啦{:5_109:}

糖甜弯了嘴 发表于 2021-9-1 12:56:34

大佬✧٩(ˊωˋ*)و✧

小伤口 发表于 2021-9-1 12:57:54

再来评一次,学习了,好好研究研究

gavinduo 发表于 2021-9-1 13:21:10

领鱼币啦

muder 发表于 2021-9-1 14:46:34

好的

咳嗽水 发表于 2021-9-1 15:15:37

来啦,来啦,领鱼币啦

不大不小甲鱼 发表于 2021-9-1 15:30:26

{:5_109:}

ILY39 发表于 2021-9-1 16:09:13

{:10_281:}

王跌宕 发表于 2021-9-1 20:00:01

围观{:10_256:}

伯劳劳 发表于 2021-9-1 22:31:20

支持

某一天 发表于 2021-9-2 02:26:43

楼主千金散尽还复来

trolwy 发表于 2021-9-2 06:56:36

挣鱼币做作业

夏季在下季 发表于 2021-9-2 09:22:02

学习学习{:5_109:}

yhdmy 发表于 2021-9-2 13:37:49

领鱼币啦

番杰 发表于 2021-9-2 15:18:54

优秀
页: [1] 2 3 4 5 6 7 8 9
查看完整版本: 【回复送鱼币啦】带大家玩点有意思的东西呀,一起来实现一波语音在线点歌小程序呗~