SpeechRecognition-mediaStreamTrack-manual.https.html (2044B)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <title>SpeechRecognition MediaStreamTrack</title> 4 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 8 <script> 9 async function getAudioTrackFromFile(filePath) { 10 const audioContext = new AudioContext(); 11 const response = await fetch(filePath); 12 const arrayBuffer = await response.arrayBuffer(); 13 const audioBuffer = await audioContext.decodeAudioData(arrayBuffer); 14 const source = audioContext.createBufferSource(); 15 source.buffer = audioBuffer; 16 17 const destination = audioContext.createMediaStreamDestination(); 18 source.connect(destination); 19 source.start(); 20 21 return destination.stream.getAudioTracks()[0]; 22 } 23 24 promise_test(async (t) => { 25 const lang = "en-US"; 26 window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; 27 const speechRecognition = new SpeechRecognition(); 28 speechRecognition.processLocally = false; 29 speechRecognition.lang = lang; 30 const audioTrack = await getAudioTrackFromFile("/media/speech.wav"); 31 32 assert_true(audioTrack instanceof MediaStreamTrack, "Audio track should be a valid MediaStreamTrack"); 33 34 const recognitionPromise = new Promise((resolve) => { 35 speechRecognition.onresult = (event) => { 36 const transcript = event.results[0][0].transcript; 37 resolve(transcript); 38 }; 39 }); 40 41 speechRecognition.start(audioTrack); 42 43 const transcript = await recognitionPromise; 44 assert_equals(transcript.toLowerCase(), "this is a sentence in a single segment", "Speech recognition should correctly recognize 'hello world'"); 45 46 // Start speech recognition without a media stream track on the same instance should fail. 47 try { 48 speechRecognition.start(); 49 assert_unreached(); 50 } catch (e) { 51 assert_equals(e.name, "InvalidStateError", "Second call to start() should throw an InvalidStateError"); 52 } 53 }, "SpeechRecognition should recognize speech from an audio file."); 54 </script> 55 </html>