/// <summary> /// Creates a Wave File Reader based on an input stream /// </summary> /// <param name="inputStream">The input stream containing a WAV file including header</param> public GrainWaveProvider(Stream inputStream) { this.waveStream = inputStream; var chunkReader = new WaveFileChunkReader(); chunkReader.ReadWaveHeader(inputStream); this.waveFormat = chunkReader.WaveFormat; this.dataPosition = chunkReader.DataChunkPosition; this.dataChunkLength = chunkReader.DataChunkLength; this.chunks = chunkReader.RiffChunks; waveStream.Position = dataPosition; var samples = (dataChunkLength / BlockAlign) * waveFormat.Channels; FSample = new float[samples]; for(int i=0; i<samples; i++) { TryReadFloat(out FSample[i]); } waveFormat = WaveFormat.CreateIeeeFloatWaveFormat(waveFormat.SampleRate, waveFormat.Channels); //grain FGrain = new Grain(); FGrain.SampleRate = waveFormat.SampleRate; FGrain.Start = 20000; FGrain.Length = 1024; FGrain.Freq = 440; FGrain.Index = 0; }
private WaveFileReader(Stream inputStream, bool ownInput) { this.waveStream = inputStream; var chunkReader = new WaveFileChunkReader(); try { chunkReader.ReadWaveHeader(inputStream); this.waveFormat = chunkReader.WaveFormat; this.dataPosition = chunkReader.DataChunkPosition; this.dataChunkLength = chunkReader.DataChunkLength; this.chunks = chunkReader.RiffChunks; } catch { if (ownInput) { inputStream.Dispose(); } throw; } Position = 0; this.ownInput = ownInput; }
/// <summary> /// Creates a Wave File Reader based on an input stream /// </summary> /// <param name="inputStream">The input stream containing a WAV file including header</param> public WaveFileReader(Stream inputStream) { this.waveStream = inputStream; var chunkReader = new WaveFileChunkReader(); chunkReader.ReadWaveHeader(inputStream); this.waveFormat = chunkReader.WaveFormat; this.dataPosition = chunkReader.DataChunkPosition; this.dataChunkLength = chunkReader.DataChunkLength; this.chunks = chunkReader.RiffChunks; Position = 0; }
public void TestEmptyFile() { // arrange var fileContents = new byte[] { 0x52, 0x49, 0x46, 0x46, // "RIFF" 0x26, 0x00, 0x00, 0x00, // ChunkSize = 38 0x57, 0x41, 0x56, 0x45, // "WAVE" 0x66, 0x6d, 0x74, 0x20, // "fmt " 0x12, 0x00, 0x00, 0x00, // Subchunk1Size = 18 0x07, 0x00, 0x02, 0x00, // AudioFormat = 7, NumChannels = 2 0x40, 0x1f, 0x00, 0x00, // SampleRate = 8000 0x80, 0x3e, 0x00, 0x00, // ByteRate = 16000 0x02, 0x00, 0x08, 0x00, // BlockAlign = 2, BitsPerSample = 8 0x00, 0x00, // ExtraParamSize = 0 0x64, 0x61, 0x74, 0x61, // "data" 0x00, 0x00, 0x00, 0x00, // Subchunk2Size = 0 }; using (var inputStream = new MemoryStream(fileContents)) { // act var chunks = new List<RiffChunk>(); var chunkReader = new WaveFileChunkReader(); chunkReader.ReadWaveHeader(inputStream); // assert Assert.AreEqual(16000, chunkReader.WaveFormat.AverageBytesPerSecond); Assert.AreEqual(8, chunkReader.WaveFormat.BitsPerSample); Assert.AreEqual(2, chunkReader.WaveFormat.Channels); Assert.AreEqual(8000, chunkReader.WaveFormat.SampleRate); Assert.AreEqual(46, chunkReader.DataChunkPosition); Assert.AreEqual(0, chunkReader.DataChunkLength); Assert.AreEqual(0, chunks.Count); } }
public long AnalyzePP(ppParser pp) { long total = 0; for (int i = 0; i < pp.Subfiles.Count; i++) { IWriteFile iw = pp.Subfiles[i]; if (!iw.Name.EndsWith(".wav")) continue; Stream str = Tools.GetReadStream(iw); if (!str.CanSeek || str.Length == 0) { str.Close(); continue; } using (str) { WaveFileChunkReader wv = new WaveFileChunkReader(); WaveFormat f; try { f = wv.ReadWaveHeader(str); } catch (FormatException) { str.Close(); continue; } long length = str.Length; long remaining = length; if (f.Channels > 1) // || wv.WaveFormat.Encoding != WaveFormatEncoding.Adpcm { total += (length / 2); remaining /= 2; } if (f.SampleRate > SampleRate) { total += remaining - (long)(((float)SampleRate / f.SampleRate) * remaining); } } if (ProgressUpdated != null) ProgressUpdated((int)Math.Floor((double)(100 * i) / pp.Subfiles.Count)); } return total; }
public void ReadWaveHeader(Stream stream) { this.dataChunkPosition = -1L; this.waveFormat = null; this.riffChunks = new List <RiffChunk>(); this.dataChunkLength = 0L; BinaryReader binaryReader = new BinaryReader(stream); this.ReadRiffHeader(binaryReader); this.riffSize = (long)((ulong)binaryReader.ReadUInt32()); if (binaryReader.ReadInt32() != ChunkIdentifier.ChunkIdentifierToInt32("WAVE")) { throw new FormatException("Not a WAVE file - no WAVE header"); } if (this.isRf64) { this.ReadDs64Chunk(binaryReader); } int num = ChunkIdentifier.ChunkIdentifierToInt32("data"); int num2 = ChunkIdentifier.ChunkIdentifierToInt32("fmt "); long num3 = Math.Min(this.riffSize + 8L, stream.Length); while (stream.Position <= num3 - 8L) { int num4 = binaryReader.ReadInt32(); uint num5 = binaryReader.ReadUInt32(); if (num4 == num) { this.dataChunkPosition = stream.Position; if (!this.isRf64) { this.dataChunkLength = (long)((ulong)num5); } stream.Position += (long)((ulong)num5); } else if (num4 == num2) { if (num5 > 2147483647u) { throw new InvalidDataException(string.Format("Format chunk length must be between 0 and {0}.", int.MaxValue)); } this.waveFormat = WaveFormat.FromFormatChunk(binaryReader, (int)num5); } else if ((ulong)num5 > (ulong)(stream.Length - stream.Position)) { if (this.strictMode) { break; } break; } else { if (this.storeAllChunks) { if (num5 > 2147483647u) { throw new InvalidDataException(string.Format("RiffChunk chunk length must be between 0 and {0}.", int.MaxValue)); } this.riffChunks.Add(WaveFileChunkReader.GetRiffChunk(stream, num4, (int)num5)); } stream.Position += (long)((ulong)num5); } } if (this.waveFormat == null) { throw new FormatException("Invalid WAV file - No fmt chunk found"); } if (this.dataChunkPosition == -1L) { throw new FormatException("Invalid WAV file - No data chunk found"); } }