예제 #1
0
        /// <summary>
        /// Reads a new WaveFormat object from a stream
        /// </summary>
        /// <param name="br">A binary reader that wraps the stream</param>
        public WaveFormat(BinaryReader br)
        {
            int formatChunkLength = br.ReadInt32();
            if(formatChunkLength < 16)
                throw new ApplicationException("Invalid WaveFormat Structure");
            this.waveFormatTag = (WaveFormatEncoding) br.ReadUInt16();
            this.channels = br.ReadInt16();
            this.sampleRate = br.ReadInt32();
            this.averageBytesPerSecond = br.ReadInt32();
            this.blockAlign = br.ReadInt16();
            this.bitsPerSample = br.ReadInt16();
            if (formatChunkLength > 16)
            {

                this.extraSize = br.ReadInt16();
                if (this.extraSize > formatChunkLength - 18)
                {
                    Console.WriteLine("Format chunk mismatch");
                    //RRL GSM exhibits this bug. Don't throw an exception
                    //throw new ApplicationException("Format chunk length mismatch");

                    this.extraSize = (short) (formatChunkLength - 18);
                }

                // read any extra data
                // br.ReadBytes(extraSize);

            }
        }
예제 #2
0
        public WaveFormat(BinaryReader br)
        {
            int num = br.ReadInt32();

            if (num < 16)
            {
                throw new SharpDXException("Invalid WaveFormat Structure", new object[0]);
            }
            this.waveFormatTag         = (WaveFormatEncoding)br.ReadUInt16();
            this.channels              = br.ReadInt16();
            this.sampleRate            = br.ReadInt32();
            this.averageBytesPerSecond = br.ReadInt32();
            this.blockAlign            = br.ReadInt16();
            this.bitsPerSample         = br.ReadInt16();
            this.extraSize             = (short)0;
            if (num <= 16)
            {
                return;
            }
            this.extraSize = br.ReadInt16();
            if ((int)this.extraSize <= num - 18)
            {
                return;
            }
            Console.WriteLine("Format chunk mismatch");
            this.extraSize = (short)(num - 18);
        }
예제 #3
0
        public uint[] GetSeekTable(uint index, out WaveFormatEncoding tag)
        {
            if (index >= bankData.EntryCount)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            tag = 0;

            var minFormat = GetMiniWaveFormat(index);
            var seekTable = FindSeekTable(index);

            switch (minFormat.FormatTag)
            {
            case MiniWaveFormat.Tag.WMA:     // xWMA is supported by XAudio 2.7 and by Xbox One
                tag = (minFormat.BitsPerSample & 0x1) > 0 ? WaveFormatEncoding.Wmaudio3 : WaveFormatEncoding.Wmaudio2;
                break;

            case MiniWaveFormat.Tag.XMA:
                tag = (WaveFormatEncoding)0x166 /* WAVE_FORMAT_XMA2 */;
                break;

            default:
                return(null);
            }

            return(null);
        }
예제 #4
0
        /// <summary>
        /// Reads a new WaveFormat object from a stream
        /// </summary>
        /// <param name="br">A binary reader that wraps the stream</param>
        public WaveFormat(BinaryReader br)
        {
            int formatChunkLength = br.ReadInt32();

            if (formatChunkLength < 16)
            {
                throw new ApplicationException("Invalid WaveFormat Structure");
            }
            this.waveFormatTag         = (WaveFormatEncoding)br.ReadUInt16();
            this.channels              = br.ReadInt16();
            this.sampleRate            = br.ReadInt32();
            this.averageBytesPerSecond = br.ReadInt32();
            this.blockAlign            = br.ReadInt16();
            this.bitsPerSample         = br.ReadInt16();
            if (formatChunkLength > 16)
            {
                this.extraSize = br.ReadInt16();
                if (this.extraSize > formatChunkLength - 18)
                {
                    Console.WriteLine("Format chunk mismatch");
                    //RRL GSM exhibits this bug. Don't throw an exception
                    //throw new ApplicationException("Format chunk length mismatch");

                    this.extraSize = (short)(formatChunkLength - 18);
                }

                // read any extra data
                // br.ReadBytes(extraSize);
            }
        }
예제 #5
0
        /// <summary>
        ///   Converts a <see cref="WaveFormatEncoding"/> and bits per sample information
        ///   into an appropriate <see cref="SampleFormat"/>.
        /// </summary>
        ///
        /// <param name="tag">The wave format tag.</param>
        /// <param name="bitsPerSample">The bits per sample.</param>
        ///
        public static SampleFormat ToSampleFormat(this WaveFormatEncoding tag, int bitsPerSample)
        {
            if (tag == WaveFormatEncoding.Pcm)
            {
                if (bitsPerSample == 16)
                {
                    return(SampleFormat.Format16Bit);
                }
                else if (bitsPerSample == 32)
                {
                    return(SampleFormat.Format32Bit);
                }
            }
            else if (tag == WaveFormatEncoding.IeeeFloat)
            {
                if (bitsPerSample == 32)
                {
                    return(SampleFormat.Format32BitIeeeFloat);
                }
                else if (bitsPerSample == 64)
                {
                    return(SampleFormat.Format64BitIeeeFloat);
                }
            }

            throw new ArgumentOutOfRangeException("tag", "Unsupported format tag.");
        }
예제 #6
0
 public AudioFormat(WaveFormatEncoding waveEncoding, int channels, int bitsPerSample, int sampleRate)
 {
     WaveEncoding  = waveEncoding;
     Channels      = channels;
     BitsPerSample = bitsPerSample;
     SampleRate    = sampleRate;
 }
