psychPortAudio / getStatus: how to know which sample is now being played

Hi,
I read a post on the forum that was similar to my question (#9244), but did not quite answer it. I will have a sound sequence of about 10 seconds in duration that will repeat over and over again for around 40 minutes - just some quiet background sounds like crickets chirping etc... At certains times I will want to ask the sound card "which sample are you playing right now?" If I use the getStatus option of PsychPortAudio, I can read out the PositionSecs field, but when I do this I get a number that seems to be the sample number with respect to the time of sound 'start', rather than something modulo the number of samples in the sound sequence. This made me wonder whether or not I was really probing the sample the sound card just played, or a (perhaps drifty) estimate of the sample the sound card just played. I agree that for short trials any clock drift is negligible, but after 10, 20, 30 minutes, and not being able to tolerate more than about 100 ms of slop, drift will be a problem. If I can be sure that 'GetStatus' tells me which sample was being played at the time (give or take the time it takes to ping the sound card), then I will just take the modulus of 'PositionSecs' and the length of my sound sequence and be assured that this number is not drifting relative to the sound coming out of the speaker.
Thanks!
Aaron
You can look at the ElapsedOutSamples field and CurrentStreamTime field. CurrentStreamTime tells you the system time (GetSecs time) at which ElapsedOutSamples samples will have left the speaker, counting from start of playback.

E.g., if you get these values at two separate points in time and divide the difference in ElapsedOutSamples by the difference in CurrentStreamTime, you can calculate the true playback rate, which will be slightly different from the selected sample rate due to clock drift between system clock and sound card clock. Or you calculate accumulated drift over time by comparing those counts and timestamps with what you'd expect if playback were perfect.

You could also detect sound dropouts (e.g., caused by massive system timing problems / system overload) by comparing the number of played samples against the expected value during a given time window.

The Xruns return field has the same purpose. It increments to a non-zero value each time an audio glitch due to buffer overrun or underrun is detected. The detector is not perfect, its miss rate is dependent on operating system, sound system, sound card and driver.

Of course, dropouts shouldn't be an issue if your system works correctly, only drift should be a concern.

Keep in mind that the returned values don't increment continuously. The sound card fetches samples in blocks of n samples for efficiency reasons, e.g., blocks with typically n=64, 128, 256 or 512 samples each, depending on system, sound card and sound settings. The counts and timestamps are only updated at block boundaries, so both the returned CurrentStreamTime and ElapsedOutSamples (and PositionSecs) "jump" with a certain granularity. However, the values are consistent with respect to each other.

Typical drift at least on my machine, as measured this way, with a bog-standard Intel HDA onboard soundchip is less than 0.25 samples per second at 48khz == about 20 microseconds per second, not too bad, probably good enough for your purpose.

For non-continuous but periodic signals you can program repetitive schedules, as shown in BasicAMAndMixScheduleDemo.m with a repetitive 3 tone sound sequence, in which playback of a sound buffer is automatically restarted/repeated ad infinitum, but with the start time of each repetition locked to system time, e.g., if your sound buffer is 10 seconds and you can allow for a pause of 0.1 secs between repetitions, you could program it so that each repetition of the 10 secs buffer starts 10.1 secs after start of the previous repetition. This way, playback can't drift wrt. to the system clock, as it is resynchronized after each 10 secs cycle.

hope it helps,
-mario


--- In psychtoolbox@yahoogroups.com, "aschurger" <aaron.schurger@...> wrote:
>
> Hi,
> I read a post on the forum that was similar to my question (#9244), but did not quite answer it. I will have a sound sequence of about 10 seconds in duration that will repeat over and over again for around 40 minutes - just some quiet background sounds like crickets chirping etc... At certains times I will want to ask the sound card "which sample are you playing right now?" If I use the getStatus option of PsychPortAudio, I can read out the PositionSecs field, but when I do this I get a number that seems to be the sample number with respect to the time of sound 'start', rather than something modulo the number of samples in the sound sequence. This made me wonder whether or not I was really probing the sample the sound card just played, or a (perhaps drifty) estimate of the sample the sound card just played. I agree that for short trials any clock drift is negligible, but after 10, 20, 30 minutes, and not being able to tolerate more than about 100 ms of slop, drift will be a problem. If I can be sure that 'GetStatus' tells me which sample was being played at the time (give or take the time it takes to ping the sound card), then I will just take the modulus of 'PositionSecs' and the length of my sound sequence and be assured that this number is not drifting relative to the sound coming out of the speaker.
> Thanks!
> Aaron
>