本文整理汇总了C#中IWaveSource.Read方法的典型用法代码示例。如果您正苦于以下问题:C# IWaveSource.Read方法的具体用法?C# IWaveSource.Read怎么用?C# IWaveSource.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IWaveSource
的用法示例。
在下文中一共展示了IWaveSource.Read方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CaptureMicToWave
/// <summary>
/// Record sound made in Mic and save it to a wave file
/// </summary>
/// <param name="wavefile">name of the wave file with extension</param>
public void CaptureMicToWave(string wavefile)
{
int i = 0;
string extension = ".wav";
foreach (var device in WaveIn.Devices)
{
_waveIn = new WaveInEvent(new WaveFormat(44100, 16, device.Channels));
_waveIn.Device = i++;
_waveIn.Initialize();
_waveIn.Start();
var waveInToSource = new SoundInSource(_waveIn);
_source = waveInToSource;
var notifyStream = new SingleBlockNotificationStream(_source);
_source = notifyStream.ToWaveSource(16);
_writerBuffer = new byte[_source.WaveFormat.BytesPerSecond];
wavefile = string.Format("{0}{1}{2}", wavefile.Remove(wavefile.LastIndexOf(extension) - (i > 1 ? 1 : 0)), i, extension);
_writer = new WaveWriter(wavefile, _source.WaveFormat);
waveInToSource.DataAvailable += (s, e) =>
{
int read = 0;
while ((read = _source.Read(_writerBuffer, 0, _writerBuffer.Length)) > 0)
{
_writer.Write(_writerBuffer, 0, read);
}
};
}
}
示例2: WriteToFile
public static void WriteToFile(string filename, IWaveSource source, bool deleteIfExists, int maxlength = -1)
{
if (deleteIfExists && File.Exists(filename))
File.Delete(filename);
int read = 0;
int r = 0;
byte[] buffer = new byte[source.WaveFormat.BytesPerSecond];
using (var w = new WaveWriter(filename, source.WaveFormat))
{
while ((read = source.Read(buffer, 0, buffer.Length)) > 0)
{
w.Write(buffer, 0, read);
r += read;
if (maxlength != -1 && r > maxlength)
break;
}
}
}
示例3: StartCapture
private void StartCapture(string fileName)
{
if (SelectedDevice == null)
return;
if(CaptureMode == CaptureMode.Capture)
_soundIn = new WasapiCapture();
else
_soundIn = new WasapiLoopbackCapture();
_soundIn.Device = SelectedDevice;
_soundIn.Initialize();
var soundInSource = new SoundInSource(_soundIn);
var singleBlockNotificationStream = new SingleBlockNotificationStream(soundInSource.ToSampleSource());
_finalSource = singleBlockNotificationStream.ToWaveSource();
_writer = new WaveWriter(fileName, _finalSource.WaveFormat);
byte[] buffer = new byte[_finalSource.WaveFormat.BytesPerSecond / 2];
soundInSource.DataAvailable += (s, e) =>
{
int read;
while((read = _finalSource.Read(buffer, 0, buffer.Length)) > 0)
_writer.Write(buffer, 0, read);
};
singleBlockNotificationStream.SingleBlockRead += SingleBlockNotificationStreamOnSingleBlockRead;
_soundIn.Start();
}
示例4: EncodeWholeSource
public static void EncodeWholeSource(MediaFoundationEncoder encoder, IWaveSource source)
{
byte[] buffer = new byte[source.WaveFormat.BytesPerSecond * 4];
int read = 0;
while ((read = source.Read(buffer, 0, buffer.Length)) > 0)
{
Debug.WriteLine(String.Format("{0:#00.00}%", (double)source.Position / (double)source.Length * 100));
encoder.Write(buffer, 0, read);
}
}
示例5: CacheSource
private void CacheSource(IWaveSource source)
{
_cache = new MemoryStream {Position = 0};
int read = 0;
int count = (int)Math.Min(source.WaveFormat.BytesPerSecond * 5, source.Length);
byte[] buffer = new byte[count];
long position = 0;
if(source.CanSeek)
position = source.Position;
while((read = source.Read(buffer, 0, count)) > 0)
{
_cache.Write(buffer, 0, read);
}
if (source.CanSeek)
{
source.Position = position;
_cache.Position = source.Position;
}
else
{
_cache.Position = 0;
}
}
示例6: EncodeWholeSource
/// <summary>
/// Encodes the whole <paramref name="source" /> with the specified <paramref name="encoder" />. The encoding process
/// stops as soon as the <see cref="IReadableAudioSource{T}.Read" /> method of the specified <paramref name="source" />
/// returns 0.
/// </summary>
/// <param name="encoder">The encoder which should be used to encode the audio data.</param>
/// <param name="source">The <see cref="IWaveSource" /> which provides the raw audio data to encode.</param>
public static void EncodeWholeSource(MediaFoundationEncoder encoder, IWaveSource source)
{
if (encoder == null)
throw new ArgumentNullException("encoder");
if (source == null)
throw new ArgumentNullException("source");
var buffer = new byte[source.WaveFormat.BytesPerSecond * 4];
int read;
while ((read = source.Read(buffer, 0, buffer.Length)) > 0)
{
Debug.WriteLine(String.Format("{0:#00.00}%", source.Position / (double) source.Length * 100));
encoder.Write(buffer, 0, read);
}
}