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
1be5537
master
1using System . Globalization ; 2using Whisper ; 3 4namespace MicrophoneCS 5{ 6/// <summary>Implementation of Callbacks abstract class, to print these segments as soon as they’re produced by the library.</summary> 7sealed class TranscribeCallbacks : Callbacks 8{ 9readonly CommandLineArgs args ; 10readonly eResultFlags resultFlags ; 11 12public TranscribeCallbacks ( CommandLineArgs args ) 13{ 14this . args = args ; 15resultFlags = args . resultFlags (); 16Console . OutputEncoding = System . Text . Encoding . UTF8 ; 17} 18 19// Terminal color map. 10 colors grouped in ranges [0.0, 0.1, ..., 0.9] 20// Lowest is red, middle is yellow, highest is green. 21readonly string [] k_colors = new string [] 22{ 23"\x1B[38;5;196m" , "\x1B[38;5;202m" , "\x1B[38;5;208m" , "\x1B[38;5;214m" , "\x1B[38;5;220m" , 24"\x1B[38;5;226m" , "\x1B[38;5;190m" , "\x1B[38;5;154m" , "\x1B[38;5;118m" , "\x1B[38;5;82m" 25}; 26 27int colorIndex ( in sToken tok ) 28{ 29float p = tok . probability ; 30float p3 = p * p * p ; 31int col = ( int )( p3 * k_colors . Length ); 32col = Math . Clamp ( col , 0 , k_colors . Length - 1 ); 33return col ; 34} 35 36public static string printTime ( TimeSpan ts ) => 37ts . ToString ( "hh':'mm':'ss'.'fff" , CultureInfo . InvariantCulture ); 38public static string printTimeWithComma ( TimeSpan ts ) => 39ts . ToString ( "hh':'mm':'ss','fff" , CultureInfo . InvariantCulture ); 40 41protected override void onNewSegment ( Context sender , int countNew ) 42{ 43TranscribeResult res = sender . results ( resultFlags ); 44ReadOnlySpan < sToken > tokens = res . tokens ; 45 46int s0 = res . segments . Length - countNew ; 47if ( s0 == 0 ) 48Console . WriteLine (); 49 50for ( int i = s0 ; i < res . segments . Length ; i ++ ) 51{ 52sSegment seg = res . segments [ i ]; 53 54if ( args . no_timestamps ) 55{ 56if ( args . print_colors && AnsiCodes . enabled ) 57{ 58foreach ( sToken tok in res . getTokens ( seg ) ) 59{ 60if ( ! args . print_special && tok . hasFlag ( eTokenFlags . Special ) ) 61continue ; 62Console . Write ( "{0}{1}{2}" , k_colors [ colorIndex ( tok ) ], tok . text , "\x1B[0m" ); 63} 64} 65else 66Console . Write ( seg . text ); 67Console . Out . Flush (); 68continue ; 69} 70 71string speaker = "" ; 72if ( args . diarize ) 73{ 74speaker = sender . detectSpeaker ( seg . time ) switch 75{ 76eSpeakerChannel . Unsure => "(speaker ?)" , 77eSpeakerChannel . Left => "(speaker 0)" , 78eSpeakerChannel . Right => "(speaker 1)" , 79 _=> "" 80}; 81} 82 83if ( args . print_colors && AnsiCodes . enabled ) 84{ 85Console . Write ( "[{0} --> {1}] {2} " , printTime ( seg . time . begin ), printTime ( seg . time . end ), speaker ); 86foreach ( sToken tok in res . getTokens ( seg ) ) 87{ 88if ( ! args . print_special && tok . hasFlag ( eTokenFlags . Special ) ) 89continue ; 90Console . Write ( "{0}{1}{2}" , k_colors [ colorIndex ( tok ) ], tok . text , "\x1B[0m" ); 91} 92Console . WriteLine (); 93} 94else 95Console . WriteLine ( "[{0} --> {1}] {2} {3}" , printTime ( seg . time . begin ), printTime ( seg . time . end ), speaker , seg . text ); 96} 97} 98} 99}