Platforms to show: All Mac Windows Linux Cross-Platform

/MacFrameworks/SpeechRecognition Test/SpeechRecognition Test


Required plugins for this example: MBS MacFrameworks Plugin, MBS Main Plugin, MBS MacBase Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacFrameworks/SpeechRecognition Test/SpeechRecognition Test

This example is the version from Sat, 21th Apr 2023.

Project "SpeechRecognition Test.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Open() If TargetMacOS And Target64Bit Then If Not SFSpeechRecognizerMBS.available Then MsgBox "Please run with MacOS 10.15 or newer." Quit end if Else MsgBox "Please run as 64-bit MacOS Application." Quit End If End EventHandler
EventHandler Function UnhandledException(error As RuntimeException) As Boolean MsgBox Join(error.Stack, EndOfLine) End EventHandler
End Class
Class MainWindow Inherits Window
Control RequestButton Inherits PushButton
ControlInstance RequestButton Inherits PushButton
EventHandler Sub Action() SFSpeechRecognizerMBS.requestAuthorization AddressOf requestAuthorizationCompletedMBS End EventHandler
End Control
Control AuthStatus Inherits Label
ControlInstance AuthStatus Inherits Label
End Control
Control List Inherits Listbox
ControlInstance List Inherits Listbox
EventHandler Sub ExpandRow(row As Integer) Dim v As Variant = Me.RowTag(row) If v <> Nil Then If v IsA SFTranscriptionMBS Then Dim r As SFTranscriptionMBS = v List.AddRow "averagePauseDuration", Str(r.averagePauseDuration) List.AddRow "formattedString", r.formattedString List.AddRow "speakingRate", Str(r.speakingRate) Dim segments() As SFTranscriptionSegmentMBS = r.segments Dim u As Integer = segments.Ubound For i As Integer = 0 To u Dim segment As SFTranscriptionSegmentMBS = segments(i) List.AddItem "Segment "+Str(i), segment Next End If If v IsA SFTranscriptionSegmentMBS Then Dim r As SFTranscriptionSegmentMBS = v List.AddRow "timestamp", Str(r.timestamp) List.AddRow "duration", Str(r.duration) List.AddRow "substring", r.substring List.AddRow "substringRange", r.substringRange.String List.AddRow "confidence", Str(r.confidence) List.AddRow "alternativeSubstrings", Join(r.alternativeSubstrings, ", ") List.Additem "voiceAnalytics", r.voiceAnalytics End If If v IsA SFVoiceAnalyticsMBS Then Dim r As SFVoiceAnalyticsMBS = v List.AddItem "jitter", r.jitter List.AddItem "pitch", r.pitch List.AddItem "shimmer", r.shimmer List.AddItem "voicing", r.voicing End If If v IsA SFAcousticFeatureMBS Then Dim r As SFAcousticFeatureMBS = v List.AddItem "frameDuration", r.frameDuration Dim values() As String Dim numbers() As Double = r.acousticFeatureValuePerFrame For Each number As Double In numbers values.Append Str(number) next List.AddItem "acousticFeatureValuePerFrame", Join(values, ", ") End If If v IsA SFSpeechRecognitionResultMBS Then Dim r As SFSpeechRecognitionResultMBS = v List.AddRow "final", Str(r.final) List.AddItem "bestTranscription", r.bestTranscription Dim Transcriptions() As SFTranscriptionMBS = r.transcriptions Dim u As Integer = Transcriptions.Ubound For i As Integer = 0 To u Dim Transcription As SFTranscriptionMBS = Transcriptions(i) List.AddItem "Transcription "+Str(i), Transcription Next End If If v IsA SFSpeechRecognitionRequestMBS Then Dim r As SFSpeechRecognitionRequestMBS = v List.AddRow "interactionIdentifier", r.interactionIdentifier List.AddRow "requiresOnDeviceRecognition", Str(r.requiresOnDeviceRecognition) List.AddRow "shouldReportPartialResults", Str(r.shouldReportPartialResults) List.AddRow "contextualStrings", Join(r.contextualStrings, ", ") List.AddRow "taskHint", Str(r.taskHint) If v IsA SFSpeechURLRecognitionRequestMBS Then Dim u As SFSpeechURLRecognitionRequestMBS = v List.AddRow "URL", u.URL End If End If If v IsA NSErrorMBS Then Dim e As NSErrorMBS = v List.AddRow "Description", e.Description List.AddRow "Code", Str(e.Code) List.AddRow "Domain", e.Domain List.AddRow "LocalizedDescription", e.LocalizedDescription List.AddRow "LocalizedFailureReason", e.LocalizedFailureReason List.AddRow "LocalizedRecoverySuggestion", e.LocalizedRecoverySuggestion End If End If End EventHandler
End Control
Control OpenButton Inherits PushButton
ControlInstance OpenButton Inherits PushButton
EventHandler Sub Action() If List.ListIndex >= 0 Then Dim v As Variant = List.RowTag(List.ListIndex) if v isa NSLocaleMBS then Dim locale As NSLocaleMBS = v // use this Localization recognizer = New MySFSpeechRecognizer(locale) Else // use user's default Localization recognizer = New MySFSpeechRecognizer End If Else recognizer = New MySFSpeechRecognizer End If If recognizer.available = False Then MsgBox "Recognizer not available" end if 'dim supportsOnDeviceRecognition as Boolean = recognizer.supportsOnDeviceRecognition recognizer.defaultTaskHint = SFSpeechRecognitionResultMBS.kTaskHintDictation Dim file As FolderItem = GetOpenFolderItem(AudioFileTypes.All) If file = Nil Then Return List.DeleteAllRows request = New SFSpeechURLRecognitionRequestMBS(file) request.shouldReportPartialResults = False request.setContextualStrings Array("Xojo", "MBS") request.taskHint = SFSpeechRecognitionResultMBS.kTaskHintDictation request.requiresOnDeviceRecognition = True // avoid 1 minute limit // log to text file Dim f As FolderItem = SpecialFolder.Desktop.Child("test.txt") If f <> Nil Then stream = TextOutputStream.Create(f) End If // and run Dim task As SFSpeechRecognitionTaskMBS = recognizer.recognitionTaskWithRequest(request, AddressOf recognitionTaskWithRequestCompleted) #Pragma Unused task // now wait for callback End EventHandler
End Control
EventHandler Sub Open() CheckStatus End EventHandler
Sub CheckStatus() Dim status As Integer = SFSpeechRecognizerMBS.authorizationStatus Select Case status Case SFSpeechRecognizerMBS.kAuthorizationStatusAuthorized RequestButton.Enabled = False AuthStatus.Text = "Authorized" list.DeleteAllRows Dim supportedLocales() As NSLocaleMBS = SFSpeechRecognizerMBS.supportedLocales For Each locale As NSLocaleMBS In supportedLocales List.AddRow locale.Identifier, locale.displayName(NSLocaleMBS.NSLocaleIdentifier, locale.Identifier) List.RowTag(List.LastIndex) = locale Next Case SFSpeechRecognizerMBS.kAuthorizationStatusDenied RequestButton.Enabled = False AuthStatus.Text = "Denied" Case SFSpeechRecognizerMBS.kAuthorizationStatusNotDetermined RequestButton.Enabled = True AuthStatus.Text = "Not Determined" Case SFSpeechRecognizerMBS.kAuthorizationStatusRestricted RequestButton.Enabled = False AuthStatus.Text = "Restricted" Else Break End Select End Sub
Sub recognitionTaskWithRequestCompleted(request as SFSpeechRecognitionRequestMBS, result as SFSpeechRecognitionResultMBS, error as NSErrorMBS, tag as Variant) #pragma unused tag // you may get multiple callbacks here If error <> Nil Then MsgBox error.LocalizedDescription End If List.AddItem "request", request List.AddItem "result", result List.AddItem "error", error // log whatever text we got If result <> Nil Then Dim st As SFTranscriptionMBS = result.bestTranscription If st <> Nil Then stream.writeline st.formattedString stream.Flush End If If result.final Then List.AddItem "done", Nil End If End If End Sub
Sub requestAuthorizationCompletedMBS(status as integer, tag as Variant) #Pragma Unused status #Pragma Unused tag CheckStatus End Sub
Property recognizer As MySFSpeechRecognizer
Property request As SFSpeechURLRecognitionRequestMBS
Property stream As TextOutputStream
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
ExternalFile Info
End ExternalFile
AudioFileTypes
Filetype audio/mp3
Filetype video/mpeg
Filetype video/quicktime
Filetype audio/m4a
Filetype audio/aiff
Filetype audio/aac
Filetype audio/x-wav
Filetype video/mp4
End AudioFileTypes
Class MySFSpeechRecognizer Inherits SFSpeechRecognizerMBS
End Class
Module ListUtilModule
Sub AddItem(extends list as listbox, name as string, item as Variant) If item <> Nil Then list.AddFolder name list.RowTag(list.LastIndex) = item Else list.AddRow name, "n/a" End If End Sub
End Module
End Project

The items on this page are in the following plugins: MBS MacFrameworks Plugin.


The biggest plugin in space...