/// <summary> /// Helper function to retrieve a WaveFormat structure from a pointer /// </summary> /// <param name="pointer">Pointer to the WaveFormat rawdata</param> /// <returns>WaveFormat structure</returns> public unsafe static WaveFormat MarshalFrom(IntPtr pointer) { if (pointer == IntPtr.Zero) { return(null); } WaveFormat.__PcmNative _PcmNative = *(WaveFormat.__PcmNative *)((void *)pointer); WAVE_FORMAT waveFormatEncoding = _PcmNative.waveFormatTag; if (_PcmNative.channels <= 2 && (waveFormatEncoding == WAVE_FORMAT.PCM || waveFormatEncoding == WAVE_FORMAT.IEEE_FLOAT || waveFormatEncoding == WAVE_FORMAT.WMAUDIO2 || waveFormatEncoding == WAVE_FORMAT.WMAUDIO3)) { WaveFormat waveFormat = new WaveFormat(); waveFormat.__MarshalFrom(ref _PcmNative); return(waveFormat); } if (waveFormatEncoding == WAVE_FORMAT.EXTENSIBLE) { WaveFormatExtensible waveFormatExtensible = new WaveFormatExtensible(); waveFormatExtensible.__MarshalFrom(ref *(WaveFormatExtensible.__Native *)((void *)pointer)); return(waveFormatExtensible); } if (waveFormatEncoding == WAVE_FORMAT.ADPCM) { WaveFormatAdpcm waveFormatAdpcm = new WaveFormatAdpcm(); waveFormatAdpcm.__MarshalFrom(ref *(WaveFormatAdpcm.__Native *)((void *)pointer)); return(waveFormatAdpcm); } throw new InvalidOperationException(string.Format("Unsupported WaveFormat [{0}]", new object[] { waveFormatEncoding })); }
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 = 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(WAVE_FORMAT 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 }); }
/// <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"); } this.waveFormatTag = ((bits < 32) ? WAVE_FORMAT.PCM : WAVE_FORMAT.IEEE_FLOAT); 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; }
/// <summary> /// Reports this WaveFormat as a string /// </summary> /// <returns>String describing the wave format</returns> public override string ToString() { WAVE_FORMAT waveFormatEncoding = this.waveFormatTag; if (waveFormatEncoding == WAVE_FORMAT.EXTENSIBLE || waveFormatEncoding == WAVE_FORMAT.PCM) { return(string.Format( CultureInfo.InvariantCulture, "{0} bit PCM: {1}kHz {2} channels", new object[] { this.bitsPerSample, this.sampleRate / 1000, this.channels })); } return(this.waveFormatTag.ToString()); }
/// <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 num = br.ReadInt32(); if (num < 16) { throw new InvalidDataException("Invalid WaveFormat Structure"); } this.waveFormatTag = (WAVE_FORMAT)br.ReadUInt16(); this.channels = br.ReadInt16(); this.sampleRate = br.ReadInt32(); this.averageBytesPerSecond = br.ReadInt32(); this.blockAlign = br.ReadInt16(); this.bitsPerSample = br.ReadInt16(); this.extraSize = 0; if (num > 16) { this.extraSize = br.ReadInt16(); if ((int)this.extraSize > num - 18) { this.extraSize = (short)(num - 18); } } }
public static WAVEFORMATEX CreateLinearPcmFormat(WAVE_FORMAT format) { switch (format) { case WAVE_FORMAT.WAVE_FORMAT_1M08: return CreateLinearPcmFormat(11025L, 8, 1); case WAVE_FORMAT.WAVE_FORMAT_1S08: return CreateLinearPcmFormat(11025L, 8, 2); case WAVE_FORMAT.WAVE_FORMAT_1M16: return CreateLinearPcmFormat(11025L, 16, 1); case WAVE_FORMAT.WAVE_FORMAT_1S16: return CreateLinearPcmFormat(11025L, 16, 2); case WAVE_FORMAT.WAVE_FORMAT_2M08: return CreateLinearPcmFormat(22050L, 8, 1); case WAVE_FORMAT.WAVE_FORMAT_2S08: return CreateLinearPcmFormat(22050L, 8, 2); case WAVE_FORMAT.WAVE_FORMAT_2M16: return CreateLinearPcmFormat(22050L, 16, 1); case WAVE_FORMAT.WAVE_FORMAT_2S16: return CreateLinearPcmFormat(22050L, 16, 2); case WAVE_FORMAT.WAVE_FORMAT_4M08: return CreateLinearPcmFormat(44100L, 8, 1); case WAVE_FORMAT.WAVE_FORMAT_4S08: return CreateLinearPcmFormat(44100L, 8, 2); case WAVE_FORMAT.WAVE_FORMAT_4M16: return CreateLinearPcmFormat(44100L, 16, 1); case WAVE_FORMAT.WAVE_FORMAT_4S16: return CreateLinearPcmFormat(44100L, 16, 2); case WAVE_FORMAT.WAVE_FORMAT_48M08: return CreateLinearPcmFormat(48000L, 8, 1); case WAVE_FORMAT.WAVE_FORMAT_48S08: return CreateLinearPcmFormat(48000L, 8, 2); case WAVE_FORMAT.WAVE_FORMAT_48M16: return CreateLinearPcmFormat(48000L, 16, 1); case WAVE_FORMAT.WAVE_FORMAT_48S16: return CreateLinearPcmFormat(48000L, 16, 2); case WAVE_FORMAT.WAVE_FORMAT_96M08: return CreateLinearPcmFormat(96000L, 8, 1); case WAVE_FORMAT.WAVE_FORMAT_96S08: return CreateLinearPcmFormat(96000L, 8, 2); case WAVE_FORMAT.WAVE_FORMAT_96M16: return CreateLinearPcmFormat(96000L, 16, 1); case WAVE_FORMAT.WAVE_FORMAT_96S16: return CreateLinearPcmFormat(96000L, 16, 2); default: throw new NotSupportedException("unsupported format"); } }