stock username
stock username

Reputation: 49

Processing data from several threads in sync

Do I need to worry about syncing issues when working with multiple threads?

I'm programming a code which calculates the latency between separate microphone channels and outputs several .wav files with the latency compensated.

Here is my current brute-force method:

  1. Create threads for each of the microphone (using NAudio WaveIn).
  2. Every time the recorded data is available, append them in big static buffers (1 for each mic).
  3. Wait for an impulse (a clap, in this case) to be detected.
  4. Look for peaks and determine the 'distance' (the difference between the array indices).
  5. After the recording is finished, store the recorded data from static memory into .wav files with the appropriate offset for each of the microphones.

My current method (number 2) works if the total recording session is short since the size of the static buffers is limited. I was feeling unsure whether the recorded data were going to be in sync or not if I accessed the recorded data every time they are available.

Possible solutions:

  1. Store them in a circular buffer. Implement a counter for each thread and increment whenever new data is available. This gives me the ability to track any possible sync issues.

  2. Don't worry about the issue because they are nonexistent?

  3. Other possible, more efficient methods that I'm unaware of?

Upvotes: 1

Views: 100

Answers (1)

srandppl
srandppl

Reputation: 571

The data you are receiving is generally equidistantly spaced and I don't see any interoperation between your threads - so computation-wise you should be good: However, in my experiences NAudio likes to drop frames if your WaveIn callback is not processed right away. This can certainly get you out of sync.

Upvotes: 1

Related Questions