本文整理汇总了C#中NAudio.Wave.WaveFileWriter.WriteSample方法的典型用法代码示例。如果您正苦于以下问题:C# WaveFileWriter.WriteSample方法的具体用法?C# WaveFileWriter.WriteSample怎么用?C# WaveFileWriter.WriteSample使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NAudio.Wave.WaveFileWriter
的用法示例。
在下文中一共展示了WaveFileWriter.WriteSample方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CombineStereoToQuad
/// <summary>
/// Combine two stereo files to one quad file
/// </summary>
/// <param name="filePathLeft">file path to the left stereo file</param>
/// <param name="filePathRight">file path to the right stereo file</param>
/// <param name="combinedFileNamePath">file path to the combined quad file</param>
/// <returns></returns>
public static bool CombineStereoToQuad(string filePathLeft, string filePathRight, string combinedFileNamePath)
{
WaveFormat waveFormatLeft = GetWaveFormat(filePathLeft);
WaveFormat waveFormatRight = GetWaveFormat(filePathRight);
if (!waveFormatLeft.Equals(waveFormatRight)) {
Console.Out.WriteLine("The two files to combine must have the same format");
return false;
}
if (waveFormatLeft.Channels != 2 || waveFormatRight.Channels != 2) {
Console.Out.WriteLine("The two files to combine must be stereo");
return false;
}
int sampleRate = waveFormatLeft.SampleRate;
float[] channel1;
float[] channel2;
float[] channel3;
float[] channel4;
SplitStereoWaveFileToMono(filePathLeft, out channel1, out channel2);
SplitStereoWaveFileToMono(filePathRight, out channel3, out channel4);
// find out what channel is longest
int maxLength = Math.Max(channel1.Length, channel3.Length);
using (WaveFileWriter wavWriter = new WaveFileWriter(combinedFileNamePath, WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, 4)))
{
// write one and one float (interlaced), pad if neccesary
for (int i = 0; i < maxLength; i++) {
if (i < channel1.Length) {
wavWriter.WriteSample(channel1[i]);
} else {
wavWriter.WriteSample(0.0f);
}
if (i < channel2.Length) {
wavWriter.WriteSample(channel2[i]);
} else {
wavWriter.WriteSample(0.0f);
}
if (i < channel3.Length) {
wavWriter.WriteSample(channel3[i]);
} else {
wavWriter.WriteSample(0.0f);
}
if (i < channel4.Length) {
wavWriter.WriteSample(channel4[i]);
} else {
wavWriter.WriteSample(0.0f);
}
}
}
return true;
}
示例2: CanAccessSamplesIndividuallyInAMonoFile
public void CanAccessSamplesIndividuallyInAMonoFile()
{
var ms = new MemoryStream();
using (var writer = new WaveFileWriter( new IgnoreDisposeStream(ms), new WaveFormat(8000, 16, 1)))
{
writer.WriteSample(0.1f);
writer.WriteSample(0.2f);
writer.WriteSample(0.3f);
writer.WriteSample(0.4f);
}
ms.Position = 0;
using (var reader = new WaveFileReader(ms))
{
Assert.AreEqual(0.1f, reader.ReadNextSampleFrame()[0], 0.001f);
Assert.AreEqual(0.2f, reader.ReadNextSampleFrame()[0], 0.001f);
Assert.AreEqual(0.3f, reader.ReadNextSampleFrame()[0], 0.001f);
Assert.AreEqual(0.4f, reader.ReadNextSampleFrame()[0], 0.001f);
Assert.IsNull(reader.ReadNextSampleFrame());
}
}
示例3: CanUseWriteSampleToA16BitFile
public void CanUseWriteSampleToA16BitFile()
{
float amplitude = 0.25f;
float frequency = 1000;
using (WaveFileWriter writer = new WaveFileWriter(new MemoryStream(), new WaveFormat(16000, 16, 1)))
{
for (int n = 0; n < 1000; n++)
{
float sample = (float)(amplitude * Math.Sin((2 * Math.PI * n * frequency) / writer.WaveFormat.SampleRate));
writer.WriteSample(sample);
}
}
}
示例4: CanAccessSamplesIndividuallyInAStereoFile
public void CanAccessSamplesIndividuallyInAStereoFile()
{
var ms = new MemoryStream();
using (var writer = new WaveFileWriter(new IgnoreDisposeStream(ms), new WaveFormat(8000, 16, 2)))
{
writer.WriteSample(0.1f);
writer.WriteSample(0.2f);
writer.WriteSample(0.3f);
writer.WriteSample(0.4f);
}
ms.Position = 0;
using (var reader = new WaveFileForwardOnlyReader(new ForwardOnlyStream(ms)))
{
var f1 = reader.ReadNextSampleFrame();
Assert.AreEqual(0.1f, f1[0], 0.0001f);
Assert.AreEqual(0.2f, f1[1], 0.0001f);
var f2 = reader.ReadNextSampleFrame();
Assert.AreEqual(0.3f, f2[0], 0.0001f);
Assert.AreEqual(0.4f, f2[1], 0.0001f);
Assert.IsNull(reader.ReadNextSampleFrame());
}
}
示例5: WavPlay
// Сгенерируем Сэмпл определённой длительности, амплитуды и частоты и запишем в звуковой файл
private void WavPlay(WaveFileWriter writer, int millisec, float amplitude, float frequency)
{
for (int n = 0; n < millisec; n++)
{
float sample = (float)(amplitude * Math.Sin((Math.PI * n * frequency) / writer.WaveFormat.SampleRate));
writer.WriteSample(sample);
}
return;
}