speech_improve.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // ==UserScript==
  2. // @name 语音增强
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description 通过替换闻达函数,提升语音功能,同时也用于演示如何用外部api提供语音服务
  6. // @author lyyyyy
  7. // @match http://127.0.0.1:17860/
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=0.1
  9. // @run-at document-idle
  10. // @grant none
  11. // ==/UserScript==
  12. let isSpeaking = false;
  13. speak = (s) => {
  14. msg = new SpeechSynthesisUtterance();
  15. msg.rate = 1;
  16. msg.pitch = 10;
  17. msg.text = s;
  18. msg.volume = 1;
  19. speechSynthesis.speak(msg)
  20. msg.onstart = (event) => {
  21. }
  22. msg.onend = (event) => {
  23. isSpeaking = false;
  24. }
  25. }
  26. stop_listen = () => {
  27. recognition.stop()
  28. app.loading = true
  29. }
  30. listen = () => {
  31. if (isSpeaking) return;
  32. recognition = new window.webkitSpeechRecognition;
  33. let final_transcript = '';
  34. recognition.continuous = true;
  35. recognition.interimResults = true;
  36. recognition.onstart = function () {
  37. };
  38. recognition.onresult = function (event) {
  39. let interim_transcript = '';
  40. for (var i = event.resultIndex; i < event.results.length; ++i) {
  41. if (event.results[i].isFinal) {
  42. final_transcript += event.results[i][0].transcript;
  43. console.log(final_transcript);
  44. app.question = final_transcript
  45. } else {
  46. interim_transcript += event.results[i][0].transcript;
  47. }
  48. }
  49. };
  50. recognition.onerror = function (e) {
  51. console.log(final_transcript);
  52. alert('语音识别失败:', e.error)
  53. app.sst_started = false
  54. console.log('======================' + "error" + '======================', e);
  55. };
  56. recognition.onend = function () {
  57. console.log(final_transcript);
  58. app.question = final_transcript
  59. if (final_transcript.length > 1)
  60. submit()
  61. app.sst_started = false
  62. console.log('======================' + "end" + '======================');
  63. }
  64. recognition.lang = "zh-CN";
  65. recognition.start()
  66. app.sst_started = true
  67. }
粤ICP备19079148号