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