@@ -54,20 +54,74 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
54
54
var fftarray = new Float32Array ( binCount ) ;
55
55
var audioarray = new Float32Array ( sampleCount ) ;
56
56
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
+
57
103
var getUserMedia = navigator . getUserMedia || navigator . webkitGetUserMedia || navigator . mozGetUserMedia || navigator . msGetUserMedia ;
58
104
if ( ! getUserMedia ) {
59
105
reportFailure ( 'This browser does not support getUserMedia. No signal will be shown.' ) ;
60
106
// don't abort, we can at least show the slides
61
107
} else {
62
108
getUserMedia . call ( navigator , { audio : true } , function getUserMediaSuccess ( stream ) {
63
109
var source = ctx . createMediaStreamSource ( stream ) ;
64
- source . connect ( fftnode ) ;
110
+ sources [ 'user' ] = source ;
111
+ wireSource ( ) ;
65
112
66
113
// https://bugzilla.mozilla.org/show_bug.cgi?id=934512
67
114
// http://stackoverflow.com/q/22860468/99692
68
115
// 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
69
117
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
+ } ) ;
71
125
}
72
126
73
127
var filterOuterCoeff = 1 / 3 ;
0 commit comments