naudio / NAudio

Audio and MIDI library for .NET

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

OffsetSampleProvider SkipOver and Take take long to complete for a large audio file

MohamedNoordin opened this issue · comments

Hello!
I use the OffsetSampleProvider class to cut a portion of a large audio file as following:
audioFile = new AudioFileReader(uri.AbsoluteUri);
trimmed = new OffsetSampleProvider(audioFile);
trimmed.SkipOver = start;
trimmed.Take = end - start;
Where start and end are time spans. However, when working with long audio files (over 2 hours), this piece of code takes long to cut a portion of the audio file, specifically, when the start and the end time spans are far away from each other. I could "see" the audio slider bar moves forward slowly. Anyway, I don't use the OffsetSampleProvider class to trim or to cut a portion of an audio file, but rather to set start and end points for a large audio file. I thought of using the WaveStream.Skip() and the WaveStream.SetLength() methods to skip and set a limit, but I need to create a new derived WaveStream class to override the method, but I'm a beginner, and don't know where to start. I thought also that the problem is with the audio player timers and dispatchers that update the view, so I got the tick time down to 10ms, but nothing has happened.
Appreciated!

OffsetSampleProvider is an inefficient way of jumping forwards because it has to read through. AudioFileReader already has a Position property which will instantly jump to the desired position, so I recommend just using that. The "take" portion will perform fine.

So you could try something like

    audioFile = new AudioFileReader(uri.AbsoluteUri);
    audioFile.CurrentTime = start;    
    trimmed = new OffsetSampleProvider(audioFile);
    trimmed.Take = end - start;