Skip to content

Commit

Permalink
Merge pull request #1 from ultreme/poc
Browse files Browse the repository at this point in the history
V1 pronounce
  • Loading branch information
moul authored Oct 24, 2019
2 parents a6187b0 + 341fde5 commit 909fbd3
Show file tree
Hide file tree
Showing 205 changed files with 255 additions and 14 deletions.
6 changes: 0 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,4 @@ workflows:
jobs:
- moul/golang-build:
gopkg: ultre.me/speechotron
- moul/golang-build:
gopkg: ultre.me/speechotron
tag: '1.12'
- moul/golang-build:
gopkg: ultre.me/speechotron
tag: '1.11'
- moul/docker-build
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.DS_Store
out.mp3

# Temporary files
*~
*#
Expand Down
5 changes: 5 additions & 0 deletions go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 87 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

162 changes: 160 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,165 @@
package main

import "fmt"
import (
"errors"
"flag"
"fmt"
"log"
"math/rand"
"os"
"os/exec"
"sort"
"strings"

packr "github.com/gobuffalo/packr/v2"
"github.com/peterbourgon/ff/ffcli"
)

var (
sayBox = packr.New("say", "./say")
pronounceBox = packr.New("pronounce", "./pronounce")
)

func main() {
fmt.Println("Hello World!")
log.SetFlags(0)

var (
sayFlags = flag.NewFlagSet("say", flag.ExitOnError)
sayVoice = sayFlags.String("v", "RANDOM", "voice")

pronounceFlags = flag.NewFlagSet("pronounce", flag.ExitOnError)
pronounceVoice = pronounceFlags.String("v", "RANDOM", "voice")
)

pronounce := &ffcli.Command{
Name: "pronounce",
Usage: "pronounce [-v VOICE] PRONOUNCE",
FlagSet: pronounceFlags,
LongHelp: fmt.Sprintf("VOICES\n %s", strings.Join(append(voiceList(pronounceBox), "RANDOM"), "\n ")),
Exec: func(args []string) error {
if len(args) < 1 {
return flag.ErrHelp
}
parts := voiceParts(pronounceBox, *pronounceVoice)

tosay := []rune(strings.Join(args, " "))

selectedParts := []string{}

for i := 0; i < len(tosay); {
maxLen := 0
selectedPart := ""
for partString := range parts {
part := []rune(partString)
if len(part) <= maxLen {
continue
}
if string(part) == string(tosay[i:i+len(part)]) {
maxLen = len(part)
selectedPart = string(part)
}
}
if selectedPart != "" {
i += maxLen
selectedParts = append(selectedParts, selectedPart)
} else {
i++ // skip unmatched parts
}
}

selectedFiles := []string{}

for _, part := range selectedParts {
randomFile := parts[part][rand.Intn(len(parts[part]))]
selectedFiles = append(selectedFiles, fmt.Sprintf("./pronounce/%s", randomFile))
}

cmdArgs := append(selectedFiles, "out.mp3")
log.Printf("+ sox %s", strings.Join(cmdArgs, " "))
cmd := exec.Command("sox", cmdArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}

log.Printf("+ afplay out.mp3")
cmd = exec.Command("afplay", "out.mp3")
if err := cmd.Run(); err != nil {
return err
}

return nil
},
}

say := &ffcli.Command{
Name: "say",
Usage: "say [-v VOICE] SAY",
FlagSet: sayFlags,
LongHelp: fmt.Sprintf("VOICES\n %s", strings.Join(append(voiceList(sayBox), "RANDOM"), "\n ")),
Exec: func(args []string) error {
if len(args) < 1 {
return flag.ErrHelp
}
_ = sayVoice
return fmt.Errorf("not implemented")
},
}

root := &ffcli.Command{
Usage: "speechotron <subcommand> [flags] [args...]",
Subcommands: []*ffcli.Command{pronounce, say},
Exec: func([]string) error { return flag.ErrHelp },
}

if err := root.Run(os.Args[1:]); err != nil {
if errors.Is(err, flag.ErrHelp) {
return
}
log.Fatalf("fatal: %+v", err)
}
}

func voiceList(box *packr.Box) []string {
voices := map[string]bool{}
for _, voice := range box.List() {
if !strings.HasSuffix(voice, ".wav") {
continue
}
voices[strings.Split(voice, "/")[0]] = true
}

ret := []string{}
for voice := range voices {
ret = append(ret, voice)
}
sort.Strings(ret)

return ret
}

