Initial checkin: wrapper for Christopher Key's HDCD decoder

This commit is contained in:
chudov
2008-11-04 01:17:42 +00:00
parent 5a92dc8b84
commit bbb87a2d49
3 changed files with 401 additions and 0 deletions

265
HDCDDotNet/HDCDDotNet.cs Normal file
View File

@@ -0,0 +1,265 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using AudioCodecsDotNet;
namespace HDCDDotNet
{
/** \brief Statistics for decoding. */
[StructLayout(LayoutKind.Sequential)]
public struct hdcd_decoder_statistics
{
public UInt32 num_packets;
/**<Total number of samples processed. */
public bool enabled_peak_extend;
/**< True if peak extend was enabled during decoding. */
public bool disabled_peak_extend;
/**< True if peak extend was disabled during decoding. */
public double min_gain_adjustment;
/**< Minimum dynamic gain used during decoding. */
public double max_gain_adjustment;
/**< Maximum dynamic gain used during decoding. */
public bool enabled_transient_filter;
/**< True if the transient filter was enabled during decoding. */
public bool disabled_transient_filter;
/**< True if the transient filter was disabled during decoding. */
};
public class HDCDDotNet
{
public HDCDDotNet (Int16 channels, Int32 sample_rate, bool decode)
{
_decoder = IntPtr.Zero;
#if !MONO
_decoder = hdcd_decoder_new();
_channelCount = channels;
if (_decoder == IntPtr.Zero)
throw new Exception("Failed to initialize HDCD decoder.");
bool b = true;
b &= hdcd_decoder_set_num_channels(_decoder, channels);
b &= hdcd_decoder_set_sample_rate(_decoder, sample_rate);
b &= hdcd_decoder_set_input_bps(_decoder, 16);
b &= hdcd_decoder_set_output_bps(_decoder, 24);
if (!b)
throw new Exception("Failed to set up HDCD _decoder parameters.");
_decoderCallback = decode ? new hdcd_decoder_write_callback(DecoderCallback) : null;
_gch = GCHandle.Alloc(this);
hdcd_decoder_init_status status = hdcd_decoder_init(_decoder, IntPtr.Zero, _decoderCallback, (IntPtr) _gch);
switch (status)
{
case hdcd_decoder_init_status.HDCD_DECODER_INIT_STATUS_OK:
break;
case hdcd_decoder_init_status.HDCD_DECODER_INIT_STATUS_MEMORY_ALOCATION_ERROR:
throw new Exception("Memory allocation error.");
case hdcd_decoder_init_status.HDCD_DECODER_INIT_STATUS_INVALID_NUM_CHANNELS:
throw new Exception("Invalid number of channels.");
case hdcd_decoder_init_status.HDCD_DECODER_INIT_STATUS_INVALID_SAMPLE_RATE:
throw new Exception("Invalid sample rate.");
default:
throw new Exception("Unknown error(" + status.ToString() + ").");
}
#else
throw new Exception("HDCD unsupported.");
#endif
}
public bool Detected
{
get
{
#if !MONO
return hdcd_decoder_detected_hdcd(_decoder);
#else
throw new Exception("HDCD unsupported.");
#endif
}
}
public int Channels
{
get
{
return _channelCount;
}
}
public void Reset()
{
#if !MONO
if (!hdcd_decoder_reset(_decoder))
#endif
throw new Exception("error resetting decoder.");
}
public void GetStatistics(out hdcd_decoder_statistics stats)
{
#if !MONO
IntPtr _statsPtr = hdcd_decoder_get_statistics(_decoder);
#else
IntPtr _statsPtr = IntPtr.Zero;
#endif
if (_statsPtr == IntPtr.Zero)
throw new Exception("HDCD statistics error.");
stats = (hdcd_decoder_statistics) Marshal.PtrToStructure(_statsPtr, typeof(hdcd_decoder_statistics));
}
public void Process(int[,] sampleBuffer, uint sampleCount)
{
#if !MONO
if (!hdcd_decoder_process_buffer_interleaved(_decoder, sampleBuffer, (int) sampleCount))
throw new Exception("HDCD processing error.");
#endif
}
public void Process(byte[] buff, uint sampleCount)
{
if (_inSampleBuffer == null || _inSampleBuffer.GetLength(0) < sampleCount)
_inSampleBuffer = new int[sampleCount, _channelCount];
AudioCodecsDotNet.AudioCodecsDotNet.BytesToFLACSamples_16(buff, 0, _inSampleBuffer, 0, sampleCount, _channelCount);
Process(_inSampleBuffer, sampleCount);
}
public void Flush ()
{
#if !MONO
if (!hdcd_decoder_flush_buffer(_decoder))
#endif
throw new Exception("error flushing buffer.");
}
public IAudioDest AudioDest
{
get
{
return _audioDest;
}
set
{
//if (hdcd_decoder_get_state(_decoder) == hdcd_decoder_state.HDCD_DECODER_STATE_DIRTY)
// Flush(); /* Flushing is currently buggy! Doesn't work twice, and can't continue after flush! */
_audioDest = value;
}
}
/** \brief Return values from hdcd_decoder_init. */
private enum hdcd_decoder_init_status
{
HDCD_DECODER_INIT_STATUS_OK = 0,
/**< Initialization was successful. */
HDCD_DECODER_INIT_STATUS_INVALID_STATE,
/**< The _decoder was already initialised. */
HDCD_DECODER_INIT_STATUS_MEMORY_ALOCATION_ERROR,
/**< Initialization failed due to a memory allocation error. */
HDCD_DECODER_INIT_STATUS_INVALID_NUM_CHANNELS,
/**< Initialization failed because the configured number of channels was invalid. */
HDCD_DECODER_INIT_STATUS_INVALID_SAMPLE_RATE,
/**< Initialization failed because the configured sample rate was invalid. */
HDCD_DECODER_INIT_STATUS_INVALID_INPUT_BPS,
/**< Initialization failed because the configured input bits per sample was invalid. */
HDCD_DECODER_INIT_STATUS_INVALID_OUTPUT_BPS
/**< Initialization failed because the configured output bits per sample was invalid. */
}
/** \brief State values for a decoder.
*
* The decoder's state can be obtained by calling hdcd_decoder_get_state().
*/
private enum hdcd_decoder_state
{
HDCD_DECODER_STATE_UNINITIALISED = 1,
/**< The decoder is uninitialised. */
HDCD_DECODER_STATE_READY,
/**< The decoder is initialised and ready to process data. */
HDCD_DECODER_STATE_DIRTY,
/**< The decoder has processed data, but has not yet been flushed. */
HDCD_DECODER_STATE_FLUSHED,
/**< The decoder has been flushed. */
HDCD_DECODER_STATE_WRITE_ERROR,
/**< An error was returned by the write callback. */
HDCD_DECODER_STATE_MEMORY_ALOCATION_ERROR
/**< Processing failed due to a memory allocation error. */
};
private IntPtr _decoder;
private int[,] _inSampleBuffer;
private int[,] _outSampleBuffer;
private int _channelCount;
hdcd_decoder_write_callback _decoderCallback;
IAudioDest _audioDest;
GCHandle _gch;
~HDCDDotNet()
{
#if !MONO
if (_decoder != IntPtr.Zero)
hdcd_decoder_delete(_decoder);
_gch.Free();
#endif
}
private delegate bool hdcd_decoder_write_callback(IntPtr decoder, IntPtr buffer, int samples, IntPtr client_data);
private unsafe bool Output(IntPtr buffer, int samples)
{
if (AudioDest == null)
return true;
if (_outSampleBuffer == null || _outSampleBuffer.GetLength(0) < samples)
_outSampleBuffer = new int[samples, _channelCount];
int loopCount = samples * _channelCount;
int* pInSamples = (int*)buffer;
fixed (int* pOutSamplesFixed = &_outSampleBuffer[0, 0])
{
int* pOutSamples = pOutSamplesFixed;
for (int i = 0; i < loopCount; i++)
*(pOutSamples++) = *(pInSamples++);
}
AudioDest.Write(_outSampleBuffer, (uint)samples);
return true;
}
private static unsafe bool DecoderCallback(IntPtr decoder, IntPtr buffer, int samples, IntPtr client_data)
{
GCHandle gch = (GCHandle)client_data;
HDCDDotNet hdcd = (HDCDDotNet)gch.Target;
return hdcd.Output(buffer, samples);
}
#if !MONO
[DllImport("hdcd.dll")]
private static extern IntPtr hdcd_decoder_new ();
[DllImport("hdcd.dll")]
private static extern void hdcd_decoder_delete(IntPtr decoder);
[DllImport("hdcd.dll")]
private static extern hdcd_decoder_state hdcd_decoder_get_state(IntPtr decoder);
[DllImport("hdcd.dll")]
private static extern bool hdcd_decoder_set_num_channels (IntPtr decoder, Int16 num_channels);
//HDCD_API uint16_t hdcd_decoder_get_num_channels(const hdcd_decoder *const _decoder);
[DllImport("hdcd.dll")]
private static extern bool hdcd_decoder_set_sample_rate(IntPtr decoder, Int32 sample_rate);
//HDCD_API uint32_t hdcd_decoder_get_sample_rate(const hdcd_decoder *const _decoder);
[DllImport("hdcd.dll")]
private static extern bool hdcd_decoder_set_input_bps(IntPtr decoder, Int16 input_bps);
//HDCD_API uint16_t hdcd_decoder_get_input_bps(const hdcd_decoder *const _decoder);
[DllImport("hdcd.dll")]
private static extern bool hdcd_decoder_set_output_bps(IntPtr decoder, Int16 output_bps);
//HDCD_API uint16_t hdcd_decoder_get_output_bps(const hdcd_decoder *const _decoder);
[DllImport("hdcd.dll")]
private static extern hdcd_decoder_init_status hdcd_decoder_init (IntPtr decoder, IntPtr unused, hdcd_decoder_write_callback write_callback, IntPtr client_data);
[DllImport("hdcd.dll")]
private static extern bool hdcd_decoder_finish(IntPtr decoder);
[DllImport("hdcd.dll")]
private static extern bool hdcd_decoder_process_buffer_interleaved(IntPtr decoder, [In, Out] int [,] input_buffer, Int32 samples);
[DllImport("hdcd.dll")]
private static extern bool hdcd_decoder_flush_buffer(IntPtr decoder);
[DllImport("hdcd.dll")]
private static extern bool hdcd_decoder_reset(IntPtr decoder);
[DllImport("hdcd.dll")]
private static extern bool hdcd_decoder_detected_hdcd(IntPtr decoder);
[DllImport("hdcd.dll")]
private static extern IntPtr hdcd_decoder_get_statistics(IntPtr decoder);
#endif
}
}

View File

@@ -0,0 +1,101 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{32338A04-5B6B-4C63-8EE7-C6400F73B5D7}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>HDCDDotNet</RootNamespace>
<AssemblyName>HDCDDotNet</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
<DebugSymbols>true</DebugSymbols>
<OutputPath>..\bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<CodeAnalysisRuleAssemblies>C:\Program Files (x86)\Microsoft Visual Studio 8\Team Tools\Static Analysis Tools\FxCop\\rules</CodeAnalysisRuleAssemblies>
<CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression>
<CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
<ErrorReport>prompt</ErrorReport>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
<OutputPath>..\bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<CodeAnalysisRuleAssemblies>C:\Program Files (x86)\Microsoft Visual Studio 8\Team Tools\Static Analysis Tools\FxCop\\rules</CodeAnalysisRuleAssemblies>
<CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression>
<CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
<ErrorReport>prompt</ErrorReport>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>
<OutputPath>..\bin\Win32\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<CodeAnalysisRuleAssemblies>C:\Program Files (x86)\Microsoft Visual Studio 8\Team Tools\Static Analysis Tools\FxCop\\rules</CodeAnalysisRuleAssemblies>
<CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression>
<CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
<ErrorReport>prompt</ErrorReport>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<OutputPath>..\bin\Win32\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<CodeAnalysisRuleAssemblies>C:\Program Files (x86)\Microsoft Visual Studio 8\Team Tools\Static Analysis Tools\FxCop\\rules</CodeAnalysisRuleAssemblies>
<CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression>
<CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
<ErrorReport>prompt</ErrorReport>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="HDCDDotNet.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AudioCodecsDotNet\AudioCodecsDotNet.csproj">
<Project>{6458A13A-30EF-45A9-9D58-E5031B17BEE2}</Project>
<Name>AudioCodecsDotNet</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("HDCDDotNet")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("HDCDDotNet")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("818197e9-ebd3-4de7-905d-ffbb0e058c08")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]