yum-archive/TaSTT-Whisper
High-performance GPGPU inference of OpenAI's Whisper automatic speech recognition (ASR) model
git clone https://git.yummers.dev/yum-archive/TaSTT-Whisper
8c4603c
master
1#pragma warning disableCS0649 // Field is never assigned to 2using ComLight ; 3using System . ComponentModel ; 4using System . Runtime . InteropServices ; 5 6namespace Whisper . Internal 7{ 8/// <summary>Size of the buffers owned by the <see cref="iTranscribeResult" /> object</summary> 9public readonly struct sTranscribeLength 10{ 11/// <summary>Count of segments</summary> 12public readonly int countSegments ; 13/// <summary>Total count of tokens, for all segments combined</summary> 14public readonly int countTokens ; 15} 16 17/// <summary>Output data from the model</summary> 18[ ComInterface ( "2871a73f-5ce3-48f8-8779-6582ee11935e" , eMarshalDirection . ToManaged ), CustomConventions ( typeof ( NativeLogger ) )] 19public interface iTranscribeResult 20{ 21/// <summary>Get size of the buffers</summary> 22[ RetValIndex , EditorBrowsable ( EditorBrowsableState . Never )] 23public sTranscribeLength getSize (); 24 25/// <summary>Pointer to segment data, a vector of <see cref="sSegment" /> structures</summary> 26[ EditorBrowsable ( EditorBrowsableState . Never )] 27public IntPtr getSegments (); 28 29/// <summary>Pointer to tokens data, a vector of <see cref="sToken" /> structures</summary> 30[ EditorBrowsable ( EditorBrowsableState . Never )] 31public IntPtr getTokens (); 32} 33} 34 35namespace Whisper 36{ 37/// <summary>Start and end times of a segment or token</summary> 38/// <remarks>The times are relative to the start of the media</remarks> 39public readonly struct sTimeInterval 40{ 41/// <summary>Start time</summary> 42public readonly TimeSpan begin ; 43/// <summary>End time</summary> 44public readonly TimeSpan end ; 45} 46 47/// <summary>Segment data</summary> 48public readonly struct sSegment 49{ 50internal readonly IntPtr m_text ; 51/// <summary>Segment text</summary> 52public string ? text => Marshal . PtrToStringUTF8 ( m_text ); 53/// <summary>Start and end times of the segment</summary> 54public readonly sTimeInterval time ; 55/// <summary>Slice of the tokens</summary> 56public readonly int firstToken , countTokens ; 57} 58 59/// <summary>Token flags</summary> 60[ Flags ] 61public enum eTokenFlags : uint 62{ 63/// <summary>The token is special</summary> 64Special = 1 , 65} 66 67/// <summary>Token data</summary> 68public readonly struct sToken 69{ 70internal readonly IntPtr m_text ; 71/// <summary>Token text</summary> 72public string ? text => Marshal . PtrToStringUTF8 ( m_text ); 73/// <summary>Start and end times of the token</summary> 74public readonly sTimeInterval time ; 75/// <summary>Probability of the token</summary> 76public readonly float probability ; 77/// <summary>Probability of the timestamp token</summary> 78public readonly float probabilityTimestamp ; 79/// <summary>Sum of probabilities of all timestamp tokens</summary> 80public readonly float ptsum ; 81/// <summary>Voice length of the token</summary> 82public readonly float vlen ; 83/// <summary>Token id</summary> 84public readonly int id ; 85/// <summary>Token flags</summary> 86readonly eTokenFlags flags ; 87/// <summary>True if the token flags has the specified bit set</summary> 88public bool hasFlag ( eTokenFlags bit ) => flags . HasFlag ( bit ); 89} 90 91/// <summary>Output data from the model</summary> 92public readonly ref struct TranscribeResult 93{ 94/// <summary>Segments in the results</summary> 95public readonly ReadOnlySpan < sSegment > segments ; 96/// <summary>Tokens in the results, for all segments</summary> 97public readonly ReadOnlySpan < sToken > tokens ; 98 99internal TranscribeResult ( Internal . iTranscribeResult i ) 100{ 101Internal . sTranscribeLength len = i . getSize (); 102 unsafe 103{ 104// This does not copy the buffers to managed memory. 105// Instead, the C# spans directly reference the native memory stored in these std::vectors 106if ( len . countSegments > 0 ) 107segments = new ReadOnlySpan < sSegment > ( ( void * ) i . getSegments (), len . countSegments ); 108else 109segments = ReadOnlySpan < sSegment > . Empty ; 110 111if ( len . countTokens > 0 ) 112tokens = new ReadOnlySpan < sToken > ( ( void * ) i . getTokens (), len . countTokens ); 113else 114tokens = ReadOnlySpan < sToken > . Empty ; 115} 116} 117 118/// <summary>Get tokens for the specified segment</summary> 119public ReadOnlySpan < sToken > getTokens ( in sSegment seg ) => 120tokens . Slice ( seg . firstToken , seg . countTokens ); 121} 122}