Skip to content

Commit 97939dc

Browse files
authored
Merge pull request #25 from vishen/text_to_speech
tts: added text-to-speech support
2 parents e039470 + df30849 commit 97939dc

File tree

780 files changed

+173779
-10350
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

780 files changed

+173779
-10350
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@ If you would like to see what a device is sending, you are able to `watch` the p
132132
$ go-chromecast watch
133133
```
134134

135+
### Text To Speech
136+
137+
Experimental text-to-speech support has been added. This uses (Google
138+
Cloud's Text-to-Speech)[https://cloud.google.com/text-to-speech/] to
139+
turn text into an mp3 audio file, this is then streamed to the device.
140+
141+
Text-to-speech api needs to be enabled https://console.cloud.google.com/flows/enableapi?apiid=texttospeech.googleapis.com and a google service account is required https://console.cloud.google.com/apis/credentials/serviceaccountkey
142+
143+
```
144+
$ go-chromecast tts <message> --google-service-account=/path/to/service/account.json
145+
```
146+
147+
135148
## User Interface
136149

137150
![User-interface example](go-chromecast-ui.png "User-interface example")
@@ -204,9 +217,10 @@ Available Commands:
204217
seek Seek by seconds into the currently playing media
205218
status Current chromecast status
206219
stop Stop casting
220+
tts text-to-speech
207221
ui Run the UI
208222
unpause Unpause the currently playing media on the chromecast
209-
watch Watch all events sent from a chromecaset device
223+
watch Watch all events sent from a chromecast device
210224
211225
Flags:
212226
-a, --addr string Address of the chromecast device
@@ -215,6 +229,7 @@ Flags:
215229
-n, --device-name string chromecast device name
216230
--disable-cache disable the cache
217231
-h, --help help for go-chromecast
232+
-i, --iface string Network interface to use
218233
-p, --port string Port of the chromecast device if 'addr' is specified (default "8009")
219234
-u, --uuid string chromecast device uuid
220235
--with-ui run with a UI

application/application.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,6 @@ func (a *Application) PlayedItems() map[string]PlayedItem {
457457
}
458458

459459
func (a *Application) Load(filenameOrUrl, contentType string, transcode bool) error {
460-
461460
var mi mediaItem
462461
if strings.HasPrefix(filenameOrUrl, "http://") || strings.HasPrefix(filenameOrUrl, "https://") {
463462
if contentType == "" {

cmd/tts.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright © 2019 Jonathan Pentecost <[email protected]>
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"fmt"
19+
"io/ioutil"
20+
"os"
21+
22+
"github.com/spf13/cobra"
23+
"github.com/vishen/go-chromecast/tts"
24+
)
25+
26+
// ttsCmd represents the tts command
27+
var ttsCmd = &cobra.Command{
28+
Use: "tts <message>",
29+
Short: "text-to-speech",
30+
Run: func(cmd *cobra.Command, args []string) {
31+
32+
if len(args) != 1 {
33+
fmt.Printf("expected exactly one argument to convert to speech\n")
34+
return
35+
}
36+
37+
googleServiceAccount, _ := cmd.Flags().GetString("google-service-account")
38+
if googleServiceAccount == "" {
39+
fmt.Printf("--google-service-account is required\n")
40+
return
41+
}
42+
43+
b, err := ioutil.ReadFile(googleServiceAccount)
44+
if err != nil {
45+
fmt.Printf("unable to open google service account file: %v\n", err)
46+
return
47+
}
48+
49+
app, err := castApplication(cmd, args)
50+
if err != nil {
51+
fmt.Printf("unable to get cast application: %v\n", err)
52+
return
53+
}
54+
55+
data, err := tts.Create("Hello, World!", b)
56+
if err != nil {
57+
fmt.Printf("%v\n", err)
58+
return
59+
}
60+
61+
f, err := ioutil.TempFile("", "go-chromecast-tts")
62+
if err != nil {
63+
fmt.Printf("unable to create temp file: %v", err)
64+
return
65+
}
66+
defer os.Remove(f.Name())
67+
68+
if _, err := f.Write(data); err != nil {
69+
fmt.Printf("unable to write to temp file: %v\n", err)
70+
return
71+
}
72+
if err := f.Close(); err != nil {
73+
fmt.Printf("unable to close temp file: %v\n", err)
74+
return
75+
}
76+
77+
if err := app.Load(f.Name(), "audio/mp3", false); err != nil {
78+
fmt.Printf("unable to load media to device: %v\n", err)
79+
return
80+
}
81+
82+
return
83+
},
84+
}
85+
86+
func init() {
87+
rootCmd.AddCommand(ttsCmd)
88+
ttsCmd.Flags().String("google-service-account", "", "google service account JSON file")
89+
}

go.mod

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
module github.com/vishen/go-chromecast
22

3+
go 1.12
4+
35
require (
4-
github.com/buger/jsonparser v0.0.0-20180808090653-f4dd9f5a6b44
5-
github.com/davecgh/go-spew v1.1.1 // indirect
6-
github.com/gogo/protobuf v1.1.1
7-
github.com/hashicorp/go.net v0.0.0-20151006203346-104dcad90073 // indirect
8-
github.com/hashicorp/mdns v0.0.0-20170221172940-4e527d9d8081
6+
cloud.google.com/go v0.37.2
7+
github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23
8+
github.com/gogo/protobuf v1.2.1
9+
github.com/hashicorp/mdns v1.0.0
910
github.com/inconshreveable/mousetrap v1.0.0 // indirect
1011
github.com/jroimartin/gocui v0.4.0
1112
github.com/mattn/go-runewidth v0.0.4 // indirect
12-
github.com/miekg/dns v1.0.8 // indirect
13-
github.com/mitchellh/go-homedir v1.0.0
14-
github.com/nsf/termbox-go v0.0.0-20181027232701-60ab7e3d12ed // indirect
15-
github.com/onsi/ginkgo v1.7.0 // indirect
16-
github.com/onsi/gomega v1.4.3 // indirect
17-
github.com/pkg/errors v0.8.0
18-
github.com/pmezard/go-difflib v1.0.0 // indirect
19-
github.com/sirupsen/logrus v1.0.6
13+
github.com/mitchellh/go-homedir v1.1.0
14+
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d // indirect
15+
github.com/pkg/errors v0.8.1
16+
github.com/sirupsen/logrus v1.4.0
2017
github.com/spf13/cobra v0.0.3
21-
github.com/spf13/pflag v1.0.2 // indirect
22-
github.com/stretchr/testify v1.2.2 // indirect
23-
golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac // indirect
24-
gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect
25-
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 // indirect
18+
github.com/spf13/pflag v1.0.3 // indirect
19+
google.golang.org/api v0.3.0
20+
google.golang.org/genproto v0.0.0-20190321212433-e79c0c59cdb5
21+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
22+
gopkg.in/yaml.v2 v2.2.2 // indirect
2623
)

0 commit comments

Comments
 (0)