func voiceParts(box *packr.Box, voice string) map[string][]string {
ret := map[string][]string{}

for _, file := range box.List() {
if !strings.HasSuffix(file, ".wav") {
continue
}

spl := strings.Split(file, "/")
actualVoice := spl[0]
if voice != "RANDOM" && voice != actualVoice {
continue
}

part := strings.Replace(spl[1], ".wav", "", -1)

if _, ok := ret[part]; !ok {
ret[part] = []string{}
}
ret[part] = append(ret[part], file)
}

return ret
}
6 changes: 0 additions & 6 deletions main_test.go

This file was deleted.

Binary file added pronounce/gajeb-1/a.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/b.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/ch.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/d.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/e.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/en.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/f.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/gu.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/h.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/i.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/j.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/k.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/l.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/m.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/n.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/o.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/on.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/ou.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/p.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/r.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/s.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/t.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/u.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/v.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/w.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/x.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/z.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/è.wav
Binary file not shown.
Binary file added pronounce/gajeb-1/é.wav
Binary file not shown.
Binary file added say/gajeb1/andouille.wav
Binary file not shown.
Binary file added say/gajeb1/bois.wav
Binary file not shown.
Binary file added say/gajeb1/bout.wav
Binary file not shown.
Binary file added say/gajeb1/caca.wav
Binary file not shown.
Binary file added say/gajeb1/carton.wav
Binary file not shown.
Binary file added say/gajeb1/chien.wav
Binary file not shown.
Binary file added say/gajeb1/contreplaqué.wav
Binary file not shown.
Binary file added say/gajeb1/crotte.wav
Binary file not shown.
Binary file added say/gajeb1/d'andouille.wav
Binary file not shown.
Binary file added say/gajeb1/d'arbre.wav
Binary file not shown.
Binary file added say/gajeb1/d'ectoplasme.wav
Binary file not shown.
Binary file added say/gajeb1/d'occasion.wav
Binary file not shown.
Binary file added say/gajeb1/d'oreille.wav
Binary file not shown.
Binary file added say/gajeb1/de.wav
Binary file not shown.
Binary file added say/gajeb1/ectoplasme.wav
Binary file not shown.
Binary file added say/gajeb1/en.wav
Binary file not shown.
Binary file added say/gajeb1/espèce.wav
Binary file not shown.
Binary file added say/gajeb1/fesse.wav
Binary file not shown.
Binary file added say/gajeb1/forbant.wav
Binary file not shown.
Binary file added say/gajeb1/formica.wav
Binary file not shown.
Binary file added say/gajeb1/freluquet.wav
Binary file not shown.
Binary file added say/gajeb1/fromage.wav
Binary file not shown.
Binary file added say/gajeb1/gigantesque.wav
Binary file not shown.
Binary file added say/gajeb1/jus.wav
Binary file not shown.
Binary file added say/gajeb1/maché.wav
Binary file not shown.
Binary file added say/gajeb1/minuscule.wav
Binary file not shown.
Binary file added say/gajeb1/moule à gauffre.wav
Binary file not shown.
Binary file added say/gajeb1/nez.wav
Binary file not shown.
Binary file added say/gajeb1/nouille.wav
Binary file not shown.
Binary file added say/gajeb1/papier.wav
Binary file not shown.
Binary file added say/gajeb1/pied.wav
Binary file not shown.
Binary file added say/gajeb1/pipi.wav
Binary file not shown.
Binary file added say/gajeb1/plat.wav
Binary file not shown.
Binary file added say/gajeb1/platre.wav
Binary file not shown.
Binary file added say/gajeb1/poubelle.wav
Binary file not shown.
Binary file added say/gajeb1/prout.wav
Binary file not shown.
Binary file added say/gajeb1/purée.wav
Binary file not shown.
Binary file added say/gajeb1/raclure.wav
Binary file not shown.
Binary file added say/gajeb1/recyclé.wav
Binary file not shown.
Binary file added say/gajeb1/résidu.wav
Binary file not shown.
Binary file added say/gajeb1/toc.wav
Binary file not shown.
Binary file added say/gajeb1/vandal.wav
Binary file not shown.
Binary file added say/gajeb1/yeux.wav
Binary file not shown.
Binary file added say/gajeb1/zizi.wav
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added say/silent.wav
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 909fbd3

Please sign in to comment.