예제 #7
0
        public WaveFormatExtensible(int rate, int bits, int channels)
            : this()
        {
            if (channels < 1)
            {
                throw new ArgumentOutOfRangeException("Channels must be 1 or greater", "channels");
            }
            // minimum 16 bytes, sometimes 18 for PCM
            this.WaveFormatTag = WaveFormatEncoding.Pcm;
            this.Channels      = (short)channels;
            this.SampleRate    = rate;
            this.BitsPerSample = (short)bits;
            this.ExtraSize     = 0;

            this.BlockAlign            = (short)(channels * (bits / 8));
            this.AverageBytesPerSecond = this.SampleRate * this.BlockAlign;

            WaveFormatTag      = WaveFormatEncoding.Extensible;
            ExtraSize          = 22;
            ValidBitsPerSample = (short)bits;
            for (int n = 0; n < channels; n++)
            {
                ChannelMask |= (1 << n);
            }
            if (bits == 32)
            {
                // KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
                SubFormat = MEDIASUBTYPE_IEEE_FLOAT; // new Guid("00000003-0000-0010-8000-00aa00389b71");
            }
            else
            {
                // KSDATAFORMAT_SUBTYPE_PCM
                SubFormat = MEDIASUBTYPE_PCM; // new Guid("00000001-0000-0010-8000-00aa00389b71");
            }
        }
예제 #8
0
        private void ReadWaveFormat(BinaryReader br, int formatChunkLength)
        {
            if (formatChunkLength < 16)
            {
                throw new InvalidDataException("Invalid WaveFormat Structure");
            }

            waveFormatTag         = (WaveFormatEncoding)br.ReadUInt16();
            channels              = br.ReadInt16();
            sampleRate            = br.ReadInt32();
            averageBytesPerSecond = br.ReadInt32();
            blockAlign            = br.ReadInt16();
            bitsPerSample         = br.ReadInt16();


            if (formatChunkLength > 16)
            {
                extraSize = br.ReadInt16();
                if (extraSize != formatChunkLength - 18)
                {
                    Debug.WriteLine("Format chunk mismatch");
                    extraSize = (short)(formatChunkLength - 18);
                }
            }
        }
 /// <summary>
 /// Initializes and A-Law or u-Law "WaveStream".  The source stream
 /// must be 16-bit PCM!
 /// </summary>
 /// <param name="_encoding">ALaw or MuLaw only.</param>
 /// <param name="_sourcestream">The input PCM stream.</param>
 public PcmToG711ConversionStream( WaveFormatEncoding _encoding,
                                   IWaveProvider _provider )
 {
     Provider = _provider;
     WaveFormat sourceformat = Provider.WaveFormat;
     if( (sourceformat.Encoding != WaveFormatEncoding.Pcm) &&
         (sourceformat.BitsPerSample != 16) )
     {
         throw new NotSupportedException("Input must be 16-bit PCM.  Try using a conversion stream.");
     }
     if( _encoding == WaveFormatEncoding.ALaw )
     {
         Encode = this.EncodeALaw;
         waveFormat = WaveFormat.CreateALawFormat( _provider.WaveFormat.SampleRate,
                                                   _provider.WaveFormat.Channels) ;
                                                     
     }
     else if( _encoding == WaveFormatEncoding.MuLaw )
     {
         Encode = this.EncodeMuLaw;
         waveFormat = WaveFormat.CreateMuLawFormat( _provider.WaveFormat.SampleRate,
                                                    _provider.WaveFormat.Channels) ;
     }
     else
     {
         throw new NotSupportedException("Encoding must be A-Law or u-Law");
     }
 }
예제 #10
0
        public static unsafe WaveFormat MarshalFrom(IntPtr pointer)
        {
            if (pointer == IntPtr.Zero)
            {
                return((WaveFormat)null);
            }
            WaveFormat.__PcmNative @ref = *(WaveFormat.__PcmNative *)(void *) pointer;
            WaveFormatEncoding     waveFormatEncoding = @ref.waveFormatTag;

            if ((int)@ref.channels <= 2 && (waveFormatEncoding == WaveFormatEncoding.Pcm || waveFormatEncoding == WaveFormatEncoding.IeeeFloat || (waveFormatEncoding == WaveFormatEncoding.Wmaudio2 || waveFormatEncoding == WaveFormatEncoding.Wmaudio3)))
            {
                WaveFormat waveFormat = new WaveFormat();
                waveFormat.__MarshalFrom(ref @ref);
                return(waveFormat);
            }
            else if (waveFormatEncoding == WaveFormatEncoding.Extensible)
            {
                WaveFormatExtensible formatExtensible = new WaveFormatExtensible();
                formatExtensible.__MarshalFrom(ref *(WaveFormatExtensible.__Native *)(void *) pointer);
                return((WaveFormat)formatExtensible);
            }
            else
            {
                if (waveFormatEncoding != WaveFormatEncoding.Adpcm)
                {
                    throw new InvalidOperationException(string.Format("Unsupported WaveFormat [{0}]", (object)waveFormatEncoding));
                }
                WaveFormatAdpcm waveFormatAdpcm = new WaveFormatAdpcm();
                waveFormatAdpcm.__MarshalFrom(ref *(WaveFormatAdpcm.__Native *)(void *) pointer);
                return((WaveFormat)waveFormatAdpcm);
            }
        }
예제 #11
0
 public AudioCodecArgs(WaveFormatEncoding waveEnconding, int channels, int bitsPerChannel, int frequency, int bitrate, short frameSize, byte complexity)
     : base(waveEnconding, channels, bitsPerChannel, frequency)
 {
     Bitrate    = bitrate;
     FrameSize  = frameSize;
     Complexity = complexity;
 }
예제 #12
0
            public AudioFrame(int size, WaveFormatEncoding encoding, int bytesPerSample)
            {
                size = Math.Max(size.CeilingToPowerOfTwo(), MIN_BUFFER_SIZE);

                data = new float[size];

                this.encoding       = encoding;
                this.bytesPerSample = bytesPerSample;
            }
예제 #13
0
 internal void __MarshalFrom(ref WaveFormat.__PcmNative @ref)
 {
     this.waveFormatTag         = @ref.waveFormatTag;
     this.channels              = @ref.channels;
     this.sampleRate            = @ref.sampleRate;
     this.averageBytesPerSecond = @ref.averageBytesPerSecond;
     this.blockAlign            = @ref.blockAlign;
     this.bitsPerSample         = @ref.bitsPerSample;
     this.extraSize             = (short)0;
 }
