-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
156 additions
and
126 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { PlaySpeed, Voices } from './model/constants.js' | ||
import { say, listen } from './speechEngine.js' | ||
import { comments } from './model/store.js' | ||
|
||
const sentences = [ | ||
{ text: 'こんにちは、ともさん', speed: PlaySpeed.slow, voice: Voices.jaM1 }, | ||
{ text: '今日はいい天気ですね', speed: PlaySpeed.slow, voice: Voices.jaF1 }, | ||
{ text: 'そうですね。', speed: PlaySpeed.slow, voice: Voices.jaM1 }, | ||
{ text: 'じゃあ、またね。', speed: PlaySpeed.slow, voice: Voices.jaF1 }, | ||
] | ||
|
||
export const playGame = async () => { | ||
for (let i in sentences) { | ||
let sentence = sentences[i] | ||
comments.update(x => [...x, { author: 'teacher', text: sentence.text }]) | ||
|
||
let duration = await say(sentence.text, sentence.speed, sentence.voice) | ||
|
||
// plus 600 ms | ||
let recognizedText = await listen(duration + 600) | ||
comments.update(x => [...x, { author: 'student', text: recognizedText }]) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import App from './App.svelte' | ||
import App from './ui/App.svelte' | ||
|
||
const app = new App({ | ||
target: document.body, | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export let PlaySpeed = { | ||
normal: 1.0, | ||
slow: 0.85, | ||
verySlow: 0.7 | ||
} | ||
|
||
export let Voices = { | ||
jaF1: 'jaF1', | ||
jaF2: 'jaF2', | ||
jaM1: 'jaM1', | ||
jaM2: 'jaM2', | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { writable } from 'svelte/store'; | ||
|
||
export const comments = writable([]) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
var recognition = new webkitSpeechRecognition() | ||
recognition.continuous = true | ||
recognition.interimResults = false | ||
|
||
export let listen = (duration) => { | ||
recognition.start(); | ||
setTimeout(() => { recognition.stop() }, duration) | ||
|
||
|
||
let promise = new Promise((resolve, reject) => { | ||
recognition.onresult = (event) => { | ||
for (var i = event.resultIndex; i < event.results.length; ++i) { | ||
resolve(event.results[i][0].transcript) | ||
} | ||
} | ||
recognition.onerror = () => { | ||
resolve("error") | ||
} | ||
}) | ||
return promise | ||
} | ||
|
||
function getMS() { | ||
return new Date().getTime() | ||
} | ||
|
||
export let say = async (text, speed, voice) => { | ||
var startMS = getMS() | ||
var response = await fetch(`/tts?text=${text}&voice=${voice}`) | ||
.then(res => | ||
res.text() | ||
) | ||
|
||
const audio = new Audio('data:audio/wav;base64,' + response) | ||
let promise = new Promise(function (resolve, reject) { | ||
audio.onended = () => { | ||
resolve(getMS() - startMS) | ||
} | ||
}) | ||
audio.play() | ||
audio.playbackRate = speed | ||
return promise | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<script> | ||
import { comments } from './model/store.js' | ||
import { beforeUpdate, afterUpdate } from 'svelte' | ||
let div | ||
let autoscroll | ||
beforeUpdate(() => { | ||
autoscroll = div && div.offsetHeight + div.scrollTop > div.scrollHeight - 20 | ||
}) | ||
afterUpdate(() => { | ||
if (autoscroll) div.scrollTo(0, div.scrollHeight) | ||
}) | ||
</script> | ||
<style> | ||
.chat { | ||
display: flex; | ||
flex-direction: column; | ||
height: 400px; | ||
max-width: 240px; | ||
border: 1px solid #eee; | ||
margin: 0px auto; | ||
} | ||
.scrollable { | ||
flex: 1 1 auto; | ||
margin: 0 0.5em; | ||
overflow-y: auto; | ||
} | ||
article { | ||
margin: 0.5em 0; | ||
} | ||
.student { | ||
text-align: right; | ||
} | ||
span { | ||
padding: 0.5em 1em; | ||
display: inline-block; | ||
} | ||
.teacher span { | ||
background-color: #eee; | ||
border-radius: 1em 1em 1em 0; | ||
} | ||
.student span { | ||
background-color: #0074d9; | ||
color: white; | ||
border-radius: 1em 1em 0 1em; | ||
} | ||
</style> | ||
|
||
<div class="chat"> | ||
<div class="scrollable" bind:this={div}> | ||
{#each $comments as comment} | ||
<article class={comment.author}> | ||
<span>{comment.text}</span> | ||
</article> | ||
{/each} | ||
</div> | ||
</div> |
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
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