Skip to content

Commit

Permalink
MACの標準音声合成に対応
Browse files Browse the repository at this point in the history
  • Loading branch information
twsnmp committed Jan 3, 2023
1 parent e3d7b39 commit 1946559
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ZIP = zip

### ターゲットパラメータ
DIST = dist
SRC = ./main.go
SRC = ./main.go ./mac.go
TARGETS = $(DIST)/twnarrator.exe $(DIST)/twnarrator.app
GO_PKGROOT = ./...

Expand Down
54 changes: 54 additions & 0 deletions mac.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"fmt"
"log"
"os"
"os/exec"
"strconv"
"strings"
)

func showMacVoices() {
result, err := exec.Command("say", "-v", "?").Output()
if err != nil {
log.Fatalf("showMacVoices err=%v", err)
}
fmt.Println(string(result))
}

func speakMac(cfg config, l string) ([]byte, error) {
cmd := exec.Command("say", "-v", cfg.voice, "-o", "/tmp/twna.wav", "--data-format=LEF32@32000", "-r",
fmt.Sprintf("%d", int(170*cfg.speed)), l)
_, err := cmd.Output()
if err != nil {
log.Fatalf("speakMac err=%v", err)
}
defer os.Remove("/tmp/twna.wav")
return os.ReadFile("/tmp/twna.wav")
}

func getConfigMac(l string) config {
ret := config{
speaker: 0,
style: 0,
speed: 1.0,
intonation: 1.0,
volume: 1.0,
pitch: 0.0,
voice: "Alex",
}
l = strings.ReplaceAll(l, "#", "")
p := strings.Split(l, ",")
if len(p) < 2 {
return ret
}
ret.voice = p[0]
if len(p) > 2 {
if v, err := strconv.ParseFloat(p[2], 64); err == nil {
ret.speed = v
}
}
return ret

}
29 changes: 25 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -66,6 +67,7 @@ var url = "http://localhost:50021"
var script = ""
var list = false
var play = false
var mac = false
var version = "vx.x.x"
var commit = ""

Expand Down Expand Up @@ -152,11 +154,22 @@ func main() {
flag.StringVar(&script, "s", "", "input script(txt or pptx")
flag.BoolVar(&list, "l", false, "list speaker")
flag.BoolVar(&play, "p", false, "play")
flag.BoolVar(&mac, "mac", false, "mac mode")
flag.Parse()
getSpeakers()
if list {
showSpeakers()
return
if mac {
if runtime.GOOS != "darwin" {
log.Fatalln("not Mac OS")
}
if list {
showMacVoices()
return
}
} else {
getSpeakers()
if list {
showSpeakers()
return
}
}
log.Printf("version=%s(%s)", version, commit)
st := time.Now()
Expand All @@ -171,6 +184,7 @@ type config struct {
intonation float64
volume float64
pitch float64
voice string
}

func playScript(file string) {
Expand Down Expand Up @@ -226,6 +240,9 @@ func playScript(file string) {
}

func speak(cfg config, l string) ([]byte, error) {
if mac {
return speakMac(cfg, l)
}
spk := speakers[cfg.speaker]
spkID := spk.Styles[cfg.style].ID
params, err := getQuery(spkID, l)
Expand All @@ -247,13 +264,17 @@ func speak(cfg config, l string) ([]byte, error) {
}

func getConfig(l string) config {
if mac {
return getConfigMac(l)
}
ret := config{
speaker: 0,
style: 0,
speed: 1.0,
intonation: 1.0,
volume: 1.0,
pitch: 0.0,
voice: "",
}
l = strings.ReplaceAll(l, "#", "")
p := strings.Split(l, ",")
Expand Down

0 comments on commit 1946559

Please sign in to comment.