Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace _3dSoundSimulation {
- /// <summary>
- /// An audio file in 16-bit mono/stereo RIFF WAVE data format.
- /// </summary>
- public class WaveFile {
- protected string _Filename;
- protected byte[] _RawContent;
- protected uint _RawLength;
- protected uint _Channels;
- protected uint _SampleCount;
- public WaveFile( string Filename ) {
- this._Filename = Filename;
- _RawContent = System.IO.File.ReadAllBytes( Filename );
- _RawLength = _RawContent[0x28] | ( uint )( _RawContent[0x29] << 8 ) | ( uint )( _RawContent[0x2a] << 16 ) | ( uint )( _RawContent[0x2b] << 24 );
- _Channels = ( uint )( _RawContent[0x16] | ( _RawContent[0x17] << 8 ) );
- _SampleCount = _RawLength / ( _Channels * 2 );
- }
- /// <summary>
- /// Returns the sample at the specified location.
- /// </summary>
- /// <param name="Number">The Sample# to retrieve.</param>
- /// <returns></returns>
- public short[] GetSample( uint Number ) {
- if ( Number >= _SampleCount ) {
- return null;
- }
- short[] Result = new short[_Channels];
- uint Pointer = 0x2c + ( _Channels * 2 ) * Number;
- for ( int i = 0; i < _Channels; Pointer +=2, i++ ) {
- Result[i] = ( short )( _RawContent[Pointer] | ( _RawContent[Pointer + 1] << 8 ) );
- }
- return Result;
- }
- /// <summary>
- /// The amount of samples in the file.
- /// This property is read-only.
- /// </summary>
- public uint SampleCount {
- get {
- return _SampleCount;
- }
- }
- /// <summary>
- /// The amount of channels in the file.
- /// This property is read-only.
- /// </summary>
- public uint Channels {
- get {
- return _Channels;
- }
- }
- /// <summary>
- /// The path to the file.
- /// This property is read-only.
- /// </summary>
- public string Filename {
- get {
- return System.IO.Path.GetFullPath( _Filename );
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment