Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Beep or Silence for a given duration in an Audio file || NAudio || C#

Tags:

c#

naudio

I am using NAudio to record and play the audio file. Now, I want to add silence or beep in an audio file for a given duration. Could you please suggest the NAudio provider name in order to achieve it?

like image 970
Arindam Rudra Avatar asked Mar 07 '23 03:03

Arindam Rudra


1 Answers

To generate a beep, take a look at SignalGenerator, which can generate a sine wave. There is a Take extension method to limit the duration of the beep. For silence, you can use the same technique, except with SilenceProvider.

Here's a simple example of two beeps with silence between them:

var beep1 = (new SignalGenerator(){ Frequency = 1000, Gain = 0.2}).Take(TimeSpan.FromSeconds(2));
var silence = new SilenceProvider(beep1.WaveFormat).ToSampleProvider().Take(TimeSpan.FromSeconds(2));
var beep2 = (new SignalGenerator() { Frequency = 1500, Gain = 0.2 }).Take(TimeSpan.FromSeconds(2));
var concat = beep1.FollowedBy(silence).FollowedBy(beep2);
using (var wo = new WaveOutEvent())
{
    wo.Init(concat);
    wo.Play();
    while(wo.PlaybackState == PlaybackState.Playing)
    {
        Thread.Sleep(500);
    }
}
like image 66
Mark Heath Avatar answered Mar 08 '23 16:03

Mark Heath