Skip to content

Commit

Permalink
adding a TotalTime property to WaveFileWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
markheath committed Nov 16, 2017
1 parent 2596c8e commit d776728
Showing 1 changed file with 16 additions and 30 deletions.
46 changes: 16 additions & 30 deletions NAudio/Wave/WaveOutputs/WaveFileWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,50 +142,37 @@ private bool HasFactChunk()
/// <summary>
/// The wave file name or null if not applicable
/// </summary>
public string Filename
{
get { return filename; }
}
public string Filename => filename;

/// <summary>
/// Number of bytes of audio in the data chunk
/// </summary>
public override long Length
{
get { return dataChunkSize; }
}
public override long Length => dataChunkSize;

/// <summary>
/// Total time (calculated from Length and average bytes per second)
/// </summary>
public TimeSpan TotalTime => TimeSpan.FromSeconds((double)Length / WaveFormat.AverageBytesPerSecond);

/// <summary>
/// WaveFormat of this wave file
/// </summary>
public WaveFormat WaveFormat
{
get { return format; }
}
public WaveFormat WaveFormat => format;

/// <summary>
/// Returns false: Cannot read from a WaveFileWriter
/// </summary>
public override bool CanRead
{
get { return false; }
}
public override bool CanRead => false;

/// <summary>
/// Returns true: Can write to a WaveFileWriter
/// </summary>
public override bool CanWrite
{
get { return true; }
}
public override bool CanWrite => true;

/// <summary>
/// Returns false: Cannot seek within a WaveFileWriter
/// </summary>
public override bool CanSeek
{
get { return false; }
}
public override bool CanSeek => false;

/// <summary>
/// Read is not supported for a WaveFileWriter
Expand Down Expand Up @@ -217,8 +204,8 @@ public override void SetLength(long value)
/// </summary>
public override long Position
{
get { return dataChunkSize; }
set { throw new InvalidOperationException("Repositioning a WaveFileWriter is not supported"); }
get => dataChunkSize;
set => throw new InvalidOperationException("Repositioning a WaveFileWriter is not supported");
}

/// <summary>
Expand All @@ -242,7 +229,7 @@ public void WriteData(byte[] data, int offset, int count)
public override void Write(byte[] data, int offset, int count)
{
if (outStream.Length + count > UInt32.MaxValue)
throw new ArgumentException("WAV file too large", "count");
throw new ArgumentException("WAV file too large", nameof(count));
outStream.Write(data, offset, count);
dataChunkSize += count;
}
Expand Down Expand Up @@ -323,7 +310,7 @@ public void WriteSamples(short[] samples, int offset, int count)
{
// 16 bit PCM data
if (WaveFormat.BitsPerSample == 16)
{
{
for (int sample = 0; sample < count; sample++)
{
writer.Write(samples[sample + offset]);
Expand All @@ -333,10 +320,9 @@ public void WriteSamples(short[] samples, int offset, int count)
// 24 bit PCM data
else if (WaveFormat.BitsPerSample == 24)
{
byte[] value;
for (int sample = 0; sample < count; sample++)
{
value = BitConverter.GetBytes(UInt16.MaxValue * (Int32)samples[sample + offset]);
var value = BitConverter.GetBytes(UInt16.MaxValue * (Int32)samples[sample + offset]);
value24[0] = value[1];
value24[1] = value[2];
value24[2] = value[3];
Expand Down

0 comments on commit d776728

Please sign in to comment.