INDExOS / media-for-mobile

Media for Mobile
Other
456 stars 178 forks source link

presentationTimeNs is not computed correctly in org.m4m.android.MicrophoneSource #71

Open sjvc opened 6 years ago

sjvc commented 6 years ago

In org.m4m.android.MicrophoneSource, inside pull(Frame frame), I found this:

long presentationTimeNs = (actualRead / (sampleRate * sampleSize * recordChannels)) / 1000000000;
long sampleTimeMicrosec = (System.nanoTime() - startTimeNs + presentationTimeNs) / 1000;
frame.setSampleTime(sampleTimeMicrosec);

I assume that presentationTimeNs it's the equivalent time in nanoseconds to actualRead bytes. But, using the code above, presentationTimeNs will always be 0.

If 1 second of data is equivalent to sampleRate * sampleSize * recordChannels bytes, then N bytes are processed in N / (sampleRate * sampleSize * recordChannels) seconds. So, to get nanoseconds correctly we should do:

long presentationTimeNs = (actualRead / (sampleRate * sampleSize * recordChannels)) * 1000000000;

But, look: denominator is an integer!, so we are still getting always 0. This is the correct way:

long presentationTimeNs = (long)((actualRead / (float)(sampleRate * sampleSize * recordChannels)) * 1000000000);

Is this correct?