Пример #1
0
    /// <summary>
    /// Open the media.
    /// Create the structures.
    /// </summary>
    protected override void OpenMediaAsync() {
      header = WaveFormatExtensible.ReadHeader(stream);
      header.ValidateWaveFormat();

      sampleSize = (long)header.Channels * header.BitsPerSample / 8 * numSamples;
      startPosition = currentPosition = stream.Position;
      pcmDataLen = stream.Length - startPosition;
      duration = header.AudioDurationFromDataLen(pcmDataLen);

      // Init
      Dictionary<MediaStreamAttributeKeys, string> streamAttributes = new Dictionary<MediaStreamAttributeKeys, string>();
      Dictionary<MediaSourceAttributesKeys, string> sourceAttributes = new Dictionary<MediaSourceAttributesKeys, string>();
      List<MediaStreamDescription> availableStreams = new List<MediaStreamDescription>();

      // Stream Description
      streamAttributes[MediaStreamAttributeKeys.CodecPrivateData] = header.ToHexString();
      MediaStreamDescription msd = new MediaStreamDescription(MediaStreamType.Audio, streamAttributes);

      this.audioDesc = msd;
      availableStreams.Add(this.audioDesc);

      sourceAttributes[MediaSourceAttributesKeys.Duration] = duration.ToString();
      ReportOpenMediaCompleted(sourceAttributes, availableStreams);
    }
Пример #2
0
 public bool Eq(WaveFormatExtensible fmt) {
   return BitsPerSample == fmt.BitsPerSample && Channels == fmt.Channels && SamplesPerSec == fmt.SamplesPerSec;
 }
Пример #3
0
    public static WaveFormatExtensible ReadHeader(Stream m_Stream/*, out int DataLength, out long DataPos*/) {
      BinaryReader Reader = new BinaryReader(m_Stream);
      if (ReadChunk(Reader) != "RIFF") throw new Exception("Invalid RIFF");

      Reader.ReadInt32(); // File length minus first 8 bytes of RIFF description, we don't use it

      if (ReadChunk(Reader) != "WAVE") throw new Exception("Invalid WAVE");

      if (ReadChunk(Reader) != "fmt ") throw new Exception("Invalid fmt");

      int FormatLength = Reader.ReadInt32();
      if (FormatLength != WaveFormatSize) throw new Exception("Invalid FormatLength");

      WaveFormatExtensible Format = new WaveFormatExtensible(); // initialize to any format
      Format.FormatTag = Reader.ReadInt16();
      Format.Channels = Reader.ReadInt16();
      Format.SamplesPerSec = Reader.ReadInt32();
      Format.AverageBytesPerSecond = Reader.ReadInt32();
      Format.BlockAlign = Reader.ReadInt16();
      Format.BitsPerSample = Reader.ReadInt16();
      if (ReadChunk(Reader) != "data") throw new Exception("Invalid data chunk");
      if (m_Stream.Position >= m_Stream.Length) throw new Exception("Missing DataLen field");
      Format.DataLen = Reader.ReadInt32();
      if (m_Stream.Length != Format.DataLen + RIFFHeaderSize) throw new Exception("Wrong WAV stream size");

      return Format;
    }
Пример #4
0
 public static void WriteHeader(Stream str, WaveFormatExtensible fmt, uint dataLen) {
   WriteHeader(str, fmt.Channels, fmt.BitsPerSample, (uint)fmt.SamplesPerSec, dataLen);
 }