v/GTT
1
0
mirror of https://github.com/eeeXun/GTT.git synced 2025-05-19 01:00:20 -07:00

feat: stop play sound

This commit is contained in:
eeeXun 2022-10-23 13:37:33 +08:00
parent 4a666c3749
commit 2c0249c526
3 changed files with 67 additions and 36 deletions

View File

@ -17,19 +17,45 @@ const (
soundURL = "https://translate.google.com.vn/translate_tts?ie=UTF-8&q=%s&tl=%s&client=tw-ob" soundURL = "https://translate.google.com.vn/translate_tts?ie=UTF-8&q=%s&tl=%s&client=tw-ob"
) )
type Lock struct {
stop bool
threadCount int8
}
func NewLock() *Lock {
return &Lock{
stop: true,
threadCount: 0,
}
}
func (l *Lock) Available() bool {
return l.stop && l.threadCount == 0
}
func (l *Lock) Acquire() {
l.stop = false
l.threadCount++
}
func (l *Lock) Release() {
l.stop = true
l.threadCount--
}
type Translator struct { type Translator struct {
srcLang string srcLang string
dstLang string dstLang string
soundLock *Lock soundLock *Lock
} }
func NewTranslator() Translator { func NewTranslator() *Translator {
return Translator{ return &Translator{
soundLock: NewLock(), soundLock: NewLock(),
} }
} }
func (t Translator) Translate(message string) (string, error) { func (t *Translator) Translate(message string) (string, error) {
var data []interface{} var data []interface{}
var translated string var translated string
@ -63,7 +89,7 @@ func (t Translator) Translate(message string) (string, error) {
return "", errors.New("Translation not found") return "", errors.New("Translation not found")
} }
func (t Translator) PlaySound(lang string, message string) error { func (t *Translator) PlaySound(lang string, message string) error {
url_str := fmt.Sprintf( url_str := fmt.Sprintf(
soundURL, soundURL,
url.QueryEscape(message), url.QueryEscape(message),
@ -71,25 +97,35 @@ func (t Translator) PlaySound(lang string, message string) error {
) )
res, err := http.Get(url_str) res, err := http.Get(url_str)
if err != nil { if err != nil {
t.soundLock.Release()
return err return err
} }
decoder, err := mp3.NewDecoder(res.Body) decoder, err := mp3.NewDecoder(res.Body)
if err != nil { if err != nil {
t.soundLock.Release()
return err return err
} }
otoCtx, readyChan, err := oto.NewContext(decoder.SampleRate(), 2, 2) otoCtx, readyChan, err := oto.NewContext(decoder.SampleRate(), 2, 2)
if err != nil { if err != nil {
t.soundLock.Release()
return err return err
} }
<-readyChan <-readyChan
player := otoCtx.NewPlayer(decoder) player := otoCtx.NewPlayer(decoder)
player.Play() player.Play()
for player.IsPlaying() { for player.IsPlaying() {
time.Sleep(time.Second) if t.soundLock.stop {
t.soundLock.Release()
return nil
} else {
time.Sleep(time.Millisecond)
}
} }
if err = player.Close(); err != nil { if err = player.Close(); err != nil {
t.soundLock.Release()
return err return err
} }
t.soundLock.Release()
return nil return nil
} }

16
ui.go
View File

@ -134,22 +134,38 @@ func translatePageHandler(event *tcell.EventKey) *tcell.EventKey {
} }
dst_box.SetText(src_text) dst_box.SetText(src_text)
case tcell.KeyCtrlO: case tcell.KeyCtrlO:
// play source sound
if translator.soundLock.Available() {
message := src_box.GetText() message := src_box.GetText()
if len(message) > 0 { if len(message) > 0 {
translator.soundLock.Acquire()
go func() {
err := translator.PlaySound(translator.srcLang, message) err := translator.PlaySound(translator.srcLang, message)
if err != nil { if err != nil {
src_box.SetText(err.Error(), true) src_box.SetText(err.Error(), true)
} }
}()
}
} }
case tcell.KeyCtrlP: case tcell.KeyCtrlP:
// play destination sound
if translator.soundLock.Available() {
message := dst_box.GetText(false) message := dst_box.GetText(false)
if len(message) > 0 { if len(message) > 0 {
translator.soundLock.Acquire()
go func() {
err := translator.PlaySound(translator.dstLang, message) err := translator.PlaySound(translator.dstLang, message)
if err != nil { if err != nil {
dst_box.SetText(err.Error()) dst_box.SetText(err.Error())
} }
}()
} }
} }
case tcell.KeyCtrlX:
// stop play sound
translator.soundLock.stop = true
}
return event return event
} }

View File

@ -13,24 +13,3 @@ func SetTermTitle(title string) {
print("\033]0;", title, "\007") print("\033]0;", title, "\007")
} }
type Lock struct {
stop bool
threadCount int8
}
func NewLock() *Lock {
return &Lock{
stop: true,
threadCount: 0,
}
}
func (l *Lock) Aquire() {
l.stop = false
l.threadCount++
}
func (l *Lock) Release() {
l.stop = true
l.threadCount--
}