Click here to Skip to main content
15,886,017 members
Articles / Desktop Programming / Windows Forms

BSEtunes

Rate me:
Please Sign up or sign in to vote.
4.67/5 (11 votes)
24 Apr 2010CPOL4 min read 64.6K   4.3K   58  
BSEtunes is a MySQL based, full manageable, networkable single or multiuser jukebox application
using System;
using System.Runtime.InteropServices;
using System.Globalization;
using BSE.Platten.Ripper.Properties;
using System.Security.Permissions;

namespace BSE.Platten.Ripper
{
	public enum VbrMethod : int
	{
		None		= -1,
		Default  	=  0,
		Old			=  1,
		New			=  2,
		Mtrh		=  3,
		Abr			=  4
	}

	/* MPEG modes */
	public enum MpegMode : uint 
	{
		Stereo = 0,
		JointStereo,
		DualChannel,   /* LAME doesn't supports this! */
		Mono,
		NotSet,
		MaxIndicator   /* Don't use this! It's used for sanity checks. */ 
	}

	public enum LameQualityPreset : int
	{
		NoPreset		=-1,
		// QUALITY PRESETS
		NormalQuality	= 0,
		LowQuality		= 1,
		HighQuality		= 2,
		VoiceQuality	= 3,
		R3mix			= 4,
		VeryHighQuality	= 5,
		Standard		= 6,
		FastStandard	= 7,
		Extreme			= 8,
		FastExtreme		= 9,
		Insane			= 10,
		Abr				= 11,
		Cbr				= 12,
		Medium			= 13,
		FastMedium		= 14,
		// NEW PRESET VALUES
		Phone	        =1000,
		SW		        =2000,
		AM		        =3000,
		FM		        =4000,
		Voice	        =5000,
		Radio	        =6000,
		Tape	        =7000,
		Hifi	        =8000,
		CD		        =9000,
		Studio	        =10000
	}

	[StructLayout(LayoutKind.Sequential), Serializable]
	public struct	MP3 //BE_CONFIG_MP3
	{
		public uint   dwSampleRate;		// 48000, 44100 and 32000 allowed
		public byte	  byMode;			// BE_MP3_MODE_STEREO, BE_MP3_MODE_DUALCHANNEL, BE_MP3_MODE_MONO
		public ushort	wBitrate;		// 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256 and 320 allowed
		public int	  bPrivate;		
		public int	  bCRC;
		public int	  bCopyright;
		public int	  bOriginal;
	} 

	[StructLayout(LayoutKind.Sequential, Size=327), Serializable]
	public struct LHV1 // BE_CONFIG_LAME LAME header version 1
	{
		public const uint MPEG1	= 1;
		public const uint MPEG2	= 0;

		// STRUCTURE INFORMATION
		public uint				dwStructVersion;	
		public uint				dwStructSize;
		// BASIC ENCODER SETTINGS
		public uint				dwSampleRate;		// SAMPLERATE OF INPUT FILE
		public uint				dwReSampleRate;		// DOWNSAMPLERATE, 0=ENCODER DECIDES  
		public MpegMode			nMode;				// STEREO, MONO
		public uint				dwBitrate;			// CBR bitrate, VBR min bitrate
		public uint				dwMaxBitrate;		// CBR ignored, VBR Max bitrate
		public LameQualityPreset nPreset;			// Quality preset
		public uint				dwMpegVersion;		// MPEG-1 OR MPEG-2
		public uint				dwPsyModel;			// FUTURE USE, SET TO 0
		public uint				dwEmphasis;			// FUTURE USE, SET TO 0
		// BIT STREAM SETTINGS
		public int				bPrivate;			// Set Private Bit (TRUE/FALSE)
		public int				bCRC;				// Insert CRC (TRUE/FALSE)
		public int				bCopyright;			// Set Copyright Bit (TRUE/FALSE)
		public int				bOriginal;			// Set Original Bit (TRUE/FALSE)
		// VBR STUFF
		public int				bWriteVBRHeader;	// WRITE XING VBR HEADER (TRUE/FALSE)
		public int				bEnableVBR;			// USE VBR ENCODING (TRUE/FALSE)
		public int				nVBRQuality;		// VBR QUALITY 0..9
		public uint				dwVbrAbr_bps;		// Use ABR in stead of nVBRQuality
        public VbrMethod        nVbrMethod;
		public int				bNoRes;				// Disable Bit resorvoir (TRUE/FALSE)
		// MISC SETTINGS
		public int				bStrictIso;			// Use strict ISO encoding rules (TRUE/FALSE)
		public ushort			nQuality;			// Quality Setting, HIGH BYTE should be NOT LOW byte, otherwhise quality=5
		// FUTURE USE, SET TO 0, align strucutre to 331 bytes
		//[ MarshalAs( UnmanagedType.ByValArray, SizeConst=255-4*4-2 )]
		//public byte[]   btReserved;//[255-4*sizeof(DWORD) - sizeof( WORD )];
		public LHV1(CWaveFormat format, uint MpeBitRate)
		{
            if (format == null)
            {
                throw new ArgumentNullException(
                    string.Format(CultureInfo.InvariantCulture, Resources.IDS_ArgumentNullException, "format"));
            }
            if ( format.wFormatTag != (short)WaveFormat.Pcm )
			{
				throw new ArgumentOutOfRangeException("format", "Only PCM format supported");
			}
			if ( format.wBitsPerSample != 16)
			{
				throw new ArgumentOutOfRangeException("format", "Only 16 bits samples supported");
			}
			dwStructVersion	= 1;
			dwStructSize		= (uint)Marshal.SizeOf(typeof(BE_CONFIG));
			switch (format.nSamplesPerSec)
			{
				case 16000 :
				case 22050 :
				case 24000 :
					dwMpegVersion		= MPEG2;
					break;
				case 32000 :
				case 44100 :
				case 48000 :
					dwMpegVersion		= MPEG1;
					break;
				default :
					throw new ArgumentOutOfRangeException("format", "Unsupported sample rate");
			}
			dwSampleRate = (uint)format.nSamplesPerSec;				// INPUT FREQUENCY
			dwReSampleRate = 0;					// DON'T RESAMPLE
			switch (format.nChannels)
			{
				case 1 :
					nMode	=	MpegMode.Mono;
					break;
				case 2 :
					nMode = MpegMode.Stereo;
					break;
				default:
					throw new ArgumentOutOfRangeException("format", "Invalid number of channels");
			}
			switch (MpeBitRate)
			{
				case 32 :
				case 40 :
				case 48 :
				case 56 :
				case 64 :
				case 80 :
				case 96 :
				case 112 :
				case 128 :
				case 160 : //Allowed bit rates in MPEG1 and MPEG2
					break; 
				case 192 :
				case 224 :
				case 256 :
				case 320 : //Allowed only in MPEG1
					if (dwMpegVersion	!= MPEG1)
					{
						throw new ArgumentOutOfRangeException("MpsBitRate", "Bit rate not compatible with input format");
					}
					break;
				case 8 :
				case 16 :
				case 24 :
				case 144 : //Allowed only in MPEG2
					if (dwMpegVersion	!= MPEG2)
					{
						throw new ArgumentOutOfRangeException("MpsBitRate", "Bit rate not compatible with input format");
					}
					break;
				default :
					throw new ArgumentOutOfRangeException("MpsBitRate", "Unsupported bit rate");
			}
			dwBitrate	= MpeBitRate;								// MINIMUM BIT RATE
            nPreset = LameQualityPreset.NormalQuality;      		// QUALITY PRESET SETTING
			dwPsyModel = 0;											// USE DEFAULT PSYCHOACOUSTIC MODEL 
			dwEmphasis = 0;											// NO EMPHASIS TURNED ON
			bOriginal = 1;											// SET ORIGINAL FLAG
			bWriteVBRHeader	= 0;					
			bNoRes = 0;												// No Bit resorvoir
			bCopyright = 0;
			bCRC = 0;
			bEnableVBR = 0;
			bPrivate = 0;
			bStrictIso = 0;
			dwMaxBitrate = 0;
			dwVbrAbr_bps = 0;
			nQuality = 0;
            nVbrMethod = VbrMethod.None;
			nVBRQuality = 0;
		}
	}

	[StructLayout(LayoutKind.Sequential), Serializable]
	public struct Acc
	{
		public uint	dwSampleRate;
		public byte	byMode;
		public ushort wBitrate;
		public byte	byEncodingMethod;
	}

	[StructLayout(LayoutKind.Explicit), Serializable]
	public class Format
	{
		[FieldOffset(0)] 
		public MP3 mp3;
		[FieldOffset(0)]
		public LHV1 lhv1;
		[FieldOffset(0)]
		public Acc acc;

		public Format(CWaveFormat format, uint MpeBitRate)
		{
			lhv1 = new LHV1(format, MpeBitRate);
		}
	}

	[StructLayout(LayoutKind.Sequential), Serializable]
	public class BE_CONFIG
	{
		// encoding formats
		public const uint BE_CONFIG_MP3	 = 0;
		public const uint BE_CONFIG_LAME = 256;

		public uint	dwConfig;	
		public Format format;

