/// <summary> /// Write a buffer of uncompressed PCM data to the buffer. /// </summary> /// <param name="buffer">Byte array defining the buffer to write.</param> /// <param name="index">Index of first value to write</param> /// <param name="count">NUmber of byte to write. Must be multiple of PCM sample size <see cref="yeti.wma.WmaWriterConfig.Format.nBlockAlign"/></param> public override void Write(byte[] buffer, int index, int count) { if ((count % m_InputDataFormat.nBlockAlign) == 0) { INSSBuffer buff; NSSBuffer nssBuff; m_Writer.AllocateSample((uint)count, out buff); using (nssBuff = new NSSBuffer(buff)) { nssBuff.Write(buffer, index, count); nssBuff.Length = (uint)count; m_Writer.WriteSample(0, m_MsAudioTime * 10000, 0, buff); } m_MsAudioTime += ((ulong)count * 1000) / (ulong)m_InputDataFormat.nAvgBytesPerSec; } else { throw new ArgumentException(string.Format("Invalid buffer size. Buffer size must be aligned to {0} bytes.", m_InputDataFormat.nBlockAlign), "count"); } }
public void OnHeader([In, MarshalAs(UnmanagedType.Interface)] INSSBuffer pHeader) { byte[] buffer; uint length; var manBuff = pHeader as ManBuffer; if (manBuff != null) { buffer = manBuff.Buffer; length = manBuff.UsedLength; manBuff.Dispose(); } else { using (var b = new NSSBuffer(pHeader)) { length = b.Length; buffer = new byte[length]; b.Read(buffer, 0, (int)length); } } if (!m_HeaderWrote) { if (BaseStream.CanSeek) { m_HeaderPosition = BaseStream.Position; m_HeaderLength = length; } m_HeaderWrote = true; base.Write(buffer, 0, (int)length); } else if (BaseStream.CanSeek && (length == m_HeaderLength)) { long pos = BaseStream.Position; BaseStream.Position = m_HeaderPosition; BaseStream.Write(buffer, 0, (int)length); BaseStream.Position = pos; } }
public override long Seek(long offset, SeekOrigin origin) { if (CanSeek) { switch (origin) { case SeekOrigin.Current: offset += m_Position; break; case SeekOrigin.End: offset += m_Length; break; } if (offset == m_Position) { return m_Position; // :-) } if ((offset < 0) || (offset > m_Length)) { throw new ArgumentException("Offset out of range", "offset"); } if ((offset % SeekAlign) > 0) { throw new ArgumentException(string.Format("Offset must be aligned by a value of SeekAlign ({0})", SeekAlign), "offset"); } ulong sampleTime = BytePosition2SampleTime(offset); m_Reader.SetRange(sampleTime, 0); m_Position = offset; if (m_BufferReader != null) { Marshal.ReleaseComObject(m_BufferReader.Buffer); } m_BufferReader = null; return offset; } throw new NotSupportedException(); }
public void OnDataUnit([In, MarshalAs(UnmanagedType.Interface)] INSSBuffer pDataUnit) { byte[] buffer; int length; var manBuff = pDataUnit as ManBuffer; if (manBuff != null) { buffer = manBuff.Buffer; length = (int)manBuff.UsedLength; manBuff.Dispose(); } else { using (var b = new NSSBuffer(pDataUnit)) { length = (int)b.Length; buffer = new byte[length]; b.Read(buffer, 0, length); } } base.Write(buffer, 0, length); }
public override int Read(byte[] buffer, int offset, int count) { if (m_Reader != null) { int read = 0; if ((m_Length > 0) && ((m_Length - m_Position) < count)) { count = (int)(m_Length - m_Position); } if (m_BufferReader != null) { while ((m_BufferReader.Position < m_BufferReader.Length) && (read < count)) { read += m_BufferReader.Read(buffer, offset, count); } } while (read < count) { INSSBuffer sample = null; try { uint flags = 0; ulong duration = 0; ulong sampleTime = 0; m_Reader.GetNextSample(m_OuputStream, out sample, out sampleTime, out duration, out flags, out m_OutputNumber, out m_OuputStream); } catch (COMException e) { if (e.ErrorCode == WM.NS_E_NO_MORE_SAMPLES) { //No more samples if (m_OutputFormat.wBitsPerSample == 8) { while (read < count) { buffer[offset + read] = 0x80; read++; } } else { Array.Clear(buffer, offset + read, count - read); read = count; } break; } else { throw (e); } } if (m_BufferReader != null) { m_BufferReader.Reset(sample); } else { m_BufferReader = new NSSBuffer(sample); } read += m_BufferReader.Read(buffer, offset + read, count - read); } if ((m_BufferReader != null) && (m_BufferReader.Position >= m_BufferReader.Length)) { m_BufferReader.Dispose(); m_BufferReader = null; } m_Position += read; return read; } throw new ObjectDisposedException(this.ToString()); }