diff --git a/NAudio/CoreAudioApi/AudioClockClient.cs b/NAudio/CoreAudioApi/AudioClockClient.cs index a48392fe..59f03ded 100644 --- a/NAudio/CoreAudioApi/AudioClockClient.cs +++ b/NAudio/CoreAudioApi/AudioClockClient.cs @@ -62,9 +62,6 @@ public ulong AdjustedPosition { get { - // figure out ticks per byte (for later) - var byteLatency = (TimeSpan.TicksPerSecond / Frequency); - ulong pos, qpos; int cnt = 0; while (!GetPosition(out pos, out qpos)) @@ -83,14 +80,14 @@ public ulong AdjustedPosition // get the current qpc count (in ticks) var qposNow = (ulong)((Stopwatch.GetTimestamp() * 10000000M) / Stopwatch.Frequency); - // find out how many ticks has passed since the device reported the position - var qposDiff = (qposNow - qpos) / 100; + // find out how many ticks have passed since the device reported the position + var qposDiff = qposNow - qpos; - // find out how many byte would have played in that time span - var bytes = qposDiff / byteLatency; + // find out how many device position units (usually bytes) would have played in that time span + var posDiff = (qposDiff * Frequency) / TimeSpan.TicksPerSecond; // add it to the position - pos += bytes; + pos += posDiff; } return pos; } diff --git a/NAudio/Wave/WaveOutputs/WasapiOut.cs b/NAudio/Wave/WaveOutputs/WasapiOut.cs index 35a40cba..309ab40d 100644 --- a/NAudio/Wave/WaveOutputs/WasapiOut.cs +++ b/NAudio/Wave/WaveOutputs/WasapiOut.cs @@ -270,11 +270,19 @@ private WaveFormat GetFallbackFormat() /// Position in bytes public long GetPosition() { - if (playbackState == PlaybackState.Stopped) + ulong pos; + switch (playbackState) { - return 0; + case PlaybackState.Stopped: + return 0; + case PlaybackState.Playing: + pos = audioClient.AudioClockClient.AdjustedPosition; + break; + default: // PlaybackState.Paused + audioClient.AudioClockClient.GetPosition(out pos, out _); + break; } - return (long)audioClient.AudioClockClient.AdjustedPosition; + return ((long)pos * outputFormat.AverageBytesPerSecond) / (long)audioClient.AudioClockClient.Frequency; } ///