attspeech

package module
v0.0.0-...-4c56807 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 24, 2014 License: MIT Imports: 11 Imported by: 0

README

attspeech

wercker status

Go client for the AT&T Speech API.

Installation

go get bitbucket.org/jsgoecke/attspeech

Documentation

http://godoc.org/github.com/jsgoecke/attspeech

Usage

Speech to Text Result
package main

import (
	"bytes"
	"fmt"
	"github.com/jsgoecke/attspeech"
	"io"
	"os"
)

func main() {
	client := attspeech.New(os.Getenv("ATT_APP_KEY"), os.Getenv("ATT_APP_SECRET"), "")
	client.SetAuthTokens()

	data := &bytes.Buffer{}
	file, err := os.Open("../test/test.wav")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer file.Close()
	_, err = io.Copy(data, file)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	apiRequest := client.NewAPIRequest(attspeech.STTResource)
	apiRequest.ContentType = "audio/x-wav"
	apiRequest.Data = data
	response, err := client.SpeechToText(apiRequest)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	fmt.Println(response)
}
Speech to Text Custom Result
package main

import (
	"bytes"
	"fmt"
	"github.com/jsgoecke/attspeech"
	"io"
	"os"
)

func main() {
	client := attspeech.New(os.Getenv("ATT_APP_KEY"), os.Getenv("ATT_APP_SECRET"), "")
	client.SetAuthTokens()

	data := &bytes.Buffer{}
	file, err := os.Open("../test/test.wav")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer file.Close()
	_, err = io.Copy(data, file)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	apiRequest := client.NewAPIRequest(attspeech.STTCResource)
	apiRequest.ContentType = "audio/x-wav"
	apiRequest.Data = data
	apiRequest.Filename = "test.wav"
	response, err := client.SpeechToTextCustom(apiRequest, srgsXML(), plsXML())
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	fmt.Println(response)
}

func plsXML() string {
	return `<?xml version="1.0" encoding="UTF-8"?> 
			<lexicon version="1.0" alphabet="sampa" xml:lang="en-US"> 
			   <lexeme> 
			       <grapheme>star</grapheme> 
			       <phoneme>tS { n</phoneme> 
			   </lexeme> 
			</lexicon>`
}

func srgsXML() string {
	return `<grammar root="top" xml:lang="en-US"> 
			  <rule id="CONTACT"> 
			      <one-of> 
			        <item>star</item> 
			        <item>key</item> 
			      </one-of> 
			  </rule> 
			  <rule id="top" scope="public"> 
			      <item> 
			          <one-of> 
			            <item>greeting</item> 
			            <item>the administration menu</item> 
			          </one-of> 
			      </item> 
			  <ruleref uri="#CONTACT"/> 
			  </rule> 
			</grammar>`
}
Text to Speech Result
package main

import (
	"fmt"
	"github.com/jsgoecke/attspeech"
	"io/ioutil"
	"os"
)

