diff options
-rw-r--r-- | sound_detector/main.cpp | 60 | ||||
-rw-r--r-- | sound_detector/sound_detector.project | 3 |
2 files changed, 43 insertions, 20 deletions
diff --git a/sound_detector/main.cpp b/sound_detector/main.cpp index 9950b4d..77557d5 100644 --- a/sound_detector/main.cpp +++ b/sound_detector/main.cpp @@ -25,10 +25,10 @@ -const unsigned int pecapture = 3, postcapture = 7, min_length = 1, thresold_percent = 5; +unsigned int precapture = 3, postcapture = 7, min_length = 1, thresold_percent = 5; unsigned rate = 0; -bool sound_detected = false; +bool sound_detected = false, debug = false; FILE *file = NULL; @@ -129,22 +129,24 @@ void handle_data() while(true) { boost::this_thread::sleep(boost::posix_time::seconds(1)); - //printf("buffer contain %u, required %d\n", buffer.size(), rate*3); if(!sound_detected) { - if(buffer.size() > rate * 3) + 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; + int thresold = (int)(((float)INT16_MAX/100.0)*(float)thresold_percent), noise = 0; lock.lock(); for(size_t i = 0; i < buffer.size(); i++) { - if(buffer[i] > thresold) + if((buffer[i] > thresold) || (buffer[i] < -thresold)) noise++; } - //printf("noise detected %d, noise required %d\n", noise, ((min_length * rate) / 10)); - if(noise > ((min_length * rate) /10)) //need to do some research + 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("write started\n"); + printf("write started\n"); boost::filesystem::path p(out_dir); p += "/"; p += time_str(); @@ -161,23 +163,27 @@ void handle_data() } else { - if(buffer.size() > (rate * ((postcapture > 3)?postcapture:3))) + 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; + int thresold = (int)(((float)INT16_MAX/100.0)*(float)thresold_percent), silence = 0; lock.lock(); for(size_t i = 0; i < buffer.size(); i++) { - if(buffer[i] < thresold) + if((buffer[i] < thresold) || (buffer[i] > -thresold) ) silence++; else silence = 0; } encode_data(vparams); - //printf("silence detected %d, silence required %d\n", silence, (postcapture * rate)); + if(debug) + printf("silence detected %d, silence required %d\n", silence, postcapture * rate); lock.unlock(); if(silence > (rate * postcapture)) { - //printf("write stopped\n"); + if(debug) + printf("write stopped\n"); fclose(file); encode_end(vparams); sound_detected = false; @@ -199,7 +205,7 @@ int stream_callback(const void *input, void *output, unsigned long frameCount, c { if(buf[i] > (int)((float)INT16_MAX/100.0*(float)thresold_percent)) printf("%d ", buf[i]); - } */ + } */ return paContinue; //debug end } @@ -232,11 +238,12 @@ int main(int argc, char **argv) int opt = -1, device = -1; if(argc == 1) { - printf("usage:\n%s -h -l -d <dev number> -o <path> -f\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 background\n", argv[0]); - return 0; + 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]); + return 1; } bool _fork = false; - while((opt = getopt(argc, argv, "hlfd:o:")) != -1) + //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) { switch(opt) { @@ -249,6 +256,21 @@ int main(int argc, char **argv) i, info->name, info->maxInputChannels, info->maxOutputChannels, info->defaultSampleRate, info->defaultLowInputLatency, info->defaultHighInputLatency); } break; + case 'v': + debug = true; + break; + case 'P': + precapture = atoi(optarg); + break; + case 'p': + postcapture = atoi(optarg); + break; + case 'm': + min_length = atoi(optarg); + break; + case 's': + thresold_percent = atoi(optarg); + break; case 'o': strcpy(out_dir, optarg); break; @@ -260,7 +282,7 @@ int main(int argc, char **argv) break; case 'h': default: - printf("usage:\n%s -h -l -d <dev number> -o <path>\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", argv[0]); + 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]); break; }; } diff --git a/sound_detector/sound_detector.project b/sound_detector/sound_detector.project index 9967e5c..7f24235 100644 --- a/sound_detector/sound_detector.project +++ b/sound_detector/sound_detector.project @@ -33,7 +33,7 @@ <Library Value="vorbisenc"/> </Linker> <ResourceCompiler Options="" Required="no"/> - <General OutputFile="$(IntermediateDirectory)/$(ProjectName)" IntermediateDirectory="./Debug" Command="./$(ProjectName)" CommandArguments="-d4 -o." UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes"/> + <General OutputFile="$(IntermediateDirectory)/$(ProjectName)" IntermediateDirectory="./Debug" Command="./$(ProjectName)" CommandArguments="-d0 -o." UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes"/> <Environment EnvVarSetName="<Use Defaults>" DbgSetName="<Use Defaults>"> <![CDATA[]]> </Environment> @@ -73,6 +73,7 @@ <General OutputFile="$(IntermediateDirectory)/$(ProjectName)" IntermediateDirectory="./Release" Command="./$(ProjectName)" CommandArguments="" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes"/> <Environment EnvVarSetName="<Use Defaults>" DbgSetName="<Use Defaults>"> <![CDATA[ + ]]> </Environment> <Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath=""> |