-
Notifications
You must be signed in to change notification settings - Fork 3
/
listen.js
197 lines (171 loc) · 6.37 KB
/
listen.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
var IsMacChrome = navigator.userAgent.indexOf('Mac') != -1 &&
navigator.userAgent.indexOf('Chrome') != -1;
var samplerate = 16000, channel = 1, stream_length = 4096;
var sinwave = function(frequency) {
this.phase = 0.0;
this.phaseStep = frequency / samplerate;
};
sinwave.prototype.next = function() {
var retval = Math.sin(2 * Math.PI * this.phase);
this.phase += this.phaseStep;
return retval;
};
var StreamGenerator = function() {
this.gen1 = new sinwave(440);
this.gen2 = new sinwave(660);
this.phase = new sinwave(880);
};
StreamGenerator.prototype.next = function() {
var stream = [];
var i, imax;
var g1 = this.gen1, g2 = this.gen2;
var v1 = this.phase.next() / 2.0 + 0.5, v2 = 1.0 - v1;
for (i = 0, imax = stream_length; i < imax; i++) {
stream[i] = (g1.next() * v1 + g2.next() * v2);
}
return stream;
};
/**
* Audio Data API用のプレイヤー (Firefox 5.0-)
*/
var MozPlayer = null;
if (typeof new Audio().mozSetup === 'function') {
MozPlayer = function() {
this.audio = new Audio();
this.audio.mozSetup(channel, samplerate);
this.timerId = null;
this.isPlaying = false;
};
MozPlayer.prototype.play = function(gen) {
var self = this;
//if (this.timerId === null) {
// this.timerId = setInterval(function() {
var s = new Float32Array(gen.next());
self.audio.mozWriteAudio(s);
// }, stream_length / samplerate * 1000);
// }
this.isPlaying = true;
};
MozPlayer.prototype.stop = function() {
if (this.timerId !== null) {
clearInterval(this.timerId);
}
this.isPlaying = false;
};
}
/**
* Web Audio API用のプレイヤー (Chrome, Safari)
*/
var WebkitPlayer = null;
// if (!IsMacChrome && typeof webkitAudioContext === 'function') {
WebkitPlayer = function() {
this.context = new webkitAudioContext();
this.node = this.context.createJavaScriptNode(stream_length, 1, channel);
this.isPlaying = false;
};
WebkitPlayer.prototype.play = function(gen) {
var self = this;
this.node.onaudioprocess = function(event) {
var data = event.outputBuffer.getChannelData(0);
var s = gen.next();
var i = data.length;
while (i--) data[i] = s[i];
};
this.node.connect(this.context.destination);
this.isPlaying = true;
};
WebkitPlayer.prototype.stop = function() {
this.node.disconnect();
this.isPlaying = false;
};
// }
/**
* Audioタグを使うプレイヤー (Chrome, Firefox, Opera)
*/
var HTML5AudioPlayer = null;
if (typeof Audio === 'function') {
HTML5AudioPlayer = function() {
this.audio = null;
this.timerId = null;
this.isPlaying = false;
};
HTML5AudioPlayer.prototype.wavheader = function(samples) {
var waveBytes = samples * channel * 2,
l1 = waveBytes - 8,
l2 = l1 - 36,
retval = String.fromCharCode(
0x52, 0x49, 0x46, 0x46, // 'RIFF'
(l1 >> 0) & 0xFF, (l1 >> 8) & 0xFF,
(l1 >> 16) & 0xFF, (l1 >> 24) & 0xFF,
0x57, 0x41, 0x56, 0x45, // 'WAVE'
0x66, 0x6D, 0x74, 0x20, // 'fmt '
0x10, 0x00, 0x00, 0x00, // byte length
0x01, 0x00, // linear pcm
channel, 0x00, // channel
(samplerate >> 0) & 0xFF,
(samplerate >> 8) & 0xFF,
(samplerate >> 16) & 0xFF,
(samplerate >> 24) & 0xFF,
((samplerate*channel*2) >> 0) & 0xFF,
((samplerate*channel*2) >> 8) & 0xFF,
((samplerate*channel*2) >> 16) & 0xFF,
((samplerate*channel*2) >> 24) & 0xFF,
0x04, 0x00, // block size
0x10, 0x00, // 16bit
0x64, 0x61, 0x74, 0x61, //'data'
(l2 >> 0) & 0xFF, (l2 >> 8) & 0xFF,
(l2 >> 16) & 0xFF, (l2 >> 24) & 0xFF);
return retval;
};
HTML5AudioPlayer.prototype.play = function(gen) {
var self = this;
var itercount = 20;
if (this.timerId === null) {
this.timerId = setInterval(function() {
var bytes = [], s, x, wav;
var i, imax, j, jmax;
for (i = 0, imax = itercount; i < imax; i++) {
s = gen.next();
for (j = 0, jmax = s.length; j < jmax; j++) {
x = (s[j] * 32767.0) >> 0;
bytes.push(String.fromCharCode(x & 0xFF, (x >> 8) & 0xFF));
}
}
wav = btoa(self.wavheader(bytes.length) + bytes.join(''));
self.audio = new Audio("data:audio/wav;base64," + wav);
self.audio.play();
}, stream_length * itercount / samplerate * 1000);
}
this.isPlaying = true;
};
HTML5AudioPlayer.prototype.stop = function() {
if (this.timerId !== null) {
clearInterval(this.timerId);
}
if (this.audio !== null) {
this.audio.pause();
this.audio = null;
}
this.isPlaying = false;
};
}
var RadioStreamGenerator = function(samples) {
this.stream = samples;
};
RadioStreamGenerator.prototype.next = function() {
return this.stream;
};
$( function() {
var player = new MozPlayer;
var socket = io.connect();
if(user !== "") {
socket.on(username, function(from, samples) {
player.play(new RadioStreamGenerator(samples));
});
}
/*
socket.on('podcast', function(from, samples) {
player.play(new RadioStreamGenerator(samples));
});
*/
});