func main() {
	client := attspeech.New(os.Getenv("ATT_APP_KEY"), os.Getenv("ATT_APP_SECRET"), "")
	client.SetAuthTokens()
	apiRequest := client.NewAPIRequest(attspeech.TTSResource)
	apiRequest.ContentType = "text/plain"
	apiRequest.Accept = "audio/x-wav"
	apiRequest.Text = "I want to be an airborne ranger, I want to live the life of danger."
	data, err := client.TextToSpeech(apiRequest)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	err = ioutil.WriteFile("/Users/jsgoecke/Desktop/tts_test.wav", data, 0644)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

Testing

cd attspeech
go get github.com/smartystreets/goconvey/convey
go test
Test Coverage

http://gocover.io/github.com/jsgoecke/attspeech

Lint

http://go-lint.appspot.com/github.com/jsgoecke/attspeech

Documentation

Index

Constants

View Source
const (
	// APIBase is the base URL for the ATT Speech API
	APIBase = "https://api.att.com"
	// STTResource is the speech to text resource
	STTResource = "/speech/v3/speechToText"
	// STTCResource is the speech to text custom resource
	STTCResource = "/speech/v3/speechToTextCustom"
	// TTSResource is the text to speech resource
	TTSResource = "/speech/v3/textToSpeech"
	// OauthResource is the oauth resource
	OauthResource = "/oauth/access_token"
	// UserAgent is the user agent use for the HTTP client
	UserAgent = "GoATTSpeechLib"
	// Version is the version of the ATT Speech API
	Version = "0.1"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	RequestError struct {
		ServiceException struct {
			MessageID string `json:"MessageId"`
			Text      string `json:"Text"`
			Variables string `json:"Variables"`
		} `json:"ServiceException"`
		PolicyException struct {
			MessageID string `json:"MessageId"`
			Text      string `json:"Text"`
			Variables string `json:"Variables"`
		} `json:"PolicyException"`
	} `json:"RequestError"`
}

APIError represents an error from the AT&T Speech API

type APIRequest

type APIRequest struct {
	Accept            string
	Authorization     string
	ContentLanguage   string
	ContentLength     string
	ContentType       string
	Data              *bytes.Buffer
	Filename          string
	Tempo             string
	Text              string
	TransferEncoding  string
	VoiceName         string
	Volume            string
	UserAgent         string
	XArg              string
	XSpeechContext    string
	XSpeechSubContext string
}

APIRequest represents the parameters for a Text to Speech request

type Client

type Client struct {
	APIBase       string
	STTResource   string
	STTCResource  string
	TTSResource   string
	OauthResource string
	ID            string
	Secret        string
	Tokens        map[string]*Token
	Scope         [3]string
}

Client is an ATT Speech API client

func New

func New(id string, secret string, apiBase string) *Client

New creates a new AttSpeechClient

client := attspeech.New("<id>", "<secret>", "")
client.SetAuthTokens()

func (*Client) NewAPIRequest

func (client *Client) NewAPIRequest(resource string) *APIRequest

NewAPIRequest sets the common headers for TTS and STT

client := attspeech.New("<id>", "<secret>", "")
client.SetAuthTokens()
apiRequest := client.NewAPIRequest(TTSResource)

Note, when setting apiRequest.XArg, always append with '+=', unless you specifically intend to overwrite the defaults for ClientApp, ClientVersion, DeviceType and DeviceOs

func (*Client) SetAuthTokens

func (client *Client) SetAuthTokens() error

SetAuthTokens sets the provided authorization tokens for the client

client := attspeech.New("<id>", "<secret>", "")
client.SetAuthTokens()

func (*Client) SpeechToText

func (client *Client) SpeechToText(apiRequest *APIRequest) (*Recognition, error)

SpeechToText converts an audio file to text

client := attspeech.New("<id>", "<secret>", "")
client.SetAuthTokens()
apiRequest := client.NewAPIRequest(STTResource)
apiRequest.Data = data // where data is audio content as *bytes.Buffer
apiRequest.ContentType = "audio/wav"
result, apiError, err := client.SpeechToText(apiRequest)

More details available here:

http://developer.att.com/apis/speech/docs#resources-speech-to-text

func (*Client) SpeechToTextCustom

func (client *Client) SpeechToTextCustom(apiRequest *APIRequest, grammar string, dictionary string) (*Recognition, error)

SpeechToTextCustom converts an audio file to text

client := attspeech.New("<id>", "<secret>", "")
client.SetAuthTokens()
apiRequest := client.NewAPIRequest(STTResource)
apiRequest.Data = data // where data is audio content as *bytes.Buffer
apiRequest.ContentType = "audio/wav"
apiRequest.Filename = "test.wav"
result, apiError, err := client.SpeechToTextCustom(apiRequest, "<some srgs XML>", "<some pls XML>")

More details available here:

http://developer.att.com/apis/speech/docs#resources-speech-to-text-custom

func (*Client) TextToSpeech

func (client *Client) TextToSpeech(apiRequest *APIRequest) ([]byte, error)

TextToSpeech converts text to a speech file

client := attspeech.New("<id>", "<secret>", "")
client.SetAuthTokens()

apiRequest := client.NewAPIRequest(TTSResource)
apiRequest.Accept = "audio/x-wav",
apiRequest.VoiceName = "crystal",
apiRequest.Text = "I want to be an airborne ranger, I want to live the life of danger.",
data, err := client.TextToSpeech(apiRequest)

More details available here:

http://developer.att.com/apis/speech/docs#resources-text-to-speech

type Recognition

type Recognition struct {
	Recognition struct {
		Status     string `json:"Status"`
		ResponseID string `json:"ResponseId"`
		NBest      []struct {
			Hypothesis    string    `json:"Hypothesis"`
			LanguageID    string    `json:"LanguageId"`
			Confidence    float32   `json:"Confidence"`
			Grade         string    `json:"Grade"`
			ResultText    string    `json:"ResultText"`
			Words         []string  `json:"Words"`
			WordScores    []float32 `json:"WordScores"`
			NluHypothesis struct {
				OutComposite []struct {
					Grammar string `json:"Grammar"`
					Out     string `json:"Out"`
				} `json:"OutComposite"`
			} `json:"NluHypothesis"`
		} `json:"NBest"`
	} `json:"Recognition"`
}

Recognition represents at AT&T recognition response

type Token

type Token struct {
	AccessToken  string `json:"access_token"`
	TokenType    string `json:"token_type"`
	ExpiresIn    int    `json:"expires_in"`
	RefreshToken string `json:"refresh_token"`
}

Token represents the authorization tokens returned by the AT&T Speech API

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL