Skip to content

Commit

Permalink
conflicts on conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Hubsch committed Aug 5, 2017
1 parent 11bb741 commit 74cdf37
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 94 deletions.
70 changes: 2 additions & 68 deletions backend/audioAnalytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,10 @@ var SpeechToTextV1 = require('watson-developer-cloud/speech-to-text/v1');
var fs = require('fs');

var speech_to_text = new SpeechToTextV1({
username: 'USER',
password: 'PASS'
username: process.env.WATSON_USERNAME,
password: process.env.WATSON_PASSWORD,
});

// var params = {
// // From file
// audio: fs.createReadStream(file),
// content_type: 'audio/flac',
// word_confidence: true,
// timestamps: true,
// keywords: ['Like', 'You Know', 'Actually', 'I Mean', 'Sort of', 'Kind Of', 'Right', 'Uh', 'Um', 'Uhm'],
// keywords_threshold: 0.5,
// profanity_filter: true
// };

// 'continuous',
// 'max_alternatives',
// 'word_alternatives_threshold',

// speech_to_text.recognize(params, function(err, res) {
// if (err)
// console.log(err);
// else
// fs.writeFile('./data.json', JSON.stringify(res));
// var analytics = {
// pauses: calculatePauses(res),
// wordFrequency: wordCount(res),
// speed: speakerSpeed(res),
// clarity: calculateClarity(res),
// duration: calculateDuration(res)
// }
// console.log(JSON.stringify(analytics, null, 2));
// });

// var res = JSON.parse(fs.readFileSync('./data.json'));
// console.log(JSON.stringify(res, null, 2));
//
// var analytics = {
// pauses: calculatePauses(res),
// wordFrequency: wordCount(res),
// speed: speakerSpeed(res),
// clarity: calculateClarity(res),
// duration: calculateDuration(res),
// cursingCount: cursingCount(res)
// }
// console.log(JSON.stringify(analytics, null, 2));

// // or streaming
// fs.createReadStream('./resources/speech.wav')
// .pipe(speech_to_text.createRecognizeStream({ content_type: 'audio/l16; rate=44100' }))
// .pipe(fs.createWriteStream('./transcription.txt'));


module.exports = function(file, cb) {

var params = {
Expand Down Expand Up @@ -204,21 +155,4 @@ module.exports = function(file, cb) {
})
return cursingCount;
}

// const ToneAnalyzerV3 = require('watson-developer-cloud/tone-analyzer/v3');
//
// const tone_analyzer = new ToneAnalyzerV3({
// username: 'USER',
// password: 'PASS',
// version_date: '2016-10-19'
// });
//
// tone_analyzer.tone({ text: 'I am amazing and you suck' }, function(err, tone) {
// if (err) {
// console.log(err);
// } else {
// console.log('tone endpoint:');
// console.log(JSON.stringify(tone, null, 2));
// }
// });
}
14 changes: 12 additions & 2 deletions frontend/components/AppUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ export function uploadImage(photoEncoding, userId) {
});
};

