BugInTheSYS

WaveFile-Klasse

Feb 18th, 2013
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _3dSoundSimulation {
  7.     /// <summary>
  8.     /// An audio file in 16-bit mono/stereo RIFF WAVE data format.
  9.     /// </summary>
  10.     public class WaveFile {
  11.         protected string _Filename;
  12.        
  13.         protected byte[] _RawContent;
  14.         protected uint _RawLength;
  15.  
  16.         protected uint _Channels;
  17.         protected uint _SampleCount;
  18.  
  19.         public WaveFile( string Filename ) {
  20.             this._Filename = Filename;
  21.             _RawContent = System.IO.File.ReadAllBytes( Filename );
  22.             _RawLength = _RawContent[0x28] | ( uint )( _RawContent[0x29] << 8 ) | ( uint )( _RawContent[0x2a] << 16 ) | ( uint )( _RawContent[0x2b] << 24 );
  23.             _Channels = ( uint )( _RawContent[0x16] | ( _RawContent[0x17] << 8 ) );
  24.             _SampleCount = _RawLength / ( _Channels * 2 );
  25.         }
  26.  
  27.         /// <summary>
  28.         /// Returns the sample at the specified location.
  29.         /// </summary>
  30.         /// <param name="Number">The Sample# to retrieve.</param>
  31.         /// <returns></returns>
  32.         public short[] GetSample( uint Number ) {
  33.             if ( Number >= _SampleCount ) {
  34.                 return null;
  35.             }
  36.             short[] Result = new short[_Channels];
  37.             uint Pointer = 0x2c + ( _Channels * 2 ) * Number;
  38.             for ( int i = 0; i < _Channels; Pointer +=2, i++ ) {
  39.                 Result[i] = ( short )( _RawContent[Pointer] | ( _RawContent[Pointer + 1] << 8 ) );
  40.             }
  41.             return Result;
  42.         }
  43.  
  44.         /// <summary>
  45.         /// The amount of samples in the file.
  46.         /// This property is read-only.
  47.         /// </summary>
  48.         public uint SampleCount {
  49.             get {
  50.                 return _SampleCount;
  51.             }
  52.         }
  53.  
  54.         /// <summary>
  55.         /// The amount of channels in the file.
  56.         /// This property is read-only.
  57.         /// </summary>
  58.         public uint Channels {
  59.             get {
  60.                 return _Channels;
  61.             }
  62.         }
  63.  
  64.         /// <summary>
  65.         /// The path to the file.
  66.         /// This property is read-only.
  67.         /// </summary>
  68.         public string Filename {
  69.             get {
  70.                 return System.IO.Path.GetFullPath( _Filename );
  71.             }
  72.         }
  73.  
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment