Showing
2 changed files
with
37 additions
and
9 deletions
| ... | @@ -8,6 +8,9 @@ import { WatchOutlined } from "@material-ui/icons"; | ... | @@ -8,6 +8,9 @@ import { WatchOutlined } from "@material-ui/icons"; |
| 8 | import io from 'socket.io-client' | 8 | import io from 'socket.io-client' |
| 9 | import tts from './tts'; | 9 | import tts from './tts'; |
| 10 | 10 | ||
| 11 | +// audioQueue에서 하나씩 뽑아서 재생한다. | ||
| 12 | +tts.enableAudio() | ||
| 13 | + | ||
| 11 | const socket = io.connect("http://localhost:3303") | 14 | const socket = io.connect("http://localhost:3303") |
| 12 | socket.on("connect", event=>{ | 15 | socket.on("connect", event=>{ |
| 13 | // 테스트용으로 umi0410에게 입장. | 16 | // 테스트용으로 umi0410에게 입장. |
| ... | @@ -19,7 +22,8 @@ socket.on("connect", event=>{ | ... | @@ -19,7 +22,8 @@ socket.on("connect", event=>{ |
| 19 | socket.on('chat message', (name, msg)=>{ | 22 | socket.on('chat message', (name, msg)=>{ |
| 20 | console.log(msg) | 23 | console.log(msg) |
| 21 | console.log("got message") | 24 | console.log("got message") |
| 22 | - tts.speak(msg) | 25 | + //tts를 audio로 변경해 audioQueue에 집어넣는다. |
| 26 | + tts.enqueueTTS(msg) | ||
| 23 | }) | 27 | }) |
| 24 | 28 | ||
| 25 | // 해당 채널을 재생하고자할 때 실행시켜주세요. | 29 | // 해당 채널을 재생하고자할 때 실행시켜주세요. | ... | ... |
| ... | @@ -7,7 +7,29 @@ const polly = new AWS.Polly({ | ... | @@ -7,7 +7,29 @@ const polly = new AWS.Polly({ |
| 7 | region: 'ap-northeast-2', | 7 | region: 'ap-northeast-2', |
| 8 | }); | 8 | }); |
| 9 | 9 | ||
| 10 | -function speak(text){ | 10 | +const audioQueue = [] |
| 11 | +// test data | ||
| 12 | +// const audioQueue = ["https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3", "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3", "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3"] | ||
| 13 | + | ||
| 14 | +const audio = new Audio() | ||
| 15 | + | ||
| 16 | +// queue에서 작업(재생할 내용)을 0.5초마다 폴링하는 방식 | ||
| 17 | +function enableAudio(){ | ||
| 18 | + setInterval(()=>{ | ||
| 19 | + // 앞선 tts 재생이 종료된 경우에만 새 tts를 실행 | ||
| 20 | + if( !audio.paused){ return;} | ||
| 21 | + | ||
| 22 | + let audioFile = audioQueue.shift() | ||
| 23 | + // 재생할 게 있으면 재생 | ||
| 24 | + if (audioFile){ | ||
| 25 | + console.log(audioFile) | ||
| 26 | + audio.setAttribute("src", audioFile) | ||
| 27 | + audio.play() | ||
| 28 | + } | ||
| 29 | + }, 500) | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +function enqueueTTS(text){ | ||
| 11 | let params = { | 33 | let params = { |
| 12 | 'Text': text, | 34 | 'Text': text, |
| 13 | 'OutputFormat': 'mp3', | 35 | 'OutputFormat': 'mp3', |
| ... | @@ -19,15 +41,17 @@ function speak(text){ | ... | @@ -19,15 +41,17 @@ function speak(text){ |
| 19 | tts.getSynthesizeSpeechUrl(params, function(error, url) { | 41 | tts.getSynthesizeSpeechUrl(params, function(error, url) { |
| 20 | if (error) { | 42 | if (error) { |
| 21 | } else { | 43 | } else { |
| 22 | - setTimeout(()=>{ | 44 | + audioQueue.push(url) |
| 23 | - console.log("실행") | ||
| 24 | - let audio = new Audio(url) | ||
| 25 | - audio.play() | ||
| 26 | - // .then(delete audio); | ||
| 27 | - }, 3000) | ||
| 28 | } | 45 | } |
| 29 | }) | 46 | }) |
| 30 | } | 47 | } |
| 48 | + | ||
| 49 | +console.log("테스트용 TTS 데이터를 주입합니다.") | ||
| 50 | +enqueueTTS("복잡한 브랜치") | ||
| 51 | +enqueueTTS("빵상 아주머니는 말씀하셨다.") | ||
| 52 | +enqueueTTS("니가가라 하와이") | ||
| 53 | + | ||
| 31 | export default { | 54 | export default { |
| 32 | - "speak": speak, | 55 | + enableAudio, |
| 56 | + enqueueTTS, | ||
| 33 | } | 57 | } | ... | ... |
-
Please register or login to post a comment