|
1 | 1 | <!DOCTYPE html> |
2 | 2 | <html lang="en"> |
3 | 3 | <head> |
4 | | - <meta charset="UTF-8"> |
5 | | - <title>Speech Detection</title> |
| 4 | +<meta charset="UTF-8"> |
| 5 | +<title>Speech Detection</title> |
6 | 6 | </head> |
7 | 7 | <body> |
8 | 8 |
|
9 | | - <div class="words" contenteditable> |
10 | | - </div> |
| 9 | +<div class="words" contenteditable></div> |
11 | 10 |
|
12 | 11 | <script> |
13 | | - window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; |
14 | 12 |
|
| 13 | +window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; |
15 | 14 |
|
16 | | -</script> |
| 15 | +const recog = new SpeechRecognition(); |
| 16 | +recog.interimResults = true; |
17 | 17 |
|
| 18 | +let p = document.createElement("p"); |
| 19 | +const words = document.querySelector(".words"); |
| 20 | +words.appendChild(p); |
18 | 21 |
|
19 | | - <style> |
20 | | - html { |
21 | | - font-size: 10px; |
| 22 | +recog.addEventListener("result", e => { |
| 23 | + // console.log(e.results); |
| 24 | + const transcript = Array.from(e.results) |
| 25 | + .map(result => result[0]) |
| 26 | + .map(result => result.transcript) |
| 27 | + .join(''); |
| 28 | + p.textContent = transcript; |
| 29 | + if (e.results[0].isFinal) { |
| 30 | + p = document.createElement('p'); |
| 31 | + words.appendChild(p); |
22 | 32 | } |
| 33 | + console.log(transcript); |
| 34 | +}) |
23 | 35 |
|
24 | | - body { |
25 | | - background:#ffc600; |
26 | | - font-family: 'helvetica neue'; |
27 | | - font-weight: 200; |
28 | | - font-size: 20px; |
29 | | - } |
| 36 | +recog.addEventListener("end", recog.start); |
30 | 37 |
|
31 | | - .words { |
32 | | - max-width:500px; |
33 | | - margin:50px auto; |
34 | | - background:white; |
35 | | - border-radius:5px; |
36 | | - box-shadow:10px 10px 0 rgba(0,0,0,0.1); |
37 | | - padding:1rem 2rem 1rem 5rem; |
38 | | - background: -webkit-gradient(linear, 0 0, 0 100%, from(#d9eaf3), color-stop(4%, #fff)) 0 4px; |
39 | | - background-size: 100% 3rem; |
40 | | - position: relative; |
41 | | - line-height:3rem; |
42 | | - } |
43 | | - p { |
44 | | - margin: 0 0 3rem 0; |
45 | | - } |
| 38 | +recog.start(); |
46 | 39 |
|
47 | | - .words:before { |
48 | | - content: ''; |
49 | | - position: absolute; |
50 | | - width: 4px; |
51 | | - top: 0; |
52 | | - left: 30px; |
53 | | - bottom: 0; |
54 | | - border: 1px solid; |
55 | | - border-color: transparent #efe4e4; |
56 | | - } |
57 | | - </style> |
| 40 | +</script> |
| 41 | + |
| 42 | + |
| 43 | +<style> |
| 44 | + |
| 45 | +html { |
| 46 | + font-size: 10px; |
| 47 | +} |
| 48 | + |
| 49 | +body { |
| 50 | + background:#ffc600; |
| 51 | + font-family: 'helvetica neue'; |
| 52 | + font-weight: 200; |
| 53 | + font-size: 20px; |
| 54 | +} |
| 55 | + |
| 56 | +.words { |
| 57 | + max-width:500px; |
| 58 | + margin:50px auto; |
| 59 | + background:white; |
| 60 | + border-radius:5px; |
| 61 | + box-shadow:10px 10px 0 rgba(0,0,0,0.1); |
| 62 | + padding:1rem 2rem 1rem 5rem; |
| 63 | + background: -webkit-gradient(linear, 0 0, 0 100%, from(#d9eaf3), color-stop(4%, #fff)) 0 4px; |
| 64 | + background-size: 100% 3rem; |
| 65 | + position: relative; |
| 66 | + line-height:3rem; |
| 67 | +} |
| 68 | + |
| 69 | +p { |
| 70 | + margin: 0 0 3rem 0; |
| 71 | +} |
| 72 | + |
| 73 | +.words:before { |
| 74 | + content: ''; |
| 75 | + position: absolute; |
| 76 | + width: 4px; |
| 77 | + top: 0; |
| 78 | + left: 30px; |
| 79 | + bottom: 0; |
| 80 | + border: 1px solid; |
| 81 | + border-color: transparent #efe4e4; |
| 82 | +} |
| 83 | + |
| 84 | +</style> |
58 | 85 |
|
59 | 86 | </body> |
60 | 87 | </html> |
0 commit comments