export function uploadAudio(audioBlob, userId) {
export function uploadAudio(audioBlob, userId, questionNum) {
var formData = new FormData();
console.log(audioBlob);
formData.append('audio', audioBlob, `${new Date().toISOString()}-${userId}.wav`);
formData.append('audio', audioBlob, `Q${questionNum}_${userId}_${new Date().toISOString()}.wav`);

axios.post('/upload_audio', formData)
.then(function(res) {
Expand All @@ -46,6 +46,16 @@ export function uploadAudio(audioBlob, userId) {
});
};

export function mergeAndAnalyze(userId) {
axios.post('/merge', { userId: userId })
.then(function(res) {
console.log('RESULT OF MERGE', res);
})
.catch(function(err) {
console.log('ERROR', err);
});
}

function dataURItoBlob(dataURI) {
var byteString = atob(dataURI.split(',')[1]);
var ab = new ArrayBuffer(byteString.length);
Expand Down
21 changes: 12 additions & 9 deletions frontend/components/Behavioral.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Navbar from './Navbar';
import TypeWriter from 'react-typewriter';
import Timer from './Timer';

import { captureUserMedia, takePhoto, uploadImage, uploadAudio } from './AppUtils';
import { captureUserMedia, takePhoto, uploadImage, uploadAudio, mergeAndAnalyze } from './AppUtils';
import RecordRTC from 'recordrtc';
const StereoAudioRecorder = RecordRTC.StereoAudioRecorder;

Expand All @@ -27,12 +27,6 @@ class Behavioral extends React.Component {
this.nextQuestion = this.nextQuestion.bind(this);
this.index = 0;
}
// timer(minTime){
// this.setState({minTime: minTime})
// setInterval(() => {

// }, 1000);
// }

componentDidMount() {
this.setState({userId: '1234'});
Expand Down Expand Up @@ -98,7 +92,6 @@ class Behavioral extends React.Component {
startRecord() {
this.setState({isRecording: true})
setTimeout(() => {
console.log('YOYOYO');
this.setState({ time: 2 });
}, 3000)
// start recording audio
Expand All @@ -120,7 +113,7 @@ class Behavioral extends React.Component {
this.state.recordAudio.stopRecording(() => {
console.log('Stop recording audio');

uploadAudio(this.state.recordAudio.blob, this.state.userId);
uploadAudio(this.state.recordAudio.blob, this.state.userId, this.index - 1);
// console.log(this.state.recordAudio.blob);
// console.log(this.state.recordAudio);
temp();
Expand Down Expand Up @@ -160,6 +153,11 @@ class Behavioral extends React.Component {
this.nextQuestion(); // force next question
}


callMerge() {
mergeAndAnalyze(this.state.userId);
}

nextQuestion() {
// setTimeout(() => {
// console.log('YOYOYO');
Expand Down Expand Up @@ -206,10 +204,15 @@ class Behavioral extends React.Component {
this.state.stream.getVideoTracks().forEach(function(track) {
track.stop();
});

// now that behavioral is over, can merge audio
this.callMerge();

this.props.history.push('/technical');
}
}


componentWillMount() {
setTimeout(() => {
this.setState({doneTyping: true})
Expand Down
Binary file added merged_1234.wav
Binary file not shown.
Binary file added merged_undefined.wav
Binary file not shown.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"license": "MIT",
"dependencies": {
"api.ai": "^1.0.3",
"audioconcat": "^0.1.2",
"axios": "^0.16.2",
"babel-core": "^6.24.1",
"babel-loader": "^6.4.1",
Expand All @@ -29,6 +30,9 @@
"enzyme": "^2.8.1",
"expect": "^1.20.2",
"express": "^4.15.2",
"ffmpeg": "0.0.4",
"ffprobe": "^1.1.0",
"fluent-ffmpeg": "^2.1.2",
"fs": "0.0.1-security",
"jsonfile": "^3.0.1",
"lodash": "^4.17.4",
Expand Down
44 changes: 29 additions & 15 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ const bodyParser = require('body-parser')
const audioAnalytics = require('./backend/audioAnalytics.js');
var Question = require('./backend/models/models').Question;
var _axios = require('axios');
var ffmpeg = require('fluent-ffmpeg');


var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './backend/uploads/pics')
}
},
})

const upload = multer({ storage: storage })
Expand All @@ -29,11 +30,9 @@ app.use(bodyParser.json());
app.post('/run_code', function(req, res) {
var questionId = req.body.questionId;
var code = req.body.code;
console.log('code', code);
var language = req.body.language;
Question.findById(questionId)
.then(foundQuestion => {
console.log('foundQuestion', foundQuestion);
var inputArr = foundQuestion.inputArr;
var outputArr = foundQuestion.outputArr;
var answerArr = [];
Expand All @@ -43,9 +42,7 @@ app.post('/run_code', function(req, res) {
Authorization: 'Token ' + token
}
})
console.log('INPUTARRONE', inputArr);
inputArr.forEach(function(input) {
console.log('INPUT', input);
answerArr.push(axios.post('https://run.glot.io/languages/javascript/latest', {
files: [
{
Expand All @@ -59,15 +56,11 @@ app.post('/run_code', function(req, res) {
return err;
}))
});
console.log('INPUT ARR', inputArr);
Promise.all(answerArr)
.then(function(resolvedArr) {
console.log('RESOLVED ARR', resolvedArr);
var status = {success: true, message: 'You got it right!'}
resolvedArr.forEach(function(data, index) {
var output = data.stdout.trim();
console.log(output);
console.log(outputArr);
if (output !== outputArr[index]) {
status = {success: false, message: data.stderr || 'Your output did not match what we expected.'}
}
Expand All @@ -87,17 +80,38 @@ app.post('/upload_image', upload.single('image'), function (req, res, next) {
res.send({ success: true });
});

var count =0;
var count = 0;
app.post('/upload_audio', upload.single('audio'), function (req, res, next) {
console.log('AUDIO');
console.log(req.file);
res.send();
audioAnalytics('./backend/uploads/pics/' + req.file.filename, (result) => {
// res.json(result);
console.log(result);
});
// audioAnalytics('./backend/uploads/pics/' + req.file.filename, (result) => {
// console.log('WATSON RESULT', result);
// });
});

app.post('/merge', function(req, res, next) {

// merge audio files -- don't necessarily need to be in order, right?
// also be sure that these files are in .wav form
ffmpeg()
.input('./backend/uploads/pics/caea3efe05f366b6619fddc55e769f12.wav')
.input('./backend/uploads/pics/0046f56e0132783a11479f1e7df32c8b.wav')
.input('./backend/uploads/pics/b19ba40a5d59ac2e4c77bdc73085b90c.wav')
.on('error', function(err) {
console.log('An error occurred: ' + err.message);
res.send({ success: false });
})
.on('end', function() {
console.log('Merging finished !');
res.send();
audioAnalytics(`./backend/temp/merged_${req.body.userId}.wav`, (result) => {
console.log('WATSON RESULTS MERGED', result);
});
})
.mergeToFile(`./backend/temp/merged_${req.body.userId}.wav`);

})

app.get('/*', (request, response) => {
response.sendFile(__dirname + '/public/index.html'); // For React/Redux
});
Expand Down
16 changes: 16 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var ffmpeg = require('fluent-ffmpeg');

// Note: requires ffmpeg to have been installed on your system
ffmpeg()
.input('./backend/uploads/pics/caea3efe05f366b6619fddc55e769f12.wav')
.input('./backend/uploads/pics/0046f56e0132783a11479f1e7df32c8b.wav')
.input('./backend/uploads/pics/b19ba40a5d59ac2e4c77bdc73085b90c.wav')
.on('error', function(err) {
console.log('An error occurred: ' + err.message);
})
.on('end', function() {
console.log('Merging finished !');
})
.mergeToFile('./temp/merged.wav');

// deleting files subsequently?

0 comments on commit 74cdf37

Please sign in to comment.