Posted in:

Occasionally I get asked how to play an extract from an audio file with NAudio. There’s actually a class in NAudio that makes this very simple – OffsetSampleProvider.

OffsetSampleProvider allows you to skip over a specified duration from the start of the source audio (with a Skip property), and to only play a specified duration by using the Take property. So you could skip the first 30 seconds and then take then next 10 seconds for example. (OffsetSampleProvider can actually do a bit more than this – it can also insert silence at the start and end)

But just to show how simple it is to use, here’s a code snippet that takes a 10 second extract from an MP3 file, after skipping the first 15 seconds:

var file = new AudioFileReader("somefile.mp3");
var trimmed = new OffsetSampleProvider(file);
trimmed.SkipOver = TimeSpan.FromSeconds(15);
trimmed.Take = TimeSpan.FromSeconds(10);

var player = new WaveOutEvent();
player.Init(trimmed);
player.Play();

Of course you could equally use WaveFileWriter to write the extract to a WAV file if you wanted to save it for playback later.

Want to get up to speed with the the fundamentals principles of digital audio and how to got about writing audio applications with NAudio? Be sure to check out my Pluralsight courses, Digital Audio Fundamentals, and Audio Programming with NAudio.

Comments

Comment by po pofla

How do you do this if the input is a MemoryStream not a file. I've already reading the file in and have manipulated the contents. What is the solution for this situation.

po pofla
Comment by Mark Heath

use `RawSourceWaveStream` and convert it to an `ISampleProvider` with the `ToSampleProvider` extension

Mark Heath
Comment by rcirrus98

I had a hard drive failure and subsequently opted for data recovery services. The hard drive I received back from the data recovery process has a lot of corrupted mp3 files. I've tried to repair them using MP3RepairTool to no avail. Before I give up and delete the files, I want to see if I can find any readable data within the file; even if it's just a few seconds at a random location within the file. I want to know if I'm deleting an audio file of my own creation or a copy of music I've purchased. Is this possible with NAudio or any other API that you are aware of? It sounds like NAudio will do this with a good file but I'm working with corrupt files so I am not sure if it's worth the time. Thanks.

rcirrus98
Comment by cmw Erie

This is great if you're not skipping too far ahead. I have some audio books which can be 6+ hours in duration. If I want to skip to say Chapter 8 (which could be well over an hour+ into the book), it takes over 2+ minutes for the OffsetSampleProvider to reach this point.

cmw Erie
Comment by Mark Heath

Yes, a good point. If you're using an AudioFileReader/Mp3FileReader etc, then you can always just set the Position directly to skip to a certain position.

Mark Heath
Comment by cmw Erie

Thanks for the tip! I was looking at this property in the AudioFileReader today. It looks like it accepts the byte position within the file. How would I quickly find this based on the time I want to start at?

cmw Erie
Comment by MusicEnthusiast:)

Hi Mark, without actually opening a video file would it be possible to extract the audio wave information(hertz and amplitude) with its timestamp of it and stored to a file?

MusicEnthusiast:)