clap-detector
Advanced tools
Comparing version 0.2.1 to 0.3.0
52
index.js
@@ -7,3 +7,3 @@ /** | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
of this software and associated documentation files (the "Software"), to dealFWAV_ | ||
in the Software without restriction, including without limitation the rights | ||
@@ -34,2 +34,3 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
var fs = require('fs'); | ||
var appRoot = require('app-root-path'); | ||
@@ -49,3 +50,4 @@ var clapDetector = (function() { | ||
CLAP_MAX_DURATION: 1500, | ||
MAX_HISTORY_LENGTH: 10 // no need to maintain big history | ||
MAX_HISTORY_LENGTH: 10, // no need to maintain big history | ||
WAV_FOLDER: 'wav' | ||
}; | ||
@@ -96,3 +98,3 @@ | ||
var random = _.random(0,99999); | ||
var filename = 'input' + random + '.wav'; | ||
var filename = appRoot + '/' + CONFIG.WAV_FOLDER + '/input' + random + '.wav'; | ||
@@ -121,5 +123,5 @@ // Listen for sound | ||
// remove file(s) | ||
fs.unlink(filename); | ||
fs.unlinkSync(filename); | ||
if(filename !== newFilename) { | ||
fs.unlink(newFilename); | ||
fs.unlinkSync(newFilename); | ||
} | ||
@@ -130,2 +132,4 @@ }); | ||
}); | ||
//child.umask(0000); | ||
} | ||
@@ -139,5 +143,5 @@ | ||
var cmd = "sox " + filename + " -n stat 2>&1"; //| sed -n 's#^Length (seconds):[^0-9]*\\([0-9.]*\\)$#\\1#p' | ||
var regExDuration = /Length[\s]+\(seconds\):[\s]+([0-9.]+)/; | ||
var regExRms = /RMS[\s]+amplitude:[\s]+([0-9.]+)/; | ||
var regExMax = /Maximum[\s]+amplitude:[\s]+([0-9.]+)/; | ||
var regExDuration = /Length[\s]+\(seconds\):[\s]+(-*[0-9.]+)/; | ||
var regExRms = /RMS[\s]+amplitude:[\s]+(-*[0-9.]+)/; | ||
var regExMax = /Maximum[\s]+amplitude:[\s]+(-*[0-9.]+)/; | ||
exec(cmd, function(error, out, stderr) { | ||
@@ -183,2 +187,23 @@ if(error) { | ||
function _prepareAudioFolder() { | ||
// Create folder for audio files | ||
var folder = appRoot + '/' + CONFIG.WAV_FOLDER; | ||
if (!fs.existsSync(folder)){ | ||
fs.mkdirSync(folder, 0777); | ||
} | ||
// Delete all files in folder | ||
try { var files = fs.readdirSync(folder); } | ||
catch(e) { return; } | ||
if (files.length > 0) { | ||
_.forEach(files, function(file) { | ||
var filePath = folder + '/' + file; | ||
fs.unlinkSync(filePath, function(err, out) { | ||
console.log("error deleting file", filePath, err, out); | ||
}); | ||
}); | ||
} | ||
} | ||
return { | ||
@@ -189,3 +214,12 @@ start: function (props) { | ||
} | ||
_listen(); | ||
// Set up umask for files creation | ||
process.umask(0000); | ||
// Prepare audio folder | ||
_prepareAudioFolder(); | ||
// Start listening | ||
setTimeout(function() { | ||
_listen(); | ||
}.bind(this), 2000); | ||
}, | ||
@@ -192,0 +226,0 @@ |
{ | ||
"name": "clap-detector", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Clap detection for node js (linux, raspberry systems)", | ||
@@ -36,2 +36,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"app-root-path": "^1.0.0", | ||
"lodash": "^3.10.1", | ||
@@ -38,0 +39,0 @@ "q": "^1.4.1" |
13023
206
3
3
+ Addedapp-root-path@^1.0.0
+ Addedapp-root-path@1.4.0(transitive)