Skip to content

Commit 73460b8

Browse files
committed
Add signal generator for no-mic operation.
1 parent d67dc5a commit 73460b8

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

presentation-main.js

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,74 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
5454
var fftarray = new Float32Array(binCount);
5555
var audioarray = new Float32Array(sampleCount);
5656

57+
var userGainNode = ctx.createGain();
58+
var userGainEl = document.getElementById('gain');
59+
function updateGain() {
60+
userGainNode.gain.value = Math.pow(10, userGainEl.valueAsNumber / 10);
61+
}
62+
updateGain();
63+
userGainEl.addEventListener('change', updateGain, false);
64+
userGainEl.addEventListener('input', updateGain, false);
65+
66+
var sources = Object.create(null);
67+
var sourceSelectEl = document.getElementById('source-select');
68+
sources['sig'] = (function() {
69+
var osc1 = ctx.createOscillator();
70+
osc1.frequency.value = sampleRate / 30;
71+
osc1.start();
72+
var gain1 = ctx.createGain();
73+
gain1.gain.value = 0.5;
74+
var osc2 = ctx.createOscillator();
75+
osc2.frequency.value = osc1.frequency.value * 5;
76+
osc2.start();
77+
var gain2 = ctx.createGain();
78+
gain2.gain.value = 0.5;
79+
osc1.connect(gain1);
80+
osc2.connect(gain2);
81+
gain2.connect(gain1);
82+
return gain1;
83+
}());
84+
var currentSource = null;
85+
function wireSource() {
86+
if (currentSource !== null) {
87+
currentSource.disconnect(fftnode);
88+
}
89+
currentSource = sources[sourceSelectEl.value] || null;
90+
if (currentSource === null && sourceSelectEl.value == 'user') {
91+
currentSource = sources['sig'];
92+
}
93+
console.log('switching to', sourceSelectEl.value, currentSource);
94+
if (currentSource !== null) {
95+
currentSource.connect(userGainNode);
96+
}
97+
}
98+
wireSource();
99+
sourceSelectEl.addEventListener('change', wireSource, false);
100+
101+
userGainNode.connect(fftnode);
102+
57103
var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
58104
if (!getUserMedia) {
59105
reportFailure('This browser does not support getUserMedia. No signal will be shown.');
60106
// don't abort, we can at least show the slides
61107
} else {
62108
getUserMedia.call(navigator, {audio: true}, function getUserMediaSuccess(stream) {
63109
var source = ctx.createMediaStreamSource(stream);
64-
source.connect(fftnode);
110+
sources['user'] = source;
111+
wireSource();
65112

66113
// https://bugzilla.mozilla.org/show_bug.cgi?id=934512
67114
// http://stackoverflow.com/q/22860468/99692
68115
// Firefox destroys the media stream source even though it is in use by the audio graph. As a workaround, make a powerless global reference to it.
116+
// TODO: moot now
69117
window[Math.random()] = function() { console.log(source); }
70-
}, reportFailure);
118+
}, function (error) {
119+
console.error('error response from getUserMedia:', error);
120+
var option = sourceSelectEl.querySelector('option[value="user"]');
121+
option.textContent += ' (unavailable)';
122+
option.disabled = true;
123+
if (sourceSelectEl.value === 'user') sourceSelectEl.value = 'sig';
124+
});
71125
}
72126

73127
var filterOuterCoeff = 1/3;

presentation.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@
4747
max-width: 50em;
4848
"></p>
4949

50+
<div id="controls" style="
51+
position: absolute; left: 0; top: 0; padding: 0.5em; display: table;
52+
">
53+
<select id="source-select" style="margin-right: 2em;">
54+
<option value="user" selected>Microphone Input</option>
55+
<option value="sig">Signal generator</option>
56+
</select>
57+
Gain <input type="range" id="gain" min="-10" max="20" value="0" step="any">
58+
</div>
59+
5060
<script src="deps/MathBox-bundle.min.js"></script>
5161
<script src="dsp.js"></script>
5262
<script src="presentation-main.js"></script>

0 commit comments

Comments
 (0)