Skip to content

Commit d62f4cd

Browse files
committed
merge from hell
1 parent fdff68a commit d62f4cd

13 files changed

+206
-86
lines changed

Podcast.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var fs = require('fs');
2+
3+
function Podcast(user, file) {
4+
this.user = user;
5+
this.file = file;
6+
this.filename = "podcasts/" + file + ".raw";
7+
this.stream = {};
8+
this.isOpen = false;
9+
this.fileDescription = {};
10+
}
11+
12+
Podcast.prototype.record = function() {
13+
var self = this;
14+
this.stream = fs.createWriteStream(this.filename);
15+
this.stream.once('open', function(fd) {
16+
self.isOpen = true;
17+
self.fileDescription = fd;
18+
});
19+
}
20+
21+
Podcast.prototype.append = function(samples) {
22+
this.stream.write(samples);
23+
}
24+
25+
Podcast.prototype.stop = function() {
26+
this.isOpen = false;
27+
this.stream.end();
28+
}
29+
30+
module.exports.Podcast = Podcast;

app.js

+34-21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ var nko = require('nko')('hfu9xf2WkLaqCx+T');
33
var sio = require('socket.io');
44
var fs = require('fs');
55

6+
require("./normalize");
7+
var Podcast = require("./Podcast").Podcast;
8+
69
var app = express.createServer();
710

811
app.set('views', __dirname + '/views');
@@ -15,10 +18,19 @@ app.use(express.bodyParser());
1518
app.use(app.router);
1619

1720
app.get('/', function(req, res, next){
18-
res.render("index");
19-
res.end();
21+
res.render("index");
22+
});
23+
24+
25+
app.get('/talk', function(req, res, next){
26+
res.render("talk");
27+
});
28+
29+
app.get('/listen', function(req, res, next){
30+
res.render("listen");
2031
});
2132

33+
2234
app.use(express.errorHandler({ showStack: true }));
2335
app.use(express.static(__dirname));
2436

@@ -27,39 +39,40 @@ app.listen(80, function () {
2739
console.log(' app listening on http://' + addr.address + ':' + addr.port);
2840
});
2941

30-
var stream;
42+
var users = {
43+
milfont: {
44+
podcasts: []
45+
}
46+
};
3147

3248
var io = sio.listen(app);
3349
io.set('log level', 1);
3450

