Skip to content

Commit

Permalink
better cli output
Browse files Browse the repository at this point in the history
  • Loading branch information
C-Loftus committed Jul 27, 2024
1 parent e1d1637 commit c87eccc
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 31 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ piper/

*.wav

QuickPiperAudiobook
QuickPiperAudiobook

*.md
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,17 @@ module QuickPiperAudiobook
go 1.22.5

require github.com/alecthomas/kong v0.9.0

require (
github.com/briandowns/spinner v1.23.1 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4 // indirect
github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.1.0 // indirect
)
27 changes: 27 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,32 @@ github.com/alecthomas/kong v0.9.0 h1:G5diXxc85KvoV2f0ZRVuMsi45IrBgx9zDNGNj165aPA
github.com/alecthomas/kong v0.9.0/go.mod h1:Y47y5gKfHp1hDc7CH7OeXgLIpp+Q2m1Ni0L5s3bI8Os=
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/briandowns/spinner v1.23.1 h1:t5fDPmScwUjozhDj4FA46p5acZWIPXYE30qW2Ptu650=
github.com/briandowns/spinner v1.23.1/go.mod h1:LaZeM4wm2Ywy6vO571mvhQNRcWfRUnXOs0RcKV0wYKM=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4 h1:ygs9POGDQpQGLJPlq4+0LBUmMBNox1N4JSpw+OETcvI=
github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4/go.mod h1:0W7dI87PvXJ1Sjs0QPvWXKcQmNERY77e8l7GFhZB/s4=
github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4 h1:qZNfIGkIANxGv/OqtnntR4DfOY2+BgwR60cAcu/i3SE=
github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4/go.mod h1:kW3HQ4UdaAyrUCSSDR4xUzBKW6O2iA4uHhk7AtyYp10=
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U=
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af h1:6yITBqGTE2lEeTPG04SN9W+iWHCRyHqlVYILiSXziwk=
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af/go.mod h1:4F09kP5F+am0jAwlQLddpoMDM+iewkxxt6nxUQ5nq5o=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
4 changes: 2 additions & 2 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func downloadIfNotExists(fileURL, fileName string) error {

func DownloadFile(url string, filename string) (*os.File, error) {

print("Downloading " + filename)
println("Downloading " + filename)

// Create the file to save the model
file, err := os.Create(filename)
Expand All @@ -51,7 +51,7 @@ func DownloadFile(url string, filename string) (*os.File, error) {
return nil, fmt.Errorf("error saving file %s: %v", filename, err)
}

fmt.Println("Downloaded successfully.")
fmt.Println("Finished downloading successfully.")

return file, nil
}
70 changes: 44 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ package main
import (
"fmt"
"io"
"log/slog"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/alecthomas/kong"
"github.com/briandowns/spinner"
"github.com/fatih/color"
"github.com/gen2brain/beeep"
)

func ConvertEbook(inputPath string) (io.Reader, error) {
func getConvertedRawText(inputPath string) (io.Reader, error) {
// Ensure input file exists
if _, err := os.Stat(inputPath); os.IsNotExist(err) {
// If the file does not exist, check if it's a URL
Expand Down Expand Up @@ -41,7 +46,9 @@ func ConvertEbook(inputPath string) (io.Reader, error) {
}
defer os.Remove(tmpFile.Name())

// Build the ebook-convert command
c := color.New(color.Bold, color.FgMagenta)
c.Println("Converting " + filepath.Base(inputPath) + " to the proper text format...")

cmd := exec.Command("ebook-convert", inputPath, tmpFile.Name())

output, err := cmd.CombinedOutput()
Expand Down Expand Up @@ -113,7 +120,10 @@ func checkEbookConvertInstalled() error {

func grabModel(modelName string) error {

modelURL := modelToURL[modelName]
modelURL, ok := modelToURL[modelName]
if !ok {
return fmt.Errorf("model not found: %s", modelName)
}
modelJSONURL := modelURL + ".json"

// Download the model
Expand Down Expand Up @@ -167,12 +177,6 @@ func findModels(dir string) ([]string, error) {
}

func runPiper(filename string, model string, text io.Reader) error {
fmt.Println("Running piper on " + filename)

// buf := new(strings.Builder)
// _, err := io.Copy(buf, text)
// // check errors
// fmt.Println(buf.String())

// Get the current working directory
currentDir, err := os.Getwd()
Expand All @@ -186,7 +190,7 @@ func runPiper(filename string, model string, text io.Reader) error {
// Define the path to the 'piper' executable within the 'piper' directory
piperExecutable := filepath.Join(piperDir, "piper")

fmt.Println("piper executable path: " + piperExecutable)
slog.Debug("piper executable path: " + piperExecutable)

// Output name is equal to the filename with .wav instead of the extension
outputName := strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename)) + ".wav"
Expand All @@ -209,16 +213,24 @@ func runPiper(filename string, model string, text io.Reader) error {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

// Print command details
print("Running command: "+strings.Join(cmd.Args, " "), "\n")
slog.Debug("Running command: " + strings.Join(cmd.Args, " "))

c := color.New(color.Bold, color.FgMagenta, color.BlinkRapid)

c.Println("Converting "+filename+"to an audiobook...", "This may take a while!")
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond) // Build our new spinner
s.Start() // Start the spinner

// Capture output
err = cmd.Run()
if err != nil {
return fmt.Errorf("failed to run piper command: %v", err)
}
s.Stop()

fmt.Println("Done. Saved audiobook as", abs)
color.New(color.Bold, color.FgGreen).Println("Done. Saved audiobook as " + abs)

beeep.Alert("Audiobook created", "Check the terminal for more info", "")

return nil
}
Expand All @@ -240,13 +252,18 @@ func main() {
}

if cli.Model == "" {
cli.Model = "en_US-lessac-medium.onnx"
defaultModel := "en_US-hfc_male-medium.onnx"
println("No model specified. Defaulting to " + defaultModel)
cli.Model = defaultModel
}

if err := checkEbookConvertInstalled(); err != nil {
fmt.Printf("Error: %v\n", err)
ctx.FatalIfErrorf(err)
return
if (filepath.Ext(cli.Input)) != ".txt" {

if err := checkEbookConvertInstalled(); err != nil {
fmt.Printf("Error: %v\n", err)
ctx.FatalIfErrorf(err)
return
}
}

// Check if piper is installed and prompt to install if not
Expand All @@ -264,7 +281,11 @@ func main() {
return
}

fmt.Println("Models found: " + strings.Join(models, ", "))
if len(models) == 0 {
fmt.Println("No models found locally")
} else {
fmt.Println("Local models found: [ " + strings.TrimSpace(strings.Join(models, " , ")) + " ]")
}

err = grabModel(cli.Model)
if err != nil {
Expand All @@ -273,22 +294,19 @@ func main() {
return
}

// Execute the ebook conversion
data, err := ConvertEbook(cli.Input)
data, err := getConvertedRawText(cli.Input)

if err != nil {
fmt.Printf("Error: %v\n", err)
ctx.FatalIfErrorf(err)
} else {
fmt.Println("Ebook conversion completed successfully.")
fmt.Println("Text conversion completed successfully.")
}

err = runPiper(cli.Input, cli.Model, data)

if err != nil {
fmt.Printf("Error: %v\n", err)
ctx.FatalIfErrorf(err)
} else {
fmt.Println("Piper completed successfully.")
color.Red("Error: %v", err)
return
}
}
1 change: 1 addition & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package main
var modelToURL = map[string]string{
"en_US-hfc_male-medium.onnx": "https://huggingface.co/rhasspy/piper-voices/resolve/main/en/en_US/hfc_male/medium/en_US-hfc_male-medium.onnx",
"en_US-hfc_female-medium.onnx": "https://huggingface.co/rhasspy/piper-voices/resolve/main/en/en_US/hfc_female/medium/en_US-hfc_female-medium.onnx",
"en_US-lessac-medium.onnx": "https://huggingface.co/rhasspy/piper-voices/resolve/main/en/en_US/lessac/medium/en_US-lessac-medium.onnx",
}
2 changes: 0 additions & 2 deletions models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"testing"
)

// TestHelloName calls greetings.Hello with a name, checking
// for a valid return value.
func TestModels(t *testing.T) {

if err := grabModel("en_US-hfc_male-medium.onnx"); err != nil {
Expand Down

0 comments on commit c87eccc

Please sign in to comment.