예제 #14
0
 // Method to marshal from native to managed struct
 internal void __MarshalFrom(ref __Native @ref)
 {
     waveFormatTag         = @ref.pcmWaveFormat.waveFormatTag;
     channels              = @ref.pcmWaveFormat.channels;
     sampleRate            = @ref.pcmWaveFormat.sampleRate;
     averageBytesPerSecond = @ref.pcmWaveFormat.averageBytesPerSecond;
     blockAlign            = @ref.pcmWaveFormat.blockAlign;
     bitsPerSample         = @ref.pcmWaveFormat.bitsPerSample;
     extraSize             = @ref.extraSize;
 }
예제 #15
0
 // Method to marshal from native to managed struct
 internal void __MarshalFrom(ref __Native @ref)
 {
     waveFormatTag = @ref.pcmWaveFormat.waveFormatTag;
     channels = @ref.pcmWaveFormat.channels;
     sampleRate = @ref.pcmWaveFormat.sampleRate;
     averageBytesPerSecond = @ref.pcmWaveFormat.averageBytesPerSecond;
     blockAlign = @ref.pcmWaveFormat.blockAlign;
     bitsPerSample = @ref.pcmWaveFormat.bitsPerSample;
     extraSize = @ref.extraSize;
 }
예제 #16
0
        public override string ToString()
        {
            WaveFormatEncoding waveFormatEncoding = this.waveFormatTag;

            if (waveFormatEncoding == WaveFormatEncoding.Pcm || waveFormatEncoding == WaveFormatEncoding.Extensible)
            {
                return(string.Format("{0} bit PCM: {1}kHz {2} channels", this.bitsPerSample, this.sampleRate / 1000, this.channels));
            }
            return(this.waveFormatTag.ToString());
        }
예제 #17
0
 // Method to marshal from native to managed struct
 internal unsafe void __MarshalFrom(ref __Native @ref)
 {
     this.waveFormatTag = @ref.pcmWaveFormat.waveFormatTag;
     this.channels = @ref.pcmWaveFormat.channels;
     this.sampleRate = @ref.pcmWaveFormat.sampleRate;
     this.averageBytesPerSecond = @ref.pcmWaveFormat.averageBytesPerSecond;
     this.blockAlign = @ref.pcmWaveFormat.blockAlign;
     this.bitsPerSample = @ref.pcmWaveFormat.bitsPerSample;
     this.extraSize = @ref.extraSize;            
 }
예제 #18
0
 // Method to marshal from native to managed struct
 internal unsafe void __MarshalFrom(ref __Native @ref)
 {
     this.waveFormatTag         = @ref.pcmWaveFormat.waveFormatTag;
     this.channels              = @ref.pcmWaveFormat.channels;
     this.sampleRate            = @ref.pcmWaveFormat.sampleRate;
     this.averageBytesPerSecond = @ref.pcmWaveFormat.averageBytesPerSecond;
     this.blockAlign            = @ref.pcmWaveFormat.blockAlign;
     this.bitsPerSample         = @ref.pcmWaveFormat.bitsPerSample;
     this.extraSize             = @ref.extraSize;
 }
예제 #19
0
 public WaveFormatExtensible(WaveFormat waveFormat)
     : this()
 {
     this.WaveFormatTag         = waveFormat.WaveFormatTag;
     this.Channels              = waveFormat.Channels;
     this.SampleRate            = waveFormat.SampleRate;
     this.AverageBytesPerSecond = waveFormat.AverageBytesPerSecond;
     this.BlockAlign            = waveFormat.BlockAlign;
     this.BitsPerSample         = waveFormat.BitsPerSample;
     this.ExtraSize             = waveFormat.ExtraSize;
 }
예제 #20
0
        private EventHandler <WaveInEventArgs> GetHandlerByEncoding(WaveFormatEncoding encoding)
        {
            switch (encoding)
            {
            case WaveFormatEncoding.IeeeFloat:
                return(HandleIeeeFloatBuffer);

            default:
                throw new NotImplementedException($"La codifica {encoding} al momento non è supportata");
            }
        }
예제 #21
0
        public float[] ReadNextSampleFrame()
        {
            WaveFormatEncoding encoding = this.waveFormat.Encoding;

            if (encoding != WaveFormatEncoding.Pcm && encoding != WaveFormatEncoding.IeeeFloat && encoding != WaveFormatEncoding.Extensible)
            {
                throw new InvalidOperationException("Only 16, 24 or 32 bit PCM or IEEE float audio data supported");
            }
            float[] array = new float[this.waveFormat.Channels];
            int     num   = this.waveFormat.Channels * (this.waveFormat.BitsPerSample / 8);

            byte[] array2 = new byte[num];
            int    num2   = this.Read(array2, 0, num);

            if (num2 == 0)
            {
                return(null);
            }
            if (num2 < num)
            {
                throw new InvalidDataException("Unexpected end of file");
            }
            int num3 = 0;

            for (int i = 0; i < this.waveFormat.Channels; i++)
            {
                if (this.waveFormat.BitsPerSample == 16)
                {
                    array[i] = (float)BitConverter.ToInt16(array2, num3) / 32768f;
                    num3    += 2;
                }
                else if (this.waveFormat.BitsPerSample == 24)
                {
                    array[i] = (float)((int)((sbyte)array2[num3 + 2]) << 16 | (int)array2[num3 + 1] << 8 | (int)array2[num3]) / 8388608f;
                    num3    += 3;
                }
                else if (this.waveFormat.BitsPerSample == 32 && this.waveFormat.Encoding == WaveFormatEncoding.IeeeFloat)
                {
                    array[i] = BitConverter.ToSingle(array2, num3);
                    num3    += 4;
                }
                else
                {
                    if (this.waveFormat.BitsPerSample != 32)
                    {
                        throw new InvalidOperationException("Unsupported bit depth");
                    }
                    array[i] = (float)BitConverter.ToInt32(array2, num3) / 2.14748365E+09f;
                    num3    += 4;
                }
            }
            return(array);
        }
