naudio / NAudio

Audio and MIDI library for .NET
MIT License
5.38k stars 1.09k forks source link

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

Closed MohamedNoordin closed 11 months ago

MohamedNoordin commented 11 months ago

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!

markheath commented 11 months ago

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;
MohamedNoordin commented 11 months ago

I marvel at your activity and ingenuity. You are excellent, Mark. Thank you very much! Now, the audio player jumps directly to the specific point, and stops at the end point in an efficient and fast way. Best regards.