1 | //"use strict";
|
---|
2 |
|
---|
3 | // https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createScriptProcessor
|
---|
4 |
|
---|
5 | var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
---|
6 |
|
---|
7 | //if( 'webkitAudioContext' in window) {
|
---|
8 | // console.log("*** Web Audio found in webkit namespace");
|
---|
9 | // audioCtx = new webkitAudioContext();
|
---|
10 | //}
|
---|
11 |
|
---|
12 | var InTheGroove = false;
|
---|
13 | var MissingTheGroovePending = false;
|
---|
14 |
|
---|
15 | //var bufferSize = 4096;
|
---|
16 | var bufferSize = 512;
|
---|
17 |
|
---|
18 | var prev_input = [0,0];
|
---|
19 |
|
---|
20 | var tinnyEffectNode = null;
|
---|
21 |
|
---|
22 | $(document).ready(function() {
|
---|
23 | $('#video').on('loadedmetadata', function() {
|
---|
24 |
|
---|
25 | displayDuration(this.duration);
|
---|
26 |
|
---|
27 | // Create a MediaElementAudioSourceNode
|
---|
28 | // Feed the HTMLMediaElement into it
|
---|
29 | var source = audioCtx.createMediaElementSource(this);
|
---|
30 | //console.log("**** video source = " + source);
|
---|
31 |
|
---|
32 | // connect the AudioBufferSourceNode to the tinnyEffectNode
|
---|
33 | // and the tinnyEffectNode to the destination, so we can play the
|
---|
34 | // music and have the filter 'kick in' when 'Missing Out' button
|
---|
35 | // pressed
|
---|
36 |
|
---|
37 | tinnyEffectNode = audioCtx.createScriptProcessor(bufferSize, 2, 2);
|
---|
38 | tinnyEffectNode.onaudioprocess = function(e) {
|
---|
39 |
|
---|
40 | for (var c = 0; c<2; c++) {
|
---|
41 | var input = e.inputBuffer.getChannelData(c);
|
---|
42 | var output = e.outputBuffer.getChannelData(c);
|
---|
43 |
|
---|
44 |
|
---|
45 | for (var i = 0; i < bufferSize; i++) {
|
---|
46 | if ( ((mediaPlaybackMode == "neutral") || (mediaPlaybackMode == "record"))
|
---|
47 | || InTheGroove) {
|
---|
48 | output[i] = input[i];
|
---|
49 | }
|
---|
50 | else {
|
---|
51 | // Make sound 'tinny' through simple high-pass filter
|
---|
52 | output[i] = input[i] - prev_input[c];
|
---|
53 | }
|
---|
54 |
|
---|
55 | prev_input[c] = input[i];
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | source.connect(tinnyEffectNode);
|
---|
61 | tinnyEffectNode.connect(audioCtx.destination);
|
---|
62 |
|
---|
63 | initGameOn();
|
---|
64 |
|
---|
65 | console.log("onLoadMetadata done");
|
---|
66 | });
|
---|
67 |
|
---|
68 | });
|
---|
69 |
|
---|
70 | function inTheGroove()
|
---|
71 | {
|
---|
72 | InTheGroove = true;
|
---|
73 | MissingTheGroovePending = false;
|
---|
74 | }
|
---|
75 |
|
---|
76 | function missingTheGroove()
|
---|
77 | {
|
---|
78 | if (MissingTheGroovePending) {
|
---|
79 | InTheGroove = false;
|
---|
80 | }
|
---|
81 | MissingTheGroovePending = false;
|
---|
82 | }
|
---|
83 |
|
---|
84 | function delayedMissingTheGroove(delay)
|
---|
85 | {
|
---|
86 | MissingTheGroovePending = true;
|
---|
87 | setTimeout(missingTheGroove,delay || 400);
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|