okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0b3299a01
main
1/** A browser recognizer adapter. It only emits text; it never submits a message. */ 2export type DictationState = 'starting' | 'listening' | 'speaking' | 'finishing' | 'finished' | 'error' ; 3export interface RecognitionResult { isFinal :boolean ; [ index :number ] :{ transcript :string }; } 4export interface Recognizer { 5continuous :boolean ; interimResults :boolean ; lang :string ; 6onstart :(() => void ) | null ; onspeechstart :(() => void ) | null ; onspeechend :(() => void ) | null ; 7onresult :(( event :{ results :ArrayLike < RecognitionResult >}) => void ) | null ; 8onerror :(( event :{ error :string }) => void ) | null ; onend :(() => void ) | null ; 9start :() => void ; stop :() => void ; abort :() => void ; 10} 11export type RecognizerConstructor = new () => Recognizer ; 12type Clock = { set :( fn :() => void , ms :number ) => unknown ; clear :( id :unknown ) => void }; 13export const DICTATION_SILENCE_MS = 3500 ; 14export const dictationError = ( error :string ) => ({ 15'not-allowed' :'Microphone access was denied. Allow it in your browser, then try again.' , 16'service-not-allowed' :'Speech recognition is unavailable in this browser. Try a browser that supports dictation.' , 17'audio-capture' :'No microphone is available. Check your microphone connection.' , 18'network' :'The browser’s speech service could not connect. Check your connection or try another browser.' , 19'no-speech' :'No speech was detected. Try speaking closer to the microphone.' , 20'language-not-supported' :'Your browser’s speech service does not support this language.' , 21}[ error ] ?? 'Dictation could not start. Try again or use your computer’s built-in dictation.' ); 22 23export function createDictation ( recognizer :Recognizer , options :{ lang :string ; onState :( state :DictationState , error ?:string ) => void ; onText :( text :string ) => void }, clock :Clock = { set :( fn , ms ) => setTimeout ( fn , ms ), clear :id => clearTimeout ( id as ReturnType < typeof setTimeout >)}) { 24let active = false , failed = false , finishing = false , disposed = false ; 25let silence :unknown = null , finishTimer :unknown = null ; 26const clearSilence = () => { if ( silence !== null ) clock . clear ( silence ); silence = null ;}; 27const clearTimers = () => { clearSilence (); if ( finishTimer !== null ) clock . clear ( finishTimer ); finishTimer = null ;}; 28function finished (){ if ( disposed ||! active ) return ; active = false ; clearTimers (); if ( ! failed ) options . onState ( 'finished' );} 29function fail ( error :string ){ if ( disposed ||! active ) return ; failed = true ; clearTimers (); options . onState ( 'error' , dictationError ( error )); try { recognizer . abort ();} catch { /* Already ended. */ } active = false ;} 30function stop (){ 31if ( ! active || finishing || disposed ) return ; finishing = true ; clearSilence (); options . onState ( 'finishing' ); 32finishTimer = clock . set (() => { finished (); try { recognizer . abort ();} catch { /* Already ended. */ }}, 2000 ); 33try { recognizer . stop ();} catch { finished (); try { recognizer . abort ();} catch { /* Already ended. */ }} 34} 35const armSilence = ( ms = DICTATION_SILENCE_MS ) => { clearSilence (); if ( active &&! finishing ) silence = clock . set ( stop , ms );}; 36recognizer . continuous = true ; recognizer . interimResults = true ; recognizer . lang = options . lang ; 37recognizer . onstart = () => { if ( ! active || disposed ) return ; options . onState ( 'listening' ); armSilence ( 10000 );}; 38recognizer . onspeechstart = () => { if ( ! active || finishing || disposed ) return ; clearSilence (); options . onState ( 'speaking' );}; 39recognizer . onspeechend = () => { if ( ! active || finishing || disposed ) return ; options . onState ( 'listening' ); armSilence ();}; 40recognizer . onresult = event => { 41if ( ! active || disposed || failed ) return ; 42// Results contain the whole recognition session; interim entries may be replaced. 43const text = Array . from ( event . results , r => r [ 0 ]?. transcript ?. trim () ?? '' ). filter ( Boolean ). join ( ' ' ); 44options . onText ( text ); if ( ! finishing ){ options . onState ( 'speaking' ); armSilence ();} 45}; 46recognizer . onerror = event => { if ( event . error === 'aborted' && finishing ){ finished (); return ;} fail ( event . error );}; 47recognizer . onend = finished ; 48return { 49start (){ if ( disposed || active ) return ; active = true ; failed = false ; finishing = false ; options . onState ( 'starting' ); armSilence ( 10000 ); try { recognizer . start ();} catch { fail ( 'start-failed' );}}, 50 stop, 51dispose (){ disposed = true ; active = false ; clearTimers (); recognizer . onstart = recognizer . onspeechstart = recognizer . onspeechend = recognizer . onend = null ; recognizer . onresult = recognizer . onerror = null ; try { recognizer . abort ();} catch { /* Already ended. */ }}, 52}; 53}