		public BE_CONFIG(CWaveFormat format, uint MpeBitRate)
		{
			this.dwConfig = BE_CONFIG_LAME;
			this.format = new Format(format, MpeBitRate);
		}
		public BE_CONFIG(CWaveFormat format) : this(format, 128)
		{
		}
	}

	[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
	public class BE_VERSION
	{ 
		public const uint BE_MAX_HOMEPAGE	= 256;
		public byte	byDLLMajorVersion;
		public byte	byDLLMinorVersion;
		public byte	byMajorVersion;
		public byte	byMinorVersion;
		// DLL Release date
		public byte	byDay;
		public byte	byMonth;
		public ushort	wYear;
		//Homepage URL
		[MarshalAs(UnmanagedType.ByValTStr, SizeConst=257/*BE_MAX_HOMEPAGE+1*/)]
		public string	zHomepage;	
		public byte	byAlphaLevel;
		public byte	byBetaLevel;
		public byte	byMMXEnabled;
		[MarshalAs(UnmanagedType.ByValArray, SizeConst=125)]
		public byte[]	btReserved;
		public BE_VERSION()
		{
			btReserved = new byte[125];
		}
	}
	/// <summary>
	/// Zusammendfassende Beschreibung f�r CLame.
	/// </summary>
	public class LameEncDll
	{
		#region Enumerationen
		
		
		
		#endregion
		
		#region Konstanten
		
		//Error codes
		public const uint BE_ERR_SUCCESSFUL = 0;
		public const uint BE_ERR_INVALID_FORMAT = 1;
		public const uint BE_ERR_INVALID_FORMAT_PARAMETERS = 2;
		public const uint BE_ERR_NO_MORE_HANDLES = 3;
		public const uint BE_ERR_INVALID_HANDLE = 4;
		
		#endregion
		
		#region FieldsProtected
		
		[DllImport("Lame_enc.dll")]
		protected static extern uint beEncodeChunk(uint hbeStream, uint nSamples, IntPtr pSamples, [In, Out] byte[] pOutput, ref uint pdwOutput);
		
		#endregion
		
		#region MethodsPublic

        [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
        public static uint EncodeChunk(uint hbeStream, byte[] buffer, int index, uint nBytes, byte[] pOutput, ref uint pdwOutput)
		{
			uint res;
			GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
			try
			{
				IntPtr ptr = (IntPtr)(handle.AddrOfPinnedObject().ToInt32()+index);
				res = beEncodeChunk(hbeStream, nBytes/2/*Samples*/, ptr, pOutput, ref pdwOutput);
			}
			finally
			{
				handle.Free();
			}
			return res;
		}
        [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
		public static uint EncodeChunk(uint hbeStream, byte[] buffer, byte[] pOutput, ref uint pdwOutput)
		{
            if (buffer == null)
            {
                throw new ArgumentNullException(
                    string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.IDS_ArgumentNullException,"buffer"));
            }
            return EncodeChunk(hbeStream, buffer, 0, (uint)buffer.Length, pOutput, ref pdwOutput);
		}

		#endregion

        internal static class NativeMethods
        {
            [DllImport("Lame_enc.dll")]
            public static extern uint beInitStream(BE_CONFIG pbeConfig, ref uint dwSamples, ref uint dwBufferSize, ref uint phbeStream);
            [DllImport("Lame_enc.dll")]
            public static extern uint beEncodeChunk(uint hbeStream, uint nSamples, short[] pInSamples, [In, Out] byte[] pOutput, ref uint pdwOutput);
            [DllImport("Lame_enc.dll")]
            public static extern uint beDeinitStream(uint hbeStream, [In, Out] byte[] pOutput, ref uint pdwOutput);
            [DllImport("Lame_enc.dll")]
            public static extern uint beCloseStream(uint hbeStream);
            [DllImport("Lame_enc.dll")]
            public static extern void beVersion([Out] BE_VERSION pbeVersion);
            [DllImport("Lame_enc.dll", CharSet = CharSet.Ansi)]
            public static extern void beWriteVBRHeader(string pszMP3FileName);
            [DllImport("Lame_enc.dll")]
            public static extern uint beEncodeChunkFloatS16NI(uint hbeStream, uint nSamples, [In]float[] buffer_l, [In]float[] buffer_r, [In, Out]byte[] pOutput, ref uint pdwOutput);
            [DllImport("Lame_enc.dll")]
            public static extern uint beFlushNoGap(uint hbeStream, [In, Out]byte[] pOutput, ref uint pdwOutput);
            [DllImport("Lame_enc.dll", CharSet = CharSet.Ansi)]
            public static extern uint beWriteInfoTag(uint hbeStream, string lpszFileName);
        }
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions