How to detect silence and cut mp3 file without re-encoding using NAudio and .NET
Asked Answered
G

3

7

I've been looking for an answer everywhere and I was only able to find some bits and pieces. What I want to do is to load multiple mp3 files (kind of temporarily merge them) and then cut them into pieces using silence detection.

My understanding is that I can use Mp3FileReader for this but the questions are: 1. How do I read say 20 seconds of audio from an mp3 file? Do I need to read 20 times reader.WaveFormat.AverageBytesPerSecond? Or maybe keep on reading frames until the sum of Mp3Frame.SampleCount / Mp3Frame.SampleRate exceeds 20 seconds? 2. How do I actually detect the silence? I would look at an appropriate number of the consecutive samples to check if they are all below some threshold. But how do I access the samples regardless of them being 8 or 16bit, mono or stereo etc.? Can I directly decode an MP3 frame? 3. After I have detected silence at say sample 10465, how do I map it back to the mp3 frame index to perform the cutting without re-encoding?

Grosswardein answered 16/1, 2014 at 9:4 Comment(0)
B
2

BEFORE READING BELOW: Mark's answer is far easier to implement, and you'll almost certainly be happy with the results. This answer is for those who are willing to spend an inordinate amount of time on it.

So with that said, cutting an MP3 file based on silence without re-encoding or full decoding is actually possible... Basically, you can look at each frame's side info and each granule's gain & huffman data to "estimate" the silence.

  • Find the silence
  • Copy all the frames from before the silence to a new file

now it gets tricky...

  • Pull the audio data from the frames after the silence, keeping track of which frame header goes with what audio data.
  • Start writing the second new file, but as you write out the frames, update the main_data_begin field so the bit reservoir is in sync with where the audio data really is.
Burette answered 16/1, 2014 at 22:34 Comment(0)
O
3

Here's the approach I'd recommend (which does involve re-encoding)

  1. Use AudioFileReader to get your MP3 as floating point samples directly in the Read method
  2. Find an open source noise gate algorithm, port it to C#, and use that to detect silence (i.e. when noise gate is closed, you have silence. You'll want to tweak threshold and attack/release times)
  3. Create a derived ISampleProvider that uses the noise gate, and in its Read method, does not return samples that are in silence
  4. Either: Pass the output into WaveFileWriter to create a WAV File and and encode the WAV file to MP3 Or: use NAudio.Lame to encode directly without a WAV step. You'll probably need to go from SampleProvider back down to 16 bit WAV provider first
Oviposit answered 16/1, 2014 at 15:38 Comment(1)
Here's a basic implementation of how to detect silence duration using NAudio, which can very easily be used to truncate the silence from the file.Matey
B
2

BEFORE READING BELOW: Mark's answer is far easier to implement, and you'll almost certainly be happy with the results. This answer is for those who are willing to spend an inordinate amount of time on it.

So with that said, cutting an MP3 file based on silence without re-encoding or full decoding is actually possible... Basically, you can look at each frame's side info and each granule's gain & huffman data to "estimate" the silence.

  • Find the silence
  • Copy all the frames from before the silence to a new file

now it gets tricky...

  • Pull the audio data from the frames after the silence, keeping track of which frame header goes with what audio data.
  • Start writing the second new file, but as you write out the frames, update the main_data_begin field so the bit reservoir is in sync with where the audio data really is.
Burette answered 16/1, 2014 at 22:34 Comment(0)
V
1

MP3 is a compressed audio format. You can't just cut bits out and expect the remainder to still be a valid MP3 file. In fact, since it's a DCT-based transform, the bits are in the frequency domain instead of the time domain. There simply are no bits for sample 10465. There's a frame which contains sample 10465, and there's a set of bits describing all frequencies in that frame.

Plain cutting the audio at sample 10465 and continuing with some random other sample probably causes a discontinuity, which means the number of frequencies present in the resulting frame skyrockets. So that definitely means a full recode. The better way is to smooth the transition, but that's not a trivial operation. And the result is of course slightly different than the input, so it still means a recode.

  1. I don't understand why you'd want to read 20 seconds of audio anyway. Where's that number coming from? You usually want to read everything.

  2. Sound is a wave; it's entirely expected that it crosses zero. So being close to zero isn't special. For a 20 Hz wave (threshold of hearing), zero crossings happen 40 times per second, but each time you'll have multiple samples near zero. So you basically need multiple samples that are all close to zero, but on both sides. 5 6 7 isn't much for 16 bits sounds, but it might very well be part of a wave that will have a maximum at 10000. You really should check for at least 0.05 seconds to catch those 20 Hz sounds.

  3. Since you detected silence in a 50 millisecond interval, you have a "position" that's approximately several hundred samples wide. With any bit of luck, there's a frame boundary in there. Cut there. Else it's time for reencoding.

Virtu answered 16/1, 2014 at 10:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.