---
title: " 使用Google语音识别引擎（Google Speech API）"
author: "kingj"
date: "2013-05-30T10:41:59Z"
tags: []
categories: ["编程语言"]
slug: "google-google-speech-api"
draft: false
lastmod: "2013-05-30T10:41:59Z"
id: 1879855
url: "https://kingj.iteye.com/blog/1879855"
---

本文转自csdn

[使用Google语音识别引擎（Google Speech API）\[3月5日修改\]](http://blog.csdn.net/dlangu0393/article/details/7214728)

分类： [Qt](http://blog.csdn.net/dlangu0393/article/category/1067548) [Google](http://blog.csdn.net/dlangu0393/article/category/1067549) [语音识别](http://blog.csdn.net/dlangu0393/article/category/1067550) [API](http://blog.csdn.net/dlangu0393/article/category/1067551) [C++](http://blog.csdn.net/dlangu0393/article/category/1067552) 2012-01-24 17:56 19352人阅读 [评论](http://blog.csdn.net/dlangu0393/article/details/7214728#comments)(72) [收藏]() [举报](http://blog.csdn.net/dlangu0393/article/details/7214728#report "举报")

[google](http://blog.csdn.net/tag/details.html?tag=google)[api](http://blog.csdn.net/tag/details.html?tag=api)[引擎](http://blog.csdn.net/tag/details.html?tag=%E5%BC%95%E6%93%8E)[header](http://blog.csdn.net/tag/details.html?tag=header)[input](http://blog.csdn.net/tag/details.html?tag=input)[chrome](http://blog.csdn.net/tag/details.html?tag=chrome)

    最近在使用Qt编写一个客户端程序的时候需要增加语音识别输入的功能。起初尝试使用SAPI来完成这个任务，但是发现SAPI不仅使用起来超级复杂，而且识别效果也很惨烈。于是就需要寻找一个更加便捷优秀的方案。

    自从Chrome 11开始，Chrome开始支持HTML5的语音输入API，QQ紧接着也推出了语音识别输入（可以看做跟风么:D)。显然这些识别操作不可能在本地完成，那么我们就有直接利用接口的可能。

    对Chromium的repo进行搜索之后，终于找到了Chromium对语音识别的实现代码：

 

[http://src.chromium.org/viewvc/chrome/trunk/src/content/browser/speech/](http://src.chromium.org/viewvc/chrome/trunk/src/content/browser/speech/)

    **分析过程就算了，直接上成果。**

    Chromium先从mic获取音频，**然后使用flac或者speex进行编码**，直接通过HTTPS POST到服务器。接口地址如下：

    **[https://www.google.com/speech-api/v1/recognize](http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN&maxresults=1)**

    Chromium在请求时还会拼上很多参数：

 

    [**xjerr=1&client=chromium&lang=en-US&maxresults=1**](http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN&maxresults=1)

    注：参数解释

        **xjerr**=1    # 不详，猜测为错误的标准

        **client**=chromium    # 客户端类型，这里是Chromium，猜测Chrome也应该可行，估计是作为统计用的。

        **lang**=en-US    # 语言类型，这里是英文，中文为zh-CN，其余语言代码参考：[http://msdn.microsoft.com/en-us/library/ms533052(v=vs.85).aspx](http://msdn.microsoft.com/en-us/library/ms533052%28v=vs.85%29.aspx)

        **maxresults**=1    # 最大返回结果数量，多个结果在hypotheses列表中保存。

    参数很明了，这给我们提供了很多便利。我们对参数进行调整，得到如下的接口地址：

    [**http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN&maxresults=1**](http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN&maxresults=1)

    

    接下来祭出wget，对已经发现的接口进行测试：

 

**\[plain]** [view plain](http://blog.csdn.net/dlangu0393/article/details/7214728 "view plain")[copy](http://blog.csdn.net/dlangu0393/article/details/7214728 "copy")[print](http://blog.csdn.net/dlangu0393/article/details/7214728 "print")[?](http://blog.csdn.net/dlangu0393/article/details/7214728 "?")

1. flac.exe -8 -f --sample-rate=16000 speechInput.wav

**\[plain]** [view plain](http://blog.csdn.net/dlangu0393/article/details/7214728 "view plain")[copy](http://blog.csdn.net/dlangu0393/article/details/7214728 "copy")[print](http://blog.csdn.net/dlangu0393/article/details/7214728 "print")[?](http://blog.csdn.net/dlangu0393/article/details/7214728 "?")

1. wget -O "GoogleSpeechAPI.txt" --user-agent="Mozilla/5.0" --post-file=test.flac --header="Content-Type: audio/x-flac; rate=16000" "http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN&maxresults=1"

    结果如下：

 

**\[javascript]** [view plain](http://blog.csdn.net/dlangu0393/article/details/7214728 "view plain")[copy](http://blog.csdn.net/dlangu0393/article/details/7214728 "copy")[print](http://blog.csdn.net/dlangu0393/article/details/7214728 "print")[?](http://blog.csdn.net/dlangu0393/article/details/7214728 "?")

01. {
02. "status":0,    /* 结果代码，详细见本文结尾 \*/
03. "id":"c421dee91abe31d9b8457f2a80ebca91-1",    /* 识别编号 \*/
04. "hypotheses":    /* 假设，即结果 \*/
05. [
06. {
07. "utterance":"下午好",    /* 话语 \*/
08. "confidence":0.2507637    /* 信心，即准确度 \*/
09. }
10. ]
11. }

 

    **注：注释后为手工添加的结果解释**

    返回结果太明了了！直接就能拿来用了不是~ 返回的**编码是UTF-8**。

    **对于编码格式，在测试中使用了FLAC编码，采样率为16kHz，经测试其他采样率同样可用，但一定要保证Header里的rate与实际数据相符。（关于其他格式的实验请看本文底部。）**

 

 

    **总结：**

    **1、基本流程：**

> **一、从音频输入设备获取原始数据。**
> 
> **二、对原始数据进行包装、编码。**
> 
> **三、将编码后的音频POST至接口地址。**
> 
> **四、分析处理接口返回的JSON并得出结果。**

    **2、请求接口**

> **地址：[http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN&maxresults=1](http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN&maxresults=1)**
> 
> **请求方式：HTTP POST**
> 
> **头部信息：Content-Type: audio/x-flac; rate=16000   （注：Content-Type根据所使用的编码格式不同而不同，详见文章底部。rate为音频采样率。）**
> 
> **请求数据：编码后的音频数据**

    **3、音频编码格式：**

> **FLAC或WAV或SPEEX**

    下面是我写的Qt(C++)中的请求：

 

**\[cpp]** [view plain](http://blog.csdn.net/dlangu0393/article/details/7214728 "view plain")[copy](http://blog.csdn.net/dlangu0393/article/details/7214728 "copy")[print](http://blog.csdn.net/dlangu0393/article/details/7214728 "print")[?](http://blog.csdn.net/dlangu0393/article/details/7214728 "?")

01. void Protocol::Request\_SPEECH(QByteArray & audioData)
02. {
03. if (!Nt\_SPEECH)
04. {
05. QNetworkRequest request;
06. QString speechAPI = "http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN&maxresults=1";
07. request.setUrl(speechAPI);
08. request.setRawHeader("User-Agent", "Mozilla/5.0");
09. request.setRawHeader("Content-Type", "audio/x-flac; rate=16000");
10. Nt\_SPEECH = NetworkMGR.post(request, audioData);
11. connect(Nt\_SPEECH, SIGNAL(readyRead()), this, SLOT(Read\_SPEECH()));
12. }
13. }

    至于读取函数，就不贴在这里了，具体见：

Protocol: [http://pastebin.com/6G6wggfF](http://pastebin.com/6G6wggfF)

AudioInput:

    speechInput.h: [http://pastebin.com/qdMPeWZD](http://pastebin.com/qdMPeWZD)

    speechInput.cpp: [http://pastebin.com/567B47qF](http://pastebin.com/567B47qF)

main:

    mainwidget: [http://pastebin.com/c8bk7zd2](http://pastebin.com/c8bk7zd2)

 

    在翻阅Chromium源码的过程之中，还发现了其他有用的东西：

Speech Input API Specification [http://www.w3.org/2005/Incubator/htmlspeech/2010/10/google-api-draft.html](http://www.w3.org/2005/Incubator/htmlspeech/2010/10/google-api-draft.html)

    到目前为止，Google好像还没有公开这个API，使用许可依旧不详，请求也没有用到任何认证。但它确实能用，而且十分方便，对于编写非商业程序的人来说，这个东西真的是再好不过了（因为它有着高的爆表的识别率）。

参考：

Chromium Repository    [http://src.chromium.org/viewvc/chrome/trunk/src/content/browser/speech/](http://src.chromium.org/viewvc/chrome/trunk/src/content/browser/speech/)

Accessing Google Speech API / Chrome 11    [http://mikepultz.com/2011/03/accessing-google-speech-api-chrome-11/](http://mikepultz.com/2011/03/accessing-google-speech-api-chrome-11/)

 

![](http://hi.csdn.net/attachment/201201/24/0_1327399151zr0r.gif)

 

附：

1、SpeechInputError interface 错误信息

**\[cpp]** [view plain](http://blog.csdn.net/dlangu0393/article/details/7214728 "view plain")[copy](http://blog.csdn.net/dlangu0393/article/details/7214728 "copy")[print](http://blog.csdn.net/dlangu0393/article/details/7214728 "print")[?](http://blog.csdn.net/dlangu0393/article/details/7214728 "?")

01. // This enumeration follows the values described here:
02. // http://www.w3.org/2005/Incubator/htmlspeech/2010/10/google-api-draft.html#speech-input-error
03. enum SpeechInputError {
04. // There was no error.
05. SPEECH\_INPUT\_ERROR\_NONE = 0,
06. // The user or a script aborted speech input.
07. SPEECH\_INPUT\_ERROR\_ABORTED,
08. // There was an error with recording audio.
09. SPEECH\_INPUT\_ERROR\_AUDIO,
10. // There was a network error.
11. SPEECH\_INPUT\_ERROR\_NETWORK,
12. // No speech heard before timeout.
13. SPEECH\_INPUT\_ERROR\_NO\_SPEECH,
14. // Speech was heard, but could not be interpreted.
15. SPEECH\_INPUT\_ERROR\_NO\_MATCH,
16. // There was an error in the speech recognition grammar.
17. SPEECH\_INPUT\_ERROR\_BAD\_GRAMMAR,
18. };

2、多种音频格式的测试

    收到朋友的邮件说使用flac实在是很不方便，问我有没有更好的解决方法，于是我尝试将其他编码格式应用于Google Speech API。以下为结果：

    1、WAV格式

    请求Header：Content-Type: audio/L16; rate=16000

    返回结果：**识别成功**

    2、MP3格式

    请求Header：Content-Type: audio/mpeg; rate=16000

    返回结果：**无法识别的编码**

    请求Header：Content-Type: audio/mpeg3; rate=16000

    返回结果：**无法识别的编码**

    请求Header：Content-Type: audio/x-mpeg; rate=16000

    返回结果：**无法识别的编码**

    请求Header：Content-Type: audio/x-mpeg-3; rate=16000

    返回结果：**无法识别的编码**

    请求Header：Content-Type: audio/mp3; rate=16000

    返回结果：**无法识别的编码**

    3、PCM格式

    请求Header：Content-Type: audio/x-ogg-pcm; rate=16000

    返回结果：**无法识别的编码**

    请求Header：Content-Type: audio/pcm; rate=16000

    返回结果：**无法识别的编码**

    4、SPEEX格式

    请求Header：Content-Type: audio/x-speex-with-header-byte; rate=16000

    返回结果：**识别成功**

    请求Header：Content-Type: audio/speex; rate=16000

    返回结果：**识别成功**

    由于识别接口并不开放，所以无法得知具体的支持格式，如果哪位朋友发现了新的支持格式，请一定要留言哦！