using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Whisper.Internal;
namespace Whisper
{
/// Extension methods of these COM interfaces
public static class ExtensionMethods
{
/// Create a context to transcribe audio with this model
public static Context createContext( this iModel model )
{
iContext ctx = model.createContextInternal();
return new Context( ctx );
}
/// Convert language into a short ID string, like "en"
[SkipLocalsInit]
public static string getCode( this eLanguage lang )
{
unsafe
{
sbyte* ptr = stackalloc sbyte[ 5 ];
*(uint*)ptr = (uint)lang;
ptr[ 4 ] = 0;
return new string( ptr );
}
}
/// Resolve integer token ID into string.
/// If the token ID was not found in the model, the method returns null without raising exceptions.
public static string? stringFromToken( this iModel model, int idToken ) =>
Marshal.PtrToStringUTF8( model.stringFromTokenInternal( idToken ) );
/// List capture devices
public static CaptureDeviceId[]? listCaptureDevices( this iMediaFoundation mf )
{
CaptureDeviceId[]? result = null;
pfnFoundCaptureDevices pfn = delegate ( int len, sCaptureDevice[]? arr, IntPtr pv )
{
try
{
if( len == 0 || arr == null )
return 1;
Debug.Assert( len == arr.Length );
result = new CaptureDeviceId[ len ];
for( int i = 0; i < len; i++ )
result[ i ] = new CaptureDeviceId( arr[ i ] );
return 0;
}
catch( Exception ex )
{
NativeLogger.captureException( ex );
return ex.HResult;
}
};
mf.listCaptureDevices( pfn, IntPtr.Zero );
return result;
}
/// Open audio capture device
public static iAudioCapture openCaptureDevice( this iMediaFoundation mf, in CaptureDeviceId id, in sCaptureParams? cp = null )
{
sCaptureParams captureParams = cp ?? new sCaptureParams();
return mf.openCaptureDevice( id.endpoint, ref captureParams );
}
}
}