07-实战项目:百度SDK语音合成

实战项目:使用SDK实现语音合成

SDK的含义和应用

名词解释

  • API(Application Programming Interface)应用程序编程接口
    • 指一些预先定义的函数,目的是提供应用程序与开发人员基于某些软件或硬件得以交互,而无需访问源码或理解内部工作机制的细节
  • SDK(Softwore Developmennt Kit)软件开发工具包
    • 软件工程师为特定的软件包、软件框架、硬件平台、操作系统等建立应用软件时的开发工具的集合。值第三方服务商提供的实现某项功能的工具包

API与SDK的比较

  • API是一个具体的函数,一个确定的功能,已经明确了它的作用。
  • SDK是很多方法的集合体,是一个工具包。
  • SDK除了提供完善的接口,还提供相关的开发环境;API的环境要自己提供

 API与SDK的优劣

属性 优势 劣势
API 开发成本低,对接简单 开发需经过对方平台,对方可获取数据信息
SDK 对接后功能稳定,响应速度快,而且对接平台不会获取相关数据 需要一定的开发、对接工作量

API访问示例

>>> import requests
>>> api = 'https://api.github.com/users/qiwsir'#老齐个人github账号主页
>>> q = requests.get(api)#get方法发出请求
>>> q
<Response [200]>#返回码200,表示访问成功
>>> import pprint#使用pprint模块友好显示访问内容
>>> pprint.pprint(q.json())#pprint方法以json格式显示内容
{'avatar_url': 'https://avatars.githubusercontent.com/u/3646955?v=4',
 'bio': None,
 'blog': 'http://www.itdiffer.com',
 'company': '易水禾软件',
 'created_at': '2013-02-20T11:44:27Z',
 'email': None,
 'events_url': 'https://api.github.com/users/qiwsir/events{/privacy}',
 'followers': 5307,
 'followers_url': 'https://api.github.com/users/qiwsir/followers',
 'following': 25,
 'following_url': 'https://api.github.com/users/qiwsir/following{/other_user}',
 'gists_url': 'https://api.github.com/users/qiwsir/gists{/gist_id}',
 'gravatar_id': '',
 'hireable': True,
 'html_url': 'https://github.com/qiwsir',
 'id': 3646955,
 'location': 'Suzhou China',
 'login': 'qiwsir',
 'name': '老齐',
 'node_id': 'MDQ6VXNlcjM2NDY5NTU=',
 'organizations_url': 'https://api.github.com/users/qiwsir/orgs',
 'public_gists': 0,
 'public_repos': 308,
 'received_events_url': 'https://api.github.com/users/qiwsir/received_events',
 'repos_url': 'https://api.github.com/users/qiwsir/repos',
 'site_admin': False,
 'starred_url': 'https://api.github.com/users/qiwsir/starred{/owner}{/repo}',
 'subscriptions_url': 'https://api.github.com/users/qiwsir/subscriptions',
 'twitter_username': None,
 'type': 'User',
 'updated_at': '2021-02-14T00:46:31Z',
 'url': 'https://api.github.com/users/qiwsir'}

 

 

实现SDK语音合成业务

  • 地址:https://ai.baidu.com/docs#/TTS-Online-Python-SDK/top
  • 安装:pip install baidu-aip --user
  • 百度SDK语音合成前的准备工作
    • 注册,并创建应用

  

    • AppID、API Key、Secret Key的值需要保存以便后续使用
  • 语音合成示例
#!/usr/bin/env python
# -*- coding=utf-8 -*

# SDK使用示例
from aip import AipSpeech

APP_ID = 'your app_id'
API_KEY = 'your api_key'
SECRET_KEY = 'your secret_key'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
s = '非常高兴跟大家一起来学习编程,在这里引用一句话,作为我们的共勉:忘记背后,努力向前,向着标杆直跑。'

sound = client.synthesis(s, 'zh', 1, {
    'vol': 5, # 音量,取值0-15,默认为5中音量
    'per': 3,
    'spd': 6, # 语速,取值0-9,默认为5中语速
    'pit': 3, # 音调,取值0-9,默认为5中语调
})

if not isinstance(sound, dict):
    with open('./tts.mp3', 'wb') as f:
        f.write(sound)
print('ok')
  •  百度语音合成SDK接口文档说明:https://ai.baidu.com/ai-doc/SPEECH/Gk4nlz8tc
posted @ 2021-02-14 09:40  西瓜的春天  阅读(105)  评论(0)    收藏  举报