2008-01-15 01:24:15 +01:00
#region - - - OpenTK . OpenAL License - - -
/ * XRamExtension . cs
* C header : \ OpenAL 1.1 SDK \ include \ xram . h
* Spec : ?
* Copyright ( c ) 2008 Christoph Brandtner and Stefanos Apostolopoulos
* See license . txt for license details ( MIT )
* http : //www.OpenTK.net */
#endregion
using System ;
2008-01-25 17:22:07 +01:00
using System.Diagnostics ;
2008-01-15 01:24:15 +01:00
using System.Runtime.InteropServices ;
namespace OpenTK.OpenAL
{
2008-01-25 17:22:07 +01:00
/// <summary>The X-Ram Extension is provided on the top-end Sound Blaster X-Fi solutions (Sound Blaster X-Fi Fatal1ty, Sound Blaster X-Fi Elite Pro, or later). These products feature 64MB of X-Ram that can only be used for audio purposes, which can be controlled by this Extension.</summary>
2008-01-15 01:24:15 +01:00
public class XRamExtension
{
2008-01-25 17:22:07 +01:00
#region Instance state
2008-01-15 01:24:15 +01:00
private bool _valid = false ;
/// <summary>Returns True if the X-Ram Extension has been found and could be initialized.</summary>
public bool IsInitialized
{
get { return _valid ; }
}
2008-01-25 17:22:07 +01:00
#endregion Instance state
2008-01-15 01:24:15 +01:00
#region X - RAM Function pointer definitions
2008-01-31 14:15:17 +01:00
//[CLSCompliant(false)]
2008-01-28 16:05:39 +01:00
private delegate bool Delegate_SetBufferMode ( int n , ref uint buffers , int value ) ;
2008-01-15 01:24:15 +01:00
//typedef ALboolean (__cdecl *EAXSetBufferMode)(ALsizei n, ALuint *buffers, ALint value);
2008-01-28 16:05:39 +01:00
[CLSCompliant( false )]
private delegate int Delegate_GetBufferMode ( uint buffer , IntPtr value ) ;
2008-01-15 01:24:15 +01:00
//typedef ALenum (__cdecl *EAXGetBufferMode)(ALuint buffer, ALint *value);
2008-01-20 20:00:03 +01:00
//[CLSCompliant(false)]
2008-01-17 21:55:56 +01:00
private Delegate_SetBufferMode Imported_SetBufferMode ;
2008-01-20 20:00:03 +01:00
//[CLSCompliant(false)]
2008-01-17 21:55:56 +01:00
private Delegate_GetBufferMode Imported_GetBufferMode ;
2008-01-15 01:24:15 +01:00
#endregion X - RAM Function pointer definitions
#region X - RAM Tokens
private int AL_EAX_RAM_SIZE , AL_EAX_RAM_FREE ,
AL_STORAGE_AUTOMATIC , AL_STORAGE_HARDWARE , AL_STORAGE_ACCESSIBLE ;
#endregion X - RAM Tokens
#region Constructor / Extension Loading
2008-01-18 14:32:51 +01:00
public XRamExtension ( )
2008-01-15 01:24:15 +01:00
{ // Query if Extension supported and retrieve Tokens/Pointers if it is.
_valid = false ;
2008-01-28 16:05:39 +01:00
if ( AL . IsExtensionPresent ( "EAX-RAM" ) = = false )
2008-01-15 01:24:15 +01:00
return ;
2008-01-28 16:05:39 +01:00
AL_EAX_RAM_SIZE = AL . GetEnumValue ( "AL_EAX_RAM_SIZE" ) ;
AL_EAX_RAM_FREE = AL . GetEnumValue ( "AL_EAX_RAM_FREE" ) ;
AL_STORAGE_AUTOMATIC = AL . GetEnumValue ( "AL_STORAGE_AUTOMATIC" ) ;
AL_STORAGE_HARDWARE = AL . GetEnumValue ( "AL_STORAGE_HARDWARE" ) ;
AL_STORAGE_ACCESSIBLE = AL . GetEnumValue ( "AL_STORAGE_ACCESSIBLE" ) ;
2008-01-15 01:24:15 +01:00
2008-01-25 17:22:07 +01:00
// Console.WriteLine("RamSize: {0} RamFree: {1} StorageAuto: {2} StorageHW: {3} StorageAccess: {4}",AL_EAX_RAM_SIZE,AL_EAX_RAM_FREE,AL_STORAGE_AUTOMATIC,AL_STORAGE_HARDWARE,AL_STORAGE_ACCESSIBLE);
2008-01-15 01:24:15 +01:00
2008-01-18 14:32:51 +01:00
if ( AL_EAX_RAM_SIZE = = 0 | |
2008-01-15 01:24:15 +01:00
AL_EAX_RAM_FREE = = 0 | |
AL_STORAGE_AUTOMATIC = = 0 | |
AL_STORAGE_HARDWARE = = 0 | |
2008-01-18 14:32:51 +01:00
AL_STORAGE_ACCESSIBLE = = 0 )
2008-01-15 01:24:15 +01:00
{
2008-01-28 16:05:39 +01:00
Trace . WriteLine ( "X-Ram: Token values could not be retrieved." ) ;
2008-01-15 01:24:15 +01:00
return ;
}
2008-01-25 17:22:07 +01:00
// Console.WriteLine("Free Ram: {0} / {1}",GetRamFree( ),GetRamSize( ));
2008-01-15 01:24:15 +01:00
try
{
2008-01-28 16:05:39 +01:00
Imported_GetBufferMode = ( Delegate_GetBufferMode ) Marshal . GetDelegateForFunctionPointer ( AL . GetProcAddress ( "EAXGetBufferMode" ) , typeof ( Delegate_GetBufferMode ) ) ;
Imported_SetBufferMode = ( Delegate_SetBufferMode ) Marshal . GetDelegateForFunctionPointer ( AL . GetProcAddress ( "EAXSetBufferMode" ) , typeof ( Delegate_SetBufferMode ) ) ;
2008-01-18 14:32:51 +01:00
} catch ( Exception e )
2008-01-15 01:24:15 +01:00
{
2008-01-28 16:05:39 +01:00
Trace . WriteLine ( "X-Ram: Attempt to marshal function pointers with AL.GetProcAddress failed. " + e . ToString ( ) ) ;
2008-01-15 01:24:15 +01:00
return ;
}
_valid = true ;
}
#endregion Constructor / Extension Loading
#region Public Methods
2008-01-25 17:22:07 +01:00
/// <summary>Query total amount of X-RAM in bytes.</summary>
2008-01-18 14:32:51 +01:00
public int GetRamSize ( )
2008-01-15 01:24:15 +01:00
{
2008-01-28 16:05:39 +01:00
return AL . Get ( ( Enums . ALGetInteger ) AL_EAX_RAM_SIZE ) ;
2008-01-15 01:24:15 +01:00
}
2008-01-25 17:22:07 +01:00
/// <summary>Query free X-RAM available in bytes.</summary>
2008-01-18 14:32:51 +01:00
public int GetRamFree ( )
2008-01-15 01:24:15 +01:00
{
2008-01-28 16:05:39 +01:00
return AL . Get ( ( Enums . ALGetInteger ) AL_EAX_RAM_FREE ) ;
2008-01-15 01:24:15 +01:00
}
2008-01-25 17:22:07 +01:00
/// <summary>This enum is used to abstract the need of using AL.GetEnumValue() with the Extension. The values do NOT correspond to AL_STORAGE_* tokens!</summary>
2008-01-15 01:24:15 +01:00
public enum XRamStorage : byte
{
2008-01-25 17:22:07 +01:00
/// <summary>Put an Open AL Buffer into X-RAM if memory is available, otherwise use host RAM. This is the default mode.</summary>
Automatic = 0 ,
2008-01-28 16:05:39 +01:00
/// <summary>Force an Open AL Buffer into X-RAM, good for non-streaming buffers.</summary>
2008-01-15 01:24:15 +01:00
Hardware = 1 ,
2008-01-28 16:05:39 +01:00
/// <summary>Force an Open AL Buffer into 'accessible' (currently host) RAM, good for streaming buffers.</summary>
2008-01-25 17:22:07 +01:00
Accessible = 2 ,
2008-01-15 01:24:15 +01:00
}
2008-01-17 20:02:37 +01:00
2008-01-25 17:22:07 +01:00
/// <summary>This function is used to set the storage Mode of an array of OpenAL Buffers.</summary>
/// <param name="n">The number of OpenAL Buffers pointed to by buffer.</param>
/// <param name="buffer">An array of OpenAL Buffer handles.</param>
/// <param name="mode">The storage mode that should be used for all the given buffers. Should be the value of one of the following enum names: XRamStorage.Automatic, XRamStorage.Hardware, XRamStorage.Accessible</param>
/// <returns>True if all the Buffers were successfully set to the requested storage mode, False otherwise.</returns>
2008-01-28 16:05:39 +01:00
[CLSCompliant( false )]
public bool SetBufferMode ( int n , ref uint buffer , XRamStorage mode )
2008-01-15 01:24:15 +01:00
{
2008-01-18 14:32:51 +01:00
switch ( mode )
2008-01-15 01:24:15 +01:00
{
2008-01-25 17:22:07 +01:00
case XRamStorage . Accessible :
2008-01-28 16:05:39 +01:00
return Imported_SetBufferMode ( n , ref buffer , AL_STORAGE_ACCESSIBLE ) ;
2008-01-18 14:32:51 +01:00
case XRamStorage . Hardware :
2008-01-28 16:05:39 +01:00
return Imported_SetBufferMode ( n , ref buffer , AL_STORAGE_HARDWARE ) ;
2008-01-18 14:32:51 +01:00
default :
2008-01-28 16:05:39 +01:00
return Imported_SetBufferMode ( n , ref buffer , AL_STORAGE_AUTOMATIC ) ;
2008-01-15 01:24:15 +01:00
}
}
2008-01-25 17:22:07 +01:00
/// <summary>This function is used to retrieve the storage Mode of a single OpenAL Buffer.</summary>
/// <param name="buffer">The handle of an OpenAL Buffer.</param>
/// <returns>The current Mode of the Buffer.</returns>
2008-01-28 16:05:39 +01:00
[CLSCompliant( false )]
2008-01-18 14:32:51 +01:00
public XRamStorage GetBufferMode ( ref uint buffer )
2008-01-17 00:57:54 +01:00
{
2008-01-28 16:05:39 +01:00
int tempresult = Imported_GetBufferMode ( buffer , IntPtr . Zero ) ; // IntPtr.Zero due to the parameter being unused/reserved atm
2008-01-25 17:22:07 +01:00
if ( tempresult = = AL_STORAGE_ACCESSIBLE )
return XRamStorage . Accessible ;
if ( tempresult = = AL_STORAGE_HARDWARE )
return XRamStorage . Hardware ;
// default:
return XRamStorage . Automatic ;
2008-01-17 00:57:54 +01:00
}
2008-01-15 01:24:15 +01:00
#endregion Public Methods
}
2008-01-18 14:32:51 +01:00
2008-01-31 14:15:17 +01:00
}