C++ How to detect audio signal from mic

I am working on a C++ program which will do the following:

1. Waits for any audio signal from an integrated laptop microphone
2. Logs that an event occurred in a .txt file (time calculated by counter)
3. Repeats

Eventually the sound will be a single clap in an otherwise quiet room. However at this point detection of ANY sound will be fine. I have investigated the use of waveIn() and the DirectSound libraries, also the use of APIs, but they seem to be complete overkill for this task. All I would like is to detect an audio signal in so that a flag may be set. From this flag the log loop will execute. Then the flag is reset and the system continues to listen. It will do this in 1 second intervals. I am not looking for code from you, I would just like some feedback on what to start with. I'm drowning in library features right now that I don't need.

Here is my code. Please note that I am not including the headers and all of my Visual C++ 2008 Express Edition project files. If it is needed I will post them, but I thought I would make this concise at first. Also ignore the rest of the stuff, I am still working on it. The part where I need to put the audio event detection code is labeled "// Audio signal event detection." Thanks in advance.

// CodeTimer.cpp : main project file.
// Using Query Performance Counter.

#include "stdafx.h"
#include <tchar.h>
#include <windows.h>
#include <iostream>
#include <fstream>

using namespace System;
using namespace std;

int main()
{
// Prepare log for output.
ofstream timeLog ("log.txt");

if (timeLog.is_open())
{
cout << "Events Detected\n";
cout << "---------------\n\n";
//timeLog.close();
}

else cout << "Unable to open file";

// Initialize timer when condition met.
int never = 0;
int clap = 0;

while(never == 0)
{
// Audio signal event detection.


// Timer series.
__int64 ctr1 = 0, ctr2 = 0, freq = 0;
int acc = 0, i = 0;

// Start timing the code.
if (QueryPerformanceCounter((LARGE_INTEGER *)&ctr1)!= 0)
{
// Code segment is being timed.
while (clap != 1)
{
acc++;

if (clap == 1)
{
timeLog << "Event logged\n";
}
}

// Finish timing the code.
QueryPerformanceCounter((LARGE_INTEGER *)&ctr2);

Console::WriteLine("Start Value: {0}",ctr1.ToString());
Console::WriteLine("End Value: {0}",ctr2.ToString());

QueryPerformanceFrequency((LARGE_INTEGER *)&freq);

Console::WriteLine(S"QueryPerformanceCounter minimum resolution: 1/{0} Seconds.",freq.ToString());
// In Visual Studio 2005, this line should be changed to: Console::WriteLine("QueryPerformanceCounter minimum resolution: 1/{0} Seconds.",freq.ToString());
Console::WriteLine("100 Increment time: {0} seconds.",((ctr2 - ctr1) * 1.0 / freq).ToString());
}
else
{
DWORD dwError = GetLastError();
Console::WriteLine(S"Error value = {0}",dwError.ToString());// In Visual Studio 2005, this line should be changed to: Console::WriteLine("Error value = {0}",dwError.ToString());
}

// Make the console window wait.
Console::WriteLine();
Console::Write("Press ENTER to finish.");
Console::Read();
}

return 0;
}
Topic archived. No new replies allowed.