-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added metadata event based on mic info #23
Open
xavicolomer
wants to merge
2
commits into
ashishbajaj99:master
Choose a base branch
from
xavicolomer:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
'use strict'; | ||
|
||
const extractMetadata = (info) => { | ||
if (info.indexOf('Hd:') === -1) { | ||
info = info.replace(/]( )+Clip:/g, '] Hd:0.0 Clip:'); | ||
} | ||
|
||
const regex = /In:([0-9.]+)% ([0-9]{2}):([0-9]{2}):([0-9]{2}).([0-9]{2}) \[([0-9]{2}):([0-9]{2}):([0-9]{2}).([0-9]{2})\] Out:([0-9.]+)k? +?\[[\! \-\=]+\|([\! \-\=]+)\] Hd:([0-9.]+)? Clip:([0-9]+)/g; | ||
const metadata = { | ||
in: 0.0, | ||
timestamp: 0, | ||
timestamp2: 0, | ||
out: 0.0, | ||
volume: 0, | ||
hd: 0, | ||
clip: 0 | ||
}; | ||
|
||
let m; | ||
|
||
while ((m = regex.exec(info)) !== null) { | ||
if (m.index === regex.lastIndex) { | ||
regex.lastIndex++; | ||
} | ||
|
||
if (m.length < 2) { | ||
return 0; | ||
} | ||
|
||
let timestampItems; | ||
|
||
for (let i = 1, len = m.length; i < len; ++i) { | ||
switch (i) { | ||
case 1: | ||
metadata.in = parseFloat(m[i]); | ||
break; | ||
case 2: | ||
timestampItems = m.slice(2, 6); | ||
metadata.timestamp = extractTimestamp(timestampItems); | ||
i = i + 3; | ||
break; | ||
case 6: | ||
timestampItems = m.slice(6, 10); | ||
metadata.timestamp2 = extractTimestamp(timestampItems); | ||
i = i + 3; | ||
break; | ||
case 10: | ||
metadata.out = parseFloat(m[i]); | ||
break; | ||
case 11: | ||
metadata.volume = extractVolumeLevel(m[i]); | ||
break; | ||
case 12: | ||
metadata.hd = parseFloat(m[i]); | ||
break; | ||
case 13: | ||
metadata.clip = parseInt(m[i], 10); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return metadata; | ||
} | ||
|
||
const extractTimestamp = (items) => { | ||
let timestamp = 0; | ||
let value = 0; | ||
|
||
for (let i = 0, len = items.length; i < len; ++i) { | ||
value = parseInt(items[i], 10); | ||
switch (i) { | ||
case 0: | ||
value *= 60; | ||
case 1: | ||
value *= 60; | ||
case 2: | ||
value *= 1000; | ||
break; | ||
case 3: | ||
value *= 10; | ||
break; | ||
} | ||
|
||
timestamp = timestamp + value; | ||
} | ||
|
||
return timestamp; | ||
} | ||
|
||
const extractVolumeLevel = (ascii) => { | ||
let volume = 0; | ||
|
||
for (let i = 0, len = ascii.length; i < len; ++i) { | ||
switch (ascii[i]) { | ||
case ' ': | ||
break; | ||
case '=': | ||
volume = volume + 2; | ||
break; | ||
case '-': | ||
volume = volume + 1; | ||
break; | ||
case '!': | ||
volume = volume + 4; | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
return volume; | ||
} | ||
|
||
module.exports = { | ||
extractMetadata | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ var isMac = require('os').type() == 'Darwin'; | |
var isWindows = require('os').type().indexOf('Windows') > -1; | ||
var IsSilence = require('./silenceTransform.js'); | ||
var PassThrough = require('stream').PassThrough; | ||
var extractMetadata = require('./metadata').extractMetadata; | ||
|
||
var mic = function mic(options) { | ||
options = options || {}; | ||
|
@@ -24,17 +25,15 @@ var mic = function mic(options) { | |
stdio: ['ignore', 'pipe', 'ignore'] | ||
}; | ||
|
||
if(debug) { | ||
audioProcessOptions.stdio[2] = 'pipe'; | ||
} | ||
audioProcessOptions.stdio[2] = 'pipe'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are you removing this from within the debug flag? |
||
|
||
// Setup format variable for arecord call | ||
if(endian === 'big') { | ||
if (endian === 'big') { | ||
formatEndian = 'BE'; | ||
} else { | ||
formatEndian = 'LE'; | ||
} | ||
if(encoding === 'unsigned-integer') { | ||
if (encoding === 'unsigned-integer') { | ||
formatEncoding = 'U'; | ||
} else { | ||
formatEncoding = 'S'; | ||
|
@@ -43,14 +42,14 @@ var mic = function mic(options) { | |
audioStream.setNumSilenceFramesExitThresh(parseInt(exitOnSilence, 10)); | ||
|
||
that.start = function start() { | ||
if(audioProcess === null) { | ||
if(isWindows){ | ||
if (audioProcess === null) { | ||
if (isWindows){ | ||
audioProcess = spawn('sox', ['-b', bitwidth, '--endian', endian, | ||
'-c', channels, '-r', rate, '-e', encoding, | ||
'-t', 'waveaudio', 'default', '-p'], | ||
audioProcessOptions) | ||
} | ||
else if(isMac){ | ||
else if (isMac){ | ||
audioProcess = spawn('rec', ['-b', bitwidth, '--endian', endian, | ||
'-c', channels, '-r', rate, '-e', encoding, | ||
'-t', fileType, '-'], audioProcessOptions) | ||
|
@@ -61,61 +60,69 @@ var mic = function mic(options) { | |
} | ||
|
||
audioProcess.on('exit', function(code, sig) { | ||
if(code != null && sig === null) { | ||
if (code != null && sig === null) { | ||
audioStream.emit('audioProcessExitComplete'); | ||
if(debug) console.log("recording audioProcess has exited with code = %d", code); | ||
if (debug) console.log("recording audioProcess has exited with code = %d", code); | ||
} | ||
}); | ||
|
||
audioProcess.stdout.pipe(audioStream); | ||
if(debug) { | ||
audioProcess.stderr.pipe(infoStream); | ||
} | ||
audioProcess.stderr.pipe(infoStream); | ||
|
||
audioStream.emit('startComplete'); | ||
} else { | ||
if(debug) { | ||
if (debug) { | ||
throw new Error("Duplicate calls to start(): Microphone already started!"); | ||
} | ||
} | ||
}; | ||
|
||
that.stop = function stop() { | ||
if(audioProcess != null) { | ||
if (audioProcess != null) { | ||
audioProcess.kill('SIGTERM'); | ||
audioProcess = null; | ||
audioStream.emit('stopComplete'); | ||
if(debug) console.log("Microphone stopped"); | ||
if (debug) console.log("Microphone stopped"); | ||
} | ||
}; | ||
|
||
that.pause = function pause() { | ||
if(audioProcess != null) { | ||
if (audioProcess != null) { | ||
audioProcess.kill('SIGSTOP'); | ||
audioStream.pause(); | ||
audioStream.emit('pauseComplete'); | ||
if(debug) console.log("Microphone paused"); | ||
if (debug) console.log("Microphone paused"); | ||
} | ||
}; | ||
|
||
that.resume = function resume() { | ||
if(audioProcess != null) { | ||
if (audioProcess != null) { | ||
audioProcess.kill('SIGCONT'); | ||
audioStream.resume(); | ||
audioStream.emit('resumeComplete'); | ||
if(debug) console.log("Microphone resumed"); | ||
if (debug) console.log("Microphone resumed"); | ||
} | ||
} | ||
|
||
that.getAudioStream = function getAudioStream() { | ||
return audioStream; | ||
} | ||
|
||
if(debug) { | ||
infoStream.on('data', function(data) { | ||
if (audioProcess != null) { | ||
const metadata = extractMetadata(data.toString()); | ||
audioStream.emit('metadata', metadata); | ||
} | ||
}); | ||
|
||
if (debug) { | ||
infoStream.on('data', function(data) { | ||
console.log("Received Info: " + data); | ||
}); | ||
console.log("Received Info: " + data + "!!"); | ||
}); | ||
|
||
infoStream.on('error', function(error) { | ||
console.log("Error in Info Stream: " + error); | ||
}); | ||
console.log("Error in Info Stream: " + error); | ||
}); | ||
} | ||
|
||
return that; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove the commented line