Azure Speech Download

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

2022-05-02 기준 버전입니다. 최신 버전을 확인하세요.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

You will need to install an extension such as Tampermonkey to install this script.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         Azure Speech Download
// @namespace
// @version      0.1
// @description  为微软的文本转语音服务的 demo 页面添加下载按钮
// @author       Puteulanus
// @homepage     https://greasyfork.org/zh-CN/scripts/444347-azure-speech-download
// @match        https://azure.microsoft.com/zh-cn/services/cognitive-services/text-to-speech/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=microsoft.com
// @require      https://cdn.bootcdn.net/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js
// @grant        none
// @run-at       document-end
// @namespace https://greasyfork.org/users/909438
// ==/UserScript==

/* globals saveAs */
/* jshint esversion: 6 */
(function() {
    'use strict';

    // Your code here...
    const SpeechSDK = window.SpeechSDK
    let fileSize = 0
    let wavFragments = []
    let enableDownload = false

    const downloadStatus = document.createElement('div')
    const downloadSize = document.createElement('div')
    const downloadButton = document.getElementById('playli').cloneNode(true)
    const buttonArea = document.getElementById('playli').parentElement

    downloadButton.id = ('donwloadli')
    downloadButton.querySelector('span svg').style.transform = 'rotate(90deg)'
    downloadButton.querySelector('span:last-of-type').textContent = '下载'
    downloadButton.querySelector('button').style.backgroundColor = 'green'
    downloadButton.querySelector('button').style.borderColor = 'green'
    downloadButton.addEventListener('click', () => {
        downloadStatus.textContent = '下载中'
        enableDownload = true
        document.getElementById('playbtn').click()
        enableDownload = false
    })
    downloadStatus.style.marginRight = '10px'
    buttonArea.appendChild(downloadButton)
    buttonArea.parentElement.appendChild(downloadStatus)
    buttonArea.parentElement.appendChild(downloadSize)

    const streamHandler = {
        write: function (dataBuffer) {
            fileSize += dataBuffer.byteLength
            downloadSize.textContent = `已接收 ${fileSize / 1000} kb`
            wavFragments.push(dataBuffer);
        },
        close: function () {
            downloadStatus.textContent = '下载完成'
            const sentAudio = new window.Uint8Array(fileSize)
            fileSize = 0
            wavFragments.reduce((size, fragment) => {
                sentAudio.set(new window.Uint8Array(fragment), size)
                return size + fragment.byteLength
            }, 0)
            wavFragments.length = 0
            saveAs(new Blob([sentAudio]), (new Date()).toISOString().replace('T', ' ').replace(':', '_').split('.')[0] + '.mp3')
        }
    }

    const outputStream = SpeechSDK.PushAudioOutputStream.create(streamHandler)

    SpeechSDK.AudioConfig.fromSpeakerOutput = (() => {
        const fromSpeakerOutput = SpeechSDK.AudioConfig.fromSpeakerOutput
        return function (audioDestination) {
            return enableDownload ? audioDestination.onAudioEnd() || SpeechSDK.AudioConfig.fromStreamOutput(outputStream) : fromSpeakerOutput(audioDestination)
        }
    })()
})();