Skip to content

Commit

Permalink
applied some code conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
kn1kn1 committed Jan 10, 2012
1 parent 0e722b4 commit 5bce887
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 175 deletions.
110 changes: 55 additions & 55 deletions app.js
@@ -1,55 +1,55 @@

/**
* Module dependencies.
*/

var express = require('express')
, sio = require('socket.io')
, routes = require('./routes')
, path = require('path')
, util = require('util')
, fs = require('fs')
, NrtSc140 = require('./nrtsc140').NrtSc140

var app = module.exports = express.createServer();

// Configuration

app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
app.use(express.errorHandler());
});

// Routes

app.get('/', routes.index);

app.listen(3000);
console.log("Express server listening on port %d in %s mode",
app.address().port, app.settings.env);

var audioDir = path.join(process.cwd(), 'public', 'audio');
if (!path.existsSync(audioDir)) {
fs.mkdirSync(audioDir);
}

// Socket IO
var io = sio.listen(app);
io.sockets.on('connection', function(socket) {
util.debug('connection');
var nrtsc140 = new NrtSc140(socket);
nrtsc140.start();
});

/**
* Module dependencies.
*/

var express = require('express')
, sio = require('socket.io')
, routes = require('./routes')
, path = require('path')
, util = require('util')
, fs = require('fs')
, NrtSc140 = require('./nrtsc140').NrtSc140

var app = module.exports = express.createServer();

// Configuration

app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
app.use(express.errorHandler());
});

// Routes

app.get('/', routes.index);

app.listen(3000);
console.log("Express server listening on port %d in %s mode",
app.address().port, app.settings.env);

var audioDir = path.join(process.cwd(), 'public', 'audio');
if (!path.existsSync(audioDir)) {
fs.mkdirSync(audioDir);
}

// Socket IO
var io = sio.listen(app);
io.sockets.on('connection', function(socket) {
util.debug('connection');
var nrtsc140 = new NrtSc140(socket);
nrtsc140.start();
});

79 changes: 48 additions & 31 deletions nrtsc140.js
Expand Up @@ -14,13 +14,13 @@ var sio = require('socket.io')
, child_process = require('child_process')
, path = require('path')
, crypto = require('crypto')
, util = require('util')
, util = require('util');

var NrtSc140 = exports.NrtSc140 = function(socket) {
this._socket = socket;
this._audioDir = path.join(process.cwd(), 'public', 'audio');
this._curFile = null;
this._generatedFiles = new Array();
this._generatedFiles = [];
this._timeoutId = null;
this._intervalId = null;
this._sclang = null;
Expand All @@ -34,22 +34,26 @@ NrtSc140.prototype.start = function() {
this._socket.on('stop', this.onStop.bind(this));
this._socket.on('restartsclang', this.onRestartSclang.bind(this));
this._socket.on('disconnect', this.onSocketDisconnect.bind(this));
}
};

NrtSc140.prototype.onRestartSclang = function() {
this._socket.emit('scserverstarting');
if (this._sclang) this._sclang.dispose();
if (this._sclang) {
this._sclang.dispose();
}
this._sclang = this.createSclang();
}
};

NrtSc140.prototype.onValidate = function(msg) {
util.debug('validate: ' + msg);
this.validateScCode(msg, this._socket);
}
};

NrtSc140.prototype.onGenerate = function(msg) {
util.debug('generate: ' + msg);
if (!this.validateScCode(msg, this._socket)) return;
if (!this.validateScCode(msg, this._socket)) {
return;
}

var aiffFile;
do {
Expand All @@ -59,9 +63,10 @@ NrtSc140.prototype.onGenerate = function(msg) {
} while (path.existsSync(aiffFile));
//this._generatedFiles.push(path.join(_audioDir, _curFile + '.*'));
this._sclang.evaluate(
's.waitForBoot(s.prepareForRecord(\''
+ aiffFile + '\');s.record;'
+ msg + '\n);', false);
's.waitForBoot(s.prepareForRecord(\'' +
aiffFile + '\');s.record;' +
msg + '\n);',
false);

var timeRemaining = MAX_RECORDING_SEC + 1;
this._timeoutId = setTimeout(function() {
Expand All @@ -75,26 +80,38 @@ NrtSc140.prototype.onGenerate = function(msg) {
timeRecorded++;
util.debug('timerecorded: ' + timeRecorded);
this._socket.emit('timerecorded', '' + timeRecorded);
if (timeRecorded >= MAX_RECORDING_SEC) clearInterval(this._intervalId);
if (timeRecorded >= MAX_RECORDING_SEC) {
clearInterval(this._intervalId);
}
}.bind(this), 1000);
};

NrtSc140.prototype.onStop = function() {
util.debug('stop');
if (this._timeoutId) clearTimeout(this._timeoutId);
if (this._intervalId) clearInterval(this._intervalId);
this._timeoutId = this.stopRecording();
}
if (this._timeoutId) {
clearTimeout(this._timeoutId);
}
if (this._intervalId) {
clearInterval(this._intervalId);
}
this.stopRecording();
};