3551
io.sockets.on('connection', function (client) {
36-
37-
client.on('user message', function (msg) {
38-
client.broadcast.emit('podcast', "MIlfont", msg);
39-
if(stream) {
40-
try {
41-
stream.write(msg);
42-
} catch(e) {
43-
console.log(stream, e);
44-
}
45-
}
52+
53+
client.on('user message', function (user, file, msg) {
54+
client.broadcast.emit('podcast', "milfont", msg);
55+
var podcast = users[user].podcasts.filter(function(podcast){
56+
return podcast.file === file;
57+
}).first();
58+
if(podcast) podcast.append(msg);
4659
});
4760

48-
client.on('start', function(name) {
49-
stream = fs.createWriteStream("podcasts/" + name + ".wav");
50-
stream.once('open', function(fd) {
51-
52-
});
61+
client.on('start', function(user, name) {
62+
var podcast = new Podcast(user, name);
63+
podcast.record();
64+
users[user].podcasts.add(podcast);
5365
});
5466

55-
client.on('stop', function() {
67+
client.on('stop', function(user, file) {
5668
console.log("stop");
57-
stream.end();
69+
users[user].podcasts.each(function(podcast){
70+
if(podcast.isOpen) podcast.stop();
71+
});
5872
});
5973

6074
client.on('listen', function(){
6175
console.log("listen");
62-
6376
})
6477

6578
});

layout.html

-42
This file was deleted.

normalize.js

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
if(typeof Array.prototype.add == "undefined") {
2+
Array.prototype.add = function(item) {
3+
this[this.length] = item;
4+
return this;
5+
}
6+
}
7+
8+
if(typeof Array.prototype.last == "undefined") {
9+
Array.prototype.last = function() {
10+
return (this.length > 0) ? this[this.length - 1] : -1;
11+
};
12+
}
13+
14+
if(typeof Array.prototype.first == "undefined") {
15+
Array.prototype.first = function() {
16+
return this[0];
17+
};
18+
}
19+
20+
if(typeof Array.prototype.each == "undefined") {
21+
Array.prototype.each = function(fn) {
22+
for(var index = 0; index < this.length; index++) {
23+
fn(this[index], index);
24+
}
25+
return this;
26+
}
27+
}
28+
29+
/*
30+
Implementação da Mozilla
31+
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map
32+
Navegadores defasados e antigos como o IE8/9 não possuem
33+
Production steps of ECMA-262, Edition 5, 15.4.4.19
34+
Reference: http://es5.github.com/#x15.4.4.19
35+
*/
36+
if (!Array.prototype.map) {
37+
Array.prototype.map = function(callback, thisArg) {
38+
var T, A, k;
39+
if (this == null) {
40+
throw new TypeError(" this is null or not defined");
41+
}
42+
var O = Object(this);
43+
var len = O.length >>> 0;
44+
if ({}.toString.call(callback) != "[object Function]") {
45+
throw new TypeError(callback + " is not a function");
46+
}
47+
if (thisArg) {
48+
T = thisArg;
49+
}
50+
A = new Array(len);
51+
k = 0;
52+
while(k < len) {
53+
var kValue, mappedValue;
54+
if (k in O) {
55+
kValue = O[ k ];
56+
mappedValue = callback.call(T, kValue, k, O);
57+
A[ k ] = mappedValue;
58+
}
59+
k++;
60+
}
61+
return A;
62+
};
63+
}
64+
65+
66+
/*
67+
Implementação da Mozilla
68+
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/Reduce
69+
70+
*/
71+
if ( !Array.prototype.reduce ) {
72+
Array.prototype.reduce = function reduce(accumlator){
73+
var i, l = this.length, curr;
74+
75+
if(typeof accumlator !== "function")
76+
throw new TypeError("First argument is not callable");
77+
78+
if((l == 0 || l === null) && (arguments.length <= 1))
79+
throw new TypeError("Array length is 0 and no second argument");
80+
81+
if(arguments.length <= 1){
82+
curr = this[0];
83+
i = 1;
84+
}
85+
else{
86+
curr = arguments[1];
87+
}
88+
89+
for(i = i || 0 ; i < l ; ++i){
90+
if(i in this)
91+
curr = accumlator.call(undefined, curr, this[i], i, this);
92+
}
93+
94+
return curr;
95+
};
96+
}
97+
98+
/*
99+
Implementação da Mozilla
100+
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter
101+
Navegadores defasados e antigos como o IE8/9 não possuem
102+
*/
103+
if(typeof Array.prototype.filter == "undefined") {
104+
Array.prototype.filter = function(fun)
105+
{
106+
"use strict";
107+
108+
if (this === void 0 || this === null)
109+
throw new TypeError();
110+
111+
var t = Object(this);
112+
var len = t.length >>> 0;
113+
if (typeof fun !== "function")
114+
throw new TypeError();
115+
116+
var res = [];
117+
var thisp = arguments[1];
118+
for (var i = 0; i < len; i++)
119+
{
120+
if (i in t)
121+
{
122+
var val = t[i]; // in case fun mutates this
123+
if (fun.call(thisp, val, i, t))
124+
res.push(val);
125+
}
126+
}
127+
128+
return res;
129+
};
130+
131+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

listen.js public/listen.js

File renamed without changes.

microphone.js public/microphone.js

File renamed without changes.

style.css public/style.css

File renamed without changes.

views/layout.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<title>Carcará SoundStream</title>
77

8-
<link rel="stylesheet" href="/style.css" type="text/css">
8+
<link rel="stylesheet" href="/public/style.css" type="text/css">
99
<script src="http://platform.twitter.com/anywhere.js?id=261Oz4VtFc8Qb8ho330uA&v=1" type="text/javascript"></script>
1010
</head>
1111
<body>
@@ -29,11 +29,11 @@ <h2>It's very simple. Just login and start to talk in your particular internet r
2929
</div>
3030
<div class="column">
3131
Start to speak on your microphone<br><br>
32-
<img src="/images/mic.png" height="50">
32+
<img src="/public/images/mic.png" height="50">
3333
</div>
3434
<div class="column">
3535
Share with your friend your new live channel<br><br>
36-
<img src="/images/listen.png" height="50">
36+
<img src="/public/images/listen.png" height="50">
3737
</div>
3838
</div>
3939
</div>

teste.html views/listen.html

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
<!doctype html>
2-
<html>
3-
<head>
1+
42
<script src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
53
<script src="/socket.io/socket.io.js"></script>
6-
7-
</head>
8-
<body>
94
<audio id="radio"></audio>
10-
<script src="listen.js"></script>
11-
</body>
12-
</html>
5+
<script src="/public/listen.js"></script>

index.html views/talk.html

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
<!doctype html>
2-
<html>
3-
<head>
1+
42
<script src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
53
<script src="/socket.io/socket.io.js"></script>
6-
<script src="microphone.js"></script>
7-
</head>
8-
<body>
4+
<script src="/public/microphone.js"></script>
5+
96
<button value="Record">Record</button>
107
<button value="Stop">Stop</button>
118

@@ -20,7 +17,7 @@
2017
var mic;
2118
$("button[value='Record']").click(function() {
2219
var name = "id_" + new Date().getTime();
23-
socket.emit("start", name);
20+
socket.emit("start", "milfont", name);
2421
var $file = $("#file");
2522
$file.append("<br/>");
2623
var link = $("<a>");
@@ -33,7 +30,7 @@
3330
mic = new Microphone(function(){
3431
mic.swf.rate = 8;
3532
mic.onSamplesAvailable = function(data,channelCount){
36-
socket.emit('user message', data);
33+
socket.emit('user message', "milfont", name, data);
3734
};
3835
mic.start();
3936
});
@@ -42,9 +39,7 @@
4239
$("button[value='Stop']").click(function() {
4340
if(mic) {
4441
mic.stop();
45-
socket.emit('stop', "stop");
42+
socket.emit('stop', "milfont");
4643
}
4744
});
4845
</script>
49-
</body>
50-
</html>

0 commit comments

Comments
 (0)