當前位置: 首頁>>代碼示例>>C#>>正文


C# WaveFileWriter.WriteSample方法代碼示例

本文整理匯總了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;
        }
開發者ID:remy22,項目名稱:AudioVSTToolbox,代碼行數:60,代碼來源:AudioUtilsNAudio.cs

示例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());
     }
 }
開發者ID:ActivePHOENiX,項目名稱:NAudio,代碼行數:20,代碼來源:WaveFileReaderTests.cs

示例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);
         }
     }
 }
開發者ID:gauravkar,項目名稱:NAudio,代碼行數:13,代碼來源:WaveFileWriterTests.cs

示例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());
            }
        }
開發者ID:AdamCLarsen,項目名稱:NAudio,代碼行數:23,代碼來源:WaveFileForwardOnlyReaderTests.cs

示例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;
 }
開發者ID:richmanfx,項目名稱:cw_win_4,代碼行數:10,代碼來源:Cw_winForm.cs


注:本文中的NAudio.Wave.WaveFileWriter.WriteSample方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。