Skip to content

Commit

Permalink
Merge pull request #584 from goddogthedoggod/fix-AdjustedPosition
Browse files Browse the repository at this point in the history
Fixed 3 issues around AudioClockClient.AdjustedPosition
  • Loading branch information
markheath authored Jan 31, 2020
2 parents 58aad8b + 12c2d9a commit f3c2a15
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
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

0 comments on commit f3c2a15

Please sign in to comment.