This project has moved and is read-only. For the latest updates, please go here.

WaveStream CurrentTime updates too slowly

Jan 2, 2014 at 3:39 AM
Hi,

I need to retrieve the playback position of the audio every 25ms to render an audio position animation smoothly, but WaveStream.CurrentTime only updates every 100ms or so.

Is there a way to improve the position latency?


// Slow
dPos = ((double)_waveStream.Position / (double)_waveStream.Length) * _waveStream.TotalTime.TotalSeconds;

// Also slow
dPos = _waveStream.CurrentTime.TotalSeconds;
Jan 3, 2014 at 4:32 PM
See if your output device has a GetPosition() method. This tells you how many bytes have been played, so you need to make sure you interpret it correctly if you have repositioned during playback.
Jan 4, 2014 at 4:12 AM
Edited Jan 4, 2014 at 5:08 AM
Ok I'll give that a try. At the moment I'm using some StopWatch and Timer trickery to guess where the high resolution position would be which works quite well.


update:

WaveOut.GetPosition seems to be missing in the current NuGet 1.7 package.

None of these objects has a .GetPosition()
    private IWavePlayer   _wavePlayer;   
    private SampleChannel _waveChannel;
    private WaveStream    _waveStream; 

            _wavePlayer = new WaveOut();
        _waveStream = new AudioFileReader(sAudioFile);
        _waveChannel =  new SampleChannel(_waveStream, true);
        _wavePlayer.Init(_waveChannel);
Jan 4, 2014 at 4:58 AM
Edited Jan 4, 2014 at 5:01 AM
FYI I'm trying to compile the latest source from the Downloads page but it seems MixerSource.cs and/or MixerInterop.cs is outdated.

'NAudio.Mixer.MixerInterop' does not contain a definition for 'MIXER_GETLINEINFOF_SOURCE' (CS0117) - NAudio\Mixer\MixerSource.cs:30,100
No overload for method 'GetMixerControl' takes 4 arguments (CS1501) - NAudio\Mixer\MixerSource.cs:80,20
'NAudio.Mixer.MixerInterop' does not contain a definition for 'MIXERLINE_COMPONENTTYPE' (CS0117) - NAudio\Mixer\MixerSource.cs:103,39
'NAudio.Mixer.MixerInterop' does not contain a definition for 'MIXERLINE_COMPONENTTYPE' (CS0117) - NAudio\Mixer\MixerSource.cs:105,39
Jan 10, 2014 at 1:54 PM
are you sure you got the latest code? I have no problem building.
Jan 10, 2014 at 3:21 PM
Edited Jan 12, 2014 at 1:01 AM
I tried the source from both the Downloads section and Source Code section and got the same error. Perhaps it's my setup, I'm using SharpDevelop 4.3 in Win8.1 with .NET 4.0 (WPF C#).

Anyway it is good now, I discovered the Latency variable which can only be accessed this way:
            _wavePlayer = new WaveOutEvent()
            {
                DesiredLatency = 100
            };  
Why I cant access DesiredLatency via a direct _wavePlayer.DesiredLatency call is confusing but anyway changing it to 100ms improved accuracy. I still need to use a bit of DispatcherTimer+Stopwatch witchcraft to get it down to 25ms, but it's all good. UI and animation as now smooth.