Created
April 25, 2023 20:42
-
-
Save SKaplanOfficial/e9087061af9420bed7df585f00508270 to your computer and use it in GitHub Desktop.
JXA script to extract spoken word content from an audio file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(() => { | |
ObjC.import('Foundation'); | |
ObjC.import('Speech'); | |
var app = Application.currentApplication(); | |
if (!$['SFSpeechRecognitionTaskDelegate']) { | |
ObjC.registerSubclass({ | |
name: 'SFSpeechRecognitionTaskDelegate', | |
superclass: 'NSObject', | |
protocols: ['SFSpeechRecognitionTaskDelegate'], | |
properties: { | |
spokenContent: 'id' | |
}, | |
methods: { | |
'speechRecognitionTask:didFinishRecognition:': { | |
types: ['void', ['id', 'id']], | |
implementation: function(task, result) { | |
this.spokenContent = result.bestTranscription.formattedString.js | |
}, | |
} | |
} | |
}) | |
} | |
const fileURL = $.NSURL.fileURLWithPath('/Users/exampleUser/Documents/Audio 1.wav'); | |
const locale = $.NSLocale.localeWithLocaleIdentifier('en-US'); | |
const recognizer = $.SFSpeechRecognizer.alloc.initWithLocale(locale); | |
const request = $.SFSpeechURLRecognitionRequest.alloc.initWithURL(fileURL); | |
const delegate = $.SFSpeechRecognitionTaskDelegate.alloc.init | |
recognizer.recognitionTaskWithRequestDelegate(request, delegate); | |
while (delegate.spokenContent.js == undefined) { | |
delay(0.1) | |
} | |
return delegate.spokenContent.js | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment