-
-
Save josue/62379312c6c9828291b9341714199108 to your computer and use it in GitHub Desktop.
Audible - Golang book reader as it prints the words it speaks (MacOS > say)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os/exec" | |
"runtime" | |
"strings" | |
"time" | |
) | |
var filename string | |
var voice string | |
var speachRate int | |
func main() { | |
flag.StringVar(&filename, "book", "", "text file book") | |
flag.StringVar(&voice, "voice", "Karen", "voice") | |
flag.IntVar(&speachRate, "speachrate", 6, "speach rate * the words from the sentence") | |
flag.Parse() | |
if err := checkSpeechSupport(); err != nil { | |
log.Fatal(err) | |
} | |
sentences := getBookContent(filename) | |
sentenceDone := make(chan bool) | |
fmt.Printf("- Reading book: %v\n\n", filename) | |
for _, sentence := range sentences { | |
sentence = sentence + "." | |
go textReader(sentence, sentenceDone) | |
speechReader(sentence) | |
<-sentenceDone | |
} | |
fmt.Println("\n- Finished book.") | |
} | |
func checkSpeechSupport() error { | |
switch runtime.GOOS { | |
case "darwin": | |
return nil | |
default: | |
return fmt.Errorf("No support for text-to-speech in OS %v", runtime.GOOS) | |
} | |
} | |
func getBookContent(file string) []string { | |
book, err := ioutil.ReadFile(file) | |
if err != nil { | |
log.Fatal(err) | |
} | |
allSentences := strings.TrimSuffix(string(book), "\n") | |
sentences := strings.Split(allSentences, ".") | |
return sentences | |
} | |
func speechReader(sentence string) { | |
switch runtime.GOOS { | |
case "darwin": | |
exec.Command("say", "-v", voice, sentence).Run() | |
default: | |
log.Fatalf("No support for text-to-speech in OS %v", runtime.GOOS) | |
} | |
} | |
func textReader(sentence string, sentenceDone chan bool) { | |
words := strings.Split(sentence, " ") | |
pausePerWord := len(words) * speachRate | |
for _, w := range words { | |
fmt.Printf("%v ", w) | |
time.Sleep(time.Millisecond * time.Duration(pausePerWord)) | |
} | |
fmt.Printf("\n") | |
sentenceDone <- true | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Tell me, Muse, of that man, so ready at need, who wandered | |
far and wide, after he had sacked the sacred citadel of | |
Troy, and many were the men whose towns he saw and whose | |
mind he learnt, yea, and many the woes he suffered in his | |
heart on the deep, striving to win his own life and the | |
return of his company. Nay, but even so he saved not his | |
company, though he desired it sore. For through the | |
blindness of their own hearts they perished, fools, who | |
devoured the oxen of Helios Hyperion: but the god took from | |
them their day of returning. Of these things, goddess, | |
daughter of Zeus, whencesoever thou hast heard thereof, | |
declare thou even unto us. | |
Now all the rest, as many as fled from sheer destruction, | |
were at home, and had escaped both war and sea, but | |
Odysseus only, craving for his wife and for his homeward | |
path, the lady nymph Calypso held, that fair goddess, in her | |
hollow caves, longing to have him for her lord. But when | |
now the year had come in the courses of the seasons, | |
wherein the gods had ordained that he should return home to | |
Ithaca, not even there was he quit of labours, not even | |
among his own; but all the gods had pity on him save | |
Poseidon, who raged continually against godlike Odysseus, | |
till he came to his own country. Howbeit Poseidon had now | |
departed for the distant Ethiopians, the Ethiopians that are | |
sundered in twain, the uttermost of men, abiding some where | |
Hyperion sinks and some where he rises. There he looked to | |
receive his hecatomb of bulls and rams, there he made merry | |
sitting at the feast, but the other gods were gathered in | |
the halls of Olympian Zeus. Then among them the father of | |
men and gods began to speak, for he bethought him in his | |
heart of noble Aegisthus, whom the son of Agamemnon, | |
far-famed Orestes, slew. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment