1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
| using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace NAudio.Wave
{
/// <summary>
/// C# (.NET) Wrapper for the libFlac library (Written in C++)
/// </summary>
/// <remarks>
/// Based a .NET/C# Interop wrapper by Stanimir Stoyanov -
/// http://stoyanoff.info/blog/2010/07/26/decoding-flac-audio-files-in-c/
/// and
/// http://stoyanoff.info/blog/2010/01/08/encoding-uncompressed-audio-with-flac-in-c/
/// using libFlac - http://flac.sourceforge.net
/// For a full description of libFlac Decoder API: http://flac.sourceforge.net/api/group__flac__stream__decoder.html
/// For a full description of libFlac Encoder API: http://flac.sourceforge.net/api/group__flac__stream__encoder.html
/// </remarks>
public class LibFLACSharp
{
#region Constants
const string DLLName = @"LibFlac.dll";
public enum StreamDecoderState
{
SearchForMetadata = 0,
ReadMetadata,
SearchForFrameSync,
ReadFrame,
EndOfStream,
OggError,
SeekError,
Aborted,
MemoryAllocationError,
Uninitialized
}
#endregion
#region Decoder API
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr FLAC__stream_decoder_new();
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool FLAC__stream_decoder_finish(IntPtr context);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool FLAC__stream_decoder_delete(IntPtr context);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern int FLAC__stream_decoder_init_file(IntPtr context, string filename, Decoder_WriteCallback write, Decoder_MetadataCallback metadata, Decoder_ErrorCallback error, IntPtr userData);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool FLAC__stream_decoder_process_single(IntPtr context);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool FLAC__stream_decoder_process_until_end_of_metadata(IntPtr context);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool FLAC__stream_decoder_process_until_end_of_stream(IntPtr context);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool FLAC__stream_decoder_seek_absolute(IntPtr context, long newSamplePosition);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool FLAC__stream_decoder_get_decode_position(IntPtr context, ref long position);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern long FLAC__stream_decoder_get_total_samples(IntPtr context);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern int FLAC__stream_decoder_get_channels(IntPtr context);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern int FLAC__stream_decoder_get_bits_per_sample(IntPtr context);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern int FLAC__stream_decoder_get_sample_rate(IntPtr context);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern StreamDecoderState FLAC__stream_decoder_get_state(IntPtr context);
// Callbacks
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void Decoder_WriteCallback(IntPtr context, IntPtr frame, IntPtr buffer, IntPtr userData);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void Decoder_ErrorCallback(IntPtr context, DecodeError status, IntPtr userData);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void Decoder_MetadataCallback(IntPtr context, IntPtr metadata, IntPtr userData);
private const int FlacMaxChannels = 8;
public struct FlacFrame
{
public FrameHeader Header;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = FlacMaxChannels)]
public FlacSubFrame[] Subframes;
public FrameFooter Footer;
}
public struct FrameHeader
{
public int BlockSize;
public int SampleRate;
public int Channels;
public int ChannelAssignment;
public int BitsPerSample;
public FrameNumberType NumberType;
public long FrameOrSampleNumber;
public byte Crc;
}
public struct FlacSubFrame
{
public SubframeType Type;
public IntPtr Data;
public int WastedBits;
}
public struct FrameFooter
{
public ushort Crc;
}
public enum FrameNumberType
{
Frame,
Sample
}
public enum SubframeType
{
Constant,
Verbatim,
Fixed,
LPC
}
public enum DecodeError
{
LostSync,
BadHeader,
FrameCrcMismatch,
UnparsableStream
}
public enum FLACMetaDataType
{
StreamInfo,
Padding,
Application,
Seekable,
VorbisComment,
CueSheet,
Picture,
Undefined
}
public struct FLACMetaData
{
// The type of the metadata block; used determine which member of the data union to dereference. If type >= FLAC__METADATA_TYPE_UNDEFINED then data.unknown must be used.
public FLACMetaDataType MetaDataType;
// true if this metadata block is the last, else false
public bool IsLast;
// Length, in bytes, of the block data as it appears in the stream.
public int Length;
// Polymorphic block data; use the type value to determine which to use.
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)]
public byte[] Data;
}
[StructLayout(LayoutKind.Explicit,Pack = 1,Size = 40)]
public struct FLACStreamInfo
{
// Note Offsets 0..3 are the byte array length (header) - we just ingore these bytes and start with Offset 4
[FieldOffset(4)]
public Int32 MinBlocksize;
[FieldOffset(8)]
public Int32 MaxBlocksize;
[FieldOffset(12)]
public Int32 min_framesize;
[FieldOffset(16)]
public Int32 max_framesize;
[FieldOffset(20)]
public Int32 SampleRate;
[FieldOffset(24)]
public Int32 Channels;
[FieldOffset(28)]
public Int32 BitsPerSample;
[FieldOffset(32)]
public Int32 TotalSamplesHi;
[FieldOffset(36)]
public Int32 TotalSamplesLo;
// [FieldOffset(40)]
// public byte[] md5sum;
}
#endregion
#region Encoder API
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr FLAC__stream_encoder_new();
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool FLAC__stream_encoder_finish(IntPtr context);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool FLAC__stream_encoder_delete(IntPtr context);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool FLAC__stream_encoder_set_channels(IntPtr context, int value);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool FLAC__stream_encoder_set_bits_per_sample(IntPtr context, int value);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool FLAC__stream_encoder_set_sample_rate(IntPtr context, int value);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool FLAC__stream_encoder_set_compression_level(IntPtr context, int value);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool FLAC__stream_encoder_set_blocksize(IntPtr context, int value);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern int FLAC__stream_encoder_init_stream(IntPtr context, Encoder_WriteCallback write, Encoder_SeekCallback seek, Encoder_TellCallback tell, Encoder_MetadataCallback metadata, IntPtr userData);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern int FLAC__stream_encoder_init_file(IntPtr context, string filename, IntPtr progress, IntPtr userData);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool FLAC__stream_encoder_process_interleaved(IntPtr context, IntPtr buffer, int samples);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool FLAC__stream_encoder_process(IntPtr context, IntPtr buffer, int samples);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool FLAC__stream_encoder_set_verify(IntPtr context, bool value);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool FLAC__stream_encoder_set_streamable_subset(IntPtr context, bool value);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool FLAC__stream_encoder_set_do_mid_side_stereo(IntPtr context, bool value);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool FLAC__stream_encoder_set_loose_mid_side_stereo(IntPtr context, bool value);
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
public static extern int FLAC__stream_encoder_get_state(IntPtr context);
// Callbacks
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int Encoder_WriteCallback(IntPtr context, IntPtr buffer, int bytes, uint samples, uint current_frame, IntPtr userData);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int Encoder_SeekCallback(IntPtr context, long absoluteOffset, IntPtr userData);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int Encoder_TellCallback(IntPtr context, out long absoluteOffset, IntPtr userData);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void Encoder_MetadataCallback(IntPtr context, IntPtr metadata, IntPtr userData);
#endregion
}
} |