OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @fileoverview The manager of audio streams and interaction with the plugin. |
| 7 */ |
| 8 |
| 9 cr.define('speech', function() { |
| 10 'use strict'; |
| 11 |
| 12 /** |
| 13 * The enum of the status of hotword audio recognition. |
| 14 * |
| 15 * @enum {number} |
| 16 */ |
| 17 var AudioState = { |
| 18 UNINITIALIZED: 0, |
| 19 READY: 1, |
| 20 RECOGNIZING: 2 |
| 21 }; |
| 22 |
| 23 /** |
| 24 * @constructor |
| 25 */ |
| 26 function AudioManager(onReady, onRecognizing, onRecognized) { |
| 27 this.state = AudioState.UNINITIALIZED; |
| 28 if (!speech.isPluginAvailable()) |
| 29 return; |
| 30 this.onReady_ = onReady; |
| 31 this.onRecognizing_ = onRecognizing; |
| 32 this.pluginManager_ = new speech.PluginManager( |
| 33 this.onPluginReady_.bind(this), onRecognized); |
| 34 this.audioContext_ = new window.webkitAudioContext(); |
| 35 this.audioProc_ = null; |
| 36 this.pluginManager_.scheduleInitialize( |
| 37 this.audioContext_.sampleRate, |
| 38 'chrome://app-list/okgoogle_hotword.config'); |
| 39 }; |
| 40 |
| 41 /** |
| 42 * Called when the plugin is ready. |
| 43 * |
| 44 * @private |
| 45 */ |
| 46 AudioManager.prototype.onPluginReady_ = function() { |
| 47 this.state = AudioState.READY; |
| 48 this.onReady_(); |
| 49 }; |
| 50 |
| 51 /** |
| 52 * Called when the audio data arrives. |
| 53 * |
| 54 * @param {Event} audioEvent The audio event. |
| 55 * @private |
| 56 */ |
| 57 AudioManager.prototype.onAudioProcess_ = function(audioEvent) { |
| 58 var data = audioEvent.inputBuffer.getChannelData(0); |
| 59 var intData = new Int16Array(data.length); |
| 60 for (var i = 0; i < data.length; ++i) |
| 61 intData[i] = Math.round(data[i] * 32767); |
| 62 this.pluginManager_.sendAudioData(intData.buffer); |
| 63 }; |
| 64 |
| 65 /** |
| 66 * Called when the audio stream is ready. |
| 67 * |
| 68 * @param {MediaStream} stream The media stream which is now available. |
| 69 * @private |
| 70 */ |
| 71 AudioManager.prototype.onAudioReady_ = function(stream) { |
| 72 var audioIn = this.audioContext_.createMediaStreamSource(stream); |
| 73 this.audioProc_ = this.audioContext_.createScriptProcessor( |
| 74 4096 /* buffer size */, 1 /* channels */, 1 /* channels */); |
| 75 this.audioProc_.onaudioprocess = this.onAudioProcess_.bind(this); |
| 76 |
| 77 audioIn.connect(this.audioProc_); |
| 78 this.audioProc_.connect(this.audioContext_.destination); |
| 79 this.state = AudioState.RECOGNIZING; |
| 80 this.onRecognizing_(); |
| 81 }; |
| 82 |
| 83 /** |
| 84 * Starts the audio recognition with the plugin. |
| 85 */ |
| 86 AudioManager.prototype.start = function() { |
| 87 // Not yet initialized. |
| 88 if (this.state != AudioState.READY) |
| 89 return; |
| 90 if (this.pluginManager_.state < speech.PluginState.READY) |
| 91 return; |
| 92 |
| 93 if (this.pluginManager_.state == speech.PluginState.READY) |
| 94 this.pluginManager_.startRecognizer(); |
| 95 |
| 96 if (this.audioProc_) { |
| 97 this.audioProc_.connect(this.audioContext_.destination); |
| 98 this.state = AudioState.RECOGNIZING; |
| 99 this.onRecognizing_(); |
| 100 return; |
| 101 } |
| 102 |
| 103 navigator.webkitGetUserMedia( |
| 104 {audio: true}, |
| 105 this.onAudioReady_.bind(this), |
| 106 function(msg) { console.error('Failed to getUserMedia: ' + msg); }); |
| 107 }; |
| 108 |
| 109 /** |
| 110 * Stops the audio recognition. |
| 111 */ |
| 112 AudioManager.prototype.stop = function() { |
| 113 if (this.state <= AudioState.READY) |
| 114 return; |
| 115 this.audioProc_.disconnect(); |
| 116 this.pluginManager_.stopRecognizer(); |
| 117 this.state = AudioState.READY; |
| 118 }; |
| 119 |
| 120 return { |
| 121 AudioManager: AudioManager |
| 122 }; |
| 123 }); |
OLD | NEW |