filoe / cscore

An advanced audio library, written in C#. Provides tons of features. From playing/recording audio to decoding/encoding audio streams/files to processing audio data in realtime (e.g. applying custom effects during playback, create visualizations,...). The possibilities are nearly unlimited.
Other
2.17k stars 450 forks source link

WMADecoder Position #81

Closed dwebb7 closed 8 years ago

dwebb7 commented 8 years ago

See the code below. When I do this same thing with a WaveFileReader or Mp3MediafoundationDecoder it reports the position I would expect but the WmaDecoder reports a position around a second before the position I set. What causes this? Thanks for your help.

public partial class MainWindow : Window
{
    private ISampleSource playSampleSource;
    private bool click;
    public MainWindow()
    {
        InitializeComponent();
        var waveFileReader = new WmaDecoder(@"C:\Users\dwebb\Desktop\testwave.wma");

        //Setup Timer
        var dispatcherTimer = new DispatcherTimer {Interval = new TimeSpan(0, 0, 0, 0, 100)};
        dispatcherTimer.Tick += dispatcherTimer_Tick;
        dispatcherTimer.Start();

        playSampleSource = waveFileReader.ToSampleSource();
        var waveSource = playSampleSource.ToWaveSource();

        var playerOut = new WasapiOut();
        playerOut.Initialize(waveSource);
        playerOut.Play();
    }

    void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        if (click)
        {
            MessageBox.Show(playSampleSource.Position.ToString());
            click = false;
        }
    }

    private void TestButton_Click(object sender, RoutedEventArgs e)
    {
        playSampleSource.Position = 300000;
        click = true;
    }
}
filoe commented 8 years ago

Could you please provide the wma file. In wma files are getting decoded by microsoft's wma decoder. I've just tested it with a wma file and works as expected. But let me take a look at your file. Thanks.

dwebb7 commented 8 years ago

Yes, here you go. This was recorded and encoded using MediaToundationEncoder in Cscore. I also ripped a song from a CD using windows media player and I get the same result. Thanks for your help. Dan

https://dl.dropboxusercontent.com/u/16609856/TestMyRecorder.wma

filoe commented 8 years ago

I've made an attempt to fix it. Please test it and give me some feedback. The problem is that media foundation seeks to the next nearest keyframe. Now I am just skipping the delta between desired position and keyframe. Hopefully it helps.

dwebb7 commented 8 years ago

Thank You! That did the trick.