source: main/trunk/model-sites-dev/respooled/collect/popup-video-respooled/js/in-the-groove.js@ 29863

Last change on this file since 29863 was 29863, checked in by davidb, 9 years ago

First cut at respooled site/collection

File size: 1.8 KB
Line 
1
2// https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createScriptProcessor
3
4var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
5
6var InTheGroove = false;
7var MissingTheGroovePending = false;
8
9//var bufferSize = 4096;
10var bufferSize = 512;
11
12
13//window.addEventListener('load', onLoad, false);
14
15
16var 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
65function inTheGroove()
66{
67 InTheGroove = true;
68 MissingTheGroovePending = false;
69}
70
71function missingTheGroove()
72{
73 if (MissingTheGroovePending) {
74 InTheGroove = false;
75 }
76 MissingTheGroovePending = false;
77}
78
79function delayedMissingTheGroove(delay)
80{
81 MissingTheGroovePending = true;
82 setTimeout(missingTheGroove,delay || 400);
83}
84
85
Note: See TracBrowser for help on using the repository browser.