Thursday, 3 October 2013

Reading Wave files (.wav) using C#


       One of my friend who is working on his project asked me this question,  So I decided to make a post which would be helpful to many amateurs.

       Wave file format is a subset of RIFF(Resource Interchange File Format) specification in which data are stored in "chunks".  To learn move about how .wav format works,  wiki would help.

       Since wave file is subset of RIFF,  they consist of two sub-chunks namely "fmt" and "data".  The "fmt" are headers which provide information about the file.  Then begins the "data" part.




        Since wave file is in bit stream format,  we use "BinaryReader" class to read the file.  Include the "System.IO" namespace.

       

     BinaryReader reader = new BinaryReader(new FileStream(@"zulu.wav",FileMode.Open));

       

        We use "byte[]" to store the data.
     
       

     byte[] sound;
      
 
       
         Then read the headers from the file.
     
       
  
     int chunkID = reader.ReadInt32();      
     int fileSize = reader.ReadInt32();     
     int riffType = reader.ReadInt32();     
     int fmtID = reader.ReadInt32();        
     int fmtSize = reader.ReadInt32();      
     int fmtCode = reader.ReadInt16();      
     int channels = reader.ReadInt16();     
     int sampleRate = reader.ReadInt32();   
     int fmtAvgBPS = reader.ReadInt32();    
     int fmtBlockAlign = reader.ReadInt16();
     int bitDepth = reader.ReadInt16();
  
 

        To read all the extra information present in the file,  we use following code.

       
    
     if(fmtSize == 18)                          
     {                                          
          // Read any extra values               
          int fmtExtraSize = reader.ReadInt16(); 
          reader.ReadBytes(fmtExtraSize);        
     }    
       
 

        The "fmt" part is done.  Now read the "data" part.

       
   
     int dataID = reader.ReadInt32();    
     int dataSize = reader.ReadInt32();  
     sound = reader.ReadBytes(dataSize);  
       
 

        The "sound" array contains the data of the .wav file.  You can now perform any operation you like to do with the file.  For example,  I just wanted to visualize the .wav file.  I used following code to do so.


       
     int i=1;
     int j=400-Convert.ToInt32(sound[0]);
     gra.DrawLine(a, 0, 400, i, j);
     Point prev=new Point(i,j);
     i++;
     Point next = new Point();
     foreach (byte temp in sound)
     {
          j=400-Convert.ToInt32(temp));
          next.X = i++;
          next.Y = j;
          gra.DrawLine(a, prev, next);
          prev = next;
     }
       
 


         The Output of the above code is shown below.




            Your Suggestions,  Opinions are most welcome.  Please do comment.


Wednesday, 26 June 2013

Convert your Photos into 3D


      When I was browsing the web,  I found this thing interesting.  So I thought I would blog about this and share it with others.  The App I was talking about is 123dapp.  This App can generate a 3D Mesh if you upload the collection of photos of the object in different angles.  You can download this Mesh and use it in your animation project, developing games etc,.  




      The entire task is shown step by step in the following video:



      The bunch of other videos is  found in the official site of the app.  Share this information if you find it useful.  

Share on Facebook

Thursday, 20 June 2013

How to install Perl in your system

Introduction :

      Perl is a interpreted and dynamic programming language.  Perl is widely used because it is more flexible.  In this post I will show you how to check whether Perl is already installed on system, if not then how to install it.


                     Caution:  Perl is addictive!!! 



Checking whether Perl is present in your system or not :

      Checking whether Perl is present in your system or not is a simple step.  Follow these steps,
  1.  Fire up your terminal. If you are using windows system then open Command Prompt(Start -> Run -> cmd).
  2.  Type  perl -v  and hit enter.  

 If you get the following message, then perl is already installed in your system.

 This is perl 5, version 12, subversion 4 (v5.12.4) built for MSWin32-x64-multi-thread (with 9 registered patches, see perl -V for more detail).

If the version of your perl is 5 or above, well and good.

If you get message like,


 Bad command or file name. 

          (or)

 perl is not recognized command... 

then perl is not already installed in your system. 

Downloading and Installing Perl :

    1.  Download perl for your operating system from official website .

    2.  Then Run the installer and install it.

    3.  Now to check whether perl is successfully installed in system or not,  Open a new command prompt (or) terminal and then type  perl -v .  If you get the message with version of perl, then perl is successfully installed in your system.