Changeset 5
- Timestamp:
- 11/08/08 16:03:59 (2 months ago)
- Location:
- trunk/AddIns/WaveAudio
- Files:
-
- 2 added
- 3 modified
- 2 copied
-
RiffReader.cs (copied) (copied from trunk/AddIns/WaveAudio/WaveReader.cs) (2 diffs, 1 prop)
-
WaveAudio.cs (modified) (3 diffs)
-
WaveAudio.csproj (modified) (2 diffs)
-
WaveAudioFormat.cs (copied) (copied from trunk/AddIns/WaveAudio/WaveFormat.cs) (3 diffs, 1 prop)
-
WaveFormatExChunk.cs (modified) (1 diff)
-
WaveTag.cs (added)
-
WaveTagFormat.cs (added)
Legend:
- Unmodified
- Added
- Removed
-
trunk/AddIns/WaveAudio/RiffReader.cs
r3 r5 19 19 20 20 using System; 21 using System.Collections.Generic; 21 22 using System.IO; 22 23 using System.Text; 23 24 using OmniEncoder.AudioModel.AddInViews; 24 25 25 namespace OmniEncoder. AudioModel.Wave26 namespace OmniEncoder.WaveAudio 26 27 { 27 class WaveReader : IDisposable28 class RiffReader : IDisposable 28 29 { 29 30 readonly BinaryReader reader; 31 readonly Stack<uint> parentChunkLength = new Stack<uint>(2); 32 readonly Stack<string> parentChunkType = new Stack<string>(2); 33 readonly Stack<uint> parentChunkStart = new Stack<uint>(2); 30 34 31 internal WaveReader(string fileName)35 internal RiffReader(string fileName) 32 36 { 33 37 reader = new BinaryReader(File.OpenRead(fileName), Encoding.ASCII); 34 38 35 try 39 if (new string(reader.ReadChars(4)) != "RIFF") 40 throw new AudioInvalidException("The file '" + fileName + "' does not contain a RIFF header."); 41 42 parentChunkLength.Push(reader.ReadUInt32() - 4); 43 parentChunkType.Push(new string(reader.ReadChars(4))); 44 parentChunkStart.Push(12); 45 } 46 47 internal string ChunkType 48 { 49 get { return parentChunkType.Peek(); } 50 } 51 52 internal bool Descend(string fourCC) 53 { 54 uint subChunkLength; 55 if (AdvanceToChunk(fourCC, out subChunkLength)) 36 56 { 37 if (new string(reader.ReadChars(4)) != "RIFF") 38 throw new AudioInvalidException("The file '" + fileName + "' does not contain a RIFF header."); 39 40 reader.BaseStream.Seek(4, SeekOrigin.Current); 41 42 if (new string(reader.ReadChars(4)) != "WAVE") 43 throw new AudioInvalidException("The file '" + fileName + "' is not a Wave file."); 57 parentChunkLength.Push(subChunkLength - 4); 58 parentChunkType.Push(new string(reader.ReadChars(4))); 59 parentChunkStart.Push((uint)reader.BaseStream.Position); 60 return true; 44 61 } 45 catch (EndOfStreamException e) 46 { 47 throw new AudioInvalidException("The file '" + fileName + "' is missing data.", e); 48 } 62 else 63 return false; 49 64 } 50 65 51 66 internal byte[] ReadChunk(string fourCC) 52 67 { 53 uint chunkSize = ReadChunkSize(fourCC); 54 return reader.ReadBytes((int)chunkSize); 68 uint chunkLength; 69 AdvanceToChunk(fourCC, out chunkLength); 70 return reader.ReadBytes((int)chunkLength); 55 71 } 56 72 57 internal uint ReadChunkSize(string fourCC)73 internal bool AdvanceToChunk(string fourCC, out uint chunkLength) 58 74 { 59 reader.BaseStream.Seek(12, SeekOrigin.Begin); 75 reader.BaseStream.Seek(parentChunkStart.Peek(), SeekOrigin.Begin); 76 77 uint parentChunkEnd = parentChunkStart.Peek() + parentChunkLength.Peek(); 60 78 61 79 string currentFourCC = new string(reader.ReadChars(4)); 62 uint c hunkSize= reader.ReadUInt32();80 uint currentChunkLength = reader.ReadUInt32(); 63 81 64 82 while (currentFourCC != fourCC) 65 83 { 66 84 // Chunks are 2-byte aligned. 67 reader.BaseStream.Seek(chunkSize + chunkSize % 2, SeekOrigin.Current); 85 reader.BaseStream.Seek(currentChunkLength + currentChunkLength % 2, SeekOrigin.Current); 86 87 if (reader.BaseStream.Position == parentChunkEnd) 88 { 89 chunkLength = 0; 90 return false; 91 } 68 92 69 93 currentFourCC = new string(reader.ReadChars(4)); 70 c hunkSize= reader.ReadUInt32();94 currentChunkLength = reader.ReadUInt32(); 71 95 } 72 96 73 return chunkSize; 97 chunkLength = currentChunkLength; 98 return true; 74 99 } 75 100 … … 96 121 } 97 122 98 ~ WaveReader()123 ~RiffReader() 99 124 { 100 125 Dispose(false); -
trunk/AddIns/WaveAudio/WaveAudio.cs
r3 r5 23 23 using OmniEncoder.AudioModel.AddInViews; 24 24 25 namespace OmniEncoder. AudioModel.Wave25 namespace OmniEncoder.WaveAudio 26 26 { 27 27 class WaveAudio : IAudio … … 33 33 readonly TimeSpan length; 34 34 35 internal WaveAudio( byte[] formatChunk, uint audioSize)35 internal WaveAudio(RiffReader reader) 36 36 { 37 byte[] formatChunk = reader.ReadChunk("fmt "); 38 37 39 // Read the array into a struct. 38 40 GCHandle handle = GCHandle.Alloc(formatChunk, GCHandleType.Pinned); … … 60 62 sampleRate = (int)waveFormatEx.SamplesPerSec; 61 63 64 // Use the size of the "data" chunk to calculate the length in seconds. 65 uint audioSize; 66 reader.AdvanceToChunk("data", out audioSize); 62 67 int lengthInSeconds = (int)Math.Round(audioSize / (double)waveFormatEx.AvgBytesPerSec); 63 68 length = new TimeSpan(0, 0, lengthInSeconds); -
trunk/AddIns/WaveAudio/WaveAudio.csproj
r2 r5 54 54 </ItemGroup> 55 55 <ItemGroup> 56 <Compile Include="RiffReader.cs" /> 56 57 <Compile Include="Properties\AssemblyInfo.cs" /> 57 58 <Compile Include="WaveAudio.cs" /> 58 <Compile Include="Wave Format.cs" />59 <Compile Include="WaveAudioFormat.cs" /> 59 60 <Compile Include="WaveFormatExChunk.cs" /> 60 <Compile Include="WaveReader.cs" /> 61 <Compile Include="WaveTag.cs" /> 62 <Compile Include="WaveTagFormat.cs" /> 61 63 </ItemGroup> 62 64 <ItemGroup> … … 64 66 <Project>{DEC1B566-9D21-4528-84B7-8D127163AD0B}</Project> 65 67 <Name>AudioModel.AddInViews</Name> 68 <Private>False</Private> 69 </ProjectReference> 70 <ProjectReference Include="..\..\AddInViews\TagModel.AddInViews\TagModel.AddInViews.csproj"> 71 <Project>{E298AEFD-04A2-4E6C-9545-2F6659CC5AF1}</Project> 72 <Name>TagModel.AddInViews</Name> 66 73 <Private>False</Private> 67 74 </ProjectReference> -
trunk/AddIns/WaveAudio/WaveAudioFormat.cs
r2 r5 23 23 using OmniEncoder.AudioModel.AddInViews; 24 24 25 namespace OmniEncoder. AudioModel.Wave25 namespace OmniEncoder.WaveAudio 26 26 { 27 27 [AddIn("Wave Audio")] 28 public class Wave Format : IAudioFormat28 public class WaveAudioFormat : IAudioFormat 29 29 { 30 30 readonly List<string> extensions; 31 31 32 public Wave Format()32 public WaveAudioFormat() 33 33 { 34 34 extensions = new List<string>(1); … … 36 36 } 37 37 38 #region IAudio ContainerMembers38 #region IAudioFormat Members 39 39 40 40 public ReadOnlyCollection<string> Extensions … … 43 43 } 44 44 45 public IAudio Get Info(string fileName)45 public IAudio GetAudio(string fileName) 46 46 { 47 using (WaveReader reader = new WaveReader(fileName)) 48 { 49 byte[] formatChunk = reader.ReadChunk("fmt "); 50 uint audioSize = reader.ReadChunkSize("data"); 51 return new WaveAudio(formatChunk, audioSize); 52 } 47 using (RiffReader reader = new RiffReader(fileName)) 48 return new WaveAudio(reader); 53 49 } 54 50 -
trunk/AddIns/WaveAudio/WaveFormatExChunk.cs
r2 r5 18 18 */ 19 19 20 namespace OmniEncoder. AudioModel.Wave20 namespace OmniEncoder.WaveAudio 21 21 { 22 22 struct WaveFormatEx
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)