Skip to content

Commit

Permalink
Sends all records to a directory
Browse files Browse the repository at this point in the history
  • Loading branch information
soney committed Aug 1, 2015
1 parent afa7e42 commit 2e52ec6
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 39 deletions.
12 changes: 12 additions & 0 deletions lib/mkdirp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var mkdirpLib = require('mkdirp');
module.exports = function(path) {
return new Promise(function(resolve, reject) {
mkdirpLib(path, function(err) {
if(err) {
reject(err);
} else {
resolve(path);
}
});
});
};
73 changes: 62 additions & 11 deletions lib/voice-assist.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
var EventEmitter = require('events').EventEmitter;
var mkdirp = require('./mkdirp');
var fs = require('fs');
var RecordingBarView = require('./RecordingBarView');
var STORAGE_DIRECTORY = __dirname + '/recordings/';

var recorder = new EventEmitter();
recorder._recording = false;
Expand Down Expand Up @@ -58,30 +61,78 @@ recorder.serialize = function() {
var recordingBarView;
var voiceRecorder = require('./voiceRecorder');
var editorRecorder = require('./editorRecorder');
var workspaceSnapshot = require('./workspaceSnapshot');
var modalPanel;
var uid;

recorder.on('start', function() {
voiceRecorder.start();
editorRecorder.start();
uid = guid();
voiceRecorder.start(uid);
editorRecorder.start(uid);
workspaceSnapshot.start(uid);
modalPanel.show();
}).on('stop', function() {
var changelogPromise = editorRecorder.stop();
var promises = voiceRecorder.stop(),
var changelogPromise = editorRecorder.stop(uid);
var promises = voiceRecorder.stop(uid),
wavPromise = promises.wav,
transcriptPromise = promises.transcript;
Promise.all([wavPromise, transcriptPromise, changelogPromise]).then(function(info) {
var zipPromise = workspaceSnapshot.stop(uid);
Promise.all([wavPromise, transcriptPromise, changelogPromise, zipPromise]).then(function(info) {
var wavFilename = info[0],
transcript = info[1],
changelog = info[2];
console.log("Wav file: ", wavFilename);
console.log("Transcript: ", transcript);
console.log("Changes: ", changelog)
changelog = info[2],
zip = info[3];

var recordingInfo = {
wav: wavFilename,
transcript: transcript,
changelog: changelog,
zip: zip
};

var folder = STORAGE_DIRECTORY + uid + '/',
filename = 'recording.json',
fullFilename = folder + filename;

return mkdirp(folder).then(function() {
return new Promise(function(resolve, reject) {
var infoString;
try {
infoString = JSON.stringify(recordingInfo);
} catch(e) {
reject(e);
return;
}

fs.writeFile(fullFilename, infoString, function(err) {
if(err) {
reject(err);
} else {
resolve(fullFilename);
}
})
});
});
}).then(function(infoFile) {
console.log(infoFile);
}, function(err) {
console.error(err.stack);
});
modalPanel.hide();
}).on('cancel', function() {
editorRecorder.cancel();
voiceRecorder.cancel();
editorRecorder.cancel(uid);
voiceRecorder.cancel(uid);
workspaceSnapshot.cancel(uid);
modalPanel.hide();
})
function guid() {
function s4() {
return Math .floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}

module.exports = recorder;
36 changes: 9 additions & 27 deletions lib/voiceRecorder.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var _ = require('./vendor/underscore'),
RecordRTC = require('recordrtc'),
fs = require('fs'),
mkdirpLib = require('mkdirp');
mkdirp = require('./mkdirp');

var RECORDING_STORAGE_DIRECTORY = __dirname + '/recordings/';

Expand All @@ -10,16 +10,6 @@ recognitionEngine.continue = false;
recognitionEngine.interimResults = false;
recognitionEngine.maxResults = 1;

function guid() {
function s4() {
return Math .floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}

function getUserMedia() {
return new Promise(function(resolve, reject) {
try {
Expand All @@ -36,21 +26,12 @@ function getUserMedia() {
});
}

function mkdirp(path) {
return new Promise(function(resolve, reject) {
mkdirpLib(path, function(err) {
if(err) {
reject(err);
} else {
resolve(path);
}
});
});
}
var transcriptPromise, audioStreamPromise, recorder;
var question_id;

module.exports = {
start: function () {
start: function (uid) {
question_id = uid;
transcriptPromise = new Promise(function(resolve, reject) {
recognitionEngine.start();
recognitionEngine.onresult = function(event) {
Expand All @@ -70,7 +51,7 @@ module.exports = {
return audioStream;
});
},
stop: function() {
stop: function(uid) {
recognitionEngine.stop();
var wavPromise = audioStreamPromise.then(function(audioStream) {
return new Promise(function(resolve, reject) {
Expand All @@ -89,10 +70,11 @@ module.exports = {
fr.readAsBinaryString(blob);
});
}).then(function(binaryContent) {
var filename = guid() + ".wav",
fullFilename = RECORDING_STORAGE_DIRECTORY + filename;
var folder = RECORDING_STORAGE_DIRECTORY + uid + '/',
filename = 'audio.wav',
fullFilename = folder + filename;

return mkdirp(RECORDING_STORAGE_DIRECTORY).then(function() {
return mkdirp(folder).then(function() {
return new Promise(function(resolve, reject) {
fs.writeFile(fullFilename, binaryContent, "binary", function(err) {
if(err) {
Expand Down
61 changes: 61 additions & 0 deletions lib/workspaceSnapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
var _ = require('./vendor/underscore'),
archiver = require('archiver'),
mkdirp = require('./mkdirp'),
fs = require('fs');

var archive;
var WORKSPACE_STORAGE_DIRECTORY = __dirname + '/recordings/';
var output;
var zipPromise;
var filename;

module.exports = {
start: function (uid) {
var rootDirectories = atom.project.getDirectories(),
directoryPaths = _.map(rootDirectories, function(dir) {
return dir.getPath();
});

var folder = WORKSPACE_STORAGE_DIRECTORY + uid + '/',
filename = 'workspace.zip',
fullFilename = folder + filename;

zipPromise = mkdirp(folder).then(function() {
output = fs.createWriteStream(fullFilename);

var outputClosePromise = new Promise(function(resolve, reject) {
output.on('close', function() {
resolve(fullFilename);
});
});

archive = archiver.create('zip', {});
archive.pipe(output);

_.each(directoryPaths, function(path) {
archive.directory(path);
})

archive.finalize();
return outputClosePromise;
}, function(e) {
console.error(e.stack);
});
},
stop: function() {
return zipPromise;
},
cancel: function(uid) {
var folder = WORKSPACE_STORAGE_DIRECTORY + uid + '/',
filename = 'workspace.zip',
fullFilename = folder + filename;

return zipPromise.then(function(filename) {
return new Promise(function(resolve) {
fs.unlink(filename, function() {
resolve();
});
});
})
}
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"mkdirp": "latest",
"atom-helpers": "latest",
"jquery": "latest",
"diff": "latest"
"diff": "latest",
"archiver": "latest"
}
}

0 comments on commit 2e52ec6

Please sign in to comment.