Skip to content

Instantly share code, notes, and snippets.

@SKaplanOfficial
Created March 19, 2023 02:07
Show Gist options
  • Save SKaplanOfficial/9cead62a19a5f5c345d0c3972e97d69e to your computer and use it in GitHub Desktop.
Save SKaplanOfficial/9cead62a19a5f5c345d0c3972e97d69e to your computer and use it in GitHub Desktop.
AppleScriptObjC script to classify sounds in sound files using the SoundAnalysis framework.
use framework "SoundAnalysis"
set confidenceThreshold to 0.6 -- Level of confidence necessary for classification to appear in result
set theResult to "" -- Sequence of sound classification labels throughout the sound file's duration
-- Analyze sound file for classifiable sounds
on analyzeSound(filePath)
global theResult
-- Initialize sound analyzer with file
set theURL to current application's NSURL's fileURLWithPath:filePath
set theAnalyzer to current application's SNAudioFileAnalyzer's alloc()'s initWithURL:theURL |error|:(missing value)
-- Initial sound classification request and add it to the analyzer
set theRequest to current application's SNClassifySoundRequest's alloc()'s initWithClassifierIdentifier:(current application's SNClassifierIdentifierVersion1) |error|:(missing value)
theAnalyzer's addRequest:(theRequest) withObserver:(me) |error|:(missing value)
-- Start the analysis and wait for it to complete
theAnalyzer's analyze()
repeat while theResult is ""
delay 0.1
end repeat
return theResult
end analyzeSound
-- Act on classification result
on request:request didProduceResult:|result|
global confidenceThreshold
global theResult
-- Add classification labels whose confidence meets the threshold
repeat with classification in |result|'s classifications()
if classification's confidence() > confidenceThreshold then
set theResult to theResult & (classification's identifier() as text) & " "
end if
end repeat
end request:didProduceResult:
-- Set the result if an error occurs to avoid infinite loop
on request:request didFailWithError:|error|
global theResult
if theResult is "" then
set theResult to " "
end if
end request:didFailWithError:
-- Set the result if request completes without classifications being made to avoid infinite loop
on requestDidComplete:request
global theResult
if theResult is "" then
set theResult to " "
end if
end requestDidComplete:
return analyzeSound("/Users/exampleUser/Documents/sound.wav")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment