Last active
September 5, 2017 14:15
-
-
Save suyash/7dc3c41631fc52321f8942333ec5031c to your computer and use it in GitHub Desktop.
synth
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
*.mp3 |
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" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"net/url" | |
"os" | |
"regexp" | |
"strings" | |
) | |
var outputFile string | |
var reg = regexp.MustCompile("\\*\\*([a-zA-Z]+)\\*\\*\\s-\\s(.+)\n") | |
const F = 40 | |
func init() { | |
flag.StringVar(&outputFile, "o", "audio", "output file name") | |
} | |
func main() { | |
flag.Parse() | |
if flag.NArg() < 1 { | |
log.Fatal("input file not specified") | |
} | |
data, err := read(flag.Arg(0)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
c, cur, str, d := 0, 0, "", make([]byte, 0) | |
for _, v := range data { | |
if len(v) > 0 { | |
str += v | |
cur = (cur + 1) % F | |
if cur == 0 { | |
println(str) | |
cd, err := query(str) | |
if err != nil { | |
log.Fatal(err) | |
} | |
d = append(d, cd...) | |
c++ | |
str = "" | |
} | |
} | |
} | |
if len(str) > 0 { | |
println(str) | |
cd, err := query(str) | |
if err != nil { | |
log.Fatal(err) | |
} | |
d = append(d, cd...) | |
c++ | |
str = "" | |
} | |
if err := write(outputFile+".mp3", d); err != nil { | |
log.Fatal(err) | |
} | |
log.Println("done") | |
} | |
func read(inputFile string) ([]string, error) { | |
data, err := ioutil.ReadFile(inputFile) | |
if err != nil { | |
return nil, err | |
} | |
s := string(data) | |
start, end := strings.Index(s, "<!-- synth start -->"), strings.Index(s, "<!-- synth end -->") | |
s = s[start+20 : end] | |
s = reg.ReplaceAllString(s, "$1 means $2. ") | |
return strings.Split(s, "\n"), nil | |
} | |
func query(text string) ([]byte, error) { | |
base := "https://watson-api-explorer.mybluemix.net/text-to-speech/api/v1/synthesize" | |
q := make(url.Values) | |
q.Add("accept", "audio/mp3") | |
q.Add("voice", "en-US_LisaVoice") | |
q.Add("text", text) | |
u, err := url.Parse(base) | |
if err != nil { | |
return nil, err | |
} | |
u.RawQuery = q.Encode() | |
log.Println(u) | |
res, err := http.Get(u.String()) | |
if err != nil { | |
return nil, err | |
} | |
body, err := ioutil.ReadAll(res.Body) | |
if err != nil { | |
return nil, err | |
} | |
if err := res.Body.Close(); err != nil { | |
return nil, err | |
} | |
return body, nil | |
} | |
func write(outputFile string, data []byte) error { | |
if _, err := os.Stat(outputFile); err == nil { | |
os.Remove(outputFile) | |
} | |
if err := ioutil.WriteFile(outputFile, data, 0644); err != nil { | |
return err | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment