Click here to Skip to main content
15,886,799 members
Articles / Programming Languages / C#

C# RIFF Parser

Rate me:
Please Sign up or sign in to vote.
4.54/5 (19 votes)
6 Jun 20057 min read 163.2K   9K   67  
Decode Resource Interchange Files (AVI, WAV, RMID...) using this pure C# parser.
using System;
using System.Text;

namespace RiffParserDemo
{
    class RiffParserDemo
    {
        // Parse a RIFF file
        static void Main(string[] args)
        {
			// Create a parser instance
            RiffParser rp = new RiffParser();
			try 
			{
				string filename = @"C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Graphics\videos\BLUR24.avi";
				//string filename = @"C:\WINNT\Media\Chimes.wav"
				if (0 != args.Length)  
				{
					filename = args[0];
				}
					
				// Specify a file to open
				rp.OpenFile(filename);

				// If we got here - the file is valid. Output information about the file
				Console.WriteLine("File " + rp.ShortName + " is a \"" + RiffParser.FromFourCC(rp.FileRIFF)
					+ "\" with a specific type of \"" + RiffParser.FromFourCC(rp.FileType) + "\"");

				// Store the size to loop on the elements
				int size = rp.DataSize;

				// Define the processing delegates
				RiffParser.ProcessChunkElement pc = new RiffParser.ProcessChunkElement(ProcessChunk);
				RiffParser.ProcessListElement pl = new RiffParser.ProcessListElement(ProcessList);

				// Read all top level elements and chunks
				while (size > 0)
				{
					// Prefix the line with the current top level type
					Console.Write(RiffParser.FromFourCC(rp.FileType) + " (" + size.ToString() + "): ");
					// Get the next element (if there is one)
					if (false == rp.ReadElement(ref size, pc, pl)) break;
				}
				// Close the stream
				rp.CloseFile();
				Console.WriteLine();
			}
			catch (Exception ex)
			{
				Console.WriteLine("-----------------");
				Console.WriteLine("Problem: " + ex.ToString());
			}
            Console.WriteLine("\n\rDone. Press 'Enter' to exit.");
            Console.ReadLine();
        }

        // Process a RIFF list element (list sub elements)
        public static void ProcessList(RiffParser rp, int FourCC, int length)
        {
            string type = RiffParser.FromFourCC(FourCC);
            Console.WriteLine("Found list element of type \"" + type + "\" and length " + length.ToString());

			// Define the processing delegates
			RiffParser.ProcessChunkElement pc = new RiffParser.ProcessChunkElement(ProcessChunk);
			RiffParser.ProcessListElement pl = new RiffParser.ProcessListElement(ProcessList);

			// Read all the elements in the current list
            try {
                while (length > 0) {
					// Prefix each line with the type of the current list
                    Console.Write(type + " (" + length.ToString() + "): ");
					// Get the next element (if there is one)
                    if (false == rp.ReadElement(ref length, pc, pl)) break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Problem: " + ex.ToString());
            }
        }

        // Process a RIFF chunk element (skip the data)
        public static void ProcessChunk(RiffParser rp, int FourCC, int length, int paddedLength)
        {
            string type = RiffParser.FromFourCC(FourCC);
            Console.WriteLine("Found chunk element of type \"" + type + "\" and length " + length.ToString());

            // Skip data and update bytesleft
            rp.SkipData(paddedLength);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Giora Tamir has been Architecting, Designing and Developing software and hardware solutions for over 15 years. As an IEEE Senior member and a talented developer, Giora blends software development, knowledge of protocols, extensive understanding of hardware and profound knowledge of both Unix and Windows based systems to provide a complete solution for both defense and commercial applications. Giora, also known as G.T., now holds the position of Principal Engineer for ProfitLine, Inc. architecting the next generation of .NET applications based on a Service-Oriented-Architecture.

Gioras areas of interest include distributed applications, networking and cryptography in addition to Unix internals and embedded programming.

Founded in 1992, ProfitLine manages hundreds of millions of dollars in annual telecom spend for its prestigious Fortune 1000 client base, such as Merrill Lynch, Charming Shoppes, Macromedia, CNA Financial Corporation, and Constellation Energy Group. ProfitLine's outsourced solution streamlines telecom administrative functions by combining a best practices approach with intelligent technology. For more information about ProfitLine, call 858.452.6800 or e-mail <a href=mailto:sales@profitline.com>sales@profitline.com.

Comments and Discussions