Licensed under CC0 (https://creativecommons.org/publicdomain/zero/1.0/deed)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const API_KEY = 'ここにAPIキーを貼る'; | |
const url = `https://api.apigw.smt.docomo.ne.jp/aiTalk/v1/textToSpeech?APIKEY=${API_KEY}`; | |
document.addEventListener('onEventReceived', async function(obj) { | |
if (obj.detail.command == 'PRIVMSG') { | |
play(obj.detail.body, 'maki'); | |
} | |
}); | |
async function play(text, who) { | |
const result = await fetch(new URL(url), { | |
method: 'POST', | |
body: `<?xml version="1.0" encoding="utf-8" ?> | |
<speak version="1.1"><voice name="${who}">${text}</voice></speak>`, | |
headers: new Headers({ | |
'Content-Type': 'application/ssml+xml;charset=UTF-8', | |
'Accept': 'audio/L16', | |
}) | |
}); | |
const audioRawBuffer = await result.arrayBuffer(); | |
const audioDataView = new DataView(audioRawBuffer); | |
const audioContext = new AudioContext(); | |
const audioBuffer = audioContext.createBuffer(1, audioDataView.byteLength / 2, 16000); | |
const channel = audioBuffer.getChannelData(0); | |
for (let i = 0; i < audioDataView.byteLength; i += 2) { | |
const value = audioDataView.getInt16(i, false); | |
channel[i / 2] = value / 32768.0; | |
} | |
const source = audioContext.createBufferSource(); | |
source.buffer = audioBuffer; | |
source.connect(audioContext.destination); | |
source.start(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment