Discussion:
[Portaudio] determine the seconds since playing started / the currently playing sample index
Patrick Dehne
2008-04-23 13:26:15 UTC
Permalink
Hi All,

I have statistics of a wav-file containing spectrum analysis
information. To display this information in sync with the currently
playing audio stream I need a way to determine the currently playing
sample index.

If I understand the portaudio api correctly for this purpose the
Pa_GetStreamTime function can be used. The documentation states:

PaTime Pa_GetStreamTime(PaStream * stream)
Determine the current time for the stream according to the same clock
used to generate buffer timestamps. This time may be used for
syncronising other events to the audio stream, for example synchronizing
audio to MIDI.

The currently sample index could then be calculated like this:
PaGetStreamTime * SampleRate * NumberOfChannels

Maybe I am doing something stupid but I am not able to calculate the
correct sample index. PaGetStreamTime returns at the start of the stream
an arbitary number. not 0 as I expected. I digged into the sources and
saw that under windows (which I use) the result of PaGetStreamTime is
the return value of QueryPerformanceCounter. This explained the returned
value but I am still without a solution ;-)

Can someone give me some guidance how to correctly calculate the
currently playing sample index / the number of seconds passed since the
stream started playing? To be in sync with the playing audio the output
latency needs to be taken into account.

Thanks in advance!

Patrick
Albert Santoni
2008-04-23 17:34:07 UTC
Permalink
Hi Patrick,
Post by Patrick Dehne
Hi All,
I have statistics of a wav-file containing spectrum analysis
information. To display this information in sync with the currently
playing audio stream I need a way to determine the currently playing
sample index.
If I understand the portaudio api correctly for this purpose the
PaTime Pa_GetStreamTime(PaStream * stream)
Determine the current time for the stream according to the same clock
used to generate buffer timestamps. This time may be used for
syncronising other events to the audio stream, for example synchronizing
audio to MIDI.
PaGetStreamTime * SampleRate * NumberOfChannels
Maybe I am doing something stupid but I am not able to calculate the
correct sample index. PaGetStreamTime returns at the start of the stream
an arbitary number. not 0 as I expected. I digged into the sources and
saw that under windows (which I use) the result of PaGetStreamTime is
the return value of QueryPerformanceCounter. This explained the returned
value but I am still without a solution ;-)
I'm not an expert on this, but what happens if you try saving the
initial value from PaGetStreamTime right after the stream is started?

The formula would then be something like:

curSampleIndex = (?PaGetStreamTime - originalTime) * SampleRate *
NumberOfChannels

Albert

Loading...