本文整理汇总了C#中NAudio.Wave.BufferedWaveProvider.ClearBuffer方法的典型用法代码示例。如果您正苦于以下问题:C# BufferedWaveProvider.ClearBuffer方法的具体用法?C# BufferedWaveProvider.ClearBuffer怎么用?C# BufferedWaveProvider.ClearBuffer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NAudio.Wave.BufferedWaveProvider
的用法示例。
在下文中一共展示了BufferedWaveProvider.ClearBuffer方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FfmpegListener
//.........这里部分代码省略.........
return;
}
RecordingFormat = new WaveFormat(_afr.SampleRate, 16, _afr.Channels);
_waveProvider = new BufferedWaveProvider(RecordingFormat) { DiscardOnBufferOverflow = true, BufferDuration = TimeSpan.FromMilliseconds(500) };
_sampleChannel = new SampleChannel(_waveProvider);
_sampleChannel.PreVolumeMeter += SampleChannelPreVolumeMeter;
int mult = _afr.BitsPerSample / 8;
double btrg = Convert.ToDouble(_afr.SampleRate * mult * _afr.Channels);
LastFrame = DateTime.UtcNow;
bool realTime = !IsFileSource;
try
{
DateTime req = DateTime.UtcNow;
while (!_stopEvent.WaitOne(10, false) && !MainForm.ShuttingDown)
{
byte[] data = _afr.ReadAudioFrame();
if (data == null || data.Equals(0))
{
if (!realTime)
{
break;
}
}
if (data != null && data.Length > 0)
{
LastFrame = DateTime.UtcNow;
var da = DataAvailable;
if (da != null)
{
//forces processing of volume level without piping it out
_waveProvider.AddSamples(data, 0, data.Length);
var sampleBuffer = new float[data.Length];
_sampleChannel.Read(sampleBuffer, 0, data.Length);
da(this, new DataAvailableEventArgs((byte[])data.Clone()));
if (WaveOutProvider != null && Listening)
{
WaveOutProvider.AddSamples(data, 0, data.Length);
}
}
if (realTime)
{
if (_stopEvent.WaitOne(30, false))
break;
}
else
{
//
double f = (data.Length / btrg) * 1000;
if (f > 0)
{
var span = DateTime.UtcNow.Subtract(req);
var msec = Convert.ToInt32(f - (int)span.TotalMilliseconds);
if ((msec > 0) && (_stopEvent.WaitOne(msec, false)))
break;
req = DateTime.UtcNow;
}
}
}
else
{
if ((DateTime.UtcNow - LastFrame).TotalMilliseconds > Timeout)
{
throw new Exception("Audio source timeout");
}
if (_stopEvent.WaitOne(30, false))
break;
}
}
}
catch (Exception e)
{
MainForm.LogExceptionToFile(e, "FFMPEG");
errmsg = e.Message;
}
if (_sampleChannel != null)
{
_sampleChannel.PreVolumeMeter -= SampleChannelPreVolumeMeter;
_sampleChannel = null;
}
if (_waveProvider != null)
{
if (_waveProvider.BufferedBytes > 0)
_waveProvider.ClearBuffer();
}
ShutDown(errmsg);
}
示例2: CanClearBeforeWritingSamples
public void CanClearBeforeWritingSamples()
{
var bwp = new BufferedWaveProvider(new WaveFormat(44100, 16, 2));
bwp.ClearBuffer();
Assert.AreEqual(0, bwp.BufferedBytes);
}
示例3: FfmpegListener
//.........这里部分代码省略.........
_afr.Timeout = Timeout;
_afr.AnalyzeDuration = AnalyseDuration;
_afr.Headers = Headers;
_afr.Cookies = Cookies;
_afr.UserAgent = UserAgent;
_afr.Open(_source);
open = true;
}
catch (Exception ex)
{
Logger.LogExceptionToFile(ex,"FFMPEG");
}
finally
{
try
{
Program.FfmpegMutex.ReleaseMutex();
}
catch (ObjectDisposedException)
{
//can happen on shutdown
}
}
if (_afr == null || !_afr.IsOpen || !open)
{
ShutDown("Could not open audio stream" + ": " + _source);
return;
}
RecordingFormat = new WaveFormat(_afr.SampleRate, 16, _afr.Channels);
_waveProvider = new BufferedWaveProvider(RecordingFormat) { DiscardOnBufferOverflow = true, BufferDuration = TimeSpan.FromMilliseconds(500) };
_sampleChannel = new SampleChannel(_waveProvider);
_sampleChannel.PreVolumeMeter += SampleChannelPreVolumeMeter;
LastFrame = DateTime.UtcNow;
try
{
while (!_stopEvent.WaitOne(10, false) && !MainForm.ShuttingDown)
{
byte[] data = _afr.ReadAudioFrame();
if (data!=null && data.Length > 0)
{
LastFrame = DateTime.UtcNow;
var da = DataAvailable;
if (da != null)
{
//forces processing of volume level without piping it out
_waveProvider.AddSamples(data, 0, data.Length);
var sampleBuffer = new float[data.Length];
int read = _sampleChannel.Read(sampleBuffer, 0, data.Length);
da(this, new DataAvailableEventArgs((byte[])data.Clone(),read));
if (Listening)
{
WaveOutProvider?.AddSamples(data, 0, read);
}
}
if (_stopEvent.WaitOne(30, false))
break;
}
else
{
if ((DateTime.UtcNow - LastFrame).TotalMilliseconds > Timeout)
{
throw new Exception("Audio source timeout");
}
if (_stopEvent.WaitOne(30, false))
break;
}
}
}
catch (Exception e)
{
Logger.LogExceptionToFile(e,"FFMPEG");
errmsg = e.Message;
}
if (_sampleChannel != null)
{
_sampleChannel.PreVolumeMeter -= SampleChannelPreVolumeMeter;
_sampleChannel = null;
}
if (_waveProvider?.BufferedBytes > 0)
_waveProvider.ClearBuffer();
if (WaveOutProvider?.BufferedBytes > 0) WaveOutProvider?.ClearBuffer();
ShutDown(errmsg);
}