当前位置: 首页>>代码示例>>C#>>正文


C# WaveFileReader.Read方法代码示例

本文整理汇总了C#中NAudio.Wave.WaveFileReader.Read方法的典型用法代码示例。如果您正苦于以下问题:C# WaveFileReader.Read方法的具体用法?C# WaveFileReader.Read怎么用?C# WaveFileReader.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NAudio.Wave.WaveFileReader的用法示例。


在下文中一共展示了WaveFileReader.Read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TrimWavFile

 private static void TrimWavFile(WaveFileReader reader, WaveFileWriter writer, int startPos, int endPos) {
     reader.Position = startPos;
     byte[] buffer = new byte[1024];
     while (reader.Position < endPos) {
         int bytesRequired = (int)(endPos - reader.Position);
         if (bytesRequired > 0) {
             int bytesToRead = Math.Min(bytesRequired, buffer.Length);
             int bytesRead = reader.Read(buffer, 0, bytesToRead);
             if (bytesRead > 0) {
                 writer.WriteData(buffer, 0, bytesRead);
             }
         }
     }
 }
开发者ID:bearhack145,项目名称:fluid-vocal,代码行数:14,代码来源:WavFileUtils.cs

示例2: ConcatenateWav

        /// <summary>
        /// Merge multiple .wav files together and save the output.
        /// </summary>
        /// <param name="outputFile">The path to save the output to.</param>
        /// <param name="sourceFiles">An IEnumerable list of files to merge.</param>
        private static void ConcatenateWav(string outputFile, IEnumerable<string> sourceFiles) {
            byte[] buffer = new byte[1024];
            WaveFileWriter waveFileWriter = null;
            
            try {
                foreach (string sourceFile in sourceFiles) {
                    using (WaveFileReader reader = new WaveFileReader(sourceFile)) {
                        if (waveFileWriter == null) {
                            // first time in create new Writer
                            waveFileWriter = new WaveFileWriter(outputFile, reader.WaveFormat);
                        }
                        else {
                            if (!reader.WaveFormat.Equals(waveFileWriter.WaveFormat)) {
                                throw new InvalidOperationException("Can't concatenate WAV Files that don't share the same format");
                            }
                        }

                        int read;
                        while ((read = reader.Read(buffer, 0, buffer.Length)) > 0) {
                            waveFileWriter.WriteData(buffer, 0, read);
                        }
                    }
                }
            }
            finally {
                if (waveFileWriter != null) {
                    waveFileWriter.Dispose();
                }
            }

        }
开发者ID:bearhack145,项目名称:fluid-vocal,代码行数:36,代码来源:WavMod.cs

示例3: ConvertWavTo10SecondWavs

 private void ConvertWavTo10SecondWavs(FileInfo inputFile)
 {
     var samplesOutWav = @"..\..\..\samples\wav10seconds\";
     using (var inAudio = new WaveFileReader(inputFile.FullName))
     {
         //Calculate required byte[] buffer.
         var buffer = new byte[10*inAudio.WaveFormat.AverageBytesPerSecond];//Assume average will be constant for WAV format.
         int index = 0;
         do
         {
             var outFile = string.Format("{0}{1}.{2:0000}.wav",
             samplesOutWav, inputFile.Name.Replace(inputFile.Extension, string.Empty), index);
             int bytesRead = 0;
             do
             {
                 bytesRead = inAudio.Read(buffer, 0, buffer.Length - bytesRead);
             } while (bytesRead > 0 && bytesRead < buffer.Length);
             //Write new file
             using (var waveWriter = new WaveFileWriter(outFile, inAudio.WaveFormat))
             {
                 waveWriter.Write(buffer, 0, buffer.Length);
             }
             index++;
         } while (inAudio.Position < inAudio.Length);
     }
 }
开发者ID:tmpreston,项目名称:BingVoiceTranscription,代码行数:26,代码来源:NAudioTasks.cs

示例4: CanLoadAndReadVariousProblemWavFiles

 public void CanLoadAndReadVariousProblemWavFiles()
 {
     string testDataFolder = @"C:\Users\Mark\Downloads\NAudio";
     if (!Directory.Exists(testDataFolder))
     {
         Assert.Ignore("{0} not found", testDataFolder);
     }
     foreach (string file in Directory.GetFiles(testDataFolder, "*.wav"))
     {
         string wavFile = Path.Combine(testDataFolder, file);
         Debug.WriteLine(String.Format("Opening {0}", wavFile));
         using (var reader = new WaveFileReader(wavFile))
         {
             byte[] buffer = new byte[reader.WaveFormat.AverageBytesPerSecond];
             int bytesRead;
             int total = 0;
             do
             {
                 bytesRead = reader.Read(buffer, 0, buffer.Length);
                 total += bytesRead;
             } while (bytesRead > 0);
             Debug.WriteLine(String.Format("Read {0} bytes", total));
         }
     }
 }
开发者ID:jithuin,项目名称:infogeezer,代码行数:25,代码来源:WaveFileReaderTests.cs

示例5: Read

        public static Sound Read(Stream source)
        {
            BinaryReaderEx reader = new BinaryReaderEx(source);
            Sound result = new Sound();

            int id = reader.ReadInt32();
            if (id == 0x00394453)
            {
                int headerLength = reader.ReadInt32();
                int sampleLength = reader.ReadInt32();

                headerLength -= 12;
                if (headerLength > 0)
                    reader.ReadBytes(headerLength);

                byte[] wavData = reader.ReadBytes(sampleLength);
                using (MemoryStream wavDataMem = new MemoryStream(wavData))
                {
                    using (WaveStream wavStream = new WaveFileReader(wavDataMem))
                    {
                        byte[] rawWaveData = new byte[wavStream.Length];
                        wavStream.Read(rawWaveData, 0, (int)wavStream.Length);
                        result.SetSound(rawWaveData, wavStream.WaveFormat);
                    }
                }
            }

            return result;
        }
开发者ID:NoOnes2,项目名称:scharfrichter,代码行数:29,代码来源:BemaniSD9.cs

示例6: FlushUpdatesHeaderEvenIfDisposeNotCalled

 public void FlushUpdatesHeaderEvenIfDisposeNotCalled()
 {
     var ms = new MemoryStream();
     var testSequence = new byte[] { 0x1, 0x2, 0xFF, 0xFE };
     var testSequence2 = new byte[] { 0x3, 0x4, 0x5 };
     var writer = new WaveFileWriter(new IgnoreDisposeStream(ms), new WaveFormat(16000, 24, 1));
     writer.Write(testSequence, 0, testSequence.Length);
     writer.Flush();
     // BUT NOT DISPOSED
     // another write that was not flushed
     writer.Write(testSequence2, 0, testSequence2.Length);
     
     // check the Reader can read it
     ms.Position = 0;
     using (var reader = new WaveFileReader(ms))
     {
         Assert.AreEqual(16000, reader.WaveFormat.SampleRate, "Sample Rate");
         Assert.AreEqual(24, reader.WaveFormat.BitsPerSample, "Bits Per Sample");
         Assert.AreEqual(1, reader.WaveFormat.Channels, "Channels");
         Assert.AreEqual(testSequence.Length, reader.Length, "File Length");
         var buffer = new byte[600]; // 24 bit audio, block align is 3
         int read = reader.Read(buffer, 0, buffer.Length);
         Assert.AreEqual(testSequence.Length, read, "Data Length");
         
         for (int n = 0; n < read; n++)
         {
             Assert.AreEqual(testSequence[n], buffer[n], "Byte " + n);
         }
     }
     writer.Dispose(); // to stop the finalizer from moaning
 }
开发者ID:ActivePHOENiX,项目名称:NAudio,代码行数:31,代码来源:WaveFileWriterTests.cs

示例7: ReaderShouldReadBackSameDataWrittenWithWrite

 public void ReaderShouldReadBackSameDataWrittenWithWrite()
 {
     var ms = new MemoryStream();
     var testSequence = new byte[] { 0x1, 0x2, 0xFF, 0xFE };
     using (var writer = new WaveFileWriter(new IgnoreDisposeStream(ms), new WaveFormat(16000, 24, 1)))
     {
         writer.Write(testSequence, 0, testSequence.Length);
     }
     // check the Reader can read it
     ms.Position = 0;
     using (var reader = new WaveFileReader(ms))
     {
         Assert.AreEqual(16000, reader.WaveFormat.SampleRate, "Sample Rate");
         Assert.AreEqual(24, reader.WaveFormat.BitsPerSample, "Bits Per Sample");
         Assert.AreEqual(1, reader.WaveFormat.Channels, "Channels");
         Assert.AreEqual(testSequence.Length, reader.Length, "File Length");
         var buffer = new byte[600]; // 24 bit audio, block align is 3
         int read = reader.Read(buffer, 0, buffer.Length);
         Assert.AreEqual(testSequence.Length, read, "Data Length");
         for (int n = 0; n < read; n++)
         {
             Assert.AreEqual(testSequence[n], buffer[n], "Byte " + n);
         }
     }
 }
开发者ID:ActivePHOENiX,项目名称:NAudio,代码行数:25,代码来源:WaveFileWriterTests.cs

示例8: Concatenate

        public static void Concatenate(String outputFile, IEnumerable<string> sourceFiles, int mseconds = 0, bool reverse = false)
        {
            byte[] buffer = new byte[1024];
            WaveFileWriter waveFileWriter = null;

            try
            {
                foreach (string sourceFile in sourceFiles)
                {
                    using (WaveFileReader reader = new WaveFileReader(sourceFile))
                    {
                        if (waveFileWriter == null)
                        {

                            waveFileWriter = new WaveFileWriter(outputFile, reader.WaveFormat);
                        }
                        else
                        {
                            if (!reader.WaveFormat.Equals(waveFileWriter.WaveFormat))
                            {
                                throw new InvalidOperationException("Can't concatenate WAV Files that don't share the same format");
                            }
                        }

                        if (mseconds > 0)
                        {

                            int avgBytesPerMillisecond = reader.WaveFormat.AverageBytesPerSecond / 1000;
                            var silenceArraySize = avgBytesPerMillisecond * mseconds;
                            byte[] silenceArray = new byte[silenceArraySize];
                            waveFileWriter.Write(silenceArray, 0, silenceArray.Length);
                        }

                        int read;
                        while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            waveFileWriter.Write(buffer, 0, read);
                        }

                    }

                }
            }
            finally
            {
                if (waveFileWriter != null)
                {
                    waveFileWriter.Dispose();
                }
            }
        }
开发者ID:lucasteles,项目名称:CaptchaComAudio,代码行数:51,代码来源:AudioTool.cs

示例9: Read

        public static Sound Read(Stream source)
        {
            Sound result = new Sound();
            BinaryReader reader = new BinaryReader(source);
            if (new string(reader.ReadChars(4)) == "2DX9")
            {
                int infoLength = reader.ReadInt32();
                int dataLength = reader.ReadInt32();
                reader.ReadInt16();
                int channel = reader.ReadInt16();
                int panning = reader.ReadInt16();
                int volume = reader.ReadInt16();
                int options = reader.ReadInt32();

                reader.ReadBytes(infoLength - 24);

                byte[] wavData = reader.ReadBytes(dataLength);
                using (MemoryStream wavDataMem = new MemoryStream(wavData))
                {
                    using (WaveStream wavStream = new WaveFileReader(wavDataMem))
                    {
                        int bytesToRead;

                        // using a mux, we force all sounds to be 2 channels
                        bytesToRead = (int)wavStream.Length;

                        byte[] rawWaveData = new byte[bytesToRead];
                        int bytesRead = wavStream.Read(rawWaveData, 0, bytesToRead);
                        result.SetSound(rawWaveData, wavStream.WaveFormat);

                        // calculate output panning
                        if (panning > 0x7F || panning < 0x01)
                            panning = 0x40;
                        result.Panning = ((float)panning - 1.0f) / 126.0f;

                        // calculate output volume
                        if (volume < 0x01)
                            volume = 0x01;
                        else if (volume > 0xFF)
                            volume = 0xFF;
                        result.Volume = VolumeTable[volume];

                        result.Channel = channel;
                    }
                }
            }

            return result;
        }
开发者ID:NoOnes2,项目名称:scharfrichter,代码行数:49,代码来源:Bemani2DXSound.cs

示例10: TrimWavFile

 private static void TrimWavFile(WaveFileReader paramWavReader, WaveFileWriter paramWavWriter, int paramStartPosition, int paramEndPosition)
 {
     paramWavReader.Position = paramStartPosition;
     Byte[] byteBuffer = new Byte[1024];
     while (paramWavWriter.Length < paramEndPosition)
     {
         int bytesRequired = (int)(paramEndPosition - paramWavReader.Position);
         if (bytesRequired > 0)
         {
             int bytesToRead = Math.Min(bytesRequired, byteBuffer.Length);
             int bytesRead = paramWavReader.Read(byteBuffer, 0, bytesToRead);
             if (bytesRead > 0)
             {
                 paramWavWriter.Write(byteBuffer, 0, bytesRead);
             }
         }
     }
 }
开发者ID:EspyingStar,项目名称:AudioQ,代码行数:18,代码来源:FileEditor.cs

示例11: Read

 public static Sound Read(Stream source)
 {
     Sound result = new Sound();
     WaveFileReader reader = new WaveFileReader(source);
     if (reader.Length > 0)
     {
         result.Data = new byte[reader.Length];
         reader.Read(result.Data, 0, result.Data.Length);
         result.Format = reader.WaveFormat;
     }
     else
     {
         result.Data = new byte[] { };
         result.Format = WaveFormat.CreateCustomFormat(WaveFormatEncoding.Pcm, 44100, 2, 44100 * 4, 4, 16);
     }
     result.Panning = 0.5f;
     result.Volume = 1.0f;
     return result;
 }
开发者ID:NoOnes2,项目名称:scharfrichter,代码行数:19,代码来源:Sound.cs

示例12: Concatenate

        private static string Concatenate(IEnumerable<Stream> sourceFiles, string tempDirectory)
        {
            var buffer = new byte[1024];
            WaveFileWriter waveFileWriter = null;
            var filename = Path.Combine(tempDirectory, string.Format("{0}.wav", Guid.NewGuid()));

            try
            {
                foreach (var sourceFile in sourceFiles)
                {
                    using (var reader = new WaveFileReader(sourceFile))
                    {
                        if (waveFileWriter == null)
                        {
                            // first time in create new Writer
                            waveFileWriter = new WaveFileWriter(filename, reader.WaveFormat);
                        }
                        else
                        {
                            if (!reader.WaveFormat.Equals(waveFileWriter.WaveFormat))
                            {
                                throw new InvalidOperationException("Can't concatenate WAV Files that don't share the same format");
                            }
                        }

                        int read;
                        while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            waveFileWriter.Write(buffer, 0, read);
                        }
                    }
                }
            }
            finally
            {
                if (waveFileWriter != null)
                {
                    waveFileWriter.Dispose();
                }
            }

            return filename;
        }
开发者ID:arabsoheyl,项目名称:fardis,代码行数:43,代码来源:PersianNumberRead.cs

示例13: CutWavFile

        private void CutWavFile(WaveFileReader reader, WaveFileWriter writer, int startPos, int endPos, Action<long> progressCallback)
        {
            reader.Position = startPos;
            byte[] buffer = new byte[1024];
            while (reader.Position < endPos)
            {
                var bytesRequired = (int)(endPos - reader.Position);
                if (bytesRequired > 0)
                {
                    int bytesToRead = Math.Min(bytesRequired, buffer.Length);
                    int bytesRead = reader.Read(buffer, 0, bytesToRead);
                    if (bytesRead > 0)
                    {
                        writer.Write(buffer, 0, bytesRead);
                    }

                    progressCallback(bytesRead);
                }
            }
        }
开发者ID:olathunberg,项目名称:RecordToMP3,代码行数:20,代码来源:WaveFileCutter.cs

示例14: ConcatenateWavs

        /// <summary>
        /// from: http://stackoverflow.com/questions/6777340/how-to-join-2-or-more-wav-files-together-programatically
        /// modified: takes startIndex in sourceFiles array and returns how many it concatenated before the limit was exceeded
        /// </summary>
        private int ConcatenateWavs(string outputFile, List<string> sourceFiles, int startIndex)
        {
            byte[] buffer = new byte[1024];
            WaveFileWriter waveFileWriter = null;
            int howManyConcatenated = 0;
            long totalBytes = 0;

            try {
                for (int i = startIndex; i < sourceFiles.Count; i++) {
                    var sourceFile = sourceFiles[i];
                    using (WaveFileReader reader = new WaveFileReader(sourceFile)) {
                        if (waveFileWriter == null) {
                            // first time in create new Writer
                            waveFileWriter = new WaveFileWriter(outputFile, reader.WaveFormat);
                        }
                        else {
                            if (!reader.WaveFormat.Equals(waveFileWriter.WaveFormat)) {
                                throw new InvalidOperationException("Can't concatenate WAV Files that don't share the same format");
                            }
                        }

                        int read;
                        while ((read = reader.Read(buffer, 0, buffer.Length)) > 0) {
                            waveFileWriter.WriteData(buffer, 0, read);
                            totalBytes += read;
                        }
                        howManyConcatenated++;
                    }
                    if (totalBytes > config_maxBytesPerFile)
                        break;
                }
            }
            finally {
                if (waveFileWriter != null) {
                    waveFileWriter.Dispose();
                }
            }
            return howManyConcatenated;
        }
开发者ID:miktemk,项目名称:TextToSpeechInterlacer,代码行数:43,代码来源:TtsReadMultilang.cs

示例15: CreateWaveFileCreatesFileOfCorrectLength

 public void CreateWaveFileCreatesFileOfCorrectLength()
 {
     string tempFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".wav");
     try
     {
         long length = 4200;
         WaveFormat waveFormat = new WaveFormat(8000, 8, 2);
         WaveFileWriter.CreateWaveFile(tempFile, new NullWaveStream(waveFormat, length));
         using (WaveFileReader reader = new WaveFileReader(tempFile))
         {
             Assert.AreEqual(waveFormat, reader.WaveFormat, "WaveFormat");
             Assert.AreEqual(length, reader.Length, "Length");
             byte[] buffer = new byte[length + 20];
             int read = reader.Read(buffer, 0, buffer.Length);
             Assert.AreEqual(length, read, "Read");
         }
     }
     finally
     {
         File.Delete(tempFile);
     }
 }
开发者ID:gauravkar,项目名称:NAudio,代码行数:22,代码来源:WaveFileWriterTests.cs


注:本文中的NAudio.Wave.WaveFileReader.Read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。