本文整理汇总了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);
}
}
}
}
示例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();
}
}
}
示例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);
}
}
示例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));
}
}
}
示例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;
}
示例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
}
示例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);
}
}
}
示例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();
}
}
}
示例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;
}
示例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);
}
}
}
}
示例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;
}
示例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;
}
示例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);
}
}
}
示例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;
}
示例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);
}
}