From c3327b9c804ee3a25d8e09fa8f92e5ae5b978615 Mon Sep 17 00:00:00 2001 From: goddogthedoggod <60386588+goddogthedoggod@users.noreply.github.com> Date: Tue, 28 Jan 2020 17:11:44 +0100 Subject: [PATCH 1/2] Corrected calculation of AdjustedPosition by removing a stray division by 100. Added conversion of result of AdjustedPosition from device position units into bytes to WasapiOut.GetPosition. --- NAudio/CoreAudioApi/AudioClockClient.cs | 13 +++++-------- NAudio/Wave/WaveOutputs/WasapiOut.cs | 3 ++- global.json | 2 +- 3 files changed, 8 insertions(+), 10 deletions(-) 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..d84b13f4 100644 --- a/NAudio/Wave/WaveOutputs/WasapiOut.cs +++ b/NAudio/Wave/WaveOutputs/WasapiOut.cs @@ -274,7 +274,8 @@ public long GetPosition() { return 0; } - return (long)audioClient.AudioClockClient.AdjustedPosition; + var clock = audioClient.AudioClockClient; + return ((long)clock.AdjustedPosition * outputFormat.AverageBytesPerSecond) / (long)clock.Frequency; } /// diff --git a/global.json b/global.json index dc03d9ee..80d84a25 100644 --- a/global.json +++ b/global.json @@ -1,5 +1,5 @@ { "msbuild-sdks": { - "MSBuild.Sdk.Extras": "2.0.31" + "MSBuild.Sdk.Extras": "2.0.54" } } \ No newline at end of file From 12c2d9ab7331b41864a66b625adb888860a03f8a Mon Sep 17 00:00:00 2001 From: goddogthedoggod <60386588+goddogthedoggod@users.noreply.github.com> Date: Wed, 29 Jan 2020 11:25:36 +0100 Subject: [PATCH 2/2] Must not use AdjustedPosition when playback is paused. --- NAudio/Wave/WaveOutputs/WasapiOut.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/NAudio/Wave/WaveOutputs/WasapiOut.cs b/NAudio/Wave/WaveOutputs/WasapiOut.cs index d84b13f4..309ab40d 100644 --- a/NAudio/Wave/WaveOutputs/WasapiOut.cs +++ b/NAudio/Wave/WaveOutputs/WasapiOut.cs @@ -270,12 +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; } - var clock = audioClient.AudioClockClient; - return ((long)clock.AdjustedPosition * outputFormat.AverageBytesPerSecond) / (long)clock.Frequency; + return ((long)pos * outputFormat.AverageBytesPerSecond) / (long)audioClient.AudioClockClient.Frequency; } ///