Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

usePrefetchPronunciationSound(nextWord?.name) 會導致讀錯下一單字。 #896

Open
raychunghk opened this issue Jan 4, 2025 · 1 comment

Comments

@raychunghk
Copy link

raychunghk commented Jan 4, 2025

  1. 我在玩自製的泰語練習字庫。
    3000_common_volubilis_thai3.json

2 我用自製的google translate TTS 語音api.

  1. 把 usePronounciation 中的generateWordSoundSrc加一個th

export function generateWordSoundSrc(word: string, pronunciation: Exclude<PronunciationType, false>): string {
 console.log(`word:${word}`)
 switch (pronunciation) {
   case 'uk':
     return `${pronunciationApi}${word}&type=1`
   case 'us':
     return `${pronunciationApi}${word}&type=2`
   case 'romaji':
     return `${pronunciationApi}${romajiToHiragana(word)}&le=jap`
   case 'zh':
     return `${pronunciationApi}${word}&le=zh`
   case 'ja':
     return `${pronunciationApi}${word}&le=jap`
   case 'de':
     return `${pronunciationApi}${word}&le=de`
   case 'hapin':
   case 'kk':
     return `${pronunciationApi}${word}&le=ru` // 有道不支持哈萨克语, 暂时用俄语发音兜底
   case 'id':
     return `${pronunciationApi}${word}&le=id`
   case 'th':
     return `https://code2.raygor.cc/proxy/5000/tts?text=${word}`
   default:
     return ''
 }
}
  1. ditionary.ts也加一個。

const thaiDicts: DictionaryResource[] = [
  {
    id: 'thai_001',
    name: 'Thai_Eng_volubilisIPA3',
    description: 'Thai_Eng_volubilisIPA3',
    category: '泰语',
    tags: ['基础3'],
    url: '/dicts/3000_common_volubilis_thai3.json',
    length: 2768,
    language: 'th',
    languageCategory: 'th',
  },
]
/**
 * Built-in dictionaries in an array.
 * Why arrays? Because it keeps the order across browsers.
 */
export const dictionaryResources: DictionaryResource[] = [
  ...chinaExam,
  ...internationalExam,
  ...childrenEnglish,
  ...programming,
  ...japaneseExam,
  ...germanExam,
  ...kazakhHapinDicts,
  ...indonesianDicts,
  ...thaiDicts,

 
]

5.然後試下。除了每節第一個字配對了,之二個字之後都讀出N+1的讀音。
6. 把。usePrefetchPronunciationSound(nextWord?.name) 取消後變正常可用。

以下是gtts的自製 讀音API


 from flask import Flask, request, send_file
from gtts import gTTS
import os
from datetime import datetime
import time

app = Flask(__name__)

@app.route('/tts', methods=['GET'])
def tts():
    # Get the 'text' parameter from the GET request
    text = request.args.get('text')
    
    if not text:
        return {"error": "Missing 'text' parameter"}, 400

    # Create a timestamped filename
    timestamp = datetime.now().strftime("%d%m%y_%H%M%S")
    filename = f"gttsmp3_{timestamp}.mp3"

    try:
        # Convert the text to Thai speech using gTTS
        tts = gTTS(text, lang="th")
        
        # Save the audio to a temporary file
        tts.save(filename)
        
        # Wait until the file is completely saved (optional)
        time.sleep(1)  # Simple wait, could be adjusted or improved

        # Send the MP3 file as a response
        return send_file(filename, as_attachment=False)

    except ValueError as ve:
        return {"error": f"Value error: {str(ve)}"}, 400
    except IOError as ie:
        return {"error": f"File I/O error: {str(ie)}"}, 500
    except Exception as e:
        return {"error": f"An unexpected error occurred: {str(e)}"}, 500

  # finally:
        # Clean up the temporary file if it exists
     #   if os.path.exists(filename):
        #   os.remove(filename)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)
@raychunghk
Copy link
Author

raychunghk commented Jan 4, 2025

不好意思,重現需要運行文中的python api,
然後把這個改成本機的api url
https://tts.raygor.cc/{word}.mp3?lang=th
to
{我本機的tts api url ?}

e.g
https://tts.raygor.cc/%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%97%E0%B9%88%E0%B8%AD%E0%B8%87%E0%B9%80%E0%B8%97%E0%B8%B5%E0%B9%88%E0%B8%A2%E0%B8%A7.mp3?lang=th

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant