Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(815)

Side by Side Diff: chrome/browser/resources/app_list/audio_manager.js

Issue 992173002: Delete the old hotwording integration. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@hotword-remove-disable-option
Patch Set: Rebase. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.
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 STOPPED: 0,
19 LISTENING: 1
20 };
21
22 /**
23 * @constructor
24 * @extends {cr.EventTarget}
25 */
26 function AudioManager() {
27 var audioContext = new window.AudioContext();
28 this.sampleRate = audioContext.sampleRate;
29 this.audioProc_ = null;
30 this.audioIn_ = null;
31 this.stream_ = null;
32 this.state = AudioState.STOPPED;
33 };
34
35 AudioManager.prototype.__proto__ = cr.EventTarget.prototype;
36
37 /**
38 * Called when the audio data arrives.
39 *
40 * @param {Event} audioEvent The audio event.
41 * @private
42 */
43 AudioManager.prototype.onAudioProcess_ = function(audioEvent) {
44 var data = audioEvent.inputBuffer.getChannelData(0);
45 var intData = new Int16Array(data.length);
46 for (var i = 0; i < data.length; ++i)
47 intData[i] = Math.round(data[i] * 32767);
48 var event = new Event('audio');
49 event.data = intData;
50 this.dispatchEvent(event);
51 };
52
53 /**
54 * Called when the audio stream is ready.
55 *
56 * @param {MediaStream} stream The media stream which is now available.
57 * @private
58 */
59 AudioManager.prototype.onAudioReady_ = function(stream) {
60 var audioContext = new window.AudioContext();
61 this.stream_ = stream;
62 this.audioIn_ = audioContext.createMediaStreamSource(stream);
63 this.audioProc_ = audioContext.createScriptProcessor(
64 4096 /* buffer size */, 1 /* channels */, 1 /* channels */);
65 this.audioProc_.onaudioprocess = this.onAudioProcess_.bind(this);
66
67 this.audioIn_.connect(this.audioProc_);
68 this.audioProc_.connect(audioContext.destination);
69 this.state = AudioState.LISTENING;
70 };
71
72 /**
73 * Starts the audio processing.
74 */
75 AudioManager.prototype.start = function() {
76 if (this.state == AudioState.LISTENING)
77 return;
78
79 navigator.webkitGetUserMedia(
80 {audio: true},
81 this.onAudioReady_.bind(this),
82 function(msg) { console.error('Failed to getUserMedia: ' + msg); });
83 };
84
85 /**
86 * Stops the audio processing.
87 */
88 AudioManager.prototype.stop = function() {
89 if (this.state != AudioState.LISTENING)
90 return;
91 this.audioProc_.disconnect();
92 this.audioIn_.disconnect();
93 var audioTracks = this.stream_.getAudioTracks();
94 for (var i = 0; i < audioTracks.length; ++i) {
95 audioTracks[i].stop();
96 }
97 this.audioProc_ = null;
98 this.audioIn_ = null;
99 this.stream_ = null;
100 this.state = AudioState.STOPPED;
101 };
102
103 return {
104 AudioManager: AudioManager,
105 AudioState: AudioState
106 };
107 });
OLDNEW
« no previous file with comments | « chrome/browser/media/media_stream_capture_indicator.cc ('k') | chrome/browser/resources/app_list/hotword_nacl.nmf » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698