v/GTT
1
0
mirror of https://github.com/eeeXun/GTT.git synced 2025-05-18 16:50:14 -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"
)
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 {
srcLang string
dstLang string
soundLock *Lock
}
func NewTranslator() Translator {
return Translator{
func NewTranslator() *Translator {
return &Translator{
soundLock: NewLock(),
}
}
func (t Translator) Translate(message string) (string, error) {
func (t *Translator) Translate(message string) (string, error) {
var data []interface{}
var translated string
@ -63,7 +89,7 @@ func (t Translator) Translate(message string) (string, error) {
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(
soundURL,
url.QueryEscape(message),
@ -71,25 +97,35 @@ func (t Translator) PlaySound(lang string, message string) error {
)
res, err := http.Get(url_str)
if err != nil {
t.soundLock.Release()
return err
}
decoder, err := mp3.NewDecoder(res.Body)
if err != nil {
t.soundLock.Release()
return err
}
otoCtx, readyChan, err := oto.NewContext(decoder.SampleRate(), 2, 2)
if err != nil {
t.soundLock.Release()
return err
}
<-readyChan
player := otoCtx.NewPlayer(decoder)
player.Play()
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 {
t.soundLock.Release()
return err
}
t.soundLock.Release()
return nil
}

36
ui.go
View File

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

View File

@ -13,24 +13,3 @@ func SetTermTitle(title string) {
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--
}