예제 #22
0
파일: WaveFormat.cs 프로젝트: tanis2000/FEZ
 public WaveFormat(int rate, int bits, int channels)
 {
     if (channels < 1)
     throw new ArgumentOutOfRangeException("Channels must be 1 or greater", "channels");
       this.waveFormatTag = bits < 32 ? WaveFormatEncoding.Pcm : WaveFormatEncoding.IeeeFloat;
       this.channels = (short) channels;
       this.sampleRate = rate;
       this.bitsPerSample = (short) bits;
       this.extraSize = (short) 0;
       this.blockAlign = (short) (channels * (bits / 8));
       this.averageBytesPerSecond = this.sampleRate * (int) this.blockAlign;
 }
예제 #23
0
 public void ReadFromStream(Stream stream)
 {
     BinaryReader br = new BinaryReader(stream);
     FormatTag = (WaveFormatEncoding)br.ReadInt16();
     Channels = br.ReadInt16();
     SamplesPerSec = br.ReadInt32();
     AverageBytesPerSecond = br.ReadInt32();
     BlockAlign = br.ReadInt16();
     BitsPerSample = br.ReadInt16();
     ExtraDataSize = br.ReadInt16();
     ExtraData = stream.EnsureRead(ExtraDataSize);
 }
예제 #24
0
        public WaveFormat DequeueWaveFormat()
        {
            int averageBytesPerSecond = DequeueInt();
            int bitsPerSample         = DequeueInt();
            int blockAlign            = DequeueInt();
            int channels = DequeueInt();
            WaveFormatEncoding encoding = DequeueWaveFormatEncoding();
            int sampleRate = DequeueInt();

            return(WaveFormat.CreateCustomFormat(encoding, sampleRate,
                                                 channels, averageBytesPerSecond, blockAlign, bitsPerSample));
        }
예제 #25
0
 /// <summary>
 /// Creates a WaveFormat with custom members
 /// </summary>
 /// <param name="tag">The encoding</param>
 /// <param name="sampleRate">Sample Rate</param>
 /// <param name="channels">Number of channels</param>
 /// <param name="averageBytesPerSecond">Average Bytes Per Second</param>
 /// <param name="blockAlign">Block Align</param>
 /// <param name="bitsPerSample">Bits Per Sample</param>
 /// <returns></returns>
 public static WaveFormat CreateCustomFormat(WaveFormatEncoding tag, int sampleRate, int channels, int averageBytesPerSecond, int blockAlign, int bitsPerSample)
 {
     WaveFormat waveFormat = new WaveFormat();
     waveFormat.waveFormatTag = tag;
     waveFormat.channels = (short)channels;
     waveFormat.sampleRate = sampleRate;
     waveFormat.averageBytesPerSecond = averageBytesPerSecond;
     waveFormat.blockAlign = (short)blockAlign;
     waveFormat.bitsPerSample = (short)bitsPerSample;
     waveFormat.extraSize = 0;
     return waveFormat;
 }
예제 #26
0
        public void ReadFromStream(Stream stream)
        {
            BinaryReader br = new BinaryReader(stream);

            FormatTag             = (WaveFormatEncoding)br.ReadInt16();
            Channels              = br.ReadInt16();
            SamplesPerSec         = br.ReadInt32();
            AverageBytesPerSecond = br.ReadInt32();
            BlockAlign            = br.ReadInt16();
            BitsPerSample         = br.ReadInt16();
            ExtraDataSize         = br.ReadInt16();
            ExtraData             = stream.EnsureRead(ExtraDataSize);
        }
예제 #27
0
        /// <summary>
        /// Creates a WaveFormat with custom members
        /// </summary>
        /// <param name="tag">The encoding</param>
        /// <param name="sampleRate">Sample Rate</param>
        /// <param name="channels">Number of channels</param>
        /// <param name="averageBytesPerSecond">Average Bytes Per Second</param>
        /// <param name="blockAlign">Block Align</param>
        /// <param name="bitsPerSample">Bits Per Sample</param>
        /// <returns></returns>
        public static WaveFormat CreateCustomFormat(WaveFormatEncoding tag, int sampleRate, int channels, int averageBytesPerSecond, int blockAlign, int bitsPerSample)
        {
            WaveFormat waveFormat = new WaveFormat();

            waveFormat.waveFormatTag         = tag;
            waveFormat.channels              = (short)channels;
            waveFormat.sampleRate            = sampleRate;
            waveFormat.averageBytesPerSecond = averageBytesPerSecond;
            waveFormat.blockAlign            = (short)blockAlign;
            waveFormat.bitsPerSample         = (short)bitsPerSample;
            waveFormat.extraSize             = 0;
            return(waveFormat);
        }
예제 #28
0
 public static WaveFormat CreateCustomFormat(WaveFormatEncoding tag, int sampleRate, int channels, int averageBytesPerSecond, int blockAlign, int bitsPerSample)
 {
     return(new WaveFormat
     {
         waveFormatTag = tag,
         channels = (short)channels,
         sampleRate = sampleRate,
         averageBytesPerSecond = averageBytesPerSecond,
         blockAlign = (short)blockAlign,
         bitsPerSample = (short)bitsPerSample,
         extraSize = 0
     });
 }
예제 #29
0
        /// <summary>
        /// Creates a WaveFormat with custom members
        /// </summary>
        /// <param name="tag">The encoding</param>
        /// <param name="sampleRate">Sample Rate</param>
        /// <param name="channels">Number of channels</param>
        /// <param name="averageBytesPerSecond">Average Bytes Per Second</param>
        /// <param name="blockAlign">Block Align</param>
        /// <param name="bitsPerSample">Bits Per Sample</param>
        /// <returns></returns>
        public static WaveFormatExtensible CreateCustomFormat(WaveFormatEncoding tag, int sampleRate, int channels, int averageBytesPerSecond, int blockAlign, int bitsPerSample)
        {
            WaveFormatExtensible waveFormat = new WaveFormatExtensible();

            waveFormat.WaveFormatTag         = tag;
            waveFormat.Channels              = (short)channels;
            waveFormat.SampleRate            = sampleRate;
            waveFormat.AverageBytesPerSecond = averageBytesPerSecond;
            waveFormat.BlockAlign            = (short)blockAlign;
            waveFormat.BitsPerSample         = (short)bitsPerSample;
            waveFormat.ExtraSize             = 0;
            return(waveFormat);
        }
