summaryrefslogtreecommitdiff
path: root/sound_detector/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'sound_detector/main.cpp')
-rw-r--r--sound_detector/main.cpp53
1 files changed, 27 insertions, 26 deletions
diff --git a/sound_detector/main.cpp b/sound_detector/main.cpp
index 77557d5..9cb67e7 100644
--- a/sound_detector/main.cpp
+++ b/sound_detector/main.cpp
@@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <complex.h>
//c++
#include <vector>
@@ -25,7 +26,8 @@
-unsigned int precapture = 3, postcapture = 7, min_length = 1, thresold_percent = 5;
+unsigned int precapture = 3, postcapture = 7, min_length = 1, thresold = 0, stop_thresold = 0;
+float thresold_percent = 5.0, stop_thresold_percent = 5.0;
unsigned rate = 0;
bool sound_detected = false, debug = false;
@@ -128,23 +130,21 @@ void handle_data()
vorbis_params vparams;
while(true)
{
- boost::this_thread::sleep(boost::posix_time::seconds(1));
+ boost::this_thread::sleep(boost::posix_time::milliseconds(100));
if(!sound_detected)
{
- if(debug)
- printf("buffer contain %lu, required %d\n", buffer.size(), rate*precapture);
if(buffer.size() > rate * precapture)
{
- int thresold = (int)(((float)INT16_MAX/100.0)*(float)thresold_percent), noise = 0;
+/* if(debug)
+ printf("buffer contain %lu, required %d\n", buffer.size(), rate*precapture); */
+ unsigned long level = 0;
lock.lock();
for(size_t i = 0; i < buffer.size(); i++)
- {
- if((buffer[i] > thresold) || (buffer[i] < -thresold))
- noise++;
- }
+ level += abs(buffer[i]);
+ level /= buffer.size();
if(debug)
- printf("noise detected %d, noise required %d\n", noise, min_length * rate);
- if(noise > ((min_length * rate))) //need to do some research
+ printf("level detected %lu, level required %d\n", level, thresold);
+ if(level > thresold)
{
printf("write started\n");
boost::filesystem::path p(out_dir);
@@ -163,24 +163,20 @@ void handle_data()
}
else
{
- if(debug)
- printf("buffer contain %lu, required %d\n", buffer.size(), rate*postcapture);
if(buffer.size() > rate * postcapture)
{
- int thresold = (int)(((float)INT16_MAX/100.0)*(float)thresold_percent), silence = 0;
+/* if(debug)
+ printf("buffer contain %lu, required %d\n", buffer.size(), rate*postcapture); */
+ unsigned long level = 0;
lock.lock();
for(size_t i = 0; i < buffer.size(); i++)
- {
- if((buffer[i] < thresold) || (buffer[i] > -thresold) )
- silence++;
- else
- silence = 0;
- }
+ level += abs(buffer[i]);
+ level /= buffer.size();
encode_data(vparams);
if(debug)
- printf("silence detected %d, silence required %d\n", silence, postcapture * rate);
+ printf("level detected %lu, level lower than %d required to stop\n", level, stop_thresold);
lock.unlock();
- if(silence > (rate * postcapture))
+ if(level < stop_thresold)
{
if(debug)
printf("write stopped\n");
@@ -238,12 +234,12 @@ int main(int argc, char **argv)
int opt = -1, device = -1;
if(argc == 1)
{
- printf("usage:\n%s -h -l -f -v -d <dev number> -o <path> -p <sec> -P <sec> -m <sec> -s <percents>\n\t-h\tthis help message\n\t-l\tdevice list\n\t-d\tdevice number from device list\n\t-o\toutput directory\n\t-f\tfork to bakground\n\t-v\tverbose\n\t-P\tpre capture seconds\n\t-p\tpost capture seconds\n\t-m\tminimum capture length\n\t-s\tsound level\n", argv[0]);
+ printf("usage:\n%s -h -l -f -v -d <dev number> -o <path> -p <sec> -P <sec> -m <sec> -s <float percents> -S <float percents>\n\t-h\tthis help message\n\t-l\tdevice list\n\t-d\tdevice number from device list\n\t-o\toutput directory\n\t-f\tfork to bakground\n\t-v\tverbose\n\t-P\tpre capture seconds\n\t-p\tpost capture seconds\n\t-m\tminimum capture length\n\t-s\tsound level in float value 5,0%% default\n\t-S minimum sound level to stop recording in float value 5,0%% is default\n", argv[0]);
return 1;
}
bool _fork = false;
//const unsigned int pecapture = 3, postcapture = 7, min_length = 1, thresold_percent = 5;
- while((opt = getopt(argc, argv, "hlfvp:P:m:s:d:o:")) != -1)
+ while((opt = getopt(argc, argv, "hlfvp:P:m:s:S:d:o:")) != -1)
{
switch(opt)
{
@@ -269,7 +265,10 @@ int main(int argc, char **argv)
min_length = atoi(optarg);
break;
case 's':
- thresold_percent = atoi(optarg);
+ thresold_percent = strtof(optarg, NULL);
+ break;
+ case 'S':
+ stop_thresold_percent = strtof(optarg, NULL);
break;
case 'o':
strcpy(out_dir, optarg);
@@ -282,7 +281,7 @@ int main(int argc, char **argv)
break;
case 'h':
default:
- printf("usage:\n%s -h -l -f -v -d <dev number> -o <path> -p <sec> -P <sec> -m <sec> -s <percents>\n\t-h\tthis help message\n\t-l\tdevice list\n\t-d\tdevice number from device list\n\t-o\toutput directory\n\t-f\tfork to bakground\n\t-v\tverbose\n\t-P\tpre capture seconds\n\t-p\tpost capture seconds\n\t-m\tminimum capture length\n\t-s\tsound level\n", argv[0]);
+ printf("usage:\n%s -h -l -f -v -d <dev number> -o <path> -p <sec> -P <sec> -m <sec> -s <float percents> -S <float percents>\n\t-h\tthis help message\n\t-l\tdevice list\n\t-d\tdevice number from device list\n\t-o\toutput directory\n\t-f\tfork to bakground\n\t-v\tverbose\n\t-P\tpre capture seconds\n\t-p\tpost capture seconds\n\t-m\tminimum capture length\n\t-s\tsound level in float value 5,0%% default\n\t-S minimum sound level to stop recording in float value 5,0%% is default\n", argv[0]);
break;
};
}
@@ -306,6 +305,8 @@ int main(int argc, char **argv)
printf("ERROR: output directory is not directory %%) \n");
return 1;
}
+ thresold = (int)(((float)INT16_MAX/100.0)*thresold_percent);
+ stop_thresold = (int)(((float)INT16_MAX/100.0)*stop_thresold_percent);
PaStream *stream;
PaStreamParameters params;
memset(&params, 0, sizeof(PaStreamParameters));