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 | displayDuration(this.duration);
|
---|
22 |
|
---|
23 | // Create a MediaElementAudioSourceNode
|
---|
24 | // Feed the HTMLMediaElement into it
|
---|
25 | var source = audioCtx.createMediaElementSource(this);
|
---|
26 |
|
---|
27 | // connect the AudioBufferSourceNode to the tinnyEffectNode
|
---|
28 | // and the tinnyEffectNode to the destination, so we can play the
|
---|
29 | // music and have the filter 'kick in' when 'Missing Out' button
|
---|
30 | // pressed
|
---|
31 |
|
---|
32 | var tinnyEffectNode = audioCtx.createScriptProcessor(bufferSize, 2, 2);
|
---|
33 | tinnyEffectNode.onaudioprocess = function(e) {
|
---|
34 |
|
---|
35 | for (var c = 0; c<2; c++) {
|
---|
36 | var input = e.inputBuffer.getChannelData(c);
|
---|
37 | var output = e.outputBuffer.getChannelData(c);
|
---|
38 |
|
---|
39 |
|
---|
40 | for (var i = 0; i < bufferSize; i++) {
|
---|
41 | if (InTheGroove) {
|
---|
42 | if (c==0) {
|
---|
43 | output[i] = input[i];
|
---|
44 | }
|
---|
45 | else {
|
---|
46 | output[i] = 0;
|
---|
47 | }
|
---|
48 | }
|
---|
49 | else {
|
---|
50 | // Make sound 'tinny' through simple high-pass filter
|
---|
51 | output[i] = input[i] - prev_input[c];
|
---|
52 | }
|
---|
53 | prev_input[c] = input[i];
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | source.connect(tinnyEffectNode);
|
---|
59 | tinnyEffectNode.connect(audioCtx.destination);
|
---|
60 |
|
---|
61 | console.log("onLoadMetadata done");
|
---|
62 | });
|
---|
63 |
|
---|
64 | });
|
---|
65 |
|
---|
66 | function inTheGroove()
|
---|
67 | {
|
---|
68 | InTheGroove = true;
|
---|
69 | MissingTheGroovePending = false;
|
---|
70 | }
|
---|
71 |
|
---|
72 | function missingTheGroove()
|
---|
73 | {
|
---|
74 | if (MissingTheGroovePending) {
|
---|
75 | InTheGroove = false;
|
---|
76 | }
|
---|
77 | MissingTheGroovePending = false;
|
---|
78 | }
|
---|
79 |
|
---|
80 | function delayedMissingTheGroove(delay)
|
---|
81 | {
|
---|
82 | MissingTheGroovePending = true;
|
---|
83 | setTimeout(missingTheGroove,delay || 400);
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|