1 // Copyright 2015 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.
6 play: function(win, frequency) {
7 win.sinewaveData_ = win.sinewaveData_ || {};
8 data = win.sinewaveData_;
10 if (!data.audioContext) {
11 data.audioContext = new AudioContext();
12 data.gainNode = data.audioContext.createGain();
13 data.gainNode.gain.value = 0.5;
14 data.gainNode.connect(data.audioContext.destination);
16 if (!data.oscillator ||
17 data.oscillator.frequency.value != frequency) {
19 // Note: We recreate the oscillator each time because this switches the
20 // audio frequency immediately. Re-using the same oscillator tends to
21 // take several hundred milliseconds to ramp-up/down the frequency.
22 if (data.oscillator) {
23 data.oscillator.stop();
24 data.oscillator.disconnect();
26 data.oscillator = data.audioContext.createOscillator();
27 data.oscillator.type = OscillatorNode.SINE;
28 data.oscillator.frequency.value = frequency;
29 data.oscillator.connect(data.gainNode);
31 data.oscillator.start();
35 if (win.sinewaveData_)
36 win.sinewaveData_.oscillator.stop();