NrtSc140.prototype.onSocketDisconnect = function() {
util.debug('disconnect');
if (this._timeoutId) clearTimeout(this._timeoutId);
if (this._intervalId) clearInterval(this._intervalId);
if (this._sclang) this._sclang.dispose();
for (var i in this._generatedFiles) {
if (this._timeoutId) {
clearTimeout(this._timeoutId);
}
if (this._intervalId) {
clearInterval(this._intervalId);
}
if (this._sclang) {
this._sclang.dispose();
}
for (var i = 0, len = this._generatedFiles.length; i < len; i++) {
rmFile(this._generatedFiles[i]);
}
}
};

NrtSc140.prototype.createSclang = function() {
var sclang =
Expand All @@ -108,12 +125,12 @@ NrtSc140.prototype.createSclang = function() {
} while (path.existsSync(workaroundTmpFile));
this._generatedFiles.push(workaroundTmpFile);
sclang.evaluate(
's.waitForBoot(s.prepareForRecord(\''
+ workaroundTmpFile
+ '\');s.record;s.stopRecording;);',
's.waitForBoot(s.prepareForRecord(\'' +
workaroundTmpFile +
'\');s.record;s.stopRecording;);',
false);
return sclang;
}
};

NrtSc140.prototype.onSclangStdoutReceived = function(data) {
util.debug('sclang stdout: ' + data);
Expand All @@ -127,7 +144,7 @@ NrtSc140.prototype.onSclangStdoutReceived = function(data) {
return; // ignore
}
this._socket.emit('stdout', msg);
}
};

NrtSc140.prototype.validateScCode = function(msg) {
if (msg.length < 1) {
Expand All @@ -144,7 +161,7 @@ NrtSc140.prototype.validateScCode = function(msg) {
}
this._socket.emit('validationerror', '');
return true;
}
};

NrtSc140.prototype.stopRecording = function() {
this._sclang.evaluate('s.stopRecording;', false);
Expand All @@ -155,13 +172,13 @@ NrtSc140.prototype.stopRecording = function() {
convert(aiffFile, mp3File, function(error, stdout, stderr) {
util.debug('stdout: ' + stdout);
util.debug('stderr: ' + stderr);
if (error != null) {
if (error) {
util.debug('exec error: ' + error);
}
this._socket.emit('filegenerated', this._curFile);
}.bind(this));
}.bind(this), 1000);
}
};

function randomString() {
var buf = crypto.randomBytes(4);
Expand All @@ -174,16 +191,16 @@ function randomString() {

function convert(aiffFile, mp3File, callback) {
child_process.exec(
'ffmpeg -i ' + aiffFile + ' -f mp3 -acodec libmp3lame -ab 192000 -ar 44100 '
+ mp3File,
'ffmpeg -i ' + aiffFile +
' -f mp3 -acodec libmp3lame -ab 192000 -ar 44100 ' + mp3File,
callback);
}

function rmFile(file) {
child_process.exec('rm -f ' + file, function(error, stdout, stderr) {
util.debug('stdout: ' + stdout);
util.debug('stderr: ' + stderr);
if (error != null) {
if (error) {
util.debug('exec error: ' + error);
}
});
Expand Down
48 changes: 24 additions & 24 deletions package.json
@@ -1,24 +1,24 @@
{
"name": "nrt-sc140"
, "version": "0.0.2"
, "description": "non-realtime sc140 sound file generator web application"
, "author": "Kenichi Kanai"
, "private": false
, "repository": {
"type": "git"
, "url": "http://github.com/kn1kn1/nrt-sc140.git"
}
, "licenses": [{
"type": "MIT"
, "url": "http://github.com/kn1kn1/nrt-sc140/blob/master/README.md"
}]
, "engines": {
"node": ">=0.6.5"
}
, "dependencies": {
"express": "2.5.2"
, "jade": ">= 0.0.1"
, "socket.io": ">=0.8.7"
, "sc4node": ">=0.0.7"
}
}
{
"name": "nrt-sc140"
, "version": "0.0.3"
, "description": "non-realtime sc140 sound file generator web application"
, "author": "Kenichi Kanai"
, "private": false
, "repository": {
"type": "git"
, "url": "http://github.com/kn1kn1/nrt-sc140.git"
}
, "licenses": [{
"type": "MIT"
, "url": "http://github.com/kn1kn1/nrt-sc140/blob/master/README.md"
}]
, "engines": {
"node": ">=0.6.5"
}
, "dependencies": {
"express": "2.5.2"
, "jade": ">= 0.0.1"
, "socket.io": ">=0.8.7"
, "sc4node": ">=0.0.7"
}
}

0 comments on commit 5bce887

Please sign in to comment.