-
Notifications
You must be signed in to change notification settings - Fork 11
/
audio-source.js
174 lines (152 loc) · 4.87 KB
/
audio-source.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
// audio-source.js
exports.SoundCloudAudioSource = function(player, streams, onPlayStream, continuous=true) {
var self = this;
var streamIndex = 0;
var analyser;
var audioCtx = new (window.AudioContext || window.webkitAudioContext);
analyser = audioCtx.createAnalyser();
analyser.fftSize = 256;
var source = audioCtx.createMediaElementSource(player);
source.connect(analyser);
analyser.connect(audioCtx.destination);
//continuous
if (continuous) {
player.addEventListener('ended', function() {self.next()});
player.addEventListener('error', function() {self.next()});
}
var sampleAudioStream = function() {
self.streamData = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(self.streamData);
// Calculate an overall volume value
var total = 0;
for (var i = 0; i < 64; i++) { // Get the volume from the first 64 bins
total += self.streamData[i];
}
self.volume = total;
var totalLow = 0;
for (var i = 0; i < 31; i++) { // Get the volume from the first 32 bins
totalLow += self.streamData[i];
}
self.volumeLow = totalLow;
var totalHi = 0;
for (var i = 31; i < 64; i++) { // Get the volume from the second 32 bins
totalHi += self.streamData[i];
}
self.volumeHi = totalHi;
};
setInterval(sampleAudioStream, 20);
// Public properties and methods
this.volume = 0;
this.volumeLow = 0;
this.volumeHi = 0;
this.streamData = new Uint8Array(analyser.frequencyBinCount);
this.isPlaying = false;
this.currentTime = 0;
this.playStream = function(stream) {
player.crossOrigin = 'anonymous';
player.setAttribute('src', stream.url);
player.play();
onPlayStream(stream)
}
this.play = function() {
player.pause();
this.isPlaying = true;
player.currentTime = 0;
this.playStream(streams[streamIndex])
}
this.resume = function() {
player.currentTime = this.currentTime;
player.play();
this.isPlaying = true;
}
this.toggle = function() {
this.isPlaying ? this.pause() : this.resume();
}
this.pause = function() {
this.currentTime = player.currentTime;
player.pause();
this.isPlaying = false;
}
this.next = function() {
if (streamIndex < streams.length - 1) {
streamIndex++;
this.play()
}
}
this.previous = function() {
if (streamIndex > 0) {
streamIndex--;
this.play();
}
}
this.setVolume = function(volume) {
player.volume = volume;
}
this.shuffle = function() {
streamIndex = 0;
var j, x, i;
for (i = streams.length; i; i--) {
j = Math.floor(Math.random() * i);
x = streams[i - 1];
streams[i - 1] = streams[j];
streams[j] = x;
}
}
};
exports.Visualizer = function() {
var audioSource;
this.init = function(options) {
audioSource = options.audioSource;
var container = document.getElementById(options.containerId);
};
};
exports.SoundcloudLoader = function(player,uiUpdater) {
var self = this;
var client_id = "26095b994cc185bc665f4c9fcce8f211"; // to get an ID go to https://developers.soundcloud.com/
this.sound = {};
this.streamUrl = "";
this.albumArt = "";
this.errorMessage = "";
this.player = player;
/**
* Loads the JSON stream data object from the URL of the track (as given in the location bar of the browser when browsing Soundcloud),
* and on success it calls the callback passed to it (for example, used to then send the stream_url to the audiosource object).
* @param track_url
* @param callback
*/
this.loadStream = function(track_url, successCallback, errorCallback) {
SC.initialize({
client_id: client_id
});
SC.get('https://api.soundcloud.com/resolve', { url: track_url }, function(sound) {
if (!sound) {
errorCallback();
}
else if (sound.errors) {
self.errorMessage = "";
for (var i = 0; i < sound.errors.length; i++) {
self.errorMessage += sound.errors[i].error_message + '<br>';
}
self.errorMessage += 'Make sure the URL has the correct format: https://soundcloud.com/user/title-of-the-track';
errorCallback();
} else {
if(sound.kind=="playlist"){
self.sound = sound;
self.streamPlaylistIndex = 0;
self.streamUrl = function(){
return sound.tracks.map(function(track) {return track.stream_url + '?client_id=' + client_id});
}
self.albumArt = function() {
return sound.tracks.map(function(track) {return track.artwork_url});
};
successCallback();
}else{
self.sound = sound;
self.streamUrl = function(){ return [sound.stream_url + '?client_id=' + client_id]; };
self.albumArt = function() {return [sound.artwork_url]};
successCallback();
}
}
});
};
};