예제 #30
0
        /// <summary>
        /// Creates a WaveFormat with custom members
        /// </summary>
        /// <param name="tag">The encoding</param>
        /// <param name="SampleRate">Sample Rate</param>
        /// <param name="Channels">Number of Channels</param>
        /// <param name="AverageBytesPerSecond">Average Bytes Per Second</param>
        /// <param name="BlockAlign">Block Align</param>
        /// <param name="BitsPerSample">Bits Per Sample</param>
        /// <returns></returns>
        public static WaveFormat CreateCustomFormat(WaveFormatEncoding tag, int SampleRate, int Channels, int AverageBytesPerSecond, int BlockAlign, int BitsPerSample)
        {
            WaveFormat waveFormat = new WaveFormat();

            waveFormat.waveFormatTag         = tag;
            waveFormat.Channels              = (short)Channels;
            waveFormat.SampleRate            = SampleRate;
            waveFormat.AverageBytesPerSecond = AverageBytesPerSecond;
            waveFormat.BlockAlign            = (short)BlockAlign;
            waveFormat.BitsPerSample         = (short)BitsPerSample;
            waveFormat.ExtraSize             = 0;
            return(waveFormat);
        }
예제 #31
0
        // Преобрузет несколько байт в float, в зависимости от типа кодирования
        private float CovnertBuffer(byte[] buffer, int startIndex, WaveFormatEncoding encoding)
        {
            switch (encoding)
            {
            case WaveFormatEncoding.Pcm:
                return(BitConverter.ToInt16(buffer, startIndex));

            case WaveFormatEncoding.IeeeFloat:
                return(BitConverter.ToSingle(buffer, startIndex));

            default:
                throw new NotSupportedException();
            }
        }
예제 #32
0
 public WaveFormat(int rate, int bits, int channels)
 {
     if (channels < 1)
     {
         throw new ArgumentOutOfRangeException("channels", "Channels must be 1 or greater");
     }
     this.waveFormatTag         = WaveFormatEncoding.Pcm;
     this.channels              = (short)channels;
     this.sampleRate            = rate;
     this.bitsPerSample         = (short)bits;
     this.extraSize             = 0;
     this.blockAlign            = (short)(channels * (bits / 8));
     this.averageBytesPerSecond = this.sampleRate * (int)this.blockAlign;
 }
예제 #33
0
        public ProcessorWaveProvider(string sourceName, IWaveProvider sourceWaveProvider, string waveFilePath, WaveFormat outFormat, Common.ProcessRadioSignalingItemDelegate sigDelegate, Action <bool> hasPropertyChanged, bool recordEnabled, Common.SignalRecordingType recordType, int recordKickTime, Common.NoiseFloor noiseFloor, int customNoiseFloor, bool removeNoise, bool decodeMDC1200, bool decodeGEStar, bool decodeFleetSync, bool decodeP25)
            : base(sourceWaveProvider, waveFilePath)
        {
            LastValidStreamTitle = string.Empty;
            _sourceName          = sourceName;
            _sourceFormat        = sourceWaveProvider.WaveFormat;
            _outFormat           = outFormat;
            _hasPropertyChanged  = hasPropertyChanged;

            _silenceHelper = new SilenceHelper(outFormat.AverageBytesPerSecond / (outFormat.BitsPerSample / 8), noiseFloor, removeNoise, customNoiseFloor);

            if (outFormat.Equals(sourceWaveProvider.WaveFormat))
            {
                _resampleStream = null;
                _useResampler   = false;
            }
            else
            {
                if (Common.AppSettings.Instance.DiagnosticMode)
                {
                    Common.ConsoleHelper.ColorWriteLine(ConsoleColor.Magenta, "{0}: Source Format <> Out Format [{1}] <> [{2}]", sourceName, sourceWaveProvider.WaveFormat, outFormat);
                }
                _resampleStream = new NAudio.Wave.Compression.AcmStream(sourceWaveProvider.WaveFormat, outFormat);
                _useResampler   = true;
            }
            if (decodeMDC1200)
            {
                _mdc = new Decoders.MDC1200(outFormat.SampleRate, ProcessMDC1200, sourceName);
            }
            else
            {
                _mdc = null;
            }
            if (decodeGEStar)
            {
                _star = new Decoders.STAR(outFormat.SampleRate, ProcessSTAR, Decoders.STAR.star_format.star_format_1_16383, sourceName);
            }
            else
            {
                _star = null;
            }
            _rootDecoder = new Decoders.RootDecoder(outFormat.SampleRate, decodeFleetSync, decodeP25, ProcessRootDecoder);

            _recorder       = new AudioRecorder(sourceName, recordType, recordKickTime, outFormat, AudioProcessingGlobals.DefaultSaveFileWaveFormat, recordEnabled);
            _bytesPerSample = outFormat.BitsPerSample / 8;
            _encoding       = outFormat.Encoding;
            _sigDelegate    = sigDelegate;
        }
예제 #34
0
        public ProcessorWaveProvider(string sourceName, IWaveProvider sourceWaveProvider, string waveFilePath, WaveFormat outFormat, Common.ProcessRadioSignalingItemDelegate sigDelegate, Action<bool> hasPropertyChanged, bool recordEnabled, Common.SignalRecordingType recordType, int recordKickTime, Common.NoiseFloor noiseFloor, int customNoiseFloor,bool removeNoise, bool decodeMDC1200, bool decodeGEStar, bool decodeFleetSync, bool decodeP25)
            : base(sourceWaveProvider, waveFilePath)
        {
            LastValidStreamTitle = string.Empty;
            _sourceName = sourceName;
            _sourceFormat = sourceWaveProvider.WaveFormat;
            _outFormat = outFormat;
            _hasPropertyChanged = hasPropertyChanged;

            _silenceHelper = new SilenceHelper(outFormat.AverageBytesPerSecond / (outFormat.BitsPerSample / 8), noiseFloor, removeNoise, customNoiseFloor);

            if (outFormat.Equals(sourceWaveProvider.WaveFormat))
            {
                _resampleStream = null;
                _useResampler = false;
            }
            else
            {
                if (Common.AppSettings.Instance.DiagnosticMode)
                {
                    Common.ConsoleHelper.ColorWriteLine(ConsoleColor.Magenta, "{0}: Source Format <> Out Format [{1}] <> [{2}]", sourceName, sourceWaveProvider.WaveFormat, outFormat);
                }
                _resampleStream = new NAudio.Wave.Compression.AcmStream(sourceWaveProvider.WaveFormat, outFormat);
                _useResampler = true;
            }
            if (decodeMDC1200)
            {
                _mdc = new Decoders.MDC1200(outFormat.SampleRate, ProcessMDC1200, sourceName);
            }
            else
            {
                _mdc = null;
            }
            if (decodeGEStar)
            {
                _star = new Decoders.STAR(outFormat.SampleRate, ProcessSTAR, Decoders.STAR.star_format.star_format_1_16383, sourceName);
            }
            else
            {
                _star = null;
            }
            _rootDecoder = new Decoders.RootDecoder(outFormat.SampleRate, decodeFleetSync, decodeP25, ProcessRootDecoder);

            _recorder = new AudioRecorder(sourceName, recordType, recordKickTime, outFormat, AudioProcessingGlobals.DefaultSaveFileWaveFormat, recordEnabled);
            _bytesPerSample = outFormat.BitsPerSample / 8;
            _encoding = outFormat.Encoding;
            _sigDelegate = sigDelegate;
        }
예제 #35
0
        /// <summary>
        /// Creates a new PCM format with the specified sample rate, bit depth and channels
        /// </summary>
        public WaveFormat(int rate, int bits, int channels)
        {
            if (channels < 1)
            {
                throw new ArgumentOutOfRangeException("Channels must be 1 or greater", "channels");
            }
            // minimum 16 bytes, sometimes 18 for PCM
            this.waveFormatTag = WaveFormatEncoding.Pcm;
            this.channels = (short)channels;
            this.sampleRate = rate;
            this.bitsPerSample = (short)bits;
            this.extraSize = 0;

            this.blockAlign = (short)(channels * (bits / 8));
            this.averageBytesPerSecond = this.sampleRate * this.blockAlign;
        }
예제 #36
0
        /// <summary>
        ///     Creates a WaveFormat with custom members
        /// </summary>
        /// <param name="tag">The encoding</param>
        /// <param name="sampleRate">Sample Rate</param>
        /// <param name="channels">Number of channels</param>
        /// <param name="averageBytesPerSecond">Average Bytes Per Second</param>
        /// <param name="blockAlign">Block Align</param>
        /// <param name="bitsPerSample">Bits Per Sample</param>
        /// <returns></returns>
        public static WaveFormat CreateCustomFormat(WaveFormatEncoding tag, int sampleRate, int channels,
                                                    int averageBytesPerSecond, int blockAlign, int bitsPerSample)
        {
            var waveFormat = new WaveFormat
            {
                _waveFormatTag         = tag,
                _channels              = (short)channels,
                _sampleRate            = sampleRate,
                _averageBytesPerSecond = averageBytesPerSecond,
                _blockAlign            = (short)blockAlign,
                _bitsPerSample         = (short)bitsPerSample,
                _extraSize             = 0
            };

            return(waveFormat);
        }
예제 #37
0
        /// <summary>
        /// Creates a new PCM format with the specified sample rate, bit depth and channels
        /// </summary>
        public WaveFormat(int rate, int bits, int channels)
        {
            if (channels < 1)
            {
                throw new ArgumentOutOfRangeException("Channels", "Channels must be 1 or greater");
            }
            // minimum 16 bytes, sometimes 18 for PCM
            waveFormatTag = WaveFormatEncoding.Pcm;
            this.channels = (short)channels;
            sampleRate    = rate;
            bitsPerSample = (short)bits;
            extraSize     = 0;

            blockAlign            = (short)(channels * (bits / 8));
            averageBytesPerSecond = this.sampleRate * this.blockAlign;
        }
예제 #38
0
        /// <summary>
        /// Creates a new PCM format with the specified sample rate, bit depth and Channels
        /// </summary>
        public WaveFormat(int rate, int bits, int Channels)
        {
            if (Channels < 1)
            {
                throw new ArgumentOutOfRangeException(Channels.ToString(), "Channels must be 1 or greater");
            }
            // minimum 16 bytes, sometimes 18 for PCM
            waveFormatTag = WaveFormatEncoding.Pcm;
            this.Channels = (short)Channels;
            SampleRate    = rate;
            BitsPerSample = (short)bits;
            ExtraSize     = 0;

            BlockAlign            = (short)(Channels * (bits / 8));
            AverageBytesPerSecond = this.SampleRate * this.BlockAlign;
        }
예제 #39
0
파일: WaveFormat.cs 프로젝트: tanis2000/FEZ
 public WaveFormat(BinaryReader br)
 {
     int num = br.ReadInt32();
       if (num < 16)
     throw new SharpDXException("Invalid WaveFormat Structure", new object[0]);
       this.waveFormatTag = (WaveFormatEncoding) br.ReadUInt16();
       this.channels = br.ReadInt16();
       this.sampleRate = br.ReadInt32();
       this.averageBytesPerSecond = br.ReadInt32();
       this.blockAlign = br.ReadInt16();
       this.bitsPerSample = br.ReadInt16();
       this.extraSize = (short) 0;
       if (num <= 16)
     return;
       this.extraSize = br.ReadInt16();
       if ((int) this.extraSize <= num - 18)
     return;
       Console.WriteLine("Format chunk mismatch");
       this.extraSize = (short) (num - 18);
 }
