Skip to content

Commit

Permalink
add -d option
Browse files Browse the repository at this point in the history
  • Loading branch information
shinshin86 committed Apr 8, 2023
1 parent 96fef3b commit a61855b
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 32 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ Execute the following command to speak the string passed as an argument.
vpeak こんにちは!
```

Converts all text files(`.txt`) in the directory specified by the `-d` option to audio files (`.wav`).

```
vpeak -d your-dir
```

Multiple options can be combined.

```sh
# ex: Convert a text file in the testdir directory into a voice file with the voice of Japanese Female 1.
vpeak -d testdir -n f1
```

Run the `help` command for more information.

```
Expand Down
112 changes: 80 additions & 32 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,39 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)

const (
VoicepeakPath = "/Applications/voicepeak.app/Contents/MacOS/voicepeak"
WavName = "output.wav"
)

func playCmd(wavName string) *exec.Cmd {
return exec.Command("afplay", wavName)
}

func vpCmd(options []string) *exec.Cmd {
_, err := exec.LookPath(VoicepeakPath)
if err != nil {
log.Fatalf("Command not found: %v", err)
}

return exec.Command(VoicepeakPath, options...)
}

func convertWavExt(filename string) string {
oldExt := filepath.Ext(filename)
return strings.TrimSuffix(filename, oldExt) + ".wav"
}

func main() {
dirOpt := flag.String("d", "", "Directory to read files from")
narratorOpt := flag.String("n", "", "Specify the narrator. See below for options.")

flag.Usage = func() {
Expand Down Expand Up @@ -42,14 +65,6 @@ func main() {
log.Fatalf("Usage: %s [-n] <text>", os.Args[0])
}

voicepeakPath := "/Applications/voicepeak.app/Contents/MacOS/voicepeak"
_, err := exec.LookPath(voicepeakPath)
if err != nil {
log.Fatalf("Command not found: %v", err)
}

text := flag.Args()[0]

narratorMap := map[string]string{
"f1": "Japanese Female 1",
"f2": "Japanese Female 2",
Expand All @@ -60,30 +75,63 @@ func main() {
"c": "Japanese Female Child",
}

options := []string{"-s", text}
if narrator, ok := narratorMap[*narratorOpt]; ok {
options = append([]string{"--narrator", narrator}, options...)
} else if *narratorOpt != "" {
log.Fatalf("Invalid narrator option: %s", *narratorOpt)
}

cmd1 := exec.Command(voicepeakPath, options...)
err = cmd1.Run()
if err != nil {
log.Fatalf("voicepeak command failed: %v", err)
}

wavName := "output.wav"

cmd2 := playCmd(wavName)
err = cmd2.Run()
if err != nil {
log.Fatalf("wav file play failed: %v", err)
}

err = os.Remove(wavName)
if err != nil {
log.Fatalf("Failed to delete %s: %v", wavName, err)
if *dirOpt == "" {
text := flag.Args()[0]
options := []string{"-s", text}
if narrator, ok := narratorMap[*narratorOpt]; ok {
options = append([]string{"--narrator", narrator}, options...)
} else if *narratorOpt != "" {
log.Fatalf("Invalid narrator option: %s", *narratorOpt)
}

cmd1 := vpCmd(options)
err := cmd1.Run()
if err != nil {
log.Fatalf("voicepeak command failed: %v", err)
}

cmd2 := playCmd(WavName)
err = cmd2.Run()
if err != nil {
log.Fatalf("wav file play failed: %v", err)
}

err = os.Remove(WavName)
if err != nil {
log.Fatalf("Failed to delete %s: %v", WavName, err)
}
} else {
files, err := ioutil.ReadDir(*dirOpt)
if err != nil {
log.Fatalf("Error reading directory: %v", err)
}

for _, file := range files {
if !file.IsDir() && filepath.Ext(file.Name()) == ".txt" {
filePath := filepath.Join(*dirOpt, file.Name())
content, err := ioutil.ReadFile(filePath)

if err != nil {
log.Printf("Error reading file (%s): %v", file.Name(), err)
continue
}

outputName := convertWavExt(file.Name())
outputPath := filepath.Join(*dirOpt, outputName)
options := []string{"-s", string(content), "-o", outputPath}
if narrator, ok := narratorMap[*narratorOpt]; ok {
options = append([]string{"--narrator", narrator}, options...)
} else if *narratorOpt != "" {
log.Fatalf("Invalid narrator option: %s", *narratorOpt)
}

cmd1 := vpCmd(options)
err = cmd1.Run()
if err != nil {
log.Fatalf("voicepeak command failed: %v", err)
}
}
}
}

fmt.Println("Commands executed successfully")
Expand Down

0 comments on commit a61855b

Please sign in to comment.