//
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE. IT CAN BE DISTRIBUTED FREE OF CHARGE AS LONG AS THIS HEADER
// REMAINS UNCHANGED.
//
// Email: yetiicb@hotmail.com
//
// Copyright (C) 2002-2003 Idael Cardoso.
//
using System;
using System.IO;
using BSE.Platten.Ripper.Properties;
using System.Globalization;
namespace BSE.Platten.Ripper
{
/// <summary>
///
/// </summary>
public class WaveWriter : AudioWriter
{
private const uint WaveHeaderSize = 38;
private const uint WaveFormatSize = 18;
private uint m_audioDataSize;
private uint m_writtenBytes;
private bool m_bClosed;
private CWaveFormat m_waveFormat;
public WaveWriter(Stream output, CWaveFormat waveFormat, uint audioDataSize)
: base(output, waveFormat)
{
this.m_audioDataSize = audioDataSize;
this.m_waveFormat = waveFormat;
WriteWaveHeader();
}
public WaveWriter(Stream output, CWaveFormat waveFormat)
: base(output, waveFormat)
{
if (!OutStream.CanSeek )
{
throw new ArgumentException("output",Resources.IDS_WaveWriterArgumentException);
}
this.m_waveFormat = waveFormat;
OutStream.Seek(WaveHeaderSize+8, SeekOrigin.Current);
}
protected void WriteWaveHeader()
{
Write(new char[]{'R', 'I', 'F', 'F'});
Write(this.m_audioDataSize + WaveHeaderSize);
Write(new char[]{'W', 'A', 'V', 'E'});
Write(new char[]{'f', 'm', 't', ' '});
Write(WaveFormatSize);
Write(this.m_waveFormat.wFormatTag);
Write(this.m_waveFormat.nChannels);
Write(this.m_waveFormat.nSamplesPerSec);
Write(this.m_waveFormat.nAvgBytesPerSec);
Write(this.m_waveFormat.nBlockAlign);
Write(this.m_waveFormat.wBitsPerSample);
Write(this.m_waveFormat.cbSize);
Write(new char[]{'d', 'a', 't', 'a'});
Write(this.m_audioDataSize);
this.m_writtenBytes -= (WaveHeaderSize+8);
}
public override void Close()
{
if (!this.m_bClosed)
{
if ( this.m_audioDataSize == 0 )
{
Seek(-(int)this.m_writtenBytes - (int)WaveHeaderSize - 8, SeekOrigin.Current);
this.m_audioDataSize = this.m_writtenBytes;
WriteWaveHeader();
}
}
this.m_bClosed = true;
base.Close ();
}
/// <summary>
/// The written string is not a length-prefixed. <c>System.IO.BynaryWriter.Write</c> writes a length-prefixed string, <see cref="System.IO.BynaryWriter.Write"/> for details.
/// </summary>
/// <param name="value">String to write</param>
public override void Write(string value)
{
if (value == null)
{
throw new ArgumentNullException(
string.Format(
CultureInfo.InvariantCulture,
Resources.IDS_ArgumentNullException,
"value"));
}
Write(value.ToCharArray());
}
public override void Write(float value)
{
base.Write (value);
this.m_writtenBytes += 4;
}
public override void Write(ulong value)
{
base.Write (value);
this.m_writtenBytes += 8;
}
public override void Write(long value)
{
base.Write (value);
this.m_writtenBytes += 8;
}
public override void Write(uint value)
{
base.Write (value);
this.m_writtenBytes += 4;
}
public override void Write(int value)
{
base.Write (value);
this.m_writtenBytes += 4;
}
public override void Write(ushort value)
{
base.Write (value);
this.m_writtenBytes += 2;
}
public override void Write(short value)
{
base.Write (value);
this.m_writtenBytes += 2;
}
public override void Write(decimal value)
{
base.Write (value);
this.m_writtenBytes += 16;
}
public override void Write(double value)
{
base.Write (value);
this.m_writtenBytes += 8;
}
public override void Write(char[] chars, int index, int count)
{
base.Write (chars, index, count);
this.m_writtenBytes += (uint)count;
}
public override void Write(char[] chars)
{
if (chars == null)
{
throw new ArgumentNullException(
string.Format(
CultureInfo.InvariantCulture,
Resources.IDS_ArgumentNullException,
"chars"));
}
base.Write (chars);
this.m_writtenBytes += (uint)chars.Length;
}
public override void Write(char ch)
{
base.Write (ch);
this.m_writtenBytes += 1;
}
public override void Write(byte[] buffer, int index, int count)
{
base.Write (buffer, index, count);
this.m_writtenBytes += (uint)count;
}
public override void Write(byte[] buffer)
{
if (buffer == null)
{
throw new ArgumentNullException(
string.Format(
CultureInfo.InvariantCulture,
Resources.IDS_ArgumentNullException,
"buffer"));
}
base.Write (buffer);
this.m_writtenBytes += (uint)buffer.Length;
}
public override void Write(sbyte value)
{
base.Write (value);
this.m_writtenBytes += 1;
}
public override void Write(byte value)
{
base.Write (value);
this.m_writtenBytes += 1;
}
public override void Write(bool value)
{
base.Write (value);
this.m_writtenBytes += 1;
}
}
}