Azure Speech Download

为微软的文本转语音服务的 demo 页面添加下载按钮

当前为 2022-05-02 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Azure Speech Download
  3. // @namespace
  4. // @version 0.1
  5. // @description 为微软的文本转语音服务的 demo 页面添加下载按钮
  6. // @author Puteulanus
  7. // @homepage https://greasyfork.org/zh-CN/scripts/444347-azure-speech-download
  8. // @match https://azure.microsoft.com/zh-cn/services/cognitive-services/text-to-speech/
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=microsoft.com
  10. // @require https://cdn.bootcdn.net/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js
  11. // @grant none
  12. // @run-at document-end
  13. // @namespace https://greasyfork.org/users/909438
  14. // ==/UserScript==
  15.  
  16. /* globals saveAs */
  17. /* jshint esversion: 6 */
  18. (function() {
  19. 'use strict';
  20.  
  21. // Your code here...
  22. const SpeechSDK = window.SpeechSDK
  23. let fileSize = 0
  24. let wavFragments = []
  25. let enableDownload = false
  26.  
  27. const downloadStatus = document.createElement('div')
  28. const downloadSize = document.createElement('div')
  29. const downloadButton = document.getElementById('playli').cloneNode(true)
  30. const buttonArea = document.getElementById('playli').parentElement
  31.  
  32. downloadButton.id = ('donwloadli')
  33. downloadButton.querySelector('span svg').style.transform = 'rotate(90deg)'
  34. downloadButton.querySelector('span:last-of-type').textContent = '下载'
  35. downloadButton.querySelector('button').style.backgroundColor = 'green'
  36. downloadButton.querySelector('button').style.borderColor = 'green'
  37. downloadButton.addEventListener('click', () => {
  38. downloadStatus.textContent = '下载中'
  39. enableDownload = true
  40. document.getElementById('playbtn').click()
  41. enableDownload = false
  42. })
  43. downloadStatus.style.marginRight = '10px'
  44. buttonArea.appendChild(downloadButton)
  45. buttonArea.parentElement.appendChild(downloadStatus)
  46. buttonArea.parentElement.appendChild(downloadSize)
  47.  
  48. const streamHandler = {
  49. write: function (dataBuffer) {
  50. fileSize += dataBuffer.byteLength
  51. downloadSize.textContent = `已接收 ${fileSize / 1000} kb`
  52. wavFragments.push(dataBuffer);
  53. },
  54. close: function () {
  55. downloadStatus.textContent = '下载完成'
  56. const sentAudio = new window.Uint8Array(fileSize)
  57. fileSize = 0
  58. wavFragments.reduce((size, fragment) => {
  59. sentAudio.set(new window.Uint8Array(fragment), size)
  60. return size + fragment.byteLength
  61. }, 0)
  62. wavFragments.length = 0
  63. saveAs(new Blob([sentAudio]), (new Date()).toISOString().replace('T', ' ').replace(':', '_').split('.')[0] + '.mp3')
  64. }
  65. }
  66.  
  67. const outputStream = SpeechSDK.PushAudioOutputStream.create(streamHandler)
  68.  
  69. SpeechSDK.AudioConfig.fromSpeakerOutput = (() => {
  70. const fromSpeakerOutput = SpeechSDK.AudioConfig.fromSpeakerOutput
  71. return function (audioDestination) {
  72. return enableDownload ? audioDestination.onAudioEnd() || SpeechSDK.AudioConfig.fromStreamOutput(outputStream) : fromSpeakerOutput(audioDestination)
  73. }
  74. })()
  75. })();