Hi Ali,
I work at MathWorks in the Audio System Toolbox team. I can't exactly replicate your experiement as I don't have your hardware, but I could notice a few possible sources of issues.
In your code you seem to be acquiring simultaneously from two different devices using the default audio drivers (typically DirectSound or WASAPI on Windows). That gives you no guarantee of synchronous acquisition for L and R. The two devices themselves may be triggered asynchronously by the operating system, giving you an arbitrary new delay between the two signals every single time you run your script.
The simplest guarantee of a synchronous acquisition comes from acquiring different channels of the same device, ideally using its ASIO driver instead of the default one. ASIO drivers guarantee synchronous multi-channel acquisition.
At a minimum, you should try something like the following, which gets L and R from the first two channels of the same device:
recObj = audiorecorder(fs, 16, 2, deviceID);
Even better, you may want to try audioDeviceReader object in Audio System Toolbox, which supports ASIO drivers and is routinely used out there for latency-sensitive acoustic measurements. See for example the first example from the reference doc page (Read from Microphone and Write to Audio File): deviceReader = audioDeviceReader('Driver','ASIO','NumChannels',2);
setup(deviceReader);
fileWriter = dsp.AudioFileWriter('myTwoChannelRecording.wav');
disp('Recording...')
tic;
while toc < 5
acquiredAudio = deviceReader();
fileWriter(acquiredAudio);
end
disp('Recording complete.')
release(deviceReader);
release(fileWriter);
audioDeviceReader also gives you the ability to acquire whatever arbitrary pair of channels from your device through what we call channel mapping.
I hope this helps.
Regards,
Gabriele.
Best Answer