Browse Source

better output of chunk data for WaveFileInspector

pull/1/head
Mark Heath 14 years ago
parent
commit
4acac968f0
  1. 59
      AudioFileInspector/FileInspectors/WaveFileInspector.cs
  2. 1
      NAudio/NAudio.csproj
  3. 55
      NAudio/Utils/ByteArrayExtensions.cs

59
AudioFileInspector/FileInspectors/WaveFileInspector.cs

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Text;
using NAudio.Wave;
using NAudio.Utils;
using System.Diagnostics;
using System.ComponentModel.Composition;
@ -53,21 +54,67 @@ namespace AudioFileInspector
case "strc":
DescribeStrc(stringBuilder, data);
break;
case "bext":
DescribeBext(stringBuilder, data);
break;
case "iXML":
stringBuilder.Append(UTF8Encoding.UTF8.GetString(data));
break;
default:
{
int n = 0;
foreach (byte b in data)
if (ByteArrayExtensions.IsEntirelyNull(data))
{
stringBuilder.AppendFormat("{0:X2} ", b);
if (++n % 8 == 0)
stringBuilder.Append("\r\n");
stringBuilder.AppendFormat("{0} null bytes\r\n", data.Length);
}
else
{
stringBuilder.AppendFormat("{0}\r\n", ByteArrayExtensions.DescribeAsHex(data," ",32));
}
stringBuilder.Append("\r\n");
}
break;
}
}
private static void DescribeBext(StringBuilder sb, byte[] data)
{
int offset = 0;
sb.AppendFormat("Description: {0}\r\n", ByteArrayExtensions.DecodeAsString(data, 0, 256, ASCIIEncoding.ASCII));
offset += 256;
sb.AppendFormat("Originator: {0}\r\n", ByteArrayExtensions.DecodeAsString(data, offset, 32, ASCIIEncoding.ASCII));
offset += 32;
sb.AppendFormat("Originator Reference: {0}\r\n", ByteArrayExtensions.DecodeAsString(data, offset, 32, ASCIIEncoding.ASCII));
offset += 32;
sb.AppendFormat("Origination Date: {0}\r\n", ByteArrayExtensions.DecodeAsString(data, offset, 10, ASCIIEncoding.ASCII));
offset += 10;
sb.AppendFormat("Origination Time: {0}\r\n", ByteArrayExtensions.DecodeAsString(data, offset, 8, ASCIIEncoding.ASCII));
offset += 8;
sb.AppendFormat("Time Reference Low: {0}\r\n", BitConverter.ToUInt32(data, offset));
offset += 4;
sb.AppendFormat("Time Reference High: {0}\r\n", BitConverter.ToUInt32(data, offset));
offset += 4;
sb.AppendFormat("Version: {0}\r\n", BitConverter.ToUInt16(data, offset));
offset += 2;
//byte[] smpteumid = 64 bytes;
offset += 64;
sb.AppendFormat("Loudness Value: {0}\r\n", BitConverter.ToUInt16(data, offset));
offset += 2;
sb.AppendFormat("Loudness Range: {0}\r\n", BitConverter.ToUInt16(data, offset));
offset += 2;
sb.AppendFormat("Max True Peak Level: {0}\r\n", BitConverter.ToUInt16(data, offset));
offset += 2;
sb.AppendFormat("Max Momentary Loudness: {0}\r\n", BitConverter.ToUInt16(data, offset));
offset += 2;
sb.AppendFormat("Max short term loudness: {0}\r\n", BitConverter.ToUInt16(data, offset));
offset += 2;
//byte[] reserved = 180 bytes;
offset += 180;
sb.AppendFormat("Coding History: {0}\r\n", ByteArrayExtensions.DecodeAsString(data, offset, data.Length-offset, ASCIIEncoding.ASCII));
}
private static void DescribeStrc(StringBuilder stringBuilder, byte[] data)
{
// First 28 bytes are header

1
NAudio/NAudio.csproj

@ -99,6 +99,7 @@
<Compile Include="CoreAudioApi\Interfaces\IAudioRenderClient.cs" />
<Compile Include="CoreAudioApi\MMDeviceCollection.cs" />
<Compile Include="CoreAudioApi\PropertyKeys.cs" />
<Compile Include="Utils\ByteArrayExtensions.cs" />
<Compile Include="Wave\SampleProviders\MultiplexingSampleProvider.cs" />
<Compile Include="Wave\SampleProviders\SampleProviderConverters.cs" />
<Compile Include="Wave\WaveInputs\WasapiCapture.cs" />

55
NAudio/Utils/ByteArrayExtensions.cs

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace NAudio.Utils
{
/// <summary>
/// these will become extension methods once we move to .NET 3.5
/// </summary>
public static class ByteArrayExtensions
{
/// <summary>
/// Checks if the buffer passed in is entirely full of nulls
/// </summary>
public static bool IsEntirelyNull(byte[] buffer)
{
foreach (byte b in buffer)
if (b != 0)
return false;
return true;
}
/// <summary>
/// Converts to a string containing the buffer described in hex
/// </summary>
public static string DescribeAsHex(byte[] buffer, string separator, int bytesPerLine)
{
StringBuilder sb = new StringBuilder();
int n = 0;
foreach (byte b in buffer)
{
sb.AppendFormat("{0:X2}{1}", b, separator);
if (++n % bytesPerLine == 0)
sb.Append("\r\n");
}
sb.Append("\r\n");
return sb.ToString();
}
/// <summary>
/// Decodes the buffer using the specified encoding, stopping at the first null
/// </summary>
public static string DecodeAsString(byte[] buffer, int offset, int length, Encoding encoding)
{
for (int n = 0; n < length; n++)
{
if (buffer[offset + n] == 0)
length = n;
}
return encoding.GetString(buffer, offset, length);
}
}
}
Loading…
Cancel
Save