I want to write sonar/radar like application. For sake of simplicity (and not to use adruino and specialized RTOS) - I simply used using audio in/out analog ports of a motherboard integrated card as an sample hardware setup + Windows API Steinberg ASIO SDK with MSVS 2015. So, for my latency tests i just used jack-jack cable from output(green) to linein(blue) minijacks. And, in my application, i have 2 important callbacks:
const int halfPeriod = 2; // 48khz rect wave (192khz samplerate)
void on_playback_finished(short* buffer, int length=1024) {
for(int i=0;i<length;i++)
buffer[i]= (i<256) ?
(((i / halfPeriod) % 2) * 60000 - 30000 )
: 0;
}
It's quite simple, it produces short "spike" (1/4th of length of the whole buffer) of square wave, rest of buffer is filled with silence.
And then second callback - which is called just when i recorded buffer from line in. And this is my question. I must somehow detect this spike (is it actually recorded, at all?) and measure latency, offset from start of recorded buffer in samples; and return that sample offset:
int on_recorded_buffer(short* buffer, int length=1024) {
// there is our spike, somewhere in the buffer[] samples array
// how to detect beginning of that spike,
// considering attenuation and possible induced noise?
return 0; // ideal case - zero sample latency
// that means "spike is detected at very beginning of recorded buffer"
// return 42; // signal start is detected at 42th sample of recorded buffer
}
what to do? need assistance, some simple algorithm)
UPD. Consider signal is mono(channels=1), bits/sample=16, Sampling rate is 192kHz, Signal length is 256 samples (1.33333ms), pause between signals is at least 768 samples (4ms). I am asking about proper algorithm of doing sample offset calculation!
UPD2 Iam not very good drawer, but to easier understanding look:

To avoid extra questions: Transmitted signal always starts at beginning of buffer. Consider transmit and recieve buffers have same timepoint (e.g. software is perfect, adds no latency). Red lines represent HW/Software transitions. Detect recieved signal and calculate offset of it start.