2

Closed

NVorbisIntegration sample returns incorrect position when seeking

description

I've started integrating vorbis support with cscore in a project. I noticed that the NVorbisIntegration sample returns incorrect position after seeking. This is caused by possible incorrect implementation of the Length() and Position() properties in the NVorbisSource class.
Having played with NAudio and NVorbis in the past, I took a look here: http://nvorbis.codeplex.com/SourceControl/latest#NAudioSupport/VorbisWaveReader.cs

After modifying the Length() and Position() properties in your NVorbisSource class to reflect those in the VorbisWaveReader.cs class above, I got a correct Position back after seeking.
Closed May 17, 2015 at 5:03 PM by filoe
Got fixed.

comments

filoe wrote Jan 8, 2015 at 3:14 PM

I'm sorry for not responding for such a long time. Could you provide the modification you've made?

rgodart wrote Jan 11, 2015 at 10:36 AM

Yes of course. I replaced your Length() and Position() functions by these:
public long Length
        {
            get { return (long)(_vorbisReader.TotalTime.TotalSeconds * _waveFormat.SampleRate * _waveFormat.Channels * sizeof(float)); }
        }

        public long Position
        {
            get
            {
                return (long)(_vorbisReader.DecodedTime.TotalSeconds * _vorbisReader.SampleRate * _vorbisReader.Channels * sizeof(float));
            }
            set
            {
                if (value < 0 || value > Length) throw new ArgumentOutOfRangeException("value");

                _vorbisReader.DecodedTime = TimeSpan.FromSeconds((double)value / _vorbisReader.SampleRate / _vorbisReader.Channels / sizeof(float));
            }
        }

rgodart wrote Jan 19, 2015 at 9:08 PM

Hi,

I've done more testing with the code sample above, and it seems it is still not correct. The reported length is 4 times too long with the vorbis files I tested with. It seems caused by the "sizeof(float)" part which is 4. So this is what I now use to calculate length and position:
public long Length
        {
            get { return (long)(_vorbisReader.TotalTime.TotalSeconds * _waveFormat.SampleRate * _waveFormat.Channels; }
        }

        public long Position
        {
            get
            {
                return (long)(_vorbisReader.DecodedTime.TotalSeconds * _vorbisReader.SampleRate * _vorbisReader.Channels;
            }
            set
            {
                if (value < 0 || value > Length) throw new ArgumentOutOfRangeException("value");

                _vorbisReader.DecodedTime = TimeSpan.FromSeconds((double)value / _vorbisReader.SampleRate / _vorbisReader.Channels;
            }
        }
However I must admit that I don't fully understand everything as I don't have experience with decoding audio. So I hope the above code is meaningful.

filoe wrote May 17, 2015 at 5:03 PM

Got fixed according to the provided code (thanks @rgodart).