예제 #40
0
		private void ReadWaveFormat(BinaryReader br, int formatChunkLength)
		{
			if (formatChunkLength < 16)
				throw new ApplicationException("Invalid WaveFormat Structure");
			waveFormatTag = (WaveFormatEncoding) br.ReadUInt16();
			channels = br.ReadInt16();
			sampleRate = br.ReadInt32();
			averageBytesPerSecond = br.ReadInt32();
			blockAlign = br.ReadInt16();
			bitsPerSample = br.ReadInt16();
			if (formatChunkLength > 16)
			{
				extraSize = br.ReadInt16();
				if (extraSize > formatChunkLength - 18)
				{
					Debug.WriteLine("Format chunk mismatch");
					extraSize = (short) (formatChunkLength - 18);
				}
			}
		}
예제 #41
0
 private void ReadWaveFormat(BinaryReader br, int formatChunkLength)
 {
     if (formatChunkLength < 16)
         throw new InvalidDataException("Invalid WaveFormat Structure");
     this.waveFormatTag = (WaveFormatEncoding)br.ReadUInt16();
     this.channels = br.ReadInt16();
     this.sampleRate = br.ReadInt32();
     this.averageBytesPerSecond = br.ReadInt32();
     this.blockAlign = br.ReadInt16();
     this.bitsPerSample = br.ReadInt16();
     if (formatChunkLength > 16)
     {
         this.extraSize = br.ReadInt16();
         if (this.extraSize != formatChunkLength - 18)
         {
             Debug.WriteLine("Format chunk mismatch");
             this.extraSize = (short)(formatChunkLength - 18);
         }
     }
 }
예제 #42
0
        protected override bool LoadAudioFile(string audioFile, DirectSound directSound)
        {
            try
            {
                // Open the wave file in binary.
                var reader = new BinaryReader(File.OpenRead(SystemConfiguration.DataFilePath + audioFile));

                // Read in the wave file header.
                chunkId = new string(reader.ReadChars(4));
                chunkSize = reader.ReadInt32();
                format = new string(reader.ReadChars(4));
                subChunkId = new string(reader.ReadChars(4));
                subChunkSize = reader.ReadInt32();
                audioFormat = (WaveFormatEncoding )reader.ReadInt16();
                numChannels = reader.ReadInt16();
                sampleRate = reader.ReadInt32();
                bytesPerSecond = reader.ReadInt32();
                blockAlign = reader.ReadInt16();
                bitsPerSample = reader.ReadInt16();
                dataChunkId = new string(reader.ReadChars(4));
                dataSize = reader.ReadInt32();

                // Check that the chunk ID is the RIFF format
                // and the file format is the WAVE format
                // and sub chunk ID is the fmt format
                // and the audio format is PCM
                // and the wave file was recorded in stereo format
                // and at a sample rate of 44.1 KHz
                // and at 16 bit format
                // and there is the data chunk header.
                // Otherwise return false.
                if (chunkId != "RIFF" || format != "WAVE" || subChunkId.Trim() != "fmt" || audioFormat != WaveFormatEncoding.Pcm || numChannels != 2 || sampleRate != 44100 || bitsPerSample != 16 || dataChunkId != "data")
                    return false;

                // Set the buffer description of the secondary sound buffer that the wave file will be loaded onto and the wave format.
                var buffer = new SoundBufferDescription();
                buffer.Flags = BufferFlags.ControlVolume;
                buffer.BufferBytes = dataSize;
                buffer.Format = new WaveFormat(44100, 16, 2);
                buffer.AlgorithmFor3D = Guid.Empty;

                // Create a temporary sound buffer with the specific buffer settings.
                _SecondaryBuffer = new SecondarySoundBuffer(directSound, buffer);

                // Read in the wave file data into the temporary buffer.
                var waveData = reader.ReadBytes(dataSize);

                // Close the reader
                reader.Close();

                // Lock the secondary buffer to write wave data into it.
                DataStream waveBufferData2;
                var waveBufferData1 = _SecondaryBuffer.Lock(0, dataSize, LockFlags.None, out waveBufferData2);

                // Copy the wave data into the buffer.
                waveBufferData1.Write(waveData, 0, dataSize);

                // Unlock the secondary buffer after the data has been written to it.
                var result = _SecondaryBuffer.Unlock(waveBufferData1, waveBufferData2);

                if (result != Result.Ok)
                    return false;
            }
            catch
            {
                return false;
            }

            return true;
        }
예제 #43
0
파일: WaveFormat.cs 프로젝트: tanis2000/FEZ
 internal void __MarshalFrom(ref WaveFormat.__PcmNative @ref)
 {
     this.waveFormatTag = @ref.waveFormatTag;
       this.channels = @ref.channels;
       this.sampleRate = @ref.sampleRate;
       this.averageBytesPerSecond = @ref.averageBytesPerSecond;
       this.blockAlign = @ref.blockAlign;
       this.bitsPerSample = @ref.bitsPerSample;
       this.extraSize = (short) 0;
 }
예제 #44
0
파일: WaveFormat.cs 프로젝트: nkast/SharpDX
 /// <summary>
 /// Creates a WaveFormat with custom members
 /// </summary>
 /// <param name="tag">The encoding</param>
 /// <param name="sampleRate">Sample Rate</param>
 /// <param name="channels">Number of channels</param>
 /// <param name="averageBytesPerSecond">Average Bytes Per Second</param>
 /// <param name="blockAlign">Block Align</param>
 /// <param name="bitsPerSample">Bits Per Sample</param>
 /// <returns></returns>
 public static WaveFormat CreateCustomFormat(WaveFormatEncoding tag, int sampleRate, int channels, int averageBytesPerSecond, int blockAlign, int bitsPerSample)
 {
     var waveFormat = new WaveFormat
                          {
                              waveFormatTag = tag,
                              channels = (short) channels,
                              sampleRate = sampleRate,
                              averageBytesPerSecond = averageBytesPerSecond,
                              blockAlign = (short) blockAlign,
                              bitsPerSample = (short) bitsPerSample,
                              extraSize = 0
                          };
     return waveFormat;
 }
