WAVWriter & LossyWAVSharp now support writing to stdout

This commit is contained in:
chudov
2008-11-22 16:40:48 +00:00
parent b5008007cf
commit 0cb204d2bc
4 changed files with 78 additions and 51 deletions

View File

@@ -505,15 +505,19 @@ namespace AudioCodecsDotNet
public class WAVWriter : IAudioDest public class WAVWriter : IAudioDest
{ {
FileStream _IO; Stream _IO;
BinaryWriter _bw; BinaryWriter _bw;
int _bitsPerSample, _channelCount, _sampleRate, _blockAlign; int _bitsPerSample, _channelCount, _sampleRate, _blockAlign;
long _sampleLen; long _sampleLen;
string _path; string _path;
private byte[] _sampleBuffer; private byte[] _sampleBuffer;
long hdrLen = 44; long hdrLen = 0;
bool _headersWritten = false;
long _finalSampleCount;
List<byte[]> _chunks = null;
List<uint> _chunkFCCs = null;
public WAVWriter(string path, int bitsPerSample, int channelCount, int sampleRate) public WAVWriter(string path, int bitsPerSample, int channelCount, int sampleRate, Stream IO)
{ {
_path = path; _path = path;
_bitsPerSample = bitsPerSample; _bitsPerSample = bitsPerSample;
@@ -521,10 +525,8 @@ namespace AudioCodecsDotNet
_sampleRate = sampleRate; _sampleRate = sampleRate;
_blockAlign = _channelCount * ((_bitsPerSample + 7) / 8); _blockAlign = _channelCount * ((_bitsPerSample + 7) / 8);
_IO = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read); _IO = IO != null ? IO : new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read);
_bw = new BinaryWriter(_IO); _bw = new BinaryWriter(_IO);
WriteHeaders();
} }
public bool SetTags(NameValueCollection tags) public bool SetTags(NameValueCollection tags)
@@ -536,16 +538,14 @@ namespace AudioCodecsDotNet
{ {
if (_sampleLen > 0) if (_sampleLen > 0)
throw new Exception("data already written, no chunks allowed"); throw new Exception("data already written, no chunks allowed");
const uint fccData = 0x61746164; if (_chunks == null)
_bw.Seek((int)hdrLen - 8, SeekOrigin.Begin); {
_bw.Write(fcc); _chunks = new List<byte[]>();
_bw.Write((uint)data.Length); _chunkFCCs = new List<uint>();
_bw.Write(data); }
if ((data.Length & 1) != 0) _chunkFCCs.Add(fcc);
_bw.Write((byte) 0); _chunks.Add(data);
_bw.Write(fccData); hdrLen += 8 + data.Length + (data.Length & 1);
_bw.Write((uint)0);
hdrLen += data.Length + (data.Length & 1) + 8;
} }
private void WriteHeaders() private void WriteHeaders()
@@ -555,15 +555,20 @@ namespace AudioCodecsDotNet
const uint fccFormat = 0x20746D66; const uint fccFormat = 0x20746D66;
const uint fccData = 0x61746164; const uint fccData = 0x61746164;
_bw.Write(fccRIFF); bool wavex = _bitsPerSample != 16 && _bitsPerSample != 24;
_bw.Write((uint)0);
_bw.Write(fccWAVE);
hdrLen += 36 + (wavex ? 24 : 0) + 8;
uint dataLen = (uint) (_finalSampleCount * _blockAlign);
uint dataLenPadded = dataLen + (dataLen & 1);
_bw.Write(fccRIFF);
_bw.Write((uint)(dataLenPadded + hdrLen - 8));
_bw.Write(fccWAVE);
_bw.Write(fccFormat); _bw.Write(fccFormat);
if (_bitsPerSample != 16 && _bitsPerSample != 24) if (wavex)
{ {
_bw.Write((uint)40); _bw.Write((uint)40);
//_bw.Write((uint)16);
_bw.Write((ushort)0xfffe); // WAVEX follows _bw.Write((ushort)0xfffe); // WAVEX follows
} }
else else
@@ -576,9 +581,7 @@ namespace AudioCodecsDotNet
_bw.Write((uint)(_sampleRate * _blockAlign)); _bw.Write((uint)(_sampleRate * _blockAlign));
_bw.Write((ushort)_blockAlign); _bw.Write((ushort)_blockAlign);
_bw.Write((ushort)((_bitsPerSample+7)/8*8)); _bw.Write((ushort)((_bitsPerSample+7)/8*8));
hdrLen = 36; if (wavex)
if (_bitsPerSample != 16 && _bitsPerSample != 24)
{ {
_bw.Write((ushort)22); // length of WAVEX structure _bw.Write((ushort)22); // length of WAVEX structure
_bw.Write((ushort)_bitsPerSample); _bw.Write((ushort)_bitsPerSample);
@@ -595,40 +598,49 @@ namespace AudioCodecsDotNet
_bw.Write((byte)0x38); _bw.Write((byte)0x38);
_bw.Write((byte)0x9b); _bw.Write((byte)0x9b);
_bw.Write((byte)0x71); _bw.Write((byte)0x71);
}
hdrLen += 24; if (_chunks != null)
for (int i = 0; i < _chunks.Count; i++)
{
_bw.Write(_chunkFCCs[i]);
_bw.Write((uint)_chunks[i].Length);
_bw.Write(_chunks[i]);
if ((_chunks[i].Length & 1) != 0)
_bw.Write((byte)0);
} }
_bw.Write(fccData); _bw.Write(fccData);
_bw.Write((uint)0); _bw.Write(dataLen);
hdrLen += 8;
_headersWritten = true;
} }
public void Close() public void Close()
{
if (_finalSampleCount == 0)
{ {
const long maxFileSize = 0x7FFFFFFEL; const long maxFileSize = 0x7FFFFFFEL;
long dataLen, dataLenPadded; long dataLen = _sampleLen * _blockAlign;
dataLen = _sampleLen * _blockAlign;
if ((dataLen & 1) == 1) if ((dataLen & 1) == 1)
_bw.Write((byte)0); _bw.Write((byte)0);
if (dataLen + hdrLen > maxFileSize) if (dataLen + hdrLen > maxFileSize)
dataLen = ((maxFileSize - hdrLen) / _blockAlign) * _blockAlign; dataLen = ((maxFileSize - hdrLen) / _blockAlign) * _blockAlign;
long dataLenPadded = dataLen + (dataLen & 1);
dataLenPadded = dataLen + (dataLen & 1);
_bw.Seek(4, SeekOrigin.Begin); _bw.Seek(4, SeekOrigin.Begin);
_bw.Write((uint)(dataLenPadded + hdrLen - 8)); _bw.Write((uint)(dataLenPadded + hdrLen - 8));
_bw.Seek((int)hdrLen - 4, SeekOrigin.Begin); _bw.Seek((int)hdrLen - 4, SeekOrigin.Begin);
_bw.Write((uint)dataLen); _bw.Write((uint)dataLen);
}
_bw.Close(); _bw.Close();
_bw = null; _bw = null;
_IO = null; _IO = null;
if (_finalSampleCount != 0 && _sampleLen != _finalSampleCount)
throw new Exception("Samples written differs from the expected sample count.");
} }
public void Delete() public void Delete()
@@ -649,7 +661,7 @@ namespace AudioCodecsDotNet
public long FinalSampleCount public long FinalSampleCount
{ {
set { } set { _finalSampleCount = value; }
} }
public long BlockSize public long BlockSize
@@ -666,6 +678,8 @@ namespace AudioCodecsDotNet
{ {
if (sampleCount == 0) if (sampleCount == 0)
return; return;
if (!_headersWritten)
WriteHeaders();
if (_sampleBuffer == null || _sampleBuffer.Length < sampleCount * _blockAlign) if (_sampleBuffer == null || _sampleBuffer.Length < sampleCount * _blockAlign)
_sampleBuffer = new byte[sampleCount * _blockAlign]; _sampleBuffer = new byte[sampleCount * _blockAlign];
AudioSamples.FLACSamplesToBytes(buff, 0, _sampleBuffer, 0, AudioSamples.FLACSamplesToBytes(buff, 0, _sampleBuffer, 0,

View File

@@ -51,7 +51,7 @@ namespace CUEToolsLib {
IAudioDest dest; IAudioDest dest;
switch (extension) { switch (extension) {
case ".wav": case ".wav":
dest = new WAVWriter(path, bitsPerSample, channelCount, sampleRate); dest = new WAVWriter(path, bitsPerSample, channelCount, sampleRate, null);
break; break;
#if !MONO #if !MONO
case ".flac": case ".flac":

View File

@@ -1,6 +1,10 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
<StartArguments>-</StartArguments> <StartArguments>"06 - Garden of Dreams - Mr. Hope Goes to Wall Street.wav" --stdout</StartArguments>
<StartWorkingDirectory>C:\Work\cuetoolsnet\bin\x64\Release\</StartWorkingDirectory>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
<StartArguments>"06 - Garden of Dreams - Mr. Hope Goes to Wall Street.wav" --stdout</StartArguments>
<StartWorkingDirectory>C:\Work\cuetoolsnet\bin\x64\Release\</StartWorkingDirectory> <StartWorkingDirectory>C:\Work\cuetoolsnet\bin\x64\Release\</StartWorkingDirectory>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@@ -28,10 +28,12 @@ namespace LossyWAVSharp
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("Advanced Options:"); Console.WriteLine("Advanced Options:");
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("- if filename=\"-\" then WAV input is taken from STDIN.");
Console.WriteLine("-q, --quality <n> quality preset (10=highest quality, 0=lowest bitrate;"); Console.WriteLine("-q, --quality <n> quality preset (10=highest quality, 0=lowest bitrate;");
Console.WriteLine(" default = --standard = 5; --insane = 10; --extreme = 7.5;"); Console.WriteLine(" default = --standard = 5; --insane = 10; --extreme = 7.5;");
Console.WriteLine(" --portable = 2.5)"); Console.WriteLine(" --portable = 2.5)");
Console.WriteLine("-N --stdinname <t> pseudo filename to use when input from STDIN."); Console.WriteLine("-N --stdinname <t> pseudo filename to use when input from STDIN.");
Console.WriteLine(" --stdout write processed WAV output to STDOUT.");
} }
static void Main(string[] args) static void Main(string[] args)
@@ -50,6 +52,7 @@ namespace LossyWAVSharp
string stdinName = null; string stdinName = null;
double quality = 5.0; double quality = 5.0;
bool createCorrection = false; bool createCorrection = false;
bool toStdout = false;
for (int arg = 1; arg < args.Length; arg++) for (int arg = 1; arg < args.Length; arg++)
{ {
bool ok = true; bool ok = true;
@@ -67,6 +70,8 @@ namespace LossyWAVSharp
stdinName = args[arg]; stdinName = args[arg];
else if ((args[arg] == "-q" || args[arg] == "--quality") && ++arg < args.Length) else if ((args[arg] == "-q" || args[arg] == "--quality") && ++arg < args.Length)
ok = double.TryParse(args[arg], out quality); ok = double.TryParse(args[arg], out quality);
else if (args[arg] == "--stdout")
toStdout = true;
else else
ok = false; ok = false;
if (!ok) if (!ok)
@@ -75,20 +80,22 @@ namespace LossyWAVSharp
return; return;
} }
} }
//Stream stdout = Console.OpenStandardOutput();
DateTime start = DateTime.Now; DateTime start = DateTime.Now;
TimeSpan lastPrint = TimeSpan.FromMilliseconds(0); TimeSpan lastPrint = TimeSpan.FromMilliseconds(0);
#if !DEBUG
try try
#endif
{ {
WAVReader audioSource = new WAVReader(sourceFile, (sourceFile == "-" ? Console.OpenStandardInput() : null)); WAVReader audioSource = new WAVReader(sourceFile, (sourceFile == "-" ? Console.OpenStandardInput() : null));
if (sourceFile == "-" && stdinName != null) sourceFile = stdinName; if (sourceFile == "-" && stdinName != null) sourceFile = stdinName;
WAVWriter audioDest = new WAVWriter(Path.ChangeExtension(sourceFile, ".lossy.wav"), audioSource.BitsPerSample, audioSource.ChannelCount, audioSource.SampleRate); WAVWriter audioDest = new WAVWriter(Path.ChangeExtension(sourceFile, ".lossy.wav"), audioSource.BitsPerSample, audioSource.ChannelCount, audioSource.SampleRate, toStdout ? Console.OpenStandardOutput() : null);
WAVWriter lwcdfDest = createCorrection ? new WAVWriter(Path.ChangeExtension(sourceFile, ".lwcdf.wav"), audioSource.BitsPerSample, audioSource.ChannelCount, audioSource.SampleRate) : null; WAVWriter lwcdfDest = createCorrection ? new WAVWriter(Path.ChangeExtension(sourceFile, ".lwcdf.wav"), audioSource.BitsPerSample, audioSource.ChannelCount, audioSource.SampleRate, null) : null;
LossyWAVWriter lossyWAV = new LossyWAVWriter(audioDest, lwcdfDest, audioSource.BitsPerSample, audioSource.ChannelCount, audioSource.SampleRate, quality); LossyWAVWriter lossyWAV = new LossyWAVWriter(audioDest, lwcdfDest, audioSource.BitsPerSample, audioSource.ChannelCount, audioSource.SampleRate, quality);
int[,] buff = new int[0x1000, audioSource.ChannelCount]; int[,] buff = new int[0x1000, audioSource.ChannelCount];
Console.WriteLine("Filename : {0}", sourceFile); Console.WriteLine("Filename : {0}", sourceFile);
Console.WriteLine("File Info : {0}kHz; {1} channel; {2} bit; {3}", audioSource.SampleRate, audioSource.ChannelCount, audioSource.BitsPerSample, TimeSpan.FromSeconds(audioSource.Length * 1.0 / audioSource.SampleRate)); Console.WriteLine("File Info : {0}kHz; {1} channel; {2} bit; {3}", audioSource.SampleRate, audioSource.ChannelCount, audioSource.BitsPerSample, TimeSpan.FromSeconds(audioSource.Length * 1.0 / audioSource.SampleRate));
lossyWAV.FinalSampleCount = (long) audioSource.Length;
do do
{ {
@@ -119,12 +126,14 @@ namespace LossyWAVSharp
audioSource.Close(); audioSource.Close();
lossyWAV.Close(); lossyWAV.Close();
} }
#if !DEBUG
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("Error: {0}", ex.Message); Console.WriteLine("Error: {0}", ex.Message);
Console.WriteLine("{0}", ex.StackTrace); //Console.WriteLine("{0}", ex.StackTrace);
} }
#endif
} }
} }
} }