From 5bce887c085970813b0c1d178f3ee3da253b8272 Mon Sep 17 00:00:00 2001 From: kn1kn1 Date: Tue, 10 Jan 2012 20:25:17 +0900 Subject: [PATCH] applied some code conventions --- app.js | 110 +++++++++++++++++------------------ nrtsc140.js | 79 +++++++++++++++---------- package.json | 48 +++++++-------- public/stylesheets/style.css | 64 ++++++++++---------- routes/index.js | 16 ++--- views/index.jade | 30 +++++----- views/layout.jade | 20 +++---- 7 files changed, 192 insertions(+), 175 deletions(-) diff --git a/app.js b/app.js index 615905d..28e0c8f 100644 --- a/app.js +++ b/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(); +}); + diff --git a/nrtsc140.js b/nrtsc140.js index 767b73a..9c14821 100644 --- a/nrtsc140.js +++ b/nrtsc140.js @@ -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; @@ -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 { @@ -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() { @@ -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 = @@ -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); @@ -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) { @@ -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); @@ -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); @@ -174,8 +191,8 @@ 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); } @@ -183,7 +200,7 @@ 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); } }); diff --git a/package.json b/package.json index c7813a6..992d534 100644 --- a/package.json +++ b/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" + } +} diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index b3e2512..8a65902 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -1,32 +1,32 @@ -#validationerror{ - margin:5px; - background:yellow; -} -#codewrap{ - margin:10px; - width:45%; - float:left; -} -#code{ - width:100%; - height:250px; -} -#stdoutwrap{ - margin:10px; - width:45%; - float:left; -} -#stdout{ - width:100%; - height:250px; -} -#buttons{ - margin:10px; - clear:left; -} -#player{ - margin:10px; -} -A{ - margin:5px; -} +#validationerror{ + margin:5px; + background:yellow; +} +#codewrap{ + margin:10px; + width:45%; + float:left; +} +#code{ + width:100%; + height:250px; +} +#stdoutwrap{ + margin:10px; + width:45%; + float:left; +} +#stdout{ + width:100%; + height:250px; +} +#buttons{ + margin:10px; + clear:left; +} +#player{ + margin:10px; +} +A{ + margin:5px; +} diff --git a/routes/index.js b/routes/index.js index 7197fa3..f1daa8c 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,8 +1,8 @@ - -/* - * GET home page. - */ - -exports.index = function(req, res){ - res.render('index', { title: 'NRT-SC140' }) -}; + +/* + * GET home page. + */ + +exports.index = function(req, res){ + res.render('index', { title: 'NRT-SC140' }) +}; diff --git a/views/index.jade b/views/index.jade index 4c05382..f07803c 100644 --- a/views/index.jade +++ b/views/index.jade @@ -1,15 +1,15 @@ -body - h3 NRT-SC140 - div non-realtime sc140 sound file generator - #validationerror - #codewrap - #codetitle - textarea#code - #stdoutwrap - #stdouttitle sclang stdout - textarea#stdout - #buttons - button#generate Generate - button#stop Stop - #player - +body + h3 NRT-SC140 + div non-realtime sc140 sound file generator + #validationerror + #codewrap + #codetitle + textarea#code + #stdoutwrap + #stdouttitle sclang stdout + textarea#stdout + #buttons + button#generate Generate + button#stop Stop + #player + diff --git a/views/layout.jade b/views/layout.jade index 2736008..22b66cf 100644 --- a/views/layout.jade +++ b/views/layout.jade @@ -1,10 +1,10 @@ -!!! -html - head - title= title - link(rel='stylesheet', href='/stylesheets/style.css') - script(type='text/javascript', src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js') - script(type='text/javascript', src='/socket.io/socket.io.js') - script(type='text/javascript', src='/javascripts/audiojs/audio.min.js') - script(type='text/javascript', src='/javascripts/client.js') - body!= body +!!! +html + head + title= title + link(rel='stylesheet', href='/stylesheets/style.css') + script(type='text/javascript', src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js') + script(type='text/javascript', src='/socket.io/socket.io.js') + script(type='text/javascript', src='/javascripts/audiojs/audio.min.js') + script(type='text/javascript', src='/javascripts/client.js') + body!= body