-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvisualizer-micro.js
226 lines (173 loc) · 7.01 KB
/
visualizer-micro.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.VisualizerMicro = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
/*
MIT License
Copyright (c) 2015 Christopher Dolphin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
var VisualizerMicro;
(function(){
var AudioContext = window.AudioContext || window.webkitAudioContext,
_warnNotSupported,
_warnAudioLoaded,
_volumeToDB,
_dbToVolume,
_onLoad,
load,
unload,
isSupported,
getSpectrum,
getWaveform,
setVolumeModifier,
logPrefix = 'VisualizerMicro: ',
loadEventString = 'canplay',
prototype;
VisualizerMicro = function () {
if (!isSupported()) {
_warnNotSupported();
return;
}
this.alreadyLoaded = false;
this.binCount = false;
this.loadEventListenerCreated = false;
this.loadedCallback = false;
this.audioSource = false;
this.dataArray = [];
};
_warnNotSupported = function () {
console.warn(logPrefix + 'Audio visualization is not supported in this browser.');
};
_warnAudioLoaded = function () {
console.warn(logPrefix + "An audio source must be loaded using load() before audio data can be retrieved.");
};
_onLoad = function () {
if (this.alreadyLoaded) {
return;
}
var source = this.context.createMediaElementSource(this.audioSource);
source.connect(this.analyser);
this.analyser.connect(this.context.destination);
this.binCount = this.analyser.frequencyBinCount;
this.dataArray = new Uint8Array(this.binCount);
this.alreadyLoaded = true;
this.loadedCallback();
};
load = function (audio, callback) {
if (!isSupported()) {
_warnNotSupported();
return;
}
if (!audio) {
console.warn(logPrefix + 'An audio source must be supplied to load().');
return;
}
if (!callback) {
console.warn(logPrefix + 'A callback must be supplied to load().');
return;
}
if (this.audioSource === audio) {
console.log(logPrefix + 'This audio source has already been loaded, no need to call load() with it again.');
return;
}
if (this.loadEventListenerCreated && this.alreadyLoaded) {
// remove old audio source so new one can be attached
unload();
}
this.context = new AudioContext();
this.analyser = this.context.createAnalyser();
this.audioSource = audio;
this.loadEventListenerCreated = true;
this.loadedCallback = callback;
if (audio.readyState === 3 || audio.readyState === 4) {
this._onLoad()
} else {
this.audioSource.addEventListener(loadEventString, _onLoad.bind(this))
}
};
unload = function () {
if (!this.audioSource) {
console.log(logPrefix + "Audio source doesn't exist. This may mean it was destroyed prematurely and could cause a memory leak.");
} else {
this.audioSource.removeEventListener(loadEventString, _onLoad);
}
this.alreadyLoaded = false;
this.loadedCallback = false;
this.loadEventListenerCreated = false;
};
isSupported = function () {
return !!AudioContext;
};
getSpectrum = function () {
if (!isSupported()) {
_warnNotSupported();
return;
}
if (!this.alreadyLoaded) {
_warnAudioLoaded();
return;
}
var spectrum = [],
spectrumLength,
minDecibels = this.analyser.minDecibels,
maxDecibels = this.analyser.maxDecibels,
s = 255 / (maxDecibels - minDecibels),
dbValue;
this.analyser.getByteFrequencyData(this.dataArray);
spectrumLength = this.dataArray.length;
for (var i = 0; i < spectrumLength; i++) {
dbValue = this.dataArray[i];
spectrum.push(dbValue / 255);
}
return spectrum;
};
getWaveform = function () {
if (!isSupported()) {
_warnNotSupported();
return;
}
if (!this.alreadyLoaded) {
_warnAudioLoaded();
return;
}
var waveform = [],
waveformLength,
value;
this.analyser.getByteTimeDomainData(this.dataArray);
waveformLength = this.dataArray.length;
for (var i = 0; i < waveformLength; i++) {
value = this.dataArray[i];
value = value - 128;
value = value / 128;
waveform.push(value);
}
return waveform;
};
setVolumeModifier = function (volume) {
console.warn(logPrefix + 'Setting the volume modifier is depreciated, all spectrum values must be relative to volume from the audio source.');
};
//set to prototype
prototype = VisualizerMicro.prototype;
prototype._onLoad = _onLoad;
prototype.load = load;
prototype.unload = unload;
prototype.isSupported = isSupported;
prototype.getSpectrum = getSpectrum;
prototype.getWaveform = getWaveform;
prototype.setVolumeModifier = setVolumeModifier;
})();
module.exports = VisualizerMicro;
},{}]},{},[1])(1)
});