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 | if ((mediaPlaybackMode == "neutral") || (mediaPlaybackMode == "record")) {
|
---|
60 |
|
---|
61 |
|
---|
62 | // Apply Graphics Equalizer, and then play the result
|
---|
63 |
|
---|
64 |
|
---|
65 | if ((typeof grapheqL !== "undefined") && (typeof grapheqR !== "undefined")) {
|
---|
66 | if (c==0) {
|
---|
67 | // left
|
---|
68 | output.set(grapheqL.process(input));
|
---|
69 |
|
---|
70 | }
|
---|
71 | else {
|
---|
72 | // right
|
---|
73 | output.set(grapheqR.process(input));
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 | else {
|
---|
78 |
|
---|
79 | for (var i = 0; i < bufferSize; i++) {
|
---|
80 | if (InTheGroove) {
|
---|
81 | output[i] = input[i];
|
---|
82 | }
|
---|
83 | else {
|
---|
84 | // Make sound 'tinny' through simple high-pass filter
|
---|
85 | output[i] = input[i] - prev_input[c];
|
---|
86 | }
|
---|
87 |
|
---|
88 | prev_input[c] = input[i];
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | }
|
---|
93 |
|
---|
94 | }
|
---|
95 |
|
---|
96 | source.connect(tinnyEffectNode);
|
---|
97 | tinnyEffectNode.connect(audioCtx.destination);
|
---|
98 |
|
---|
99 | initGameOn();
|
---|
100 |
|
---|
101 | console.log("onLoadMetadata done");
|
---|
102 | });
|
---|
103 |
|
---|
104 | });
|
---|
105 |
|
---|
106 | function inTheGroove()
|
---|
107 | {
|
---|
108 | InTheGroove = true;
|
---|
109 | MissingTheGroovePending = false;
|
---|
110 | }
|
---|
111 |
|
---|
112 | function missingTheGroove()
|
---|
113 | {
|
---|
114 | if (MissingTheGroovePending) {
|
---|
115 | InTheGroove = false;
|
---|
116 | }
|
---|
117 | MissingTheGroovePending = false;
|
---|
118 | }
|
---|
119 |
|
---|
120 | function delayedMissingTheGroove(delay)
|
---|
121 | {
|
---|
122 | MissingTheGroovePending = true;
|
---|
123 | setTimeout(missingTheGroove,delay || 400);
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|