Skip to content

Commit

Permalink
Corrected calculation of AdjustedPosition by removing a stray divisio…
Browse files Browse the repository at this point in the history
…n by 100.

Added conversion of result of AdjustedPosition from device position units into bytes to WasapiOut.GetPosition.
  • Loading branch information
goddogthedoggod committed Jan 28, 2020
1 parent 4362fa3 commit c3327b9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 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
3 changes: 2 additions & 1 deletion NAudio/Wave/WaveOutputs/WasapiOut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <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"
}
}

0 comments on commit c3327b9

Please sign in to comment.