Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed 3 issues around AudioClockClient.AdjustedPosition #584

Merged
merged 2 commits into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions NAudio/CoreAudioApi/AudioClockClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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;
}
Expand Down
14 changes: 11 additions & 3 deletions NAudio/Wave/WaveOutputs/WasapiOut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,19 @@ private WaveFormat GetFallbackFormat()
/// <returns>Position in bytes</returns>
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;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"msbuild-sdks": {
"MSBuild.Sdk.Extras": "2.0.31"
"MSBuild.Sdk.Extras": "2.0.54"
}
}