예제 #45
0
 public static float[][] BytesToFloatSamples(byte[] bytes, int bytesRecorded, int bytesPerSample, WaveFormatEncoding encoding, int channelCount)
 {
     try
     {
         if (bytes == null || bytes.Length <= 0)
             return null;
         int sampleCnt = (bytesRecorded / bytesPerSample) / channelCount;
         int offset = 0;
         int sampleNum = 0;
         List<float[]> samples = new List<float[]>();
         for (int i = 0; i < channelCount; i++)
         {
             samples.Add(new float[sampleCnt]);
         }
         while (offset < bytes.Length)
         {
             for (int iChannel = 0; iChannel < channelCount; iChannel++)
             {
                 switch (bytesPerSample)
                 {
                     case 2:
                         {
                             samples[iChannel][sampleNum] = BitConverter.ToInt16(bytes, offset) / 32768f;
                             offset += 2;
                             break;
                         }
                     case 3:
                         {
                             samples[iChannel][sampleNum] = (((sbyte)bytes[offset + 2] << 16) | (bytes[offset + 1] << 8) | bytes[offset]) / 8388608f;
                             offset += 3;
                             break;
                         }
                     case 4:
                         {
                             if (encoding == WaveFormatEncoding.IeeeFloat)
                             {
                                 samples[iChannel][sampleNum] = BitConverter.ToSingle(bytes, offset);
                             }
                             else
                             {
                                 samples[iChannel][sampleNum] = BitConverter.ToInt32(bytes, offset) / (Int32.MaxValue + 1f);
                             }
                             offset += 4;
                             break;
                         }
                     default:
                         {
                             offset += bytesPerSample;
                             break;
                         }
                 }
             }
             sampleNum++;
         }
         return samples.ToArray();
     }
     catch { return null; }
 }
예제 #46
0
 public static byte[][] BytesToSampleBytes(byte[] bytes, int bytesRecorded, int bytesPerSample, WaveFormatEncoding encoding, int channelCount)
 {
     try
     {
         if (bytes == null || bytes.Length <= 0)
             return null;
         int sampleCnt = (bytesRecorded / bytesPerSample) / channelCount;
         int offset = 0;
         int sampleNum = 0;
         List<byte[]> samples = new List<byte[]>();
         for (int i = 0; i < channelCount; i++)
         {
             samples.Add(new byte[sampleCnt * bytesPerSample]);
         }
         while (offset < bytes.Length)
         {
             for (int iChannel = 0; iChannel < channelCount; iChannel++)
             {
                 switch (bytesPerSample)
                 {
                     case 2:
                         {
                             samples[iChannel][sampleNum * 2] = bytes[offset];
                             samples[iChannel][(sampleNum * 2) + 1] = bytes[offset + 1];
                             offset += 2;
                             break;
                         }
                     case 3:
                         {
                             samples[iChannel][sampleNum * 3] = bytes[offset];
                             samples[iChannel][(sampleNum * 3) + 1] = bytes[offset + 1];
                             samples[iChannel][(sampleNum * 3) + 2] = bytes[offset + 2];
                             offset += 3;
                             break;
                         }
                     case 4:
                         {
                             samples[iChannel][sampleNum * 4] = bytes[offset];
                             samples[iChannel][(sampleNum * 4) + 1] = bytes[offset + 1];
                             samples[iChannel][(sampleNum * 4) + 2] = bytes[offset + 2];
                             samples[iChannel][(sampleNum * 4) + 3] = bytes[offset + 3];
                             offset += 4;
                             break;
                         }
                     default:
                         {
                             offset += bytesPerSample;
                             break;
                         }
                 }
             }
             sampleNum++;
         }
         return samples.ToArray();
     }
     catch { return null; }
 }
예제 #47
0
 public static float[] BytesToFloatSamples(byte[] bytes, int bytesRecorded, int bytesPerSample, WaveFormatEncoding encoding)
 {
     if (bytes == null || bytes.Length <= 0)
         return null;
     float[][] samples=BytesToFloatSamples(bytes, bytesRecorded,bytesPerSample,encoding,1);
     if (samples != null && samples.Length > 0)
         return samples[0];
     else
         return null;
 }
예제 #48
0
        public uint[] GetSeekTable(uint index, out WaveFormatEncoding tag)
        {
            if (index >= bankData.EntryCount)
                throw new ArgumentOutOfRangeException("index");

            tag = 0;

            var minFormat = GetMiniWaveFormat(index);
            var seekTable = FindSeekTable(index);
            switch (minFormat.FormatTag)
            {
                case MiniWaveFormat.Tag.WMA: // xWMA is supported by XAudio 2.7 and by Xbox One
                    tag = (minFormat.BitsPerSample & 0x1) > 0 ? WaveFormatEncoding.Wmaudio3 : WaveFormatEncoding.Wmaudio2;
                    break;

                case MiniWaveFormat.Tag.XMA:
                    tag = (WaveFormatEncoding)0x166 /* WAVE_FORMAT_XMA2 */;
                    break;

                default:
                    return null;
            }

            return null;
        }
예제 #49
0
        /// <summary>
        /// Creates a new PCM format with the specified sample rate, bit depth and channels
        /// </summary>
        public WaveFormat(int rate, int bits, int channels)
        {
            if (channels < 1)
            {
                throw new ArgumentOutOfRangeException("channels", "Channels must be 1 or greater");
            }
            // minimum 16 bytes, sometimes 18 for PCM
            waveFormatTag = bits < 32 ? WaveFormatEncoding.Pcm : WaveFormatEncoding.IeeeFloat;
            this.channels = (short)channels;
            sampleRate = rate;
            bitsPerSample = (short)bits;
            extraSize = 0;

            blockAlign = (short)(channels * (bits / 8));
            averageBytesPerSecond = sampleRate * blockAlign;
        }